FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/takdec.c
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 80 115 69.6%
Functions: 4 4 100.0%
Branches: 30 55 54.5%

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