2 * Bitmap Brothers JV demuxer
3 * Copyright (c) 2005, 2011 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Bitmap Brothers JV demuxer
25 * @author Peter Ross <pross@xvid.org>
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
33 #define JV_PREAMBLE_SIZE 5
36 int audio_size; /**< audio packet size (bytes) */
37 int video_size; /**< video packet size (bytes) */
38 int palette_size; /**< palette size (bytes) */
39 int video_type; /**< per-frame video compression type */
52 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
54 static int read_probe(AVProbeData *pd)
56 if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) <= pd->buf_size - 4 &&
57 !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
58 return AVPROBE_SCORE_MAX;
62 static int read_header(AVFormatContext *s)
64 JVDemuxContext *jv = s->priv_data;
65 AVIOContext *pb = s->pb;
67 int64_t audio_pts = 0;
73 ast = avformat_new_stream(s, NULL);
74 vst = avformat_new_stream(s, NULL);
76 return AVERROR(ENOMEM);
78 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
79 vst->codec->codec_id = AV_CODEC_ID_JV;
80 vst->codec->codec_tag = 0; /* no fourcc */
81 vst->codec->width = avio_rl16(pb);
82 vst->codec->height = avio_rl16(pb);
85 ast->nb_index_entries = avio_rl16(pb);
86 avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
90 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
91 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
92 ast->codec->codec_tag = 0; /* no fourcc */
93 ast->codec->sample_rate = avio_rl16(pb);
94 ast->codec->channels = 1;
95 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
96 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
100 ast->index_entries = av_malloc(ast->nb_index_entries * sizeof(*ast->index_entries));
101 if (!ast->index_entries)
102 return AVERROR(ENOMEM);
104 jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
106 return AVERROR(ENOMEM);
108 offset = 0x68 + ast->nb_index_entries * 16;
109 for(i = 0; i < ast->nb_index_entries; i++) {
110 AVIndexEntry *e = ast->index_entries + i;
111 JVFrame *jvf = jv->frames + i;
113 /* total frame size including audio, video, palette data and padding */
114 e->size = avio_rl32(pb);
119 jvf->audio_size = avio_rl32(pb);
120 jvf->video_size = avio_rl32(pb);
121 jvf->palette_size = avio_r8(pb) ? 768 : 0;
122 jvf->video_size = FFMIN(FFMAX(jvf->video_size, 0),
123 INT_MAX - JV_PREAMBLE_SIZE - jvf->palette_size);
125 av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
126 jvf->video_type = avio_r8(pb);
129 e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
130 audio_pts += jvf->audio_size;
132 e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
135 jv->state = JV_AUDIO;
139 static int read_packet(AVFormatContext *s, AVPacket *pkt)
141 JVDemuxContext *jv = s->priv_data;
142 AVIOContext *pb = s->pb;
143 AVStream *ast = s->streams[0];
145 while (!url_feof(s->pb) && jv->pts < ast->nb_index_entries) {
146 const AVIndexEntry *e = ast->index_entries + jv->pts;
147 const JVFrame *jvf = jv->frames + jv->pts;
152 if (jvf->audio_size ) {
153 if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
154 return AVERROR(ENOMEM);
155 pkt->stream_index = 0;
156 pkt->pts = e->timestamp;
157 pkt->flags |= AV_PKT_FLAG_KEY;
162 if (jvf->video_size || jvf->palette_size) {
163 int size = jvf->video_size + jvf->palette_size;
164 if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
165 return AVERROR(ENOMEM);
167 AV_WL32(pkt->data, jvf->video_size);
168 pkt->data[4] = jvf->video_type;
169 if ((size = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size)) < 0)
172 pkt->size = size + JV_PREAMBLE_SIZE;
173 pkt->stream_index = 1;
175 if (jvf->video_type != 1)
176 pkt->flags |= AV_PKT_FLAG_KEY;
180 avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
181 - jvf->palette_size, 0));
182 jv->state = JV_AUDIO;
190 static int read_seek(AVFormatContext *s, int stream_index,
191 int64_t ts, int flags)
193 JVDemuxContext *jv = s->priv_data;
194 AVStream *ast = s->streams[0];
197 if (flags & (AVSEEK_FLAG_BYTE|AVSEEK_FLAG_FRAME))
198 return AVERROR(ENOSYS);
200 switch(stream_index) {
202 i = av_index_search_timestamp(ast, ts, flags);
211 if (i < 0 || i >= ast->nb_index_entries)
213 if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
216 jv->state = JV_AUDIO;
221 static int read_close(AVFormatContext *s)
223 JVDemuxContext *jv = s->priv_data;
225 av_freep(&jv->frames);
230 AVInputFormat ff_jv_demuxer = {
232 .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
233 .priv_data_size = sizeof(JVDemuxContext),
234 .read_probe = read_probe,
235 .read_header = read_header,
236 .read_packet = read_packet,
237 .read_seek = read_seek,
238 .read_close = read_close,