3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #define BITSTREAM_READER_LE
23 #include "libavcodec/bitstream.h"
24 #include "libavcodec/tak.h"
31 typedef struct TAKDemuxContext {
36 static int tak_probe(AVProbeData *p)
38 if (!memcmp(p->buf, "tBaK", 4))
39 return AVPROBE_SCORE_EXTENSION;
43 static int tak_read_header(AVFormatContext *s)
45 TAKDemuxContext *tc = s->priv_data;
46 AVIOContext *pb = s->pb;
49 uint8_t *buffer = NULL;
52 st = avformat_new_stream(s, 0);
54 return AVERROR(ENOMEM);
56 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
57 st->codecpar->codec_id = AV_CODEC_ID_TAK;
58 st->need_parsing = AVSTREAM_PARSE_FULL;
61 if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
62 avio_seek(pb, -4, SEEK_CUR);
66 while (!pb->eof_reached) {
67 enum TAKMetaDataType type;
70 type = avio_r8(pb) & 0x7f;
74 case TAK_METADATA_STREAMINFO:
75 case TAK_METADATA_LAST_FRAME:
76 case TAK_METADATA_ENCODER:
77 buffer = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
79 return AVERROR(ENOMEM);
81 if (avio_read(pb, buffer, size) != size) {
86 bitstream_init8(&bc, buffer, size);
88 case TAK_METADATA_MD5: {
93 return AVERROR_INVALIDDATA;
94 avio_read(pb, md5, 16);
96 av_log(s, AV_LOG_VERBOSE, "MD5=");
97 for (i = 0; i < 16; i++)
98 av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
99 av_log(s, AV_LOG_VERBOSE, "\n");
102 case TAK_METADATA_END: {
103 int64_t curpos = avio_tell(pb);
105 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
107 avio_seek(pb, curpos, SEEK_SET);
110 tc->data_end += curpos;
114 ret = avio_skip(pb, size);
119 if (type == TAK_METADATA_STREAMINFO) {
122 avpriv_tak_parse_streaminfo(&bc, &ti);
124 st->duration = ti.samples;
125 st->codecpar->bits_per_coded_sample = ti.bps;
127 st->codecpar->channel_layout = ti.ch_layout;
128 st->codecpar->sample_rate = ti.sample_rate;
129 st->codecpar->channels = ti.channels;
131 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
132 st->codecpar->extradata = buffer;
133 st->codecpar->extradata_size = size;
135 } else if (type == TAK_METADATA_LAST_FRAME) {
137 return AVERROR_INVALIDDATA;
139 tc->data_end = bitstream_read_63(&bc, TAK_LAST_FRAME_POS_BITS) +
140 bitstream_read(&bc, TAK_LAST_FRAME_SIZE_BITS);
142 } else if (type == TAK_METADATA_ENCODER) {
143 av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
144 bitstream_read(&bc, TAK_ENCODER_VERSION_BITS));
152 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
154 TAKDemuxContext *tc = s->priv_data;
157 if (tc->mlast_frame) {
158 AVIOContext *pb = s->pb;
161 left = tc->data_end - avio_tell(s->pb);
162 size = FFMIN(left, 1024);
166 ret = av_get_packet(pb, pkt, size);
170 pkt->stream_index = 0;
172 ret = ff_raw_read_partial_packet(s, pkt);
178 AVInputFormat ff_tak_demuxer = {
180 .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
181 .priv_data_size = sizeof(TAKDemuxContext),
182 .read_probe = tak_probe,
183 .read_header = tak_read_header,
184 .read_packet = raw_read_packet,
185 .flags = AVFMT_GENERIC_INDEX,
187 .raw_codec_id = AV_CODEC_ID_TAK,