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 "libavcodec/tak.h"
25 #include "avio_internal.h"
30 typedef struct TAKDemuxContext
{
35 static int tak_probe(AVProbeData
*p
)
37 if (!memcmp(p
->buf
, "tBaK", 4))
38 return AVPROBE_SCORE_EXTENSION
;
42 static unsigned long tak_check_crc(unsigned long checksum
, const uint8_t *buf
,
45 return av_crc(av_crc_get_table(AV_CRC_24_IEEE
), checksum
, buf
, len
);
48 static int tak_read_header(AVFormatContext
*s
)
50 TAKDemuxContext
*tc
= s
->priv_data
;
51 AVIOContext
*pb
= s
->pb
;
54 uint8_t *buffer
= NULL
;
57 st
= avformat_new_stream(s
, 0);
59 return AVERROR(ENOMEM
);
61 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
62 st
->codec
->codec_id
= AV_CODEC_ID_TAK
;
63 st
->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
66 if (avio_rl32(pb
) != MKTAG('t', 'B', 'a', 'K')) {
67 avio_seek(pb
, -4, SEEK_CUR
);
71 while (!avio_feof(pb
)) {
72 enum TAKMetaDataType type
;
75 type
= avio_r8(pb
) & 0x7f;
79 case TAK_METADATA_STREAMINFO
:
80 case TAK_METADATA_LAST_FRAME
:
81 case TAK_METADATA_ENCODER
:
83 return AVERROR_INVALIDDATA
;
85 buffer
= av_malloc(size
- 3 + FF_INPUT_BUFFER_PADDING_SIZE
);
87 return AVERROR(ENOMEM
);
88 memset(buffer
+ size
- 3, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
90 ffio_init_checksum(pb
, tak_check_crc
, 0xCE04B7U
);
91 if (avio_read(pb
, buffer
, size
- 3) != size
- 3) {
95 if (ffio_get_checksum(s
->pb
) != avio_rb24(pb
)) {
96 av_log(s
, AV_LOG_ERROR
, "%d metadata block CRC error.\n", type
);
97 if (s
->error_recognition
& AV_EF_EXPLODE
) {
99 return AVERROR_INVALIDDATA
;
103 init_get_bits8(&gb
, buffer
, size
- 3);
105 case TAK_METADATA_MD5
: {
110 return AVERROR_INVALIDDATA
;
111 ffio_init_checksum(pb
, tak_check_crc
, 0xCE04B7U
);
112 avio_read(pb
, md5
, 16);
113 if (ffio_get_checksum(s
->pb
) != avio_rb24(pb
)) {
114 av_log(s
, AV_LOG_ERROR
, "MD5 metadata block CRC error.\n");
115 if (s
->error_recognition
& AV_EF_EXPLODE
)
116 return AVERROR_INVALIDDATA
;
119 av_log(s
, AV_LOG_VERBOSE
, "MD5=");
120 for (i
= 0; i
< 16; i
++)
121 av_log(s
, AV_LOG_VERBOSE
, "%02x", md5
[i
]);
122 av_log(s
, AV_LOG_VERBOSE
, "\n");
125 case TAK_METADATA_END
: {
126 int64_t curpos
= avio_tell(pb
);
130 avio_seek(pb
, curpos
, SEEK_SET
);
133 tc
->data_end
+= curpos
;
137 ret
= avio_skip(pb
, size
);
142 if (type
== TAK_METADATA_STREAMINFO
) {
145 avpriv_tak_parse_streaminfo(&gb
, &ti
);
147 st
->duration
= ti
.samples
;
148 st
->codec
->bits_per_coded_sample
= ti
.bps
;
150 st
->codec
->channel_layout
= ti
.ch_layout
;
151 st
->codec
->sample_rate
= ti
.sample_rate
;
152 st
->codec
->channels
= ti
.channels
;
154 avpriv_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
155 st
->codec
->extradata
= buffer
;
156 st
->codec
->extradata_size
= size
- 3;
158 } else if (type
== TAK_METADATA_LAST_FRAME
) {
160 return AVERROR_INVALIDDATA
;
162 tc
->data_end
= get_bits64(&gb
, TAK_LAST_FRAME_POS_BITS
) +
163 get_bits(&gb
, TAK_LAST_FRAME_SIZE_BITS
);
165 } else if (type
== TAK_METADATA_ENCODER
) {
166 av_log(s
, AV_LOG_VERBOSE
, "encoder version: %0X\n",
167 get_bits_long(&gb
, TAK_ENCODER_VERSION_BITS
));
175 static int raw_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
177 TAKDemuxContext
*tc
= s
->priv_data
;
180 if (tc
->mlast_frame
) {
181 AVIOContext
*pb
= s
->pb
;
184 left
= tc
->data_end
- avio_tell(pb
);
185 size
= FFMIN(left
, 1024);
189 ret
= av_get_packet(pb
, pkt
, size
);
193 pkt
->stream_index
= 0;
195 ret
= ff_raw_read_partial_packet(s
, pkt
);
201 AVInputFormat ff_tak_demuxer
= {
203 .long_name
= NULL_IF_CONFIG_SMALL("raw TAK"),
204 .priv_data_size
= sizeof(TAKDemuxContext
),
205 .read_probe
= tak_probe
,
206 .read_header
= tak_read_header
,
207 .read_packet
= raw_read_packet
,
208 .flags
= AVFMT_GENERIC_INDEX
,
210 .raw_codec_id
= AV_CODEC_ID_TAK
,