]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/video.c
Let swscale handle video conversion when available (close #1695)
[vlc] / modules / codec / avcodec / video.c
index 12391a04384c220c04b5b52af7abdcefef9c37a5..8a8e1f88647448b0c62d68fb534f5456a04d7803 100644 (file)
@@ -45,6 +45,7 @@
 #endif
 
 #include "avcodec.h"
+#include "chroma.h"
 
 /*****************************************************************************
  * decoder_sys_t : decoder descriptor
@@ -80,12 +81,6 @@ struct decoder_sys_t
 
     int i_buffer_orig, i_buffer;
     char *p_buffer_orig, *p_buffer;
-
-    /* Postprocessing handle */
-    void *p_pp;
-    bool b_pp;
-    bool b_pp_async;
-    bool b_pp_init;
 };
 
 /* FIXME (dummy palette for now) */
@@ -97,6 +92,7 @@ static AVPaletteControl palette_control;
 static void ffmpeg_InitCodec      ( decoder_t * );
 static void ffmpeg_CopyPicture    ( decoder_t *, picture_t *, AVFrame * );
 static int  ffmpeg_GetFrameBuf    ( struct AVCodecContext *, AVFrame * );
+static int  ffmpeg_ReGetFrameBuf( struct AVCodecContext *, AVFrame * );
 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
 
 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
@@ -155,9 +151,11 @@ static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
     case PIX_FMT_GRAY8:
         return VLC_FOURCC('G','R','E','Y');
 
-    case PIX_FMT_YUV410P:
-    case PIX_FMT_YUV411P:
     default:
+#if defined(HAVE_LIBSWSCALE_SWSCALE_H)  || defined(HAVE_FFMPEG_SWSCALE_H)
+        if( GetVlcChroma( i_ff_chroma ) )
+            return GetVlcChroma( i_ff_chroma );
+#endif
         return 0;
     }
 }
@@ -166,7 +164,6 @@ static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
                                             AVCodecContext *p_context )
 {
-    decoder_sys_t *p_sys = p_dec->p_sys;
     picture_t *p_pic;
 
     p_dec->fmt_out.video.i_width = p_context->width;
@@ -220,13 +217,6 @@ static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
 
     p_pic = p_dec->pf_vout_buffer_new( p_dec );
 
-// FIXME    if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
-// FIXME    {
-// FIXME        InitPostproc( p_sys->p_pp, p_context->width,
-// FIXME                          p_context->height, p_context->pix_fmt );
-// FIXME        p_sys->b_pp_init = true;
-// FIXME    }
-
     return p_pic;
 }
 
@@ -246,7 +236,6 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
     if( ( p_dec->p_sys = p_sys =
           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
     {
-        msg_Err( p_dec, "out of memory" );
         return VLC_ENOMEM;
     }
     memset( p_sys, 0, sizeof(decoder_sys_t) );
@@ -360,10 +349,6 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
         p_sys->b_direct_rendering = 1;
     }
 
-    p_sys->p_pp = NULL;
-    p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = false;
-    // FIXME p_sys->p_pp = OpenPostproc( p_dec, &p_sys->b_pp_async );
-
     /* ffmpeg doesn't properly release old pictures when frames are skipped */
     //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
     if( p_sys->b_direct_rendering )
@@ -375,6 +360,7 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
     /* Always use our get_buffer wrapper so we can calculate the
      * PTS correctly */
     p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
+    p_sys->p_context->reget_buffer = ffmpeg_ReGetFrameBuf;
     p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
     p_sys->p_context->opaque = p_dec;
 
@@ -390,6 +376,11 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
     p_sys->i_buffer = 0;
     p_sys->i_buffer_orig = 1;
     p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
+    if( !p_sys->p_buffer_orig )
+    {
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = VIDEO_ES;
@@ -399,13 +390,14 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
     if( p_dec->fmt_in.video.p_palette )
         p_sys->p_context->palctrl =
             (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
-    else
+    else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
         p_sys->p_context->palctrl = &palette_control;
 
     /* ***** Open the codec ***** */
     vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
     if( lock == NULL )
     {
+        free( p_sys->p_buffer_orig );
         free( p_sys );
         return VLC_ENOMEM;
     }
@@ -414,6 +406,7 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
     {
         vlc_mutex_unlock( lock );
         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
+        free( p_sys->p_buffer_orig );
         free( p_sys );
         return VLC_EGENERIC;
     }
@@ -530,9 +523,6 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
      * Do the actual decoding now
      */
 
-    /* Check if post-processing was enabled */
-    p_sys->b_pp = p_sys->b_pp_async;
-
     /* Don't forget that ffmpeg requires a little more bytes
      * that the real frame size */
     if( p_block->i_buffer > 0 )
@@ -548,6 +538,11 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         }
         p_sys->p_buffer = p_sys->p_buffer_orig;
         p_sys->i_buffer = p_block->i_buffer;
+        if( !p_sys->p_buffer )
+        {
+            block_Release( p_block );
+            return NULL;
+        }
         vlc_memcpy( p_sys->p_buffer, p_block->p_buffer, p_block->i_buffer );
         memset( p_sys->p_buffer + p_block->i_buffer, 0,
                 FF_INPUT_BUFFER_PADDING_SIZE );
@@ -725,7 +720,6 @@ void EndVideoDec( decoder_t *p_dec )
     decoder_sys_t *p_sys = p_dec->p_sys;
 
     if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
-    // FIXME ClosePostproc( p_dec, p_sys->p_pp );
     free( p_sys->p_buffer_orig );
 }
 
@@ -746,6 +740,8 @@ static void ffmpeg_InitCodec( decoder_t *p_dec )
         p_sys->p_context->extradata_size = i_size + 12;
         p = p_sys->p_context->extradata  =
             malloc( p_sys->p_context->extradata_size );
+        if( !p )
+            return;
 
         memcpy( &p[0],  "SVQ3", 4 );
         memset( &p[4], 0, 8 );
@@ -784,13 +780,15 @@ static void ffmpeg_InitCodec( decoder_t *p_dec )
         {
             p_sys->p_context->extradata_size = 8;
             p_sys->p_context->extradata = malloc( 8 );
+            if( p_sys->p_context->extradata )
+            {
+                memcpy( p_sys->p_context->extradata,
+                        p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
+                p_sys->p_context->sub_id = ((uint32_t*)p_dec->fmt_in.p_extra)[1];
 
-            memcpy( p_sys->p_context->extradata,
-                    p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
-            p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];
-
-            msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
-                      p_sys->p_context->sub_id );
+                msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
+                          p_sys->p_context->sub_id );
+            }
         }
     }
     else
@@ -798,10 +796,13 @@ static void ffmpeg_InitCodec( decoder_t *p_dec )
         p_sys->p_context->extradata_size = i_size;
         p_sys->p_context->extradata =
             malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
-        memcpy( p_sys->p_context->extradata,
-                p_dec->fmt_in.p_extra, i_size );
-        memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
-                0, FF_INPUT_BUFFER_PADDING_SIZE );
+        if( p_sys->p_context->extradata )
+        {
+            memcpy( p_sys->p_context->extradata,
+                    p_dec->fmt_in.p_extra, i_size );
+            memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
+                    0, FF_INPUT_BUFFER_PADDING_SIZE );
+        }
     }
 }
 
@@ -820,25 +821,20 @@ static void ffmpeg_CopyPicture( decoder_t *p_dec,
         uint8_t *p_dst, *p_src;
         int i_src_stride, i_dst_stride;
 
-        // FIXME if( p_sys->p_pp && p_sys->b_pp )
-        // FIXME    PostprocPict( p_sys->p_pp, p_pic, p_ff_pic );
-        // FIXME else
+        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
         {
-            for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+            p_src  = p_ff_pic->data[i_plane];
+            p_dst = p_pic->p[i_plane].p_pixels;
+            i_src_stride = p_ff_pic->linesize[i_plane];
+            i_dst_stride = p_pic->p[i_plane].i_pitch;
+
+            i_size = __MIN( i_src_stride, i_dst_stride );
+            for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
+                 i_line++ )
             {
-                p_src  = p_ff_pic->data[i_plane];
-                p_dst = p_pic->p[i_plane].p_pixels;
-                i_src_stride = p_ff_pic->linesize[i_plane];
-                i_dst_stride = p_pic->p[i_plane].i_pitch;
-
-                i_size = __MIN( i_src_stride, i_dst_stride );
-                for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
-                     i_line++ )
-                {
-                    vlc_memcpy( p_dst, p_src, i_size );
-                    p_src += i_src_stride;
-                    p_dst += i_dst_stride;
-                }
+                vlc_memcpy( p_dst, p_src, i_size );
+                p_src += i_src_stride;
+                p_dst += i_dst_stride;
             }
         }
     }
@@ -893,6 +889,8 @@ static void ffmpeg_CopyPicture( decoder_t *p_dec,
  * It is used for direct rendering as well as to get the right PTS for each
  * decoded picture (even in indirect rendering mode).
  *****************************************************************************/
+static void ffmpeg_SetFrameBufferPts( decoder_t *p_dec, AVFrame *p_ff_pic );
+
 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
                                AVFrame *p_ff_pic )
 {
@@ -901,32 +899,13 @@ static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
     picture_t *p_pic;
 
     /* Set picture PTS */
-    if( p_sys->input_pts )
-    {
-        p_ff_pic->pts = p_sys->input_pts;
-    }
-    else if( p_sys->input_dts )
-    {
-        /* Some demuxers only set the dts so let's try to find a useful
-         * timestamp from this */
-        if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
-            !p_ff_pic->reference || !p_sys->i_pts )
-        {
-            p_ff_pic->pts = p_sys->input_dts;
-        }
-        else p_ff_pic->pts = 0;
-    }
-    else p_ff_pic->pts = 0;
-
-    if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
-    {
-        p_sys->input_pts = p_sys->input_dts = 0;
-    }
+    ffmpeg_SetFrameBufferPts( p_dec, p_ff_pic );
 
+    /* */
     p_ff_pic->opaque = 0;
 
     /* Not much to do in indirect rendering mode */
-    if( !p_sys->b_direct_rendering || p_sys->b_pp )
+    if( !p_sys->b_direct_rendering )
     {
         return avcodec_default_get_buffer( p_context, p_ff_pic );
     }
@@ -973,6 +952,44 @@ static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
 
     return 0;
 }
+static int  ffmpeg_ReGetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
+{
+    decoder_t *p_dec = (decoder_t *)p_context->opaque;
+
+    /* Set picture PTS */
+    ffmpeg_SetFrameBufferPts( p_dec, p_ff_pic );
+
+    /* We always use default reget function, it works perfectly fine */
+    return avcodec_default_reget_buffer( p_context, p_ff_pic );
+}
+
+static void ffmpeg_SetFrameBufferPts( decoder_t *p_dec, AVFrame *p_ff_pic )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+
+    /* Set picture PTS */
+    if( p_sys->input_pts )
+    {
+        p_ff_pic->pts = p_sys->input_pts;
+    }
+    else if( p_sys->input_dts )
+    {
+        /* Some demuxers only set the dts so let's try to find a useful
+         * timestamp from this */
+        if( !p_sys->p_context->has_b_frames || !p_sys->b_has_b_frames ||
+            !p_ff_pic->reference || !p_sys->i_pts )
+        {
+            p_ff_pic->pts = p_sys->input_dts;
+        }
+        else p_ff_pic->pts = 0;
+    }
+    else p_ff_pic->pts = 0;
+
+    if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
+    {
+        p_sys->input_pts = p_sys->input_dts = 0;
+    }
+}
 
 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
                                     AVFrame *p_ff_pic )