FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/takdec.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 81 118 68.6%
Functions: 4 4 100.0%
Branches: 30 57 52.6%

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