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.5KB

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