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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * yuy2_i420.c : Packed YUV 4:2:2 to Planar YUV conversion module for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2007 the VideoLAN team
  5.  * $Id: 3f3e8822cedfb54cc1ca7cc5e6a97ee21981089a $
  6.  *
  7.  * Authors: Antoine Cellerier <dionoea at videolan dot 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 "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,cyuv"
  34. #define DEST_FOURCC  "I420"
  35. /*****************************************************************************
  36.  * Local and extern prototypes.
  37.  *****************************************************************************/
  38. static int  Activate ( vlc_object_t * );
  39. static void YUY2_I420           ( filter_t *, picture_t *, picture_t * );
  40. static void YVYU_I420           ( filter_t *, picture_t *, picture_t * );
  41. static void UYVY_I420           ( filter_t *, picture_t *, picture_t * );
  42. static void cyuv_I420           ( filter_t *, picture_t *, picture_t * );
  43. static picture_t *YUY2_I420_Filter    ( filter_t *, picture_t * );
  44. static picture_t *YVYU_I420_Filter    ( filter_t *, picture_t * );
  45. static picture_t *UYVY_I420_Filter    ( filter_t *, picture_t * );
  46. static picture_t *cyuv_I420_Filter    ( filter_t *, picture_t * );
  47. /*****************************************************************************
  48.  * Module descriptor
  49.  *****************************************************************************/
  50. vlc_module_begin ()
  51.     set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) )
  52.     set_capability( "video filter2", 80 )
  53.     set_callbacks( Activate, NULL )
  54. vlc_module_end ()
  55. /*****************************************************************************
  56.  * Activate: allocate a chroma function
  57.  *****************************************************************************
  58.  * This function allocates and initializes a chroma function
  59.  *****************************************************************************/
  60. static int Activate( vlc_object_t *p_this )
  61. {
  62.     filter_t *p_filter = (filter_t *)p_this;
  63.     if( p_filter->fmt_in.video.i_width & 1
  64.      || p_filter->fmt_in.video.i_height & 1 )
  65.     {
  66.         return -1;
  67.     }
  68.     if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width
  69.      || p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
  70.         return -1;
  71.     switch( p_filter->fmt_out.video.i_chroma )
  72.     {
  73.         case VLC_FOURCC('I','4','2','0'):
  74.             switch( p_filter->fmt_in.video.i_chroma )
  75.             {
  76.                 case VLC_FOURCC('Y','U','Y','2'):
  77.                 case VLC_FOURCC('Y','U','N','V'):
  78.                     p_filter->pf_video_filter = YUY2_I420_Filter;
  79.                     break;
  80.                 case VLC_FOURCC('Y','V','Y','U'):
  81.                     p_filter->pf_video_filter = YVYU_I420_Filter;
  82.                     break;
  83.                 case VLC_FOURCC('U','Y','V','Y'):
  84.                 case VLC_FOURCC('U','Y','N','V'):
  85.                 case VLC_FOURCC('Y','4','2','2'):
  86.                     p_filter->pf_video_filter = UYVY_I420_Filter;
  87.                     break;
  88.                 case VLC_FOURCC('c','y','u','v'):
  89.                     p_filter->pf_video_filter = cyuv_I420_Filter;
  90.                     break;
  91.                 default:
  92.                     return -1;
  93.             }
  94.             break;
  95.         default:
  96.             return -1;
  97.     }
  98.     return 0;
  99. }
  100. /* Following functions are local */
  101. VIDEO_FILTER_WRAPPER( YUY2_I420 )
  102. VIDEO_FILTER_WRAPPER( YVYU_I420 )
  103. VIDEO_FILTER_WRAPPER( UYVY_I420 )
  104. VIDEO_FILTER_WRAPPER( cyuv_I420 )
  105. /*****************************************************************************
  106.  * YUY2_I420: packed YUY2 4:2:2 to planar YUV 4:2:0
  107.  *****************************************************************************/
  108. static void YUY2_I420( filter_t *p_filter, picture_t *p_source,
  109.                                            picture_t *p_dest )
  110. {
  111.     uint8_t *p_line = p_source->p->p_pixels;
  112.     uint8_t *p_y = p_dest->Y_PIXELS;
  113.     uint8_t *p_u = p_dest->U_PIXELS;
  114.     uint8_t *p_v = p_dest->V_PIXELS;
  115.     int i_x, i_y;
  116.     const int i_dest_margin = p_dest->p[0].i_pitch
  117.                                  - p_dest->p[0].i_visible_pitch;
  118.     const int i_dest_margin_c = p_dest->p[1].i_pitch
  119.                                  - p_dest->p[1].i_visible_pitch;
  120.     const int i_source_margin = p_source->p->i_pitch
  121.                                - p_source->p->i_visible_pitch;
  122.     bool b_skip = false;
  123.     for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
  124.     {
  125.         if( b_skip )
  126.         {
  127.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  128.             {
  129.     #define C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v )      
  130.                 *p_y++ = *p_line++; p_line++; 
  131.                 *p_y++ = *p_line++; p_line++
  132.                 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
  133.                 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
  134.                 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
  135.                 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
  136.             }
  137.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  138.             {
  139.                 C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
  140.             }
  141.         }
  142.         else
  143.         {
  144.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  145.             {
  146.     #define C_YUYV_YUV422( p_line, p_y, p_u, p_v )      
  147.                 *p_y++ = *p_line++; *p_u++ = *p_line++; 
  148.                 *p_y++ = *p_line++; *p_v++ = *p_line++
  149.                 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
  150.                 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
  151.                 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
  152.                 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
  153.             }
  154.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  155.             {
  156.                 C_YUYV_YUV422( p_line, p_y, p_u, p_v );
  157.             }
  158.         }
  159.         p_line += i_source_margin;
  160.         p_y += i_dest_margin;
  161.         p_u += i_dest_margin_c;
  162.         p_v += i_dest_margin_c;
  163.         b_skip = !b_skip;
  164.     }
  165. }
  166. /*****************************************************************************
  167.  * YVYU_I420: packed YVYU 4:2:2 to planar YUV 4:2:0
  168.  *****************************************************************************/
  169. static void YVYU_I420( filter_t *p_filter, picture_t *p_source,
  170.                                            picture_t *p_dest )
  171. {
  172.     uint8_t *p_line = p_source->p->p_pixels;
  173.     uint8_t *p_y = p_dest->Y_PIXELS;
  174.     uint8_t *p_u = p_dest->U_PIXELS;
  175.     uint8_t *p_v = p_dest->V_PIXELS;
  176.     int i_x, i_y;
  177.     const int i_dest_margin = p_dest->p[0].i_pitch
  178.                                  - p_dest->p[0].i_visible_pitch;
  179.     const int i_dest_margin_c = p_dest->p[1].i_pitch
  180.                                  - p_dest->p[1].i_visible_pitch;
  181.     const int i_source_margin = p_source->p->i_pitch
  182.                                - p_source->p->i_visible_pitch;
  183.     bool b_skip = false;
  184.     for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
  185.     {
  186.         if( b_skip )
  187.         {
  188.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  189.             {
  190.     #define C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v )      
  191.                 *p_y++ = *p_line++; p_line++; 
  192.                 *p_y++ = *p_line++; p_line++
  193.                 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
  194.                 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
  195.                 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
  196.                 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
  197.             }
  198.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  199.             {
  200.                 C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
  201.             }
  202.         }
  203.         else
  204.         {
  205.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  206.             {
  207.     #define C_YVYU_YUV422( p_line, p_y, p_u, p_v )      
  208.                 *p_y++ = *p_line++; *p_v++ = *p_line++; 
  209.                 *p_y++ = *p_line++; *p_u++ = *p_line++
  210.                 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
  211.                 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
  212.                 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
  213.                 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
  214.             }
  215.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  216.             {
  217.                 C_YVYU_YUV422( p_line, p_y, p_u, p_v );
  218.             }
  219.         }
  220.         p_line += i_source_margin;
  221.         p_y += i_dest_margin;
  222.         p_u += i_dest_margin_c;
  223.         p_v += i_dest_margin_c;
  224.         b_skip = !b_skip;
  225.     }
  226. }
  227. /*****************************************************************************
  228.  * UYVY_I420: packed UYVY 4:2:2 to planar YUV 4:2:0
  229.  *****************************************************************************/
  230. static void UYVY_I420( filter_t *p_filter, picture_t *p_source,
  231.                                            picture_t *p_dest )
  232. {
  233.     uint8_t *p_line = p_source->p->p_pixels;
  234.     uint8_t *p_y = p_dest->Y_PIXELS;
  235.     uint8_t *p_u = p_dest->U_PIXELS;
  236.     uint8_t *p_v = p_dest->V_PIXELS;
  237.     int i_x, i_y;
  238.     const int i_dest_margin = p_dest->p[0].i_pitch
  239.                                  - p_dest->p[0].i_visible_pitch;
  240.     const int i_dest_margin_c = p_dest->p[1].i_pitch
  241.                                  - p_dest->p[1].i_visible_pitch;
  242.     const int i_source_margin = p_source->p->i_pitch
  243.                                - p_source->p->i_visible_pitch;
  244.     bool b_skip = false;
  245.     for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
  246.     {
  247.         if( b_skip )
  248.         {
  249.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  250.             {
  251.     #define C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v )      
  252.                 *p_u++ = *p_line++; p_line++; 
  253.                 *p_v++ = *p_line++; p_line++
  254.                 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
  255.                 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
  256.                 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
  257.                 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
  258.             }
  259.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  260.             {
  261.                 C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
  262.             }
  263.         }
  264.         else
  265.         {
  266.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  267.             {
  268.     #define C_UYVY_YUV422( p_line, p_y, p_u, p_v )      
  269.                 *p_u++ = *p_line++; *p_y++ = *p_line++; 
  270.                 *p_v++ = *p_line++; *p_y++ = *p_line++
  271.                 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
  272.                 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
  273.                 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
  274.                 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
  275.             }
  276.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  277.             {
  278.                 C_UYVY_YUV422( p_line, p_y, p_u, p_v );
  279.             }
  280.         }
  281.         p_line += i_source_margin;
  282.         p_y += i_dest_margin;
  283.         p_u += i_dest_margin_c;
  284.         p_v += i_dest_margin_c;
  285.         b_skip = !b_skip;
  286.     }
  287. }
  288. /*****************************************************************************
  289.  * cyuv_I420: upside-down packed UYVY 4:2:2 to planar YUV 4:2:0
  290.  * FIXME
  291.  *****************************************************************************/
  292. static void cyuv_I420( filter_t *p_filter, picture_t *p_source,
  293.                                            picture_t *p_dest )
  294. {
  295.     uint8_t *p_line = p_source->p->p_pixels;
  296.     uint8_t *p_y = p_dest->Y_PIXELS;
  297.     uint8_t *p_u = p_dest->U_PIXELS;
  298.     uint8_t *p_v = p_dest->V_PIXELS;
  299.     int i_x, i_y;
  300.     const int i_dest_margin = p_dest->p[0].i_pitch
  301.                                  - p_dest->p[0].i_visible_pitch;
  302.     const int i_dest_margin_c = p_dest->p[1].i_pitch
  303.                                  - p_dest->p[1].i_visible_pitch;
  304.     const int i_source_margin = p_source->p->i_pitch
  305.                                - p_source->p->i_visible_pitch;
  306.     bool b_skip = false;
  307.     for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
  308.     {
  309.         if( b_skip )
  310.         {
  311.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  312.             {
  313.     #define C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v )      
  314.                 *p_y++ = *p_line++; p_line++; 
  315.                 *p_y++ = *p_line++; p_line++
  316.                 C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
  317.                 C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
  318.                 C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
  319.                 C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
  320.             }
  321.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  322.             {
  323.                 C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
  324.             }
  325.         }
  326.         else
  327.         {
  328.             for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
  329.             {
  330.     #define C_cyuv_YUV422( p_line, p_y, p_u, p_v )      
  331.                 *p_y++ = *p_line++; *p_v++ = *p_line++; 
  332.                 *p_y++ = *p_line++; *p_u++ = *p_line++
  333.                 C_cyuv_YUV422( p_line, p_y, p_u, p_v );
  334.                 C_cyuv_YUV422( p_line, p_y, p_u, p_v );
  335.                 C_cyuv_YUV422( p_line, p_y, p_u, p_v );
  336.                 C_cyuv_YUV422( p_line, p_y, p_u, p_v );
  337.             }
  338.             for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
  339.             {
  340.                 C_cyuv_YUV422( p_line, p_y, p_u, p_v );
  341.             }
  342.         }
  343.         p_line += i_source_margin;
  344.         p_y += i_dest_margin;
  345.         p_u += i_dest_margin_c;
  346.         p_v += i_dest_margin_c;
  347.         b_skip = !b_skip;
  348.     }
  349. }