FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/vc1_parser.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 119 130 91.5%
Branches: 82 104 78.8%

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