libavformat/4xm.c
cef4ba9e
 /*
  * 4X Technologies .4xm File Demuxer (no muxer)
6dfa70f2
  * Copyright (c) 2003  The FFmpeg Project
cef4ba9e
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
cef4ba9e
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
cef4ba9e
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
cef4ba9e
  * 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
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cef4ba9e
  */
 
 /**
ba87f080
  * @file
cef4ba9e
  * 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/
  */
 
6a5d31ac
 #include "libavutil/intreadwrite.h"
3383a53e
 #include "libavutil/intfloat.h"
cef4ba9e
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
cef4ba9e
 
470bce2b
 #define     RIFF_TAG MKTAG('R', 'I', 'F', 'F')
c54286ab
 #define  FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
470bce2b
 #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', '_')
cef4ba9e
 
 #define vtrk_SIZE 0x44
 #define strk_SIZE 0x28
 
 #define GET_LIST_HEADER() \
e63a3628
     fourcc_tag = avio_rl32(pb); \
e6496ea7
     size       = avio_rl32(pb); \
cef4ba9e
     if (fourcc_tag != LIST_TAG) \
         return AVERROR_INVALIDDATA; \
e63a3628
     fourcc_tag = avio_rl32(pb);
cef4ba9e
 
 typedef struct AudioTrack {
     int sample_rate;
     int bits;
     int channels;
8e7284ba
     int stream_index;
4b465299
     int adpcm;
9913860b
     int64_t audio_pts;
cef4ba9e
 } AudioTrack;
 
 typedef struct FourxmDemuxContext {
8e7284ba
     int video_stream_index;
cef4ba9e
     int track_count;
     AudioTrack *tracks;
8e7284ba
 
d66dae57
     int64_t video_pts;
3bf57acb
     AVRational fps;
cef4ba9e
 } FourxmDemuxContext;
 
 static int fourxm_probe(AVProbeData *p)
 {
fead30d4
     if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
c54286ab
         (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
cef4ba9e
         return 0;
 
     return AVPROBE_SCORE_MAX;
 }
 
e7a44f87
 static int parse_vtrk(AVFormatContext *s,
42d73f7f
                       FourxmDemuxContext *fourxm, uint8_t *buf, int size,
                       int left)
e7a44f87
 {
     AVStream *st;
     /* check that there is enough data */
42d73f7f
     if (size != vtrk_SIZE || left < size + 8) {
e7a44f87
         return AVERROR_INVALIDDATA;
     }
 
     /* allocate a new AVStream */
     st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
 
3bf57acb
     avpriv_set_pts_info(st, 60, fourxm->fps.den, fourxm->fps.num);
e7a44f87
 
     fourxm->video_stream_index = st->index;
 
     st->codec->codec_type     = AVMEDIA_TYPE_VIDEO;
     st->codec->codec_id       = AV_CODEC_ID_4XM;
d256ed78
 
059a9348
     st->codec->extradata      = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
d256ed78
     if (!st->codec->extradata)
         return AVERROR(ENOMEM);
e7a44f87
     st->codec->extradata_size = 4;
     AV_WL32(st->codec->extradata, AV_RL32(buf + 16));
     st->codec->width  = AV_RL32(buf + 36);
     st->codec->height = AV_RL32(buf + 40);
 
     return 0;
 }
 
 
 static int parse_strk(AVFormatContext *s,
42d73f7f
                       FourxmDemuxContext *fourxm, uint8_t *buf, int size,
                       int left)
e7a44f87
 {
     AVStream *st;
     int track;
     /* check that there is enough data */
42d73f7f
     if (size != strk_SIZE || left < size + 8)
e7a44f87
         return AVERROR_INVALIDDATA;
 
     track = AV_RL32(buf + 8);
60657ee3
     if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1) {
         av_log(s, AV_LOG_ERROR, "current_track too large\n");
         return AVERROR_INVALIDDATA;
     }
9411e9ca
 
e7a44f87
     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 ||
         fourxm->tracks[track].sample_rate <= 0 ||
a7c1689d
         fourxm->tracks[track].bits        <= 0) {
e7a44f87
         av_log(s, AV_LOG_ERROR, "audio header invalid\n");
         return AVERROR_INVALIDDATA;
     }
60657ee3
     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;
     }
 
e7a44f87
     /* 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;
 
     st->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
     st->codec->codec_tag             = 0;
     st->codec->channels              = fourxm->tracks[track].channels;
     st->codec->sample_rate           = fourxm->tracks[track].sample_rate;
     st->codec->bits_per_coded_sample = fourxm->tracks[track].bits;
     st->codec->bit_rate              = st->codec->channels *
                                        st->codec->sample_rate *
                                        st->codec->bits_per_coded_sample;
     st->codec->block_align           = st->codec->channels *
                                        st->codec->bits_per_coded_sample;
 
     if (fourxm->tracks[track].adpcm){
         st->codec->codec_id = AV_CODEC_ID_ADPCM_4XM;
     } else if (st->codec->bits_per_coded_sample == 8) {
         st->codec->codec_id = AV_CODEC_ID_PCM_U8;
     } else
         st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
 
     return 0;
 }
 
6e9651d1
 static int fourxm_read_header(AVFormatContext *s)
cef4ba9e
 {
471fe57e
     AVIOContext *pb = s->pb;
cef4ba9e
     unsigned int fourcc_tag;
     unsigned int size;
     int header_size;
e4141433
     FourxmDemuxContext *fourxm = s->priv_data;
cef4ba9e
     unsigned char *header;
68e1794e
     int i, ret;
cef4ba9e
 
     fourxm->track_count = 0;
e6496ea7
     fourxm->tracks      = NULL;
3bf57acb
     fourxm->fps         = (AVRational){1,1};
cef4ba9e
 
     /* skip the first 3 32-bit numbers */
45a8a02a
     avio_skip(pb, 12);
cef4ba9e
 
     /* check for LIST-HEAD */
     GET_LIST_HEADER();
     header_size = size - 4;
59afda9f
     if (fourcc_tag != HEAD_TAG || header_size < 0)
cef4ba9e
         return AVERROR_INVALIDDATA;
 
     /* allocate space for the header and load the whole thing */
     header = av_malloc(header_size);
     if (!header)
769e10f0
         return AVERROR(ENOMEM);
e6496ea7
     if (avio_read(pb, header, header_size) != header_size) {
cc988dd7
         av_free(header);
6f3e0b21
         return AVERROR(EIO);
cc988dd7
     }
cef4ba9e
 
     /* take the lazy approach and search for any and all vtrk and strk chunks */
     for (i = 0; i < header_size - 8; i++) {
fead30d4
         fourcc_tag = AV_RL32(&header[i]);
e6496ea7
         size       = AV_RL32(&header[i + 4]);
474e31c9
         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;
         }
cef4ba9e
 
d66dae57
         if (fourcc_tag == std__TAG) {
42d73f7f
             if (header_size - i < 16) {
b90e795f
                 av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
42d73f7f
                 ret = AVERROR_INVALIDDATA;
                 goto fail;
b90e795f
             }
3bf57acb
             fourxm->fps = av_d2q(av_int2float(AV_RL32(&header[i + 12])), 10000);
d66dae57
         } else if (fourcc_tag == vtrk_TAG) {
42d73f7f
             if ((ret = parse_vtrk(s, fourxm, header + i, size,
                                   header_size - i)) < 0)
68e1794e
                 goto fail;
8e7284ba
 
a985a940
             i += 8 + size;
cef4ba9e
         } else if (fourcc_tag == strk_TAG) {
42d73f7f
             if ((ret = parse_strk(s, fourxm, header + i, size,
                                   header_size - i)) < 0)
1b8741a6
                 goto fail;
8e7284ba
 
e7a44f87
             i += 8 + size;
cef4ba9e
         }
     }
 
     /* skip over the LIST-MOVI chunk (which is where the stream should be */
     GET_LIST_HEADER();
e6496ea7
     if (fourcc_tag != MOVI_TAG) {
         ret = AVERROR_INVALIDDATA;
68e1794e
         goto fail;
     }
cef4ba9e
 
68e1794e
     av_free(header);
8e7284ba
     /* initialize context members */
caf5fb95
     fourxm->video_pts = -1;  /* first frame will push to 0 */
8e7284ba
 
cef4ba9e
     return 0;
68e1794e
 fail:
     av_freep(&fourxm->tracks);
     av_free(header);
     return ret;
cef4ba9e
 }
 
 static int fourxm_read_packet(AVFormatContext *s,
8e7284ba
                               AVPacket *pkt)
cef4ba9e
 {
     FourxmDemuxContext *fourxm = s->priv_data;
e6496ea7
     AVIOContext *pb            = s->pb;
cef4ba9e
     unsigned int fourcc_tag;
e65ab9d9
     unsigned int size;
cef4ba9e
     int ret = 0;
9913860b
     unsigned int track_number;
cef4ba9e
     int packet_read = 0;
8e7284ba
     unsigned char header[8];
d66dae57
     int audio_frame_count;
cef4ba9e
 
     while (!packet_read) {
e63a3628
         if ((ret = avio_read(s->pb, header, 8)) < 0)
8e7284ba
             return ret;
fead30d4
         fourcc_tag = AV_RL32(&header[0]);
e6496ea7
         size       = AV_RL32(&header[4]);
d34ec64a
         if (avio_feof(pb))
6f3e0b21
             return AVERROR(EIO);
cef4ba9e
         switch (fourcc_tag) {
73a19b2d
         case LIST_TAG:
d66dae57
             /* this is a good time to bump the video pts */
e6496ea7
             fourxm->video_pts++;
d66dae57
 
73a19b2d
             /* skip the LIST-* tag and move on to the next fourcc */
e63a3628
             avio_rl32(pb);
73a19b2d
             break;
 
cef4ba9e
         case ifrm_TAG:
         case pfrm_TAG:
07870f85
         case cfrm_TAG:
         case ifr2_TAG:
         case pfr2_TAG:
         case cfr2_TAG:
8e7284ba
             /* allocate 8 more bytes than 'size' to account for fourcc
              * and size */
0ecca7a4
             if (size + 8 < size || av_new_packet(pkt, size + 8))
6f3e0b21
                 return AVERROR(EIO);
8e7284ba
             pkt->stream_index = fourxm->video_stream_index;
e6496ea7
             pkt->pts          = fourxm->video_pts;
             pkt->pos          = avio_tell(s->pb);
8e7284ba
             memcpy(pkt->data, header, 8);
e63a3628
             ret = avio_read(s->pb, &pkt->data[8], size);
8e7284ba
 
e6496ea7
             if (ret < 0) {
8e7284ba
                 av_free_packet(pkt);
9b195dd5
             } else {
8e7284ba
                 packet_read = 1;
9b195dd5
                 av_shrink_packet(pkt, ret + 8);
             }
4a106616
             break;
8e7284ba
 
cef4ba9e
         case snd__TAG:
e63a3628
             track_number = avio_rl32(pb);
e65ab9d9
             avio_skip(pb, 4);
e6496ea7
             size -= 8;
4b465299
 
e6496ea7
             if (track_number < fourxm->track_count &&
                 fourxm->tracks[track_number].channels > 0) {
                 ret = av_get_packet(s->pb, pkt, size);
                 if (ret < 0)
6f3e0b21
                     return AVERROR(EIO);
115329f1
                 pkt->stream_index =
9913860b
                     fourxm->tracks[track_number].stream_index;
e6496ea7
                 pkt->pts    = fourxm->tracks[track_number].audio_pts;
2692067a
                 packet_read = 1;
8e7284ba
 
d66dae57
                 /* pts accounting */
                 audio_frame_count = size;
9913860b
                 if (fourxm->tracks[track_number].adpcm)
e6496ea7
                     audio_frame_count -= 2 * (fourxm->tracks[track_number].channels);
                 audio_frame_count /= fourxm->tracks[track_number].channels;
                 if (fourxm->tracks[track_number].adpcm) {
d66dae57
                     audio_frame_count *= 2;
e6496ea7
                 } else
d66dae57
                     audio_frame_count /=
e6496ea7
                         (fourxm->tracks[track_number].bits / 8);
9913860b
                 fourxm->tracks[track_number].audio_pts += audio_frame_count;
cef4ba9e
             } else {
45a8a02a
                 avio_skip(pb, size);
cef4ba9e
             }
             break;
 
         default:
45a8a02a
             avio_skip(pb, size);
cef4ba9e
             break;
         }
     }
     return ret;
 }
 
 static int fourxm_read_close(AVFormatContext *s)
 {
e4141433
     FourxmDemuxContext *fourxm = s->priv_data;
cef4ba9e
 
be195ed1
     av_freep(&fourxm->tracks);
cef4ba9e
 
     return 0;
 }
 
66355be3
 AVInputFormat ff_fourxm_demuxer = {
dfc2c4d9
     .name           = "4xm",
6774247a
     .long_name      = NULL_IF_CONFIG_SMALL("4X Technologies"),
dfc2c4d9
     .priv_data_size = sizeof(FourxmDemuxContext),
     .read_probe     = fourxm_probe,
     .read_header    = fourxm_read_header,
     .read_packet    = fourxm_read_packet,
     .read_close     = fourxm_read_close,
cef4ba9e
 };