cdxl.c 7.37 KB
Newer Older
Paul B Mahol's avatar
Paul B Mahol committed
1 2 3 4
/*
 * CDXL demuxer
 * Copyright (c) 2011-2012 Paul B Mahol
 *
5
 * This file is part of FFmpeg.
Paul B Mahol's avatar
Paul B Mahol committed
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
Paul B Mahol's avatar
Paul B Mahol committed
8 9 10 11
 * 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.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Paul B Mahol's avatar
Paul B Mahol committed
13 14 15 16 17
 * 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
18
 * License along with FFmpeg; if not, write to the Free Software
Paul B Mahol's avatar
Paul B Mahol committed
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/channel_layout.h"
Paul B Mahol's avatar
Paul B Mahol committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#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;
} CDXLDemuxContext;

42 43
static int cdxl_read_probe(AVProbeData *p)
{
44
    int score = AVPROBE_SCORE_EXTENSION + 10;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    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;
}

Paul B Mahol's avatar
Paul B Mahol committed
84 85 86 87 88
static int cdxl_read_header(AVFormatContext *s)
{
    CDXLDemuxContext *cdxl = s->priv_data;
    int ret;

89
    if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
Paul B Mahol's avatar
Paul B Mahol committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        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;

    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;
108 109
    uint32_t current_size, video_size, image_size;
    uint16_t audio_size, palette_size, width, height;
Paul B Mahol's avatar
Paul B Mahol committed
110 111 112
    int64_t  pos;
    int      ret;

Paul B Mahol's avatar
Paul B Mahol committed
113
    if (url_feof(pb))
Paul B Mahol's avatar
Paul B Mahol committed
114 115 116 117 118 119 120 121 122 123 124 125
        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;
    }

    current_size = AV_RB32(&cdxl->header[2]);
126 127
    width        = AV_RB16(&cdxl->header[14]);
    height       = AV_RB16(&cdxl->header[16]);
Paul B Mahol's avatar
Paul B Mahol committed
128 129
    palette_size = AV_RB16(&cdxl->header[20]);
    audio_size   = AV_RB16(&cdxl->header[22]);
130 131
    image_size   = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
    video_size   = palette_size + image_size;
Paul B Mahol's avatar
Paul B Mahol committed
132 133 134

    if (palette_size > 512)
        return AVERROR_INVALIDDATA;
135
    if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
Paul B Mahol's avatar
Paul B Mahol committed
136 137 138 139 140 141 142 143 144 145
        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);

            st->codec->codec_type    = AVMEDIA_TYPE_AUDIO;
            st->codec->codec_tag     = 0;
146
            st->codec->codec_id      = AV_CODEC_ID_PCM_S8;
147 148 149 150 151 152 153
            if (cdxl->header[1] & 0x10) {
                st->codec->channels       = 2;
                st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
            } else {
                st->codec->channels       = 1;
                st->codec->channel_layout = AV_CH_LAYOUT_MONO;
            }
Paul B Mahol's avatar
Paul B Mahol committed
154
            st->codec->sample_rate   = cdxl->sample_rate;
155
            st->start_time           = 0;
Paul B Mahol's avatar
Paul B Mahol committed
156
            cdxl->audio_stream_index = st->index;
157
            avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
Paul B Mahol's avatar
Paul B Mahol committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        }

        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);

            st->codec->codec_type    = AVMEDIA_TYPE_VIDEO;
            st->codec->codec_tag     = 0;
175
            st->codec->codec_id      = AV_CODEC_ID_CDXL;
176 177
            st->codec->width         = width;
            st->codec->height        = height;
178
            st->start_time           = 0;
Paul B Mahol's avatar
Paul B Mahol committed
179
            cdxl->video_stream_index = st->index;
180 181 182 183
            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);
Paul B Mahol's avatar
Paul B Mahol committed
184 185 186 187 188 189 190 191 192 193
        }

        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) {
            av_free_packet(pkt);
            return ret;
        }
194
        av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
Paul B Mahol's avatar
Paul B Mahol committed
195 196 197
        pkt->stream_index  = cdxl->video_stream_index;
        pkt->flags        |= AV_PKT_FLAG_KEY;
        pkt->pos           = pos;
198
        pkt->duration      = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
Paul B Mahol's avatar
Paul B Mahol committed
199 200 201
        cdxl->read_chunk   = audio_size;
    }

202 203
    if (!cdxl->read_chunk)
        avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
Paul B Mahol's avatar
Paul B Mahol committed
204 205 206 207 208
    return ret;
}

#define OFFSET(x) offsetof(CDXLDemuxContext, x)
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 },
Paul B Mahol's avatar
Paul B Mahol committed
211 212 213 214 215 216 217 218 219 220 221 222
    { 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",
223
    .long_name      = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
Paul B Mahol's avatar
Paul B Mahol committed
224
    .priv_data_size = sizeof(CDXLDemuxContext),
225
    .read_probe     = cdxl_read_probe,
Paul B Mahol's avatar
Paul B Mahol committed
226 227 228 229 230 231
    .read_header    = cdxl_read_header,
    .read_packet    = cdxl_read_packet,
    .extensions     = "cdxl,xl",
    .flags          = AVFMT_GENERIC_INDEX,
    .priv_class     = &cdxl_demuxer_class,
};