Statistics
| Branch: | Revision:

ffmpeg / libavcodec / parser.c @ 8a01fc47

History | View | Annotate | Download (24.2 KB)

1 8424cf50 Fabrice Bellard
/*
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 99f06236 Michael Niedermayer
            parser->codec_ids[2] == codec_id ||
42
            parser->codec_ids[3] == codec_id ||
43
            parser->codec_ids[4] == codec_id)
44 8424cf50 Fabrice Bellard
            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 a62aecce Michael Niedermayer
    s->fetch_timestamp=1;
66 8424cf50 Fabrice Bellard
    return s;
67
}
68
69 6e45e928 Fabrice Bellard
/* NOTE: buf_size == 0 is used to signal EOF so that the last frame
70
   can be returned if necessary */
71 8424cf50 Fabrice Bellard
int av_parser_parse(AVCodecParserContext *s, 
72
                    AVCodecContext *avctx,
73
                    uint8_t **poutbuf, int *poutbuf_size, 
74 b84f2a35 Fabrice Bellard
                    const uint8_t *buf, int buf_size,
75
                    int64_t pts, int64_t dts)
76 8424cf50 Fabrice Bellard
{
77 b84f2a35 Fabrice Bellard
    int index, i, k;
78 6e45e928 Fabrice Bellard
    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 b84f2a35 Fabrice Bellard
    } 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 a62aecce Michael Niedermayer
        if (s->fetch_timestamp){
94
            s->fetch_timestamp=0;
95 b84f2a35 Fabrice Bellard
            s->last_pts = pts;
96
            s->last_dts = dts;
97 c77a9a0e Michael Niedermayer
            s->cur_frame_pts[k] =
98
            s->cur_frame_dts[k] = AV_NOPTS_VALUE;
99 b84f2a35 Fabrice Bellard
        }
100 6e45e928 Fabrice Bellard
    }
101
102 8424cf50 Fabrice Bellard
    /* WARNING: the returned index can be negative */
103
    index = s->parser->parser_parse(s, avctx, poutbuf, poutbuf_size, buf, buf_size);
104 c77a9a0e Michael Niedermayer
//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 8424cf50 Fabrice Bellard
    /* update the file pointer */
106
    if (*poutbuf_size) {
107 b84f2a35 Fabrice Bellard
        /* fill the data for the current frame */
108 8424cf50 Fabrice Bellard
        s->frame_offset = s->last_frame_offset;
109 b84f2a35 Fabrice Bellard
        s->pts = s->last_pts;
110
        s->dts = s->last_dts;
111
        
112
        /* offset of the next frame */
113 8424cf50 Fabrice Bellard
        s->last_frame_offset = s->cur_offset + index;
114 b84f2a35 Fabrice Bellard
        /* 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 a62aecce Michael Niedermayer
127 b84f2a35 Fabrice Bellard
        s->last_pts = s->cur_frame_pts[k];
128
        s->last_dts = s->cur_frame_dts[k];
129 a62aecce Michael Niedermayer
        
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 8424cf50 Fabrice Bellard
    }
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 e4cb187d Michael Niedermayer
    ParseContext pc;
162
/* XXX/FIXME PC1 vs. PC */
163 8424cf50 Fabrice Bellard
    /* MPEG2 specific */
164
    int frame_rate;
165
    int progressive_sequence;
166
    int width, height;
167 c6f353ff Fabrice Bellard
168 8424cf50 Fabrice Bellard
    /* XXX: suppress that, needed by MPEG4 */
169
    MpegEncContext *enc;
170 c6f353ff Fabrice Bellard
    int first_picture;
171 8424cf50 Fabrice Bellard
} ParseContext1;
172
173
/**
174
 * combines the (truncated) bitstream to a complete frame
175
 * @returns -1 if no complete frame could be created
176
 */
177 e4cb187d Michael Niedermayer
int ff_combine_frame(ParseContext *pc, int next, uint8_t **buf, int *buf_size)
178 8424cf50 Fabrice Bellard
{
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
    pc->last_index= pc->index;
192
193
    /* copy into buffer end return */
194
    if(next == END_NOT_FOUND){
195
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
196
197
        memcpy(&pc->buffer[pc->index], *buf, *buf_size);
198
        pc->index += *buf_size;
199
        return -1;
200
    }
201
202
    *buf_size=
203
    pc->overread_index= pc->index + next;
204
    
205
    /* append to buffer */
206
    if(pc->index){
207
        pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
208
209
        memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
210
        pc->index = 0;
211
        *buf= pc->buffer;
212
    }
213
214
    /* store overread bytes */
215
    for(;next < 0; next++){
216
        pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
217
        pc->overread++;
218
    }
219
220
#if 0
221
    if(pc->overread){
222
        printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
223
        printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
224
    }
225
#endif
226
227
    return 0;
228
}
229
230
static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
231
{
232
    const uint8_t *buf_ptr;
233
    unsigned int state=0xFFFFFFFF, v;
234
    int val;
235
236
    buf_ptr = *pbuf_ptr;
237
    while (buf_ptr < buf_end) {
238
        v = *buf_ptr++;
239
        if (state == 0x000001) {
240
            state = ((state << 8) | v) & 0xffffff;
241
            val = state;
242
            goto found;
243
        }
244
        state = ((state << 8) | v) & 0xffffff;
245
    }
246
    val = -1;
247
 found:
248
    *pbuf_ptr = buf_ptr;
249
    return val;
250
}
251
252
/* XXX: merge with libavcodec ? */
253
#define MPEG1_FRAME_RATE_BASE 1001
254
255
static const int frame_rate_tab[16] = {
256
        0,        
257
    24000,
258
    24024,
259
    25025,
260
    30000,
261
    30030,
262
    50050,
263
    60000,
264
    60060,
265
  // Xing's 15fps: (9)
266
    15015,
267
  // libmpeg3's "Unofficial economy rates": (10-13)
268
     5005,
269
    10010,
270
    12012,
271
    15015,
272
  // random, just to avoid segfault !never encode these
273
    25025,
274
    25025,
275
};
276
277
static void mpegvideo_extract_headers(AVCodecParserContext *s, 
278
                                      AVCodecContext *avctx,
279
                                      const uint8_t *buf, int buf_size)
280
{
281
    ParseContext1 *pc = s->priv_data;
282
    const uint8_t *buf_end;
283
    int32_t start_code;
284
    int frame_rate_index, ext_type, bytes_left;
285
    int frame_rate_ext_n, frame_rate_ext_d;
286 8dab64b6 Michael Niedermayer
    int picture_structure, top_field_first, repeat_first_field, progressive_frame;
287 8424cf50 Fabrice Bellard
    int horiz_size_ext, vert_size_ext;
288
289
    s->repeat_pict = 0;
290
    buf_end = buf + buf_size;
291
    while (buf < buf_end) {
292
        start_code = find_start_code(&buf, buf_end);
293
        bytes_left = buf_end - buf;
294
        switch(start_code) {
295
        case PICTURE_START_CODE:
296
            if (bytes_left >= 2) {
297
                s->pict_type = (buf[1] >> 3) & 7;
298
            }
299
            break;
300
        case SEQ_START_CODE:
301
            if (bytes_left >= 4) {
302 0b2346d3 Michael Niedermayer
                pc->width  = (buf[0] << 4) | (buf[1] >> 4);
303
                pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
304 21adafec Michael Niedermayer
                avcodec_set_dimensions(avctx, pc->width, pc->height);
305 8424cf50 Fabrice Bellard
                frame_rate_index = buf[3] & 0xf;
306
                pc->frame_rate = avctx->frame_rate = frame_rate_tab[frame_rate_index];
307
                avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE;
308 3e9d718e Fabrice Bellard
                avctx->codec_id = CODEC_ID_MPEG1VIDEO;
309
                avctx->sub_id = 1;
310 8424cf50 Fabrice Bellard
            }
311
            break;
312
        case EXT_START_CODE:
313
            if (bytes_left >= 1) {
314
                ext_type = (buf[0] >> 4);
315
                switch(ext_type) {
316
                case 0x1: /* sequence extension */
317
                    if (bytes_left >= 6) {
318
                        horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
319
                        vert_size_ext = (buf[2] >> 5) & 3;
320
                        frame_rate_ext_n = (buf[5] >> 5) & 3;
321
                        frame_rate_ext_d = (buf[5] & 0x1f);
322
                        pc->progressive_sequence = buf[1] & (1 << 3);
323
324 0b2346d3 Michael Niedermayer
                        pc->width  |=(horiz_size_ext << 12);
325
                        pc->height |=( vert_size_ext << 12);
326 21adafec Michael Niedermayer
                        avcodec_set_dimensions(avctx, pc->width, pc->height);
327 8424cf50 Fabrice Bellard
                        avctx->frame_rate = pc->frame_rate * (frame_rate_ext_n + 1);
328
                        avctx->frame_rate_base = MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d + 1);
329 3e9d718e Fabrice Bellard
                        avctx->codec_id = CODEC_ID_MPEG2VIDEO;
330 8424cf50 Fabrice Bellard
                        avctx->sub_id = 2; /* forces MPEG2 */
331
                    }
332
                    break;
333
                case 0x8: /* picture coding extension */
334
                    if (bytes_left >= 5) {
335 5bb994e2 Michael Niedermayer
                        picture_structure = buf[2]&3;
336 8424cf50 Fabrice Bellard
                        top_field_first = buf[3] & (1 << 7);
337
                        repeat_first_field = buf[3] & (1 << 1);
338
                        progressive_frame = buf[4] & (1 << 7);
339
                    
340
                        /* check if we must repeat the frame */
341
                        if (repeat_first_field) {
342
                            if (pc->progressive_sequence) {
343
                                if (top_field_first)
344
                                    s->repeat_pict = 4;
345
                                else
346
                                    s->repeat_pict = 2;
347
                            } else if (progressive_frame) {
348
                                s->repeat_pict = 1;
349
                            }
350
                        }
351 8dab64b6 Michael Niedermayer
                        
352
                        /* the packet only represents half a frame 
353
                           XXX,FIXME maybe find a different solution */
354
                        if(picture_structure != 3)
355
                            s->repeat_pict = -1;
356 8424cf50 Fabrice Bellard
                    }
357
                    break;
358
                }
359
            }
360
            break;
361
        case -1:
362
            goto the_end;
363
        default:
364
            /* we stop parsing when we encounter a slice. It ensures
365
               that this function takes a negligible amount of time */
366
            if (start_code >= SLICE_MIN_START_CODE && 
367
                start_code <= SLICE_MAX_START_CODE)
368
                goto the_end;
369
            break;
370
        }
371
    }
372
 the_end: ;
373
}
374
375
static int mpegvideo_parse(AVCodecParserContext *s,
376
                           AVCodecContext *avctx,
377
                           uint8_t **poutbuf, int *poutbuf_size, 
378
                           const uint8_t *buf, int buf_size)
379
{
380 e4cb187d Michael Niedermayer
    ParseContext1 *pc1 = s->priv_data;
381
    ParseContext *pc= &pc1->pc;
382 8424cf50 Fabrice Bellard
    int next;
383
    
384 e4cb187d Michael Niedermayer
    next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
385 8424cf50 Fabrice Bellard
    
386 e4cb187d Michael Niedermayer
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
387 8424cf50 Fabrice Bellard
        *poutbuf = NULL;
388
        *poutbuf_size = 0;
389
        return buf_size;
390
    }
391
    /* we have a full frame : we just parse the first few MPEG headers
392
       to have the full timing information. The time take by this
393
       function should be negligible for uncorrupted streams */
394
    mpegvideo_extract_headers(s, avctx, buf, buf_size);
395
#if 0
396
    printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n", 
397
           s->pict_type, (double)avctx->frame_rate / avctx->frame_rate_base, s->repeat_pict);
398
#endif
399
400
    *poutbuf = (uint8_t *)buf;
401
    *poutbuf_size = buf_size;
402
    return next;
403
}
404
405 e4cb187d Michael Niedermayer
void ff_parse_close(AVCodecParserContext *s)
406 8424cf50 Fabrice Bellard
{
407 e4cb187d Michael Niedermayer
    ParseContext *pc = s->priv_data;
408 8424cf50 Fabrice Bellard
409
    av_free(pc->buffer);
410
}
411
412 e4cb187d Michael Niedermayer
static void parse1_close(AVCodecParserContext *s)
413 8424cf50 Fabrice Bellard
{
414 e4cb187d Michael Niedermayer
    ParseContext1 *pc1 = s->priv_data;
415 8424cf50 Fabrice Bellard
416 e4cb187d Michael Niedermayer
    av_free(pc1->pc.buffer);
417
    av_free(pc1->enc);
418 8424cf50 Fabrice Bellard
}
419
420 e4cb187d Michael Niedermayer
/*************************/
421
422 8424cf50 Fabrice Bellard
/* used by parser */
423
/* XXX: make it use less memory */
424
static int av_mpeg4_decode_header(AVCodecParserContext *s1, 
425
                                  AVCodecContext *avctx,
426
                                  const uint8_t *buf, int buf_size)
427
{
428
    ParseContext1 *pc = s1->priv_data;
429
    MpegEncContext *s = pc->enc;
430
    GetBitContext gb1, *gb = &gb1;
431
    int ret;
432
433
    s->avctx = avctx;
434 c6f353ff Fabrice Bellard
    s->current_picture_ptr = &s->current_picture;
435
436
    if (avctx->extradata_size && pc->first_picture){
437
        init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
438
        ret = ff_mpeg4_decode_picture_header(s, gb);
439
    }
440
441 8424cf50 Fabrice Bellard
    init_get_bits(gb, buf, 8 * buf_size);
442
    ret = ff_mpeg4_decode_picture_header(s, gb);
443
    if (s->width) {
444 21adafec Michael Niedermayer
        avcodec_set_dimensions(avctx, s->width, s->height);
445 8424cf50 Fabrice Bellard
    }
446 c6f353ff Fabrice Bellard
    pc->first_picture = 0;
447 8424cf50 Fabrice Bellard
    return ret;
448
}
449
450 e96682e6 Michael Niedermayer
static int mpeg4video_parse_init(AVCodecParserContext *s)
451 8424cf50 Fabrice Bellard
{
452
    ParseContext1 *pc = s->priv_data;
453 c6f353ff Fabrice Bellard
454 8424cf50 Fabrice Bellard
    pc->enc = av_mallocz(sizeof(MpegEncContext));
455
    if (!pc->enc)
456
        return -1;
457 c6f353ff Fabrice Bellard
    pc->first_picture = 1;
458 8424cf50 Fabrice Bellard
    return 0;
459
}
460
461
static int mpeg4video_parse(AVCodecParserContext *s,
462
                           AVCodecContext *avctx,
463
                           uint8_t **poutbuf, int *poutbuf_size, 
464
                           const uint8_t *buf, int buf_size)
465
{
466 e4cb187d Michael Niedermayer
    ParseContext *pc = s->priv_data;
467 8424cf50 Fabrice Bellard
    int next;
468
    
469 e4cb187d Michael Niedermayer
    next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
470 8424cf50 Fabrice Bellard
471 e4cb187d Michael Niedermayer
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
472 8424cf50 Fabrice Bellard
        *poutbuf = NULL;
473
        *poutbuf_size = 0;
474
        return buf_size;
475
    }
476
    av_mpeg4_decode_header(s, avctx, buf, buf_size);
477
478
    *poutbuf = (uint8_t *)buf;
479
    *poutbuf_size = buf_size;
480
    return next;
481
}
482
483
/*************************/
484
485
typedef struct MpegAudioParseContext {
486
    uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];        /* input buffer */
487
    uint8_t *inbuf_ptr;
488
    int frame_size;
489
    int free_format_frame_size;
490
    int free_format_next_header;
491
} MpegAudioParseContext;
492
493
#define MPA_HEADER_SIZE 4
494
495
/* header + layer + bitrate + freq + lsf/mpeg25 */
496
#define SAME_HEADER_MASK \
497
   (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
498
499
static int mpegaudio_parse_init(AVCodecParserContext *s1)
500
{
501
    MpegAudioParseContext *s = s1->priv_data;
502
    s->inbuf_ptr = s->inbuf;
503
    return 0;
504
}
505
506
static int mpegaudio_parse(AVCodecParserContext *s1,
507
                           AVCodecContext *avctx,
508
                           uint8_t **poutbuf, int *poutbuf_size, 
509
                           const uint8_t *buf, int buf_size)
510
{
511
    MpegAudioParseContext *s = s1->priv_data;
512
    int len, ret;
513
    uint32_t header;
514
    const uint8_t *buf_ptr;
515
516
    *poutbuf = NULL;
517
    *poutbuf_size = 0;
518
    buf_ptr = buf;
519
    while (buf_size > 0) {
520
        len = s->inbuf_ptr - s->inbuf;
521
        if (s->frame_size == 0) {
522
            /* special case for next header for first frame in free
523
               format case (XXX: find a simpler method) */
524
            if (s->free_format_next_header != 0) {
525
                s->inbuf[0] = s->free_format_next_header >> 24;
526
                s->inbuf[1] = s->free_format_next_header >> 16;
527
                s->inbuf[2] = s->free_format_next_header >> 8;
528
                s->inbuf[3] = s->free_format_next_header;
529
                s->inbuf_ptr = s->inbuf + 4;
530
                s->free_format_next_header = 0;
531
                goto got_header;
532
            }
533
            /* no header seen : find one. We need at least MPA_HEADER_SIZE
534
               bytes to parse it */
535
            len = MPA_HEADER_SIZE - len;
536
            if (len > buf_size)
537
                len = buf_size;
538
            if (len > 0) {
539
                memcpy(s->inbuf_ptr, buf_ptr, len);
540
                buf_ptr += len;
541
                buf_size -= len;
542
                s->inbuf_ptr += len;
543
            }
544
            if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
545
            got_header:
546
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
547
                    (s->inbuf[2] << 8) | s->inbuf[3];
548
549
                ret = mpa_decode_header(avctx, header);
550
                if (ret < 0) {
551
                    /* no sync found : move by one byte (inefficient, but simple!) */
552
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
553
                    s->inbuf_ptr--;
554
                    dprintf("skip %x\n", header);
555
                    /* reset free format frame size to give a chance
556
                       to get a new bitrate */
557
                    s->free_format_frame_size = 0;
558
                } else {
559
                    s->frame_size = ret;
560
#if 0
561
                    /* free format: prepare to compute frame size */
562
                    if (decode_header(s, header) == 1) {
563
                        s->frame_size = -1;
564
                    }
565
#endif
566
                }
567
            }
568
        } else 
569
#if 0
570
        if (s->frame_size == -1) {
571
            /* free format : find next sync to compute frame size */
572
            len = MPA_MAX_CODED_FRAME_SIZE - len;
573
            if (len > buf_size)
574
                len = buf_size;
575
            if (len == 0) {
576
                /* frame too long: resync */
577
                s->frame_size = 0;
578
                memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
579
                s->inbuf_ptr--;
580
            } else {
581
                uint8_t *p, *pend;
582
                uint32_t header1;
583
                int padding;
584

585
                memcpy(s->inbuf_ptr, buf_ptr, len);
586
                /* check for header */
587
                p = s->inbuf_ptr - 3;
588
                pend = s->inbuf_ptr + len - 4;
589
                while (p <= pend) {
590
                    header = (p[0] << 24) | (p[1] << 16) |
591
                        (p[2] << 8) | p[3];
592
                    header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
593
                        (s->inbuf[2] << 8) | s->inbuf[3];
594
                    /* check with high probability that we have a
595
                       valid header */
596
                    if ((header & SAME_HEADER_MASK) ==
597
                        (header1 & SAME_HEADER_MASK)) {
598
                        /* header found: update pointers */
599
                        len = (p + 4) - s->inbuf_ptr;
600
                        buf_ptr += len;
601
                        buf_size -= len;
602
                        s->inbuf_ptr = p;
603
                        /* compute frame size */
604
                        s->free_format_next_header = header;
605
                        s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
606
                        padding = (header1 >> 9) & 1;
607
                        if (s->layer == 1)
608
                            s->free_format_frame_size -= padding * 4;
609
                        else
610
                            s->free_format_frame_size -= padding;
611
                        dprintf("free frame size=%d padding=%d\n", 
612
                                s->free_format_frame_size, padding);
613
                        decode_header(s, header1);
614
                        goto next_data;
615
                    }
616
                    p++;
617
                }
618
                /* not found: simply increase pointers */
619
                buf_ptr += len;
620
                s->inbuf_ptr += len;
621
                buf_size -= len;
622
            }
623
        } else 
624
#endif
625
        if (len < s->frame_size) {
626
            if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
627
                s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
628
            len = s->frame_size - len;
629
            if (len > buf_size)
630
                len = buf_size;
631
            memcpy(s->inbuf_ptr, buf_ptr, len);
632
            buf_ptr += len;
633
            s->inbuf_ptr += len;
634
            buf_size -= len;
635
        }
636
        //    next_data:
637
        if (s->frame_size > 0 && 
638
            (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
639
            *poutbuf = s->inbuf;
640
            *poutbuf_size = s->inbuf_ptr - s->inbuf;
641
            s->inbuf_ptr = s->inbuf;
642
            s->frame_size = 0;
643
            break;
644
        }
645
    }
646
    return buf_ptr - buf;
647
}
648
649
#ifdef CONFIG_AC3
650
extern int a52_syncinfo (const uint8_t * buf, int * flags,
651
                         int * sample_rate, int * bit_rate);
652
653
typedef struct AC3ParseContext {
654
    uint8_t inbuf[4096]; /* input buffer */
655
    uint8_t *inbuf_ptr;
656
    int frame_size;
657
    int flags;
658
} AC3ParseContext;
659
660
#define AC3_HEADER_SIZE 7
661
#define A52_LFE 16
662
663
static int ac3_parse_init(AVCodecParserContext *s1)
664
{
665
    AC3ParseContext *s = s1->priv_data;
666
    s->inbuf_ptr = s->inbuf;
667
    return 0;
668
}
669
670
static int ac3_parse(AVCodecParserContext *s1,
671
                     AVCodecContext *avctx,
672
                     uint8_t **poutbuf, int *poutbuf_size, 
673
                     const uint8_t *buf, int buf_size)
674
{
675
    AC3ParseContext *s = s1->priv_data;
676
    const uint8_t *buf_ptr;
677
    int len, sample_rate, bit_rate;
678
    static const int ac3_channels[8] = {
679
        2, 1, 2, 3, 3, 4, 4, 5
680
    };
681
682
    *poutbuf = NULL;
683
    *poutbuf_size = 0;
684
685
    buf_ptr = buf;
686
    while (buf_size > 0) {
687
        len = s->inbuf_ptr - s->inbuf;
688
        if (s->frame_size == 0) {
689
            /* no header seen : find one. We need at least 7 bytes to parse it */
690
            len = AC3_HEADER_SIZE - len;
691
            if (len > buf_size)
692
                len = buf_size;
693
            memcpy(s->inbuf_ptr, buf_ptr, len);
694
            buf_ptr += len;
695
            s->inbuf_ptr += len;
696
            buf_size -= len;
697
            if ((s->inbuf_ptr - s->inbuf) == AC3_HEADER_SIZE) {
698
                len = a52_syncinfo(s->inbuf, &s->flags, &sample_rate, &bit_rate);
699
                if (len == 0) {
700
                    /* no sync found : move by one byte (inefficient, but simple!) */
701
                    memmove(s->inbuf, s->inbuf + 1, AC3_HEADER_SIZE - 1);
702
                    s->inbuf_ptr--;
703
                } else {
704
                    s->frame_size = len;
705
                    /* update codec info */
706
                    avctx->sample_rate = sample_rate;
707 20da3179 Michael Niedermayer
                    /* set channels,except if the user explicitly requests 1 or 2 channels, XXX/FIXME this is a bit ugly */
708
                    if(avctx->channels!=1 && avctx->channels!=2){
709
                        avctx->channels = ac3_channels[s->flags & 7];
710
                        if (s->flags & A52_LFE)
711
                            avctx->channels++;
712
                    }
713 8424cf50 Fabrice Bellard
                    avctx->bit_rate = bit_rate;
714
                    avctx->frame_size = 6 * 256;
715
                }
716
            }
717
        } else if (len < s->frame_size) {
718
            len = s->frame_size - len;
719
            if (len > buf_size)
720
                len = buf_size;
721
722
            memcpy(s->inbuf_ptr, buf_ptr, len);
723
            buf_ptr += len;
724
            s->inbuf_ptr += len;
725
            buf_size -= len;
726
        } else {
727
            *poutbuf = s->inbuf;
728
            *poutbuf_size = s->frame_size;
729
            s->inbuf_ptr = s->inbuf;
730
            s->frame_size = 0;
731
            break;
732
        }
733
    }
734
    return buf_ptr - buf;
735
}
736
#endif
737
738
AVCodecParser mpegvideo_parser = {
739
    { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
740
    sizeof(ParseContext1),
741
    NULL,
742
    mpegvideo_parse,
743 e4cb187d Michael Niedermayer
    parse1_close,
744 8424cf50 Fabrice Bellard
};
745
746
AVCodecParser mpeg4video_parser = {
747
    { CODEC_ID_MPEG4 },
748
    sizeof(ParseContext1),
749
    mpeg4video_parse_init,
750
    mpeg4video_parse,
751 e4cb187d Michael Niedermayer
    parse1_close,
752 8424cf50 Fabrice Bellard
};
753
754
AVCodecParser mpegaudio_parser = {
755
    { CODEC_ID_MP2, CODEC_ID_MP3 },
756
    sizeof(MpegAudioParseContext),
757
    mpegaudio_parse_init,
758
    mpegaudio_parse,
759
    NULL,
760
};
761
762
#ifdef CONFIG_AC3
763
AVCodecParser ac3_parser = {
764
    { CODEC_ID_AC3 },
765
    sizeof(AC3ParseContext),
766
    ac3_parse_init,
767
    ac3_parse,
768
    NULL,
769
};
770
#endif