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.

291 lines
9.8KB

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