00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/channel_layout.h"
00029 #include "libavutil/intreadwrite.h"
00030 #include "avformat.h"
00031 #include "internal.h"
00032
00033 #define JV_PREAMBLE_SIZE 5
00034
00035 typedef struct {
00036 int audio_size;
00037 int video_size;
00038 int palette_size;
00039 int video_type;
00040 } JVFrame;
00041
00042 typedef struct {
00043 JVFrame *frames;
00044 enum {
00045 JV_AUDIO = 0,
00046 JV_VIDEO,
00047 JV_PADDING
00048 } state;
00049 int64_t pts;
00050 } JVDemuxContext;
00051
00052 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
00053
00054 static int read_probe(AVProbeData *pd)
00055 {
00056 if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) <= pd->buf_size - 4 &&
00057 !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
00058 return AVPROBE_SCORE_MAX;
00059 return 0;
00060 }
00061
00062 static int read_header(AVFormatContext *s)
00063 {
00064 JVDemuxContext *jv = s->priv_data;
00065 AVIOContext *pb = s->pb;
00066 AVStream *vst, *ast;
00067 int64_t audio_pts = 0;
00068 int64_t offset;
00069 int i;
00070
00071 avio_skip(pb, 80);
00072
00073 ast = avformat_new_stream(s, NULL);
00074 vst = avformat_new_stream(s, NULL);
00075 if (!ast || !vst)
00076 return AVERROR(ENOMEM);
00077
00078 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00079 vst->codec->codec_id = AV_CODEC_ID_JV;
00080 vst->codec->codec_tag = 0;
00081 vst->codec->width = avio_rl16(pb);
00082 vst->codec->height = avio_rl16(pb);
00083 vst->duration =
00084 vst->nb_frames =
00085 ast->nb_index_entries = avio_rl16(pb);
00086 avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
00087
00088 avio_skip(pb, 4);
00089
00090 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00091 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
00092 ast->codec->codec_tag = 0;
00093 ast->codec->sample_rate = avio_rl16(pb);
00094 ast->codec->channels = 1;
00095 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
00096 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00097
00098 avio_skip(pb, 10);
00099
00100 ast->index_entries = av_malloc(ast->nb_index_entries * sizeof(*ast->index_entries));
00101 if (!ast->index_entries)
00102 return AVERROR(ENOMEM);
00103
00104 jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
00105 if (!jv->frames)
00106 return AVERROR(ENOMEM);
00107
00108 offset = 0x68 + ast->nb_index_entries * 16;
00109 for(i = 0; i < ast->nb_index_entries; i++) {
00110 AVIndexEntry *e = ast->index_entries + i;
00111 JVFrame *jvf = jv->frames + i;
00112
00113
00114 e->size = avio_rl32(pb);
00115 e->timestamp = i;
00116 e->pos = offset;
00117 offset += e->size;
00118
00119 jvf->audio_size = avio_rl32(pb);
00120 jvf->video_size = avio_rl32(pb);
00121 jvf->palette_size = avio_r8(pb) ? 768 : 0;
00122 jvf->video_size = FFMIN(FFMAX(jvf->video_size, 0),
00123 INT_MAX - JV_PREAMBLE_SIZE - jvf->palette_size);
00124 if (avio_r8(pb))
00125 av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
00126 jvf->video_type = avio_r8(pb);
00127 avio_skip(pb, 1);
00128
00129 e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
00130 audio_pts += jvf->audio_size;
00131
00132 e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
00133 }
00134
00135 jv->state = JV_AUDIO;
00136 return 0;
00137 }
00138
00139 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00140 {
00141 JVDemuxContext *jv = s->priv_data;
00142 AVIOContext *pb = s->pb;
00143 AVStream *ast = s->streams[0];
00144
00145 while (!url_feof(s->pb) && jv->pts < ast->nb_index_entries) {
00146 const AVIndexEntry *e = ast->index_entries + jv->pts;
00147 const JVFrame *jvf = jv->frames + jv->pts;
00148
00149 switch(jv->state) {
00150 case JV_AUDIO:
00151 jv->state++;
00152 if (jvf->audio_size ) {
00153 if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
00154 return AVERROR(ENOMEM);
00155 pkt->stream_index = 0;
00156 pkt->pts = e->timestamp;
00157 pkt->flags |= AV_PKT_FLAG_KEY;
00158 return 0;
00159 }
00160 case JV_VIDEO:
00161 jv->state++;
00162 if (jvf->video_size || jvf->palette_size) {
00163 int size = jvf->video_size + jvf->palette_size;
00164 if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
00165 return AVERROR(ENOMEM);
00166
00167 AV_WL32(pkt->data, jvf->video_size);
00168 pkt->data[4] = jvf->video_type;
00169 if ((size = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size)) < 0)
00170 return AVERROR(EIO);
00171
00172 pkt->size = size + JV_PREAMBLE_SIZE;
00173 pkt->stream_index = 1;
00174 pkt->pts = jv->pts;
00175 if (jvf->video_type != 1)
00176 pkt->flags |= AV_PKT_FLAG_KEY;
00177 return 0;
00178 }
00179 case JV_PADDING:
00180 avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
00181 - jvf->palette_size, 0));
00182 jv->state = JV_AUDIO;
00183 jv->pts++;
00184 }
00185 }
00186
00187 return AVERROR(EIO);
00188 }
00189
00190 static int read_seek(AVFormatContext *s, int stream_index,
00191 int64_t ts, int flags)
00192 {
00193 JVDemuxContext *jv = s->priv_data;
00194 AVStream *ast = s->streams[0];
00195 int i;
00196
00197 if (flags & (AVSEEK_FLAG_BYTE|AVSEEK_FLAG_FRAME))
00198 return AVERROR(ENOSYS);
00199
00200 switch(stream_index) {
00201 case 0:
00202 i = av_index_search_timestamp(ast, ts, flags);
00203 break;
00204 case 1:
00205 i = ts;
00206 break;
00207 default:
00208 return 0;
00209 }
00210
00211 if (i < 0 || i >= ast->nb_index_entries)
00212 return 0;
00213 if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
00214 return -1;
00215
00216 jv->state = JV_AUDIO;
00217 jv->pts = i;
00218 return 0;
00219 }
00220
00221 static int read_close(AVFormatContext *s)
00222 {
00223 JVDemuxContext *jv = s->priv_data;
00224
00225 av_freep(&jv->frames);
00226
00227 return 0;
00228 }
00229
00230 AVInputFormat ff_jv_demuxer = {
00231 .name = "jv",
00232 .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
00233 .priv_data_size = sizeof(JVDemuxContext),
00234 .read_probe = read_probe,
00235 .read_header = read_header,
00236 .read_packet = read_packet,
00237 .read_seek = read_seek,
00238 .read_close = read_close,
00239 };