OSDN Git Service

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