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