FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_parser.c
Date: 2024-04-17 23:49:55
Exec Total Coverage
Lines: 157 164 95.7%
Functions: 4 4 100.0%
Branches: 108 131 82.4%

Line Branch Exec Source
1 /*
2 * MPEG-1 / MPEG-2 video parser
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
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 #include "libavutil/avassert.h"
24 #include "decode.h"
25 #include "parser.h"
26 #include "mpeg12.h"
27 #include "mpeg12data.h"
28 #include "startcode.h"
29
30 struct MpvParseContext {
31 ParseContext pc;
32 AVRational frame_rate;
33 int progressive_sequence;
34 int width, height;
35 };
36
37 /**
38 * Find the end of the current frame in the bitstream.
39 * @return the position of the first byte of the next frame, or -1
40 */
41 54407 static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf,
42 int buf_size, AVCodecParserContext *s)
43 {
44 int i;
45 54407 uint32_t state = pc->state;
46
47 /* EOF considered as end of frame */
48
2/2
✓ Branch 0 taken 341 times.
✓ Branch 1 taken 54066 times.
54407 if (buf_size == 0)
49 341 return 0;
50
51 /*
52 0 frame start -> 1/4
53 1 first_SEQEXT -> 0/2
54 2 first field start -> 3/0
55 3 second_SEQEXT -> 2/0
56 4 searching end
57 */
58
59
2/2
✓ Branch 0 taken 260512 times.
✓ Branch 1 taken 47487 times.
307999 for (i = 0; i < buf_size; i++) {
60 av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
61
2/2
✓ Branch 0 taken 26689 times.
✓ Branch 1 taken 233823 times.
260512 if (pc->frame_start_found & 1) {
62
4/4
✓ Branch 0 taken 10705 times.
✓ Branch 1 taken 15984 times.
✓ Branch 2 taken 2713 times.
✓ Branch 3 taken 7992 times.
26689 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
63 2713 pc->frame_start_found--;
64
2/2
✓ Branch 0 taken 7992 times.
✓ Branch 1 taken 15984 times.
23976 else if (state == EXT_START_CODE + 2) {
65
2/2
✓ Branch 0 taken 4135 times.
✓ Branch 1 taken 3857 times.
7992 if ((buf[i] & 3) == 3)
66 4135 pc->frame_start_found = 0;
67 else
68 3857 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
69 }
70 26689 state++;
71 } else {
72 233823 i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
73
6/6
✓ Branch 0 taken 24749 times.
✓ Branch 1 taken 209074 times.
✓ Branch 2 taken 17749 times.
✓ Branch 3 taken 7000 times.
✓ Branch 4 taken 7022 times.
✓ Branch 5 taken 10727 times.
233823 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
74 7022 i++;
75 7022 pc->frame_start_found = 4;
76 }
77
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 233812 times.
233823 if (state == SEQ_END_CODE) {
78 11 pc->frame_start_found = 0;
79 11 pc->state = -1;
80 11 return i + 1;
81 }
82
3/4
✓ Branch 0 taken 40638 times.
✓ Branch 1 taken 193174 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40638 times.
233812 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
83 pc->frame_start_found = 0;
84
4/4
✓ Branch 0 taken 58365 times.
✓ Branch 1 taken 175447 times.
✓ Branch 2 taken 10705 times.
✓ Branch 3 taken 47660 times.
233812 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
85 10705 pc->frame_start_found++;
86
4/4
✓ Branch 0 taken 175447 times.
✓ Branch 1 taken 58365 times.
✓ Branch 2 taken 136158 times.
✓ Branch 3 taken 39289 times.
233812 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
87
4/4
✓ Branch 0 taken 130032 times.
✓ Branch 1 taken 6126 times.
✓ Branch 2 taken 442 times.
✓ Branch 3 taken 129590 times.
136158 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
88 6568 pc->frame_start_found = 0;
89 6568 pc->state = -1;
90 6568 return i - 3;
91 }
92 }
93
5/6
✓ Branch 0 taken 9923 times.
✓ Branch 1 taken 217321 times.
✓ Branch 2 taken 9923 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7000 times.
✓ Branch 5 taken 2923 times.
227244 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
94 7000 ff_fetch_timestamp(s, i - 3, 1, i > 3);
95 }
96 }
97 }
98 47487 pc->state = state;
99 47487 return END_NOT_FOUND;
100 }
101
102 9743 static void mpegvideo_extract_headers(AVCodecParserContext *s,
103 AVCodecContext *avctx,
104 const uint8_t *buf, int buf_size)
105 {
106 9743 struct MpvParseContext *pc = s->priv_data;
107 9743 const uint8_t *buf_end = buf + buf_size;
108 int bytes_left;
109 9743 int did_set_size=0;
110 9743 int set_dim_ret = 0;
111 9743 int bit_rate = 0;
112 9743 int vbv_delay = 0;
113 9743 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
114
115 // number of picture coding extensions (i.e. MPEG2 pictures)
116 // in this packet - should be 1 or 2
117 9743 int nb_pic_ext = 0;
118 // when there are two pictures in the packet this indicates
119 // which field is in the first of them
120 9743 int first_field = AV_FIELD_UNKNOWN;
121
122 //FIXME replace the crap with get_bits()
123
2/2
✓ Branch 0 taken 35745 times.
✓ Branch 1 taken 274 times.
36019 while (buf < buf_end) {
124 35745 uint32_t start_code = -1;
125 35745 buf= avpriv_find_start_code(buf, buf_end, &start_code);
126 35745 bytes_left = buf_end - buf;
127
4/5
✓ Branch 0 taken 9442 times.
✓ Branch 1 taken 1995 times.
✓ Branch 2 taken 11532 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12776 times.
35745 switch(start_code) {
128 9442 case PICTURE_START_CODE:
129
1/2
✓ Branch 0 taken 9442 times.
✗ Branch 1 not taken.
9442 if (bytes_left >= 2) {
130 9442 s->pict_type = (buf[1] >> 3) & 7;
131
1/2
✓ Branch 0 taken 9442 times.
✗ Branch 1 not taken.
9442 if (bytes_left >= 4)
132 9442 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
133 }
134 9442 break;
135 1995 case SEQ_START_CODE:
136
1/2
✓ Branch 0 taken 1995 times.
✗ Branch 1 not taken.
1995 if (bytes_left >= 7) {
137 int frame_rate_index;
138
139 1995 pc->width = (buf[0] << 4) | (buf[1] >> 4);
140 1995 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
141
6/8
✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 1900 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1713 times.
✓ Branch 5 taken 187 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1713 times.
1995 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
142 282 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
143 282 did_set_size=1;
144 }
145 1995 pix_fmt = AV_PIX_FMT_YUV420P;
146 1995 frame_rate_index = buf[3] & 0xf;
147 1995 pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index];
148 1995 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
149 1995 avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
150 #if FF_API_TICKS_PER_FRAME
151 FF_DISABLE_DEPRECATION_WARNINGS
152 1995 avctx->ticks_per_frame = 1;
153 FF_ENABLE_DEPRECATION_WARNINGS
154 #endif
155 }
156 1995 break;
157 11532 case EXT_START_CODE:
158
1/2
✓ Branch 0 taken 11532 times.
✗ Branch 1 not taken.
11532 if (bytes_left >= 1) {
159
3/3
✓ Branch 0 taken 1856 times.
✓ Branch 1 taken 8570 times.
✓ Branch 2 taken 1106 times.
11532 switch (buf[0] >> 4) { // ext_type
160 1856 case 0x1: /* sequence extension */
161
1/2
✓ Branch 0 taken 1856 times.
✗ Branch 1 not taken.
1856 if (bytes_left >= 6) {
162 1856 int horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
163 1856 int vert_size_ext = (buf[2] >> 5) & 3;
164 1856 int bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
165 1856 int frame_rate_ext_n = (buf[5] >> 5) & 3;
166 1856 int frame_rate_ext_d = (buf[5] & 0x1f);
167 1856 pc->progressive_sequence = buf[1] & (1 << 3);
168 1856 avctx->has_b_frames= !(buf[5] >> 7);
169
170
2/4
✓ Branch 0 taken 801 times.
✓ Branch 1 taken 1055 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1856 switch ((buf[1] >> 1) & 3) { // chroma_format
171 801 case 1: pix_fmt = AV_PIX_FMT_YUV420P; break;
172 1055 case 2: pix_fmt = AV_PIX_FMT_YUV422P; break;
173 case 3: pix_fmt = AV_PIX_FMT_YUV444P; break;
174 }
175
176 1856 pc->width = (pc->width & 0xFFF) | (horiz_size_ext << 12);
177 1856 pc->height = (pc->height& 0xFFF) | ( vert_size_ext << 12);
178 1856 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
179
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 1593 times.
1856 if(did_set_size)
180 263 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
181 1856 avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1);
182 1856 avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1);
183 1856 avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
184 #if FF_API_TICKS_PER_FRAME
185 FF_DISABLE_DEPRECATION_WARNINGS
186 1856 avctx->ticks_per_frame = 2;
187 FF_ENABLE_DEPRECATION_WARNINGS
188 #endif
189 }
190 1856 break;
191 8570 case 0x8: /* picture coding extension */
192
1/2
✓ Branch 0 taken 8570 times.
✗ Branch 1 not taken.
8570 if (bytes_left >= 5) {
193 8570 int top_field_first = buf[3] & (1 << 7);
194 8570 int repeat_first_field = buf[3] & (1 << 1);
195 8570 int progressive_frame = buf[4] & (1 << 7);
196
197 /* check if we must repeat the frame */
198 8570 s->repeat_pict = 1;
199
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8561 times.
8570 if (repeat_first_field) {
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (pc->progressive_sequence) {
201 if (top_field_first)
202 s->repeat_pict = 5;
203 else
204 s->repeat_pict = 3;
205
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 } else if (progressive_frame) {
206 9 s->repeat_pict = 2;
207 }
208 }
209
210
4/4
✓ Branch 0 taken 5445 times.
✓ Branch 1 taken 3125 times.
✓ Branch 2 taken 5171 times.
✓ Branch 3 taken 274 times.
8570 if (!pc->progressive_sequence && !progressive_frame) {
211
2/2
✓ Branch 0 taken 1863 times.
✓ Branch 1 taken 3308 times.
5171 if (top_field_first)
212 1863 s->field_order = AV_FIELD_TT;
213 else
214 3308 s->field_order = AV_FIELD_BB;
215 } else
216 3399 s->field_order = AV_FIELD_PROGRESSIVE;
217
218 8570 s->picture_structure = buf[2] & 3;
219
220
1/2
✓ Branch 0 taken 8570 times.
✗ Branch 1 not taken.
8570 if (!nb_pic_ext) {
221 // remember parity of the first field for the case
222 // when there are 2 fields in packet
223
3/3
✓ Branch 0 taken 1935 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6629 times.
8570 switch (s->picture_structure) {
224 1935 case AV_PICTURE_STRUCTURE_BOTTOM_FIELD: first_field = AV_FIELD_BB; break;
225 6 case AV_PICTURE_STRUCTURE_TOP_FIELD: first_field = AV_FIELD_TT; break;
226 }
227 }
228
229 8570 nb_pic_ext++;
230 }
231 8570 break;
232 }
233 }
234 11532 break;
235 case -1:
236 9469 goto the_end;
237 12776 default:
238 /* we stop parsing when we encounter a slice. It ensures
239 that this function takes a negligible amount of time */
240
1/2
✓ Branch 0 taken 12776 times.
✗ Branch 1 not taken.
12776 if (start_code >= SLICE_MIN_START_CODE &&
241
2/2
✓ Branch 0 taken 9469 times.
✓ Branch 1 taken 3307 times.
12776 start_code <= SLICE_MAX_START_CODE)
242 9469 goto the_end;
243 3307 break;
244 }
245 }
246 274 the_end:
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9743 times.
9743 if (set_dim_ret < 0)
248 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions\n");
249
250
6/6
✓ Branch 0 taken 8840 times.
✓ Branch 1 taken 903 times.
✓ Branch 2 taken 1856 times.
✓ Branch 3 taken 6984 times.
✓ Branch 4 taken 1163 times.
✓ Branch 5 taken 693 times.
9743 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate && bit_rate != 0x3FFFF) {
251 1163 avctx->rc_max_rate = 400LL*bit_rate;
252 }
253
2/2
✓ Branch 0 taken 1995 times.
✓ Branch 1 taken 7748 times.
9743 if (bit_rate &&
254
5/6
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 1856 times.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1065 times.
✓ Branch 5 taken 930 times.
1995 ((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) {
255 1065 avctx->bit_rate = 400LL*bit_rate;
256 }
257
258
2/2
✓ Branch 0 taken 1995 times.
✓ Branch 1 taken 7748 times.
9743 if (pix_fmt != AV_PIX_FMT_NONE) {
259 1995 s->format = pix_fmt;
260 1995 s->width = pc->width;
261 1995 s->height = pc->height;
262 1995 s->coded_width = FFALIGN(pc->width, 16);
263 1995 s->coded_height = FFALIGN(pc->height, 16);
264 }
265
266
3/4
✓ Branch 0 taken 8840 times.
✓ Branch 1 taken 903 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8840 times.
9743 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO || nb_pic_ext > 1) {
267 903 s->repeat_pict = 1;
268 903 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 903 times.
903 s->field_order = nb_pic_ext > 1 ? first_field : AV_FIELD_PROGRESSIVE;
270 }
271 9743 }
272
273 57230 static int mpegvideo_parse(AVCodecParserContext *s,
274 AVCodecContext *avctx,
275 const uint8_t **poutbuf, int *poutbuf_size,
276 const uint8_t *buf, int buf_size)
277 {
278 57230 struct MpvParseContext *pc1 = s->priv_data;
279 57230 ParseContext *pc= &pc1->pc;
280 int next;
281
282
2/2
✓ Branch 0 taken 2823 times.
✓ Branch 1 taken 54407 times.
57230 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
283 2823 next= buf_size;
284 }else{
285 54407 next = mpeg1_find_frame_end(pc, buf, buf_size, s);
286
287
2/2
✓ Branch 1 taken 47487 times.
✓ Branch 2 taken 6920 times.
54407 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
288 47487 *poutbuf = NULL;
289 47487 *poutbuf_size = 0;
290 47487 return buf_size;
291 }
292
293 }
294 /* we have a full frame : we just parse the first few MPEG headers
295 to have the full timing information. The time take by this
296 function should be negligible for uncorrupted streams */
297 9743 mpegvideo_extract_headers(s, avctx, buf, buf_size);
298 ff_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
299 s->pict_type, av_q2d(avctx->framerate), s->repeat_pict);
300
301 9743 *poutbuf = buf;
302 9743 *poutbuf_size = buf_size;
303 9743 return next;
304 }
305
306 682 static int mpegvideo_parse_init(AVCodecParserContext *s)
307 {
308 682 s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
309 682 return 0;
310 }
311
312 const AVCodecParser ff_mpegvideo_parser = {
313 .codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO },
314 .priv_data_size = sizeof(struct MpvParseContext),
315 .parser_init = mpegvideo_parse_init,
316 .parser_parse = mpegvideo_parse,
317 .parser_close = ff_parse_close,
318 };
319