vqf.c 8.47 KB
Newer Older
Vitor Sessak's avatar
Vitor Sessak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * VQF demuxer
 * Copyright (c) 2009 Vitor Sessak
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "avformat.h"
23
#include "internal.h"
Vitor Sessak's avatar
Vitor Sessak committed
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/dict.h"
26
#include "libavutil/mathematics.h"
27
#include "riff.h"
Vitor Sessak's avatar
Vitor Sessak committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

typedef struct VqfContext {
    int frame_bit_len;
    uint8_t last_frame_bits;
    int remaining_bits;
} VqfContext;

static int vqf_probe(AVProbeData *probe_packet)
{
    if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
        return 0;

    if (!memcmp(probe_packet->buf + 4, "97012000", 8))
        return AVPROBE_SCORE_MAX;

    if (!memcmp(probe_packet->buf + 4, "00052200", 8))
        return AVPROBE_SCORE_MAX;

46 47 48
    if (AV_RL32(probe_packet->buf + 12) > (1<<27))
        return AVPROBE_SCORE_EXTENSION/2;

49
    return AVPROBE_SCORE_EXTENSION;
Vitor Sessak's avatar
Vitor Sessak committed
50 51
}

52
static void add_metadata(AVFormatContext *s, uint32_t tag,
Vitor Sessak's avatar
Vitor Sessak committed
53 54
                         unsigned int tag_len, unsigned int remaining)
{
55
    int len = FFMIN(tag_len, remaining);
56
    char *buf, key[5] = {0};
Vitor Sessak's avatar
Vitor Sessak committed
57

58 59
    if (len == UINT_MAX)
        return;
Vitor Sessak's avatar
Vitor Sessak committed
60

61 62 63
    buf = av_malloc(len+1);
    if (!buf)
        return;
64
    avio_read(s->pb, buf, len);
Vitor Sessak's avatar
Vitor Sessak committed
65
    buf[len] = 0;
66 67
    AV_WL32(key, tag);
    av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL);
Vitor Sessak's avatar
Vitor Sessak committed
68 69
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static const AVMetadataConv vqf_metadata_conv[] = {
    { "(c) ", "copyright" },
    { "ARNG", "arranger"  },
    { "AUTH", "author"    },
    { "BAND", "band"      },
    { "CDCT", "conductor" },
    { "COMT", "comment"   },
    { "FILE", "filename"  },
    { "GENR", "genre"     },
    { "LABL", "publisher" },
    { "MUSC", "composer"  },
    { "NAME", "title"     },
    { "NOTE", "note"      },
    { "PROD", "producer"  },
    { "PRSN", "personnel" },
    { "REMX", "remixer"   },
    { "SING", "singer"    },
    { "TRCK", "track"     },
    { "WORD", "words"     },
    { 0 },
};

92
static int vqf_read_header(AVFormatContext *s)
Vitor Sessak's avatar
Vitor Sessak committed
93 94
{
    VqfContext *c = s->priv_data;
95
    AVStream *st  = avformat_new_stream(s, NULL);
Vitor Sessak's avatar
Vitor Sessak committed
96 97 98 99 100
    int chunk_tag;
    int rate_flag = -1;
    int header_size;
    int read_bitrate = 0;
    int size;
101
    uint8_t comm_chunk[12];
Vitor Sessak's avatar
Vitor Sessak committed
102 103 104 105

    if (!st)
        return AVERROR(ENOMEM);

106
    avio_skip(s->pb, 12);
Vitor Sessak's avatar
Vitor Sessak committed
107

108
    header_size = avio_rb32(s->pb);
Vitor Sessak's avatar
Vitor Sessak committed
109

110
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
111
    st->codec->codec_id   = AV_CODEC_ID_TWINVQ;
Vitor Sessak's avatar
Vitor Sessak committed
112 113 114 115
    st->start_time = 0;

    do {
        int len;
116
        chunk_tag = avio_rl32(s->pb);
Vitor Sessak's avatar
Vitor Sessak committed
117 118 119 120

        if (chunk_tag == MKTAG('D','A','T','A'))
            break;

121
        len = avio_rb32(s->pb);
Vitor Sessak's avatar
Vitor Sessak committed
122 123 124 125 126 127 128 129 130 131

        if ((unsigned) len > INT_MAX/2) {
            av_log(s, AV_LOG_ERROR, "Malformed header\n");
            return -1;
        }

        header_size -= 8;

        switch(chunk_tag){
        case MKTAG('C','O','M','M'):
132 133 134 135
            avio_read(s->pb, comm_chunk, 12);
            st->codec->channels = AV_RB32(comm_chunk    ) + 1;
            read_bitrate        = AV_RB32(comm_chunk + 4);
            rate_flag           = AV_RB32(comm_chunk + 8);
136
            avio_skip(s->pb, len-12);
Vitor Sessak's avatar
Vitor Sessak committed
137

138 139 140 141 142
            if (st->codec->channels <= 0) {
                av_log(s, AV_LOG_ERROR, "Invalid number of channels\n");
                return AVERROR_INVALIDDATA;
            }

Vitor Sessak's avatar
Vitor Sessak committed
143 144
            st->codec->bit_rate              = read_bitrate*1000;
            break;
145 146 147 148 149 150 151 152
        case MKTAG('D','S','I','Z'): // size of compressed data
        {
            char buf[8] = {0};
            int size = avio_rb32(s->pb);

            snprintf(buf, sizeof(buf), "%d", size);
            av_dict_set(&s->metadata, "size", buf, 0);
        }
Vitor Sessak's avatar
Vitor Sessak committed
153
            break;
154 155 156 157 158 159 160
        case MKTAG('Y','E','A','R'): // recording date
        case MKTAG('E','N','C','D'): // compression date
        case MKTAG('E','X','T','R'): // reserved
        case MKTAG('_','Y','M','H'): // reserved
        case MKTAG('_','N','T','T'): // reserved
        case MKTAG('_','I','D','3'): // reserved for ID3 tags
            avio_skip(s->pb, FFMIN(len, header_size));
Vitor Sessak's avatar
Vitor Sessak committed
161 162
            break;
        default:
163
            add_metadata(s, chunk_tag, len, header_size);
Vitor Sessak's avatar
Vitor Sessak committed
164 165 166 167 168
            break;
        }

        header_size -= len;

169
    } while (header_size >= 0 && !url_feof(s->pb));
Vitor Sessak's avatar
Vitor Sessak committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

    switch (rate_flag) {
    case -1:
        av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
        return -1;
    case 44:
        st->codec->sample_rate = 44100;
        break;
    case 22:
        st->codec->sample_rate = 22050;
        break;
    case 11:
        st->codec->sample_rate = 11025;
        break;
    default:
185 186 187
        if (rate_flag < 8 || rate_flag > 44) {
            av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
            return AVERROR_INVALIDDATA;
188
        }
Vitor Sessak's avatar
Vitor Sessak committed
189 190 191 192
        st->codec->sample_rate = rate_flag*1000;
        break;
    }

193 194 195 196 197 198 199
    if (read_bitrate / st->codec->channels <  8 ||
        read_bitrate / st->codec->channels > 48) {
        av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
               read_bitrate / st->codec->channels);
        return AVERROR_INVALIDDATA;
    }

Vitor Sessak's avatar
Vitor Sessak committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    switch (((st->codec->sample_rate/1000) << 8) +
            read_bitrate/st->codec->channels) {
    case (11<<8) + 8 :
    case (8 <<8) + 8 :
    case (11<<8) + 10:
    case (22<<8) + 32:
        size = 512;
        break;
    case (16<<8) + 16:
    case (22<<8) + 20:
    case (22<<8) + 24:
        size = 1024;
        break;
    case (44<<8) + 40:
    case (44<<8) + 48:
        size = 2048;
        break;
    default:
        av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
               st->codec->sample_rate, st->codec->bit_rate);
        return -1;
    }
    c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
223
    avpriv_set_pts_info(st, 64, size, st->codec->sample_rate);
Vitor Sessak's avatar
Vitor Sessak committed
224

225
    /* put first 12 bytes of COMM chunk in extradata */
226
    if (ff_alloc_extradata(st->codec, 12))
227 228 229
        return AVERROR(ENOMEM);
    memcpy(st->codec->extradata, comm_chunk, 12);

230 231
    ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);

Vitor Sessak's avatar
Vitor Sessak committed
232 233 234 235 236 237 238 239 240 241 242 243
    return 0;
}

static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    VqfContext *c = s->priv_data;
    int ret;
    int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;

    if (av_new_packet(pkt, size+2) < 0)
        return AVERROR(EIO);

244 245
    pkt->pos          = avio_tell(s->pb);
    pkt->stream_index = 0;
246
    pkt->duration     = 1;
247

Vitor Sessak's avatar
Vitor Sessak committed
248 249
    pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
    pkt->data[1] = c->last_frame_bits;
250
    ret = avio_read(s->pb, pkt->data+2, size);
Vitor Sessak's avatar
Vitor Sessak committed
251

252
    if (ret != size) {
Vitor Sessak's avatar
Vitor Sessak committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        av_free_packet(pkt);
        return AVERROR(EIO);
    }

    c->last_frame_bits = pkt->data[size+1];
    c->remaining_bits  = (size << 3) - c->frame_bit_len + c->remaining_bits;

    return size+2;
}

static int vqf_read_seek(AVFormatContext *s,
                         int stream_index, int64_t timestamp, int flags)
{
    VqfContext *c = s->priv_data;
    AVStream *st;
    int ret;
    int64_t pos;

    st = s->streams[stream_index];
    pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
                         st->time_base.num,
                         st->time_base.den * (int64_t)c->frame_bit_len,
                         (flags & AVSEEK_FLAG_BACKWARD) ?
                                                   AV_ROUND_DOWN : AV_ROUND_UP);
    pos *= c->frame_bit_len;

    st->cur_dts = av_rescale(pos, st->time_base.den,
                             st->codec->bit_rate * (int64_t)st->time_base.num);

282
    if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
Vitor Sessak's avatar
Vitor Sessak committed
283 284 285 286 287 288
        return ret;

    c->remaining_bits = -7 - ((pos-7)&7);
    return 0;
}

289
AVInputFormat ff_vqf_demuxer = {
290 291 292 293 294 295 296
    .name           = "vqf",
    .long_name      = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
    .priv_data_size = sizeof(VqfContext),
    .read_probe     = vqf_probe,
    .read_header    = vqf_read_header,
    .read_packet    = vqf_read_packet,
    .read_seek      = vqf_read_seek,
297
    .extensions     = "vqf,vql,vqe",
Vitor Sessak's avatar
Vitor Sessak committed
298
};