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.

289 lines
9.7KB

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