2
* Interplay C93 demuxer
3
* Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
#include "libavutil/intreadwrite.h"
35
C93BlockRecord block_records[512];
38
uint32_t frame_offsets[32];
40
int next_pkt_is_audio;
45
static int probe(AVProbeData *p)
51
for (i = 0; i < 16; i += 4) {
52
if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
54
index += p->buf[i + 2];
56
return AVPROBE_SCORE_MAX;
59
static int read_header(AVFormatContext *s,
60
AVFormatParameters *ap)
63
ByteIOContext *pb = s->pb;
64
C93DemuxContext *c93 = s->priv_data;
68
for (i = 0; i < 512; i++) {
69
c93->block_records[i].index = get_le16(pb);
70
c93->block_records[i].length = get_byte(pb);
71
c93->block_records[i].frames = get_byte(pb);
72
if (c93->block_records[i].frames > 32) {
73
av_log(s, AV_LOG_ERROR, "too many frames in block\n");
74
return AVERROR_INVALIDDATA;
76
framecount += c93->block_records[i].frames;
79
/* Audio streams are added if audio packets are found */
80
s->ctx_flags |= AVFMTCTX_NOHEADER;
82
video = av_new_stream(s, 0);
84
return AVERROR(ENOMEM);
86
video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
87
video->codec->codec_id = CODEC_ID_C93;
88
video->codec->width = 320;
89
video->codec->height = 192;
90
/* 4:3 320x200 with 8 empty lines */
91
video->sample_aspect_ratio = (AVRational) { 5, 6 };
92
video->time_base = (AVRational) { 2, 25 };
93
video->nb_frames = framecount;
94
video->duration = framecount;
95
video->start_time = 0;
97
c93->current_block = 0;
98
c93->current_frame = 0;
99
c93->next_pkt_is_audio = 0;
103
#define C93_HAS_PALETTE 0x01
104
#define C93_FIRST_FRAME 0x02
106
static int read_packet(AVFormatContext *s, AVPacket *pkt)
108
ByteIOContext *pb = s->pb;
109
C93DemuxContext *c93 = s->priv_data;
110
C93BlockRecord *br = &c93->block_records[c93->current_block];
114
if (c93->next_pkt_is_audio) {
115
c93->current_frame++;
116
c93->next_pkt_is_audio = 0;
117
datasize = get_le16(pb);
120
c93->audio = av_new_stream(s, 1);
122
return AVERROR(ENOMEM);
123
c93->audio->codec->codec_type = AVMEDIA_TYPE_AUDIO;
125
url_fskip(pb, 26); /* VOC header */
126
ret = voc_get_packet(s, pkt, c93->audio, datasize - 26);
128
pkt->stream_index = 1;
129
pkt->flags |= AV_PKT_FLAG_KEY;
134
if (c93->current_frame >= br->frames) {
135
if (c93->current_block >= 511 || !br[1].length)
138
c93->current_block++;
139
c93->current_frame = 0;
142
if (c93->current_frame == 0) {
143
url_fseek(pb, br->index * 2048, SEEK_SET);
144
for (i = 0; i < 32; i++) {
145
c93->frame_offsets[i] = get_le32(pb);
149
url_fseek(pb,br->index * 2048 +
150
c93->frame_offsets[c93->current_frame], SEEK_SET);
151
datasize = get_le16(pb); /* video frame size */
153
ret = av_new_packet(pkt, datasize + 768 + 1);
157
pkt->size = datasize + 1;
159
ret = get_buffer(pb, pkt->data + 1, datasize);
160
if (ret < datasize) {
165
datasize = get_le16(pb); /* palette size */
167
if (datasize != 768) {
168
av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
169
ret = AVERROR_INVALIDDATA;
172
pkt->data[0] |= C93_HAS_PALETTE;
173
ret = get_buffer(pb, pkt->data + pkt->size, datasize);
174
if (ret < datasize) {
180
pkt->stream_index = 0;
181
c93->next_pkt_is_audio = 1;
183
/* only the first frame is guaranteed to not reference previous frames */
184
if (c93->current_block == 0 && c93->current_frame == 0) {
185
pkt->flags |= AV_PKT_FLAG_KEY;
186
pkt->data[0] |= C93_FIRST_FRAME;
195
AVInputFormat c93_demuxer = {
197
NULL_IF_CONFIG_SMALL("Interplay C93"),
198
sizeof(C93DemuxContext),