FFmpeg coverage


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