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.

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