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/tak.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 int tak_read_header(AVFormatContext *s)
44 TAKDemuxContext *tc = s->priv_data;
45 AVIOContext *pb = s->pb;
48 uint8_t *buffer = NULL;
51 st = avformat_new_stream(s, 0);
53 return AVERROR(ENOMEM);
55 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
56 st->codecpar->codec_id = AV_CODEC_ID_TAK;
57 st->need_parsing = AVSTREAM_PARSE_FULL;
60 if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
61 avio_seek(pb, -4, SEEK_CUR);
65 while (!pb->eof_reached) {
66 enum TAKMetaDataType type;
69 type = avio_r8(pb) & 0x7f;
73 case TAK_METADATA_STREAMINFO:
74 case TAK_METADATA_LAST_FRAME:
75 case TAK_METADATA_ENCODER:
76 buffer = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
78 return AVERROR(ENOMEM);
80 if (avio_read(pb, buffer, size) != size) {
85 init_get_bits(&gb, buffer, size * 8);
87 case TAK_METADATA_MD5: {
92 return AVERROR_INVALIDDATA;
93 avio_read(pb, md5, 16);
95 av_log(s, AV_LOG_VERBOSE, "MD5=");
96 for (i = 0; i < 16; i++)
97 av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
98 av_log(s, AV_LOG_VERBOSE, "\n");
101 case TAK_METADATA_END: {
102 int64_t curpos = avio_tell(pb);
104 if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
106 avio_seek(pb, curpos, SEEK_SET);
109 tc->data_end += curpos;
113 ret = avio_skip(pb, size);
118 if (type == TAK_METADATA_STREAMINFO) {
121 avpriv_tak_parse_streaminfo(&gb, &ti);
123 st->duration = ti.samples;
124 st->codecpar->bits_per_coded_sample = ti.bps;
126 st->codecpar->channel_layout = ti.ch_layout;
127 st->codecpar->sample_rate = ti.sample_rate;
128 st->codecpar->channels = ti.channels;
130 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
131 st->codecpar->extradata = buffer;
132 st->codecpar->extradata_size = size;
134 } else if (type == TAK_METADATA_LAST_FRAME) {
136 return AVERROR_INVALIDDATA;
138 tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
139 get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
141 } else if (type == TAK_METADATA_ENCODER) {
142 av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
143 get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
151 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
153 TAKDemuxContext *tc = s->priv_data;
156 if (tc->mlast_frame) {
157 AVIOContext *pb = s->pb;
160 left = tc->data_end - avio_tell(s->pb);
161 size = FFMIN(left, 1024);
165 ret = av_get_packet(pb, pkt, size);
169 pkt->stream_index = 0;
171 ret = ff_raw_read_partial_packet(s, pkt);
177 AVInputFormat ff_tak_demuxer = {
179 .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
180 .priv_data_size = sizeof(TAKDemuxContext),
181 .read_probe = tak_probe,
182 .read_header = tak_read_header,
183 .read_packet = raw_read_packet,
184 .flags = AVFMT_GENERIC_INDEX,
186 .raw_codec_id = AV_CODEC_ID_TAK,