jvdec.c 7.82 KB
Newer Older
Peter Ross's avatar
Peter Ross committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Bitmap Brothers JV demuxer
 * Copyright (c) 2005, 2011 Peter Ross <pross@xvid.org>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Bitmap Brothers JV demuxer
 * @author Peter Ross <pross@xvid.org>
 */

28
#include "libavutil/channel_layout.h"
Peter Ross's avatar
Peter Ross committed
29
#include "libavutil/intreadwrite.h"
30

Peter Ross's avatar
Peter Ross committed
31
#include "avformat.h"
32
#include "internal.h"
Peter Ross's avatar
Peter Ross committed
33

34 35
#define JV_PREAMBLE_SIZE 5

36
typedef struct JVFrame {
37 38 39 40
    int audio_size;    /**< audio packet size (bytes) */
    int video_size;    /**< video packet size (bytes) */
    int palette_size;  /**< palette size (bytes) */
    int video_type;    /**< per-frame video compression type */
Peter Ross's avatar
Peter Ross committed
41 42
} JVFrame;

43
typedef struct JVDemuxContext {
Peter Ross's avatar
Peter Ross committed
44 45 46 47 48 49 50 51 52 53 54 55 56
    JVFrame *frames;
    enum {
        JV_AUDIO = 0,
        JV_VIDEO,
        JV_PADDING
    } state;
    int64_t pts;
} JVDemuxContext;

#define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."

static int read_probe(AVProbeData *pd)
{
57
    if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
58
        !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
Peter Ross's avatar
Peter Ross committed
59 60 61 62
        return AVPROBE_SCORE_MAX;
    return 0;
}

63 64 65 66 67 68 69 70 71
static int read_close(AVFormatContext *s)
{
    JVDemuxContext *jv = s->priv_data;

    av_freep(&jv->frames);

    return 0;
}

72
static int read_header(AVFormatContext *s)
Peter Ross's avatar
Peter Ross committed
73 74 75 76 77 78 79 80 81 82
{
    JVDemuxContext *jv = s->priv_data;
    AVIOContext *pb = s->pb;
    AVStream *vst, *ast;
    int64_t audio_pts = 0;
    int64_t offset;
    int i;

    avio_skip(pb, 80);

83
    ast = avformat_new_stream(s, NULL);
84
    vst = avformat_new_stream(s, NULL);
Peter Ross's avatar
Peter Ross committed
85 86 87
    if (!ast || !vst)
        return AVERROR(ENOMEM);

88 89 90 91 92
    vst->codecpar->codec_type  = AVMEDIA_TYPE_VIDEO;
    vst->codecpar->codec_id    = AV_CODEC_ID_JV;
    vst->codecpar->codec_tag   = 0; /* no fourcc */
    vst->codecpar->width       = avio_rl16(pb);
    vst->codecpar->height      = avio_rl16(pb);
93
    vst->duration           =
Peter Ross's avatar
Peter Ross committed
94 95
    vst->nb_frames          =
    ast->nb_index_entries   = avio_rl16(pb);
96
    avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
Peter Ross's avatar
Peter Ross committed
97 98 99

    avio_skip(pb, 4);

100 101 102 103 104 105 106
    ast->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
    ast->codecpar->codec_id       = AV_CODEC_ID_PCM_U8;
    ast->codecpar->codec_tag      = 0; /* no fourcc */
    ast->codecpar->sample_rate    = avio_rl16(pb);
    ast->codecpar->channels       = 1;
    ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
    avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
Peter Ross's avatar
Peter Ross committed
107 108 109

    avio_skip(pb, 10);

110 111
    ast->index_entries = av_malloc(ast->nb_index_entries *
                                   sizeof(*ast->index_entries));
Peter Ross's avatar
Peter Ross committed
112 113 114 115 116 117 118 119
    if (!ast->index_entries)
        return AVERROR(ENOMEM);

    jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
    if (!jv->frames)
        return AVERROR(ENOMEM);

    offset = 0x68 + ast->nb_index_entries * 16;
120
    for (i = 0; i < ast->nb_index_entries; i++) {
Peter Ross's avatar
Peter Ross committed
121 122 123 124
        AVIndexEntry *e   = ast->index_entries + i;
        JVFrame      *jvf = jv->frames + i;

        /* total frame size including audio, video, palette data and padding */
125 126 127 128
        e->size      = avio_rl32(pb);
        e->timestamp = i;
        e->pos       = offset;
        offset      += e->size;
Peter Ross's avatar
Peter Ross committed
129

130 131
        jvf->audio_size   = avio_rl32(pb);
        jvf->video_size   = avio_rl32(pb);
132
        jvf->palette_size = avio_r8(pb) ? 768 : 0;
133 134 135 136 137 138 139 140 141

        if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
            e->size - jvf->audio_size
                    - jvf->video_size
                    - jvf->palette_size < 0) {
            if (s->error_recognition & AV_EF_EXPLODE) {
                read_close(s);
                return AVERROR_INVALIDDATA;
            }
142 143
            jvf->audio_size   =
            jvf->video_size   =
144 145 146
            jvf->palette_size = 0;
        }

Peter Ross's avatar
Peter Ross committed
147
        if (avio_r8(pb))
148
            av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
149

Peter Ross's avatar
Peter Ross committed
150 151 152 153
        jvf->video_type = avio_r8(pb);
        avio_skip(pb, 1);

        e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
154
        audio_pts   += jvf->audio_size;
Peter Ross's avatar
Peter Ross committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168

        e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
    }

    jv->state = JV_AUDIO;
    return 0;
}

static int read_packet(AVFormatContext *s, AVPacket *pkt)
{
    JVDemuxContext *jv = s->priv_data;
    AVIOContext *pb = s->pb;
    AVStream *ast = s->streams[0];

169
    while (!avio_feof(s->pb) && jv->pts < ast->nb_index_entries) {
Peter Ross's avatar
Peter Ross committed
170 171 172
        const AVIndexEntry *e   = ast->index_entries + jv->pts;
        const JVFrame      *jvf = jv->frames + jv->pts;

173
        switch (jv->state) {
Peter Ross's avatar
Peter Ross committed
174 175
        case JV_AUDIO:
            jv->state++;
176
            if (jvf->audio_size) {
Peter Ross's avatar
Peter Ross committed
177 178 179 180
                if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
                    return AVERROR(ENOMEM);
                pkt->stream_index = 0;
                pkt->pts          = e->timestamp;
181
                pkt->flags       |= AV_PKT_FLAG_KEY;
Peter Ross's avatar
Peter Ross committed
182 183 184 185
                return 0;
            }
        case JV_VIDEO:
            jv->state++;
186
            if (jvf->video_size || jvf->palette_size) {
187
                int ret;
188
                int size = jvf->video_size + jvf->palette_size;
189
                if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
Peter Ross's avatar
Peter Ross committed
190 191 192
                    return AVERROR(ENOMEM);

                AV_WL32(pkt->data, jvf->video_size);
193
                pkt->data[4] = jvf->video_type;
194 195 196 197 198
                ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size);
                if (ret < 0)
                    return ret;
                if (ret < size) {
                    memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
199
                           AV_INPUT_BUFFER_PADDING_SIZE);
200 201 202
                    pkt->flags |= AV_PKT_FLAG_CORRUPT;
                }
                pkt->size         = ret + JV_PREAMBLE_SIZE;
Peter Ross's avatar
Peter Ross committed
203 204 205
                pkt->stream_index = 1;
                pkt->pts          = jv->pts;
                if (jvf->video_type != 1)
206
                    pkt->flags |= AV_PKT_FLAG_KEY;
Peter Ross's avatar
Peter Ross committed
207 208 209 210
                return 0;
            }
        case JV_PADDING:
            avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
211
                                        - jvf->palette_size, 0));
Peter Ross's avatar
Peter Ross committed
212 213 214 215 216
            jv->state = JV_AUDIO;
            jv->pts++;
        }
    }

217 218 219
    if (s->pb->eof_reached)
        return AVERROR_EOF;

Peter Ross's avatar
Peter Ross committed
220 221 222 223 224 225 226 227 228 229
    return AVERROR(EIO);
}

static int read_seek(AVFormatContext *s, int stream_index,
                     int64_t ts, int flags)
{
    JVDemuxContext *jv = s->priv_data;
    AVStream *ast = s->streams[0];
    int i;

230
    if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
231
        return AVERROR(ENOSYS);
Peter Ross's avatar
Peter Ross committed
232

233
    switch (stream_index) {
Peter Ross's avatar
Peter Ross committed
234 235 236 237 238 239 240 241 242 243 244 245
    case 0:
        i = av_index_search_timestamp(ast, ts, flags);
        break;
    case 1:
        i = ts;
        break;
    default:
        return 0;
    }

    if (i < 0 || i >= ast->nb_index_entries)
        return 0;
246 247
    if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
        return -1;
Peter Ross's avatar
Peter Ross committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261

    jv->state = JV_AUDIO;
    jv->pts   = i;
    return 0;
}

AVInputFormat ff_jv_demuxer = {
    .name           = "jv",
    .long_name      = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
    .priv_data_size = sizeof(JVDemuxContext),
    .read_probe     = read_probe,
    .read_header    = read_header,
    .read_packet    = read_packet,
    .read_seek      = read_seek,
262
    .read_close     = read_close,
Peter Ross's avatar
Peter Ross committed
263
};