jvdec.c
Go to the documentation of this file.
1 /*
2  * Bitmap Brothers JV demuxer
3  * Copyright (c) 2005, 2011 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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.
11  *
12  * Libav 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.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "internal.h"
31 
32 #define JV_PREAMBLE_SIZE 5
33 
34 typedef struct {
35  int audio_size;
36  int video_size;
38  int video_type;
39 } JVFrame;
40 
41 typedef struct {
43  enum {
44  JV_AUDIO = 0,
46  JV_PADDING
47  } state;
48  int64_t pts;
50 
51 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
52 
53 static int read_probe(AVProbeData *pd)
54 {
55  if (pd->buf[0] == 'J' && pd->buf[1] == 'V' &&
56  !memcmp(pd->buf + 4, MAGIC, FFMIN(strlen(MAGIC), pd->buf_size - 4)))
57  return AVPROBE_SCORE_MAX;
58  return 0;
59 }
60 
63 {
64  JVDemuxContext *jv = s->priv_data;
65  AVIOContext *pb = s->pb;
66  AVStream *vst, *ast;
67  int64_t audio_pts = 0;
68  int64_t offset;
69  int i;
70 
71  avio_skip(pb, 80);
72 
73  ast = avformat_new_stream(s, NULL);
74  vst = avformat_new_stream(s, NULL);
75  if (!ast || !vst)
76  return AVERROR(ENOMEM);
77 
79  vst->codec->codec_id = 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);
83  vst->nb_frames =
84  ast->nb_index_entries = avio_rl16(pb);
85  avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
86 
87  avio_skip(pb, 4);
88 
91  ast->codec->codec_tag = 0; /* no fourcc */
92  ast->codec->sample_rate = avio_rl16(pb);
93  ast->codec->channels = 1;
94  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
95 
96  avio_skip(pb, 10);
97 
98  ast->index_entries = av_malloc(ast->nb_index_entries * sizeof(*ast->index_entries));
99  if (!ast->index_entries)
100  return AVERROR(ENOMEM);
101 
102  jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
103  if (!jv->frames)
104  return AVERROR(ENOMEM);
105 
106  offset = 0x68 + ast->nb_index_entries * 16;
107  for(i = 0; i < ast->nb_index_entries; i++) {
108  AVIndexEntry *e = ast->index_entries + i;
109  JVFrame *jvf = jv->frames + i;
110 
111  /* total frame size including audio, video, palette data and padding */
112  e->size = avio_rl32(pb);
113  e->timestamp = i;
114  e->pos = offset;
115  offset += e->size;
116 
117  jvf->audio_size = avio_rl32(pb);
118  jvf->video_size = avio_rl32(pb);
119  jvf->palette_size = avio_r8(pb) ? 768 : 0;
120  jvf->video_size = FFMIN(FFMAX(jvf->video_size, 0),
121  INT_MAX - JV_PREAMBLE_SIZE - jvf->palette_size);
122  if (avio_r8(pb))
123  av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
124  jvf->video_type = avio_r8(pb);
125  avio_skip(pb, 1);
126 
127  e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
128  audio_pts += jvf->audio_size;
129 
130  e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
131  }
132 
133  jv->state = JV_AUDIO;
134  return 0;
135 }
136 
138 {
139  JVDemuxContext *jv = s->priv_data;
140  AVIOContext *pb = s->pb;
141  AVStream *ast = s->streams[0];
142 
143  while (!s->pb->eof_reached && jv->pts < ast->nb_index_entries) {
144  const AVIndexEntry *e = ast->index_entries + jv->pts;
145  const JVFrame *jvf = jv->frames + jv->pts;
146 
147  switch(jv->state) {
148  case JV_AUDIO:
149  jv->state++;
150  if (jvf->audio_size ) {
151  if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
152  return AVERROR(ENOMEM);
153  pkt->stream_index = 0;
154  pkt->pts = e->timestamp;
155  pkt->flags |= AV_PKT_FLAG_KEY;
156  return 0;
157  }
158  case JV_VIDEO:
159  jv->state++;
160  if (jvf->video_size || jvf->palette_size) {
161  int size = jvf->video_size + jvf->palette_size;
162  if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
163  return AVERROR(ENOMEM);
164 
165  AV_WL32(pkt->data, jvf->video_size);
166  pkt->data[4] = jvf->video_type;
167  if (avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size) < 0)
168  return AVERROR(EIO);
169 
170  pkt->size = size + JV_PREAMBLE_SIZE;
171  pkt->stream_index = 1;
172  pkt->pts = jv->pts;
173  if (jvf->video_type != 1)
174  pkt->flags |= AV_PKT_FLAG_KEY;
175  return 0;
176  }
177  case JV_PADDING:
178  avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
179  - jvf->palette_size, 0));
180  jv->state = JV_AUDIO;
181  jv->pts++;
182  }
183  }
184 
185  return AVERROR(EIO);
186 }
187 
188 static int read_seek(AVFormatContext *s, int stream_index,
189  int64_t ts, int flags)
190 {
191  JVDemuxContext *jv = s->priv_data;
192  AVStream *ast = s->streams[0];
193  int i;
194 
195  if (flags & (AVSEEK_FLAG_BYTE|AVSEEK_FLAG_FRAME))
196  return AVERROR(ENOSYS);
197 
198  switch(stream_index) {
199  case 0:
200  i = av_index_search_timestamp(ast, ts, flags);
201  break;
202  case 1:
203  i = ts;
204  break;
205  default:
206  return 0;
207  }
208 
209  if (i < 0 || i >= ast->nb_index_entries)
210  return 0;
211 
212  jv->state = JV_AUDIO;
213  jv->pts = i;
214  avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET);
215  return 0;
216 }
217 
219  .name = "jv",
220  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
221  .priv_data_size = sizeof(JVDemuxContext),
225  .read_seek = read_seek,
226 };
int video_type
palette size (bytes)
Definition: jvdec.c:38
Bytestream IO Context.
Definition: avio.h:68
int size
int64_t pts
Definition: jvdec.c:48
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int64_t pos
Definition: avformat.h:588
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
Definition: jvdec.c:34
int size
Definition: avcodec.h:909
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:818
JVFrame * frames
Definition: jvdec.c:42
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: jvdec.c:137
Format I/O context.
Definition: avformat.h:863
#define MAGIC
Definition: jvdec.c:51
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
AVStream ** streams
Definition: avformat.h:908
#define JV_PREAMBLE_SIZE
Definition: jvdec.c:32
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:269
static int read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: jvdec.c:188
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
#define AVINDEX_KEYFRAME
Definition: avformat.h:590
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: jvdec.c:61
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1516
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#define AVERROR(e)
Definition: error.h:43
int64_t timestamp
Definition: avformat.h:589
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
#define FFMAX(a, b)
Definition: common.h:53
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:342
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
#define FFMIN(a, b)
Definition: common.h:55
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:53
int width
picture width / height.
Definition: avcodec.h:1408
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
enum JVDemuxContext::@78 state
int palette_size
video packet size (bytes)
Definition: jvdec.c:37
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
int nb_index_entries
Definition: avformat.h:820
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:1619
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
static uint32_t state
Definition: trasher.c:25
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
int video_size
audio packet size (bytes)
Definition: jvdec.c:36
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:701
int eof_reached
true if eof reached
Definition: avio.h:98
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:1621
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int stream_index
Definition: avcodec.h:910
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:271
int audio_size
Definition: jvdec.c:35
enum CodecID codec_id
Definition: avcodec.h:1575
AVInputFormat ff_jv_demuxer
Definition: jvdec.c:218