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
00025
00026 static int voc_probe(AVProbeData *p)
00027 {
00028 int version, check;
00029
00030 if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
00031 return 0;
00032 version = AV_RL16(p->buf + 22);
00033 check = AV_RL16(p->buf + 24);
00034 if (~version + 0x1234 != check)
00035 return 10;
00036
00037 return AVPROBE_SCORE_MAX;
00038 }
00039
00040 static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
00041 {
00042 VocDecContext *voc = s->priv_data;
00043 ByteIOContext *pb = s->pb;
00044 int header_size;
00045 AVStream *st;
00046
00047 url_fskip(pb, 20);
00048 header_size = get_le16(pb) - 22;
00049 if (header_size != 4) {
00050 av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
00051 return AVERROR(ENOSYS);
00052 }
00053 url_fskip(pb, header_size);
00054 st = av_new_stream(s, 0);
00055 if (!st)
00056 return AVERROR(ENOMEM);
00057 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00058
00059 voc->remaining_size = 0;
00060 return 0;
00061 }
00062
00063 int
00064 voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
00065 {
00066 VocDecContext *voc = s->priv_data;
00067 AVCodecContext *dec = st->codec;
00068 ByteIOContext *pb = s->pb;
00069 VocType type;
00070 int size;
00071 int sample_rate = 0;
00072 int channels = 1;
00073
00074 while (!voc->remaining_size) {
00075 type = get_byte(pb);
00076 if (type == VOC_TYPE_EOF)
00077 return AVERROR(EIO);
00078 voc->remaining_size = get_le24(pb);
00079 if (!voc->remaining_size) {
00080 if (url_is_streamed(s->pb))
00081 return AVERROR(EIO);
00082 voc->remaining_size = url_fsize(pb) - url_ftell(pb);
00083 }
00084 max_size -= 4;
00085
00086 switch (type) {
00087 case VOC_TYPE_VOICE_DATA:
00088 dec->sample_rate = 1000000 / (256 - get_byte(pb));
00089 if (sample_rate)
00090 dec->sample_rate = sample_rate;
00091 dec->channels = channels;
00092 dec->codec_id = ff_codec_get_id(ff_voc_codec_tags, get_byte(pb));
00093 dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
00094 voc->remaining_size -= 2;
00095 max_size -= 2;
00096 channels = 1;
00097 break;
00098
00099 case VOC_TYPE_VOICE_DATA_CONT:
00100 break;
00101
00102 case VOC_TYPE_EXTENDED:
00103 sample_rate = get_le16(pb);
00104 get_byte(pb);
00105 channels = get_byte(pb) + 1;
00106 sample_rate = 256000000 / (channels * (65536 - sample_rate));
00107 voc->remaining_size = 0;
00108 max_size -= 4;
00109 break;
00110
00111 case VOC_TYPE_NEW_VOICE_DATA:
00112 dec->sample_rate = get_le32(pb);
00113 dec->bits_per_coded_sample = get_byte(pb);
00114 dec->channels = get_byte(pb);
00115 dec->codec_id = ff_codec_get_id(ff_voc_codec_tags, get_le16(pb));
00116 url_fskip(pb, 4);
00117 voc->remaining_size -= 12;
00118 max_size -= 12;
00119 break;
00120
00121 default:
00122 url_fskip(pb, voc->remaining_size);
00123 max_size -= voc->remaining_size;
00124 voc->remaining_size = 0;
00125 break;
00126 }
00127 }
00128
00129 dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
00130
00131 if (max_size <= 0)
00132 max_size = 2048;
00133 size = FFMIN(voc->remaining_size, max_size);
00134 voc->remaining_size -= size;
00135 return av_get_packet(pb, pkt, size);
00136 }
00137
00138 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
00139 {
00140 return voc_get_packet(s, pkt, s->streams[0], 0);
00141 }
00142
00143 AVInputFormat voc_demuxer = {
00144 "voc",
00145 NULL_IF_CONFIG_SMALL("Creative Voice file format"),
00146 sizeof(VocDecContext),
00147 voc_probe,
00148 voc_read_header,
00149 voc_read_packet,
00150 .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
00151 };