| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | * VQF demuxer | ||
| 3 | * Copyright (c) 2009 Vitor Sessak | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "avformat.h" | ||
| 23 | #include "avio_internal.h" | ||
| 24 | #include "demux.h" | ||
| 25 | #include "internal.h" | ||
| 26 | #include "libavutil/intreadwrite.h" | ||
| 27 | #include "libavutil/dict.h" | ||
| 28 | #include "libavutil/mathematics.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "metadata.h" | ||
| 31 | |||
| 32 | typedef struct VqfContext { | ||
| 33 | int frame_bit_len; | ||
| 34 | uint8_t last_frame_bits; | ||
| 35 | int remaining_bits; | ||
| 36 | } VqfContext; | ||
| 37 | |||
| 38 | 7279 | static int vqf_probe(const AVProbeData *probe_packet) | |
| 39 | { | ||
| 40 | 2/2✓ Branch 0 taken 7277 times. ✓ Branch 1 taken 2 times. | 7279 | if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N')) | 
| 41 | 7277 | return 0; | |
| 42 | |||
| 43 | 1/2✓ Branch 0 taken 2 times. ✗ Branch 1 not taken. | 2 | if (!memcmp(probe_packet->buf + 4, "97012000", 8)) | 
| 44 | 2 | return AVPROBE_SCORE_MAX; | |
| 45 | |||
| 46 | ✗ | if (!memcmp(probe_packet->buf + 4, "00052200", 8)) | |
| 47 | ✗ | return AVPROBE_SCORE_MAX; | |
| 48 | |||
| 49 | ✗ | if (AV_RL32(probe_packet->buf + 12) > (1<<27)) | |
| 50 | ✗ | return AVPROBE_SCORE_EXTENSION/2; | |
| 51 | |||
| 52 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
| 53 | } | ||
| 54 | |||
| 55 | 10 | static int add_metadata(AVFormatContext *s, uint32_t tag, | |
| 56 | unsigned int tag_len, unsigned int remaining) | ||
| 57 | { | ||
| 58 | 10 | int len = FFMIN(tag_len, remaining); | |
| 59 | 10 | char *buf, key[5] = {0}; | |
| 60 | int ret; | ||
| 61 | |||
| 62 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 10 times. | 10 | if (len == UINT_MAX) | 
| 63 | ✗ | return AVERROR_INVALIDDATA; | |
| 64 | |||
| 65 | 10 | buf = av_malloc(len+1); | |
| 66 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 10 times. | 10 | if (!buf) | 
| 67 | ✗ | return AVERROR(ENOMEM); | |
| 68 | |||
| 69 | 10 | ret = ffio_read_size(s->pb, buf, len); | |
| 70 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 10 times. | 10 | if (ret < 0) { | 
| 71 | ✗ | av_free(buf); | |
| 72 | ✗ | return ret; | |
| 73 | } | ||
| 74 | 10 | buf[len] = 0; | |
| 75 | 10 | AV_WL32(key, tag); | |
| 76 | 10 | return av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL); | |
| 77 | } | ||
| 78 | |||
| 79 | static const AVMetadataConv vqf_metadata_conv[] = { | ||
| 80 | { "(c) ", "copyright" }, | ||
| 81 | { "ARNG", "arranger" }, | ||
| 82 | { "AUTH", "author" }, | ||
| 83 | { "BAND", "band" }, | ||
| 84 | { "CDCT", "conductor" }, | ||
| 85 | { "COMT", "comment" }, | ||
| 86 | { "FILE", "filename" }, | ||
| 87 | { "GENR", "genre" }, | ||
| 88 | { "LABL", "publisher" }, | ||
| 89 | { "MUSC", "composer" }, | ||
| 90 | { "NAME", "title" }, | ||
| 91 | { "NOTE", "note" }, | ||
| 92 | { "PROD", "producer" }, | ||
| 93 | { "PRSN", "personnel" }, | ||
| 94 | { "REMX", "remixer" }, | ||
| 95 | { "SING", "singer" }, | ||
| 96 | { "TRCK", "track" }, | ||
| 97 | { "WORD", "words" }, | ||
| 98 | { 0 }, | ||
| 99 | }; | ||
| 100 | |||
| 101 | 2 | static int vqf_read_header(AVFormatContext *s) | |
| 102 | { | ||
| 103 | 2 | VqfContext *c = s->priv_data; | |
| 104 | 2 | AVStream *st = avformat_new_stream(s, NULL); | |
| 105 | int chunk_tag; | ||
| 106 | 2 | int rate_flag = -1; | |
| 107 | int header_size; | ||
| 108 | 2 | int read_bitrate = 0; | |
| 109 | int size, ret; | ||
| 110 | uint8_t comm_chunk[12]; | ||
| 111 | |||
| 112 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. | 2 | if (!st) | 
| 113 | ✗ | return AVERROR(ENOMEM); | |
| 114 | |||
| 115 | 2 | avio_skip(s->pb, 12); | |
| 116 | |||
| 117 | 2 | header_size = avio_rb32(s->pb); | |
| 118 | |||
| 119 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. | 2 | if (header_size < 0) | 
| 120 | ✗ | return AVERROR_INVALIDDATA; | |
| 121 | |||
| 122 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 123 | 2 | st->codecpar->codec_id = AV_CODEC_ID_TWINVQ; | |
| 124 | 2 | st->start_time = 0; | |
| 125 | |||
| 126 | do { | ||
| 127 | int len; | ||
| 128 | 16 | chunk_tag = avio_rl32(s->pb); | |
| 129 | |||
| 130 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 14 times. | 16 | if (chunk_tag == MKTAG('D','A','T','A')) | 
| 131 | 2 | break; | |
| 132 | |||
| 133 | 14 | len = avio_rb32(s->pb); | |
| 134 | |||
| 135 | 2/4✓ Branch 0 taken 14 times. ✗ Branch 1 not taken. ✗ Branch 2 not taken. ✓ Branch 3 taken 14 times. | 14 | if ((unsigned) len > INT_MAX/2 || header_size < 8) { | 
| 136 | ✗ | av_log(s, AV_LOG_ERROR, "Malformed header\n"); | |
| 137 | ✗ | return -1; | |
| 138 | } | ||
| 139 | |||
| 140 | 14 | header_size -= 8; | |
| 141 | |||
| 142 | 3/4✓ Branch 0 taken 2 times. ✓ Branch 1 taken 2 times. ✗ Branch 2 not taken. ✓ Branch 3 taken 10 times. | 14 | switch(chunk_tag){ | 
| 143 | 2 | case MKTAG('C','O','M','M'): | |
| 144 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. | 2 | if (len < 12) | 
| 145 | ✗ | return AVERROR_INVALIDDATA; | |
| 146 | |||
| 147 | 2 | ret = ffio_read_size(s->pb, comm_chunk, 12); | |
| 148 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. | 2 | if (ret < 0) | 
| 149 | ✗ | return ret; | |
| 150 | 2 | st->codecpar->ch_layout.nb_channels = AV_RB32(comm_chunk) + 1; | |
| 151 | 2 | read_bitrate = AV_RB32(comm_chunk + 4); | |
| 152 | 2 | rate_flag = AV_RB32(comm_chunk + 8); | |
| 153 | 2 | avio_skip(s->pb, len-12); | |
| 154 | |||
| 155 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. | 2 | if (st->codecpar->ch_layout.nb_channels <= 0) { | 
| 156 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid number of channels\n"); | |
| 157 | ✗ | return AVERROR_INVALIDDATA; | |
| 158 | } | ||
| 159 | |||
| 160 | 2 | st->codecpar->bit_rate = (int64_t)read_bitrate * 1000; | |
| 161 | 2 | break; | |
| 162 | 2 | case MKTAG('D','S','I','Z'): // size of compressed data | |
| 163 | { | ||
| 164 | 2 | av_dict_set_int(&s->metadata, "size", avio_rb32(s->pb), 0); | |
| 165 | } | ||
| 166 | 2 | break; | |
| 167 | ✗ | case MKTAG('Y','E','A','R'): // recording date | |
| 168 | case MKTAG('E','N','C','D'): // compression date | ||
| 169 | case MKTAG('E','X','T','R'): // reserved | ||
| 170 | case MKTAG('_','Y','M','H'): // reserved | ||
| 171 | case MKTAG('_','N','T','T'): // reserved | ||
| 172 | case MKTAG('_','I','D','3'): // reserved for ID3 tags | ||
| 173 | ✗ | avio_skip(s->pb, FFMIN(len, header_size)); | |
| 174 | ✗ | break; | |
| 175 | 10 | default: | |
| 176 | 10 | ret = add_metadata(s, chunk_tag, len, header_size); | |
| 177 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 10 times. | 10 | if (ret < 0) | 
| 178 | ✗ | return ret; | |
| 179 | 10 | break; | |
| 180 | } | ||
| 181 | |||
| 182 | 14 | header_size -= len; | |
| 183 | |||
| 184 | 2/4✓ Branch 0 taken 14 times. ✗ Branch 1 not taken. ✓ Branch 3 taken 14 times. ✗ Branch 4 not taken. | 14 | } while (header_size >= 0 && !avio_feof(s->pb)); | 
| 185 | |||
| 186 | 1/5✗ Branch 0 not taken. ✗ Branch 1 not taken. ✓ Branch 2 taken 2 times. ✗ Branch 3 not taken. ✗ Branch 4 not taken. | 2 | switch (rate_flag) { | 
| 187 | ✗ | case -1: | |
| 188 | ✗ | av_log(s, AV_LOG_ERROR, "COMM tag not found!\n"); | |
| 189 | ✗ | return -1; | |
| 190 | ✗ | case 44: | |
| 191 | ✗ | st->codecpar->sample_rate = 44100; | |
| 192 | ✗ | break; | |
| 193 | 2 | case 22: | |
| 194 | 2 | st->codecpar->sample_rate = 22050; | |
| 195 | 2 | break; | |
| 196 | ✗ | case 11: | |
| 197 | ✗ | st->codecpar->sample_rate = 11025; | |
| 198 | ✗ | break; | |
| 199 | ✗ | default: | |
| 200 | ✗ | if (rate_flag < 8 || rate_flag > 44) { | |
| 201 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag); | |
| 202 | ✗ | return AVERROR_INVALIDDATA; | |
| 203 | } | ||
| 204 | ✗ | st->codecpar->sample_rate = rate_flag*1000; | |
| 205 | ✗ | break; | |
| 206 | } | ||
| 207 | |||
| 208 | 1/2✓ Branch 0 taken 2 times. ✗ Branch 1 not taken. | 2 | if (read_bitrate / st->codecpar->ch_layout.nb_channels < 8 || | 
| 209 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. | 2 | read_bitrate / st->codecpar->ch_layout.nb_channels > 48) { | 
| 210 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n", | |
| 211 | ✗ | read_bitrate / st->codecpar->ch_layout.nb_channels); | |
| 212 | ✗ | return AVERROR_INVALIDDATA; | |
| 213 | } | ||
| 214 | |||
| 215 | 2 | switch (((st->codecpar->sample_rate/1000) << 8) + | |
| 216 | 1/4✗ Branch 0 not taken. ✓ Branch 1 taken 2 times. ✗ Branch 2 not taken. ✗ Branch 3 not taken. | 2 | read_bitrate/st->codecpar->ch_layout.nb_channels) { | 
| 217 | ✗ | case (11<<8) + 8 : | |
| 218 | case (8 <<8) + 8 : | ||
| 219 | case (11<<8) + 10: | ||
| 220 | case (22<<8) + 32: | ||
| 221 | ✗ | size = 512; | |
| 222 | ✗ | break; | |
| 223 | 2 | case (16<<8) + 16: | |
| 224 | case (22<<8) + 20: | ||
| 225 | case (22<<8) + 24: | ||
| 226 | 2 | size = 1024; | |
| 227 | 2 | break; | |
| 228 | ✗ | case (44<<8) + 40: | |
| 229 | case (44<<8) + 48: | ||
| 230 | ✗ | size = 2048; | |
| 231 | ✗ | break; | |
| 232 | ✗ | default: | |
| 233 | ✗ | av_log(s, AV_LOG_ERROR, "Mode not supported: %d Hz, %"PRId64" kb/s.\n", | |
| 234 | ✗ | st->codecpar->sample_rate, st->codecpar->bit_rate); | |
| 235 | ✗ | return -1; | |
| 236 | } | ||
| 237 | 2 | c->frame_bit_len = st->codecpar->bit_rate*size/st->codecpar->sample_rate; | |
| 238 | 2 | avpriv_set_pts_info(st, 64, size, st->codecpar->sample_rate); | |
| 239 | |||
| 240 | /* put first 12 bytes of COMM chunk in extradata */ | ||
| 241 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 2 times. | 2 | if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0) | 
| 242 | ✗ | return ret; | |
| 243 | 2 | memcpy(st->codecpar->extradata, comm_chunk, 12); | |
| 244 | |||
| 245 | 2 | ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv); | |
| 246 | |||
| 247 | 2 | return 0; | |
| 248 | } | ||
| 249 | |||
| 250 | 5178 | static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 251 | { | ||
| 252 | 5178 | VqfContext *c = s->priv_data; | |
| 253 | int ret; | ||
| 254 | 5178 | int size = (c->frame_bit_len - c->remaining_bits + 7)>>3; | |
| 255 | |||
| 256 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 5178 times. | 5178 | if ((ret = av_new_packet(pkt, size + 2)) < 0) | 
| 257 | ✗ | return ret; | |
| 258 | |||
| 259 | 5178 | pkt->pos = avio_tell(s->pb); | |
| 260 | 5178 | pkt->stream_index = 0; | |
| 261 | 5178 | pkt->duration = 1; | |
| 262 | |||
| 263 | 5178 | pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip | |
| 264 | 5178 | pkt->data[1] = c->last_frame_bits; | |
| 265 | 5178 | ret = ffio_read_size(s->pb, pkt->data + 2, size); | |
| 266 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 5176 times. | 5178 | if (ret < 0) | 
| 267 | 2 | return ret; | |
| 268 | |||
| 269 | 5176 | c->last_frame_bits = pkt->data[size+1]; | |
| 270 | 5176 | c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits; | |
| 271 | |||
| 272 | 5176 | return 0; | |
| 273 | } | ||
| 274 | |||
| 275 | ✗ | static int vqf_read_seek(AVFormatContext *s, | |
| 276 | int stream_index, int64_t timestamp, int flags) | ||
| 277 | { | ||
| 278 | ✗ | VqfContext *c = s->priv_data; | |
| 279 | AVStream *st; | ||
| 280 | int64_t ret; | ||
| 281 | int64_t pos; | ||
| 282 | |||
| 283 | ✗ | st = s->streams[stream_index]; | |
| 284 | ✗ | pos = av_rescale_rnd(timestamp * st->codecpar->bit_rate, | |
| 285 | ✗ | st->time_base.num, | |
| 286 | ✗ | st->time_base.den * (int64_t)c->frame_bit_len, | |
| 287 | ✗ | (flags & AVSEEK_FLAG_BACKWARD) ? | |
| 288 | AV_ROUND_DOWN : AV_ROUND_UP); | ||
| 289 | ✗ | pos *= c->frame_bit_len; | |
| 290 | |||
| 291 | ✗ | ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den, | |
| 292 | ✗ | st->codecpar->bit_rate * (int64_t)st->time_base.num); | |
| 293 | |||
| 294 | ✗ | if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + ffformatcontext(s)->data_offset, SEEK_SET)) < 0) | |
| 295 | ✗ | return ret; | |
| 296 | |||
| 297 | ✗ | c->remaining_bits = -7 - ((pos-7)&7); | |
| 298 | ✗ | return 0; | |
| 299 | } | ||
| 300 | |||
| 301 | const FFInputFormat ff_vqf_demuxer = { | ||
| 302 | .p.name = "vqf", | ||
| 303 | .p.long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"), | ||
| 304 | .p.extensions = "vqf,vql,vqe", | ||
| 305 | .priv_data_size = sizeof(VqfContext), | ||
| 306 | .read_probe = vqf_probe, | ||
| 307 | .read_header = vqf_read_header, | ||
| 308 | .read_packet = vqf_read_packet, | ||
| 309 | .read_seek = vqf_read_seek, | ||
| 310 | }; | ||
| 311 |