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.

160 lines
4.7KB

  1. /*
  2. * MPEG4 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. struct MpegEncContext enc;
  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 == 0x1B6) {
  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. pc->frame_start_found = 0;
  58. pc->state = -1;
  59. return i - 3;
  60. }
  61. }
  62. }
  63. pc->frame_start_found = vop_found;
  64. pc->state = state;
  65. return END_NOT_FOUND;
  66. }
  67. /* XXX: make it use less memory */
  68. static int av_mpeg4_decode_header(AVCodecParserContext *s1,
  69. AVCodecContext *avctx,
  70. const uint8_t *buf, int buf_size)
  71. {
  72. struct Mp4vParseContext *pc = s1->priv_data;
  73. MpegEncContext *s = &pc->enc;
  74. GetBitContext gb1, *gb = &gb1;
  75. int ret;
  76. s->avctx = avctx;
  77. s->current_picture_ptr = &s->current_picture;
  78. if (avctx->extradata_size && pc->first_picture) {
  79. init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
  80. ret = ff_mpeg4_decode_picture_header(s, gb);
  81. }
  82. init_get_bits(gb, buf, 8 * buf_size);
  83. ret = ff_mpeg4_decode_picture_header(s, gb);
  84. if (s->width && (!avctx->width || !avctx->height ||
  85. !avctx->coded_width || !avctx->coded_height)) {
  86. ret = ff_set_dimensions(avctx, s->width, s->height);
  87. if (ret < 0)
  88. return ret;
  89. }
  90. if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
  91. av_assert1(s1->pts == AV_NOPTS_VALUE);
  92. av_assert1(s1->dts == AV_NOPTS_VALUE);
  93. s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
  94. }
  95. s1->pict_type = s->pict_type;
  96. pc->first_picture = 0;
  97. return ret;
  98. }
  99. static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
  100. {
  101. struct Mp4vParseContext *pc = s->priv_data;
  102. ff_mpeg4videodec_static_init();
  103. pc->first_picture = 1;
  104. pc->enc.quant_precision = 5;
  105. pc->enc.slice_context_count = 1;
  106. pc->enc.showed_packed_warning = 1;
  107. return 0;
  108. }
  109. static int mpeg4video_parse(AVCodecParserContext *s,
  110. AVCodecContext *avctx,
  111. const uint8_t **poutbuf, int *poutbuf_size,
  112. const uint8_t *buf, int buf_size)
  113. {
  114. ParseContext *pc = s->priv_data;
  115. int next;
  116. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  117. next = buf_size;
  118. } else {
  119. next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
  120. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  121. *poutbuf = NULL;
  122. *poutbuf_size = 0;
  123. return buf_size;
  124. }
  125. }
  126. av_mpeg4_decode_header(s, avctx, buf, buf_size);
  127. *poutbuf = buf;
  128. *poutbuf_size = buf_size;
  129. return next;
  130. }
  131. AVCodecParser ff_mpeg4video_parser = {
  132. .codec_ids = { AV_CODEC_ID_MPEG4 },
  133. .priv_data_size = sizeof(struct Mp4vParseContext),
  134. .parser_init = mpeg4video_parse_init,
  135. .parser_parse = mpeg4video_parse,
  136. .parser_close = ff_parse_close,
  137. .split = ff_mpeg4video_split,
  138. };