FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * Bitmap Brothers JV demuxer
25  * @author Peter Ross <pross@xvid.org>
26  */
27 
29 #include "libavutil/intreadwrite.h"
30 
31 #include "avformat.h"
32 #include "internal.h"
33 
34 #define JV_PREAMBLE_SIZE 5
35 
36 typedef struct JVFrame {
37  int audio_size; /**< audio packet size (bytes) */
38  int video_size; /**< video packet size (bytes) */
39  int palette_size; /**< palette size (bytes) */
40  int video_type; /**< per-frame video compression type */
41 } JVFrame;
42 
43 typedef struct JVDemuxContext {
45  enum {
46  JV_AUDIO = 0,
49  } state;
50  int64_t pts;
52 
53 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
54 
55 static int read_probe(AVProbeData *pd)
56 {
57  if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
58  !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
59  return AVPROBE_SCORE_MAX;
60  return 0;
61 }
62 
64 {
65  JVDemuxContext *jv = s->priv_data;
66 
67  av_freep(&jv->frames);
68 
69  return 0;
70 }
71 
73 {
74  JVDemuxContext *jv = s->priv_data;
75  AVIOContext *pb = s->pb;
76  AVStream *vst, *ast;
77  int64_t audio_pts = 0;
78  int64_t offset;
79  int i;
80 
81  avio_skip(pb, 80);
82 
83  ast = avformat_new_stream(s, NULL);
84  vst = avformat_new_stream(s, NULL);
85  if (!ast || !vst)
86  return AVERROR(ENOMEM);
87 
90  vst->codecpar->codec_tag = 0; /* no fourcc */
91  vst->codecpar->width = avio_rl16(pb);
92  vst->codecpar->height = avio_rl16(pb);
93  vst->duration =
94  vst->nb_frames =
95  ast->nb_index_entries = avio_rl16(pb);
96  avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
97 
98  avio_skip(pb, 4);
99 
102  ast->codecpar->codec_tag = 0; /* no fourcc */
103  ast->codecpar->sample_rate = avio_rl16(pb);
104  ast->codecpar->channels = 1;
106  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
107 
108  avio_skip(pb, 10);
109 
111  sizeof(*ast->index_entries));
112  if (!ast->index_entries)
113  return AVERROR(ENOMEM);
114 
115  jv->frames = av_malloc(ast->nb_index_entries * sizeof(JVFrame));
116  if (!jv->frames)
117  return AVERROR(ENOMEM);
118 
119  offset = 0x68 + ast->nb_index_entries * 16;
120  for (i = 0; i < ast->nb_index_entries; i++) {
121  AVIndexEntry *e = ast->index_entries + i;
122  JVFrame *jvf = jv->frames + i;
123 
124  /* total frame size including audio, video, palette data and padding */
125  e->size = avio_rl32(pb);
126  e->timestamp = i;
127  e->pos = offset;
128  offset += e->size;
129 
130  jvf->audio_size = avio_rl32(pb);
131  jvf->video_size = avio_rl32(pb);
132  jvf->palette_size = avio_r8(pb) ? 768 : 0;
133 
134  if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
135  e->size - jvf->audio_size
136  - jvf->video_size
137  - jvf->palette_size < 0) {
138  if (s->error_recognition & AV_EF_EXPLODE) {
139  read_close(s);
140  return AVERROR_INVALIDDATA;
141  }
142  jvf->audio_size =
143  jvf->video_size =
144  jvf->palette_size = 0;
145  }
146 
147  if (avio_r8(pb))
148  av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
149 
150  jvf->video_type = avio_r8(pb);
151  avio_skip(pb, 1);
152 
153  e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
154  audio_pts += jvf->audio_size;
155 
156  e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
157  }
158 
159  jv->state = JV_AUDIO;
160  return 0;
161 }
162 
164 {
165  JVDemuxContext *jv = s->priv_data;
166  AVIOContext *pb = s->pb;
167  AVStream *ast = s->streams[0];
168 
169  while (!avio_feof(s->pb) && jv->pts < ast->nb_index_entries) {
170  const AVIndexEntry *e = ast->index_entries + jv->pts;
171  const JVFrame *jvf = jv->frames + jv->pts;
172 
173  switch (jv->state) {
174  case JV_AUDIO:
175  jv->state++;
176  if (jvf->audio_size) {
177  if (av_get_packet(s->pb, pkt, jvf->audio_size) < 0)
178  return AVERROR(ENOMEM);
179  pkt->stream_index = 0;
180  pkt->pts = e->timestamp;
181  pkt->flags |= AV_PKT_FLAG_KEY;
182  return 0;
183  }
184  case JV_VIDEO:
185  jv->state++;
186  if (jvf->video_size || jvf->palette_size) {
187  int ret;
188  int size = jvf->video_size + jvf->palette_size;
189  if (av_new_packet(pkt, size + JV_PREAMBLE_SIZE))
190  return AVERROR(ENOMEM);
191 
192  AV_WL32(pkt->data, jvf->video_size);
193  pkt->data[4] = jvf->video_type;
194  ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size);
195  if (ret < 0)
196  return ret;
197  if (ret < size) {
198  memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
200  pkt->flags |= AV_PKT_FLAG_CORRUPT;
201  }
202  pkt->size = ret + JV_PREAMBLE_SIZE;
203  pkt->stream_index = 1;
204  pkt->pts = jv->pts;
205  if (jvf->video_type != 1)
206  pkt->flags |= AV_PKT_FLAG_KEY;
207  return 0;
208  }
209  case JV_PADDING:
210  avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
211  - jvf->palette_size, 0));
212  jv->state = JV_AUDIO;
213  jv->pts++;
214  }
215  }
216 
217  if (s->pb->eof_reached)
218  return AVERROR_EOF;
219 
220  return AVERROR(EIO);
221 }
222 
223 static int read_seek(AVFormatContext *s, int stream_index,
224  int64_t ts, int flags)
225 {
226  JVDemuxContext *jv = s->priv_data;
227  AVStream *ast = s->streams[0];
228  int i;
229 
230  if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
231  return AVERROR(ENOSYS);
232 
233  switch (stream_index) {
234  case 0:
235  i = av_index_search_timestamp(ast, ts, flags);
236  break;
237  case 1:
238  i = ts;
239  break;
240  default:
241  return 0;
242  }
243 
244  if (i < 0 || i >= ast->nb_index_entries)
245  return 0;
246  if (avio_seek(s->pb, ast->index_entries[i].pos, SEEK_SET) < 0)
247  return -1;
248 
249  jv->state = JV_AUDIO;
250  jv->pts = i;
251  return 0;
252 }
253 
255  .name = "jv",
256  .long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
257  .priv_data_size = sizeof(JVDemuxContext),
261  .read_seek = read_seek,
263 };
enum JVDemuxContext::@254 state
int video_type
per-frame video compression type
Definition: jvdec.c:40
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t pts
Definition: jvdec.c:50
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4882
int64_t pos
Definition: avformat.h:803
Definition: jvdec.c:36
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
int size
Definition: avcodec.h:1446
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1103
JVFrame * frames
Definition: jvdec.c:44
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
static AVPacket pkt
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: jvdec.c:163
Format I/O context.
Definition: avformat.h:1351
#define MAGIC
Definition: jvdec.c:53
#define av_malloc(s)
int width
Video only.
Definition: avcodec.h:3966
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
#define JV_PREAMBLE_SIZE
Definition: jvdec.c:34
uint8_t * data
Definition: avcodec.h:1445
#define AVERROR_EOF
End of file.
Definition: error.h:55
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:310
ptrdiff_t size
Definition: opengl_enc.c:101
static int read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: jvdec.c:223
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4002
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AVINDEX_KEYFRAME
Definition: avformat.h:810
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2148
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:770
#define AVERROR(e)
Definition: error.h:43
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:804
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
audio channel layout utility functions
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2669
int palette_size
palette size (bytes)
Definition: jvdec.c:39
static int read_close(AVFormatContext *s)
Definition: jvdec.c:63
Stream structure.
Definition: avformat.h:874
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
int nb_index_entries
Definition: avformat.h:1105
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2505
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define flags(name, subs,...)
Definition: cbs_av1.c:596
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:923
int sample_rate
Audio only.
Definition: avcodec.h:4010
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
Main libavformat public API header.
int video_size
video packet size (bytes)
Definition: jvdec.c:38
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1618
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:925
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1478
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
int eof_reached
true if eof reached
Definition: avio.h:239
void * priv_data
Format private data.
Definition: avformat.h:1379
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:2507
int channels
Audio only.
Definition: avcodec.h:4006
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3904
int stream_index
Definition: avcodec.h:1447
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1422
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1438
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int audio_size
audio packet size (bytes)
Definition: jvdec.c:37
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
AVInputFormat ff_jv_demuxer
Definition: jvdec.c:254
static int read_header(AVFormatContext *s)
Definition: jvdec.c:72