mpeg4video_parser.c 3.7 KB

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