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.

143 lines
4.0KB

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