• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 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 the video width */
00109     number = AV_RL32(&p->buf[0]);
00110     if ((number == 0) || (number > 1024))
00111        return 0;
00112 
00113     /* check the video height */
00114     number = AV_RL32(&p->buf[4]);
00115     if ((number == 0) || (number > 1024))
00116        return 0;
00117 
00118     /* check the audio sample rate */
00119     number = AV_RL32(&p->buf[8]);
00120     if ((number != 0) && ((number < 8000) | (number > 48000)))
00121         return 0;
00122 
00123     /* check the audio bytes/sample */
00124     number = AV_RL32(&p->buf[12]);
00125     if (number > 2)
00126         return 0;
00127 
00128     /* check the audio channels */
00129     number = AV_RL32(&p->buf[16]);
00130     if (number > 2)
00131         return 0;
00132 
00133     /* return half certainly since this check is a bit sketchy */
00134     return AVPROBE_SCORE_MAX / 2;
00135 }
00136 
00137 static int idcin_read_header(AVFormatContext *s,
00138                              AVFormatParameters *ap)
00139 {
00140     ByteIOContext *pb = s->pb;
00141     IdcinDemuxContext *idcin = s->priv_data;
00142     AVStream *st;
00143     unsigned int width, height;
00144     unsigned int sample_rate, bytes_per_sample, channels;
00145 
00146     /* get the 5 header parameters */
00147     width = get_le32(pb);
00148     height = get_le32(pb);
00149     sample_rate = get_le32(pb);
00150     bytes_per_sample = get_le32(pb);
00151     channels = get_le32(pb);
00152 
00153     st = av_new_stream(s, 0);
00154     if (!st)
00155         return AVERROR(ENOMEM);
00156     av_set_pts_info(st, 33, 1, IDCIN_FPS);
00157     idcin->video_stream_index = st->index;
00158     st->codec->codec_type = CODEC_TYPE_VIDEO;
00159     st->codec->codec_id = CODEC_ID_IDCIN;
00160     st->codec->codec_tag = 0;  /* no fourcc */
00161     st->codec->width = width;
00162     st->codec->height = height;
00163 
00164     /* load up the Huffman tables into extradata */
00165     st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
00166     st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
00167     if (get_buffer(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
00168         HUFFMAN_TABLE_SIZE)
00169         return AVERROR(EIO);
00170     /* save a reference in order to transport the palette */
00171     st->codec->palctrl = &idcin->palctrl;
00172 
00173     /* if sample rate is 0, assume no audio */
00174     if (sample_rate) {
00175         idcin->audio_present = 1;
00176         st = av_new_stream(s, 0);
00177         if (!st)
00178             return AVERROR(ENOMEM);
00179         av_set_pts_info(st, 33, 1, IDCIN_FPS);
00180         idcin->audio_stream_index = st->index;
00181         st->codec->codec_type = CODEC_TYPE_AUDIO;
00182         st->codec->codec_tag = 1;
00183         st->codec->channels = channels;
00184         st->codec->sample_rate = sample_rate;
00185         st->codec->bits_per_coded_sample = bytes_per_sample * 8;
00186         st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
00187         st->codec->block_align = bytes_per_sample * channels;
00188         if (bytes_per_sample == 1)
00189             st->codec->codec_id = CODEC_ID_PCM_U8;
00190         else
00191             st->codec->codec_id = CODEC_ID_PCM_S16LE;
00192 
00193         if (sample_rate % 14 != 0) {
00194             idcin->audio_chunk_size1 = (sample_rate / 14) *
00195             bytes_per_sample * channels;
00196             idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
00197                 bytes_per_sample * channels;
00198         } else {
00199             idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
00200                 (sample_rate / 14) * bytes_per_sample * channels;
00201         }
00202         idcin->current_audio_chunk = 0;
00203     } else
00204         idcin->audio_present = 1;
00205 
00206     idcin->next_chunk_is_video = 1;
00207     idcin->pts = 0;
00208 
00209     return 0;
00210 }
00211 
00212 static int idcin_read_packet(AVFormatContext *s,
00213                              AVPacket *pkt)
00214 {
00215     int ret;
00216     unsigned int command;
00217     unsigned int chunk_size;
00218     IdcinDemuxContext *idcin = s->priv_data;
00219     ByteIOContext *pb = s->pb;
00220     int i;
00221     int palette_scale;
00222     unsigned char r, g, b;
00223     unsigned char palette_buffer[768];
00224 
00225     if (url_feof(s->pb))
00226         return AVERROR(EIO);
00227 
00228     if (idcin->next_chunk_is_video) {
00229         command = get_le32(pb);
00230         if (command == 2) {
00231             return AVERROR(EIO);
00232         } else if (command == 1) {
00233             /* trigger a palette change */
00234             idcin->palctrl.palette_changed = 1;
00235             if (get_buffer(pb, palette_buffer, 768) != 768)
00236                 return AVERROR(EIO);
00237             /* scale the palette as necessary */
00238             palette_scale = 2;
00239             for (i = 0; i < 768; i++)
00240                 if (palette_buffer[i] > 63) {
00241                     palette_scale = 0;
00242                     break;
00243                 }
00244 
00245             for (i = 0; i < 256; i++) {
00246                 r = palette_buffer[i * 3    ] << palette_scale;
00247                 g = palette_buffer[i * 3 + 1] << palette_scale;
00248                 b = palette_buffer[i * 3 + 2] << palette_scale;
00249                 idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
00250             }
00251         }
00252 
00253         chunk_size = get_le32(pb);
00254         /* skip the number of decoded bytes (always equal to width * height) */
00255         url_fseek(pb, 4, SEEK_CUR);
00256         chunk_size -= 4;
00257         ret= av_get_packet(pb, pkt, chunk_size);
00258         if (ret != chunk_size)
00259             return AVERROR(EIO);
00260         pkt->stream_index = idcin->video_stream_index;
00261         pkt->pts = idcin->pts;
00262     } else {
00263         /* send out the audio chunk */
00264         if (idcin->current_audio_chunk)
00265             chunk_size = idcin->audio_chunk_size2;
00266         else
00267             chunk_size = idcin->audio_chunk_size1;
00268         ret= av_get_packet(pb, pkt, chunk_size);
00269         if (ret != chunk_size)
00270             return AVERROR(EIO);
00271         pkt->stream_index = idcin->audio_stream_index;
00272         pkt->pts = idcin->pts;
00273 
00274         idcin->current_audio_chunk ^= 1;
00275         idcin->pts++;
00276     }
00277 
00278     if (idcin->audio_present)
00279         idcin->next_chunk_is_video ^= 1;
00280 
00281     return ret;
00282 }
00283 
00284 AVInputFormat idcin_demuxer = {
00285     "idcin",
00286     NULL_IF_CONFIG_SMALL("id Cinematic format"),
00287     sizeof(IdcinDemuxContext),
00288     idcin_probe,
00289     idcin_read_header,
00290     idcin_read_packet,
00291 };

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1