FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/jvdec.c
Date: 2024-03-28 04:31:58
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/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
30
31 #include "avformat.h"
32 #include "demux.h"
33 #include "internal.h"
34
35 #define JV_PREAMBLE_SIZE 5
36
37 typedef struct JVFrame {
38 int audio_size; /**< audio packet size (bytes) */
39 int video_size; /**< video packet size (bytes) */
40 uint16_t palette_size; /**< palette size (bytes) */
41 uint8_t video_type; /**< per-frame video compression type */
42 } JVFrame;
43
44 typedef struct JVDemuxContext {
45 JVFrame *frames;
46 enum {
47 JV_AUDIO = 0,
48 JV_VIDEO,
49 JV_PADDING
50 } state;
51 int64_t pts;
52 } JVDemuxContext;
53
54 #define MAGIC " Compression by John M Phillips Copyright (C) 1995 The Bitmap Brothers Ltd."
55
56 7124 static int read_probe(const AVProbeData *pd)
57 {
58
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7122 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
7124 if (pd->buf[0] == 'J' && pd->buf[1] == 'V' && strlen(MAGIC) + 4 <= pd->buf_size &&
59
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !memcmp(pd->buf + 4, MAGIC, strlen(MAGIC)))
60 2 return AVPROBE_SCORE_MAX;
61 7122 return 0;
62 }
63
64 2 static int read_close(AVFormatContext *s)
65 {
66 2 JVDemuxContext *jv = s->priv_data;
67
68 2 av_freep(&jv->frames);
69
70 2 return 0;
71 }
72
73 2 static int read_header(AVFormatContext *s)
74 {
75 2 JVDemuxContext *jv = s->priv_data;
76 2 AVIOContext *pb = s->pb;
77 AVStream *vst, *ast;
78 FFStream *asti;
79 2 int64_t audio_pts = 0;
80 int64_t offset;
81
82 2 avio_skip(pb, 80);
83
84 2 ast = avformat_new_stream(s, NULL);
85 2 vst = avformat_new_stream(s, NULL);
86
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!ast || !vst)
87 return AVERROR(ENOMEM);
88 2 asti = ffstream(ast);
89
90 2 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
91 2 vst->codecpar->codec_id = AV_CODEC_ID_JV;
92 2 vst->codecpar->codec_tag = 0; /* no fourcc */
93 2 vst->codecpar->width = avio_rl16(pb);
94 2 vst->codecpar->height = avio_rl16(pb);
95 2 vst->duration =
96 2 vst->nb_frames =
97 2 asti->nb_index_entries = avio_rl16(pb);
98 2 avpriv_set_pts_info(vst, 64, avio_rl16(pb), 1000);
99
100 2 avio_skip(pb, 4);
101
102 2 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
103 2 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
104 2 ast->codecpar->codec_tag = 0; /* no fourcc */
105 2 ast->codecpar->sample_rate = avio_rl16(pb);
106 2 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
107 2 avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
108
109 2 avio_skip(pb, 10);
110
111 2 asti->index_entries = av_malloc(asti->nb_index_entries *
112 sizeof(*asti->index_entries));
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!asti->index_entries)
114 return AVERROR(ENOMEM);
115
116 2 jv->frames = av_malloc(asti->nb_index_entries * sizeof(*jv->frames));
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!jv->frames)
118 return AVERROR(ENOMEM);
119 2 offset = 0x68 + asti->nb_index_entries * 16;
120
2/2
✓ Branch 0 taken 4020 times.
✓ Branch 1 taken 2 times.
4022 for (int i = 0; i < asti->nb_index_entries; i++) {
121 4020 AVIndexEntry *e = asti->index_entries + i;
122 4020 JVFrame *jvf = jv->frames + i;
123
124 /* total frame size including audio, video, palette data and padding */
125 4020 e->size = avio_rl32(pb);
126 4020 e->timestamp = i;
127 4020 e->pos = offset;
128 4020 offset += e->size;
129
130 4020 jvf->audio_size = avio_rl32(pb);
131 4020 jvf->video_size = avio_rl32(pb);
132
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 3972 times.
4020 jvf->palette_size = avio_r8(pb) ? 768 : 0;
133
134
1/2
✓ Branch 0 taken 4020 times.
✗ Branch 1 not taken.
4020 if ((jvf->video_size | jvf->audio_size) & ~0xFFFFFF ||
135 4020 e->size - jvf->audio_size
136 4020 - jvf->video_size
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4020 times.
4020 - jvf->palette_size < 0) {
138 if (s->error_recognition & AV_EF_EXPLODE)
139 return AVERROR_INVALIDDATA;
140 jvf->audio_size =
141 jvf->video_size =
142 jvf->palette_size = 0;
143 }
144
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4020 times.
4020 if (avio_r8(pb))
146 av_log(s, AV_LOG_WARNING, "unsupported audio codec\n");
147
148 4020 jvf->video_type = avio_r8(pb);
149 4020 avio_skip(pb, 1);
150
151
1/2
✓ Branch 0 taken 4020 times.
✗ Branch 1 not taken.
4020 e->timestamp = jvf->audio_size ? audio_pts : AV_NOPTS_VALUE;
152 4020 audio_pts += jvf->audio_size;
153
154 4020 e->flags = jvf->video_type != 1 ? AVINDEX_KEYFRAME : 0;
155 }
156
157 2 jv->state = JV_AUDIO;
158 2 return 0;
159 }
160
161 38 static int read_packet(AVFormatContext *s, AVPacket *pkt)
162 {
163 38 JVDemuxContext *jv = s->priv_data;
164 38 AVIOContext *pb = s->pb;
165 38 AVStream *ast = s->streams[0];
166 38 FFStream *const asti = ffstream(ast);
167 int ret;
168
169
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) {
170 52 const AVIndexEntry *const e = asti->index_entries + jv->pts;
171 52 const JVFrame *jvf = jv->frames + jv->pts;
172
173
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) {
174 18 case JV_AUDIO:
175 18 jv->state++;
176
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (jvf->audio_size) {
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_get_packet(s->pb, pkt, jvf->audio_size)) < 0)
178 return ret;
179 18 pkt->stream_index = 0;
180 18 pkt->pts = e->timestamp;
181 18 pkt->flags |= AV_PKT_FLAG_KEY;
182 18 return 0;
183 }
184 case JV_VIDEO:
185 18 jv->state++;
186
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) {
187 18 int size = jvf->video_size + jvf->palette_size;
188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_new_packet(pkt, size + JV_PREAMBLE_SIZE)) < 0)
189 return ret;
190
191 18 AV_WL32(pkt->data, jvf->video_size);
192 18 pkt->data[4] = jvf->video_type;
193 18 ret = avio_read(pb, pkt->data + JV_PREAMBLE_SIZE, size);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
195 return ret;
196
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (ret < size) {
197 2 memset(pkt->data + JV_PREAMBLE_SIZE + ret, 0,
198 AV_INPUT_BUFFER_PADDING_SIZE);
199 2 pkt->flags |= AV_PKT_FLAG_CORRUPT;
200 }
201 18 pkt->size = ret + JV_PREAMBLE_SIZE;
202 18 pkt->stream_index = 1;
203 18 pkt->pts = jv->pts;
204
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 14 times.
18 if (jvf->video_type != 1)
205 4 pkt->flags |= AV_PKT_FLAG_KEY;
206 18 return 0;
207 }
208 case JV_PADDING:
209 16 avio_skip(pb, FFMAX(e->size - jvf->audio_size - jvf->video_size
210 - jvf->palette_size, 0));
211 16 jv->state = JV_AUDIO;
212 16 jv->pts++;
213 }
214 }
215
216
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->pb->eof_reached)
217 2 return AVERROR_EOF;
218
219 return AVERROR(EIO);
220 }
221
222 static int read_seek(AVFormatContext *s, int stream_index,
223 int64_t ts, int flags)
224 {
225 JVDemuxContext *jv = s->priv_data;
226 AVStream *ast = s->streams[0];
227 FFStream *const asti = ffstream(ast);
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 >= asti->nb_index_entries)
245 return 0;
246 if (avio_seek(s->pb, asti->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
254 const FFInputFormat ff_jv_demuxer = {
255 .p.name = "jv",
256 .p.long_name = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV"),
257 .priv_data_size = sizeof(JVDemuxContext),
258 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
259 .read_probe = read_probe,
260 .read_header = read_header,
261 .read_packet = read_packet,
262 .read_seek = read_seek,
263 .read_close = read_close,
264 };
265