FFmpeg
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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/parseutils.h"
25 #include "libavutil/opt.h"
26 #include "avformat.h"
27 #include "internal.h"
28 
29 #define CDXL_HEADER_SIZE 32
30 
31 typedef struct CDXLDemuxContext {
32  AVClass *class;
35  int srate;
41  int64_t filesize;
42  int64_t pos;
44 
45 static int cdxl_read_probe(const AVProbeData *p)
46 {
47  int score = AVPROBE_SCORE_EXTENSION + 10;
48  const uint8_t *buf = p->buf;
49 
50  if (p->buf_size < CDXL_HEADER_SIZE)
51  return 0;
52 
53  /* check type */
54  if (buf[0] > 1)
55  return 0;
56 
57  /* reserved bytes should always be set to 0 */
58  if (AV_RL24(&buf[29]))
59  return 0;
60 
61  /* check palette size */
62  if (!AV_RN16(&buf[20]))
63  return 0;
64  if (buf[0] == 1 && AV_RB16(&buf[20]) > 512)
65  return 0;
66  if (buf[0] == 0 && AV_RB16(&buf[20]) > 768)
67  return 0;
68 
69  if (!AV_RN16(&buf[22]) && AV_RN16(&buf[24]))
70  return 0;
71 
72  if (buf[0] == 0 && (!buf[26] || !AV_RB16(&buf[24])))
73  return 0;
74 
75  /* check number of planes */
76  if (buf[19] != 6 && buf[19] != 8 && buf[19] != 24)
77  return 0;
78 
79  if (buf[18])
80  return 0;
81 
82  /* check widh and height */
83  if (AV_RB16(&buf[14]) > 640 || AV_RB16(&buf[16]) > 480 ||
84  AV_RB16(&buf[14]) == 0 || AV_RB16(&buf[16]) == 0)
85  return 0;
86 
87  /* chunk size */
88  if (AV_RB32(&buf[2]) <= AV_RB16(&buf[20]) + AV_RB16(&buf[22]) * (1 + !!(buf[1] & 0x10)) + CDXL_HEADER_SIZE)
89  return 0;
90 
91  /* previous chunk size */
92  if (AV_RN32(&buf[6]))
93  score /= 2;
94 
95  /* current frame number, usually starts from 1 */
96  if (AV_RB32(&buf[10]) != 1)
97  score /= 2;
98 
99  return score;
100 }
101 
103 {
104  CDXLDemuxContext *cdxl = s->priv_data;
105 
106  cdxl->read_chunk = 0;
107  cdxl->video_stream_index = -1;
108  cdxl->audio_stream_index = -1;
109 
110  cdxl->filesize = avio_size(s->pb);
111 
112  s->ctx_flags |= AVFMTCTX_NOHEADER;
113 
114  return 0;
115 }
116 
118 {
119  CDXLDemuxContext *cdxl = s->priv_data;
120  AVIOContext *pb = s->pb;
121  uint32_t current_size, video_size, image_size;
122  uint16_t audio_size, palette_size, width, height;
123  int channels, type, format, ret;
124 
125  if (avio_feof(pb))
126  return AVERROR_EOF;
127 
128  if (!cdxl->read_chunk) {
129  cdxl->pos = avio_tell(pb);
131  return AVERROR_EOF;
132  }
133  if (cdxl->header[0] > 1) {
134  av_log(s, AV_LOG_ERROR, "unsupported cdxl file\n");
135  return AVERROR_INVALIDDATA;
136  }
137 
138  type = cdxl->header[0];
139  channels = 1 + !!(cdxl->header[1] & 0x10);
140  format = cdxl->header[1] & 0xE0;
141  current_size = AV_RB32(&cdxl->header[2]);
142  width = AV_RB16(&cdxl->header[14]);
143  height = AV_RB16(&cdxl->header[16]);
144  palette_size = AV_RB16(&cdxl->header[20]);
145  audio_size = AV_RB16(&cdxl->header[22]) * channels;
146  cdxl->srate = AV_RB16(&cdxl->header[24]);
147  if (!cdxl->srate && audio_size)
148  cdxl->srate = cdxl->sample_rate;
149  cdxl->frate.num = cdxl->header[26];
150  cdxl->frate.den = 1;
151  if (cdxl->header[19] == 0 ||
152  FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
153  return AVERROR_INVALIDDATA;
154  if (format == 0x20)
155  image_size = width * height * cdxl->header[19] / 8;
156  else
157  image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
158  video_size = palette_size + image_size;
159 
160  if ((type == 1 && palette_size > 512) ||
161  (type == 0 && palette_size > 768))
162  return AVERROR_INVALIDDATA;
163  if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
164  return AVERROR_INVALIDDATA;
165 
166  if (!cdxl->frate.num && audio_size && cdxl->srate > 0) {
167  cdxl->frate = (AVRational){ cdxl->srate, audio_size };
168  } else if (!cdxl->frate.num) {
169  cdxl->frate = cdxl->frame_rate;
170  }
171 
172  if (cdxl->read_chunk && audio_size) {
173  if (cdxl->audio_stream_index == -1) {
175  if (!st)
176  return AVERROR(ENOMEM);
177 
179  st->codecpar->codec_tag = 0;
182  st->codecpar->sample_rate= cdxl->srate;
183  st->start_time = 0;
184  cdxl->audio_stream_index = st->index;
185  avpriv_set_pts_info(st, 64, 1, cdxl->srate);
186  if (current_size && cdxl->filesize > 0 && audio_size > 0)
187  st->duration = (cdxl->filesize / current_size) * audio_size / channels;
188  }
189 
190  ret = av_get_packet(pb, pkt, audio_size);
191  if (ret < 0)
192  return ret;
194  pkt->pos = cdxl->pos;
195  pkt->duration = audio_size / channels;
196  cdxl->read_chunk = 0;
197  } else {
198  if (cdxl->video_stream_index == -1) {
200  if (!st)
201  return AVERROR(ENOMEM);
202 
204  st->codecpar->codec_tag = 0;
206  st->codecpar->width = width;
207  st->codecpar->height = height;
208 
209  if (current_size && cdxl->filesize > 0)
210  st->nb_frames = cdxl->filesize / current_size;
211  st->start_time = 0;
212  cdxl->video_stream_index = st->index;
213  avpriv_set_pts_info(st, 64, cdxl->frate.den, cdxl->frate.num);
214  }
215 
216  if ((ret = av_new_packet(pkt, video_size + CDXL_HEADER_SIZE)) < 0)
217  return ret;
218  memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
219  ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
220  if (ret < 0) {
221  return ret;
222  }
226  pkt->pos = cdxl->pos;
227  pkt->duration = 1;
228  cdxl->read_chunk = audio_size;
229  }
230 
231  if (!cdxl->read_chunk)
232  avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
233  return ret;
234 }
235 
236 static int read_seek(AVFormatContext *s, int stream_index,
237  int64_t timestamp, int flags)
238 {
239  CDXLDemuxContext *cdxl = s->priv_data;
240 
241  cdxl->read_chunk = 0;
242 
243  return -1;
244 }
245 
246 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
247 static const AVOption cdxl_options[] = {
248  { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64=11025 }, 8000, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
249  { "frame_rate", "", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, { .str="15" }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
250  { NULL },
251 };
252 
253 static const AVClass cdxl_demuxer_class = {
254  .class_name = "CDXL demuxer",
255  .item_name = av_default_item_name,
256  .option = cdxl_options,
257  .version = LIBAVUTIL_VERSION_INT,
258 };
259 
261  .name = "cdxl",
262  .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
263  .priv_data_size = sizeof(CDXLDemuxContext),
264  .priv_class = &cdxl_demuxer_class,
268  .read_seek = read_seek,
269  .extensions = "cdxl,xl",
271 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
CDXLDemuxContext::frate
AVRational frate
Definition: cdxl.c:34
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:238
cdxl_read_header
static int cdxl_read_header(AVFormatContext *s)
Definition: cdxl.c:102
AV_RN16
#define AV_RN16(p)
Definition: intreadwrite.h:360
CDXLDemuxContext::audio_stream_index
int audio_stream_index
Definition: cdxl.c:40
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:66
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:456
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:354
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
sample_rate
sample_rate
Definition: ffmpeg_filter.c:156
CDXLDemuxContext::pos
int64_t pos
Definition: cdxl.c:42
avpriv_set_pts_info
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:763
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:112
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:510
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:482
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:998
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:656
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:97
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:455
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:128
CDXLDemuxContext::read_chunk
int read_chunk
Definition: cdxl.c:33
channels
channels
Definition: aptx.h:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1216
CDXLDemuxContext::header
uint8_t header[CDXL_HEADER_SIZE]
Definition: cdxl.c:38
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:533
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1167
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:364
parseutils.h
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:453
CDXLDemuxContext::frame_rate
AVRational frame_rate
Definition: cdxl.c:36
cdxl_read_probe
static int cdxl_read_probe(const AVProbeData *p)
Definition: cdxl.c:45
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:463
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:1000
CDXL_HEADER_SIZE
#define CDXL_HEADER_SIZE
Definition: cdxl.c:29
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
read_seek
static int read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: cdxl.c:236
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
height
#define height
CDXLDemuxContext
Definition: cdxl.c:31
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
cdxl_demuxer_class
static const AVClass cdxl_demuxer_class
Definition: cdxl.c:253
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:962
AVCodecParameters::height
int height
Definition: codec_par.h:129
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
av_get_packet
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:102
CDXLDemuxContext::srate
int srate
Definition: cdxl.c:35
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:948
cdxl_read_packet
static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: cdxl.c:117
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
OFFSET
#define OFFSET(x)
Definition: cdxl.c:246
CDXLDemuxContext::video_stream_index
int video_stream_index
Definition: cdxl.c:39
avformat.h
cdxl_options
static const AVOption cdxl_options[]
Definition: cdxl.c:247
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:956
channel_layout.h
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:643
CDXLDemuxContext::sample_rate
int sample_rate
Definition: cdxl.c:37
AVPacket::stream_index
int stream_index
Definition: packet.h:376
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:349
ff_cdxl_demuxer
const AVInputFormat ff_cdxl_demuxer
Definition: cdxl.c:260
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:394
CDXLDemuxContext::filesize
int64_t filesize
Definition: cdxl.c:41
AV_CODEC_ID_PCM_S8_PLANAR
@ AV_CODEC_ID_PCM_S8_PLANAR
Definition: codec_id.h:353
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:988
AV_CODEC_ID_CDXL
@ AV_CODEC_ID_CDXL
Definition: codec_id.h:211
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:377