~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/mmf.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Yamaha SMAF format
 
3
 * Copyright (c) 2005 Vidar Madsen
 
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
#include "avformat.h"
 
22
#include "allformats.h"
 
23
#include "riff.h"
 
24
 
 
25
typedef struct {
 
26
    offset_t atrpos, atsqpos, awapos;
 
27
    offset_t data_size;
 
28
} MMFContext;
 
29
 
 
30
static int mmf_rates[] = { 4000, 8000, 11025, 22050, 44100 };
 
31
 
 
32
static int mmf_rate_code(int rate)
 
33
{
 
34
    int i;
 
35
    for(i = 0; i < 5; i++)
 
36
        if(mmf_rates[i] == rate)
 
37
            return i;
 
38
    return -1;
 
39
}
 
40
 
 
41
static int mmf_rate(int code)
 
42
{
 
43
    if((code < 0) || (code > 4))
 
44
        return -1;
 
45
    return mmf_rates[code];
 
46
}
 
47
 
 
48
#ifdef CONFIG_MUXERS
 
49
/* Copy of end_tag() from avienc.c, but for big-endian chunk size */
 
50
static void end_tag_be(ByteIOContext *pb, offset_t start)
 
51
{
 
52
    offset_t pos;
 
53
 
 
54
    pos = url_ftell(pb);
 
55
    url_fseek(pb, start - 4, SEEK_SET);
 
56
    put_be32(pb, (uint32_t)(pos - start));
 
57
    url_fseek(pb, pos, SEEK_SET);
 
58
}
 
59
 
 
60
static int mmf_write_header(AVFormatContext *s)
 
61
{
 
62
    MMFContext *mmf = s->priv_data;
 
63
    ByteIOContext *pb = &s->pb;
 
64
    offset_t pos;
 
65
    int rate;
 
66
 
 
67
    rate = mmf_rate_code(s->streams[0]->codec->sample_rate);
 
68
    if(rate < 0) {
 
69
        av_log(s, AV_LOG_ERROR, "Unsupported sample rate %d\n", s->streams[0]->codec->sample_rate);
 
70
        return -1;
 
71
    }
 
72
 
 
73
    put_tag(pb, "MMMD");
 
74
    put_be32(pb, 0);
 
75
    pos = start_tag(pb, "CNTI");
 
76
    put_byte(pb, 0); /* class */
 
77
    put_byte(pb, 0); /* type */
 
78
    put_byte(pb, 0); /* code type */
 
79
    put_byte(pb, 0); /* status */
 
80
    put_byte(pb, 0); /* counts */
 
81
    put_tag(pb, "VN:libavcodec,"); /* metadata ("ST:songtitle,VN:version,...") */
 
82
    end_tag_be(pb, pos);
 
83
 
 
84
    put_buffer(pb, "ATR\x00", 4);
 
85
    put_be32(pb, 0);
 
86
    mmf->atrpos = url_ftell(pb);
 
87
    put_byte(pb, 0); /* format type */
 
88
    put_byte(pb, 0); /* sequence type */
 
89
    put_byte(pb, (0 << 7) | (1 << 4) | rate); /* (channel << 7) | (format << 4) | rate */
 
90
    put_byte(pb, 0); /* wave base bit */
 
91
    put_byte(pb, 2); /* time base d */
 
92
    put_byte(pb, 2); /* time base g */
 
93
 
 
94
    put_tag(pb, "Atsq");
 
95
    put_be32(pb, 16);
 
96
    mmf->atsqpos = url_ftell(pb);
 
97
    /* Will be filled on close */
 
98
    put_buffer(pb, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16);
 
99
 
 
100
    mmf->awapos = start_tag(pb, "Awa\x01");
 
101
 
 
102
    av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
 
103
 
 
104
    put_flush_packet(pb);
 
105
 
 
106
    return 0;
 
107
}
 
108
 
 
109
static int mmf_write_packet(AVFormatContext *s, AVPacket *pkt)
 
110
{
 
111
    ByteIOContext *pb = &s->pb;
 
112
    put_buffer(pb, pkt->data, pkt->size);
 
113
    return 0;
 
114
}
 
115
 
 
116
/* Write a variable-length symbol */
 
117
static void put_varlength(ByteIOContext *pb, int val)
 
118
{
 
119
    if(val < 128)
 
120
        put_byte(pb, val);
 
121
    else {
 
122
        val -= 128;
 
123
        put_byte(pb, 0x80 | val >> 7);
 
124
        put_byte(pb, 0x7f & val);
 
125
    }
 
126
}
 
127
 
 
128
static int mmf_write_trailer(AVFormatContext *s)
 
129
{
 
130
    ByteIOContext *pb = &s->pb;
 
131
    MMFContext *mmf = s->priv_data;
 
132
    offset_t pos, size;
 
133
    int gatetime;
 
134
 
 
135
    if (!url_is_streamed(&s->pb)) {
 
136
        /* Fill in length fields */
 
137
        end_tag_be(pb, mmf->awapos);
 
138
        end_tag_be(pb, mmf->atrpos);
 
139
        end_tag_be(pb, 8);
 
140
 
 
141
        pos = url_ftell(pb);
 
142
        size = pos - mmf->awapos;
 
143
 
 
144
        /* Fill Atsq chunk */
 
145
        url_fseek(pb, mmf->atsqpos, SEEK_SET);
 
146
 
 
147
        /* "play wav" */
 
148
        put_byte(pb, 0); /* start time */
 
149
        put_byte(pb, 1); /* (channel << 6) | wavenum */
 
150
        gatetime = size * 500 / s->streams[0]->codec->sample_rate;
 
151
        put_varlength(pb, gatetime); /* duration */
 
152
 
 
153
        /* "nop" */
 
154
        put_varlength(pb, gatetime); /* start time */
 
155
        put_buffer(pb, "\xff\x00", 2); /* nop */
 
156
 
 
157
        /* "end of sequence" */
 
158
        put_buffer(pb, "\x00\x00\x00\x00", 4);
 
159
 
 
160
        url_fseek(pb, pos, SEEK_SET);
 
161
 
 
162
        put_flush_packet(pb);
 
163
    }
 
164
    return 0;
 
165
}
 
166
#endif //CONFIG_MUXERS
 
167
 
 
168
static int mmf_probe(AVProbeData *p)
 
169
{
 
170
    /* check file header */
 
171
    if (p->buf_size <= 32)
 
172
        return 0;
 
173
    if (p->buf[0] == 'M' && p->buf[1] == 'M' &&
 
174
        p->buf[2] == 'M' && p->buf[3] == 'D' &&
 
175
        p->buf[8] == 'C' && p->buf[9] == 'N' &&
 
176
        p->buf[10] == 'T' && p->buf[11] == 'I')
 
177
        return AVPROBE_SCORE_MAX;
 
178
    else
 
179
        return 0;
 
180
}
 
181
 
 
182
/* mmf input */
 
183
static int mmf_read_header(AVFormatContext *s,
 
184
                           AVFormatParameters *ap)
 
185
{
 
186
    MMFContext *mmf = s->priv_data;
 
187
    unsigned int tag;
 
188
    ByteIOContext *pb = &s->pb;
 
189
    AVStream *st;
 
190
    offset_t file_size, size;
 
191
    int rate, params;
 
192
 
 
193
    tag = get_le32(pb);
 
194
    if (tag != MKTAG('M', 'M', 'M', 'D'))
 
195
        return -1;
 
196
    file_size = get_be32(pb);
 
197
 
 
198
    /* Skip some unused chunks that may or may not be present */
 
199
    for(;; url_fseek(pb, size, SEEK_CUR)) {
 
200
        tag = get_le32(pb);
 
201
        size = get_be32(pb);
 
202
        if(tag == MKTAG('C','N','T','I')) continue;
 
203
        if(tag == MKTAG('O','P','D','A')) continue;
 
204
        break;
 
205
    }
 
206
 
 
207
    /* Tag = "ATRx", where "x" = track number */
 
208
    if ((tag & 0xffffff) == MKTAG('M', 'T', 'R', 0)) {
 
209
        av_log(s, AV_LOG_ERROR, "MIDI like format found, unsupported\n");
 
210
        return -1;
 
211
    }
 
212
    if ((tag & 0xffffff) != MKTAG('A', 'T', 'R', 0)) {
 
213
        av_log(s, AV_LOG_ERROR, "Unsupported SMAF chunk %08x\n", tag);
 
214
        return -1;
 
215
    }
 
216
 
 
217
    get_byte(pb); /* format type */
 
218
    get_byte(pb); /* sequence type */
 
219
    params = get_byte(pb); /* (channel << 7) | (format << 4) | rate */
 
220
    rate = mmf_rate(params & 0x0f);
 
221
    if(rate  < 0) {
 
222
        av_log(s, AV_LOG_ERROR, "Invalid sample rate\n");
 
223
        return -1;
 
224
    }
 
225
    get_byte(pb); /* wave base bit */
 
226
    get_byte(pb); /* time base d */
 
227
    get_byte(pb); /* time base g */
 
228
 
 
229
    /* Skip some unused chunks that may or may not be present */
 
230
    for(;; url_fseek(pb, size, SEEK_CUR)) {
 
231
        tag = get_le32(pb);
 
232
        size = get_be32(pb);
 
233
        if(tag == MKTAG('A','t','s','q')) continue;
 
234
        if(tag == MKTAG('A','s','p','I')) continue;
 
235
        break;
 
236
    }
 
237
 
 
238
    /* Make sure it's followed by an Awa chunk, aka wave data */
 
239
    if ((tag & 0xffffff) != MKTAG('A', 'w', 'a', 0)) {
 
240
        av_log(s, AV_LOG_ERROR, "Unexpected SMAF chunk %08x\n", tag);
 
241
        return -1;
 
242
    }
 
243
    mmf->data_size = size;
 
244
 
 
245
    st = av_new_stream(s, 0);
 
246
    if (!st)
 
247
        return AVERROR_NOMEM;
 
248
 
 
249
    st->codec->codec_type = CODEC_TYPE_AUDIO;
 
250
    st->codec->codec_id = CODEC_ID_ADPCM_YAMAHA;
 
251
    st->codec->sample_rate = rate;
 
252
    st->codec->channels = 1;
 
253
    st->codec->bits_per_sample = 4;
 
254
    st->codec->bit_rate = st->codec->sample_rate * st->codec->bits_per_sample;
 
255
 
 
256
    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
 
257
 
 
258
    return 0;
 
259
}
 
260
 
 
261
#define MAX_SIZE 4096
 
262
 
 
263
static int mmf_read_packet(AVFormatContext *s,
 
264
                           AVPacket *pkt)
 
265
{
 
266
    MMFContext *mmf = s->priv_data;
 
267
    AVStream *st;
 
268
    int ret, size;
 
269
 
 
270
    if (url_feof(&s->pb))
 
271
        return AVERROR_IO;
 
272
    st = s->streams[0];
 
273
 
 
274
    size = MAX_SIZE;
 
275
    if(size > mmf->data_size)
 
276
        size = mmf->data_size;
 
277
 
 
278
    if(!size)
 
279
        return AVERROR_IO;
 
280
 
 
281
    if (av_new_packet(pkt, size))
 
282
        return AVERROR_IO;
 
283
    pkt->stream_index = 0;
 
284
 
 
285
    ret = get_buffer(&s->pb, pkt->data, pkt->size);
 
286
    if (ret < 0)
 
287
        av_free_packet(pkt);
 
288
 
 
289
    mmf->data_size -= ret;
 
290
 
 
291
    pkt->size = ret;
 
292
    return ret;
 
293
}
 
294
 
 
295
static int mmf_read_close(AVFormatContext *s)
 
296
{
 
297
    return 0;
 
298
}
 
299
 
 
300
static int mmf_read_seek(AVFormatContext *s,
 
301
                         int stream_index, int64_t timestamp, int flags)
 
302
{
 
303
    return pcm_read_seek(s, stream_index, timestamp, flags);
 
304
}
 
305
 
 
306
#ifdef CONFIG_MMF_DEMUXER
 
307
AVInputFormat mmf_demuxer = {
 
308
    "mmf",
 
309
    "mmf format",
 
310
    sizeof(MMFContext),
 
311
    mmf_probe,
 
312
    mmf_read_header,
 
313
    mmf_read_packet,
 
314
    mmf_read_close,
 
315
    mmf_read_seek,
 
316
};
 
317
#endif
 
318
#ifdef CONFIG_MMF_MUXER
 
319
AVOutputFormat mmf_muxer = {
 
320
    "mmf",
 
321
    "mmf format",
 
322
    "application/vnd.smaf",
 
323
    "mmf",
 
324
    sizeof(MMFContext),
 
325
    CODEC_ID_ADPCM_YAMAHA,
 
326
    CODEC_ID_NONE,
 
327
    mmf_write_header,
 
328
    mmf_write_packet,
 
329
    mmf_write_trailer,
 
330
};
 
331
#endif