]> git.sesse.net Git - ffmpeg/blob - libavcodec/parser.c
use dc tables from msmpeg4 instead of duplicating them patch by anonymous
[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                         avctx->has_b_frames= buf[5] >> 7;
329
330                         pc->width  |=(horiz_size_ext << 12);
331                         pc->height |=( vert_size_ext << 12);
332                         avcodec_set_dimensions(avctx, pc->width, pc->height);
333                         avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
334                         avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
335                         avctx->codec_id = CODEC_ID_MPEG2VIDEO;
336                         avctx->sub_id = 2; /* forces MPEG2 */
337                     }
338                     break;
339                 case 0x8: /* picture coding extension */
340                     if (bytes_left >= 5) {
341                         picture_structure = buf[2]&3;
342                         top_field_first = buf[3] & (1 << 7);
343                         repeat_first_field = buf[3] & (1 << 1);
344                         progressive_frame = buf[4] & (1 << 7);
345                     
346                         /* check if we must repeat the frame */
347                         if (repeat_first_field) {
348                             if (pc->progressive_sequence) {
349                                 if (top_field_first)
350                                     s->repeat_pict = 4;
351                                 else
352                                     s->repeat_pict = 2;
353                             } else if (progressive_frame) {
354                                 s->repeat_pict = 1;
355                             }
356                         }
357                         
358                         /* the packet only represents half a frame 
359                            XXX,FIXME maybe find a different solution */
360                         if(picture_structure != 3)
361                             s->repeat_pict = -1;
362                     }
363                     break;
364                 }
365             }
366             break;
367         case -1:
368             goto the_end;
369         default:
370             /* we stop parsing when we encounter a slice. It ensures
371                that this function takes a negligible amount of time */
372             if (start_code >= SLICE_MIN_START_CODE && 
373                 start_code <= SLICE_MAX_START_CODE)
374                 goto the_end;
375             break;
376         }
377     }
378  the_end: ;
379 }
380
381 static int mpegvideo_parse(AVCodecParserContext *s,
382                            AVCodecContext *avctx,
383                            uint8_t **poutbuf, int *poutbuf_size, 
384                            const uint8_t *buf, int buf_size)
385 {
386     ParseContext1 *pc1 = s->priv_data;
387     ParseContext *pc= &pc1->pc;
388     int next;
389     
390     next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
391     
392     if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
393         *poutbuf = NULL;
394         *poutbuf_size = 0;
395         return buf_size;
396     }
397     /* we have a full frame : we just parse the first few MPEG headers
398        to have the full timing information. The time take by this
399        function should be negligible for uncorrupted streams */
400     mpegvideo_extract_headers(s, avctx, buf, buf_size);
401 #if 0
402     printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", 
403            s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
404 #endif
405
406     *poutbuf = (uint8_t *)buf;
407     *poutbuf_size = buf_size;
408     return next;
409 }
410
411 void ff_parse_close(AVCodecParserContext *s)
412 {
413     ParseContext *pc = s->priv_data;
414
415     av_free(pc->buffer);
416 }
417
418 static void parse1_close(AVCodecParserContext *s)
419 {
420     ParseContext1 *pc1 = s->priv_data;
421
422     av_free(pc1->pc.buffer);
423     av_free(pc1->enc);
424 }
425
426 /*************************/
427
428 /* used by parser */
429 /* XXX: make it use less memory */
430 static int av_mpeg4_decode_header(AVCodecParserContext *s1, 
431                                   AVCodecContext *avctx,
432                                   const uint8_t *buf, int buf_size)
433 {
434     ParseContext1 *pc = s1->priv_data;
435     MpegEncContext *s = pc->enc;
436     GetBitContext gb1, *gb = &gb1;
437     int ret;
438
439     s->avctx = avctx;
440     s->current_picture_ptr = &s->current_picture;
441
442     if (avctx->extradata_size && pc->first_picture){
443         init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
444         ret = ff_mpeg4_decode_picture_header(s, gb);
445     }
446
447     init_get_bits(gb, buf, 8 * buf_size);
448     ret = ff_mpeg4_decode_picture_header(s, gb);
449     if (s->width) {
450         avcodec_set_dimensions(avctx, s->width, s->height);
451     }
452     pc->first_picture = 0;
453     return ret;
454 }
455
456 static int mpeg4video_parse_init(AVCodecParserContext *s)
457 {
458     ParseContext1 *pc = s->priv_data;
459
460     pc->enc = av_mallocz(sizeof(MpegEncContext));
461     if (!pc->enc)
462         return -1;
463     pc->first_picture = 1;
464     return 0;
465 }
466
467 static int mpeg4video_parse(AVCodecParserContext *s,
468                            AVCodecContext *avctx,
469                            uint8_t **poutbuf, int *poutbuf_size, 
470                            const uint8_t *buf, int buf_size)
471 {
472     ParseContext *pc = s->priv_data;
473     int next;
474     
475     next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
476
477     if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
478         *poutbuf = NULL;
479         *poutbuf_size = 0;
480         return buf_size;
481     }
482     av_mpeg4_decode_header(s, avctx, buf, buf_size);
483
484     *poutbuf = (uint8_t *)buf;
485     *poutbuf_size = buf_size;
486     return next;
487 }
488
489 /*************************/
490
491 typedef struct MpegAudioParseContext {
492     uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];    /* input buffer */
493     uint8_t *inbuf_ptr;
494     int frame_size;
495     int free_format_frame_size;
496     int free_format_next_header;
497 } MpegAudioParseContext;
498
499 #define MPA_HEADER_SIZE 4
500
501 /* header + layer + bitrate + freq + lsf/mpeg25 */
502 #define SAME_HEADER_MASK \
503    (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
504
505 static int mpegaudio_parse_init(AVCodecParserContext *s1)
506 {
507     MpegAudioParseContext *s = s1->priv_data;
508     s->inbuf_ptr = s->inbuf;
509     return 0;
510 }
511
512 static int mpegaudio_parse(AVCodecParserContext *s1,
513                            AVCodecContext *avctx,
514                            uint8_t **poutbuf, int *poutbuf_size, 
515                            const uint8_t *buf, int buf_size)
516 {
517     MpegAudioParseContext *s = s1->priv_data;
518     int len, ret;
519     uint32_t header;
520     const uint8_t *buf_ptr;
521
522     *poutbuf = NULL;
523     *poutbuf_size = 0;
524     buf_ptr = buf;
525     while (buf_size > 0) {
526         len = s->inbuf_ptr - s->inbuf;
527         if (s->frame_size == 0) {
528             /* special case for next header for first frame in free
529                format case (XXX: find a simpler method) */
530             if (s->free_format_next_header != 0) {
531                 s->inbuf[0] = s->free_format_next_header >> 24;
532                 s->inbuf[1] = s->free_format_next_header >> 16;
533                 s->inbuf[2] = s->free_format_next_header >> 8;
534                 s->inbuf[3] = s->free_format_next_header;
535                 s->inbuf_ptr = s->inbuf + 4;
536                 s->free_format_next_header = 0;
537                 goto got_header;
538             }
539             /* no header seen : find one. We need at least MPA_HEADER_SIZE
540                bytes to parse it */
541             len = MPA_HEADER_SIZE - len;
542             if (len > buf_size)
543                 len = buf_size;
544             if (len > 0) {
545                 memcpy(s->inbuf_ptr, buf_ptr, len);
546                 buf_ptr += len;
547                 buf_size -= len;
548                 s->inbuf_ptr += len;
549             }
550             if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
551             got_header:
552                 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
553                     (s->inbuf[2] << 8) | s->inbuf[3];
554
555                 ret = mpa_decode_header(avctx, header);
556                 if (ret < 0) {
557                     /* no sync found : move by one byte (inefficient, but simple!) */
558                     memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
559                     s->inbuf_ptr--;
560                     dprintf("skip %x\n", header);
561                     /* reset free format frame size to give a chance
562                        to get a new bitrate */
563                     s->free_format_frame_size = 0;
564                 } else {
565                     s->frame_size = ret;
566 #if 0
567                     /* free format: prepare to compute frame size */
568                     if (decode_header(s, header) == 1) {
569                         s->frame_size = -1;
570                     }
571 #endif
572                 }
573             }
574         } else 
575 #if 0
576         if (s->frame_size == -1) {
577             /* free format : find next sync to compute frame size */
578             len = MPA_MAX_CODED_FRAME_SIZE - len;
579             if (len > buf_size)
580                 len = buf_size;
581             if (len == 0) {
582                 /* frame too long: resync */
583                 s->frame_size = 0;
584                 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
585                 s->inbuf_ptr--;
586             } else {
587                 uint8_t *p, *pend;
588                 uint32_t header1;
589                 int padding;
590
591                 memcpy(s->inbuf_ptr, buf_ptr, len);
592                 /* check for header */
593                 p = s->inbuf_ptr - 3;
594                 pend = s->inbuf_ptr + len - 4;
595                 while (p <= pend) {
596                     header = (p[0] << 24) | (p[1] << 16) |
597                         (p[2] << 8) | p[3];
598                     header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
599                         (s->inbuf[2] << 8) | s->inbuf[3];
600                     /* check with high probability that we have a
601                        valid header */
602                     if ((header & SAME_HEADER_MASK) ==
603                         (header1 & SAME_HEADER_MASK)) {
604                         /* header found: update pointers */
605                         len = (p + 4) - s->inbuf_ptr;
606                         buf_ptr += len;
607                         buf_size -= len;
608                         s->inbuf_ptr = p;
609                         /* compute frame size */
610                         s->free_format_next_header = header;
611                         s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
612                         padding = (header1 >> 9) & 1;
613                         if (s->layer == 1)
614                             s->free_format_frame_size -= padding * 4;
615                         else
616                             s->free_format_frame_size -= padding;
617                         dprintf("free frame size=%d padding=%d\n", 
618                                 s->free_format_frame_size, padding);
619                         decode_header(s, header1);
620                         goto next_data;
621                     }
622                     p++;
623                 }
624                 /* not found: simply increase pointers */
625                 buf_ptr += len;
626                 s->inbuf_ptr += len;
627                 buf_size -= len;
628             }
629         } else 
630 #endif
631         if (len < s->frame_size) {
632             if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
633                 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
634             len = s->frame_size - len;
635             if (len > buf_size)
636                 len = buf_size;
637             memcpy(s->inbuf_ptr, buf_ptr, len);
638             buf_ptr += len;
639             s->inbuf_ptr += len;
640             buf_size -= len;
641         }
642         //    next_data:
643         if (s->frame_size > 0 && 
644             (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
645             *poutbuf = s->inbuf;
646             *poutbuf_size = s->inbuf_ptr - s->inbuf;
647             s->inbuf_ptr = s->inbuf;
648             s->frame_size = 0;
649             break;
650         }
651     }
652     return buf_ptr - buf;
653 }
654
655 #ifdef CONFIG_AC3
656 extern int a52_syncinfo (const uint8_t * buf, int * flags,
657                          int * sample_rate, int * bit_rate);
658
659 typedef struct AC3ParseContext {
660     uint8_t inbuf[4096]; /* input buffer */
661     uint8_t *inbuf_ptr;
662     int frame_size;
663     int flags;
664 } AC3ParseContext;
665
666 #define AC3_HEADER_SIZE 7
667 #define A52_LFE 16
668
669 static int ac3_parse_init(AVCodecParserContext *s1)
670 {
671     AC3ParseContext *s = s1->priv_data;
672     s->inbuf_ptr = s->inbuf;
673     return 0;
674 }
675
676 static int ac3_parse(AVCodecParserContext *s1,
677                      AVCodecContext *avctx,
678                      uint8_t **poutbuf, int *poutbuf_size, 
679                      const uint8_t *buf, int buf_size)
680 {
681     AC3ParseContext *s = s1->priv_data;
682     const uint8_t *buf_ptr;
683     int len, sample_rate, bit_rate;
684     static const int ac3_channels[8] = {
685         2, 1, 2, 3, 3, 4, 4, 5
686     };
687
688     *poutbuf = NULL;
689     *poutbuf_size = 0;
690
691     buf_ptr = buf;
692     while (buf_size > 0) {
693         len = s->inbuf_ptr - s->inbuf;
694         if (s->frame_size == 0) {
695             /* no header seen : find one. We need at least 7 bytes to parse it */
696             len = AC3_HEADER_SIZE - len;
697             if (len > buf_size)
698                 len = buf_size;
699             memcpy(s->inbuf_ptr, buf_ptr, len);
700             buf_ptr += len;
701             s->inbuf_ptr += len;
702             buf_size -= len;
703             if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
704                 len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
705                 if (len == 0) {
706                     /* no sync found : move by one byte (inefficient, but simple!) */
707                     memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
708                     s->inbuf_ptr--;
709                 } else {
710                     s->frame_size = len;
711                     /* update codec info */
712                     avctx->sample_rate = sample_rate;
713                     /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
714                     if(avctx->channels!=1 && avctx->channels!=2){
715                         avctx->channels = ac3_channels[s->flags & 7];
716                         if (s->flags & A52_LFE)
717                             avctx->channels++;
718                     }
719                     avctx->bit_rate = bit_rate;
720                     avctx->frame_size = 6 * 256;
721                 }
722             }
723         } else if (len < s->frame_size) {
724             len = s->frame_size - len;
725             if (len > buf_size)
726                 len = buf_size;
727
728             memcpy(s->inbuf_ptr, buf_ptr, len);
729             buf_ptr += len;
730             s->inbuf_ptr += len;
731             buf_size -= len;
732         } else {
733             *poutbuf = s->inbuf;
734             *poutbuf_size = s->frame_size;
735             s->inbuf_ptr = s->inbuf;
736             s->frame_size = 0;
737             break;
738         }
739     }
740     return buf_ptr - buf;
741 }
742 #endif
743
744 AVCodecParser mpegvideo_parser = {
745     { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
746     sizeof(ParseContext1),
747     NULL,
748     mpegvideo_parse,
749     parse1_close,
750 };
751
752 AVCodecParser mpeg4video_parser = {
753     { CODEC_ID_MPEG4 },
754     sizeof(ParseContext1),
755     mpeg4video_parse_init,
756     mpeg4video_parse,
757     parse1_close,
758 };
759
760 AVCodecParser mpegaudio_parser = {
761     { CODEC_ID_MP2, CODEC_ID_MP3 },
762     sizeof(MpegAudioParseContext),
763     mpegaudio_parse_init,
764     mpegaudio_parse,
765     NULL,
766 };
767
768 #ifdef CONFIG_AC3
769 AVCodecParser ac3_parser = {
770     { CODEC_ID_AC3 },
771     sizeof(AC3ParseContext),
772     ac3_parse_init,
773     ac3_parse,
774     NULL,
775 };
776 #endif