3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/crc.h"
24 #define BITSTREAM_READER_LE
25 #include "libavcodec/tak.h"
29 #include "avio_internal.h"
33 typedef struct TAKDemuxContext
{
38 static int tak_probe(AVProbeData
*p
)
40 if (!memcmp(p
->buf
, "tBaK", 4))
41 return AVPROBE_SCORE_EXTENSION
;
45 static unsigned long tak_check_crc(unsigned long checksum
, const uint8_t *buf
,
48 return av_crc(av_crc_get_table(AV_CRC_24_IEEE
), checksum
, buf
, len
);
51 static int tak_read_header(AVFormatContext
*s
)
53 TAKDemuxContext
*tc
= s
->priv_data
;
54 AVIOContext
*pb
= s
->pb
;
57 uint8_t *buffer
= NULL
;
60 st
= avformat_new_stream(s
, 0);
62 return AVERROR(ENOMEM
);
64 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
65 st
->codecpar
->codec_id
= AV_CODEC_ID_TAK
;
66 st
->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
69 if (avio_rl32(pb
) != MKTAG('t', 'B', 'a', 'K')) {
70 avio_seek(pb
, -4, SEEK_CUR
);
74 while (!avio_feof(pb
)) {
75 enum TAKMetaDataType type
;
78 type
= avio_r8(pb
) & 0x7f;
82 case TAK_METADATA_STREAMINFO
:
83 case TAK_METADATA_LAST_FRAME
:
84 case TAK_METADATA_ENCODER
:
86 return AVERROR_INVALIDDATA
;
88 buffer
= av_malloc(size
- 3 + AV_INPUT_BUFFER_PADDING_SIZE
);
90 return AVERROR(ENOMEM
);
91 memset(buffer
+ size
- 3, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
93 ffio_init_checksum(pb
, tak_check_crc
, 0xCE04B7U
);
94 if (avio_read(pb
, buffer
, size
- 3) != size
- 3) {
98 if (ffio_get_checksum(s
->pb
) != avio_rb24(pb
)) {
99 av_log(s
, AV_LOG_ERROR
, "%d metadata block CRC error.\n", type
);
100 if (s
->error_recognition
& AV_EF_EXPLODE
) {
102 return AVERROR_INVALIDDATA
;
106 init_get_bits8(&gb
, buffer
, size
- 3);
108 case TAK_METADATA_MD5
: {
113 return AVERROR_INVALIDDATA
;
114 ffio_init_checksum(pb
, tak_check_crc
, 0xCE04B7U
);
115 avio_read(pb
, md5
, 16);
116 if (ffio_get_checksum(s
->pb
) != avio_rb24(pb
)) {
117 av_log(s
, AV_LOG_ERROR
, "MD5 metadata block CRC error.\n");
118 if (s
->error_recognition
& AV_EF_EXPLODE
)
119 return AVERROR_INVALIDDATA
;
122 av_log(s
, AV_LOG_VERBOSE
, "MD5=");
123 for (i
= 0; i
< 16; i
++)
124 av_log(s
, AV_LOG_VERBOSE
, "%02x", md5
[i
]);
125 av_log(s
, AV_LOG_VERBOSE
, "\n");
128 case TAK_METADATA_END
: {
129 int64_t curpos
= avio_tell(pb
);
133 avio_seek(pb
, curpos
, SEEK_SET
);
136 tc
->data_end
+= curpos
;
140 ret
= avio_skip(pb
, size
);
145 if (type
== TAK_METADATA_STREAMINFO
) {
148 avpriv_tak_parse_streaminfo(&gb
, &ti
);
150 st
->duration
= ti
.samples
;
151 st
->codecpar
->bits_per_coded_sample
= ti
.bps
;
153 st
->codecpar
->channel_layout
= ti
.ch_layout
;
154 st
->codecpar
->sample_rate
= ti
.sample_rate
;
155 st
->codecpar
->channels
= ti
.channels
;
157 avpriv_set_pts_info(st
, 64, 1, st
->codecpar
->sample_rate
);
158 st
->codecpar
->extradata
= buffer
;
159 st
->codecpar
->extradata_size
= size
- 3;
161 } else if (type
== TAK_METADATA_LAST_FRAME
) {
163 return AVERROR_INVALIDDATA
;
165 tc
->data_end
= get_bits64(&gb
, TAK_LAST_FRAME_POS_BITS
) +
166 get_bits(&gb
, TAK_LAST_FRAME_SIZE_BITS
);
168 } else if (type
== TAK_METADATA_ENCODER
) {
169 av_log(s
, AV_LOG_VERBOSE
, "encoder version: %0X\n",
170 get_bits_long(&gb
, TAK_ENCODER_VERSION_BITS
));
178 static int raw_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
180 TAKDemuxContext
*tc
= s
->priv_data
;
183 if (tc
->mlast_frame
) {
184 AVIOContext
*pb
= s
->pb
;
187 left
= tc
->data_end
- avio_tell(pb
);
188 size
= FFMIN(left
, 1024);
192 ret
= av_get_packet(pb
, pkt
, size
);
196 pkt
->stream_index
= 0;
198 ret
= ff_raw_read_partial_packet(s
, pkt
);
204 AVInputFormat ff_tak_demuxer
= {
206 .long_name
= NULL_IF_CONFIG_SMALL("raw TAK"),
207 .priv_data_size
= sizeof(TAKDemuxContext
),
208 .read_probe
= tak_probe
,
209 .read_header
= tak_read_header
,
210 .read_packet
= raw_read_packet
,
211 .flags
= AVFMT_GENERIC_INDEX
,
213 .raw_codec_id
= AV_CODEC_ID_TAK
,