annotate ffmpeg/libavformat/iss.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 * ISS (.iss) file demuxer
yading@11 3 * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
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 * Funcom ISS file demuxer
yading@11 25 * @author Jaikrishnan Menon
yading@11 26 * @see http://wiki.multimedia.cx/index.php?title=FunCom_ISS
yading@11 27 */
yading@11 28
yading@11 29 #include "libavutil/channel_layout.h"
yading@11 30 #include "avformat.h"
yading@11 31 #include "internal.h"
yading@11 32 #include "libavutil/avstring.h"
yading@11 33
yading@11 34 #define ISS_SIG "IMA_ADPCM_Sound"
yading@11 35 #define ISS_SIG_LEN 15
yading@11 36 #define MAX_TOKEN_SIZE 20
yading@11 37
yading@11 38 typedef struct {
yading@11 39 int packet_size;
yading@11 40 int sample_start_pos;
yading@11 41 } IssDemuxContext;
yading@11 42
yading@11 43 static void get_token(AVIOContext *s, char *buf, int maxlen)
yading@11 44 {
yading@11 45 int i = 0;
yading@11 46 char c;
yading@11 47
yading@11 48 while ((c = avio_r8(s))) {
yading@11 49 if(c == ' ')
yading@11 50 break;
yading@11 51 if (i < maxlen-1)
yading@11 52 buf[i++] = c;
yading@11 53 }
yading@11 54
yading@11 55 if(!c)
yading@11 56 avio_r8(s);
yading@11 57
yading@11 58 buf[i] = 0; /* Ensure null terminated, but may be truncated */
yading@11 59 }
yading@11 60
yading@11 61 static int iss_probe(AVProbeData *p)
yading@11 62 {
yading@11 63 if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
yading@11 64 return 0;
yading@11 65
yading@11 66 return AVPROBE_SCORE_MAX;
yading@11 67 }
yading@11 68
yading@11 69 static av_cold int iss_read_header(AVFormatContext *s)
yading@11 70 {
yading@11 71 IssDemuxContext *iss = s->priv_data;
yading@11 72 AVIOContext *pb = s->pb;
yading@11 73 AVStream *st;
yading@11 74 char token[MAX_TOKEN_SIZE];
yading@11 75 int stereo, rate_divisor;
yading@11 76
yading@11 77 get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
yading@11 78 get_token(pb, token, sizeof(token)); //packet size
yading@11 79 sscanf(token, "%d", &iss->packet_size);
yading@11 80 get_token(pb, token, sizeof(token)); //File ID
yading@11 81 get_token(pb, token, sizeof(token)); //out size
yading@11 82 get_token(pb, token, sizeof(token)); //stereo
yading@11 83 sscanf(token, "%d", &stereo);
yading@11 84 get_token(pb, token, sizeof(token)); //Unknown1
yading@11 85 get_token(pb, token, sizeof(token)); //RateDivisor
yading@11 86 sscanf(token, "%d", &rate_divisor);
yading@11 87 get_token(pb, token, sizeof(token)); //Unknown2
yading@11 88 get_token(pb, token, sizeof(token)); //Version ID
yading@11 89 get_token(pb, token, sizeof(token)); //Size
yading@11 90
yading@11 91 if (iss->packet_size <= 0) {
yading@11 92 av_log(s, AV_LOG_ERROR, "packet_size %d is invalid\n", iss->packet_size);
yading@11 93 return AVERROR_INVALIDDATA;
yading@11 94 }
yading@11 95
yading@11 96 iss->sample_start_pos = avio_tell(pb);
yading@11 97
yading@11 98 st = avformat_new_stream(s, NULL);
yading@11 99 if (!st)
yading@11 100 return AVERROR(ENOMEM);
yading@11 101 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
yading@11 102 st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS;
yading@11 103 if (stereo) {
yading@11 104 st->codec->channels = 2;
yading@11 105 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
yading@11 106 } else {
yading@11 107 st->codec->channels = 1;
yading@11 108 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
yading@11 109 }
yading@11 110 st->codec->sample_rate = 44100;
yading@11 111 if(rate_divisor > 0)
yading@11 112 st->codec->sample_rate /= rate_divisor;
yading@11 113 st->codec->bits_per_coded_sample = 4;
yading@11 114 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate
yading@11 115 * st->codec->bits_per_coded_sample;
yading@11 116 st->codec->block_align = iss->packet_size;
yading@11 117 avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
yading@11 118
yading@11 119 return 0;
yading@11 120 }
yading@11 121
yading@11 122 static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
yading@11 123 {
yading@11 124 IssDemuxContext *iss = s->priv_data;
yading@11 125 int ret = av_get_packet(s->pb, pkt, iss->packet_size);
yading@11 126
yading@11 127 if(ret != iss->packet_size)
yading@11 128 return AVERROR(EIO);
yading@11 129
yading@11 130 pkt->stream_index = 0;
yading@11 131 pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
yading@11 132 if(s->streams[0]->codec->channels > 0)
yading@11 133 pkt->pts /= s->streams[0]->codec->channels*2;
yading@11 134 return 0;
yading@11 135 }
yading@11 136
yading@11 137 AVInputFormat ff_iss_demuxer = {
yading@11 138 .name = "iss",
yading@11 139 .long_name = NULL_IF_CONFIG_SMALL("Funcom ISS"),
yading@11 140 .priv_data_size = sizeof(IssDemuxContext),
yading@11 141 .read_probe = iss_probe,
yading@11 142 .read_header = iss_read_header,
yading@11 143 .read_packet = iss_read_packet,
yading@11 144 };