| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | /** | ||
| 24 | * @file | ||
| 25 | * VC-1 and WMV3 parser | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/attributes.h" | ||
| 29 | #include "libavutil/avassert.h" | ||
| 30 | #include "parser.h" | ||
| 31 | #include "parser_internal.h" | ||
| 32 | #include "vc1.h" | ||
| 33 | #include "get_bits.h" | ||
| 34 | #include "vc1dsp.h" | ||
| 35 | |||
| 36 | /** The maximum number of bytes of a sequence, entry point or | ||
| 37 | * frame header whose values we pay any attention to */ | ||
| 38 | #define UNESCAPED_THRESHOLD 37 | ||
| 39 | |||
| 40 | /** The maximum number of bytes of a sequence, entry point or | ||
| 41 | * frame header which must be valid memory (because they are | ||
| 42 | * used to update the bitstream cache in skip_bits() calls) | ||
| 43 | */ | ||
| 44 | #define UNESCAPED_LIMIT 144 | ||
| 45 | |||
| 46 | typedef enum { | ||
| 47 | NO_MATCH, | ||
| 48 | ONE_ZERO, | ||
| 49 | TWO_ZEROS, | ||
| 50 | ONE | ||
| 51 | } VC1ParseSearchState; | ||
| 52 | |||
| 53 | typedef struct VC1ParseContext { | ||
| 54 | ParseContext pc; | ||
| 55 | VC1Context v; | ||
| 56 | uint8_t prev_start_code; | ||
| 57 | size_t bytes_to_skip; | ||
| 58 | uint8_t unesc_buffer[UNESCAPED_LIMIT]; | ||
| 59 | size_t unesc_index; | ||
| 60 | VC1ParseSearchState search_state; | ||
| 61 | } VC1ParseContext; | ||
| 62 | |||
| 63 | 973 | static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx, | |
| 64 | const uint8_t *buf, int buf_size) | ||
| 65 | { | ||
| 66 | /* Parse the header we just finished unescaping */ | ||
| 67 | 973 | VC1ParseContext *vpc = s->priv_data; | |
| 68 | GetBitContext gb; | ||
| 69 | int ret; | ||
| 70 | 973 | vpc->v.s.avctx = avctx; | |
| 71 | 973 | ret = init_get_bits8(&gb, buf, buf_size); | |
| 72 | av_assert1(ret >= 0); // buf_size is bounded by UNESCAPED_THRESHOLD | ||
| 73 | |||
| 74 |
4/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 305 times.
✓ Branch 3 taken 645 times.
|
973 | switch (vpc->prev_start_code) { |
| 75 | 9 | case VC1_CODE_SEQHDR & 0xFF: | |
| 76 | 9 | ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb); | |
| 77 | 9 | break; | |
| 78 | 14 | case VC1_CODE_ENTRYPOINT & 0xFF: | |
| 79 | 14 | ff_vc1_decode_entry_point(avctx, &vpc->v, &gb); | |
| 80 | 14 | break; | |
| 81 | 305 | case VC1_CODE_FRAME & 0xFF: | |
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
|
305 | if(vpc->v.profile < PROFILE_ADVANCED) |
| 83 | ✗ | ret = ff_vc1_parse_frame_header (&vpc->v, &gb); | |
| 84 | else | ||
| 85 | 305 | ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb); | |
| 86 | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
|
305 | if (ret < 0) |
| 88 | ✗ | break; | |
| 89 | |||
| 90 | /* keep AV_PICTURE_TYPE_BI internal to VC1 */ | ||
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
|
305 | if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI) |
| 92 | ✗ | s->pict_type = AV_PICTURE_TYPE_B; | |
| 93 | else | ||
| 94 | 305 | s->pict_type = vpc->v.s.pict_type; | |
| 95 | |||
| 96 |
1/2✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
|
305 | if (vpc->v.broadcast){ |
| 97 | // process pulldown flags | ||
| 98 | 305 | s->repeat_pict = 1; | |
| 99 | // Pulldown flags are only valid when 'broadcast' has been set. | ||
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
|
305 | if (vpc->v.rff){ |
| 101 | // repeat field | ||
| 102 | ✗ | s->repeat_pict = 2; | |
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 305 times.
|
305 | }else if (vpc->v.rptfrm){ |
| 104 | // repeat frames | ||
| 105 | ✗ | s->repeat_pict = vpc->v.rptfrm * 2 + 1; | |
| 106 | } | ||
| 107 | }else{ | ||
| 108 | ✗ | s->repeat_pict = 0; | |
| 109 | } | ||
| 110 | |||
| 111 |
4/6✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 261 times.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
|
305 | if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf) |
| 112 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB; |
| 113 | else | ||
| 114 | 261 | s->field_order = AV_FIELD_PROGRESSIVE; | |
| 115 | |||
| 116 | 305 | break; | |
| 117 | } | ||
| 118 | 1946 | s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P | |
| 119 |
2/2✓ Branch 0 taken 965 times.
✓ Branch 1 taken 8 times.
|
973 | : AV_PIX_FMT_NONE; |
| 120 |
3/4✓ Branch 0 taken 961 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 961 times.
✗ Branch 3 not taken.
|
973 | if (avctx->width && avctx->height) { |
| 121 | 961 | s->width = avctx->width; | |
| 122 | 961 | s->height = avctx->height; | |
| 123 | 961 | s->coded_width = FFALIGN(avctx->coded_width, 16); | |
| 124 | 961 | s->coded_height = FFALIGN(avctx->coded_height, 16); | |
| 125 | } | ||
| 126 | 973 | } | |
| 127 | |||
| 128 | 4745 | static int vc1_parse(AVCodecParserContext *s, | |
| 129 | AVCodecContext *avctx, | ||
| 130 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 131 | const uint8_t *buf, int buf_size) | ||
| 132 | { | ||
| 133 | /* Here we do the searching for frame boundaries and headers at | ||
| 134 | * the same time. Only a minimal amount at the start of each | ||
| 135 | * header is unescaped. */ | ||
| 136 | 4745 | VC1ParseContext *vpc = s->priv_data; | |
| 137 | 4745 | int pic_found = vpc->pc.frame_start_found; | |
| 138 | 4745 | uint8_t *unesc_buffer = vpc->unesc_buffer; | |
| 139 | 4745 | size_t unesc_index = vpc->unesc_index; | |
| 140 | 4745 | VC1ParseSearchState search_state = vpc->search_state; | |
| 141 | 4745 | int start_code_found = 0; | |
| 142 | 4745 | int next = END_NOT_FOUND; | |
| 143 | 4745 | int i = vpc->bytes_to_skip; | |
| 144 | |||
| 145 |
4/4✓ Branch 0 taken 4736 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 4720 times.
|
4745 | if (pic_found && buf_size == 0) { |
| 146 | /* EOF considered as end of frame */ | ||
| 147 | 16 | memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index); | |
| 148 | 16 | vc1_extract_header(s, avctx, unesc_buffer, unesc_index); | |
| 149 | 16 | next = 0; | |
| 150 | } | ||
| 151 |
2/2✓ Branch 0 taken 5393 times.
✓ Branch 1 taken 4453 times.
|
9846 | while (i < buf_size) { |
| 152 | uint8_t b; | ||
| 153 | 5393 | start_code_found = 0; | |
| 154 |
4/4✓ Branch 0 taken 38265 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 32986 times.
✓ Branch 3 taken 5279 times.
|
38288 | while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) { |
| 155 | 32986 | b = buf[i++]; | |
| 156 | 32986 | unesc_buffer[unesc_index++] = b; | |
| 157 |
2/2✓ Branch 0 taken 32765 times.
✓ Branch 1 taken 221 times.
|
32986 | if (search_state <= ONE_ZERO) |
| 158 |
2/2✓ Branch 0 taken 860 times.
✓ Branch 1 taken 31905 times.
|
32765 | search_state = b ? NO_MATCH : search_state + 1; |
| 159 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 91 times.
|
221 | else if (search_state == TWO_ZEROS) { |
| 160 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 39 times.
|
130 | if (b == 1) |
| 161 | 91 | search_state = ONE; | |
| 162 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | else if (b > 1) { |
| 163 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 17 times.
|
39 | if (b == 3) |
| 164 | 22 | unesc_index--; // swallow emulation prevention byte | |
| 165 | 39 | search_state = NO_MATCH; | |
| 166 | } | ||
| 167 | } | ||
| 168 | else { // search_state == ONE | ||
| 169 | // Header unescaping terminates early due to detection of next start code | ||
| 170 | 91 | search_state = NO_MATCH; | |
| 171 | 91 | start_code_found = 1; | |
| 172 | 91 | break; | |
| 173 | } | ||
| 174 | } | ||
| 175 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 5393 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
5393 | if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) && |
| 176 | ✗ | unesc_index >= UNESCAPED_THRESHOLD && | |
| 177 | ✗ | vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF)) | |
| 178 | { | ||
| 179 | // No need to keep scanning the rest of the buffer for | ||
| 180 | // start codes if we know it contains a complete frame and | ||
| 181 | // we've already unescaped all we need of the frame header | ||
| 182 | ✗ | vc1_extract_header(s, avctx, unesc_buffer, unesc_index); | |
| 183 | ✗ | unesc_index = 0; | |
| 184 | ✗ | break; | |
| 185 | } | ||
| 186 |
3/4✓ Branch 0 taken 5279 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 5279 times.
✗ Branch 3 not taken.
|
5393 | if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) { |
| 187 |
2/2✓ Branch 0 taken 52860 times.
✓ Branch 1 taken 4413 times.
|
57273 | while (i < buf_size) { |
| 188 |
2/2✓ Branch 0 taken 27593 times.
✓ Branch 1 taken 25267 times.
|
52860 | if (search_state == NO_MATCH) { |
| 189 | 27593 | i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i); | |
| 190 |
2/2✓ Branch 0 taken 23222 times.
✓ Branch 1 taken 4371 times.
|
27593 | if (i < buf_size) { |
| 191 | 23222 | search_state = ONE_ZERO; | |
| 192 | } | ||
| 193 | 27593 | i++; | |
| 194 | } else { | ||
| 195 | 25267 | b = buf[i++]; | |
| 196 |
2/2✓ Branch 0 taken 23231 times.
✓ Branch 1 taken 2036 times.
|
25267 | if (search_state == ONE_ZERO) |
| 197 |
2/2✓ Branch 0 taken 22061 times.
✓ Branch 1 taken 1170 times.
|
23231 | search_state = b ? NO_MATCH : TWO_ZEROS; |
| 198 |
2/2✓ Branch 0 taken 1170 times.
✓ Branch 1 taken 866 times.
|
2036 | else if (search_state == TWO_ZEROS) { |
| 199 |
1/2✓ Branch 0 taken 1170 times.
✗ Branch 1 not taken.
|
1170 | if (b >= 1) |
| 200 |
2/2✓ Branch 0 taken 866 times.
✓ Branch 1 taken 304 times.
|
1170 | search_state = b == 1 ? ONE : NO_MATCH; |
| 201 | } | ||
| 202 | else { // search_state == ONE | ||
| 203 | 866 | search_state = NO_MATCH; | |
| 204 | 866 | start_code_found = 1; | |
| 205 | 866 | break; | |
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } | ||
| 210 |
2/2✓ Branch 0 taken 957 times.
✓ Branch 1 taken 4436 times.
|
5393 | if (start_code_found) { |
| 211 | 957 | vc1_extract_header(s, avctx, unesc_buffer, unesc_index); | |
| 212 | |||
| 213 | 957 | vpc->prev_start_code = b; | |
| 214 | 957 | unesc_index = 0; | |
| 215 | |||
| 216 |
1/2✓ Branch 0 taken 957 times.
✗ Branch 1 not taken.
|
957 | if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) { |
| 217 |
5/6✓ Branch 0 taken 31 times.
✓ Branch 1 taken 926 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
|
957 | if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) { |
| 218 | 9 | pic_found = 1; | |
| 219 | } | ||
| 220 |
6/6✓ Branch 0 taken 926 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 890 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 293 times.
✓ Branch 5 taken 597 times.
|
948 | else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF) |
| 221 |
2/2✓ Branch 0 taken 292 times.
✓ Branch 1 taken 1 times.
|
293 | && b != (VC1_CODE_ENDOFSEQ & 0xFF)) { |
| 222 | 292 | next = i - 4; | |
| 223 | 292 | pic_found = b == (VC1_CODE_FRAME & 0xFF); | |
| 224 | 292 | break; | |
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | 4745 | vpc->pc.frame_start_found = pic_found; | |
| 231 | 4745 | vpc->unesc_index = unesc_index; | |
| 232 | 4745 | vpc->search_state = search_state; | |
| 233 | |||
| 234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4745 times.
|
4745 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 235 | ✗ | next = buf_size; | |
| 236 | } else { | ||
| 237 |
2/2✓ Branch 1 taken 4437 times.
✓ Branch 2 taken 308 times.
|
4745 | if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) { |
| 238 | 4437 | vpc->bytes_to_skip = 0; | |
| 239 | 4437 | *poutbuf = NULL; | |
| 240 | 4437 | *poutbuf_size = 0; | |
| 241 | 4437 | return buf_size; | |
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | /* If we return with a valid pointer to a combined frame buffer | ||
| 246 | * then on the next call then we'll have been unhelpfully rewound | ||
| 247 | * by up to 4 bytes (depending upon whether the start code | ||
| 248 | * overlapped the input buffer, and if so by how much). We don't | ||
| 249 | * want this: it will either cause spurious second detections of | ||
| 250 | * the start code we've already seen, or cause extra bytes to be | ||
| 251 | * inserted at the start of the unescaped buffer. */ | ||
| 252 | 308 | vpc->bytes_to_skip = 4; | |
| 253 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
308 | if (next < 0 && next != END_NOT_FOUND) |
| 254 | ✗ | vpc->bytes_to_skip += next; | |
| 255 | |||
| 256 | 308 | *poutbuf = buf; | |
| 257 | 308 | *poutbuf_size = buf_size; | |
| 258 | 308 | return next; | |
| 259 | } | ||
| 260 | |||
| 261 | 8 | static av_cold int vc1_parse_init(AVCodecParserContext *s) | |
| 262 | { | ||
| 263 | 8 | VC1ParseContext *vpc = s->priv_data; | |
| 264 | 8 | vpc->v.s.slice_context_count = 1; | |
| 265 | 8 | vpc->v.first_pic_header_flag = 1; | |
| 266 | 8 | vpc->v.parse_only = 1; | |
| 267 | 8 | vpc->prev_start_code = 0; | |
| 268 | 8 | vpc->bytes_to_skip = 0; | |
| 269 | 8 | vpc->unesc_index = 0; | |
| 270 | 8 | vpc->search_state = NO_MATCH; | |
| 271 | 8 | ff_vc1dsp_init(&vpc->v.vc1dsp); /* startcode_find_candidate */ | |
| 272 | 8 | return 0; | |
| 273 | } | ||
| 274 | |||
| 275 | const FFCodecParser ff_vc1_parser = { | ||
| 276 | PARSER_CODEC_LIST(AV_CODEC_ID_VC1), | ||
| 277 | .priv_data_size = sizeof(VC1ParseContext), | ||
| 278 | .init = vc1_parse_init, | ||
| 279 | .parse = vc1_parse, | ||
| 280 | .close = ff_parse_close, | ||
| 281 | }; | ||
| 282 |