Git init
[framework/multimedia/ffmpeg.git] / libavcodec / libvorbis.c
1 /*
2  * copyright (c) 2002 Mark Hills <mark@pogo.org.uk>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * 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  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Ogg Vorbis codec support via libvorbisenc.
24  * @author Mark Hills <mark@pogo.org.uk>
25  */
26
27 #include <vorbis/vorbisenc.h>
28
29 #include "libavutil/opt.h"
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "vorbis.h"
33 #include "libavutil/mathematics.h"
34
35 #undef NDEBUG
36 #include <assert.h>
37
38 #define OGGVORBIS_FRAME_SIZE 64
39
40 #define BUFFER_SIZE (1024*64)
41
42 typedef struct OggVorbisContext {
43     AVClass *av_class;
44     vorbis_info vi ;
45     vorbis_dsp_state vd ;
46     vorbis_block vb ;
47     uint8_t buffer[BUFFER_SIZE];
48     int buffer_index;
49     int eof;
50
51     /* decoder */
52     vorbis_comment vc ;
53     ogg_packet op;
54
55     double iblock;
56 } OggVorbisContext ;
57
58 static const AVOption options[]={
59 {"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},
60 {NULL}
61 };
62 static const AVClass class = { "libvorbis", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
63
64 static av_cold int oggvorbis_init_encoder(vorbis_info *vi, AVCodecContext *avccontext) {
65     OggVorbisContext *context = avccontext->priv_data ;
66     double cfreq;
67
68     if(avccontext->flags & CODEC_FLAG_QSCALE) {
69         /* variable bitrate */
70         if(vorbis_encode_setup_vbr(vi, avccontext->channels,
71                 avccontext->sample_rate,
72                 avccontext->global_quality / (float)FF_QP2LAMBDA / 10.0))
73             return -1;
74     } else {
75         int minrate = avccontext->rc_min_rate > 0 ? avccontext->rc_min_rate : -1;
76         int maxrate = avccontext->rc_min_rate > 0 ? avccontext->rc_max_rate : -1;
77
78         /* constant bitrate */
79         if(vorbis_encode_setup_managed(vi, avccontext->channels,
80                 avccontext->sample_rate, minrate, avccontext->bit_rate, maxrate))
81             return -1;
82
83         /* variable bitrate by estimate, disable slow rate management */
84         if(minrate == -1 && maxrate == -1)
85             if(vorbis_encode_ctl(vi, OV_ECTL_RATEMANAGE2_SET, NULL))
86                 return -1;
87     }
88
89     /* cutoff frequency */
90     if(avccontext->cutoff > 0) {
91         cfreq = avccontext->cutoff / 1000.0;
92         if(vorbis_encode_ctl(vi, OV_ECTL_LOWPASS_SET, &cfreq))
93             return -1;
94     }
95
96     if(context->iblock){
97         vorbis_encode_ctl(vi, OV_ECTL_IBLOCK_SET, &context->iblock);
98     }
99
100     if (avccontext->channels == 3 &&
101             avccontext->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
102         avccontext->channels == 4 &&
103             avccontext->channel_layout != AV_CH_LAYOUT_2_2 &&
104             avccontext->channel_layout != AV_CH_LAYOUT_QUAD ||
105         avccontext->channels == 5 &&
106             avccontext->channel_layout != AV_CH_LAYOUT_5POINT0 &&
107             avccontext->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
108         avccontext->channels == 6 &&
109             avccontext->channel_layout != AV_CH_LAYOUT_5POINT1 &&
110             avccontext->channel_layout != AV_CH_LAYOUT_5POINT1_BACK ||
111         avccontext->channels == 7 &&
112             avccontext->channel_layout != (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER) ||
113         avccontext->channels == 8 &&
114             avccontext->channel_layout != AV_CH_LAYOUT_7POINT1) {
115         if (avccontext->channel_layout) {
116             char name[32];
117             av_get_channel_layout_string(name, sizeof(name), avccontext->channels,
118                                          avccontext->channel_layout);
119             av_log(avccontext, AV_LOG_ERROR, "%s not supported by Vorbis: "
120                                              "output stream will have incorrect "
121                                              "channel layout.\n", name);
122         } else {
123             av_log(avccontext, AV_LOG_WARNING, "No channel layout specified. The encoder "
124                                                "will use Vorbis channel layout for "
125                                                "%d channels.\n", avccontext->channels);
126         }
127     }
128
129     return vorbis_encode_setup_init(vi);
130 }
131
132 /* How many bytes are needed for a buffer of length 'l' */
133 static int xiph_len(int l) { return (1 + l / 255 + l); }
134
135 static av_cold int oggvorbis_encode_init(AVCodecContext *avccontext) {
136     OggVorbisContext *context = avccontext->priv_data ;
137     ogg_packet header, header_comm, header_code;
138     uint8_t *p;
139     unsigned int offset;
140
141     vorbis_info_init(&context->vi) ;
142     if(oggvorbis_init_encoder(&context->vi, avccontext) < 0) {
143         av_log(avccontext, AV_LOG_ERROR, "oggvorbis_encode_init: init_encoder failed\n") ;
144         return -1 ;
145     }
146     vorbis_analysis_init(&context->vd, &context->vi) ;
147     vorbis_block_init(&context->vd, &context->vb) ;
148
149     vorbis_comment_init(&context->vc);
150     vorbis_comment_add_tag(&context->vc, "encoder", LIBAVCODEC_IDENT) ;
151
152     vorbis_analysis_headerout(&context->vd, &context->vc, &header,
153                                 &header_comm, &header_code);
154
155     avccontext->extradata_size=
156         1 + xiph_len(header.bytes) + xiph_len(header_comm.bytes) +
157         header_code.bytes;
158     p = avccontext->extradata =
159       av_malloc(avccontext->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
160     p[0] = 2;
161     offset = 1;
162     offset += av_xiphlacing(&p[offset], header.bytes);
163     offset += av_xiphlacing(&p[offset], header_comm.bytes);
164     memcpy(&p[offset], header.packet, header.bytes);
165     offset += header.bytes;
166     memcpy(&p[offset], header_comm.packet, header_comm.bytes);
167     offset += header_comm.bytes;
168     memcpy(&p[offset], header_code.packet, header_code.bytes);
169     offset += header_code.bytes;
170     assert(offset == avccontext->extradata_size);
171
172 /*    vorbis_block_clear(&context->vb);
173     vorbis_dsp_clear(&context->vd);
174     vorbis_info_clear(&context->vi);*/
175     vorbis_comment_clear(&context->vc);
176
177     avccontext->frame_size = OGGVORBIS_FRAME_SIZE ;
178
179     avccontext->coded_frame= avcodec_alloc_frame();
180     avccontext->coded_frame->key_frame= 1;
181
182     return 0 ;
183 }
184
185
186 static int oggvorbis_encode_frame(AVCodecContext *avccontext,
187                                   unsigned char *packets,
188                            int buf_size, void *data)
189 {
190     OggVorbisContext *context = avccontext->priv_data ;
191     ogg_packet op ;
192     signed short *audio = data ;
193     int l;
194
195     if(data) {
196         const int samples = avccontext->frame_size;
197         float **buffer ;
198         int c, channels = context->vi.channels;
199
200         buffer = vorbis_analysis_buffer(&context->vd, samples) ;
201         for (c = 0; c < channels; c++) {
202             int co = (channels > 8) ? c :
203                 ff_vorbis_encoding_channel_layout_offsets[channels-1][c];
204             for(l = 0 ; l < samples ; l++)
205                 buffer[c][l]=audio[l*channels+co]/32768.f;
206         }
207         vorbis_analysis_wrote(&context->vd, samples) ;
208     } else {
209         if(!context->eof)
210             vorbis_analysis_wrote(&context->vd, 0) ;
211         context->eof = 1;
212     }
213
214     while(vorbis_analysis_blockout(&context->vd, &context->vb) == 1) {
215         vorbis_analysis(&context->vb, NULL);
216         vorbis_bitrate_addblock(&context->vb) ;
217
218         while(vorbis_bitrate_flushpacket(&context->vd, &op)) {
219             /* i'd love to say the following line is a hack, but sadly it's
220              * not, apparently the end of stream decision is in libogg. */
221             if(op.bytes==1 && op.e_o_s)
222                 continue;
223             if (context->buffer_index + sizeof(ogg_packet) + op.bytes > BUFFER_SIZE) {
224                 av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
225                 return -1;
226             }
227             memcpy(context->buffer + context->buffer_index, &op, sizeof(ogg_packet));
228             context->buffer_index += sizeof(ogg_packet);
229             memcpy(context->buffer + context->buffer_index, op.packet, op.bytes);
230             context->buffer_index += op.bytes;
231 //            av_log(avccontext, AV_LOG_DEBUG, "e%d / %d\n", context->buffer_index, op.bytes);
232         }
233     }
234
235     l=0;
236     if(context->buffer_index){
237         ogg_packet *op2= (ogg_packet*)context->buffer;
238         op2->packet = context->buffer + sizeof(ogg_packet);
239
240         l=  op2->bytes;
241         avccontext->coded_frame->pts= av_rescale_q(op2->granulepos, (AVRational){1, avccontext->sample_rate}, avccontext->time_base);
242         //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
243
244         if (l > buf_size) {
245             av_log(avccontext, AV_LOG_ERROR, "libvorbis: buffer overflow.");
246             return -1;
247         }
248
249         memcpy(packets, op2->packet, l);
250         context->buffer_index -= l + sizeof(ogg_packet);
251         memmove(context->buffer, context->buffer + l + sizeof(ogg_packet), context->buffer_index);
252 //        av_log(avccontext, AV_LOG_DEBUG, "E%d\n", l);
253     }
254
255     return l;
256 }
257
258
259 static av_cold int oggvorbis_encode_close(AVCodecContext *avccontext) {
260     OggVorbisContext *context = avccontext->priv_data ;
261 /*  ogg_packet op ; */
262
263     vorbis_analysis_wrote(&context->vd, 0) ; /* notify vorbisenc this is EOF */
264
265     vorbis_block_clear(&context->vb);
266     vorbis_dsp_clear(&context->vd);
267     vorbis_info_clear(&context->vi);
268
269     av_freep(&avccontext->coded_frame);
270     av_freep(&avccontext->extradata);
271
272     return 0 ;
273 }
274
275
276 AVCodec ff_libvorbis_encoder = {
277     "libvorbis",
278     AVMEDIA_TYPE_AUDIO,
279     CODEC_ID_VORBIS,
280     sizeof(OggVorbisContext),
281     oggvorbis_encode_init,
282     oggvorbis_encode_frame,
283     oggvorbis_encode_close,
284     .capabilities= CODEC_CAP_DELAY,
285     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
286     .long_name= NULL_IF_CONFIG_SMALL("libvorbis Vorbis"),
287     .priv_class= &class,
288 } ;