]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/copy.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / packetizer / copy.c
index c0cd614cab1afd7637e02cf249d3246dbe795439..dbaa96b321c033f1850019817af4db1fe7d1d7bf 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * copy.c
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: copy.c,v 1.1 2002/12/14 21:32:41 fenrir Exp $
+ * Copyright (C) 2001, 2002, 2006 the VideoLAN team
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Eric Petit <titer@videolan.org>
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
-#include <vlc/aout.h>
-#include <vlc/decoder.h>
-#include <vlc/input.h>
-#include <vlc/sout.h>
 
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>                                              /* strdup() */
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
+#include <vlc_block.h>
+#include <vlc_bits.h>
 
 /*****************************************************************************
- * Local prototypes
+ * Module descriptor
  *****************************************************************************/
-typedef struct packetizer_thread_s
-{
-    /* Input properties */
-    decoder_fifo_t          *p_fifo;
-
-    /* Output properties */
-    sout_input_t            *p_sout_input;
-    sout_packet_format_t    output_format;
-
-} packetizer_thread_t;
-
-static int  Open    ( vlc_object_t * );
-static int  Run     ( decoder_fifo_t * );
+static int  Open ( vlc_object_t * );
+static void Close( vlc_object_t * );
 
-static int  InitThread     ( packetizer_thread_t * );
-static void PacketizeThread   ( packetizer_thread_t * );
-static void EndThread      ( packetizer_thread_t * );
+vlc_module_begin ()
+    set_category( CAT_SOUT )
+    set_subcategory( SUBCAT_SOUT_PACKETIZER )
+    set_description( N_("Copy packetizer") )
+    set_capability( "packetizer", 1 )
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 /*****************************************************************************
- * Module descriptor
+ * Local prototypes
  *****************************************************************************/
+struct decoder_sys_t
+{
+    block_t *p_block;
+    void (*pf_parse)( decoder_t *, block_t * );
+};
 
-vlc_module_begin();
-    set_description( _("Copy packetizer") );
-    set_capability( "packetizer", 0 );
-    set_callbacks( Open, NULL );
-vlc_module_end();
+static block_t *Packetize   ( decoder_t *, block_t ** );
+static block_t *PacketizeSub( decoder_t *, block_t ** );
 
+static void ParseWMV3( decoder_t *, block_t * );
 
 /*****************************************************************************
- * OpenDecoder: probe the packetizer and return score
+ * Open: probe the packetizer and return score
  *****************************************************************************
  * Tries to launch a decoder and return score so that the interface is able
  * to choose.
  *****************************************************************************/
 static int Open( vlc_object_t *p_this )
 {
-    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+    decoder_t     *p_dec = (decoder_t*)p_this;
+    decoder_sys_t *p_sys;
 
-    p_fifo->pf_run = Run;
+    if( p_dec->fmt_in.i_cat != AUDIO_ES &&
+        p_dec->fmt_in.i_cat != VIDEO_ES &&
+        p_dec->fmt_in.i_cat != SPU_ES )
+    {
+        msg_Err( p_dec, "invalid ES type" );
+        return VLC_EGENERIC;
+    }
 
-    return VLC_SUCCESS;
+    if( p_dec->fmt_in.i_cat == SPU_ES )
+        p_dec->pf_packetize = PacketizeSub;
+    else
+        p_dec->pf_packetize = Packetize;
 
-#if 0
-    if( p_fifo->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'a') )
-        ....
-#endif
+    /* Create the output format */
+    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
+
+    /* Fix the value of the fourcc for audio */
+    if( p_dec->fmt_in.i_cat == AUDIO_ES )
+    {
+        p_dec->fmt_out.i_codec = vlc_fourcc_GetCodecAudio( p_dec->fmt_in.i_codec,
+                                                           p_dec->fmt_in.audio.i_bitspersample );
+        if( !p_dec->fmt_out.i_codec )
+        {
+            msg_Err( p_dec, "unknown raw audio sample size" );
+            return VLC_EGENERIC;
+        }
+    }
+
+    p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
+    p_sys->p_block    = NULL;
+    switch( p_dec->fmt_in.i_codec )
+    {
+    case VLC_CODEC_WMV3:
+        p_sys->pf_parse = ParseWMV3;
+        break;
+    default:
+        p_sys->pf_parse = NULL;
+        break;
+    }
+
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * RunDecoder: this function is called just after the thread is created
+ * Close:
  *****************************************************************************/
-static int Run( decoder_fifo_t *p_fifo )
+static void Close( vlc_object_t *p_this )
 {
-    packetizer_thread_t *p_pack;
-    int b_error;
+    decoder_t     *p_dec = (decoder_t*)p_this;
 
-    msg_Info( p_fifo, "Running copy packetizer" );
-    if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
+    if( p_dec->p_sys->p_block )
     {
-        msg_Err( p_fifo, "out of memory" );
-        DecoderError( p_fifo );
-        return( -1 );
+        block_ChainRelease( p_dec->p_sys->p_block );
     }
-    memset( p_pack, 0, sizeof( packetizer_thread_t ) );
 
-    p_pack->p_fifo = p_fifo;
+    es_format_Clean( &p_dec->fmt_out );
+    free( p_dec->p_sys );
+}
+
+/*****************************************************************************
+ * Packetize: packetize an unit (here copy a complete block )
+ *****************************************************************************/
+static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
+{
+    block_t *p_block;
+    block_t *p_ret = p_dec->p_sys->p_block;
 
-    if( InitThread( p_pack ) != 0 )
+    if( pp_block == NULL || *pp_block == NULL )
+        return NULL;
+    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
     {
-        DecoderError( p_fifo );
-        return( -1 );
+        block_Release( *pp_block );
+        return NULL;
     }
 
-    while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
+    p_block = *pp_block;
+    *pp_block = NULL;
+
+    if( p_block->i_dts <= VLC_TS_INVALID )
     {
-        PacketizeThread( p_pack );
+        p_block->i_dts = p_block->i_pts;
     }
 
-
-    if( ( b_error = p_pack->p_fifo->b_error ) )
+    if( p_block->i_dts <= VLC_TS_INVALID )
     {
-        DecoderError( p_pack->p_fifo );
+        msg_Dbg( p_dec, "need valid dts" );
+        block_Release( p_block );
+        return NULL;
     }
 
-    EndThread( p_pack );
-    if( b_error )
+    if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
     {
-        return( -1 );
+        p_ret->i_length = p_block->i_pts - p_ret->i_pts;
     }
+    p_dec->p_sys->p_block = p_block;
 
-    return( 0 );
+    if( p_ret && p_dec->p_sys->pf_parse )
+        p_dec->p_sys->pf_parse( p_dec, p_ret );
+    return p_ret;
 }
 
-
-#define FREE( p ) if( p ) free( p ); p = NULL
-
 /*****************************************************************************
- * InitThread: initialize data before entering main loop
+ * PacketizeSub: packetize an unit (here copy a complete block )
  *****************************************************************************/
-
-static int InitThread( packetizer_thread_t *p_pack )
+static block_t *PacketizeSub( decoder_t *p_dec, block_t **pp_block )
 {
+    block_t *p_block;
 
-//    p_pack->output_format.i_cat = p_pack->p_fifo->i_cat;
-    p_pack->output_format.i_fourcc = p_pack->p_fifo->i_fourcc;
-
-    p_pack->p_sout_input = 
-        sout_InputNew( p_pack->p_fifo,
-                       &p_pack->output_format );
-
-    if( !p_pack->p_sout_input )
+    if( pp_block == NULL || *pp_block == NULL )
+        return NULL;
+    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
     {
-        msg_Err( p_pack->p_fifo, 
-                 "cannot add a new stream" );
-        return( -1 );
+        block_Release( *pp_block );
+        return NULL;
     }
 
-    return( 0 );
-}
+    p_block = *pp_block;
+    *pp_block = NULL;
 
-/*****************************************************************************
- * PacketizeThread: packetize an unit (here copy a complete pes)
- *****************************************************************************/
-static void PacketizeThread( packetizer_thread_t *p_pack )
-{
-    sout_buffer_t   *p_sout_buffer;
-    pes_packet_t    *p_pes;
-    size_t          i_size;
-
-    /* **** get samples count **** */
-    input_ExtractPES( p_pack->p_fifo, &p_pes );
-    if( !p_pes )
+    if( p_block->i_dts <= VLC_TS_INVALID )
     {
-        p_pack->p_fifo->b_error = 1;
-        return;
+        p_block->i_dts = p_block->i_pts;
     }
-    i_size = p_pes->i_pes_size;
 
-    if( i_size > 0 )
+    if( p_block->i_dts <= VLC_TS_INVALID )
     {
-        data_packet_t   *p_data;
-        size_t          i_buffer;
-
-        p_sout_buffer = 
-            sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
-        if( !p_sout_buffer )
-        {
-            p_pack->p_fifo->b_error = 1;
-            return;
-        }
-        /* TODO: memcpy of the pes packet */
-        for( i_buffer = 0, p_data = p_pes->p_first;
-             p_data != NULL && i_buffer < i_size;
-             p_data = p_data->p_next)
-        {
-            size_t          i_copy;
-
-            i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, 
-                            i_size - i_buffer );
-            if( i_copy > 0 )
-            {
-                p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
-                                                  p_data->p_payload_start,
-                                                  i_copy );
-            }
-            i_buffer += i_copy;
-        }
-
-        sout_InputSendBuffer( p_pack->p_sout_input,
-                               p_sout_buffer );
+        msg_Dbg( p_dec, "need valid dts" );
+        block_Release( p_block );
+        return NULL;
     }
 
-    input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
+    return p_block;
 }
 
-
-/*****************************************************************************
- * EndThread : packetizer thread destruction
- *****************************************************************************/
-static void EndThread ( packetizer_thread_t *p_pack)
+/* Parse WMV3 packet and extract frame type information */
+static void ParseWMV3( decoder_t *p_dec, block_t *p_block )
 {
-    if( p_pack->p_sout_input )
-    {
-        sout_InputDelete( p_pack->p_sout_input );
-    }
+    bs_t s;
+
+    /* Parse Sequence header */
+    bs_init( &s, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
+    if( bs_read( &s, 2 ) == 3 )
+        return;
+    bs_skip( &s, 22 );
+    const bool b_range_reduction = bs_read( &s, 1 );
+    const bool b_has_frames = bs_read( &s, 3 ) > 0;
+    bs_skip( &s, 2 );
+    const bool b_frame_interpolation = bs_read( &s, 1 );
+    if( bs_eof( &s ) )
+        return;
+
+    /* Parse frame type */
+    bs_init( &s, p_block->p_buffer, p_block->i_buffer );
+    bs_skip( &s, b_frame_interpolation +
+                 2 +
+                 b_range_reduction );
+
+    p_block->i_flags &= ~BLOCK_FLAG_TYPE_MASK;
+    if( bs_read( &s, 1 ) )
+        p_block->i_flags |= BLOCK_FLAG_TYPE_P;
+    else if( !b_has_frames || bs_read( &s, 1 ) )
+        p_block->i_flags |= BLOCK_FLAG_TYPE_I;
+    else
+        p_block->i_flags |= BLOCK_FLAG_TYPE_B;
 }