rv34_parser.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * RV30/40 parser
  3. * Copyright (c) 2011 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RV30/40 parser
  24. */
  25. #include "parser.h"
  26. #include "libavutil/intreadwrite.h"
  27. typedef struct {
  28. ParseContext pc;
  29. int64_t key_dts;
  30. int key_pts;
  31. } RV34ParseContext;
  32. static const int rv_to_av_frame_type[4] = {
  33. AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B,
  34. };
  35. static int rv34_parse(AVCodecParserContext *s,
  36. AVCodecContext *avctx,
  37. const uint8_t **poutbuf, int *poutbuf_size,
  38. const uint8_t *buf, int buf_size)
  39. {
  40. RV34ParseContext *pc = s->priv_data;
  41. int type, pts, hdr;
  42. if (buf_size < 13 + *buf * 8) {
  43. *poutbuf = buf;
  44. *poutbuf_size = buf_size;
  45. return buf_size;
  46. }
  47. hdr = AV_RB32(buf + 9 + *buf * 8);
  48. if (avctx->codec_id == CODEC_ID_RV30) {
  49. type = (hdr >> 27) & 3;
  50. pts = (hdr >> 7) & 0x1FFF;
  51. } else {
  52. type = (hdr >> 29) & 3;
  53. pts = (hdr >> 6) & 0x1FFF;
  54. }
  55. if (type != 3 && s->pts != AV_NOPTS_VALUE) {
  56. pc->key_dts = s->pts;
  57. pc->key_pts = pts;
  58. } else {
  59. if (type != 3)
  60. s->pts = pc->key_dts + ((pts - pc->key_pts) & 0x1FFF);
  61. else
  62. s->pts = pc->key_dts - ((pc->key_pts - pts) & 0x1FFF);
  63. }
  64. s->pict_type = rv_to_av_frame_type[type];
  65. *poutbuf = buf;
  66. *poutbuf_size = buf_size;
  67. return buf_size;
  68. }
  69. #if CONFIG_RV30_PARSER
  70. AVCodecParser ff_rv30_parser = {
  71. .codec_ids = { CODEC_ID_RV30 },
  72. .priv_data_size = sizeof(RV34ParseContext),
  73. .parser_parse = rv34_parse,
  74. };
  75. #endif
  76. #if CONFIG_RV40_PARSER
  77. AVCodecParser ff_rv40_parser = {
  78. .codec_ids = { CODEC_ID_RV40 },
  79. .priv_data_size = sizeof(RV34ParseContext),
  80. .parser_parse = rv34_parse,
  81. };
  82. #endif