Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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;
41 
43 {
44  CDXLDemuxContext *cdxl = s->priv_data;
45  int ret;
46 
47  if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
49  "Could not parse framerate: %s.\n", cdxl->framerate);
50  return ret;
51  }
52 
53  cdxl->read_chunk = 0;
54  cdxl->video_stream_index = -1;
55  cdxl->audio_stream_index = -1;
56 
58 
59  return 0;
60 }
61 
63 {
64  CDXLDemuxContext *cdxl = s->priv_data;
65  AVIOContext *pb = s->pb;
66  uint32_t current_size, video_size, image_size;
67  uint16_t audio_size, palette_size, width, height;
68  int64_t pos;
69  int ret;
70 
71  if (pb->eof_reached)
72  return AVERROR_EOF;
73 
74  pos = avio_tell(pb);
75  if (!cdxl->read_chunk &&
77  return AVERROR_EOF;
78  if (cdxl->header[0] != 1) {
79  av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
80  return AVERROR_INVALIDDATA;
81  }
82 
83  current_size = AV_RB32(&cdxl->header[2]);
84  width = AV_RB16(&cdxl->header[14]);
85  height = AV_RB16(&cdxl->header[16]);
86  palette_size = AV_RB16(&cdxl->header[20]);
87  audio_size = AV_RB16(&cdxl->header[22]);
88  image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
89  video_size = palette_size + image_size;
90 
91  if (palette_size > 512)
92  return AVERROR_INVALIDDATA;
93  if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
94  return AVERROR_INVALIDDATA;
95 
96  if (cdxl->read_chunk && audio_size) {
97  if (cdxl->audio_stream_index == -1) {
99  if (!st)
100  return AVERROR(ENOMEM);
101 
103  st->codec->codec_tag = 0;
105  if (cdxl->header[1] & 0x10) {
106  st->codec->channels = 2;
108  } else {
109  st->codec->channels = 1;
111  }
112  st->codec->sample_rate = cdxl->sample_rate;
113  st->start_time = 0;
114  cdxl->audio_stream_index = st->index;
115  avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
116  }
117 
118  ret = av_get_packet(pb, pkt, audio_size);
119  if (ret < 0)
120  return ret;
121  pkt->stream_index = cdxl->audio_stream_index;
122  pkt->pos = pos;
123  pkt->duration = audio_size;
124  cdxl->read_chunk = 0;
125  } else {
126  if (cdxl->video_stream_index == -1) {
128  if (!st)
129  return AVERROR(ENOMEM);
130 
132  st->codec->codec_tag = 0;
134  st->codec->width = width;
135  st->codec->height = height;
136  st->start_time = 0;
137  cdxl->video_stream_index = st->index;
138  if (cdxl->framerate)
139  avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
140  else
141  avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
142  }
143 
144  if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
145  return AVERROR(ENOMEM);
146  memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
147  ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
148  if (ret < 0) {
149  av_free_packet(pkt);
150  return ret;
151  }
153  pkt->stream_index = cdxl->video_stream_index;
154  pkt->flags |= AV_PKT_FLAG_KEY;
155  pkt->pos = pos;
156  pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
157  cdxl->read_chunk = audio_size;
158  }
159 
160  if (!cdxl->read_chunk)
161  avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
162  return ret;
163 }
164 
165 #define OFFSET(x) offsetof(CDXLDemuxContext, x)
166 static const AVOption cdxl_options[] = {
167  { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
168  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
169  { NULL },
170 };
171 
172 static const AVClass cdxl_demuxer_class = {
173  .class_name = "CDXL demuxer",
174  .item_name = av_default_item_name,
175  .option = cdxl_options,
176  .version = LIBAVUTIL_VERSION_INT,
177 };
178 
180  .name = "cdxl",
181  .long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
182  .priv_data_size = sizeof(CDXLDemuxContext),
185  .extensions = "cdxl,xl",
187  .priv_class = &cdxl_demuxer_class,
188 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:122
AVOption.
Definition: opt.h:234
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
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:2829
int num
numerator
Definition: rational.h:44
int index
stream index in AVFormatContext
Definition: avformat.h:700
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:971
int audio_stream_index
Definition: cdxl.c:39
static const AVClass cdxl_demuxer_class
Definition: cdxl.c:172
#define OFFSET(x)
Definition: cdxl.c:165
#define FFALIGN(x, a)
Definition: common.h:62
Format I/O context.
Definition: avformat.h:922
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:38
uint8_t
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:901
AVOptions.
#define AV_RB32
Definition: intreadwrite.h:130
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
uint8_t * data
Definition: avcodec.h:973
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:463
static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: cdxl.c:62
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
static const AVOption cdxl_options[]
Definition: cdxl.c:166
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
int read_chunk
Definition: cdxl.c:36
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
uint8_t header[CDXL_HEADER_SIZE]
Definition: cdxl.c:37
audio channel layout utility functions
int width
picture width / height.
Definition: avcodec.h:1229
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
AVInputFormat ff_cdxl_demuxer
Definition: cdxl.c:179
enum AVMediaType codec_type
Definition: avcodec.h:1058
char * framerate
Definition: cdxl.c:34
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
av_default_item_name
Definition: dnxhdenc.c:52
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
Describe the class of an AVClass context structure.
Definition: log.h:33
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:417
rational number numerator/denominator
Definition: rational.h:43
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
int video_stream_index
Definition: cdxl.c:38
misc parsing utilities
static int cdxl_read_header(AVFormatContext *s)
Definition: cdxl.c:42
int height
Definition: gxfenc.c:72
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
int den
denominator
Definition: rational.h:45
int sample_rate
Definition: cdxl.c:33
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1808
void * priv_data
Format private data.
Definition: avformat.h:950
#define CDXL_HEADER_SIZE
Definition: cdxl.c:29
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
#define AV_CH_LAYOUT_MONO
AVRational fps
Definition: cdxl.c:35
This structure stores compressed data.
Definition: avcodec.h:950