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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * resize.c: video scaling module for YUVP/A, I420 and RGBA pictures
  3.  *  Uses the low quality "nearest neighbour" algorithm.
  4.  *****************************************************************************
  5.  * Copyright (C) 2003-2007 the VideoLAN team
  6.  * $Id: 159bd180fca315d1f9889bf6db0b74c9fcd80b1a $
  7.  *
  8.  * Authors: Gildas Bazin <gbazin@videolan.org>
  9.  *          Antoine Cellerier <dionoea @t videolan dot org>
  10.  *
  11.  * This program is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24.  *****************************************************************************/
  25. /*****************************************************************************
  26.  * Preamble
  27.  *****************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. # include "config.h"
  30. #endif
  31. #include <vlc_common.h>
  32. #include <vlc_plugin.h>
  33. #include <vlc_vout.h>
  34. #include "vlc_filter.h"
  35. /*****************************************************************************
  36.  * filter_sys_t : filter descriptor
  37.  *****************************************************************************/
  38. struct filter_sys_t
  39. {
  40.     es_format_t fmt_in;
  41.     es_format_t fmt_out;
  42. };
  43. /****************************************************************************
  44.  * Local prototypes
  45.  ****************************************************************************/
  46. static int  OpenFilter ( vlc_object_t * );
  47. static void CloseFilter( vlc_object_t * );
  48. static picture_t *Filter( filter_t *, picture_t * );
  49. /*****************************************************************************
  50.  * Module descriptor
  51.  *****************************************************************************/
  52. vlc_module_begin ()
  53.     set_description( N_("Video scaling filter") )
  54.     set_capability( "video filter2", 10 )
  55.     set_callbacks( OpenFilter, CloseFilter )
  56. vlc_module_end ()
  57. /*****************************************************************************
  58.  * OpenFilter: probe the filter and return score
  59.  *****************************************************************************/
  60. static int OpenFilter( vlc_object_t *p_this )
  61. {
  62.     filter_t *p_filter = (filter_t*)p_this;
  63.     filter_sys_t *p_sys;
  64.     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
  65.           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
  66.           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
  67.           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
  68.           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','V','3','2') &&
  69.           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') ) ||
  70.         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
  71.     {
  72.         return VLC_EGENERIC;
  73.     }
  74.     /* Allocate the memory needed to store the decoder's structure */
  75.     if( ( p_filter->p_sys = p_sys =
  76.           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
  77.         return VLC_ENOMEM;
  78.     p_filter->pf_video_filter = Filter;
  79.     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
  80.              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
  81.              p_filter->fmt_out.video.i_height );
  82.     return VLC_SUCCESS;
  83. }
  84. /*****************************************************************************
  85.  * CloseFilter: clean up the filter
  86.  *****************************************************************************/
  87. static void CloseFilter( vlc_object_t *p_this )
  88. {
  89.     filter_t *p_filter = (filter_t*)p_this;
  90.     filter_sys_t *p_sys = p_filter->p_sys;
  91.     free( p_sys );
  92. }
  93. /****************************************************************************
  94.  * Filter: the whole thing
  95.  ****************************************************************************/
  96. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  97. {
  98.     picture_t *p_pic_dst;
  99.     int i_plane;
  100.     if( !p_pic ) return NULL;
  101.     if( (p_filter->fmt_in.video.i_height == 0) ||
  102.         (p_filter->fmt_in.video.i_width == 0) )
  103.         return NULL;
  104.     if( (p_filter->fmt_out.video.i_height == 0) ||
  105.         (p_filter->fmt_out.video.i_width == 0) )
  106.         return NULL;
  107.     /* Request output picture */
  108.     p_pic_dst = filter_NewPicture( p_filter );
  109.     if( !p_pic_dst )
  110.     {
  111.         picture_Release( p_pic );
  112.         return NULL;
  113.     }
  114.     if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','G','B','A') &&
  115.         p_filter->fmt_in.video.i_chroma != VLC_FOURCC('R','V','3','2') )
  116.     {
  117.         for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
  118.         {
  119.             const int i_src_pitch    = p_pic->p[i_plane].i_pitch;
  120.             const int i_dst_pitch    = p_pic_dst->p[i_plane].i_pitch;
  121.             const int i_src_height   = p_filter->fmt_in.video.i_height;
  122.             const int i_src_width    = p_filter->fmt_in.video.i_width;
  123.             const int i_dst_height   = p_filter->fmt_out.video.i_height;
  124.             const int i_dst_width    = p_filter->fmt_out.video.i_width;
  125.             const int i_dst_visible_lines =
  126.                                        p_pic_dst->p[i_plane].i_visible_lines;
  127.             const int i_dst_visible_pitch =
  128.                                        p_pic_dst->p[i_plane].i_visible_pitch;
  129.             const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
  130. #define SHIFT_SIZE 16
  131.             const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
  132.                                        / i_dst_height;
  133.             const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
  134.                                        / i_dst_width;
  135.             const int i_src_height_1 = i_src_height - 1;
  136.             const int i_src_width_1  = i_src_width - 1;
  137.             uint8_t *p_src = p_pic->p[i_plane].p_pixels;
  138.             uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
  139.             uint8_t *p_dstendline = p_dst + i_dst_visible_pitch;
  140.             const uint8_t *p_dstend = p_dst + i_dst_visible_lines*i_dst_pitch;
  141.             const int i_shift_height = i_dst_height / i_src_height;
  142.             const int i_shift_width = i_dst_width / i_src_width;
  143.             int l = 1<<(SHIFT_SIZE-i_shift_height);
  144.             for( ; p_dst < p_dstend;
  145.                  p_dst += i_dst_hidden_pitch,
  146.                  p_dstendline += i_dst_pitch, l += i_height_coef )
  147.             {
  148.                 int k = 1<<(SHIFT_SIZE-i_shift_width);
  149.                 uint8_t *p_srcl = p_src
  150.                        + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*i_src_pitch);
  151.                 for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
  152.                 {
  153.                     *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     else /* RGBA */
  159.     {
  160.         const int i_src_pitch = p_pic->p->i_pitch;
  161.         const int i_dst_pitch = p_pic_dst->p->i_pitch;
  162.         const int i_src_height   = p_filter->fmt_in.video.i_height;
  163.         const int i_src_width    = p_filter->fmt_in.video.i_width;
  164.         const int i_dst_height   = p_filter->fmt_out.video.i_height;
  165.         const int i_dst_width    = p_filter->fmt_out.video.i_width;
  166.         const int i_dst_visible_lines =
  167.                                    p_pic_dst->p->i_visible_lines;
  168.         const int i_dst_visible_pitch =
  169.                                    p_pic_dst->p->i_visible_pitch;
  170.         const int i_dst_hidden_pitch  = i_dst_pitch - i_dst_visible_pitch;
  171.         const int i_height_coef  = ( i_src_height << SHIFT_SIZE )
  172.                                    / i_dst_height;
  173.         const int i_width_coef   = ( i_src_width << SHIFT_SIZE )
  174.                                    / i_dst_width;
  175.         const int i_src_height_1 = i_src_height - 1;
  176.         const int i_src_width_1  = i_src_width - 1;
  177.         uint32_t *p_src = (uint32_t*)p_pic->p->p_pixels;
  178.         uint32_t *p_dst = (uint32_t*)p_pic_dst->p->p_pixels;
  179.         uint32_t *p_dstendline = p_dst + (i_dst_visible_pitch>>2);
  180.         const uint32_t *p_dstend = p_dst + i_dst_visible_lines*(i_dst_pitch>>2);
  181.         const int i_shift_height = i_dst_height / i_src_height;
  182.         const int i_shift_width = i_dst_width / i_src_width;
  183.         int l = 1<<(SHIFT_SIZE-i_shift_height);
  184.         for( ; p_dst < p_dstend;
  185.              p_dst += (i_dst_hidden_pitch>>2),
  186.              p_dstendline += (i_dst_pitch>>2),
  187.              l += i_height_coef )
  188.         {
  189.             int k = 1<<(SHIFT_SIZE-i_shift_width);
  190.             uint32_t *p_srcl = p_src
  191.                     + (__MIN( i_src_height_1, l >> SHIFT_SIZE )*(i_src_pitch>>2));
  192.             for( ; p_dst < p_dstendline; p_dst++, k += i_width_coef )
  193.             {
  194.                 *p_dst = p_srcl[__MIN( i_src_width_1, k >> SHIFT_SIZE )];
  195.             }
  196.         }
  197.     }
  198.     picture_CopyProperties( p_pic_dst, p_pic );
  199.     picture_Release( p_pic );
  200.     return p_pic_dst;
  201. }