00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/parseutils.h"
00024 #include "libavutil/opt.h"
00025 #include "avformat.h"
00026 #include "internal.h"
00027
00028 #define CDXL_HEADER_SIZE 32
00029
00030 typedef struct CDXLDemuxContext {
00031 AVClass *class;
00032 int sample_rate;
00033 char *framerate;
00034 AVRational fps;
00035 int read_chunk;
00036 uint8_t header[CDXL_HEADER_SIZE];
00037 int video_stream_index;
00038 int audio_stream_index;
00039 } CDXLDemuxContext;
00040
00041 static int cdxl_read_header(AVFormatContext *s)
00042 {
00043 CDXLDemuxContext *cdxl = s->priv_data;
00044 int ret;
00045
00046 if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
00047 av_log(s, AV_LOG_ERROR,
00048 "Could not parse framerate: %s.\n", cdxl->framerate);
00049 return ret;
00050 }
00051
00052 cdxl->read_chunk = 0;
00053 cdxl->video_stream_index = -1;
00054 cdxl->audio_stream_index = -1;
00055
00056 s->ctx_flags |= AVFMTCTX_NOHEADER;
00057
00058 return 0;
00059 }
00060
00061 static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
00062 {
00063 CDXLDemuxContext *cdxl = s->priv_data;
00064 AVIOContext *pb = s->pb;
00065 uint32_t current_size, video_size, image_size;
00066 uint16_t audio_size, palette_size, width, height;
00067 int64_t pos;
00068 int ret;
00069
00070 if (pb->eof_reached)
00071 return AVERROR_EOF;
00072
00073 pos = avio_tell(pb);
00074 if (!cdxl->read_chunk &&
00075 avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
00076 return AVERROR_EOF;
00077 if (cdxl->header[0] != 1) {
00078 av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
00079 return AVERROR_INVALIDDATA;
00080 }
00081
00082 current_size = AV_RB32(&cdxl->header[2]);
00083 width = AV_RB16(&cdxl->header[14]);
00084 height = AV_RB16(&cdxl->header[16]);
00085 palette_size = AV_RB16(&cdxl->header[20]);
00086 audio_size = AV_RB16(&cdxl->header[22]);
00087 image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
00088 video_size = palette_size + image_size;
00089
00090 if (palette_size > 512)
00091 return AVERROR_INVALIDDATA;
00092 if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
00093 return AVERROR_INVALIDDATA;
00094
00095 if (cdxl->read_chunk && audio_size) {
00096 if (cdxl->audio_stream_index == -1) {
00097 AVStream *st = avformat_new_stream(s, NULL);
00098 if (!st)
00099 return AVERROR(ENOMEM);
00100
00101 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00102 st->codec->codec_tag = 0;
00103 st->codec->codec_id = CODEC_ID_PCM_S8;
00104 st->codec->channels = cdxl->header[1] & 0x10 ? 2 : 1;
00105 st->codec->sample_rate = cdxl->sample_rate;
00106 st->start_time = 0;
00107 cdxl->audio_stream_index = st->index;
00108 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
00109 }
00110
00111 ret = av_get_packet(pb, pkt, audio_size);
00112 if (ret < 0)
00113 return ret;
00114 pkt->stream_index = cdxl->audio_stream_index;
00115 pkt->pos = pos;
00116 pkt->duration = audio_size;
00117 cdxl->read_chunk = 0;
00118 } else {
00119 if (cdxl->video_stream_index == -1) {
00120 AVStream *st = avformat_new_stream(s, NULL);
00121 if (!st)
00122 return AVERROR(ENOMEM);
00123
00124 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00125 st->codec->codec_tag = 0;
00126 st->codec->codec_id = CODEC_ID_CDXL;
00127 st->codec->width = width;
00128 st->codec->height = height;
00129 st->start_time = 0;
00130 cdxl->video_stream_index = st->index;
00131 if (cdxl->framerate)
00132 avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
00133 else
00134 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
00135 }
00136
00137 if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
00138 return AVERROR(ENOMEM);
00139 memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
00140 ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
00141 if (ret < 0) {
00142 av_free_packet(pkt);
00143 return ret;
00144 }
00145 av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
00146 pkt->stream_index = cdxl->video_stream_index;
00147 pkt->flags |= AV_PKT_FLAG_KEY;
00148 pkt->pos = pos;
00149 pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
00150 cdxl->read_chunk = audio_size;
00151 }
00152
00153 if (!cdxl->read_chunk)
00154 avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
00155 return ret;
00156 }
00157
00158 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
00159 static const AVOption cdxl_options[] = {
00160 { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .dbl = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00161 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
00162 { NULL },
00163 };
00164
00165 static const AVClass cdxl_demuxer_class = {
00166 .class_name = "CDXL demuxer",
00167 .item_name = av_default_item_name,
00168 .option = cdxl_options,
00169 .version = LIBAVUTIL_VERSION_INT,
00170 };
00171
00172 AVInputFormat ff_cdxl_demuxer = {
00173 .name = "cdxl",
00174 .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video format"),
00175 .priv_data_size = sizeof(CDXLDemuxContext),
00176 .read_header = cdxl_read_header,
00177 .read_packet = cdxl_read_packet,
00178 .extensions = "cdxl,xl",
00179 .flags = AVFMT_GENERIC_INDEX,
00180 .priv_class = &cdxl_demuxer_class,
00181 };