FFmpeg
Loading...
Searching...
No Matches
4xm.c
Go to the documentation of this file.
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
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
79
88
89static int fourxm_probe(const AVProbeData *p)
90{
91 if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
92 (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
93 return 0;
94
95 return AVPROBE_SCORE_MAX;
96}
97
99 FourxmDemuxContext *fourxm, uint8_t *buf, int size,
100 int left)
101{
102 AVStream *st;
103 /* check that there is enough data */
104 if (size != vtrk_SIZE || left < size + 8) {
105 return AVERROR_INVALIDDATA;
106 }
107
108 /* allocate a new AVStream */
110 if (!st)
111 return AVERROR(ENOMEM);
112
113 avpriv_set_pts_info(st, 60, fourxm->fps.den, fourxm->fps.num);
114
115 fourxm->video_stream_index = st->index;
116
119
121 if (!st->codecpar->extradata)
122 return AVERROR(ENOMEM);
123 st->codecpar->extradata_size = 4;
124 AV_WL32(st->codecpar->extradata, AV_RL32(buf + 16));
125 st->codecpar->width = AV_RL32(buf + 36);
126 st->codecpar->height = AV_RL32(buf + 40);
127
128 return 0;
129}
130
131
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 if (size != strk_SIZE || left < size + 8)
140 return AVERROR_INVALIDDATA;
141
142 track = AV_RL32(buf + 8);
143 if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1 ||
144 track >= s->max_streams) {
145 av_log(s, AV_LOG_ERROR, "current_track too large\n");
146 return AVERROR_INVALIDDATA;
147 }
148
149 if (track + 1 > fourxm->track_count) {
150 if (av_reallocp_array(&fourxm->tracks, track + 1, sizeof(AudioTrack)))
151 return AVERROR(ENOMEM);
152 memset(&fourxm->tracks[fourxm->track_count], 0,
153 sizeof(AudioTrack) * (track + 1 - fourxm->track_count));
154 fourxm->track_count = track + 1;
155 } else {
156 if (fourxm->tracks[track].bits)
157 return AVERROR_INVALIDDATA;
158 }
159 fourxm->tracks[track].adpcm = AV_RL32(buf + 12);
160 fourxm->tracks[track].channels = AV_RL32(buf + 36);
161 fourxm->tracks[track].sample_rate = AV_RL32(buf + 40);
162 fourxm->tracks[track].bits = AV_RL32(buf + 44);
163 fourxm->tracks[track].audio_pts = 0;
164
165 if (fourxm->tracks[track].channels <= 0 ||
166 fourxm->tracks[track].channels > FF_SANE_NB_CHANNELS ||
167 fourxm->tracks[track].sample_rate <= 0 ||
168 fourxm->tracks[track].bits <= 0 ||
169 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 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 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 */
186 if (!st)
187 return AVERROR(ENOMEM);
188
189 st->id = track;
190 avpriv_set_pts_info(st, 60, 1, fourxm->tracks[track].sample_rate);
191
192 fourxm->tracks[track].stream_index = st->index;
193
195 st->codecpar->codec_tag = 0;
196 st->codecpar->ch_layout.nb_channels = fourxm->tracks[track].channels;
197 st->codecpar->sample_rate = fourxm->tracks[track].sample_rate;
198 st->codecpar->bits_per_coded_sample = fourxm->tracks[track].bits;
200 st->codecpar->sample_rate *
204
205 if (fourxm->tracks[track].adpcm){
207 } else if (st->codecpar->bits_per_coded_sample == 8) {
209 } else
211
212 return 0;
213}
214
216{
217 AVIOContext *pb = s->pb;
218 unsigned int fourcc_tag;
219 unsigned int size;
220 int header_size;
221 FourxmDemuxContext *fourxm = s->priv_data;
222 unsigned char *header = NULL;
223 int i, ret;
224
225 fourxm->track_count = 0;
226 fourxm->tracks = NULL;
227 fourxm->fps = (AVRational){1,1};
228 fourxm->video_stream_index = -1;
229
230 /* skip the first 3 32-bit numbers */
231 avio_skip(pb, 12);
232
233 /* check for LIST-HEAD */
235 header_size = size - 4;
236 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 header = av_malloc(header_size);
241 if (!header)
242 return AVERROR(ENOMEM);
243 ret = ffio_read_size(pb, header, header_size);
244 if (ret < 0) {
246 return ret;
247 }
248
249 /* take the lazy approach and search for any and all vtrk and strk chunks */
250 for (i = 0; i < header_size - 8; i++) {
251 fourcc_tag = AV_RL32(&header[i]);
252 size = AV_RL32(&header[i + 4]);
253 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);
256 goto fail;
257 }
258
259 if (fourcc_tag == std__TAG) {
260 if (header_size - i < 16) {
261 av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
263 goto fail;
264 }
265 fourxm->fps = av_d2q(av_int2float(AV_RL32(&header[i + 12])), 10000);
266 } else if (fourcc_tag == vtrk_TAG) {
267 if ((ret = parse_vtrk(s, fourxm, header + i, size,
268 header_size - i)) < 0)
269 goto fail;
270
271 i += 8 + size;
272 } else if (fourcc_tag == strk_TAG) {
273 if ((ret = parse_strk(s, fourxm, header + i, size,
274 header_size - i)) < 0)
275 goto fail;
276
277 i += 8 + size;
278 }
279 }
280
281 /* skip over the LIST-MOVI chunk (which is where the stream should be */
283 if (fourcc_tag != MOVI_TAG) {
285 goto fail;
286 }
287
289 /* initialize context members */
290 fourxm->video_pts = -1; /* first frame will push to 0 */
291
292 return 0;
293fail:
295 return ret;
296}
297
299 AVPacket *pkt)
300{
301 FourxmDemuxContext *fourxm = s->priv_data;
302 AVIOContext *pb = s->pb;
303 unsigned int fourcc_tag;
304 unsigned int size;
305 int ret = 0;
306 unsigned int track_number;
307 int packet_read = 0;
308 unsigned char header[8];
310
311 while (!packet_read) {
312 if ((ret = avio_read(s->pb, header, 8)) < 0)
313 return ret;
314 fourcc_tag = AV_RL32(&header[0]);
315 size = AV_RL32(&header[4]);
316 if (avio_feof(pb))
317 return AVERROR_INVALIDDATA;
318 switch (fourcc_tag) {
319 case LIST_TAG:
320 /* this is a good time to bump the video pts */
321 fourxm->video_pts++;
322
323 /* skip the LIST-* tag and move on to the next fourcc */
324 avio_rl32(pb);
325 break;
326
327 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 if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - 8)
336 return AVERROR_INVALIDDATA;
337 if (fourxm->video_stream_index < 0)
338 return AVERROR_INVALIDDATA;
339 if ((ret = av_new_packet(pkt, size + 8)) < 0)
340 return ret;
341 pkt->stream_index = fourxm->video_stream_index;
342 pkt->pts = fourxm->video_pts;
343 pkt->pos = avio_tell(s->pb);
344 memcpy(pkt->data, header, 8);
345 ret = avio_read(s->pb, &pkt->data[8], size);
346
347 if (ret < 0) {
349 } else {
350 packet_read = 1;
351 av_shrink_packet(pkt, ret + 8);
352 }
353 break;
354
355 case snd__TAG:
356 track_number = avio_rl32(pb);
357 avio_skip(pb, 4);
358 size -= 8;
359
360 if (track_number < fourxm->track_count &&
361 fourxm->tracks[track_number].channels > 0) {
362 ret = av_get_packet(s->pb, pkt, size);
363 if (ret < 0)
364 return ret;
365 pkt->stream_index =
366 fourxm->tracks[track_number].stream_index;
367 pkt->pts = fourxm->tracks[track_number].audio_pts;
368 packet_read = 1;
369
370 /* pts accounting */
372 if (fourxm->tracks[track_number].adpcm)
373 audio_frame_count -= 2 * (fourxm->tracks[track_number].channels);
374 audio_frame_count /= fourxm->tracks[track_number].channels;
375 if (fourxm->tracks[track_number].adpcm) {
377 } else
379 (fourxm->tracks[track_number].bits / 8);
380 fourxm->tracks[track_number].audio_pts += audio_frame_count;
381 } else {
382 avio_skip(pb, size);
383 }
384 break;
385
386 default:
387 avio_skip(pb, size);
388 break;
389 }
390 }
391 return ret;
392}
393
395{
396 FourxmDemuxContext *fourxm = s->priv_data;
397
398 av_freep(&fourxm->tracks);
399
400 return 0;
401}
402
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,
412};
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int audio_frame_count
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define fail
Definition test.h:479
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_ADPCM_4XM
Definition codec_id.h:377
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_4XM
Definition codec_id.h:84
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition rational.c:110
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition mem.c:225
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition intfloat.h:40
#define AV_WL32(p, v)
#define AV_RL32(p)
common internal api header.
#define FF_SANE_NB_CHANNELS
Definition internal.h:37
const FFInputFormat ff_fourxm_demuxer
Definition 4xm.c:403
static int fourxm_probe(const AVProbeData *p)
Definition 4xm.c:89
#define snd__TAG
Definition 4xm.c:57
#define LIST_TAG
Definition 4xm.c:41
static int parse_strk(AVFormatContext *s, FourxmDemuxContext *fourxm, uint8_t *buf, int size, int left)
Definition 4xm.c:132
#define HEAD_TAG
Definition 4xm.c:42
static int parse_vtrk(AVFormatContext *s, FourxmDemuxContext *fourxm, uint8_t *buf, int size, int left)
Definition 4xm.c:98
#define ifrm_TAG
Definition 4xm.c:51
#define FOURXMV_TAG
Definition 4xm.c:40
static int fourxm_read_close(AVFormatContext *s)
Definition 4xm.c:394
#define RIFF_TAG
Definition 4xm.c:39
static int fourxm_read_header(AVFormatContext *s)
Definition 4xm.c:215
#define ifr2_TAG
Definition 4xm.c:54
#define vtrk_SIZE
Definition 4xm.c:59
static int fourxm_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition 4xm.c:298
#define strk_TAG
Definition 4xm.c:50
#define vtrk_TAG
Definition 4xm.c:49
#define GET_LIST_HEADER()
Definition 4xm.c:62
#define pfrm_TAG
Definition 4xm.c:52
#define cfrm_TAG
Definition 4xm.c:53
#define cfr2_TAG
Definition 4xm.c:56
#define std__TAG
Definition 4xm.c:47
#define MOVI_TAG
Definition 4xm.c:44
#define strk_SIZE
Definition 4xm.c:60
#define pfr2_TAG
Definition 4xm.c:55
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
static const uint8_t header[24]
Definition sdr2.c:68
int nb_channels
Number of channels in this layout.
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int id
Format-specific stream ID.
Definition avformat.h:778
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t audio_pts
Definition 4xm.c:77
int channels
Definition 4xm.c:74
int bits
Definition 4xm.c:73
int adpcm
Definition 4xm.c:76
int sample_rate
Definition 4xm.c:72
int stream_index
Definition 4xm.c:75
int64_t video_pts
Definition 4xm.c:85
AudioTrack * tracks
Definition 4xm.c:83
AVRational fps
Definition 4xm.c:86
int video_stream_index
Definition 4xm.c:81
int track_count
Definition 4xm.c:82
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
int size