Statistics
| Branch: | Revision:

ffmpeg / libavcodec / libvorbis.c @ b6675279

History | View | Annotate | Download (8.56 KB)

1 04d7f601 Diego Biurrun
/*
2
 * copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3
 *
4 2912e87a Mans Rullgard
 * This file is part of Libav.
5 b78e7197 Diego Biurrun
 *
6 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
7 04d7f601 Diego Biurrun
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 04d7f601 Diego Biurrun
 *
11 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
12 04d7f601 Diego Biurrun
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
18 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 04d7f601 Diego Biurrun
 */
20
21 983e3246 Michael Niedermayer
/**
22 ba87f080 Diego Biurrun
 * @file
23 983e3246 Michael Niedermayer
 * Ogg Vorbis codec support via libvorbisenc.
24
 * @author Mark Hills <mark@pogo.org.uk>
25 81e0d0b4 Mark Hills
 */
26
27
#include <vorbis/vorbisenc.h>
28
29 77336a5e Michael Niedermayer
#include "libavutil/opt.h"
30 81e0d0b4 Mark Hills
#include "avcodec.h"
31 2c124cb6 Ramiro Polla
#include "bytestream.h"
32 9577838f James Darnley
#include "vorbis.h"
33 81e0d0b4 Mark Hills
34 3f4993f1 Michael Niedermayer
#undef NDEBUG
35
#include <assert.h>
36
37 6d8f985e Michael Niedermayer
#define OGGVORBIS_FRAME_SIZE 64
38 81e0d0b4 Mark Hills
39 6d8f985e Michael Niedermayer
#define BUFFER_SIZE (1024*64)
40 81e0d0b4 Mark Hills
41
typedef struct OggVorbisContext {
42 77336a5e Michael Niedermayer
    AVClass *av_class;
43 81e0d0b4 Mark Hills
    vorbis_info vi ;
44
    vorbis_dsp_state vd ;
45
    vorbis_block vb ;
46 6d8f985e Michael Niedermayer
    uint8_t buffer[BUFFER_SIZE];
47
    int buffer_index;
48 9c8f0768 Nicolas George
    int eof;
49 3aca208a Mark Hills
50
    /* decoder */
51
    vorbis_comment vc ;
52 d76f581f Michael Niedermayer
    ogg_packet op;
53 77336a5e Michael Niedermayer
54
    double iblock;
55 81e0d0b4 Mark Hills
} OggVorbisContext ;
56
57 77336a5e Michael Niedermayer
static const AVOption options[]={
58 b6675279 Anton Khirnov
{"iblock", "Sets the impulse block bias", offsetof(OggVorbisContext, iblock), FF_OPT_TYPE_DOUBLE, {.dbl = 0}, -15, 0, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM},
59 77336a5e Michael Niedermayer
{NULL}
60
};
61 da754858 James Zern
static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
62 81e0d0b4 Mark Hills
63 5ef251e5 Daniel Verkamp
static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
64 77336a5e Michael Niedermayer
    OggVorbisContext *context = avccontext->priv_data ;
65 13c71451 Justin Ruggles
    double cfreq;
66 c55427f8 Mark Hills
67 1ff8f2b3 Justin Ruggles
    if(avccontext->flags & CODEC_FLAG_QSCALE) {
68 13c71451 Justin Ruggles
        /* variable bitrate */
69
        if(vorbis_encode_setup_vbr(vi, avccontext->channels,
70 1ff8f2b3 Justin Ruggles
                avccontext->sample_rate,
71 05276956 Nicolas George
                avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0))
72 13c71451 Justin Ruggles
            return -1;
73
    } else {
74 e5a5ea9e David Conrad
        int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
75
        int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
76
77 13c71451 Justin Ruggles
        /* constant bitrate */
78
        if(vorbis_encode_setup_managed(vi, avccontext->channels,
79 e5a5ea9e David Conrad
                avccontext->sample_rate, minrate, avccontext->bit_rate, maxrate))
80 13c71451 Justin Ruggles
            return -1;
81
82 57ebbccf David Conrad
        /* variable bitrate by estimate, disable slow rate management */
83
        if(minrate == -1 && maxrate == -1)
84
            if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
85
                return -1;
86 13c71451 Justin Ruggles
    }
87 c55427f8 Mark Hills
88 13c71451 Justin Ruggles
    /* cutoff frequency */
89
    if(avccontext->cutoff > 0) {
90
        cfreq = avccontext->cutoff / 1000.0;
91
        if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
92
            return -1;
93
    }
94 81e0d0b4 Mark Hills
95 77336a5e Michael Niedermayer
    if(context->iblock){
96
        vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
97
    }
98
99 13c71451 Justin Ruggles
    return vorbis_encode_setup_init(vi);
100 81e0d0b4 Mark Hills
}
101
102 fd7242dd Pascal Massimino
/* How many bytes are needed for a buffer of length 'l' */
103
static int xiph_len(int l) { return (1 + l / 255 + l); }
104
105 98a6fff9 Zuxy Meng
static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext) {
106 81e0d0b4 Mark Hills
    OggVorbisContext *context = avccontext->priv_data ;
107 bbb77e7c Michael Niedermayer
    ogg_packet header, header_comm, header_code;
108
    uint8_t *p;
109 fd7242dd Pascal Massimino
    unsigned int offset;
110 81e0d0b4 Mark Hills
111
    vorbis_info_init(&context->vi) ;
112
    if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
113 fb0a835f Baptiste Coudurier
        av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n") ;
114 bb270c08 Diego Biurrun
        return -1 ;
115 81e0d0b4 Mark Hills
    }
116
    vorbis_analysis_init(&context->vd, &context->vi) ;
117
    vorbis_block_init(&context->vd, &context->vb) ;
118
119 bbb77e7c Michael Niedermayer
    vorbis_comment_init(&context->vc);
120
    vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
121
122
    vorbis_analysis_headerout(&context->vd, &context->vc, &header,
123
                                &header_comm, &header_code);
124 115329f1 Diego Biurrun
125 fd7242dd Pascal Massimino
    avccontext->extradata_size=
126
        1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
127
        header_code.bytes;
128
    p = avccontext->extradata =
129
      av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
130 ad2b531d Måns Rullgård
    p[0] = 2;
131
    offset = 1;
132
    offset += av_xiphlacing(&p[offset], header.bytes);
133
    offset += av_xiphlacing(&p[offset], header_comm.bytes);
134
    memcpy(&p[offset], header.packet, header.bytes);
135
    offset += header.bytes;
136
    memcpy(&p[offset], header_comm.packet, header_comm.bytes);
137
    offset += header_comm.bytes;
138
    memcpy(&p[offset], header_code.packet, header_code.bytes);
139
    offset += header_code.bytes;
140 fd7242dd Pascal Massimino
    assert(offset == avccontext->extradata_size);
141 115329f1 Diego Biurrun
142 bbb77e7c Michael Niedermayer
/*    vorbis_block_clear(&context->vb);
143
    vorbis_dsp_clear(&context->vd);
144
    vorbis_info_clear(&context->vi);*/
145
    vorbis_comment_clear(&context->vc);
146 115329f1 Diego Biurrun
147 81e0d0b4 Mark Hills
    avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
148 115329f1 Diego Biurrun
149 492cd3a9 Michael Niedermayer
    avccontext->coded_frame= avcodec_alloc_frame();
150
    avccontext->coded_frame->key_frame= 1;
151 115329f1 Diego Biurrun
152 81e0d0b4 Mark Hills
    return 0 ;
153
}
154
155
156 3aca208a Mark Hills
static int oggvorbis_encode_frame(AVCodecContext *avccontext,
157 bb270c08 Diego Biurrun
                                  unsigned char *packets,
158
                           int buf_size, void *data)
159 81e0d0b4 Mark Hills
{
160
    OggVorbisContext *context = avccontext->priv_data ;
161
    ogg_packet op ;
162 0d00cf38 Sigbjørn Skjæret
    signed short *audio = data ;
163 9c8f0768 Nicolas George
    int l;
164
165
    if(data) {
166 42859ddb Pascal Massimino
        const int samples = avccontext->frame_size;
167 9c8f0768 Nicolas George
        float **buffer ;
168 9577838f James Darnley
        int c, channels = context->vi.channels;
169 81e0d0b4 Mark Hills
170 a6080be7 Benoit Fouet
        buffer = vorbis_analysis_buffer(&context->vd, samples) ;
171 9577838f James Darnley
        for (c = 0; c < channels; c++) {
172
            int co = (channels > 8) ? c :
173
                ff_vorbis_encoding_channel_layout_offsets[channels-1][c];
174 a6080be7 Benoit Fouet
            for(l = 0 ; l < samples ; l++)
175 9577838f James Darnley
                buffer[c][l]=audio[l*channels+co]/32768.f;
176 bb270c08 Diego Biurrun
        }
177 a6080be7 Benoit Fouet
        vorbis_analysis_wrote(&context->vd, samples) ;
178 9c8f0768 Nicolas George
    } else {
179
        if(!context->eof)
180
            vorbis_analysis_wrote(&context->vd, 0) ;
181
        context->eof = 1;
182
    }
183 81e0d0b4 Mark Hills
184
    while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
185 bb270c08 Diego Biurrun
        vorbis_analysis(&context->vb, NULL);
186
        vorbis_bitrate_addblock(&context->vb) ;
187 81e0d0b4 Mark Hills
188 bb270c08 Diego Biurrun
        while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
189 0afd2a92 Diego Biurrun
            /* i'd love to say the following line is a hack, but sadly it's
190
             * not, apparently the end of stream decision is in libogg. */
191 d99659f0 Martin Storsjö
            if(op.bytes==1 && op.e_o_s)
192 6f824977 Michael Niedermayer
                continue;
193 c426562c Pascal Massimino
            if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
194
                av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
195
                return -1;
196
            }
197 6d8f985e Michael Niedermayer
            memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
198
            context->buffer_index += sizeof(ogg_packet);
199
            memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
200
            context->buffer_index += op.bytes;
201
//            av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
202 bb270c08 Diego Biurrun
        }
203 81e0d0b4 Mark Hills
    }
204
205 3f4993f1 Michael Niedermayer
    l=0;
206 6d8f985e Michael Niedermayer
    if(context->buffer_index){
207 bbb77e7c Michael Niedermayer
        ogg_packet *op2= (ogg_packet*)context->buffer;
208 6d8f985e Michael Niedermayer
        op2->packet = context->buffer + sizeof(ogg_packet);
209 3f4993f1 Michael Niedermayer
210 6f824977 Michael Niedermayer
        l=  op2->bytes;
211 42661adf Michael Niedermayer
        avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
212 3723f01c Michael Niedermayer
        //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
213 3f4993f1 Michael Niedermayer
214 c426562c Pascal Massimino
        if (l > buf_size) {
215
            av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
216
            return -1;
217
        }
218
219 6f824977 Michael Niedermayer
        memcpy(packets, op2->packet, l);
220
        context->buffer_index -= l + sizeof(ogg_packet);
221 1204a13c Martin Storsjö
        memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
222 6d8f985e Michael Niedermayer
//        av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
223
    }
224
225 3f4993f1 Michael Niedermayer
    return l;
226 81e0d0b4 Mark Hills
}
227
228
229 98a6fff9 Zuxy Meng
static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext) {
230 81e0d0b4 Mark Hills
    OggVorbisContext *context = avccontext->priv_data ;
231
/*  ogg_packet op ; */
232 115329f1 Diego Biurrun
233 81e0d0b4 Mark Hills
    vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
234
235
    vorbis_block_clear(&context->vb);
236
    vorbis_dsp_clear(&context->vd);
237
    vorbis_info_clear(&context->vi);
238 492cd3a9 Michael Niedermayer
239
    av_freep(&avccontext->coded_frame);
240 bbb77e7c Michael Niedermayer
    av_freep(&avccontext->extradata);
241 115329f1 Diego Biurrun
242 81e0d0b4 Mark Hills
    return 0 ;
243
}
244
245
246 d36beb3f Diego Elio Pettenò
AVCodec ff_libvorbis_encoder = {
247 d0d866e0 Aurelien Jacobs
    "libvorbis",
248 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_AUDIO,
249 81e0d0b4 Mark Hills
    CODEC_ID_VORBIS,
250
    sizeof(OggVorbisContext),
251
    oggvorbis_encode_init,
252
    oggvorbis_encode_frame,
253 6f824977 Michael Niedermayer
    oggvorbis_encode_close,
254
    .capabilities= CODEC_CAP_DELAY,
255 5d6e4c16 Stefano Sabatini
    .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
256 fe4bf374 Stefano Sabatini
    .long_name= NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
257 77336a5e Michael Niedermayer
    .priv_class= &class,
258 3aca208a Mark Hills
} ;