Libav 0.7.1
|
00001 /* 00002 * 4X Technologies .4xm File Demuxer (no muxer) 00003 * Copyright (c) 2003 The ffmpeg Project 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 /* skip the first 3 32-bit numbers */ 00109 avio_skip(pb, 12); 00110 00111 /* check for LIST-HEAD */ 00112 GET_LIST_HEADER(); 00113 header_size = size - 4; 00114 if (fourcc_tag != HEAD_TAG || header_size < 0) 00115 return AVERROR_INVALIDDATA; 00116 00117 /* allocate space for the header and load the whole thing */ 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 /* take the lazy approach and search for any and all vtrk and strk chunks */ 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 /* check that there is enough data */ 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 /* allocate a new AVStream */ 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 /* check that there is enough data */ 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(fourxm->tracks, 00176 (current_track + 1) * sizeof(AudioTrack)); 00177 if (!fourxm->tracks) { 00178 ret = AVERROR(ENOMEM); 00179 goto fail; 00180 } 00181 memset(&fourxm->tracks[fourxm->track_count], 0, 00182 sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count)); 00183 fourxm->track_count = current_track + 1; 00184 } 00185 fourxm->tracks[current_track].adpcm = AV_RL32(&header[i + 12]); 00186 fourxm->tracks[current_track].channels = AV_RL32(&header[i + 36]); 00187 fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]); 00188 fourxm->tracks[current_track].bits = AV_RL32(&header[i + 44]); 00189 fourxm->tracks[current_track].audio_pts = 0; 00190 if( fourxm->tracks[current_track].channels <= 0 00191 || fourxm->tracks[current_track].sample_rate <= 0 00192 || fourxm->tracks[current_track].bits < 0){ 00193 av_log(s, AV_LOG_ERROR, "audio header invalid\n"); 00194 ret= -1; 00195 goto fail; 00196 } 00197 i += 8 + size; 00198 00199 /* allocate a new AVStream */ 00200 st = av_new_stream(s, current_track); 00201 if (!st){ 00202 ret= AVERROR(ENOMEM); 00203 goto fail; 00204 } 00205 00206 av_set_pts_info(st, 60, 1, fourxm->tracks[current_track].sample_rate); 00207 00208 fourxm->tracks[current_track].stream_index = st->index; 00209 00210 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00211 st->codec->codec_tag = 0; 00212 st->codec->channels = fourxm->tracks[current_track].channels; 00213 st->codec->sample_rate = fourxm->tracks[current_track].sample_rate; 00214 st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits; 00215 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * 00216 st->codec->bits_per_coded_sample; 00217 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample; 00218 if (fourxm->tracks[current_track].adpcm){ 00219 st->codec->codec_id = CODEC_ID_ADPCM_4XM; 00220 }else if (st->codec->bits_per_coded_sample == 8){ 00221 st->codec->codec_id = CODEC_ID_PCM_U8; 00222 }else 00223 st->codec->codec_id = CODEC_ID_PCM_S16LE; 00224 } 00225 } 00226 00227 /* skip over the LIST-MOVI chunk (which is where the stream should be */ 00228 GET_LIST_HEADER(); 00229 if (fourcc_tag != MOVI_TAG){ 00230 ret= AVERROR_INVALIDDATA; 00231 goto fail; 00232 } 00233 00234 av_free(header); 00235 /* initialize context members */ 00236 fourxm->video_pts = -1; /* first frame will push to 0 */ 00237 00238 return 0; 00239 fail: 00240 av_freep(&fourxm->tracks); 00241 av_free(header); 00242 return ret; 00243 } 00244 00245 static int fourxm_read_packet(AVFormatContext *s, 00246 AVPacket *pkt) 00247 { 00248 FourxmDemuxContext *fourxm = s->priv_data; 00249 AVIOContext *pb = s->pb; 00250 unsigned int fourcc_tag; 00251 unsigned int size; 00252 int ret = 0; 00253 unsigned int track_number; 00254 int packet_read = 0; 00255 unsigned char header[8]; 00256 int audio_frame_count; 00257 00258 while (!packet_read) { 00259 00260 if ((ret = avio_read(s->pb, header, 8)) < 0) 00261 return ret; 00262 fourcc_tag = AV_RL32(&header[0]); 00263 size = AV_RL32(&header[4]); 00264 if (pb->eof_reached) 00265 return AVERROR(EIO); 00266 switch (fourcc_tag) { 00267 00268 case LIST_TAG: 00269 /* this is a good time to bump the video pts */ 00270 fourxm->video_pts ++; 00271 00272 /* skip the LIST-* tag and move on to the next fourcc */ 00273 avio_rl32(pb); 00274 break; 00275 00276 case ifrm_TAG: 00277 case pfrm_TAG: 00278 case cfrm_TAG: 00279 case ifr2_TAG: 00280 case pfr2_TAG: 00281 case cfr2_TAG: 00282 /* allocate 8 more bytes than 'size' to account for fourcc 00283 * and size */ 00284 if (size + 8 < size || av_new_packet(pkt, size + 8)) 00285 return AVERROR(EIO); 00286 pkt->stream_index = fourxm->video_stream_index; 00287 pkt->pts = fourxm->video_pts; 00288 pkt->pos = avio_tell(s->pb); 00289 memcpy(pkt->data, header, 8); 00290 ret = avio_read(s->pb, &pkt->data[8], size); 00291 00292 if (ret < 0){ 00293 av_free_packet(pkt); 00294 }else 00295 packet_read = 1; 00296 break; 00297 00298 case snd__TAG: 00299 track_number = avio_rl32(pb); 00300 avio_skip(pb, 4); 00301 size-=8; 00302 00303 if (track_number < fourxm->track_count && fourxm->tracks[track_number].channels>0) { 00304 ret= av_get_packet(s->pb, pkt, size); 00305 if(ret<0) 00306 return AVERROR(EIO); 00307 pkt->stream_index = 00308 fourxm->tracks[track_number].stream_index; 00309 pkt->pts = fourxm->tracks[track_number].audio_pts; 00310 packet_read = 1; 00311 00312 /* pts accounting */ 00313 audio_frame_count = size; 00314 if (fourxm->tracks[track_number].adpcm) 00315 audio_frame_count -= 00316 2 * (fourxm->tracks[track_number].channels); 00317 audio_frame_count /= 00318 fourxm->tracks[track_number].channels; 00319 if (fourxm->tracks[track_number].adpcm){ 00320 audio_frame_count *= 2; 00321 }else 00322 audio_frame_count /= 00323 (fourxm->tracks[track_number].bits / 8); 00324 fourxm->tracks[track_number].audio_pts += audio_frame_count; 00325 00326 } else { 00327 avio_skip(pb, size); 00328 } 00329 break; 00330 00331 default: 00332 avio_skip(pb, size); 00333 break; 00334 } 00335 } 00336 return ret; 00337 } 00338 00339 static int fourxm_read_close(AVFormatContext *s) 00340 { 00341 FourxmDemuxContext *fourxm = s->priv_data; 00342 00343 av_freep(&fourxm->tracks); 00344 00345 return 0; 00346 } 00347 00348 AVInputFormat ff_fourxm_demuxer = { 00349 "4xm", 00350 NULL_IF_CONFIG_SMALL("4X Technologies format"), 00351 sizeof(FourxmDemuxContext), 00352 fourxm_probe, 00353 fourxm_read_header, 00354 fourxm_read_packet, 00355 fourxm_read_close, 00356 };