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 | 54468 | static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, | |
42 | int buf_size, AVCodecParserContext *s) | ||
43 | { | ||
44 | int i; | ||
45 | 54468 | uint32_t state = pc->state; | |
46 | |||
47 | /* EOF considered as end of frame */ | ||
48 |
2/2✓ Branch 0 taken 345 times.
✓ Branch 1 taken 54123 times.
|
54468 | if (buf_size == 0) |
49 | 345 | 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 261104 times.
✓ Branch 1 taken 47539 times.
|
308643 | 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 26709 times.
✓ Branch 1 taken 234395 times.
|
261104 | if (pc->frame_start_found & 1) { |
62 |
4/4✓ Branch 0 taken 10711 times.
✓ Branch 1 taken 15998 times.
✓ Branch 2 taken 2712 times.
✓ Branch 3 taken 7999 times.
|
26709 | if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80) |
63 | 2712 | pc->frame_start_found--; | |
64 |
2/2✓ Branch 0 taken 7999 times.
✓ Branch 1 taken 15998 times.
|
23997 | else if (state == EXT_START_CODE + 2) { |
65 |
2/2✓ Branch 0 taken 4142 times.
✓ Branch 1 taken 3857 times.
|
7999 | if ((buf[i] & 3) == 3) |
66 | 4142 | pc->frame_start_found = 0; | |
67 | else | ||
68 | 3857 | pc->frame_start_found = (pc->frame_start_found + 1) & 3; | |
69 | } | ||
70 | 26709 | state++; | |
71 | } else { | ||
72 | 234395 | i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1; | |
73 |
6/6✓ Branch 0 taken 24776 times.
✓ Branch 1 taken 209619 times.
✓ Branch 2 taken 17768 times.
✓ Branch 3 taken 7008 times.
✓ Branch 4 taken 7029 times.
✓ Branch 5 taken 10739 times.
|
234395 | if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) { |
74 | 7029 | i++; | |
75 | 7029 | pc->frame_start_found = 4; | |
76 | } | ||
77 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 234384 times.
|
234395 | 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 193746 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40638 times.
|
234384 | if (pc->frame_start_found == 2 && state == SEQ_START_CODE) |
83 | ✗ | pc->frame_start_found = 0; | |
84 |
4/4✓ Branch 0 taken 58385 times.
✓ Branch 1 taken 175999 times.
✓ Branch 2 taken 10711 times.
✓ Branch 3 taken 47674 times.
|
234384 | if (pc->frame_start_found < 4 && state == EXT_START_CODE) |
85 | 10711 | pc->frame_start_found++; | |
86 |
4/4✓ Branch 0 taken 175999 times.
✓ Branch 1 taken 58385 times.
✓ Branch 2 taken 136667 times.
✓ Branch 3 taken 39332 times.
|
234384 | if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) { |
87 |
4/4✓ Branch 0 taken 130535 times.
✓ Branch 1 taken 6132 times.
✓ Branch 2 taken 441 times.
✓ Branch 3 taken 130094 times.
|
136667 | if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) { |
88 | 6573 | pc->frame_start_found = 0; | |
89 | 6573 | pc->state = -1; | |
90 | 6573 | return i - 3; | |
91 | } | ||
92 | } | ||
93 |
5/6✓ Branch 0 taken 9937 times.
✓ Branch 1 taken 217874 times.
✓ Branch 2 taken 9937 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7007 times.
✓ Branch 5 taken 2930 times.
|
227811 | if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) { |
94 | 7007 | ff_fetch_timestamp(s, i - 3, 1, i > 3); | |
95 | } | ||
96 | } | ||
97 | } | ||
98 | 47539 | pc->state = state; | |
99 | 47539 | return END_NOT_FOUND; | |
100 | } | ||
101 | |||
102 | 9636 | static void mpegvideo_extract_headers(AVCodecParserContext *s, | |
103 | AVCodecContext *avctx, | ||
104 | const uint8_t *buf, int buf_size) | ||
105 | { | ||
106 | 9636 | struct MpvParseContext *pc = s->priv_data; | |
107 | 9636 | const uint8_t *buf_end = buf + buf_size; | |
108 | int bytes_left; | ||
109 | 9636 | int did_set_size=0; | |
110 | 9636 | int set_dim_ret = 0; | |
111 | 9636 | int bit_rate = 0; | |
112 | 9636 | int vbv_delay = 0; | |
113 | 9636 | 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 | 9636 | 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 | 9636 | int first_field = AV_FIELD_UNKNOWN; | |
121 | |||
122 | //FIXME replace the crap with get_bits() | ||
123 |
2/2✓ Branch 0 taken 35392 times.
✓ Branch 1 taken 272 times.
|
35664 | while (buf < buf_end) { |
124 | 35392 | uint32_t start_code = -1; | |
125 | 35392 | buf= avpriv_find_start_code(buf, buf_end, &start_code); | |
126 | 35392 | bytes_left = buf_end - buf; | |
127 |
4/5✓ Branch 0 taken 9337 times.
✓ Branch 1 taken 1980 times.
✓ Branch 2 taken 11412 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12663 times.
|
35392 | switch(start_code) { |
128 | 9337 | case PICTURE_START_CODE: | |
129 |
1/2✓ Branch 0 taken 9337 times.
✗ Branch 1 not taken.
|
9337 | if (bytes_left >= 2) { |
130 | 9337 | s->pict_type = (buf[1] >> 3) & 7; | |
131 |
1/2✓ Branch 0 taken 9337 times.
✗ Branch 1 not taken.
|
9337 | if (bytes_left >= 4) |
132 | 9337 | vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3); | |
133 | } | ||
134 | 9337 | break; | |
135 | 1980 | case SEQ_START_CODE: | |
136 |
1/2✓ Branch 0 taken 1980 times.
✗ Branch 1 not taken.
|
1980 | if (bytes_left >= 7) { |
137 | int frame_rate_index; | ||
138 | |||
139 | 1980 | pc->width = (buf[0] << 4) | (buf[1] >> 4); | |
140 | 1980 | pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
141 |
6/8✓ Branch 0 taken 1885 times.
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 1885 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1698 times.
✓ Branch 5 taken 187 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1698 times.
|
1980 | 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 | 1980 | pix_fmt = AV_PIX_FMT_YUV420P; | |
146 | 1980 | frame_rate_index = buf[3] & 0xf; | |
147 | 1980 | pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index]; | |
148 | 1980 | bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6); | |
149 | 1980 | avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO; | |
150 | } | ||
151 | 1980 | break; | |
152 | 11412 | case EXT_START_CODE: | |
153 |
1/2✓ Branch 0 taken 11412 times.
✗ Branch 1 not taken.
|
11412 | if (bytes_left >= 1) { |
154 |
3/3✓ Branch 0 taken 1841 times.
✓ Branch 1 taken 8465 times.
✓ Branch 2 taken 1106 times.
|
11412 | switch (buf[0] >> 4) { // ext_type |
155 | 1841 | case 0x1: /* sequence extension */ | |
156 |
1/2✓ Branch 0 taken 1841 times.
✗ Branch 1 not taken.
|
1841 | if (bytes_left >= 6) { |
157 | 1841 | int horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
158 | 1841 | int vert_size_ext = (buf[2] >> 5) & 3; | |
159 | 1841 | int bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); | |
160 | 1841 | int frame_rate_ext_n = (buf[5] >> 5) & 3; | |
161 | 1841 | int frame_rate_ext_d = (buf[5] & 0x1f); | |
162 | 1841 | pc->progressive_sequence = buf[1] & (1 << 3); | |
163 | 1841 | avctx->has_b_frames= !(buf[5] >> 7); | |
164 | |||
165 |
2/4✓ Branch 0 taken 786 times.
✓ Branch 1 taken 1055 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1841 | switch ((buf[1] >> 1) & 3) { // chroma_format |
166 | 786 | case 1: pix_fmt = AV_PIX_FMT_YUV420P; break; | |
167 | 1055 | case 2: pix_fmt = AV_PIX_FMT_YUV422P; break; | |
168 | ✗ | case 3: pix_fmt = AV_PIX_FMT_YUV444P; break; | |
169 | } | ||
170 | |||
171 | 1841 | pc->width = (pc->width & 0xFFF) | (horiz_size_ext << 12); | |
172 | 1841 | pc->height = (pc->height& 0xFFF) | ( vert_size_ext << 12); | |
173 | 1841 | bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18); | |
174 |
2/2✓ Branch 0 taken 263 times.
✓ Branch 1 taken 1578 times.
|
1841 | if(did_set_size) |
175 | 263 | set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height); | |
176 | 1841 | avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1); | |
177 | 1841 | avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1); | |
178 | 1841 | avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO; | |
179 | } | ||
180 | 1841 | break; | |
181 | 8465 | case 0x8: /* picture coding extension */ | |
182 |
1/2✓ Branch 0 taken 8465 times.
✗ Branch 1 not taken.
|
8465 | if (bytes_left >= 5) { |
183 | 8465 | int top_field_first = buf[3] & (1 << 7); | |
184 | 8465 | int repeat_first_field = buf[3] & (1 << 1); | |
185 | 8465 | int progressive_frame = buf[4] & (1 << 7); | |
186 | |||
187 | /* check if we must repeat the frame */ | ||
188 | 8465 | s->repeat_pict = 1; | |
189 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8456 times.
|
8465 | if (repeat_first_field) { |
190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | 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 9 times.
✗ Branch 1 not taken.
|
9 | } else if (progressive_frame) { |
196 | 9 | s->repeat_pict = 2; | |
197 | } | ||
198 | } | ||
199 | |||
200 |
4/4✓ Branch 0 taken 5652 times.
✓ Branch 1 taken 2813 times.
✓ Branch 2 taken 5378 times.
✓ Branch 3 taken 274 times.
|
8465 | if (!pc->progressive_sequence && !progressive_frame) { |
201 |
2/2✓ Branch 0 taken 1870 times.
✓ Branch 1 taken 3508 times.
|
5378 | if (top_field_first) |
202 | 1870 | s->field_order = AV_FIELD_TT; | |
203 | else | ||
204 | 3508 | s->field_order = AV_FIELD_BB; | |
205 | } else | ||
206 | 3087 | s->field_order = AV_FIELD_PROGRESSIVE; | |
207 | |||
208 | 8465 | s->picture_structure = buf[2] & 3; | |
209 | |||
210 |
1/2✓ Branch 0 taken 8465 times.
✗ Branch 1 not taken.
|
8465 | if (!nb_pic_ext) { |
211 | // remember parity of the first field for the case | ||
212 | // when there are 2 fields in packet | ||
213 |
3/3✓ Branch 0 taken 1935 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6524 times.
|
8465 | switch (s->picture_structure) { |
214 | 1935 | case AV_PICTURE_STRUCTURE_BOTTOM_FIELD: first_field = AV_FIELD_BB; break; | |
215 | 6 | case AV_PICTURE_STRUCTURE_TOP_FIELD: first_field = AV_FIELD_TT; break; | |
216 | } | ||
217 | } | ||
218 | |||
219 | 8465 | nb_pic_ext++; | |
220 | } | ||
221 | 8465 | break; | |
222 | } | ||
223 | } | ||
224 | 11412 | break; | |
225 | ✗ | case -1: | |
226 | 9364 | goto the_end; | |
227 | 12663 | default: | |
228 | /* we stop parsing when we encounter a slice. It ensures | ||
229 | that this function takes a negligible amount of time */ | ||
230 |
1/2✓ Branch 0 taken 12663 times.
✗ Branch 1 not taken.
|
12663 | if (start_code >= SLICE_MIN_START_CODE && |
231 |
2/2✓ Branch 0 taken 9364 times.
✓ Branch 1 taken 3299 times.
|
12663 | start_code <= SLICE_MAX_START_CODE) |
232 | 9364 | goto the_end; | |
233 | 3299 | break; | |
234 | } | ||
235 | } | ||
236 | 272 | the_end: | |
237 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9636 times.
|
9636 | if (set_dim_ret < 0) |
238 | ✗ | av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions\n"); | |
239 | |||
240 |
6/6✓ Branch 0 taken 8734 times.
✓ Branch 1 taken 902 times.
✓ Branch 2 taken 1841 times.
✓ Branch 3 taken 6893 times.
✓ Branch 4 taken 1163 times.
✓ Branch 5 taken 678 times.
|
9636 | if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate && bit_rate != 0x3FFFF) { |
241 | 1163 | avctx->rc_max_rate = 400LL*bit_rate; | |
242 | } | ||
243 |
2/2✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 7656 times.
|
9636 | if (bit_rate && |
244 |
5/6✓ Branch 0 taken 139 times.
✓ Branch 1 taken 1841 times.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1065 times.
✓ Branch 5 taken 915 times.
|
1980 | ((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) { |
245 | 1065 | avctx->bit_rate = 400LL*bit_rate; | |
246 | } | ||
247 | |||
248 |
2/2✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 7656 times.
|
9636 | if (pix_fmt != AV_PIX_FMT_NONE) { |
249 | 1980 | s->format = pix_fmt; | |
250 | 1980 | s->width = pc->width; | |
251 | 1980 | s->height = pc->height; | |
252 | 1980 | s->coded_width = FFALIGN(pc->width, 16); | |
253 | 1980 | s->coded_height = FFALIGN(pc->height, 16); | |
254 | } | ||
255 | |||
256 |
3/4✓ Branch 0 taken 8734 times.
✓ Branch 1 taken 902 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8734 times.
|
9636 | if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO || nb_pic_ext > 1) { |
257 | 902 | s->repeat_pict = 1; | |
258 | 902 | s->picture_structure = AV_PICTURE_STRUCTURE_FRAME; | |
259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 902 times.
|
902 | s->field_order = nb_pic_ext > 1 ? first_field : AV_FIELD_PROGRESSIVE; |
260 | } | ||
261 | 9636 | } | |
262 | |||
263 | 57175 | static int mpegvideo_parse(AVCodecParserContext *s, | |
264 | AVCodecContext *avctx, | ||
265 | const uint8_t **poutbuf, int *poutbuf_size, | ||
266 | const uint8_t *buf, int buf_size) | ||
267 | { | ||
268 | 57175 | struct MpvParseContext *pc1 = s->priv_data; | |
269 | 57175 | ParseContext *pc= &pc1->pc; | |
270 | int next; | ||
271 | |||
272 |
2/2✓ Branch 0 taken 2707 times.
✓ Branch 1 taken 54468 times.
|
57175 | if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ |
273 | 2707 | next= buf_size; | |
274 | }else{ | ||
275 | 54468 | next = mpeg1_find_frame_end(pc, buf, buf_size, s); | |
276 | |||
277 |
2/2✓ Branch 1 taken 47539 times.
✓ Branch 2 taken 6929 times.
|
54468 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
278 | 47539 | *poutbuf = NULL; | |
279 | 47539 | *poutbuf_size = 0; | |
280 | 47539 | return buf_size; | |
281 | } | ||
282 | |||
283 | } | ||
284 | /* we have a full frame : we just parse the first few MPEG headers | ||
285 | to have the full timing information. The time take by this | ||
286 | function should be negligible for uncorrupted streams */ | ||
287 | 9636 | mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
288 | ff_dlog(NULL, "pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", | ||
289 | s->pict_type, av_q2d(avctx->framerate), s->repeat_pict); | ||
290 | |||
291 | 9636 | *poutbuf = buf; | |
292 | 9636 | *poutbuf_size = buf_size; | |
293 | 9636 | return next; | |
294 | } | ||
295 | |||
296 | 679 | static int mpegvideo_parse_init(AVCodecParserContext *s) | |
297 | { | ||
298 | 679 | s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial | |
299 | 679 | return 0; | |
300 | } | ||
301 | |||
302 | const AVCodecParser ff_mpegvideo_parser = { | ||
303 | .codec_ids = { AV_CODEC_ID_MPEG1VIDEO, AV_CODEC_ID_MPEG2VIDEO }, | ||
304 | .priv_data_size = sizeof(struct MpvParseContext), | ||
305 | .parser_init = mpegvideo_parse_init, | ||
306 | .parser_parse = mpegvideo_parse, | ||
307 | .parser_close = ff_parse_close, | ||
308 | }; | ||
309 |