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.

202 lines
5.8KB

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