rawvideo.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:12k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
  3.  *****************************************************************************
  4.  * Copyright (C) 2001, 2002 the VideoLAN team
  5.  * $Id: e79e2edff172683498be140b54ebfc5d9515d335 $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_codec.h>
  32. #include <vlc_vout.h>
  33. /*****************************************************************************
  34.  * decoder_sys_t : raw video decoder descriptor
  35.  *****************************************************************************/
  36. struct decoder_sys_t
  37. {
  38.     /* Module mode */
  39.     bool b_packetizer;
  40.     /*
  41.      * Input properties
  42.      */
  43.     size_t i_raw_size;
  44.     bool b_invert;
  45.     /*
  46.      * Common properties
  47.      */
  48.     date_t pts;
  49. };
  50. /****************************************************************************
  51.  * Local prototypes
  52.  ****************************************************************************/
  53. static int  OpenDecoder   ( vlc_object_t * );
  54. static int  OpenPacketizer( vlc_object_t * );
  55. static void CloseDecoder  ( vlc_object_t * );
  56. static void *DecodeBlock  ( decoder_t *, block_t ** );
  57. static picture_t *DecodeFrame( decoder_t *, block_t * );
  58. static block_t   *SendFrame  ( decoder_t *, block_t * );
  59. /*****************************************************************************
  60.  * Module descriptor
  61.  *****************************************************************************/
  62. vlc_module_begin ()
  63.     set_description( N_("Pseudo raw video decoder") )
  64.     set_capability( "decoder", 50 )
  65.     set_category( CAT_INPUT )
  66.     set_subcategory( SUBCAT_INPUT_VCODEC )
  67.     set_callbacks( OpenDecoder, CloseDecoder )
  68.     add_submodule ()
  69.     set_description( N_("Pseudo raw video packetizer") )
  70.     set_capability( "packetizer", 100 )
  71.     set_callbacks( OpenPacketizer, CloseDecoder )
  72. vlc_module_end ()
  73. /*****************************************************************************
  74.  * OpenDecoder: probe the decoder and return score
  75.  *****************************************************************************/
  76. static int OpenDecoder( vlc_object_t *p_this )
  77. {
  78.     decoder_t *p_dec = (decoder_t*)p_this;
  79.     decoder_sys_t *p_sys;
  80.     switch( p_dec->fmt_in.i_codec )
  81.     {
  82.         /* Planar YUV */
  83.         case VLC_FOURCC('I','4','4','4'):
  84.         case VLC_FOURCC('I','4','2','2'):
  85.         case VLC_FOURCC('I','4','2','0'):
  86.         case VLC_FOURCC('Y','V','1','2'):
  87.         case VLC_FOURCC('I','Y','U','V'):
  88.         case VLC_FOURCC('I','4','1','1'):
  89.         case VLC_FOURCC('I','4','1','0'):
  90.         case VLC_FOURCC('Y','V','U','9'):
  91.         case VLC_FOURCC('Y','4','2','B'):
  92.         case VLC_FOURCC('Y','4','1','B'):
  93.         /* Packed YUV */
  94.         case VLC_FOURCC('Y','U','Y','2'):
  95.         case VLC_FOURCC('Y','8','0','0'):
  96.         case VLC_FOURCC('U','Y','V','Y'):
  97.         case VLC_FOURCC('H','D','Y','C'):
  98.         /* RGB */
  99.         case VLC_FOURCC('R','V','3','2'):
  100.         case VLC_FOURCC('R','V','2','4'):
  101.         case VLC_FOURCC('R','V','1','6'):
  102.         case VLC_FOURCC('R','V','1','5'):
  103.             break;
  104.         case VLC_FOURCC('2','V','u','y'):
  105.         case VLC_FOURCC('2','v','u','y'):
  106.         case VLC_FOURCC('A','V','U','I'):
  107.             p_dec->fmt_in.i_codec = VLC_FOURCC('U','Y','V','Y');
  108.             break;
  109.         case VLC_FOURCC('y','v','1','2'):
  110.             p_dec->fmt_in.i_codec = VLC_FOURCC('Y','V','1','2');
  111.             break;
  112.         default:
  113.             return VLC_EGENERIC;
  114.     }
  115.     /* Allocate the memory needed to store the decoder's structure */
  116.     if( ( p_dec->p_sys = p_sys =
  117.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  118.         return VLC_ENOMEM;
  119.     /* Misc init */
  120.     p_dec->p_sys->b_packetizer = false;
  121.     p_sys->b_invert = 0;
  122.     if( (int)p_dec->fmt_in.video.i_height < 0 )
  123.     {
  124.         /* Frames are coded from bottom to top */
  125.         p_dec->fmt_in.video.i_height =
  126.             (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
  127.         p_sys->b_invert = true;
  128.     }
  129.     if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
  130.     {
  131.         msg_Err( p_dec, "invalid display size %dx%d",
  132.                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
  133.         return VLC_EGENERIC;
  134.     }
  135.     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
  136.     date_Init( &p_sys->pts, p_dec->fmt_out.video.i_frame_rate,
  137.                p_dec->fmt_out.video.i_frame_rate_base );
  138.     if( p_dec->fmt_out.video.i_frame_rate == 0 ||
  139.         p_dec->fmt_out.video.i_frame_rate_base == 0)
  140.     {
  141.         msg_Warn( p_dec, "invalid frame rate %d/%d, using 25 fps instead",
  142.                   p_dec->fmt_out.video.i_frame_rate,
  143.                   p_dec->fmt_out.video.i_frame_rate_base);
  144.         date_Init( &p_sys->pts, 25, 1 );
  145.     }
  146.     /* Find out p_vdec->i_raw_size */
  147.     vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
  148.                      p_dec->fmt_in.video.i_width,
  149.                      p_dec->fmt_in.video.i_height,
  150.                      p_dec->fmt_in.video.i_aspect );
  151.     p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
  152.         p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
  153.     if( !p_dec->fmt_in.video.i_aspect )
  154.     {
  155.         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
  156.             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
  157.     }
  158.     /* Set callbacks */
  159.     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
  160.         DecodeBlock;
  161.     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
  162.         DecodeBlock;
  163.     return VLC_SUCCESS;
  164. }
  165. static int OpenPacketizer( vlc_object_t *p_this )
  166. {
  167.     decoder_t *p_dec = (decoder_t*)p_this;
  168.     int i_ret = OpenDecoder( p_this );
  169.     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
  170.     return i_ret;
  171. }
  172. /****************************************************************************
  173.  * DecodeBlock: the whole thing
  174.  ****************************************************************************
  175.  * This function must be fed with complete frames.
  176.  ****************************************************************************/
  177. static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  178. {
  179.     decoder_sys_t *p_sys = p_dec->p_sys;
  180.     block_t *p_block;
  181.     void *p_buf;
  182.     if( !pp_block || !*pp_block ) return NULL;
  183.     p_block = *pp_block;
  184.     if( !p_block->i_pts && !p_block->i_dts && !date_Get( &p_sys->pts ) )
  185.     {
  186.         /* We've just started the stream, wait for the first PTS. */
  187.         block_Release( p_block );
  188.         return NULL;
  189.     }
  190.     /* Date management: If there is a pts avaliable, use that. */
  191.     if( p_block->i_pts )
  192.     {
  193.         date_Set( &p_sys->pts, p_block->i_pts );
  194.     }
  195.     else if( p_block->i_dts )
  196.     {
  197.         /* NB, davidf doesn't quite agree with this in general, it is ok
  198.          * for rawvideo since it is in order (ie pts=dts), however, it
  199.          * may not be ok for an out-of-order codec, so don't copy this
  200.          * without thinking */
  201.         date_Set( &p_sys->pts, p_block->i_dts );
  202.     }
  203.     if( p_block->i_buffer < p_sys->i_raw_size )
  204.     {
  205.         msg_Warn( p_dec, "invalid frame size (%zu < %zu)",
  206.                   p_block->i_buffer, p_sys->i_raw_size );
  207.         block_Release( p_block );
  208.         return NULL;
  209.     }
  210.     if( p_sys->b_packetizer )
  211.     {
  212.         p_buf = SendFrame( p_dec, p_block );
  213.     }
  214.     else
  215.     {
  216.         p_buf = DecodeFrame( p_dec, p_block );
  217.     }
  218.     /* Date management: 1 frame per packet */
  219.     date_Increment( &p_sys->pts, 1 );
  220.     *pp_block = NULL;
  221.     return p_buf;
  222. }
  223. /*****************************************************************************
  224.  * FillPicture:
  225.  *****************************************************************************/
  226. static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
  227. {
  228.     int i_plane;
  229.     decoder_sys_t *p_sys = p_dec->p_sys;
  230.     uint8_t *p_src = p_block->p_buffer;
  231.     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
  232.     {
  233.         int i_pitch = p_pic->p[i_plane].i_pitch;
  234.         int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
  235.         int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
  236.         uint8_t *p_dst = p_pic->p[i_plane].p_pixels;
  237.         uint8_t *p_dst_end = p_dst+i_pitch*i_visible_lines;
  238.         if( p_sys->b_invert )
  239.             for( p_dst_end -= i_pitch; p_dst <= p_dst_end;
  240.                  p_dst_end -= i_pitch, p_src += i_visible_pitch )
  241.                 vlc_memcpy( p_dst_end, p_src, i_visible_pitch );
  242.         else
  243.             for( ; p_dst < p_dst_end;
  244.                  p_dst += i_pitch, p_src += i_visible_pitch )
  245.                 vlc_memcpy( p_dst, p_src, i_visible_pitch );
  246.     }
  247. }
  248. /*****************************************************************************
  249.  * DecodeFrame: decodes a video frame.
  250.  *****************************************************************************/
  251. static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
  252. {
  253.     decoder_sys_t *p_sys = p_dec->p_sys;
  254.     picture_t *p_pic;
  255.     /* Get a new picture */
  256.     p_pic = decoder_NewPicture( p_dec );
  257.     if( !p_pic )
  258.     {
  259.         block_Release( p_block );
  260.         return NULL;
  261.     }
  262.     FillPicture( p_dec, p_block, p_pic );
  263.     p_pic->date = date_Get( &p_sys->pts );
  264.     p_pic->b_progressive = true;
  265.     block_Release( p_block );
  266.     return p_pic;
  267. }
  268. /*****************************************************************************
  269.  * SendFrame: send a video frame to the stream output.
  270.  *****************************************************************************/
  271. static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
  272. {
  273.     decoder_sys_t *p_sys = p_dec->p_sys;
  274.     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
  275.     if( p_sys->b_invert )
  276.     {
  277.         picture_t pic;
  278.         uint8_t *p_tmp, *p_pixels;
  279.         int i, j;
  280.         /* Fill in picture_t fields */
  281.         vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
  282.                           p_dec->fmt_out.video.i_width,
  283.                           p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
  284.         if( !pic.i_planes )
  285.         {
  286.             msg_Err( p_dec, "unsupported chroma" );
  287.             return p_block;
  288.         }
  289.         p_tmp = malloc( pic.p[0].i_pitch );
  290.         if( !p_tmp )
  291.             return p_block;
  292.         p_pixels = p_block->p_buffer;
  293.         for( i = 0; i < pic.i_planes; i++ )
  294.         {
  295.             int i_pitch = pic.p[i].i_pitch;
  296.             uint8_t *p_top = p_pixels;
  297.             uint8_t *p_bottom = p_pixels + i_pitch *
  298.                 (pic.p[i].i_visible_lines - 1);
  299.             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
  300.             {
  301.                 vlc_memcpy( p_tmp, p_bottom, pic.p[i].i_visible_pitch  );
  302.                 vlc_memcpy( p_bottom, p_top, pic.p[i].i_visible_pitch  );
  303.                 vlc_memcpy( p_top, p_tmp, pic.p[i].i_visible_pitch  );
  304.                 p_top += i_pitch;
  305.                 p_bottom -= i_pitch;
  306.             }
  307.             p_pixels += i_pitch * pic.p[i].i_lines;
  308.         }
  309.         free( p_tmp );
  310.     }
  311.     return p_block;
  312. }
  313. /*****************************************************************************
  314.  * CloseDecoder: decoder destruction
  315.  *****************************************************************************/
  316. static void CloseDecoder( vlc_object_t *p_this )
  317. {
  318.     decoder_t *p_dec = (decoder_t*)p_this;
  319.     free( p_dec->p_sys );
  320. }