OSDN Git Service

x264 bump to r1339-82b80ef
[handbrake-jp/handbrake-jp-git.git] / contrib / ffmpeg / A00-latm.patch
1 diff -Naur ffmpeg-r19067.orig/libavcodec/Makefile ffmpeg-r19067/libavcodec/Makefile
2 --- ffmpeg-r19067.orig/libavcodec/Makefile      2009-05-25 18:19:35.000000000 -0400
3 +++ ffmpeg-r19067/libavcodec/Makefile   2009-06-01 12:39:48.000000000 -0400
4 @@ -382,7 +382,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_LIBOPENJPEG)             += libopenjpeg.o
13 @@ -395,7 +395,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-r19067.orig/libavcodec/allcodecs.c ffmpeg-r19067/libavcodec/allcodecs.c
23 --- ffmpeg-r19067.orig/libavcodec/allcodecs.c   2009-05-12 15:56:48.000000000 -0400
24 +++ ffmpeg-r19067/libavcodec/allcodecs.c        2009-06-01 12:39:48.000000000 -0400
25 @@ -306,6 +306,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 @@ -319,6 +320,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-r19067.orig/libavcodec/avcodec.h ffmpeg-r19067/libavcodec/avcodec.h
42 --- ffmpeg-r19067.orig/libavcodec/avcodec.h     2009-05-31 02:51:18.000000000 -0400
43 +++ ffmpeg-r19067/libavcodec/avcodec.h  2009-06-01 12:39:48.000000000 -0400
44 @@ -271,6 +271,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-r19067.orig/libavcodec/latm_parser.c ffmpeg-r19067/libavcodec/latm_parser.c
53 --- ffmpeg-r19067.orig/libavcodec/latm_parser.c 1969-12-31 19:00:00.000000000 -0500
54 +++ ffmpeg-r19067/libavcodec/latm_parser.c      2009-06-01 12:39:48.000000000 -0400
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-r19067.orig/libavcodec/latmaac.c ffmpeg-r19067/libavcodec/latmaac.c
185 --- ffmpeg-r19067.orig/libavcodec/latmaac.c     1969-12-31 19:00:00.000000000 -0500
186 +++ ffmpeg-r19067/libavcodec/latmaac.c  2009-06-01 12:39:48.000000000 -0400
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 "get_bits.h"
221 +#include "put_bits.h"
222 +#include "mpeg4audio.h"
223 +#include "neaacdec.h"
224 +
225 +#define min(a,b) ((a)<(b) ? (a) : (b))
226 +
227 +
228 +/*
229 +    Note: This decoder filter is intended to decode LATM streams transferred
230 +    in MPEG transport streams which are only supposed to contain one program.
231 +    To do a more complex LATM demuxing a separate LATM demuxer should be used.
232 +*/
233 +
234 +#define AAC_NONE 0            // mode not detected (or indicated in mediatype)
235 +#define AAC_LATM 1            // LATM packets (ISO/IEC 14496-3  1.7.3 Multiplex layer)
236 +
237 +#define SYNC_LATM 0x2b7            // 11 bits
238 +
239 +#define MAX_SIZE 8*1024
240 +
241 +typedef struct AACConfig
242 +{
243 +    uint8_t    extra[64];            // should be way enough
244 +    int        extrasize;
245 +
246 +    int        audioObjectType;
247 +    int        samplingFrequencyIndex;
248 +    int        samplingFrequency;
249 +    int        channelConfiguration;
250 +    int        channels;
251 +} AACConfig;
252 +
253 +typedef struct AACParser
254 +{
255 +    AACConfig          config;
256 +    uint8_t            frameLengthType;
257 +    uint16_t           muxSlotLengthBytes;
258 +
259 +    uint8_t            audio_mux_version;
260 +    uint8_t            audio_mux_version_A;
261 +    int                taraFullness;
262 +    uint8_t            config_crc;
263 +    int64_t            other_data_bits;
264 +
265 +    int                mode;
266 +    int                offset;        // byte offset in "buf" buffer
267 +    uint8_t            buf[MAX_SIZE]; // allocated buffer
268 +    int                count;         // number of bytes written in buffer
269 +} AACParser;
270 +
271 +typedef struct AACDecoder 
272 +{
273 +    AACParser          *parser;
274 +    faacDecHandle      aac_decoder;
275 +    int                open;
276 +    uint32_t           in_samplerate;
277 +    uint8_t            in_channels;
278 +} AACDecoder;
279 +
280 +typedef struct {
281 +    AACDecoder*        decoder;
282 +} FAACContext;
283 +
284 +static inline int64_t latm_get_value(GetBitContext *b)
285 +{
286 +    uint8_t bytesForValue = get_bits(b, 2);
287 +    int64_t value = 0;
288 +    int i;
289 +    for (i=0; i<=bytesForValue; i++) {
290 +        value <<= 8;
291 +        value |= get_bits(b, 8);
292 +    }
293 +    return value;
294 +}
295 +
296 +static void readGASpecificConfig(struct AACConfig *cfg, GetBitContext *b, PutBitContext *o)
297 +{
298 +    int framelen_flag = get_bits(b, 1);
299 +    put_bits(o, 1, framelen_flag);
300 +    int dependsOnCoder = get_bits(b, 1);
301 +    put_bits(o, 1, dependsOnCoder);
302 +    int ext_flag;
303 +    int delay;
304 +    int layerNr;
305 +
306 +    if (dependsOnCoder) {
307 +        delay = get_bits(b, 14);
308 +        put_bits(o, 14, delay);
309 +    }
310 +    ext_flag = get_bits(b, 1);
311 +    put_bits(o, 1, ext_flag);
312 +    if (!cfg->channelConfiguration) {
313 +        // program config element
314 +        // TODO:
315 +    }
316 +
317 +    if (cfg->audioObjectType == 6 || cfg->audioObjectType == 20) {
318 +        layerNr = get_bits(b, 3);
319 +        put_bits(o, 3, layerNr);
320 +    }
321 +    if (ext_flag) {
322 +        if (cfg->audioObjectType == 22) {
323 +            skip_bits(b, 5);                    // numOfSubFrame
324 +            skip_bits(b, 11);                    // layer_length
325 +
326 +            put_bits(o, 16, 0);
327 +        }
328 +        if (cfg->audioObjectType == 17 ||
329 +            cfg->audioObjectType == 19 ||
330 +            cfg->audioObjectType == 20 ||
331 +            cfg->audioObjectType == 23) {
332 +
333 +            skip_bits(b, 3);                    // stuff
334 +            put_bits(o, 3, 0);
335 +        }
336 +
337 +        skip_bits(b, 1);                        // extflag3
338 +        put_bits(o, 1, 0);
339 +    }
340 +}
341 +
342 +static int readAudioSpecificConfig(struct AACConfig *cfg, GetBitContext *b)
343 +{
344 +    PutBitContext o;
345 +    init_put_bits(&o, cfg->extra, sizeof(cfg->extra));
346 +
347 +    // returns the number of bits read
348 +    int ret = 0;
349 +    int sbr_present = -1;
350 +
351 +    // object
352 +    cfg->audioObjectType = get_bits(b, 5);
353 +        put_bits(&o, 5, cfg->audioObjectType);
354 +    if (cfg->audioObjectType == 31) {
355 +        uint8_t n = get_bits(b, 6);
356 +        put_bits(&o, 6, n);
357 +        cfg->audioObjectType = 32 + n;
358 +    }
359 +
360 +    cfg->samplingFrequencyIndex = get_bits(b, 4);
361 +    cfg->samplingFrequency = ff_mpeg4audio_sample_rates[cfg->samplingFrequencyIndex];
362 +    put_bits(&o, 4, cfg->samplingFrequencyIndex);
363 +    if (cfg->samplingFrequencyIndex == 0x0f) {
364 +        uint32_t f = get_bits_long(b, 24);
365 +        put_bits(&o, 24, f);
366 +        cfg->samplingFrequency = f;
367 +    }
368 +    cfg->channelConfiguration = get_bits(b, 4);
369 +    put_bits(&o, 4, cfg->channelConfiguration);
370 +    cfg->channels = ff_mpeg4audio_channels[cfg->channelConfiguration];
371 +
372 +    if (cfg->audioObjectType == 5) {
373 +        sbr_present = 1;
374 +
375 +        // TODO: parsing !!!!!!!!!!!!!!!!
376 +    }
377 +
378 +    switch (cfg->audioObjectType) {
379 +    case 1:
380 +    case 2:
381 +    case 3:
382 +    case 4:
383 +    case 6:
384 +    case 7:
385 +    case 17:
386 +    case 19:
387 +    case 20:
388 +    case 21:
389 +    case 22:
390 +    case 23:
391 +        readGASpecificConfig(cfg, b, &o);
392 +        break;
393 +    }
394 +
395 +    if (sbr_present == -1) {
396 +        if (cfg->samplingFrequency <= 24000) {
397 +            cfg->samplingFrequency *= 2;
398 +        }            
399 +    }
400 +
401 +    // count the extradata
402 +    ret = put_bits_count(&o);
403 +    align_put_bits(&o);
404 +    flush_put_bits(&o);
405 +    cfg->extrasize = (ret + 7) >> 3;
406 +    return ret;
407 +}
408 +
409 +static void readStreamMuxConfig(struct AACParser *parser, GetBitContext *b)
410 +{
411 +    parser->audio_mux_version_A = 0;
412 +    parser->audio_mux_version = get_bits(b, 1);
413 +    if (parser->audio_mux_version == 1) {                // audioMuxVersion
414 +        parser->audio_mux_version_A = get_bits(b, 1);
415 +    }
416 +
417 +    if (parser->audio_mux_version_A == 0) {
418 +        if (parser->audio_mux_version == 1) {
419 +            parser->taraFullness = latm_get_value(b);
420 +        }
421 +        get_bits(b, 1);                    // allStreamSameTimeFraming = 1
422 +        get_bits(b, 6);                    // numSubFrames = 0
423 +        get_bits(b, 4);                    // numPrograms = 0
424 +
425 +        // for each program
426 +        get_bits(b, 3);                    // numLayer = 0
427 +
428 +        // for each layer
429 +        if (parser->audio_mux_version == 0) {
430 +            // audio specific config.
431 +            readAudioSpecificConfig(&parser->config, b);
432 +        } else {
433 +            int ascLen = latm_get_value(b);
434 +            ascLen -= readAudioSpecificConfig(&parser->config, b);
435 +
436 +            // fill bits
437 +            while (ascLen > 16) {
438 +                skip_bits(b, 16);
439 +                ascLen -= 16;
440 +            }
441 +            skip_bits(b, ascLen);                    
442 +        }
443 +
444 +        // these are not needed... perhaps
445 +        int frame_length_type = get_bits(b, 3);
446 +        parser->frameLengthType = frame_length_type;
447 +        if (frame_length_type == 0) {
448 +            get_bits(b, 8);
449 +        } else if (frame_length_type == 1) {
450 +            get_bits(b, 9);
451 +        } else if (frame_length_type == 3 ||
452 +            frame_length_type == 4 ||
453 +            frame_length_type == 5) {
454 +            int celp_table_index = get_bits(b, 6);
455 +        } else if (frame_length_type == 6 ||
456 +            frame_length_type == 7) {
457 +            int hvxc_table_index = get_bits(b, 1);
458 +        }
459 +
460 +        // other data
461 +        parser->other_data_bits = 0;
462 +        if (get_bits(b, 1)) {
463 +            // other data present
464 +            if (parser->audio_mux_version == 1) {
465 +                parser->other_data_bits = latm_get_value(b);
466 +            } else {
467 +                // other data not present
468 +                parser->other_data_bits = 0;
469 +                int esc, tmp;
470 +                do {
471 +                    parser->other_data_bits <<= 8;
472 +                    esc = get_bits(b, 1);
473 +                    tmp = get_bits(b, 8);
474 +                    parser->other_data_bits |= tmp;
475 +                } while (esc);
476 +            }
477 +        }
478 +
479 +        // CRC
480 +        if (get_bits(b, 1)) {
481 +            parser->config_crc = get_bits(b, 8);
482 +        }
483 +    } else {
484 +        // tbd
485 +    }
486 +}
487 +
488 +static void readPayloadLengthInfo(struct AACParser *parser, GetBitContext *b)
489 +{
490 +    uint8_t tmp;
491 +    if (parser->frameLengthType == 0) {
492 +        parser->muxSlotLengthBytes = 0;
493 +        do {
494 +            tmp = get_bits(b, 8);
495 +            parser->muxSlotLengthBytes += tmp;
496 +        } while (tmp == 255);
497 +    } else {
498 +        if (parser->frameLengthType == 5 ||
499 +            parser->frameLengthType == 7 ||
500 +            parser->frameLengthType == 3) {
501 +            get_bits(b, 2);
502 +        }
503 +    }
504 +}
505 +
506 +static void readAudioMuxElement(struct AACParser *parser, GetBitContext *b, uint8_t *payload, int *payloadsize)
507 +{
508 +    uint8_t    use_same_mux = get_bits(b, 1);
509 +    if (!use_same_mux) {
510 +        readStreamMuxConfig(parser, b);
511 +    }
512 +
513 +    if (parser->audio_mux_version_A == 0) {
514 +        int j;
515 +
516 +        readPayloadLengthInfo(parser, b);
517 +
518 +        // copy data
519 +        for (j=0; j<parser->muxSlotLengthBytes; j++) {
520 +            *payload++ = get_bits(b, 8);
521 +        }
522 +        *payloadsize = parser->muxSlotLengthBytes;
523 +
524 +        // ignore otherdata
525 +    } else {
526 +        // TBD
527 +    }
528 +}
529 +
530 +static int readAudioSyncStream(struct AACParser *parser, GetBitContext *b, int size, uint8_t *payload, int *payloadsize)
531 +{
532 +    // ISO/IEC 14496-3 Table 1.28 - Syntax of AudioMuxElement()
533 +    if (get_bits(b, 11) != 0x2b7) return -1;        // not LATM
534 +    int muxlength = get_bits(b, 13);
535 +
536 +    if (3+muxlength > size) return 0;            // not enough data
537 +
538 +    readAudioMuxElement(parser, b, payload, payloadsize);
539 +
540 +    // we don't parse anything else here...
541 +    return (3+muxlength);
542 +}
543 +
544 +
545 +static void flush_buf(struct AACParser *parser, int offset) {
546 +    int bytes_to_flush = min(parser->count, offset);
547 +    int left = (parser->count - bytes_to_flush);
548 +
549 +    if (bytes_to_flush > 0) {
550 +        if (left > 0) {
551 +            memcpy(parser->buf, parser->buf+bytes_to_flush, left);
552 +            parser->count = left;
553 +        } else {
554 +            parser->count = 0;
555 +        }
556 +    }
557 +}
558 +
559 +static struct AACParser *latm_create_parser()
560 +{
561 +    struct AACParser *parser = (struct AACParser *)av_malloc(sizeof(struct AACParser));
562 +    memset(parser, 0, sizeof(struct AACParser));
563 +    return parser;
564 +}
565 +
566 +static void latm_destroy_parser(struct AACParser *parser)
567 +{
568 +    av_free(parser);
569 +}
570 +
571 +static void latm_flush(struct AACParser *parser)
572 +{
573 +    parser->offset = 0;
574 +    parser->count = 0;
575 +}
576 +
577 +static void latm_write_data(struct AACParser *parser, uint8_t *data, int len)
578 +{
579 +    // buffer overflow check... just ignore the data before
580 +    if (parser->count + len > MAX_SIZE) {
581 +        flush_buf(parser, parser->offset);
582 +        parser->offset = 0;
583 +        if (parser->count + len > MAX_SIZE) {
584 +            int to_flush = (parser->count+len) - MAX_SIZE;
585 +            flush_buf(parser, to_flush);
586 +        }
587 +    }
588 +
589 +    // append data
590 +    memcpy(parser->buf+parser->count, data, len);
591 +    parser->count += len;
592 +}
593 +
594 +static int latm_parse_packet(struct AACParser *parser, uint8_t *data, int maxsize)
595 +{
596 +    /*
597 +        Return value is either number of bytes parsed or
598 +        -1 when failed.
599 +        0 = need more data.
600 +    */
601 +
602 +    uint8_t    *start = parser->buf + parser->offset;
603 +    int        bytes  = parser->count - parser->offset;
604 +    GetBitContext    b;
605 +    init_get_bits(&b, start, bytes);
606 +
607 +    if (parser->mode == AAC_LATM) {
608 +        int outsize = 0;
609 +        int    ret = readAudioSyncStream(parser, &b, bytes, data, &outsize);
610 +
611 +        if (ret < 0) return -1;
612 +        if (ret == 0) return 0;
613 +
614 +        // update the offset
615 +        parser->offset += ret;
616 +        return outsize;
617 +    }
618 +
619 +    // check for syncwords
620 +    while (bytes > 2) {
621 +        if (show_bits(&b, 11) == SYNC_LATM) {
622 +            // we must parse config first...
623 +            int outsize = 0;
624 +
625 +            // check if there is a complete packet available...
626 +            int ret = readAudioSyncStream(parser, &b, bytes, data, &outsize);
627 +            if (ret < 0) return -1;
628 +            if (ret == 0) return 0;
629 +            parser->offset += ret;
630 +
631 +            parser->mode = AAC_LATM;
632 +            return outsize;
633 +        }
634 +        skip_bits(&b, 8);
635 +        parser->offset++;
636 +        bytes--;
637 +    }
638 +    return 0;
639 +}
640 +
641 +static void aac_filter_close(AACDecoder *decoder)
642 +{
643 +    if (decoder->aac_decoder) {
644 +        NeAACDecClose(decoder->aac_decoder);
645 +        decoder->aac_decoder = NULL;
646 +    }
647 +    decoder->open = 0;
648 +}
649 +
650 +static int aac_decoder_open(AACDecoder *decoder)
651 +{
652 +    if (decoder->aac_decoder) return 0;
653 +
654 +    decoder->aac_decoder = NeAACDecOpen();
655 +    if (!decoder->aac_decoder) return -1;
656 +
657 +    // are we going to initialize from decoder specific info ?
658 +    if (decoder->parser->config.extrasize > 0) {
659 +        char ret = NeAACDecInit2(decoder->aac_decoder, (unsigned char*)decoder->parser->config.extra, decoder->parser->config.extrasize, &decoder->in_samplerate, &decoder->in_channels);
660 +        if (ret < 0) {
661 +            aac_filter_close(decoder);        // gone wrong ?
662 +            return -1;
663 +        }
664 +        decoder->open = 1;
665 +    } else {
666 +        // we'll open the decoder later...
667 +        decoder->open = 0;
668 +    }
669 +    return 0;
670 +}
671 +
672 +AACDecoder *aac_filter_create()
673 +{
674 +    AACDecoder *decoder = (AACDecoder *)av_malloc(sizeof(AACDecoder));
675 +    decoder->parser = latm_create_parser();
676 +    decoder->aac_decoder = NULL;
677 +    decoder->open = 0;
678 +    return (void *)decoder;
679 +}
680 +
681 +void aac_filter_destroy(AACDecoder *decoder)
682 +{
683 +    aac_filter_close(decoder);
684 +    latm_destroy_parser(decoder->parser);
685 +    av_free(decoder);
686 +}
687 +
688 +int aac_filter_receive(AACDecoder *decoder, void *out, int *out_size, uint8_t *data, int size)
689 +{
690 +    uint8_t    tempbuf[32*1024];
691 +    int        ret;
692 +    int        consumed = size;
693 +    int        decoded;
694 +    int        max_size = *out_size;
695 +    
696 +    *out_size = 0;
697 +
698 +    //-------------------------------------------------------------------------
699 +    // Multiplex Parsing
700 +    //-------------------------------------------------------------------------
701 +
702 +    latm_write_data(decoder->parser, data, size);
703 +
704 +    do {
705 +        ret = latm_parse_packet(decoder->parser, tempbuf, sizeof(tempbuf));
706 +                if (ret < 0) {
707 +                        latm_flush(decoder->parser);
708 +                        return consumed;
709 +                }
710 +        if (ret == 0) return consumed;
711 +
712 +        data = tempbuf;
713 +        size = ret;
714 +
715 +        //-------------------------------------------------------------------------
716 +        // Initialize decoder (if necessary)
717 +        //-------------------------------------------------------------------------
718 +        if (!decoder->open) {
719 +            aac_filter_close(decoder);
720 +            if (decoder->parser->mode == AAC_LATM) {
721 +                ret = aac_decoder_open(decoder);
722 +                if (ret < 0) return consumed;
723 +            }
724 +
725 +            if(!decoder->open) return consumed;
726 +        }
727 +
728 +        //-------------------------------------------------------------------------
729 +        // Decode samples
730 +        //-------------------------------------------------------------------------
731 +        NeAACDecFrameInfo    info;
732 +        void *buf = NeAACDecDecode(decoder->aac_decoder, &info, data, size);
733 +
734 +        if (buf) {
735 +            decoder->in_samplerate = info.samplerate;
736 +            decoder->in_channels = info.channels;
737 +
738 +            //---------------------------------------------------------------------
739 +            // Deliver decoded samples
740 +            //---------------------------------------------------------------------
741 +
742 +            // kram dekoduje 16-bit. my vypustame 16-bit. takze by to malo byt okej
743 +            decoded = info.samples * sizeof(short);
744 +
745 +            // napraskame tam sample
746 +            *out_size += decoded;
747 +            if(*out_size > max_size) {
748 +                av_log(NULL, AV_LOG_ERROR, "overflow!\n");
749 +            } else {
750 +                memcpy(out, buf, decoded);
751 +                out = (unsigned char *)out + decoded;
752 +            }
753 +        } else {
754 +            // need more data
755 +            break;
756 +        }
757 +
758 +    } while (1);    // decode all packets
759 +    return consumed;
760 +}
761 +
762 +void aac_filter_getinfo(AACDecoder *decoder, int *sample_rate, int *channels)
763 +{
764 +    if(!decoder->open) return;
765 +    *sample_rate = decoder->in_samplerate;
766 +    *channels = decoder->in_channels;
767 +}
768 +
769 +static int faac_decode_init(AVCodecContext *avctx)
770 +{
771 +    FAACContext *s = avctx->priv_data;
772 +    avctx->frame_size = 360;
773 +    avctx->sample_rate = 48000;
774 +    avctx->channels = 2;
775 +    avctx->bit_rate = 8192 * 8 * avctx->sample_rate / avctx->frame_size;
776 +    s->decoder = aac_filter_create();
777 +    return 0;
778 +}
779 +
780 +static int faac_decode_frame(AVCodecContext *avctx,
781 +                             void *data, int *data_size,
782 +                             AVPacket *avpkt)
783 +{
784 +    FAACContext *s = avctx->priv_data;
785 +    int ret;
786 +
787 +    if (s->decoder == NULL) faac_decode_init(avctx);
788 +    ret = aac_filter_receive(s->decoder, data, data_size, avpkt->data, avpkt->size);
789 +    aac_filter_getinfo(s->decoder, &(avctx->sample_rate), &(avctx->channels));
790 +    return ret;
791 +}
792 +
793 +static int faac_decode_end(AVCodecContext *avctx)
794 +{
795 +    FAACContext *s = avctx->priv_data;
796 +    if(s->decoder != NULL) {
797 +        aac_filter_destroy(s->decoder);
798 +    }
799 +    return 0;
800 +}
801 +
802 +AVCodec libfaad2_decoder = {
803 +    .name = "AAC_LATM",
804 +    .type = CODEC_TYPE_AUDIO,
805 +    .id = CODEC_ID_AAC_LATM,
806 +    .priv_data_size = sizeof (FAACContext),
807 +    .init = faac_decode_init,
808 +    .close = faac_decode_end,
809 +    .decode = faac_decode_frame,
810 +    .long_name = "AAC over LATM",
811 +};
812 diff -Naur ffmpeg-r19067.orig/libavformat/mpeg.c ffmpeg-r19067/libavformat/mpeg.c
813 --- ffmpeg-r19067.orig/libavformat/mpeg.c       2009-04-17 14:08:39.000000000 -0400
814 +++ ffmpeg-r19067/libavformat/mpeg.c    2009-06-01 12:39:48.000000000 -0400
815 @@ -279,7 +279,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 @@ -446,6 +446,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-r19067.orig/libavformat/mpeg.h ffmpeg-r19067/libavformat/mpeg.h
835 --- ffmpeg-r19067.orig/libavformat/mpeg.h       2009-01-19 10:46:40.000000000 -0500
836 +++ ffmpeg-r19067/libavformat/mpeg.h    2009-06-01 12:39:48.000000000 -0400
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-r19067.orig/libavformat/mpegts.c ffmpeg-r19067/libavformat/mpegts.c
846 --- ffmpeg-r19067.orig/libavformat/mpegts.c     2009-05-31 04:47:40.000000000 -0400
847 +++ ffmpeg-r19067/libavformat/mpegts.c  2009-06-01 12:39:48.000000000 -0400
848 @@ -495,6 +495,7 @@
849      { 0x04, CODEC_TYPE_AUDIO,        CODEC_ID_MP3 },
850      { 0x0f, CODEC_TYPE_AUDIO,        CODEC_ID_AAC },
851      { 0x10, CODEC_TYPE_VIDEO,      CODEC_ID_MPEG4 },
852 +    { 0x11, CODEC_TYPE_AUDIO,   CODEC_ID_AAC_LATM },
853      { 0x1b, CODEC_TYPE_VIDEO,       CODEC_ID_H264 },
854      { 0xd1, CODEC_TYPE_VIDEO,      CODEC_ID_DIRAC },
855      { 0xea, CODEC_TYPE_VIDEO,        CODEC_ID_VC1 },
856 @@ -933,7 +934,7 @@
857                      if (pes->st->discard == AVDISCARD_ALL ||
858                          !((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                      pes->state = MPEGTS_PESHEADER_FILL;
864                      pes->total_size = AV_RB16(pes->header + 4);
865 diff -Naur ffmpeg-r19067.orig/libavformat/mpegts.h ffmpeg-r19067/libavformat/mpegts.h
866 --- ffmpeg-r19067.orig/libavformat/mpegts.h     2009-05-30 23:02:12.000000000 -0400
867 +++ ffmpeg-r19067/libavformat/mpegts.h  2009-06-01 12:39:48.000000000 -0400
868 @@ -47,6 +47,7 @@
869  #define STREAM_TYPE_PRIVATE_DATA    0x06
870  #define STREAM_TYPE_AUDIO_AAC       0x0f
871  #define STREAM_TYPE_VIDEO_MPEG4     0x10
872 +#define STREAM_TYPE_AUDIO_AAC_LATM  0x11
873  #define STREAM_TYPE_VIDEO_H264      0x1b
874  #define STREAM_TYPE_VIDEO_VC1       0xea
875  #define STREAM_TYPE_VIDEO_DIRAC     0xd1