FFmpeg
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 #include "libavutil/mem.h"
31 
32 #include "avformat.h"
33 #include "demux.h"
34 #include "internal.h"
35 
36 #define JV_PREAMBLE_SIZE 5
37 
38 typedef struct JVFrame {
39  int audio_size; /**< audio packet size (bytes) */
40  int video_size; /**< video packet size (bytes) */
41  uint16_t palette_size; /**< palette size (bytes) */
42  uint8_t video_type; /**< per-frame video compression type */
43 } JVFrame;
44 
45 typedef struct JVDemuxContext {
47  enum {
48  JV_AUDIO = 0,
51  } state;
54 
55 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
56 
57 static int read_probe(const AVProbeData *pd)
58 {
59  if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
60  !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
61  return AVPROBE_SCORE_MAX;
62  return 0;
63 }
64 
66 {
67  JVDemuxContext *jv = s->priv_data;
68 
69  av_freep(&jv->frames);
70 
71  return 0;
72 }
73 
75 {
76  JVDemuxContext *jv = s->priv_data;
77  AVIOContext *pb = s->pb;
78  AVStream *vst, *ast;
79  FFStream *asti;
80  int64_t audio_pts = 0;
82 
83  avio_skip(pb, 80);
84 
85  ast = avformat_new_stream(s, NULL);
86  vst = avformat_new_stream(s, NULL);
87  if (!ast || !vst)
88  return AVERROR(ENOMEM);
89  asti = ffstream(ast);
90 
93  vst->codecpar->codec_tag = 0; /* no fourcc */
94  vst->codecpar->width = avio_rl16(pb);
95  vst->codecpar->height = avio_rl16(pb);
96  vst->duration =
97  vst->nb_frames =
98  asti->nb_index_entries = avio_rl16(pb);
99  avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
100 
101  avio_skip(pb, 4);
102 
105  ast->codecpar->codec_tag = 0; /* no fourcc */
106  ast->codecpar->sample_rate = avio_rl16(pb);
108  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
109 
110  avio_skip(pb, 10);
111 
113  sizeof(*asti->index_entries));
114  if (!asti->index_entries)
115  return AVERROR(ENOMEM);
116 
117  jv->frames = av_malloc(asti->nb_index_entries * sizeof(*jv->frames));
118  if (!jv->frames)
119  return AVERROR(ENOMEM);
120  offset = 0x68 + asti->nb_index_entries * 16;
121  for (int i = 0; i < asti->nb_index_entries; i++) {
122  AVIndexEntry *e = asti->index_entries + i;
123  JVFrame *jvf = jv->frames + i;
124 
125  /* total frame size including audio, video, palette data and padding */
126  e->size = avio_rl32(pb);
127  e->timestamp = i;
128  e->pos = offset;
129  offset += e->size;
130 
131  jvf->audio_size = avio_rl32(pb);
132  jvf->video_size = avio_rl32(pb);
133  jvf->palette_size = avio_r8(pb) ? 768 : 0;
134 
135  if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
136  e->size - jvf->audio_size
137  - jvf->video_size
138  - jvf->palette_size < 0) {
139  if (s->error_recognition & AV_EF_EXPLODE)
140  return AVERROR_INVALIDDATA;
141  jvf->audio_size =
142  jvf->video_size =
143  jvf->palette_size = 0;
144  }
145 
146  if (avio_r8(pb))
147  av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
148 
149  jvf->video_type = avio_r8(pb);
150  avio_skip(pb, 1);
151 
152  e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
153  audio_pts += jvf->audio_size;
154 
155  e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
156  }
157 
158  jv->state = JV_AUDIO;
159  return 0;
160 }
161 
163 {
164  JVDemuxContext *jv = s->priv_data;
165  AVIOContext *pb = s->pb;
166  AVStream *ast = s->streams[0];
167  FFStream *const asti = ffstream(ast);
168  int ret;
169 
170  while (!avio_feof(s->pb) && jv->pts < asti->nb_index_entries) {
171  const AVIndexEntry *const e = asti->index_entries + jv->pts;
172  const JVFrame *jvf = jv->frames + jv->pts;
173 
174  switch (jv->state) {
175  case JV_AUDIO:
176  jv->state++;
177  if (jvf->audio_size) {
178  if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0)
179  return ret;
180  pkt->stream_index = 0;
181  pkt->pts = e->timestamp;
183  return 0;
184  }
185  case JV_VIDEO:
186  jv->state++;
187  if (jvf->video_size || jvf->palette_size) {
188  int size = jvf->video_size + jvf->palette_size;
189  if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0)
190  return ret;
191 
192  AV_WL32(pkt->data, jvf->video_size);
193  pkt->data[4] = jvf->video_type;
195  if (ret < 0)
196  return ret;
197  if (ret < size) {
198  memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
201  }
203  pkt->stream_index = 1;
204  pkt->pts = jv->pts;
205  if (jvf->video_type != 1)
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  FFStream *const asti = ffstream(ast);
229  int i;
230 
232  return AVERROR(ENOSYS);
233 
234  switch (stream_index) {
235  case 0:
236  i = av_index_search_timestamp(ast, ts, flags);
237  break;
238  case 1:
239  i = ts;
240  break;
241  default:
242  return 0;
243  }
244 
245  if (i < 0 || i >= asti->nb_index_entries)
246  return 0;
247  if (avio_seek(s->pb, asti->index_entries[i].pos, SEEK_SET) < 0)
248  return -1;
249 
250  jv->state = JV_AUDIO;
251  jv->pts = i;
252  return 0;
253 }
254 
256  .p.name = "jv",
257  .p.long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
258  .priv_data_size = sizeof(JVDemuxContext),
259  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
263  .read_seek = read_seek,
265 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
AVSEEK_FLAG_FRAME
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:2501
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:539
JVDemuxContext::JV_AUDIO
@ JV_AUDIO
Definition: jvdec.c:48
AVSEEK_FLAG_BYTE
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2499
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
JVFrame
Definition: jvdec.c:38
JVFrame::video_size
int video_size
video packet size (bytes)
Definition: jvdec.c:40
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
AVIndexEntry
Definition: avformat.h:602
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:610
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:807
JVDemuxContext::pts
int64_t pts
Definition: jvdec.c:52
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
read_close
static int read_close(AVFormatContext *s)
Definition: jvdec.c:65
pkt
AVPacket * pkt
Definition: movenc.c:60
JVFrame::audio_size
int audio_size
audio packet size (bytes)
Definition: jvdec.c:39
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:595
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
AVIndexEntry::size
int size
Definition: avformat.h:613
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:604
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: jvdec.c:162
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:57
AVIndexEntry::flags
int flags
Definition: avformat.h:612
JVDemuxContext::JV_PADDING
@ JV_PADDING
Definition: jvdec.c:50
FFStream::nb_index_entries
int nb_index_entries
Definition: internal.h:190
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
JVDemuxContext::state
enum JVDemuxContext::@393 state
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:809
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:540
MAGIC
#define MAGIC
Definition: jvdec.c:55
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
FFStream
Definition: internal.h:132
size
int size
Definition: twinvq_data.h:10344
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
JVFrame::video_type
uint8_t video_type
per-frame video compression type
Definition: jvdec.c:42
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
JV_PREAMBLE_SIZE
#define JV_PREAMBLE_SIZE
Definition: jvdec.c:36
read_header
static int read_header(AVFormatContext *s)
Definition: jvdec.c:74
read_seek
static int read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: jvdec.c:223
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
JVDemuxContext::JV_VIDEO
@ JV_VIDEO
Definition: jvdec.c:49
AVCodecParameters::height
int height
Definition: codec_par.h:135
demux.h
av_get_packet
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:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
avformat.h
AV_CODEC_ID_JV
@ AV_CODEC_ID_JV
Definition: codec_id.h:201
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ff_jv_demuxer
const FFInputFormat ff_jv_demuxer
Definition: jvdec.c:255
channel_layout.h
JVFrame::palette_size
uint16_t palette_size
palette size (bytes)
Definition: jvdec.c:41
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:603
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
FFStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: internal.h:188
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:339
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:392
JVDemuxContext::frames
JVFrame * frames
Definition: jvdec.c:46
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:42
JVDemuxContext
Definition: jvdec.c:45
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: seek.c:245
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346