libavformat/c93.c
9a0ddd09
 /*
  * Interplay C93 demuxer
  * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
e5a389a1
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
9a0ddd09
  */
 
 #include "avformat.h"
 #include "voc.h"
9f449d57
 #include "libavutil/intreadwrite.h"
9a0ddd09
 
 typedef struct {
     uint16_t index;
     uint8_t length;
     uint8_t frames;
 } C93BlockRecord;
 
 typedef struct {
e998ba4f
     VocDecContext voc;
9a0ddd09
 
     C93BlockRecord block_records[512];
     int current_block;
 
     uint32_t frame_offsets[32];
     int current_frame;
     int next_pkt_is_audio;
 
     AVStream *audio;
 } C93DemuxContext;
 
f118d254
 static int probe(AVProbeData *p)
9a0ddd09
 {
9f449d57
     int i;
     int index = 1;
     if (p->buf_size < 16)
         return 0;
     for (i = 0; i < 16; i += 4) {
         if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
             return 0;
         index += p->buf[i + 2];
     }
     return AVPROBE_SCORE_MAX;
9a0ddd09
 }
 
f118d254
 static int read_header(AVFormatContext *s,
9a0ddd09
                            AVFormatParameters *ap)
 {
     AVStream *video;
ae628ec1
     AVIOContext *pb = s->pb;
9a0ddd09
     C93DemuxContext *c93 = s->priv_data;
     int i;
     int framecount = 0;
 
     for (i = 0; i < 512; i++) {
b7effd4e
         c93->block_records[i].index = avio_rl16(pb);
         c93->block_records[i].length = avio_r8(pb);
         c93->block_records[i].frames = avio_r8(pb);
9a0ddd09
         if (c93->block_records[i].frames > 32) {
             av_log(s, AV_LOG_ERROR, "too many frames in block\n");
             return AVERROR_INVALIDDATA;
         }
         framecount += c93->block_records[i].frames;
     }
 
     /* Audio streams are added if audio packets are found */
     s->ctx_flags |= AVFMTCTX_NOHEADER;
 
     video = av_new_stream(s, 0);
     if (!video)
769e10f0
         return AVERROR(ENOMEM);
9a0ddd09
 
72415b2a
     video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
9a0ddd09
     video->codec->codec_id = CODEC_ID_C93;
     video->codec->width = 320;
     video->codec->height = 192;
     /* 4:3 320x200 with 8 empty lines */
59729451
     video->sample_aspect_ratio = (AVRational) { 5, 6 };
a351110e
     av_set_pts_info(video, 64, 2, 25);
9a0ddd09
     video->nb_frames = framecount;
     video->duration = framecount;
     video->start_time = 0;
 
     c93->current_block = 0;
     c93->current_frame = 0;
     c93->next_pkt_is_audio = 0;
     return 0;
 }
 
 #define C93_HAS_PALETTE 0x01
 #define C93_FIRST_FRAME 0x02
 
f118d254
 static int read_packet(AVFormatContext *s, AVPacket *pkt)
9a0ddd09
 {
ae628ec1
     AVIOContext *pb = s->pb;
9a0ddd09
     C93DemuxContext *c93 = s->priv_data;
     C93BlockRecord *br = &c93->block_records[c93->current_block];
     int datasize;
     int ret, i;
 
     if (c93->next_pkt_is_audio) {
         c93->current_frame++;
         c93->next_pkt_is_audio = 0;
b7effd4e
         datasize = avio_rl16(pb);
9a0ddd09
         if (datasize > 42) {
             if (!c93->audio) {
                 c93->audio = av_new_stream(s, 1);
                 if (!c93->audio)
769e10f0
                     return AVERROR(ENOMEM);
72415b2a
                 c93->audio->codec->codec_type = AVMEDIA_TYPE_AUDIO;
9a0ddd09
             }
45a8a02a
             avio_skip(pb, 26); /* VOC header */
9a0ddd09
             ret = voc_get_packet(s, pkt, c93->audio, datasize - 26);
             if (ret > 0) {
                 pkt->stream_index = 1;
cc947f04
                 pkt->flags |= AV_PKT_FLAG_KEY;
9a0ddd09
                 return ret;
             }
         }
     }
     if (c93->current_frame >= br->frames) {
         if (c93->current_block >= 511 || !br[1].length)
6f3e0b21
             return AVERROR(EIO);
9a0ddd09
         br++;
         c93->current_block++;
         c93->current_frame = 0;
     }
 
     if (c93->current_frame == 0) {
6b4aa5da
         avio_seek(pb, br->index * 2048, SEEK_SET);
9a0ddd09
         for (i = 0; i < 32; i++) {
b7effd4e
             c93->frame_offsets[i] = avio_rl32(pb);
9a0ddd09
         }
     }
 
6b4aa5da
     avio_seek(pb,br->index * 2048 +
9a0ddd09
             c93->frame_offsets[c93->current_frame], SEEK_SET);
b7effd4e
     datasize = avio_rl16(pb); /* video frame size */
9a0ddd09
 
     ret = av_new_packet(pkt, datasize + 768 + 1);
     if (ret < 0)
         return ret;
     pkt->data[0] = 0;
     pkt->size = datasize + 1;
 
b7effd4e
     ret = avio_read(pb, pkt->data + 1, datasize);
9a0ddd09
     if (ret < datasize) {
6f3e0b21
         ret = AVERROR(EIO);
9a0ddd09
         goto fail;
     }
 
b7effd4e
     datasize = avio_rl16(pb); /* palette size */
9a0ddd09
     if (datasize) {
         if (datasize != 768) {
             av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
             ret = AVERROR_INVALIDDATA;
             goto fail;
         }
         pkt->data[0] |= C93_HAS_PALETTE;
b7effd4e
         ret = avio_read(pb, pkt->data + pkt->size, datasize);
9a0ddd09
         if (ret < datasize) {
6f3e0b21
             ret = AVERROR(EIO);
9a0ddd09
             goto fail;
         }
         pkt->size += 768;
     }
     pkt->stream_index = 0;
     c93->next_pkt_is_audio = 1;
 
     /* only the first frame is guaranteed to not reference previous frames */
     if (c93->current_block == 0 && c93->current_frame == 0) {
cc947f04
         pkt->flags |= AV_PKT_FLAG_KEY;
9a0ddd09
         pkt->data[0] |= C93_FIRST_FRAME;
     }
     return 0;
 
     fail:
     av_free_packet(pkt);
     return ret;
 }
 
c6610a21
 AVInputFormat ff_c93_demuxer = {
9a0ddd09
     "c93",
bde15e74
     NULL_IF_CONFIG_SMALL("Interplay C93"),
9a0ddd09
     sizeof(C93DemuxContext),
f118d254
     probe,
     read_header,
     read_packet,
9a0ddd09
 };