OSDN Git Service

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