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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * grey_yuv.c : grayscale to others conversion module for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2007, 2008 the VideoLAN team
  5.  * $Id: 99e150c99fa7b1e37bf38d110d5be09789f8ebf6 $
  6.  *
  7.  * Authors: Sam Hocevar <sam@zoy.org>
  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_filter.h>
  32. #include <vlc_vout.h>
  33. #define SRC_FOURCC  "GREY"
  34. #define DEST_FOURCC "I420,YUY2"
  35. /*****************************************************************************
  36.  * Local and extern prototypes.
  37.  *****************************************************************************/
  38. static int  Activate ( vlc_object_t * );
  39. static void GREY_I420( filter_t *, picture_t *, picture_t * );
  40. static void GREY_YUY2( filter_t *, picture_t *, picture_t * );
  41. static picture_t *GREY_I420_Filter( filter_t *, picture_t * );
  42. static picture_t *GREY_YUY2_Filter( filter_t *, picture_t * );
  43. /*****************************************************************************
  44.  * Module descriptor.
  45.  *****************************************************************************/
  46. vlc_module_begin ()
  47.     set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) )
  48.     set_capability( "video filter2", 80 )
  49.     set_callbacks( Activate, NULL )
  50. vlc_module_end ()
  51. /*****************************************************************************
  52.  * Activate: allocate a chroma function
  53.  *****************************************************************************
  54.  * This function allocates and initializes a chroma function
  55.  *****************************************************************************/
  56. static int Activate( vlc_object_t *p_this )
  57. {
  58.     filter_t *p_filter = (filter_t *)p_this;
  59.     if( p_filter->fmt_out.video.i_width & 1
  60.      || p_filter->fmt_out.video.i_height & 1 )
  61.     {
  62.         return -1;
  63.     }
  64.     if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width
  65.      || p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
  66.         return -1;
  67.     switch( p_filter->fmt_in.video.i_chroma )
  68.     {
  69.         case VLC_FOURCC('Y','8','0','0'):
  70.             p_filter->fmt_in.video.i_chroma = VLC_FOURCC('G','R','E','Y');
  71.         case VLC_FOURCC('G','R','E','Y'):
  72.             switch( p_filter->fmt_out.video.i_chroma )
  73.             {
  74.                 case VLC_FOURCC('I','4','2','0'):
  75.                     p_filter->pf_video_filter = GREY_I420_Filter;
  76.                     break;
  77.                 case VLC_FOURCC('Y','U','Y','2'):
  78.                     p_filter->pf_video_filter = GREY_YUY2_Filter;
  79.                     break;
  80.                 default:
  81.                     return -1;
  82.             }
  83.             break;
  84.         default:
  85.             return -1;
  86.     }
  87.     return 0;
  88. }
  89. VIDEO_FILTER_WRAPPER( GREY_I420 )
  90. VIDEO_FILTER_WRAPPER( GREY_YUY2 )
  91. /* Following functions are local */
  92. /*****************************************************************************
  93.  * GREY_I420: 8-bit grayscale to planar YUV 4:2:0
  94.  *****************************************************************************/
  95. static void GREY_I420( filter_t *p_filter, picture_t *p_source,
  96.                                            picture_t *p_dest )
  97. {
  98.     uint8_t *p_line = p_source->p->p_pixels;
  99.     uint8_t *p_y = p_dest->Y_PIXELS;
  100.     uint8_t *p_u = p_dest->U_PIXELS;
  101.     uint8_t *p_v = p_dest->V_PIXELS;
  102.     int i_x, i_y;
  103.     const int i_source_margin = p_source->p->i_pitch
  104.                                  - p_source->p->i_visible_pitch;
  105.     const int i_dest_margin = p_dest->p[0].i_pitch
  106.                                - p_dest->p[0].i_visible_pitch;
  107.     const int i_dest_margin_c = p_dest->p[1].i_pitch
  108.                                  - p_dest->p[1].i_visible_pitch;
  109.     for( i_y = p_filter->fmt_in.video.i_height / 2; i_y-- ; )
  110.     {
  111.         memset(p_u, 0x80, p_dest->p[1].i_visible_pitch);
  112.         p_u += i_dest_margin_c;
  113.         memset(p_v, 0x80, p_dest->p[1].i_visible_pitch);
  114.         p_v += i_dest_margin_c;
  115.     }
  116.     for( i_y = p_filter->fmt_in.video.i_height; i_y-- ; )
  117.     {
  118.         for( i_x = p_filter->fmt_in.video.i_width / 8; i_x-- ; )
  119.         {
  120.             *p_y++ = *p_line++; *p_y++ = *p_line++;
  121.             *p_y++ = *p_line++; *p_y++ = *p_line++;
  122.             *p_y++ = *p_line++; *p_y++ = *p_line++;
  123.             *p_y++ = *p_line++; *p_y++ = *p_line++;
  124.         }
  125.         for( i_x = p_filter->fmt_in.video.i_width % 8; i_x-- ; )
  126.         {
  127.             *p_y++ = *p_line++;
  128.         }
  129.         p_line += i_source_margin;
  130.         p_y += i_dest_margin;
  131.     }
  132. }
  133. /*****************************************************************************
  134.  * GREY_YUY2: 8-bit grayscale to packed YUY2
  135.  *****************************************************************************/
  136. static void GREY_YUY2( filter_t *p_filter, picture_t *p_source,
  137.                                            picture_t *p_dest )
  138. {
  139.     uint8_t *p_in = p_source->p->p_pixels;
  140.     uint8_t *p_out = p_dest->p->p_pixels;
  141.     int i_x, i_y;
  142.     const int i_source_margin = p_source->p->i_pitch
  143.                                  - p_source->p->i_visible_pitch;
  144.     const int i_dest_margin = p_dest->p->i_pitch
  145.                                - p_dest->p->i_visible_pitch;
  146.     for( i_y = p_filter->fmt_out.video.i_height; i_y-- ; )
  147.     {
  148.         for( i_x = p_filter->fmt_out.video.i_width / 8; i_x-- ; )
  149.         {
  150.             *p_out++ = *p_in++; *p_out++ = 0x80;
  151.             *p_out++ = *p_in++; *p_out++ = 0x80;
  152.             *p_out++ = *p_in++; *p_out++ = 0x80;
  153.             *p_out++ = *p_in++; *p_out++ = 0x80;
  154.             *p_out++ = *p_in++; *p_out++ = 0x80;
  155.             *p_out++ = *p_in++; *p_out++ = 0x80;
  156.             *p_out++ = *p_in++; *p_out++ = 0x80;
  157.             *p_out++ = *p_in++; *p_out++ = 0x80;
  158.         }
  159.         for( i_x = (p_filter->fmt_out.video.i_width % 8) / 2; i_x-- ; )
  160.         {
  161.             *p_out++ = *p_in++; *p_out++ = 0x80;
  162.             *p_out++ = *p_in++; *p_out++ = 0x80;
  163.         }
  164.         p_in += i_source_margin;
  165.         p_out += i_dest_margin;
  166.     }
  167. }