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.

209 lines
6.0KB

  1. /*
  2. * VC-1 and WMV3 parser
  3. * Copyright (c) 2006-2007 Konstantin Shishkov
  4. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, 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. /**
  23. * @file
  24. * VC-1 and WMV3 parser
  25. */
  26. #include "libavutil/attributes.h"
  27. #include "parser.h"
  28. #include "vc1.h"
  29. #include "get_bits.h"
  30. typedef struct {
  31. ParseContext pc;
  32. VC1Context v;
  33. } VC1ParseContext;
  34. static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
  35. const uint8_t *buf, int buf_size)
  36. {
  37. VC1ParseContext *vpc = s->priv_data;
  38. GetBitContext gb;
  39. const uint8_t *start, *end, *next;
  40. uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  41. vpc->v.s.avctx = avctx;
  42. vpc->v.parse_only = 1;
  43. vpc->v.first_pic_header_flag = 1;
  44. next = buf;
  45. s->repeat_pict = 0;
  46. for(start = buf, end = buf + buf_size; next < end; start = next){
  47. int buf2_size, size;
  48. next = find_next_marker(start + 4, end);
  49. size = next - start - 4;
  50. buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
  51. init_get_bits(&gb, buf2, buf2_size * 8);
  52. if(size <= 0) continue;
  53. switch(AV_RB32(start)){
  54. case VC1_CODE_SEQHDR:
  55. ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
  56. break;
  57. case VC1_CODE_ENTRYPOINT:
  58. ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
  59. break;
  60. case VC1_CODE_FRAME:
  61. if(vpc->v.profile < PROFILE_ADVANCED)
  62. ff_vc1_parse_frame_header (&vpc->v, &gb);
  63. else
  64. ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
  65. /* keep AV_PICTURE_TYPE_BI internal to VC1 */
  66. if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
  67. s->pict_type = AV_PICTURE_TYPE_B;
  68. else
  69. s->pict_type = vpc->v.s.pict_type;
  70. if (avctx->ticks_per_frame > 1){
  71. // process pulldown flags
  72. s->repeat_pict = 1;
  73. // Pulldown flags are only valid when 'broadcast' has been set.
  74. // So ticks_per_frame will be 2
  75. if (vpc->v.rff){
  76. // repeat field
  77. s->repeat_pict = 2;
  78. }else if (vpc->v.rptfrm){
  79. // repeat frames
  80. s->repeat_pict = vpc->v.rptfrm * 2 + 1;
  81. }
  82. }
  83. if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
  84. s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
  85. else
  86. s->field_order = AV_FIELD_PROGRESSIVE;
  87. break;
  88. }
  89. }
  90. av_free(buf2);
  91. }
  92. /**
  93. * Find the end of the current frame in the bitstream.
  94. * @return the position of the first byte of the next frame, or -1
  95. */
  96. static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
  97. int buf_size) {
  98. int pic_found, i;
  99. uint32_t state;
  100. pic_found= pc->frame_start_found;
  101. state= pc->state;
  102. i=0;
  103. if(!pic_found){
  104. for(i=0; i<buf_size; i++){
  105. state= (state<<8) | buf[i];
  106. if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
  107. i++;
  108. pic_found=1;
  109. break;
  110. }
  111. }
  112. }
  113. if(pic_found){
  114. /* EOF considered as end of frame */
  115. if (buf_size == 0)
  116. return 0;
  117. for(; i<buf_size; i++){
  118. state= (state<<8) | buf[i];
  119. if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
  120. pc->frame_start_found=0;
  121. pc->state=-1;
  122. return i-3;
  123. }
  124. }
  125. }
  126. pc->frame_start_found= pic_found;
  127. pc->state= state;
  128. return END_NOT_FOUND;
  129. }
  130. static int vc1_parse(AVCodecParserContext *s,
  131. AVCodecContext *avctx,
  132. const uint8_t **poutbuf, int *poutbuf_size,
  133. const uint8_t *buf, int buf_size)
  134. {
  135. VC1ParseContext *vpc = s->priv_data;
  136. int next;
  137. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  138. next= buf_size;
  139. }else{
  140. next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
  141. if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
  142. *poutbuf = NULL;
  143. *poutbuf_size = 0;
  144. return buf_size;
  145. }
  146. }
  147. vc1_extract_headers(s, avctx, buf, buf_size);
  148. *poutbuf = buf;
  149. *poutbuf_size = buf_size;
  150. return next;
  151. }
  152. static int vc1_split(AVCodecContext *avctx,
  153. const uint8_t *buf, int buf_size)
  154. {
  155. int i;
  156. uint32_t state= -1;
  157. int charged=0;
  158. for(i=0; i<buf_size; i++){
  159. state= (state<<8) | buf[i];
  160. if(IS_MARKER(state)){
  161. if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
  162. charged=1;
  163. }else if(charged){
  164. return i-3;
  165. }
  166. }
  167. }
  168. return 0;
  169. }
  170. static av_cold int vc1_parse_init(AVCodecParserContext *s)
  171. {
  172. VC1ParseContext *vpc = s->priv_data;
  173. vpc->v.s.slice_context_count = 1;
  174. return ff_vc1_init_common(&vpc->v);
  175. }
  176. AVCodecParser ff_vc1_parser = {
  177. .codec_ids = { AV_CODEC_ID_VC1 },
  178. .priv_data_size = sizeof(VC1ParseContext),
  179. .parser_init = vc1_parse_init,
  180. .parser_parse = vc1_parse,
  181. .parser_close = ff_parse_close,
  182. .split = vc1_split,
  183. };