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_close(AVFormatContext *s)
64 JVDemuxContext *jv = s->priv_data;
66 av_freep(&jv->frames);
71 static int read_header(AVFormatContext *s)
73 JVDemuxContext *jv = s->priv_data;
74 AVIOContext *pb = s->pb;
76 int64_t audio_pts = 0;
82 ast = avformat_new_stream(s, NULL);
83 vst = avformat_new_stream(s, NULL);
85 return AVERROR(ENOMEM);
87 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
88 vst->codec->codec_id = AV_CODEC_ID_JV;
89 vst->codec->codec_tag = 0; /* no fourcc */
90 vst->codec->width = avio_rl16(pb);
91 vst->codec->height = avio_rl16(pb);
94 ast->nb_index_entries = avio_rl16(pb);
95 avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
99 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
100 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
101 ast->codec->codec_tag = 0; /* no fourcc */
102 ast->codec->sample_rate = avio_rl16(pb);
103 ast->codec->channels = 1;
104 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
105 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
109 ast->index_entries = av_malloc(ast->nb_index_entries * sizeof(*ast->index_entries));
110 if (!ast->index_entries)
111 return AVERROR(ENOMEM);
113 jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
115 return AVERROR(ENOMEM);
117 offset = 0x68 + ast->nb_index_entries * 16;
118 for(i = 0; i < ast->nb_index_entries; i++) {
119 AVIndexEntry *e = ast->index_entries + i;
120 JVFrame *jvf = jv->frames + i;
122 /* total frame size including audio, video, palette data and padding */
123 e->size = avio_rl32(pb);
128 jvf->audio_size = avio_rl32(pb);
129 jvf->video_size = avio_rl32(pb);
130 jvf->palette_size = avio_r8(pb) ? 768 : 0;
132 if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
133 e->size - jvf->audio_size
135 - jvf->palette_size < 0) {
136 if (s->error_recognition & AV_EF_EXPLODE) {
138 return AVERROR_INVALIDDATA;
142 jvf->palette_size = 0;
146 av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
148 jvf->video_type = avio_r8(pb);
151 e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
152 audio_pts += jvf->audio_size;
154 e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
157 jv->state = JV_AUDIO;
161 static int read_packet(AVFormatContext *s, AVPacket *pkt)
163 JVDemuxContext *jv = s->priv_data;
164 AVIOContext *pb = s->pb;
165 AVStream *ast = s->streams[0];
167 while (!url_feof(s->pb) && jv->pts < ast->nb_index_entries) {
168 const AVIndexEntry *e = ast->index_entries + jv->pts;
169 const JVFrame *jvf = jv->frames + jv->pts;
174 if (jvf->audio_size ) {
175 if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
176 return AVERROR(ENOMEM);
177 pkt->stream_index = 0;
178 pkt->pts = e->timestamp;
179 pkt->flags |= AV_PKT_FLAG_KEY;
184 if (jvf->video_size || jvf->palette_size) {
185 int size = jvf->video_size + jvf->palette_size;
186 if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
187 return AVERROR(ENOMEM);
189 AV_WL32(pkt->data, jvf->video_size);
190 pkt->data[4] = jvf->video_type;
191 if ((size = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size)) < 0)
193 memset(pkt->data + JV_PREAMBLE_SIZE + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
195 pkt->size = size + JV_PREAMBLE_SIZE;
196 pkt->stream_index = 1;
198 if (jvf->video_type != 1)
199 pkt->flags |= AV_PKT_FLAG_KEY;
203 avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
204 - jvf->palette_size, 0));
205 jv->state = JV_AUDIO;
210 if (s->pb->eof_reached)
216 static int read_seek(AVFormatContext *s, int stream_index,
217 int64_t ts, int flags)
219 JVDemuxContext *jv = s->priv_data;
220 AVStream *ast = s->streams[0];
223 if (flags & (AVSEEK_FLAG_BYTE|AVSEEK_FLAG_FRAME))
224 return AVERROR(ENOSYS);
226 switch(stream_index) {
228 i = av_index_search_timestamp(ast, ts, flags);
237 if (i < 0 || i >= ast->nb_index_entries)
239 if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
242 jv->state = JV_AUDIO;
247 AVInputFormat ff_jv_demuxer = {
249 .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
250 .priv_data_size = sizeof(JVDemuxContext),
251 .read_probe = read_probe,
252 .read_header = read_header,
253 .read_packet = read_packet,
254 .read_seek = read_seek,
255 .read_close = read_close,