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
23 #include "avio_internal.h"
27 #include "libavutil/intreadwrite.h"
29 typedef struct C93BlockRecord
{
35 typedef struct C93DemuxContext
{
38 C93BlockRecord block_records
[512];
41 uint32_t frame_offsets
[32];
43 int next_pkt_is_audio
;
48 static int probe(const AVProbeData
*p
)
54 for (i
= 0; i
< 16; i
+= 4) {
55 if (AV_RL16(p
->buf
+ i
) != index
|| !p
->buf
[i
+ 2] || !p
->buf
[i
+ 3])
57 index
+= p
->buf
[i
+ 2];
59 return AVPROBE_SCORE_MAX
;
62 static int read_header(AVFormatContext
*s
)
65 AVIOContext
*pb
= s
->pb
;
66 C93DemuxContext
*c93
= s
->priv_data
;
70 for (i
= 0; i
< 512; i
++) {
71 c93
->block_records
[i
].index
= avio_rl16(pb
);
72 c93
->block_records
[i
].length
= avio_r8(pb
);
73 c93
->block_records
[i
].frames
= avio_r8(pb
);
74 if (c93
->block_records
[i
].frames
> 32) {
75 av_log(s
, AV_LOG_ERROR
, "too many frames in block\n");
76 return AVERROR_INVALIDDATA
;
78 framecount
+= c93
->block_records
[i
].frames
;
81 /* Audio streams are added if audio packets are found */
82 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
84 video
= avformat_new_stream(s
, NULL
);
86 return AVERROR(ENOMEM
);
88 video
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
89 video
->codecpar
->codec_id
= AV_CODEC_ID_C93
;
90 video
->codecpar
->width
= 320;
91 video
->codecpar
->height
= 192;
92 /* 4:3 320x200 with 8 empty lines */
93 video
->sample_aspect_ratio
= (AVRational
) { 5, 6 };
94 avpriv_set_pts_info(video
, 64, 2, 25);
95 video
->nb_frames
= framecount
;
96 video
->duration
= framecount
;
97 video
->start_time
= 0;
99 c93
->current_block
= 0;
100 c93
->current_frame
= 0;
101 c93
->next_pkt_is_audio
= 0;
105 #define C93_HAS_PALETTE 0x01
106 #define C93_FIRST_FRAME 0x02
108 static int read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
110 AVIOContext
*pb
= s
->pb
;
111 C93DemuxContext
*c93
= s
->priv_data
;
112 C93BlockRecord
*br
= &c93
->block_records
[c93
->current_block
];
116 if (c93
->next_pkt_is_audio
) {
117 c93
->current_frame
++;
118 c93
->next_pkt_is_audio
= 0;
119 datasize
= avio_rl16(pb
);
122 c93
->audio
= avformat_new_stream(s
, NULL
);
124 return AVERROR(ENOMEM
);
125 c93
->audio
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
127 avio_skip(pb
, 26); /* VOC header */
128 ret
= ff_voc_get_packet(s
, pkt
, c93
->audio
, datasize
- 26);
130 pkt
->stream_index
= 1;
131 pkt
->flags
|= AV_PKT_FLAG_KEY
;
136 if (c93
->current_frame
>= br
->frames
) {
137 if (c93
->current_block
>= 511 || !br
[1].length
)
140 c93
->current_block
++;
141 c93
->current_frame
= 0;
144 if (c93
->current_frame
== 0) {
145 avio_seek(pb
, br
->index
* 2048, SEEK_SET
);
146 for (i
= 0; i
< 32; i
++) {
147 c93
->frame_offsets
[i
] = avio_rl32(pb
);
151 avio_seek(pb
,br
->index
* 2048 +
152 c93
->frame_offsets
[c93
->current_frame
], SEEK_SET
);
153 datasize
= avio_rl16(pb
); /* video frame size */
155 ret
= av_new_packet(pkt
, datasize
+ 768 + 1);
159 pkt
->size
= datasize
+ 1;
161 ret
= ffio_read_size(pb
, pkt
->data
+ 1, datasize
);
166 datasize
= avio_rl16(pb
); /* palette size */
168 if (datasize
!= 768) {
169 av_log(s
, AV_LOG_ERROR
, "invalid palette size %u\n", datasize
);
170 return AVERROR_INVALIDDATA
;
172 pkt
->data
[0] |= C93_HAS_PALETTE
;
173 ret
= ffio_read_size(pb
, pkt
->data
+ pkt
->size
, datasize
);
179 pkt
->stream_index
= 0;
180 c93
->next_pkt_is_audio
= 1;
182 /* only the first frame is guaranteed to not reference previous frames */
183 if (c93
->current_block
== 0 && c93
->current_frame
== 0) {
184 pkt
->flags
|= AV_PKT_FLAG_KEY
;
185 pkt
->data
[0] |= C93_FIRST_FRAME
;
190 const FFInputFormat ff_c93_demuxer
= {
192 .p
.long_name
= NULL_IF_CONFIG_SMALL("Interplay C93"),
193 .priv_data_size
= sizeof(C93DemuxContext
),
195 .read_header
= read_header
,
196 .read_packet
= read_packet
,