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
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->track_count = current_track + 1;
00176 fourxm->tracks = av_realloc(fourxm->tracks,
00177 fourxm->track_count * sizeof(AudioTrack));
00178 if (!fourxm->tracks) {
00179 ret= AVERROR(ENOMEM);
00180 goto fail;
00181 }
00182 }
00183 fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]);
00184 fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]);
00185 fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
00186 fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]);
00187 fourxm->tracks[current_track].audio_pts = 0;
00188 i += 8 + size;
00189
00190
00191 st = av_new_stream(s, current_track);
00192 if (!st){
00193 ret= AVERROR(ENOMEM);
00194 goto fail;
00195 }
00196
00197 av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate);
00198
00199 fourxm->tracks[current_track].stream_index = st->index;
00200
00201 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00202 st->codec->codec_tag = 0;
00203 st->codec->channels = fourxm->tracks[current_track].channels;
00204 st->codec->sample_rate = fourxm->tracks[current_track].sample_rate;
00205 st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
00206 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00207 st->codec->bits_per_coded_sample;
00208 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00209 if (fourxm->tracks[current_track].adpcm){
00210 st->codec->codec_id = CODEC_ID_ADPCM_4XM;
00211 }else if (st->codec->bits_per_coded_sample == 8){
00212 st->codec->codec_id = CODEC_ID_PCM_U8;
00213 }else
00214 st->codec->codec_id = CODEC_ID_PCM_S16LE;
00215 }
00216 }
00217
00218
00219 GET_LIST_HEADER();
00220 if (fourcc_tag != MOVI_TAG){
00221 ret= AVERROR_INVALIDDATA;
00222 goto fail;
00223 }
00224
00225 av_free(header);
00226
00227 fourxm->video_pts = -1;
00228
00229 return 0;
00230 fail:
00231 av_freep(&fourxm->tracks);
00232 av_free(header);
00233 return ret;
00234 }
00235
00236 static int fourxm_read_packet(AVFormatContext *s,
00237 AVPacket *pkt)
00238 {
00239 FourxmDemuxContext *fourxm = s->priv_data;
00240 ByteIOContext *pb = s->pb;
00241 unsigned int fourcc_tag;
00242 unsigned int size, out_size;
00243 int ret = 0;
00244 unsigned int track_number;
00245 int packet_read = 0;
00246 unsigned char header[8];
00247 int audio_frame_count;
00248
00249 while (!packet_read) {
00250
00251 if ((ret = get_buffer(s->pb, header, 8)) < 0)
00252 return ret;
00253 fourcc_tag = AV_RL32(&header[0]);
00254 size = AV_RL32(&header[4]);
00255 if (url_feof(pb))
00256 return AVERROR(EIO);
00257 switch (fourcc_tag) {
00258
00259 case LIST_TAG:
00260
00261 fourxm->video_pts ++;
00262
00263
00264 get_le32(pb);
00265 break;
00266
00267 case ifrm_TAG:
00268 case pfrm_TAG:
00269 case cfrm_TAG:
00270 case ifr2_TAG:
00271 case pfr2_TAG:
00272 case cfr2_TAG:
00273
00274
00275 if (size + 8 < size || av_new_packet(pkt, size + 8))
00276 return AVERROR(EIO);
00277 pkt->stream_index = fourxm->video_stream_index;
00278 pkt->pts = fourxm->video_pts;
00279 pkt->pos = url_ftell(s->pb);
00280 memcpy(pkt->data, header, 8);
00281 ret = get_buffer(s->pb, &pkt->data[8], size);
00282
00283 if (ret < 0){
00284 av_free_packet(pkt);
00285 }else
00286 packet_read = 1;
00287 break;
00288
00289 case snd__TAG:
00290 track_number = get_le32(pb);
00291 out_size= get_le32(pb);
00292 size-=8;
00293
00294 if (track_number < fourxm->track_count) {
00295 ret= av_get_packet(s->pb, pkt, size);
00296 if(ret<0)
00297 return AVERROR(EIO);
00298 pkt->stream_index =
00299 fourxm->tracks[track_number].stream_index;
00300 pkt->pts = fourxm->tracks[track_number].audio_pts;
00301 packet_read = 1;
00302
00303
00304 audio_frame_count = size;
00305 if (fourxm->tracks[track_number].adpcm)
00306 audio_frame_count -=
00307 2 * (fourxm->tracks[track_number].channels);
00308 audio_frame_count /=
00309 fourxm->tracks[track_number].channels;
00310 if (fourxm->tracks[track_number].adpcm){
00311 audio_frame_count *= 2;
00312 }else
00313 audio_frame_count /=
00314 (fourxm->tracks[track_number].bits / 8);
00315 fourxm->tracks[track_number].audio_pts += audio_frame_count;
00316
00317 } else {
00318 url_fseek(pb, size, SEEK_CUR);
00319 }
00320 break;
00321
00322 default:
00323 url_fseek(pb, size, SEEK_CUR);
00324 break;
00325 }
00326 }
00327 return ret;
00328 }
00329
00330 static int fourxm_read_close(AVFormatContext *s)
00331 {
00332 FourxmDemuxContext *fourxm = s->priv_data;
00333
00334 av_freep(&fourxm->tracks);
00335
00336 return 0;
00337 }
00338
00339 AVInputFormat fourxm_demuxer = {
00340 "4xm",
00341 NULL_IF_CONFIG_SMALL("4X Technologies format"),
00342 sizeof(FourxmDemuxContext),
00343 fourxm_probe,
00344 fourxm_read_header,
00345 fourxm_read_packet,
00346 fourxm_read_close,
00347 };