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