]> git.sesse.net Git - vlc/blob - modules/codec/opus.c
mft: Link to mfplat when building with msvc
[vlc] / modules / codec / opus.c
1 /*****************************************************************************
2  * opus.c: opus decoder/encoder module making use of libopus.
3  *****************************************************************************
4  * Copyright (C) 2003-2009, 2012 VLC authors and VideoLAN
5  *
6  * Authors: Gregory Maxwell <greg@xiph.org>
7  * Based on speex.c by: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
35 #include <vlc_aout.h>
36 #include "../demux/xiph.h"
37
38 #include <ogg/ogg.h>
39 #include <opus.h>
40 #include <opus_multistream.h>
41
42 #include "opus_header.h"
43
44 #ifndef OPUS_SET_GAIN
45 #include <math.h>
46 #endif
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  OpenDecoder   ( vlc_object_t * );
52 static void CloseDecoder  ( vlc_object_t * );
53 #ifdef ENABLE_SOUT
54 static int  OpenEncoder   ( vlc_object_t * );
55 static void CloseEncoder  ( vlc_object_t * );
56 #endif
57
58 vlc_module_begin ()
59     set_category( CAT_INPUT )
60     set_subcategory( SUBCAT_INPUT_ACODEC )
61
62     set_description( N_("Opus audio decoder") )
63     set_capability( "decoder", 100 )
64     set_shortname( N_("Opus") )
65     set_callbacks( OpenDecoder, CloseDecoder )
66
67 #ifdef ENABLE_SOUT
68     add_submodule ()
69     set_description( N_("Opus audio encoder") )
70     set_capability( "encoder", 150 )
71     set_shortname( N_("Opus") )
72     set_callbacks( OpenEncoder, CloseEncoder )
73 #endif
74
75 vlc_module_end ()
76
77 /*****************************************************************************
78  * decoder_sys_t : opus decoder descriptor
79  *****************************************************************************/
80 struct decoder_sys_t
81 {
82     /*
83      * Input properties
84      */
85     bool b_has_headers;
86
87     /*
88      * Opus properties
89      */
90     OpusHeader header;
91     OpusMSDecoder *p_st;
92
93     /*
94      * Common properties
95      */
96     date_t end_date;
97 };
98
99 static const int pi_channels_maps[9] =
100 {
101     0,
102     AOUT_CHAN_CENTER,
103     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
104     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
105     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
106      | AOUT_CHAN_REARRIGHT,
107     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
108      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
109     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
110      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
111     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
112      | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
113      | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
114     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
115      | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
116      | AOUT_CHAN_LFE,
117 };
118
119 /*
120 **  channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
121 */
122
123 /* recommended vorbis channel order for 8 channels */
124 static const uint32_t pi_8channels_in[] =
125 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
126   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
127   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE, 0 };
128
129 /* recommended vorbis channel order for 7 channels */
130 static const uint32_t pi_7channels_in[] =
131 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
132   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
133   AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE, 0 };
134
135 /* recommended vorbis channel order for 6 channels */
136 static const uint32_t pi_6channels_in[] =
137 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
138   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
139
140 /* recommended vorbis channel order for 5 channels */
141 static const uint32_t pi_5channels_in[] =
142 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
143   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
144
145 /* recommended vorbis channel order for 4 channels */
146 static const uint32_t pi_4channels_in[] =
147 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
148
149 /* recommended vorbis channel order for 3 channels */
150 static const uint32_t pi_3channels_in[] =
151 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
152
153 /****************************************************************************
154  * Local prototypes
155  ****************************************************************************/
156
157 static block_t *DecodeBlock  ( decoder_t *, block_t ** );
158 static int  ProcessHeaders( decoder_t * );
159 static int  ProcessInitialHeader ( decoder_t *, ogg_packet * );
160 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
161
162 static block_t *DecodePacket( decoder_t *, ogg_packet *, int, int );
163
164 /*****************************************************************************
165  * OpenDecoder: probe the decoder and return score
166  *****************************************************************************/
167 static int OpenDecoder( vlc_object_t *p_this )
168 {
169     decoder_t *p_dec = (decoder_t*)p_this;
170     decoder_sys_t *p_sys;
171
172     if( p_dec->fmt_in.i_codec != VLC_CODEC_OPUS )
173         return VLC_EGENERIC;
174
175     /* Allocate the memory needed to store the decoder's structure */
176     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
177         return VLC_ENOMEM;
178     p_dec->p_sys->b_has_headers = false;
179
180     date_Set( &p_sys->end_date, 0 );
181
182     /* Set output properties */
183     p_dec->fmt_out.i_cat = AUDIO_ES;
184     p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
185
186     p_dec->pf_decode_audio = DecodeBlock;
187     p_dec->pf_packetize    = DecodeBlock;
188
189     p_sys->p_st = NULL;
190
191     return VLC_SUCCESS;
192 }
193
194 /****************************************************************************
195  * DecodeBlock: the whole thing
196  ****************************************************************************
197  * This function must be fed with ogg packets.
198  ****************************************************************************/
199 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
200 {
201     decoder_sys_t *p_sys = p_dec->p_sys;
202     ogg_packet oggpacket;
203
204     if( !pp_block || !*pp_block)
205         return NULL;
206
207     /* Block to Ogg packet */
208     oggpacket.packet = (*pp_block)->p_buffer;
209     oggpacket.bytes = (*pp_block)->i_buffer;
210
211     oggpacket.granulepos = -1;
212     oggpacket.b_o_s = 0;
213     oggpacket.e_o_s = 0;
214     oggpacket.packetno = 0;
215
216     /* Check for headers */
217     if( !p_sys->b_has_headers )
218     {
219         if( ProcessHeaders( p_dec ) )
220         {
221             block_Release( *pp_block );
222             return NULL;
223         }
224         p_sys->b_has_headers = true;
225     }
226
227     return ProcessPacket( p_dec, &oggpacket, pp_block );
228 }
229
230 /*****************************************************************************
231  * ProcessHeaders: process Opus headers.
232  *****************************************************************************/
233 static int ProcessHeaders( decoder_t *p_dec )
234 {
235     ogg_packet oggpacket;
236
237     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
238     void     *pp_data[XIPH_MAX_HEADER_COUNT];
239     unsigned i_count;
240
241     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
242                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
243         return VLC_EGENERIC;
244     if( i_count < 2 )
245         return VLC_EGENERIC;;
246
247     oggpacket.granulepos = -1;
248     oggpacket.e_o_s = 0;
249     oggpacket.packetno = 0;
250
251     /* Take care of the initial Opus header */
252     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
253     oggpacket.bytes  = pi_size[0];
254     oggpacket.packet = pp_data[0];
255     int ret = ProcessInitialHeader( p_dec, &oggpacket );
256
257     if (ret != VLC_SUCCESS)
258         msg_Err( p_dec, "initial Opus header is corrupted" );
259
260     return ret;
261 }
262
263 /*****************************************************************************
264  * ProcessInitialHeader: processes the inital Opus header packet.
265  *****************************************************************************/
266 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
267 {
268     int err;
269     unsigned char new_stream_map[8];
270     decoder_sys_t *p_sys = p_dec->p_sys;
271
272     OpusHeader *p_header = &p_sys->header;
273
274     if( !opus_header_parse((unsigned char *)p_oggpacket->packet,p_oggpacket->bytes,p_header) )
275     {
276         msg_Err( p_dec, "cannot read Opus header" );
277         return VLC_EGENERIC;
278     }
279     msg_Dbg( p_dec, "Opus audio with %d channels", p_header->channels);
280
281     if((p_header->channels>2 && p_header->channel_mapping==0) ||
282         p_header->channels>8 ||
283         p_header->channel_mapping>1)
284     {
285         msg_Err( p_dec, "Unsupported channel mapping" );
286         return VLC_EGENERIC;
287     }
288
289     /* Setup the format */
290     p_dec->fmt_out.audio.i_physical_channels =
291         p_dec->fmt_out.audio.i_original_channels =
292             pi_channels_maps[p_header->channels];
293     p_dec->fmt_out.audio.i_channels = p_header->channels;
294     p_dec->fmt_out.audio.i_rate = 48000;
295
296     if( p_header->channels>2 )
297     {
298         static const uint32_t *pi_ch[6] = { pi_3channels_in, pi_4channels_in,
299                                             pi_5channels_in, pi_6channels_in,
300                                             pi_7channels_in, pi_8channels_in };
301         uint8_t pi_chan_table[AOUT_CHAN_MAX];
302
303         aout_CheckChannelReorder( pi_ch[p_header->channels-3], NULL,
304                                   p_dec->fmt_out.audio.i_physical_channels,
305                                   pi_chan_table );
306         for(int i=0;i<p_header->channels;i++)
307             new_stream_map[pi_chan_table[i]]=p_header->stream_map[i];
308     }
309     /* Opus decoder init */
310     p_sys->p_st = opus_multistream_decoder_create( 48000, p_header->channels,
311                     p_header->nb_streams, p_header->nb_coupled,
312                     p_header->channels>2?new_stream_map:p_header->stream_map,
313                     &err );
314     if( !p_sys->p_st || err!=OPUS_OK )
315     {
316         msg_Err( p_dec, "decoder initialization failed" );
317         return VLC_EGENERIC;
318     }
319
320 #ifdef OPUS_SET_GAIN
321     if( opus_multistream_decoder_ctl( p_sys->p_st,OPUS_SET_GAIN(p_header->gain) ) != OPUS_OK )
322     {
323         msg_Err( p_dec, "OPUS_SET_GAIN failed" );
324         opus_multistream_decoder_destroy( p_sys->p_st );
325         return VLC_EGENERIC;
326     }
327 #endif
328
329     date_Init( &p_sys->end_date, 48000, 1 );
330
331     return VLC_SUCCESS;
332 }
333
334 /*****************************************************************************
335  * ProcessPacket: processes a Opus packet.
336  *****************************************************************************/
337 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
338                             block_t **pp_block )
339 {
340     decoder_sys_t *p_sys = p_dec->p_sys;
341     block_t *p_block = *pp_block;
342
343     /* Date management */
344     if( p_block && p_block->i_pts > VLC_TS_INVALID &&
345         p_block->i_pts != date_Get( &p_sys->end_date ) )
346     {
347         date_Set( &p_sys->end_date, p_block->i_pts );
348     }
349
350     if( !date_Get( &p_sys->end_date ) )
351     {
352         /* We've just started the stream, wait for the first PTS. */
353         if( p_block ) block_Release( p_block );
354         return NULL;
355     }
356
357     *pp_block = NULL; /* To avoid being fed the same packet again */
358
359     if( !p_block )
360         return NULL;
361
362     block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket,
363                                            p_block->i_nb_samples,
364                                            (int)p_block->i_length );
365
366     block_Release( p_block );
367     return p_aout_buffer;
368 }
369
370 /*****************************************************************************
371  * DecodePacket: decodes a Opus packet.
372  *****************************************************************************/
373 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
374                               int i_nb_samples, int i_end_trim )
375 {
376     decoder_sys_t *p_sys = p_dec->p_sys;
377
378     if( !p_oggpacket->bytes )
379         return NULL;
380
381     int spp;
382     spp=opus_packet_get_nb_frames(p_oggpacket->packet,p_oggpacket->bytes);
383     if(spp>0)spp*=opus_packet_get_samples_per_frame(p_oggpacket->packet,48000);
384     if(spp<120||spp>120*48)return NULL;
385
386     /* Since the information isn't always available at the demux level
387      * use the packet's sample number */
388     if(!i_nb_samples)
389         i_nb_samples = spp;
390
391     block_t *p_aout_buffer=decoder_NewAudioBuffer( p_dec, spp );
392     if ( !p_aout_buffer )
393     {
394         msg_Err(p_dec, "Oops: No new buffer was returned!");
395         return NULL;
396     }
397
398     spp=opus_multistream_decode_float(p_sys->p_st, p_oggpacket->packet,
399          p_oggpacket->bytes, (float *)p_aout_buffer->p_buffer, spp, 0);
400     if( spp < 0 || i_nb_samples <= 0 || i_end_trim >= i_nb_samples)
401     {
402         block_Release(p_aout_buffer);
403         if( spp < 0 )
404             msg_Err( p_dec, "Error: corrupted stream?" );
405         return NULL;
406     }
407
408     p_aout_buffer->i_buffer = (i_nb_samples - i_end_trim) *
409                               p_sys->header.channels * sizeof(float);
410
411     if( spp > i_nb_samples )
412     {
413         memmove(p_aout_buffer->p_buffer,
414             p_aout_buffer->p_buffer
415             + (spp - i_nb_samples)*p_sys->header.channels*sizeof(float),
416             p_aout_buffer->i_buffer);
417     }
418     i_nb_samples -= i_end_trim;
419
420 #ifndef OPUS_SET_GAIN
421     if(p_sys->header.gain!=0)
422     {
423         float gain = pow(10., p_sys->header.gain/5120.);
424         float *buf =(float *)p_aout_buffer->p_buffer;
425         int i;
426         for( i = 0; i < i_nb_samples*p_sys->header.channels; i++)
427             buf[i] *= gain;
428     }
429 #endif
430     p_aout_buffer->i_nb_samples = i_nb_samples;
431     p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
432     p_aout_buffer->i_length = date_Increment( &p_sys->end_date, i_nb_samples )
433         - p_aout_buffer->i_pts;
434     return p_aout_buffer;
435 }
436
437 /*****************************************************************************
438  * CloseDecoder: Opus decoder destruction
439  *****************************************************************************/
440 static void CloseDecoder( vlc_object_t *p_this )
441 {
442     decoder_t * p_dec = (decoder_t *)p_this;
443     decoder_sys_t *p_sys = p_dec->p_sys;
444
445     if( p_sys->p_st ) opus_multistream_decoder_destroy(p_sys->p_st);
446
447     free( p_sys );
448 }
449
450 #ifdef ENABLE_SOUT
451
452 /* only ever encode 20 ms at a time, going longer doesn't yield much compression
453    gain, shorter does have a compression loss, and doesn't matter so much in
454    Ogg, unless you really need low latency, which would also require muxing one
455    packet per page. */
456 static const unsigned OPUS_FRAME_SIZE = 960; /* 48000 * 20 / 1000 */
457
458 struct encoder_sys_t
459 {
460     OpusMSEncoder *enc;
461     float *buffer;
462     unsigned i_nb_samples;
463     int i_samples_delay;
464     block_t *padding;
465     int nb_streams;
466 };
467
468 static unsigned fill_buffer(encoder_t *enc, unsigned src_start, block_t *src,
469                             unsigned samples)
470 {
471     encoder_sys_t *p_sys = enc->p_sys;
472     const unsigned channels = enc->fmt_out.audio.i_channels;
473     const float *src_buf = ((const float *) src->p_buffer) + src_start;
474     float *dest_buf = p_sys->buffer + (p_sys->i_nb_samples * channels);
475     const unsigned len = samples * channels;
476
477     memcpy(dest_buf, src_buf, len * sizeof(float));
478
479     p_sys->i_nb_samples += samples;
480     src_start += len;
481
482     src->i_nb_samples -= samples;
483     return src_start;
484 }
485
486 static block_t *Encode(encoder_t *enc, block_t *buf)
487 {
488     encoder_sys_t *sys = enc->p_sys;
489
490     if (!buf)
491         return NULL;
492
493     mtime_t i_pts = buf->i_pts -
494                 (mtime_t) CLOCK_FREQ * (mtime_t) sys->i_samples_delay /
495                 (mtime_t) enc->fmt_in.audio.i_rate;
496
497     sys->i_samples_delay += buf->i_nb_samples;
498
499     block_t *result = NULL;
500     unsigned src_start = 0;
501     unsigned padding_start = 0;
502     /* The maximum Opus frame size is 1275 bytes + TOC sequence length. */
503     const unsigned OPUS_MAX_ENCODED_BYTES = ((1275 + 3) * sys->nb_streams) - 2;
504
505     while (sys->i_nb_samples + buf->i_nb_samples >= OPUS_FRAME_SIZE)
506     {
507         block_t *out_block = block_Alloc(OPUS_MAX_ENCODED_BYTES);
508
509         /* add padding to beginning */
510         if (sys->padding)
511         {
512             const size_t leftover_space = OPUS_FRAME_SIZE - sys->i_nb_samples;
513             padding_start = fill_buffer(enc, padding_start, sys->padding,
514                     __MIN(sys->padding->i_nb_samples, leftover_space));
515             if (sys->padding->i_nb_samples <= 0)
516             {
517                 block_Release(sys->padding);
518                 sys->padding = NULL;
519             }
520         }
521
522         /* padding may have been freed either before or inside previous
523          * if-statement */
524         if (!sys->padding)
525         {
526             const size_t leftover_space = OPUS_FRAME_SIZE - sys->i_nb_samples;
527             src_start = fill_buffer(enc, src_start, buf,
528                     __MIN(buf->i_nb_samples, leftover_space));
529         }
530
531         opus_int32 bytes_encoded = opus_multistream_encode_float(sys->enc, sys->buffer,
532                 OPUS_FRAME_SIZE, out_block->p_buffer, out_block->i_buffer);
533
534         if (bytes_encoded < 0)
535         {
536             block_Release(out_block);
537         }
538         else
539         {
540             out_block->i_length = (mtime_t) CLOCK_FREQ *
541                 (mtime_t) OPUS_FRAME_SIZE / (mtime_t) enc->fmt_in.audio.i_rate;
542
543             out_block->i_dts = out_block->i_pts = i_pts;
544
545             sys->i_samples_delay -= OPUS_FRAME_SIZE;
546
547             i_pts += out_block->i_length;
548
549             sys->i_nb_samples = 0;
550
551             out_block->i_buffer = bytes_encoded;
552             block_ChainAppend(&result, out_block);
553         }
554     }
555
556     /* put leftover samples at beginning of buffer */
557     if (buf->i_nb_samples > 0)
558         fill_buffer(enc, src_start, buf, buf->i_nb_samples);
559
560     return result;
561 }
562
563 static int OpenEncoder(vlc_object_t *p_this)
564 {
565     encoder_t *enc = (encoder_t *)p_this;
566
567     if (enc->fmt_out.i_codec != VLC_CODEC_OPUS)
568         return VLC_EGENERIC;
569
570     encoder_sys_t *sys = malloc(sizeof(*sys));
571     if (!sys)
572         return VLC_ENOMEM;
573
574     int status = VLC_SUCCESS;
575     sys->buffer = NULL;
576     sys->enc = NULL;
577
578     enc->pf_encode_audio = Encode;
579     enc->fmt_in.i_codec = VLC_CODEC_FL32;
580     enc->fmt_in.audio.i_rate = /* Only 48kHz */
581     enc->fmt_out.audio.i_rate = 48000;
582     enc->fmt_out.audio.i_channels = enc->fmt_in.audio.i_channels;
583
584     OpusHeader header;
585
586     if (opus_prepare_header(enc->fmt_out.audio.i_channels,
587             enc->fmt_out.audio.i_rate,
588             &header))
589     {
590         msg_Err(enc, "Failed to prepare header.");
591         status = VLC_ENOMEM;
592         goto error;
593     }
594
595     /* needed for max encoded size calculation */
596     sys->nb_streams = header.nb_streams;
597
598     int err;
599     sys->enc =
600         opus_multistream_surround_encoder_create(enc->fmt_in.audio.i_rate,
601                 enc->fmt_in.audio.i_channels, header.channel_mapping,
602                 &header.nb_streams, &header.nb_coupled, header.stream_map,
603                 OPUS_APPLICATION_AUDIO, &err);
604
605     if (err != OPUS_OK)
606     {
607         msg_Err(enc, "Could not create encoder: error %d", err);
608         sys->enc = NULL;
609         status = VLC_EGENERIC;
610         goto error;
611     }
612
613     /* TODO: vbr, fec */
614
615     if( enc->fmt_out.i_bitrate )
616         opus_multistream_encoder_ctl(sys->enc, OPUS_SET_BITRATE( enc->fmt_out.i_bitrate ));
617
618     /* Buffer for incoming audio, since opus only accepts frame sizes that are
619        multiples of 2.5ms */
620     enc->p_sys = sys;
621     sys->buffer = malloc(OPUS_FRAME_SIZE * header.channels * sizeof(float));
622     if (!sys->buffer) {
623         status = VLC_ENOMEM;
624         goto error;
625     }
626
627     sys->i_nb_samples = 0;
628
629     sys->i_samples_delay = 0;
630     int ret = opus_multistream_encoder_ctl(enc->p_sys->enc,
631             OPUS_GET_LOOKAHEAD(&sys->i_samples_delay));
632     if (ret != OPUS_OK)
633         msg_Err(enc, "Unable to get number of lookahead samples: %s\n",
634                 opus_strerror(ret));
635
636     header.preskip = sys->i_samples_delay;
637
638     /* Now that we have preskip, we can write the header to extradata */
639     if (opus_write_header((uint8_t **) &enc->fmt_out.p_extra,
640                           &enc->fmt_out.i_extra, &header, opus_get_version_string()))
641     {
642         msg_Err(enc, "Failed to write header.");
643         status = VLC_ENOMEM;
644         goto error;
645     }
646
647     if (sys->i_samples_delay > 0)
648     {
649         const unsigned padding_samples = sys->i_samples_delay *
650             enc->fmt_out.audio.i_channels;
651         sys->padding = block_Alloc(padding_samples * sizeof(float));
652         if (!sys->padding) {
653             status = VLC_ENOMEM;
654             goto error;
655         }
656         sys->padding->i_nb_samples = sys->i_samples_delay;
657         float *pad_ptr = (float *) sys->padding->p_buffer;
658         memset(pad_ptr, 0, padding_samples * sizeof(float));
659     }
660     else
661     {
662         sys->padding = NULL;
663     }
664
665     return status;
666
667 error:
668     if (sys->enc)
669         opus_multistream_encoder_destroy(sys->enc);
670     free(sys->buffer);
671     free(sys);
672     return status;
673 }
674
675 static void CloseEncoder(vlc_object_t *p_this)
676 {
677     encoder_t *enc = (encoder_t *)p_this;
678     encoder_sys_t *sys = enc->p_sys;
679
680     opus_multistream_encoder_destroy(sys->enc);
681     if (sys->padding)
682         block_Release(sys->padding);
683     free(sys->buffer);
684     free(sys);
685 }
686 #endif /* ENABLE_SOUT */