]> git.sesse.net Git - vlc/blob - modules/codec/opus.c
Opus decoder.
[vlc] / modules / codec / opus.c
1 /*****************************************************************************
2  * opus.c: opus decoder/encoder module making use of libopus.
3  *****************************************************************************
4  * Copyright (C) 2003-2009, 2012 the VideoLAN team
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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO: preskip, trimming, file duration
26  */
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_input.h>
38 #include <vlc_codec.h>
39 #include <vlc_aout.h>
40 #include "../demux/xiph.h"
41
42 #include <ogg/ogg.h>
43 #include <opus.h>
44 #include <opus_multistream.h>
45
46 #include "opus_header.h"
47
48 #ifndef OPUS_SET_GAIN
49 #include <math.h>
50 #endif
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  OpenDecoder   ( vlc_object_t * );
56 static void CloseDecoder  ( vlc_object_t * );
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 vlc_module_end ()
68
69 /*****************************************************************************
70  * decoder_sys_t : opus decoder descriptor
71  *****************************************************************************/
72 struct decoder_sys_t
73 {
74     /*
75      * Input properties
76      */
77     bool b_has_headers;
78
79     /*
80      * Opus properties
81      */
82     OpusHeader header;
83     OpusMSDecoder *p_st;
84
85     /*
86      * Common properties
87      */
88     date_t end_date;
89 };
90
91 static const int pi_channels_maps[9] =
92 {
93     0,
94     AOUT_CHAN_CENTER,
95     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
96     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
97     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
98      | AOUT_CHAN_REARRIGHT,
99     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
100      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
101     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
102      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
103     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
104      | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
105      | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
106     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
107      | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
108      | AOUT_CHAN_LFE,
109 };
110
111 /*
112 **  channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
113 */
114
115 /* recommended vorbis channel order for 8 channels */
116 static const uint32_t pi_8channels_in[] =
117 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
118   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
119   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE, 0 };
120
121 /* recommended vorbis channel order for 7 channels */
122 static const uint32_t pi_7channels_in[] =
123 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
124   AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
125   AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE, 0 };
126
127 /* recommended vorbis channel order for 6 channels */
128 static const uint32_t pi_6channels_in[] =
129 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
130   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_LFE, 0 };
131
132 /* recommended vorbis channel order for 5 channels */
133 static const uint32_t pi_5channels_in[] =
134 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
135   AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
136
137 /* recommended vorbis channel order for 4 channels */
138 static const uint32_t pi_4channels_in[] =
139 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
140
141 /* recommended vorbis channel order for 3 channels */
142 static const uint32_t pi_3channels_in[] =
143 { AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
144
145 /****************************************************************************
146  * Local prototypes
147  ****************************************************************************/
148
149 static block_t *DecodeBlock  ( decoder_t *, block_t ** );
150 static int  ProcessHeaders( decoder_t * );
151 static int  ProcessInitialHeader ( decoder_t *, ogg_packet * );
152 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
153
154 static block_t *DecodePacket( decoder_t *, ogg_packet *, int, int );
155
156 /*****************************************************************************
157  * OpenDecoder: probe the decoder and return score
158  *****************************************************************************/
159 static int OpenDecoder( vlc_object_t *p_this )
160 {
161     decoder_t *p_dec = (decoder_t*)p_this;
162     decoder_sys_t *p_sys;
163
164     if( p_dec->fmt_in.i_codec != VLC_CODEC_OPUS )
165         return VLC_EGENERIC;
166
167     /* Allocate the memory needed to store the decoder's structure */
168     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
169         return VLC_ENOMEM;
170     p_dec->p_sys->b_has_headers = false;
171
172     date_Set( &p_sys->end_date, 0 );
173
174     /* Set output properties */
175     p_dec->fmt_out.i_cat = AUDIO_ES;
176     p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
177
178     p_dec->pf_decode_audio = DecodeBlock;
179     p_dec->pf_packetize    = DecodeBlock;
180
181     p_sys->p_st = NULL;
182
183     return VLC_SUCCESS;
184 }
185
186 /****************************************************************************
187  * DecodeBlock: the whole thing
188  ****************************************************************************
189  * This function must be fed with ogg packets.
190  ****************************************************************************/
191 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
192 {
193     decoder_sys_t *p_sys = p_dec->p_sys;
194     ogg_packet oggpacket;
195
196     if( !pp_block || !*pp_block)
197         return NULL;
198
199     /* Block to Ogg packet */
200     oggpacket.packet = (*pp_block)->p_buffer;
201     oggpacket.bytes = (*pp_block)->i_buffer;
202
203     oggpacket.granulepos = -1;
204     oggpacket.b_o_s = 0;
205     oggpacket.e_o_s = 0;
206     oggpacket.packetno = 0;
207
208     /* Check for headers */
209     if( !p_sys->b_has_headers )
210     {
211         if( ProcessHeaders( p_dec ) )
212         {
213             block_Release( *pp_block );
214             return NULL;
215         }
216         p_sys->b_has_headers = true;
217     }
218
219     return ProcessPacket( p_dec, &oggpacket, pp_block );
220 }
221
222 /*****************************************************************************
223  * ProcessHeaders: process Opus headers.
224  *****************************************************************************/
225 static int ProcessHeaders( decoder_t *p_dec )
226 {
227     ogg_packet oggpacket;
228
229     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
230     void     *pp_data[XIPH_MAX_HEADER_COUNT];
231     unsigned i_count;
232     int ret = VLC_EGENERIC;
233
234     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
235                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
236         return VLC_EGENERIC;
237     if( i_count < 2 )
238         goto end;
239
240     oggpacket.granulepos = -1;
241     oggpacket.e_o_s = 0;
242     oggpacket.packetno = 0;
243
244     /* Take care of the initial Opus header */
245     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
246     oggpacket.bytes  = pi_size[0];
247     oggpacket.packet = pp_data[0];
248     ret = ProcessInitialHeader( p_dec, &oggpacket );
249
250     if (ret != VLC_SUCCESS)
251         msg_Err( p_dec, "initial Opus header is corrupted" );
252
253 end:
254     for( unsigned i = 0; i < i_count; i++ )
255         free( pp_data[i] );
256
257     return ret;
258 }
259
260 /*****************************************************************************
261  * ProcessInitialHeader: processes the inital Opus header packet.
262  *****************************************************************************/
263 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
264 {
265     int err;
266     int pi_chan_table[AOUT_CHAN_MAX];
267     unsigned char new_stream_map[8];
268     decoder_sys_t *p_sys = p_dec->p_sys;
269
270     OpusHeader *p_header = &p_sys->header;
271
272     if( !opus_header_parse((unsigned char *)p_oggpacket->packet,p_oggpacket->bytes,p_header) )
273     {
274         msg_Err( p_dec, "cannot read Opus header" );
275         return VLC_EGENERIC;
276     }
277     msg_Dbg( p_dec, "Opus audio with %d channels", p_header->channels);
278
279     if((p_header->channels>2 && p_header->channel_mapping==0) ||
280        (p_header->channels>8 && p_header->channel_mapping==1) ||
281         p_header->channel_mapping>1)
282     {
283         msg_Err( p_dec, "Unsupported channel mapping" );
284         return VLC_EGENERIC;
285     }
286
287     /* Setup the format */
288     p_dec->fmt_out.audio.i_physical_channels =
289         p_dec->fmt_out.audio.i_original_channels =
290             pi_channels_maps[p_header->channels];
291     p_dec->fmt_out.audio.i_channels = p_header->channels;
292     p_dec->fmt_out.audio.i_rate = 48000;
293
294     if( p_header->channels>2 )
295     {
296         static const uint32_t *pi_ch[6] = { pi_3channels_in, pi_4channels_in,
297                                             pi_5channels_in, pi_6channels_in,
298                                             pi_7channels_in, pi_8channels_in };
299         aout_CheckChannelReorder( pi_ch[p_header->channels-3], NULL,
300                                   p_dec->fmt_out.audio.i_physical_channels,
301                                   p_header->channels,
302                                   pi_chan_table );
303         for(int i=0;i<p_header->channels;i++)
304             new_stream_map[pi_chan_table[i]]=p_header->stream_map[i];
305     }
306     /* Opus decoder init */
307     p_sys->p_st = opus_multistream_decoder_create( 48000, p_header->channels,
308                     p_header->nb_streams, p_header->nb_coupled,
309                     p_header->channels>2?new_stream_map:p_header->stream_map,
310                     &err );
311     if( !p_sys->p_st || err!=OPUS_OK )
312     {
313         msg_Err( p_dec, "decoder initialization failed" );
314         return VLC_EGENERIC;
315     }
316
317 #ifdef OPUS_SET_GAIN
318     if( opus_multistream_decoder_ctl( p_sys->p_st,OPUS_SET_GAIN(p_header->gain) ) != OPUS_OK )
319     {
320         msg_Err( p_dec, "OPUS_SET_GAIN failed" );
321         opus_multistream_decoder_destroy( p_sys->p_st );
322         return VLC_EGENERIC;
323     }
324 #endif
325
326     date_Init( &p_sys->end_date, 48000, 1 );
327
328     return VLC_SUCCESS;
329 }
330
331 /*****************************************************************************
332  * ProcessPacket: processes a Opus packet.
333  *****************************************************************************/
334 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
335                             block_t **pp_block )
336 {
337     decoder_sys_t *p_sys = p_dec->p_sys;
338     block_t *p_block = *pp_block;
339
340     /* Date management */
341     if( p_block && p_block->i_pts > VLC_TS_INVALID &&
342         p_block->i_pts != date_Get( &p_sys->end_date ) )
343     {
344         date_Set( &p_sys->end_date, p_block->i_pts );
345     }
346
347     if( !date_Get( &p_sys->end_date ) )
348     {
349         /* We've just started the stream, wait for the first PTS. */
350         if( p_block ) block_Release( p_block );
351         return NULL;
352     }
353
354     *pp_block = NULL; /* To avoid being fed the same packet again */
355
356     {
357         block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket,
358                                                p_block->i_nb_samples,
359                                                (int)p_block->i_length );
360
361         block_Release( p_block );
362         return p_aout_buffer;
363     }
364 }
365
366 /*****************************************************************************
367  * DecodePacket: decodes a Opus packet.
368  *****************************************************************************/
369 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
370                               int i_nb_samples, int i_end_trim )
371 {
372     decoder_sys_t *p_sys = p_dec->p_sys;
373
374     if( !p_oggpacket->bytes )
375         return NULL;
376
377     int spp;
378     spp=opus_packet_get_nb_frames(p_oggpacket->packet,p_oggpacket->bytes);
379     if(spp>0)spp*=opus_packet_get_samples_per_frame(p_oggpacket->packet,48000);
380     if(spp<120||spp>120*48)return NULL;
381
382     block_t *p_aout_buffer=decoder_NewAudioBuffer( p_dec, spp );
383     if ( !p_aout_buffer )
384     {
385         msg_Err(p_dec, "Oops: No new buffer was returned!");
386         return NULL;
387     }
388
389     spp=opus_multistream_decode_float(p_sys->p_st, p_oggpacket->packet,
390          p_oggpacket->bytes, (float *)p_aout_buffer->p_buffer, spp, 0);
391     if( spp < 0 || i_nb_samples <= 0 || i_end_trim >= i_nb_samples)
392     {
393         block_Release(p_aout_buffer);
394         if( spp < 0 )
395             msg_Err( p_dec, "Error: corrupted stream?" );
396         return NULL;
397     }
398     if( spp > i_nb_samples )
399     {
400         memmove(p_aout_buffer->p_buffer,
401             p_aout_buffer->p_buffer
402             + (spp - i_nb_samples)*p_sys->header.channels*sizeof(float),
403             (i_nb_samples - i_end_trim)*p_sys->header.channels*sizeof(float));
404     }
405     i_nb_samples -= i_end_trim;
406
407 #ifndef OPUS_SET_GAIN
408     if(p_sys->header.gain!=0)
409     {
410         float gain = pow(10., p_sys->header.gain/5120.);
411         float *buf =(float *)p_aout_buffer->p_buffer;
412         int i;
413         for( i = 0; i < i_nb_samples*p_sys->header.channels; i++)
414             buf[i] *= gain;
415     }
416 #endif
417     p_aout_buffer->i_nb_samples = i_nb_samples;
418     p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
419     p_aout_buffer->i_length = date_Increment( &p_sys->end_date, i_nb_samples )
420         - p_aout_buffer->i_pts;
421     return p_aout_buffer;
422 }
423
424 /*****************************************************************************
425  * CloseDecoder: Opus decoder destruction
426  *****************************************************************************/
427 static void CloseDecoder( vlc_object_t *p_this )
428 {
429     decoder_t * p_dec = (decoder_t *)p_this;
430     decoder_sys_t *p_sys = p_dec->p_sys;
431
432     if( p_sys->p_st ) opus_multistream_decoder_destroy(p_sys->p_st);
433
434     free( p_sys );
435 }