FFmpeg coverage


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