libavformat/c93.c
Go to the documentation of this file.
00001 /*
00002  * Interplay C93 demuxer
00003  * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
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 
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "voc.h"
00025 #include "libavutil/intreadwrite.h"
00026 
00027 typedef struct {
00028     uint16_t index;
00029     uint8_t length;
00030     uint8_t frames;
00031 } C93BlockRecord;
00032 
00033 typedef struct {
00034     VocDecContext voc;
00035 
00036     C93BlockRecord block_records[512];
00037     int current_block;
00038 
00039     uint32_t frame_offsets[32];
00040     int current_frame;
00041     int next_pkt_is_audio;
00042 
00043     AVStream *audio;
00044 } C93DemuxContext;
00045 
00046 static int probe(AVProbeData *p)
00047 {
00048     int i;
00049     int index = 1;
00050     if (p->buf_size < 16)
00051         return 0;
00052     for (i = 0; i < 16; i += 4) {
00053         if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
00054             return 0;
00055         index += p->buf[i + 2];
00056     }
00057     return AVPROBE_SCORE_MAX;
00058 }
00059 
00060 static int read_header(AVFormatContext *s,
00061                            AVFormatParameters *ap)
00062 {
00063     AVStream *video;
00064     AVIOContext *pb = s->pb;
00065     C93DemuxContext *c93 = s->priv_data;
00066     int i;
00067     int framecount = 0;
00068 
00069     for (i = 0; i < 512; i++) {
00070         c93->block_records[i].index = avio_rl16(pb);
00071         c93->block_records[i].length = avio_r8(pb);
00072         c93->block_records[i].frames = avio_r8(pb);
00073         if (c93->block_records[i].frames > 32) {
00074             av_log(s, AV_LOG_ERROR, "too many frames in block\n");
00075             return AVERROR_INVALIDDATA;
00076         }
00077         framecount += c93->block_records[i].frames;
00078     }
00079 
00080     /* Audio streams are added if audio packets are found */
00081     s->ctx_flags |= AVFMTCTX_NOHEADER;
00082 
00083     video = avformat_new_stream(s, NULL);
00084     if (!video)
00085         return AVERROR(ENOMEM);
00086 
00087     video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00088     video->codec->codec_id = CODEC_ID_C93;
00089     video->codec->width = 320;
00090     video->codec->height = 192;
00091     /* 4:3 320x200 with 8 empty lines */
00092     video->sample_aspect_ratio = (AVRational) { 5, 6 };
00093     avpriv_set_pts_info(video, 64, 2, 25);
00094     video->nb_frames = framecount;
00095     video->duration = framecount;
00096     video->start_time = 0;
00097 
00098     c93->current_block = 0;
00099     c93->current_frame = 0;
00100     c93->next_pkt_is_audio = 0;
00101     return 0;
00102 }
00103 
00104 #define C93_HAS_PALETTE 0x01
00105 #define C93_FIRST_FRAME 0x02
00106 
00107 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00108 {
00109     AVIOContext *pb = s->pb;
00110     C93DemuxContext *c93 = s->priv_data;
00111     C93BlockRecord *br = &c93->block_records[c93->current_block];
00112     int datasize;
00113     int ret, i;
00114 
00115     if (c93->next_pkt_is_audio) {
00116         c93->current_frame++;
00117         c93->next_pkt_is_audio = 0;
00118         datasize = avio_rl16(pb);
00119         if (datasize > 42) {
00120             if (!c93->audio) {
00121                 c93->audio = avformat_new_stream(s, NULL);
00122                 if (!c93->audio)
00123                     return AVERROR(ENOMEM);
00124                 c93->audio->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00125             }
00126             avio_skip(pb, 26); /* VOC header */
00127             ret = voc_get_packet(s, pkt, c93->audio, datasize - 26);
00128             if (ret > 0) {
00129                 pkt->stream_index = 1;
00130                 pkt->flags |= AV_PKT_FLAG_KEY;
00131                 return ret;
00132             }
00133         }
00134     }
00135     if (c93->current_frame >= br->frames) {
00136         if (c93->current_block >= 511 || !br[1].length)
00137             return AVERROR(EIO);
00138         br++;
00139         c93->current_block++;
00140         c93->current_frame = 0;
00141     }
00142 
00143     if (c93->current_frame == 0) {
00144         avio_seek(pb, br->index * 2048, SEEK_SET);
00145         for (i = 0; i < 32; i++) {
00146             c93->frame_offsets[i] = avio_rl32(pb);
00147         }
00148     }
00149 
00150     avio_seek(pb,br->index * 2048 +
00151             c93->frame_offsets[c93->current_frame], SEEK_SET);
00152     datasize = avio_rl16(pb); /* video frame size */
00153 
00154     ret = av_new_packet(pkt, datasize + 768 + 1);
00155     if (ret < 0)
00156         return ret;
00157     pkt->data[0] = 0;
00158     pkt->size = datasize + 1;
00159 
00160     ret = avio_read(pb, pkt->data + 1, datasize);
00161     if (ret < datasize) {
00162         ret = AVERROR(EIO);
00163         goto fail;
00164     }
00165 
00166     datasize = avio_rl16(pb); /* palette size */
00167     if (datasize) {
00168         if (datasize != 768) {
00169             av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
00170             ret = AVERROR_INVALIDDATA;
00171             goto fail;
00172         }
00173         pkt->data[0] |= C93_HAS_PALETTE;
00174         ret = avio_read(pb, pkt->data + pkt->size, datasize);
00175         if (ret < datasize) {
00176             ret = AVERROR(EIO);
00177             goto fail;
00178         }
00179         pkt->size += 768;
00180     }
00181     pkt->stream_index = 0;
00182     c93->next_pkt_is_audio = 1;
00183 
00184     /* only the first frame is guaranteed to not reference previous frames */
00185     if (c93->current_block == 0 && c93->current_frame == 0) {
00186         pkt->flags |= AV_PKT_FLAG_KEY;
00187         pkt->data[0] |= C93_FIRST_FRAME;
00188     }
00189     return 0;
00190 
00191     fail:
00192     av_free_packet(pkt);
00193     return ret;
00194 }
00195 
00196 AVInputFormat ff_c93_demuxer = {
00197     .name           = "c93",
00198     .long_name      = NULL_IF_CONFIG_SMALL("Interplay C93"),
00199     .priv_data_size = sizeof(C93DemuxContext),
00200     .read_probe     = probe,
00201     .read_header    = read_header,
00202     .read_packet    = read_packet,
00203 };