yading@11: /* yading@11: * id Quake II CIN File Demuxer yading@11: * Copyright (c) 2003 The ffmpeg Project yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * id Quake II CIN file demuxer by Mike Melanson (melanson@pcisys.net) yading@11: * For more information about the id CIN format, visit: yading@11: * http://www.csse.monash.edu.au/~timf/ yading@11: * yading@11: * CIN is a somewhat quirky and ill-defined format. Here are some notes yading@11: * for anyone trying to understand the technical details of this format: yading@11: * yading@11: * The format has no definite file signature. This is problematic for a yading@11: * general-purpose media player that wants to automatically detect file yading@11: * types. However, a CIN file does start with 5 32-bit numbers that yading@11: * specify audio and video parameters. This demuxer gets around the lack yading@11: * of file signature by performing sanity checks on those parameters. yading@11: * Probabalistically, this is a reasonable solution since the number of yading@11: * valid combinations of the 5 parameters is a very small subset of the yading@11: * total 160-bit number space. yading@11: * yading@11: * Refer to the function idcin_probe() for the precise A/V parameters yading@11: * that this demuxer allows. yading@11: * yading@11: * Next, each audio and video frame has a duration of 1/14 sec. If the yading@11: * audio sample rate is a multiple of the common frequency 22050 Hz it will yading@11: * divide evenly by 14. However, if the sample rate is 11025 Hz: yading@11: * 11025 (samples/sec) / 14 (frames/sec) = 787.5 (samples/frame) yading@11: * The way the CIN stores audio in this case is by storing 787 sample yading@11: * frames in the first audio frame and 788 sample frames in the second yading@11: * audio frame. Therefore, the total number of bytes in an audio frame yading@11: * is given as: yading@11: * audio frame #0: 787 * (bytes/sample) * (# channels) bytes in frame yading@11: * audio frame #1: 788 * (bytes/sample) * (# channels) bytes in frame yading@11: * audio frame #2: 787 * (bytes/sample) * (# channels) bytes in frame yading@11: * audio frame #3: 788 * (bytes/sample) * (# channels) bytes in frame yading@11: * yading@11: * Finally, not all id CIN creation tools agree on the resolution of the yading@11: * color palette, apparently. Some creation tools specify red, green, and yading@11: * blue palette components in terms of 6-bit VGA color DAC values which yading@11: * range from 0..63. Other tools specify the RGB components as full 8-bit yading@11: * values that range from 0..255. Since there are no markers in the file to yading@11: * differentiate between the two variants, this demuxer uses the following yading@11: * heuristic: yading@11: * - load the 768 palette bytes from disk yading@11: * - assume that they will need to be shifted left by 2 bits to yading@11: * transform them from 6-bit values to 8-bit values yading@11: * - scan through all 768 palette bytes yading@11: * - if any bytes exceed 63, do not shift the bytes at all before yading@11: * transmitting them to the video decoder yading@11: */ yading@11: yading@11: #include "libavutil/channel_layout.h" yading@11: #include "libavutil/imgutils.h" yading@11: #include "libavutil/intreadwrite.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: yading@11: #define HUFFMAN_TABLE_SIZE (64 * 1024) yading@11: #define IDCIN_FPS 14 yading@11: yading@11: typedef struct IdcinDemuxContext { yading@11: int video_stream_index; yading@11: int audio_stream_index; yading@11: int audio_chunk_size1; yading@11: int audio_chunk_size2; yading@11: int block_align; yading@11: yading@11: /* demux state variables */ yading@11: int current_audio_chunk; yading@11: int next_chunk_is_video; yading@11: int audio_present; yading@11: int64_t first_pkt_pos; yading@11: } IdcinDemuxContext; yading@11: yading@11: static int idcin_probe(AVProbeData *p) yading@11: { yading@11: unsigned int number; yading@11: yading@11: /* yading@11: * This is what you could call a "probabilistic" file check: id CIN yading@11: * files don't have a definite file signature. In lieu of such a marker, yading@11: * perform sanity checks on the 5 32-bit header fields: yading@11: * width, height: greater than 0, less than or equal to 1024 yading@11: * audio sample rate: greater than or equal to 8000, less than or yading@11: * equal to 48000, or 0 for no audio yading@11: * audio sample width (bytes/sample): 0 for no audio, or 1 or 2 yading@11: * audio channels: 0 for no audio, or 1 or 2 yading@11: */ yading@11: yading@11: /* check we have enough data to do all checks, otherwise the yading@11: 0-padding may cause a wrong recognition */ yading@11: if (p->buf_size < 20) yading@11: return 0; yading@11: yading@11: /* check the video width */ yading@11: number = AV_RL32(&p->buf[0]); yading@11: if ((number == 0) || (number > 1024)) yading@11: return 0; yading@11: yading@11: /* check the video height */ yading@11: number = AV_RL32(&p->buf[4]); yading@11: if ((number == 0) || (number > 1024)) yading@11: return 0; yading@11: yading@11: /* check the audio sample rate */ yading@11: number = AV_RL32(&p->buf[8]); yading@11: if ((number != 0) && ((number < 8000) | (number > 48000))) yading@11: return 0; yading@11: yading@11: /* check the audio bytes/sample */ yading@11: number = AV_RL32(&p->buf[12]); yading@11: if (number > 2) yading@11: return 0; yading@11: yading@11: /* check the audio channels */ yading@11: number = AV_RL32(&p->buf[16]); yading@11: if (number > 2) yading@11: return 0; yading@11: yading@11: /* return half certainly since this check is a bit sketchy */ yading@11: return AVPROBE_SCORE_MAX / 2; yading@11: } yading@11: yading@11: static int idcin_read_header(AVFormatContext *s) yading@11: { yading@11: AVIOContext *pb = s->pb; yading@11: IdcinDemuxContext *idcin = s->priv_data; yading@11: AVStream *st; yading@11: unsigned int width, height; yading@11: unsigned int sample_rate, bytes_per_sample, channels; yading@11: int ret; yading@11: yading@11: /* get the 5 header parameters */ yading@11: width = avio_rl32(pb); yading@11: height = avio_rl32(pb); yading@11: sample_rate = avio_rl32(pb); yading@11: bytes_per_sample = avio_rl32(pb); yading@11: channels = avio_rl32(pb); yading@11: yading@11: if (s->pb->eof_reached) { yading@11: av_log(s, AV_LOG_ERROR, "incomplete header\n"); yading@11: return s->pb->error ? s->pb->error : AVERROR_EOF; yading@11: } yading@11: yading@11: if (av_image_check_size(width, height, 0, s) < 0) yading@11: return AVERROR_INVALIDDATA; yading@11: if (sample_rate > 0) { yading@11: if (sample_rate < 14 || sample_rate > INT_MAX) { yading@11: av_log(s, AV_LOG_ERROR, "invalid sample rate: %u\n", sample_rate); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: if (bytes_per_sample < 1 || bytes_per_sample > 2) { yading@11: av_log(s, AV_LOG_ERROR, "invalid bytes per sample: %u\n", yading@11: bytes_per_sample); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: if (channels < 1 || channels > 2) { yading@11: av_log(s, AV_LOG_ERROR, "invalid channels: %u\n", channels); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: idcin->audio_present = 1; yading@11: } else { yading@11: /* if sample rate is 0, assume no audio */ yading@11: idcin->audio_present = 0; yading@11: } yading@11: yading@11: st = avformat_new_stream(s, NULL); yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: avpriv_set_pts_info(st, 33, 1, IDCIN_FPS); yading@11: st->start_time = 0; yading@11: idcin->video_stream_index = st->index; yading@11: st->codec->codec_type = AVMEDIA_TYPE_VIDEO; yading@11: st->codec->codec_id = AV_CODEC_ID_IDCIN; yading@11: st->codec->codec_tag = 0; /* no fourcc */ yading@11: st->codec->width = width; yading@11: st->codec->height = height; yading@11: yading@11: /* load up the Huffman tables into extradata */ yading@11: st->codec->extradata_size = HUFFMAN_TABLE_SIZE; yading@11: st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE); yading@11: ret = avio_read(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE); yading@11: if (ret < 0) { yading@11: return ret; yading@11: } else if (ret != HUFFMAN_TABLE_SIZE) { yading@11: av_log(s, AV_LOG_ERROR, "incomplete header\n"); yading@11: return AVERROR(EIO); yading@11: } yading@11: yading@11: if (idcin->audio_present) { yading@11: idcin->audio_present = 1; yading@11: st = avformat_new_stream(s, NULL); yading@11: if (!st) yading@11: return AVERROR(ENOMEM); yading@11: avpriv_set_pts_info(st, 63, 1, sample_rate); yading@11: st->start_time = 0; yading@11: idcin->audio_stream_index = st->index; yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: st->codec->codec_tag = 1; yading@11: st->codec->channels = channels; yading@11: st->codec->channel_layout = channels > 1 ? AV_CH_LAYOUT_STEREO : yading@11: AV_CH_LAYOUT_MONO; yading@11: st->codec->sample_rate = sample_rate; yading@11: st->codec->bits_per_coded_sample = bytes_per_sample * 8; yading@11: st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels; yading@11: st->codec->block_align = idcin->block_align = bytes_per_sample * channels; yading@11: if (bytes_per_sample == 1) yading@11: st->codec->codec_id = AV_CODEC_ID_PCM_U8; yading@11: else yading@11: st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; yading@11: yading@11: if (sample_rate % 14 != 0) { yading@11: idcin->audio_chunk_size1 = (sample_rate / 14) * yading@11: bytes_per_sample * channels; yading@11: idcin->audio_chunk_size2 = (sample_rate / 14 + 1) * yading@11: bytes_per_sample * channels; yading@11: } else { yading@11: idcin->audio_chunk_size1 = idcin->audio_chunk_size2 = yading@11: (sample_rate / 14) * bytes_per_sample * channels; yading@11: } yading@11: idcin->current_audio_chunk = 0; yading@11: } yading@11: yading@11: idcin->next_chunk_is_video = 1; yading@11: idcin->first_pkt_pos = avio_tell(s->pb); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int idcin_read_packet(AVFormatContext *s, yading@11: AVPacket *pkt) yading@11: { yading@11: int ret; yading@11: unsigned int command; yading@11: unsigned int chunk_size; yading@11: IdcinDemuxContext *idcin = s->priv_data; yading@11: AVIOContext *pb = s->pb; yading@11: int i; yading@11: int palette_scale; yading@11: unsigned char r, g, b; yading@11: unsigned char palette_buffer[768]; yading@11: uint32_t palette[256]; yading@11: yading@11: if (url_feof(s->pb)) yading@11: return s->pb->error ? s->pb->error : AVERROR_EOF; yading@11: yading@11: if (idcin->next_chunk_is_video) { yading@11: command = avio_rl32(pb); yading@11: if (command == 2) { yading@11: return AVERROR(EIO); yading@11: } else if (command == 1) { yading@11: /* trigger a palette change */ yading@11: ret = avio_read(pb, palette_buffer, 768); yading@11: if (ret < 0) { yading@11: return ret; yading@11: } else if (ret != 768) { yading@11: av_log(s, AV_LOG_ERROR, "incomplete packet\n"); yading@11: return AVERROR(EIO); yading@11: } yading@11: /* scale the palette as necessary */ yading@11: palette_scale = 2; yading@11: for (i = 0; i < 768; i++) yading@11: if (palette_buffer[i] > 63) { yading@11: palette_scale = 0; yading@11: break; yading@11: } yading@11: yading@11: for (i = 0; i < 256; i++) { yading@11: r = palette_buffer[i * 3 ] << palette_scale; yading@11: g = palette_buffer[i * 3 + 1] << palette_scale; yading@11: b = palette_buffer[i * 3 + 2] << palette_scale; yading@11: palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b); yading@11: if (palette_scale == 2) yading@11: palette[i] |= palette[i] >> 6 & 0x30303; yading@11: } yading@11: } yading@11: yading@11: if (s->pb->eof_reached) { yading@11: av_log(s, AV_LOG_ERROR, "incomplete packet\n"); yading@11: return s->pb->error ? s->pb->error : AVERROR_EOF; yading@11: } yading@11: chunk_size = avio_rl32(pb); yading@11: if (chunk_size < 4 || chunk_size > INT_MAX - 4) { yading@11: av_log(s, AV_LOG_ERROR, "invalid chunk size: %u\n", chunk_size); yading@11: return AVERROR_INVALIDDATA; yading@11: } yading@11: /* skip the number of decoded bytes (always equal to width * height) */ yading@11: avio_skip(pb, 4); yading@11: if (chunk_size < 4) yading@11: return AVERROR_INVALIDDATA; yading@11: chunk_size -= 4; yading@11: ret= av_get_packet(pb, pkt, chunk_size); yading@11: if (ret < 0) yading@11: return ret; yading@11: else if (ret != chunk_size) { yading@11: av_log(s, AV_LOG_ERROR, "incomplete packet\n"); yading@11: av_free_packet(pkt); yading@11: return AVERROR(EIO); yading@11: } yading@11: if (command == 1) { yading@11: uint8_t *pal; yading@11: yading@11: pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, yading@11: AVPALETTE_SIZE); yading@11: if (!pal) { yading@11: av_free_packet(pkt); yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: memcpy(pal, palette, AVPALETTE_SIZE); yading@11: pkt->flags |= AV_PKT_FLAG_KEY; yading@11: } yading@11: pkt->stream_index = idcin->video_stream_index; yading@11: pkt->duration = 1; yading@11: } else { yading@11: /* send out the audio chunk */ yading@11: if (idcin->current_audio_chunk) yading@11: chunk_size = idcin->audio_chunk_size2; yading@11: else yading@11: chunk_size = idcin->audio_chunk_size1; yading@11: ret= av_get_packet(pb, pkt, chunk_size); yading@11: if (ret < 0) yading@11: return ret; yading@11: pkt->stream_index = idcin->audio_stream_index; yading@11: pkt->duration = chunk_size / idcin->block_align; yading@11: yading@11: idcin->current_audio_chunk ^= 1; yading@11: } yading@11: yading@11: if (idcin->audio_present) yading@11: idcin->next_chunk_is_video ^= 1; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int idcin_read_seek(AVFormatContext *s, int stream_index, yading@11: int64_t timestamp, int flags) yading@11: { yading@11: IdcinDemuxContext *idcin = s->priv_data; yading@11: yading@11: if (idcin->first_pkt_pos > 0) { yading@11: int ret = avio_seek(s->pb, idcin->first_pkt_pos, SEEK_SET); yading@11: if (ret < 0) yading@11: return ret; yading@11: ff_update_cur_dts(s, s->streams[idcin->video_stream_index], 0); yading@11: idcin->next_chunk_is_video = 1; yading@11: idcin->current_audio_chunk = 0; yading@11: return 0; yading@11: } yading@11: return -1; yading@11: } yading@11: yading@11: AVInputFormat ff_idcin_demuxer = { yading@11: .name = "idcin", yading@11: .long_name = NULL_IF_CONFIG_SMALL("id Cinematic"), yading@11: .priv_data_size = sizeof(IdcinDemuxContext), yading@11: .read_probe = idcin_probe, yading@11: .read_header = idcin_read_header, yading@11: .read_packet = idcin_read_packet, yading@11: .read_seek = idcin_read_seek, yading@11: .flags = AVFMT_NO_BYTE_SEEK, yading@11: };