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.

147 lines
4.3KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "internal.h"
  23. #include "parser.h"
  24. #include "mpegvideo.h"
  25. #include "mpeg4video.h"
  26. #include "mpeg4video_parser.h"
  27. struct Mp4vParseContext {
  28. ParseContext pc;
  29. Mpeg4DecContext dec_ctx;
  30. int first_picture;
  31. };
  32. int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  33. {
  34. int vop_found, i;
  35. uint32_t state;
  36. vop_found = pc->frame_start_found;
  37. state = pc->state;
  38. i = 0;
  39. if (!vop_found) {
  40. for (i = 0; i < buf_size; i++) {
  41. state = (state << 8) | buf[i];
  42. if (state == 0x1B6) {
  43. i++;
  44. vop_found = 1;
  45. break;
  46. }
  47. }
  48. }
  49. if (vop_found) {
  50. /* EOF considered as end of frame */
  51. if (buf_size == 0)
  52. return 0;
  53. for (; i < buf_size; i++) {
  54. state = (state << 8) | buf[i];
  55. if ((state & 0xFFFFFF00) == 0x100) {
  56. pc->frame_start_found = 0;
  57. pc->state = -1;
  58. return i - 3;
  59. }
  60. }
  61. }
  62. pc->frame_start_found = vop_found;
  63. pc->state = state;
  64. return END_NOT_FOUND;
  65. }
  66. /* XXX: make it use less memory */
  67. static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx,
  68. const uint8_t *buf, int buf_size)
  69. {
  70. struct Mp4vParseContext *pc = s1->priv_data;
  71. Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
  72. MpegEncContext *s = &dec_ctx->m;
  73. GetBitContext gb1, *gb = &gb1;
  74. int ret;
  75. s->avctx = avctx;
  76. s->current_picture_ptr = &s->current_picture;
  77. if (avctx->extradata_size && pc->first_picture) {
  78. init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
  79. ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
  80. }
  81. init_get_bits(gb, buf, 8 * buf_size);
  82. ret = ff_mpeg4_decode_picture_header(dec_ctx, gb);
  83. if (s->width && (!avctx->width || !avctx->height ||
  84. !avctx->coded_width || !avctx->coded_height)) {
  85. ret = ff_set_dimensions(avctx, s->width, s->height);
  86. if (ret < 0)
  87. return ret;
  88. }
  89. s1->pict_type = s->pict_type;
  90. pc->first_picture = 0;
  91. return ret;
  92. }
  93. static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
  94. {
  95. struct Mp4vParseContext *pc = s->priv_data;
  96. pc->first_picture = 1;
  97. pc->dec_ctx.m.slice_context_count = 1;
  98. return 0;
  99. }
  100. static int mpeg4video_parse(AVCodecParserContext *s,
  101. AVCodecContext *avctx,
  102. const uint8_t **poutbuf, int *poutbuf_size,
  103. const uint8_t *buf, int buf_size)
  104. {
  105. ParseContext *pc = s->priv_data;
  106. int next;
  107. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  108. next = buf_size;
  109. } else {
  110. next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
  111. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  112. *poutbuf = NULL;
  113. *poutbuf_size = 0;
  114. return buf_size;
  115. }
  116. }
  117. mpeg4_decode_header(s, avctx, buf, buf_size);
  118. *poutbuf = buf;
  119. *poutbuf_size = buf_size;
  120. return next;
  121. }
  122. AVCodecParser ff_mpeg4video_parser = {
  123. .codec_ids = { AV_CODEC_ID_MPEG4 },
  124. .priv_data_size = sizeof(struct Mp4vParseContext),
  125. .parser_init = mpeg4video_parse_init,
  126. .parser_parse = mpeg4video_parse,
  127. .parser_close = ff_parse_close,
  128. .split = ff_mpeg4video_split,
  129. };