• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Raw TAK demuxer
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "libavutil/crc.h"
23 
24 #define BITSTREAM_READER_LE
25 #include "libavcodec/tak.h"
26 
27 #include "apetag.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "internal.h"
31 #include "rawdec.h"
32 
33 typedef struct TAKDemuxContext {
34     AVClass *class;
35     int     raw_packet_size;
36     int     mlast_frame;
37     int64_t data_end;
38 } TAKDemuxContext;
39 
tak_probe(const AVProbeData * p)40 static int tak_probe(const AVProbeData *p)
41 {
42     if (!memcmp(p->buf, "tBaK", 4))
43         return AVPROBE_SCORE_EXTENSION;
44     return 0;
45 }
46 
tak_check_crc(unsigned long checksum,const uint8_t * buf,unsigned int len)47 static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf,
48                                    unsigned int len)
49 {
50     return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len);
51 }
52 
tak_read_header(AVFormatContext * s)53 static int tak_read_header(AVFormatContext *s)
54 {
55     TAKDemuxContext *tc = s->priv_data;
56     AVIOContext *pb     = s->pb;
57     GetBitContext gb;
58     AVStream *st;
59     uint8_t *buffer = NULL;
60     int ret;
61 
62     st = avformat_new_stream(s, 0);
63     if (!st)
64         return AVERROR(ENOMEM);
65 
66     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
67     st->codecpar->codec_id   = AV_CODEC_ID_TAK;
68     st->need_parsing         = AVSTREAM_PARSE_FULL_RAW;
69 
70     tc->mlast_frame = 0;
71     if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
72         avio_seek(pb, -4, SEEK_CUR);
73         return 0;
74     }
75 
76     while (!avio_feof(pb)) {
77         enum TAKMetaDataType type;
78         int size;
79 
80         type = avio_r8(pb) & 0x7f;
81         size = avio_rl24(pb);
82 
83         switch (type) {
84         case TAK_METADATA_STREAMINFO:
85             if (st->codecpar->extradata)
86                 return AVERROR_INVALIDDATA;
87         case TAK_METADATA_LAST_FRAME:
88         case TAK_METADATA_ENCODER:
89             if (size <= 3)
90                 return AVERROR_INVALIDDATA;
91 
92             buffer = av_malloc(size - 3 + AV_INPUT_BUFFER_PADDING_SIZE);
93             if (!buffer)
94                 return AVERROR(ENOMEM);
95             memset(buffer + size - 3, 0, AV_INPUT_BUFFER_PADDING_SIZE);
96 
97             ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
98             if (avio_read(pb, buffer, size - 3) != size - 3) {
99                 av_freep(&buffer);
100                 return AVERROR(EIO);
101             }
102             if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
103                 av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
104                 if (s->error_recognition & AV_EF_EXPLODE) {
105                     av_freep(&buffer);
106                     return AVERROR_INVALIDDATA;
107                 }
108             }
109 
110             break;
111         case TAK_METADATA_MD5: {
112             uint8_t md5[16];
113             int i;
114 
115             if (size != 19)
116                 return AVERROR_INVALIDDATA;
117             ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
118             avio_read(pb, md5, 16);
119             if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
120                 av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
121                 if (s->error_recognition & AV_EF_EXPLODE)
122                     return AVERROR_INVALIDDATA;
123             }
124 
125             av_log(s, AV_LOG_VERBOSE, "MD5=");
126             for (i = 0; i < 16; i++)
127                 av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
128             av_log(s, AV_LOG_VERBOSE, "\n");
129             break;
130         }
131         case TAK_METADATA_END: {
132             int64_t curpos = avio_tell(pb);
133 
134             if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
135                 ff_ape_parse_tag(s);
136                 avio_seek(pb, curpos, SEEK_SET);
137             }
138 
139             tc->data_end += curpos;
140             return 0;
141         }
142         default:
143             ret = avio_skip(pb, size);
144             if (ret < 0)
145                 return ret;
146         }
147 
148         if (type == TAK_METADATA_STREAMINFO) {
149             TAKStreamInfo ti;
150 
151             ret = avpriv_tak_parse_streaminfo(&ti, buffer, size -3);
152             if (ret < 0)
153                 goto end;
154             if (ti.samples > 0)
155                 st->duration = ti.samples;
156             st->codecpar->bits_per_coded_sample = ti.bps;
157             if (ti.ch_layout)
158                 st->codecpar->channel_layout = ti.ch_layout;
159             st->codecpar->sample_rate           = ti.sample_rate;
160             st->codecpar->channels              = ti.channels;
161             st->start_time                   = 0;
162             avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
163             st->codecpar->extradata             = buffer;
164             st->codecpar->extradata_size        = size - 3;
165             buffer                           = NULL;
166         } else if (type == TAK_METADATA_LAST_FRAME) {
167             if (size != 11) {
168                 ret = AVERROR_INVALIDDATA;
169                 goto end;
170             }
171             init_get_bits8(&gb, buffer, size - 3);
172             tc->mlast_frame = 1;
173             tc->data_end    = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
174                               get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
175             av_freep(&buffer);
176         } else if (type == TAK_METADATA_ENCODER) {
177             init_get_bits8(&gb, buffer, size - 3);
178             av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
179                    get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
180             av_freep(&buffer);
181         }
182     }
183 
184     return AVERROR_EOF;
185 end:
186     av_freep(&buffer);
187     return ret;
188 }
189 
raw_read_packet(AVFormatContext * s,AVPacket * pkt)190 static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
191 {
192     TAKDemuxContext *tc = s->priv_data;
193     int ret;
194 
195     if (tc->mlast_frame) {
196         AVIOContext *pb = s->pb;
197         int64_t size, left;
198 
199         left = tc->data_end - avio_tell(pb);
200         size = FFMIN(left, 1024);
201         if (size <= 0)
202             return AVERROR_EOF;
203 
204         ret = av_get_packet(pb, pkt, size);
205         if (ret < 0)
206             return ret;
207 
208         pkt->stream_index = 0;
209     } else {
210         ret = ff_raw_read_partial_packet(s, pkt);
211     }
212 
213     return ret;
214 }
215 
216 FF_RAW_DEMUXER_CLASS(tak)
217 AVInputFormat ff_tak_demuxer = {
218     .name           = "tak",
219     .long_name      = NULL_IF_CONFIG_SMALL("raw TAK"),
220     .priv_data_size = sizeof(TAKDemuxContext),
221     .read_probe     = tak_probe,
222     .read_header    = tak_read_header,
223     .read_packet    = raw_read_packet,
224     .flags          = AVFMT_GENERIC_INDEX,
225     .extensions     = "tak",
226     .raw_codec_id   = AV_CODEC_ID_TAK,
227     .priv_class     = &tak_demuxer_class,
228 };
229