]> git.sesse.net Git - vlc/blobdiff - modules/codec/rawvideo.c
opus: handle RTP depayloading (fixes #11938)
[vlc] / modules / codec / rawvideo.c
index de85678a52872cc12989423010f541348ed4a4d5..63fd39d89d7d2b5d957324eddfbd6a522814e4be 100644 (file)
 /*****************************************************************************
- * 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.3 2003/04/27 17:53:20 gbazin Exp $
+ * Copyright (C) 2001, 2002 VLC authors and VideoLAN
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
- * 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., 59 Temple Place - Suite 330, Boston, MA  02111, 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.
  *****************************************************************************/
 
 /*****************************************************************************
  * 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"
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-
-typedef struct
-{
-    /* Input properties */
-    decoder_fifo_t *p_fifo;
-    int            i_raw_size;
-
-    /* Output properties */
-
-    mtime_t             pts;
-
-    vout_thread_t       *p_vout;
-
-} vdec_thread_t;
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-static int  OpenDecoder    ( vlc_object_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 * );
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_codec.h>
 
 /*****************************************************************************
- * Module descriptor
+ * decoder_sys_t : raw video decoder descriptor
  *****************************************************************************/
-
-vlc_module_begin();
-    set_description( _("Pseudo Raw Video decoder") );
-    set_capability( "decoder", 50 );
-    set_callbacks( OpenDecoder, NULL );
-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 )
+struct decoder_sys_t
 {
-    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
-
-    switch( p_fifo->i_fourcc )
-    {
-        /* Planar YUV */
-        case VLC_FOURCC('I','4','4','4'):
-        case VLC_FOURCC('I','4','2','2'):
-        case VLC_FOURCC('I','4','2','0'):
-        case VLC_FOURCC('Y','V','1','2'):
-        case VLC_FOURCC('I','Y','U','V'):
-        case VLC_FOURCC('I','4','1','1'):
-        case VLC_FOURCC('I','4','1','0'):
-
-       /* Packed YUV */
-        case VLC_FOURCC('Y','U','Y','2'):
-        case VLC_FOURCC('U','Y','V','Y'):
-
-       /* 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;
-
-        default:
-            return VLC_EGENERIC;
-    }
-
-}
+    /*
+     * Input properties
+     */
+    size_t size;
+    unsigned pitches[PICTURE_PLANE_MAX];
+    unsigned lines[PICTURE_PLANE_MAX];
+
+    /*
+     * Common properties
+     */
+    date_t pts;
+};
+
+/****************************************************************************
+ * Local prototypes
+ ****************************************************************************/
+static int  OpenDecoder   ( vlc_object_t * );
+static int  OpenPacketizer( vlc_object_t * );
+static void CloseCommon   ( vlc_object_t * );
 
 /*****************************************************************************
- * RunDecoder: this function is called just after the thread is created
+ * Module descriptor
  *****************************************************************************/
-static int RunDecoder( decoder_fifo_t *p_fifo )
+vlc_module_begin ()
+    set_description( N_("Pseudo raw video decoder") )
+    set_capability( "decoder", 50 )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_VCODEC )
+    set_callbacks( OpenDecoder, CloseCommon )
+
+    add_submodule ()
+    set_description( N_("Pseudo raw video packetizer") )
+    set_capability( "packetizer", 100 )
+    set_callbacks( OpenPacketizer, CloseCommon )
+vlc_module_end ()
+
+/**
+ * Common initialization for decoder and packetizer
+ */
+static int OpenCommon( decoder_t *p_dec )
 {
-    vdec_thread_t *p_vdec;
-    int b_error;
+    const vlc_chroma_description_t *dsc =
+        vlc_fourcc_GetChromaDescription( p_dec->fmt_in.i_codec );
+    if( dsc == NULL || dsc->plane_count == 0 )
+        return VLC_EGENERIC;
 
-    if( !( p_vdec = malloc( sizeof( vdec_thread_t ) ) ) )
+    if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height == 0 )
     {
-        msg_Err( p_fifo, "out of memory" );
-        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;
     }
-    memset( p_vdec, 0, sizeof( vdec_thread_t ) );
 
-    p_vdec->p_fifo = p_fifo;
-
-    if( InitThread( p_vdec ) != 0 )
-    {
-        DecoderError( p_fifo );
-        return( -1 );
-    }
+    /* Allocate the memory needed to store the decoder's structure */
+    decoder_sys_t *p_sys = calloc(1, sizeof(*p_sys));
+    if( unlikely(p_sys == NULL) )
+        return VLC_ENOMEM;
 
-    while( ( !p_vdec->p_fifo->b_die )&&( !p_vdec->p_fifo->b_error ) )
-    {
-        DecodeThread( p_vdec );
-    }
+    if( !p_dec->fmt_in.video.i_visible_width )
+        p_dec->fmt_in.video.i_visible_width = p_dec->fmt_in.video.i_width;
+    if( !p_dec->fmt_in.video.i_visible_height )
+        p_dec->fmt_in.video.i_visible_height = p_dec->fmt_in.video.i_height;
 
+    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
 
-    if( ( b_error = p_vdec->p_fifo->b_error ) )
+    date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
+               p_dec->fmt_out.video.i_frame_rate_base );
+    if( p_dec->fmt_out.video.i_frame_rate == 0 ||
+        p_dec->fmt_out.video.i_frame_rate_base == 0)
     {
-        DecoderError( p_vdec->p_fifo );
+        msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
+                  p_dec->fmt_out.video.i_frame_rate,
+                  p_dec->fmt_out.video.i_frame_rate_base);
+        date_Init( &p_sys->pts, 25, 1 );
     }
 
-    EndThread( p_vdec );
-    if( b_error )
+    for( unsigned i = 0; i < dsc->plane_count; i++ )
     {
-        return( -1 );
+        unsigned pitch = p_dec->fmt_in.video.i_width * dsc->pixel_size
+                         * dsc->p[i].w.num / dsc->p[i].w.den;
+        unsigned lines = p_dec->fmt_in.video.i_height
+                         * dsc->p[i].h.num / dsc->p[i].h.den;
+
+        p_sys->pitches[i] = pitch;
+        p_sys->lines[i] = lines;
+        p_sys->size += pitch * lines;
     }
 
-    return( 0 );
+    p_dec->p_sys           = p_sys;
+    return VLC_SUCCESS;
 }
 
+/****************************************************************************
+ * 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;
 
-#define FREE( p ) if( p ) { free( p ); p = NULL; }
+    if( pp_block == NULL || *pp_block == NULL )
+        return NULL;
 
+    block_t *p_block = *pp_block;
 
-/*****************************************************************************
- * InitThread: initialize data before entering main loop
- *****************************************************************************/
-static int InitThread( vdec_thread_t * p_vdec )
-{
-    picture_t *p_pic;
-    int i;
-
-#define bih ((BITMAPINFOHEADER*)p_vdec->p_fifo->p_bitmapinfoheader)
-    if( bih == NULL )
-    {
-        msg_Err( p_vdec->p_fifo,
-                 "info missing, fatal" );
-        return( VLC_EGENERIC );
-    }
-    if( bih->biWidth <= 0 || bih->biHeight <= 0 )
+    if( p_block->i_pts <= VLC_TS_INVALID && p_block->i_dts <= VLC_TS_INVALID &&
+        !date_Get( &p_sys->pts ) )
     {
-        msg_Err( p_vdec->p_fifo,
-                 "invalid display size %dx%d",
-                 bih->biWidth, bih->biHeight );
-        return( VLC_EGENERIC );
+        /* We've just started the stream, wait for the first PTS. */
+        block_Release( p_block );
+        return NULL;
     }
 
-    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 )
+    /* Date management: If there is a pts avaliable, use that. */
+    if( p_block->i_pts > VLC_TS_INVALID )
     {
-        msg_Err( p_vdec->p_fifo, "failled created vout" );
-        return( VLC_EGENERIC );
+        date_Set( &p_sys->pts, p_block->i_pts );
     }
-
-    /* Get a 1 picture to be able to compute p_vdec->i_raw_size */
-    p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 );
-    if( p_pic == NULL )
+    else if( p_block->i_dts > VLC_TS_INVALID )
     {
-        msg_Err( p_vdec->p_fifo, "failled to get a vout picture" );
-        return( VLC_EGENERIC );
+        /* NB, davidf doesn't quite agree with this in general, it is ok
+         * for rawvideo since it is in order (ie pts=dts), however, it
+         * may not be ok for an out-of-order codec, so don't copy this
+         * without thinking */
+        date_Set( &p_sys->pts, p_block->i_dts );
     }
 
-    p_vdec->i_raw_size = 0;
-    for( i = 0; i < p_pic->i_planes; i++ )
+    if( p_block->i_buffer < p_sys->size )
     {
-        p_vdec->i_raw_size += p_pic->p[i].i_lines * p_pic->p[i].i_visible_pitch
-            * p_pic->p[i].i_pixel_pitch;
-    }
+        msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
+                  p_block->i_buffer, p_sys->size );
 
-    vout_DestroyPicture( p_vdec->p_vout, p_pic );
+        block_Release( p_block );
+        return NULL;
+    }
 
-    return( VLC_SUCCESS );
-#undef bih
+    *pp_block = NULL;
+    return p_block;
 }
 
-
-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;
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    const uint8_t *p_src = p_block->p_buffer;
 
-    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;
-
-    for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+    for( int i = 0; i < p_pic->i_planes; i++ )
     {
+        uint8_t *p_dst = p_pic->p[i].p_pixels;
 
-        uint8_t *p_dst;
-        int     i_dst;
-
-        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;
-
-        while( i_dst > 0 )
+        for( int x = 0; x < p_pic->p[i].i_visible_lines; x++ )
         {
-            int i_copy;
-
-            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;
-
-            i_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 );
-            }
+            memcpy( p_dst, p_src, p_pic->p[i].i_visible_pitch );
+            p_src += p_sys->pitches[i];
+            p_dst += p_pic->p[i].i_pitch;
         }
+
+        p_src += p_sys->pitches[i]
+               * (p_sys->lines[i] - p_pic->p[i].i_visible_lines);
     }
 }
 
 /*****************************************************************************
- * DecodeThread: decodes a frame
+ * DecodeFrame: decodes a video frame.
  *****************************************************************************/
-static void DecodeThread( vdec_thread_t *p_vdec )
+static picture_t *DecodeFrame( decoder_t *p_dec, block_t **pp_block )
 {
-    int             i_size;
-    pes_packet_t    *p_pes;
-    picture_t       *p_pic;
+    block_t *p_block = DecodeBlock( p_dec, pp_block );
+    if( p_block == NULL )
+        return NULL;
 
-    /* **** 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;
+    decoder_sys_t *p_sys = p_dec->p_sys;
 
-    if( i_size < p_vdec->i_raw_size )
+    /* Get a new picture */
+    picture_t *p_pic = decoder_NewPicture( p_dec );
+    if( p_pic == NULL )
     {
-        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;
+        block_Release( p_block );
+        return NULL;
     }
 
-    /* **** 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 )
-        {
-            return;
-        }
-        msleep( VOUT_OUTMEM_SLEEP );
-    }
+    FillPicture( p_dec, p_block, p_pic );
 
+    /* Date management: 1 frame per packet */
+    p_pic->date = date_Get( &p_dec->p_sys->pts );
+    date_Increment( &p_sys->pts, 1 );
 
-    /* **** fill p_pic **** */
-    FillPicture( p_pes, p_pic );
+    if( p_block->i_flags & BLOCK_FLAG_INTERLACED_MASK )
+    {
+        p_pic->b_progressive = false;
+        p_pic->i_nb_fields = 2;
+        if( p_block->i_flags & BLOCK_FLAG_TOP_FIELD_FIRST )
+            p_pic->b_top_field_first = true;
+        else
+            p_pic->b_top_field_first = false;
+    }
+    else
+        p_pic->b_progressive = true;
 
-    /* **** display p_pic **** */
-    vout_DatePicture( p_vdec->p_vout, p_pic, p_pes->i_pts);
-    vout_DisplayPicture( p_vdec->p_vout, p_pic );
+    block_Release( p_block );
+    return p_pic;
+}
 
+static int OpenDecoder( vlc_object_t *p_this )
+{
+    decoder_t *p_dec = (decoder_t *)p_this;
 
-    input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
+    int ret = OpenCommon( p_dec );
+    if( ret == VLC_SUCCESS )
+        p_dec->pf_decode_video = DecodeFrame;
+    return ret;
 }
 
-
 /*****************************************************************************
- * EndThread : faad decoder thread destruction
+ * SendFrame: send a video frame to the stream output.
  *****************************************************************************/
-static void EndThread (vdec_thread_t *p_vdec)
+static block_t *SendFrame( decoder_t *p_dec, block_t **pp_block )
 {
-    if( p_vdec->p_vout )
-    {
-        vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
-    }
+    block_t *p_block = DecodeBlock( p_dec, pp_block );
+    if( p_block == NULL )
+        return NULL;
 
-    msg_Dbg( p_vdec->p_fifo, "raw video decoder closed" );
+    decoder_sys_t *p_sys = p_dec->p_sys;
 
-    free( p_vdec );
+    /* Date management: 1 frame per packet */
+    p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
+    date_Increment( &p_sys->pts, 1 );
+    return p_block;
+}
+
+static int OpenPacketizer( vlc_object_t *p_this )
+{
+    decoder_t *p_dec = (decoder_t *)p_this;
+
+    int ret = OpenCommon( p_dec );
+    if( ret == VLC_SUCCESS )
+        p_dec->pf_packetize = SendFrame;
+    return ret;
+}
+
+/**
+ * Common deinitialization
+ */
+static void CloseCommon( vlc_object_t *p_this )
+{
+    decoder_t *p_dec = (decoder_t*)p_this;
+    free( p_dec->p_sys );
 }