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 {
00096 AVIOContext *pb = s->pb;
00097 unsigned int fourcc_tag;
00098 unsigned int size;
00099 int header_size;
00100 FourxmDemuxContext *fourxm = s->priv_data;
00101 unsigned char *header;
00102 int i, ret;
00103 AVStream *st;
00104
00105 fourxm->track_count = 0;
00106 fourxm->tracks = NULL;
00107 fourxm->fps = 1.0;
00108
00109
00110 avio_skip(pb, 12);
00111
00112
00113 GET_LIST_HEADER();
00114 header_size = size - 4;
00115 if (fourcc_tag != HEAD_TAG || header_size < 0)
00116 return AVERROR_INVALIDDATA;
00117
00118
00119 header = av_malloc(header_size);
00120 if (!header)
00121 return AVERROR(ENOMEM);
00122 if (avio_read(pb, header, header_size) != header_size){
00123 av_free(header);
00124 return AVERROR(EIO);
00125 }
00126
00127
00128 for (i = 0; i < header_size - 8; i++) {
00129 fourcc_tag = AV_RL32(&header[i]);
00130 size = AV_RL32(&header[i + 4]);
00131 if (size > header_size - i - 8 && (fourcc_tag == vtrk_TAG || fourcc_tag == strk_TAG)) {
00132 av_log(s, AV_LOG_ERROR, "chunk larger than array %d>%d\n", size, header_size - i - 8);
00133 return AVERROR_INVALIDDATA;
00134 }
00135
00136 if (fourcc_tag == std__TAG) {
00137 if (header_size < i + 16) {
00138 av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
00139 return AVERROR_INVALIDDATA;
00140 }
00141 fourxm->fps = av_int2float(AV_RL32(&header[i + 12]));
00142 } else if (fourcc_tag == vtrk_TAG) {
00143
00144 if (size != vtrk_SIZE) {
00145 ret= AVERROR_INVALIDDATA;
00146 goto fail;
00147 }
00148 fourxm->width = AV_RL32(&header[i + 36]);
00149 fourxm->height = AV_RL32(&header[i + 40]);
00150
00151
00152 st = avformat_new_stream(s, NULL);
00153 if (!st){
00154 ret= AVERROR(ENOMEM);
00155 goto fail;
00156 }
00157 avpriv_set_pts_info(st, 60, 1, fourxm->fps);
00158
00159 fourxm->video_stream_index = st->index;
00160
00161 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00162 st->codec->codec_id = AV_CODEC_ID_4XM;
00163 st->codec->extradata_size = 4;
00164 st->codec->extradata = av_malloc(4);
00165 AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
00166 st->codec->width = fourxm->width;
00167 st->codec->height = fourxm->height;
00168
00169 i += 8 + size;
00170 } else if (fourcc_tag == strk_TAG) {
00171 int current_track;
00172
00173 if (size != strk_SIZE) {
00174 ret= AVERROR_INVALIDDATA;
00175 goto fail;
00176 }
00177 current_track = AV_RL32(&header[i + 8]);
00178 if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
00179 av_log(s, AV_LOG_ERROR, "current_track too large\n");
00180 ret = AVERROR_INVALIDDATA;
00181 goto fail;
00182 }
00183 if (current_track + 1 > fourxm->track_count) {
00184 fourxm->tracks = av_realloc_f(fourxm->tracks,
00185 sizeof(AudioTrack),
00186 current_track + 1);
00187 if (!fourxm->tracks) {
00188 ret = AVERROR(ENOMEM);
00189 goto fail;
00190 }
00191 memset(&fourxm->tracks[fourxm->track_count], 0,
00192 sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
00193 fourxm->track_count = current_track + 1;
00194 }
00195 fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]);
00196 fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]);
00197 fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
00198 fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]);
00199 fourxm->tracks[current_track].audio_pts = 0;
00200 if( fourxm->tracks[current_track].channels <= 0
00201 || fourxm->tracks[current_track].sample_rate <= 0
00202 || fourxm->tracks[current_track].bits < 0){
00203 av_log(s, AV_LOG_ERROR, "audio header invalid\n");
00204 ret = AVERROR_INVALIDDATA;
00205 goto fail;
00206 }
00207 if(!fourxm->tracks[current_track].adpcm && fourxm->tracks[current_track].bits<8){
00208 av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
00209 ret = AVERROR_INVALIDDATA;
00210 goto fail;
00211 }
00212 i += 8 + size;
00213
00214
00215 st = avformat_new_stream(s, NULL);
00216 if (!st){
00217 ret= AVERROR(ENOMEM);
00218 goto fail;
00219 }
00220
00221 st->id = current_track;
00222 avpriv_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00223
00224 fourxm->tracks[current_track].stream_index = st->index;
00225
00226 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00227 st->codec->codec_tag = 0;
00228 st->codec->channels = fourxm->tracks[current_track].channels;
00229 st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
00230 st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
00231 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00232 st->codec->bits_per_coded_sample;
00233 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00234 if (fourxm->tracks[current_track].adpcm){
00235 st->codec->codec_id = AV_CODEC_ID_ADPCM_4XM;
00236 }else if (st->codec->bits_per_coded_sample == 8){
00237 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
00238 }else
00239 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
00240 }
00241 }
00242
00243
00244 GET_LIST_HEADER();
00245 if (fourcc_tag != MOVI_TAG){
00246 ret= AVERROR_INVALIDDATA;
00247 goto fail;
00248 }
00249
00250 av_free(header);
00251
00252 fourxm->video_pts = -1;
00253
00254 return 0;
00255 fail:
00256 av_freep(&fourxm->tracks);
00257 av_free(header);
00258 return ret;
00259 }
00260
00261 static int fourxm_read_packet(AVFormatContext *s,
00262 AVPacket *pkt)
00263 {
00264 FourxmDemuxContext *fourxm = s->priv_data;
00265 AVIOContext *pb = s->pb;
00266 unsigned int fourcc_tag;
00267 unsigned int size;
00268 int ret = 0;
00269 unsigned int track_number;
00270 int packet_read = 0;
00271 unsigned char header[8];
00272 int audio_frame_count;
00273
00274 while (!packet_read) {
00275
00276 if ((ret = avio_read(s->pb, header, 8)) < 0)
00277 return ret;
00278 fourcc_tag = AV_RL32(&header[0]);
00279 size = AV_RL32(&header[4]);
00280 if (url_feof(pb))
00281 return AVERROR(EIO);
00282 switch (fourcc_tag) {
00283
00284 case LIST_TAG:
00285
00286 fourxm->video_pts ++;
00287
00288
00289 avio_rl32(pb);
00290 break;
00291
00292 case ifrm_TAG:
00293 case pfrm_TAG:
00294 case cfrm_TAG:
00295 case ifr2_TAG:
00296 case pfr2_TAG:
00297 case cfr2_TAG:
00298
00299
00300 if (size + 8 < size || av_new_packet(pkt, size + 8))
00301 return AVERROR(EIO);
00302 pkt->stream_index = fourxm->video_stream_index;
00303 pkt->pts = fourxm->video_pts;
00304 pkt->pos = avio_tell(s->pb);
00305 memcpy(pkt->data, header, 8);
00306 ret = avio_read(s->pb, &pkt->data[8], size);
00307
00308 if (ret < 0){
00309 av_free_packet(pkt);
00310 }else
00311 packet_read = 1;
00312 break;
00313
00314 case snd__TAG:
00315 track_number = avio_rl32(pb);
00316 avio_skip(pb, 4);
00317 size-=8;
00318
00319 if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) {
00320 ret= av_get_packet(s->pb, pkt, size);
00321 if(ret<0)
00322 return AVERROR(EIO);
00323 pkt->stream_index =
00324 fourxm->tracks[track_number].stream_index;
00325 pkt->pts = fourxm->tracks[track_number].audio_pts;
00326 packet_read = 1;
00327
00328
00329 audio_frame_count = size;
00330 if (fourxm->tracks[track_number].adpcm)
00331 audio_frame_count -=
00332 2 * (fourxm->tracks[track_number].channels);
00333 audio_frame_count /=
00334 fourxm->tracks[track_number].channels;
00335 if (fourxm->tracks[track_number].adpcm){
00336 audio_frame_count *= 2;
00337 }else
00338 audio_frame_count /=
00339 (fourxm->tracks[track_number].bits / 8);
00340 fourxm->tracks[track_number].audio_pts += audio_frame_count;
00341
00342 } else {
00343 avio_skip(pb, size);
00344 }
00345 break;
00346
00347 default:
00348 avio_skip(pb, size);
00349 break;
00350 }
00351 }
00352 return ret;
00353 }
00354
00355 static int fourxm_read_close(AVFormatContext *s)
00356 {
00357 FourxmDemuxContext *fourxm = s->priv_data;
00358
00359 av_freep(&fourxm->tracks);
00360
00361 return 0;
00362 }
00363
00364 AVInputFormat ff_fourxm_demuxer = {
00365 .name = "4xm",
00366 .long_name = NULL_IF_CONFIG_SMALL("4X Technologies"),
00367 .priv_data_size = sizeof(FourxmDemuxContext),
00368 .read_probe = fourxm_probe,
00369 .read_header = fourxm_read_header,
00370 .read_packet = fourxm_read_packet,
00371 .read_close = fourxm_read_close,
00372 };