Statistics
| Branch: | Revision:

ffmpeg / libavformat / vqf.c @ e356fc57

History | View | Annotate | Download (7.32 KB)

1 616e899c Vitor Sessak
/*
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 "libavutil/intreadwrite.h"
24
25
typedef struct VqfContext {
26
    int frame_bit_len;
27
    uint8_t last_frame_bits;
28
    int remaining_bits;
29
} VqfContext;
30
31
static int vqf_probe(AVProbeData *probe_packet)
32
{
33
    if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
34
        return 0;
35
36
    if (!memcmp(probe_packet->buf + 4, "97012000", 8))
37
        return AVPROBE_SCORE_MAX;
38
39
    if (!memcmp(probe_packet->buf + 4, "00052200", 8))
40
        return AVPROBE_SCORE_MAX;
41
42
    return AVPROBE_SCORE_MAX/2;
43
}
44
45
static void add_metadata(AVFormatContext *s, const char *tag,
46
                         unsigned int tag_len, unsigned int remaining)
47
{
48 12ad6671 Michael Niedermayer
    int len = FFMIN(tag_len, remaining);
49
    char *buf;
50 616e899c Vitor Sessak
51 12ad6671 Michael Niedermayer
    if (len == UINT_MAX)
52
        return;
53 616e899c Vitor Sessak
54 12ad6671 Michael Niedermayer
    buf = av_malloc(len+1);
55
    if (!buf)
56
        return;
57 b7effd4e Anton Khirnov
    avio_read(s->pb, buf, len);
58 616e899c Vitor Sessak
    buf[len] = 0;
59 12ad6671 Michael Niedermayer
    av_metadata_set2(&s->metadata, tag, buf, AV_METADATA_DONT_STRDUP_VAL);
60 616e899c Vitor Sessak
}
61
62
static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
63
{
64
    VqfContext *c = s->priv_data;
65
    AVStream *st  = av_new_stream(s, 0);
66
    int chunk_tag;
67
    int rate_flag = -1;
68
    int header_size;
69
    int read_bitrate = 0;
70
    int size;
71
72
    if (!st)
73
        return AVERROR(ENOMEM);
74
75 e356fc57 Anton Khirnov
    avio_seek(s->pb, 12, SEEK_CUR);
76 616e899c Vitor Sessak
77 b7effd4e Anton Khirnov
    header_size = avio_rb32(s->pb);
78 616e899c Vitor Sessak
79 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
80 616e899c Vitor Sessak
    st->codec->codec_id   = CODEC_ID_TWINVQ;
81
    st->start_time = 0;
82
83
    do {
84
        int len;
85 b7effd4e Anton Khirnov
        chunk_tag = avio_rl32(s->pb);
86 616e899c Vitor Sessak
87
        if (chunk_tag == MKTAG('D','A','T','A'))
88
            break;
89
90 b7effd4e Anton Khirnov
        len = avio_rb32(s->pb);
91 616e899c Vitor Sessak
92
        if ((unsigned) len > INT_MAX/2) {
93
            av_log(s, AV_LOG_ERROR, "Malformed header\n");
94
            return -1;
95
        }
96
97
        header_size -= 8;
98
99
        switch(chunk_tag){
100
        case MKTAG('C','O','M','M'):
101 b7effd4e Anton Khirnov
            st->codec->channels = avio_rb32(s->pb) + 1;
102
            read_bitrate        = avio_rb32(s->pb);
103
            rate_flag           = avio_rb32(s->pb);
104 e356fc57 Anton Khirnov
            avio_seek(s->pb, len-12, SEEK_CUR);
105 616e899c Vitor Sessak
106
            st->codec->bit_rate              = read_bitrate*1000;
107
            st->codec->bits_per_coded_sample = 16;
108
            break;
109
        case MKTAG('N','A','M','E'):
110
            add_metadata(s, "title"    , len, header_size);
111
            break;
112
        case MKTAG('(','c',')',' '):
113
            add_metadata(s, "copyright", len, header_size);
114
            break;
115
        case MKTAG('A','U','T','H'):
116
            add_metadata(s, "author"   , len, header_size);
117
            break;
118
        case MKTAG('A','L','B','M'):
119
            add_metadata(s, "album"    , len, header_size);
120
            break;
121
        case MKTAG('T','R','C','K'):
122
            add_metadata(s, "track"    , len, header_size);
123
            break;
124
        case MKTAG('C','O','M','T'):
125
            add_metadata(s, "comment"  , len, header_size);
126
            break;
127
        case MKTAG('F','I','L','E'):
128
            add_metadata(s, "filename" , len, header_size);
129
            break;
130
        case MKTAG('D','S','I','Z'):
131
            add_metadata(s, "size"     , len, header_size);
132
            break;
133
        case MKTAG('D','A','T','E'):
134
            add_metadata(s, "date"     , len, header_size);
135
            break;
136
        case MKTAG('G','E','N','R'):
137
            add_metadata(s, "genre"    , len, header_size);
138
            break;
139
        default:
140
            av_log(s, AV_LOG_ERROR, "Unknown chunk: %c%c%c%c\n",
141
                   ((char*)&chunk_tag)[0], ((char*)&chunk_tag)[1],
142
                   ((char*)&chunk_tag)[2], ((char*)&chunk_tag)[3]);
143 e356fc57 Anton Khirnov
            avio_seek(s->pb, FFMIN(len, header_size), SEEK_CUR);
144 616e899c Vitor Sessak
            break;
145
        }
146
147
        header_size -= len;
148
149
    } while (header_size >= 0);
150
151
    switch (rate_flag) {
152
    case -1:
153
        av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
154
        return -1;
155
    case 44:
156
        st->codec->sample_rate = 44100;
157
        break;
158
    case 22:
159
        st->codec->sample_rate = 22050;
160
        break;
161
    case 11:
162
        st->codec->sample_rate = 11025;
163
        break;
164
    default:
165
        st->codec->sample_rate = rate_flag*1000;
166
        break;
167
    }
168
169
    switch (((st->codec->sample_rate/1000) << 8) +
170
            read_bitrate/st->codec->channels) {
171
    case (11<<8) + 8 :
172
    case (8 <<8) + 8 :
173
    case (11<<8) + 10:
174
    case (22<<8) + 32:
175
        size = 512;
176
        break;
177
    case (16<<8) + 16:
178
    case (22<<8) + 20:
179
    case (22<<8) + 24:
180
        size = 1024;
181
        break;
182
    case (44<<8) + 40:
183
    case (44<<8) + 48:
184
        size = 2048;
185
        break;
186
    default:
187
        av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
188
               st->codec->sample_rate, st->codec->bit_rate);
189
        return -1;
190
    }
191
    c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
192 efd8b009 Vitor Sessak
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
193 616e899c Vitor Sessak
194
    return 0;
195
}
196
197
static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
198
{
199
    VqfContext *c = s->priv_data;
200
    int ret;
201
    int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
202
203
    pkt->pos          = url_ftell(s->pb);
204
    pkt->stream_index = 0;
205
206
    if (av_new_packet(pkt, size+2) < 0)
207
        return AVERROR(EIO);
208
209
    pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
210
    pkt->data[1] = c->last_frame_bits;
211 b7effd4e Anton Khirnov
    ret = avio_read(s->pb, pkt->data+2, size);
212 616e899c Vitor Sessak
213
    if (ret<=0) {
214
        av_free_packet(pkt);
215
        return AVERROR(EIO);
216
    }
217
218
    c->last_frame_bits = pkt->data[size+1];
219
    c->remaining_bits  = (size << 3) - c->frame_bit_len + c->remaining_bits;
220
221
    return size+2;
222
}
223
224
static int vqf_read_seek(AVFormatContext *s,
225
                         int stream_index, int64_t timestamp, int flags)
226
{
227
    VqfContext *c = s->priv_data;
228
    AVStream *st;
229
    int ret;
230
    int64_t pos;
231
232
    st = s->streams[stream_index];
233
    pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
234
                         st->time_base.num,
235
                         st->time_base.den * (int64_t)c->frame_bit_len,
236
                         (flags & AVSEEK_FLAG_BACKWARD) ?
237
                                                   AV_ROUND_DOWN : AV_ROUND_UP);
238
    pos *= c->frame_bit_len;
239
240
    st->cur_dts = av_rescale(pos, st->time_base.den,
241
                             st->codec->bit_rate * (int64_t)st->time_base.num);
242
243 6b4aa5da Anton Khirnov
    if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
244 616e899c Vitor Sessak
        return ret;
245
246
    c->remaining_bits = -7 - ((pos-7)&7);
247
    return 0;
248
}
249
250 c6610a21 Diego Elio Pettenò
AVInputFormat ff_vqf_demuxer = {
251 616e899c Vitor Sessak
    "vqf",
252
    NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
253
    sizeof(VqfContext),
254
    vqf_probe,
255
    vqf_read_header,
256
    vqf_read_packet,
257
    NULL,
258
    vqf_read_seek,
259
    .extensions = "vqf",
260
};