📄 yuvscaler.c
字号:
/* * yuvscaler.c * Copyright (C) 2001-2004 Xavier Biquard <xbiquard@free.fr> * * * Scales arbitrary sized yuv frame to yuv frames suitable for VCD, SVCD or specified * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */// Implementation: there are two scaling methods: one for not_interlaced output and one for interlaced output. // // First version doing only downscaling with no interlacing// June 2001: interlacing capable version// July 2001: upscaling capable version// September 2001: line switching// September/October 2001: new yuv4mpeg header// October 2001: first MMX part for bicubic// September/November 2001: what a mess this code! => cleaning and splitting// December 2001: implementation of time reordering of frames// January 2002: sample aspect ratio calculation by Matto// February 2002: interlacing specification now possible. Replaced alloca with malloc// Mars 2002: sample aspect ratio calculations are back!// May/June 2002: remove file reading capabilities (do not duplicate lav2yuv), add -O DVD, add color chrominance correction // as well as luminance linear reequilibrium. Lots of code cleaning, function renaming, etc...// Keywords concerning interlacing/preprocessing now under INPUT case// October 2002: yuvscaler functionnalities not related to image rescaling now part of yuvcorrect// January 2003: reimplementation of the bicubic algorithm => goes faster// December-January 2004: First MMX subroutine for bicubic calculus => speed x2// January-February 2004: make it go even faster// This first MMX version showed the limits of the use of cspline_w and cspline_h pointers => second// MMX version will implement dedicated cspline_w and cspline_h pointers for MMX treatment// // // TODO:// no more global variables for librarification// Remove file reading/writing// treat the interlace case + specific cases#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <math.h>#include <signal.h>#include "lav_io.h"#include "editlist.h"// #include "jpegutils.c"#include "mjpeg_logging.h"#include "yuv4mpeg.h"#include "mjpeg_types.h"#include "yuvscaler.h"#include "mpegconsts.h"#include "attributes.h"#ifdef HAVE_ASM_MMX#include <fcntl.h>#include "../utils/mmx.h"#endif#define yuvscaler_VERSION "15-02-2004"// For pointer address alignement#define ALIGNEMENT 16 // 16 bytes alignement for mmx registers in SIMD instructions for Pentium#define MAXWIDTHNEIGHBORS 16float PI = 3.141592654;// For inputunsigned int input_width;unsigned int input_height;y4m_ratio_t input_sar; // see yuv4mpeg.h and yuv4mpeg_intern.h for possible valuesunsigned int input_useful = 0; // =1 if specifiedunsigned int input_useful_width = 0;unsigned int input_useful_height = 0;unsigned int input_discard_col_left = 0;unsigned int input_discard_col_right = 0;unsigned int input_discard_line_above = 0;unsigned int input_discard_line_under = 0;unsigned int input_black = 0; //=1 if black borders on input framesunsigned int input_black_line_above = 0;unsigned int input_black_line_under = 0;unsigned int input_black_col_right = 0;unsigned int input_black_col_left = 0;unsigned int input_active_height = 0;unsigned int input_active_width = 0;uint8_t input_y_min, input_y_max;float Ufactor, Vfactor, Gamma;// Downscaling ratiosunsigned int input_height_slice;unsigned int output_height_slice;unsigned int input_width_slice;unsigned int output_width_slice;// For padded_inputunsigned int padded_width = 0;unsigned int padded_height = 0;// For outputunsigned int display_width;unsigned int display_height;unsigned int output_width;unsigned int output_height;unsigned int output_active_width;unsigned int output_active_height;unsigned int output_black_line_above = 0;unsigned int output_black_line_under = 0;unsigned int output_black_col_right = 0;unsigned int output_black_col_left = 0;unsigned int output_skip_line_above = 0;unsigned int output_skip_line_under = 0;unsigned int output_skip_col_right = 0;unsigned int output_skip_col_left = 0;unsigned int black = 0, black_line = 0, black_col = 0; // =1 if black lines must be generated on outputunsigned int skip = 0, skip_line = 0, skip_col = 0; // =1 if lines or columns from the active output will not be displayed on output frames// NB: as these number may not be multiple of output_[height,width]_slice, it is not possible to remove the corresponding pixels in// the input frame, a solution that could speed up things. unsigned int vcd = 0; //=1 if vcd output was selectedunsigned int svcd = 0; //=1 if svcd output was selectedunsigned int dvd = 0; //=1 if dvd output was selected// Global variablesint interlaced = -1; //=Y4M_ILACE_NONE for not-interlaced scaling, =Y4M_ILACE_TOP_FIRST or Y4M_ILACE_BOT_FIRST for interlaced scalingint norm = -1; // =0 for PAL and =1 for NTSCint wide = 0; // =1 for wide (16:9) input to standard (4:3) output conversionint ratio = 0;int size_keyword = 0; // =1 is the SIZE keyword has been usedint infile = 0; // =0 for stdin (default) =1 for fileint algorithm = -1; // =0 for resample, and =1 for bicubicunsigned int specific = 0; // is >0 if a specific downscaling speed enhanced treatment of data is possibleunsigned int mono = 0; // is =1 for monochrome output// Keywords for argument passing const char VCD_KEYWORD[] = "VCD";const char HIRESSTILL[] = "HIRESSTILL";const char LOSVCDSTILL[] = "LOSVCDSTILL";const char LOVCDSTILL[] = "LOVCDSTILL";const char SVCD_KEYWORD[] = "SVCD";const char DVD_KEYWORD[] = "DVD";const char SIZE_KEYWORD[] = "SIZE_";const char USE_KEYWORD[] = "USE_";const char WIDE2STD_KEYWORD[] = "WIDE2STD";const char INFILE_KEYWORD[] = "INFILE_";const char RATIO_KEYWORD[] = "RATIO_";const char MONO_KEYWORD[] = "MONOCHROME";const char FASTVCD[] = "FASTVCD";const char FAST_WIDE2VCD[] = "FAST_WIDE2VCD";const char WIDE2VCD[] = "WIDE2VCD";const char RESAMPLE[] = "RESAMPLE";const char BICUBIC[] = "BICUBIC";const char ACTIVE[] = "ACTIVE";const char NO_HEADER[] = "NO_HEADER";const char NOMMX[] = "NOMMX";// Specific to BICUBIC algorithm// 2048=2^11#define FLOAT2INTEGER 2048#define FLOAT2INTEGERPOWER 11unsigned int bicubic_div_width = FLOAT2INTEGER, bicubic_div_height = FLOAT2INTEGER;unsigned int multiplicative;// Unclassifiedunsigned long int diviseur;uint8_t *divide;unsigned short int *u_i_p;unsigned int out_nb_col_slice, out_nb_line_slice;const static char *legal_opt_flags = "k:I:d:n:v:M:m:O:whtg";int verbose = 1;#define PARAM_LINE_MAX 256uint8_t blacky = 16;uint8_t blackuv = 128;uint8_t no_header = 0; // =1 for no stream header output #ifdef HAVE_ASM_MMXint16_t *mmx_padded, *mmx_cubic;int32_t *mmx_res;int mmx = 1; // =1 for mmx activated, =0 for deactivated/not available#endifint32_t *intermediate,*intermediate_p,*inter_begin;// *************************************************************************************voidyuvscaler_print_usage (char *argv[]){ fprintf (stderr, "usage: yuvscaler -I [input_keyword] -M [mode_keyword] -O [output_keyword] [-S 0|1] [-n p|s|n] [-v 0-2] [-h]\n" "yuvscaler UPscales or DOWNscales arbitrary-sized YUV frames coming from stdin (in YUV4MPEG 4:2:2 format)\n" "to a specified YUV frame sizes to stdout. Please use yuvcorrect for interlacing or color corrections\n" "\n" "yuvscaler is keyword driven :\n" "\t -I for keyword concerning INPUT frame characteristics\n" "\t -M for keyword concerning the scaling MODE of yuvscaler\n" "\t -O for keyword concerning OUTPUT frame characteristics\n" "\n" "Possible input keyword are:\n" "\t USE_WidthxHeight+WidthOffset+HeightOffset to select a useful area of the input frame (all multiple of 2,\n" "\t Height and HeightOffset multiple of 4 if interlaced), the rest of the image being discarded\n" "\t ACTIVE_WidthxHeight+WidthOffset+HeightOffset to select an active area of the input frame (all multiple of 2,\n" "\t Height and HeightOffset multiple of 4 if interlaced), the rest of the image being made black\n" "\n" "Possible mode keyword are:\n" "\t BICUBIC to use the (Mitchell-Netravalli) high-quality bicubic upscaling and/or downscaling algorithm\n" "\t RESAMPLE to use a classical resampling algorithm -only for downscaling- that goes much faster than bicubic\n" "\t For coherence reason, yuvscaler will use RESAMPLE if only downscaling is necessary, BICUBIC otherwise\n" "\t WIDE2STD to converts widescreen (16:9) input frames to standard output (4:3), generating necessary black lines\n" "\t RATIO_WidthIn_WidthOut_HeightIn_HeightOut to specified conversion ratios of\n" "\t WidthIn/WidthOut for width and HeightIN/HeightOut for height to be applied to the useful area.\n" "\t The output active area that results from scaling the input useful area might be different\n" "\t from the display area specified thereafter using the -O KEYWORD syntax.\n" "\t In that case, yuvscaler will automatically generate necessary black lines and columns and/or skip necessary\n" "\t lines and columns to get an active output centered within the display size.\n" "\t WIDE2VCD to transcode wide (16:9) frames to VCD (equivalent to -M WIDE2STD -O VCD)\n" "\t FASTVCD to transcode full sized frames to VCD (equivalent to -M RATIO_2_1_2_1 -O VCD)\n" "\t FAST_WIDE2VCD to transcode full sized wide (16:9) frames to VCD (-M WIDE2STD -M RATIO_2_1_2_1 -O VCD)\n" "\t NO_HEADER to suppress stream header generation on output (chaining scaling with different ratios)\n" "\t By default, yuvscaler will use either interlaced or not-interlaced scaling according to the input header interlace information.\n" "\t If this information is missing in the header (cf. mpeg2dec), yuvscaler will use interlaced acaling\n" "\n" "Possible output keywords are:\n" "\t MONOCHROME to generate monochrome frames on output\n" "\t VCD to generate VCD compliant frames, taking care of PAL and NTSC standards, not-interlaced/progressive frames\n" "\t SVCD to generate SVCD compliant frames, taking care of PAL and NTSC standards, any interlacing types\n" "\t DVD to generate DVD compliant frames, taking care of PAL and NTSC standards, any interlacing types\n" "\t (SVCD and DVD: if input is not-interlaced/progressive, output interlacing will be taken as top_first)\n" "\t HIRESSTILL to generate HIgh-RESolution STILL images: not-interlaced/progressive frames of size 704x(PAL-576,NTSC-480)\n" "\t LOSVCDSTILL to generate LOw-resolution SVCD still images, not-interlaced/progressive frames, size 480x(PAL-576,NTSC-480)\n" "\t LOVCDSTILL to generate LOw-resolution VCD still images, not-interlaced/progressive frames, size 352x(PAL-288,NTSC-240)\n" "\t SIZE_WidthxHeight to generate frames of size WidthxHeight on output (multiple of 2, Height of 4 if interlaced)\n" "\n" "-n (usually not necessary) if norm could not be determined from data flux, specifies the OUTPUT norm for VCD/SVCD p=pal,s=secam,n=ntsc\n" "-v Specifies the degree of verbosity: 0=quiet, 1=normal, 2=verbose/debug\n" "-h : print this lot!\n"); exit (1);}// *************************************************************************************// *************************************************************************************voidyuvscaler_print_information (y4m_stream_info_t in_streaminfo, y4m_ratio_t frame_rate){ // This function print USER'S INFORMATION const char TOP_FIRST[] = "INTERLACED_TOP_FIRST"; const char BOT_FIRST[] = "INTERLACED_BOTTOM_FIRST"; const char NOT_INTER[] = "NOT_INTERLACED"; const char PROGRESSIVE[] = "PROGRESSIVE"; y4m_log_stream_info (LOG_INFO, "input: ", &in_streaminfo); switch (interlaced) { case Y4M_ILACE_NONE: mjpeg_info ("from %ux%u, take %ux%u+%u+%u, %s/%s", input_width, input_height, input_useful_width, input_useful_height, input_discard_col_left, input_discard_line_above, NOT_INTER, PROGRESSIVE); break; case Y4M_ILACE_TOP_FIRST: mjpeg_info ("from %ux%u, take %ux%u+%u+%u, %s", input_width, input_height, input_useful_width, input_useful_height, input_discard_col_left, input_discard_line_above, TOP_FIRST); break; case Y4M_ILACE_BOTTOM_FIRST: mjpeg_info ("from %ux%u, take %ux%u+%u+%u, %s", input_width, input_height, input_useful_width, input_useful_height, input_discard_col_left, input_discard_line_above, BOT_FIRST); break; default: mjpeg_info ("from %ux%u, take %ux%u+%u+%u", input_width, input_height, input_useful_width, input_useful_height, input_discard_col_left, input_discard_line_above); } if (input_black == 1) { mjpeg_info ("with %u and %u black line above and under", input_black_line_above, input_black_line_under); mjpeg_info ("and %u and %u black col left and right", input_black_col_left, input_black_col_right); mjpeg_info ("%u %u", input_active_width, input_active_height); } mjpeg_info ("scale to %ux%u, %ux%u being displayed", output_active_width, output_active_height, display_width, display_height); switch (algorithm) { case 0: mjpeg_info ("Scaling uses the %s algorithm, ", RESAMPLE); break; case 1: mjpeg_info ("Scaling uses the %s algorithm, ", BICUBIC); break; default: mjpeg_error_exit1 ("Unknown algorithm %d", algorithm); } if (black == 1) { mjpeg_info ("black lines: %u above and %u under", output_black_line_above, output_black_line_under); mjpeg_info ("black columns: %u left and %u right", output_black_col_left, output_black_col_right); } if (skip == 1) { mjpeg_info ("skipped lines: %u above and %u under", output_skip_line_above, output_skip_line_under); mjpeg_info ("skipped columns: %u left and %u right", output_skip_col_left, output_skip_col_right); } mjpeg_info ("frame rate: %.3f fps", Y4M_RATIO_DBL (frame_rate));}// *************************************************************************************// *************************************************************************************uint8_tyuvscaler_nearest_integer_division (unsigned long int p, unsigned long int q){ // This function returns the nearest integer of the ratio p/q. // As this ratio in yuvscaler corresponds to a pixel value, it should be between 0 and 255 unsigned long int ratio = p / q; unsigned long int reste = p % q; unsigned long int frontiere = q - q / 2; // Do **not** change this into q/2 => it is not the same for odd q numbers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -