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.

140 lines
3.8KB

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