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.

180 lines
4.9KB

  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 libavcodec/vc1_parser.c
  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. for(start = buf, end = buf + buf_size; next < end; start = next){
  44. int buf2_size, size;
  45. next = find_next_marker(start + 4, end);
  46. size = next - start - 4;
  47. buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
  48. init_get_bits(&gb, buf2, buf2_size * 8);
  49. if(size <= 0) continue;
  50. switch(AV_RB32(start)){
  51. case VC1_CODE_SEQHDR:
  52. vc1_decode_sequence_header(avctx, &vpc->v, &gb);
  53. break;
  54. case VC1_CODE_ENTRYPOINT:
  55. vc1_decode_entry_point(avctx, &vpc->v, &gb);
  56. break;
  57. case VC1_CODE_FRAME:
  58. if(vpc->v.profile < PROFILE_ADVANCED)
  59. vc1_parse_frame_header (&vpc->v, &gb);
  60. else
  61. vc1_parse_frame_header_adv(&vpc->v, &gb);
  62. /* keep FF_BI_TYPE internal to VC1 */
  63. if (vpc->v.s.pict_type == FF_BI_TYPE)
  64. s->pict_type = FF_B_TYPE;
  65. else
  66. s->pict_type = vpc->v.s.pict_type;
  67. break;
  68. }
  69. }
  70. av_free(buf2);
  71. }
  72. /**
  73. * finds the end of the current frame in the bitstream.
  74. * @return the position of the first byte of the next frame, or -1
  75. */
  76. static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
  77. int buf_size) {
  78. int pic_found, i;
  79. uint32_t state;
  80. pic_found= pc->frame_start_found;
  81. state= pc->state;
  82. i=0;
  83. if(!pic_found){
  84. for(i=0; i<buf_size; i++){
  85. state= (state<<8) | buf[i];
  86. if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
  87. i++;
  88. pic_found=1;
  89. break;
  90. }
  91. }
  92. }
  93. if(pic_found){
  94. /* EOF considered as end of frame */
  95. if (buf_size == 0)
  96. return 0;
  97. for(; i<buf_size; i++){
  98. state= (state<<8) | buf[i];
  99. if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
  100. pc->frame_start_found=0;
  101. pc->state=-1;
  102. return i-3;
  103. }
  104. }
  105. }
  106. pc->frame_start_found= pic_found;
  107. pc->state= state;
  108. return END_NOT_FOUND;
  109. }
  110. static int vc1_parse(AVCodecParserContext *s,
  111. AVCodecContext *avctx,
  112. const uint8_t **poutbuf, int *poutbuf_size,
  113. const uint8_t *buf, int buf_size)
  114. {
  115. VC1ParseContext *vpc = s->priv_data;
  116. int next;
  117. if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
  118. next= buf_size;
  119. }else{
  120. next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
  121. if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
  122. *poutbuf = NULL;
  123. *poutbuf_size = 0;
  124. return buf_size;
  125. }
  126. }
  127. vc1_extract_headers(s, avctx, buf, buf_size);
  128. *poutbuf = buf;
  129. *poutbuf_size = buf_size;
  130. return next;
  131. }
  132. static int vc1_split(AVCodecContext *avctx,
  133. const uint8_t *buf, int buf_size)
  134. {
  135. int i;
  136. uint32_t state= -1;
  137. int charged=0;
  138. for(i=0; i<buf_size; i++){
  139. state= (state<<8) | buf[i];
  140. if(IS_MARKER(state)){
  141. if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
  142. charged=1;
  143. }else if(charged){
  144. return i-3;
  145. }
  146. }
  147. }
  148. return 0;
  149. }
  150. AVCodecParser vc1_parser = {
  151. { CODEC_ID_VC1 },
  152. sizeof(VC1ParseContext),
  153. NULL,
  154. vc1_parse,
  155. ff_parse1_close,
  156. vc1_split,
  157. };