1 /*****************************************************************************
2 * opus.c: opus decoder/encoder module making use of libopus.
3 *****************************************************************************
4 * Copyright (C) 2003-2009, 2012 VLC authors and VideoLAN
6 * Authors: Gregory Maxwell <greg@xiph.org>
7 * Based on speex.c by: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 * TODO: preskip, trimming, file duration
28 /*****************************************************************************
30 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_input.h>
38 #include <vlc_codec.h>
40 #include "../demux/xiph.h"
44 #include <opus_multistream.h>
46 #include "opus_header.h"
52 /*****************************************************************************
54 *****************************************************************************/
55 static int OpenDecoder ( vlc_object_t * );
56 static void CloseDecoder ( vlc_object_t * );
59 set_category( CAT_INPUT )
60 set_subcategory( SUBCAT_INPUT_ACODEC )
62 set_description( N_("Opus audio decoder") )
63 set_capability( "decoder", 100 )
64 set_shortname( N_("Opus") )
65 set_callbacks( OpenDecoder, CloseDecoder )
69 /*****************************************************************************
70 * decoder_sys_t : opus decoder descriptor
71 *****************************************************************************/
91 static const int pi_channels_maps[9] =
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
112 ** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
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 };
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 };
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 };
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 };
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 };
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 };
145 /****************************************************************************
147 ****************************************************************************/
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 ** );
154 static block_t *DecodePacket( decoder_t *, ogg_packet *, int, int );
156 /*****************************************************************************
157 * OpenDecoder: probe the decoder and return score
158 *****************************************************************************/
159 static int OpenDecoder( vlc_object_t *p_this )
161 decoder_t *p_dec = (decoder_t*)p_this;
162 decoder_sys_t *p_sys;
164 if( p_dec->fmt_in.i_codec != VLC_CODEC_OPUS )
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 )
170 p_dec->p_sys->b_has_headers = false;
172 date_Set( &p_sys->end_date, 0 );
174 /* Set output properties */
175 p_dec->fmt_out.i_cat = AUDIO_ES;
176 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
178 p_dec->pf_decode_audio = DecodeBlock;
179 p_dec->pf_packetize = DecodeBlock;
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 )
193 decoder_sys_t *p_sys = p_dec->p_sys;
194 ogg_packet oggpacket;
196 if( !pp_block || !*pp_block)
199 /* Block to Ogg packet */
200 oggpacket.packet = (*pp_block)->p_buffer;
201 oggpacket.bytes = (*pp_block)->i_buffer;
203 oggpacket.granulepos = -1;
206 oggpacket.packetno = 0;
208 /* Check for headers */
209 if( !p_sys->b_has_headers )
211 if( ProcessHeaders( p_dec ) )
213 block_Release( *pp_block );
216 p_sys->b_has_headers = true;
219 return ProcessPacket( p_dec, &oggpacket, pp_block );
222 /*****************************************************************************
223 * ProcessHeaders: process Opus headers.
224 *****************************************************************************/
225 static int ProcessHeaders( decoder_t *p_dec )
227 ogg_packet oggpacket;
229 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
230 void *pp_data[XIPH_MAX_HEADER_COUNT];
232 int ret = VLC_EGENERIC;
234 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
235 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
240 oggpacket.granulepos = -1;
242 oggpacket.packetno = 0;
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 );
250 if (ret != VLC_SUCCESS)
251 msg_Err( p_dec, "initial Opus header is corrupted" );
254 for( unsigned i = 0; i < i_count; i++ )
260 /*****************************************************************************
261 * ProcessInitialHeader: processes the inital Opus header packet.
262 *****************************************************************************/
263 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
266 unsigned char new_stream_map[8];
267 decoder_sys_t *p_sys = p_dec->p_sys;
269 OpusHeader *p_header = &p_sys->header;
271 if( !opus_header_parse((unsigned char *)p_oggpacket->packet,p_oggpacket->bytes,p_header) )
273 msg_Err( p_dec, "cannot read Opus header" );
276 msg_Dbg( p_dec, "Opus audio with %d channels", p_header->channels);
278 if((p_header->channels>2 && p_header->channel_mapping==0) ||
279 (p_header->channels>8 && p_header->channel_mapping==1) ||
280 p_header->channel_mapping>1)
282 msg_Err( p_dec, "Unsupported channel mapping" );
286 /* Setup the format */
287 p_dec->fmt_out.audio.i_physical_channels =
288 p_dec->fmt_out.audio.i_original_channels =
289 pi_channels_maps[p_header->channels];
290 p_dec->fmt_out.audio.i_channels = p_header->channels;
291 p_dec->fmt_out.audio.i_rate = 48000;
293 if( p_header->channels>2 )
295 static const uint32_t *pi_ch[6] = { pi_3channels_in, pi_4channels_in,
296 pi_5channels_in, pi_6channels_in,
297 pi_7channels_in, pi_8channels_in };
298 uint8_t pi_chan_table[AOUT_CHAN_MAX];
300 aout_CheckChannelReorder( pi_ch[p_header->channels-3], NULL,
301 p_dec->fmt_out.audio.i_physical_channels,
303 for(int i=0;i<p_header->channels;i++)
304 new_stream_map[pi_chan_table[i]]=p_header->stream_map[i];
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,
311 if( !p_sys->p_st || err!=OPUS_OK )
313 msg_Err( p_dec, "decoder initialization failed" );
318 if( opus_multistream_decoder_ctl( p_sys->p_st,OPUS_SET_GAIN(p_header->gain) ) != OPUS_OK )
320 msg_Err( p_dec, "OPUS_SET_GAIN failed" );
321 opus_multistream_decoder_destroy( p_sys->p_st );
326 date_Init( &p_sys->end_date, 48000, 1 );
331 /*****************************************************************************
332 * ProcessPacket: processes a Opus packet.
333 *****************************************************************************/
334 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
337 decoder_sys_t *p_sys = p_dec->p_sys;
338 block_t *p_block = *pp_block;
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 ) )
344 date_Set( &p_sys->end_date, p_block->i_pts );
347 if( !date_Get( &p_sys->end_date ) )
349 /* We've just started the stream, wait for the first PTS. */
350 if( p_block ) block_Release( p_block );
354 *pp_block = NULL; /* To avoid being fed the same packet again */
357 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket,
358 p_block->i_nb_samples,
359 (int)p_block->i_length );
361 block_Release( p_block );
362 return p_aout_buffer;
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 )
372 decoder_sys_t *p_sys = p_dec->p_sys;
374 if( !p_oggpacket->bytes )
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;
382 block_t *p_aout_buffer=decoder_NewAudioBuffer( p_dec, spp );
383 if ( !p_aout_buffer )
385 msg_Err(p_dec, "Oops: No new buffer was returned!");
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)
393 block_Release(p_aout_buffer);
395 msg_Err( p_dec, "Error: corrupted stream?" );
398 if( spp > i_nb_samples )
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));
405 i_nb_samples -= i_end_trim;
407 #ifndef OPUS_SET_GAIN
408 if(p_sys->header.gain!=0)
410 float gain = pow(10., p_sys->header.gain/5120.);
411 float *buf =(float *)p_aout_buffer->p_buffer;
413 for( i = 0; i < i_nb_samples*p_sys->header.channels; i++)
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;
424 /*****************************************************************************
425 * CloseDecoder: Opus decoder destruction
426 *****************************************************************************/
427 static void CloseDecoder( vlc_object_t *p_this )
429 decoder_t * p_dec = (decoder_t *)p_this;
430 decoder_sys_t *p_sys = p_dec->p_sys;
432 if( p_sys->p_st ) opus_multistream_decoder_destroy(p_sys->p_st);