2012-02-14 17:36:20 +00:00
|
|
|
/*
|
|
|
|
* CDXL demuxer
|
|
|
|
* Copyright (c) 2011-2012 Paul B Mahol
|
|
|
|
*
|
2012-06-29 01:55:56 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-02-14 17:36:20 +00:00
|
|
|
*
|
2012-06-29 01:55:56 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-02-14 17:36:20 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-06-29 01:55:56 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-02-14 17:36:20 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-06-29 01:55:56 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-02-14 17:36:20 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-04-07 16:49:11 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-02-14 17:36:20 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#define CDXL_HEADER_SIZE 32
|
|
|
|
|
|
|
|
typedef struct CDXLDemuxContext {
|
|
|
|
AVClass *class;
|
|
|
|
int sample_rate;
|
|
|
|
char *framerate;
|
|
|
|
AVRational fps;
|
|
|
|
int read_chunk;
|
|
|
|
uint8_t header[CDXL_HEADER_SIZE];
|
|
|
|
int video_stream_index;
|
|
|
|
int audio_stream_index;
|
2012-12-17 11:55:34 +00:00
|
|
|
int64_t filesize;
|
2012-02-14 17:36:20 +00:00
|
|
|
} CDXLDemuxContext;
|
|
|
|
|
2012-11-04 16:16:01 +00:00
|
|
|
static int cdxl_read_probe(AVProbeData *p)
|
|
|
|
{
|
2013-05-05 11:16:53 +00:00
|
|
|
int score = AVPROBE_SCORE_EXTENSION + 10;
|
2012-11-04 16:16:01 +00:00
|
|
|
|
|
|
|
if (p->buf_size < CDXL_HEADER_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* reserved bytes should always be set to 0 */
|
|
|
|
if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check type */
|
|
|
|
if (p->buf[0] != 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check palette size */
|
|
|
|
if (AV_RB16(&p->buf[20]) > 512)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check number of planes */
|
|
|
|
if (p->buf[18] || !p->buf[19])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check widh and height */
|
|
|
|
if (!AV_RN16(&p->buf[14]) || !AV_RN16(&p->buf[16]))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* chunk size */
|
|
|
|
if (AV_RB32(&p->buf[2]) < AV_RB16(&p->buf[22]) + AV_RB16(&p->buf[20]) + CDXL_HEADER_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* previous chunk size */
|
|
|
|
if (AV_RN32(&p->buf[6]))
|
|
|
|
score /= 2;
|
|
|
|
|
|
|
|
/* current frame number, usually starts from 1 */
|
|
|
|
if (AV_RB16(&p->buf[12]) != 1)
|
|
|
|
score /= 2;
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:36:20 +00:00
|
|
|
static int cdxl_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
CDXLDemuxContext *cdxl = s->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
2012-02-21 18:15:51 +00:00
|
|
|
if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
|
2012-02-14 17:36:20 +00:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Could not parse framerate: %s.\n", cdxl->framerate);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
cdxl->read_chunk = 0;
|
|
|
|
cdxl->video_stream_index = -1;
|
|
|
|
cdxl->audio_stream_index = -1;
|
|
|
|
|
2012-12-17 11:55:34 +00:00
|
|
|
cdxl->filesize = avio_size(s->pb);
|
|
|
|
|
2012-02-14 17:36:20 +00:00
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
CDXLDemuxContext *cdxl = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
2012-02-16 21:48:57 +00:00
|
|
|
uint32_t current_size, video_size, image_size;
|
|
|
|
uint16_t audio_size, palette_size, width, height;
|
2012-02-14 17:36:20 +00:00
|
|
|
int64_t pos;
|
2016-02-18 22:47:39 +00:00
|
|
|
int format, frames, ret;
|
2012-02-14 17:36:20 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(pb))
|
2012-02-14 17:36:20 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
pos = avio_tell(pb);
|
|
|
|
if (!cdxl->read_chunk &&
|
|
|
|
avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
if (cdxl->header[0] != 1) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:47:39 +00:00
|
|
|
format = cdxl->header[1] & 0xE0;
|
2012-02-14 17:36:20 +00:00
|
|
|
current_size = AV_RB32(&cdxl->header[2]);
|
2012-02-16 21:48:57 +00:00
|
|
|
width = AV_RB16(&cdxl->header[14]);
|
|
|
|
height = AV_RB16(&cdxl->header[16]);
|
2012-02-14 17:36:20 +00:00
|
|
|
palette_size = AV_RB16(&cdxl->header[20]);
|
|
|
|
audio_size = AV_RB16(&cdxl->header[22]);
|
2014-12-31 20:41:46 +00:00
|
|
|
if (FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2016-02-18 22:47:39 +00:00
|
|
|
if (format == 0x20)
|
|
|
|
image_size = width * height * cdxl->header[19] / 8;
|
|
|
|
else
|
|
|
|
image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
|
2012-02-16 21:48:57 +00:00
|
|
|
video_size = palette_size + image_size;
|
2012-02-14 17:36:20 +00:00
|
|
|
|
|
|
|
if (palette_size > 512)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-02-16 21:48:57 +00:00
|
|
|
if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
|
2012-02-14 17:36:20 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if (cdxl->read_chunk && audio_size) {
|
|
|
|
if (cdxl->audio_stream_index == -1) {
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_tag = 0;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
|
2012-04-07 16:49:11 +00:00
|
|
|
if (cdxl->header[1] & 0x10) {
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->channels = 2;
|
|
|
|
st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
|
2012-04-07 16:49:11 +00:00
|
|
|
} else {
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->channels = 1;
|
|
|
|
st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
|
2012-04-07 16:49:11 +00:00
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->sample_rate = cdxl->sample_rate;
|
2012-02-21 18:15:51 +00:00
|
|
|
st->start_time = 0;
|
2012-02-14 17:36:20 +00:00
|
|
|
cdxl->audio_stream_index = st->index;
|
2012-02-21 18:15:51 +00:00
|
|
|
avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
|
2012-02-14 17:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_get_packet(pb, pkt, audio_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
pkt->stream_index = cdxl->audio_stream_index;
|
|
|
|
pkt->pos = pos;
|
|
|
|
pkt->duration = audio_size;
|
|
|
|
cdxl->read_chunk = 0;
|
|
|
|
} else {
|
|
|
|
if (cdxl->video_stream_index == -1) {
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_tag = 0;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_CDXL;
|
|
|
|
st->codecpar->width = width;
|
|
|
|
st->codecpar->height = height;
|
2012-12-17 11:55:34 +00:00
|
|
|
|
|
|
|
if (audio_size + video_size && cdxl->filesize > 0) {
|
|
|
|
frames = cdxl->filesize / (audio_size + video_size);
|
|
|
|
|
|
|
|
if(cdxl->framerate)
|
|
|
|
st->duration = frames;
|
|
|
|
else
|
2014-12-29 19:53:56 +00:00
|
|
|
st->duration = frames * (int64_t)audio_size;
|
2012-12-17 11:55:34 +00:00
|
|
|
}
|
2012-02-21 18:15:51 +00:00
|
|
|
st->start_time = 0;
|
2012-02-14 17:36:20 +00:00
|
|
|
cdxl->video_stream_index = st->index;
|
2012-02-21 18:15:51 +00:00
|
|
|
if (cdxl->framerate)
|
|
|
|
avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
|
|
|
|
else
|
|
|
|
avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
|
2012-02-14 17:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
|
|
|
|
ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
|
|
|
|
if (ret < 0) {
|
2015-10-23 09:11:31 +00:00
|
|
|
av_packet_unref(pkt);
|
2012-02-14 17:36:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-02-26 16:08:38 +00:00
|
|
|
av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
|
2012-02-14 17:36:20 +00:00
|
|
|
pkt->stream_index = cdxl->video_stream_index;
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
pkt->pos = pos;
|
2012-02-21 18:15:51 +00:00
|
|
|
pkt->duration = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
|
2012-02-14 17:36:20 +00:00
|
|
|
cdxl->read_chunk = audio_size;
|
|
|
|
}
|
|
|
|
|
2012-02-16 21:48:57 +00:00
|
|
|
if (!cdxl->read_chunk)
|
|
|
|
avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
|
2012-02-14 17:36:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(CDXLDemuxContext, x)
|
|
|
|
static const AVOption cdxl_options[] = {
|
2012-08-31 10:22:31 +00:00
|
|
|
{ "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT, { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
|
2012-02-21 18:15:51 +00:00
|
|
|
{ "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
|
2012-02-14 17:36:20 +00:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass cdxl_demuxer_class = {
|
|
|
|
.class_name = "CDXL demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = cdxl_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVInputFormat ff_cdxl_demuxer = {
|
|
|
|
.name = "cdxl",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
|
2012-02-14 17:36:20 +00:00
|
|
|
.priv_data_size = sizeof(CDXLDemuxContext),
|
2012-11-04 16:16:01 +00:00
|
|
|
.read_probe = cdxl_read_probe,
|
2012-02-14 17:36:20 +00:00
|
|
|
.read_header = cdxl_read_header,
|
|
|
|
.read_packet = cdxl_read_packet,
|
|
|
|
.extensions = "cdxl,xl",
|
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
.priv_class = &cdxl_demuxer_class,
|
|
|
|
};
|