Libav
|
00001 /* 00002 * id Quake II CIN File Demuxer 00003 * Copyright (c) 2003 The ffmpeg Project 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00071 #include "libavutil/intreadwrite.h" 00072 #include "avformat.h" 00073 00074 #define HUFFMAN_TABLE_SIZE (64 * 1024) 00075 #define IDCIN_FPS 14 00076 00077 typedef struct IdcinDemuxContext { 00078 int video_stream_index; 00079 int audio_stream_index; 00080 int audio_chunk_size1; 00081 int audio_chunk_size2; 00082 00083 /* demux state variables */ 00084 int current_audio_chunk; 00085 int next_chunk_is_video; 00086 int audio_present; 00087 00088 int64_t pts; 00089 00090 AVPaletteControl palctrl; 00091 } IdcinDemuxContext; 00092 00093 static int idcin_probe(AVProbeData *p) 00094 { 00095 unsigned int number; 00096 00097 /* 00098 * This is what you could call a "probabilistic" file check: id CIN 00099 * files don't have a definite file signature. In lieu of such a marker, 00100 * perform sanity checks on the 5 32-bit header fields: 00101 * width, height: greater than 0, less than or equal to 1024 00102 * audio sample rate: greater than or equal to 8000, less than or 00103 * equal to 48000, or 0 for no audio 00104 * audio sample width (bytes/sample): 0 for no audio, or 1 or 2 00105 * audio channels: 0 for no audio, or 1 or 2 00106 */ 00107 00108 /* check we have enough data to do all checks, otherwise the 00109 0-padding may cause a wrong recognition */ 00110 if (p->buf_size < 20) 00111 return 0; 00112 00113 /* check the video width */ 00114 number = AV_RL32(&p->buf[0]); 00115 if ((number == 0) || (number > 1024)) 00116 return 0; 00117 00118 /* check the video height */ 00119 number = AV_RL32(&p->buf[4]); 00120 if ((number == 0) || (number > 1024)) 00121 return 0; 00122 00123 /* check the audio sample rate */ 00124 number = AV_RL32(&p->buf[8]); 00125 if ((number != 0) && ((number < 8000) | (number > 48000))) 00126 return 0; 00127 00128 /* check the audio bytes/sample */ 00129 number = AV_RL32(&p->buf[12]); 00130 if (number > 2) 00131 return 0; 00132 00133 /* check the audio channels */ 00134 number = AV_RL32(&p->buf[16]); 00135 if (number > 2) 00136 return 0; 00137 00138 /* return half certainly since this check is a bit sketchy */ 00139 return AVPROBE_SCORE_MAX / 2; 00140 } 00141 00142 static int idcin_read_header(AVFormatContext *s, 00143 AVFormatParameters *ap) 00144 { 00145 ByteIOContext *pb = s->pb; 00146 IdcinDemuxContext *idcin = s->priv_data; 00147 AVStream *st; 00148 unsigned int width, height; 00149 unsigned int sample_rate, bytes_per_sample, channels; 00150 00151 /* get the 5 header parameters */ 00152 width = get_le32(pb); 00153 height = get_le32(pb); 00154 sample_rate = get_le32(pb); 00155 bytes_per_sample = get_le32(pb); 00156 channels = get_le32(pb); 00157 00158 st = av_new_stream(s, 0); 00159 if (!st) 00160 return AVERROR(ENOMEM); 00161 av_set_pts_info(st, 33, 1, IDCIN_FPS); 00162 idcin->video_stream_index = st->index; 00163 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00164 st->codec->codec_id = CODEC_ID_IDCIN; 00165 st->codec->codec_tag = 0; /* no fourcc */ 00166 st->codec->width = width; 00167 st->codec->height = height; 00168 00169 /* load up the Huffman tables into extradata */ 00170 st->codec->extradata_size = HUFFMAN_TABLE_SIZE; 00171 st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE); 00172 if (get_buffer(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) != 00173 HUFFMAN_TABLE_SIZE) 00174 return AVERROR(EIO); 00175 /* save a reference in order to transport the palette */ 00176 st->codec->palctrl = &idcin->palctrl; 00177 00178 /* if sample rate is 0, assume no audio */ 00179 if (sample_rate) { 00180 idcin->audio_present = 1; 00181 st = av_new_stream(s, 0); 00182 if (!st) 00183 return AVERROR(ENOMEM); 00184 av_set_pts_info(st, 33, 1, IDCIN_FPS); 00185 idcin->audio_stream_index = st->index; 00186 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00187 st->codec->codec_tag = 1; 00188 st->codec->channels = channels; 00189 st->codec->sample_rate = sample_rate; 00190 st->codec->bits_per_coded_sample = bytes_per_sample * 8; 00191 st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels; 00192 st->codec->block_align = bytes_per_sample * channels; 00193 if (bytes_per_sample == 1) 00194 st->codec->codec_id = CODEC_ID_PCM_U8; 00195 else 00196 st->codec->codec_id = CODEC_ID_PCM_S16LE; 00197 00198 if (sample_rate % 14 != 0) { 00199 idcin->audio_chunk_size1 = (sample_rate / 14) * 00200 bytes_per_sample * channels; 00201 idcin->audio_chunk_size2 = (sample_rate / 14 + 1) * 00202 bytes_per_sample * channels; 00203 } else { 00204 idcin->audio_chunk_size1 = idcin->audio_chunk_size2 = 00205 (sample_rate / 14) * bytes_per_sample * channels; 00206 } 00207 idcin->current_audio_chunk = 0; 00208 } else 00209 idcin->audio_present = 1; 00210 00211 idcin->next_chunk_is_video = 1; 00212 idcin->pts = 0; 00213 00214 return 0; 00215 } 00216 00217 static int idcin_read_packet(AVFormatContext *s, 00218 AVPacket *pkt) 00219 { 00220 int ret; 00221 unsigned int command; 00222 unsigned int chunk_size; 00223 IdcinDemuxContext *idcin = s->priv_data; 00224 ByteIOContext *pb = s->pb; 00225 int i; 00226 int palette_scale; 00227 unsigned char r, g, b; 00228 unsigned char palette_buffer[768]; 00229 00230 if (url_feof(s->pb)) 00231 return AVERROR(EIO); 00232 00233 if (idcin->next_chunk_is_video) { 00234 command = get_le32(pb); 00235 if (command == 2) { 00236 return AVERROR(EIO); 00237 } else if (command == 1) { 00238 /* trigger a palette change */ 00239 idcin->palctrl.palette_changed = 1; 00240 if (get_buffer(pb, palette_buffer, 768) != 768) 00241 return AVERROR(EIO); 00242 /* scale the palette as necessary */ 00243 palette_scale = 2; 00244 for (i = 0; i < 768; i++) 00245 if (palette_buffer[i] > 63) { 00246 palette_scale = 0; 00247 break; 00248 } 00249 00250 for (i = 0; i < 256; i++) { 00251 r = palette_buffer[i * 3 ] << palette_scale; 00252 g = palette_buffer[i * 3 + 1] << palette_scale; 00253 b = palette_buffer[i * 3 + 2] << palette_scale; 00254 idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b); 00255 } 00256 } 00257 00258 chunk_size = get_le32(pb); 00259 /* skip the number of decoded bytes (always equal to width * height) */ 00260 url_fseek(pb, 4, SEEK_CUR); 00261 chunk_size -= 4; 00262 ret= av_get_packet(pb, pkt, chunk_size); 00263 if (ret < 0) 00264 return ret; 00265 pkt->stream_index = idcin->video_stream_index; 00266 pkt->pts = idcin->pts; 00267 } else { 00268 /* send out the audio chunk */ 00269 if (idcin->current_audio_chunk) 00270 chunk_size = idcin->audio_chunk_size2; 00271 else 00272 chunk_size = idcin->audio_chunk_size1; 00273 ret= av_get_packet(pb, pkt, chunk_size); 00274 if (ret < 0) 00275 return ret; 00276 pkt->stream_index = idcin->audio_stream_index; 00277 pkt->pts = idcin->pts; 00278 00279 idcin->current_audio_chunk ^= 1; 00280 idcin->pts++; 00281 } 00282 00283 if (idcin->audio_present) 00284 idcin->next_chunk_is_video ^= 1; 00285 00286 return ret; 00287 } 00288 00289 AVInputFormat idcin_demuxer = { 00290 "idcin", 00291 NULL_IF_CONFIG_SMALL("id Cinematic format"), 00292 sizeof(IdcinDemuxContext), 00293 idcin_probe, 00294 idcin_read_header, 00295 idcin_read_packet, 00296 };