libavformat/4xm.c
cef4ba9e
 /*
  * 4X Technologies .4xm File Demuxer (no muxer)
  * Copyright (c) 2003  The ffmpeg Project
  *
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); \
     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 {
     int width;
     int height;
8e7284ba
     int video_stream_index;
cef4ba9e
     int track_count;
     AudioTrack *tracks;
8e7284ba
 
d66dae57
     int64_t video_pts;
11498da3
     float 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;
 }
 
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
     AVStream *st;
 
     fourxm->track_count = 0;
     fourxm->tracks = NULL;
11498da3
     fourxm->fps = 1.0;
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);
e63a3628
     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]);
         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) {
3383a53e
             fourxm->fps = av_int2float(AV_RL32(&header[i + 12]));
d66dae57
         } else if (fourcc_tag == vtrk_TAG) {
cef4ba9e
             /* check that there is enough data */
             if (size != vtrk_SIZE) {
68e1794e
                 ret= AVERROR_INVALIDDATA;
                 goto fail;
cef4ba9e
             }
4f989885
             fourxm->width  = AV_RL32(&header[i + 36]);
fead30d4
             fourxm->height = AV_RL32(&header[i + 40]);
8e7284ba
 
             /* allocate a new AVStream */
3b3bbdd3
             st = avformat_new_stream(s, NULL);
cc988dd7
             if (!st){
68e1794e
                 ret= AVERROR(ENOMEM);
                 goto fail;
cc988dd7
             }
c3f9ebf7
             avpriv_set_pts_info(st, 60, 1, fourxm->fps);
8e7284ba
 
             fourxm->video_stream_index = st->index;
 
72415b2a
             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c
             st->codec->codec_id = CODEC_ID_4XM;
b6f508bb
             st->codec->extradata_size = 4;
             st->codec->extradata = av_malloc(4);
             AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
4f989885
             st->codec->width  = fourxm->width;
01f4895c
             st->codec->height = fourxm->height;
8e7284ba
 
a985a940
             i += 8 + size;
cef4ba9e
         } else if (fourcc_tag == strk_TAG) {
a10f1cbb
             int current_track;
cef4ba9e
             /* check that there is enough data */
             if (size != strk_SIZE) {
68e1794e
                 ret= AVERROR_INVALIDDATA;
                 goto fail;
cef4ba9e
             }
fead30d4
             current_track = AV_RL32(&header[i + 8]);
0838cfdc
             if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
                 av_log(s, AV_LOG_ERROR, "current_track too large\n");
e8a06b14
                 ret = AVERROR_INVALIDDATA;
0838cfdc
                 goto fail;
             }
cef4ba9e
             if (current_track + 1 > fourxm->track_count) {
0cc44fac
                 fourxm->tracks = av_realloc_f(fourxm->tracks,
                                               sizeof(AudioTrack),
a1876e00
                                               current_track + 1);
cef4ba9e
                 if (!fourxm->tracks) {
79964745
                     ret = AVERROR(ENOMEM);
68e1794e
                     goto fail;
cef4ba9e
                 }
a1876e00
                 memset(&fourxm->tracks[fourxm->track_count], 0,
                        sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
                 fourxm->track_count = current_track + 1;
cef4ba9e
             }
4f989885
             fourxm->tracks[current_track].adpcm       = AV_RL32(&header[i + 12]);
             fourxm->tracks[current_track].channels    = AV_RL32(&header[i + 36]);
fead30d4
             fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
4f989885
             fourxm->tracks[current_track].bits        = AV_RL32(&header[i + 44]);
9913860b
             fourxm->tracks[current_track].audio_pts   = 0;
8bb7d97b
             if(   fourxm->tracks[current_track].channels    <= 0
                || fourxm->tracks[current_track].sample_rate <= 0
                || fourxm->tracks[current_track].bits        <  0){
                 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
e8a06b14
                 ret = AVERROR_INVALIDDATA;
8bb7d97b
                 goto fail;
             }
cef4ba9e
             i += 8 + size;
8e7284ba
 
             /* allocate a new AVStream */
84ad31ff
             st = avformat_new_stream(s, NULL);
cc988dd7
             if (!st){
68e1794e
                 ret= AVERROR(ENOMEM);
                 goto fail;
cc988dd7
             }
8e7284ba
 
84ad31ff
             st->id = current_track;
c3f9ebf7
             avpriv_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
9ee91c2f
 
8e7284ba
             fourxm->tracks[current_track].stream_index = st->index;
 
72415b2a
             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
899af1a2
             st->codec->codec_tag = 0;
4f989885
             st->codec->channels              = fourxm->tracks[current_track].channels;
             st->codec->sample_rate           = fourxm->tracks[current_track].sample_rate;
dd1c8f3e
             st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
4f989885
             st->codec->bit_rate              = st->codec->channels * st->codec->sample_rate *
dd1c8f3e
                 st->codec->bits_per_coded_sample;
             st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
0d29b7d5
             if (fourxm->tracks[current_track].adpcm){
01f4895c
                 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
0d29b7d5
             }else if (st->codec->bits_per_coded_sample == 8){
01f4895c
                 st->codec->codec_id = CODEC_ID_PCM_U8;
0d29b7d5
             }else
01f4895c
                 st->codec->codec_id = CODEC_ID_PCM_S16LE;
cef4ba9e
         }
     }
 
     /* skip over the LIST-MOVI chunk (which is where the stream should be */
     GET_LIST_HEADER();
68e1794e
     if (fourcc_tag != MOVI_TAG){
         ret= AVERROR_INVALIDDATA;
         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;
471fe57e
     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]);
         size = AV_RL32(&header[4]);
cef4ba9e
         if (url_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 */
caf5fb95
             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;
d66dae57
             pkt->pts = fourxm->video_pts;
384c9c2f
             pkt->pos = avio_tell(s->pb);
8e7284ba
             memcpy(pkt->data, header, 8);
e63a3628
             ret = avio_read(s->pb, &pkt->data[8], size);
8e7284ba
 
0d29b7d5
             if (ret < 0){
8e7284ba
                 av_free_packet(pkt);
0d29b7d5
             }else
8e7284ba
                 packet_read = 1;
4a106616
             break;
8e7284ba
 
cef4ba9e
         case snd__TAG:
e63a3628
             track_number = avio_rl32(pb);
e65ab9d9
             avio_skip(pb, 4);
4b465299
             size-=8;
 
8bb7d97b
             if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
899681cd
                 ret= av_get_packet(s->pb, pkt, size);
2692067a
                 if(ret<0)
6f3e0b21
                     return AVERROR(EIO);
115329f1
                 pkt->stream_index =
9913860b
                     fourxm->tracks[track_number].stream_index;
                 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)
115329f1
                     audio_frame_count -=
9913860b
                         2 * (fourxm->tracks[track_number].channels);
d66dae57
                 audio_frame_count /=
9913860b
                       fourxm->tracks[track_number].channels;
0d29b7d5
                 if (fourxm->tracks[track_number].adpcm){
d66dae57
                     audio_frame_count *= 2;
0d29b7d5
                 }else
d66dae57
                     audio_frame_count /=
9913860b
                     (fourxm->tracks[track_number].bits / 8);
                 fourxm->tracks[track_number].audio_pts += audio_frame_count;
8e7284ba
 
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",
     .long_name      = NULL_IF_CONFIG_SMALL("4X Technologies format"),
     .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
 };