📄 common.h
字号:
/*****************************************************************************
* common.h: h264 encoder
*****************************************************************************
* Copyright (C) 2003 Laurent Aimar
* $Id: common.h,v 1.1 2004/06/03 19:27:06 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#ifndef _COMMON_H
#define _COMMON_H 1
#define HAVE_STDINT_H 1
#ifdef HAVE_STDINT_H
#include "stdint.h"
#else
#include <inttypes.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef _MSC_VER
#define snprintf _snprintf
#define X264_VERSION "" // no configure script for msvc
#endif
/* threads */
#ifdef __WIN32__
#include <windows.h>
#define pthread_t HANDLE
#define pthread_create(t,u,f,d) *(t)=CreateThread(NULL,0,f,d,0,NULL)
#define pthread_join(t,s) { WaitForSingleObject(t,INFINITE); \
CloseHandle(t); }
#define HAVE_PTHREAD 1
#elif defined(SYS_BEOS)
#include <kernel/OS.h>
#define pthread_t thread_id
#define pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
resume_thread(*(t)); }
#define pthread_join(t,s) { long tmp; \
wait_for_thread(t,(s)?(long*)(s):&tmp); }
#define HAVE_PTHREAD 1
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
#endif
/****************************************************************************
* Macros
****************************************************************************/
#define X264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
#define X264_MAX(a,b) ( (a)>(b) ? (a) : (b) )
#define X264_MIN3(a,b,c) X264_MIN((a),X264_MIN((b),(c)))
#define X264_MAX3(a,b,c) X264_MAX((a),X264_MAX((b),(c)))
#define X264_MIN4(a,b,c,d) X264_MIN((a),X264_MIN3((b),(c),(d)))
#define X264_MAX4(a,b,c,d) X264_MAX((a),X264_MAX3((b),(c),(d)))
#define XCHG(type,a,b) { type t = a; a = b; b = t; }
#define FIX8(f) ((int)(f*(1<<8)+.5))
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif
#define CHECKED_MALLOC( var, size )\
{\
var = x264_malloc( size );\
if( !var )\
{\
x264_log( h, X264_LOG_ERROR, "malloc failed\n" );\
goto fail;\
}\
}
#define X264_BFRAME_MAX 16
#define X264_SLICE_MAX 4
#define X264_NAL_MAX (4 + X264_SLICE_MAX)
/****************************************************************************
* Includes
****************************************************************************/
#include "x264.h"
#include "bs.h"
#include "set.h"
#include "predict.h"
#include "pixel.h"
#include "mc.h"
#include "frame.h"
#include "dct.h"
//#include "cabac.h"
#include "csp.h"
#include "quant.h"
/****************************************************************************
* Generals functions
****************************************************************************/
/* x264_malloc : will do or emulate a memalign
* XXX you HAVE TO use x264_free for buffer allocated
* with x264_malloc
*/
void *x264_malloc( int );
void *x264_realloc( void *p, int i_size );
void x264_free( void * );
/* x264_slurp_file: malloc space for the whole file and read it */
char *x264_slurp_file( const char *filename );
/* mdate: return the current date in microsecond */
///int64_t x264_mdate( void );
/* x264_param2string: return a (malloced) string containing most of
* the encoding options */
char *x264_param2string( x264_param_t *p, int b_res );
/* log */
void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
void x264_reduce_fraction( int *n, int *d );
static int x264_clip3( int v, int i_min, int i_max )
{
return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
}
static float x264_clip3f( float v, float f_min, float f_max )
{
return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
}
static int x264_median( int a, int b, int c )
{
int min = a, max =a;
if( b < min )
min = b;
else
max = b; /* no need to do 'b > max' (more consuming than always doing affectation) */
if( c < min )
min = c;
else if( c > max )
max = c;
return a + b + c - min - max;
}
/****************************************************************************
*
****************************************************************************/
enum slice_type_e
{
SLICE_TYPE_P = 0,
SLICE_TYPE_B = 1,
SLICE_TYPE_I = 2,
SLICE_TYPE_SP = 3,
SLICE_TYPE_SI = 4
};
static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
typedef struct
{
x264_sps_t *sps;
x264_pps_t *pps;
int i_type;
int i_first_mb;
int i_last_mb;
int i_pps_id;
int i_frame_num;
int b_field_pic;
int b_bottom_field;
int i_idr_pic_id; /* -1 if nal_type != 5 */
int i_poc_lsb;
int i_delta_poc_bottom;
int i_delta_poc[2];
int i_redundant_pic_cnt;
int b_direct_spatial_mv_pred;
int b_num_ref_idx_override;
int i_num_ref_idx_l0_active;
int i_num_ref_idx_l1_active;
int b_ref_pic_list_reordering_l0;
int b_ref_pic_list_reordering_l1;
struct {
int idc;
int arg;
} ref_pic_list_order[2][16];
int i_cabac_init_idc;
int i_qp;
int i_qp_delta;
int b_sp_for_swidth;
int i_qs_delta;
/* deblocking filter */
int i_disable_deblocking_filter_idc;
int i_alpha_c0_offset;
int i_beta_offset;
} x264_slice_header_t;
/* From ffmpeg
*/
#define X264_SCAN8_SIZE (6*8)
#define X264_SCAN8_0 (4+1*8)
static const int x264_scan8[16+2*4] =
{
/* Luma */
4+1*8, 5+1*8, 4+2*8, 5+2*8,
6+1*8, 7+1*8, 6+2*8, 7+2*8,
4+3*8, 5+3*8, 4+4*8, 5+4*8,
6+3*8, 7+3*8, 6+4*8, 7+4*8,
/* Cb */
1+1*8, 2+1*8,
1+2*8, 2+2*8,
/* Cr */
1+4*8, 2+4*8,
1+5*8, 2+5*8,
};
/*
0 1 2 3 4 5 6 7
0
1 B B L L L L
2 B B L L L L
3 L L L L
4 R R L L L L
5 R R
*/
//@Pan:typedef struct x264_ratecontrol_t x264_ratecontrol_t;
typedef struct x264_vlc_table_t x264_vlc_table_t;
typedef struct
{
int pict_type;
int kept_as_ref;
float qscale;
int mv_bits;
int i_tex_bits;
int p_tex_bits;
int misc_bits;
uint64_t expected_bits;
float new_qscale;
int new_qp;
int i_count;
int p_count;
int s_count;
float blurred_complexity;
char direct_mode;
} ratecontrol_entry_t;
typedef struct
{
double coeff;
double count;
double decay;
} predictor_t;
typedef struct
{
/* constants */
int b_abr;
int b_2pass;
int b_vbv;
double fps;
double bitrate;
double rate_tolerance;
int nmb; /* number of macroblocks in a frame */
int qp_constant[5];
/* current frame */
ratecontrol_entry_t *rce;
int qp; /* qp for current frame */
int qpm; /* qp for current macroblock */
float qpa; /* average of macroblocks' qp */
int slice_type;
int qp_force;
/* VBV stuff */
double buffer_size;
double buffer_fill;
double buffer_rate; /* # of bits added to buffer_fill after each frame */
predictor_t pred[5]; /* predict frame size from satd */
/* ABR stuff */
int last_satd;
double last_rceq;
double cplxr_sum; /* sum of bits*qscale/rceq */
double expected_bits_sum; /* sum of qscale2bits after rceq, ratefactor, and overflow */
double wanted_bits_window; /* target bitrate * window */
double cbr_decay;
double short_term_cplxsum;
double short_term_cplxcount;
double rate_factor_constant;
double ip_offset;
double pb_offset;
/* 2pass stuff */
FILE *p_stat_file_out;
char *psz_stat_file_tmpname;
int num_entries; /* number of ratecontrol_entry_ts */
ratecontrol_entry_t *entry; /* FIXME: copy needed data and free this once init is done */
double last_qscale;
double last_qscale_for[5]; /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */
int last_non_b_pict_type;
double accum_p_qp; /* for determining I-frame quant */
double accum_p_norm;
double last_accum_p_norm;
double lmin[5]; /* min qscale by frame type */
double lmax[5];
double lstep; /* max change (multiply) in qscale per frame */
double i_cplx_sum[5]; /* estimated total texture bits in intra MBs at qscale=1 */
double p_cplx_sum[5];
double mv_bits_sum[5];
int frame_count[5]; /* number of frames of each type */
/* MBRC stuff */
double frame_size_planned;
int first_row, last_row; /* region of the frame to be encoded by this thread */
predictor_t *row_pred;
predictor_t row_preds[5];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -