[vlc-devel] [PATCH] Opus decoder
Rafaël Carré
funman at videolan.org
Mon Aug 6 18:46:51 CEST 2012
From: Gregory Maxwell <greg at xiph.org>
Preskip is not handled, end trimming is not handled,
seek position doesn't respect preskip and does not
pre-roll. Multichannel has mask bugs for some channel
counts.
---
configure.ac | 7 +-
include/vlc_fourcc.h | 1 +
modules/LIST | 1 +
modules/codec/Modules.am | 1 +
modules/codec/opus.c | 422 +++++++++++++++++++++++++++++++++++++++++++
modules/codec/opus_header.c | 268 +++++++++++++++++++++++++++
modules/codec/opus_header.h | 49 +++++
modules/demux/ogg.c | 67 ++++++-
modules/demux/ogg.h | 2 +
9 files changed, 815 insertions(+), 3 deletions(-)
create mode 100644 modules/codec/opus.c
create mode 100644 modules/codec/opus_header.c
create mode 100644 modules/codec/opus_header.h
diff --git a/configure.ac b/configure.ac
index f0865f4..8e961d3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -633,7 +633,7 @@ AC_CHECK_FUNC(getopt_long,, [
AC_SUBST(GNUGETOPT_LIBS)
AC_CHECK_LIB(m,cos,[
- VLC_ADD_LIBS([adjust wave ripple psychedelic gradient a52tofloat32 dtstofloat32 x264 goom visual panoramix rotate noise grain scene kate flac lua chorus_flanger freetype avcodec avformat access_avio swscale postproc i420_rgb faad twolame equalizer spatializer param_eq samplerate freetype mpc dmo mp4 quicktime qt4 compressor headphone_channel_mixer normvol audiobargraph_a speex mono colorthres extract ball access_imem hotkeys mosaic gaussianblur dbus x26410b hqdn3d anaglyph oldrc ncurses],[-lm])
+ VLC_ADD_LIBS([adjust wave ripple psychedelic gradient a52tofloat32 dtstofloat32 x264 goom visual panoramix rotate noise grain scene kate flac lua chorus_flanger freetype avcodec avformat access_avio swscale postproc i420_rgb faad twolame equalizer spatializer param_eq samplerate freetype mpc dmo mp4 quicktime qt4 compressor headphone_channel_mixer normvol audiobargraph_a speex opus mono colorthres extract ball access_imem hotkeys mosaic gaussianblur dbus x26410b hqdn3d anaglyph oldrc ncurses],[-lm])
LIBM="-lm"
], [
LIBM=""
@@ -2724,6 +2724,11 @@ AS_IF([test "${enable_speex}" != "no"], [
AM_CONDITIONAL([HAVE_SPEEXDSP], [test "$have_speexdsp" = "yes"])
dnl
+dnl Opus plugin
+dnl
+PKG_ENABLE_MODULES_VLC([OPUS], [], [ogg opus], [Opus support], [auto])
+
+dnl
dnl theora decoder plugin
dnl
PKG_ENABLE_MODULES_VLC([THEORA], [], [ogg theoradec >= 1.0 theoraenc], [experimental theora codec], [auto])
diff --git a/include/vlc_fourcc.h b/include/vlc_fourcc.h
index 0306127..fd7b9ec 100644
--- a/include/vlc_fourcc.h
+++ b/include/vlc_fourcc.h
@@ -257,6 +257,7 @@
#define VLC_CODEC_TRUEHD VLC_FOURCC('t','r','h','d')
#define VLC_CODEC_DVAUDIO VLC_FOURCC('d','v','a','u')
#define VLC_CODEC_SPEEX VLC_FOURCC('s','p','x',' ')
+#define VLC_CODEC_OPUS VLC_FOURCC('O','p','u','s')
#define VLC_CODEC_VORBIS VLC_FOURCC('v','o','r','b')
#define VLC_CODEC_MACE3 VLC_FOURCC('M','A','C','3')
#define VLC_CODEC_MACE6 VLC_FOURCC('M','A','C','6')
diff --git a/modules/LIST b/modules/LIST
index f6dda85..f514915 100644
--- a/modules/LIST
+++ b/modules/LIST
@@ -231,6 +231,7 @@ $Id$
* opencv_example: OpenCV example (face identification)
* opencv_wrapper: OpenCV wrapper video filter
* opensles_android: OpenSL ES audio output for Android
+ * opus: a opus audio decoder/packetizer using the libopus library
* os2drive: service discovery for OS/2 drives
* osd_parser: OSD import module
* osdmenu: video_filter for displaying and streaming a On Screen Display menu
diff --git a/modules/codec/Modules.am b/modules/codec/Modules.am
index 6f0fddd..7f7ffca 100644
--- a/modules/codec/Modules.am
+++ b/modules/codec/Modules.am
@@ -9,6 +9,7 @@ SOURCES_theora = theora.c
SOURCES_tremor = vorbis.c
SOURCES_speex = speex.c
SOURCES_adpcm = adpcm.c
+SOURCES_opus = opus.c opus_header.c opus_header.h
SOURCES_uleaddvaudio = uleaddvaudio.c
SOURCES_mpeg_audio = mpeg_audio.c
SOURCES_libmpeg2 = libmpeg2.c
diff --git a/modules/codec/opus.c b/modules/codec/opus.c
new file mode 100644
index 0000000..105a2b6
--- /dev/null
+++ b/modules/codec/opus.c
@@ -0,0 +1,422 @@
+/*****************************************************************************
+ * opus.c: opus decoder/encoder module making use of libopus.
+ *****************************************************************************
+ * Copyright (C) 2003-2009, 2012 the VideoLAN team
+ *
+ * Authors: Gregory Maxwell <greg at xiph.org>
+ * Based on speex.c by: Gildas Bazin <gbazin at videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*
+ * TODO: preskip, trimming, file duration
+ */
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_input.h>
+#include <vlc_codec.h>
+#include <vlc_aout.h>
+#include "../demux/xiph.h"
+
+#include <ogg/ogg.h>
+#include <opus.h>
+#include <opus_multistream.h>
+
+#include "opus_header.h"
+
+#ifndef OPUS_SET_GAIN
+#include <math.h>
+#endif
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+static int OpenDecoder ( vlc_object_t * );
+static void CloseDecoder ( vlc_object_t * );
+
+vlc_module_begin ()
+ set_category( CAT_INPUT )
+ set_subcategory( SUBCAT_INPUT_ACODEC )
+
+ set_description( N_("Opus audio decoder") )
+ set_capability( "decoder", 100 )
+ set_shortname( N_("Opus") )
+ set_callbacks( OpenDecoder, CloseDecoder )
+
+vlc_module_end ()
+
+/*****************************************************************************
+ * decoder_sys_t : opus decoder descriptor
+ *****************************************************************************/
+struct decoder_sys_t
+{
+ /*
+ * Input properties
+ */
+ bool b_has_headers;
+
+ /*
+ * Opus properties
+ */
+ OpusHeader header;
+ OpusMSDecoder *p_st;
+ int i_to_skip;
+
+ /*
+ * Common properties
+ */
+ date_t end_date;
+};
+
+static const int pi_channels_maps[9] =
+{
+ 0,
+ AOUT_CHAN_CENTER,
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
+ AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
+ | AOUT_CHAN_REARRIGHT,
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
+ | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
+ | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
+ | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
+ | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
+ | AOUT_CHAN_LFE,
+};
+
+/*
+** channel order as defined in http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9
+*/
+
+/* recommended vorbis channel order for 8 channels */
+static const uint32_t pi_8channels_in[] =
+{ AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
+ AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
+ AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
+
+/* recommended vorbis channel order for 7 channels */
+static const uint32_t pi_7channels_in[] =
+{ AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
+ AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
+ AOUT_CHAN_REARCENTER,AOUT_CHAN_LFE,0 };
+
+/* recommended vorbis channel order for 6 channels */
+static const uint32_t pi_6channels_in[] =
+{ AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT,
+ AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,AOUT_CHAN_LFE,0 };
+
+/* recommended vorbis channel order for 4 channels */
+static const uint32_t pi_4channels_in[] =
+{ AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT, AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, 0 };
+
+/* recommended vorbis channel order for 3 channels */
+static const uint32_t pi_3channels_in[] =
+{ AOUT_CHAN_LEFT, AOUT_CHAN_CENTER, AOUT_CHAN_RIGHT, 0 };
+
+/****************************************************************************
+ * Local prototypes
+ ****************************************************************************/
+
+static block_t *DecodeBlock ( decoder_t *, block_t ** );
+static int ProcessHeaders( decoder_t * );
+static int ProcessInitialHeader ( decoder_t *, ogg_packet * );
+static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
+
+static block_t *DecodePacket( decoder_t *, ogg_packet * );
+
+/*****************************************************************************
+ * OpenDecoder: probe the decoder and return score
+ *****************************************************************************/
+static int OpenDecoder( vlc_object_t *p_this )
+{
+ decoder_t *p_dec = (decoder_t*)p_this;
+ decoder_sys_t *p_sys;
+
+ if( p_dec->fmt_in.i_codec != VLC_CODEC_OPUS )
+ return VLC_EGENERIC;
+
+ /* Allocate the memory needed to store the decoder's structure */
+ if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
+ return VLC_ENOMEM;
+ p_dec->p_sys->b_has_headers = false;
+
+ date_Set( &p_sys->end_date, 0 );
+
+ /* Set output properties */
+ p_dec->fmt_out.i_cat = AUDIO_ES;
+ p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
+
+ p_dec->pf_decode_audio = DecodeBlock;
+ p_dec->pf_packetize = DecodeBlock;
+
+ p_sys->p_st = NULL;
+ p_sys->i_to_skip = 0;
+
+ return VLC_SUCCESS;
+}
+
+/****************************************************************************
+ * DecodeBlock: the whole thing
+ ****************************************************************************
+ * This function must be fed with ogg packets.
+ ****************************************************************************/
+static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ ogg_packet oggpacket;
+
+ if( !pp_block || !*pp_block)
+ return NULL;
+
+ /* Block to Ogg packet */
+ oggpacket.packet = (*pp_block)->p_buffer;
+ oggpacket.bytes = (*pp_block)->i_buffer;
+
+ oggpacket.granulepos = -1;
+ oggpacket.b_o_s = 0;
+ oggpacket.e_o_s = 0;
+ oggpacket.packetno = 0;
+
+ /* Check for headers */
+ if( !p_sys->b_has_headers )
+ {
+ if( ProcessHeaders( p_dec ) )
+ {
+ block_Release( *pp_block );
+ return NULL;
+ }
+ p_sys->b_has_headers = true;
+ }
+
+ return ProcessPacket( p_dec, &oggpacket, pp_block );
+}
+
+/*****************************************************************************
+ * ProcessHeaders: process Opus headers.
+ *****************************************************************************/
+static int ProcessHeaders( decoder_t *p_dec )
+{
+ ogg_packet oggpacket;
+
+ unsigned pi_size[XIPH_MAX_HEADER_COUNT];
+ void *pp_data[XIPH_MAX_HEADER_COUNT];
+ unsigned i_count;
+ int ret = VLC_EGENERIC;
+
+ if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
+ p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
+ return VLC_EGENERIC;
+ if( i_count < 2 )
+ goto end;
+
+ oggpacket.granulepos = -1;
+ oggpacket.e_o_s = 0;
+ oggpacket.packetno = 0;
+
+ /* Take care of the initial Opus header */
+ oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
+ oggpacket.bytes = pi_size[0];
+ oggpacket.packet = pp_data[0];
+ ret = ProcessInitialHeader( p_dec, &oggpacket );
+
+ if (ret != VLC_SUCCESS)
+ msg_Err( p_dec, "initial Opus header is corrupted" );
+
+end:
+ for( unsigned i = 0; i < i_count; i++ )
+ free( pp_data[i] );
+
+ return ret;
+}
+
+/*****************************************************************************
+ * ProcessInitialHeader: processes the inital Opus header packet.
+ *****************************************************************************/
+static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
+{
+ int err;
+ int pi_chan_table[AOUT_CHAN_MAX];
+ unsigned char new_stream_map[8];
+ decoder_sys_t *p_sys = p_dec->p_sys;
+
+ OpusHeader *p_header = &p_sys->header;
+
+ if( !opus_header_parse((unsigned char *)p_oggpacket->packet,p_oggpacket->bytes,p_header) )
+ {
+ msg_Err( p_dec, "cannot read Opus header" );
+ return VLC_EGENERIC;
+ }
+ msg_Dbg( p_dec, "Opus audio with %d channels", p_header->channels);
+
+ if((p_header->channels>2 && p_header->channel_mapping==0) ||
+ (p_header->channels>8 && p_header->channel_mapping==1) ||
+ p_header->channel_mapping>1)
+ {
+ msg_Err( p_dec, "Unsupported channel mapping" );
+ return VLC_EGENERIC;
+ }
+
+ /* Setup the format */
+ p_dec->fmt_out.audio.i_physical_channels =
+ p_dec->fmt_out.audio.i_original_channels =
+ pi_channels_maps[p_header->channels];
+ p_dec->fmt_out.audio.i_channels = p_header->channels;
+ p_dec->fmt_out.audio.i_rate = 48000;
+
+ if( p_header->channels>2 )
+ {
+ const uint32_t *pi_ch[6] = { pi_3channels_in, pi_4channels_in,
+ pi_6channels_in, pi_6channels_in,
+ pi_7channels_in, pi_8channels_in };
+ aout_CheckChannelReorder( pi_ch[p_header->channels-2], NULL,
+ p_dec->fmt_out.audio.i_physical_channels,
+ p_header->channels,
+ pi_chan_table );
+ for(int i=0;i<p_header->channels;i++)
+ new_stream_map[i]=p_header->stream_map[pi_chan_table[i]];
+ }
+ /* Opus decoder init */
+ p_sys->p_st = opus_multistream_decoder_create( 48000, p_header->channels,
+ p_header->nb_streams, p_header->nb_coupled,
+ p_header->channels>2?new_stream_map:p_header->stream_map,
+ &err );
+ if( !p_sys->p_st || err!=OPUS_OK )
+ {
+ msg_Err( p_dec, "decoder initialization failed" );
+ return VLC_EGENERIC;
+ }
+
+ p_sys->i_to_skip = p_header->preskip;
+
+#ifdef OPUS_SET_GAIN
+ if( opus_multistream_decoder_ctl( p_sys->p_st,OPUS_SET_GAIN(p_header->gain) ) != OPUS_OK )
+ {
+ msg_Err( p_dec, "OPUS_SET_GAIN failed" );
+ opus_multistream_decoder_destroy( p_sys->p_st );
+ return VLC_EGENERIC;
+ }
+#endif
+
+ date_Init( &p_sys->end_date, 48000, 1 );
+
+ return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * ProcessPacket: processes a Opus packet.
+ *****************************************************************************/
+static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
+ block_t **pp_block )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ block_t *p_block = *pp_block;
+
+ /* Date management */
+ if( p_block && p_block->i_pts > VLC_TS_INVALID &&
+ p_block->i_pts != date_Get( &p_sys->end_date ) )
+ {
+ date_Set( &p_sys->end_date, p_block->i_pts );
+ }
+
+ if( !date_Get( &p_sys->end_date ) )
+ {
+ /* We've just started the stream, wait for the first PTS. */
+ if( p_block ) block_Release( p_block );
+ return NULL;
+ }
+
+ *pp_block = NULL; /* To avoid being fed the same packet again */
+
+ {
+ block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
+
+ if( p_block )
+ block_Release( p_block );
+ return p_aout_buffer;
+ }
+}
+
+/*****************************************************************************
+ * DecodePacket: decodes a Opus packet.
+ *****************************************************************************/
+static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+
+ if( !p_oggpacket->bytes )
+ return NULL;
+
+ opus_int32 spp;
+ int err;
+ spp=opus_packet_get_nb_frames(p_oggpacket->packet,p_oggpacket->bytes);
+ if(spp>0)spp*=opus_packet_get_samples_per_frame(p_oggpacket->packet,48000);
+ if(spp<120)return NULL;
+
+ block_t *p_aout_buffer=decoder_NewAudioBuffer( p_dec, spp );
+ if ( !p_aout_buffer )
+ {
+ msg_Err(p_dec, "Oops: No new buffer was returned!");
+ return NULL;
+ }
+
+ err=opus_multistream_decode_float(p_sys->p_st, p_oggpacket->packet,
+ p_oggpacket->bytes, (float *)p_aout_buffer->p_buffer, spp, 0);
+ if(err<0)
+ {
+ block_Release(p_aout_buffer);
+ msg_Err( p_dec, "Error: corrupted stream?" );
+ return NULL;
+ }
+
+#ifndef OPUS_SET_GAIN
+ if(p_sys->header.gain!=0)
+ {
+ float gain = pow(10., p_sys->header.gain/5120.);
+ float *buf =(float *)p_aout_buffer->p_buffer;
+ for(int i=0;i<err*p_sys->header.channels;i++)
+ buf[i]*=gain;
+ }
+#endif
+ p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
+ p_aout_buffer->i_length = date_Increment( &p_sys->end_date, err ) -
+ p_aout_buffer->i_pts;
+ return p_aout_buffer;
+}
+
+/*****************************************************************************
+ * CloseDecoder: Opus decoder destruction
+ *****************************************************************************/
+static void CloseDecoder( vlc_object_t *p_this )
+{
+ decoder_t * p_dec = (decoder_t *)p_this;
+ decoder_sys_t *p_sys = p_dec->p_sys;
+
+ if( p_sys->p_st ) opus_multistream_decoder_destroy(p_sys->p_st);
+
+ free( p_sys );
+}
diff --git a/modules/codec/opus_header.c b/modules/codec/opus_header.c
new file mode 100644
index 0000000..cb5fb93
--- /dev/null
+++ b/modules/codec/opus_header.c
@@ -0,0 +1,268 @@
+/* Copyright (C)2012 Xiph.Org Foundation
+ File: opus_header.c
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "opus_header.h"
+#include <string.h>
+#include <stdio.h>
+
+/* Header contents:
+ - "OpusHead" (64 bits)
+ - version number (8 bits)
+ - Channels C (8 bits)
+ - Pre-skip (16 bits)
+ - Sampling rate (32 bits)
+ - Gain in dB (16 bits, S7.8)
+ - Mapping (8 bits, 0=single stream (mono/stereo) 1=Vorbis mapping,
+ 2..254: reserved, 255: multistream with no mapping)
+
+ - if (mapping != 0)
+ - N = totel number of streams (8 bits)
+ - M = number of paired streams (8 bits)
+ - C times channel origin
+ - if (C<2*M)
+ - stream = byte/2
+ - if (byte&0x1 == 0)
+ - left
+ else
+ - right
+ - else
+ - stream = byte-M
+*/
+
+typedef struct {
+ unsigned char *data;
+ int maxlen;
+ int pos;
+} Packet;
+
+typedef struct {
+ const unsigned char *data;
+ int maxlen;
+ int pos;
+} ROPacket;
+
+static int write_uint32(Packet *p, ogg_uint32_t val)
+{
+ if (p->pos>p->maxlen-4)
+ return 0;
+ p->data[p->pos ] = (val ) & 0xFF;
+ p->data[p->pos+1] = (val>> 8) & 0xFF;
+ p->data[p->pos+2] = (val>>16) & 0xFF;
+ p->data[p->pos+3] = (val>>24) & 0xFF;
+ p->pos += 4;
+ return 1;
+}
+
+static int write_uint16(Packet *p, ogg_uint16_t val)
+{
+ if (p->pos>p->maxlen-2)
+ return 0;
+ p->data[p->pos ] = (val ) & 0xFF;
+ p->data[p->pos+1] = (val>> 8) & 0xFF;
+ p->pos += 2;
+ return 1;
+}
+
+static int write_chars(Packet *p, const unsigned char *str, int nb_chars)
+{
+ if (p->pos>p->maxlen-nb_chars)
+ return 0;
+ for (int i=0;i<nb_chars;i++)
+ p->data[p->pos++] = str[i];
+ return 1;
+}
+
+static int read_uint32(ROPacket *p, ogg_uint32_t *val)
+{
+ if (p->pos>p->maxlen-4)
+ return 0;
+ *val = (ogg_uint32_t)p->data[p->pos ];
+ *val |= (ogg_uint32_t)p->data[p->pos+1]<< 8;
+ *val |= (ogg_uint32_t)p->data[p->pos+2]<<16;
+ *val |= (ogg_uint32_t)p->data[p->pos+3]<<24;
+ p->pos += 4;
+ return 1;
+}
+
+static int read_uint16(ROPacket *p, ogg_uint16_t *val)
+{
+ if (p->pos>p->maxlen-2)
+ return 0;
+ *val = (ogg_uint16_t)p->data[p->pos ];
+ *val |= (ogg_uint16_t)p->data[p->pos+1]<<8;
+ p->pos += 2;
+ return 1;
+}
+
+static int read_chars(ROPacket *p, unsigned char *str, int nb_chars)
+{
+ if (p->pos>p->maxlen-nb_chars)
+ return 0;
+ for (int i=0;i<nb_chars;i++)
+ str[i] = p->data[p->pos++];
+ return 1;
+}
+
+int opus_header_parse(const unsigned char *packet, int len, OpusHeader *h)
+{
+ char str[9];
+ ROPacket p;
+ unsigned char ch;
+ ogg_uint16_t shortval;
+
+ p.data = packet;
+ p.maxlen = len;
+ p.pos = 0;
+ str[8] = 0;
+ if (len<19)return 0;
+ read_chars(&p, (unsigned char*)str, 8);
+ if (memcmp(str, "OpusHead", 8)!=0)
+ return 0;
+
+ if (!read_chars(&p, &ch, 1))
+ return 0;
+ h->version = ch;
+ if((h->version&240) != 0) /* Only major version 0 supported. */
+ return 0;
+
+ if (!read_chars(&p, &ch, 1))
+ return 0;
+ h->channels = ch;
+ if (h->channels == 0)
+ return 0;
+
+ if (!read_uint16(&p, &shortval))
+ return 0;
+ h->preskip = shortval;
+
+ if (!read_uint32(&p, &h->input_sample_rate))
+ return 0;
+
+ if (!read_uint16(&p, &shortval))
+ return 0;
+ h->gain = (short)shortval;
+
+ if (!read_chars(&p, &ch, 1))
+ return 0;
+ h->channel_mapping = ch;
+
+ if (h->channel_mapping != 0)
+ {
+ if (!read_chars(&p, &ch, 1))
+ return 0;
+
+ if (ch<1)
+ return 0;
+ h->nb_streams = ch;
+
+ if (!read_chars(&p, &ch, 1))
+ return 0;
+
+ if (ch>h->nb_streams || (ch+h->nb_streams)>255)
+ return 0;
+ h->nb_coupled = ch;
+
+ /* Multi-stream support */
+ for (int i=0;i<h->channels;i++)
+ {
+ if (!read_chars(&p, &h->stream_map[i], 1))
+ return 0;
+ if (h->stream_map[i]>(h->nb_streams+h->nb_coupled) && h->stream_map[i]!=255)
+ return 0;
+ }
+ } else {
+ if(h->channels>2)
+ return 0;
+ h->nb_streams = 1;
+ h->nb_coupled = h->channels>1;
+ h->stream_map[0]=0;
+ h->stream_map[1]=1;
+ }
+ /*For version 0/1 we know there won't be any more data
+ so reject any that have data past the end.*/
+ if ((h->version==0 || h->version==1) && p.pos != len)
+ return 0;
+ return 1;
+}
+
+int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len)
+{
+ Packet p;
+ unsigned char ch;
+
+ p.data = packet;
+ p.maxlen = len;
+ p.pos = 0;
+ if (len<19)return 0;
+ if (!write_chars(&p, (const unsigned char*)"OpusHead", 8))
+ return 0;
+ /* Version is 1 */
+ ch = 1;
+ if (!write_chars(&p, &ch, 1))
+ return 0;
+
+ ch = h->channels;
+ if (!write_chars(&p, &ch, 1))
+ return 0;
+
+ if (!write_uint16(&p, h->preskip))
+ return 0;
+
+ if (!write_uint32(&p, h->input_sample_rate))
+ return 0;
+
+ if (!write_uint16(&p, h->gain))
+ return 0;
+
+ ch = h->channel_mapping;
+ if (!write_chars(&p, &ch, 1))
+ return 0;
+
+ if (h->channel_mapping != 0)
+ {
+ ch = h->nb_streams;
+ if (!write_chars(&p, &ch, 1))
+ return 0;
+
+ ch = h->nb_coupled;
+ if (!write_chars(&p, &ch, 1))
+ return 0;
+
+ /* Multi-stream support */
+ for (int i=0;i<h->channels;i++)
+ {
+ if (!write_chars(&p, &h->stream_map[i], 1))
+ return 0;
+ }
+ }
+
+ return p.pos;
+}
diff --git a/modules/codec/opus_header.h b/modules/codec/opus_header.h
new file mode 100644
index 0000000..633c2f4
--- /dev/null
+++ b/modules/codec/opus_header.h
@@ -0,0 +1,49 @@
+/* Copyright (C)2012 Xiph.Org Foundation
+ File: opus_header.h
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef OPUS_HEADER_H
+#define OPUS_HEADER_H
+
+#include <ogg/ogg.h>
+
+typedef struct {
+ int version;
+ int channels; /* Number of channels: 1..255 */
+ int preskip;
+ ogg_uint32_t input_sample_rate;
+ int gain; /* in dB S7.8 should be zero whenever possible */
+ int channel_mapping;
+ /* The rest is only used if channel_mapping != 0 */
+ int nb_streams;
+ int nb_coupled;
+ unsigned char stream_map[255];
+} OpusHeader;
+
+int opus_header_parse(const unsigned char *header, int len, OpusHeader *h);
+int opus_header_to_packet(const OpusHeader *h, unsigned char *packet, int len);
+
+#endif
diff --git a/modules/demux/ogg.c b/modules/demux/ogg.c
index 606fbe5..78f4bf3 100644
--- a/modules/demux/ogg.c
+++ b/modules/demux/ogg.c
@@ -139,6 +139,7 @@ static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8
static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
+static void Ogg_ReadOpusHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
@@ -349,6 +350,7 @@ static int Demux( demux_t * p_demux )
/* An Ogg/vorbis packet contains an end date granulepos */
if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
+ p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
p_stream->fmt.i_codec == VLC_CODEC_FLAC )
{
if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
@@ -602,6 +604,11 @@ static void Ogg_DecodePacket( demux_t *p_demux,
b_xiph = true;
break;
+ case VLC_CODEC_OPUS:
+ if( p_stream->i_packets_backup == 2 ) p_stream->b_force_backup = 0;
+ b_xiph = true;
+ break;
+
case VLC_CODEC_FLAC:
if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
{
@@ -690,6 +697,7 @@ static void Ogg_DecodePacket( demux_t *p_demux,
/* Convert the pcr into a pts */
if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
+ p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
p_stream->fmt.i_codec == VLC_CODEC_FLAC )
{
if( p_stream->i_pcr >= 0 )
@@ -708,7 +716,14 @@ static void Ogg_DecodePacket( demux_t *p_demux,
p_stream->i_previous_pcr = p_stream->i_pcr;
/* The granulepos is the end date of the sample */
- i_pts = p_stream->i_pcr;
+ if( p_stream->i_pcr > p_stream->i_pre_skip )
+ {
+ i_pts = p_stream->i_pcr - p_stream->i_pre_skip;
+ }
+ else
+ {
+ i_pts = 0;
+ }
}
}
@@ -735,13 +750,21 @@ static void Ogg_DecodePacket( demux_t *p_demux,
if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
+ p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
p_stream->i_pcr >= 0 )
{
p_stream->i_previous_pcr = p_stream->i_pcr;
/* The granulepos is the start date of the sample */
- i_pts = p_stream->i_pcr;
+ if ( p_stream->i_pcr > p_stream->i_pre_skip )
+ {
+ i_pts = p_stream->i_pcr - p_stream->i_pre_skip;
+ }
+ else
+ {
+ i_pts = 0;
+ }
}
if( !b_selected )
@@ -808,6 +831,7 @@ static void Ogg_DecodePacket( demux_t *p_demux,
if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
+ p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
@@ -947,6 +971,16 @@ static int Ogg_FindLogicalStreams( demux_t *p_demux )
p_stream->fmt.audio.i_channels,
(int)p_stream->f_rate, p_stream->fmt.i_bitrate );
}
+ /* Check for Opus header */
+ else if( oggpacket.bytes >= 8 &&
+ ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
+ {
+ Ogg_ReadOpusHeader( p_stream, &oggpacket );
+ msg_Dbg( p_demux, "found opus header, channels: %i, "
+ "pre-skip: %i",
+ p_stream->fmt.audio.i_channels,
+ (int)p_stream->i_pre_skip);
+ }
/* Check for Flac header (< version 1.1.1) */
else if( oggpacket.bytes >= 4 &&
! memcmp( oggpacket.packet, "fLaC", 4 ) )
@@ -1545,6 +1579,9 @@ static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8
case VLC_CODEC_SPEEX:
Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
break;
+ case VLC_CODEC_OPUS:
+ Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
+ break;
/* N headers with the 2° one being the comments */
case VLC_CODEC_KATE:
@@ -1684,6 +1721,32 @@ static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
}
+static void Ogg_ReadOpusHeader( logical_stream_t *p_stream,
+ ogg_packet *p_oggpacket )
+{
+ oggpack_buffer opb;
+
+ p_stream->fmt.i_cat = AUDIO_ES;
+ p_stream->fmt.i_codec = VLC_CODEC_OPUS;
+
+ /* Signal that we want to keep a backup of the opus
+ * stream headers. They will be used when switching between
+ * audio streams. */
+ p_stream->b_force_backup = 1;
+
+ /* All OggOpus streams are timestamped at 48kHz and
+ * can be played at 48kHz. */
+ p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
+ p_stream->fmt.i_bitrate = 0;
+
+ /* Cheat and get additionnal info ;) */
+ oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
+ oggpack_adv( &opb, 64 );
+ oggpack_adv( &opb, 8 ); /* version_id */
+ p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
+ p_stream->i_pre_skip = oggpack_read( &opb, 16 );
+}
+
static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
ogg_packet *p_oggpacket )
{
diff --git a/modules/demux/ogg.h b/modules/demux/ogg.h
index 3ad627d..6dae0a4 100644
--- a/modules/demux/ogg.h
+++ b/modules/demux/ogg.h
@@ -65,6 +65,8 @@ typedef struct logical_stream_s
/* Misc */
bool b_reinit;
int i_granule_shift;
+ /* Opus has a starting offset in the headers. */
+ int i_pre_skip;
/* offset of first keyframe for theora; can be 0 or 1 depending on version number */
int64_t i_keyframe_offset;
--
1.7.10.4
More information about the vlc-devel
mailing list