]> git.sesse.net Git - vlc/blobdiff - modules/codec/avcodec/video.c
Added BLOCK_FLAG_END_OF_SEQUENCE and use it for mpeg2.
[vlc] / modules / codec / avcodec / video.c
index 8a8e1f88647448b0c62d68fb534f5456a04d7803..496f6eb80d8bb1c902fa80e1692cf0a46610c413 100644 (file)
@@ -81,10 +81,14 @@ struct decoder_sys_t
 
     int i_buffer_orig, i_buffer;
     char *p_buffer_orig, *p_buffer;
+
+
+    /* */
+    bool b_flush;
 };
 
 /* FIXME (dummy palette for now) */
-static AVPaletteControl palette_control;
+static const AVPaletteControl palette_control;
 
 /*****************************************************************************
  * Local prototypes
@@ -94,6 +98,7 @@ 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 void ffmpeg_NextPts( decoder_t *, int i_block_rate );
 
 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
 {
@@ -104,61 +109,6 @@ static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
 /*****************************************************************************
  * Local Functions
  *****************************************************************************/
-static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
-{
-    switch( i_ff_chroma )
-    {
-    case PIX_FMT_YUV420P:
-    case PIX_FMT_YUVJ420P: /* Hacky but better then chroma conversion */
-        return VLC_FOURCC('I','4','2','0');
-    case PIX_FMT_YUV422P:
-    case PIX_FMT_YUVJ422P: /* Hacky but better then chroma conversion */
-        return VLC_FOURCC('I','4','2','2');
-    case PIX_FMT_YUV444P:
-    case PIX_FMT_YUVJ444P: /* Hacky but better then chroma conversion */
-        return VLC_FOURCC('I','4','4','4');
-
-    case PIX_FMT_YUV422:
-        return VLC_FOURCC('Y','U','Y','2');
-
-#if defined(WORDS_BIGENDIAN)
-    case PIX_FMT_BGR8:
-        return VLC_FOURCC('R','G','B','8');
-    case PIX_FMT_BGR555:
-        return VLC_FOURCC('R','V','1','5');
-    case PIX_FMT_BGR565:
-        return VLC_FOURCC('R','V','1','6');
-    case PIX_FMT_BGR24:
-        return VLC_FOURCC('R','V','2','4');
-#else
-#if defined(PIX_FMT_RGB8)
-    case PIX_FMT_RGB8:
-        return VLC_FOURCC('R','G','B','8');
-#endif
-    case PIX_FMT_RGB555:
-        return VLC_FOURCC('R','V','1','5');
-    case PIX_FMT_RGB565:
-        return VLC_FOURCC('R','V','1','6');
-    case PIX_FMT_RGB24:
-        return VLC_FOURCC('R','V','2','4');
-#endif
-    case PIX_FMT_RGBA32:
-        return VLC_FOURCC('R','V','3','2');
-#ifdef PIX_FMT_RGBA
-    case PIX_FMT_RGBA:
-        return VLC_FOURCC('R','G','B','A');
-#endif
-    case PIX_FMT_GRAY8:
-        return VLC_FOURCC('G','R','E','Y');
-
-    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;
-    }
-}
 
 /* Returns a new picture buffer */
 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
@@ -168,18 +118,18 @@ static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
 
     p_dec->fmt_out.video.i_width = p_context->width;
     p_dec->fmt_out.video.i_height = p_context->height;
-    p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
 
     if( !p_context->width || !p_context->height )
     {
         return NULL; /* invalid display size */
     }
 
-    if( !p_dec->fmt_out.i_codec )
+    if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
     {
-        /* we make conversion if possible*/
+        /* we are doomed, but not really, because most codecs set their pix_fmt much later */
         p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
     }
+    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
 
     /* If an aspect-ratio was specified in the input format then force it */
     if( p_dec->fmt_in.video.i_aspect )
@@ -372,6 +322,7 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
     p_sys->i_pts = 0;
     p_sys->b_has_b_frames = false;
     p_sys->b_first_frame = true;
+    p_sys->b_flush = false;
     p_sys->i_late_frames = 0;
     p_sys->i_buffer = 0;
     p_sys->i_buffer_orig = 1;
@@ -384,14 +335,19 @@ int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
 
     /* Set output properties */
     p_dec->fmt_out.i_cat = VIDEO_ES;
-    p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
+    if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
+    {
+        /* we are doomed. but not really, because most codecs set their pix_fmt later on */
+        p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
+    }
+    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
 
     /* Setup palette */
     if( p_dec->fmt_in.video.p_palette )
         p_sys->p_context->palctrl =
             (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
     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;
+        p_sys->p_context->palctrl = (AVPaletteControl *) &palette_control;
 
     /* ***** Open the codec ***** */
     vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
@@ -486,7 +442,7 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         (p_sys->i_late_frames > 4) )
     {
         b_drawpicture = 0;
-        if( p_sys->i_late_frames < 8 )
+        if( p_sys->i_late_frames < 12 )
         {
             p_sys->p_context->skip_frame =
                     (p_sys->i_skip_frame <= AVDISCARD_BIDIR) ?
@@ -527,6 +483,8 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
      * that the real frame size */
     if( p_block->i_buffer > 0 )
     {
+        p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
+
         p_sys->i_buffer = p_block->i_buffer;
         if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
             p_sys->i_buffer_orig )
@@ -550,16 +508,18 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         p_block->i_buffer = 0;
     }
 
-    while( p_sys->i_buffer > 0 )
+    while( p_sys->i_buffer > 0 || p_sys->b_flush )
     {
         int i_used, b_gotpicture;
         picture_t *p_pic;
 
         i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
                                        &b_gotpicture,
-                                       (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
+                                       p_sys->i_buffer <= 0 && p_sys->b_flush ? NULL : (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
+
         if( b_null_size && p_sys->p_context->width > 0 &&
-            p_sys->p_context->height > 0 )
+            p_sys->p_context->height > 0 &&
+            !p_sys->b_flush )
         {
             /* Reparse it to not drop the I frame */
             b_null_size = false;
@@ -570,6 +530,12 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
                                            (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
         }
 
+        if( p_sys->b_flush )
+            p_sys->b_first_frame = true;
+
+        if( p_sys->i_buffer <= 0 )
+            p_sys->b_flush = false;
+
         if( i_used < 0 )
         {
             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
@@ -586,6 +552,8 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         p_sys->i_buffer -= i_used;
         p_sys->p_buffer += i_used;
 
+    p_sys->b_first_frame = true;
+
         /* Nothing to display */
         if( !b_gotpicture )
         {
@@ -593,6 +561,10 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
             continue;
         }
 
+        /* Set the PTS */
+        if( p_sys->p_ff_pic->pts )
+            p_sys->i_pts = p_sys->p_ff_pic->pts;
+
         /* Update frame late count (except when doing preroll) */
         if( p_sys->i_pts && decoder_GetDisplayDate(p_dec, p_sys->i_pts) <= mdate() &&
             !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
@@ -612,6 +584,8 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
             if( !b_drawpicture && p_pic )
                 p_dec->pf_vout_buffer_del( p_dec, p_pic );
+
+            ffmpeg_NextPts( p_dec, p_block->i_rate );
             continue;
         }
 
@@ -634,9 +608,6 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
         }
 
-        /* Set the PTS */
-        if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
-
         /* Sanity check (seems to be needed for some streams) */
         if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
         {
@@ -667,24 +638,7 @@ picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
         {
             p_pic->date = p_sys->i_pts;
 
-            /* interpolate the next PTS */
-            if( p_dec->fmt_in.video.i_frame_rate > 0 &&
-                p_dec->fmt_in.video.i_frame_rate_base > 0 )
-            {
-                p_sys->i_pts += INT64_C(1000000) *
-                    (2 + p_sys->p_ff_pic->repeat_pict) *
-                    p_dec->fmt_in.video.i_frame_rate_base *
-                    p_block->i_rate / INPUT_RATE_DEFAULT /
-                    (2 * p_dec->fmt_in.video.i_frame_rate);
-            }
-            else if( p_sys->p_context->time_base.den > 0 )
-            {
-                p_sys->i_pts += INT64_C(1000000) *
-                    (2 + p_sys->p_ff_pic->repeat_pict) *
-                    p_sys->p_context->time_base.num *
-                    p_block->i_rate / INPUT_RATE_DEFAULT /
-                    (2 * p_sys->p_context->time_base.den);
-            }
+            ffmpeg_NextPts( p_dec, p_block->i_rate );
 
             if( p_sys->b_first_frame )
             {
@@ -815,7 +769,7 @@ static void ffmpeg_CopyPicture( decoder_t *p_dec,
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-    if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
+    if( TestFfmpegChroma( p_sys->p_context->pix_fmt, -1 ) == VLC_SUCCESS )
     {
         int i_plane, i_size, i_line;
         uint8_t *p_dst, *p_src;
@@ -840,46 +794,9 @@ static void ffmpeg_CopyPicture( decoder_t *p_dec,
     }
     else
     {
-        AVPicture dest_pic;
-        int i;
-
-        /* we need to convert to I420 */
-        switch( p_sys->p_context->pix_fmt )
-        {
-        case PIX_FMT_YUV410P:
-        case PIX_FMT_YUV411P:
-        case PIX_FMT_RGB32:
-        case PIX_FMT_RGB24:
-#if defined(PIX_FMT_RGB8)
-        case PIX_FMT_RGB8:
-#endif
-#if defined(PIX_FMT_BRG32)
-        case PIX_FMT_BGR32:
-#endif
-        case PIX_FMT_BGR24:
-#if defined(PIX_FMT_BGR8)
-        case PIX_FMT_BGR8:
-#endif
-        case PIX_FMT_PAL8:
-            for( i = 0; i < p_pic->i_planes; i++ )
-            {
-                dest_pic.data[i] = p_pic->p[i].p_pixels;
-                dest_pic.linesize[i] = p_pic->p[i].i_pitch;
-            }
-#if !defined(HAVE_LIBSWSCALE_SWSCALE_H)  && !defined(HAVE_FFMPEG_SWSCALE_H)
-            img_convert( &dest_pic, PIX_FMT_YUV420P,
-                         (AVPicture *)p_ff_pic,
-                         p_sys->p_context->pix_fmt,
-                         p_sys->p_context->width,
-                         p_sys->p_context->height );
-#endif
-            break;
-        default:
-            msg_Err( p_dec, "don't know how to convert chroma %i",
-                     p_sys->p_context->pix_fmt );
-            p_dec->b_error = 1;
-            break;
-        }
+        msg_Err( p_dec, "don't know how to convert chroma %i",
+                 p_sys->p_context->pix_fmt );
+        p_dec->b_error = 1;
     }
 }
 
@@ -912,13 +829,14 @@ static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
 
     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
      * so this check is necessary. */
-    if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
+    if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS ||
         p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
     {
         msg_Dbg( p_dec, "disabling direct rendering" );
         p_sys->b_direct_rendering = 0;
         return avcodec_default_get_buffer( p_context, p_ff_pic );
     }
+    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
 
     /* Get a new picture */
     //p_sys->p_vout->render.b_allow_modify_pics = 0;
@@ -1015,3 +933,30 @@ static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
         p_dec->pf_picture_unlink( p_dec, p_pic );
     }
 }
+
+static void ffmpeg_NextPts( decoder_t *p_dec, int i_block_rate )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+
+    if( p_sys->i_pts <= 0 )
+        return;
+
+    /* interpolate the next PTS */
+    if( p_dec->fmt_in.video.i_frame_rate > 0 &&
+        p_dec->fmt_in.video.i_frame_rate_base > 0 )
+    {
+        p_sys->i_pts += INT64_C(1000000) *
+            (2 + p_sys->p_ff_pic->repeat_pict) *
+            p_dec->fmt_in.video.i_frame_rate_base *
+            i_block_rate / INPUT_RATE_DEFAULT /
+            (2 * p_dec->fmt_in.video.i_frame_rate);
+    }
+    else if( p_sys->p_context->time_base.den > 0 )
+    {
+        p_sys->i_pts += INT64_C(1000000) *
+            (2 + p_sys->p_ff_pic->repeat_pict) *
+            p_sys->p_context->time_base.num *
+            i_block_rate / INPUT_RATE_DEFAULT /
+            (2 * p_sys->p_context->time_base.den);
+    }
+}