]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/encoder.c
Plugins: push cancellation down
[vlc] / modules / codec / avcodec / encoder.c
index 23c2674465ef3a08507432b61864e87f8c713f54..98089fbe13f1f1098742930ec3b08b5a35cd0bbc 100644 (file)
@@ -69,7 +69,7 @@ static block_t *EncodeVideo( encoder_t *, picture_t * );
 static block_t *EncodeAudio( encoder_t *, aout_buffer_t * );
 
 struct thread_context_t;
-static int FfmpegThread( struct thread_context_t *p_context );
+static void* FfmpegThread( vlc_object_t *p_this );
 static int FfmpegExecute( AVCodecContext *s,
                           int (*pf_func)(AVCodecContext *c2, void *arg2),
                           void **arg, int *ret, int count );
@@ -131,7 +131,6 @@ struct encoder_sys_t
     int        i_qmin;
     int        i_qmax;
     int        i_hq;
-    bool       b_strict_rc;
     int        i_rc_buffer_size;
     float      f_rc_buffer_aggressivity;
     bool       b_pre_me;
@@ -153,7 +152,7 @@ struct encoder_sys_t
 };
 
 static const char *const ppsz_enc_options[] = {
-    "keyint", "bframes", "vt", "qmin", "qmax", "hq", "strict-rc",
+    "keyint", "bframes", "vt", "qmin", "qmax", "hq",
     "rc-buffer-size", "rc-buffer-aggressivity", "pre-me", "hurry-up",
     "interlace", "i-quant-factor", "noise-reduction", "mpeg4-matrix",
     "trellis", "qscale", "strict", "lumi-masking", "dark-masking",
@@ -213,7 +212,7 @@ int OpenEncoder( vlc_object_t *p_this )
     if( !GetFfmpegCodec( p_enc->fmt_out.i_codec, &i_cat, &i_codec_id,
                              &psz_namecodec ) )
     {
-        if( GetFfmpegChroma( p_enc->fmt_out.i_codec ) < 0 )
+        if( TestFfmpegChroma( -1, p_enc->fmt_out.i_codec ) != VLC_SUCCESS )
         {
             /* handed chroma output */
             return VLC_EGENERIC;
@@ -318,8 +317,6 @@ int OpenEncoder( vlc_object_t *p_this )
         p_sys->i_noise_reduction = 1;
     }
 
-    var_Get( p_enc, ENC_CFG_PREFIX "strict-rc", &val );
-    p_sys->b_strict_rc = val.b_bool;
     var_Get( p_enc, ENC_CFG_PREFIX "rc-buffer-size", &val );
     p_sys->i_rc_buffer_size = val.i_int;
     var_Get( p_enc, ENC_CFG_PREFIX "rc-buffer-aggressivity", &val );
@@ -381,7 +378,7 @@ int OpenEncoder( vlc_object_t *p_this )
 
 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(40<<8)+4)
     var_Get( p_enc, ENC_CFG_PREFIX "aac-profile", &val );
-    p_sys->i_aac_profile = FF_PROFILE_UNKNOWN;
+    p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
     if( val.psz_string && *val.psz_string )
     {
         if( !strncmp( val.psz_string, "main", 4 ) )
@@ -396,8 +393,8 @@ int OpenEncoder( vlc_object_t *p_this )
             p_sys->i_aac_profile = FF_PROFILE_AAC_LTP;
         else
         {
-            msg_Warn( p_enc, "unknown AAC profile requested" );
-            p_sys->i_aac_profile = FF_PROFILE_UNKNOWN;
+            msg_Warn( p_enc, "unknown AAC profile requested, setting it to low" );
+            p_sys->i_aac_profile = FF_PROFILE_AAC_LOW;
         }
     }
     free( val.psz_string );
@@ -463,7 +460,9 @@ int OpenEncoder( vlc_object_t *p_this )
         p_sys->p_buffer_out = malloc( p_sys->i_buffer_out );
 
         p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
-        p_context->pix_fmt = GetFfmpegChroma( p_enc->fmt_in.i_codec );
+        p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec;
+        GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
+
         if( p_codec->pix_fmts )
         {
             const enum PixelFormat *p = p_codec->pix_fmts;
@@ -472,20 +471,10 @@ int OpenEncoder( vlc_object_t *p_this )
                 if( *p == p_context->pix_fmt ) break;
             }
             if( *p == -1 ) p_context->pix_fmt = p_codec->pix_fmts[0];
-            p_enc->fmt_in.i_codec = GetVlcChroma( p_context->pix_fmt );
+            GetVlcChroma( &p_enc->fmt_in.video, p_context->pix_fmt );
+            p_enc->fmt_in.i_codec = p_enc->fmt_in.video.i_chroma;
         }
 
-        if ( p_sys->b_strict_rc )
-        {
-            p_context->rc_qsquish = 1.0;
-            p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
-            p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
-            p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
-            /* This is from ffmpeg's ffmpeg.c : */
-            p_context->rc_initial_buffer_occupancy
-                = p_sys->i_rc_buffer_size * 3/4;
-            p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
-        }
 
         if ( p_sys->f_i_quant_factor != 0.0 )
             p_context->i_quant_factor = p_sys->f_i_quant_factor;
@@ -561,6 +550,17 @@ int OpenEncoder( vlc_object_t *p_this )
             p_context->flags |= CODEC_FLAG_QSCALE;
             p_context->global_quality = p_sys->i_quality;
         }
+        else
+        {
+            p_context->rc_qsquish = 1.0;
+            p_context->rc_max_rate = p_enc->fmt_out.i_bitrate;
+            p_context->rc_min_rate = p_enc->fmt_out.i_bitrate;
+            p_context->rc_buffer_size = p_sys->i_rc_buffer_size;
+            /* This is from ffmpeg's ffmpeg.c : */
+            p_context->rc_initial_buffer_occupancy
+                = p_sys->i_rc_buffer_size * 3/4;
+            p_context->rc_buffer_aggressivity = p_sys->f_rc_buffer_aggressivity;
+        }
     }
     else if( p_enc->fmt_in.i_cat == AUDIO_ES )
     {
@@ -575,8 +575,8 @@ int OpenEncoder( vlc_object_t *p_this )
         if ( p_enc->fmt_out.i_codec == VLC_FOURCC('m','p','4','a') )
         {
             /* XXX: FAAC does resample only when setting the INPUT samplerate
-             * to the desired value (-R option of the faac frontend) */
-            p_enc->fmt_in.audio.i_rate = p_context->sample_rate;
+             * to the desired value (-R option of the faac frontend)
+            p_enc->fmt_in.audio.i_rate = p_context->sample_rate;*/
 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(40<<8)+4)
         /* Ignore FF_PROFILE_UNKNOWN */
         if( p_sys->i_aac_profile >= FF_PROFILE_AAC_MAIN )
@@ -591,8 +591,8 @@ int OpenEncoder( vlc_object_t *p_this )
     if( i_codec_id == CODEC_ID_RAWVIDEO )
     {
         /* XXX: hack: Force same codec (will be handled by transcode) */
-        p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
-        p_context->pix_fmt = GetFfmpegChroma( p_enc->fmt_in.i_codec );
+        p_enc->fmt_in.video.i_chroma = p_enc->fmt_in.i_codec = p_enc->fmt_out.i_codec;
+        GetFfmpegChroma( &p_context->pix_fmt, p_enc->fmt_in.video );
     }
 
     /* Make sure we get extradata filled by the encoder */
@@ -703,18 +703,20 @@ int OpenEncoder( vlc_object_t *p_this )
 /****************************************************************************
  * Ffmpeg threading system
  ****************************************************************************/
-static int FfmpegThread( struct thread_context_t *p_context )
+static void* FfmpegThread( vlc_object_t *p_this )
 {
-    while ( !p_context->b_die && !p_context->b_error )
+    struct thread_context_t *p_context = (struct thread_context_t *)p_this;
+    int canc = vlc_savecancel ();
+    while ( vlc_object_alive (p_context) && !p_context->b_error )
     {
         vlc_mutex_lock( &p_context->lock );
-        while ( !p_context->b_work && !p_context->b_die && !p_context->b_error )
+        while ( !p_context->b_work && vlc_object_alive (p_context) && !p_context->b_error )
         {
             vlc_cond_wait( &p_context->cond, &p_context->lock );
         }
         p_context->b_work = 0;
         vlc_mutex_unlock( &p_context->lock );
-        if ( p_context->b_die || p_context->b_error )
+        if ( !vlc_object_alive (p_context) || p_context->b_error )
             break;
 
         if ( p_context->pf_func )
@@ -729,7 +731,8 @@ static int FfmpegThread( struct thread_context_t *p_context )
         vlc_mutex_unlock( &p_context->lock );
     }
 
-    return 0;
+    vlc_restorecancel (canc);
+    return NULL;
 }
 
 static int FfmpegExecute( AVCodecContext *s,