]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/avcodec.c
avcodec: make vlc_va_t a VLC object
[vlc] / modules / codec / avcodec / avcodec.c
index 632938ed7becbf8ae51e40c57231acde0dff330e..2cd356e73c4ea4bdba708d9bcee86524ecec92c2 100644 (file)
 #include <vlc_avcodec.h>
 #include <vlc_cpu.h>
 
-/* ffmpeg header */
 #define HAVE_MMX 1
-#ifdef HAVE_LIBAVCODEC_AVCODEC_H
-#   include <libavcodec/avcodec.h>
-#elif defined(HAVE_FFMPEG_AVCODEC_H)
-#   include <ffmpeg/avcodec.h>
-#else
-#   include <avcodec.h>
-#endif
+#include <libavcodec/avcodec.h>
 
 #include "avcodec.h"
-#include "avutil.h"
 #include "chroma.h"
+#include "avcommon.h"
 
 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
 #   error You must update libavcodec to a version >= 52.25.0
@@ -59,7 +52,7 @@
 struct decoder_sys_t
 {
     /* Common part between video and audio decoder */
-    FFMPEG_COMMON_MEMBERS
+    AVCODEC_COMMON_MEMBERS
 };
 
 /****************************************************************************
@@ -72,6 +65,12 @@ static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
 static const char *const nloopf_list_text[] =
   { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
 
+#if defined(HAVE_AVCODEC_VDA)
+static const int  nvda_pix_fmt_list[] = { 0, 1 };
+static const char *const nvda_pix_fmt_list_text[] =
+  { N_("420YpCbCr8Planar"), N_("422YpCbCr8") };
+#endif
+
 #ifdef ENABLE_SOUT
 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
 static const char *const enc_hq_list_text[] = {
@@ -96,51 +95,71 @@ vlc_module_begin ()
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_VCODEC )
     /* decoder main module */
-#if defined(MODULE_NAME_is_ffmpegaltivec) \
-     || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
-    set_description( N_("AltiVec FFmpeg audio/video decoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") )
-    /*add_requirement( ALTIVEC )*/
-    set_capability( "decoder", 71 )
-#else
     set_description( N_("FFmpeg audio/video decoder") )
     set_help( MODULE_DESCRIPTION )
     set_capability( "decoder", 70 )
-#endif
     set_section( N_("Decoding") , NULL )
     set_callbacks( OpenDecoder, CloseDecoder )
 
 
-    add_bool( "ffmpeg-dr", true, DR_TEXT, DR_TEXT, true )
-    add_integer ( "ffmpeg-error-resilience", 1, ERROR_TEXT,
+    add_obsolete_bool( "ffmpeg-dr" ) /* removed since 2.1.0 */
+    add_bool( "avcodec-dr", true, DR_TEXT, DR_TEXT, true )
+    add_obsolete_integer ( "ffmpeg-error-resilience" ) /* removed since 2.1.0 */
+    add_integer ( "avcodec-error-resilience", 1, ERROR_TEXT,
         ERROR_LONGTEXT, true )
-    add_integer ( "ffmpeg-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
+    add_obsolete_integer ( "ffmpeg-workaround-bugs" ) /* removed since 2.1.0 */
+    add_integer ( "avcodec-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
         false )
-    add_bool( "ffmpeg-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
+    add_obsolete_bool( "ffmpeg-hurry-up" ) /* removed since 2.1.0 */
+    add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
         false )
-    add_integer( "ffmpeg-skip-frame", 0, SKIP_FRAME_TEXT,
+    add_obsolete_integer( "ffmpeg-skip-frame") /* removed since 2.1.0 */
+    add_integer( "avcodec-skip-frame", 0, SKIP_FRAME_TEXT,
         SKIP_FRAME_LONGTEXT, true )
         change_integer_range( -1, 4 )
-    add_integer( "ffmpeg-skip-idct", 0, SKIP_IDCT_TEXT,
+    add_obsolete_integer( "ffmpeg-skip-idct" ) /* removed since 2.1.0 */
+    add_integer( "avcodec-skip-idct", 0, SKIP_IDCT_TEXT,
         SKIP_IDCT_LONGTEXT, true )
         change_integer_range( -1, 4 )
-    add_integer ( "ffmpeg-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
+    add_obsolete_integer ( "ffmpeg-vismv" ) /* removed since 2.1.0 */
+    add_integer ( "avcodec-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
         true )
-    add_integer ( "ffmpeg-lowres", 0, LOWRES_TEXT, LOWRES_LONGTEXT,
+    add_obsolete_integer ( "ffmpeg-lowres" ) /* removed since 2.1.0 */
+    add_integer ( "avcodec-lowres", 0, LOWRES_TEXT, LOWRES_LONGTEXT,
         true )
         change_integer_range( 0, 2 )
-    add_bool( "ffmpeg-fast", false, FAST_TEXT, FAST_LONGTEXT, true )
-    add_integer ( "ffmpeg-skiploopfilter", 0, SKIPLOOPF_TEXT,
-                  SKIPLOOPF_LONGTEXT, true )
+    add_obsolete_bool( "ffmpeg-fast" ) /* removed since 2.1.0 */
+    add_bool( "avcodec-fast", false, FAST_TEXT, FAST_LONGTEXT, false )
+    add_obsolete_integer ( "ffmpeg-skiploopfilter" ) /* removed since 2.1.0 */
+    add_integer ( "avcodec-skiploopfilter", 0, SKIPLOOPF_TEXT,
+                  SKIPLOOPF_LONGTEXT, false)
         change_safe ()
         change_integer_list( nloopf_list, nloopf_list_text )
 
-    add_integer( "ffmpeg-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
+    add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
+    add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
                  true )
-#if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2)
-    add_bool( "ffmpeg-hw", false, HW_TEXT, HW_LONGTEXT, true )
+    add_obsolete_string( "ffmpeg-codec" ) /* removed since 2.1.0 */
+    add_string( "avcodec-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
+#if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2) || defined(HAVE_AVCODEC_VDA)
+    add_obsolete_bool( "ffmpeg-hw" ) /* removed since 2.1.0 */
+    add_bool( "avcodec-hw",
+#if !defined(HAVE_AVCODEC_VDA)
+    false
+#else
+    true
+#endif
+    , HW_TEXT, HW_LONGTEXT, false )
+#if defined(HAVE_AVCODEC_VDA)
+    add_integer ( "avcodec-vda-pix-fmt", 0, VDA_PIX_FMT_TEXT,
+                  VDA_PIX_FMT_LONGTEXT, false)
+        change_safe ()
+        change_integer_list( nvda_pix_fmt_list, nvda_pix_fmt_list_text )
+#endif
 #endif
 #if defined(FF_THREAD_FRAME)
-    add_integer( "ffmpeg-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
+    add_obsolete_integer( "ffmpeg-threads" ) /* removed since 2.1.0 */
+    add_integer( "avcodec-threads", 1, THREADS_TEXT, THREADS_LONGTEXT, true );
 #endif
 
 
@@ -153,9 +172,39 @@ vlc_module_begin ()
     set_capability( "encoder", 100 )
     set_callbacks( OpenEncoder, CloseEncoder )
 
+    /* removed in 2.1.0 */
+    add_obsolete_string( "sout-ffmpeg-codec" )
+    add_obsolete_string( "sout-ffmpeg-hq" )
+    add_obsolete_integer( "sout-ffmpeg-keyint" )
+    add_obsolete_integer( "sout-ffmpeg-bframes" )
+    add_obsolete_bool( "sout-ffmpeg-hurry-up" )
+    add_obsolete_bool( "sout-ffmpeg-interlace" )
+    add_obsolete_bool( "sout-ffmpeg-interlace-me" )
+    add_obsolete_integer( "sout-ffmpeg-vt" )
+    add_obsolete_bool( "sout-ffmpeg-pre-me" )
+    add_obsolete_integer( "sout-ffmpeg-rc-buffer-size" )
+    add_obsolete_float( "sout-ffmpeg-rc-buffer-aggressivity" )
+    add_obsolete_float( "sout-ffmpeg-i-quant-factor" )
+    add_obsolete_integer( "sout-ffmpeg-noise-reduction" )
+    add_obsolete_bool( "sout-ffmpeg-mpeg4-matrix" )
+    add_obsolete_integer( "sout-ffmpeg-qmin" )
+    add_obsolete_integer( "sout-ffmpeg-qmax" )
+    add_obsolete_bool( "sout-ffmpeg-trellis" )
+    add_obsolete_float( "sout-ffmpeg-qscale" )
+    add_obsolete_integer( "sout-ffmpeg-strict" )
+    add_obsolete_float( "sout-ffmpeg-lumi-masking" )
+    add_obsolete_float( "sout-ffmpeg-dark-masking" )
+    add_obsolete_float( "sout-ffmpeg-p-masking" )
+    add_obsolete_float( "sout-ffmpeg-border-masking" )
+    add_obsolete_integer( "sout-ffmpeg-luma-elim-threshold" )
+    add_obsolete_integer( "sout-ffmpeg-chroma-elim-threshold" )
+    add_obsolete_string( "sout-ffmpeg-aac-profile" )
+
+
+    add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
     add_string( ENC_CFG_PREFIX "hq", "simple", ENC_HQ_TEXT,
                 ENC_HQ_LONGTEXT, false )
-        change_string_list( enc_hq_list, enc_hq_list_text, 0 )
+        change_string_list( enc_hq_list, enc_hq_list_text )
     add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
                  ENC_KEYINT_LONGTEXT, false )
     add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
@@ -170,7 +219,7 @@ vlc_module_begin ()
                  ENC_VT_LONGTEXT, true )
     add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
               ENC_PRE_ME_LONGTEXT, true )
-    add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8,
+    add_integer( ENC_CFG_PREFIX "rc-buffer-size", 0,
                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
@@ -190,6 +239,7 @@ vlc_module_begin ()
                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
     add_integer( ENC_CFG_PREFIX "strict", 0,
                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
+        change_integer_range( -2, 2 )
     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
     add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
@@ -243,10 +293,25 @@ static int OpenDecoder( vlc_object_t *p_this )
     }
 
     /* Initialization must be done before avcodec_find_decoder() */
-    InitLibavcodec(p_this);
+    vlc_init_avcodec();
 
     /* *** ask ffmpeg for a decoder *** */
-    p_codec = avcodec_find_decoder( i_codec_id );
+    char *psz_decoder = var_CreateGetString( p_this, "avcodec-codec" );
+    if( psz_decoder && *psz_decoder )
+    {
+        p_codec = avcodec_find_decoder_by_name( psz_decoder );
+        if( !p_codec )
+            msg_Err( p_this, "Decoder `%s' not found", psz_decoder );
+        else if( p_codec->id != i_codec_id )
+        {
+            msg_Err( p_this, "Decoder `%s' can't handle %4.4s",
+                    psz_decoder, (char*)&p_dec->fmt_in.i_codec );
+            p_codec = NULL;
+        }
+    }
+    free( psz_decoder );
+    if( !p_codec )
+        p_codec = avcodec_find_decoder( i_codec_id );
     if( !p_codec )
     {
         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
@@ -254,50 +319,21 @@ static int OpenDecoder( vlc_object_t *p_this )
     }
 
     /* *** get a p_context *** */
+#if LIBAVCODEC_VERSION_MAJOR >= 54
+    p_context = avcodec_alloc_context3(p_codec);
+#else
     p_context = avcodec_alloc_context();
+#endif
     if( !p_context )
         return VLC_ENOMEM;
-    p_context->debug = var_InheritInteger( p_dec, "ffmpeg-debug" );
+    p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
     p_context->opaque = (void *)p_this;
 
-    /* Set CPU capabilities */
-    unsigned i_cpu = vlc_CPU();
-    p_context->dsp_mask = 0;
-    if( !(i_cpu & CPU_CAPABILITY_MMX) )
-    {
-        p_context->dsp_mask |= FF_MM_MMX;
-    }
-    if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
-    {
-        p_context->dsp_mask |= FF_MM_MMXEXT;
-    }
-    if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
-    {
-        p_context->dsp_mask |= FF_MM_3DNOW;
-    }
-    if( !(i_cpu & CPU_CAPABILITY_SSE) )
-    {
-        p_context->dsp_mask |= FF_MM_SSE;
-    }
-    if( !(i_cpu & CPU_CAPABILITY_SSE2) )
-    {
-        p_context->dsp_mask |= FF_MM_SSE2;
-    }
-#ifdef FF_MM_SSE3
-    if( !(i_cpu & CPU_CAPABILITY_SSE3) )
-        p_context->dsp_mask |= FF_MM_SSE3;
-#endif
-#ifdef FF_MM_SSSE3
-    if( !(i_cpu & CPU_CAPABILITY_SSSE3) )
-        p_context->dsp_mask |= FF_MM_SSSE3;
-#endif
-#ifdef FF_MM_SSE4
-    if( !(i_cpu & CPU_CAPABILITY_SSE4_1) )
-        p_context->dsp_mask |= FF_MM_SSE4;
-#endif
-#ifdef FF_MM_SSE42
-    if( !(i_cpu & CPU_CAPABILITY_SSE4_2) )
-        p_context->dsp_mask |= FF_MM_SSE42;
+    /* set CPU capabilities */
+#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT( 51, 25, 0 )
+    av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
+#else
+    p_context->dsp_mask = GetVlcDspMask();
 #endif
 
     p_dec->b_need_packetized = true;
@@ -373,31 +409,6 @@ static void CloseDecoder( vlc_object_t *p_this )
     free( p_sys );
 }
 
-void InitLibavcodec( vlc_object_t *p_object )
-{
-    static bool b_ffmpeginit = false;
-
-    vlc_avcodec_lock();
-
-    /* *** init ffmpeg library (libavcodec) *** */
-    if( !b_ffmpeginit )
-    {
-        avcodec_init();
-        avcodec_register_all();
-        av_log_set_callback( LibavutilCallback );
-        b_ffmpeginit = true;
-
-        msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
-                 LIBAVCODEC_VERSION_INT );
-    }
-    else
-    {
-        msg_Dbg( p_object, "libavcodec already initialized" );
-    }
-
-    vlc_avcodec_unlock();
-}
-
 /*****************************************************************************
  * ffmpeg_OpenCodec:
  *****************************************************************************/
@@ -410,7 +421,8 @@ int ffmpeg_OpenCodec( decoder_t *p_dec )
         if( p_sys->i_codec_id == CODEC_ID_VC1 ||
             p_sys->i_codec_id == CODEC_ID_VORBIS ||
             p_sys->i_codec_id == CODEC_ID_THEORA ||
-            p_sys->i_codec_id == CODEC_ID_AAC )
+            ( p_sys->i_codec_id == CODEC_ID_AAC &&
+              !p_dec->fmt_in.b_packetized ) )
         {
             msg_Warn( p_dec, "waiting for extra data for codec %s",
                       p_sys->psz_namecodec );
@@ -431,14 +443,23 @@ int ffmpeg_OpenCodec( decoder_t *p_dec )
         p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
         p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
         p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
+        if( p_sys->i_codec_id == CODEC_ID_ADPCM_G726 &&
+            p_sys->p_context->bit_rate > 0 &&
+            p_sys->p_context->sample_rate >  0)
+            p_sys->p_context->bits_per_coded_sample = p_sys->p_context->bit_rate /
+                                                      p_sys->p_context->sample_rate;
     }
     int ret;
     vlc_avcodec_lock();
+#if LIBAVCODEC_VERSION_MAJOR >= 54
+    ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, NULL /* options */ );
+#else
     ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
+#endif
     vlc_avcodec_unlock();
     if( ret < 0 )
         return VLC_EGENERIC;
-    msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
+    msg_Dbg( p_dec, "avcodec codec (%s) started", p_sys->psz_namecodec );
 
 #ifdef HAVE_AVCODEC_MT
     if( p_dec->fmt_in.i_cat == VIDEO_ES )