FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/jvdec.c
Date: 2026-09-12 04:54:02
Exec Total Coverage
Lines: 101 136 74.3%
Functions: 4 5 80.0%
Branches: 35 67 52.2%

Line Branch Exec Source
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
28 #include "libavutil/attributes.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/intreadwrite.h"
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
39 typedef 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
46 typedef struct JVDemuxContext {
47 JVFrame *frames;
48 enum {
49 JV_AUDIO = 0,
50 JV_VIDEO,
51 JV_PADDING
52 } state;
53 int64_t pts;
54 } JVDemuxContext;
55
56 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
57
58 8041 static int read_probe(const AVProbeData *pd)
59 {
60
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8039 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
8041 if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
61
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
62 2 return AVPROBE_SCORE_MAX;
63 8039 return 0;
64 }
65
66 2 static int read_close(AVFormatContext *s)
67 {
68 2 JVDemuxContext *jv = s->priv_data;
69
70 2 av_freep(&jv->frames);
71
72 2 return 0;
73 }
74
75 2 static int read_header(AVFormatContext *s)
76 {
77 2 JVDemuxContext *jv = s->priv_data;
78 2 AVIOContext *pb = s->pb;
79 AVStream *vst, *ast;
80 FFStream *asti;
81 2 int64_t audio_pts = 0;
82 int64_t offset;
83
84 2 avio_skip(pb, 80);
85
86 2 ast = avformat_new_stream(s, NULL);
87 2 vst = avformat_new_stream(s, NULL);
88
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!ast || !vst)
89 return AVERROR(ENOMEM);
90 2 asti = ffstream(ast);
91
92 2 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
93 2 vst->codecpar->codec_id = AV_CODEC_ID_JV;
94 2 vst->codecpar->codec_tag = 0; /* no fourcc */
95 2 vst->codecpar->width = avio_rl16(pb);
96 2 vst->codecpar->height = avio_rl16(pb);
97 2 vst->duration =
98 2 vst->nb_frames =
99 2 asti->nb_index_entries = avio_rl16(pb);
100 2 avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
101
102 2 avio_skip(pb, 4);
103
104 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
105 2 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
106 2 ast->codecpar->codec_tag = 0; /* no fourcc */
107 2 ast->codecpar->sample_rate = avio_rl16(pb);
108 2 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
109 2 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
110
111 2 avio_skip(pb, 10);
112
113 2 asti->index_entries = av_malloc(asti->nb_index_entries *
114 sizeof(*asti->index_entries));
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!asti->index_entries)
116 return AVERROR(ENOMEM);
117
118 2 jv->frames = av_malloc(asti->nb_index_entries * sizeof(*jv->frames));
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!jv->frames)
120 return AVERROR(ENOMEM);
121 2 offset = 0x68 + asti->nb_index_entries * 16;
122
2/2
✓ Branch 0 taken 4020 times.
✓ Branch 1 taken 2 times.
4022 for (int i = 0; i < asti->nb_index_entries; i++) {
123 4020 AVIndexEntry *e = asti->index_entries + i;
124 4020 JVFrame *jvf = jv->frames + i;
125
126 /* total frame size including audio, video, palette data and padding */
127 4020 e->size = avio_rl32(pb);
128 4020 e->timestamp = i;
129 4020 e->pos = offset;
130 4020 offset += e->size;
131
132 4020 jvf->audio_size = avio_rl32(pb);
133 4020 jvf->video_size = avio_rl32(pb);
134
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 3972 times.
4020 jvf->palette_size = avio_r8(pb) ? 768 : 0;
135
136
1/2
✓ Branch 0 taken 4020 times.
✗ Branch 1 not taken.
4020 if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
137 4020 e->size - jvf->audio_size
138 4020 - jvf->video_size
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4020 times.
4020 - 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4020 times.
4020 if (avio_r8(pb))
148 av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
149
150 4020 jvf->video_type = avio_r8(pb);
151 4020 avio_skip(pb, 1);
152
153
1/2
✓ Branch 0 taken 4020 times.
✗ Branch 1 not taken.
4020 e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
154 4020 audio_pts += jvf->audio_size;
155
156 4020 e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
157 }
158
159 2 jv->state = JV_AUDIO;
160 2 return 0;
161 }
162
163 38 static int read_packet(AVFormatContext *s, AVPacket *pkt)
164 {
165 38 JVDemuxContext *jv = s->priv_data;
166 38 AVIOContext *pb = s->pb;
167 38 AVStream *ast = s->streams[0];
168 38 FFStream *const asti = ffstream(ast);
169 int ret;
170
171
3/4
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
92 while (!avio_feof(s->pb) && jv->pts < asti->nb_index_entries) {
172 52 const AVIndexEntry *const e = asti->index_entries + jv->pts;
173 52 const JVFrame *jvf = jv->frames + jv->pts;
174
175
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 16 times.
52 switch (jv->state) {
176 18 case JV_AUDIO:
177 18 jv->state++;
178
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (jvf->audio_size) {
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0)
180 return ret;
181 18 pkt->stream_index = 0;
182 18 pkt->pts = e->timestamp;
183 18 pkt->flags |= AV_PKT_FLAG_KEY;
184 18 return 0;
185 }
186 av_fallthrough;
187 case JV_VIDEO:
188 18 jv->state++;
189
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
18 if (jvf->video_size || jvf->palette_size) {
190 18 int size = jvf->video_size + jvf->palette_size;
191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0)
192 return ret;
193
194 18 AV_WL32(pkt->data, jvf->video_size);
195 18 pkt->data[4] = jvf->video_type;
196 18 ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
198 return ret;
199
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (ret < size) {
200 2 memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
201 AV_INPUT_BUFFER_PADDING_SIZE);
202 2 pkt->flags |= AV_PKT_FLAG_CORRUPT;
203 }
204 18 pkt->size = ret + JV_PREAMBLE_SIZE;
205 18 pkt->stream_index = 1;
206 18 pkt->pts = jv->pts;
207
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
18 if (jvf->video_type != 1)
208 4 pkt->flags |= AV_PKT_FLAG_KEY;
209 18 return 0;
210 }
211 av_fallthrough;
212 case JV_PADDING:
213 16 avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
214 - jvf->palette_size, 0));
215 16 jv->state = JV_AUDIO;
216 16 jv->pts++;
217 }
218 }
219
220
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->pb->eof_reached)
221 2 return AVERROR_EOF;
222
223 return AVERROR_INVALIDDATA;
224 }
225
226 static 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
234 if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
235 return AVERROR(ENOSYS);
236
237 switch (stream_index) {
238 case 0:
239 i = av_index_search_timestamp(ast, ts, flags);
240 break;
241 case 1:
242 i = ts;
243 break;
244 default:
245 return 0;
246 }
247
248 if (i < 0 || i >= asti->nb_index_entries)
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
258 const FFInputFormat ff_jv_demuxer = {
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,
263 .read_probe = read_probe,
264 .read_header = read_header,
265 .read_packet = read_packet,
266 .read_seek = read_seek,
267 .read_close = read_close,
268 };
269