00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "voc.h"
00024 #include "internal.h"
00025
00026
00027 static int voc_probe(AVProbeData *p)
00028 {
00029 int version, check;
00030
00031 if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
00032 return 0;
00033 version = AV_RL16(p->buf + 22);
00034 check = AV_RL16(p->buf + 24);
00035 if (~version + 0x1234 != check)
00036 return 10;
00037
00038 return AVPROBE_SCORE_MAX;
00039 }
00040
00041 static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
00042 {
00043 VocDecContext *voc = s->priv_data;
00044 AVIOContext *pb = s->pb;
00045 int header_size;
00046 AVStream *st;
00047
00048 avio_skip(pb, 20);
00049 header_size = avio_rl16(pb) - 22;
00050 if (header_size != 4) {
00051 av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
00052 return AVERROR(ENOSYS);
00053 }
00054 avio_skip(pb, header_size);
00055 st = av_new_stream(s, 0);
00056 if (!st)
00057 return AVERROR(ENOMEM);
00058 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00059
00060 voc->remaining_size = 0;
00061 return 0;
00062 }
00063
00064 int
00065 voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
00066 {
00067 VocDecContext *voc = s->priv_data;
00068 AVCodecContext *dec = st->codec;
00069 AVIOContext *pb = s->pb;
00070 VocType type;
00071 int size, tmp_codec=-1;
00072 int sample_rate = 0;
00073 int channels = 1;
00074
00075 while (!voc->remaining_size) {
00076 type = avio_r8(pb);
00077 if (type == VOC_TYPE_EOF)
00078 return AVERROR(EIO);
00079 voc->remaining_size = avio_rl24(pb);
00080 if (!voc->remaining_size) {
00081 if (!s->pb->seekable)
00082 return AVERROR(EIO);
00083 voc->remaining_size = avio_size(pb) - avio_tell(pb);
00084 }
00085 max_size -= 4;
00086
00087 switch (type) {
00088 case VOC_TYPE_VOICE_DATA:
00089 dec->sample_rate = 1000000 / (256 - avio_r8(pb));
00090 if (sample_rate)
00091 dec->sample_rate = sample_rate;
00092 dec->channels = channels;
00093 tmp_codec = avio_r8(pb);
00094 dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
00095 voc->remaining_size -= 2;
00096 max_size -= 2;
00097 channels = 1;
00098 break;
00099
00100 case VOC_TYPE_VOICE_DATA_CONT:
00101 break;
00102
00103 case VOC_TYPE_EXTENDED:
00104 sample_rate = avio_rl16(pb);
00105 avio_r8(pb);
00106 channels = avio_r8(pb) + 1;
00107 sample_rate = 256000000 / (channels * (65536 - sample_rate));
00108 voc->remaining_size = 0;
00109 max_size -= 4;
00110 break;
00111
00112 case VOC_TYPE_NEW_VOICE_DATA:
00113 dec->sample_rate = avio_rl32(pb);
00114 dec->bits_per_coded_sample = avio_r8(pb);
00115 dec->channels = avio_r8(pb);
00116 tmp_codec = avio_rl16(pb);
00117 avio_skip(pb, 4);
00118 voc->remaining_size -= 12;
00119 max_size -= 12;
00120 break;
00121
00122 default:
00123 avio_skip(pb, voc->remaining_size);
00124 max_size -= voc->remaining_size;
00125 voc->remaining_size = 0;
00126 break;
00127 }
00128 }
00129
00130 if (tmp_codec >= 0) {
00131 tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
00132 if (dec->codec_id == CODEC_ID_NONE)
00133 dec->codec_id = tmp_codec;
00134 else if (dec->codec_id != tmp_codec)
00135 av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
00136 if (dec->codec_id == CODEC_ID_NONE) {
00137 if (s->audio_codec_id == CODEC_ID_NONE) {
00138 av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
00139 return AVERROR(EINVAL);
00140 }
00141 av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
00142 }
00143 }
00144
00145 dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
00146
00147 if (max_size <= 0)
00148 max_size = 2048;
00149 size = FFMIN(voc->remaining_size, max_size);
00150 voc->remaining_size -= size;
00151 return av_get_packet(pb, pkt, size);
00152 }
00153
00154 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
00155 {
00156 return voc_get_packet(s, pkt, s->streams[0], 0);
00157 }
00158
00159 AVInputFormat ff_voc_demuxer = {
00160 "voc",
00161 NULL_IF_CONFIG_SMALL("Creative Voice file format"),
00162 sizeof(VocDecContext),
00163 voc_probe,
00164 voc_read_header,
00165 voc_read_packet,
00166 .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
00167 };