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