FFmpeg  2.1.1
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 
22 #include "libavutil/channel_layout.h"
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;
41 
43 {
44  int score = AVPROBE_SCORE_EXTENSION + 10;
45 
46  if (p->buf_size < CDXL_HEADER_SIZE)
47  return 0;
48 
49  /* reserved bytes should always be set to 0 */
50  if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
51  return 0;
52 
53  /* check type */
54  if (p->buf[0] != 1)
55  return 0;
56 
57  /* check palette size */
58  if (AV_RB16(&p->buf[20]) > 512)
59  return 0;
60 
61  /* check number of planes */
62  if (p->buf[18] || !p->buf[19])
63  return 0;
64 
65  /* check widh and height */
66  if (!AV_RN16(&p->buf[14]) || !AV_RN16(&p->buf[16]))
67  return 0;
68 
69  /* chunk size */
70  if (AV_RB32(&p->buf[2]) < AV_RB16(&p->buf[22]) + AV_RB16(&p->buf[20]) + CDXL_HEADER_SIZE)
71  return 0;
72 
73  /* previous chunk size */
74  if (AV_RN32(&p->buf[6]))
75  score /= 2;
76 
77  /* current frame number, usually starts from 1 */
78  if (AV_RB16(&p->buf[12]) != 1)
79  score /= 2;
80 
81  return score;
82 }
83 
85 {
86  CDXLDemuxContext *cdxl = s->priv_data;
87  int ret;
88 
89  if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
91  "Could not parse framerate: %s.\n", cdxl->framerate);
92  return ret;
93  }
94 
95  cdxl->read_chunk = 0;
96  cdxl->video_stream_index = -1;
97  cdxl->audio_stream_index = -1;
98 
100 
101  return 0;
102 }
103 
105 {
106  CDXLDemuxContext *cdxl = s->priv_data;
107  AVIOContext *pb = s->pb;
108  uint32_t current_size, video_size, image_size;
109  uint16_t audio_size, palette_size, width, height;
110  int64_t pos;
111  int ret;
112 
113  if (url_feof(pb))
114  return AVERROR_EOF;
115 
116  pos = avio_tell(pb);
117  if (!cdxl->read_chunk &&
119  return AVERROR_EOF;
120  if (cdxl->header[0] != 1) {
121  av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
122  return AVERROR_INVALIDDATA;
123  }
124 
125  current_size = AV_RB32(&cdxl->header[2]);
126  width = AV_RB16(&cdxl->header[14]);
127  height = AV_RB16(&cdxl->header[16]);
128  palette_size = AV_RB16(&cdxl->header[20]);
129  audio_size = AV_RB16(&cdxl->header[22]);
130  image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
131  video_size = palette_size + image_size;
132 
133  if (palette_size > 512)
134  return AVERROR_INVALIDDATA;
135  if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
136  return AVERROR_INVALIDDATA;
137 
138  if (cdxl->read_chunk && audio_size) {
139  if (cdxl->audio_stream_index == -1) {
140  AVStream *st = avformat_new_stream(s, NULL);
141  if (!st)
142  return AVERROR(ENOMEM);
143 
145  st->codec->codec_tag = 0;
147  if (cdxl->header[1] & 0x10) {
148  st->codec->channels = 2;
150  } else {
151  st->codec->channels = 1;
153  }
154  st->codec->sample_rate = cdxl->sample_rate;
155  st->start_time = 0;
156  cdxl->audio_stream_index = st->index;
157  avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
158  }
159 
160  ret = av_get_packet(pb, pkt, audio_size);
161  if (ret < 0)
162  return ret;
163  pkt->stream_index = cdxl->audio_stream_index;
164  pkt->pos = pos;
165  pkt->duration = audio_size;
166  cdxl->read_chunk = 0;
167  } else {
168  if (cdxl->video_stream_index == -1) {
169  AVStream *st = avformat_new_stream(s, NULL);
170  if (!st)
171  return AVERROR(ENOMEM);
172 
174  st->codec->codec_tag = 0;
176  st->codec->width = width;
177  st->codec->height = height;
178  st->start_time = 0;
179  cdxl->video_stream_index = st->index;
180  if (cdxl->framerate)
181  avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
182  else
183  avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
184  }
185 
186  if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
187  return AVERROR(ENOMEM);
188  memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
189  ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
190  if (ret < 0) {
191  av_free_packet(pkt);
192  return ret;
193  }
195  pkt->stream_index = cdxl->video_stream_index;
196  pkt->flags |= AV_PKT_FLAG_KEY;
197  pkt->pos = pos;
198  pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
199  cdxl->read_chunk = audio_size;
200  }
201 
202  if (!cdxl->read_chunk)
203  avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
204  return ret;
205 }
206 
207 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
208 static const AVOption cdxl_options[] = {
209  { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
210  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
211  { NULL },
212 };
213 
214 static const AVClass cdxl_demuxer_class = {
215  .class_name = "CDXL demuxer",
216  .item_name = av_default_item_name,
217  .option = cdxl_options,
218  .version = LIBAVUTIL_VERSION_INT,
219 };
220 
222  .name = "cdxl",
223  .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
224  .priv_data_size = sizeof(CDXLDemuxContext),
228  .extensions = "cdxl,xl",
230  .priv_class = &cdxl_demuxer_class,
231 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
AVOption.
Definition: opt.h:253
#define AV_RN32(p)
Definition: intreadwrite.h:356
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1092
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
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:3922
static int64_t audio_size
Definition: ffmpeg.c:125
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:668
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:1004
int audio_stream_index
Definition: cdxl.c:39
static const AVClass cdxl_demuxer_class
Definition: cdxl.c:214
#define OFFSET(x)
Definition: cdxl.c:207
Format I/O context.
Definition: avformat.h:968
#define AV_RB16(x)
Definition: intreadwrite.h:232
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
uint8_t
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:55
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
int duration
Duration of this packet in AVStream-&gt;time_base units, 0 if unknown.
Definition: avcodec.h:1085
static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: cdxl.c:104
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:200
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:83
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void * priv_data
Format private data.
Definition: avformat.h:988
static const AVOption cdxl_options[]
Definition: cdxl.c:208
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
int read_chunk
Definition: cdxl.c:36
int av_parse_video_rate(AVRational *rate, const char *str)
Parse str and store the detected values in *rate.
Definition: parseutils.c:168
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:355
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
#define AV_RN16(p)
Definition: intreadwrite.h:352
uint8_t header[CDXL_HEADER_SIZE]
Definition: cdxl.c:37
static int64_t video_size
Definition: ffmpeg.c:124
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:54
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:341
#define AV_RN64(p)
Definition: intreadwrite.h:360
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:592
Stream structure.
Definition: avformat.h:667
static int width
Definition: utils.c:158
sample_rate
AVInputFormat ff_cdxl_demuxer
Definition: cdxl.c:221
enum AVMediaType codec_type
Definition: avcodec.h:1154
char * framerate
Definition: cdxl.c:34
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:938
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
Describe the class of an AVClass context structure.
Definition: log.h:50
rational number numerator/denominator
Definition: rational.h:43
uint8_t * data
Definition: avcodec.h:1063
int video_stream_index
Definition: cdxl.c:38
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
static int flags
Definition: cpu.c:45
#define AVERROR_EOF
static int cdxl_read_header(AVFormatContext *s)
Definition: cdxl.c:84
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:284
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:713
Main libavformat public API header.
#define FFALIGN(x, a)
Definition: avcodec.h:930
int den
denominator
Definition: rational.h:45
#define AV_RB32(x)
Definition: intreadwrite.h:258
#define AVERROR_INVALIDDATA
int sample_rate
Definition: cdxl.c:33
int channels
number of audio channels
Definition: avcodec.h:1874
#define AVERROR(e)
#define CDXL_HEADER_SIZE
Definition: cdxl.c:29
static AVPacket pkt
Definition: demuxing.c:52
int stream_index
Definition: avcodec.h:1065
#define AV_CH_LAYOUT_MONO
AVRational fps
Definition: cdxl.c:35
This structure stores compressed data.
Definition: avcodec.h:1040
static int cdxl_read_probe(AVProbeData *p)
Definition: cdxl.c:42