rawvideo.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:11k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * rawvideo.c: Pseudo video decoder/packetizer for raw video data
  3.  *****************************************************************************
  4.  * Copyright (C) 2001, 2002 VideoLAN
  5.  * $Id: rawvideo.c 8683 2004-09-10 13:15:59Z gbazin $
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #include <vlc/vlc.h>
  27. #include <vlc/decoder.h>
  28. #include <vlc/vout.h>
  29. /*****************************************************************************
  30.  * decoder_sys_t : raw video decoder descriptor
  31.  *****************************************************************************/
  32. struct decoder_sys_t
  33. {
  34.     /* Module mode */
  35.     vlc_bool_t b_packetizer;
  36.     /*
  37.      * Input properties
  38.      */
  39.     int i_raw_size;
  40.     vlc_bool_t b_invert;
  41.     /*
  42.      * Common properties
  43.      */
  44.     mtime_t i_pts;
  45. };
  46. /****************************************************************************
  47.  * Local prototypes
  48.  ****************************************************************************/
  49. static int  OpenDecoder   ( vlc_object_t * );
  50. static int  OpenPacketizer( vlc_object_t * );
  51. static void CloseDecoder  ( vlc_object_t * );
  52. static void *DecodeBlock  ( decoder_t *, block_t ** );
  53. static picture_t *DecodeFrame( decoder_t *, block_t * );
  54. static block_t   *SendFrame  ( decoder_t *, block_t * );
  55. /*****************************************************************************
  56.  * Module descriptor
  57.  *****************************************************************************/
  58. vlc_module_begin();
  59.     set_description( _("Pseudo raw video decoder") );
  60.     set_capability( "decoder", 50 );
  61.     set_callbacks( OpenDecoder, CloseDecoder );
  62.     add_submodule();
  63.     set_description( _("Pseudo raw video packetizer") );
  64.     set_capability( "packetizer", 100 );
  65.     set_callbacks( OpenPacketizer, CloseDecoder );
  66. vlc_module_end();
  67. /*****************************************************************************
  68.  * OpenDecoder: probe the decoder and return score
  69.  *****************************************************************************/
  70. static int OpenDecoder( vlc_object_t *p_this )
  71. {
  72.     decoder_t *p_dec = (decoder_t*)p_this;
  73.     decoder_sys_t *p_sys;
  74.     switch( p_dec->fmt_in.i_codec )
  75.     {
  76.         /* Planar YUV */
  77.         case VLC_FOURCC('I','4','4','4'):
  78.         case VLC_FOURCC('I','4','2','2'):
  79.         case VLC_FOURCC('I','4','2','0'):
  80.         case VLC_FOURCC('Y','V','1','2'):
  81.         case VLC_FOURCC('I','Y','U','V'):
  82.         case VLC_FOURCC('I','4','1','1'):
  83.         case VLC_FOURCC('I','4','1','0'):
  84.         case VLC_FOURCC('Y','V','U','9'):
  85.         /* Packed YUV */
  86.         case VLC_FOURCC('Y','U','Y','2'):
  87.         case VLC_FOURCC('U','Y','V','Y'):
  88.         /* RGB */
  89.         case VLC_FOURCC('R','V','3','2'):
  90.         case VLC_FOURCC('R','V','2','4'):
  91.         case VLC_FOURCC('R','V','1','6'):
  92.         case VLC_FOURCC('R','V','1','5'):
  93.             break;
  94.         default:
  95.             return VLC_EGENERIC;
  96.     }
  97.     /* Allocate the memory needed to store the decoder's structure */
  98.     if( ( p_dec->p_sys = p_sys =
  99.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  100.     {
  101.         msg_Err( p_dec, "out of memory" );
  102.         return VLC_EGENERIC;
  103.     }
  104.     /* Misc init */
  105.     p_dec->p_sys->b_packetizer = VLC_FALSE;
  106.     p_sys->i_pts = 0;
  107.     p_sys->b_invert = 0;
  108.     if( (int)p_dec->fmt_in.video.i_height < 0 )
  109.     {
  110.         /* Frames are coded from bottom to top */
  111.         p_dec->fmt_in.video.i_height =
  112.             (unsigned int)(-(int)p_dec->fmt_in.video.i_height);
  113.         p_sys->b_invert = VLC_TRUE;
  114.     }
  115.     if( p_dec->fmt_in.video.i_width <= 0 || p_dec->fmt_in.video.i_height <= 0 )
  116.     {
  117.         msg_Err( p_dec, "invalid display size %dx%d",
  118.                  p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
  119.         return VLC_EGENERIC;
  120.     }
  121.     /* Find out p_vdec->i_raw_size */
  122.     vout_InitFormat( &p_dec->fmt_out.video, p_dec->fmt_in.i_codec,
  123.                      p_dec->fmt_in.video.i_width,
  124.                      p_dec->fmt_in.video.i_height,
  125.                      p_dec->fmt_in.video.i_aspect );
  126.     p_sys->i_raw_size = p_dec->fmt_out.video.i_bits_per_pixel *
  127.         p_dec->fmt_out.video.i_width * p_dec->fmt_out.video.i_height / 8;
  128.     /* Set output properties */
  129.     p_dec->fmt_out.i_cat = VIDEO_ES;
  130.     p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
  131.     if( !p_dec->fmt_in.video.i_aspect )
  132.     {
  133.         p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR *
  134.             p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
  135.     }
  136.     else p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
  137.     if( p_dec->fmt_in.video.i_rmask )
  138.         p_dec->fmt_out.video.i_rmask = p_dec->fmt_in.video.i_rmask;
  139.     if( p_dec->fmt_in.video.i_gmask )
  140.         p_dec->fmt_out.video.i_gmask = p_dec->fmt_in.video.i_gmask;
  141.     if( p_dec->fmt_in.video.i_bmask )
  142.         p_dec->fmt_out.video.i_bmask = p_dec->fmt_in.video.i_bmask;
  143.     /* Set callbacks */
  144.     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
  145.         DecodeBlock;
  146.     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
  147.         DecodeBlock;
  148.     return VLC_SUCCESS;
  149. }
  150. static int OpenPacketizer( vlc_object_t *p_this )
  151. {
  152.     decoder_t *p_dec = (decoder_t*)p_this;
  153.     int i_ret = OpenDecoder( p_this );
  154.     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
  155.     return i_ret;
  156. }
  157. /****************************************************************************
  158.  * DecodeBlock: the whole thing
  159.  ****************************************************************************
  160.  * This function must be fed with complete frames.
  161.  ****************************************************************************/
  162. static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  163. {
  164.     decoder_sys_t *p_sys = p_dec->p_sys;
  165.     block_t *p_block;
  166.     void *p_buf;
  167.     if( !pp_block || !*pp_block ) return NULL;
  168.     p_block = *pp_block;
  169.     if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
  170.     {
  171.         /* We've just started the stream, wait for the first PTS. */
  172.         block_Release( p_block );
  173.         return NULL;
  174.     }
  175.     /* Date management */
  176.     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
  177.     {
  178.         if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
  179.         else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
  180.     }
  181.     if( p_block->i_buffer < p_sys->i_raw_size )
  182.     {
  183.         msg_Warn( p_dec, "invalid frame size (%d < %d)",
  184.                   p_block->i_buffer, p_sys->i_raw_size );
  185.         block_Release( p_block );
  186.         return NULL;
  187.     }
  188.     if( p_sys->b_packetizer )
  189.     {
  190.         p_buf = SendFrame( p_dec, p_block );
  191.     }
  192.     else
  193.     {
  194.         p_buf = DecodeFrame( p_dec, p_block );
  195.     }
  196.     /* Date management: 1 frame per packet */
  197.     p_sys->i_pts += ( I64C(1000000) * 1.0 / 25 /*FIXME*/ );
  198.     *pp_block = NULL;
  199.     return p_buf;
  200. }
  201. /*****************************************************************************
  202.  * FillPicture:
  203.  *****************************************************************************/
  204. static void FillPicture( decoder_t *p_dec, block_t *p_block, picture_t *p_pic )
  205. {
  206.     uint8_t *p_src, *p_dst;
  207.     int i_src, i_plane, i_line, i_width;
  208.     decoder_sys_t *p_sys = p_dec->p_sys;
  209.     p_src = p_block->p_buffer;
  210.     i_src = p_block->i_buffer;
  211.     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
  212.     {
  213.         p_dst = p_pic->p[i_plane].p_pixels;
  214.         i_width = p_pic->p[i_plane].i_visible_pitch;
  215.         if( p_sys->b_invert )
  216.             p_src += (i_width * (p_pic->p[i_plane].i_visible_lines - 1));
  217.         for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines; i_line++ )
  218.         {
  219.             p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_width );
  220.             p_src += p_sys->b_invert ? -i_width : i_width;
  221.             p_dst += p_pic->p[i_plane].i_pitch;
  222.         }
  223.         if( p_sys->b_invert )
  224.             p_src += (i_width * (p_pic->p[i_plane].i_visible_lines + 1));
  225.     }
  226. }
  227. /*****************************************************************************
  228.  * DecodeFrame: decodes a video frame.
  229.  *****************************************************************************/
  230. static picture_t *DecodeFrame( decoder_t *p_dec, block_t *p_block )
  231. {
  232.     decoder_sys_t *p_sys = p_dec->p_sys;
  233.     picture_t *p_pic;
  234.     /* Get a new picture */
  235.     p_pic = p_dec->pf_vout_buffer_new( p_dec );
  236.     if( !p_pic )
  237.     {
  238.         block_Release( p_block );
  239.         return NULL;
  240.     }
  241.     FillPicture( p_dec, p_block, p_pic );
  242.     p_pic->date = p_sys->i_pts;
  243.     block_Release( p_block );
  244.     return p_pic;
  245. }
  246. /*****************************************************************************
  247.  * SendFrame: send a video frame to the stream output.
  248.  *****************************************************************************/
  249. static block_t *SendFrame( decoder_t *p_dec, block_t *p_block )
  250. {
  251.     decoder_sys_t *p_sys = p_dec->p_sys;
  252.     p_block->i_dts = p_block->i_pts = p_sys->i_pts;
  253.     if( p_sys->b_invert )
  254.     {
  255.         picture_t pic;
  256.         uint8_t *p_tmp, *p_pixels;
  257.         int i, j;
  258.         /* Fill in picture_t fields */
  259.         vout_InitPicture( VLC_OBJECT(p_dec), &pic, p_dec->fmt_out.i_codec,
  260.                           p_dec->fmt_out.video.i_width,
  261.                           p_dec->fmt_out.video.i_height, VOUT_ASPECT_FACTOR );
  262.         if( !pic.i_planes )
  263.         {
  264.             msg_Err( p_dec, "unsupported chroma" );
  265.             return p_block;
  266.         }
  267.         p_tmp = malloc( pic.p[0].i_visible_pitch );
  268.         p_pixels = p_block->p_buffer;
  269.         for( i = 0; i < pic.i_planes; i++ )
  270.         {
  271.             int i_pitch = pic.p[i].i_visible_pitch;
  272.             uint8_t *p_top = p_pixels;
  273.             uint8_t *p_bottom = p_pixels + i_pitch *
  274.                 (pic.p[i].i_visible_lines - 1);
  275.             for( j = 0; j < pic.p[i].i_visible_lines / 2; j++ )
  276.             {
  277.                 p_dec->p_vlc->pf_memcpy( p_tmp, p_bottom,
  278.                                          pic.p[i].i_visible_pitch  );
  279.                 p_dec->p_vlc->pf_memcpy( p_bottom, p_top,
  280.                                          pic.p[i].i_visible_pitch  );
  281.                 p_dec->p_vlc->pf_memcpy( p_top, p_tmp,
  282.                                          pic.p[i].i_visible_pitch  );
  283.                 p_top += i_pitch;
  284.                 p_bottom -= i_pitch;
  285.             }
  286.             p_pixels += i_pitch * pic.p[i].i_lines;
  287.         }
  288.         free( p_tmp );
  289.     }
  290.     return p_block;
  291. }
  292. /*****************************************************************************
  293.  * CloseDecoder: decoder destruction
  294.  *****************************************************************************/
  295. static void CloseDecoder( vlc_object_t *p_this )
  296. {
  297.     decoder_t *p_dec = (decoder_t*)p_this;
  298.     free( p_dec->p_sys );
  299. }