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
26 #include "libavutil/intreadwrite.h"
28 typedef struct C93BlockRecord
{
34 typedef struct C93DemuxContext
{
37 C93BlockRecord block_records
[512];
40 uint32_t frame_offsets
[32];
42 int next_pkt_is_audio
;
47 static int probe(const AVProbeData
*p
)
53 for (i
= 0; i
< 16; i
+= 4) {
54 if (AV_RL16(p
->buf
+ i
) != index
|| !p
->buf
[i
+ 2] || !p
->buf
[i
+ 3])
56 index
+= p
->buf
[i
+ 2];
58 return AVPROBE_SCORE_MAX
;
61 static int read_header(AVFormatContext
*s
)
64 AVIOContext
*pb
= s
->pb
;
65 C93DemuxContext
*c93
= s
->priv_data
;
69 for (i
= 0; i
< 512; i
++) {
70 c93
->block_records
[i
].index
= avio_rl16(pb
);
71 c93
->block_records
[i
].length
= avio_r8(pb
);
72 c93
->block_records
[i
].frames
= avio_r8(pb
);
73 if (c93
->block_records
[i
].frames
> 32) {
74 av_log(s
, AV_LOG_ERROR
, "too many frames in block\n");
75 return AVERROR_INVALIDDATA
;
77 framecount
+= c93
->block_records
[i
].frames
;
80 /* Audio streams are added if audio packets are found */
81 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
83 video
= avformat_new_stream(s
, NULL
);
85 return AVERROR(ENOMEM
);
87 video
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
88 video
->codecpar
->codec_id
= AV_CODEC_ID_C93
;
89 video
->codecpar
->width
= 320;
90 video
->codecpar
->height
= 192;
91 /* 4:3 320x200 with 8 empty lines */
92 video
->sample_aspect_ratio
= (AVRational
) { 5, 6 };
93 avpriv_set_pts_info(video
, 64, 2, 25);
94 video
->nb_frames
= framecount
;
95 video
->duration
= framecount
;
96 video
->start_time
= 0;
98 c93
->current_block
= 0;
99 c93
->current_frame
= 0;
100 c93
->next_pkt_is_audio
= 0;
104 #define C93_HAS_PALETTE 0x01
105 #define C93_FIRST_FRAME 0x02
107 static int read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
109 AVIOContext
*pb
= s
->pb
;
110 C93DemuxContext
*c93
= s
->priv_data
;
111 C93BlockRecord
*br
= &c93
->block_records
[c93
->current_block
];
115 if (c93
->next_pkt_is_audio
) {
116 c93
->current_frame
++;
117 c93
->next_pkt_is_audio
= 0;
118 datasize
= avio_rl16(pb
);
121 c93
->audio
= avformat_new_stream(s
, NULL
);
123 return AVERROR(ENOMEM
);
124 c93
->audio
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
126 avio_skip(pb
, 26); /* VOC header */
127 ret
= ff_voc_get_packet(s
, pkt
, c93
->audio
, datasize
- 26);
129 pkt
->stream_index
= 1;
130 pkt
->flags
|= AV_PKT_FLAG_KEY
;
135 if (c93
->current_frame
>= br
->frames
) {
136 if (c93
->current_block
>= 511 || !br
[1].length
)
139 c93
->current_block
++;
140 c93
->current_frame
= 0;
143 if (c93
->current_frame
== 0) {
144 avio_seek(pb
, br
->index
* 2048, SEEK_SET
);
145 for (i
= 0; i
< 32; i
++) {
146 c93
->frame_offsets
[i
] = avio_rl32(pb
);
150 avio_seek(pb
,br
->index
* 2048 +
151 c93
->frame_offsets
[c93
->current_frame
], SEEK_SET
);
152 datasize
= avio_rl16(pb
); /* video frame size */
154 ret
= av_new_packet(pkt
, datasize
+ 768 + 1);
158 pkt
->size
= datasize
+ 1;
160 ret
= avio_read(pb
, pkt
->data
+ 1, datasize
);
161 if (ret
< datasize
) {
165 datasize
= avio_rl16(pb
); /* palette size */
167 if (datasize
!= 768) {
168 av_log(s
, AV_LOG_ERROR
, "invalid palette size %u\n", datasize
);
169 return AVERROR_INVALIDDATA
;
171 pkt
->data
[0] |= C93_HAS_PALETTE
;
172 ret
= avio_read(pb
, pkt
->data
+ pkt
->size
, datasize
);
173 if (ret
< datasize
) {
178 pkt
->stream_index
= 0;
179 c93
->next_pkt_is_audio
= 1;
181 /* only the first frame is guaranteed to not reference previous frames */
182 if (c93
->current_block
== 0 && c93
->current_frame
== 0) {
183 pkt
->flags
|= AV_PKT_FLAG_KEY
;
184 pkt
->data
[0] |= C93_FIRST_FRAME
;
189 const FFInputFormat ff_c93_demuxer
= {
191 .p
.long_name
= NULL_IF_CONFIG_SMALL("Interplay C93"),
192 .priv_data_size
= sizeof(C93DemuxContext
),
194 .read_header
= read_header
,
195 .read_packet
= read_packet
,