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 "internal.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "libavutil/dict.h"
00026 #include "libavutil/mathematics.h"
00027 #include "riff.h"
00028
00029 typedef struct VqfContext {
00030 int frame_bit_len;
00031 uint8_t last_frame_bits;
00032 int remaining_bits;
00033 } VqfContext;
00034
00035 static int vqf_probe(AVProbeData *probe_packet)
00036 {
00037 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
00038 return 0;
00039
00040 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
00041 return AVPROBE_SCORE_MAX;
00042
00043 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
00044 return AVPROBE_SCORE_MAX;
00045
00046 return AVPROBE_SCORE_MAX/2;
00047 }
00048
00049 static void add_metadata(AVFormatContext *s, uint32_t tag,
00050 unsigned int tag_len, unsigned int remaining)
00051 {
00052 int len = FFMIN(tag_len, remaining);
00053 char *buf, key[5] = {0};
00054
00055 if (len == UINT_MAX)
00056 return;
00057
00058 buf = av_malloc(len+1);
00059 if (!buf)
00060 return;
00061 avio_read(s->pb, buf, len);
00062 buf[len] = 0;
00063 AV_WL32(key, tag);
00064 av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL);
00065 }
00066
00067 static const AVMetadataConv vqf_metadata_conv[] = {
00068 { "(c) ", "copyright" },
00069 { "ARNG", "arranger" },
00070 { "AUTH", "author" },
00071 { "BAND", "band" },
00072 { "CDCT", "conductor" },
00073 { "COMT", "comment" },
00074 { "FILE", "filename" },
00075 { "GENR", "genre" },
00076 { "LABL", "publisher" },
00077 { "MUSC", "composer" },
00078 { "NAME", "title" },
00079 { "NOTE", "note" },
00080 { "PROD", "producer" },
00081 { "PRSN", "personnel" },
00082 { "REMX", "remixer" },
00083 { "SING", "singer" },
00084 { "TRCK", "track" },
00085 { "WORD", "words" },
00086 { 0 },
00087 };
00088
00089 static int vqf_read_header(AVFormatContext *s)
00090 {
00091 VqfContext *c = s->priv_data;
00092 AVStream *st = avformat_new_stream(s, NULL);
00093 int chunk_tag;
00094 int rate_flag = -1;
00095 int header_size;
00096 int read_bitrate = 0;
00097 int size;
00098 uint8_t comm_chunk[12];
00099
00100 if (!st)
00101 return AVERROR(ENOMEM);
00102
00103 avio_skip(s->pb, 12);
00104
00105 header_size = avio_rb32(s->pb);
00106
00107 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00108 st->codec->codec_id = CODEC_ID_TWINVQ;
00109 st->start_time = 0;
00110
00111 do {
00112 int len;
00113 chunk_tag = avio_rl32(s->pb);
00114
00115 if (chunk_tag == MKTAG('D','A','T','A'))
00116 break;
00117
00118 len = avio_rb32(s->pb);
00119
00120 if ((unsigned) len > INT_MAX/2) {
00121 av_log(s, AV_LOG_ERROR, "Malformed header\n");
00122 return -1;
00123 }
00124
00125 header_size -= 8;
00126
00127 switch(chunk_tag){
00128 case MKTAG('C','O','M','M'):
00129 avio_read(s->pb, comm_chunk, 12);
00130 st->codec->channels = AV_RB32(comm_chunk ) + 1;
00131 read_bitrate = AV_RB32(comm_chunk + 4);
00132 rate_flag = AV_RB32(comm_chunk + 8);
00133 avio_skip(s->pb, len-12);
00134
00135 st->codec->bit_rate = read_bitrate*1000;
00136 break;
00137 case MKTAG('D','S','I','Z'):
00138 {
00139 char buf[8] = {0};
00140 int size = avio_rb32(s->pb);
00141
00142 snprintf(buf, sizeof(buf), "%d", size);
00143 av_dict_set(&s->metadata, "size", buf, 0);
00144 }
00145 break;
00146 case MKTAG('Y','E','A','R'):
00147 case MKTAG('E','N','C','D'):
00148 case MKTAG('E','X','T','R'):
00149 case MKTAG('_','Y','M','H'):
00150 case MKTAG('_','N','T','T'):
00151 case MKTAG('_','I','D','3'):
00152 avio_skip(s->pb, FFMIN(len, header_size));
00153 break;
00154 default:
00155 add_metadata(s, chunk_tag, len, header_size);
00156 break;
00157 }
00158
00159 header_size -= len;
00160
00161 } while (header_size >= 0);
00162
00163 switch (rate_flag) {
00164 case -1:
00165 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
00166 return -1;
00167 case 44:
00168 st->codec->sample_rate = 44100;
00169 break;
00170 case 22:
00171 st->codec->sample_rate = 22050;
00172 break;
00173 case 11:
00174 st->codec->sample_rate = 11025;
00175 break;
00176 default:
00177 st->codec->sample_rate = rate_flag*1000;
00178 break;
00179 }
00180
00181 switch (((st->codec->sample_rate/1000) << 8) +
00182 read_bitrate/st->codec->channels) {
00183 case (11<<8) + 8 :
00184 case (8 <<8) + 8 :
00185 case (11<<8) + 10:
00186 case (22<<8) + 32:
00187 size = 512;
00188 break;
00189 case (16<<8) + 16:
00190 case (22<<8) + 20:
00191 case (22<<8) + 24:
00192 size = 1024;
00193 break;
00194 case (44<<8) + 40:
00195 case (44<<8) + 48:
00196 size = 2048;
00197 break;
00198 default:
00199 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
00200 st->codec->sample_rate, st->codec->bit_rate);
00201 return -1;
00202 }
00203 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
00204 avpriv_set_pts_info(st, 64, size, st->codec->sample_rate);
00205
00206
00207 if (!(st->codec->extradata = av_malloc(12 + FF_INPUT_BUFFER_PADDING_SIZE)))
00208 return AVERROR(ENOMEM);
00209 st->codec->extradata_size = 12;
00210 memcpy(st->codec->extradata, comm_chunk, 12);
00211
00212 ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
00213
00214 return 0;
00215 }
00216
00217 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
00218 {
00219 VqfContext *c = s->priv_data;
00220 int ret;
00221 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
00222
00223 if (av_new_packet(pkt, size+2) < 0)
00224 return AVERROR(EIO);
00225
00226 pkt->pos = avio_tell(s->pb);
00227 pkt->stream_index = 0;
00228 pkt->duration = 1;
00229
00230 pkt->data[0] = 8 - c->remaining_bits;
00231 pkt->data[1] = c->last_frame_bits;
00232 ret = avio_read(s->pb, pkt->data+2, size);
00233
00234 if (ret<=0) {
00235 av_free_packet(pkt);
00236 return AVERROR(EIO);
00237 }
00238
00239 c->last_frame_bits = pkt->data[size+1];
00240 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
00241
00242 return size+2;
00243 }
00244
00245 static int vqf_read_seek(AVFormatContext *s,
00246 int stream_index, int64_t timestamp, int flags)
00247 {
00248 VqfContext *c = s->priv_data;
00249 AVStream *st;
00250 int ret;
00251 int64_t pos;
00252
00253 st = s->streams[stream_index];
00254 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
00255 st->time_base.num,
00256 st->time_base.den * (int64_t)c->frame_bit_len,
00257 (flags & AVSEEK_FLAG_BACKWARD) ?
00258 AV_ROUND_DOWN : AV_ROUND_UP);
00259 pos *= c->frame_bit_len;
00260
00261 st->cur_dts = av_rescale(pos, st->time_base.den,
00262 st->codec->bit_rate * (int64_t)st->time_base.num);
00263
00264 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
00265 return ret;
00266
00267 c->remaining_bits = -7 - ((pos-7)&7);
00268 return 0;
00269 }
00270
00271 AVInputFormat ff_vqf_demuxer = {
00272 .name = "vqf",
00273 .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
00274 .priv_data_size = sizeof(VqfContext),
00275 .read_probe = vqf_probe,
00276 .read_header = vqf_read_header,
00277 .read_packet = vqf_read_packet,
00278 .read_seek = vqf_read_seek,
00279 .extensions = "vqf,vql,vqe",
00280 };