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 {
37 uint8_t header[CDXL_HEADER_SIZE];
38 int video_stream_index;
39 int audio_stream_index;
43 static int cdxl_read_probe(AVProbeData *p)
45 int score = AVPROBE_SCORE_EXTENSION + 10;
47 if (p->buf_size < CDXL_HEADER_SIZE)
50 /* reserved bytes should always be set to 0 */
51 if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
58 /* check palette size */
59 if (AV_RB16(&p->buf[20]) > 512)
62 /* check number of planes */
63 if (p->buf[18] || !p->buf[19])
66 /* check widh and height */
67 if (!AV_RN16(&p->buf[14]) || !AV_RN16(&p->buf[16]))
71 if (AV_RB32(&p->buf[2]) < AV_RB16(&p->buf[22]) + AV_RB16(&p->buf[20]) + CDXL_HEADER_SIZE)
74 /* previous chunk size */
75 if (AV_RN32(&p->buf[6]))
78 /* current frame number, usually starts from 1 */
79 if (AV_RB16(&p->buf[12]) != 1)
85 static int cdxl_read_header(AVFormatContext *s)
87 CDXLDemuxContext *cdxl = s->priv_data;
90 if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
91 av_log(s, AV_LOG_ERROR,
92 "Could not parse framerate: %s.\n", cdxl->framerate);
97 cdxl->video_stream_index = -1;
98 cdxl->audio_stream_index = -1;
100 cdxl->filesize = avio_size(s->pb);
102 s->ctx_flags |= AVFMTCTX_NOHEADER;
107 static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
109 CDXLDemuxContext *cdxl = s->priv_data;
110 AVIOContext *pb = s->pb;
111 uint32_t current_size, video_size, image_size;
112 uint16_t audio_size, palette_size, width, height;
120 if (!cdxl->read_chunk &&
121 avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
123 if (cdxl->header[0] != 1) {
124 av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
125 return AVERROR_INVALIDDATA;
128 current_size = AV_RB32(&cdxl->header[2]);
129 width = AV_RB16(&cdxl->header[14]);
130 height = AV_RB16(&cdxl->header[16]);
131 palette_size = AV_RB16(&cdxl->header[20]);
132 audio_size = AV_RB16(&cdxl->header[22]);
133 image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
134 video_size = palette_size + image_size;
136 if (palette_size > 512)
137 return AVERROR_INVALIDDATA;
138 if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
139 return AVERROR_INVALIDDATA;
141 if (cdxl->read_chunk && audio_size) {
142 if (cdxl->audio_stream_index == -1) {
143 AVStream *st = avformat_new_stream(s, NULL);
145 return AVERROR(ENOMEM);
147 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
148 st->codec->codec_tag = 0;
149 st->codec->codec_id = AV_CODEC_ID_PCM_S8;
150 if (cdxl->header[1] & 0x10) {
151 st->codec->channels = 2;
152 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
154 st->codec->channels = 1;
155 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
157 st->codec->sample_rate = cdxl->sample_rate;
159 cdxl->audio_stream_index = st->index;
160 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
163 ret = av_get_packet(pb, pkt, audio_size);
166 pkt->stream_index = cdxl->audio_stream_index;
168 pkt->duration = audio_size;
169 cdxl->read_chunk = 0;
171 if (cdxl->video_stream_index == -1) {
172 AVStream *st = avformat_new_stream(s, NULL);
174 return AVERROR(ENOMEM);
176 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
177 st->codec->codec_tag = 0;
178 st->codec->codec_id = AV_CODEC_ID_CDXL;
179 st->codec->width = width;
180 st->codec->height = height;
182 if (audio_size + video_size && cdxl->filesize > 0) {
183 frames = cdxl->filesize / (audio_size + video_size);
186 st->duration = frames;
188 st->duration = frames * audio_size;
191 cdxl->video_stream_index = st->index;
193 avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
195 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
198 if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
199 return AVERROR(ENOMEM);
200 memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
201 ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
206 av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
207 pkt->stream_index = cdxl->video_stream_index;
208 pkt->flags |= AV_PKT_FLAG_KEY;
210 pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
211 cdxl->read_chunk = audio_size;
214 if (!cdxl->read_chunk)
215 avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
219 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
220 static const AVOption cdxl_options[] = {
221 { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
222 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
226 static const AVClass cdxl_demuxer_class = {
227 .class_name = "CDXL demuxer",
228 .item_name = av_default_item_name,
229 .option = cdxl_options,
230 .version = LIBAVUTIL_VERSION_INT,
233 AVInputFormat ff_cdxl_demuxer = {
235 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
236 .priv_data_size = sizeof(CDXLDemuxContext),
237 .read_probe = cdxl_read_probe,
238 .read_header = cdxl_read_header,
239 .read_packet = cdxl_read_packet,
240 .extensions = "cdxl,xl",
241 .flags = AVFMT_GENERIC_INDEX,
242 .priv_class = &cdxl_demuxer_class,