FFmpeg
Loading...
Searching...
No Matches
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
31#include "libavutil/mem.h"
32
33#include "avformat.h"
34#include "demux.h"
35#include "internal.h"
36
37#define JV_PREAMBLE_SIZE 5
38
39typedef struct JVFrame {
40 int audio_size; /**< audio packet size (bytes) */
41 int video_size; /**< video packet size (bytes) */
42 uint16_t palette_size; /**< palette size (bytes) */
43 uint8_t video_type; /**< per-frame video compression type */
44} JVFrame;
45
55
56#define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
57
58static int read_probe(const AVProbeData *pd)
59{
60 if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
61 !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
62 return AVPROBE_SCORE_MAX;
63 return 0;
64}
65
67{
68 JVDemuxContext *jv = s->priv_data;
69
70 av_freep(&jv->frames);
71
72 return 0;
73}
74
76{
77 JVDemuxContext *jv = s->priv_data;
78 AVIOContext *pb = s->pb;
79 AVStream *vst, *ast;
80 FFStream *asti;
81 int64_t audio_pts = 0;
83
84 avio_skip(pb, 80);
85
88 if (!ast || !vst)
89 return AVERROR(ENOMEM);
90 asti = ffstream(ast);
91
94 vst->codecpar->codec_tag = 0; /* no fourcc */
95 vst->codecpar->width = avio_rl16(pb);
96 vst->codecpar->height = avio_rl16(pb);
97 vst->duration =
98 vst->nb_frames =
99 asti->nb_index_entries = avio_rl16(pb);
100 avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
101
102 avio_skip(pb, 4);
103
106 ast->codecpar->codec_tag = 0; /* no fourcc */
107 ast->codecpar->sample_rate = avio_rl16(pb);
109 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
110
111 avio_skip(pb, 10);
112
114 sizeof(*asti->index_entries));
115 if (!asti->index_entries)
116 return AVERROR(ENOMEM);
117
118 jv->frames = av_malloc(asti->nb_index_entries * sizeof(*jv->frames));
119 if (!jv->frames)
120 return AVERROR(ENOMEM);
121 offset = 0x68 + asti->nb_index_entries * 16;
122 for (int i = 0; i < asti->nb_index_entries; i++) {
123 AVIndexEntry *e = asti->index_entries + i;
124 JVFrame *jvf = jv->frames + i;
125
126 /* total frame size including audio, video, palette data and padding */
127 e->size = avio_rl32(pb);
128 e->timestamp = i;
129 e->pos = offset;
130 offset += e->size;
131
132 jvf->audio_size = avio_rl32(pb);
133 jvf->video_size = avio_rl32(pb);
134 jvf->palette_size = avio_r8(pb) ? 768 : 0;
135
136 if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
137 e->size - jvf->audio_size
138 - jvf->video_size
139 - jvf->palette_size < 0) {
140 if (s->error_recognition & AV_EF_EXPLODE)
141 return AVERROR_INVALIDDATA;
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 FFStream *const asti = ffstream(ast);
169 int ret;
170
171 while (!avio_feof(s->pb) && jv->pts < asti->nb_index_entries) {
172 const AVIndexEntry *const e = asti->index_entries + jv->pts;
173 const JVFrame *jvf = jv->frames + jv->pts;
174
175 switch (jv->state) {
176 case JV_AUDIO:
177 jv->state++;
178 if (jvf->audio_size) {
179 if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0)
180 return ret;
181 pkt->stream_index = 0;
182 pkt->pts = e->timestamp;
183 pkt->flags |= AV_PKT_FLAG_KEY;
184 return 0;
185 }
187 case JV_VIDEO:
188 jv->state++;
189 if (jvf->video_size || jvf->palette_size) {
190 int size = jvf->video_size + jvf->palette_size;
191 if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0)
192 return ret;
193
194 AV_WL32(pkt->data, jvf->video_size);
195 pkt->data[4] = jvf->video_type;
196 ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size);
197 if (ret < 0)
198 return ret;
199 if (ret < size) {
200 memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
202 pkt->flags |= AV_PKT_FLAG_CORRUPT;
203 }
204 pkt->size = ret + JV_PREAMBLE_SIZE;
205 pkt->stream_index = 1;
206 pkt->pts = jv->pts;
207 if (jvf->video_type != 1)
208 pkt->flags |= AV_PKT_FLAG_KEY;
209 return 0;
210 }
212 case JV_PADDING:
213 avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
214 - jvf->palette_size, 0));
215 jv->state = JV_AUDIO;
216 jv->pts++;
217 }
218 }
219
220 if (s->pb->eof_reached)
221 return AVERROR_EOF;
222
223 return AVERROR_INVALIDDATA;
224}
225
226static int read_seek(AVFormatContext *s, int stream_index,
227 int64_t ts, int flags)
228{
229 JVDemuxContext *jv = s->priv_data;
230 AVStream *ast = s->streams[0];
231 FFStream *const asti = ffstream(ast);
232 int i;
233
235 return AVERROR(ENOSYS);
236
237 switch (stream_index) {
238 case 0:
240 break;
241 case 1:
242 i = ts;
243 break;
244 default:
245 return 0;
246 }
247
249 return 0;
250 if (avio_seek(s->pb, asti->index_entries[i].pos, SEEK_SET) < 0)
251 return -1;
252
253 jv->state = JV_AUDIO;
254 jv->pts = i;
255 return 0;
256}
257
259 .p.name = "jv",
260 .p.long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
261 .priv_data_size = sizeof(JVDemuxContext),
262 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
268};
const FFInputFormat ff_jv_demuxer
Definition jvdec.c:258
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:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition avformat.h:2617
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:98
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition avformat.h:2619
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
#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
static AVPacket * pkt
static struct @346255127015250356166251341105367306144006377143 state
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_JV
Definition codec_id.h:199
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
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
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_WL32(p, v)
unsigned offset
Definition libaomenc.c:763
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define JV_PREAMBLE_SIZE
Definition jvdec.c:37
static int read_close(AVFormatContext *s)
Definition jvdec.c:66
static int read_probe(const AVProbeData *pd)
Definition jvdec.c:58
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition jvdec.c:163
static int read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition jvdec.c:226
#define MAGIC
Definition jvdec.c:56
static int read_header(AVFormatContext *s)
Definition jvdec.c:75
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
An AVChannelLayout holds information about the channel layout of audio data.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int64_t pos
Definition avformat.h:621
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:622
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
int buf_size
Size of buf except extra allocated bytes.
Definition avformat.h:474
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:473
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
int64_t pts
Definition jvdec.c:53
enum JVDemuxContext::@370157173232171063375120312265053074235201217031 state
JVFrame * frames
Definition jvdec.c:47
uint16_t palette_size
palette size (bytes)
Definition jvdec.c:42
int audio_size
audio packet size (bytes)
Definition jvdec.c:40
int video_size
video packet size (bytes)
Definition jvdec.c:41
uint8_t video_type
per-frame video compression type
Definition jvdec.c:43
#define av_freep(p)
#define av_log(a,...)
int size