FFmpeg
Loading...
Searching...
No Matches
cdxl.c
Go to the documentation of this file.
1/*
2 * CDXL demuxer
3 * Copyright (c) 2011-2012 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
25#include "libavutil/opt.h"
26#include "avformat.h"
27#include "demux.h"
28#include "internal.h"
29
30#define CDXL_HEADER_SIZE 32
31
45
46static int cdxl_read_probe(const AVProbeData *p)
47{
48 int score = AVPROBE_SCORE_EXTENSION + 10;
49 const uint8_t *buf = p->buf;
50
51 if (p->buf_size < CDXL_HEADER_SIZE)
52 return 0;
53
54 /* check type */
55 if (buf[0] > 1)
56 return 0;
57
58 /* reserved bytes should always be set to 0 */
59 if (AV_RL24(&buf[29]))
60 return 0;
61
62 /* check palette size */
63 if (!AV_RN16(&buf[20]))
64 return 0;
65 if (buf[0] == 1 && AV_RB16(&buf[20]) > 512)
66 return 0;
67 if (buf[0] == 0 && AV_RB16(&buf[20]) > 768)
68 return 0;
69
70 if (!AV_RN16(&buf[22]) && AV_RN16(&buf[24]))
71 return 0;
72
73 if (buf[0] == 0 && (!buf[26] || !AV_RB16(&buf[24])))
74 return 0;
75
76 /* check number of planes */
77 if (buf[19] != 6 && buf[19] != 8 && buf[19] != 24)
78 return 0;
79
80 if (buf[18])
81 return 0;
82
83 /* check widh and height */
84 if (AV_RB16(&buf[14]) > 640 || AV_RB16(&buf[16]) > 480 ||
85 AV_RB16(&buf[14]) == 0 || AV_RB16(&buf[16]) == 0)
86 return 0;
87
88 /* chunk size */
89 if (AV_RB32(&buf[2]) <= AV_RB16(&buf[20]) + AV_RB16(&buf[22]) * (1 + !!(buf[1] & 0x10)) + CDXL_HEADER_SIZE)
90 return 0;
91
92 /* previous chunk size */
93 if (AV_RN32(&buf[6]))
94 score /= 2;
95
96 /* current frame number, usually starts from 1 */
97 if (AV_RB32(&buf[10]) != 1)
98 score /= 2;
99
100 return score;
101}
102
104{
105 CDXLDemuxContext *cdxl = s->priv_data;
106
107 cdxl->read_chunk = 0;
108 cdxl->video_stream_index = -1;
109 cdxl->audio_stream_index = -1;
110
111 cdxl->filesize = avio_size(s->pb);
112
113 s->ctx_flags |= AVFMTCTX_NOHEADER;
114
115 return 0;
116}
117
119{
120 CDXLDemuxContext *cdxl = s->priv_data;
121 AVIOContext *pb = s->pb;
122 uint32_t current_size, video_size, image_size;
123 uint16_t audio_size, palette_size, width, height;
124 int channels, type, format, ret;
125
126 if (avio_feof(pb))
127 return AVERROR_EOF;
128
129 if (!cdxl->read_chunk) {
130 cdxl->pos = avio_tell(pb);
132 return AVERROR_EOF;
133 }
134 if (cdxl->header[0] > 1) {
135 av_log(s, AV_LOG_ERROR, "unsupported cdxl file\n");
136 return AVERROR_INVALIDDATA;
137 }
138
139 type = cdxl->header[0];
140 channels = 1 + !!(cdxl->header[1] & 0x10);
141 format = cdxl->header[1] & 0xE0;
142 current_size = AV_RB32(&cdxl->header[2]);
143 width = AV_RB16(&cdxl->header[14]);
144 height = AV_RB16(&cdxl->header[16]);
145 palette_size = AV_RB16(&cdxl->header[20]);
146 audio_size = AV_RB16(&cdxl->header[22]) * channels;
147 cdxl->srate = AV_RB16(&cdxl->header[24]);
148 if (!cdxl->srate && audio_size)
149 cdxl->srate = cdxl->sample_rate;
150 cdxl->frate.num = cdxl->header[26];
151 cdxl->frate.den = 1;
152 if (cdxl->header[19] == 0 ||
153 FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
154 return AVERROR_INVALIDDATA;
155 if (format == 0x20)
156 image_size = width * height * cdxl->header[19] / 8;
157 else
158 image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
159 video_size = palette_size + image_size;
160
161 if ((type == 1 && palette_size > 512) ||
162 (type == 0 && palette_size > 768))
163 return AVERROR_INVALIDDATA;
164 if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
165 return AVERROR_INVALIDDATA;
166
167 if (!cdxl->frate.num && audio_size && cdxl->srate > 0) {
168 cdxl->frate = (AVRational){ cdxl->srate, audio_size };
169 } else if (!cdxl->frate.num) {
170 cdxl->frate = cdxl->frame_rate;
171 }
172
173 if (cdxl->read_chunk && audio_size) {
174 if (cdxl->audio_stream_index == -1) {
176 if (!st)
177 return AVERROR(ENOMEM);
178
180 st->codecpar->codec_tag = 0;
183 st->codecpar->sample_rate= cdxl->srate;
184 st->start_time = 0;
185 cdxl->audio_stream_index = st->index;
186 avpriv_set_pts_info(st, 64, 1, cdxl->srate);
187 if (current_size && cdxl->filesize > 0 && audio_size > 0)
188 st->duration = (cdxl->filesize / current_size) * audio_size / channels;
189 }
190
191 ret = av_get_packet(pb, pkt, audio_size);
192 if (ret < 0)
193 return ret;
194 pkt->stream_index = cdxl->audio_stream_index;
195 pkt->pos = cdxl->pos;
196 pkt->duration = audio_size / channels;
197 cdxl->read_chunk = 0;
198 } else {
199 if (cdxl->video_stream_index == -1) {
201 if (!st)
202 return AVERROR(ENOMEM);
203
205 st->codecpar->codec_tag = 0;
207 st->codecpar->width = width;
208 st->codecpar->height = height;
209
210 if (current_size && cdxl->filesize > 0)
211 st->nb_frames = cdxl->filesize / current_size;
212 st->start_time = 0;
213 cdxl->video_stream_index = st->index;
214 avpriv_set_pts_info(st, 64, cdxl->frate.den, cdxl->frate.num);
215 }
216
217 if ((ret = av_new_packet(pkt, video_size + CDXL_HEADER_SIZE)) < 0)
218 return ret;
219 memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
220 ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
221 if (ret < 0) {
222 return ret;
223 }
225 pkt->stream_index = cdxl->video_stream_index;
226 pkt->flags |= AV_PKT_FLAG_KEY;
227 pkt->pos = cdxl->pos;
228 pkt->duration = 1;
229 cdxl->read_chunk = audio_size;
230 }
231
232 if (!cdxl->read_chunk)
233 avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
234 return ret;
235}
236
237static int read_seek(AVFormatContext *s, int stream_index,
238 int64_t timestamp, int flags)
239{
240 CDXLDemuxContext *cdxl = s->priv_data;
241
242 cdxl->read_chunk = 0;
243
244 return -1;
245}
246
247#define OFFSET(x) offsetof(CDXLDemuxContext, x)
248static const AVOption cdxl_options[] = {
249 { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64=11025 }, 8000, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
250 { "frame_rate", "", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, { .str="15" }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
251 { NULL },
252};
253
255 .class_name = "CDXL demuxer",
256 .item_name = av_default_item_name,
257 .option = cdxl_options,
258 .version = LIBAVUTIL_VERSION_INT,
259};
260
262 .p.name = "cdxl",
263 .p.long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
264 .p.priv_class = &cdxl_demuxer_class,
265 .p.extensions = "cdxl,xl",
266 .p.flags = AVFMT_GENERIC_INDEX,
267 .priv_data_size = sizeof(CDXLDemuxContext),
272};
static const char *const format[]
Definition af_aiir.c:444
const FFInputFormat ff_cdxl_demuxer
Definition cdxl.c:261
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_CDXL
Definition codec_id.h:208
@ AV_CODEC_ID_PCM_S8_PLANAR
Definition codec_id.h:357
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
cl_device_type type
#define AV_RL24(x)
#define AV_RN32(p)
#define AV_RB32(p)
#define AV_RN16(p)
#define AV_RB16(p)
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition cdxl.c:237
static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition cdxl.c:118
static int cdxl_read_header(AVFormatContext *s)
Definition cdxl.c:103
static int cdxl_read_probe(const AVProbeData *p)
Definition cdxl.c:46
#define CDXL_HEADER_SIZE
Definition cdxl.c:30
static const AVOption cdxl_options[]
Definition cdxl.c:248
#define OFFSET(x)
Definition cdxl.c:247
static const AVClass cdxl_demuxer_class
Definition cdxl.c:254
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFALIGN(x, a)
Definition macros.h:78
AVOptions.
misc parsing utilities
Describe the class of an AVClass context structure.
Definition log.h:76
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int sample_rate
Definition cdxl.c:38
int audio_stream_index
Definition cdxl.c:41
int video_stream_index
Definition cdxl.c:40
uint8_t header[CDXL_HEADER_SIZE]
Definition cdxl.c:39
AVRational frame_rate
Definition cdxl.c:37
int read_chunk
Definition cdxl.c:34
AVRational frate
Definition cdxl.c:35
int64_t filesize
Definition cdxl.c:42
int64_t pos
Definition cdxl.c:43
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89