You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.9KB

  1. /*
  2. * MPEG-4 video frame extraction
  3. * Copyright (c) 2003 Fabrice Bellard
  4. * Copyright (c) 2003 Michael Niedermayer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #define UNCHECKED_BITSTREAM_READER 1
  23. #include "internal.h"
  24. #include "parser.h"
  25. #include "mpegvideo.h"
  26. #include "mpeg4video.h"
  27. #include "mpeg4video_parser.h"
  28. struct Mp4vParseContext {
  29. ParseContext pc;
  30. Mpeg4DecContext dec_ctx;
  31. int first_picture;
  32. };
  33. int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  34. {
  35. int vop_found, i;
  36. uint32_t state;
  37. vop_found = pc->frame_start_found;
  38. state = pc->state;
  39. i = 0;
  40. if (!vop_found) {
  41. for (i = 0; i < buf_size; i++) {
  42. state = (state << 8) | buf[i];
  43. if (state == VOP_STARTCODE) {
  44. i++;
  45. vop_found = 1;
  46. break;
  47. }
  48. }
  49. }
  50. if (vop_found) {
  51. /* EOF considered as end of frame */
  52. if (buf_size == 0)
  53. return 0;
  54. for (; i < buf_size; i++) {
  55. state = (state << 8) | buf[i];
  56. if ((state & 0xFFFFFF00) == 0x100) {
  57. if (state == SLICE_STARTCODE || state == EXT_STARTCODE)
  58. continue;
  59. pc->frame_start_found = 0;
  60. pc->state = -1;
  61. return i - 3;
  62. }
  63. }
  64. }
  65. pc->frame_start_found = vop_found;
  66. pc->state = state;
  67. return END_NOT_FOUND;
  68. }
  69. /* XXX: make it use less memory */
  70. static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx,
  71. const uint8_t *buf, int buf_size)
  72. {
  73. struct Mp4vParseContext *pc = s1->priv_data;
  74. Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
  75. MpegEncContext *s = &dec_ctx->m;
  76. GetBitContext gb1, *gb = &gb1;
  77. int ret;
  78. s->avctx = avctx;
  79. s->current_picture_ptr = &s->current_picture;
  80. if (avctx->extradata_size && pc->first_picture) {
  81. init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
  82. ret = ff_mpeg4_decode_picture_header(dec_ctx, gb, 1);
  83. if (ret < 0)
  84. av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
  85. }
  86. init_get_bits(gb, buf, 8 * buf_size);
  87. ret = ff_mpeg4_decode_picture_header(dec_ctx, gb, 0);
  88. if (s->width && (!avctx->width || !avctx->height ||
  89. !avctx->coded_width || !avctx->coded_height)) {
  90. ret = ff_set_dimensions(avctx, s->width, s->height);
  91. if (ret < 0)
  92. return ret;
  93. }
  94. if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
  95. av_assert1(s1->pts == AV_NOPTS_VALUE);
  96. av_assert1(s1->dts == AV_NOPTS_VALUE);
  97. s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
  98. }
  99. s1->pict_type = s->pict_type;
  100. pc->first_picture = 0;
  101. return ret;
  102. }
  103. static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
  104. {
  105. struct Mp4vParseContext *pc = s->priv_data;
  106. ff_mpeg4videodec_static_init();
  107. pc->first_picture = 1;
  108. pc->dec_ctx.m.quant_precision = 5;
  109. pc->dec_ctx.m.slice_context_count = 1;
  110. pc->dec_ctx.showed_packed_warning = 1;
  111. return 0;
  112. }
  113. static int mpeg4video_parse(AVCodecParserContext *s,
  114. AVCodecContext *avctx,
  115. const uint8_t **poutbuf, int *poutbuf_size,
  116. const uint8_t *buf, int buf_size)
  117. {
  118. ParseContext *pc = s->priv_data;
  119. int next;
  120. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  121. next = buf_size;
  122. } else {
  123. next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
  124. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  125. *poutbuf = NULL;
  126. *poutbuf_size = 0;
  127. return buf_size;
  128. }
  129. }
  130. mpeg4_decode_header(s, avctx, buf, buf_size);
  131. *poutbuf = buf;
  132. *poutbuf_size = buf_size;
  133. return next;
  134. }
  135. AVCodecParser ff_mpeg4video_parser = {
  136. .codec_ids = { AV_CODEC_ID_MPEG4 },
  137. .priv_data_size = sizeof(struct Mp4vParseContext),
  138. .parser_init = mpeg4video_parse_init,
  139. .parser_parse = mpeg4video_parse,
  140. .parser_close = ff_parse_close,
  141. .split = ff_mpeg4video_split,
  142. };