📄 tmp4.cpp
字号:
} while(Enc->Finish_Encoding()) { if (Out!=0 && fwrite( Enc->Get_Bits(), Enc->Get_Bits_Length(), 1, Out )!=1) { fprintf( stderr, "Write error!\n" ); Err = -1; goto End; } Out_Size += Enc->Get_Bits_Length(); if (Debug==2) { printf( "*Pos:%d Frame size:%d\n", Pos, Enc->Get_Bits_Length()); Pos += Enc->Get_Bits_Length(); } } if (!Quiet || Show_PSNR) { pTimer.Stop(); const int Nb = pTimer.Get_Count(); const float BpF = (Nb>0) ? 1.0f*Out_Size/Nb : 0; const float Actual_Bit_Rate = 8.f*BpF*FPS/1000.f; if (!Quiet) { pTimer.Elapsed_FPS(0,"\n\n"); printf( " * File size : %ld bytes\n", Out_Size ); printf( " * Frames : %dx%d x %d\n", W, H*2/3, Nb); printf( " * Avrg frame size : %.0f bytes\n", BpF ); printf( " * Avrg kBits/sec : %.2f (FPS=%.3f)\n", Actual_Bit_Rate, FPS ); if (PSNR_Infos.Cnt>0) { if (Show_PSNR&2) printf( " * Avrg PSNR : %.3f (Y:%.3f U:%.3f V:%.3f)\n", PSNR_Infos.PSNR_A/PSNR_Infos.Cnt, PSNR_Infos.PSNR_Y/PSNR_Infos.Cnt, PSNR_Infos.PSNR_U/PSNR_Infos.Cnt, PSNR_Infos.PSNR_V/PSNR_Infos.Cnt ); } printf( "\n" ); } else if ((Show_PSNR&4) && PSNR_Infos.Cnt>0) // rate-distortion stats printf( "%d %.3f %ld %.4f %.4f %.4f %.4f\n", Bit_Rate>0 ? 0 : Global_Q, Actual_Bit_Rate, Out_Size, PSNR_Infos.PSNR_A/PSNR_Infos.Cnt, PSNR_Infos.PSNR_Y/PSNR_Infos.Cnt, PSNR_Infos.PSNR_U/PSNR_Infos.Cnt, PSNR_Infos.PSNR_V/PSNR_Infos.Cnt ); }End: MP4_Delete_Encoder(Enc); if (In!=stdin && In!=0) fclose(In); if (Out!=0) fclose(Out); return Err;}////////////////////////////////////////////////////////////// Options and params////////////////////////////////////////////////////////////static void Banner( ){ printf( "\n -= Toy MPEG4 plailleure & encaudeure (v%.2f) =-\n\n", VERSION ); printf( " (C) 2003 Pascal Massimino (skal.planet-d.net)\n" );}static void Help( char **argv, SKL_CST_STRING S=0 ){ if (S!=0) printf( S ); printf( " * Usage: %s [options] [in-file]\n", argv[0] ); printf( "\n" ); printf( " While encoding (-o option), PGM [in-file] must be supplied\n" ); printf( " or piped into stdin.\n" ); printf( "\n" ); printf( " * Encoder options:\n" ); printf( " -o <name> ........ Out file for encoding\n" ); printf( " -q <int> ......... Global quantizer [%d]\n", Global_Q ); printf( " -br <int> ........ target bit-rate rate (in kbits/s) [%d]\n", Bit_Rate ); printf( " -fps <float> ..... target frame/s) (0=free) [%.3f]\n", FPS ); printf( " -qtype <int> ..... Quant type (0:H263, 1:MPEG4) [%d]\n", Quant_Type ); printf( " -imax <int> ...... Intra max delay [%d]\n", Intra_Max ); printf( " -4v <int> ........ 4MV probing percent (0=none (off), 100=full) [%d]\n", Inter4V_Probing ); printf( " -sad <int> ....... Search metric to use (0=SAD) [%d]\n", Search_Metric ); printf( " -sad_lo <int> .... SAD lower limit for SKIP [%d]\n", SAD_Skip_Limit ); printf( " -sad_hi <int> .... SAD high limit for INTRA [%d]\n", SAD_Intra_Limit ); printf( " -lambda <float> .. Lambda MV weight for cost function [%.3f]\n", Lambda ); printf( " -red ............. Use reduced frames\n" ); printf( " -redf ............ Force use of reduced frames\n" ); printf( " -gmc ............. Use GMC (with decision)\n" ); printf( " -gmcf ............ Force GMC usage for all frames\n" ); printf( " -gmc_acc <int> ... GMC accuracy [0..3]=[1/2,..,1/16]pel [%d]\n", GMC_Accuracy ); printf( " -intl ............ Enable Interlaced encoding (with decisions)\n" ); printf( " -intldct ......... force interlaced DCT encoding\n" ); printf( " -intlfld ......... force interlaced field-prediction encoding\n" ); printf( " -fld <int> ....... Field-Prediction worthiness probing percent (0=none (off), 100=full) [%d]\n", Field_Pred_Probing ); printf( " -s <int> ......... MV-search method\n" ); printf( " -mv <int> ........ MV-search window size [%d]\n", MV_Size ); printf( " -il <int> ........ Scene-change limit, in percent [%d]\n", Intra_Limit ); printf( " -ithresh <int> ... Set inter threshold (0=disable) [%d]\n", Inter_Thresh ); printf( " -trellis ......... Use trellis-based quantization (slower) [%d]\n", Use_Trellis ); printf( " -qpel ............ Use quarter pixel interpolation\n" ); printf( " -hpel ............ Use half pixel interpolation (default)\n" ); printf( " -fpel ............ Use full pixel only\n" ); printf( " -dq <float> ...... Luminance masking strength (0:off) [%.4f]\n", dQuant_Amp ); printf( " -buf_size <int> .. Encoding buffer size (0=disable) [%d]\n", Enc_Buf_Size ); printf( " -seq_code ........ Emit SEQuence codes at start/end [%d]\n", Sequence_Codes ); printf( " -psnr ............ Print overall PSNR, averaged\n" ); printf( " -psnr_all ........ Print PSNR for each frame\n" ); printf( " -rdist ........... Only print rate-distortion infos\n" ); printf( " -pass <int> ...... Pass number (0=none, 1/2) [%d]\n", Pass_Nb); printf( " -passfile <name> . Pass file to use [%s]\n", Pass_File ); printf( " -passrf <float> .. 2nd-pass reaction factor [%.2f]\n", Pass_RF); printf( " -yuv <w> <h> ..... Input is YUV420 of size w x h\n" ); printf( " -custom <name> ... Read custom intra+inter matrix from file\n" ); printf( " -sqcif -qcif -cif\n" ); printf( " -4cif -pal -ntsc\n" ); printf( " -720p -1080p ..... Predefined shortcuts for -yuv <w> <h> option \n" ); printf( " -detect_edges .... Don't use! :)\n" ); printf( "\n" ); printf( " * Decoder options:\n" ); printf( " -avi ............. input stream is AVI\n" );// printf( " -mpg ............. input stream is MPEG-system\n" ); printf( " -save ............ Save ppm files snapxxx.ppm\n" ); printf( " -pgm ............. Send pgm-pipe files to stdout\n" ); printf( " -yuv_out ......... send raw yuv420 to stdout\n" ); printf( " -mem ............. Load file in memory\n" ); printf( " -jump <int> ...... Jump to position (WARNING!!! DANGEROUS)\n" ); printf( " -test ............ Speed test (<=> -noshow -vtrace 0 -mem)\n" ); printf( " -delay <msec> .... Delay between frames display [%d]\n", Delay ); printf( " -rgb ............. yuv->rgb565 conversion\n" ); printf( " -rgb32 ........... yuv->rgb32 conversion\n" ); printf( " -tt .............. transfer type for yuv->rgb32 [%d]\n", Transfer_Type ); printf( " -slice ........... Use per-slice output (is just an example)\n" ); printf( " -psnr_f <file> ... Compute decoded PSNR with ref YUV file <file>\n" ); printf( "\n" ); printf( " * Common options:\n" ); printf( " -nb <int> ........ Nb frames to decode/encode\n" ); printf( " -skip <int> ...... Nb frames to skip before decoding/encoding\n" ); printf( " -mpg2 ............ Input is a MPEG1/2 bitstream\n" ); printf( " -stream .......... Encoding will re-emit VOL-header at key-frames\n" ); printf( " -noshow .......... Don't display frames\n" ); printf( " -cpu <type>....... CPU to use. type={x86,sse,mmx,c,ref,alt}\n" ); printf( " -quiet ........... Keep quiet\n" ); printf( " -debug <int> ..... Debug level [%d]\n", Debug ); printf( " -adebug <int> .... Analyzer verbose level [%d]\n", ADebug ); printf( " -vtrace <level> .. Visual trace (debug) [%d]\n", VTrace ); printf( " -trace_mem ....... Trace memory usage (not for dll) [%d]\n", Trace_Mem ); printf( " -v ............... Prints version and exits. [%.2f]\n", VERSION ); printf( " -h ............... This help.\n" ); printf( "\n" ); printf( "\n" ); printf( "Examples:\n" ); printf( " %s -noshow -pgm test.m4v | %s -o test2.m4v -q 3\n", argv[0], argv[0] ); printf( " [will transcode 'test.m4v' into 'test2.m4v']\n" ); printf( "\n" ); printf( " %s test2.m4v\n", argv[0] ); printf( " [will decode and display 'test2.m4v']\n" ); printf( "\n" ); printf( " ffmpeg -i austin.avi -f pgmyuv -f imagepipe pipe: | %s -o goldmember.m4v\n", argv[0] ); printf( " [oh, behave!]\n" ); printf( "\n" ); exit( 0 );}//////////////////////////////////////////////////////////// Command line parsingstatic void Default_Params(){ Save = 0; Show = 1; With_Slices = 0; From_Mem = 0; Trace_Mem = 0; MPEG12 = 0; Nb_Frames =-1; // all Skip_Frames = 0; // none Jump_To = 0; // disabled Streaming = 0; // default: storage Sequence_Codes = 0; // default: no sequence start/end codes Global_Q = 4; Quant_Type = 1; Bit_Rate = 0; Search_Method = 0; MV_Size = 1; Hi_Mem = 0; // default: Don't use additional memory Enc_Buf_Size = 0; Use_GMC =-1; // disabled GMC_Accuracy = 3; // 1/16th pel Use_Reduced =-1; // disabled Intra_Limit = 20; // 20% of intra-coded => scene change Search_Metric = 0; // SAD SAD_Skip_Limit = 256; // Sad()<SAD_Skip_Limit*Q ? => SKIP SAD_Intra_Limit = 2000; // Sad()>SAD_Intra_Limit*Q ? => INTRA FPS = 0.; Interlace_DCT = 0; Interlace_Field = 0; Sub_Pixel = 1; // default: half-pel dQuant_Amp = 0.0f; // Default: off. Recommended value: ~8.0. Values below ~5. are not worth (file size grows) Lambda = 1.0f; // Neutral default value Intra_Max = 200; Inter4V_Probing = 80; // usually a good compromise between speed and PSNR gain due to 4V Field_Pred_Probing = 40; // ditto. Inter_Thresh =-1; // disabled Use_Trellis = 0; Show_PSNR = 0; Pass_Nb = 0; Pass_RF = 0.10f; Edge_Detect = 0; System_Stream = 0; Delay = 0; To_RGB = 0x20565; Transfer_Type = 1; VTrace = 0; Cpu = SKL_CPU_DETECT; // will be probed later, at DSP's init. Debug = 0; ADebug = 0; Quiet = 0; Out_File = 0; In_File = 0; Pass_File = "pass1.log"; Ref_YUV_File = 0; YUV_Input = 0; YUV_W = 0; YUV_H = 0; Custom_Matrix = 0;}static void Missing( SKL_CST_STRING S ){ Skl_Throw( SKL_MSG_EXCEPTION( "Missing value after %s option\n", S ) );}static const char *Parse_String_Value(const int argc, const char * const *argv, int &k){ if (++k==argc) Missing( argv[k-1] ); return argv[k];}static int Parse_Int_Value(const int argc, const char * const *argv, int &k, const int Min=0x80000000, const int Max=0x7fffffff){ if (++k==argc) Missing( argv[k-1] ); int Val = atoi(argv[k]); if (Val<Min) return Min; else if (Val>Max) return Max; else return Val;}static float Parse_Float_Value(const int argc, const char * const *argv, int &k, const float Min=-1.e30f, const float Max=1.e30f){ if (++k==argc) Missing( argv[k-1] ); float Val = (float)atof(argv[k]); if (Val<Min) return Min; else if (Val>Max) return Max; else return Val;}static SKL_CPU_FEATURE Get_Feature(const int argc, const char * const *argv, int &k){ if (++k==argc) Missing( argv[k-1] ); if ( !strcmp( argv[k], "ref" ) ) return SKL_CPU_REF; else if ( !strcmp( argv[k], "c" ) ) return SKL_CPU_C; else if ( !strcmp( argv[k], "x86" ) ) return SKL_CPU_X86; else if ( !strcmp( argv[k], "sse" ) ) return SKL_CPU_SSE; else if ( !strcmp( argv[k], "sse2") ) return SKL_CPU_SSE2; else if ( !strcmp( argv[k], "mmx" ) ) return SKL_CPU_MMX; else if ( !strcmp( argv[k], "alt" ) ) return SKL_CPU_ALT; else { printf( "Unknown cpu '%s'! Reverting to 'c'\n", argv[k] ); return SKL_CPU_C; }}static void Parse_Options( int argc, char **argv ){ Default_Params(); for( int k=1; k<argc; ++k ) { if ( !strcmp( argv[k], "-h" ) || !strcmp( argv[k], "-H" ) ) Help( argv ); else if ( !strcmp( argv[k], "-save" ) ) { Save = 1; } else if ( !strcmp( argv[k], "-pgm" ) ) { Save = 2; Show = 0; } else if ( !strcmp( argv[k], "-yuv_out" ) ) { Save = 3; Show = 0; } else if ( !strcmp( argv[k], "-noshow" ) ) { Show = 0; } else if ( !strcmp( argv[k], "-slice" ) ) { With_Slices = 1; } else if ( !strcmp( argv[k], "-mem" ) ) From_Mem = 1; else if ( !strcmp( argv[k], "-quiet" ) ) Quiet = 1; else if ( !strcmp( argv[k], "-detect_edges" ) ) Edge_Detect = 1; else if ( !strcmp( argv[k], "-rdist" ) ) { Show_PSNR = 2|4; Quiet=1; Show = 0; } else if ( !strcmp( argv[k], "-psnr_all" ) ) Show_PSNR |= 1; else if ( !strcmp( argv[k], "-psnr" ) ) Show_PSNR |= 2; else if ( !strcmp( argv[k], "-trellis" ) ) Use_Trellis = 1; else if ( !strcmp( argv[k], "-qpel" ) ) Sub_Pixel = 2; else if ( !strcmp( argv[k], "-hpel" ) ) Sub_Pixel = 1; else if ( !strcmp( argv[k], "-fpel" ) ) Sub_Pixel = 0; else if ( !strcmp( argv[k], "-red" ) ) Use_Reduced = 1; else if ( !strcmp( argv[k], "-redf" ) ) Use_Reduced = 2; else if ( !strcmp( argv[k], "-gmc" ) ) Use_GMC = 0; // enabled else if ( !strcmp( argv[k], "-gmcf" ) ) Use_GMC = 2; // force else if ( !strcmp( argv[k], "-low_mem" ) ) Hi_Mem = 0; else if ( !strcmp( argv[k], "-hi_mem" ) ) Hi_Mem = 1; else if ( !strcmp( argv[k], "-trace_mem" ) ) Trace_Mem = 1; else if ( !strcmp( argv[k], "-rgb" ) ) { To_RGB = 0x20565; VTrace = 0; } else if ( !strcmp( argv[k], "-rgb32" ) ) { To_RGB = 0x40888; VTrace = 0; } else if ( !strcmp( argv[k], "-mpg2" ) ) MPEG12 = 1; else if ( !strcmp( argv[k], "-avi" ) ) System_Stream = 1; else if ( !strcmp( argv[k], "-mpg" ) ) System_Stream = 2; else if ( !strcmp( argv[k], "-stream" ) ) Streaming = 1; else if ( !strcmp( argv[k], "-seq_code" ) ) Sequence_Codes = 1; else if ( !strcmp( argv[k], "-test" ) ) { Show=0; VTrace=0; To_RGB=0; From_Mem=1; Quiet=2; } else if ( !strcmp( argv[k], "-cpu" ) ) Cpu = Get_Feature(argc, argv, k); else if ( !strcmp( argv[k], "-nb" ) ) Nb_Frames = Parse_Int_Value(argc, argv, k, -1); else if ( !strcmp( argv[k], "-skip" ) ) Skip_Frames = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-jump" ) ) Jump_To = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-q" ) ) Global_Q = Parse_Int_Value(argc, argv, k, 1, 31); else if ( !strcmp( argv[k], "-mv" ) ) MV_Size = Parse_Int_Value(argc, argv, k, 1, 7); else if ( !strcmp( argv[k], "-qtype" ) ) Quant_Type = Parse_Int_Value(argc, argv, k, 0, 1); else if ( !strcmp( argv[k], "-br" ) ) Bit_Rate = Parse_Int_Value(argc, argv, k); else if ( !strcmp( argv[k], "-fps" ) ) FPS = Parse_Float_Value(argc, argv, k, 0.); else if ( !strcmp( argv[k], "-pass" ) ) Pass_Nb = Parse_Int_Value(argc, argv, k, 0, 2); else if ( !strcmp( argv[k], "-passrf" ) ) Pass_RF = Parse_Float_Value(argc, argv, k, 0., 10.); else if ( !strcmp( argv[k], "-s" ) ) Search_Method = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-metric" ) ) Search_Metric = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-sad_lo" ) ) SAD_Skip_Limit = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-sad_hi" ) ) SAD_Intra_Limit = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-lambda" ) ) Lambda = Parse_Float_Value(argc, argv, k, 0.); else if ( !strcmp( argv[k], "-gmc_acc" ) ) GMC_Accuracy = Parse_Int_Value(argc, argv, k, 0, 3); else if ( !strcmp( argv[k], "-il" ) ) Intra_Limit = Parse_Int_Value(argc, argv, k); else if ( !strcmp( argv[k], "-imax" ) ) Intra_Max = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-4v" ) ) Inter4V_Probing = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-fld" ) ) Field_Pred_Probing = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-ithresh" ) ) Inter_Thresh = Parse_Int_Value(argc, argv, k); else if ( !strcmp( argv[k], "-dq" ) ) dQuant_Amp = Parse_Float_Value(argc, argv, k); else if ( !strcmp( argv[k], "-buf_size" ) ) Enc_Buf_Size = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-delay" ) ) Delay = Parse_Int_Value(argc, argv, k, 0); else if ( !strcmp( argv[k], "-vtrace" ) ) { VTrace = Parse_Int_Value(argc, argv, k, 0); To_RGB = 0; /*disable conversion*/ } else if ( !strcmp( argv[k], "-debug" ) ) Debug = Parse_Int_Value(argc, argv, k); else if ( !strcmp( argv[k], "-adebug" ) ) ADebug = Parse_Int_Value(argc, argv, k); else if ( !strcmp( argv[k], "-tt" ) ) Transfer_Type = Parse_Int_Value(argc, argv, k); else if ( !strcmp( argv[k], "-sqcif" ) ) { YUV_Input = 1; YUV_W = 128; YUV_H = 96; } else if ( !strcmp( argv[k], "-qcif" ) ) { YUV_Input = 1; YUV_W = 176; YUV_H = 144; } else if ( !strcmp( argv[k], "-cif" ) ) { YUV_Input = 1; YUV_W = 352; YUV_H = 288; } else if ( !strcmp( argv[k], "-4cif" ) ) { YUV_Input = 1; YUV_W = 704; YUV_H = 576; } else if ( !strcmp( argv[k], "-pal" ) ) { YUV_Input = 1; YUV_W = 720; YUV_H = 576; } else if ( !strcmp( argv[k], "-ntsc" ) ) { YUV_Input = 1; YUV_W = 720; YUV_H = 480; } else if ( !strcmp( argv[k], "-720p" ) ) { YUV_Input = 1; YUV_W = 1280; YUV_H = 720; } else if ( !strcmp( argv[k], "-1080p" ) ) { YUV_Input = 1; YUV_W = 1920; YUV_H = 1080; } else if ( !strcmp( argv[k], "-yuv" ) ) { YUV_W = Parse_Int_Value(argc, argv, k); YUV_H = Parse_Int_Value(argc, argv, k); YUV_Input = 1; } else if ( !strcmp( argv[k], "-v" ) ) { printf( "%.2f\n", VERSION ); exit(0); } else if ( !strcmp( argv[k], "-intl" ) ) { Interlace_DCT = 1; // decide Interlace_Field = 1; // decide } else if ( !strcmp( argv[k], "-intldct" ) ) Interlace_DCT = 2; // force else if ( !strcmp( argv[k], "-intlfld" ) ) Interlace_Field = 2; // force else if ( !strcmp( argv[k], "-o" ) ) Out_File = Parse_String_Value(argc, argv, k); else if ( !strcmp( argv[k], "-passfile" ) ) Pass_File = Parse_String_Value(argc, argv, k); else if ( !strcmp( argv[k], "-custom" ) ) Custom_Matrix = Parse_String_Value(argc, argv, k); else if ( !strcmp( argv[k], "-psnr_f" ) ) { Ref_YUV_File = Parse_String_Value(argc, argv, k); Show_PSNR |= 8; } else if ( argv[k][0] == '-' ) Skl_Throw( SKL_MSG_EXCEPTION("Unknown option '%s' !", argv[k]) ); else In_File = argv[k]; } if (In_File==0 && Out_File==0 && Pass_Nb!=1) Help(argv, "Missing input file!\n\n" ); if (!Quiet) Quiet = (Save==2 || Save==3 || Debug>=1);}////////////////////////////////////////////////////////////// main(), at last////////////////////////////////////////////////////////////#ifdef _WINDOWS#include <io.h>#include <fcntl.h>#endifint main(int argc, char *argv[]){#ifdef _WINDOWS _setmode(_fileno(stdin), _O_BINARY); /* thanks to Marcos Morais <morais at dee.ufcg.edu.br> */ _setmode(_fileno(stdout), _O_BINARY);#endif try { Parse_Options( argc, argv ); if (!Quiet) Banner(); if (Trace_Mem) // won't trace the codec in DLL, since it has its own memory arena. All_Mem = ::new SKL_MEM_TRC_I(2); else All_Mem = ::new SKL_MEM_I(); if (All_Mem==0) throw SKL_MEM_EXCEPTION( "memory pool", 0 ); if (Out_File==0 && Pass_Nb!=1) Decode_MPEG4(In_File); else Encode_MPEG4(Out_File, In_File); } catch(const SKL_EXCEPTION &e) { printf( "Caught exception: %s\n", e.Get_Message() ); e.Print(); return 1; } if (All_Mem!=0) ::delete All_Mem; return 0;}//////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -