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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * resize.c: video scaling module for YUVP/A pictures
  3.  *  Uses the low quality "nearest neighbour" algorithm.
  4.  *****************************************************************************
  5.  * Copyright (C) 2003 VideoLAN
  6.  * $Id: scale.c 8991 2004-10-14 22:14:09Z gbazin $
  7.  *
  8.  * Author: Gildas Bazin <gbazin@videolan.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <vlc/vlc.h>
  28. #include <vlc/decoder.h>
  29. #include "vlc_filter.h"
  30. /*****************************************************************************
  31.  * filter_sys_t : filter descriptor
  32.  *****************************************************************************/
  33. struct filter_sys_t
  34. {
  35.     es_format_t fmt_in;
  36.     es_format_t fmt_out;
  37. };
  38. /****************************************************************************
  39.  * Local prototypes
  40.  ****************************************************************************/
  41. static int  OpenFilter ( vlc_object_t * );
  42. static void CloseFilter( vlc_object_t * );
  43. static picture_t *Filter( filter_t *, picture_t * );
  44. /*****************************************************************************
  45.  * Module descriptor
  46.  *****************************************************************************/
  47. vlc_module_begin();
  48.     set_description( _("Video scaling filter") );
  49.     set_capability( "video filter2", 10000 );
  50.     set_callbacks( OpenFilter, CloseFilter );
  51. vlc_module_end();
  52. /*****************************************************************************
  53.  * OpenFilter: probe the filter and return score
  54.  *****************************************************************************/
  55. static int OpenFilter( vlc_object_t *p_this )
  56. {
  57.     filter_t *p_filter = (filter_t*)p_this;
  58.     filter_sys_t *p_sys;
  59.     if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') &&
  60.           p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') ) ||
  61.         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
  62.     {
  63.         return VLC_EGENERIC;
  64.     }
  65.     /* Allocate the memory needed to store the decoder's structure */
  66.     if( ( p_filter->p_sys = p_sys =
  67.           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
  68.     {
  69.         msg_Err( p_filter, "out of memory" );
  70.         return VLC_EGENERIC;
  71.     }
  72.     p_filter->pf_video_filter = Filter;
  73.     msg_Dbg( p_filter, "%ix%i -> %ix%i", p_filter->fmt_in.video.i_width,
  74.              p_filter->fmt_in.video.i_height, p_filter->fmt_out.video.i_width,
  75.              p_filter->fmt_out.video.i_height );
  76.     return VLC_SUCCESS;
  77. }
  78. /*****************************************************************************
  79.  * CloseFilter: clean up the filter
  80.  *****************************************************************************/
  81. static void CloseFilter( vlc_object_t *p_this )
  82. {
  83.     filter_t *p_filter = (filter_t*)p_this;
  84.     filter_sys_t *p_sys = p_filter->p_sys;
  85.     free( p_sys );
  86. }
  87. /****************************************************************************
  88.  * Filter: the whole thing
  89.  ****************************************************************************/
  90. static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
  91. {
  92.     filter_sys_t *p_sys = p_filter->p_sys;
  93.     picture_t *p_pic_dst;
  94.     int i_plane, i, j, k, l;
  95.     /* Request output picture */
  96.     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
  97.     if( !p_pic_dst )
  98.     {
  99.         msg_Warn( p_filter, "can't get output picture" );
  100.         p_pic->pf_release( p_pic );
  101.         return NULL;
  102.     }
  103.     for( i_plane = 0; i_plane < p_pic_dst->i_planes; i_plane++ )
  104.     {
  105.         uint8_t *p_src = p_pic->p[i_plane].p_pixels;
  106.         uint8_t *p_dst = p_pic_dst->p[i_plane].p_pixels;
  107.         int i_src_pitch = p_pic->p[i_plane].i_pitch;
  108.         int i_dst_pitch = p_pic_dst->p[i_plane].i_pitch;
  109.         for( i = 0; i < p_pic_dst->p[i_plane].i_visible_lines; i++ )
  110.         {
  111.             l = ( p_filter->fmt_in.video.i_height * i +
  112.                   p_filter->fmt_out.video.i_height / 2 ) /
  113.                 p_filter->fmt_out.video.i_height;
  114.             for( j = 0; j < p_pic_dst->p[i_plane].i_visible_pitch; j++ )
  115.             {
  116.                 k = ( p_filter->fmt_in.video.i_width * j +
  117.                       p_filter->fmt_out.video.i_width / 2 ) /
  118.                     p_filter->fmt_out.video.i_width;
  119.                 p_dst[i * i_dst_pitch + j] = p_src[l * i_src_pitch + k];
  120.             }
  121.         }
  122.     }
  123.     p_pic_dst->date = p_pic->date;
  124.     p_pic_dst->b_force = p_pic->b_force;
  125.     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
  126.     p_pic_dst->b_progressive = p_pic->b_progressive;
  127.     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
  128.     p_pic->pf_release( p_pic );
  129.     return p_pic_dst;
  130. }