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.

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