2011-03-10 14:29:22 +02:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
2012-04-07 23:47:15 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2011-03-10 14:29:22 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2013-12-13 16:30:22 +03:00
|
|
|
|
2011-03-10 14:29:22 +02:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2011-03-10 14:29:22 +02:00
|
|
|
|
2011-03-12 04:31:40 +02:00
|
|
|
#define JV_PREAMBLE_SIZE 5
|
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct JVFrame {
|
2013-05-23 16:51:43 +03:00
|
|
|
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 */
|
2011-03-10 14:29:22 +02:00
|
|
|
} JVFrame;
|
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct JVDemuxContext {
|
2011-03-10 14:29:22 +02:00
|
|
|
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."
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int read_probe(const AVProbeData *pd)
|
2011-03-10 14:29:22 +02:00
|
|
|
{
|
2015-11-06 22:04:34 +02:00
|
|
|
if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
|
2012-04-19 22:43:15 +03:00
|
|
|
!memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
|
2011-03-10 14:29:22 +02:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-03 12:51:44 +03:00
|
|
|
static int read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
JVDemuxContext *jv = s->priv_data;
|
|
|
|
|
|
|
|
av_freep(&jv->frames);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int read_header(AVFormatContext *s)
|
2011-03-10 14:29:22 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
ast = avformat_new_stream(s, NULL);
|
2011-06-18 12:43:24 +03:00
|
|
|
vst = avformat_new_stream(s, NULL);
|
2011-03-10 14:29:22 +02:00
|
|
|
if (!ast || !vst)
|
|
|
|
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 21:42:52 +03:00
|
|
|
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);
|
2012-03-14 06:14:01 +03:00
|
|
|
vst->duration =
|
2011-03-10 14:29:22 +02:00
|
|
|
vst->nb_frames =
|
|
|
|
ast->nb_index_entries = avio_rl16(pb);
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
|
2011-03-10 14:29:22 +02:00
|
|
|
|
|
|
|
avio_skip(pb, 4);
|
|
|
|
|
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 21:42:52 +03:00
|
|
|
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);
|
2011-03-10 14:29:22 +02:00
|
|
|
|
|
|
|
avio_skip(pb, 10);
|
|
|
|
|
2013-12-13 16:30:22 +03:00
|
|
|
ast->index_entries = av_malloc(ast->nb_index_entries *
|
|
|
|
sizeof(*ast->index_entries));
|
2011-03-10 14:29:22 +02:00
|
|
|
if (!ast->index_entries)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
|
2019-10-09 05:56:22 +02:00
|
|
|
if (!jv->frames) {
|
|
|
|
av_freep(&ast->index_entries);
|
2011-03-10 14:29:22 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2019-10-09 05:56:22 +02:00
|
|
|
}
|
2011-03-10 14:29:22 +02:00
|
|
|
offset = 0x68 + ast->nb_index_entries * 16;
|
2013-12-13 16:30:22 +03:00
|
|
|
for (i = 0; i < ast->nb_index_entries; i++) {
|
2011-03-10 14:29:22 +02:00
|
|
|
AVIndexEntry *e = ast->index_entries + i;
|
|
|
|
JVFrame *jvf = jv->frames + i;
|
|
|
|
|
|
|
|
/* total frame size including audio, video, palette data and padding */
|
2013-12-13 16:30:22 +03:00
|
|
|
e->size = avio_rl32(pb);
|
|
|
|
e->timestamp = i;
|
|
|
|
e->pos = offset;
|
|
|
|
offset += e->size;
|
2011-03-10 14:29:22 +02:00
|
|
|
|
2013-12-13 16:30:22 +03:00
|
|
|
jvf->audio_size = avio_rl32(pb);
|
|
|
|
jvf->video_size = avio_rl32(pb);
|
2011-03-12 04:31:26 +02:00
|
|
|
jvf->palette_size = avio_r8(pb) ? 768 : 0;
|
2013-12-13 05:07:57 +03:00
|
|
|
|
|
|
|
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);
|
2019-10-09 05:56:22 +02:00
|
|
|
av_freep(&jv->frames);
|
|
|
|
av_freep(&ast->index_entries);
|
2013-12-13 05:07:57 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2013-12-13 16:30:22 +03:00
|
|
|
jvf->audio_size =
|
|
|
|
jvf->video_size =
|
2013-12-13 05:07:57 +03:00
|
|
|
jvf->palette_size = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-10 14:29:22 +02:00
|
|
|
if (avio_r8(pb))
|
2013-12-13 16:30:22 +03:00
|
|
|
av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
|
2013-12-13 05:07:57 +03:00
|
|
|
|
2011-03-10 14:29:22 +02:00
|
|
|
jvf->video_type = avio_r8(pb);
|
|
|
|
avio_skip(pb, 1);
|
|
|
|
|
|
|
|
e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
|
2013-12-13 16:30:22 +03:00
|
|
|
audio_pts += jvf->audio_size;
|
2011-03-10 14:29:22 +02:00
|
|
|
|
|
|
|
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];
|
2019-12-10 23:59:53 +02:00
|
|
|
int ret;
|
2011-03-10 14:29:22 +02:00
|
|
|
|
2014-08-07 23:12:41 +03:00
|
|
|
while (!avio_feof(s->pb) && jv->pts < ast->nb_index_entries) {
|
2011-03-10 14:29:22 +02:00
|
|
|
const AVIndexEntry *e = ast->index_entries + jv->pts;
|
|
|
|
const JVFrame *jvf = jv->frames + jv->pts;
|
|
|
|
|
2013-12-13 16:30:22 +03:00
|
|
|
switch (jv->state) {
|
2011-03-10 14:29:22 +02:00
|
|
|
case JV_AUDIO:
|
|
|
|
jv->state++;
|
2013-12-13 16:30:22 +03:00
|
|
|
if (jvf->audio_size) {
|
2019-12-10 23:59:53 +02:00
|
|
|
if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0)
|
|
|
|
return ret;
|
2011-03-10 14:29:22 +02:00
|
|
|
pkt->stream_index = 0;
|
|
|
|
pkt->pts = e->timestamp;
|
2011-04-02 12:35:14 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2011-03-10 14:29:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case JV_VIDEO:
|
|
|
|
jv->state++;
|
2011-03-12 04:31:26 +02:00
|
|
|
if (jvf->video_size || jvf->palette_size) {
|
|
|
|
int size = jvf->video_size + jvf->palette_size;
|
2019-12-10 23:59:53 +02:00
|
|
|
if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0)
|
|
|
|
return ret;
|
2011-03-10 14:29:22 +02:00
|
|
|
|
|
|
|
AV_WL32(pkt->data, jvf->video_size);
|
2013-12-13 16:30:22 +03:00
|
|
|
pkt->data[4] = jvf->video_type;
|
2014-02-12 22:08:52 +03:00
|
|
|
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,
|
2015-06-29 23:48:34 +02:00
|
|
|
AV_INPUT_BUFFER_PADDING_SIZE);
|
2014-02-12 22:08:52 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_CORRUPT;
|
|
|
|
}
|
|
|
|
pkt->size = ret + JV_PREAMBLE_SIZE;
|
2011-03-10 14:29:22 +02:00
|
|
|
pkt->stream_index = 1;
|
|
|
|
pkt->pts = jv->pts;
|
|
|
|
if (jvf->video_type != 1)
|
2011-04-02 12:35:14 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2011-03-10 14:29:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case JV_PADDING:
|
|
|
|
avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
|
2011-03-12 04:31:26 +02:00
|
|
|
- jvf->palette_size, 0));
|
2011-03-10 14:29:22 +02:00
|
|
|
jv->state = JV_AUDIO;
|
|
|
|
jv->pts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 05:10:16 +03:00
|
|
|
if (s->pb->eof_reached)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2011-03-10 14:29:22 +02:00
|
|
|
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;
|
|
|
|
|
2013-12-13 16:30:22 +03:00
|
|
|
if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
|
2011-04-06 09:16:06 +03:00
|
|
|
return AVERROR(ENOSYS);
|
2011-03-10 14:29:22 +02:00
|
|
|
|
2013-12-13 16:30:22 +03:00
|
|
|
switch (stream_index) {
|
2011-03-10 14:29:22 +02:00
|
|
|
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;
|
2011-09-14 20:25:43 +03:00
|
|
|
if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
|
|
|
|
return -1;
|
2011-03-10 14:29:22 +02:00
|
|
|
|
|
|
|
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,
|
2012-07-03 12:51:44 +03:00
|
|
|
.read_close = read_close,
|
2011-03-10 14:29:22 +02:00
|
|
|
};
|