Browse code

TAK demuxer, decoder and parser

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2012/10/02 22:43:19
Showing 18 changed files
... ...
@@ -8,6 +8,7 @@ version next:
8 8
 - Opus encoder using libopus
9 9
 - ffprobe -select_streams option
10 10
 - Pinnacle TARGA CineWave YUV16 decoder
11
+- TAK demuxer, decoder and parser
11 12
 
12 13
 
13 14
 version 1.0:
... ...
@@ -1824,6 +1824,7 @@ sap_muxer_select="rtp_muxer rtp_protocol"
1824 1824
 sdp_demuxer_select="rtpdec"
1825 1825
 smoothstreaming_muxer_select="ismv_muxer"
1826 1826
 spdif_muxer_select="aac_parser"
1827
+tak_demuxer_select="tak_parser"
1827 1828
 tg2_muxer_select="mov_muxer"
1828 1829
 tgp_muxer_select="mov_muxer"
1829 1830
 w64_demuxer_deps="wav_demuxer"
... ...
@@ -304,6 +304,7 @@ library:
304 304
 @item raw video                 @tab X @tab X
305 305
 @item raw id RoQ                @tab X @tab
306 306
 @item raw Shorten               @tab   @tab X
307
+@item raw TAK                   @tab   @tab X
307 308
 @item raw TrueHD                @tab X @tab X
308 309
 @item raw VC-1                  @tab   @tab X
309 310
 @item raw PCM A-law             @tab X @tab X
... ...
@@ -863,6 +864,7 @@ following image formats are supported:
863 863
     @tab experimental codec
864 864
 @item Speex                  @tab  E  @tab  E
865 865
     @tab supported through external library libspeex
866
+@item TAK (Tom's lossless Audio Kompressor)  @tab     @tab  X
866 867
 @item True Audio (TTA)       @tab     @tab  X
867 868
 @item TrueHD                 @tab     @tab  X
868 869
     @tab Used in HD-DVD and Blu-Ray discs.
... ...
@@ -400,6 +400,7 @@ OBJS-$(CONFIG_SVQ3_DECODER)            += svq3.o svq13.o h263.o h264.o        \
400 400
                                           h264_loopfilter.o h264_direct.o     \
401 401
                                           h264_sei.o h264_ps.o h264_refs.o    \
402 402
                                           h264_cavlc.o h264_cabac.o cabac.o
403
+OBJS-$(CONFIG_TAK_DECODER)             += takdec.o tak.o
403 404
 OBJS-$(CONFIG_TARGA_DECODER)           += targa.o
404 405
 OBJS-$(CONFIG_TARGA_ENCODER)           += targaenc.o rle.o
405 406
 OBJS-$(CONFIG_TARGA_Y216_DECODER)      += targa_y216dec.o
... ...
@@ -713,6 +714,7 @@ OBJS-$(CONFIG_MPEGVIDEO_PARSER)        += mpegvideo_parser.o    \
713 713
 OBJS-$(CONFIG_PNM_PARSER)              += pnm_parser.o pnm.o
714 714
 OBJS-$(CONFIG_RV30_PARSER)             += rv34_parser.o
715 715
 OBJS-$(CONFIG_RV40_PARSER)             += rv34_parser.o
716
+OBJS-$(CONFIG_TAK_PARSER)              += tak_parser.o tak.o
716 717
 OBJS-$(CONFIG_VC1_PARSER)              += vc1_parser.o vc1.o vc1data.o \
717 718
                                           msmpeg4.o msmpeg4data.o mpeg4video.o \
718 719
                                           h263.o
... ...
@@ -328,6 +328,7 @@ void avcodec_register_all(void)
328 328
     REGISTER_DECODER (SMACKAUD, smackaud);
329 329
     REGISTER_ENCDEC  (SONIC, sonic);
330 330
     REGISTER_ENCODER (SONIC_LS, sonic_ls);
331
+    REGISTER_DECODER (TAK, tak);
331 332
     REGISTER_DECODER (TRUEHD, truehd);
332 333
     REGISTER_DECODER (TRUESPEECH, truespeech);
333 334
     REGISTER_DECODER (TTA, tta);
... ...
@@ -486,6 +487,7 @@ void avcodec_register_all(void)
486 486
     REGISTER_PARSER  (PNM, pnm);
487 487
     REGISTER_PARSER  (RV30, rv30);
488 488
     REGISTER_PARSER  (RV40, rv40);
489
+    REGISTER_PARSER  (TAK, tak);
489 490
     REGISTER_PARSER  (VC1, vc1);
490 491
     REGISTER_PARSER  (VORBIS, vorbis);
491 492
     REGISTER_PARSER  (VP3, vp3);
... ...
@@ -431,6 +431,7 @@ enum AVCodecID {
431 431
     AV_CODEC_ID_SONIC_LS    = MKBETAG('S','O','N','L'),
432 432
     AV_CODEC_ID_PAF_AUDIO   = MKBETAG('P','A','F','A'),
433 433
     AV_CODEC_ID_OPUS        = MKBETAG('O','P','U','S'),
434
+    AV_CODEC_ID_TAK         = MKBETAG('t','B','a','K'),
434 435
 
435 436
     /* subtitle codecs */
436 437
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
... ...
@@ -2252,6 +2252,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
2252 2252
         .long_name = NULL_IF_CONFIG_SMALL("Opus (Opus Interactive Audio Codec)"),
2253 2253
         .props     = AV_CODEC_PROP_LOSSY,
2254 2254
     },
2255
+    {
2256
+        .id        = AV_CODEC_ID_TAK,
2257
+        .type      = AVMEDIA_TYPE_AUDIO,
2258
+        .name      = "tak",
2259
+        .long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
2260
+        .props     = AV_CODEC_PROP_LOSSLESS,
2261
+    },
2255 2262
 
2256 2263
     /* subtitle codecs */
2257 2264
     {
2258 2265
new file mode 100644
... ...
@@ -0,0 +1,177 @@
0
+/*
1
+ * TAK common code
2
+ * Copyright (c) 2012 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "tak.h"
22
+#include "libavutil/crc.h"
23
+#include "libavutil/intreadwrite.h"
24
+#include "libavutil/bswap.h"
25
+
26
+static const int64_t tak_channel_layouts[] = {
27
+    0,
28
+    AV_CH_FRONT_LEFT,
29
+    AV_CH_FRONT_RIGHT,
30
+    AV_CH_FRONT_CENTER,
31
+    AV_CH_LOW_FREQUENCY,
32
+    AV_CH_BACK_LEFT,
33
+    AV_CH_BACK_RIGHT,
34
+    AV_CH_FRONT_LEFT_OF_CENTER,
35
+    AV_CH_FRONT_RIGHT_OF_CENTER,
36
+    AV_CH_BACK_CENTER,
37
+    AV_CH_SIDE_LEFT,
38
+    AV_CH_SIDE_RIGHT,
39
+    AV_CH_TOP_CENTER,
40
+    AV_CH_TOP_FRONT_LEFT,
41
+    AV_CH_TOP_FRONT_CENTER,
42
+    AV_CH_TOP_FRONT_RIGHT,
43
+    AV_CH_TOP_BACK_LEFT,
44
+    AV_CH_TOP_BACK_CENTER,
45
+    AV_CH_TOP_BACK_RIGHT,
46
+};
47
+
48
+static const uint16_t frame_duration_type_quants[] = {
49
+    3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
50
+};
51
+
52
+static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
53
+{
54
+    int nb_samples, max_nb_samples;
55
+
56
+    if (type <= TAK_FST_250ms) {
57
+        nb_samples     = sample_rate * frame_duration_type_quants[type] >>
58
+                                       TAK_FRAME_DURATION_QUANT_SHIFT;
59
+        max_nb_samples = 16384;
60
+    } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
61
+        nb_samples     = frame_duration_type_quants[type];
62
+        max_nb_samples = sample_rate * frame_duration_type_quants[TAK_FST_250ms] >>
63
+                                       TAK_FRAME_DURATION_QUANT_SHIFT;
64
+    } else {
65
+        return AVERROR_INVALIDDATA;
66
+    }
67
+
68
+    if (nb_samples <= 0 || nb_samples > max_nb_samples)
69
+        return AVERROR_INVALIDDATA;
70
+
71
+    return nb_samples;
72
+}
73
+
74
+static int crc_init = 0;
75
+#if CONFIG_SMALL
76
+#define CRC_TABLE_SIZE 257
77
+#else
78
+#define CRC_TABLE_SIZE 1024
79
+#endif
80
+static AVCRC crc_24[CRC_TABLE_SIZE];
81
+
82
+av_cold void ff_tak_init_crc(void)
83
+{
84
+    if (!crc_init) {
85
+        av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
86
+        crc_init = 1;
87
+    }
88
+}
89
+
90
+int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
91
+{
92
+    uint32_t crc, CRC;
93
+
94
+    if (buf_size < 4)
95
+        return AVERROR_INVALIDDATA;
96
+    buf_size -= 3;
97
+
98
+    CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
99
+    crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
100
+    if (CRC != crc)
101
+        return AVERROR_INVALIDDATA;
102
+
103
+    return 0;
104
+}
105
+
106
+void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
107
+{
108
+    uint64_t channel_mask = 0;
109
+    int frame_type, i;
110
+
111
+    s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
112
+    skip_bits(gb, TAK_ENCODER_PROFILE_BITS);
113
+
114
+    frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
115
+    s->samples = get_bits_longlong(gb, TAK_SIZE_SAMPLES_NUM_BITS);
116
+
117
+    s->data_type   = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
118
+    s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) + TAK_SAMPLE_RATE_MIN;
119
+    s->bps         = get_bits(gb, TAK_FORMAT_BPS_BITS) + TAK_BPS_MIN;
120
+    s->channels    = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) + TAK_CHANNELS_MIN;
121
+
122
+    if (get_bits1(gb)) {
123
+        skip_bits(gb, TAK_FORMAT_VALID_BITS);
124
+        if (get_bits1(gb)) {
125
+            for (i = 0; i < s->channels; i++) {
126
+                int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
127
+
128
+                if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
129
+                    channel_mask |= tak_channel_layouts[value];
130
+            }
131
+        }
132
+    }
133
+
134
+    s->ch_layout     = channel_mask;
135
+    s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
136
+}
137
+
138
+#define FRAME_IS_LAST       1
139
+#define FRAME_HAVE_INFO     2
140
+#define FRAME_HAVE_METADATA 4
141
+
142
+int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
143
+                               TAKStreamInfo *ti, int log_level_offset)
144
+{
145
+    int flags;
146
+
147
+    if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
148
+        av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
149
+        return AVERROR_INVALIDDATA;
150
+    }
151
+
152
+    flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
153
+    ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
154
+
155
+    if (flags & FRAME_IS_LAST) {
156
+        ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
157
+        skip_bits(gb, 2);
158
+    } else {
159
+        ti->last_frame_samples = 0;
160
+    }
161
+
162
+    if (flags & FRAME_HAVE_INFO) {
163
+        avpriv_tak_parse_streaminfo(gb, ti);
164
+
165
+        if (get_bits(gb, 6))
166
+            skip_bits(gb, 25);
167
+        align_get_bits(gb);
168
+    }
169
+
170
+    if (flags & FRAME_HAVE_METADATA)
171
+        return AVERROR_INVALIDDATA;
172
+
173
+    skip_bits(gb, 24);
174
+
175
+    return 0;
176
+}
0 177
new file mode 100644
... ...
@@ -0,0 +1,151 @@
0
+/*
1
+ * TAK decoder/demuxer common code
2
+ * Copyright (c) 2012 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions
24
+ */
25
+
26
+#ifndef AVCODEC_TAK_H
27
+#define AVCODEC_TAK_H
28
+
29
+#define BITSTREAM_READER_LE
30
+#include "get_bits.h"
31
+#include "avcodec.h"
32
+
33
+#define TAK_FORMAT_DATA_TYPE_BITS    3
34
+#define TAK_FORMAT_SAMPLE_RATE_BITS  18
35
+#define TAK_FORMAT_BPS_BITS          5
36
+#define TAK_FORMAT_CHANNEL_BITS      4
37
+#define TAK_FORMAT_VALID_BITS        5
38
+#define TAK_FORMAT_CH_LAYOUT_BITS    6
39
+#define TAK_SIZE_FRAME_DURATION_BITS 4
40
+#define TAK_SIZE_SAMPLES_NUM_BITS    35
41
+#define TAK_LAST_FRAME_POS_BITS      40
42
+#define TAK_LAST_FRAME_SIZE_BITS     24
43
+#define TAK_ENCODER_CODEC_BITS       6
44
+#define TAK_ENCODER_PROFILE_BITS     4
45
+#define TAK_ENCODER_VERSION_BITS     24
46
+#define TAK_SAMPLE_RATE_MIN          6000
47
+#define TAK_CHANNELS_MIN             1
48
+#define TAK_BPS_MIN                  8
49
+#define TAK_FRAME_HEADER_FLAGS_BITS         3
50
+#define TAK_FRAME_HEADER_SYNC_ID            0xA0FF
51
+#define TAK_FRAME_HEADER_SYNC_ID_BITS       16
52
+#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS  14
53
+#define TAK_FRAME_HEADER_NO_BITS            21
54
+#define TAK_FRAME_DURATION_QUANT_SHIFT      5
55
+#define TAK_CRC24_BITS                      24
56
+
57
+#define TAK_MAX_CHANNELS                  ( 1 << TAK_FORMAT_CHANNEL_BITS )
58
+
59
+#define TAK_MIN_FRAME_HEADER_BITS         ( TAK_FRAME_HEADER_SYNC_ID_BITS + \
60
+                                            TAK_FRAME_HEADER_FLAGS_BITS   + \
61
+                                            TAK_FRAME_HEADER_NO_BITS      + \
62
+                                            TAK_CRC24_BITS )
63
+
64
+#define TAK_MIN_FRAME_HEADER_LAST_BITS    ( TAK_MIN_FRAME_HEADER_BITS + 2 + \
65
+                                            TAK_FRAME_HEADER_SAMPLE_COUNT_BITS )
66
+
67
+#define TAK_ENCODER_BITS         ( TAK_ENCODER_CODEC_BITS        + \
68
+                                   TAK_ENCODER_PROFILE_BITS )
69
+
70
+#define TAK_SIZE_BITS            ( TAK_SIZE_SAMPLES_NUM_BITS     + \
71
+                                   TAK_SIZE_FRAME_DURATION_BITS )
72
+
73
+#define TAK_FORMAT_BITS          ( TAK_FORMAT_DATA_TYPE_BITS     + \
74
+                                   TAK_FORMAT_SAMPLE_RATE_BITS   + \
75
+                                   TAK_FORMAT_BPS_BITS           + \
76
+                                   TAK_FORMAT_CHANNEL_BITS + 1   + \
77
+                                   TAK_FORMAT_VALID_BITS   + 1   + \
78
+                                   TAK_FORMAT_CH_LAYOUT_BITS     * \
79
+                                   TAK_MAX_CHANNELS )
80
+
81
+#define TAK_STREAMINFO_BITS      ( TAK_ENCODER_BITS              + \
82
+                                   TAK_SIZE_BITS                 + \
83
+                                   TAK_FORMAT_BITS )
84
+
85
+#define TAK_MAX_FRAME_HEADER_BITS  ( TAK_MIN_FRAME_HEADER_LAST_BITS + \
86
+                                     TAK_STREAMINFO_BITS + 31 )
87
+
88
+#define TAK_STREAMINFO_BYTES        (( TAK_STREAMINFO_BITS + 7 ) / 8)
89
+#define TAK_MAX_FRAME_HEADER_BYTES  (( TAK_MAX_FRAME_HEADER_BITS + 7 ) / 8)
90
+#define TAK_MIN_FRAME_HEADER_BYTES  (( TAK_MIN_FRAME_HEADER_BITS + 7 ) / 8)
91
+
92
+enum TAKMetaDataType {
93
+    TAK_METADATA_END = 0,
94
+    TAK_METADATA_STREAMINFO,
95
+    TAK_METADATA_SEEKTABLE,
96
+    TAK_METADATA_SIMPLE_WAVE_DATA,
97
+    TAK_METADATA_ENCODER,
98
+    TAK_METADATA_PADDING,
99
+    TAK_METADATA_MD5,
100
+    TAK_METADATA_LAST_FRAME,
101
+};
102
+
103
+enum TAKFrameSizeType {
104
+    TAK_FST_94ms = 0,
105
+    TAK_FST_125ms,
106
+    TAK_FST_188ms,
107
+    TAK_FST_250ms,
108
+    TAK_FST_4096,
109
+    TAK_FST_8192,
110
+    TAK_FST_16384,
111
+    TAK_FST_512,
112
+    TAK_FST_1024,
113
+    TAK_FST_2048,
114
+};
115
+
116
+typedef struct TAKStreamInfo {
117
+    int      codec;
118
+    int      data_type;
119
+    int      sample_rate;
120
+    int      channels;
121
+    int      bps;
122
+    int      frame_num;
123
+    int      frame_samples;
124
+    int      last_frame_samples;
125
+    uint64_t ch_layout;
126
+    int64_t  samples;
127
+} TAKStreamInfo;
128
+
129
+void ff_tak_init_crc(void);
130
+
131
+int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size);
132
+
133
+/**
134
+ * Parse the Streaminfo metadata block
135
+ * @param[in]  gb      pointer to GetBitContext
136
+ * @param[out] s       where parsed information is stored
137
+ */
138
+void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s);
139
+
140
+/**
141
+ * Validate and decode a frame header.
142
+ * @param      avctx AVCodecContext to use as av_log() context
143
+ * @param[in]  gb    GetBitContext from which to read frame header
144
+ * @param[out] s     frame information
145
+ * @param      log_level_offset  log level offset. can be used to silence error messages.
146
+ * @return non-zero on error, 0 if ok
147
+ */
148
+int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
149
+                               TAKStreamInfo *s, int log_level_offset);
150
+#endif /* AVCODEC_TAK_H */
0 151
new file mode 100644
... ...
@@ -0,0 +1,126 @@
0
+/*
1
+ * TAK parser
2
+ * Copyright (c) 2012 Michael Niedermayer
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * TAK parser
24
+ **/
25
+
26
+#include "parser.h"
27
+#include "tak.h"
28
+
29
+typedef struct TAKParseContext {
30
+    ParseContext  pc;
31
+    TAKStreamInfo ti;
32
+    int index;
33
+} TAKParseContext;
34
+
35
+static av_cold int tak_init(AVCodecParserContext *s)
36
+{
37
+    ff_tak_init_crc();
38
+    return 0;
39
+}
40
+
41
+static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
42
+                     const uint8_t **poutbuf, int *poutbuf_size,
43
+                     const uint8_t *buf, int buf_size)
44
+{
45
+    TAKParseContext *t = s->priv_data;
46
+    ParseContext *pc = &t->pc;
47
+    int next = END_NOT_FOUND;
48
+    GetBitContext gb;
49
+    int consumed = 0;
50
+    int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
51
+
52
+    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
53
+        TAKStreamInfo ti;
54
+        init_get_bits(&gb, buf, buf_size);
55
+        if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
56
+            s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples :
57
+                                                     t->ti.frame_samples;
58
+        *poutbuf      = buf;
59
+        *poutbuf_size = buf_size;
60
+        return buf_size;
61
+    }
62
+
63
+    while (buf_size || t->index + needed <= pc->index) {
64
+        if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
65
+            int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES, buf_size);
66
+            const uint8_t *tmp_buf = buf;
67
+
68
+            ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);
69
+            consumed += tmp_buf_size;
70
+            buf      += tmp_buf_size;
71
+            buf_size -= tmp_buf_size;
72
+        }
73
+
74
+        for (; t->index + needed <= pc->index; t->index++) {
75
+            if (pc->buffer[ t->index     ] == 0xFF &&
76
+                pc->buffer[ t->index + 1 ] == 0xA0) {
77
+                TAKStreamInfo ti;
78
+
79
+                init_get_bits(&gb, pc->buffer + t->index,
80
+                              8 * (pc->index - t->index));
81
+                if (!ff_tak_decode_frame_header(avctx, &gb,
82
+                        pc->frame_start_found ? &ti : &t->ti, 127) &&
83
+                    !ff_tak_check_crc(pc->buffer + t->index,
84
+                                      get_bits_count(&gb) / 8)) {
85
+                    if (!pc->frame_start_found) {
86
+                        pc->frame_start_found = 1;
87
+                        s->duration = t->ti.last_frame_samples ?
88
+                                      t->ti.last_frame_samples :
89
+                                      t->ti.frame_samples;
90
+                    } else {
91
+                        pc->frame_start_found = 0;
92
+                        next = t->index - pc->index;
93
+                        t->index = 0;
94
+                        goto found;
95
+                    }
96
+                }
97
+            }
98
+        }
99
+    }
100
+found:
101
+
102
+    if (consumed && !buf_size && next == END_NOT_FOUND ||
103
+        ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
104
+        *poutbuf      = NULL;
105
+        *poutbuf_size = 0;
106
+        return buf_size + consumed;
107
+    }
108
+
109
+    if (next != END_NOT_FOUND) {
110
+        next += consumed;
111
+        pc->overread = FFMAX(0, -next);
112
+    }
113
+
114
+    *poutbuf      = buf;
115
+    *poutbuf_size = buf_size;
116
+    return next;
117
+}
118
+
119
+AVCodecParser ff_tak_parser = {
120
+    .codec_ids      = { AV_CODEC_ID_TAK },
121
+    .priv_data_size = sizeof(TAKParseContext),
122
+    .parser_init    = tak_init,
123
+    .parser_parse   = tak_parse,
124
+    .parser_close   = ff_parse_close,
125
+};
0 126
new file mode 100644
... ...
@@ -0,0 +1,972 @@
0
+/*
1
+ * TAK decoder
2
+ * Copyright (c) 2012 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * TAK (Tom's lossless Audio Kompressor) decoder
24
+ * @author Paul B Mahol
25
+ */
26
+
27
+#include "tak.h"
28
+#include "avcodec.h"
29
+#include "unary.h"
30
+#include "dsputil.h"
31
+
32
+#define MAX_SUBFRAMES   8                       ///< max number of subframes per channel
33
+#define MAX_PREDICTORS  256
34
+
35
+typedef struct MCDParam {
36
+    int8_t     present;                         ///< is decorrelation parameters available for this channel
37
+    int8_t     index;                           ///< index into array of decorrelation types
38
+    int8_t     chan1;
39
+    int8_t     chan2;
40
+} MCDParam;
41
+
42
+typedef struct TAKDecContext {
43
+    AVCodecContext *avctx;                      ///< parent AVCodecContext
44
+    AVFrame        frame;                       ///< AVFrame for decoded output
45
+    DSPContext     dsp;
46
+    TAKStreamInfo  ti;
47
+    GetBitContext  gb;                          ///< bitstream reader initialized to start at the current frame
48
+
49
+    int            nb_samples;                  ///< number of samples in the current frame
50
+    int32_t        *decode_buffer;
51
+    int            decode_buffer_size;
52
+    int32_t        *decoded[TAK_MAX_CHANNELS];  ///< decoded samples for each channel
53
+
54
+    int8_t         lpc_mode[TAK_MAX_CHANNELS];
55
+    int8_t         sample_shift[TAK_MAX_CHANNELS];  ///< shift applied to every sample in the channel
56
+    int32_t        xred;
57
+    int            size;
58
+    int            ared;
59
+    int            filter_order;
60
+    int16_t        predictors[MAX_PREDICTORS];
61
+    int            nb_subframes;                ///< number of subframes in the current frame
62
+    int16_t        subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
63
+    int            subframe_scale;
64
+
65
+    int8_t         dmode;                       ///< channel decorrelation type in the current frame
66
+    int8_t         dshift;
67
+    int16_t        dfactor;
68
+    int8_t         dval1;
69
+    int8_t         dval2;
70
+
71
+    MCDParam       mcdparams[TAK_MAX_CHANNELS]; ///< multichannel decorrelation parameters
72
+
73
+    int            wlength;
74
+    int            uval;
75
+    int            rval;
76
+    int8_t         coding_mode[128];
77
+    DECLARE_ALIGNED(16, int16_t, filter)[MAX_PREDICTORS];
78
+    DECLARE_ALIGNED(16, int16_t, residues)[544];
79
+} TAKDecContext;
80
+
81
+static const int8_t mc_dmodes[] = {
82
+    1, 3, 4, 6,
83
+};
84
+
85
+static const uint16_t predictor_sizes[] = {
86
+    4, 8, 12, 16, 24, 32, 48, 64, 80, 96, 128, 160, 192, 224, 256, 0,
87
+};
88
+
89
+static const struct CParam {
90
+    int        init;
91
+    int        escape;
92
+    int        scale;
93
+    int        aescape;
94
+    int        bias;
95
+} xcodes[50] = {
96
+    { 0x01, 0x0000001, 0x0000001, 0x0000003, 0x0000008 },
97
+    { 0x02, 0x0000003, 0x0000001, 0x0000007, 0x0000006 },
98
+    { 0x03, 0x0000005, 0x0000002, 0x000000E, 0x000000D },
99
+    { 0x03, 0x0000003, 0x0000003, 0x000000D, 0x0000018 },
100
+    { 0x04, 0x000000B, 0x0000004, 0x000001C, 0x0000019 },
101
+    { 0x04, 0x0000006, 0x0000006, 0x000001A, 0x0000030 },
102
+    { 0x05, 0x0000016, 0x0000008, 0x0000038, 0x0000032 },
103
+    { 0x05, 0x000000C, 0x000000C, 0x0000034, 0x0000060 },
104
+    { 0x06, 0x000002C, 0x0000010, 0x0000070, 0x0000064 },
105
+    { 0x06, 0x0000018, 0x0000018, 0x0000068, 0x00000C0 },
106
+    { 0x07, 0x0000058, 0x0000020, 0x00000E0, 0x00000C8 },
107
+    { 0x07, 0x0000030, 0x0000030, 0x00000D0, 0x0000180 },
108
+    { 0x08, 0x00000B0, 0x0000040, 0x00001C0, 0x0000190 },
109
+    { 0x08, 0x0000060, 0x0000060, 0x00001A0, 0x0000300 },
110
+    { 0x09, 0x0000160, 0x0000080, 0x0000380, 0x0000320 },
111
+    { 0x09, 0x00000C0, 0x00000C0, 0x0000340, 0x0000600 },
112
+    { 0x0A, 0x00002C0, 0x0000100, 0x0000700, 0x0000640 },
113
+    { 0x0A, 0x0000180, 0x0000180, 0x0000680, 0x0000C00 },
114
+    { 0x0B, 0x0000580, 0x0000200, 0x0000E00, 0x0000C80 },
115
+    { 0x0B, 0x0000300, 0x0000300, 0x0000D00, 0x0001800 },
116
+    { 0x0C, 0x0000B00, 0x0000400, 0x0001C00, 0x0001900 },
117
+    { 0x0C, 0x0000600, 0x0000600, 0x0001A00, 0x0003000 },
118
+    { 0x0D, 0x0001600, 0x0000800, 0x0003800, 0x0003200 },
119
+    { 0x0D, 0x0000C00, 0x0000C00, 0x0003400, 0x0006000 },
120
+    { 0x0E, 0x0002C00, 0x0001000, 0x0007000, 0x0006400 },
121
+    { 0x0E, 0x0001800, 0x0001800, 0x0006800, 0x000C000 },
122
+    { 0x0F, 0x0005800, 0x0002000, 0x000E000, 0x000C800 },
123
+    { 0x0F, 0x0003000, 0x0003000, 0x000D000, 0x0018000 },
124
+    { 0x10, 0x000B000, 0x0004000, 0x001C000, 0x0019000 },
125
+    { 0x10, 0x0006000, 0x0006000, 0x001A000, 0x0030000 },
126
+    { 0x11, 0x0016000, 0x0008000, 0x0038000, 0x0032000 },
127
+    { 0x11, 0x000C000, 0x000C000, 0x0034000, 0x0060000 },
128
+    { 0x12, 0x002C000, 0x0010000, 0x0070000, 0x0064000 },
129
+    { 0x12, 0x0018000, 0x0018000, 0x0068000, 0x00C0000 },
130
+    { 0x13, 0x0058000, 0x0020000, 0x00E0000, 0x00C8000 },
131
+    { 0x13, 0x0030000, 0x0030000, 0x00D0000, 0x0180000 },
132
+    { 0x14, 0x00B0000, 0x0040000, 0x01C0000, 0x0190000 },
133
+    { 0x14, 0x0060000, 0x0060000, 0x01A0000, 0x0300000 },
134
+    { 0x15, 0x0160000, 0x0080000, 0x0380000, 0x0320000 },
135
+    { 0x15, 0x00C0000, 0x00C0000, 0x0340000, 0x0600000 },
136
+    { 0x16, 0x02C0000, 0x0100000, 0x0700000, 0x0640000 },
137
+    { 0x16, 0x0180000, 0x0180000, 0x0680000, 0x0C00000 },
138
+    { 0x17, 0x0580000, 0x0200000, 0x0E00000, 0x0C80000 },
139
+    { 0x17, 0x0300000, 0x0300000, 0x0D00000, 0x1800000 },
140
+    { 0x18, 0x0B00000, 0x0400000, 0x1C00000, 0x1900000 },
141
+    { 0x18, 0x0600000, 0x0600000, 0x1A00000, 0x3000000 },
142
+    { 0x19, 0x1600000, 0x0800000, 0x3800000, 0x3200000 },
143
+    { 0x19, 0x0C00000, 0x0C00000, 0x3400000, 0x6000000 },
144
+    { 0x1A, 0x2C00000, 0x1000000, 0x7000000, 0x6400000 },
145
+    { 0x1A, 0x1800000, 0x1800000, 0x6800000, 0xC000000 },
146
+};
147
+
148
+static void tak_set_bps(AVCodecContext *avctx)
149
+{
150
+    switch (avctx->bits_per_coded_sample) {
151
+    case 8:
152
+        avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
153
+        break;
154
+    case 16:
155
+        avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
156
+        break;
157
+    case 24:
158
+        avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
159
+        break;
160
+    }
161
+}
162
+
163
+static int get_shift(int sample_rate)
164
+{
165
+    int shift;
166
+
167
+    if (sample_rate < 11025)
168
+        shift = 3;
169
+    else if (sample_rate < 22050)
170
+        shift = 2;
171
+    else if (sample_rate < 44100)
172
+        shift = 1;
173
+    else
174
+        shift = 0;
175
+
176
+    return shift;
177
+}
178
+
179
+static int get_scale(int sample_rate, int shift)
180
+{
181
+    return FFALIGN(sample_rate + 511 >> 9, 4) << shift;
182
+}
183
+
184
+static av_cold int tak_decode_init(AVCodecContext *avctx)
185
+{
186
+    TAKDecContext *s = avctx->priv_data;
187
+
188
+    ff_tak_init_crc();
189
+    ff_dsputil_init(&s->dsp, avctx);
190
+
191
+    s->avctx = avctx;
192
+    avcodec_get_frame_defaults(&s->frame);
193
+    avctx->coded_frame = &s->frame;
194
+
195
+    s->uval = get_scale(avctx->sample_rate, get_shift(avctx->sample_rate));
196
+    s->subframe_scale = get_scale(avctx->sample_rate, 1);
197
+
198
+    tak_set_bps(avctx);
199
+
200
+    return 0;
201
+}
202
+
203
+static int get_code(GetBitContext *gb, int nbits)
204
+{
205
+    if (nbits == 1) {
206
+        skip_bits1(gb);
207
+        return 0;
208
+    } else {
209
+        return get_sbits(gb, nbits);
210
+    }
211
+}
212
+
213
+static void decode_lpc(int32_t *coeffs, int mode, int length)
214
+{
215
+    int i, a1, a2, a3, a4, a5;
216
+
217
+    if (length < 2)
218
+        return;
219
+
220
+    if (mode == 1) {
221
+        a1 = *coeffs++;
222
+        for (i = 0; i < (length - 1 >> 1); i++) {
223
+            *coeffs   += a1;
224
+            coeffs[1] += *coeffs;
225
+            a1      = coeffs[1];
226
+            coeffs    += 2;
227
+        }
228
+        if ((length - 1) & 1)
229
+            *coeffs += a1;
230
+    } else if (mode == 2) {
231
+        a1     = coeffs[1];
232
+        a2     = a1 + *coeffs;
233
+        coeffs[1] = a2;
234
+        if (length > 2) {
235
+            coeffs += 2;
236
+            for (i = 0; i < (length - 2 >> 1); i++) {
237
+                a3     = *coeffs + a1;
238
+                a4     = a3 + a2;
239
+                *coeffs   = a4;
240
+                a1     = coeffs[1] + a3;
241
+                a2     = a1 + a4;
242
+                coeffs[1] = a2;
243
+                coeffs   += 2;
244
+            }
245
+            if (length & 1)
246
+                *coeffs  += a1 + a2;
247
+        }
248
+    } else if (mode == 3) {
249
+        a1     = coeffs[1];
250
+        a2     = a1 + *coeffs;
251
+        coeffs[1] = a2;
252
+        if (length > 2) {
253
+            a3   = coeffs[2];
254
+            a4   = a3 + a1;
255
+            a5   = a4 + a2;
256
+            coeffs += 3;
257
+            for (i = 0; i < length - 3; i++) {
258
+                a3  += *coeffs;
259
+                a4  += a3;
260
+                a5  += a4;
261
+                *coeffs = a5;
262
+                coeffs++;
263
+            }
264
+        }
265
+    }
266
+}
267
+
268
+static int decode_segment(TAKDecContext *s, int8_t value, int32_t *dst, int len)
269
+{
270
+    GetBitContext *gb = &s->gb;
271
+
272
+    if (!value) {
273
+        memset(dst, 0, len * 4);
274
+    } else {
275
+        int x, y, z, i = 0;
276
+
277
+        value--;
278
+        do {
279
+            while (1) {
280
+                x = get_bits_long(gb, xcodes[value].init);
281
+                if (x >= xcodes[value].escape)
282
+                    break;
283
+                dst[i++] = (x >> 1) ^ -(x & 1);
284
+                if (i >= len)
285
+                    return 0;
286
+            }
287
+
288
+            y = get_bits1(gb);
289
+            x = (y << xcodes[value].init) | x;
290
+            if (x >= xcodes[value].aescape) {
291
+                int c = get_unary(gb, 1, 9);
292
+
293
+                if (c == 9) {
294
+                    int d;
295
+
296
+                    z = x + xcodes[value].bias;
297
+                    d = get_bits(gb, 3);
298
+                    if (d == 7) {
299
+                        d = get_bits(gb, 5) + 7;
300
+                        if (d > 29)
301
+                            return AVERROR_INVALIDDATA;
302
+                    }
303
+                    if (d)
304
+                        z += xcodes[value].scale * (get_bits_long(gb, d) + 1);
305
+                } else {
306
+                    z = xcodes[value].scale * c + x - xcodes[value].escape;
307
+                }
308
+            } else {
309
+                z = x - (xcodes[value].escape & -y);
310
+            }
311
+            dst[i++] = (z >> 1) ^ -(z & 1);
312
+        } while (i < len);
313
+    }
314
+
315
+    return 0;
316
+}
317
+
318
+static int xget(TAKDecContext *s, int d, int q)
319
+{
320
+    int x;
321
+
322
+    x = d / q;
323
+
324
+    s->rval = d - (x * q);
325
+
326
+    if (s->rval < q / 2) {
327
+        s->rval += q;
328
+    } else {
329
+        x++;
330
+    }
331
+
332
+    if (x <= 1 || x > 128)
333
+        return -1;
334
+
335
+    return x;
336
+}
337
+
338
+static int get_len(TAKDecContext *s, int b)
339
+{
340
+    if (b >= s->wlength - 1)
341
+        return s->rval;
342
+    else
343
+        return s->uval;
344
+}
345
+
346
+static int decode_coeffs(TAKDecContext *s, int32_t *dst, int length)
347
+{
348
+    GetBitContext *gb = &s->gb;
349
+    int i, v, ret;
350
+
351
+    if (length > s->nb_samples)
352
+        return AVERROR_INVALIDDATA;
353
+
354
+    if (get_bits1(gb)) {
355
+        if ((s->wlength = xget(s, length, s->uval)) < 0)
356
+            return AVERROR_INVALIDDATA;
357
+
358
+        s->coding_mode[0] = v = get_bits(gb, 6);
359
+        if (s->coding_mode[0] > FF_ARRAY_ELEMS(xcodes))
360
+            return AVERROR_INVALIDDATA;
361
+
362
+        for (i = 1; i < s->wlength; i++) {
363
+            int c = get_unary(gb, 1, 6);
364
+
365
+            if (c > 5) {
366
+                v = get_bits(gb, 6);
367
+            } else if (c > 2) {
368
+                int t = get_bits1(gb);
369
+
370
+                v += (-t ^ (c - 1)) + t;
371
+            } else {
372
+                v += (-(c & 1) ^ (((c & 1) + c) >> 1)) + (c & 1);
373
+            }
374
+
375
+            if (v > FF_ARRAY_ELEMS(xcodes))
376
+                return AVERROR_INVALIDDATA;
377
+            s->coding_mode[i] = v;
378
+        }
379
+
380
+        i = 0;
381
+        while (i < s->wlength) {
382
+            int len = 0;
383
+
384
+            v = s->coding_mode[i];
385
+            do {
386
+                len += get_len(s, i);
387
+                i++;
388
+
389
+                if (i == s->wlength)
390
+                    break;
391
+            } while (v == s->coding_mode[i]);
392
+
393
+            if ((ret = decode_segment(s, v, dst, len)) < 0)
394
+                return ret;
395
+            dst += len;
396
+        }
397
+    } else {
398
+        v = get_bits(gb, 6);
399
+        if (v > FF_ARRAY_ELEMS(xcodes))
400
+            return AVERROR_INVALIDDATA;
401
+        if ((ret = decode_segment(s, v, dst, length)) < 0)
402
+            return ret;
403
+    }
404
+
405
+    return 0;
406
+}
407
+
408
+static int get_b(GetBitContext *gb)
409
+{
410
+    if (get_bits1(gb))
411
+        return get_bits(gb, 4) + 1;
412
+    else
413
+        return 0;
414
+}
415
+
416
+static int decode_subframe(TAKDecContext *s, int32_t *ptr, int subframe_size,
417
+                           int prev_subframe_size)
418
+{
419
+    GetBitContext  *gb = &s->gb;
420
+    int tmp, x, y, i, j, ret = 0;
421
+    int tfilter[MAX_PREDICTORS];
422
+
423
+    if (get_bits1(gb)) {
424
+        s->filter_order = predictor_sizes[get_bits(gb, 4)];
425
+
426
+        if (prev_subframe_size > 0 && get_bits1(gb)) {
427
+            if (s->filter_order > prev_subframe_size)
428
+                return AVERROR_INVALIDDATA;
429
+
430
+            ptr           -= s->filter_order;
431
+            subframe_size += s->filter_order;
432
+
433
+            if (s->filter_order > subframe_size)
434
+                return AVERROR_INVALIDDATA;
435
+        } else {
436
+            int lpc;
437
+
438
+            if (s->filter_order > subframe_size)
439
+                return AVERROR_INVALIDDATA;
440
+
441
+            lpc = get_bits(gb, 2);
442
+            if (lpc > 2)
443
+                return AVERROR_INVALIDDATA;
444
+
445
+            if ((ret = decode_coeffs(s, ptr, s->filter_order)) < 0)
446
+                return ret;
447
+
448
+            decode_lpc(ptr, lpc, s->filter_order);
449
+        }
450
+
451
+        s->xred = get_b(gb);
452
+        s->size = get_bits1(gb) + 5;
453
+
454
+        if (get_bits1(gb)) {
455
+            s->ared = get_bits(gb, 3) + 1;
456
+            if (s->ared > 7)
457
+                return AVERROR_INVALIDDATA;
458
+        } else {
459
+            s->ared = 0;
460
+        }
461
+        s->predictors[0] = get_code(gb, 10);
462
+        s->predictors[1] = get_code(gb, 10);
463
+        s->predictors[2] = get_code(gb, s->size + 1) << (9 - s->size);
464
+        s->predictors[3] = get_code(gb, s->size + 1) << (9 - s->size);
465
+        if (s->filter_order > 4) {
466
+            tmp = s->size + 1 - get_bits1(gb);
467
+
468
+            for (i = 4; i < s->filter_order; i++) {
469
+                if (!(i & 3))
470
+                    x = tmp - get_bits(gb, 2);
471
+                s->predictors[i] = get_code(gb, x) << (9 - s->size);
472
+            }
473
+        }
474
+
475
+        tfilter[0] = s->predictors[0] << 6;
476
+        for (i = 1; i < s->filter_order; i++) {
477
+            int32_t *p1 = &tfilter[0];
478
+            int32_t *p2 = &tfilter[i - 1];
479
+
480
+            for (j = 0; j < (i + 1) / 2; j++) {
481
+                x     = *p1 + (s->predictors[i] * *p2 + 256 >> 9);
482
+                *p2  += s->predictors[i] * *p1 + 256 >> 9;
483
+                *p1++ = x;
484
+                p2--;
485
+            }
486
+
487
+            tfilter[i] = s->predictors[i] << 6;
488
+        }
489
+
490
+        x = -1 << (32 - (s->ared + 5));
491
+        y =  1 << ((s->ared + 5) - 1);
492
+        for (i = 0, j = s->filter_order - 1; i < s->filter_order / 2; i++, j--) {
493
+            tmp = y + tfilter[j];
494
+            s->filter[j] = -(x & -(y + tfilter[i] >> 31) |
495
+                            (y + tfilter[i]) >> (s->ared + 5));
496
+            s->filter[i] = -(x & -(tmp >> 31) | (tmp >> s->ared + 5));
497
+        }
498
+
499
+        if ((ret = decode_coeffs(s, &ptr[s->filter_order],
500
+                                 subframe_size - s->filter_order)) < 0)
501
+            return ret;
502
+
503
+        for (i = 0; i < s->filter_order; i++)
504
+            s->residues[i] = *ptr++ >> s->xred;
505
+
506
+        y    = FF_ARRAY_ELEMS(s->residues) - s->filter_order;
507
+        x    = subframe_size - s->filter_order;
508
+        while (x > 0) {
509
+            tmp = FFMIN(y, x);
510
+
511
+            for (i = 0; i < tmp; i++) {
512
+                int v, w, m;
513
+
514
+                v = 1 << (10 - s->ared - 1);
515
+                if (!(s->filter_order & 15)) {
516
+                    v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
517
+                                                    s->filter_order);
518
+                } else if (s->filter_order & 4) {
519
+                    for (j = 0; j < s->filter_order; j += 4) {
520
+                        v += s->residues[i + j + 3] * s->filter[j + 3] +
521
+                             s->residues[i + j + 2] * s->filter[j + 2] +
522
+                             s->residues[i + j + 1] * s->filter[j + 1] +
523
+                             s->residues[i + j    ] * s->filter[j    ];
524
+                    }
525
+                } else {
526
+                    for (j = 0; j < s->filter_order; j += 8) {
527
+                        v += s->residues[i + j + 7] * s->filter[j + 7] +
528
+                             s->residues[i + j + 6] * s->filter[j + 6] +
529
+                             s->residues[i + j + 5] * s->filter[j + 5] +
530
+                             s->residues[i + j + 4] * s->filter[j + 4] +
531
+                             s->residues[i + j + 3] * s->filter[j + 3] +
532
+                             s->residues[i + j + 2] * s->filter[j + 2] +
533
+                             s->residues[i + j + 1] * s->filter[j + 1] +
534
+                             s->residues[i + j    ] * s->filter[j    ];
535
+                    }
536
+                }
537
+                m = (-1 << (32 - (10 - s->ared))) & -(v >> 31) | (v >> 10 - s->ared);
538
+                m = av_clip(m, -8192, 8191);
539
+                w = (m << s->xred) - *ptr;
540
+                *ptr++ = w;
541
+                s->residues[s->filter_order + i] = w >> s->xred;
542
+            }
543
+
544
+            x -= tmp;
545
+            if (x > 0)
546
+                memcpy(s->residues, &s->residues[y], 2 * s->filter_order);
547
+        }
548
+
549
+        emms_c();
550
+    } else {
551
+        ret = decode_coeffs(s, ptr, subframe_size);
552
+    }
553
+
554
+    return ret;
555
+}
556
+
557
+static int decode_channel(TAKDecContext *s, int chan)
558
+{
559
+    AVCodecContext *avctx = s->avctx;
560
+    GetBitContext  *gb = &s->gb;
561
+    int32_t *dst = s->decoded[chan];
562
+    int i = 0, ret, prev = 0;
563
+    int left = s->nb_samples - 1;
564
+
565
+    s->sample_shift[chan] = get_b(gb);
566
+    if (s->sample_shift[chan] >= avctx->bits_per_coded_sample)
567
+        return AVERROR_INVALIDDATA;
568
+
569
+    *dst++ = get_code(gb, avctx->bits_per_coded_sample - s->sample_shift[chan]);
570
+    s->lpc_mode[chan] = get_bits(gb, 2);
571
+    s->nb_subframes   = get_bits(gb, 3) + 1;
572
+
573
+    if (s->nb_subframes > 1) {
574
+        if (get_bits_left(gb) < (s->nb_subframes - 1) * 6)
575
+            return AVERROR_INVALIDDATA;
576
+
577
+        for (; i < s->nb_subframes - 1; i++) {
578
+            int v = get_bits(gb, 6);
579
+
580
+            s->subframe_len[i] = (v - prev) * s->subframe_scale;
581
+            if (s->subframe_len[i] <= 0)
582
+                return AVERROR_INVALIDDATA;
583
+
584
+            left -= s->subframe_len[i];
585
+            prev  = v;
586
+        }
587
+
588
+        if (left <= 0)
589
+            return AVERROR_INVALIDDATA;
590
+    }
591
+
592
+    s->subframe_len[i] = left;
593
+    prev = 0;
594
+    for (i = 0; i < s->nb_subframes; i++) {
595
+        if ((ret = decode_subframe(s, dst, s->subframe_len[i], prev)) < 0)
596
+            return ret;
597
+        dst += s->subframe_len[i];
598
+        prev = s->subframe_len[i];
599
+    }
600
+
601
+    return 0;
602
+}
603
+
604
+static int decorrelate(TAKDecContext *s, int c1, int c2, int length)
605
+{
606
+    GetBitContext  *gb = &s->gb;
607
+    uint32_t *p1 = s->decoded[c1] + 1;
608
+    uint32_t *p2 = s->decoded[c2] + 1;
609
+    int a, b, i, x, tmp;
610
+
611
+    if (s->dmode > 3) {
612
+        s->dshift = get_b(gb);
613
+        if (s->dmode > 5) {
614
+            if (get_bits1(gb))
615
+                s->filter_order = 16;
616
+            else
617
+                s->filter_order = 8;
618
+
619
+            s->dval1 = get_bits1(gb);
620
+            s->dval2 = get_bits1(gb);
621
+
622
+            for (i = 0; i < s->filter_order; i++) {
623
+                if (!(i & 3))
624
+                    x = 14 - get_bits(gb, 3);
625
+                s->filter[i] = get_code(gb, x);
626
+            }
627
+        } else {
628
+            s->dfactor = get_code(gb, 10);
629
+        }
630
+    }
631
+
632
+    switch (s->dmode) {
633
+    case 1:
634
+        for (i = 0; i < length; i++, p1++, p2++)
635
+            *p2 += *p1;
636
+        break;
637
+    case 2:
638
+        for (i = 0; i < length; i++, p1++, p2++)
639
+            *p1 = *p2 - *p1;
640
+        break;
641
+    case 3:
642
+        for (i = 0; i < length; i++, p1++, p2++) {
643
+            x   = (*p2 & 1) + 2 * *p1;
644
+            a   = -*p2 + x;
645
+            b   =  *p2 + x;
646
+            *p1 = a & 0x80000000 | (a >> 1);
647
+            *p2 = b & 0x80000000 | (b >> 1);
648
+        }
649
+        break;
650
+    case 4:
651
+        FFSWAP(uint32_t *, p1, p2);
652
+    case 5:
653
+        if (s->dshift)
654
+            tmp = -1 << (32 - s->dshift);
655
+        else
656
+            tmp = 0;
657
+
658
+        for (i = 0; i < length; i++, p1++, p2++) {
659
+            x   = s->dfactor * (tmp & -(*p2 >> 31) | (*p2 >> s->dshift)) + 128;
660
+            *p1 = ((-(x >> 31) & 0xFF000000 | (x >> 8)) << s->dshift) - *p1;
661
+        }
662
+        break;
663
+    case 6:
664
+        FFSWAP(uint32_t *, p1, p2);
665
+    case 7:
666
+        if (length < 256)
667
+            return AVERROR_INVALIDDATA;
668
+
669
+        a = s->filter_order / 2;
670
+        b = length - (s->filter_order - 1);
671
+
672
+        if (s->dval1) {
673
+            for (i = 0; i < a; i++)
674
+                p1[i] += p2[i];
675
+        }
676
+
677
+        if (s->dval2) {
678
+            x = a + b;
679
+            for (i = 0; i < length - x; i++)
680
+                p1[x + i] += p2[x + i];
681
+        }
682
+
683
+        for (i = 0; i < s->filter_order; i++)
684
+            s->residues[i] = *p2++ >> s->dshift;
685
+
686
+        p1 += a;
687
+        x = FF_ARRAY_ELEMS(s->residues) - s->filter_order;
688
+        for (; b > 0; b -= tmp) {
689
+            tmp = FFMIN(b, x);
690
+
691
+            for (i = 0; i < tmp; i++)
692
+                s->residues[s->filter_order + i] = *p2++ >> s->dshift;
693
+
694
+            for (i = 0; i < tmp; i++) {
695
+                int v, w, m;
696
+
697
+                v = 1 << 9;
698
+
699
+                if (s->filter_order == 16) {
700
+                    v += s->dsp.scalarproduct_int16(&s->residues[i], s->filter,
701
+                                                    s->filter_order);
702
+                } else {
703
+                    v += s->residues[i + 7] * s->filter[7] +
704
+                         s->residues[i + 6] * s->filter[6] +
705
+                         s->residues[i + 5] * s->filter[5] +
706
+                         s->residues[i + 4] * s->filter[4] +
707
+                         s->residues[i + 3] * s->filter[3] +
708
+                         s->residues[i + 2] * s->filter[2] +
709
+                         s->residues[i + 1] * s->filter[1] +
710
+                         s->residues[i    ] * s->filter[0];
711
+                }
712
+
713
+                m = (-1 << 22) & -(v >> 31) | (v >> 10);
714
+                m = av_clip(m, -8192, 8191);
715
+                w = (m << s->dshift) - *p1;
716
+                *p1++ = w;
717
+            }
718
+
719
+            memcpy(s->residues, &s->residues[tmp], 2 * s->filter_order);
720
+        }
721
+
722
+        emms_c();
723
+        break;
724
+    }
725
+
726
+    return 0;
727
+}
728
+
729
+static int tak_decode_frame(AVCodecContext *avctx, void *data,
730
+                            int *got_frame_ptr, AVPacket *pkt)
731
+{
732
+    TAKDecContext  *s = avctx->priv_data;
733
+    GetBitContext *gb = &s->gb;
734
+    int chan, i, ret, hsize;
735
+    int32_t *p;
736
+
737
+    if (pkt->size < TAK_MIN_FRAME_HEADER_BYTES)
738
+        return AVERROR_INVALIDDATA;
739
+
740
+    init_get_bits(gb, pkt->data, pkt->size * 8);
741
+
742
+    if ((ret = ff_tak_decode_frame_header(avctx, gb, &s->ti, 0)) < 0)
743
+        return ret;
744
+
745
+    if (avctx->err_recognition & AV_EF_CRCCHECK) {
746
+        hsize = get_bits_count(gb) / 8;
747
+        if (ff_tak_check_crc(pkt->data, hsize)) {
748
+            av_log(avctx, AV_LOG_ERROR, "CRC error\n");
749
+            return AVERROR_INVALIDDATA;
750
+        }
751
+    }
752
+
753
+    if (s->ti.codec != 2 && s->ti.codec != 4) {
754
+        av_log(avctx, AV_LOG_ERROR, "unsupported codec: %d\n", s->ti.codec);
755
+        return AVERROR_PATCHWELCOME;
756
+    }
757
+    if (s->ti.data_type) {
758
+        av_log(avctx, AV_LOG_ERROR, "unsupported data type: %d\n", s->ti.data_type);
759
+        return AVERROR_INVALIDDATA;
760
+    }
761
+    if (s->ti.codec == 2 && s->ti.channels > 2) {
762
+        av_log(avctx, AV_LOG_ERROR, "invalid number of channels: %d\n", s->ti.channels);
763
+        return AVERROR_INVALIDDATA;
764
+    }
765
+    if (s->ti.channels > 6) {
766
+        av_log(avctx, AV_LOG_ERROR, "unsupported number of channels: %d\n", s->ti.channels);
767
+        return AVERROR_INVALIDDATA;
768
+    }
769
+
770
+    if (s->ti.frame_samples <= 0) {
771
+        av_log(avctx, AV_LOG_ERROR, "unsupported/invalid number of samples\n");
772
+        return AVERROR_INVALIDDATA;
773
+    }
774
+
775
+    if (s->ti.bps != avctx->bits_per_coded_sample) {
776
+        avctx->bits_per_coded_sample = s->ti.bps;
777
+        tak_set_bps(avctx);
778
+    }
779
+    if (s->ti.sample_rate != avctx->sample_rate) {
780
+        avctx->sample_rate = s->ti.sample_rate;
781
+        s->uval = get_scale(avctx->sample_rate, get_shift(avctx->sample_rate));
782
+        s->subframe_scale = get_scale(avctx->sample_rate, 1);
783
+    }
784
+    if (s->ti.ch_layout)
785
+        avctx->channel_layout = s->ti.ch_layout;
786
+    avctx->channels = s->ti.channels;
787
+
788
+    s->nb_samples = s->ti.last_frame_samples ? s->ti.last_frame_samples :
789
+                                               s->ti.frame_samples;
790
+
791
+    s->frame.nb_samples = s->nb_samples;
792
+    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0)
793
+        return ret;
794
+
795
+    if (avctx->bits_per_coded_sample <= 16) {
796
+        av_fast_malloc(&s->decode_buffer, &s->decode_buffer_size,
797
+                       sizeof(*s->decode_buffer) * FFALIGN(s->nb_samples, 8) *
798
+                       avctx->channels + FF_INPUT_BUFFER_PADDING_SIZE);
799
+        if (!s->decode_buffer)
800
+            return AVERROR(ENOMEM);
801
+        for (chan = 0; chan < avctx->channels; chan++)
802
+            s->decoded[chan] = s->decode_buffer +
803
+                               chan * FFALIGN(s->nb_samples, 8);
804
+    } else {
805
+        for (chan = 0; chan < avctx->channels; chan++)
806
+            s->decoded[chan] = (int32_t *)s->frame.data[chan];
807
+    }
808
+
809
+    if (s->nb_samples < 16) {
810
+        for (chan = 0; chan < avctx->channels; chan++) {
811
+            p = s->decoded[chan];
812
+            for (i = 0; i < s->nb_samples; i++)
813
+                *p++ = get_code(gb, avctx->bits_per_coded_sample);
814
+        }
815
+    } else {
816
+        if (s->ti.codec == 2) {
817
+            for (chan = 0; chan < avctx->channels; chan++) {
818
+                if (ret = decode_channel(s, chan))
819
+                    return ret;
820
+            }
821
+
822
+            if (avctx->channels == 2) {
823
+                s->nb_subframes = get_bits(gb, 1) + 1;
824
+                if (s->nb_subframes > 1)
825
+                    s->subframe_len[1] = get_bits(gb, 6);
826
+
827
+                s->dmode = get_bits(gb, 3);
828
+                if (ret = decorrelate(s, 0, 1, s->nb_samples - 1))
829
+                    return ret;
830
+            }
831
+        } else if (s->ti.codec == 4) {
832
+            if (get_bits1(gb)) {
833
+                int ch_mask = 0;
834
+
835
+                chan = get_bits(gb, 4) + 1;
836
+                if (chan > avctx->channels)
837
+                    return AVERROR_INVALIDDATA;
838
+
839
+                for (i = 0; i < chan; i++) {
840
+                    int nbit = get_bits(gb, 4);
841
+
842
+                    if (nbit >= avctx->channels)
843
+                        return AVERROR_INVALIDDATA;
844
+
845
+                    if (ch_mask & 1 << nbit)
846
+                        return AVERROR_INVALIDDATA;
847
+
848
+                    s->mcdparams[i].present = get_bits1(gb);
849
+                    if (s->mcdparams[i].present) {
850
+                        s->mcdparams[i].index = get_bits(gb, 2);
851
+                        s->mcdparams[i].chan2 = get_bits(gb, 4);
852
+                        if (s->mcdparams[i].index == 1) {
853
+                            if ((nbit == s->mcdparams[i].chan2) ||
854
+                                (ch_mask & 1 << s->mcdparams[i].chan2))
855
+                                return AVERROR_INVALIDDATA;
856
+
857
+                            ch_mask |= 1 << s->mcdparams[i].chan2;
858
+                        } else if (!(ch_mask & 1 << s->mcdparams[i].chan2)) {
859
+                            return AVERROR_INVALIDDATA;
860
+                        }
861
+                    }
862
+                    s->mcdparams[i].chan1 = nbit;
863
+
864
+                    ch_mask |= 1 << nbit;
865
+                }
866
+            } else {
867
+                chan = avctx->channels;
868
+                for (i = 0; i < chan; i++) {
869
+                    s->mcdparams[i].present = 0;
870
+                    s->mcdparams[i].chan1   = i;
871
+                }
872
+            }
873
+
874
+            for (i = 0; i < chan; i++) {
875
+                if (s->mcdparams[i].present && s->mcdparams[i].index == 1) {
876
+                    if (ret = decode_channel(s, s->mcdparams[i].chan2))
877
+                        return ret;
878
+                }
879
+
880
+                if (ret = decode_channel(s, s->mcdparams[i].chan1))
881
+                    return ret;
882
+
883
+                if (s->mcdparams[i].present) {
884
+                    s->dmode = mc_dmodes[s->mcdparams[i].index];
885
+                    if (ret = decorrelate(s, s->mcdparams[i].chan2,
886
+                                             s->mcdparams[i].chan1,
887
+                                             s->nb_samples - 1))
888
+                        return ret;
889
+                }
890
+            }
891
+        }
892
+
893
+        for (chan = 0; chan < avctx->channels; chan++) {
894
+            p = s->decoded[chan];
895
+            decode_lpc(p, s->lpc_mode[chan], s->nb_samples);
896
+
897
+            if (s->sample_shift[chan] > 0) {
898
+                for (i = 0; i < s->nb_samples; i++)
899
+                    *p++ <<= s->sample_shift[chan];
900
+            }
901
+        }
902
+    }
903
+
904
+    align_get_bits(gb);
905
+    skip_bits(gb, 24);
906
+    if (get_bits_left(gb) < 0)
907
+        av_log(avctx, AV_LOG_DEBUG, "overread\n");
908
+    else if (get_bits_left(gb) > 0)
909
+        av_log(avctx, AV_LOG_DEBUG, "underread\n");
910
+
911
+    if (avctx->err_recognition & AV_EF_CRCCHECK) {
912
+        if (ff_tak_check_crc(pkt->data + hsize,
913
+                             get_bits_count(gb) / 8 - hsize)) {
914
+            av_log(avctx, AV_LOG_ERROR, "CRC error\n");
915
+            return AVERROR_INVALIDDATA;
916
+        }
917
+    }
918
+
919
+    // convert to output buffer
920
+    switch (avctx->bits_per_coded_sample) {
921
+    case 8:
922
+        for (chan = 0; chan < avctx->channels; chan++) {
923
+            uint8_t *samples = (uint8_t *)s->frame.data[chan];
924
+            p = s->decoded[chan];
925
+            for (i = 0; i < s->nb_samples; i++, p++)
926
+                *samples++ = *p + 0x80;
927
+        }
928
+        break;
929
+    case 16:
930
+        for (chan = 0; chan < avctx->channels; chan++) {
931
+            int16_t *samples = (int16_t *)s->frame.data[chan];
932
+            p = s->decoded[chan];
933
+            for (i = 0; i < s->nb_samples; i++, p++)
934
+                *samples++ = *p;
935
+        }
936
+        break;
937
+    case 24:
938
+        for (chan = 0; chan < avctx->channels; chan++) {
939
+            int32_t *samples = (int32_t *)s->frame.data[chan];
940
+            for (i = 0; i < s->nb_samples; i++)
941
+                *samples++ <<= 8;
942
+        }
943
+        break;
944
+    }
945
+
946
+    *got_frame_ptr   = 1;
947
+    *(AVFrame *)data = s->frame;
948
+
949
+    return pkt->size;
950
+}
951
+
952
+static av_cold int tak_decode_close(AVCodecContext *avctx)
953
+{
954
+    TAKDecContext *s = avctx->priv_data;
955
+
956
+    av_freep(&s->decode_buffer);
957
+
958
+    return 0;
959
+}
960
+
961
+AVCodec ff_tak_decoder = {
962
+    .name           = "tak",
963
+    .type           = AVMEDIA_TYPE_AUDIO,
964
+    .id             = AV_CODEC_ID_TAK,
965
+    .priv_data_size = sizeof(TAKDecContext),
966
+    .init           = tak_decode_init,
967
+    .close          = tak_decode_close,
968
+    .decode         = tak_decode_frame,
969
+    .capabilities   = CODEC_CAP_DR1,
970
+    .long_name      = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
971
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 54
32
-#define LIBAVCODEC_VERSION_MINOR 64
32
+#define LIBAVCODEC_VERSION_MINOR 65
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -333,6 +333,7 @@ OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
333 333
 OBJS-$(CONFIG_SUBVIEWER_DEMUXER)         += subviewerdec.o
334 334
 OBJS-$(CONFIG_SWF_DEMUXER)               += swfdec.o swf.o
335 335
 OBJS-$(CONFIG_SWF_MUXER)                 += swfenc.o swf.o
336
+OBJS-$(CONFIG_TAK_DEMUXER)               += takdec.o apetag.o img2.o rawdec.o
336 337
 OBJS-$(CONFIG_THP_DEMUXER)               += thp.o
337 338
 OBJS-$(CONFIG_TIERTEXSEQ_DEMUXER)        += tiertexseq.o
338 339
 OBJS-$(CONFIG_MKVTIMESTAMP_V2_MUXER)     += mkvtimestamp_v2.o
... ...
@@ -233,6 +233,7 @@ void av_register_all(void)
233 233
     REGISTER_DEMUXER  (STR, str);
234 234
     REGISTER_DEMUXER  (SUBVIEWER, subviewer);
235 235
     REGISTER_MUXDEMUX (SWF, swf);
236
+    REGISTER_DEMUXER  (TAK, tak);
236 237
     REGISTER_MUXER    (TG2, tg2);
237 238
     REGISTER_MUXER    (TGP, tgp);
238 239
     REGISTER_DEMUXER  (THP, thp);
239 240
new file mode 100644
... ...
@@ -0,0 +1,184 @@
0
+/*
1
+ * Raw TAK demuxer
2
+ * Copyright (c) 2012 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavcodec/tak.h"
22
+#include "avformat.h"
23
+#include "internal.h"
24
+#include "rawdec.h"
25
+#include "apetag.h"
26
+
27
+typedef struct TAKDemuxContext {
28
+    int      mlast_frame;
29
+    int64_t  left;
30
+} TAKDemuxContext;
31
+
32
+static int tak_probe(AVProbeData *p)
33
+{
34
+    if (!memcmp(p->buf, "tBaK", 4))
35
+        return AVPROBE_SCORE_MAX / 2;
36
+    return 0;
37
+}
38
+
39
+static int tak_read_header(AVFormatContext *s)
40
+{
41
+    TAKDemuxContext *tc = s->priv_data;
42
+    AVIOContext *pb = s->pb;
43
+    GetBitContext gb;
44
+    AVStream *st;
45
+    uint8_t *buffer = NULL;
46
+    int ret;
47
+
48
+    st = avformat_new_stream(s, 0);
49
+    if (!st)
50
+        return AVERROR(ENOMEM);
51
+
52
+    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
53
+    st->codec->codec_id   = AV_CODEC_ID_TAK;
54
+    st->need_parsing      = AVSTREAM_PARSE_FULL_RAW;
55
+
56
+    tc->mlast_frame = 0;
57
+    if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
58
+        avio_seek(pb, -4, SEEK_CUR);
59
+        return 0;
60
+    }
61
+
62
+    while (!url_feof(pb)) {
63
+        enum TAKMetaDataType type;
64
+        int size;
65
+
66
+        type = avio_r8(pb) & 0x7f;
67
+        size = avio_rl24(pb);
68
+
69
+        switch (type) {
70
+        case TAK_METADATA_STREAMINFO:
71
+        case TAK_METADATA_LAST_FRAME:
72
+        case TAK_METADATA_ENCODER:
73
+            buffer = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
74
+            if (!buffer)
75
+                return AVERROR(ENOMEM);
76
+
77
+            if (avio_read(pb, buffer, size) != size) {
78
+                av_freep(&buffer);
79
+                return AVERROR(EIO);
80
+            }
81
+
82
+            init_get_bits(&gb, buffer, size * 8);
83
+            break;
84
+        case TAK_METADATA_MD5: {
85
+            uint8_t md5[16];
86
+            int i;
87
+
88
+            if (size != 19)
89
+                return AVERROR_INVALIDDATA;
90
+            avio_read(pb, md5, 16);
91
+            avio_skip(pb, 3);
92
+            av_log(s, AV_LOG_VERBOSE, "MD5=");
93
+            for (i = 0; i < 16; i++)
94
+                av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
95
+            av_log(s, AV_LOG_VERBOSE, "\n");
96
+            break;
97
+            }
98
+        case TAK_METADATA_END:
99
+            if (pb->seekable) {
100
+                int64_t curpos = avio_tell(pb);
101
+
102
+                ff_ape_parse_tag(s);
103
+                avio_seek(pb, curpos, SEEK_SET);
104
+            }
105
+            return 0;
106
+            break;
107
+        default:
108
+            ret = avio_skip(pb, size);
109
+            if (ret < 0)
110
+                return ret;
111
+        }
112
+
113
+        if (type == TAK_METADATA_STREAMINFO) {
114
+            TAKStreamInfo ti;
115
+
116
+            avpriv_tak_parse_streaminfo(&gb, &ti);
117
+            if (ti.samples > 0)
118
+                st->duration = ti.samples;
119
+            st->codec->bits_per_coded_sample = ti.bps;
120
+            if (ti.ch_layout)
121
+                st->codec->channel_layout = ti.ch_layout;
122
+            st->codec->sample_rate = ti.sample_rate;
123
+            st->codec->channels    = ti.channels;
124
+            st->start_time         = 0;
125
+            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
126
+            st->codec->extradata      = buffer;
127
+            st->codec->extradata_size = size;
128
+            buffer = NULL;
129
+        } else if (type == TAK_METADATA_LAST_FRAME) {
130
+            if (size != 11)
131
+                return AVERROR_INVALIDDATA;
132
+            tc->mlast_frame = 1;
133
+            tc->left = get_bits_longlong(&gb, TAK_LAST_FRAME_POS_BITS) +
134
+                       get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
135
+            av_freep(&buffer);
136
+        } else if (type == TAK_METADATA_ENCODER) {
137
+            av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
138
+                   get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
139
+            av_freep(&buffer);
140
+        }
141
+    }
142
+
143
+    return AVERROR_EOF;
144
+}
145
+
146
+static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
147
+{
148
+    TAKDemuxContext *tc = s->priv_data;
149
+    int ret;
150
+
151
+    if (tc->mlast_frame) {
152
+        AVIOContext *pb = s->pb;
153
+        int64_t size;
154
+
155
+        size = FFMIN(tc->left, 1024);
156
+        if (!size)
157
+            return AVERROR_EOF;
158
+
159
+        ret = av_get_packet(pb, pkt, size);
160
+        if (ret < 0)
161
+            return ret;
162
+
163
+        pkt->stream_index = 0;
164
+        pkt->pos = avio_tell(pb);
165
+        tc->left -= ret;
166
+    } else {
167
+        ret = ff_raw_read_partial_packet(s, pkt);
168
+    }
169
+
170
+    return ret;
171
+}
172
+
173
+AVInputFormat ff_tak_demuxer = {
174
+    .name           = "tak",
175
+    .long_name      = NULL_IF_CONFIG_SMALL("raw TAK"),
176
+    .priv_data_size = sizeof(TAKDemuxContext),
177
+    .read_probe     = tak_probe,
178
+    .read_header    = tak_read_header,
179
+    .read_packet    = raw_read_packet,
180
+    .flags          = AVFMT_GENERIC_INDEX,
181
+    .extensions     = "tak",
182
+    .raw_codec_id   = AV_CODEC_ID_TAK,
183
+};
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 54
33
-#define LIBAVFORMAT_VERSION_MINOR 29
34
-#define LIBAVFORMAT_VERSION_MICRO 105
33
+#define LIBAVFORMAT_VERSION_MINOR 30
34
+#define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \
... ...
@@ -10,6 +10,9 @@ fate-lossless-monkeysaudio: CMD = md5 -i $(SAMPLES)/lossless-audio/luckynight-pa
10 10
 FATE_SAMPLES_LOSSLESS_AUDIO += fate-lossless-shorten
11 11
 fate-lossless-shorten: CMD = md5 -i $(SAMPLES)/lossless-audio/luckynight-partial.shn -f s16le
12 12
 
13
+FATE_SAMPLES_LOSSLESS_AUDIO += fate-lossless-tak
14
+fate-lossless-tak: CMD = crc -i $(SAMPLES)/lossless-audio/luckynight-partial.tak -f s16le
15
+
13 16
 FATE_SAMPLES_LOSSLESS_AUDIO += fate-lossless-tta
14 17
 fate-lossless-tta: CMD = crc -i $(SAMPLES)/lossless-audio/inside.tta
15 18
 
16 19
new file mode 100644
... ...
@@ -0,0 +1 @@
0
+CRC=0x4ec0971f