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.

282 lines
9.4KB

  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. /** The maximum number of bytes of a sequence, entry point or
  31. * frame header whose values we pay any attention to */
  32. #define UNESCAPED_THRESHOLD 37
  33. /** The maximum number of bytes of a sequence, entry point or
  34. * frame header which must be valid memory (because they are
  35. * used to update the bitstream cache in skip_bits() calls)
  36. */
  37. #define UNESCAPED_LIMIT 144
  38. typedef enum {
  39. NO_MATCH,
  40. ONE_ZERO,
  41. TWO_ZEROS,
  42. ONE
  43. } VC1ParseSearchState;
  44. typedef struct {
  45. ParseContext pc;
  46. VC1Context v;
  47. uint8_t prev_start_code;
  48. size_t bytes_to_skip;
  49. uint8_t unesc_buffer[UNESCAPED_LIMIT];
  50. size_t unesc_index;
  51. VC1ParseSearchState search_state;
  52. } VC1ParseContext;
  53. static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx,
  54. const uint8_t *buf, int buf_size)
  55. {
  56. /* Parse the header we just finished unescaping */
  57. VC1ParseContext *vpc = s->priv_data;
  58. GetBitContext gb;
  59. vpc->v.s.avctx = avctx;
  60. vpc->v.parse_only = 1;
  61. init_get_bits(&gb, buf, buf_size * 8);
  62. switch (vpc->prev_start_code) {
  63. case VC1_CODE_SEQHDR & 0xFF:
  64. ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
  65. break;
  66. case VC1_CODE_ENTRYPOINT & 0xFF:
  67. ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
  68. break;
  69. case VC1_CODE_FRAME & 0xFF:
  70. if(vpc->v.profile < PROFILE_ADVANCED)
  71. ff_vc1_parse_frame_header (&vpc->v, &gb);
  72. else
  73. ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
  74. /* keep AV_PICTURE_TYPE_BI internal to VC1 */
  75. if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
  76. s->pict_type = AV_PICTURE_TYPE_B;
  77. else
  78. s->pict_type = vpc->v.s.pict_type;
  79. if (avctx->ticks_per_frame > 1){
  80. // process pulldown flags
  81. s->repeat_pict = 1;
  82. // Pulldown flags are only valid when 'broadcast' has been set.
  83. // So ticks_per_frame will be 2
  84. if (vpc->v.rff){
  85. // repeat field
  86. s->repeat_pict = 2;
  87. }else if (vpc->v.rptfrm){
  88. // repeat frames
  89. s->repeat_pict = vpc->v.rptfrm * 2 + 1;
  90. }
  91. }else{
  92. s->repeat_pict = 0;
  93. }
  94. if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
  95. s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
  96. else
  97. s->field_order = AV_FIELD_PROGRESSIVE;
  98. break;
  99. }
  100. }
  101. static int vc1_parse(AVCodecParserContext *s,
  102. AVCodecContext *avctx,
  103. const uint8_t **poutbuf, int *poutbuf_size,
  104. const uint8_t *buf, int buf_size)
  105. {
  106. /* Here we do the searching for frame boundaries and headers at
  107. * the same time. Only a minimal amount at the start of each
  108. * header is unescaped. */
  109. VC1ParseContext *vpc = s->priv_data;
  110. int pic_found = vpc->pc.frame_start_found;
  111. uint8_t *unesc_buffer = vpc->unesc_buffer;
  112. size_t unesc_index = vpc->unesc_index;
  113. VC1ParseSearchState search_state = vpc->search_state;
  114. int next = END_NOT_FOUND;
  115. int i = vpc->bytes_to_skip;
  116. if (pic_found && buf_size == 0) {
  117. /* EOF considered as end of frame */
  118. memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
  119. vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  120. next = 0;
  121. }
  122. while (i < buf_size) {
  123. int start_code_found = 0;
  124. uint8_t b;
  125. while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
  126. b = buf[i++];
  127. unesc_buffer[unesc_index++] = b;
  128. if (search_state <= ONE_ZERO)
  129. search_state = b ? NO_MATCH : search_state + 1;
  130. else if (search_state == TWO_ZEROS) {
  131. if (b == 1)
  132. search_state = ONE;
  133. else if (b > 1) {
  134. if (b == 3)
  135. unesc_index--; // swallow emulation prevention byte
  136. search_state = NO_MATCH;
  137. }
  138. }
  139. else { // search_state == ONE
  140. // Header unescaping terminates early due to detection of next start code
  141. search_state = NO_MATCH;
  142. start_code_found = 1;
  143. break;
  144. }
  145. }
  146. if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
  147. unesc_index >= UNESCAPED_THRESHOLD &&
  148. vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
  149. {
  150. // No need to keep scanning the rest of the buffer for
  151. // start codes if we know it contains a complete frame and
  152. // we've already unescaped all we need of the frame header
  153. vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  154. break;
  155. }
  156. if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
  157. while (i < buf_size) {
  158. if (search_state == NO_MATCH) {
  159. i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
  160. if (i < buf_size) {
  161. search_state = ONE_ZERO;
  162. }
  163. i++;
  164. } else {
  165. b = buf[i++];
  166. if (search_state == ONE_ZERO)
  167. search_state = b ? NO_MATCH : TWO_ZEROS;
  168. else if (search_state == TWO_ZEROS) {
  169. if (b >= 1)
  170. search_state = b == 1 ? ONE : NO_MATCH;
  171. }
  172. else { // search_state == ONE
  173. search_state = NO_MATCH;
  174. start_code_found = 1;
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. if (start_code_found) {
  181. vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  182. vpc->prev_start_code = b;
  183. unesc_index = 0;
  184. if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
  185. if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
  186. pic_found = 1;
  187. }
  188. else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
  189. next = i - 4;
  190. pic_found = b == (VC1_CODE_FRAME & 0xFF);
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. vpc->pc.frame_start_found = pic_found;
  197. vpc->unesc_index = unesc_index;
  198. vpc->search_state = search_state;
  199. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  200. next = buf_size;
  201. } else {
  202. if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
  203. vpc->bytes_to_skip = 0;
  204. *poutbuf = NULL;
  205. *poutbuf_size = 0;
  206. return buf_size;
  207. }
  208. }
  209. /* If we return with a valid pointer to a combined frame buffer
  210. * then on the next call then we'll have been unhelpfully rewound
  211. * by up to 4 bytes (depending upon whether the start code
  212. * overlapped the input buffer, and if so by how much). We don't
  213. * want this: it will either cause spurious second detections of
  214. * the start code we've already seen, or cause extra bytes to be
  215. * inserted at the start of the unescaped buffer. */
  216. vpc->bytes_to_skip = 4;
  217. if (next < 0)
  218. vpc->bytes_to_skip += next;
  219. *poutbuf = buf;
  220. *poutbuf_size = buf_size;
  221. return next;
  222. }
  223. static int vc1_split(AVCodecContext *avctx,
  224. const uint8_t *buf, int buf_size)
  225. {
  226. int i;
  227. uint32_t state= -1;
  228. int charged=0;
  229. for(i=0; i<buf_size; i++){
  230. state= (state<<8) | buf[i];
  231. if(IS_MARKER(state)){
  232. if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
  233. charged=1;
  234. }else if(charged){
  235. return i-3;
  236. }
  237. }
  238. }
  239. return 0;
  240. }
  241. static av_cold int vc1_parse_init(AVCodecParserContext *s)
  242. {
  243. VC1ParseContext *vpc = s->priv_data;
  244. vpc->v.s.slice_context_count = 1;
  245. vpc->prev_start_code = 0;
  246. vpc->bytes_to_skip = 0;
  247. vpc->unesc_index = 0;
  248. vpc->search_state = NO_MATCH;
  249. return ff_vc1_init_common(&vpc->v);
  250. }
  251. AVCodecParser ff_vc1_parser = {
  252. .codec_ids = { AV_CODEC_ID_VC1 },
  253. .priv_data_size = sizeof(VC1ParseContext),
  254. .parser_init = vc1_parse_init,
  255. .parser_parse = vc1_parse,
  256. .parser_close = ff_parse_close,
  257. .split = vc1_split,
  258. };