libavformat/jvdec.c
bfaefd87
 /*
  * 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>
  */
 
ef1b23ad
 #include "libavutil/channel_layout.h"
bfaefd87
 #include "libavutil/intreadwrite.h"
d509ae5b
 
bfaefd87
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
bfaefd87
 
772cb062
 #define JV_PREAMBLE_SIZE 5
 
daf8cf35
 typedef struct JVFrame {
682f0035
     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 */
bfaefd87
 } JVFrame;
 
daf8cf35
 typedef struct JVDemuxContext {
bfaefd87
     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."
 
 static int read_probe(AVProbeData *pd)
 {
e2172244
     if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
2a70d830
         !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
bfaefd87
         return AVPROBE_SCORE_MAX;
     return 0;
 }
 
e518cb86
 static int read_close(AVFormatContext *s)
 {
     JVDemuxContext *jv = s->priv_data;
 
     av_freep(&jv->frames);
 
     return 0;
 }
 
6e9651d1
 static int read_header(AVFormatContext *s)
bfaefd87
 {
     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);
 
3b3bbdd3
     ast = avformat_new_stream(s, NULL);
84ad31ff
     vst = avformat_new_stream(s, NULL);
bfaefd87
     if (!ast || !vst)
         return AVERROR(ENOMEM);
 
b4b167ec
     vst->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
36ef5369
     vst->codec->codec_id    = AV_CODEC_ID_JV;
bfaefd87
     vst->codec->codec_tag   = 0; /* no fourcc */
     vst->codec->width       = avio_rl16(pb);
     vst->codec->height      = avio_rl16(pb);
d3d1b25e
     vst->duration           =
bfaefd87
     vst->nb_frames          =
     ast->nb_index_entries   = avio_rl16(pb);
c3f9ebf7
     avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
bfaefd87
 
     avio_skip(pb, 4);
 
d509ae5b
     ast->codec->codec_type     = AVMEDIA_TYPE_AUDIO;
     ast->codec->codec_id       = AV_CODEC_ID_PCM_U8;
     ast->codec->codec_tag      = 0; /* no fourcc */
     ast->codec->sample_rate    = avio_rl16(pb);
     ast->codec->channels       = 1;
ef1b23ad
     ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
c3f9ebf7
     avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
bfaefd87
 
     avio_skip(pb, 10);
 
d509ae5b
     ast->index_entries = av_malloc(ast->nb_index_entries *
                                    sizeof(*ast->index_entries));
bfaefd87
     if (!ast->index_entries)
         return AVERROR(ENOMEM);
 
     jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
     if (!jv->frames)
         return AVERROR(ENOMEM);
 
     offset = 0x68 + ast->nb_index_entries * 16;
d509ae5b
     for (i = 0; i < ast->nb_index_entries; i++) {
bfaefd87
         AVIndexEntry *e   = ast->index_entries + i;
         JVFrame      *jvf = jv->frames + i;
 
         /* total frame size including audio, video, palette data and padding */
d509ae5b
         e->size      = avio_rl32(pb);
         e->timestamp = i;
         e->pos       = offset;
         offset      += e->size;
bfaefd87
 
d509ae5b
         jvf->audio_size   = avio_rl32(pb);
         jvf->video_size   = avio_rl32(pb);
20c1281f
         jvf->palette_size = avio_r8(pb) ? 768 : 0;
15739a9b
 
         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);
                 return AVERROR_INVALIDDATA;
             }
d509ae5b
             jvf->audio_size   =
             jvf->video_size   =
15739a9b
             jvf->palette_size = 0;
         }
 
bfaefd87
         if (avio_r8(pb))
d509ae5b
             av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
15739a9b
 
bfaefd87
         jvf->video_type = avio_r8(pb);
         avio_skip(pb, 1);
 
         e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
d509ae5b
         audio_pts   += jvf->audio_size;
bfaefd87
 
         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];
 
d34ec64a
     while (!avio_feof(s->pb) && jv->pts < ast->nb_index_entries) {
bfaefd87
         const AVIndexEntry *e   = ast->index_entries + jv->pts;
         const JVFrame      *jvf = jv->frames + jv->pts;
 
d509ae5b
         switch (jv->state) {
bfaefd87
         case JV_AUDIO:
             jv->state++;
d509ae5b
             if (jvf->audio_size) {
bfaefd87
                 if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
                     return AVERROR(ENOMEM);
                 pkt->stream_index = 0;
                 pkt->pts          = e->timestamp;
b4b167ec
                 pkt->flags       |= AV_PKT_FLAG_KEY;
bfaefd87
                 return 0;
             }
         case JV_VIDEO:
             jv->state++;
20c1281f
             if (jvf->video_size || jvf->palette_size) {
8a225034
                 int ret;
20c1281f
                 int size = jvf->video_size + jvf->palette_size;
772cb062
                 if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
bfaefd87
                     return AVERROR(ENOMEM);
 
                 AV_WL32(pkt->data, jvf->video_size);
d509ae5b
                 pkt->data[4] = jvf->video_type;
8a225034
                 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,
059a9348
                            AV_INPUT_BUFFER_PADDING_SIZE);
8a225034
                     pkt->flags |= AV_PKT_FLAG_CORRUPT;
                 }
                 pkt->size         = ret + JV_PREAMBLE_SIZE;
bfaefd87
                 pkt->stream_index = 1;
                 pkt->pts          = jv->pts;
                 if (jvf->video_type != 1)
b4b167ec
                     pkt->flags |= AV_PKT_FLAG_KEY;
bfaefd87
                 return 0;
             }
         case JV_PADDING:
             avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
20c1281f
                                         - jvf->palette_size, 0));
bfaefd87
             jv->state = JV_AUDIO;
             jv->pts++;
         }
     }
 
027712e8
     if (s->pb->eof_reached)
         return AVERROR_EOF;
 
bfaefd87
     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;
 
d509ae5b
     if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
f5b386af
         return AVERROR(ENOSYS);
bfaefd87
 
d509ae5b
     switch (stream_index) {
bfaefd87
     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;
83335817
     if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
         return -1;
bfaefd87
 
     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,
596814f9
     .read_close     = read_close,
bfaefd87
 };