Mercurial > libavcodec.hg
annotate parser.c @ 2769:1394b45a7bf4 libavcodec
support changing in bitstream global headers into extradata style and back
author | michael |
---|---|
date | Mon, 27 Jun 2005 00:04:03 +0000 |
parents | ef44d24680d1 |
children | 09108466b7d0 |
rev | line source |
---|---|
1613 | 1 /* |
2 * Audio and Video frame extraction | |
3 * Copyright (c) 2003 Fabrice Bellard. | |
4 * Copyright (c) 2003 Michael Niedermayer. | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 */ | |
20 #include "avcodec.h" | |
21 #include "mpegvideo.h" | |
22 #include "mpegaudio.h" | |
23 | |
24 AVCodecParser *av_first_parser = NULL; | |
25 | |
26 void av_register_codec_parser(AVCodecParser *parser) | |
27 { | |
28 parser->next = av_first_parser; | |
29 av_first_parser = parser; | |
30 } | |
31 | |
32 AVCodecParserContext *av_parser_init(int codec_id) | |
33 { | |
34 AVCodecParserContext *s; | |
35 AVCodecParser *parser; | |
36 int ret; | |
2486
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
37 |
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
38 if(codec_id == CODEC_ID_NONE) |
f2a9559db6ac
10l (array gets padded with 0 which is CODEC_ID_NONE -> parsers claim to support CODEC_ID_NONE)
michael
parents:
2480
diff
changeset
|
39 return NULL; |
1613 | 40 |
41 for(parser = av_first_parser; parser != NULL; parser = parser->next) { | |
42 if (parser->codec_ids[0] == codec_id || | |
43 parser->codec_ids[1] == codec_id || | |
2348 | 44 parser->codec_ids[2] == codec_id || |
45 parser->codec_ids[3] == codec_id || | |
46 parser->codec_ids[4] == codec_id) | |
1613 | 47 goto found; |
48 } | |
49 return NULL; | |
50 found: | |
51 s = av_mallocz(sizeof(AVCodecParserContext)); | |
52 if (!s) | |
53 return NULL; | |
54 s->parser = parser; | |
55 s->priv_data = av_mallocz(parser->priv_data_size); | |
56 if (!s->priv_data) { | |
57 av_free(s); | |
58 return NULL; | |
59 } | |
60 if (parser->parser_init) { | |
61 ret = parser->parser_init(s); | |
62 if (ret != 0) { | |
63 av_free(s->priv_data); | |
64 av_free(s); | |
65 return NULL; | |
66 } | |
67 } | |
2030 | 68 s->fetch_timestamp=1; |
1613 | 69 return s; |
70 } | |
71 | |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
72 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
73 can be returned if necessary */ |
1613 | 74 int av_parser_parse(AVCodecParserContext *s, |
75 AVCodecContext *avctx, | |
76 uint8_t **poutbuf, int *poutbuf_size, | |
1696 | 77 const uint8_t *buf, int buf_size, |
78 int64_t pts, int64_t dts) | |
1613 | 79 { |
1696 | 80 int index, i, k; |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
81 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
82 |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
83 if (buf_size == 0) { |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
84 /* padding is always necessary even if EOF, so we add it here */ |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
85 memset(dummy_buf, 0, sizeof(dummy_buf)); |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
86 buf = dummy_buf; |
1696 | 87 } else { |
88 /* add a new packet descriptor */ | |
89 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
90 s->cur_frame_start_index = k; | |
91 s->cur_frame_offset[k] = s->cur_offset; | |
92 s->cur_frame_pts[k] = pts; | |
93 s->cur_frame_dts[k] = dts; | |
94 | |
95 /* fill first PTS/DTS */ | |
2030 | 96 if (s->fetch_timestamp){ |
97 s->fetch_timestamp=0; | |
1696 | 98 s->last_pts = pts; |
99 s->last_dts = dts; | |
2107 | 100 s->cur_frame_pts[k] = |
101 s->cur_frame_dts[k] = AV_NOPTS_VALUE; | |
1696 | 102 } |
1694
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
103 } |
13169235c306
added End Of File handling to return last picture for MPEG1/2/4
bellard
parents:
1681
diff
changeset
|
104 |
1613 | 105 /* WARNING: the returned index can be negative */ |
106 index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size); | |
2107 | 107 //av_log(NULL, AV_LOG_DEBUG, "parser: in:%lld, %lld, out:%lld, %lld, in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id); |
1613 | 108 /* update the file pointer */ |
109 if (*poutbuf_size) { | |
1696 | 110 /* fill the data for the current frame */ |
1613 | 111 s->frame_offset = s->last_frame_offset; |
1696 | 112 s->pts = s->last_pts; |
113 s->dts = s->last_dts; | |
114 | |
115 /* offset of the next frame */ | |
1613 | 116 s->last_frame_offset = s->cur_offset + index; |
1696 | 117 /* find the packet in which the new frame starts. It |
118 is tricky because of MPEG video start codes | |
119 which can begin in one packet and finish in | |
120 another packet. In the worst case, an MPEG | |
121 video start code could be in 4 different | |
122 packets. */ | |
123 k = s->cur_frame_start_index; | |
124 for(i = 0; i < AV_PARSER_PTS_NB; i++) { | |
125 if (s->last_frame_offset >= s->cur_frame_offset[k]) | |
126 break; | |
127 k = (k - 1) & (AV_PARSER_PTS_NB - 1); | |
128 } | |
2030 | 129 |
1696 | 130 s->last_pts = s->cur_frame_pts[k]; |
131 s->last_dts = s->cur_frame_dts[k]; | |
2030 | 132 |
133 /* some parsers tell us the packet size even before seeing the first byte of the next packet, | |
134 so the next pts/dts is in the next chunk */ | |
135 if(index == buf_size){ | |
136 s->fetch_timestamp=1; | |
137 } | |
1613 | 138 } |
139 if (index < 0) | |
140 index = 0; | |
141 s->cur_offset += index; | |
142 return index; | |
143 } | |
144 | |
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
145 int av_parser_change(AVCodecParserContext *s, |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
146 AVCodecContext *avctx, |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
147 uint8_t **poutbuf, int *poutbuf_size, |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
148 const uint8_t *buf, int buf_size, int keyframe){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
149 |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
150 if(s && s->parser->split){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
151 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) && !(avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
152 int i= s->parser->split(avctx, buf, buf_size); |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
153 buf += i; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
154 buf_size -= i; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
155 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
156 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
157 |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
158 *poutbuf= buf; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
159 *poutbuf_size= buf_size; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
160 if(avctx->extradata){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
161 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
162 /*||(s->pict_type != I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
163 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
164 int size= buf_size + avctx->extradata_size; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
165 *poutbuf_size= size; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
166 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
167 |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
168 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size); |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
169 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size); |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
170 return 1; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
171 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
172 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
173 |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
174 return 0; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
175 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
176 |
1613 | 177 void av_parser_close(AVCodecParserContext *s) |
178 { | |
179 if (s->parser->parser_close) | |
180 s->parser->parser_close(s); | |
181 av_free(s->priv_data); | |
182 av_free(s); | |
183 } | |
184 | |
185 /*****************************************************/ | |
186 | |
187 //#define END_NOT_FOUND (-100) | |
188 | |
189 #define PICTURE_START_CODE 0x00000100 | |
190 #define SEQ_START_CODE 0x000001b3 | |
191 #define EXT_START_CODE 0x000001b5 | |
192 #define SLICE_MIN_START_CODE 0x00000101 | |
193 #define SLICE_MAX_START_CODE 0x000001af | |
194 | |
195 typedef struct ParseContext1{ | |
1988 | 196 ParseContext pc; |
197 /* XXX/FIXME PC1 vs. PC */ | |
1613 | 198 /* MPEG2 specific */ |
199 int frame_rate; | |
200 int progressive_sequence; | |
201 int width, height; | |
1614 | 202 |
1613 | 203 /* XXX: suppress that, needed by MPEG4 */ |
204 MpegEncContext *enc; | |
1614 | 205 int first_picture; |
1613 | 206 } ParseContext1; |
207 | |
208 /** | |
209 * combines the (truncated) bitstream to a complete frame | |
210 * @returns -1 if no complete frame could be created | |
211 */ | |
1988 | 212 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size) |
1613 | 213 { |
214 #if 0 | |
215 if(pc->overread){ | |
216 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
217 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
218 } | |
219 #endif | |
220 | |
221 /* copy overreaded bytes from last frame into buffer */ | |
222 for(; pc->overread>0; pc->overread--){ | |
223 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; | |
224 } | |
2386 | 225 |
226 /* flush remaining if EOF */ | |
227 if(!*buf_size && next == END_NOT_FOUND){ | |
228 next= 0; | |
229 } | |
230 | |
1613 | 231 pc->last_index= pc->index; |
232 | |
233 /* copy into buffer end return */ | |
234 if(next == END_NOT_FOUND){ | |
235 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
236 | |
237 memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
238 pc->index += *buf_size; | |
239 return -1; | |
240 } | |
241 | |
242 *buf_size= | |
243 pc->overread_index= pc->index + next; | |
244 | |
245 /* append to buffer */ | |
246 if(pc->index){ | |
247 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); | |
248 | |
249 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); | |
250 pc->index = 0; | |
251 *buf= pc->buffer; | |
252 } | |
253 | |
254 /* store overread bytes */ | |
255 for(;next < 0; next++){ | |
256 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; | |
257 pc->overread++; | |
258 } | |
259 | |
260 #if 0 | |
261 if(pc->overread){ | |
262 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index); | |
263 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); | |
264 } | |
265 #endif | |
266 | |
267 return 0; | |
268 } | |
269 | |
270 static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end) | |
271 { | |
272 const uint8_t *buf_ptr; | |
273 unsigned int state=0xFFFFFFFF, v; | |
274 int val; | |
275 | |
276 buf_ptr = *pbuf_ptr; | |
277 while (buf_ptr < buf_end) { | |
278 v = *buf_ptr++; | |
279 if (state == 0x000001) { | |
280 state = ((state << 8) | v) & 0xffffff; | |
281 val = state; | |
282 goto found; | |
283 } | |
284 state = ((state << 8) | v) & 0xffffff; | |
285 } | |
286 val = -1; | |
287 found: | |
288 *pbuf_ptr = buf_ptr; | |
289 return val; | |
290 } | |
291 | |
292 /* XXX: merge with libavcodec ? */ | |
293 #define MPEG1_FRAME_RATE_BASE 1001 | |
294 | |
295 static const int frame_rate_tab[16] = { | |
296 0, | |
297 24000, | |
298 24024, | |
299 25025, | |
300 30000, | |
301 30030, | |
302 50050, | |
303 60000, | |
304 60060, | |
305 // Xing's 15fps: (9) | |
306 15015, | |
307 // libmpeg3's "Unofficial economy rates": (10-13) | |
308 5005, | |
309 10010, | |
310 12012, | |
311 15015, | |
312 // random, just to avoid segfault !never encode these | |
313 25025, | |
314 25025, | |
315 }; | |
316 | |
2637 | 317 //FIXME move into mpeg12.c |
1613 | 318 static void mpegvideo_extract_headers(AVCodecParserContext *s, |
319 AVCodecContext *avctx, | |
320 const uint8_t *buf, int buf_size) | |
321 { | |
322 ParseContext1 *pc = s->priv_data; | |
323 const uint8_t *buf_end; | |
324 int32_t start_code; | |
325 int frame_rate_index, ext_type, bytes_left; | |
326 int frame_rate_ext_n, frame_rate_ext_d; | |
2119 | 327 int picture_structure, top_field_first, repeat_first_field, progressive_frame; |
2539 | 328 int horiz_size_ext, vert_size_ext, bit_rate_ext; |
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
329 //FIXME replace the crap with get_bits() |
1613 | 330 s->repeat_pict = 0; |
331 buf_end = buf + buf_size; | |
332 while (buf < buf_end) { | |
333 start_code = find_start_code(&buf, buf_end); | |
334 bytes_left = buf_end - buf; | |
335 switch(start_code) { | |
336 case PICTURE_START_CODE: | |
337 if (bytes_left >= 2) { | |
338 s->pict_type = (buf[1] >> 3) & 7; | |
339 } | |
340 break; | |
341 case SEQ_START_CODE: | |
2539 | 342 if (bytes_left >= 7) { |
2269 | 343 pc->width = (buf[0] << 4) | (buf[1] >> 4); |
344 pc->height = ((buf[1] & 0x0f) << 8) | buf[2]; | |
2270 | 345 avcodec_set_dimensions(avctx, pc->width, pc->height); |
1613 | 346 frame_rate_index = buf[3] & 0xf; |
2637 | 347 pc->frame_rate = avctx->time_base.den = frame_rate_tab[frame_rate_index]; |
348 avctx->time_base.num = MPEG1_FRAME_RATE_BASE; | |
2539 | 349 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400; |
1681 | 350 avctx->codec_id = CODEC_ID_MPEG1VIDEO; |
351 avctx->sub_id = 1; | |
1613 | 352 } |
353 break; | |
354 case EXT_START_CODE: | |
355 if (bytes_left >= 1) { | |
356 ext_type = (buf[0] >> 4); | |
357 switch(ext_type) { | |
358 case 0x1: /* sequence extension */ | |
359 if (bytes_left >= 6) { | |
360 horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7); | |
361 vert_size_ext = (buf[2] >> 5) & 3; | |
2539 | 362 bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1); |
1613 | 363 frame_rate_ext_n = (buf[5] >> 5) & 3; |
364 frame_rate_ext_d = (buf[5] & 0x1f); | |
365 pc->progressive_sequence = buf[1] & (1 << 3); | |
2565 | 366 avctx->has_b_frames= !(buf[5] >> 7); |
1613 | 367 |
2269 | 368 pc->width |=(horiz_size_ext << 12); |
369 pc->height |=( vert_size_ext << 12); | |
2539 | 370 avctx->bit_rate += (bit_rate_ext << 18) * 400; |
2270 | 371 avcodec_set_dimensions(avctx, pc->width, pc->height); |
2637 | 372 avctx->time_base.den = pc->frame_rate * (frame_rate_ext_n + 1); |
373 avctx->time_base.num = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1); | |
1681 | 374 avctx->codec_id = CODEC_ID_MPEG2VIDEO; |
1613 | 375 avctx->sub_id = 2; /* forces MPEG2 */ |
376 } | |
377 break; | |
378 case 0x8: /* picture coding extension */ | |
379 if (bytes_left >= 5) { | |
2120 | 380 picture_structure = buf[2]&3; |
1613 | 381 top_field_first = buf[3] & (1 << 7); |
382 repeat_first_field = buf[3] & (1 << 1); | |
383 progressive_frame = buf[4] & (1 << 7); | |
384 | |
385 /* check if we must repeat the frame */ | |
386 if (repeat_first_field) { | |
387 if (pc->progressive_sequence) { | |
388 if (top_field_first) | |
389 s->repeat_pict = 4; | |
390 else | |
391 s->repeat_pict = 2; | |
392 } else if (progressive_frame) { | |
393 s->repeat_pict = 1; | |
394 } | |
395 } | |
2119 | 396 |
397 /* the packet only represents half a frame | |
398 XXX,FIXME maybe find a different solution */ | |
399 if(picture_structure != 3) | |
400 s->repeat_pict = -1; | |
1613 | 401 } |
402 break; | |
403 } | |
404 } | |
405 break; | |
406 case -1: | |
407 goto the_end; | |
408 default: | |
409 /* we stop parsing when we encounter a slice. It ensures | |
410 that this function takes a negligible amount of time */ | |
411 if (start_code >= SLICE_MIN_START_CODE && | |
412 start_code <= SLICE_MAX_START_CODE) | |
413 goto the_end; | |
414 break; | |
415 } | |
416 } | |
417 the_end: ; | |
418 } | |
419 | |
420 static int mpegvideo_parse(AVCodecParserContext *s, | |
421 AVCodecContext *avctx, | |
422 uint8_t **poutbuf, int *poutbuf_size, | |
423 const uint8_t *buf, int buf_size) | |
424 { | |
1988 | 425 ParseContext1 *pc1 = s->priv_data; |
426 ParseContext *pc= &pc1->pc; | |
1613 | 427 int next; |
428 | |
1988 | 429 next= ff_mpeg1_find_frame_end(pc, buf, buf_size); |
1613 | 430 |
1988 | 431 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
1613 | 432 *poutbuf = NULL; |
433 *poutbuf_size = 0; | |
434 return buf_size; | |
435 } | |
436 /* we have a full frame : we just parse the first few MPEG headers | |
437 to have the full timing information. The time take by this | |
438 function should be negligible for uncorrupted streams */ | |
439 mpegvideo_extract_headers(s, avctx, buf, buf_size); | |
440 #if 0 | |
441 printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", | |
2637 | 442 s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict); |
1613 | 443 #endif |
444 | |
445 *poutbuf = (uint8_t *)buf; | |
446 *poutbuf_size = buf_size; | |
447 return next; | |
448 } | |
449 | |
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
450 static int mpegvideo_split(AVCodecContext *avctx, |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
451 const uint8_t *buf, int buf_size) |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
452 { |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
453 int i; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
454 uint32_t state= -1; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
455 |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
456 for(i=0; i<buf_size; i++){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
457 state= (state<<8) | buf[i]; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
458 if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
459 return i-4; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
460 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
461 return 0; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
462 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
463 |
1988 | 464 void ff_parse_close(AVCodecParserContext *s) |
1613 | 465 { |
1988 | 466 ParseContext *pc = s->priv_data; |
1613 | 467 |
468 av_free(pc->buffer); | |
1988 | 469 } |
470 | |
471 static void parse1_close(AVCodecParserContext *s) | |
472 { | |
473 ParseContext1 *pc1 = s->priv_data; | |
474 | |
475 av_free(pc1->pc.buffer); | |
476 av_free(pc1->enc); | |
1613 | 477 } |
478 | |
479 /*************************/ | |
480 | |
481 /* used by parser */ | |
482 /* XXX: make it use less memory */ | |
483 static int av_mpeg4_decode_header(AVCodecParserContext *s1, | |
484 AVCodecContext *avctx, | |
485 const uint8_t *buf, int buf_size) | |
486 { | |
487 ParseContext1 *pc = s1->priv_data; | |
488 MpegEncContext *s = pc->enc; | |
489 GetBitContext gb1, *gb = &gb1; | |
490 int ret; | |
491 | |
492 s->avctx = avctx; | |
1614 | 493 s->current_picture_ptr = &s->current_picture; |
494 | |
495 if (avctx->extradata_size && pc->first_picture){ | |
496 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); | |
497 ret = ff_mpeg4_decode_picture_header(s, gb); | |
498 } | |
499 | |
1613 | 500 init_get_bits(gb, buf, 8 * buf_size); |
501 ret = ff_mpeg4_decode_picture_header(s, gb); | |
502 if (s->width) { | |
2270 | 503 avcodec_set_dimensions(avctx, s->width, s->height); |
1613 | 504 } |
1614 | 505 pc->first_picture = 0; |
1613 | 506 return ret; |
507 } | |
508 | |
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1988
diff
changeset
|
509 static int mpeg4video_parse_init(AVCodecParserContext *s) |
1613 | 510 { |
511 ParseContext1 *pc = s->priv_data; | |
1614 | 512 |
1613 | 513 pc->enc = av_mallocz(sizeof(MpegEncContext)); |
514 if (!pc->enc) | |
515 return -1; | |
1614 | 516 pc->first_picture = 1; |
1613 | 517 return 0; |
518 } | |
519 | |
520 static int mpeg4video_parse(AVCodecParserContext *s, | |
521 AVCodecContext *avctx, | |
522 uint8_t **poutbuf, int *poutbuf_size, | |
523 const uint8_t *buf, int buf_size) | |
524 { | |
1988 | 525 ParseContext *pc = s->priv_data; |
1613 | 526 int next; |
527 | |
1988 | 528 next= ff_mpeg4_find_frame_end(pc, buf, buf_size); |
1613 | 529 |
1988 | 530 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) { |
1613 | 531 *poutbuf = NULL; |
532 *poutbuf_size = 0; | |
533 return buf_size; | |
534 } | |
535 av_mpeg4_decode_header(s, avctx, buf, buf_size); | |
536 | |
537 *poutbuf = (uint8_t *)buf; | |
538 *poutbuf_size = buf_size; | |
539 return next; | |
540 } | |
541 | |
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
542 static int mpeg4video_split(AVCodecContext *avctx, |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
543 const uint8_t *buf, int buf_size) |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
544 { |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
545 int i; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
546 uint32_t state= -1; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
547 |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
548 for(i=0; i<buf_size; i++){ |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
549 state= (state<<8) | buf[i]; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
550 if(state == 0x1B3 || state == 0x1B6) |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
551 return i-4; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
552 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
553 return 0; |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
554 } |
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
555 |
1613 | 556 /*************************/ |
557 | |
558 typedef struct MpegAudioParseContext { | |
559 uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE]; /* input buffer */ | |
560 uint8_t *inbuf_ptr; | |
561 int frame_size; | |
562 int free_format_frame_size; | |
563 int free_format_next_header; | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
564 uint32_t header; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
565 int header_count; |
1613 | 566 } MpegAudioParseContext; |
567 | |
568 #define MPA_HEADER_SIZE 4 | |
569 | |
570 /* header + layer + bitrate + freq + lsf/mpeg25 */ | |
2522
e25782262d7d
kill warnings patch by (M«©ns Rullg«©rd <mru inprovide com>)
michael
parents:
2486
diff
changeset
|
571 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */ |
1613 | 572 #define SAME_HEADER_MASK \ |
2480 | 573 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19)) |
1613 | 574 |
575 static int mpegaudio_parse_init(AVCodecParserContext *s1) | |
576 { | |
577 MpegAudioParseContext *s = s1->priv_data; | |
578 s->inbuf_ptr = s->inbuf; | |
579 return 0; | |
580 } | |
581 | |
582 static int mpegaudio_parse(AVCodecParserContext *s1, | |
583 AVCodecContext *avctx, | |
584 uint8_t **poutbuf, int *poutbuf_size, | |
585 const uint8_t *buf, int buf_size) | |
586 { | |
587 MpegAudioParseContext *s = s1->priv_data; | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
588 int len, ret, sr; |
1613 | 589 uint32_t header; |
590 const uint8_t *buf_ptr; | |
591 | |
592 *poutbuf = NULL; | |
593 *poutbuf_size = 0; | |
594 buf_ptr = buf; | |
595 while (buf_size > 0) { | |
596 len = s->inbuf_ptr - s->inbuf; | |
597 if (s->frame_size == 0) { | |
598 /* special case for next header for first frame in free | |
599 format case (XXX: find a simpler method) */ | |
600 if (s->free_format_next_header != 0) { | |
601 s->inbuf[0] = s->free_format_next_header >> 24; | |
602 s->inbuf[1] = s->free_format_next_header >> 16; | |
603 s->inbuf[2] = s->free_format_next_header >> 8; | |
604 s->inbuf[3] = s->free_format_next_header; | |
605 s->inbuf_ptr = s->inbuf + 4; | |
606 s->free_format_next_header = 0; | |
607 goto got_header; | |
608 } | |
609 /* no header seen : find one. We need at least MPA_HEADER_SIZE | |
610 bytes to parse it */ | |
611 len = MPA_HEADER_SIZE - len; | |
612 if (len > buf_size) | |
613 len = buf_size; | |
614 if (len > 0) { | |
615 memcpy(s->inbuf_ptr, buf_ptr, len); | |
616 buf_ptr += len; | |
617 buf_size -= len; | |
618 s->inbuf_ptr += len; | |
619 } | |
620 if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) { | |
621 got_header: | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
622 sr= avctx->sample_rate; |
1613 | 623 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
624 (s->inbuf[2] << 8) | s->inbuf[3]; | |
625 | |
626 ret = mpa_decode_header(avctx, header); | |
627 if (ret < 0) { | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
628 s->header_count= -2; |
1613 | 629 /* no sync found : move by one byte (inefficient, but simple!) */ |
630 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
631 s->inbuf_ptr--; | |
632 dprintf("skip %x\n", header); | |
633 /* reset free format frame size to give a chance | |
634 to get a new bitrate */ | |
635 s->free_format_frame_size = 0; | |
636 } else { | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
637 if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header) |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
638 s->header_count= -3; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
639 s->header= header; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
640 s->header_count++; |
1613 | 641 s->frame_size = ret; |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
642 |
1613 | 643 #if 0 |
644 /* free format: prepare to compute frame size */ | |
645 if (decode_header(s, header) == 1) { | |
646 s->frame_size = -1; | |
647 } | |
648 #endif | |
649 } | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
650 if(s->header_count <= 0) |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
651 avctx->sample_rate= sr; //FIXME ugly |
1613 | 652 } |
653 } else | |
654 #if 0 | |
655 if (s->frame_size == -1) { | |
656 /* free format : find next sync to compute frame size */ | |
657 len = MPA_MAX_CODED_FRAME_SIZE - len; | |
658 if (len > buf_size) | |
659 len = buf_size; | |
660 if (len == 0) { | |
661 /* frame too long: resync */ | |
662 s->frame_size = 0; | |
663 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
664 s->inbuf_ptr--; | |
665 } else { | |
666 uint8_t *p, *pend; | |
667 uint32_t header1; | |
668 int padding; | |
669 | |
670 memcpy(s->inbuf_ptr, buf_ptr, len); | |
671 /* check for header */ | |
672 p = s->inbuf_ptr - 3; | |
673 pend = s->inbuf_ptr + len - 4; | |
674 while (p <= pend) { | |
675 header = (p[0] << 24) | (p[1] << 16) | | |
676 (p[2] << 8) | p[3]; | |
677 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
678 (s->inbuf[2] << 8) | s->inbuf[3]; | |
679 /* check with high probability that we have a | |
680 valid header */ | |
681 if ((header & SAME_HEADER_MASK) == | |
682 (header1 & SAME_HEADER_MASK)) { | |
683 /* header found: update pointers */ | |
684 len = (p + 4) - s->inbuf_ptr; | |
685 buf_ptr += len; | |
686 buf_size -= len; | |
687 s->inbuf_ptr = p; | |
688 /* compute frame size */ | |
689 s->free_format_next_header = header; | |
690 s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
691 padding = (header1 >> 9) & 1; | |
692 if (s->layer == 1) | |
693 s->free_format_frame_size -= padding * 4; | |
694 else | |
695 s->free_format_frame_size -= padding; | |
696 dprintf("free frame size=%d padding=%d\n", | |
697 s->free_format_frame_size, padding); | |
698 decode_header(s, header1); | |
699 goto next_data; | |
700 } | |
701 p++; | |
702 } | |
703 /* not found: simply increase pointers */ | |
704 buf_ptr += len; | |
705 s->inbuf_ptr += len; | |
706 buf_size -= len; | |
707 } | |
708 } else | |
709 #endif | |
710 if (len < s->frame_size) { | |
711 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) | |
712 s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
713 len = s->frame_size - len; | |
714 if (len > buf_size) | |
715 len = buf_size; | |
716 memcpy(s->inbuf_ptr, buf_ptr, len); | |
717 buf_ptr += len; | |
718 s->inbuf_ptr += len; | |
719 buf_size -= len; | |
720 } | |
721 // next_data: | |
722 if (s->frame_size > 0 && | |
723 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { | |
2470
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
724 if(s->header_count > 0){ |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
725 *poutbuf = s->inbuf; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
726 *poutbuf_size = s->inbuf_ptr - s->inbuf; |
06aafb585f69
require a few valid and equal mp3 headers for resync
michael
parents:
2389
diff
changeset
|
727 } |
1613 | 728 s->inbuf_ptr = s->inbuf; |
729 s->frame_size = 0; | |
730 break; | |
731 } | |
732 } | |
733 return buf_ptr - buf; | |
734 } | |
735 | |
736 #ifdef CONFIG_AC3 | |
737 extern int a52_syncinfo (const uint8_t * buf, int * flags, | |
738 int * sample_rate, int * bit_rate); | |
739 | |
740 typedef struct AC3ParseContext { | |
741 uint8_t inbuf[4096]; /* input buffer */ | |
742 uint8_t *inbuf_ptr; | |
743 int frame_size; | |
744 int flags; | |
745 } AC3ParseContext; | |
746 | |
747 #define AC3_HEADER_SIZE 7 | |
748 #define A52_LFE 16 | |
749 | |
750 static int ac3_parse_init(AVCodecParserContext *s1) | |
751 { | |
752 AC3ParseContext *s = s1->priv_data; | |
753 s->inbuf_ptr = s->inbuf; | |
754 return 0; | |
755 } | |
756 | |
757 static int ac3_parse(AVCodecParserContext *s1, | |
758 AVCodecContext *avctx, | |
759 uint8_t **poutbuf, int *poutbuf_size, | |
760 const uint8_t *buf, int buf_size) | |
761 { | |
762 AC3ParseContext *s = s1->priv_data; | |
763 const uint8_t *buf_ptr; | |
764 int len, sample_rate, bit_rate; | |
765 static const int ac3_channels[8] = { | |
766 2, 1, 2, 3, 3, 4, 4, 5 | |
767 }; | |
768 | |
769 *poutbuf = NULL; | |
770 *poutbuf_size = 0; | |
771 | |
772 buf_ptr = buf; | |
773 while (buf_size > 0) { | |
774 len = s->inbuf_ptr - s->inbuf; | |
775 if (s->frame_size == 0) { | |
776 /* no header seen : find one. We need at least 7 bytes to parse it */ | |
777 len = AC3_HEADER_SIZE - len; | |
778 if (len > buf_size) | |
779 len = buf_size; | |
780 memcpy(s->inbuf_ptr, buf_ptr, len); | |
781 buf_ptr += len; | |
782 s->inbuf_ptr += len; | |
783 buf_size -= len; | |
784 if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) { | |
785 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate); | |
786 if (len == 0) { | |
787 /* no sync found : move by one byte (inefficient, but simple!) */ | |
788 memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1); | |
789 s->inbuf_ptr--; | |
790 } else { | |
791 s->frame_size = len; | |
792 /* update codec info */ | |
793 avctx->sample_rate = sample_rate; | |
1987 | 794 /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */ |
795 if(avctx->channels!=1 && avctx->channels!=2){ | |
796 avctx->channels = ac3_channels[s->flags & 7]; | |
797 if (s->flags & A52_LFE) | |
798 avctx->channels++; | |
799 } | |
1613 | 800 avctx->bit_rate = bit_rate; |
801 avctx->frame_size = 6 * 256; | |
802 } | |
803 } | |
804 } else if (len < s->frame_size) { | |
805 len = s->frame_size - len; | |
806 if (len > buf_size) | |
807 len = buf_size; | |
808 | |
809 memcpy(s->inbuf_ptr, buf_ptr, len); | |
810 buf_ptr += len; | |
811 s->inbuf_ptr += len; | |
812 buf_size -= len; | |
813 } else { | |
814 *poutbuf = s->inbuf; | |
815 *poutbuf_size = s->frame_size; | |
816 s->inbuf_ptr = s->inbuf; | |
817 s->frame_size = 0; | |
818 break; | |
819 } | |
820 } | |
821 return buf_ptr - buf; | |
822 } | |
823 #endif | |
824 | |
825 AVCodecParser mpegvideo_parser = { | |
826 { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO }, | |
827 sizeof(ParseContext1), | |
828 NULL, | |
829 mpegvideo_parse, | |
1988 | 830 parse1_close, |
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
831 mpegvideo_split, |
1613 | 832 }; |
833 | |
834 AVCodecParser mpeg4video_parser = { | |
835 { CODEC_ID_MPEG4 }, | |
836 sizeof(ParseContext1), | |
837 mpeg4video_parse_init, | |
838 mpeg4video_parse, | |
1988 | 839 parse1_close, |
2769
1394b45a7bf4
support changing in bitstream global headers into extradata style and back
michael
parents:
2637
diff
changeset
|
840 mpeg4video_split, |
1613 | 841 }; |
842 | |
843 AVCodecParser mpegaudio_parser = { | |
844 { CODEC_ID_MP2, CODEC_ID_MP3 }, | |
845 sizeof(MpegAudioParseContext), | |
846 mpegaudio_parse_init, | |
847 mpegaudio_parse, | |
848 NULL, | |
849 }; | |
850 | |
851 #ifdef CONFIG_AC3 | |
852 AVCodecParser ac3_parser = { | |
853 { CODEC_ID_AC3 }, | |
854 sizeof(AC3ParseContext), | |
855 ac3_parse_init, | |
856 ac3_parse, | |
857 NULL, | |
858 }; | |
859 #endif |