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
28
34 };
35
36 #if !FF_API_FLAG_TRUNCATED
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 static int mpeg1_find_frame_end(
ParseContext *pc,
const uint8_t *buf,
43 {
46
47 /* EOF considered as end of frame */
48 if (buf_size == 0)
49 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 for (
i = 0;
i < buf_size;
i++) {
65 if ((buf[
i] & 3) == 3)
67 else
69 }
71 } else {
76 }
81 }
91 }
92 }
95 }
96 }
97 }
100 }
101 #endif
102
105 const uint8_t *buf, int buf_size)
106 {
108 const uint8_t *buf_end = buf + buf_size;
110 int frame_rate_index, ext_type, bytes_left;
111 int frame_rate_ext_n, frame_rate_ext_d;
112 int top_field_first, repeat_first_field, progressive_frame;
113 int horiz_size_ext, vert_size_ext, bit_rate_ext;
114 int did_set_size=0;
115 int set_dim_ret = 0;
116 int bit_rate = 0;
117 int vbv_delay = 0;
118 int chroma_format;
120 //FIXME replace the crap with get_bits()
122
123 while (buf < buf_end) {
126 bytes_left = buf_end - buf;
129 if (bytes_left >= 2) {
130 s->pict_type = (buf[1] >> 3) & 7;
131 if (bytes_left >= 4)
132 vbv_delay = ((buf[1] & 0x07) << 13) | (buf[2] << 5) | (buf[3] >> 3);
133 }
134 break;
136 if (bytes_left >= 7) {
137 pc->width = (buf[0] << 4) | (buf[1] >> 4);
138 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
141 did_set_size=1;
142 }
144 frame_rate_index = buf[3] & 0xf;
146 bit_rate = (buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6);
149 }
150 break;
152 if (bytes_left >= 1) {
153 ext_type = (buf[0] >> 4);
154 switch(ext_type) {
155 case 0x1: /* sequence extension */
156 if (bytes_left >= 6) {
157 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
158 vert_size_ext = (buf[2] >> 5) & 3;
159 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
160 frame_rate_ext_n = (buf[5] >> 5) & 3;
161 frame_rate_ext_d = (buf[5] & 0x1f);
162 pc->progressive_sequence = buf[1] & (1 << 3);
164
165 chroma_format = (buf[1] >> 1) & 3;
166 switch (chroma_format) {
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)
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);
181 }
182 break;
183 case 0x8: /* picture coding extension */
184 if (bytes_left >= 5) {
185 top_field_first = buf[3] & (1 << 7);
186 repeat_first_field = buf[3] & (1 << 1);
187 progressive_frame = buf[4] & (1 << 7);
188
189 /* check if we must repeat the frame */
191 if (repeat_first_field) {
192 if (
pc->progressive_sequence) {
193 if (top_field_first)
195 else
197 } else if (progressive_frame) {
199 }
200 }
201
202 if (!
pc->progressive_sequence && !progressive_frame) {
203 if (top_field_first)
205 else
207 } else
209 }
210 break;
211 }
212 }
213 break;
214 case -1:
215 goto the_end;
216 default:
217 /* we stop parsing when we encounter a slice. It ensures
218 that this function takes a negligible amount of time */
221 goto the_end;
222 break;
223 }
224 }
225 the_end:
226 if (set_dim_ret < 0)
228
231 }
232 if (bit_rate &&
235 }
236
239 s->width =
pc->width;
240 s->height =
pc->height;
243 }
244
245 #if FF_API_AVCTX_TIMEBASE
248 #endif
249 }
250
253 const uint8_t **poutbuf, int *poutbuf_size,
254 const uint8_t *buf, int buf_size)
255 {
258 int next;
259
261 next= buf_size;
262 }else{
263 #if FF_API_FLAG_TRUNCATED
265 #else
266 next = mpeg1_find_frame_end(
pc, buf, buf_size,
s);
267 #endif
268
271 *poutbuf_size = 0;
272 return buf_size;
273 }
274
275 }
276 /* we have a full frame : we just parse the first few MPEG headers
277 to have the full timing information. The time take by this
278 function should be negligible for uncorrupted streams */
280 ff_dlog(
NULL,
"pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
282
283 *poutbuf = buf;
284 *poutbuf_size = buf_size;
285 return next;
286 }
287
289 {
291 return 0;
292 }
293
300 };