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