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/intreadwrite.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/opt.h"
28 #define CDXL_HEADER_SIZE 32
30 typedef struct CDXLDemuxContext {
36 uint8_t header[CDXL_HEADER_SIZE];
37 int video_stream_index;
38 int audio_stream_index;
41 static int cdxl_read_header(AVFormatContext *s)
43 CDXLDemuxContext *cdxl = s->priv_data;
46 if ((ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
47 av_log(s, AV_LOG_ERROR,
48 "Could not parse framerate: %s.\n", cdxl->framerate);
53 cdxl->video_stream_index = -1;
54 cdxl->audio_stream_index = -1;
56 s->ctx_flags |= AVFMTCTX_NOHEADER;
61 static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
63 CDXLDemuxContext *cdxl = s->priv_data;
64 AVIOContext *pb = s->pb;
65 uint32_t current_size;
66 uint16_t audio_size, palette_size;
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 palette_size = AV_RB16(&cdxl->header[20]);
85 audio_size = AV_RB16(&cdxl->header[22]);
87 if (palette_size > 512)
88 return AVERROR_INVALIDDATA;
89 if (current_size < audio_size + palette_size + CDXL_HEADER_SIZE)
90 return AVERROR_INVALIDDATA;
91 video_size = current_size - audio_size - CDXL_HEADER_SIZE;
93 if (cdxl->read_chunk && audio_size) {
94 if (cdxl->audio_stream_index == -1) {
95 AVStream *st = avformat_new_stream(s, NULL);
97 return AVERROR(ENOMEM);
99 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
100 st->codec->codec_tag = 0;
101 st->codec->codec_id = CODEC_ID_PCM_S8;
102 st->codec->channels = cdxl->header[1] & 0x10 ? 2 : 1;
103 st->codec->sample_rate = cdxl->sample_rate;
104 cdxl->audio_stream_index = st->index;
105 avpriv_set_pts_info(st, 32, 1, cdxl->sample_rate);
108 ret = av_get_packet(pb, pkt, audio_size);
111 pkt->stream_index = cdxl->audio_stream_index;
113 pkt->duration = audio_size;
114 cdxl->read_chunk = 0;
116 if (cdxl->video_stream_index == -1) {
117 AVStream *st = avformat_new_stream(s, NULL);
119 return AVERROR(ENOMEM);
121 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
122 st->codec->codec_tag = 0;
123 st->codec->codec_id = CODEC_ID_CDXL;
124 st->codec->width = AV_RB16(&cdxl->header[14]);
125 st->codec->height = AV_RB16(&cdxl->header[16]);
126 cdxl->video_stream_index = st->index;
127 avpriv_set_pts_info(st, 63, cdxl->fps.den, cdxl->fps.num);
130 if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
131 return AVERROR(ENOMEM);
132 memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
133 ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
138 pkt->stream_index = cdxl->video_stream_index;
139 pkt->flags |= AV_PKT_FLAG_KEY;
141 cdxl->read_chunk = audio_size;
147 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
148 static const AVOption cdxl_options[] = {
149 { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .dbl = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
150 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "10" }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
154 static const AVClass cdxl_demuxer_class = {
155 .class_name = "CDXL demuxer",
156 .item_name = av_default_item_name,
157 .option = cdxl_options,
158 .version = LIBAVUTIL_VERSION_INT,
161 AVInputFormat ff_cdxl_demuxer = {
163 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video format"),
164 .priv_data_size = sizeof(CDXLDemuxContext),
165 .read_header = cdxl_read_header,
166 .read_packet = cdxl_read_packet,
167 .extensions = "cdxl,xl",
168 .flags = AVFMT_GENERIC_INDEX,
169 .priv_class = &cdxl_demuxer_class,