FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/bethsoftvid.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 116 150 77.3%
Functions: 4 4 100.0%
Branches: 44 68 64.7%

Line Branch Exec Source
1 /*
2 * Bethsoft VID format Demuxer
3 * Copyright (c) 2007 Nicholas Tung
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 * @brief Bethesda Softworks VID (.vid) file demuxer
25 * @author Nicholas Tung [ntung (at. ntung com] (2007-03)
26 * @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
27 * @see http://www.svatopluk.com/andux/docs/dfvid.html
28 */
29
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "avformat.h"
34 #include "internal.h"
35 #include "libavcodec/bethsoftvideo.h"
36
37 #define BVID_PALETTE_SIZE 3 * 256
38
39 #define DEFAULT_SAMPLE_RATE 11111
40
41 typedef struct BVID_DemuxContext
42 {
43 int nframes;
44 int sample_rate; /**< audio sample rate */
45 int width; /**< video width */
46 int height; /**< video height */
47 /** delay value between frames, added to individual frame delay.
48 * custom units, which will be added to other custom units (~=16ms according
49 * to free, unofficial documentation) */
50 int bethsoft_global_delay;
51 int video_index; /**< video stream index */
52 int audio_index; /**< audio stream index */
53 int has_palette;
54 uint8_t palette[BVID_PALETTE_SIZE];
55
56 int is_finished;
57
58 } BVID_DemuxContext;
59
60 6782 static int vid_probe(const AVProbeData *p)
61 {
62 // little-endian VID tag, file starts with "VID\0"
63
2/2
✓ Branch 0 taken 6781 times.
✓ Branch 1 taken 1 times.
6782 if (AV_RL32(p->buf) != MKTAG('V', 'I', 'D', 0))
64 6781 return 0;
65
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (p->buf[4] != 2)
67 return AVPROBE_SCORE_MAX / 4;
68
69 1 return AVPROBE_SCORE_MAX;
70 }
71
72 1 static int vid_read_header(AVFormatContext *s)
73 {
74 1 BVID_DemuxContext *vid = s->priv_data;
75 1 AVIOContext *pb = s->pb;
76 int ret;
77
78 /* load main header. Contents:
79 * bytes: 'V' 'I' 'D'
80 * int16s: always_512, nframes, width, height, delay, always_14
81 */
82 1 avio_skip(pb, 5);
83 1 vid->nframes = avio_rl16(pb);
84 1 vid->width = avio_rl16(pb);
85 1 vid->height = avio_rl16(pb);
86 1 vid->bethsoft_global_delay = avio_rl16(pb);
87 1 avio_rl16(pb);
88
89 1 ret = av_image_check_size(vid->width, vid->height, 0, s);
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
91 return ret;
92
93 // wait until the first packet to create each stream
94 1 vid->video_index = -1;
95 1 vid->audio_index = -1;
96 1 vid->sample_rate = DEFAULT_SAMPLE_RATE;
97 1 s->ctx_flags |= AVFMTCTX_NOHEADER;
98
99 1 return 0;
100 }
101
102 #define BUFFER_PADDING_SIZE 1000
103 77 static int read_frame(BVID_DemuxContext *vid, AVIOContext *pb, AVPacket *pkt,
104 uint8_t block_type, AVFormatContext *s)
105 {
106 77 uint8_t * vidbuf_start = NULL;
107 77 int vidbuf_nbytes = 0;
108 int code;
109 77 int bytes_copied = 0;
110 int position, duration, npixels;
111 unsigned int vidbuf_capacity;
112 77 int ret = 0;
113 AVStream *st;
114
115
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 76 times.
77 if (vid->video_index < 0) {
116 1 st = avformat_new_stream(s, NULL);
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
118 return AVERROR(ENOMEM);
119 1 vid->video_index = st->index;
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (vid->audio_index < 0) {
121 avpriv_request_sample(s, "Using default video time base since "
122 "having no audio packet before the first "
123 "video packet");
124 }
125 1 avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
126 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
127 1 st->codecpar->codec_id = AV_CODEC_ID_BETHSOFTVID;
128 1 st->codecpar->width = vid->width;
129 1 st->codecpar->height = vid->height;
130 }
131 77 st = s->streams[vid->video_index];
132 77 npixels = st->codecpar->width * st->codecpar->height;
133
134 77 vidbuf_start = av_malloc(vidbuf_capacity = BUFFER_PADDING_SIZE);
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if(!vidbuf_start)
136 return AVERROR(ENOMEM);
137
138 // save the file position for the packet, include block type
139 77 position = avio_tell(pb) - 1;
140
141 77 vidbuf_start[vidbuf_nbytes++] = block_type;
142
143 // get the current packet duration
144 77 duration = vid->bethsoft_global_delay + avio_rl16(pb);
145
146 // set the y offset if it exists (decoder header data should be in data section)
147
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1 times.
77 if(block_type == VIDEO_YOFF_P_FRAME){
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
76 if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], 2) != 2) {
149 ret = AVERROR(EIO);
150 goto fail;
151 }
152 76 vidbuf_nbytes += 2;
153 }
154
155 do{
156 234453 uint8_t *tmp = av_fast_realloc(vidbuf_start, &vidbuf_capacity,
157 234453 vidbuf_nbytes + BUFFER_PADDING_SIZE);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234453 times.
234453 if (!tmp) {
159 ret = AVERROR(ENOMEM);
160 goto fail;
161 }
162 234453 vidbuf_start = tmp;
163
164 234453 code = avio_r8(pb);
165 234453 vidbuf_start[vidbuf_nbytes++] = code;
166
167
2/2
✓ Branch 0 taken 120639 times.
✓ Branch 1 taken 113814 times.
234453 if(code >= 0x80){ // rle sequence
168
2/2
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 120135 times.
120639 if(block_type == VIDEO_I_FRAME)
169 504 vidbuf_start[vidbuf_nbytes++] = avio_r8(pb);
170
2/2
✓ Branch 0 taken 113738 times.
✓ Branch 1 taken 76 times.
113814 } else if(code){ // plain sequence
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 113738 times.
113738 if (avio_read(pb, &vidbuf_start[vidbuf_nbytes], code) != code) {
172 ret = AVERROR(EIO);
173 goto fail;
174 }
175 113738 vidbuf_nbytes += code;
176 }
177 234453 bytes_copied += code & 0x7F;
178
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 234452 times.
234453 if(bytes_copied == npixels){ // sometimes no stop character is given, need to keep track of bytes copied
179 // may contain a 0 byte even if read all pixels
180
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if(avio_r8(pb))
181 1 avio_seek(pb, -1, SEEK_CUR);
182 1 break;
183 }
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234452 times.
234452 if (bytes_copied > npixels) {
185 ret = AVERROR_INVALIDDATA;
186 goto fail;
187 }
188
2/2
✓ Branch 0 taken 234376 times.
✓ Branch 1 taken 76 times.
234452 } while(code);
189
190 // copy data into packet
191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if ((ret = av_new_packet(pkt, vidbuf_nbytes)) < 0)
192 goto fail;
193 77 memcpy(pkt->data, vidbuf_start, vidbuf_nbytes);
194
195 77 pkt->pos = position;
196 77 pkt->stream_index = vid->video_index;
197 77 pkt->duration = duration;
198
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 76 times.
77 if (block_type == VIDEO_I_FRAME)
199 1 pkt->flags |= AV_PKT_FLAG_KEY;
200
201 /* if there is a new palette available, add it to packet side data */
202
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 76 times.
77 if (vid->has_palette) {
203 1 uint8_t *pdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
204 BVID_PALETTE_SIZE);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pdata) {
206 ret = AVERROR(ENOMEM);
207 av_log(s, AV_LOG_ERROR, "Failed to allocate palette side data\n");
208 goto fail;
209 }
210 1 memcpy(pdata, vid->palette, BVID_PALETTE_SIZE);
211 1 vid->has_palette = 0;
212 }
213
214 77 vid->nframes--; // used to check if all the frames were read
215 77 fail:
216 77 av_free(vidbuf_start);
217 77 return ret;
218 }
219
220 151 static int vid_read_packet(AVFormatContext *s,
221 AVPacket *pkt)
222 {
223 151 BVID_DemuxContext *vid = s->priv_data;
224 151 AVIOContext *pb = s->pb;
225 unsigned char block_type;
226 int audio_length;
227 int ret_value;
228
229
2/4
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 151 times.
151 if(vid->is_finished || avio_feof(pb))
230 return AVERROR_EOF;
231
232 151 block_type = avio_r8(pb);
233
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 71 times.
✓ Branch 3 taken 77 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
151 switch(block_type){
234 1 case PALETTE_BLOCK:
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (vid->has_palette) {
236 av_log(s, AV_LOG_WARNING, "discarding unused palette\n");
237 vid->has_palette = 0;
238 }
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_read(pb, vid->palette, BVID_PALETTE_SIZE) != BVID_PALETTE_SIZE) {
240 return AVERROR(EIO);
241 }
242 1 vid->has_palette = 1;
243 1 return vid_read_packet(s, pkt);
244
245 2 case FIRST_AUDIO_BLOCK:
246 2 avio_rl16(pb);
247 // soundblaster DAC used for sample rate, as on specification page (link above)
248 2 vid->sample_rate = 1000000 / (256 - avio_r8(pb));
249 73 case AUDIO_BLOCK:
250
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 72 times.
73 if (vid->audio_index < 0) {
251 1 AVStream *st = avformat_new_stream(s, NULL);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
253 return AVERROR(ENOMEM);
254 1 vid->audio_index = st->index;
255 1 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
256 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
257 1 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
258 1 st->codecpar->bits_per_coded_sample = 8;
259 1 st->codecpar->sample_rate = vid->sample_rate;
260 1 st->codecpar->bit_rate = 8 * st->codecpar->sample_rate;
261 1 st->start_time = 0;
262 1 avpriv_set_pts_info(st, 64, 1, vid->sample_rate);
263 }
264 73 audio_length = avio_rl16(pb);
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
73 if ((ret_value = av_get_packet(pb, pkt, audio_length)) != audio_length) {
266 if (ret_value < 0)
267 return ret_value;
268 av_log(s, AV_LOG_ERROR, "incomplete audio block\n");
269 return AVERROR(EIO);
270 }
271 73 pkt->stream_index = vid->audio_index;
272 73 pkt->duration = audio_length;
273 73 pkt->flags |= AV_PKT_FLAG_KEY;
274 73 return 0;
275
276 77 case VIDEO_P_FRAME:
277 case VIDEO_YOFF_P_FRAME:
278 case VIDEO_I_FRAME:
279 77 return read_frame(vid, pb, pkt, block_type, s);
280
281 case EOF_BLOCK:
282 if(vid->nframes != 0)
283 av_log(s, AV_LOG_VERBOSE, "reached terminating character but not all frames read.\n");
284 vid->is_finished = 1;
285 return AVERROR(EIO);
286 default:
287 av_log(s, AV_LOG_ERROR, "unknown block (character = %c, decimal = %d, hex = %x)!!!\n",
288 block_type, block_type, block_type);
289 return AVERROR_INVALIDDATA;
290 }
291 }
292
293 const AVInputFormat ff_bethsoftvid_demuxer = {
294 .name = "bethsoftvid",
295 .long_name = NULL_IF_CONFIG_SMALL("Bethesda Softworks VID"),
296 .priv_data_size = sizeof(BVID_DemuxContext),
297 .read_probe = vid_probe,
298 .read_header = vid_read_header,
299 .read_packet = vid_read_packet,
300 };
301