]> git.sesse.net Git - ffmpeg/blob - libavcodec/parser.c
kill warnings patch by (Måns Rullgård <mru inprovide com>)
[ffmpeg] / libavcodec / parser.c
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     if(codec_id == CODEC_ID_NONE)
39         return NULL;
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 ||
44             parser->codec_ids[2] == codec_id ||
45             parser->codec_ids[3] == codec_id ||
46             parser->codec_ids[4] == codec_id)
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     }
68     s->fetch_timestamp=1;
69     return s;
70 }
71
72 /* NOTE: buf_size == 0 is used to signal EOF so that the last frame
73    can be returned if necessary */
74 int av_parser_parse(AVCodecParserContext *s, 
75                     AVCodecContext *avctx,
76                     uint8_t **poutbuf, int *poutbuf_size, 
77                     const uint8_t *buf, int buf_size,
78                     int64_t pts, int64_t dts)
79 {
80     int index, i, k;
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;
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 */
96         if (s->fetch_timestamp){
97             s->fetch_timestamp=0;
98             s->last_pts = pts;
99             s->last_dts = dts;
100             s->cur_frame_pts[k] =
101             s->cur_frame_dts[k] = AV_NOPTS_VALUE;
102         }
103     }
104
105     /* WARNING: the returned index can be negative */
106     index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
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);
108     /* update the file pointer */
109     if (*poutbuf_size) {
110         /* fill the data for the current frame */
111         s->frame_offset = s->last_frame_offset;
112         s->pts = s->last_pts;
113         s->dts = s->last_dts;
114         
115         /* offset of the next frame */
116         s->last_frame_offset = s->cur_offset + index;
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         }
129
130         s->last_pts = s->cur_frame_pts[k];
131         s->last_dts = s->cur_frame_dts[k];
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         }
138     }
139     if (index < 0)
140         index = 0;
141     s->cur_offset += index;
142     return index;
143 }
144
145 void 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
163 typedef struct ParseContext1{
164     ParseContext pc;
165 /* XXX/FIXME PC1 vs. PC */
166     /* MPEG2 specific */
167     int frame_rate;
168     int progressive_sequence;
169     int width, height;
170
171     /* XXX: suppress that, needed by MPEG4 */
172     MpegEncContext *enc;
173     int first_picture;
174 } ParseContext1;
175
176 /**
177  * combines the (truncated) bitstream to a complete frame
178  * @returns -1 if no complete frame could be created
179  */
180 int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
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     }
193
194     /* flush remaining if EOF */
195     if(!*buf_size && next == END_NOT_FOUND){
196         next= 0;
197     }
198
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
238 static 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
263 static 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
285 static void mpegvideo_extract_headers(AVCodecParserContext *s, 
286                                       AVCodecContext *avctx,
287                                       const uint8_t *buf, int buf_size)
288 {
289     ParseContext1 *pc = s->priv_data;
290     const uint8_t *buf_end;
291     int32_t start_code;
292     int frame_rate_index, ext_type, bytes_left;
293     int frame_rate_ext_n, frame_rate_ext_d;
294     int picture_structure, top_field_first, repeat_first_field, progressive_frame;
295     int horiz_size_ext, vert_size_ext;
296
297     s->repeat_pict = 0;
298     buf_end = buf + buf_size;
299     while (buf < buf_end) {
300         start_code = find_start_code(&buf, buf_end);
301         bytes_left = buf_end - buf;
302         switch(start_code) {
303         case PICTURE_START_CODE:
304             if (bytes_left >= 2) {
305                 s->pict_type = (buf[1] >> 3) & 7;
306             }
307             break;
308         case SEQ_START_CODE:
309             if (bytes_left >= 4) {
310                 pc->width  = (buf[0] << 4) | (buf[1] >> 4);
311                 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
312                 avcodec_set_dimensions(avctx, pc->width, pc->height);
313                 frame_rate_index = buf[3] & 0xf;
314                 pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
315                 avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
316                 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
317                 avctx->sub_id = 1;
318             }
319             break;
320         case EXT_START_CODE:
321             if (bytes_left >= 1) {
322                 ext_type = (buf[0] >> 4);
323                 switch(ext_type) {
324                 case 0x1: /* sequence extension */
325                     if (bytes_left >= 6) {
326                         horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
327                         vert_size_ext = (buf[2] >> 5) & 3;
328                         frame_rate_ext_n = (buf[5] >> 5) & 3;
329                         frame_rate_ext_d = (buf[5] & 0x1f);
330                         pc->progressive_sequence = buf[1] & (1 << 3);
331                         avctx->has_b_frames= buf[5] >> 7;
332
333                         pc->width  |=(horiz_size_ext << 12);
334                         pc->height |=( vert_size_ext << 12);
335                         avcodec_set_dimensions(avctx, pc->width, pc->height);
336                         avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
337                         avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
338                         avctx->codec_id = CODEC_ID_MPEG2VIDEO;
339                         avctx->sub_id = 2; /* forces MPEG2 */
340                     }
341                     break;
342                 case 0x8: /* picture coding extension */
343                     if (bytes_left >= 5) {
344                         picture_structure = buf[2]&3;
345                         top_field_first = buf[3] & (1 << 7);
346                         repeat_first_field = buf[3] & (1 << 1);
347                         progressive_frame = buf[4] & (1 << 7);
348                     
349                         /* check if we must repeat the frame */
350                         if (repeat_first_field) {
351                             if (pc->progressive_sequence) {
352                                 if (top_field_first)
353                                     s->repeat_pict = 4;
354                                 else
355                                     s->repeat_pict = 2;
356                             } else if (progressive_frame) {
357                                 s->repeat_pict = 1;
358                             }
359                         }
360                         
361                         /* the packet only represents half a frame 
362                            XXX,FIXME maybe find a different solution */
363                         if(picture_structure != 3)
364                             s->repeat_pict = -1;
365                     }
366                     break;
367                 }
368             }
369             break;
370         case -1:
371             goto the_end;
372         default:
373             /* we stop parsing when we encounter a slice. It ensures
374                that this function takes a negligible amount of time */
375             if (start_code >= SLICE_MIN_START_CODE && 
376                 start_code <= SLICE_MAX_START_CODE)
377                 goto the_end;
378             break;
379         }
380     }
381  the_end: ;
382 }
383
384 static int mpegvideo_parse(AVCodecParserContext *s,
385                            AVCodecContext *avctx,
386                            uint8_t **poutbuf, int *poutbuf_size, 
387                            const uint8_t *buf, int buf_size)
388 {
389     ParseContext1 *pc1 = s->priv_data;
390     ParseContext *pc= &pc1->pc;
391     int next;
392     
393     next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
394     
395     if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
396         *poutbuf = NULL;
397         *poutbuf_size = 0;
398         return buf_size;
399     }
400     /* we have a full frame : we just parse the first few MPEG headers
401        to have the full timing information. The time take by this
402        function should be negligible for uncorrupted streams */
403     mpegvideo_extract_headers(s, avctx, buf, buf_size);
404 #if 0
405     printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", 
406            s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
407 #endif
408
409     *poutbuf = (uint8_t *)buf;
410     *poutbuf_size = buf_size;
411     return next;
412 }
413
414 void ff_parse_close(AVCodecParserContext *s)
415 {
416     ParseContext *pc = s->priv_data;
417
418     av_free(pc->buffer);
419 }
420
421 static void parse1_close(AVCodecParserContext *s)
422 {
423     ParseContext1 *pc1 = s->priv_data;
424
425     av_free(pc1->pc.buffer);
426     av_free(pc1->enc);
427 }
428
429 /*************************/
430
431 /* used by parser */
432 /* XXX: make it use less memory */
433 static int av_mpeg4_decode_header(AVCodecParserContext *s1, 
434                                   AVCodecContext *avctx,
435                                   const uint8_t *buf, int buf_size)
436 {
437     ParseContext1 *pc = s1->priv_data;
438     MpegEncContext *s = pc->enc;
439     GetBitContext gb1, *gb = &gb1;
440     int ret;
441
442     s->avctx = avctx;
443     s->current_picture_ptr = &s->current_picture;
444
445     if (avctx->extradata_size && pc->first_picture){
446         init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
447         ret = ff_mpeg4_decode_picture_header(s, gb);
448     }
449
450     init_get_bits(gb, buf, 8 * buf_size);
451     ret = ff_mpeg4_decode_picture_header(s, gb);
452     if (s->width) {
453         avcodec_set_dimensions(avctx, s->width, s->height);
454     }
455     pc->first_picture = 0;
456     return ret;
457 }
458
459 static int mpeg4video_parse_init(AVCodecParserContext *s)
460 {
461     ParseContext1 *pc = s->priv_data;
462
463     pc->enc = av_mallocz(sizeof(MpegEncContext));
464     if (!pc->enc)
465         return -1;
466     pc->first_picture = 1;
467     return 0;
468 }
469
470 static int mpeg4video_parse(AVCodecParserContext *s,
471                            AVCodecContext *avctx,
472                            uint8_t **poutbuf, int *poutbuf_size, 
473                            const uint8_t *buf, int buf_size)
474 {
475     ParseContext *pc = s->priv_data;
476     int next;
477     
478     next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
479
480     if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
481         *poutbuf = NULL;
482         *poutbuf_size = 0;
483         return buf_size;
484     }
485     av_mpeg4_decode_header(s, avctx, buf, buf_size);
486
487     *poutbuf = (uint8_t *)buf;
488     *poutbuf_size = buf_size;
489     return next;
490 }
491
492 /*************************/
493
494 typedef struct MpegAudioParseContext {
495     uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];    /* input buffer */
496     uint8_t *inbuf_ptr;
497     int frame_size;
498     int free_format_frame_size;
499     int free_format_next_header;
500     uint32_t header;
501     int header_count;
502 } MpegAudioParseContext;
503
504 #define MPA_HEADER_SIZE 4
505
506 /* header + layer + bitrate + freq + lsf/mpeg25 */
507 #undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
508 #define SAME_HEADER_MASK \
509    (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
510
511 static int mpegaudio_parse_init(AVCodecParserContext *s1)
512 {
513     MpegAudioParseContext *s = s1->priv_data;
514     s->inbuf_ptr = s->inbuf;
515     return 0;
516 }
517
518 static int mpegaudio_parse(AVCodecParserContext *s1,
519                            AVCodecContext *avctx,
520                            uint8_t **poutbuf, int *poutbuf_size, 
521                            const uint8_t *buf, int buf_size)
522 {
523     MpegAudioParseContext *s = s1->priv_data;
524     int len, ret, sr;
525     uint32_t header;
526     const uint8_t *buf_ptr;
527
528     *poutbuf = NULL;
529     *poutbuf_size = 0;
530     buf_ptr = buf;
531     while (buf_size > 0) {
532         len = s->inbuf_ptr - s->inbuf;
533         if (s->frame_size == 0) {
534             /* special case for next header for first frame in free
535                format case (XXX: find a simpler method) */
536             if (s->free_format_next_header != 0) {
537                 s->inbuf[0] = s->free_format_next_header >> 24;
538                 s->inbuf[1] = s->free_format_next_header >> 16;
539                 s->inbuf[2] = s->free_format_next_header >> 8;
540                 s->inbuf[3] = s->free_format_next_header;
541                 s->inbuf_ptr = s->inbuf + 4;
542                 s->free_format_next_header = 0;
543                 goto got_header;
544             }
545             /* no header seen : find one. We need at least MPA_HEADER_SIZE
546                bytes to parse it */
547             len = MPA_HEADER_SIZE - len;
548             if (len > buf_size)
549                 len = buf_size;
550             if (len > 0) {
551                 memcpy(s->inbuf_ptr, buf_ptr, len);
552                 buf_ptr += len;
553                 buf_size -= len;
554                 s->inbuf_ptr += len;
555             }
556             if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
557             got_header:
558                 sr= avctx->sample_rate;
559                 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
560                     (s->inbuf[2] << 8) | s->inbuf[3];
561
562                 ret = mpa_decode_header(avctx, header);
563                 if (ret < 0) {
564                     s->header_count= -2;
565                     /* no sync found : move by one byte (inefficient, but simple!) */
566                     memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
567                     s->inbuf_ptr--;
568                     dprintf("skip %x\n", header);
569                     /* reset free format frame size to give a chance
570                        to get a new bitrate */
571                     s->free_format_frame_size = 0;
572                 } else {
573                     if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
574                         s->header_count= -3;
575                     s->header= header;
576                     s->header_count++;
577                     s->frame_size = ret;
578                     
579 #if 0
580                     /* free format: prepare to compute frame size */
581                     if (decode_header(s, header) == 1) {
582                         s->frame_size = -1;
583                     }
584 #endif
585                 }
586                 if(s->header_count <= 0)
587                     avctx->sample_rate= sr; //FIXME ugly
588             }
589         } else 
590 #if 0
591         if (s->frame_size == -1) {
592             /* free format : find next sync to compute frame size */
593             len = MPA_MAX_CODED_FRAME_SIZE - len;
594             if (len > buf_size)
595                 len = buf_size;
596             if (len == 0) {
597                 /* frame too long: resync */
598                 s->frame_size = 0;
599                 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
600                 s->inbuf_ptr--;
601             } else {
602                 uint8_t *p, *pend;
603                 uint32_t header1;
604                 int padding;
605
606                 memcpy(s->inbuf_ptr, buf_ptr, len);
607                 /* check for header */
608                 p = s->inbuf_ptr - 3;
609                 pend = s->inbuf_ptr + len - 4;
610                 while (p <= pend) {
611                     header = (p[0] << 24) | (p[1] << 16) |
612                         (p[2] << 8) | p[3];
613                     header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
614                         (s->inbuf[2] << 8) | s->inbuf[3];
615                     /* check with high probability that we have a
616                        valid header */
617                     if ((header & SAME_HEADER_MASK) ==
618                         (header1 & SAME_HEADER_MASK)) {
619                         /* header found: update pointers */
620                         len = (p + 4) - s->inbuf_ptr;
621                         buf_ptr += len;
622                         buf_size -= len;
623                         s->inbuf_ptr = p;
624                         /* compute frame size */
625                         s->free_format_next_header = header;
626                         s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
627                         padding = (header1 >> 9) & 1;
628                         if (s->layer == 1)
629                             s->free_format_frame_size -= padding * 4;
630                         else
631                             s->free_format_frame_size -= padding;
632                         dprintf("free frame size=%d padding=%d\n", 
633                                 s->free_format_frame_size, padding);
634                         decode_header(s, header1);
635                         goto next_data;
636                     }
637                     p++;
638                 }
639                 /* not found: simply increase pointers */
640                 buf_ptr += len;
641                 s->inbuf_ptr += len;
642                 buf_size -= len;
643             }
644         } else 
645 #endif
646         if (len < s->frame_size) {
647             if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
648                 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
649             len = s->frame_size - len;
650             if (len > buf_size)
651                 len = buf_size;
652             memcpy(s->inbuf_ptr, buf_ptr, len);
653             buf_ptr += len;
654             s->inbuf_ptr += len;
655             buf_size -= len;
656         }
657         //    next_data:
658         if (s->frame_size > 0 && 
659             (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
660             if(s->header_count > 0){
661                 *poutbuf = s->inbuf;
662                 *poutbuf_size = s->inbuf_ptr - s->inbuf;
663             }
664             s->inbuf_ptr = s->inbuf;
665             s->frame_size = 0;
666             break;
667         }
668     }
669     return buf_ptr - buf;
670 }
671
672 #ifdef CONFIG_AC3
673 extern int a52_syncinfo (const uint8_t * buf, int * flags,
674                          int * sample_rate, int * bit_rate);
675
676 typedef struct AC3ParseContext {
677     uint8_t inbuf[4096]; /* input buffer */
678     uint8_t *inbuf_ptr;
679     int frame_size;
680     int flags;
681 } AC3ParseContext;
682
683 #define AC3_HEADER_SIZE 7
684 #define A52_LFE 16
685
686 static int ac3_parse_init(AVCodecParserContext *s1)
687 {
688     AC3ParseContext *s = s1->priv_data;
689     s->inbuf_ptr = s->inbuf;
690     return 0;
691 }
692
693 static int ac3_parse(AVCodecParserContext *s1,
694                      AVCodecContext *avctx,
695                      uint8_t **poutbuf, int *poutbuf_size, 
696                      const uint8_t *buf, int buf_size)
697 {
698     AC3ParseContext *s = s1->priv_data;
699     const uint8_t *buf_ptr;
700     int len, sample_rate, bit_rate;
701     static const int ac3_channels[8] = {
702         2, 1, 2, 3, 3, 4, 4, 5
703     };
704
705     *poutbuf = NULL;
706     *poutbuf_size = 0;
707
708     buf_ptr = buf;
709     while (buf_size > 0) {
710         len = s->inbuf_ptr - s->inbuf;
711         if (s->frame_size == 0) {
712             /* no header seen : find one. We need at least 7 bytes to parse it */
713             len = AC3_HEADER_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             if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
721                 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
722                 if (len == 0) {
723                     /* no sync found : move by one byte (inefficient, but simple!) */
724                     memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
725                     s->inbuf_ptr--;
726                 } else {
727                     s->frame_size = len;
728                     /* update codec info */
729                     avctx->sample_rate = sample_rate;
730                     /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
731                     if(avctx->channels!=1 && avctx->channels!=2){
732                         avctx->channels = ac3_channels[s->flags & 7];
733                         if (s->flags & A52_LFE)
734                             avctx->channels++;
735                     }
736                     avctx->bit_rate = bit_rate;
737                     avctx->frame_size = 6 * 256;
738                 }
739             }
740         } else if (len < s->frame_size) {
741             len = s->frame_size - len;
742             if (len > buf_size)
743                 len = buf_size;
744
745             memcpy(s->inbuf_ptr, buf_ptr, len);
746             buf_ptr += len;
747             s->inbuf_ptr += len;
748             buf_size -= len;
749         } else {
750             *poutbuf = s->inbuf;
751             *poutbuf_size = s->frame_size;
752             s->inbuf_ptr = s->inbuf;
753             s->frame_size = 0;
754             break;
755         }
756     }
757     return buf_ptr - buf;
758 }
759 #endif
760
761 AVCodecParser mpegvideo_parser = {
762     { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
763     sizeof(ParseContext1),
764     NULL,
765     mpegvideo_parse,
766     parse1_close,
767 };
768
769 AVCodecParser mpeg4video_parser = {
770     { CODEC_ID_MPEG4 },
771     sizeof(ParseContext1),
772     mpeg4video_parse_init,
773     mpeg4video_parse,
774     parse1_close,
775 };
776
777 AVCodecParser mpegaudio_parser = {
778     { CODEC_ID_MP2, CODEC_ID_MP3 },
779     sizeof(MpegAudioParseContext),
780     mpegaudio_parse_init,
781     mpegaudio_parse,
782     NULL,
783 };
784
785 #ifdef CONFIG_AC3
786 AVCodecParser ac3_parser = {
787     { CODEC_ID_AC3 },
788     sizeof(AC3ParseContext),
789     ac3_parse_init,
790     ac3_parse,
791     NULL,
792 };
793 #endif