annotate ffmpeg/libavformat/idcin.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * id Quake II CIN File Demuxer
yading@11 3 * Copyright (c) 2003 The ffmpeg Project
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 /**
yading@11 23 * @file
yading@11 24 * id Quake II CIN file demuxer by Mike Melanson (melanson@pcisys.net)
yading@11 25 * For more information about the id CIN format, visit:
yading@11 26 * http://www.csse.monash.edu.au/~timf/
yading@11 27 *
yading@11 28 * CIN is a somewhat quirky and ill-defined format. Here are some notes
yading@11 29 * for anyone trying to understand the technical details of this format:
yading@11 30 *
yading@11 31 * The format has no definite file signature. This is problematic for a
yading@11 32 * general-purpose media player that wants to automatically detect file
yading@11 33 * types. However, a CIN file does start with 5 32-bit numbers that
yading@11 34 * specify audio and video parameters. This demuxer gets around the lack
yading@11 35 * of file signature by performing sanity checks on those parameters.
yading@11 36 * Probabalistically, this is a reasonable solution since the number of
yading@11 37 * valid combinations of the 5 parameters is a very small subset of the
yading@11 38 * total 160-bit number space.
yading@11 39 *
yading@11 40 * Refer to the function idcin_probe() for the precise A/V parameters
yading@11 41 * that this demuxer allows.
yading@11 42 *
yading@11 43 * Next, each audio and video frame has a duration of 1/14 sec. If the
yading@11 44 * audio sample rate is a multiple of the common frequency 22050 Hz it will
yading@11 45 * divide evenly by 14. However, if the sample rate is 11025 Hz:
yading@11 46 * 11025 (samples/sec) / 14 (frames/sec) = 787.5 (samples/frame)
yading@11 47 * The way the CIN stores audio in this case is by storing 787 sample
yading@11 48 * frames in the first audio frame and 788 sample frames in the second
yading@11 49 * audio frame. Therefore, the total number of bytes in an audio frame
yading@11 50 * is given as:
yading@11 51 * audio frame #0: 787 * (bytes/sample) * (# channels) bytes in frame
yading@11 52 * audio frame #1: 788 * (bytes/sample) * (# channels) bytes in frame
yading@11 53 * audio frame #2: 787 * (bytes/sample) * (# channels) bytes in frame
yading@11 54 * audio frame #3: 788 * (bytes/sample) * (# channels) bytes in frame
yading@11 55 *
yading@11 56 * Finally, not all id CIN creation tools agree on the resolution of the
yading@11 57 * color palette, apparently. Some creation tools specify red, green, and
yading@11 58 * blue palette components in terms of 6-bit VGA color DAC values which
yading@11 59 * range from 0..63. Other tools specify the RGB components as full 8-bit
yading@11 60 * values that range from 0..255. Since there are no markers in the file to
yading@11 61 * differentiate between the two variants, this demuxer uses the following
yading@11 62 * heuristic:
yading@11 63 * - load the 768 palette bytes from disk
yading@11 64 * - assume that they will need to be shifted left by 2 bits to
yading@11 65 * transform them from 6-bit values to 8-bit values
yading@11 66 * - scan through all 768 palette bytes
yading@11 67 * - if any bytes exceed 63, do not shift the bytes at all before
yading@11 68 * transmitting them to the video decoder
yading@11 69 */
yading@11 70
yading@11 71 #include "libavutil/channel_layout.h"
yading@11 72 #include "libavutil/imgutils.h"
yading@11 73 #include "libavutil/intreadwrite.h"
yading@11 74 #include "avformat.h"
yading@11 75 #include "internal.h"
yading@11 76
yading@11 77 #define HUFFMAN_TABLE_SIZE (64 * 1024)
yading@11 78 #define IDCIN_FPS 14
yading@11 79
yading@11 80 typedef struct IdcinDemuxContext {
yading@11 81 int video_stream_index;
yading@11 82 int audio_stream_index;
yading@11 83 int audio_chunk_size1;
yading@11 84 int audio_chunk_size2;
yading@11 85 int block_align;
yading@11 86
yading@11 87 /* demux state variables */
yading@11 88 int current_audio_chunk;
yading@11 89 int next_chunk_is_video;
yading@11 90 int audio_present;
yading@11 91 int64_t first_pkt_pos;
yading@11 92 } IdcinDemuxContext;
yading@11 93
yading@11 94 static int idcin_probe(AVProbeData *p)
yading@11 95 {
yading@11 96 unsigned int number;
yading@11 97
yading@11 98 /*
yading@11 99 * This is what you could call a "probabilistic" file check: id CIN
yading@11 100 * files don't have a definite file signature. In lieu of such a marker,
yading@11 101 * perform sanity checks on the 5 32-bit header fields:
yading@11 102 * width, height: greater than 0, less than or equal to 1024
yading@11 103 * audio sample rate: greater than or equal to 8000, less than or
yading@11 104 * equal to 48000, or 0 for no audio
yading@11 105 * audio sample width (bytes/sample): 0 for no audio, or 1 or 2
yading@11 106 * audio channels: 0 for no audio, or 1 or 2
yading@11 107 */
yading@11 108
yading@11 109 /* check we have enough data to do all checks, otherwise the
yading@11 110 0-padding may cause a wrong recognition */
yading@11 111 if (p->buf_size < 20)
yading@11 112 return 0;
yading@11 113
yading@11 114 /* check the video width */
yading@11 115 number = AV_RL32(&p->buf[0]);
yading@11 116 if ((number == 0) || (number > 1024))
yading@11 117 return 0;
yading@11 118
yading@11 119 /* check the video height */
yading@11 120 number = AV_RL32(&p->buf[4]);
yading@11 121 if ((number == 0) || (number > 1024))
yading@11 122 return 0;
yading@11 123
yading@11 124 /* check the audio sample rate */
yading@11 125 number = AV_RL32(&p->buf[8]);
yading@11 126 if ((number != 0) && ((number < 8000) | (number > 48000)))
yading@11 127 return 0;
yading@11 128
yading@11 129 /* check the audio bytes/sample */
yading@11 130 number = AV_RL32(&p->buf[12]);
yading@11 131 if (number > 2)
yading@11 132 return 0;
yading@11 133
yading@11 134 /* check the audio channels */
yading@11 135 number = AV_RL32(&p->buf[16]);
yading@11 136 if (number > 2)
yading@11 137 return 0;
yading@11 138
yading@11 139 /* return half certainly since this check is a bit sketchy */
yading@11 140 return AVPROBE_SCORE_MAX / 2;
yading@11 141 }
yading@11 142
yading@11 143 static int idcin_read_header(AVFormatContext *s)
yading@11 144 {
yading@11 145 AVIOContext *pb = s->pb;
yading@11 146 IdcinDemuxContext *idcin = s->priv_data;
yading@11 147 AVStream *st;
yading@11 148 unsigned int width, height;
yading@11 149 unsigned int sample_rate, bytes_per_sample, channels;
yading@11 150 int ret;
yading@11 151
yading@11 152 /* get the 5 header parameters */
yading@11 153 width = avio_rl32(pb);
yading@11 154 height = avio_rl32(pb);
yading@11 155 sample_rate = avio_rl32(pb);
yading@11 156 bytes_per_sample = avio_rl32(pb);
yading@11 157 channels = avio_rl32(pb);
yading@11 158
yading@11 159 if (s->pb->eof_reached) {
yading@11 160 av_log(s, AV_LOG_ERROR, "incomplete header\n");
yading@11 161 return s->pb->error ? s->pb->error : AVERROR_EOF;
yading@11 162 }
yading@11 163
yading@11 164 if (av_image_check_size(width, height, 0, s) < 0)
yading@11 165 return AVERROR_INVALIDDATA;
yading@11 166 if (sample_rate > 0) {
yading@11 167 if (sample_rate < 14 || sample_rate > INT_MAX) {
yading@11 168 av_log(s, AV_LOG_ERROR, "invalid sample rate: %u\n", sample_rate);
yading@11 169 return AVERROR_INVALIDDATA;
yading@11 170 }
yading@11 171 if (bytes_per_sample < 1 || bytes_per_sample > 2) {
yading@11 172 av_log(s, AV_LOG_ERROR, "invalid bytes per sample: %u\n",
yading@11 173 bytes_per_sample);
yading@11 174 return AVERROR_INVALIDDATA;
yading@11 175 }
yading@11 176 if (channels < 1 || channels > 2) {
yading@11 177 av_log(s, AV_LOG_ERROR, "invalid channels: %u\n", channels);
yading@11 178 return AVERROR_INVALIDDATA;
yading@11 179 }
yading@11 180 idcin->audio_present = 1;
yading@11 181 } else {
yading@11 182 /* if sample rate is 0, assume no audio */
yading@11 183 idcin->audio_present = 0;
yading@11 184 }
yading@11 185
yading@11 186 st = avformat_new_stream(s, NULL);
yading@11 187 if (!st)
yading@11 188 return AVERROR(ENOMEM);
yading@11 189 avpriv_set_pts_info(st, 33, 1, IDCIN_FPS);
yading@11 190 st->start_time = 0;
yading@11 191 idcin->video_stream_index = st->index;
yading@11 192 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
yading@11 193 st->codec->codec_id = AV_CODEC_ID_IDCIN;
yading@11 194 st->codec->codec_tag = 0; /* no fourcc */
yading@11 195 st->codec->width = width;
yading@11 196 st->codec->height = height;
yading@11 197
yading@11 198 /* load up the Huffman tables into extradata */
yading@11 199 st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
yading@11 200 st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
yading@11 201 ret = avio_read(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE);
yading@11 202 if (ret < 0) {
yading@11 203 return ret;
yading@11 204 } else if (ret != HUFFMAN_TABLE_SIZE) {
yading@11 205 av_log(s, AV_LOG_ERROR, "incomplete header\n");
yading@11 206 return AVERROR(EIO);
yading@11 207 }
yading@11 208
yading@11 209 if (idcin->audio_present) {
yading@11 210 idcin->audio_present = 1;
yading@11 211 st = avformat_new_stream(s, NULL);
yading@11 212 if (!st)
yading@11 213 return AVERROR(ENOMEM);
yading@11 214 avpriv_set_pts_info(st, 63, 1, sample_rate);
yading@11 215 st->start_time = 0;
yading@11 216 idcin->audio_stream_index = st->index;
yading@11 217 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
yading@11 218 st->codec->codec_tag = 1;
yading@11 219 st->codec->channels = channels;
yading@11 220 st->codec->channel_layout = channels > 1 ? AV_CH_LAYOUT_STEREO :
yading@11 221 AV_CH_LAYOUT_MONO;
yading@11 222 st->codec->sample_rate = sample_rate;
yading@11 223 st->codec->bits_per_coded_sample = bytes_per_sample * 8;
yading@11 224 st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
yading@11 225 st->codec->block_align = idcin->block_align = bytes_per_sample * channels;
yading@11 226 if (bytes_per_sample == 1)
yading@11 227 st->codec->codec_id = AV_CODEC_ID_PCM_U8;
yading@11 228 else
yading@11 229 st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
yading@11 230
yading@11 231 if (sample_rate % 14 != 0) {
yading@11 232 idcin->audio_chunk_size1 = (sample_rate / 14) *
yading@11 233 bytes_per_sample * channels;
yading@11 234 idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
yading@11 235 bytes_per_sample * channels;
yading@11 236 } else {
yading@11 237 idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
yading@11 238 (sample_rate / 14) * bytes_per_sample * channels;
yading@11 239 }
yading@11 240 idcin->current_audio_chunk = 0;
yading@11 241 }
yading@11 242
yading@11 243 idcin->next_chunk_is_video = 1;
yading@11 244 idcin->first_pkt_pos = avio_tell(s->pb);
yading@11 245
yading@11 246 return 0;
yading@11 247 }
yading@11 248
yading@11 249 static int idcin_read_packet(AVFormatContext *s,
yading@11 250 AVPacket *pkt)
yading@11 251 {
yading@11 252 int ret;
yading@11 253 unsigned int command;
yading@11 254 unsigned int chunk_size;
yading@11 255 IdcinDemuxContext *idcin = s->priv_data;
yading@11 256 AVIOContext *pb = s->pb;
yading@11 257 int i;
yading@11 258 int palette_scale;
yading@11 259 unsigned char r, g, b;
yading@11 260 unsigned char palette_buffer[768];
yading@11 261 uint32_t palette[256];
yading@11 262
yading@11 263 if (url_feof(s->pb))
yading@11 264 return s->pb->error ? s->pb->error : AVERROR_EOF;
yading@11 265
yading@11 266 if (idcin->next_chunk_is_video) {
yading@11 267 command = avio_rl32(pb);
yading@11 268 if (command == 2) {
yading@11 269 return AVERROR(EIO);
yading@11 270 } else if (command == 1) {
yading@11 271 /* trigger a palette change */
yading@11 272 ret = avio_read(pb, palette_buffer, 768);
yading@11 273 if (ret < 0) {
yading@11 274 return ret;
yading@11 275 } else if (ret != 768) {
yading@11 276 av_log(s, AV_LOG_ERROR, "incomplete packet\n");
yading@11 277 return AVERROR(EIO);
yading@11 278 }
yading@11 279 /* scale the palette as necessary */
yading@11 280 palette_scale = 2;
yading@11 281 for (i = 0; i < 768; i++)
yading@11 282 if (palette_buffer[i] > 63) {
yading@11 283 palette_scale = 0;
yading@11 284 break;
yading@11 285 }
yading@11 286
yading@11 287 for (i = 0; i < 256; i++) {
yading@11 288 r = palette_buffer[i * 3 ] << palette_scale;
yading@11 289 g = palette_buffer[i * 3 + 1] << palette_scale;
yading@11 290 b = palette_buffer[i * 3 + 2] << palette_scale;
yading@11 291 palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
yading@11 292 if (palette_scale == 2)
yading@11 293 palette[i] |= palette[i] >> 6 & 0x30303;
yading@11 294 }
yading@11 295 }
yading@11 296
yading@11 297 if (s->pb->eof_reached) {
yading@11 298 av_log(s, AV_LOG_ERROR, "incomplete packet\n");
yading@11 299 return s->pb->error ? s->pb->error : AVERROR_EOF;
yading@11 300 }
yading@11 301 chunk_size = avio_rl32(pb);
yading@11 302 if (chunk_size < 4 || chunk_size > INT_MAX - 4) {
yading@11 303 av_log(s, AV_LOG_ERROR, "invalid chunk size: %u\n", chunk_size);
yading@11 304 return AVERROR_INVALIDDATA;
yading@11 305 }
yading@11 306 /* skip the number of decoded bytes (always equal to width * height) */
yading@11 307 avio_skip(pb, 4);
yading@11 308 if (chunk_size < 4)
yading@11 309 return AVERROR_INVALIDDATA;
yading@11 310 chunk_size -= 4;
yading@11 311 ret= av_get_packet(pb, pkt, chunk_size);
yading@11 312 if (ret < 0)
yading@11 313 return ret;
yading@11 314 else if (ret != chunk_size) {
yading@11 315 av_log(s, AV_LOG_ERROR, "incomplete packet\n");
yading@11 316 av_free_packet(pkt);
yading@11 317 return AVERROR(EIO);
yading@11 318 }
yading@11 319 if (command == 1) {
yading@11 320 uint8_t *pal;
yading@11 321
yading@11 322 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
yading@11 323 AVPALETTE_SIZE);
yading@11 324 if (!pal) {
yading@11 325 av_free_packet(pkt);
yading@11 326 return AVERROR(ENOMEM);
yading@11 327 }
yading@11 328 memcpy(pal, palette, AVPALETTE_SIZE);
yading@11 329 pkt->flags |= AV_PKT_FLAG_KEY;
yading@11 330 }
yading@11 331 pkt->stream_index = idcin->video_stream_index;
yading@11 332 pkt->duration = 1;
yading@11 333 } else {
yading@11 334 /* send out the audio chunk */
yading@11 335 if (idcin->current_audio_chunk)
yading@11 336 chunk_size = idcin->audio_chunk_size2;
yading@11 337 else
yading@11 338 chunk_size = idcin->audio_chunk_size1;
yading@11 339 ret= av_get_packet(pb, pkt, chunk_size);
yading@11 340 if (ret < 0)
yading@11 341 return ret;
yading@11 342 pkt->stream_index = idcin->audio_stream_index;
yading@11 343 pkt->duration = chunk_size / idcin->block_align;
yading@11 344
yading@11 345 idcin->current_audio_chunk ^= 1;
yading@11 346 }
yading@11 347
yading@11 348 if (idcin->audio_present)
yading@11 349 idcin->next_chunk_is_video ^= 1;
yading@11 350
yading@11 351 return 0;
yading@11 352 }
yading@11 353
yading@11 354 static int idcin_read_seek(AVFormatContext *s, int stream_index,
yading@11 355 int64_t timestamp, int flags)
yading@11 356 {
yading@11 357 IdcinDemuxContext *idcin = s->priv_data;
yading@11 358
yading@11 359 if (idcin->first_pkt_pos > 0) {
yading@11 360 int ret = avio_seek(s->pb, idcin->first_pkt_pos, SEEK_SET);
yading@11 361 if (ret < 0)
yading@11 362 return ret;
yading@11 363 ff_update_cur_dts(s, s->streams[idcin->video_stream_index], 0);
yading@11 364 idcin->next_chunk_is_video = 1;
yading@11 365 idcin->current_audio_chunk = 0;
yading@11 366 return 0;
yading@11 367 }
yading@11 368 return -1;
yading@11 369 }
yading@11 370
yading@11 371 AVInputFormat ff_idcin_demuxer = {
yading@11 372 .name = "idcin",
yading@11 373 .long_name = NULL_IF_CONFIG_SMALL("id Cinematic"),
yading@11 374 .priv_data_size = sizeof(IdcinDemuxContext),
yading@11 375 .read_probe = idcin_probe,
yading@11 376 .read_header = idcin_read_header,
yading@11 377 .read_packet = idcin_read_packet,
yading@11 378 .read_seek = idcin_read_seek,
yading@11 379 .flags = AVFMT_NO_BYTE_SEEK,
yading@11 380 };