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