mpegvideo_parser.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * MPEG1 / MPEG2 video parser
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  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. static void mpegvideo_extract_headers(AVCodecParserContext *s,
  25. AVCodecContext *avctx,
  26. const uint8_t *buf, int buf_size)
  27. {
  28. ParseContext1 *pc = s->priv_data;
  29. const uint8_t *buf_end = buf + buf_size;
  30. uint32_t start_code;
  31. int frame_rate_index, ext_type, bytes_left;
  32. int frame_rate_ext_n, frame_rate_ext_d;
  33. int picture_structure, top_field_first, repeat_first_field, progressive_frame;
  34. int horiz_size_ext, vert_size_ext, bit_rate_ext;
  35. int did_set_size=0;
  36. //FIXME replace the crap with get_bits()
  37. s->repeat_pict = 0;
  38. while (buf < buf_end) {
  39. start_code= -1;
  40. buf= ff_find_start_code(buf, buf_end, &start_code);
  41. bytes_left = buf_end - buf;
  42. switch(start_code) {
  43. case PICTURE_START_CODE:
  44. if (bytes_left >= 2) {
  45. s->pict_type = (buf[1] >> 3) & 7;
  46. }
  47. break;
  48. case SEQ_START_CODE:
  49. if (bytes_left >= 7) {
  50. pc->width = (buf[0] << 4) | (buf[1] >> 4);
  51. pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
  52. if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
  53. avcodec_set_dimensions(avctx, pc->width, pc->height);
  54. did_set_size=1;
  55. }
  56. frame_rate_index = buf[3] & 0xf;
  57. pc->frame_rate.den = avctx->time_base.den = ff_frame_rate_tab[frame_rate_index].num;
  58. pc->frame_rate.num = avctx->time_base.num = ff_frame_rate_tab[frame_rate_index].den;
  59. avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
  60. avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  61. avctx->sub_id = 1;
  62. }
  63. break;
  64. case EXT_START_CODE:
  65. if (bytes_left >= 1) {
  66. ext_type = (buf[0] >> 4);
  67. switch(ext_type) {
  68. case 0x1: /* sequence extension */
  69. if (bytes_left >= 6) {
  70. horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
  71. vert_size_ext = (buf[2] >> 5) & 3;
  72. bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
  73. frame_rate_ext_n = (buf[5] >> 5) & 3;
  74. frame_rate_ext_d = (buf[5] & 0x1f);
  75. pc->progressive_sequence = buf[1] & (1 << 3);
  76. avctx->has_b_frames= !(buf[5] >> 7);
  77. pc->width |=(horiz_size_ext << 12);
  78. pc->height |=( vert_size_ext << 12);
  79. avctx->bit_rate += (bit_rate_ext << 18) * 400;
  80. if(did_set_size)
  81. avcodec_set_dimensions(avctx, pc->width, pc->height);
  82. avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1) * 2;
  83. avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
  84. avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  85. avctx->sub_id = 2; /* forces MPEG2 */
  86. }
  87. break;
  88. case 0x8: /* picture coding extension */
  89. if (bytes_left >= 5) {
  90. picture_structure = buf[2]&3;
  91. top_field_first = buf[3] & (1 << 7);
  92. repeat_first_field = buf[3] & (1 << 1);
  93. progressive_frame = buf[4] & (1 << 7);
  94. /* check if we must repeat the frame */
  95. s->repeat_pict = 1;
  96. if (repeat_first_field) {
  97. if (pc->progressive_sequence) {
  98. if (top_field_first)
  99. s->repeat_pict = 5;
  100. else
  101. s->repeat_pict = 3;
  102. } else if (progressive_frame) {
  103. s->repeat_pict = 2;
  104. }
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. break;
  111. case -1:
  112. goto the_end;
  113. default:
  114. /* we stop parsing when we encounter a slice. It ensures
  115. that this function takes a negligible amount of time */
  116. if (start_code >= SLICE_MIN_START_CODE &&
  117. start_code <= SLICE_MAX_START_CODE)
  118. goto the_end;
  119. break;
  120. }
  121. }
  122. the_end: ;
  123. }
  124. static int mpegvideo_parse(AVCodecParserContext *s,
  125. AVCodecContext *avctx,
  126. const uint8_t **poutbuf, int *poutbuf_size,
  127. const uint8_t *buf, int buf_size)
  128. {
  129. ParseContext1 *pc1 = s->priv_data;
  130. ParseContext *pc= &pc1->pc;
  131. int next;
  132. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  133. next= buf_size;
  134. }else{
  135. next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s);
  136. if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  137. *poutbuf = NULL;
  138. *poutbuf_size = 0;
  139. return buf_size;
  140. }
  141. }
  142. /* we have a full frame : we just parse the first few MPEG headers
  143. to have the full timing information. The time take by this
  144. function should be negligible for uncorrupted streams */
  145. mpegvideo_extract_headers(s, avctx, buf, buf_size);
  146. #if 0
  147. printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
  148. s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
  149. #endif
  150. *poutbuf = buf;
  151. *poutbuf_size = buf_size;
  152. return next;
  153. }
  154. static int mpegvideo_split(AVCodecContext *avctx,
  155. const uint8_t *buf, int buf_size)
  156. {
  157. int i;
  158. uint32_t state= -1;
  159. for(i=0; i<buf_size; i++){
  160. state= (state<<8) | buf[i];
  161. if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
  162. return i-3;
  163. }
  164. return 0;
  165. }
  166. AVCodecParser ff_mpegvideo_parser = {
  167. { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
  168. sizeof(ParseContext1),
  169. NULL,
  170. mpegvideo_parse,
  171. ff_parse1_close,
  172. mpegvideo_split,
  173. };