00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/intfloat.h"
00032 #include "avformat.h"
00033 #include "internal.h"
00034
00035 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
00036 #define FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
00037 #define LIST_TAG MKTAG('L', 'I', 'S', 'T')
00038 #define HEAD_TAG MKTAG('H', 'E', 'A', 'D')
00039 #define TRK__TAG MKTAG('T', 'R', 'K', '_')
00040 #define MOVI_TAG MKTAG('M', 'O', 'V', 'I')
00041 #define VTRK_TAG MKTAG('V', 'T', 'R', 'K')
00042 #define STRK_TAG MKTAG('S', 'T', 'R', 'K')
00043 #define std__TAG MKTAG('s', 't', 'd', '_')
00044 #define name_TAG MKTAG('n', 'a', 'm', 'e')
00045 #define vtrk_TAG MKTAG('v', 't', 'r', 'k')
00046 #define strk_TAG MKTAG('s', 't', 'r', 'k')
00047 #define ifrm_TAG MKTAG('i', 'f', 'r', 'm')
00048 #define pfrm_TAG MKTAG('p', 'f', 'r', 'm')
00049 #define cfrm_TAG MKTAG('c', 'f', 'r', 'm')
00050 #define ifr2_TAG MKTAG('i', 'f', 'r', '2')
00051 #define pfr2_TAG MKTAG('p', 'f', 'r', '2')
00052 #define cfr2_TAG MKTAG('c', 'f', 'r', '2')
00053 #define snd__TAG MKTAG('s', 'n', 'd', '_')
00054
00055 #define vtrk_SIZE 0x44
00056 #define strk_SIZE 0x28
00057
00058 #define GET_LIST_HEADER() \
00059 fourcc_tag = avio_rl32(pb); \
00060 size = avio_rl32(pb); \
00061 if (fourcc_tag != LIST_TAG) \
00062 return AVERROR_INVALIDDATA; \
00063 fourcc_tag = avio_rl32(pb);
00064
00065 typedef struct AudioTrack {
00066 int sample_rate;
00067 int bits;
00068 int channels;
00069 int stream_index;
00070 int adpcm;
00071 int64_t audio_pts;
00072 } AudioTrack;
00073
00074 typedef struct FourxmDemuxContext {
00075 int width;
00076 int height;
00077 int video_stream_index;
00078 int track_count;
00079 AudioTrack *tracks;
00080
00081 int64_t video_pts;
00082 float fps;
00083 } FourxmDemuxContext;
00084
00085 static int fourxm_probe(AVProbeData *p)
00086 {
00087 if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
00088 (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
00089 return 0;
00090
00091 return AVPROBE_SCORE_MAX;
00092 }
00093
00094 static int fourxm_read_header(AVFormatContext *s,
00095 AVFormatParameters *ap)
00096 {
00097 AVIOContext *pb = s->pb;
00098 unsigned int fourcc_tag;
00099 unsigned int size;
00100 int header_size;
00101 FourxmDemuxContext *fourxm = s->priv_data;
00102 unsigned char *header;
00103 int i, ret;
00104 AVStream *st;
00105
00106 fourxm->track_count = 0;
00107 fourxm->tracks = NULL;
00108 fourxm->fps = 1.0;
00109
00110
00111 avio_skip(pb, 12);
00112
00113
00114 GET_LIST_HEADER();
00115 header_size = size - 4;
00116 if (fourcc_tag != HEAD_TAG || header_size < 0)
00117 return AVERROR_INVALIDDATA;
00118
00119
00120 header = av_malloc(header_size);
00121 if (!header)
00122 return AVERROR(ENOMEM);
00123 if (avio_read(pb, header, header_size) != header_size){
00124 av_free(header);
00125 return AVERROR(EIO);
00126 }
00127
00128
00129 for (i = 0; i < header_size - 8; i++) {
00130 fourcc_tag = AV_RL32(&header[i]);
00131 size = AV_RL32(&header[i + 4]);
00132
00133 if (fourcc_tag == std__TAG) {
00134 fourxm->fps = av_int2float(AV_RL32(&header[i + 12]));
00135 } else if (fourcc_tag == vtrk_TAG) {
00136
00137 if (size != vtrk_SIZE) {
00138 ret= AVERROR_INVALIDDATA;
00139 goto fail;
00140 }
00141 fourxm->width = AV_RL32(&header[i + 36]);
00142 fourxm->height = AV_RL32(&header[i + 40]);
00143
00144
00145 st = avformat_new_stream(s, NULL);
00146 if (!st){
00147 ret= AVERROR(ENOMEM);
00148 goto fail;
00149 }
00150 avpriv_set_pts_info(st, 60, 1, fourxm->fps);
00151
00152 fourxm->video_stream_index = st->index;
00153
00154 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00155 st->codec->codec_id = CODEC_ID_4XM;
00156 st->codec->extradata_size = 4;
00157 st->codec->extradata = av_malloc(4);
00158 AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
00159 st->codec->width = fourxm->width;
00160 st->codec->height = fourxm->height;
00161
00162 i += 8 + size;
00163 } else if (fourcc_tag == strk_TAG) {
00164 int current_track;
00165
00166 if (size != strk_SIZE) {
00167 ret= AVERROR_INVALIDDATA;
00168 goto fail;
00169 }
00170 current_track = AV_RL32(&header[i + 8]);
00171 if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
00172 av_log(s, AV_LOG_ERROR, "current_track too large\n");
00173 ret= -1;
00174 goto fail;
00175 }
00176 if (current_track + 1 > fourxm->track_count) {
00177 fourxm->tracks = av_realloc(fourxm->tracks,
00178 (current_track + 1) * sizeof(AudioTrack));
00179 if (!fourxm->tracks) {
00180 ret = AVERROR(ENOMEM);
00181 goto fail;
00182 }
00183 memset(&fourxm->tracks[fourxm->track_count], 0,
00184 sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
00185 fourxm->track_count = current_track + 1;
00186 }
00187 fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]);
00188 fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]);
00189 fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
00190 fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]);
00191 fourxm->tracks[current_track].audio_pts = 0;
00192 if( fourxm->tracks[current_track].channels <= 0
00193 || fourxm->tracks[current_track].sample_rate <= 0
00194 || fourxm->tracks[current_track].bits < 0){
00195 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
00196 ret= -1;
00197 goto fail;
00198 }
00199 i += 8 + size;
00200
00201
00202 st = avformat_new_stream(s, NULL);
00203 if (!st){
00204 ret= AVERROR(ENOMEM);
00205 goto fail;
00206 }
00207
00208 st->id = current_track;
00209 avpriv_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00210
00211 fourxm->tracks[current_track].stream_index = st->index;
00212
00213 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00214 st->codec->codec_tag = 0;
00215 st->codec->channels = fourxm->tracks[current_track].channels;
00216 st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
00217 st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
00218 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00219 st->codec->bits_per_coded_sample;
00220 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00221 if (fourxm->tracks[current_track].adpcm){
00222 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
00223 }else if (st->codec->bits_per_coded_sample == 8){
00224 st->codec->codec_id = CODEC_ID_PCM_U8;
00225 }else
00226 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00227 }
00228 }
00229
00230
00231 GET_LIST_HEADER();
00232 if (fourcc_tag != MOVI_TAG){
00233 ret= AVERROR_INVALIDDATA;
00234 goto fail;
00235 }
00236
00237 av_free(header);
00238
00239 fourxm->video_pts = -1;
00240
00241 return 0;
00242 fail:
00243 av_freep(&fourxm->tracks);
00244 av_free(header);
00245 return ret;
00246 }
00247
00248 static int fourxm_read_packet(AVFormatContext *s,
00249 AVPacket *pkt)
00250 {
00251 FourxmDemuxContext *fourxm = s->priv_data;
00252 AVIOContext *pb = s->pb;
00253 unsigned int fourcc_tag;
00254 unsigned int size;
00255 int ret = 0;
00256 unsigned int track_number;
00257 int packet_read = 0;
00258 unsigned char header[8];
00259 int audio_frame_count;
00260
00261 while (!packet_read) {
00262
00263 if ((ret = avio_read(s->pb, header, 8)) < 0)
00264 return ret;
00265 fourcc_tag = AV_RL32(&header[0]);
00266 size = AV_RL32(&header[4]);
00267 if (pb->eof_reached)
00268 return AVERROR(EIO);
00269 switch (fourcc_tag) {
00270
00271 case LIST_TAG:
00272
00273 fourxm->video_pts ++;
00274
00275
00276 avio_rl32(pb);
00277 break;
00278
00279 case ifrm_TAG:
00280 case pfrm_TAG:
00281 case cfrm_TAG:
00282 case ifr2_TAG:
00283 case pfr2_TAG:
00284 case cfr2_TAG:
00285
00286
00287 if (size + 8 < size || av_new_packet(pkt, size + 8))
00288 return AVERROR(EIO);
00289 pkt->stream_index = fourxm->video_stream_index;
00290 pkt->pts = fourxm->video_pts;
00291 pkt->pos = avio_tell(s->pb);
00292 memcpy(pkt->data, header, 8);
00293 ret = avio_read(s->pb, &pkt->data[8], size);
00294
00295 if (ret < 0){
00296 av_free_packet(pkt);
00297 }else
00298 packet_read = 1;
00299 break;
00300
00301 case snd__TAG:
00302 track_number = avio_rl32(pb);
00303 avio_skip(pb, 4);
00304 size-=8;
00305
00306 if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
00307 ret= av_get_packet(s->pb, pkt, size);
00308 if(ret<0)
00309 return AVERROR(EIO);
00310 pkt->stream_index =
00311 fourxm->tracks[track_number].stream_index;
00312 pkt->pts = fourxm->tracks[track_number].audio_pts;
00313 packet_read = 1;
00314
00315
00316 audio_frame_count = size;
00317 if (fourxm->tracks[track_number].adpcm)
00318 audio_frame_count -=
00319 2 * (fourxm->tracks[track_number].channels);
00320 audio_frame_count /=
00321 fourxm->tracks[track_number].channels;
00322 if (fourxm->tracks[track_number].adpcm){
00323 audio_frame_count *= 2;
00324 }else
00325 audio_frame_count /=
00326 (fourxm->tracks[track_number].bits / 8);
00327 fourxm->tracks[track_number].audio_pts += audio_frame_count;
00328
00329 } else {
00330 avio_skip(pb, size);
00331 }
00332 break;
00333
00334 default:
00335 avio_skip(pb, size);
00336 break;
00337 }
00338 }
00339 return ret;
00340 }
00341
00342 static int fourxm_read_close(AVFormatContext *s)
00343 {
00344 FourxmDemuxContext *fourxm = s->priv_data;
00345
00346 av_freep(&fourxm->tracks);
00347
00348 return 0;
00349 }
00350
00351 AVInputFormat ff_fourxm_demuxer = {
00352 .name = "4xm",
00353 .long_name = NULL_IF_CONFIG_SMALL("4X Technologies format"),
00354 .priv_data_size = sizeof(FourxmDemuxContext),
00355 .read_probe = fourxm_probe,
00356 .read_header = fourxm_read_header,
00357 .read_packet = fourxm_read_packet,
00358 .read_close = fourxm_read_close,
00359 };