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;
34  char *framerate;
40  int64_t filesize;
42 
43 static int cdxl_read_probe(const AVProbeData *p)
44 {
45  int score = AVPROBE_SCORE_EXTENSION + 10;
46 
47  if (p->buf_size < CDXL_HEADER_SIZE)
48  return 0;
49 
50  /* reserved bytes should always be set to 0 */
51  if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
52  return 0;
53 
54  /* check type */
55  if (p->buf[0] != 1)
56  return 0;
57 
58  /* check palette size */
59  if (AV_RB16(&p->buf[20]) > 512)
60  return 0;
61 
62  /* check number of planes */
63  if (p->buf[18] || !p->buf[19])
64  return 0;
65 
66  /* check widh and height */
67  if (!AV_RN16(&p->buf[14]) || !AV_RN16(&p->buf[16]))
68  return 0;
69 
70  /* chunk size */
71  if (AV_RB32(&p->buf[2]) < AV_RB16(&p->buf[22]) + AV_RB16(&p->buf[20]) + CDXL_HEADER_SIZE)
72  return 0;
73 
74  /* previous chunk size */
75  if (AV_RN32(&p->buf[6]))
76  score /= 2;
77 
78  /* current frame number, usually starts from 1 */
79  if (AV_RB16(&p->buf[12]) != 1)
80  score /= 2;
81 
82  return score;
83 }
84 
86 {
87  CDXLDemuxContext *cdxl = s->priv_data;
88  int ret;
89 
90  if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
92  "Could not parse framerate: %s.\n", cdxl->framerate);
93  return ret;
94  }
95 
96  cdxl->read_chunk = 0;
97  cdxl->video_stream_index = -1;
98  cdxl->audio_stream_index = -1;
99 
100  cdxl->filesize = avio_size(s->pb);
101 
102  s->ctx_flags |= AVFMTCTX_NOHEADER;
103 
104  return 0;
105 }
106 
108 {
109  CDXLDemuxContext *cdxl = s->priv_data;
110  AVIOContext *pb = s->pb;
111  uint32_t current_size, video_size, image_size;
112  uint16_t audio_size, palette_size, width, height;
113  int64_t pos;
114  int format, frames, ret;
115 
116  if (avio_feof(pb))
117  return AVERROR_EOF;
118 
119  pos = avio_tell(pb);
120  if (!cdxl->read_chunk &&
122  return AVERROR_EOF;
123  if (cdxl->header[0] != 1) {
124  av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
125  return AVERROR_INVALIDDATA;
126  }
127 
128  format = cdxl->header[1] & 0xE0;
129  current_size = AV_RB32(&cdxl->header[2]);
130  width = AV_RB16(&cdxl->header[14]);
131  height = AV_RB16(&cdxl->header[16]);
132  palette_size = AV_RB16(&cdxl->header[20]);
133  audio_size = AV_RB16(&cdxl->header[22]);
134  if (cdxl->header[19] == 0 ||
135  FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
136  return AVERROR_INVALIDDATA;
137  if (format == 0x20)
138  image_size = width * height * cdxl->header[19] / 8;
139  else
140  image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
141  video_size = palette_size + image_size;
142 
143  if (palette_size > 512)
144  return AVERROR_INVALIDDATA;
145  if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
146  return AVERROR_INVALIDDATA;
147 
148  if (cdxl->read_chunk && audio_size) {
149  if (cdxl->audio_stream_index == -1) {
151  if (!st)
152  return AVERROR(ENOMEM);
153 
155  st->codecpar->codec_tag = 0;
157  if (cdxl->header[1] & 0x10) {
158  st->codecpar->channels = 2;
160  } else {
161  st->codecpar->channels = 1;
163  }
164  st->codecpar->sample_rate = cdxl->sample_rate;
165  st->start_time = 0;
166  cdxl->audio_stream_index = st->index;
167  avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
168  }
169 
170  ret = av_get_packet(pb, pkt, audio_size);
171  if (ret < 0)
172  return ret;
174  pkt->pos = pos;
175  pkt->duration = audio_size;
176  cdxl->read_chunk = 0;
177  } else {
178  if (cdxl->video_stream_index == -1) {
180  if (!st)
181  return AVERROR(ENOMEM);
182 
184  st->codecpar->codec_tag = 0;
186  st->codecpar->width = width;
187  st->codecpar->height = height;
188 
189  if (audio_size + video_size && cdxl->filesize > 0) {
190  frames = cdxl->filesize / (audio_size + video_size);
191 
192  if(cdxl->framerate)
193  st->duration = frames;
194  else
195  st->duration = frames * (int64_t)audio_size;
196  }
197  st->start_time = 0;
198  cdxl->video_stream_index = st->index;
199  if (cdxl->framerate)
200  avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
201  else
202  avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
203  }
204 
205  if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
206  return AVERROR(ENOMEM);
207  memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
208  ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
209  if (ret < 0) {
211  return ret;
212  }
216  pkt->pos = pos;
217  pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
218  cdxl->read_chunk = audio_size;
219  }
220 
221  if (!cdxl->read_chunk)
222  avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
223  return ret;
224 }
225 
226 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
227 static const AVOption cdxl_options[] = {
228  { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
229  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
230  { NULL },
231 };
232 
233 static const AVClass cdxl_demuxer_class = {
234  .class_name = "CDXL demuxer",
235  .item_name = av_default_item_name,
236  .option = cdxl_options,
237  .version = LIBAVUTIL_VERSION_INT,
238 };
239 
241  .name = "cdxl",
242  .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
243  .priv_data_size = sizeof(CDXLDemuxContext),
247  .extensions = "cdxl,xl",
249  .priv_class = &cdxl_demuxer_class,
250 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
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
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
cdxl_read_header
static int cdxl_read_header(AVFormatContext *s)
Definition: cdxl.c:85
AV_RN16
#define AV_RN16(p)
Definition: intreadwrite.h:360
CDXLDemuxContext::audio_stream_index
int audio_stream_index
Definition: cdxl.c:39
AV_RN64
#define AV_RN64(p)
Definition: intreadwrite.h:368
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3961
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:336
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
sample_rate
sample_rate
Definition: ffmpeg_filter.c:191
framerate
int framerate
Definition: h264_levels.c:65
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:468
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:919
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: avcodec.h:467
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVInputFormat
Definition: avformat.h:640
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
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:86
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:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
CDXLDemuxContext::read_chunk
int read_chunk
Definition: cdxl.c:36
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
CDXLDemuxContext::header
uint8_t header[CDXL_HEADER_SIZE]
Definition: cdxl.c:37
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
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:1291
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_cdxl_demuxer
AVInputFormat ff_cdxl_demuxer
Definition: cdxl.c:240
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
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:446
cdxl_read_probe
static int cdxl_read_probe(const AVProbeData *p)
Definition: cdxl.c:43
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:456
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
CDXL_HEADER_SIZE
#define CDXL_HEADER_SIZE
Definition: cdxl.c:29
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
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:188
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4910
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:92
height
#define height
CDXLDemuxContext
Definition: cdxl.c:31
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
cdxl_demuxer_class
static const AVClass cdxl_demuxer_class
Definition: cdxl.c:233
av_parse_video_rate
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:179
AVCodecParameters::height
int height
Definition: avcodec.h:4024
CDXLDemuxContext::framerate
char * framerate
Definition: cdxl.c:34
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:277
uint8_t
uint8_t
Definition: audio_convert.c:194
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:313
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:870
cdxl_read_packet
static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: cdxl.c:107
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:72
OFFSET
#define OFFSET(x)
Definition: cdxl.c:226
CDXLDemuxContext::video_stream_index
int video_stream_index
Definition: cdxl.c:38
avformat.h
cdxl_options
static const AVOption cdxl_options[]
Definition: cdxl.c:227
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:871
channel_layout.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
CDXLDemuxContext::sample_rate
int sample_rate
Definition: cdxl.c:33
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:48
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1497
CDXLDemuxContext::filesize
int64_t filesize
Definition: cdxl.c:40
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4059
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
CDXLDemuxContext::fps
AVRational fps
Definition: cdxl.c:35
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:909
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:227
AV_CODEC_ID_CDXL
@ AV_CODEC_ID_CDXL
Definition: avcodec.h:377
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:94
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:358