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