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)
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 = avformat_new_stream(s, NULL);
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 ff_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             if (!dec->sample_rate) {
00090                 dec->sample_rate = 1000000 / (256 - avio_r8(pb));
00091                 if (sample_rate)
00092                     dec->sample_rate = sample_rate;
00093                 avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
00094             } else
00095                 avio_skip(pb, 1);
00096             dec->channels = channels;
00097             tmp_codec = avio_r8(pb);
00098             dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
00099             voc->remaining_size -= 2;
00100             max_size -= 2;
00101             channels = 1;
00102             break;
00103 
00104         case VOC_TYPE_VOICE_DATA_CONT:
00105             break;
00106 
00107         case VOC_TYPE_EXTENDED:
00108             sample_rate = avio_rl16(pb);
00109             avio_r8(pb);
00110             channels = avio_r8(pb) + 1;
00111             sample_rate = 256000000 / (channels * (65536 - sample_rate));
00112             voc->remaining_size = 0;
00113             max_size -= 4;
00114             break;
00115 
00116         case VOC_TYPE_NEW_VOICE_DATA:
00117             if (!dec->sample_rate) {
00118                 dec->sample_rate = avio_rl32(pb);
00119                 avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
00120             } else
00121                 avio_skip(pb, 4);
00122             dec->bits_per_coded_sample = avio_r8(pb);
00123             dec->channels = avio_r8(pb);
00124             tmp_codec = avio_rl16(pb);
00125             avio_skip(pb, 4);
00126             voc->remaining_size -= 12;
00127             max_size -= 12;
00128             break;
00129 
00130         default:
00131             avio_skip(pb, voc->remaining_size);
00132             max_size -= voc->remaining_size;
00133             voc->remaining_size = 0;
00134             break;
00135         }
00136     }
00137 
00138     if (tmp_codec >= 0) {
00139         tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
00140         if (dec->codec_id == AV_CODEC_ID_NONE)
00141             dec->codec_id = tmp_codec;
00142         else if (dec->codec_id != tmp_codec)
00143             av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
00144         if (dec->codec_id == AV_CODEC_ID_NONE) {
00145             if (s->audio_codec_id == AV_CODEC_ID_NONE) {
00146                 av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
00147                 return AVERROR(EINVAL);
00148             }
00149             av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
00150         }
00151     }
00152 
00153     dec->bit_rate = dec->sample_rate * dec->channels * dec->bits_per_coded_sample;
00154 
00155     if (max_size <= 0)
00156         max_size = 2048;
00157     size = FFMIN(voc->remaining_size, max_size);
00158     voc->remaining_size -= size;
00159     return av_get_packet(pb, pkt, size);
00160 }
00161 
00162 static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
00163 {
00164     return ff_voc_get_packet(s, pkt, s->streams[0], 0);
00165 }
00166 
00167 AVInputFormat ff_voc_demuxer = {
00168     .name           = "voc",
00169     .long_name      = NULL_IF_CONFIG_SMALL("Creative Voice"),
00170     .priv_data_size = sizeof(VocDecContext),
00171     .read_probe     = voc_probe,
00172     .read_header    = voc_read_header,
00173     .read_packet    = voc_read_packet,
00174     .codec_tag      = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
00175 };