[vlc-devel] [PATCH 2/3] Introduce new codec module to decode dirac video via libschroedinger
jrosser
jonathan.rosser at gmail.com
Thu Jun 26 19:03:35 CEST 2008
---
configure.ac | 14 ++
modules/codec/Modules.am | 1 +
modules/codec/schroedinger.c | 439 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 454 insertions(+), 0 deletions(-)
create mode 100644 modules/codec/schroedinger.c
diff --git a/configure.ac b/configure.ac
index 8295e82..5be84bf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3581,6 +3581,20 @@ if test "${enable_dirac}" = "yes"; then
fi
dnl
+dnl schroedinger decoder plugin (for dirac format video)
+dnl
+AC_ARG_ENABLE(schroedinger,
+[ --enable-schroedinger high performance dirac codec (default disabled)])
+if test "${enable_schroedinger}" = "yes"; then
+ PKG_CHECK_MODULES(SCHROEDINGER,[schroedinger-1.0 >= 1.0], [
+ VLC_ADD_PLUGIN([schroedinger])
+ VLC_ADD_CFLAGS([schroedinger],[$SCHROEDINGER_CFLAGS])
+ VLC_ADD_LIBS([schroedinger],[$SCHROEDINGER_LIBS -lstdc++]) ],[
+ AC_MSG_ERROR([libschroedinger doesn't appear to be installed on you system.])
+ ])
+fi
+
+dnl
dnl PNG decoder module
dnl
AC_ARG_ENABLE(png,
diff --git a/modules/codec/Modules.am b/modules/codec/Modules.am
index 7b530e5..5a4c2e0 100644
--- a/modules/codec/Modules.am
+++ b/modules/codec/Modules.am
@@ -36,3 +36,4 @@ SOURCES_cdg = cdg.c
SOURCES_fluidsynth = fluidsynth.c
SOURCES_cc = cc.c cc.h
SOURCES_kate = kate.c
+SOURCES_schroedinger = schroedinger.c
diff --git a/modules/codec/schroedinger.c b/modules/codec/schroedinger.c
new file mode 100644
index 0000000..a143f48
--- /dev/null
+++ b/modules/codec/schroedinger.c
@@ -0,0 +1,439 @@
+/*****************************************************************************
+ * schroedinger.c: Dirac decoder module making use of libschroedinger.
+ * (http://www.bbc.co.uk/rd/projects/dirac/index.shtml)
+ * (http://diracvideo.org)
+ *****************************************************************************
+ * Copyright (C) 1999-2001 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Jonathan Rosser <jonathan.rosser at gmail.com>
+ *
+ * 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.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
+#include <vlc_sout.h>
+#include <vlc_vout.h>
+
+#include <schroedinger/schro.h>
+
+/*****************************************************************************
+ * picture_pts_t : store pts alongside picture number, not carried through
+ * decoder
+ *****************************************************************************/
+struct picture_pts_t
+{
+ uint32_t u_pnum; //picture number from dirac header
+ mtime_t i_pts; //pts for this picture
+ int i_empty; //not in use
+};
+
+/*****************************************************************************
+ * decoder_sys_t : Schroedinger decoder descriptor
+ *****************************************************************************/
+#define PTS_TLB_SIZE 16
+struct decoder_sys_t
+{
+ /*
+ * Dirac properties
+ */
+ SchroDecoder *p_schro;
+ SchroVideoFormat *p_format;
+ struct picture_pts_t pts_tlb[PTS_TLB_SIZE];
+ uint32_t b_discontinuity;
+};
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int OpenDecoder ( vlc_object_t * );
+static void CloseDecoder ( vlc_object_t * );
+static picture_t *DecodeBlock ( decoder_t *p_dec, block_t **pp_block );
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+
+vlc_module_begin();
+ set_category( CAT_INPUT );
+ set_subcategory( SUBCAT_INPUT_VCODEC );
+ set_description( N_("Schroedinger video decoder") );
+ set_capability( "decoder", 100 );
+ set_callbacks( OpenDecoder, CloseDecoder );
+ add_shortcut( "schroedinger" );
+vlc_module_end();
+
+/*****************************************************************************
+ * 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;
+ SchroDecoder *p_schro;
+
+ if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','r','a','c') )
+ {
+ return VLC_EGENERIC;
+ }
+
+ /* Initialise the schroedinger decoder */
+ schro_init();
+ if( !(p_schro = schro_decoder_new()) ) return VLC_EGENERIC;
+
+ /* Allocate the memory needed to store the decoder's structure */
+ if( ( p_dec->p_sys = p_sys =
+ (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
+ return VLC_ENOMEM;
+
+ p_sys->p_schro = p_schro;
+ p_sys->p_format = NULL;
+ p_sys->b_discontinuity = 1;
+
+ for( int i=0; i<PTS_TLB_SIZE; i++) {
+ p_sys->pts_tlb[i].i_empty = 1;
+ }
+
+ /* Set output properties */
+ p_dec->fmt_out.i_cat = VIDEO_ES;
+ p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
+
+ /* Set callbacks */
+ p_dec->pf_decode_video = DecodeBlock;
+
+ return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * SetPictureFormat: Set the decoded picture params to the ones from the stream
+ *****************************************************************************/
+//FIXME - return type?
+static void SetVideoFormat( decoder_t *p_dec)
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ double f_aspect;
+
+ p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
+ if( p_sys->p_format == NULL ) return;
+
+ switch( p_sys->p_format->chroma_format )
+ {
+ case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0'); break;
+ case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','2'); break;
+ case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','4','4'); break;
+ default:
+ p_dec->fmt_out.i_codec = 0;
+ break;
+ }
+
+ p_dec->fmt_out.video.i_visible_width =
+ p_dec->fmt_out.video.i_width = p_sys->p_format->width;
+
+ p_dec->fmt_out.video.i_visible_height =
+ p_dec->fmt_out.video.i_height = p_sys->p_format->height;
+
+ /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
+ f_aspect = (double)
+ ( p_sys->p_format->aspect_ratio_numerator * p_sys->p_format->width ) /
+ ( p_sys->p_format->aspect_ratio_denominator * p_sys->p_format->height);
+
+ p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * f_aspect;
+
+ p_dec->fmt_out.video.i_frame_rate =
+ p_sys->p_format->frame_rate_numerator;
+ p_dec->fmt_out.video.i_frame_rate_base =
+ p_sys->p_format->frame_rate_denominator;
+}
+
+/*****************************************************************************
+ * StorePicturePTS: Store the PTS value for a particular picture number
+ *****************************************************************************/
+static void StorePicturePTS( decoder_t *p_dec, block_t *p_block)
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ uint32_t u_pnum;
+ mtime_t i_pts;
+
+ u_pnum = p_block->p_buffer[13] << 24;
+ u_pnum += p_block->p_buffer[14] << 16;
+ u_pnum += p_block->p_buffer[15] << 8;
+ u_pnum += p_block->p_buffer[16];
+
+ i_pts = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
+
+ for( int i=0; i<PTS_TLB_SIZE; i++ ) {
+ if( p_sys->pts_tlb[i].i_empty ) {
+
+ p_sys->pts_tlb[i].u_pnum = u_pnum;
+ p_sys->pts_tlb[i].i_pts = i_pts;
+ p_sys->pts_tlb[i].i_empty = 0;
+
+ return;
+ }
+ }
+
+ msg_Err( p_dec, "Could not store PTS %lld for picture %u", i_pts, u_pnum );
+}
+
+/*****************************************************************************
+ * GetPicturePTS: Retrieve the PTS value for a particular picture number
+ *****************************************************************************/
+static mtime_t GetPicturePTS( decoder_t *p_dec, uint32_t u_pnum )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ mtime_t i_pts = 0;
+
+ for( int i=0; i<PTS_TLB_SIZE; i++ ) {
+ if( (!p_sys->pts_tlb[i].i_empty) &&
+ (p_sys->pts_tlb[i].u_pnum == u_pnum)) {
+
+ p_sys->pts_tlb[i].i_empty = 1;
+ return p_sys->pts_tlb[i].i_pts;
+ }
+ }
+
+ msg_Err( p_dec, "Could not retrieve PTS for picture %u", u_pnum );
+ return 0;
+}
+
+/*****************************************************************************
+ * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
+ *****************************************************************************/
+static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ SchroFrame *p_schroframe = schro_frame_new();
+ picture_t *p_pic = p_dec->pf_vout_buffer_new( p_dec );
+
+ if( !p_schroframe )
+ return NULL;
+
+ if( !p_pic )
+ return NULL;
+
+ p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
+ if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
+ {
+ p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
+ }
+ else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
+ {
+ p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
+ }
+
+ p_schroframe->width = p_sys->p_format->width;
+ p_schroframe->height = p_sys->p_format->height;
+ p_schroframe->priv = p_pic;
+
+ for( int i=0; i<3; i++ )
+ {
+ p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
+ p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
+ p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
+ p_schroframe->components[i].length =
+ p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
+ p_schroframe->components[i].data = p_pic->p[i].p_pixels;
+
+ if(i!=0)
+ {
+ p_schroframe->components[i].v_shift =
+ SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
+ p_schroframe->components[i].h_shift =
+ SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
+ }
+ }
+
+ return p_schroframe;
+}
+
+/*****************************************************************************
+ * SchroBufferFree: schro_buffer callback to release the associated block_t
+ * FIXME - this does not work yet
+ *****************************************************************************/
+static void SchroBufferFree( SchroBuffer *buf, void *priv)
+{
+ block_t *p_block = priv;
+
+ if( !p_block )
+ return;
+
+ p_block->i_buffer = 0;
+ block_Release( priv );
+}
+
+/*****************************************************************************
+ * CloseDecoder: 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;
+
+ schro_decoder_free( p_sys->p_schro );
+ free( p_sys );
+}
+
+/****************************************************************************
+ * DecodeBlock: the whole thing
+ ****************************************************************************
+ * This function must be fed with complete frames.
+ ****************************************************************************/
+static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
+{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ int state;
+ SchroBuffer *p_schrobuffer;
+ SchroFrame *p_schroframe;
+ picture_t *p_pic;
+ block_t *p_block;
+ uint32_t u_pnum;
+ uint32_t i_buffer_pushed = 0;
+
+ if( !pp_block || !*pp_block ) return NULL;
+
+ p_block = *pp_block;
+
+ /* reset the decoder when seeking as the decode in progress is invalid */
+ if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) {
+ msg_Dbg( p_dec, "SCHRO_DECODER_RESET" );
+ schro_decoder_reset( p_sys->p_schro );
+
+ for( int i=0; i<PTS_TLB_SIZE; i++ )
+ p_sys->pts_tlb[i].i_empty = 1;
+
+ p_sys->b_discontinuity = 1;
+ }
+
+ if( !p_block->i_buffer )
+ {
+ block_Release( p_block );
+ return NULL;
+ }
+
+ /* blocks that do not start with the parse info prefix are invalid */
+ if( p_block->p_buffer[0] != 'B' || p_block->p_buffer[1] != 'B' ||
+ p_block->p_buffer[2] != 'C' || p_block->p_buffer[3] != 'D')
+ return NULL;
+
+ /* after a discontinuity, decoding can only commence at sequence headers */
+ if( p_sys->b_discontinuity )
+ {
+ if( p_block->p_buffer[4] == 0x00 )
+ p_sys->b_discontinuity = 0;
+ else
+ {
+ p_block->i_buffer = 0;
+ block_Release( p_block );
+ return NULL;
+ }
+ }
+
+ while( 1 )
+ {
+ state = schro_decoder_wait( p_sys->p_schro );
+
+ switch( state )
+ {
+
+ case SCHRO_DECODER_NEED_BITS:
+
+ if( i_buffer_pushed )
+ {
+ block_Release( p_block );
+ return NULL;
+ }
+
+ msg_Dbg( p_dec, "SCHRO_DECODER_NEED_BITS len=%d pts=%lld",
+ (uint32_t)p_block->i_buffer,
+ p_block->i_pts);
+
+ /* for each parse unit that is a picture, store the PTS */
+ if( p_block->p_buffer[4] & 0x08 )
+ StorePicturePTS( p_dec, p_block );
+#if 0
+ /* attempt to wrap the block_t in a schrobuffer */
+ /* i don't know how to properly take ownership of the block_t */
+ /* and release it when done, so this does not yet work */
+ p_schrobuffer = schro_buffer_new_with_data( p_block->p_buffer,
+ p_block->i_buffer);
+ p_schrobuffer->free = SchroBufferFree;
+ p_schrobuffer->priv = p_block;
+#else
+ p_schrobuffer = schro_buffer_new_and_alloc( p_block->i_buffer );
+ vlc_memcpy( p_schrobuffer->data, p_block->p_buffer, p_block->i_buffer );
+ p_block->i_buffer = 0;
+#endif
+ state = schro_decoder_push( p_sys->p_schro, p_schrobuffer );
+ i_buffer_pushed = 1;
+
+
+ if( state == SCHRO_DECODER_FIRST_ACCESS_UNIT )
+ {
+ msg_Dbg( p_dec, "SCHRO_DECODER_FIRST_ACCESS_UNIT");
+ SetVideoFormat( p_dec );
+ }
+
+ break;
+
+ case SCHRO_DECODER_NEED_FRAME:
+ msg_Dbg( p_dec, "SCHRO_DECODER_NEED_FRAME" );
+ p_schroframe = CreateSchroFrameFromPic( p_dec );
+
+ if( !p_schroframe )
+ {
+ msg_Dbg( p_dec, "Could not allocate picture for decoder");
+ return NULL;
+ }
+
+ schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
+ break;
+
+ case SCHRO_DECODER_OK:
+ u_pnum = schro_decoder_get_picture_number( p_sys->p_schro );
+ p_schroframe = schro_decoder_pull( p_sys->p_schro );
+ p_pic = p_schroframe->priv;
+ schro_frame_unref( p_schroframe );
+ p_pic->date = GetPicturePTS( p_dec, u_pnum );
+ msg_Dbg( p_dec, "SCHRO_DECODER_OK num=%u date=%lld", u_pnum, p_pic->date);
+ //don't set this, as we have a pts on each picture
+ //p_pic->b_force = 1; // HACK
+ return p_pic;
+ break;
+
+ case SCHRO_DECODER_EOS:
+ msg_Dbg( p_dec, "SCHRO_DECODER_EOS");
+ break;
+
+ case SCHRO_DECODER_ERROR:
+ msg_Dbg( p_dec, "SCHRO_DECODER_ERROR");
+ return NULL;
+ break;
+
+ }
+ }
+
+ /* Never reached */
+ return NULL;
+}
+
--
1.5.4.3
More information about the vlc-devel
mailing list