4xm.c 12.9 KB
Newer Older
1 2
/*
 * 4X Technologies .4xm File Demuxer (no muxer)
3
 * Copyright (c) 2003  The FFmpeg project
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 25 26 27 28 29
 * 4X Technologies file demuxer
 * by Mike Melanson (melanson@pcisys.net)
 * for more information on the .4xm file format, visit:
 *   http://www.pcisys.net/~melanson/codecs/
 */

30
#include "libavutil/intreadwrite.h"
31
#include "libavutil/intfloat.h"
32
#include "libavcodec/internal.h"
33
#include "avformat.h"
34
#include "internal.h"
35

Diego Biurrun's avatar
Diego Biurrun committed
36
#define     RIFF_TAG MKTAG('R', 'I', 'F', 'F')
37
#define  FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
Diego Biurrun's avatar
Diego Biurrun committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define     LIST_TAG MKTAG('L', 'I', 'S', 'T')
#define     HEAD_TAG MKTAG('H', 'E', 'A', 'D')
#define     TRK__TAG MKTAG('T', 'R', 'K', '_')
#define     MOVI_TAG MKTAG('M', 'O', 'V', 'I')
#define     VTRK_TAG MKTAG('V', 'T', 'R', 'K')
#define     STRK_TAG MKTAG('S', 'T', 'R', 'K')
#define     std__TAG MKTAG('s', 't', 'd', '_')
#define     name_TAG MKTAG('n', 'a', 'm', 'e')
#define     vtrk_TAG MKTAG('v', 't', 'r', 'k')
#define     strk_TAG MKTAG('s', 't', 'r', 'k')
#define     ifrm_TAG MKTAG('i', 'f', 'r', 'm')
#define     pfrm_TAG MKTAG('p', 'f', 'r', 'm')
#define     cfrm_TAG MKTAG('c', 'f', 'r', 'm')
#define     ifr2_TAG MKTAG('i', 'f', 'r', '2')
#define     pfr2_TAG MKTAG('p', 'f', 'r', '2')
#define     cfr2_TAG MKTAG('c', 'f', 'r', '2')
#define     snd__TAG MKTAG('s', 'n', 'd', '_')
55 56 57 58 59

#define vtrk_SIZE 0x44
#define strk_SIZE 0x28

#define GET_LIST_HEADER() \
60
    fourcc_tag = avio_rl32(pb); \
61
    size       = avio_rl32(pb); \
62 63
    if (fourcc_tag != LIST_TAG) \
        return AVERROR_INVALIDDATA; \
64
    fourcc_tag = avio_rl32(pb);
65 66 67 68 69

typedef struct AudioTrack {
    int sample_rate;
    int bits;
    int channels;
70
    int stream_index;
Michael Niedermayer's avatar
Michael Niedermayer committed
71
    int adpcm;
72
    int64_t audio_pts;
73 74 75
} AudioTrack;

typedef struct FourxmDemuxContext {
76
    int video_stream_index;
77 78
    int track_count;
    AudioTrack *tracks;
79

80
    int64_t video_pts;
81
    AVRational fps;
82 83 84 85
} FourxmDemuxContext;

static int fourxm_probe(AVProbeData *p)
{
86
    if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
87
        (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
88 89 90 91 92
        return 0;

    return AVPROBE_SCORE_MAX;
}

93
static int parse_vtrk(AVFormatContext *s,
94 95
                      FourxmDemuxContext *fourxm, uint8_t *buf, int size,
                      int left)
96 97 98
{
    AVStream *st;
    /* check that there is enough data */
99
    if (size != vtrk_SIZE || left < size + 8) {
100 101 102 103 104 105 106 107
        return AVERROR_INVALIDDATA;
    }

    /* allocate a new AVStream */
    st = avformat_new_stream(s, NULL);
    if (!st)
        return AVERROR(ENOMEM);

108
    avpriv_set_pts_info(st, 60, fourxm->fps.den, fourxm->fps.num);
109 110 111

    fourxm->video_stream_index = st->index;

112 113
    st->codecpar->codec_type     = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id       = AV_CODEC_ID_4XM;
114

115 116
    st->codecpar->extradata      = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
    if (!st->codecpar->extradata)
117
        return AVERROR(ENOMEM);
118 119 120 121
    st->codecpar->extradata_size = 4;
    AV_WL32(st->codecpar->extradata, AV_RL32(buf + 16));
    st->codecpar->width  = AV_RL32(buf + 36);
    st->codecpar->height = AV_RL32(buf + 40);
122 123 124 125 126 127

    return 0;
}


static int parse_strk(AVFormatContext *s,
128 129
                      FourxmDemuxContext *fourxm, uint8_t *buf, int size,
                      int left)
130 131 132 133
{
    AVStream *st;
    int track;
    /* check that there is enough data */
134
    if (size != strk_SIZE || left < size + 8)
135 136 137
        return AVERROR_INVALIDDATA;

    track = AV_RL32(buf + 8);
138 139 140 141
    if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1) {
        av_log(s, AV_LOG_ERROR, "current_track too large\n");
        return AVERROR_INVALIDDATA;
    }
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156
    if (track + 1 > fourxm->track_count) {
        if (av_reallocp_array(&fourxm->tracks, track + 1, sizeof(AudioTrack)))
            return AVERROR(ENOMEM);
        memset(&fourxm->tracks[fourxm->track_count], 0,
               sizeof(AudioTrack) * (track + 1 - fourxm->track_count));
        fourxm->track_count = track + 1;
    }
    fourxm->tracks[track].adpcm       = AV_RL32(buf + 12);
    fourxm->tracks[track].channels    = AV_RL32(buf + 36);
    fourxm->tracks[track].sample_rate = AV_RL32(buf + 40);
    fourxm->tracks[track].bits        = AV_RL32(buf + 44);
    fourxm->tracks[track].audio_pts   = 0;

    if (fourxm->tracks[track].channels    <= 0 ||
157
        fourxm->tracks[track].channels     > FF_SANE_NB_CHANNELS ||
158
        fourxm->tracks[track].sample_rate <= 0 ||
159 160
        fourxm->tracks[track].bits        <= 0 ||
        fourxm->tracks[track].bits         > INT_MAX / FF_SANE_NB_CHANNELS) {
161 162 163
        av_log(s, AV_LOG_ERROR, "audio header invalid\n");
        return AVERROR_INVALIDDATA;
    }
164 165 166 167 168
    if (!fourxm->tracks[track].adpcm && fourxm->tracks[track].bits<8) {
        av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
        return AVERROR_INVALIDDATA;
    }

169 170 171 172 173 174
    if (fourxm->tracks[track].sample_rate > INT64_MAX / fourxm->tracks[track].bits / fourxm->tracks[track].channels) {
        av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * %d * %d\n",
               fourxm->tracks[track].sample_rate, fourxm->tracks[track].bits, fourxm->tracks[track].channels);
        return AVERROR_INVALIDDATA;
    }

175 176 177 178 179 180 181 182 183 184
    /* allocate a new AVStream */
    st = avformat_new_stream(s, NULL);
    if (!st)
        return AVERROR(ENOMEM);

    st->id = track;
    avpriv_set_pts_info(st, 60, 1, fourxm->tracks[track].sample_rate);

    fourxm->tracks[track].stream_index = st->index;

185 186 187 188 189
    st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
    st->codecpar->codec_tag             = 0;
    st->codecpar->channels              = fourxm->tracks[track].channels;
    st->codecpar->sample_rate           = fourxm->tracks[track].sample_rate;
    st->codecpar->bits_per_coded_sample = fourxm->tracks[track].bits;
190
    st->codecpar->bit_rate              = (int64_t)st->codecpar->channels *
191 192 193 194
                                          st->codecpar->sample_rate *
                                          st->codecpar->bits_per_coded_sample;
    st->codecpar->block_align           = st->codecpar->channels *
                                          st->codecpar->bits_per_coded_sample;
195 196

    if (fourxm->tracks[track].adpcm){
197 198 199
        st->codecpar->codec_id = AV_CODEC_ID_ADPCM_4XM;
    } else if (st->codecpar->bits_per_coded_sample == 8) {
        st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
200
    } else
201
        st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
202 203 204 205

    return 0;
}

206
static int fourxm_read_header(AVFormatContext *s)
207
{
208
    AVIOContext *pb = s->pb;
209 210 211
    unsigned int fourcc_tag;
    unsigned int size;
    int header_size;
212
    FourxmDemuxContext *fourxm = s->priv_data;
213
    unsigned char *header;
214
    int i, ret;
215 216

    fourxm->track_count = 0;
217
    fourxm->tracks      = NULL;
218
    fourxm->fps         = (AVRational){1,1};
219 220

    /* skip the first 3 32-bit numbers */
221
    avio_skip(pb, 12);
222 223 224 225

    /* check for LIST-HEAD */
    GET_LIST_HEADER();
    header_size = size - 4;
226
    if (fourcc_tag != HEAD_TAG || header_size < 0)
227 228 229 230 231
        return AVERROR_INVALIDDATA;

    /* allocate space for the header and load the whole thing */
    header = av_malloc(header_size);
    if (!header)
232
        return AVERROR(ENOMEM);
233
    if (avio_read(pb, header, header_size) != header_size) {
234
        av_free(header);
235
        return AVERROR(EIO);
236
    }
237 238 239

    /* take the lazy approach and search for any and all vtrk and strk chunks */
    for (i = 0; i < header_size - 8; i++) {
240
        fourcc_tag = AV_RL32(&header[i]);
241
        size       = AV_RL32(&header[i + 4]);
242 243 244 245
        if (size > header_size - i - 8 && (fourcc_tag == vtrk_TAG || fourcc_tag == strk_TAG)) {
            av_log(s, AV_LOG_ERROR, "chunk larger than array %d>%d\n", size, header_size - i - 8);
            return AVERROR_INVALIDDATA;
        }
246

247
        if (fourcc_tag == std__TAG) {
248
            if (header_size - i < 16) {
249
                av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
250 251
                ret = AVERROR_INVALIDDATA;
                goto fail;
252
            }
253
            fourxm->fps = av_d2q(av_int2float(AV_RL32(&header[i + 12])), 10000);
254
        } else if (fourcc_tag == vtrk_TAG) {
255 256
            if ((ret = parse_vtrk(s, fourxm, header + i, size,
                                  header_size - i)) < 0)
257
                goto fail;
258

259
            i += 8 + size;
260
        } else if (fourcc_tag == strk_TAG) {
261 262
            if ((ret = parse_strk(s, fourxm, header + i, size,
                                  header_size - i)) < 0)
263
                goto fail;
264

265
            i += 8 + size;
266 267 268 269 270
        }
    }

    /* skip over the LIST-MOVI chunk (which is where the stream should be */
    GET_LIST_HEADER();
271 272
    if (fourcc_tag != MOVI_TAG) {
        ret = AVERROR_INVALIDDATA;
273 274
        goto fail;
    }
275

276
    av_free(header);
277
    /* initialize context members */
278
    fourxm->video_pts = -1;  /* first frame will push to 0 */
279

280
    return 0;
281 282 283 284
fail:
    av_freep(&fourxm->tracks);
    av_free(header);
    return ret;
285 286 287
}

static int fourxm_read_packet(AVFormatContext *s,
288
                              AVPacket *pkt)
289 290
{
    FourxmDemuxContext *fourxm = s->priv_data;
291
    AVIOContext *pb            = s->pb;
292
    unsigned int fourcc_tag;
Mans Rullgard's avatar
Mans Rullgard committed
293
    unsigned int size;
294
    int ret = 0;
295
    unsigned int track_number;
296
    int packet_read = 0;
297
    unsigned char header[8];
298
    int audio_frame_count;
299 300

    while (!packet_read) {
301
        if ((ret = avio_read(s->pb, header, 8)) < 0)
302
            return ret;
303
        fourcc_tag = AV_RL32(&header[0]);
304
        size       = AV_RL32(&header[4]);
305
        if (avio_feof(pb))
306
            return AVERROR(EIO);
307
        switch (fourcc_tag) {
308
        case LIST_TAG:
309
            /* this is a good time to bump the video pts */
310
            fourxm->video_pts++;
311

312
            /* skip the LIST-* tag and move on to the next fourcc */
313
            avio_rl32(pb);
314 315
            break;

316 317
        case ifrm_TAG:
        case pfrm_TAG:
318 319 320 321
        case cfrm_TAG:
        case ifr2_TAG:
        case pfr2_TAG:
        case cfr2_TAG:
322 323
            /* allocate 8 more bytes than 'size' to account for fourcc
             * and size */
324
            if (size + 8 < size || av_new_packet(pkt, size + 8))
325
                return AVERROR(EIO);
326
            pkt->stream_index = fourxm->video_stream_index;
327 328
            pkt->pts          = fourxm->video_pts;
            pkt->pos          = avio_tell(s->pb);
329
            memcpy(pkt->data, header, 8);
330
            ret = avio_read(s->pb, &pkt->data[8], size);
331

332
            if (ret < 0) {
333
                av_packet_unref(pkt);
334
            } else {
335
                packet_read = 1;
336 337
                av_shrink_packet(pkt, ret + 8);
            }
338
            break;
339

340
        case snd__TAG:
341
            track_number = avio_rl32(pb);
Mans Rullgard's avatar
Mans Rullgard committed
342
            avio_skip(pb, 4);
343
            size -= 8;
Michael Niedermayer's avatar
Michael Niedermayer committed
344

345 346 347 348
            if (track_number < fourxm->track_count &&
                fourxm->tracks[track_number].channels > 0) {
                ret = av_get_packet(s->pb, pkt, size);
                if (ret < 0)
349
                    return AVERROR(EIO);
350
                pkt->stream_index =
351
                    fourxm->tracks[track_number].stream_index;
352
                pkt->pts    = fourxm->tracks[track_number].audio_pts;
Michael Niedermayer's avatar
Michael Niedermayer committed
353
                packet_read = 1;
354

355 356
                /* pts accounting */
                audio_frame_count = size;
357
                if (fourxm->tracks[track_number].adpcm)
358 359 360
                    audio_frame_count -= 2 * (fourxm->tracks[track_number].channels);
                audio_frame_count /= fourxm->tracks[track_number].channels;
                if (fourxm->tracks[track_number].adpcm) {
361
                    audio_frame_count *= 2;
362
                } else
363
                    audio_frame_count /=
364
                        (fourxm->tracks[track_number].bits / 8);
365
                fourxm->tracks[track_number].audio_pts += audio_frame_count;
366
            } else {
367
                avio_skip(pb, size);
368 369 370 371
            }
            break;

        default:
372
            avio_skip(pb, size);
373 374 375 376 377 378 379 380
            break;
        }
    }
    return ret;
}

static int fourxm_read_close(AVFormatContext *s)
{
381
    FourxmDemuxContext *fourxm = s->priv_data;
382

383
    av_freep(&fourxm->tracks);
384 385 386 387

    return 0;
}

388
AVInputFormat ff_fourxm_demuxer = {
389
    .name           = "4xm",
390
    .long_name      = NULL_IF_CONFIG_SMALL("4X Technologies"),
391 392 393 394 395
    .priv_data_size = sizeof(FourxmDemuxContext),
    .read_probe     = fourxm_probe,
    .read_header    = fourxm_read_header,
    .read_packet    = fourxm_read_packet,
    .read_close     = fourxm_read_close,
396
};