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