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.

297 lines
10KB

  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 VC1ParseContext {
  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_bits8(&gb, buf, buf_size);
  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. s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
  107. : AV_PIX_FMT_NONE;
  108. if (avctx->width && avctx->height) {
  109. s->width = avctx->width;
  110. s->height = avctx->height;
  111. s->coded_width = FFALIGN(avctx->coded_width, 16);
  112. s->coded_height = FFALIGN(avctx->coded_height, 16);
  113. }
  114. }
  115. static int vc1_parse(AVCodecParserContext *s,
  116. AVCodecContext *avctx,
  117. const uint8_t **poutbuf, int *poutbuf_size,
  118. const uint8_t *buf, int buf_size)
  119. {
  120. /* Here we do the searching for frame boundaries and headers at
  121. * the same time. Only a minimal amount at the start of each
  122. * header is unescaped. */
  123. VC1ParseContext *vpc = s->priv_data;
  124. int pic_found = vpc->pc.frame_start_found;
  125. uint8_t *unesc_buffer = vpc->unesc_buffer;
  126. size_t unesc_index = vpc->unesc_index;
  127. VC1ParseSearchState search_state = vpc->search_state;
  128. int start_code_found = 0;
  129. int next = END_NOT_FOUND;
  130. int i = vpc->bytes_to_skip;
  131. if (pic_found && buf_size == 0) {
  132. /* EOF considered as end of frame */
  133. memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
  134. vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  135. next = 0;
  136. }
  137. while (i < buf_size) {
  138. uint8_t b;
  139. start_code_found = 0;
  140. while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
  141. b = buf[i++];
  142. unesc_buffer[unesc_index++] = b;
  143. if (search_state <= ONE_ZERO)
  144. search_state = b ? NO_MATCH : search_state + 1;
  145. else if (search_state == TWO_ZEROS) {
  146. if (b == 1)
  147. search_state = ONE;
  148. else if (b > 1) {
  149. if (b == 3)
  150. unesc_index--; // swallow emulation prevention byte
  151. search_state = NO_MATCH;
  152. }
  153. }
  154. else { // search_state == ONE
  155. // Header unescaping terminates early due to detection of next start code
  156. search_state = NO_MATCH;
  157. start_code_found = 1;
  158. break;
  159. }
  160. }
  161. if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
  162. unesc_index >= UNESCAPED_THRESHOLD &&
  163. vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
  164. {
  165. // No need to keep scanning the rest of the buffer for
  166. // start codes if we know it contains a complete frame and
  167. // we've already unescaped all we need of the frame header
  168. vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  169. break;
  170. }
  171. if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
  172. while (i < buf_size) {
  173. if (search_state == NO_MATCH) {
  174. i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
  175. if (i < buf_size) {
  176. search_state = ONE_ZERO;
  177. }
  178. i++;
  179. } else {
  180. b = buf[i++];
  181. if (search_state == ONE_ZERO)
  182. search_state = b ? NO_MATCH : TWO_ZEROS;
  183. else if (search_state == TWO_ZEROS) {
  184. if (b >= 1)
  185. search_state = b == 1 ? ONE : NO_MATCH;
  186. }
  187. else { // search_state == ONE
  188. search_state = NO_MATCH;
  189. start_code_found = 1;
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. if (start_code_found) {
  196. vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
  197. vpc->prev_start_code = b;
  198. unesc_index = 0;
  199. if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
  200. if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
  201. pic_found = 1;
  202. }
  203. else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
  204. next = i - 4;
  205. pic_found = b == (VC1_CODE_FRAME & 0xFF);
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. vpc->pc.frame_start_found = pic_found;
  212. vpc->unesc_index = unesc_index;
  213. vpc->search_state = search_state;
  214. if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
  215. next = buf_size;
  216. } else {
  217. if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
  218. vpc->bytes_to_skip = 0;
  219. *poutbuf = NULL;
  220. *poutbuf_size = 0;
  221. return buf_size;
  222. }
  223. }
  224. /* If we return with a valid pointer to a combined frame buffer
  225. * then on the next call then we'll have been unhelpfully rewound
  226. * by up to 4 bytes (depending upon whether the start code
  227. * overlapped the input buffer, and if so by how much). We don't
  228. * want this: it will either cause spurious second detections of
  229. * the start code we've already seen, or cause extra bytes to be
  230. * inserted at the start of the unescaped buffer. */
  231. vpc->bytes_to_skip = 4;
  232. if (next < 0 && next != END_NOT_FOUND)
  233. vpc->bytes_to_skip += next;
  234. *poutbuf = buf;
  235. *poutbuf_size = buf_size;
  236. return next;
  237. }
  238. static int vc1_split(AVCodecContext *avctx,
  239. const uint8_t *buf, int buf_size)
  240. {
  241. uint32_t state = -1;
  242. int charged = 0;
  243. const uint8_t *ptr = buf, *end = buf + buf_size;
  244. while (ptr < end) {
  245. ptr = avpriv_find_start_code(ptr, end, &state);
  246. if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
  247. charged = 1;
  248. } else if (charged && IS_MARKER(state))
  249. return ptr - 4 - buf;
  250. }
  251. return 0;
  252. }
  253. static av_cold int vc1_parse_init(AVCodecParserContext *s)
  254. {
  255. VC1ParseContext *vpc = s->priv_data;
  256. vpc->v.s.slice_context_count = 1;
  257. vpc->v.first_pic_header_flag = 1;
  258. vpc->prev_start_code = 0;
  259. vpc->bytes_to_skip = 0;
  260. vpc->unesc_index = 0;
  261. vpc->search_state = NO_MATCH;
  262. return ff_vc1_init_common(&vpc->v);
  263. }
  264. AVCodecParser ff_vc1_parser = {
  265. .codec_ids = { AV_CODEC_ID_VC1 },
  266. .priv_data_size = sizeof(VC1ParseContext),
  267. .parser_init = vc1_parse_init,
  268. .parser_parse = vc1_parse,
  269. .parser_close = ff_parse_close,
  270. .split = vc1_split,
  271. };