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.

153 lines
4.4KB

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