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"
23 #include "libavutil/mem.h"
25 #define BITSTREAM_READER_LE
26 #include "libavcodec/tak.h"
30 #include "avio_internal.h"
35 typedef struct TAKDemuxContext
{
36 FFRawDemuxerContext rawctx
;
41 static int tak_probe(const AVProbeData
*p
)
43 if (!memcmp(p
->buf
, "tBaK", 4))
44 return AVPROBE_SCORE_EXTENSION
;
48 static unsigned long tak_check_crc(unsigned long checksum
, const uint8_t *buf
,
51 return av_crc(av_crc_get_table(AV_CRC_24_IEEE
), checksum
, buf
, len
);
54 static int tak_read_header(AVFormatContext
*s
)
56 TAKDemuxContext
*tc
= s
->priv_data
;
57 AVIOContext
*pb
= s
->pb
;
60 uint8_t *buffer
= NULL
;
63 st
= avformat_new_stream(s
, 0);
65 return AVERROR(ENOMEM
);
67 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
68 st
->codecpar
->codec_id
= AV_CODEC_ID_TAK
;
69 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
72 if (avio_rl32(pb
) != MKTAG('t', 'B', 'a', 'K')) {
73 avio_seek(pb
, -4, SEEK_CUR
);
77 while (!avio_feof(pb
)) {
78 enum TAKMetaDataType type
;
81 type
= avio_r8(pb
) & 0x7f;
85 case TAK_METADATA_STREAMINFO
:
86 if (st
->codecpar
->extradata
)
87 return AVERROR_INVALIDDATA
;
88 case TAK_METADATA_LAST_FRAME
:
89 case TAK_METADATA_ENCODER
:
91 return AVERROR_INVALIDDATA
;
93 buffer
= av_malloc(size
- 3 + AV_INPUT_BUFFER_PADDING_SIZE
);
95 return AVERROR(ENOMEM
);
96 memset(buffer
+ size
- 3, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
98 ffio_init_checksum(pb
, tak_check_crc
, 0xCE04B7U
);
99 ret
= ffio_read_size(pb
, buffer
, size
- 3);
104 if (ffio_get_checksum(s
->pb
) != avio_rb24(pb
)) {
105 av_log(s
, AV_LOG_ERROR
, "%d metadata block CRC error.\n", type
);
106 if (s
->error_recognition
& AV_EF_EXPLODE
) {
108 return AVERROR_INVALIDDATA
;
113 case TAK_METADATA_MD5
: {
115 char md5_hex
[2 * sizeof(md5
) + 1];
118 return AVERROR_INVALIDDATA
;
119 ffio_init_checksum(pb
, tak_check_crc
, 0xCE04B7U
);
120 ret
= ffio_read_size(pb
, md5
, 16);
123 if (ffio_get_checksum(s
->pb
) != avio_rb24(pb
)) {
124 av_log(s
, AV_LOG_ERROR
, "MD5 metadata block CRC error.\n");
125 if (s
->error_recognition
& AV_EF_EXPLODE
)
126 return AVERROR_INVALIDDATA
;
129 ff_data_to_hex(md5_hex
, md5
, sizeof(md5
), 1);
130 av_log(s
, AV_LOG_VERBOSE
, "MD5=%s\n", md5_hex
);
133 case TAK_METADATA_END
: {
134 int64_t curpos
= avio_tell(pb
);
136 if (pb
->seekable
& AVIO_SEEKABLE_NORMAL
) {
138 avio_seek(pb
, curpos
, SEEK_SET
);
141 tc
->data_end
+= curpos
;
145 int64_t ret64
= avio_skip(pb
, size
);
151 if (type
== TAK_METADATA_STREAMINFO
) {
154 ret
= avpriv_tak_parse_streaminfo(&ti
, buffer
, size
-3);
158 st
->duration
= ti
.samples
;
159 st
->codecpar
->bits_per_coded_sample
= ti
.bps
;
161 av_channel_layout_from_mask(&st
->codecpar
->ch_layout
, ti
.ch_layout
);
163 av_channel_layout_uninit(&st
->codecpar
->ch_layout
);
164 st
->codecpar
->ch_layout
.order
= AV_CHANNEL_ORDER_UNSPEC
;
165 st
->codecpar
->ch_layout
.nb_channels
= ti
.channels
;
167 st
->codecpar
->sample_rate
= ti
.sample_rate
;
169 avpriv_set_pts_info(st
, 64, 1, st
->codecpar
->sample_rate
);
170 st
->codecpar
->extradata
= buffer
;
171 st
->codecpar
->extradata_size
= size
- 3;
173 } else if (type
== TAK_METADATA_LAST_FRAME
) {
175 ret
= AVERROR_INVALIDDATA
;
178 init_get_bits8(&gb
, buffer
, size
- 3);
180 tc
->data_end
= get_bits64(&gb
, TAK_LAST_FRAME_POS_BITS
) +
181 get_bits(&gb
, TAK_LAST_FRAME_SIZE_BITS
);
183 } else if (type
== TAK_METADATA_ENCODER
) {
184 av_log(s
, AV_LOG_VERBOSE
, "encoder version: %0X\n",
196 static int raw_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
198 TAKDemuxContext
*tc
= s
->priv_data
;
201 if (tc
->mlast_frame
) {
202 AVIOContext
*pb
= s
->pb
;
205 left
= tc
->data_end
- avio_tell(pb
);
206 size
= FFMIN(left
, 1024);
210 ret
= av_get_packet(pb
, pkt
, size
);
214 pkt
->stream_index
= 0;
216 ret
= ff_raw_read_partial_packet(s
, pkt
);
222 const FFInputFormat ff_tak_demuxer
= {
224 .p
.long_name
= NULL_IF_CONFIG_SMALL("raw TAK"),
225 .p
.flags
= AVFMT_GENERIC_INDEX
,
226 .p
.extensions
= "tak",
227 .p
.priv_class
= &ff_raw_demuxer_class
,
228 .priv_data_size
= sizeof(TAKDemuxContext
),
229 .read_probe
= tak_probe
,
230 .read_header
= tak_read_header
,
231 .read_packet
= raw_read_packet
,
232 .raw_codec_id
= AV_CODEC_ID_TAK
,