OSDN Git Service

gitattirubes test 2
[handbrake-jp/handbrake-jp.git] / contrib / patch-ffmpeg-latm.patch
1 Index: libavcodec/Makefile
2 ===================================================================
3 --- libavcodec/Makefile (revision 14016)
4 +++ libavcodec/Makefile (working copy)
5 @@ -322,7 +322,7 @@
6  OBJS-$(CONFIG_LIBDIRAC_DECODER)        += libdiracdec.o
7  OBJS-$(CONFIG_LIBDIRAC_ENCODER)        += libdiracenc.o libdirac_libschro.o
8  OBJS-$(CONFIG_LIBFAAC)                 += libfaac.o
9 -OBJS-$(CONFIG_LIBFAAD)                 += libfaad.o
10 +OBJS-$(CONFIG_LIBFAAD)                 += libfaad.o latmaac.o
11  OBJS-$(CONFIG_LIBGSM)                  += libgsm.o
12  OBJS-$(CONFIG_LIBMP3LAME)              += libmp3lame.o
13  OBJS-$(CONFIG_LIBSCHROEDINGER_DECODER) += libschroedingerdec.o libschroedinger.o libdirac_libschro.o
14 @@ -333,7 +333,7 @@
15  OBJS-$(CONFIG_LIBXVID)                 += libxvidff.o libxvid_rc.o
16  
17  
18 -OBJS-$(CONFIG_AAC_PARSER)              += aac_parser.o aac_ac3_parser.o mpeg4audio.o
19 +OBJS-$(CONFIG_AAC_PARSER)              += aac_parser.o aac_ac3_parser.o mpeg4audio.o latm_parser.o
20  OBJS-$(CONFIG_AC3_PARSER)              += ac3_parser.o ac3tab.o aac_ac3_parser.o
21  OBJS-$(CONFIG_CAVSVIDEO_PARSER)        += cavs_parser.o
22  OBJS-$(CONFIG_DCA_PARSER)              += dca_parser.o
23 Index: libavcodec/latmaac.c
24 ===================================================================
25 --- libavcodec/latmaac.c        (revision 0)
26 +++ libavcodec/latmaac.c        (revision 0)
27 @@ -0,0 +1,624 @@
28 +/*
29 + * copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
30 + *
31 + * This file is part of FFmpeg.
32 + *
33 + * FFmpeg is free software; you can redistribute it and/or
34 + * modify it under the terms of the GNU Lesser General Public
35 + * License as published by the Free Software Foundation; either
36 + * version 2.1 of the License, or (at your option) any later version.
37 + *
38 + * FFmpeg is distributed in the hope that it will be useful,
39 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41 + * Lesser General Public License for more details.
42 + *
43 + * You should have received a copy of the GNU Lesser General Public
44 + * License along with FFmpeg; if not, write to the Free Software
45 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46 + */
47 +
48 +/**
49 + * @file latmaac.c
50 + * LATM wrapped AAC decoder
51 + */
52 +
53 +#include <stdio.h>
54 +#include <stdlib.h>
55 +#include <string.h>
56 +#include <math.h>
57 +#include <sys/types.h>
58 +
59 +#include "parser.h"
60 +#include "bitstream.h"
61 +#include "mpeg4audio.h"
62 +#include "neaacdec.h"
63 +
64 +#define min(a,b) ((a)<(b) ? (a) : (b))
65 +
66 +
67 +/*
68 +    Note: This decoder filter is intended to decode LATM streams transferred
69 +    in MPEG transport streams which are only supposed to contain one program.
70 +    To do a more complex LATM demuxing a separate LATM demuxer should be used.
71 +*/
72 +
73 +#define AAC_NONE 0            // mode not detected (or indicated in mediatype)
74 +#define AAC_LATM 1            // LATM packets (ISO/IEC 14496-3  1.7.3 Multiplex layer)
75 +
76 +#define SYNC_LATM 0x2b7            // 11 bits
77 +
78 +#define MAX_SIZE 8*1024
79 +
80 +typedef struct AACConfig
81 +{
82 +    uint8_t    extra[64];            // should be way enough
83 +    int        extrasize;
84 +
85 +    int        audioObjectType;
86 +    int        samplingFrequencyIndex;
87 +    int        samplingFrequency;
88 +    int        channelConfiguration;
89 +    int        channels;
90 +} AACConfig;
91 +
92 +typedef struct AACParser
93 +{
94 +    AACConfig          config;
95 +    uint8_t            frameLengthType;
96 +    uint16_t           muxSlotLengthBytes;
97 +
98 +    uint8_t            audio_mux_version;
99 +    uint8_t            audio_mux_version_A;
100 +    int                taraFullness;
101 +    uint8_t            config_crc;
102 +    int64_t            other_data_bits;
103 +
104 +    int                mode;
105 +    int                offset;        // byte offset in "buf" buffer
106 +    uint8_t            buf[MAX_SIZE]; // allocated buffer
107 +    int                count;         // number of bytes written in buffer
108 +} AACParser;
109 +
110 +typedef struct AACDecoder 
111 +{
112 +    AACParser          *parser;
113 +    faacDecHandle      aac_decoder;
114 +    int                open;
115 +    uint32_t           in_samplerate;
116 +    uint8_t            in_channels;
117 +} AACDecoder;
118 +
119 +typedef struct {
120 +    AACDecoder*        decoder;
121 +} FAACContext;
122 +
123 +static inline int64_t latm_get_value(GetBitContext *b)
124 +{
125 +    uint8_t bytesForValue = get_bits(b, 2);
126 +    int64_t value = 0;
127 +    int i;
128 +    for (i=0; i<=bytesForValue; i++) {
129 +        value <<= 8;
130 +        value |= get_bits(b, 8);
131 +    }
132 +    return value;
133 +}
134 +
135 +static void readGASpecificConfig(struct AACConfig *cfg, GetBitContext *b, PutBitContext *o)
136 +{
137 +    int framelen_flag = get_bits(b, 1);
138 +    put_bits(o, 1, framelen_flag);
139 +    int dependsOnCoder = get_bits(b, 1);
140 +    put_bits(o, 1, dependsOnCoder);
141 +    int ext_flag;
142 +    int delay;
143 +    int layerNr;
144 +
145 +    if (dependsOnCoder) {
146 +        delay = get_bits(b, 14);
147 +        put_bits(o, 14, delay);
148 +    }
149 +    ext_flag = get_bits(b, 1);
150 +    put_bits(o, 1, ext_flag);
151 +    if (!cfg->channelConfiguration) {
152 +        // program config element
153 +        // TODO:
154 +    }
155 +
156 +    if (cfg->audioObjectType == 6 || cfg->audioObjectType == 20) {
157 +        layerNr = get_bits(b, 3);
158 +        put_bits(o, 3, layerNr);
159 +    }
160 +    if (ext_flag) {
161 +        if (cfg->audioObjectType == 22) {
162 +            skip_bits(b, 5);                    // numOfSubFrame
163 +            skip_bits(b, 11);                    // layer_length
164 +
165 +            put_bits(o, 16, 0);
166 +        }
167 +        if (cfg->audioObjectType == 17 ||
168 +            cfg->audioObjectType == 19 ||
169 +            cfg->audioObjectType == 20 ||
170 +            cfg->audioObjectType == 23) {
171 +
172 +            skip_bits(b, 3);                    // stuff
173 +            put_bits(o, 3, 0);
174 +        }
175 +
176 +        skip_bits(b, 1);                        // extflag3
177 +        put_bits(o, 1, 0);
178 +    }
179 +}
180 +
181 +static int readAudioSpecificConfig(struct AACConfig *cfg, GetBitContext *b)
182 +{
183 +    PutBitContext o;
184 +    init_put_bits(&o, cfg->extra, sizeof(cfg->extra));
185 +
186 +    // returns the number of bits read
187 +    int ret = 0;
188 +    int sbr_present = -1;
189 +
190 +    // object
191 +    cfg->audioObjectType = get_bits(b, 5);
192 +        put_bits(&o, 5, cfg->audioObjectType);
193 +    if (cfg->audioObjectType == 31) {
194 +        uint8_t n = get_bits(b, 6);
195 +        put_bits(&o, 6, n);
196 +        cfg->audioObjectType = 32 + n;
197 +    }
198 +
199 +    cfg->samplingFrequencyIndex = get_bits(b, 4);
200 +    cfg->samplingFrequency = ff_mpeg4audio_sample_rates[cfg->samplingFrequencyIndex];
201 +    put_bits(&o, 4, cfg->samplingFrequencyIndex);
202 +    if (cfg->samplingFrequencyIndex == 0x0f) {
203 +        uint32_t f = get_bits_long(b, 24);
204 +        put_bits(&o, 24, f);
205 +        cfg->samplingFrequency = f;
206 +    }
207 +    cfg->channelConfiguration = get_bits(b, 4);
208 +    put_bits(&o, 4, cfg->channelConfiguration);
209 +    cfg->channels = ff_mpeg4audio_channels[cfg->channelConfiguration];
210 +
211 +    if (cfg->audioObjectType == 5) {
212 +        sbr_present = 1;
213 +
214 +        // TODO: parsing !!!!!!!!!!!!!!!!
215 +    }
216 +
217 +    switch (cfg->audioObjectType) {
218 +    case 1:
219 +    case 2:
220 +    case 3:
221 +    case 4:
222 +    case 6:
223 +    case 7:
224 +    case 17:
225 +    case 19:
226 +    case 20:
227 +    case 21:
228 +    case 22:
229 +    case 23:
230 +        readGASpecificConfig(cfg, b, &o);
231 +        break;
232 +    }
233 +
234 +    if (sbr_present == -1) {
235 +        if (cfg->samplingFrequency <= 24000) {
236 +            cfg->samplingFrequency *= 2;
237 +        }            
238 +    }
239 +
240 +    // count the extradata
241 +    ret = put_bits_count(&o);
242 +    align_put_bits(&o);
243 +    flush_put_bits(&o);
244 +    cfg->extrasize = (ret + 7) >> 3;
245 +    return ret;
246 +}
247 +
248 +static void readStreamMuxConfig(struct AACParser *parser, GetBitContext *b)
249 +{
250 +    parser->audio_mux_version_A = 0;
251 +    parser->audio_mux_version = get_bits(b, 1);
252 +    if (parser->audio_mux_version == 1) {                // audioMuxVersion
253 +        parser->audio_mux_version_A = get_bits(b, 1);
254 +    }
255 +
256 +    if (parser->audio_mux_version_A == 0) {
257 +        if (parser->audio_mux_version == 1) {
258 +            parser->taraFullness = latm_get_value(b);
259 +        }
260 +        get_bits(b, 1);                    // allStreamSameTimeFraming = 1
261 +        get_bits(b, 6);                    // numSubFrames = 0
262 +        get_bits(b, 4);                    // numPrograms = 0
263 +
264 +        // for each program
265 +        get_bits(b, 3);                    // numLayer = 0
266 +
267 +        // for each layer
268 +        if (parser->audio_mux_version == 0) {
269 +            // audio specific config.
270 +            readAudioSpecificConfig(&parser->config, b);
271 +        } else {
272 +            int ascLen = latm_get_value(b);
273 +            ascLen -= readAudioSpecificConfig(&parser->config, b);
274 +
275 +            // fill bits
276 +            while (ascLen > 16) {
277 +                skip_bits(b, 16);
278 +                ascLen -= 16;
279 +            }
280 +            skip_bits(b, ascLen);                    
281 +        }
282 +
283 +        // these are not needed... perhaps
284 +        int frame_length_type = get_bits(b, 3);
285 +        parser->frameLengthType = frame_length_type;
286 +        if (frame_length_type == 0) {
287 +            get_bits(b, 8);
288 +        } else if (frame_length_type == 1) {
289 +            get_bits(b, 9);
290 +        } else if (frame_length_type == 3 ||
291 +            frame_length_type == 4 ||
292 +            frame_length_type == 5) {
293 +            int celp_table_index = get_bits(b, 6);
294 +        } else if (frame_length_type == 6 ||
295 +            frame_length_type == 7) {
296 +            int hvxc_table_index = get_bits(b, 1);
297 +        }
298 +
299 +        // other data
300 +        parser->other_data_bits = 0;
301 +        if (get_bits(b, 1)) {
302 +            // other data present
303 +            if (parser->audio_mux_version == 1) {
304 +                parser->other_data_bits = latm_get_value(b);
305 +            } else {
306 +                // other data not present
307 +                parser->other_data_bits = 0;
308 +                int esc, tmp;
309 +                do {
310 +                    parser->other_data_bits <<= 8;
311 +                    esc = get_bits(b, 1);
312 +                    tmp = get_bits(b, 8);
313 +                    parser->other_data_bits |= tmp;
314 +                } while (esc);
315 +            }
316 +        }
317 +
318 +        // CRC
319 +        if (get_bits(b, 1)) {
320 +            parser->config_crc = get_bits(b, 8);
321 +        }
322 +    } else {
323 +        // tbd
324 +    }
325 +}
326 +
327 +static void readPayloadLengthInfo(struct AACParser *parser, GetBitContext *b)
328 +{
329 +    uint8_t tmp;
330 +    if (parser->frameLengthType == 0) {
331 +        parser->muxSlotLengthBytes = 0;
332 +        do {
333 +            tmp = get_bits(b, 8);
334 +            parser->muxSlotLengthBytes += tmp;
335 +        } while (tmp == 255);
336 +    } else {
337 +        if (parser->frameLengthType == 5 ||
338 +            parser->frameLengthType == 7 ||
339 +            parser->frameLengthType == 3) {
340 +            get_bits(b, 2);
341 +        }
342 +    }
343 +}
344 +
345 +static void readAudioMuxElement(struct AACParser *parser, GetBitContext *b, uint8_t *payload, int *payloadsize)
346 +{
347 +    uint8_t    use_same_mux = get_bits(b, 1);
348 +    if (!use_same_mux) {
349 +        readStreamMuxConfig(parser, b);
350 +    }
351 +
352 +    if (parser->audio_mux_version_A == 0) {
353 +        int j;
354 +
355 +        readPayloadLengthInfo(parser, b);
356 +
357 +        // copy data
358 +        for (j=0; j<parser->muxSlotLengthBytes; j++) {
359 +            *payload++ = get_bits(b, 8);
360 +        }
361 +        *payloadsize = parser->muxSlotLengthBytes;
362 +
363 +        // ignore otherdata
364 +    } else {
365 +        // TBD
366 +    }
367 +}
368 +
369 +static int readAudioSyncStream(struct AACParser *parser, GetBitContext *b, int size, uint8_t *payload, int *payloadsize)
370 +{
371 +    // ISO/IEC 14496-3 Table 1.28 - Syntax of AudioMuxElement()
372 +    if (get_bits(b, 11) != 0x2b7) return -1;        // not LATM
373 +    int muxlength = get_bits(b, 13);
374 +
375 +    if (3+muxlength > size) return 0;            // not enough data
376 +
377 +    readAudioMuxElement(parser, b, payload, payloadsize);
378 +
379 +    // we don't parse anything else here...
380 +    return (3+muxlength);
381 +}
382 +
383 +
384 +static void flush_buf(struct AACParser *parser, int offset) {
385 +    int bytes_to_flush = min(parser->count, offset);
386 +    int left = (parser->count - bytes_to_flush);
387 +
388 +    if (bytes_to_flush > 0) {
389 +        if (left > 0) {
390 +            memcpy(parser->buf, parser->buf+bytes_to_flush, left);
391 +            parser->count = left;
392 +        } else {
393 +            parser->count = 0;
394 +        }
395 +    }
396 +}
397 +
398 +static struct AACParser *latm_create_parser()
399 +{
400 +    struct AACParser *parser = (struct AACParser *)av_malloc(sizeof(struct AACParser));
401 +    memset(parser, 0, sizeof(struct AACParser));
402 +    return parser;
403 +}
404 +
405 +static void latm_destroy_parser(struct AACParser *parser)
406 +{
407 +    av_free(parser);
408 +}
409 +
410 +static void latm_flush(struct AACParser *parser)
411 +{
412 +    parser->offset = 0;
413 +    parser->count = 0;
414 +}
415 +
416 +static void latm_write_data(struct AACParser *parser, uint8_t *data, int len)
417 +{
418 +    // buffer overflow check... just ignore the data before
419 +    if (parser->count + len > MAX_SIZE) {
420 +        flush_buf(parser, parser->offset);
421 +        parser->offset = 0;
422 +        if (parser->count + len > MAX_SIZE) {
423 +            int to_flush = (parser->count+len) - MAX_SIZE;
424 +            flush_buf(parser, to_flush);
425 +        }
426 +    }
427 +
428 +    // append data
429 +    memcpy(parser->buf+parser->count, data, len);
430 +    parser->count += len;
431 +}
432 +
433 +static int latm_parse_packet(struct AACParser *parser, uint8_t *data, int maxsize)
434 +{
435 +    /*
436 +        Return value is either number of bytes parsed or
437 +        -1 when failed.
438 +        0 = need more data.
439 +    */
440 +
441 +    uint8_t    *start = parser->buf + parser->offset;
442 +    int        bytes  = parser->count - parser->offset;
443 +    GetBitContext    b;
444 +    init_get_bits(&b, start, bytes);
445 +
446 +    if (parser->mode == AAC_LATM) {
447 +        int outsize = 0;
448 +        int    ret = readAudioSyncStream(parser, &b, bytes, data, &outsize);
449 +
450 +        if (ret < 0) return -1;
451 +        if (ret == 0) return 0;
452 +
453 +        // update the offset
454 +        parser->offset += ret;
455 +        return outsize;
456 +    }
457 +
458 +    // check for syncwords
459 +    while (bytes > 2) {
460 +        if (show_bits(&b, 11) == SYNC_LATM) {
461 +            // we must parse config first...
462 +            int outsize = 0;
463 +
464 +            // check if there is a complete packet available...
465 +            int ret = readAudioSyncStream(parser, &b, bytes, data, &outsize);
466 +            if (ret < 0) return -1;
467 +            if (ret == 0) return 0;
468 +            parser->offset += ret;
469 +
470 +            parser->mode = AAC_LATM;
471 +            return outsize;
472 +        }
473 +        skip_bits(&b, 8);
474 +        parser->offset++;
475 +        bytes--;
476 +    }
477 +    return 0;
478 +}
479 +
480 +static void aac_filter_close(AACDecoder *decoder)
481 +{
482 +    if (decoder->aac_decoder) {
483 +        NeAACDecClose(decoder->aac_decoder);
484 +        decoder->aac_decoder = NULL;
485 +    }
486 +    decoder->open = 0;
487 +}
488 +
489 +static int aac_decoder_open(AACDecoder *decoder)
490 +{
491 +    if (decoder->aac_decoder) return 0;
492 +
493 +    decoder->aac_decoder = NeAACDecOpen();
494 +    if (!decoder->aac_decoder) return -1;
495 +
496 +    // are we going to initialize from decoder specific info ?
497 +    if (decoder->parser->config.extrasize > 0) {
498 +        char ret = NeAACDecInit2(decoder->aac_decoder, (unsigned char*)decoder->parser->config.extra, decoder->parser->config.extrasize, &decoder->in_samplerate, &decoder->in_channels);
499 +        if (ret < 0) {
500 +            aac_filter_close(decoder);        // gone wrong ?
501 +            return -1;
502 +        }
503 +        decoder->open = 1;
504 +    } else {
505 +        // we'll open the decoder later...
506 +        decoder->open = 0;
507 +    }
508 +    return 0;
509 +}
510 +
511 +AACDecoder *aac_filter_create()
512 +{
513 +    AACDecoder *decoder = (AACDecoder *)av_malloc(sizeof(AACDecoder));
514 +    decoder->parser = latm_create_parser();
515 +    decoder->aac_decoder = NULL;
516 +    decoder->open = 0;
517 +    return (void *)decoder;
518 +}
519 +
520 +void aac_filter_destroy(AACDecoder *decoder)
521 +{
522 +    aac_filter_close(decoder);
523 +    latm_destroy_parser(decoder->parser);
524 +    av_free(decoder);
525 +}
526 +
527 +int aac_filter_receive(AACDecoder *decoder, void *out, int *out_size, uint8_t *data, int size)
528 +{
529 +    uint8_t    tempbuf[32*1024];
530 +    int        ret;
531 +    int        consumed = size;
532 +    int        decoded;
533 +    int        max_size = *out_size;
534 +    
535 +    *out_size = 0;
536 +
537 +    //-------------------------------------------------------------------------
538 +    // Multiplex Parsing
539 +    //-------------------------------------------------------------------------
540 +
541 +    latm_write_data(decoder->parser, data, size);
542 +
543 +    do {
544 +        ret = latm_parse_packet(decoder->parser, tempbuf, sizeof(tempbuf));
545 +                if (ret < 0) {
546 +                        latm_flush(decoder->parser);
547 +                        return consumed;
548 +                }
549 +        if (ret == 0) return consumed;
550 +
551 +        data = tempbuf;
552 +        size = ret;
553 +
554 +        //-------------------------------------------------------------------------
555 +        // Initialize decoder (if necessary)
556 +        //-------------------------------------------------------------------------
557 +        if (!decoder->open) {
558 +            aac_filter_close(decoder);
559 +            if (decoder->parser->mode == AAC_LATM) {
560 +                ret = aac_decoder_open(decoder);
561 +                if (ret < 0) return consumed;
562 +            }
563 +
564 +            if(!decoder->open) return consumed;
565 +        }
566 +
567 +        //-------------------------------------------------------------------------
568 +        // Decode samples
569 +        //-------------------------------------------------------------------------
570 +        NeAACDecFrameInfo    info;
571 +        void *buf = NeAACDecDecode(decoder->aac_decoder, &info, data, size);
572 +
573 +        if (buf) {
574 +            decoder->in_samplerate = info.samplerate;
575 +            decoder->in_channels = info.channels;
576 +
577 +            //---------------------------------------------------------------------
578 +            // Deliver decoded samples
579 +            //---------------------------------------------------------------------
580 +
581 +            // kram dekoduje 16-bit. my vypustame 16-bit. takze by to malo byt okej
582 +            decoded = info.samples * sizeof(short);
583 +
584 +            // napraskame tam sample
585 +            *out_size += decoded;
586 +            if(*out_size > max_size) {
587 +                av_log(NULL, AV_LOG_ERROR, "overflow!\n");
588 +            } else {
589 +                memcpy(out, buf, decoded);
590 +                out = (unsigned char *)out + decoded;
591 +            }
592 +        } else {
593 +            // need more data
594 +            break;
595 +        }
596 +
597 +    } while (1);    // decode all packets
598 +    return consumed;
599 +}
600 +
601 +void aac_filter_getinfo(AACDecoder *decoder, int *sample_rate, int *channels)
602 +{
603 +    if(!decoder->open) return;
604 +    *sample_rate = decoder->in_samplerate;
605 +    *channels = decoder->in_channels;
606 +}
607 +
608 +static int faac_decode_init(AVCodecContext *avctx)
609 +{
610 +    FAACContext *s = avctx->priv_data;
611 +    avctx->frame_size = 360;
612 +    avctx->sample_rate = 48000;
613 +    avctx->channels = 2;
614 +    avctx->bit_rate = 8192 * 8 * avctx->sample_rate / avctx->frame_size;
615 +    s->decoder = aac_filter_create();
616 +    return 0;
617 +}
618 +
619 +static int faac_decode_frame(AVCodecContext *avctx,
620 +                             void *data, int *data_size,
621 +                             uint8_t *buf, int buf_size)
622 +{
623 +    FAACContext *s = avctx->priv_data;
624 +    int ret;
625 +
626 +    if (s->decoder == NULL) faac_decode_init(avctx);
627 +    ret = aac_filter_receive(s->decoder, data, data_size, buf, buf_size);
628 +    aac_filter_getinfo(s->decoder, &(avctx->sample_rate), &(avctx->channels));
629 +    return ret;
630 +}
631 +
632 +static int faac_decode_end(AVCodecContext *avctx)
633 +{
634 +    FAACContext *s = avctx->priv_data;
635 +    if(s->decoder != NULL) {
636 +        aac_filter_destroy(s->decoder);
637 +    }
638 +    return 0;
639 +}
640 +
641 +AVCodec libfaad2_decoder = {
642 +    .name = "AAC_LATM",
643 +    .type = CODEC_TYPE_AUDIO,
644 +    .id = CODEC_ID_AAC_LATM,
645 +    .priv_data_size = sizeof (FAACContext),
646 +    .init = faac_decode_init,
647 +    .close = faac_decode_end,
648 +    .decode = faac_decode_frame,
649 +    .long_name = "AAC over LATM",
650 +};
651 +
652 Index: libavcodec/latm_parser.c
653 ===================================================================
654 --- libavcodec/latm_parser.c    (revision 0)
655 +++ libavcodec/latm_parser.c    (revision 0)
656 @@ -0,0 +1,128 @@
657 +/*
658 + * LATM parser
659 + * Copyright (c) 2008 Paul Kendall <paul@kcbbs.gen.nz>
660 + *
661 + * This file is part of FFmpeg.
662 + *
663 + * FFmpeg is free software; you can redistribute it and/or
664 + * modify it under the terms of the GNU Lesser General Public
665 + * License as published by the Free Software Foundation; either
666 + * version 2.1 of the License, or (at your option) any later version.
667 + *
668 + * FFmpeg is distributed in the hope that it will be useful,
669 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
670 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
671 + * Lesser General Public License for more details.
672 + *
673 + * You should have received a copy of the GNU Lesser General Public
674 + * License along with FFmpeg; if not, write to the Free Software
675 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
676 + */
677 +
678 +/**
679 + * @file latm_parser.c
680 + * LATM parser
681 + */
682 +
683 +#include "parser.h"
684 +
685 +#define LATM_HEADER     0x56e000       // 0x2b7 (11 bits)
686 +#define LATM_MASK       0xFFE000       // top 11 bits
687 +#define LATM_SIZE_MASK  0x001FFF       // bottom 13 bits
688 +
689 +typedef struct LATMParseContext{
690 +    ParseContext pc;
691 +    int count;
692 +} LATMParseContext;
693 +
694 +/**
695 + * finds the end of the current frame in the bitstream.
696 + * @return the position of the first byte of the next frame, or -1
697 + */
698 +static int latm_find_frame_end(AVCodecParserContext *s1, const uint8_t *buf,
699 +                               int buf_size) {
700 +    LATMParseContext *s = s1->priv_data;
701 +    ParseContext *pc = &s->pc;
702 +    int pic_found, i;
703 +    uint32_t state;
704 +
705 +    pic_found = pc->frame_start_found;
706 +    state = pc->state;
707 +
708 +    i = 0;
709 +    if(!pic_found){
710 +        for(i=0; i<buf_size; i++){
711 +            state = (state<<8) | buf[i];
712 +            if((state & LATM_MASK) == LATM_HEADER){
713 +                i++;
714 +                s->count = - i;
715 +                pic_found=1;
716 +                break;
717 +            }
718 +        }
719 +    }
720 +
721 +    if(pic_found){
722 +        /* EOF considered as end of frame */
723 +        if (buf_size == 0)
724 +            return 0;
725 +        if((state & LATM_SIZE_MASK) - s->count <= buf_size) {
726 +            pc->frame_start_found = 0;
727 +            pc->state = -1;
728 +            return (state & LATM_SIZE_MASK) - s->count;
729 +       }
730 +    }
731 +
732 +    s->count += buf_size;
733 +    pc->frame_start_found = pic_found;
734 +    pc->state = state;
735 +    return END_NOT_FOUND;
736 +}
737 +
738 +static int latm_parse(AVCodecParserContext *s1,
739 +                           AVCodecContext *avctx,
740 +                           const uint8_t **poutbuf, int *poutbuf_size,
741 +                           const uint8_t *buf, int buf_size)
742 +{
743 +    LATMParseContext *s = s1->priv_data;
744 +    ParseContext *pc = &s->pc;
745 +    int next;
746 +
747 +    if(s1->flags & PARSER_FLAG_COMPLETE_FRAMES){
748 +        next = buf_size;
749 +    }else{
750 +        next = latm_find_frame_end(s1, buf, buf_size);
751 +
752 +        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
753 +            *poutbuf = NULL;
754 +            *poutbuf_size = 0;
755 +            return buf_size;
756 +        }
757 +    }
758 +    *poutbuf = buf;
759 +    *poutbuf_size = buf_size;
760 +    return next;
761 +}
762 +
763 +static int latm_split(AVCodecContext *avctx,
764 +                           const uint8_t *buf, int buf_size)
765 +{
766 +    int i;
767 +    uint32_t state= -1;
768 +
769 +    for(i=0; i<buf_size; i++){
770 +        state= (state<<8) | buf[i];
771 +        if((state & LATM_MASK) == LATM_HEADER)
772 +            return i-2;
773 +    }
774 +    return 0;
775 +}
776 +
777 +AVCodecParser aac_latm_parser = {
778 +    { CODEC_ID_AAC_LATM },
779 +    sizeof(LATMParseContext),
780 +    NULL,
781 +    latm_parse,
782 +    ff_parse_close,
783 +    latm_split,
784 +};
785 Index: libavcodec/allcodecs.c
786 ===================================================================
787 --- libavcodec/allcodecs.c      (revision 14016)
788 +++ libavcodec/allcodecs.c      (working copy)
789 @@ -280,6 +280,7 @@
790      REGISTER_ENCDEC  (LIBDIRAC, libdirac);
791      REGISTER_ENCODER (LIBFAAC, libfaac);
792      REGISTER_DECODER (LIBFAAD, libfaad);
793 +    REGISTER_DECODER (LIBFAAD, libfaad2);
794      REGISTER_ENCDEC  (LIBGSM, libgsm);
795      REGISTER_ENCDEC  (LIBGSM_MS, libgsm_ms);
796      REGISTER_ENCODER (LIBMP3LAME, libmp3lame);
797 @@ -294,6 +295,7 @@
798  
799      /* parsers */
800      REGISTER_PARSER  (AAC, aac);
801 +    REGISTER_PARSER  (AAC, aac_latm);
802      REGISTER_PARSER  (AC3, ac3);
803      REGISTER_PARSER  (CAVSVIDEO, cavsvideo);
804      REGISTER_PARSER  (DCA, dca);
805 Index: libavcodec/avcodec.h
806 ===================================================================
807 --- libavcodec/avcodec.h        (revision 14016)
808 +++ libavcodec/avcodec.h        (working copy)
809 @@ -259,4 +259,5 @@
810      CODEC_ID_AAC,
811 +    CODEC_ID_AAC_LATM,
812      CODEC_ID_AC3,
813      CODEC_ID_DTS,
814      CODEC_ID_VORBIS,
815 Index: libavformat/mpegts.c
816 ===================================================================
817 --- libavformat/mpegts.c        (revision 14016)
818 +++ libavformat/mpegts.c        (working copy)
819 @@ -611,6 +611,7 @@
820          case STREAM_TYPE_VIDEO_H264:
821          case STREAM_TYPE_VIDEO_VC1:
822          case STREAM_TYPE_AUDIO_AAC:
823 +        case STREAM_TYPE_AUDIO_AAC_LATM:
824          case STREAM_TYPE_AUDIO_AC3:
825          case STREAM_TYPE_AUDIO_DTS:
826          case STREAM_TYPE_AUDIO_HDMV_DTS:
827 @@ -832,7 +833,7 @@
828                      code = pes->header[3] | 0x100;
829                      if (!((code >= 0x1c0 && code <= 0x1df) ||
830                            (code >= 0x1e0 && code <= 0x1ef) ||
831 -                          (code == 0x1bd) || (code == 0x1fd)))
832 +                          (code == 0x1bd) || (code == 0x1fa) || (code == 0x1fd)))
833                          goto skip;
834                      if (!pes->st) {
835                          /* allocate stream */
836 @@ -948,6 +949,10 @@
837          codec_type = CODEC_TYPE_AUDIO;
838          codec_id = CODEC_ID_AAC;
839          break;
840 +    case STREAM_TYPE_AUDIO_AAC_LATM:
841 +        codec_type = CODEC_TYPE_AUDIO;
842 +        codec_id = CODEC_ID_AAC_LATM;
843 +        break;
844      case STREAM_TYPE_AUDIO_AC3:
845          codec_type = CODEC_TYPE_AUDIO;
846          codec_id = CODEC_ID_AC3;
847 Index: libavformat/mpegts.h
848 ===================================================================
849 --- libavformat/mpegts.h        (revision 14016)
850 +++ libavformat/mpegts.h        (working copy)
851 @@ -50,6 +50,7 @@
852  #define STREAM_TYPE_PRIVATE_DATA    0x06
853  #define STREAM_TYPE_AUDIO_AAC       0x0f
854  #define STREAM_TYPE_VIDEO_MPEG4     0x10
855 +#define STREAM_TYPE_AUDIO_AAC_LATM  0x11
856  #define STREAM_TYPE_VIDEO_H264      0x1b
857  #define STREAM_TYPE_VIDEO_VC1       0xea
858  
859 Index: libavformat/mpeg.c
860 ===================================================================
861 --- libavformat/mpeg.c  (revision 14016)
862 +++ libavformat/mpeg.c  (working copy)
863 @@ -281,7 +281,7 @@
864      /* find matching stream */
865      if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
866            (startcode >= 0x1e0 && startcode <= 0x1ef) ||
867 -          (startcode == 0x1bd) || (startcode == 0x1fd)))
868 +          (startcode == 0x1bd) || (startcode == 0x1fa) || (startcode == 0x1fd)))
869          goto redo;
870      if (ppos) {
871          *ppos = url_ftell(s->pb) - 4;
872 @@ -439,6 +439,9 @@
873          } else if(es_type == STREAM_TYPE_AUDIO_AAC){
874              codec_id = CODEC_ID_AAC;
875              type = CODEC_TYPE_AUDIO;
876 +        } else if(es_type == STREAM_TYPE_AUDIO_AAC_LATM){
877 +            codec_id = CODEC_ID_AAC_LATM;
878 +            type = CODEC_TYPE_AUDIO;
879          } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
880              codec_id = CODEC_ID_MPEG4;
881              type = CODEC_TYPE_VIDEO;
882 Index: libavformat/mpeg.h
883 ===================================================================
884 --- libavformat/mpeg.h  (revision 14016)
885 +++ libavformat/mpeg.h  (working copy)
886 @@ -53,6 +53,7 @@
887  #define STREAM_TYPE_PRIVATE_DATA    0x06
888  #define STREAM_TYPE_AUDIO_AAC       0x0f
889  #define STREAM_TYPE_VIDEO_MPEG4     0x10
890 +#define STREAM_TYPE_AUDIO_AAC_LATM  0x11
891  #define STREAM_TYPE_VIDEO_H264      0x1b
892  
893  #define STREAM_TYPE_AUDIO_AC3       0x81