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