FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_parser.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 114 121 94.2%
Branches: 58 76 76.3%

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 "parser.h"
24 #include "mpeg12.h"
25 #include "internal.h"
26
27 struct MpvParseContext {
28 ParseContext pc;
29 AVRational frame_rate;
30 int progressive_sequence;
31 int width, height;
32 };
33
34 #if !FF_API_FLAG_TRUNCATED
35 /**
36 * Find the end of the current frame in the bitstream.
37 * @return the position of the first byte of the next frame, or -1
38 */
39 static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf,
40 int buf_size, AVCodecParserContext *s)
41 {
42 int i;
43 uint32_t state = pc->state;
44
45 /* EOF considered as end of frame */
46 if (buf_size == 0)
47 return 0;
48
49 /*
50 0 frame start -> 1/4
51 1 first_SEQEXT -> 0/2
52 2 first field start -> 3/0
53 3 second_SEQEXT -> 2/0
54 4 searching end
55 */
56
57 for (i = 0; i < buf_size; i++) {
58 av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
59 if (pc->frame_start_found & 1) {
60 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
61 pc->frame_start_found--;
62 else if (state == EXT_START_CODE + 2) {
63 if ((buf[i] & 3) == 3)
64 pc->frame_start_found = 0;
65 else
66 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
67 }
68 state++;
69 } else {
70 i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
71 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
72 i++;
73 pc->frame_start_found = 4;
74 }
75 if (state == SEQ_END_CODE) {
76 pc->frame_start_found = 0;
77 pc->state = -1;
78 return i + 1;
79 }
80 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
81 pc->frame_start_found = 0;
82 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
83 pc->frame_start_found++;
84 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
85 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
86 pc->frame_start_found = 0;
87 pc->state = -1;
88 return i - 3;
89 }
90 }
91 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
92 ff_fetch_timestamp(s, i - 3, 1, i > 3);
93 }
94 }
95 }
96 pc->state = state;
97 return END_NOT_FOUND;
98 }
99 #endif
100
101 8443 static void mpegvideo_extract_headers(AVCodecParserContext *s,
102 AVCodecContext *avctx,
103 const uint8_t *buf, int buf_size)
104 {
105 8443 struct MpvParseContext *pc = s->priv_data;
106 8443 const uint8_t *buf_end = buf + buf_size;
107 uint32_t start_code;
108 int frame_rate_index, ext_type, bytes_left;
109 int frame_rate_ext_n, frame_rate_ext_d;
110 int top_field_first, repeat_first_field, progressive_frame;
111 int horiz_size_ext, vert_size_ext, bit_rate_ext;
112 8443 int did_set_size=0;
113 8443 int set_dim_ret = 0;
114 8443 int bit_rate = 0;
115 8443 int vbv_delay = 0;
116 int chroma_format;
117 8443 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
118 //FIXME replace the crap with get_bits()
119 8443 s->repeat_pict = 0;
120
121
2/2
✓ Branch 0 taken 30823 times.
✓ Branch 1 taken 250 times.
31073 while (buf < buf_end) {
122 30823 start_code= -1;
123 30823 buf= avpriv_find_start_code(buf, buf_end, &start_code);
124 30823 bytes_left = buf_end - buf;
125
4/5
✓ Branch 0 taken 8176 times.
✓ Branch 1 taken 1904 times.
✓ Branch 2 taken 9697 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11046 times.
30823 switch(start_code) {
126 8176 case PICTURE_START_CODE:
127
1/2
✓ Branch 0 taken 8176 times.
✗ Branch 1 not taken.
8176 if (bytes_left >= 2) {
128 8176 s->pict_type = (buf[1] >> 3) & 7;
129
1/2
✓ Branch 0 taken 8176 times.
✗ Branch 1 not taken.
8176 if (bytes_left >= 4)
130 8176 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
131 }
132 8176 break;
133 1904 case SEQ_START_CODE:
134
1/2
✓ Branch 0 taken 1904 times.
✗ Branch 1 not taken.
1904 if (bytes_left >= 7) {
135 1904 pc->width = (buf[0] << 4) | (buf[1] >> 4);
136 1904 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
137
6/8
✓ Branch 0 taken 1819 times.
✓ Branch 1 taken 85 times.
✓ Branch 2 taken 1819 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1738 times.
✓ Branch 5 taken 81 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1738 times.
1904 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
138 166 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
139 166 did_set_size=1;
140 }
141 1904 pix_fmt = AV_PIX_FMT_YUV420P;
142 1904 frame_rate_index = buf[3] & 0xf;
143 1904 pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index];
144 1904 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
145 1904 avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
146 1904 avctx->ticks_per_frame = 1;
147 }
148 1904 break;
149 9697 case EXT_START_CODE:
150
1/2
✓ Branch 0 taken 9697 times.
✗ Branch 1 not taken.
9697 if (bytes_left >= 1) {
151
3/3
✓ Branch 0 taken 1765 times.
✓ Branch 1 taken 7304 times.
✓ Branch 2 taken 628 times.
9697 ext_type = (buf[0] >> 4);
152 switch(ext_type) {
153 1765 case 0x1: /* sequence extension */
154
1/2
✓ Branch 0 taken 1765 times.
✗ Branch 1 not taken.
1765 if (bytes_left >= 6) {
155 1765 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
156 1765 vert_size_ext = (buf[2] >> 5) & 3;
157 1765 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
158 1765 frame_rate_ext_n = (buf[5] >> 5) & 3;
159 1765 frame_rate_ext_d = (buf[5] & 0x1f);
160 1765 pc->progressive_sequence = buf[1] & (1 << 3);
161 1765 avctx->has_b_frames= !(buf[5] >> 7);
162
163
2/4
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 1037 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1765 chroma_format = (buf[1] >> 1) & 3;
164 switch (chroma_format) {
165 728 case 1: pix_fmt = AV_PIX_FMT_YUV420P; break;
166 1037 case 2: pix_fmt = AV_PIX_FMT_YUV422P; break;
167 case 3: pix_fmt = AV_PIX_FMT_YUV444P; break;
168 }
169
170 1765 pc->width = (pc->width & 0xFFF) | (horiz_size_ext << 12);
171 1765 pc->height = (pc->height& 0xFFF) | ( vert_size_ext << 12);
172 1765 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
173
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 1613 times.
1765 if(did_set_size)
174 152 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
175 1765 avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1);
176 1765 avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1);
177 1765 avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
178 1765 avctx->ticks_per_frame = 2;
179 }
180 1765 break;
181 7304 case 0x8: /* picture coding extension */
182
1/2
✓ Branch 0 taken 7304 times.
✗ Branch 1 not taken.
7304 if (bytes_left >= 5) {
183 7304 top_field_first = buf[3] & (1 << 7);
184 7304 repeat_first_field = buf[3] & (1 << 1);
185 7304 progressive_frame = buf[4] & (1 << 7);
186
187 /* check if we must repeat the frame */
188 7304 s->repeat_pict = 1;
189
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7298 times.
7304 if (repeat_first_field) {
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pc->progressive_sequence) {
191 if (top_field_first)
192 s->repeat_pict = 5;
193 else
194 s->repeat_pict = 3;
195
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 } else if (progressive_frame) {
196 6 s->repeat_pict = 2;
197 }
198 }
199
200
4/4
✓ Branch 0 taken 4197 times.
✓ Branch 1 taken 3107 times.
✓ Branch 2 taken 3926 times.
✓ Branch 3 taken 271 times.
7304 if (!pc->progressive_sequence && !progressive_frame) {
201
2/2
✓ Branch 0 taken 591 times.
✓ Branch 1 taken 3335 times.
3926 if (top_field_first)
202 591 s->field_order = AV_FIELD_TT;
203 else
204 3335 s->field_order = AV_FIELD_BB;
205 } else
206 3378 s->field_order = AV_FIELD_PROGRESSIVE;
207 }
208 7304 break;
209 }
210 }
211 9697 break;
212 case -1:
213 goto the_end;
214 11046 default:
215 /* we stop parsing when we encounter a slice. It ensures
216 that this function takes a negligible amount of time */
217
1/2
✓ Branch 0 taken 11046 times.
✗ Branch 1 not taken.
11046 if (start_code >= SLICE_MIN_START_CODE &&
218
2/2
✓ Branch 0 taken 8193 times.
✓ Branch 1 taken 2853 times.
11046 start_code <= SLICE_MAX_START_CODE)
219 8193 goto the_end;
220 2853 break;
221 }
222 }
223 250 the_end:
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8443 times.
8443 if (set_dim_ret < 0)
225 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions\n");
226
227
4/4
✓ Branch 0 taken 7540 times.
✓ Branch 1 taken 903 times.
✓ Branch 2 taken 1765 times.
✓ Branch 3 taken 5775 times.
8443 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate) {
228 1765 avctx->rc_max_rate = 400LL*bit_rate;
229 }
230
2/2
✓ Branch 0 taken 1904 times.
✓ Branch 1 taken 6539 times.
8443 if (bit_rate &&
231
5/6
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 1765 times.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1015 times.
✓ Branch 5 taken 889 times.
1904 ((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) {
232 1015 avctx->bit_rate = 400LL*bit_rate;
233 }
234
235
2/2
✓ Branch 0 taken 1904 times.
✓ Branch 1 taken 6539 times.
8443 if (pix_fmt != AV_PIX_FMT_NONE) {
236 1904 s->format = pix_fmt;
237 1904 s->width = pc->width;
238 1904 s->height = pc->height;
239 1904 s->coded_width = FFALIGN(pc->width, 16);
240 1904 s->coded_height = FFALIGN(pc->height, 16);
241 }
242
243 #if FF_API_AVCTX_TIMEBASE
244
2/2
✓ Branch 0 taken 8160 times.
✓ Branch 1 taken 283 times.
8443 if (avctx->framerate.num)
245 8160 avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
246 #endif
247 8443 }
248
249 44548 static int mpegvideo_parse(AVCodecParserContext *s,
250 AVCodecContext *avctx,
251 const uint8_t **poutbuf, int *poutbuf_size,
252 const uint8_t *buf, int buf_size)
253 {
254 44548 struct MpvParseContext *pc1 = s->priv_data;
255 44548 ParseContext *pc= &pc1->pc;
256 int next;
257
258
2/2
✓ Branch 0 taken 2775 times.
✓ Branch 1 taken 41773 times.
44548 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
259 2775 next= buf_size;
260 }else{
261 #if FF_API_FLAG_TRUNCATED
262 41773 next= ff_mpeg1_find_frame_end(pc, buf, buf_size, s);
263 #else
264 next = mpeg1_find_frame_end(pc, buf, buf_size, s);
265 #endif
266
267
2/2
✓ Branch 1 taken 36105 times.
✓ Branch 2 taken 5668 times.
41773 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
268 36105 *poutbuf = NULL;
269 36105 *poutbuf_size = 0;
270 36105 return buf_size;
271 }
272
273 }
274 /* we have a full frame : we just parse the first few MPEG headers
275 to have the full timing information. The time take by this
276 function should be negligible for uncorrupted streams */
277 8443 mpegvideo_extract_headers(s, avctx, buf, buf_size);
278 ff_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
279 s->pict_type, av_q2d(avctx->framerate), s->repeat_pict);
280
281 8443 *poutbuf = buf;
282 8443 *poutbuf_size = buf_size;
283 8443 return next;
284 }
285
286 655 static int mpegvideo_parse_init(AVCodecParserContext *s)
287 {
288 655 s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
289 655 return 0;
290 }
291
292 const AVCodecParser ff_mpegvideo_parser = {
293 .codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO },
294 .priv_data_size = sizeof(struct MpvParseContext),
295 .parser_init = mpegvideo_parse_init,
296 .parser_parse = mpegvideo_parse,
297 .parser_close = ff_parse_close,
298 };
299