00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/dict.h"
00025
00026 typedef struct VqfContext {
00027 int frame_bit_len;
00028 uint8_t last_frame_bits;
00029 int remaining_bits;
00030 } VqfContext;
00031
00032 static int vqf_probe(AVProbeData *probe_packet)
00033 {
00034 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
00035 return 0;
00036
00037 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
00038 return AVPROBE_SCORE_MAX;
00039
00040 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
00041 return AVPROBE_SCORE_MAX;
00042
00043 return AVPROBE_SCORE_MAX/2;
00044 }
00045
00046 static void add_metadata(AVFormatContext *s, const char *tag,
00047 unsigned int tag_len, unsigned int remaining)
00048 {
00049 int len = FFMIN(tag_len, remaining);
00050 char *buf;
00051
00052 if (len == UINT_MAX)
00053 return;
00054
00055 buf = av_malloc(len+1);
00056 if (!buf)
00057 return;
00058 avio_read(s->pb, buf, len);
00059 buf[len] = 0;
00060 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
00061 }
00062
00063 static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
00064 {
00065 VqfContext *c = s->priv_data;
00066 AVStream *st = av_new_stream(s, 0);
00067 int chunk_tag;
00068 int rate_flag = -1;
00069 int header_size;
00070 int read_bitrate = 0;
00071 int size;
00072
00073 if (!st)
00074 return AVERROR(ENOMEM);
00075
00076 avio_skip(s->pb, 12);
00077
00078 header_size = avio_rb32(s->pb);
00079
00080 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00081 st->codec->codec_id = CODEC_ID_TWINVQ;
00082 st->start_time = 0;
00083
00084 do {
00085 int len;
00086 chunk_tag = avio_rl32(s->pb);
00087
00088 if (chunk_tag == MKTAG('D','A','T','A'))
00089 break;
00090
00091 len = avio_rb32(s->pb);
00092
00093 if ((unsigned) len > INT_MAX/2) {
00094 av_log(s, AV_LOG_ERROR, "Malformed header\n");
00095 return -1;
00096 }
00097
00098 header_size -= 8;
00099
00100 switch(chunk_tag){
00101 case MKTAG('C','O','M','M'):
00102 st->codec->channels = avio_rb32(s->pb) + 1;
00103 read_bitrate = avio_rb32(s->pb);
00104 rate_flag = avio_rb32(s->pb);
00105 avio_skip(s->pb, len-12);
00106
00107 st->codec->bit_rate = read_bitrate*1000;
00108 st->codec->bits_per_coded_sample = 16;
00109 break;
00110 case MKTAG('N','A','M','E'):
00111 add_metadata(s, "title" , len, header_size);
00112 break;
00113 case MKTAG('(','c',')',' '):
00114 add_metadata(s, "copyright", len, header_size);
00115 break;
00116 case MKTAG('A','U','T','H'):
00117 add_metadata(s, "author" , len, header_size);
00118 break;
00119 case MKTAG('A','L','B','M'):
00120 add_metadata(s, "album" , len, header_size);
00121 break;
00122 case MKTAG('T','R','C','K'):
00123 add_metadata(s, "track" , len, header_size);
00124 break;
00125 case MKTAG('C','O','M','T'):
00126 add_metadata(s, "comment" , len, header_size);
00127 break;
00128 case MKTAG('F','I','L','E'):
00129 add_metadata(s, "filename" , len, header_size);
00130 break;
00131 case MKTAG('D','S','I','Z'):
00132 add_metadata(s, "size" , len, header_size);
00133 break;
00134 case MKTAG('D','A','T','E'):
00135 add_metadata(s, "date" , len, header_size);
00136 break;
00137 case MKTAG('G','E','N','R'):
00138 add_metadata(s, "genre" , len, header_size);
00139 break;
00140 default:
00141 av_log(s, AV_LOG_ERROR, "Unknown chunk: %c%c%c%c\n",
00142 ((char*)&chunk_tag)[0], ((char*)&chunk_tag)[1],
00143 ((char*)&chunk_tag)[2], ((char*)&chunk_tag)[3]);
00144 avio_skip(s->pb, FFMIN(len, header_size));
00145 break;
00146 }
00147
00148 header_size -= len;
00149
00150 } while (header_size >= 0);
00151
00152 switch (rate_flag) {
00153 case -1:
00154 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
00155 return -1;
00156 case 44:
00157 st->codec->sample_rate = 44100;
00158 break;
00159 case 22:
00160 st->codec->sample_rate = 22050;
00161 break;
00162 case 11:
00163 st->codec->sample_rate = 11025;
00164 break;
00165 default:
00166 st->codec->sample_rate = rate_flag*1000;
00167 break;
00168 }
00169
00170 switch (((st->codec->sample_rate/1000) << 8) +
00171 read_bitrate/st->codec->channels) {
00172 case (11<<8) + 8 :
00173 case (8 <<8) + 8 :
00174 case (11<<8) + 10:
00175 case (22<<8) + 32:
00176 size = 512;
00177 break;
00178 case (16<<8) + 16:
00179 case (22<<8) + 20:
00180 case (22<<8) + 24:
00181 size = 1024;
00182 break;
00183 case (44<<8) + 40:
00184 case (44<<8) + 48:
00185 size = 2048;
00186 break;
00187 default:
00188 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
00189 st->codec->sample_rate, st->codec->bit_rate);
00190 return -1;
00191 }
00192 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
00193 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00194
00195 return 0;
00196 }
00197
00198 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
00199 {
00200 VqfContext *c = s->priv_data;
00201 int ret;
00202 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
00203
00204 pkt->pos = avio_tell(s->pb);
00205 pkt->stream_index = 0;
00206
00207 if (av_new_packet(pkt, size+2) < 0)
00208 return AVERROR(EIO);
00209
00210 pkt->data[0] = 8 - c->remaining_bits;
00211 pkt->data[1] = c->last_frame_bits;
00212 ret = avio_read(s->pb, pkt->data+2, size);
00213
00214 if (ret<=0) {
00215 av_free_packet(pkt);
00216 return AVERROR(EIO);
00217 }
00218
00219 c->last_frame_bits = pkt->data[size+1];
00220 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
00221
00222 return size+2;
00223 }
00224
00225 static int vqf_read_seek(AVFormatContext *s,
00226 int stream_index, int64_t timestamp, int flags)
00227 {
00228 VqfContext *c = s->priv_data;
00229 AVStream *st;
00230 int ret;
00231 int64_t pos;
00232
00233 st = s->streams[stream_index];
00234 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
00235 st->time_base.num,
00236 st->time_base.den * (int64_t)c->frame_bit_len,
00237 (flags & AVSEEK_FLAG_BACKWARD) ?
00238 AV_ROUND_DOWN : AV_ROUND_UP);
00239 pos *= c->frame_bit_len;
00240
00241 st->cur_dts = av_rescale(pos, st->time_base.den,
00242 st->codec->bit_rate * (int64_t)st->time_base.num);
00243
00244 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
00245 return ret;
00246
00247 c->remaining_bits = -7 - ((pos-7)&7);
00248 return 0;
00249 }
00250
00251 AVInputFormat ff_vqf_demuxer = {
00252 "vqf",
00253 NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
00254 sizeof(VqfContext),
00255 vqf_probe,
00256 vqf_read_header,
00257 vqf_read_packet,
00258 NULL,
00259 vqf_read_seek,
00260 .extensions = "vqf",
00261 };