avcodec/x86/h264_idct: Fix ff_h264_luma_dc_dequant_idct_sse2 checkasm failures
[ffmpeg.git] / libavcodec / mpegvideo_parser.c
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 static int mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf,
43 int buf_size, AVCodecParserContext *s)
44 {
45 int i;
46 uint32_t state = pc->state;
47
48 /* EOF considered as end of frame */
49 if (buf_size == 0)
50 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 for (i = 0; i < buf_size; i++) {
61 av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
62 if (pc->frame_start_found & 1) {
63 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
64 pc->frame_start_found--;
65 else if (state == EXT_START_CODE + 2) {
66 if ((buf[i] & 3) == 3)
67 pc->frame_start_found = 0;
68 else
69 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
70 }
71 state++;
72 } else {
73 i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
74 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
75 i++;
76 pc->frame_start_found = 4;
77 }
78 if (state == SEQ_END_CODE) {
79 pc->frame_start_found = 0;
80 pc->state = -1;
81 return i + 1;
82 }
83 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
84 pc->frame_start_found = 0;
85 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
86 pc->frame_start_found++;
87 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
88 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
89 pc->frame_start_found = 0;
90 pc->state = -1;
91 return i - 3;
92 }
93 }
94 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
95 ff_fetch_timestamp(s, i - 3, 1, i > 3);
96 }
97 }
98 }
99 pc->state = state;
100 return END_NOT_FOUND;
101 }
102
103 static void mpegvideo_extract_headers(AVCodecParserContext *s,
104 AVCodecContext *avctx,
105 const uint8_t *buf, int buf_size)
106 {
107 struct MpvParseContext *pc = s->priv_data;
108 const uint8_t *buf_end = buf + buf_size;
109 int bytes_left;
110 int did_set_size=0;
111 int set_dim_ret = 0;
112 int bit_rate = 0;
113 int vbv_delay = 0;
114 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 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 int first_field = AV_FIELD_UNKNOWN;
122
123 //FIXME replace the crap with get_bits()
124 while (buf < buf_end) {
125 uint32_t start_code = -1;
126 buf= avpriv_find_start_code(buf, buf_end, &start_code);
127 bytes_left = buf_end - buf;
128 switch(start_code) {
129 case PICTURE_START_CODE:
130 if (bytes_left >= 2) {
131 s->pict_type = (buf[1] >> 3) & 7;
132 if (bytes_left >= 4)
133 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
134 }
135 break;
136 case SEQ_START_CODE:
137 if (bytes_left >= 7) {
138 int frame_rate_index;
139
140 pc->width = (buf[0] << 4) | (buf[1] >> 4);
141 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
142 if(!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height){
143 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
144 did_set_size=1;
145 }
146 pix_fmt = AV_PIX_FMT_YUV420P;
147 frame_rate_index = buf[3] & 0xf;
148 pc->frame_rate = avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_index];
149 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
150 avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
151 }
152 break;
153 case EXT_START_CODE:
154 if (bytes_left >= 1) {
155 switch (buf[0] >> 4) { // ext_type
156 case 0x1: /* sequence extension */
157 if (bytes_left >= 6) {
158 int horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
159 int vert_size_ext = (buf[2] >> 5) & 3;
160 int bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
161 int frame_rate_ext_n = (buf[5] >> 5) & 3;
162 int frame_rate_ext_d = (buf[5] & 0x1f);
163 pc->progressive_sequence = buf[1] & (1 << 3);
164 avctx->has_b_frames= !(buf[5] >> 7);
165
166 switch ((buf[1] >> 1) & 3) { // chroma_format
167 case 1: pix_fmt = AV_PIX_FMT_YUV420P; break;
168 case 2: pix_fmt = AV_PIX_FMT_YUV422P; break;
169 case 3: pix_fmt = AV_PIX_FMT_YUV444P; break;
170 }
171
172 pc->width = (pc->width & 0xFFF) | (horiz_size_ext << 12);
173 pc->height = (pc->height& 0xFFF) | ( vert_size_ext << 12);
174 bit_rate = (bit_rate&0x3FFFF) | (bit_rate_ext << 18);
175 if(did_set_size)
176 set_dim_ret = ff_set_dimensions(avctx, pc->width, pc->height);
177 avctx->framerate.num = pc->frame_rate.num * (frame_rate_ext_n + 1);
178 avctx->framerate.den = pc->frame_rate.den * (frame_rate_ext_d + 1);
179 avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
180 }
181 break;
182 case 0x8: /* picture coding extension */
183 if (bytes_left >= 5) {
184 int top_field_first = buf[3] & (1 << 7);
185 int repeat_first_field = buf[3] & (1 << 1);
186 int progressive_frame = buf[4] & (1 << 7);
187
188 /* check if we must repeat the frame */
189 s->repeat_pict = 1;
190 if (repeat_first_field) {
191 if (pc->progressive_sequence) {
192 if (top_field_first)
193 s->repeat_pict = 5;
194 else
195 s->repeat_pict = 3;
196 } else if (progressive_frame) {
197 s->repeat_pict = 2;
198 }
199 }
200
201 if (!pc->progressive_sequence && !progressive_frame) {
202 if (top_field_first)
203 s->field_order = AV_FIELD_TT;
204 else
205 s->field_order = AV_FIELD_BB;
206 } else
207 s->field_order = AV_FIELD_PROGRESSIVE;
208
209 s->picture_structure = buf[2] & 3;
210
211 if (!nb_pic_ext) {
212 // remember parity of the first field for the case
213 // when there are 2 fields in packet
214 switch (s->picture_structure) {
215 case AV_PICTURE_STRUCTURE_BOTTOM_FIELD: first_field = AV_FIELD_BB; break;
216 case AV_PICTURE_STRUCTURE_TOP_FIELD: first_field = AV_FIELD_TT; break;
217 }
218 }
219
220 nb_pic_ext++;
221 }
222 break;
223 }
224 }
225 break;
226 case -1:
227 goto the_end;
228 default:
229 /* we stop parsing when we encounter a slice. It ensures
230 that this function takes a negligible amount of time */
231 if (start_code >= SLICE_MIN_START_CODE &&
232 start_code <= SLICE_MAX_START_CODE)
233 goto the_end;
234 break;
235 }
236 }
237 the_end:
238 if (set_dim_ret < 0)
239 av_log(avctx, AV_LOG_ERROR, "Failed to set dimensions\n");
240
241 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && bit_rate && bit_rate != 0x3FFFF) {
242 avctx->rc_max_rate = 400LL*bit_rate;
243 }
244 if (bit_rate &&
245 ((avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && bit_rate != 0x3FFFF) || vbv_delay != 0xFFFF)) {
246 avctx->bit_rate = 400LL*bit_rate;
247 }
248
249 if (pix_fmt != AV_PIX_FMT_NONE) {
250 s->format = pix_fmt;
251 s->width = pc->width;
252 s->height = pc->height;
253 s->coded_width = FFALIGN(pc->width, 16);
254 s->coded_height = FFALIGN(pc->height, 16);
255 }
256
257 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO || nb_pic_ext > 1) {
258 s->repeat_pict = 1;
259 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
260 s->field_order = nb_pic_ext > 1 ? first_field : AV_FIELD_PROGRESSIVE;
261 }
262 }
263
264 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 struct MpvParseContext *pc1 = s->priv_data;
270 ParseContext *pc= &pc1->pc;
271 int next;
272
273 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
274 next= buf_size;
275 }else{
276 next = mpeg1_find_frame_end(pc, buf, buf_size, s);
277
278 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
279 *poutbuf = NULL;
280 *poutbuf_size = 0;
281 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 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 *poutbuf = buf;
293 *poutbuf_size = buf_size;
294 return next;
295 }
296
297 static av_cold int mpegvideo_parse_init(AVCodecParserContext *s)
298 {
299 s->pict_type = AV_PICTURE_TYPE_NONE; // first frame might be partial
300 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 };