3 * Copyright (c) 2011-2012 Paul B Mahol
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/parseutils.h"
25 #include "libavutil/opt.h"
29 #define CDXL_HEADER_SIZE 32
31 typedef struct CDXLDemuxContext {
36 uint8_t header[CDXL_HEADER_SIZE];
37 int video_stream_index;
38 int audio_stream_index;
42 static int cdxl_read_probe(const AVProbeData *p)
44 int score = AVPROBE_SCORE_EXTENSION + 10;
45 const uint8_t *buf = p->buf;
47 if (p->buf_size < CDXL_HEADER_SIZE)
54 /* reserved bytes should always be set to 0 */
55 if (AV_RL24(&buf[29]))
58 /* check palette size */
59 if (!AV_RN16(&buf[20]))
61 if (buf[0] == 1 && AV_RB16(&buf[20]) > 512)
63 if (buf[0] == 0 && AV_RB16(&buf[20]) > 768)
66 if (!AV_RN16(&buf[22]) && AV_RN16(&buf[24]))
69 if (buf[0] == 0 && (!buf[26] || !AV_RB16(&buf[24])))
72 /* check number of planes */
73 if (buf[19] != 6 && buf[19] != 8 && buf[19] != 24)
79 /* check widh and height */
80 if (AV_RB16(&buf[14]) > 640 || AV_RB16(&buf[16]) > 480 ||
81 AV_RB16(&buf[14]) == 0 || AV_RB16(&buf[16]) == 0)
85 if (AV_RB32(&buf[2]) <= AV_RB16(&buf[20]) + AV_RB16(&buf[22]) * (1 + !!(buf[1] & 0x10)) + CDXL_HEADER_SIZE)
88 /* previous chunk size */
92 /* current frame number, usually starts from 1 */
93 if (AV_RB32(&buf[10]) != 1)
99 static int cdxl_read_header(AVFormatContext *s)
101 CDXLDemuxContext *cdxl = s->priv_data;
103 cdxl->read_chunk = 0;
104 cdxl->video_stream_index = -1;
105 cdxl->audio_stream_index = -1;
107 cdxl->filesize = avio_size(s->pb);
109 s->ctx_flags |= AVFMTCTX_NOHEADER;
114 static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
116 CDXLDemuxContext *cdxl = s->priv_data;
117 AVIOContext *pb = s->pb;
118 uint32_t current_size, video_size, image_size;
119 uint16_t audio_size, palette_size, width, height;
121 int type, format, frames, ret;
127 if (!cdxl->read_chunk &&
128 avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
130 if (cdxl->header[0] > 1) {
131 av_log(s, AV_LOG_ERROR, "unsupported cdxl file\n");
132 return AVERROR_INVALIDDATA;
135 type = cdxl->header[0];
136 format = cdxl->header[1] & 0xE0;
137 current_size = AV_RB32(&cdxl->header[2]);
138 width = AV_RB16(&cdxl->header[14]);
139 height = AV_RB16(&cdxl->header[16]);
140 palette_size = AV_RB16(&cdxl->header[20]);
141 audio_size = AV_RB16(&cdxl->header[22]) * (1 + !!(cdxl->header[1] & 0x10));
142 cdxl->srate = AV_RB16(&cdxl->header[24]);
145 cdxl->frate = cdxl->header[26];
148 if (cdxl->header[19] == 0 ||
149 FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
150 return AVERROR_INVALIDDATA;
152 image_size = width * height * cdxl->header[19] / 8;
154 image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
155 video_size = palette_size + image_size;
157 if ((type == 1 && palette_size > 512) ||
158 (type == 0 && palette_size > 768))
159 return AVERROR_INVALIDDATA;
160 if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
161 return AVERROR_INVALIDDATA;
163 if (cdxl->read_chunk && audio_size) {
164 if (cdxl->audio_stream_index == -1) {
165 AVStream *st = avformat_new_stream(s, NULL);
167 return AVERROR(ENOMEM);
169 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
170 st->codecpar->codec_tag = 0;
171 st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
172 if (cdxl->header[1] & 0x10) {
173 st->codecpar->channels = 2;
174 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
176 st->codecpar->channels = 1;
177 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
179 st->codecpar->sample_rate= cdxl->srate;
181 cdxl->audio_stream_index = st->index;
182 avpriv_set_pts_info(st, 64, 1, cdxl->srate);
185 ret = av_get_packet(pb, pkt, audio_size);
188 pkt->stream_index = cdxl->audio_stream_index;
190 pkt->duration = audio_size;
191 cdxl->read_chunk = 0;
193 if (cdxl->video_stream_index == -1) {
194 AVStream *st = avformat_new_stream(s, NULL);
196 return AVERROR(ENOMEM);
198 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
199 st->codecpar->codec_tag = 0;
200 st->codecpar->codec_id = AV_CODEC_ID_CDXL;
201 st->codecpar->width = width;
202 st->codecpar->height = height;
204 if (audio_size + video_size && cdxl->filesize > 0) {
205 frames = cdxl->filesize / (audio_size + video_size);
208 st->duration = frames;
210 st->duration = frames * (int64_t)audio_size;
213 cdxl->video_stream_index = st->index;
215 avpriv_set_pts_info(st, 64, 1, cdxl->frate);
217 avpriv_set_pts_info(st, 64, 1, cdxl->srate);
220 if ((ret = av_new_packet(pkt, video_size + CDXL_HEADER_SIZE)) < 0)
222 memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
223 ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
227 av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
228 pkt->stream_index = cdxl->video_stream_index;
229 pkt->flags |= AV_PKT_FLAG_KEY;
231 pkt->duration = cdxl->frate ? 1 : audio_size ? audio_size : 220;
232 cdxl->read_chunk = audio_size;
235 if (!cdxl->read_chunk)
236 avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
240 AVInputFormat ff_cdxl_demuxer = {
242 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
243 .priv_data_size = sizeof(CDXLDemuxContext),
244 .read_probe = cdxl_read_probe,
245 .read_header = cdxl_read_header,
246 .read_packet = cdxl_read_packet,
247 .extensions = "cdxl,xl",
248 .flags = AVFMT_GENERIC_INDEX,