FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_parser.c
Date: 2026-01-14 03:33:33
Exec Total Coverage
Lines: 155 162 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 "parser_internal.h"
29 #include "startcode.h"
30
31 struct MpvParseContext {
32 ParseContext pc;
33 AVRational frame_rate;
34 int progressive_sequence;
35 int width, height;
36 };
37
38 /**
39 * Find the end of the current frame in the bitstream.
40 * @return the position of the first byte of the next frame, or -1
41 */
42 56372 static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf,
43 int buf_size, AVCodecParserContext *s)
44 {
45 int i;
46 56372 uint32_t state = pc->state;
47
48 /* EOF considered as end of frame */
49
2/2
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 56023 times.
56372 if (buf_size == 0)
50 349 return 0;
51
52 /*
53 0 frame start -> 1/4
54 1 first_SEQEXT -> 0/2
55 2 first field start -> 3/0
56 3 second_SEQEXT -> 2/0
57 4 searching end
58 */
59
60
2/2
✓ Branch 0 taken 275321 times.
✓ Branch 1 taken 49136 times.
324457 for (i = 0; i < buf_size; i++) {
61 av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
62
2/2
✓ Branch 0 taken 28032 times.
✓ Branch 1 taken 247289 times.
275321 if (pc->frame_start_found & 1) {
63
4/4
✓ Branch 0 taken 11266 times.
✓ Branch 1 taken 16766 times.
✓ Branch 2 taken 2883 times.
✓ Branch 3 taken 8383 times.
28032 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
64 2883 pc->frame_start_found--;
65
2/2
✓ Branch 0 taken 8383 times.
✓ Branch 1 taken 16766 times.
25149 else if (state == EXT_START_CODE + 2) {
66
2/2
✓ Branch 0 taken 4367 times.
✓ Branch 1 taken 4016 times.
8383 if ((buf[i] & 3) == 3)
67 4367 pc->frame_start_found = 0;
68 else
69 4016 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
70 }
71 28032 state++;
72 } else {
73 247289 i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
74
6/6
✓ Branch 0 taken 26259 times.
✓ Branch 1 taken 221030 times.
✓ Branch 2 taken 18946 times.
✓ Branch 3 taken 7313 times.
✓ Branch 4 taken 7335 times.
✓ Branch 5 taken 11611 times.
247289 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
75 7335 i++;
76 7335 pc->frame_start_found = 4;
77 }
78
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 247278 times.
247289 if (state == SEQ_END_CODE) {
79 11 pc->frame_start_found = 0;
80 11 pc->state = -1;
81 11 return i + 1;
82 }
83
3/4
✓ Branch 0 taken 42944 times.
✓ Branch 1 taken 204334 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 42944 times.
247278 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
84 pc->frame_start_found = 0;
85
4/4
✓ Branch 0 taken 61868 times.
✓ Branch 1 taken 185410 times.
✓ Branch 2 taken 11266 times.
✓ Branch 3 taken 50602 times.
247278 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
86 11266 pc->frame_start_found++;
87
4/4
✓ Branch 0 taken 185410 times.
✓ Branch 1 taken 61868 times.
✓ Branch 2 taken 145123 times.
✓ Branch 3 taken 40287 times.
247278 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
88
4/4
✓ Branch 0 taken 138698 times.
✓ Branch 1 taken 6425 times.
✓ Branch 2 taken 451 times.
✓ Branch 3 taken 138247 times.
145123 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
89 6876 pc->frame_start_found = 0;
90 6876 pc->state = -1;
91 6876 return i - 3;
92 }
93 }
94
5/6
✓ Branch 0 taken 10718 times.
✓ Branch 1 taken 229684 times.
✓ Branch 2 taken 10718 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7312 times.
✓ Branch 5 taken 3406 times.
240402 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
95 7312 ff_fetch_timestamp(s, i - 3, 1, i > 3);
96 }
97 }
98 }
99 49136 pc->state = state;
100 49136 return END_NOT_FOUND;
101 }
102
103 9944 static void mpegvideo_extract_headers(AVCodecParserContext *s,
104 AVCodecContext *avctx,
105 const uint8_t *buf, int buf_size)
106 {
107 9944 struct MpvParseContext *pc = s->priv_data;
108 9944 const uint8_t *buf_end = buf + buf_size;
109 int bytes_left;
110 9944 int did_set_size=0;
111 9944 int set_dim_ret = 0;
112 9944 int bit_rate = 0;
113 9944 int vbv_delay = 0;
114 9944 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
115
116 // number of picture coding extensions (i.e. MPEG2 pictures)
117 // in this packet - should be 1 or 2
118 9944 int nb_pic_ext = 0;
119 // when there are two pictures in the packet this indicates
120 // which field is in the first of them
121 9944 int first_field = AV_FIELD_UNKNOWN;
122
123 //FIXME replace the crap with get_bits()
124
2/2
✓ Branch 0 taken 36868 times.
✓ Branch 1 taken 274 times.
37142 while (buf < buf_end) {
125 36868 uint32_t start_code = -1;
126 36868 buf= avpriv_find_start_code(buf, buf_end, &start_code);
127 36868 bytes_left = buf_end - buf;
128
4/5
✓ Branch 0 taken 9641 times.
✓ Branch 1 taken 1991 times.
✓ Branch 2 taken 11806 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13430 times.
36868 switch(start_code) {
129 9641 case PICTURE_START_CODE:
130
1/2
✓ Branch 0 taken 9641 times.
✗ Branch 1 not taken.
9641 if (bytes_left >= 2) {
131 9641 s->pict_type = (buf[1] >> 3) & 7;
132
1/2
✓ Branch 0 taken 9641 times.
✗ Branch 1 not taken.
9641 if (bytes_left >= 4)
133 9641 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
134 }
135 9641 break;
136 1991 case SEQ_START_CODE:
137
1/2
✓ Branch 0 taken 1991 times.
✗ Branch 1 not taken.
1991 if (bytes_left >= 7) {
138 int frame_rate_index;
139
140 1991 pc->width = (buf[0] << 4) | (buf[1] >> 4);
141 1991 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
142
6/8
✓ Branch 0 taken 1894 times.
✓ Branch 1 taken 97 times.
✓ Branch 2 taken 1894 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1705 times.
✓ Branch 5 taken 189 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1705 times.
1991 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
143 286 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
144 286 did_set_size=1;
145 }
146 1991 pix_fmt = AV_PIX_FMT_YUV420P;
147 1991 frame_rate_index = buf[3] & 0xf;
148 1991 pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index];
149 1991 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
150 1991 avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
151 }
152 1991 break;
153 11806 case EXT_START_CODE:
154
1/2
✓ Branch 0 taken 11806 times.
✗ Branch 1 not taken.
11806 if (bytes_left >= 1) {
155
3/3
✓ Branch 0 taken 1852 times.
✓ Branch 1 taken 8769 times.
✓ Branch 2 taken 1185 times.
11806 switch (buf[0] >> 4) { // ext_type
156 1852 case 0x1: /* sequence extension */
157
1/2
✓ Branch 0 taken 1852 times.
✗ Branch 1 not taken.
1852 if (bytes_left >= 6) {
158 1852 int horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
159 1852 int vert_size_ext = (buf[2] >> 5) & 3;
160 1852 int bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
161 1852 int frame_rate_ext_n = (buf[5] >> 5) & 3;
162 1852 int frame_rate_ext_d = (buf[5] & 0x1f);
163 1852 pc->progressive_sequence = buf[1] & (1 << 3);
164 1852 avctx->has_b_frames= !(buf[5] >> 7);
165
166
2/4
✓ Branch 0 taken 797 times.
✓ Branch 1 taken 1055 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1852 switch ((buf[1] >> 1) & 3) { // chroma_format
167 797 case 1: pix_fmt = AV_PIX_FMT_YUV420P; break;
168 1055 case 2: pix_fmt = AV_PIX_FMT_YUV422P; break;
169 case 3: pix_fmt = AV_PIX_FMT_YUV444P; break;
170 }
171
172 1852 pc->width = (pc->width & 0xFFF) | (horiz_size_ext << 12);
173 1852 pc->height = (pc->height& 0xFFF) | ( vert_size_ext << 12);
174 1852 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
175
2/2
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 1585 times.
1852 if(did_set_size)
176 267 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
177 1852 avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1);
178 1852 avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1);
179 1852 avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
180 }
181 1852 break;
182 8769 case 0x8: /* picture coding extension */
183
1/2
✓ Branch 0 taken 8769 times.
✗ Branch 1 not taken.
8769 if (bytes_left >= 5) {
184 8769 int top_field_first = buf[3] & (1 << 7);
185 8769 int repeat_first_field = buf[3] & (1 << 1);
186 8769 int progressive_frame = buf[4] & (1 << 7);
187
188 /* check if we must repeat the frame */
189 8769 s->repeat_pict = 1;
190
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8760 times.
8769 if (repeat_first_field) {
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (pc->progressive_sequence) {
192 if (top_field_first)
193 s->repeat_pict = 5;
194 else
195 s->repeat_pict = 3;
196
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 } else if (progressive_frame) {
197 9 s->repeat_pict = 2;
198 }
199 }
200
201
4/4
✓ Branch 0 taken 5956 times.
✓ Branch 1 taken 2813 times.
✓ Branch 2 taken 5682 times.
✓ Branch 3 taken 274 times.
8769 if (!pc->progressive_sequence && !progressive_frame) {
202
2/2
✓ Branch 0 taken 2094 times.
✓ Branch 1 taken 3588 times.
5682 if (top_field_first)
203 2094 s->field_order = AV_FIELD_TT;
204 else
205 3588 s->field_order = AV_FIELD_BB;
206 } else
207 3087 s->field_order = AV_FIELD_PROGRESSIVE;
208
209 8769 s->picture_structure = buf[2] & 3;
210
211
1/2
✓ Branch 0 taken 8769 times.
✗ Branch 1 not taken.
8769 if (!nb_pic_ext) {
212 // remember parity of the first field for the case
213 // when there are 2 fields in packet
214
3/3
✓ Branch 0 taken 2015 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6748 times.
8769 switch (s->picture_structure) {
215 2015 case AV_PICTURE_STRUCTURE_BOTTOM_FIELD: first_field = AV_FIELD_BB; break;
216 6 case AV_PICTURE_STRUCTURE_TOP_FIELD: first_field = AV_FIELD_TT; break;
217 }
218 }
219
220 8769 nb_pic_ext++;
221 }
222 8769 break;
223 }
224 }
225 11806 break;
226 case -1:
227 9670 goto the_end;
228 13430 default:
229 /* we stop parsing when we encounter a slice. It ensures
230 that this function takes a negligible amount of time */
231
1/2
✓ Branch 0 taken 13430 times.
✗ Branch 1 not taken.
13430 if (start_code >= SLICE_MIN_START_CODE &&
232
2/2
✓ Branch 0 taken 9670 times.
✓ Branch 1 taken 3760 times.
13430 start_code <= SLICE_MAX_START_CODE)
233 9670 goto the_end;
234 3760 break;
235 }
236 }
237 274 the_end:
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9944 times.
9944 if (set_dim_ret < 0)
239 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions\n");
240
241
6/6
✓ Branch 0 taken 9042 times.
✓ Branch 1 taken 902 times.
✓ Branch 2 taken 1852 times.
✓ Branch 3 taken 7190 times.
✓ Branch 4 taken 1174 times.
✓ Branch 5 taken 678 times.
9944 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate && bit_rate != 0x3FFFF) {
242 1174 avctx->rc_max_rate = 400LL*bit_rate;
243 }
244
2/2
✓ Branch 0 taken 1991 times.
✓ Branch 1 taken 7953 times.
9944 if (bit_rate &&
245
5/6
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 1852 times.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1070 times.
✓ Branch 5 taken 921 times.
1991 ((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) {
246 1070 avctx->bit_rate = 400LL*bit_rate;
247 }
248
249
2/2
✓ Branch 0 taken 1991 times.
✓ Branch 1 taken 7953 times.
9944 if (pix_fmt != AV_PIX_FMT_NONE) {
250 1991 s->format = pix_fmt;
251 1991 s->width = pc->width;
252 1991 s->height = pc->height;
253 1991 s->coded_width = FFALIGN(pc->width, 16);
254 1991 s->coded_height = FFALIGN(pc->height, 16);
255 }
256
257
3/4
✓ Branch 0 taken 9042 times.
✓ Branch 1 taken 902 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9042 times.
9944 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO || nb_pic_ext > 1) {
258 902 s->repeat_pict = 1;
259 902 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 902 times.
902 s->field_order = nb_pic_ext > 1 ? first_field : AV_FIELD_PROGRESSIVE;
261 }
262 9944 }
263
264 59080 static int mpegvideo_parse(AVCodecParserContext *s,
265 AVCodecContext *avctx,
266 const uint8_t **poutbuf, int *poutbuf_size,
267 const uint8_t *buf, int buf_size)
268 {
269 59080 struct MpvParseContext *pc1 = s->priv_data;
270 59080 ParseContext *pc= &pc1->pc;
271 int next;
272
273
2/2
✓ Branch 0 taken 2708 times.
✓ Branch 1 taken 56372 times.
59080 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
274 2708 next= buf_size;
275 }else{
276 56372 next = mpeg1_find_frame_end(pc, buf, buf_size, s);
277
278
2/2
✓ Branch 1 taken 49136 times.
✓ Branch 2 taken 7236 times.
56372 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
279 49136 *poutbuf = NULL;
280 49136 *poutbuf_size = 0;
281 49136 return buf_size;
282 }
283
284 }
285 /* we have a full frame : we just parse the first few MPEG headers
286 to have the full timing information. The time take by this
287 function should be negligible for uncorrupted streams */
288 9944 mpegvideo_extract_headers(s, avctx, buf, buf_size);
289 ff_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
290 s->pict_type, av_q2d(avctx->framerate), s->repeat_pict);
291
292 9944 *poutbuf = buf;
293 9944 *poutbuf_size = buf_size;
294 9944 return next;
295 }
296
297 687 static av_cold int mpegvideo_parse_init(AVCodecParserContext *s)
298 {
299 687 s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
300 687 return 0;
301 }
302
303 const FFCodecParser ff_mpegvideo_parser = {
304 PARSER_CODEC_LIST(AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO),
305 .priv_data_size = sizeof(struct MpvParseContext),
306 .init = mpegvideo_parse_init,
307 .parse = mpegvideo_parse,
308 .close = ff_parse_close,
309 };
310