3 * Copyright (c) 2011-2012 Paul B Mahol
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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;
42 static int cdxl_read_header(AVFormatContext *s)
44 CDXLDemuxContext *cdxl = s->priv_data;
47 if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
48 av_log(s, AV_LOG_ERROR,
49 "Could not parse framerate: %s.\n", cdxl->framerate);
54 cdxl->video_stream_index = -1;
55 cdxl->audio_stream_index = -1;
57 s->ctx_flags |= AVFMTCTX_NOHEADER;
62 static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
64 CDXLDemuxContext *cdxl = s->priv_data;
65 AVIOContext *pb = s->pb;
66 uint32_t current_size, video_size, image_size;
67 uint16_t audio_size, palette_size, width, height;
75 if (!cdxl->read_chunk &&
76 avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
78 if (cdxl->header[0] != 1) {
79 av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
80 return AVERROR_INVALIDDATA;
83 current_size = AV_RB32(&cdxl->header[2]);
84 width = AV_RB16(&cdxl->header[14]);
85 height = AV_RB16(&cdxl->header[16]);
86 palette_size = AV_RB16(&cdxl->header[20]);
87 audio_size = AV_RB16(&cdxl->header[22]);
88 image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
89 video_size = palette_size + image_size;
91 if (palette_size > 512)
92 return AVERROR_INVALIDDATA;
93 if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
94 return AVERROR_INVALIDDATA;
96 if (cdxl->read_chunk && audio_size) {
97 if (cdxl->audio_stream_index == -1) {
98 AVStream *st = avformat_new_stream(s, NULL);
100 return AVERROR(ENOMEM);
102 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
103 st->codec->codec_tag = 0;
104 st->codec->codec_id = AV_CODEC_ID_PCM_S8;
105 if (cdxl->header[1] & 0x10) {
106 st->codec->channels = 2;
107 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
109 st->codec->channels = 1;
110 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
112 st->codec->sample_rate = cdxl->sample_rate;
114 cdxl->audio_stream_index = st->index;
115 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
118 ret = av_get_packet(pb, pkt, audio_size);
121 pkt->stream_index = cdxl->audio_stream_index;
123 pkt->duration = audio_size;
124 cdxl->read_chunk = 0;
126 if (cdxl->video_stream_index == -1) {
127 AVStream *st = avformat_new_stream(s, NULL);
129 return AVERROR(ENOMEM);
131 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
132 st->codec->codec_tag = 0;
133 st->codec->codec_id = AV_CODEC_ID_CDXL;
134 st->codec->width = width;
135 st->codec->height = height;
137 cdxl->video_stream_index = st->index;
139 avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
141 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
144 if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
145 return AVERROR(ENOMEM);
146 memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
147 ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
152 av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
153 pkt->stream_index = cdxl->video_stream_index;
154 pkt->flags |= AV_PKT_FLAG_KEY;
156 pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
157 cdxl->read_chunk = audio_size;
160 if (!cdxl->read_chunk)
161 avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
165 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
166 static const AVOption cdxl_options[] = {
167 { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
168 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
172 static const AVClass cdxl_demuxer_class = {
173 .class_name = "CDXL demuxer",
174 .item_name = av_default_item_name,
175 .option = cdxl_options,
176 .version = LIBAVUTIL_VERSION_INT,
179 AVInputFormat ff_cdxl_demuxer = {
181 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
182 .priv_data_size = sizeof(CDXLDemuxContext),
183 .read_header = cdxl_read_header,
184 .read_packet = cdxl_read_packet,
185 .extensions = "cdxl,xl",
186 .flags = AVFMT_GENERIC_INDEX,
187 .priv_class = &cdxl_demuxer_class,