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