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 #include "libavcodec/tak.h"
28 typedef struct TAKDemuxContext {
33 static int tak_probe(AVProbeData *p)
35 if (!memcmp(p->buf, "tBaK", 4))
36 return AVPROBE_SCORE_EXTENSION;
40 static int tak_read_header(AVFormatContext *s)
42 TAKDemuxContext *tc = s->priv_data;
43 AVIOContext *pb = s->pb;
46 uint8_t *buffer = NULL;
49 st = avformat_new_stream(s, 0);
51 return AVERROR(ENOMEM);
53 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
54 st->codec->codec_id = AV_CODEC_ID_TAK;
55 st->need_parsing = AVSTREAM_PARSE_FULL;
58 if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
59 avio_seek(pb, -4, SEEK_CUR);
63 while (!pb->eof_reached) {
64 enum TAKMetaDataType type;
67 type = avio_r8(pb) & 0x7f;
71 case TAK_METADATA_STREAMINFO:
72 case TAK_METADATA_LAST_FRAME:
73 case TAK_METADATA_ENCODER:
74 buffer = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
76 return AVERROR(ENOMEM);
78 if (avio_read(pb, buffer, size) != size) {
83 init_get_bits(&gb, buffer, size * 8);
85 case TAK_METADATA_MD5: {
90 return AVERROR_INVALIDDATA;
91 avio_read(pb, md5, 16);
93 av_log(s, AV_LOG_VERBOSE, "MD5=");
94 for (i = 0; i < 16; i++)
95 av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
96 av_log(s, AV_LOG_VERBOSE, "\n");
99 case TAK_METADATA_END: {
100 int64_t curpos = avio_tell(pb);
104 avio_seek(pb, curpos, SEEK_SET);
107 tc->data_end += curpos;
111 ret = avio_skip(pb, size);
116 if (type == TAK_METADATA_STREAMINFO) {
119 avpriv_tak_parse_streaminfo(&gb, &ti);
121 st->duration = ti.samples;
122 st->codec->bits_per_coded_sample = ti.bps;
124 st->codec->channel_layout = ti.ch_layout;
125 st->codec->sample_rate = ti.sample_rate;
126 st->codec->channels = ti.channels;
128 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
129 st->codec->extradata = buffer;
130 st->codec->extradata_size = size;
132 } else if (type == TAK_METADATA_LAST_FRAME) {
134 return AVERROR_INVALIDDATA;
136 tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
137 get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
139 } else if (type == TAK_METADATA_ENCODER) {
140 av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
141 get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
149 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
151 TAKDemuxContext *tc = s->priv_data;
154 if (tc->mlast_frame) {
155 AVIOContext *pb = s->pb;
158 left = tc->data_end - avio_tell(s->pb);
159 size = FFMIN(left, 1024);
163 ret = av_get_packet(pb, pkt, size);
167 pkt->stream_index = 0;
169 ret = ff_raw_read_partial_packet(s, pkt);
175 AVInputFormat ff_tak_demuxer = {
177 .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
178 .priv_data_size = sizeof(TAKDemuxContext),
179 .read_probe = tak_probe,
180 .read_header = tak_read_header,
181 .read_packet = raw_read_packet,
182 .flags = AVFMT_GENERIC_INDEX,
184 .raw_codec_id = AV_CODEC_ID_TAK,