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