c93.c 5.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22
 */

#include "avformat.h"
23
#include "internal.h"
24
#include "voc.h"
25
#include "libavutil/intreadwrite.h"
26

27
typedef struct C93BlockRecord {
28 29 30 31 32
    uint16_t index;
    uint8_t length;
    uint8_t frames;
} C93BlockRecord;

33
typedef struct C93DemuxContext {
34
    VocDecContext voc;
35 36 37 38 39 40 41 42 43 44 45

    C93BlockRecord block_records[512];
    int current_block;

    uint32_t frame_offsets[32];
    int current_frame;
    int next_pkt_is_audio;

    AVStream *audio;
} C93DemuxContext;

46
static int probe(AVProbeData *p)
47
{
48 49 50 51 52 53 54 55 56 57
    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;
58 59
}

60
static int read_header(AVFormatContext *s)
61 62
{
    AVStream *video;
63
    AVIOContext *pb = s->pb;
64 65 66 67 68
    C93DemuxContext *c93 = s->priv_data;
    int i;
    int framecount = 0;

    for (i = 0; i < 512; i++) {
69 70 71
        c93->block_records[i].index = avio_rl16(pb);
        c93->block_records[i].length = avio_r8(pb);
        c93->block_records[i].frames = avio_r8(pb);
72 73 74 75 76 77 78 79 80 81
        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;

82
    video = avformat_new_stream(s, NULL);
83
    if (!video)
84
        return AVERROR(ENOMEM);
85

86 87 88 89
    video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    video->codecpar->codec_id = AV_CODEC_ID_C93;
    video->codecpar->width = 320;
    video->codecpar->height = 192;
90
    /* 4:3 320x200 with 8 empty lines */
91
    video->sample_aspect_ratio = (AVRational) { 5, 6 };
92
    avpriv_set_pts_info(video, 64, 2, 25);
93 94 95 96 97 98 99 100 101 102 103 104 105
    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

106
static int read_packet(AVFormatContext *s, AVPacket *pkt)
107
{
108
    AVIOContext *pb = s->pb;
109 110 111 112 113 114 115 116
    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;
117
        datasize = avio_rl16(pb);
118 119
        if (datasize > 42) {
            if (!c93->audio) {
120
                c93->audio = avformat_new_stream(s, NULL);
121
                if (!c93->audio)
122
                    return AVERROR(ENOMEM);
123
                c93->audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
124
            }
125
            avio_skip(pb, 26); /* VOC header */
126
            ret = ff_voc_get_packet(s, pkt, c93->audio, datasize - 26);
127 128
            if (ret > 0) {
                pkt->stream_index = 1;
129
                pkt->flags |= AV_PKT_FLAG_KEY;
130 131 132 133 134 135
                return ret;
            }
        }
    }
    if (c93->current_frame >= br->frames) {
        if (c93->current_block >= 511 || !br[1].length)
Piotr Bandurski's avatar
Piotr Bandurski committed
136
            return AVERROR_EOF;
137 138 139 140 141 142
        br++;
        c93->current_block++;
        c93->current_frame = 0;
    }

    if (c93->current_frame == 0) {
143
        avio_seek(pb, br->index * 2048, SEEK_SET);
144
        for (i = 0; i < 32; i++) {
145
            c93->frame_offsets[i] = avio_rl32(pb);
146 147 148
        }
    }

149
    avio_seek(pb,br->index * 2048 +
150
            c93->frame_offsets[c93->current_frame], SEEK_SET);
151
    datasize = avio_rl16(pb); /* video frame size */
152 153 154 155 156 157 158

    ret = av_new_packet(pkt, datasize + 768 + 1);
    if (ret < 0)
        return ret;
    pkt->data[0] = 0;
    pkt->size = datasize + 1;

159
    ret = avio_read(pb, pkt->data + 1, datasize);
160
    if (ret < datasize) {
161
        ret = AVERROR(EIO);
162 163 164
        goto fail;
    }

165
    datasize = avio_rl16(pb); /* palette size */
166 167 168 169 170 171 172
    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;
173
        ret = avio_read(pb, pkt->data + pkt->size, datasize);
174
        if (ret < datasize) {
175
            ret = AVERROR(EIO);
176 177 178 179 180 181 182 183 184
            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) {
185
        pkt->flags |= AV_PKT_FLAG_KEY;
186 187 188 189 190
        pkt->data[0] |= C93_FIRST_FRAME;
    }
    return 0;

    fail:
191
    av_packet_unref(pkt);
192 193 194
    return ret;
}

195
AVInputFormat ff_c93_demuxer = {
196 197 198 199 200 201
    .name           = "c93",
    .long_name      = NULL_IF_CONFIG_SMALL("Interplay C93"),
    .priv_data_size = sizeof(C93DemuxContext),
    .read_probe     = probe,
    .read_header    = read_header,
    .read_packet    = read_packet,
202
};