]> git.sesse.net Git - vlc/blobdiff - modules/codec/rawvideo.c
Swedish translation update by Daniel Nylander
[vlc] / modules / codec / rawvideo.c
index bb2817f71f75088bfdb66d85d8dd31df29a8a64f..c1498644df2659eb9b0803bedc629ee968974eac 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * rawvideo.c: Pseudo audio decoder; for raw video data
+ * rawvideo.c: Pseudo video decoder/packetizer for raw video data
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: rawvideo.c,v 1.5 2003/05/02 03:40:01 fenrir Exp $
+ * Copyright (C) 2001, 2002 the VideoLAN team
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
-#include <vlc/vout.h>
-#include <vlc/decoder.h>
-#include <vlc/input.h>
-
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>                                              /* strdup() */
-#include "codecs.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
+#include <vlc_vout.h>
+
 /*****************************************************************************
- * Local prototypes
+ * decoder_sys_t : raw video decoder descriptor
  *****************************************************************************/
-
-typedef struct
+struct decoder_sys_t
 {
-    /* Input properties */
-    decoder_fifo_t *p_fifo;
-    int            i_raw_size;
+    /* Module mode */
+    bool b_packetizer;
 
-    /* Output properties */
+    /*
+     * Input properties
+     */
+    size_t i_raw_size;
+    bool b_invert;
 
-    mtime_t             pts;
+    /*
+     * Common properties
+     */
+    mtime_t i_pts;
 
-    vout_thread_t       *p_vout;
+};
 
-} vdec_thread_t;
+/****************************************************************************
+ * Local prototypes
+ ****************************************************************************/
+static int  OpenDecoder   ( vlc_object_t * );
+static int  OpenPacketizer( vlc_object_t * );
+static void CloseDecoder  ( vlc_object_t * );
 
-static int  OpenDecoder    ( vlc_object_t * );
+static void *DecodeBlock  ( decoder_t *, block_t ** );
 
-static int  RunDecoder     ( decoder_fifo_t * );
-static int  InitThread     ( vdec_thread_t * );
-static void DecodeThread   ( vdec_thread_t * );
-static void EndThread      ( vdec_thread_t * );
+static picture_t *DecodeFrame( decoder_t *, block_t * );
+static block_t   *SendFrame  ( decoder_t *, block_t * );
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-
 vlc_module_begin();
-    set_description( _("Pseudo Raw Video decoder") );
+    set_description( N_("Pseudo raw video decoder") );
     set_capability( "decoder", 50 );
-    set_callbacks( OpenDecoder, NULL );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_VCODEC );
+    set_callbacks( OpenDecoder, CloseDecoder );
+
+    add_submodule();
+    set_description( N_("Pseudo raw video packetizer") );
+    set_capability( "packetizer", 100 );
+    set_callbacks( OpenPacketizer, CloseDecoder );
 vlc_module_end();
 
-
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
- *****************************************************************************
- * Tries to launch a decoder and return score so that the interface is able
- * to choose.
  *****************************************************************************/
 static int OpenDecoder( vlc_object_t *p_this )
 {
-    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+    decoder_t *p_dec = (decoder_t*)p_this;
+    decoder_sys_t *p_sys;
 
-    switch( p_fifo->i_fourcc )
+    switch( p_dec->fmt_in.i_codec )
     {
         /* Planar YUV */
         case VLC_FOURCC('I','4','4','4'):
@@ -88,235 +100,269 @@ static int OpenDecoder( vlc_object_t *p_this )
         case VLC_FOURCC('I','Y','U','V'):
         case VLC_FOURCC('I','4','1','1'):
         case VLC_FOURCC('I','4','1','0'):
+        case VLC_FOURCC('Y','V','U','9'):
+        case VLC_FOURCC('Y','4','2','B'):
+        case VLC_FOURCC('Y','4','1','B'):
 
-       /* Packed YUV */
+        /* Packed YUV */
         case VLC_FOURCC('Y','U','Y','2'):
+        case VLC_FOURCC('Y','8','0','0'):
         case VLC_FOURCC('U','Y','V','Y'):
+        case VLC_FOURCC('H','D','Y','C'):
 
-       /* RGB */
+        /* RGB */
         case VLC_FOURCC('R','V','3','2'):
         case VLC_FOURCC('R','V','2','4'):
         case VLC_FOURCC('R','V','1','6'):
         case VLC_FOURCC('R','V','1','5'):
-
-            p_fifo->pf_run = RunDecoder;
-            return VLC_SUCCESS;
+            break;
+        case VLC_FOURCC('2','V','u','y'):
+        case VLC_FOURCC('2','v','u','y'):
+        case VLC_FOURCC('A','V','U','I'):
+            p_dec->fmt_in.i_codec = VLC_FOURCC('U','Y','V','Y');
+            break;
+        case VLC_FOURCC('y','v','1','2'):
+            p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2');
+            break;
 
         default:
             return VLC_EGENERIC;
     }
 
-}
-
-/*****************************************************************************
- * RunDecoder: this function is called just after the thread is created
- *****************************************************************************/
-static int RunDecoder( decoder_fifo_t *p_fifo )
-{
-    vdec_thread_t *p_vdec;
-    int b_error;
+    /* Allocate the memory needed to store the decoder's structure */
+    if( ( p_dec->p_sys = p_sys =
+          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
+        return VLC_ENOMEM;
+    /* Misc init */
+    p_dec->p_sys->b_packetizer = false;
+    p_sys->i_pts = 0;
+    p_sys->b_invert = 0;
 
-    if( !( p_vdec = malloc( sizeof( vdec_thread_t ) ) ) )
+    if( (int)p_dec->fmt_in.video.i_height < 0 )
     {
-        msg_Err( p_fifo, "out of memory" );
-        DecoderError( p_fifo );
-        return( -1 );
+        /* Frames are coded from bottom to top */
+        p_dec->fmt_in.video.i_height =
+            (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
+        p_sys->b_invert = true;
     }
-    memset( p_vdec, 0, sizeof( vdec_thread_t ) );
 
-    p_vdec->p_fifo = p_fifo;
-
-    if( InitThread( p_vdec ) != 0 )
+    if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
     {
-        DecoderError( p_fifo );
-        return( -1 );
+        msg_Err( p_dec, "invalid display size %dx%d",
+                 p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
+        return VLC_EGENERIC;
     }
 
-    while( ( !p_vdec->p_fifo->b_die )&&( !p_vdec->p_fifo->b_error ) )
+    /* Find out p_vdec->i_raw_size */
+    vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
+                     p_dec->fmt_in.video.i_width,
+                     p_dec->fmt_in.video.i_height,
+                     p_dec->fmt_in.video.i_aspect );
+    p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
+        p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
+
+    /* Set output properties */
+    p_dec->fmt_out.i_cat = VIDEO_ES;
+    p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
+    if( !p_dec->fmt_in.video.i_aspect )
     {
-        DecodeThread( p_vdec );
+        p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
+            p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
     }
+    else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
+
+    if( p_dec->fmt_in.video.i_rmask )
+        p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
+    if( p_dec->fmt_in.video.i_gmask )
+        p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
+    if( p_dec->fmt_in.video.i_bmask )
+        p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
+
+    /* Set callbacks */
+    p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
+        DecodeBlock;
+    p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
+        DecodeBlock;
+
+    return VLC_SUCCESS;
+}
 
+static int OpenPacketizer( vlc_object_t *p_this )
+{
+    decoder_t *p_dec = (decoder_t*)p_this;
 
-    if( ( b_error = p_vdec->p_fifo->b_error ) )
-    {
-        DecoderError( p_vdec->p_fifo );
-    }
+    int i_ret = OpenDecoder( p_this );
 
-    EndThread( p_vdec );
-    if( b_error )
-    {
-        return( -1 );
-    }
+    if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
 
-    return( 0 );
+    return i_ret;
 }
 
+/****************************************************************************
+ * DecodeBlock: the whole thing
+ ****************************************************************************
+ * This function must be fed with complete frames.
+ ****************************************************************************/
+static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    block_t *p_block;
+    void *p_buf;
 
-#define FREE( p ) if( p ) { free( p ); p = NULL; }
-
+    if( !pp_block || !*pp_block ) return NULL;
 
-/*****************************************************************************
- * InitThread: initialize data before entering main loop
- *****************************************************************************/
-static int InitThread( vdec_thread_t * p_vdec )
-{
-    picture_t pic;
-    int i;
+    p_block = *pp_block;
 
-#define bih ((BITMAPINFOHEADER*)p_vdec->p_fifo->p_bitmapinfoheader)
-    if( bih == NULL )
+    if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
     {
-        msg_Err( p_vdec->p_fifo,
-                 "info missing, fatal" );
-        return( VLC_EGENERIC );
+        /* We've just started the stream, wait for the first PTS. */
+        block_Release( p_block );
+        return NULL;
     }
-    if( bih->biWidth <= 0 || bih->biHeight <= 0 )
+
+    /* Date management */
+    if( p_block->i_pts > 0 || p_block->i_dts > 0 )
     {
-        msg_Err( p_vdec->p_fifo,
-                 "invalid display size %dx%d",
-                 bih->biWidth, bih->biHeight );
-        return( VLC_EGENERIC );
+        if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
+        else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
     }
 
-    p_vdec->p_vout = vout_Request( p_vdec->p_fifo, NULL,
-                                   bih->biWidth, bih->biHeight,
-                                   p_vdec->p_fifo->i_fourcc,
-                                   VOUT_ASPECT_FACTOR * bih->biWidth /
-                                   bih->biHeight );
-
-    if( p_vdec->p_vout == NULL )
+    if( p_block->i_buffer < p_sys->i_raw_size )
     {
-        msg_Err( p_vdec->p_fifo, "failed created vout" );
-        return( VLC_EGENERIC );
+        msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
+                  p_block->i_buffer, p_sys->i_raw_size );
+
+        block_Release( p_block );
+        return NULL;
     }
 
-    /* Find out p_vdec->i_raw_size */
-    vout_InitPicture( VLC_OBJECT(p_vdec->p_fifo), &pic,
-                      bih->biWidth, bih->biHeight, p_vdec->p_fifo->i_fourcc );
-    p_vdec->i_raw_size = 0;
-    for( i = 0; i < pic.i_planes; i++ )
+    if( p_sys->b_packetizer )
     {
-        p_vdec->i_raw_size += pic.p[i].i_lines * pic.p[i].i_visible_pitch;
+        p_buf = SendFrame( p_dec, p_block );
+    }
+    else
+    {
+        p_buf = DecodeFrame( p_dec, p_block );
     }
 
-    return( VLC_SUCCESS );
-#undef bih
-}
+    /* Date management: 1 frame per packet */
+    p_sys->i_pts += ( INT64_C(1000000) * 1.0 / 25 /*FIXME*/ );
+    *pp_block = NULL;
 
+    return p_buf;
+}
 
-static void FillPicture( pes_packet_t *p_pes, picture_t *p_pic )
+/*****************************************************************************
+ * FillPicture:
+ *****************************************************************************/
+static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
 {
     int i_plane;
-
-    data_packet_t   *p_data;
-    uint8_t *p_src;
-    int     i_src;
-
-    p_data = p_pes->p_first;
-    p_src  = p_data->p_payload_start;
-    i_src = p_data->p_payload_end - p_data->p_payload_start;
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    uint8_t *p_src = p_block->p_buffer;
 
     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
     {
+        int i_pitch = p_pic->p[i_plane].i_pitch;
+        int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
+        int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
+        uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
+        uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
+
+        if( p_sys->b_invert )
+            for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
+                 p_dst_end -= i_pitch, p_src += i_visible_pitch )
+                vlc_memcpy( p_dst_end, p_src, i_visible_pitch );
+        else
+            for( ; p_dst < p_dst_end;
+                 p_dst += i_pitch, p_src += i_visible_pitch )
+                vlc_memcpy( p_dst, p_src, i_visible_pitch );
+    }
+}
 
-        uint8_t *p_dst;
-        int     i_dst;
+/*****************************************************************************
+ * DecodeFrame: decodes a video frame.
+ *****************************************************************************/
+static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    picture_t *p_pic;
 
-        p_dst = p_pic->p[i_plane].p_pixels;
-        i_dst = p_pic->p[i_plane].i_pitch * p_pic->p[i_plane].i_lines;
+    /* Get a new picture */
+    p_pic = p_dec->pf_vout_buffer_new( p_dec );
+    if( !p_pic )
+    {
+        block_Release( p_block );
+        return NULL;
+    }
 
-        while( i_dst > 0 )
-        {
-            int i_copy;
+    FillPicture( p_dec, p_block, p_pic );
 
-            i_copy = __MIN( i_src, i_dst );
-            if( i_copy > 0 )
-            {
-                memcpy( p_dst, p_src, i_copy );
-            }
-            i_dst -= i_copy;
-            p_dst += i_copy;
+    p_pic->date = p_sys->i_pts;
 
-            i_src -= i_copy;
-            p_src += i_copy;
-            if( i_src <= 0 )
-            {
-                do
-                {
-                    p_data = p_data->p_next;
-                    if( p_data == NULL )
-                    {
-                        return;
-                    }
-                    p_src  = p_data->p_payload_start;
-                    i_src = p_data->p_payload_end - p_data->p_payload_start;
-                } while( i_src <= 0 );
-            }
-        }
-    }
+    block_Release( p_block );
+    return p_pic;
 }
 
 /*****************************************************************************
- * DecodeThread: decodes a frame
+ * SendFrame: send a video frame to the stream output.
  *****************************************************************************/
-static void DecodeThread( vdec_thread_t *p_vdec )
+static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
 {
-    int             i_size;
-    pes_packet_t    *p_pes;
-    picture_t       *p_pic;
+    decoder_sys_t *p_sys = p_dec->p_sys;
 
-    /* **** get frame **** */
-    input_ExtractPES( p_vdec->p_fifo, &p_pes );
-    if( !p_pes )
-    {
-        p_vdec->p_fifo->b_error = 1;
-        return;
-    }
-    i_size = p_pes->i_pes_size;
+    p_block->i_dts = p_block->i_pts = p_sys->i_pts;
 
-    if( i_size < p_vdec->i_raw_size )
+    if( p_sys->b_invert )
     {
-        msg_Warn( p_vdec->p_fifo, "invalid frame size (%d < %d)",
-                  i_size, p_vdec->i_raw_size );
-        //input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
-        return;
-    }
+        picture_t pic;
+        uint8_t *p_tmp, *p_pixels;
+        int i, j;
 
-    /* **** get video picture **** */
-    while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
-    {
-        if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
+        /* Fill in picture_t fields */
+        vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
+                          p_dec->fmt_out.video.i_width,
+                          p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
+
+        if( !pic.i_planes )
         {
-            return;
+            msg_Err( p_dec, "unsupported chroma" );
+            return p_block;
         }
-        msleep( VOUT_OUTMEM_SLEEP );
-    }
-
 
-    /* **** fill p_pic **** */
-    FillPicture( p_pes, p_pic );
+        p_tmp = malloc( pic.p[0].i_pitch );
+        if( !p_tmp )
+            return p_block;
+        p_pixels = p_block->p_buffer;
+        for( i = 0; i < pic.i_planes; i++ )
+        {
+            int i_pitch = pic.p[i].i_pitch;
+            uint8_t *p_top = p_pixels;
+            uint8_t *p_bottom = p_pixels + i_pitch *
+                (pic.p[i].i_visible_lines - 1);
 
-    /* **** display p_pic **** */
-    vout_DatePicture( p_vdec->p_vout, p_pic, p_pes->i_pts);
-    vout_DisplayPicture( p_vdec->p_vout, p_pic );
+            for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
+            {
+                vlc_memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch  );
+                vlc_memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch  );
+                vlc_memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch  );
+                p_top += i_pitch;
+                p_bottom -= i_pitch;
+            }
 
+            p_pixels += i_pitch * pic.p[i].i_lines;
+        }
+        free( p_tmp );
+    }
 
-    input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
+    return p_block;
 }
 
-
 /*****************************************************************************
- * EndThread : faad decoder thread destruction
+ * CloseDecoder: decoder destruction
  *****************************************************************************/
-static void EndThread (vdec_thread_t *p_vdec)
+static void CloseDecoder( vlc_object_t *p_this )
 {
-    if( p_vdec->p_vout )
-    {
-        vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
-    }
-
-    msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );
-
-    free( p_vdec );
+    decoder_t *p_dec = (decoder_t*)p_this;
+    free( p_dec->p_sys );
 }