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 = get_le32(pb); \
00058 size = get_le32(pb); \
00059 if (fourcc_tag != LIST_TAG) \
00060 return AVERROR_INVALIDDATA; \
00061 fourcc_tag = get_le32(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 ByteIOContext *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 url_fseek(pb, 12, SEEK_CUR);
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 (get_buffer(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 if (size > header_size - i - 8 && (fourcc_tag == vtrk_TAG || fourcc_tag == strk_TAG)) {
00131 av_log(s, AV_LOG_ERROR, "chunk larger than array %d>%d\n", size, header_size - i - 8);
00132 return AVERROR_INVALIDDATA;
00133 }
00134
00135 if (fourcc_tag == std__TAG) {
00136 fourxm->fps = av_int2flt(AV_RL32(&header[i + 12]));
00137 } else if (fourcc_tag == vtrk_TAG) {
00138
00139 if (size != vtrk_SIZE) {
00140 ret= AVERROR_INVALIDDATA;
00141 goto fail;
00142 }
00143 fourxm->width = AV_RL32(&header[i + 36]);
00144 fourxm->height = AV_RL32(&header[i + 40]);
00145
00146
00147 st = av_new_stream(s, 0);
00148 if (!st){
00149 ret= AVERROR(ENOMEM);
00150 goto fail;
00151 }
00152 av_set_pts_info(st, 60, 1, fourxm->fps);
00153
00154 fourxm->video_stream_index = st->index;
00155
00156 st->codec->codec_type = CODEC_TYPE_VIDEO;
00157 st->codec->codec_id = CODEC_ID_4XM;
00158 st->codec->extradata_size = 4;
00159 st->codec->extradata = av_malloc(4);
00160 AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
00161 st->codec->width = fourxm->width;
00162 st->codec->height = fourxm->height;
00163
00164 i += 8 + size;
00165 } else if (fourcc_tag == strk_TAG) {
00166 int current_track;
00167
00168 if (size != strk_SIZE) {
00169 ret= AVERROR_INVALIDDATA;
00170 goto fail;
00171 }
00172 current_track = AV_RL32(&header[i + 8]);
00173 if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
00174 av_log(s, AV_LOG_ERROR, "current_track too large\n");
00175 ret= -1;
00176 goto fail;
00177 }
00178 if (current_track + 1 > fourxm->track_count) {
00179 fourxm->track_count = current_track + 1;
00180 fourxm->tracks = av_realloc(fourxm->tracks,
00181 fourxm->track_count * sizeof(AudioTrack));
00182 if (!fourxm->tracks) {
00183 ret= AVERROR(ENOMEM);
00184 goto fail;
00185 }
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 i += 8 + size;
00193
00194
00195 st = av_new_stream(s, current_track);
00196 if (!st){
00197 ret= AVERROR(ENOMEM);
00198 goto fail;
00199 }
00200
00201 av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00202
00203 fourxm->tracks[current_track].stream_index = st->index;
00204
00205 st->codec->codec_type = CODEC_TYPE_AUDIO;
00206 st->codec->codec_tag = 0;
00207 st->codec->channels = fourxm->tracks[current_track].channels;
00208 st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
00209 st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
00210 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00211 st->codec->bits_per_coded_sample;
00212 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00213 if (fourxm->tracks[current_track].adpcm){
00214 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
00215 }else if (st->codec->bits_per_coded_sample == 8){
00216 st->codec->codec_id = CODEC_ID_PCM_U8;
00217 }else
00218 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00219 }
00220 }
00221
00222
00223 GET_LIST_HEADER();
00224 if (fourcc_tag != MOVI_TAG){
00225 ret= AVERROR_INVALIDDATA;
00226 goto fail;
00227 }
00228
00229 av_free(header);
00230
00231 fourxm->video_pts = -1;
00232
00233 return 0;
00234 fail:
00235 av_freep(&fourxm->tracks);
00236 av_free(header);
00237 return ret;
00238 }
00239
00240 static int fourxm_read_packet(AVFormatContext *s,
00241 AVPacket *pkt)
00242 {
00243 FourxmDemuxContext *fourxm = s->priv_data;
00244 ByteIOContext *pb = s->pb;
00245 unsigned int fourcc_tag;
00246 unsigned int size, out_size;
00247 int ret = 0;
00248 unsigned int track_number;
00249 int packet_read = 0;
00250 unsigned char header[8];
00251 int audio_frame_count;
00252
00253 while (!packet_read) {
00254
00255 if ((ret = get_buffer(s->pb, header, 8)) < 0)
00256 return ret;
00257 fourcc_tag = AV_RL32(&header[0]);
00258 size = AV_RL32(&header[4]);
00259 if (url_feof(pb))
00260 return AVERROR(EIO);
00261 switch (fourcc_tag) {
00262
00263 case LIST_TAG:
00264
00265 fourxm->video_pts ++;
00266
00267
00268 get_le32(pb);
00269 break;
00270
00271 case ifrm_TAG:
00272 case pfrm_TAG:
00273 case cfrm_TAG:
00274 case ifr2_TAG:
00275 case pfr2_TAG:
00276 case cfr2_TAG:
00277
00278
00279 if (size + 8 < size || av_new_packet(pkt, size + 8))
00280 return AVERROR(EIO);
00281 pkt->stream_index = fourxm->video_stream_index;
00282 pkt->pts = fourxm->video_pts;
00283 pkt->pos = url_ftell(s->pb);
00284 memcpy(pkt->data, header, 8);
00285 ret = get_buffer(s->pb, &pkt->data[8], size);
00286
00287 if (ret < 0){
00288 av_free_packet(pkt);
00289 }else
00290 packet_read = 1;
00291 break;
00292
00293 case snd__TAG:
00294 track_number = get_le32(pb);
00295 out_size= get_le32(pb);
00296 size-=8;
00297
00298 if (track_number < fourxm->track_count) {
00299 ret= av_get_packet(s->pb, pkt, size);
00300 if(ret<0)
00301 return AVERROR(EIO);
00302 pkt->stream_index =
00303 fourxm->tracks[track_number].stream_index;
00304 pkt->pts = fourxm->tracks[track_number].audio_pts;
00305 packet_read = 1;
00306
00307
00308 audio_frame_count = size;
00309 if (fourxm->tracks[track_number].adpcm)
00310 audio_frame_count -=
00311 2 * (fourxm->tracks[track_number].channels);
00312 audio_frame_count /=
00313 fourxm->tracks[track_number].channels;
00314 if (fourxm->tracks[track_number].adpcm){
00315 audio_frame_count *= 2;
00316 }else
00317 audio_frame_count /=
00318 (fourxm->tracks[track_number].bits / 8);
00319 fourxm->tracks[track_number].audio_pts += audio_frame_count;
00320
00321 } else {
00322 url_fseek(pb, size, SEEK_CUR);
00323 }
00324 break;
00325
00326 default:
00327 url_fseek(pb, size, SEEK_CUR);
00328 break;
00329 }
00330 }
00331 return ret;
00332 }
00333
00334 static int fourxm_read_close(AVFormatContext *s)
00335 {
00336 FourxmDemuxContext *fourxm = s->priv_data;
00337
00338 av_freep(&fourxm->tracks);
00339
00340 return 0;
00341 }
00342
00343 AVInputFormat fourxm_demuxer = {
00344 "4xm",
00345 NULL_IF_CONFIG_SMALL("4X Technologies format"),
00346 sizeof(FourxmDemuxContext),
00347 fourxm_probe,
00348 fourxm_read_header,
00349 fourxm_read_packet,
00350 fourxm_read_close,
00351 };