]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/avcodec.c
avcodec: adjust MT contention scope
[vlc] / modules / codec / avcodec / avcodec.c
index d79d6b0c79424ba82cdb47ba5c967a67135a34ca..04ade540e98b99b708bdd180c17cecabe0acc8cf 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * avcodec.c: video and audio decoder and encoder using libavcodec
  *****************************************************************************
- * Copyright (C) 1999-2008 the VideoLAN team
+ * Copyright (C) 1999-2008 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Gildas Bazin <gbazin@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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser 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.
+ * You should have received a copy of the GNU Lesser 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.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <vlc_avcodec.h>
 #include <vlc_cpu.h>
 
-/* ffmpeg header */
 #define HAVE_MMX 1
-#ifdef HAVE_LIBAVCODEC_AVCODEC_H
-#   include <libavcodec/avcodec.h>
-#else
-#   include <avcodec.h>
-#endif
+#include <libavcodec/avcodec.h>
 
 #include "avcodec.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
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 53, 34, 0 )
+#   error You must update libavcodec to a version >= 53.34.0
+#elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 54, 25, 0 )
+#   warning You should update libavcodec to a version >= 54.25.0
 #endif
 
 /*****************************************************************************
@@ -70,12 +67,6 @@ 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[] = {
@@ -130,9 +121,6 @@ vlc_module_begin ()
     add_integer ( "avcodec-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
         true )
     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_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 */
@@ -140,26 +128,23 @@ vlc_module_begin ()
                   SKIPLOOPF_LONGTEXT, false)
         change_safe ()
         change_integer_list( nloopf_list, nloopf_list_text )
+#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 54, 41, 0 )
+    add_bool( "avcodec-ignorecrop", false, IGNORECROP_TEXT, IGNORECROP_LONGTEXT,
+        true )
+#endif
 
     add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
     add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_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", false, 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
+    add_module( "avcodec-hw", "hw decoder", "none", HW_TEXT, HW_LONGTEXT, false )
 #if defined(FF_THREAD_FRAME)
     add_obsolete_integer( "ffmpeg-threads" ) /* removed since 2.1.0 */
     add_integer( "avcodec-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
 #endif
+    add_string( "avcodec-options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
 
 
 #ifdef ENABLE_SOUT
@@ -201,9 +186,9 @@ vlc_module_begin ()
 
 
     add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
-    add_string( ENC_CFG_PREFIX "hq", "simple", ENC_HQ_TEXT,
+    add_string( ENC_CFG_PREFIX "hq", "rd", 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,
@@ -255,14 +240,9 @@ vlc_module_begin ()
     /* Audio AAC encoder profile */
     add_string( ENC_CFG_PREFIX "aac-profile", "low",
                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
-#endif /* ENABLE_SOUT */
 
-    /* video filter submodule */
-    add_submodule ()
-    set_capability( "video filter2", 0 )
-    set_callbacks( OpenDeinterlace, CloseDeinterlace )
-    set_description( N_("FFmpeg deinterlace video filter") )
-    add_shortcut( "ffmpeg-deinterlace" )
+    add_string( ENC_CFG_PREFIX "options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
+#endif /* ENABLE_SOUT */
 
 #ifdef MERGE_FFMPEG
     add_submodule ()
@@ -318,45 +298,17 @@ 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, "avcodec-debug" );
     p_context->opaque = (void *)p_this;
 
-    /* Set CPU capabilities */
-    p_context->dsp_mask = 0;
-#if defined (__i386__) || defined (__x86_64__)
-    if( !vlc_CPU_MMX() )
-        p_context->dsp_mask |= AV_CPU_FLAG_MMX;
-    if( !vlc_CPU_MMXEXT() )
-        p_context->dsp_mask |= AV_CPU_FLAG_MMX2;
-    if( !vlc_CPU_3dNOW() )
-        p_context->dsp_mask |= AV_CPU_FLAG_3DNOW;
-    if( !vlc_CPU_SSE() )
-        p_context->dsp_mask |= AV_CPU_FLAG_SSE;
-    if( !vlc_CPU_SSE2() )
-        p_context->dsp_mask |= AV_CPU_FLAG_SSE2;
-# ifdef AV_CPU_FLAG_SSE3
-    if( !vlc_CPU_SSE3() )
-        p_context->dsp_mask |= AV_CPU_FLAG_SSE3;
-# endif
-# ifdef AV_CPU_FLAG_SSSE3
-    if( !vlc_CPU_SSE3() )
-        p_context->dsp_mask |= AV_CPU_FLAG_SSSE3;
-# endif
-# ifdef AV_CPU_FLAG_SSE4
-    if( !vlc_CPU_SSE4_1() )
-        p_context->dsp_mask |= AV_CPU_FLAG_SSE4;
-# endif
-# ifdef AV_CPU_FLAG_SSE42
-    if( !vlc_CPU_SSE4_2() )
-        p_context->dsp_mask |= AV_CPU_FLAG_SSE42;
-# endif
+    /* set CPU capabilities */
+#if LIBAVUTIL_VERSION_CHECK(51, 25, 0, 42, 100)
+    av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
+#else
+    p_context->dsp_mask = GetVlcDspMask();
 #endif
 
     p_dec->b_need_packetized = true;
@@ -403,15 +355,9 @@ static void CloseDecoder( vlc_object_t *p_this )
 
     switch( p_sys->i_cat )
     {
-    case AUDIO_ES:
-         EndAudioDec ( p_dec );
-        break;
     case VIDEO_ES:
          EndVideoDec ( p_dec );
         break;
-    case SPU_ES:
-         EndSubtitleDec( p_dec );
-        break;
     }
 
     if( p_sys->p_context )
@@ -441,10 +387,10 @@ int ffmpeg_OpenCodec( decoder_t *p_dec )
 
     if( p_sys->p_context->extradata_size <= 0 )
     {
-        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 &&
+        if( p_sys->i_codec_id == AV_CODEC_ID_VC1 ||
+            p_sys->i_codec_id == AV_CODEC_ID_VORBIS ||
+            p_sys->i_codec_id == AV_CODEC_ID_THEORA ||
+            ( p_sys->i_codec_id == AV_CODEC_ID_AAC &&
               !p_dec->fmt_in.b_packetized ) )
         {
             msg_Warn( p_dec, "waiting for extra data for codec %s",
@@ -466,23 +412,32 @@ 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 &&
+        if( p_sys->i_codec_id == AV_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;
+    char *psz_opts = var_InheritString( p_dec, "avcodec-options" );
+    AVDictionary *options = NULL;
+    if (psz_opts && *psz_opts)
+        options = vlc_av_get_options(psz_opts);
+    free(psz_opts);
+
     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
+    ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, options ? &options : NULL );
     vlc_avcodec_unlock();
+
+    AVDictionaryEntry *t = NULL;
+    while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
+        msg_Err( p_dec, "Unknown option \"%s\"", t->key );
+    }
+    av_dict_free(&options);
+
     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 )