| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Wing Commander III Movie (.mve) File Demuxer | ||
| 3 | * Copyright (c) 2003 The FFmpeg project | ||
| 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 | * Wing Commander III Movie file demuxer | ||
| 25 | * by Mike Melanson (melanson@pcisys.net) | ||
| 26 | * for more information on the WC3 .mve file format, visit: | ||
| 27 | * http://www.pcisys.net/~melanson/codecs/ | ||
| 28 | */ | ||
| 29 | |||
| 30 | #include "libavutil/avstring.h" | ||
| 31 | #include "libavutil/channel_layout.h" | ||
| 32 | #include "libavutil/intreadwrite.h" | ||
| 33 | #include "libavutil/dict.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "avformat.h" | ||
| 36 | #include "avio_internal.h" | ||
| 37 | #include "demux.h" | ||
| 38 | #include "internal.h" | ||
| 39 | |||
| 40 | #define FORM_TAG MKTAG('F', 'O', 'R', 'M') | ||
| 41 | #define MOVE_TAG MKTAG('M', 'O', 'V', 'E') | ||
| 42 | #define PC__TAG MKTAG('_', 'P', 'C', '_') | ||
| 43 | #define SOND_TAG MKTAG('S', 'O', 'N', 'D') | ||
| 44 | #define BNAM_TAG MKTAG('B', 'N', 'A', 'M') | ||
| 45 | #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E') | ||
| 46 | #define PALT_TAG MKTAG('P', 'A', 'L', 'T') | ||
| 47 | #define INDX_TAG MKTAG('I', 'N', 'D', 'X') | ||
| 48 | #define BRCH_TAG MKTAG('B', 'R', 'C', 'H') | ||
| 49 | #define SHOT_TAG MKTAG('S', 'H', 'O', 'T') | ||
| 50 | #define VGA__TAG MKTAG('V', 'G', 'A', ' ') | ||
| 51 | #define TEXT_TAG MKTAG('T', 'E', 'X', 'T') | ||
| 52 | #define AUDI_TAG MKTAG('A', 'U', 'D', 'I') | ||
| 53 | |||
| 54 | /* video resolution unless otherwise specified */ | ||
| 55 | #define WC3_DEFAULT_WIDTH 320 | ||
| 56 | #define WC3_DEFAULT_HEIGHT 165 | ||
| 57 | |||
| 58 | /* always use the same PCM audio parameters */ | ||
| 59 | #define WC3_SAMPLE_RATE 22050 | ||
| 60 | #define WC3_AUDIO_BITS 16 | ||
| 61 | |||
| 62 | /* nice, constant framerate */ | ||
| 63 | #define WC3_FRAME_FPS 15 | ||
| 64 | |||
| 65 | #define PALETTE_SIZE (256 * 3) | ||
| 66 | |||
| 67 | typedef struct Wc3DemuxContext { | ||
| 68 | int width; | ||
| 69 | int height; | ||
| 70 | int64_t pts; | ||
| 71 | int video_stream_index; | ||
| 72 | int audio_stream_index; | ||
| 73 | |||
| 74 | AVPacket *vpkt; | ||
| 75 | |||
| 76 | } Wc3DemuxContext; | ||
| 77 | |||
| 78 | 1 | static int wc3_read_close(AVFormatContext *s) | |
| 79 | { | ||
| 80 | 1 | Wc3DemuxContext *wc3 = s->priv_data; | |
| 81 | |||
| 82 | 1 | av_packet_free(&wc3->vpkt); | |
| 83 | |||
| 84 | 1 | return 0; | |
| 85 | } | ||
| 86 | |||
| 87 | 7480 | static int wc3_probe(const AVProbeData *p) | |
| 88 | { | ||
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7480 times.
|
7480 | if (p->buf_size < 12) |
| 90 | ✗ | return 0; | |
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7458 times.
|
7480 | if ((AV_RL32(&p->buf[0]) != FORM_TAG) || |
| 93 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1 times.
|
22 | (AV_RL32(&p->buf[8]) != MOVE_TAG)) |
| 94 | 7479 | return 0; | |
| 95 | |||
| 96 | 1 | return AVPROBE_SCORE_MAX; | |
| 97 | } | ||
| 98 | |||
| 99 | 1 | static int wc3_read_header(AVFormatContext *s) | |
| 100 | { | ||
| 101 | 1 | Wc3DemuxContext *wc3 = s->priv_data; | |
| 102 | 1 | AVIOContext *pb = s->pb; | |
| 103 | unsigned int fourcc_tag; | ||
| 104 | unsigned int size; | ||
| 105 | AVStream *st; | ||
| 106 | 1 | int ret = 0; | |
| 107 | char *buffer; | ||
| 108 | |||
| 109 | /* default context members */ | ||
| 110 | 1 | wc3->width = WC3_DEFAULT_WIDTH; | |
| 111 | 1 | wc3->height = WC3_DEFAULT_HEIGHT; | |
| 112 | 1 | wc3->pts = 0; | |
| 113 | 1 | wc3->video_stream_index = wc3->audio_stream_index = 0; | |
| 114 | 1 | wc3->vpkt = av_packet_alloc(); | |
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!wc3->vpkt) |
| 116 | ✗ | return AVERROR(ENOMEM); | |
| 117 | |||
| 118 | /* skip the first 3 32-bit numbers */ | ||
| 119 | 1 | avio_skip(pb, 12); | |
| 120 | |||
| 121 | /* traverse through the chunks and load the header information before | ||
| 122 | * the first BRCH tag */ | ||
| 123 | 1 | fourcc_tag = avio_rl32(pb); | |
| 124 | 1 | size = (avio_rb32(pb) + 1) & (~1); | |
| 125 | |||
| 126 | do { | ||
| 127 |
3/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
27 | switch (fourcc_tag) { |
| 128 | |||
| 129 | 2 | case SOND_TAG: | |
| 130 | case INDX_TAG: | ||
| 131 | /* SOND unknown, INDX unnecessary; ignore both */ | ||
| 132 | 2 | avio_skip(pb, size); | |
| 133 | 2 | break; | |
| 134 | |||
| 135 | 1 | case PC__TAG: | |
| 136 | /* number of palettes, unneeded */ | ||
| 137 | 1 | avio_skip(pb, 12); | |
| 138 | 1 | break; | |
| 139 | |||
| 140 | ✗ | case BNAM_TAG: | |
| 141 | /* load up the name */ | ||
| 142 | ✗ | buffer = av_malloc(size+1); | |
| 143 | ✗ | if (!buffer) | |
| 144 | ✗ | return AVERROR(ENOMEM); | |
| 145 | ✗ | if ((ret = ffio_read_size(pb, buffer, size)) < 0) { | |
| 146 | ✗ | av_freep(&buffer); | |
| 147 | ✗ | return ret; | |
| 148 | } | ||
| 149 | ✗ | buffer[size] = 0; | |
| 150 | ✗ | av_dict_set(&s->metadata, "title", buffer, | |
| 151 | AV_DICT_DONT_STRDUP_VAL); | ||
| 152 | ✗ | break; | |
| 153 | |||
| 154 | ✗ | case SIZE_TAG: | |
| 155 | /* video resolution override */ | ||
| 156 | ✗ | wc3->width = avio_rl32(pb); | |
| 157 | ✗ | wc3->height = avio_rl32(pb); | |
| 158 | ✗ | break; | |
| 159 | |||
| 160 | 24 | case PALT_TAG: | |
| 161 | /* one of several palettes */ | ||
| 162 | 24 | avio_seek(pb, -8, SEEK_CUR); | |
| 163 | 24 | av_append_packet(pb, wc3->vpkt, 8 + PALETTE_SIZE); | |
| 164 | 24 | break; | |
| 165 | |||
| 166 | ✗ | default: | |
| 167 | ✗ | av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n", | |
| 168 | ✗ | av_fourcc2str(fourcc_tag)); | |
| 169 | ✗ | return AVERROR_INVALIDDATA; | |
| 170 | } | ||
| 171 | |||
| 172 | 27 | fourcc_tag = avio_rl32(pb); | |
| 173 | /* chunk sizes are 16-bit aligned */ | ||
| 174 | 27 | size = (avio_rb32(pb) + 1) & (~1); | |
| 175 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (avio_feof(pb)) |
| 176 | ✗ | return AVERROR_INVALIDDATA; | |
| 177 | |||
| 178 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
|
27 | } while (fourcc_tag != BRCH_TAG); |
| 179 | |||
| 180 | /* initialize the decoder streams */ | ||
| 181 | 1 | st = avformat_new_stream(s, NULL); | |
| 182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st) |
| 183 | ✗ | return AVERROR(ENOMEM); | |
| 184 | 1 | avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS); | |
| 185 | 1 | wc3->video_stream_index = st->index; | |
| 186 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 187 | 1 | st->codecpar->codec_id = AV_CODEC_ID_XAN_WC3; | |
| 188 | 1 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
| 189 | 1 | st->codecpar->width = wc3->width; | |
| 190 | 1 | st->codecpar->height = wc3->height; | |
| 191 | |||
| 192 | 1 | st = avformat_new_stream(s, NULL); | |
| 193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!st) |
| 194 | ✗ | return AVERROR(ENOMEM); | |
| 195 | 1 | avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS); | |
| 196 | 1 | wc3->audio_stream_index = st->index; | |
| 197 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 198 | 1 | st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; | |
| 199 | 1 | st->codecpar->codec_tag = 1; | |
| 200 | 1 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
| 201 | 1 | st->codecpar->bits_per_coded_sample = WC3_AUDIO_BITS; | |
| 202 | 1 | st->codecpar->sample_rate = WC3_SAMPLE_RATE; | |
| 203 | 1 | st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate * | |
| 204 | 1 | st->codecpar->bits_per_coded_sample; | |
| 205 | 1 | st->codecpar->block_align = WC3_AUDIO_BITS * st->codecpar->ch_layout.nb_channels; | |
| 206 | |||
| 207 | 1 | return 0; | |
| 208 | } | ||
| 209 | |||
| 210 | 71 | static int wc3_read_packet(AVFormatContext *s, | |
| 211 | AVPacket *pkt) | ||
| 212 | { | ||
| 213 | 71 | Wc3DemuxContext *wc3 = s->priv_data; | |
| 214 | 71 | AVIOContext *pb = s->pb; | |
| 215 | unsigned int fourcc_tag; | ||
| 216 | unsigned int size; | ||
| 217 | 71 | int packet_read = 0; | |
| 218 | 71 | int ret = 0; | |
| 219 | unsigned char text[1024]; | ||
| 220 | |||
| 221 |
2/2✓ Branch 0 taken 73 times.
✓ Branch 1 taken 70 times.
|
143 | while (!packet_read) { |
| 222 | |||
| 223 | 73 | fourcc_tag = avio_rl32(pb); | |
| 224 | /* chunk sizes are 16-bit aligned */ | ||
| 225 | 73 | size = (avio_rb32(pb) + 1) & (~1); | |
| 226 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 72 times.
|
73 | if (avio_feof(pb)) |
| 227 | 1 | return AVERROR_INVALIDDATA; | |
| 228 | |||
| 229 |
4/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 35 times.
✗ Branch 5 not taken.
|
72 | switch (fourcc_tag) { |
| 230 | |||
| 231 | ✗ | case BRCH_TAG: | |
| 232 | /* no-op */ | ||
| 233 | 72 | break; | |
| 234 | |||
| 235 | 1 | case SHOT_TAG: | |
| 236 | /* load up new palette */ | ||
| 237 | 1 | avio_seek(pb, -8, SEEK_CUR); | |
| 238 | 1 | av_append_packet(pb, wc3->vpkt, 8 + 4); | |
| 239 | 1 | break; | |
| 240 | |||
| 241 | 35 | case VGA__TAG: | |
| 242 | /* send out video chunk */ | ||
| 243 | 35 | avio_seek(pb, -8, SEEK_CUR); | |
| 244 | 35 | ret= av_append_packet(pb, wc3->vpkt, 8 + size); | |
| 245 | // ignore error if we have some data | ||
| 246 |
1/2✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
|
35 | if (wc3->vpkt->size > 0) |
| 247 | 35 | ret = 0; | |
| 248 | 35 | av_packet_move_ref(pkt, wc3->vpkt); | |
| 249 | 35 | pkt->stream_index = wc3->video_stream_index; | |
| 250 | 35 | pkt->pts = wc3->pts; | |
| 251 | 35 | packet_read = 1; | |
| 252 | 35 | break; | |
| 253 | |||
| 254 | 1 | case TEXT_TAG: | |
| 255 | /* subtitle chunk */ | ||
| 256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if ((unsigned)size > sizeof(text)) |
| 257 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 258 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | else if ((ret = ffio_read_size(pb, text, size)) == size) { |
| 259 | 1 | int i = 0; | |
| 260 | 1 | av_log (s, AV_LOG_DEBUG, "Subtitle time!\n"); | |
| 261 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1) |
| 262 | ✗ | return AVERROR_INVALIDDATA; | |
| 263 | 1 | av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]); | |
| 264 | 1 | i += text[i] + 1; | |
| 265 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1) |
| 266 | ✗ | return AVERROR_INVALIDDATA; | |
| 267 | 1 | av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]); | |
| 268 | 1 | i += text[i] + 1; | |
| 269 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1) |
| 270 | ✗ | return AVERROR_INVALIDDATA; | |
| 271 | 1 | av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]); | |
| 272 | } | ||
| 273 | 1 | break; | |
| 274 | |||
| 275 | 35 | case AUDI_TAG: | |
| 276 | /* send out audio chunk */ | ||
| 277 | 35 | ret= av_get_packet(pb, pkt, size); | |
| 278 | 35 | pkt->stream_index = wc3->audio_stream_index; | |
| 279 | 35 | pkt->pts = wc3->pts; | |
| 280 | |||
| 281 | /* time to advance pts */ | ||
| 282 | 35 | wc3->pts++; | |
| 283 | |||
| 284 | 35 | packet_read = 1; | |
| 285 | 35 | break; | |
| 286 | |||
| 287 | ✗ | default: | |
| 288 | ✗ | av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n", | |
| 289 | ✗ | av_fourcc2str(fourcc_tag)); | |
| 290 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 291 | ✗ | packet_read = 1; | |
| 292 | ✗ | break; | |
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | 70 | return ret; | |
| 297 | } | ||
| 298 | |||
| 299 | const FFInputFormat ff_wc3_demuxer = { | ||
| 300 | .p.name = "wc3movie", | ||
| 301 | .p.long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"), | ||
| 302 | .priv_data_size = sizeof(Wc3DemuxContext), | ||
| 303 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 304 | .read_probe = wc3_probe, | ||
| 305 | .read_header = wc3_read_header, | ||
| 306 | .read_packet = wc3_read_packet, | ||
| 307 | .read_close = wc3_read_close, | ||
| 308 | }; | ||
| 309 |