OSDN Git Service

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