FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/4xm.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 151 196 77.0%
Functions: 6 6 100.0%
Branches: 68 110 61.8%

Line Branch Exec Source
1 /*
2 * 4X Technologies .4xm File Demuxer (no muxer)
3 * Copyright (c) 2003 The FFmpeg project
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 /**
23 * @file
24 * 4X Technologies file demuxer
25 * by Mike Melanson (melanson@pcisys.net)
26 * for more information on the .4xm file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 */
29
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/intfloat.h"
32 #include "libavutil/mem.h"
33 #include "libavcodec/internal.h"
34 #include "avformat.h"
35 #include "demux.h"
36 #include "internal.h"
37
38 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
39 #define FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
40 #define LIST_TAG MKTAG('L', 'I', 'S', 'T')
41 #define HEAD_TAG MKTAG('H', 'E', 'A', 'D')
42 #define TRK__TAG MKTAG('T', 'R', 'K', '_')
43 #define MOVI_TAG MKTAG('M', 'O', 'V', 'I')
44 #define VTRK_TAG MKTAG('V', 'T', 'R', 'K')
45 #define STRK_TAG MKTAG('S', 'T', 'R', 'K')
46 #define std__TAG MKTAG('s', 't', 'd', '_')
47 #define name_TAG MKTAG('n', 'a', 'm', 'e')
48 #define vtrk_TAG MKTAG('v', 't', 'r', 'k')
49 #define strk_TAG MKTAG('s', 't', 'r', 'k')
50 #define ifrm_TAG MKTAG('i', 'f', 'r', 'm')
51 #define pfrm_TAG MKTAG('p', 'f', 'r', 'm')
52 #define cfrm_TAG MKTAG('c', 'f', 'r', 'm')
53 #define ifr2_TAG MKTAG('i', 'f', 'r', '2')
54 #define pfr2_TAG MKTAG('p', 'f', 'r', '2')
55 #define cfr2_TAG MKTAG('c', 'f', 'r', '2')
56 #define snd__TAG MKTAG('s', 'n', 'd', '_')
57
58 #define vtrk_SIZE 0x44
59 #define strk_SIZE 0x28
60
61 #define GET_LIST_HEADER() \
62 fourcc_tag = avio_rl32(pb); \
63 size = avio_rl32(pb); \
64 if (fourcc_tag != LIST_TAG) { \
65 ret = AVERROR_INVALIDDATA; \
66 goto fail; \
67 } \
68 fourcc_tag = avio_rl32(pb);
69
70 typedef struct AudioTrack {
71 int sample_rate;
72 int bits;
73 int channels;
74 int stream_index;
75 int adpcm;
76 int64_t audio_pts;
77 } AudioTrack;
78
79 typedef struct FourxmDemuxContext {
80 int video_stream_index;
81 int track_count;
82 AudioTrack *tracks;
83
84 int64_t video_pts;
85 AVRational fps;
86 } FourxmDemuxContext;
87
88 7186 static int fourxm_probe(const AVProbeData *p)
89 {
90
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 6226 times.
7186 if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
91
2/2
✓ Branch 0 taken 957 times.
✓ Branch 1 taken 3 times.
960 (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
92 7183 return 0;
93
94 3 return AVPROBE_SCORE_MAX;
95 }
96
97 3 static int parse_vtrk(AVFormatContext *s,
98 FourxmDemuxContext *fourxm, uint8_t *buf, int size,
99 int left)
100 {
101 AVStream *st;
102 /* check that there is enough data */
103
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (size != vtrk_SIZE || left < size + 8) {
104 return AVERROR_INVALIDDATA;
105 }
106
107 /* allocate a new AVStream */
108 3 st = avformat_new_stream(s, NULL);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st)
110 return AVERROR(ENOMEM);
111
112 3 avpriv_set_pts_info(st, 60, fourxm->fps.den, fourxm->fps.num);
113
114 3 fourxm->video_stream_index = st->index;
115
116 3 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
117 3 st->codecpar->codec_id = AV_CODEC_ID_4XM;
118
119 3 st->codecpar->extradata = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st->codecpar->extradata)
121 return AVERROR(ENOMEM);
122 3 st->codecpar->extradata_size = 4;
123 3 AV_WL32(st->codecpar->extradata, AV_RL32(buf + 16));
124 3 st->codecpar->width = AV_RL32(buf + 36);
125 3 st->codecpar->height = AV_RL32(buf + 40);
126
127 3 return 0;
128 }
129
130
131 8 static int parse_strk(AVFormatContext *s,
132 FourxmDemuxContext *fourxm, uint8_t *buf, int size,
133 int left)
134 {
135 AVStream *st;
136 int track;
137 /* check that there is enough data */
138
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (size != strk_SIZE || left < size + 8)
139 return AVERROR_INVALIDDATA;
140
141 8 track = AV_RL32(buf + 8);
142
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1 ||
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 track >= s->max_streams) {
144 av_log(s, AV_LOG_ERROR, "current_track too large\n");
145 return AVERROR_INVALIDDATA;
146 }
147
148
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (track + 1 > fourxm->track_count) {
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (av_reallocp_array(&fourxm->tracks, track + 1, sizeof(AudioTrack)))
150 return AVERROR(ENOMEM);
151 8 memset(&fourxm->tracks[fourxm->track_count], 0,
152 8 sizeof(AudioTrack) * (track + 1 - fourxm->track_count));
153 8 fourxm->track_count = track + 1;
154 } else {
155 if (fourxm->tracks[track].bits)
156 return AVERROR_INVALIDDATA;
157 }
158 8 fourxm->tracks[track].adpcm = AV_RL32(buf + 12);
159 8 fourxm->tracks[track].channels = AV_RL32(buf + 36);
160 8 fourxm->tracks[track].sample_rate = AV_RL32(buf + 40);
161 8 fourxm->tracks[track].bits = AV_RL32(buf + 44);
162 8 fourxm->tracks[track].audio_pts = 0;
163
164
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (fourxm->tracks[track].channels <= 0 ||
165
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 fourxm->tracks[track].channels > FF_SANE_NB_CHANNELS ||
166
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 fourxm->tracks[track].sample_rate <= 0 ||
167
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 fourxm->tracks[track].bits <= 0 ||
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 fourxm->tracks[track].bits > INT_MAX / FF_SANE_NB_CHANNELS) {
169 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
170 return AVERROR_INVALIDDATA;
171 }
172
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
8 if (!fourxm->tracks[track].adpcm && fourxm->tracks[track].bits<8) {
173 av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
174 return AVERROR_INVALIDDATA;
175 }
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (fourxm->tracks[track].sample_rate > INT64_MAX / fourxm->tracks[track].bits / fourxm->tracks[track].channels) {
178 av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * %d * %d\n",
179 fourxm->tracks[track].sample_rate, fourxm->tracks[track].bits, fourxm->tracks[track].channels);
180 return AVERROR_INVALIDDATA;
181 }
182
183 /* allocate a new AVStream */
184 8 st = avformat_new_stream(s, NULL);
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!st)
186 return AVERROR(ENOMEM);
187
188 8 st->id = track;
189 8 avpriv_set_pts_info(st, 60, 1, fourxm->tracks[track].sample_rate);
190
191 8 fourxm->tracks[track].stream_index = st->index;
192
193 8 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
194 8 st->codecpar->codec_tag = 0;
195 8 st->codecpar->ch_layout.nb_channels = fourxm->tracks[track].channels;
196 8 st->codecpar->sample_rate = fourxm->tracks[track].sample_rate;
197 8 st->codecpar->bits_per_coded_sample = fourxm->tracks[track].bits;
198 8 st->codecpar->bit_rate = (int64_t)st->codecpar->ch_layout.nb_channels *
199 8 st->codecpar->sample_rate *
200 8 st->codecpar->bits_per_coded_sample;
201 8 st->codecpar->block_align = st->codecpar->ch_layout.nb_channels *
202 8 st->codecpar->bits_per_coded_sample;
203
204
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 if (fourxm->tracks[track].adpcm){
205 7 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_4XM;
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (st->codecpar->bits_per_coded_sample == 8) {
207 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
208 } else
209 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
210
211 8 return 0;
212 }
213
214 3 static int fourxm_read_header(AVFormatContext *s)
215 {
216 3 AVIOContext *pb = s->pb;
217 unsigned int fourcc_tag;
218 unsigned int size;
219 int header_size;
220 3 FourxmDemuxContext *fourxm = s->priv_data;
221 3 unsigned char *header = NULL;
222 int i, ret;
223
224 3 fourxm->track_count = 0;
225 3 fourxm->tracks = NULL;
226 3 fourxm->fps = (AVRational){1,1};
227 3 fourxm->video_stream_index = -1;
228
229 /* skip the first 3 32-bit numbers */
230 3 avio_skip(pb, 12);
231
232 /* check for LIST-HEAD */
233
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 GET_LIST_HEADER();
234 3 header_size = size - 4;
235
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (fourcc_tag != HEAD_TAG || header_size < 0)
236 return AVERROR_INVALIDDATA;
237
238 /* allocate space for the header and load the whole thing */
239 3 header = av_malloc(header_size);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!header)
241 return AVERROR(ENOMEM);
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_read(pb, header, header_size) != header_size) {
243 av_free(header);
244 return AVERROR(EIO);
245 }
246
247 /* take the lazy approach and search for any and all vtrk and strk chunks */
248
2/2
✓ Branch 0 taken 893 times.
✓ Branch 1 taken 3 times.
896 for (i = 0; i < header_size - 8; i++) {
249 893 fourcc_tag = AV_RL32(&header[i]);
250 893 size = AV_RL32(&header[i + 4]);
251
4/6
✓ Branch 0 taken 849 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 849 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 849 times.
893 if (size > header_size - i - 8 && (fourcc_tag == vtrk_TAG || fourcc_tag == strk_TAG)) {
252 av_log(s, AV_LOG_ERROR, "chunk larger than array %d>%d\n", size, header_size - i - 8);
253 ret = AVERROR_INVALIDDATA;
254 goto fail;
255 }
256
257
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 890 times.
893 if (fourcc_tag == std__TAG) {
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (header_size - i < 16) {
259 av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
260 ret = AVERROR_INVALIDDATA;
261 goto fail;
262 }
263 3 fourxm->fps = av_d2q(av_int2float(AV_RL32(&header[i + 12])), 10000);
264
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 887 times.
890 } else if (fourcc_tag == vtrk_TAG) {
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = parse_vtrk(s, fourxm, header + i, size,
266 header_size - i)) < 0)
267 goto fail;
268
269 3 i += 8 + size;
270
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 879 times.
887 } else if (fourcc_tag == strk_TAG) {
271
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if ((ret = parse_strk(s, fourxm, header + i, size,
272 header_size - i)) < 0)
273 goto fail;
274
275 8 i += 8 + size;
276 }
277 }
278
279 /* skip over the LIST-MOVI chunk (which is where the stream should be */
280
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 GET_LIST_HEADER();
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (fourcc_tag != MOVI_TAG) {
282 ret = AVERROR_INVALIDDATA;
283 goto fail;
284 }
285
286 3 av_free(header);
287 /* initialize context members */
288 3 fourxm->video_pts = -1; /* first frame will push to 0 */
289
290 3 return 0;
291 fail:
292 av_free(header);
293 return ret;
294 }
295
296 602 static int fourxm_read_packet(AVFormatContext *s,
297 AVPacket *pkt)
298 {
299 602 FourxmDemuxContext *fourxm = s->priv_data;
300 602 AVIOContext *pb = s->pb;
301 unsigned int fourcc_tag;
302 unsigned int size;
303 602 int ret = 0;
304 unsigned int track_number;
305 602 int packet_read = 0;
306 unsigned char header[8];
307 int64_t audio_frame_count;
308
309
2/2
✓ Branch 0 taken 818 times.
✓ Branch 1 taken 599 times.
1417 while (!packet_read) {
310
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 815 times.
818 if ((ret = avio_read(s->pb, header, 8)) < 0)
311 3 return ret;
312 815 fourcc_tag = AV_RL32(&header[0]);
313 815 size = AV_RL32(&header[4]);
314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 815 times.
815 if (avio_feof(pb))
315 return AVERROR(EIO);
316
3/4
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 251 times.
✓ Branch 2 taken 348 times.
✗ Branch 3 not taken.
815 switch (fourcc_tag) {
317 216 case LIST_TAG:
318 /* this is a good time to bump the video pts */
319 216 fourxm->video_pts++;
320
321 /* skip the LIST-* tag and move on to the next fourcc */
322 216 avio_rl32(pb);
323 216 break;
324
325 251 case ifrm_TAG:
326 case pfrm_TAG:
327 case cfrm_TAG:
328 case ifr2_TAG:
329 case pfr2_TAG:
330 case cfr2_TAG:
331 /* allocate 8 more bytes than 'size' to account for fourcc
332 * and size */
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 8)
334 return AVERROR_INVALIDDATA;
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (fourxm->video_stream_index < 0)
336 return AVERROR_INVALIDDATA;
337
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 251 times.
251 if ((ret = av_new_packet(pkt, size + 8)) < 0)
338 return ret;
339 251 pkt->stream_index = fourxm->video_stream_index;
340 251 pkt->pts = fourxm->video_pts;
341 251 pkt->pos = avio_tell(s->pb);
342 251 memcpy(pkt->data, header, 8);
343 251 ret = avio_read(s->pb, &pkt->data[8], size);
344
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (ret < 0) {
346 av_packet_unref(pkt);
347 } else {
348 251 packet_read = 1;
349 251 av_shrink_packet(pkt, ret + 8);
350 }
351 251 break;
352
353 348 case snd__TAG:
354 348 track_number = avio_rl32(pb);
355 348 avio_skip(pb, 4);
356 348 size -= 8;
357
358
1/2
✓ Branch 0 taken 348 times.
✗ Branch 1 not taken.
348 if (track_number < fourxm->track_count &&
359
1/2
✓ Branch 0 taken 348 times.
✗ Branch 1 not taken.
348 fourxm->tracks[track_number].channels > 0) {
360 348 ret = av_get_packet(s->pb, pkt, size);
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 348 times.
348 if (ret < 0)
362 return ret;
363 348 pkt->stream_index =
364 348 fourxm->tracks[track_number].stream_index;
365 348 pkt->pts = fourxm->tracks[track_number].audio_pts;
366 348 packet_read = 1;
367
368 /* pts accounting */
369 348 audio_frame_count = size;
370
2/2
✓ Branch 0 taken 333 times.
✓ Branch 1 taken 15 times.
348 if (fourxm->tracks[track_number].adpcm)
371 333 audio_frame_count -= 2 * (fourxm->tracks[track_number].channels);
372 348 audio_frame_count /= fourxm->tracks[track_number].channels;
373
2/2
✓ Branch 0 taken 333 times.
✓ Branch 1 taken 15 times.
348 if (fourxm->tracks[track_number].adpcm) {
374 333 audio_frame_count *= 2;
375 } else
376 15 audio_frame_count /=
377 15 (fourxm->tracks[track_number].bits / 8);
378 348 fourxm->tracks[track_number].audio_pts += audio_frame_count;
379 } else {
380 avio_skip(pb, size);
381 }
382 348 break;
383
384 default:
385 avio_skip(pb, size);
386 break;
387 }
388 }
389 599 return ret;
390 }
391
392 3 static int fourxm_read_close(AVFormatContext *s)
393 {
394 3 FourxmDemuxContext *fourxm = s->priv_data;
395
396 3 av_freep(&fourxm->tracks);
397
398 3 return 0;
399 }
400
401 const FFInputFormat ff_fourxm_demuxer = {
402 .p.name = "4xm",
403 .p.long_name = NULL_IF_CONFIG_SMALL("4X Technologies"),
404 .priv_data_size = sizeof(FourxmDemuxContext),
405 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
406 .read_probe = fourxm_probe,
407 .read_header = fourxm_read_header,
408 .read_packet = fourxm_read_packet,
409 .read_close = fourxm_read_close,
410 };
411