129a84457aed4c45bc900998b5e11c03023264208James Dong/* ------------------------------------------------------------------ 229a84457aed4c45bc900998b5e11c03023264208James Dong * Copyright (C) 1998-2009 PacketVideo 329a84457aed4c45bc900998b5e11c03023264208James Dong * 429a84457aed4c45bc900998b5e11c03023264208James Dong * Licensed under the Apache License, Version 2.0 (the "License"); 529a84457aed4c45bc900998b5e11c03023264208James Dong * you may not use this file except in compliance with the License. 629a84457aed4c45bc900998b5e11c03023264208James Dong * You may obtain a copy of the License at 729a84457aed4c45bc900998b5e11c03023264208James Dong * 829a84457aed4c45bc900998b5e11c03023264208James Dong * http://www.apache.org/licenses/LICENSE-2.0 929a84457aed4c45bc900998b5e11c03023264208James Dong * 1029a84457aed4c45bc900998b5e11c03023264208James Dong * Unless required by applicable law or agreed to in writing, software 1129a84457aed4c45bc900998b5e11c03023264208James Dong * distributed under the License is distributed on an "AS IS" BASIS, 1229a84457aed4c45bc900998b5e11c03023264208James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 1329a84457aed4c45bc900998b5e11c03023264208James Dong * express or implied. 1429a84457aed4c45bc900998b5e11c03023264208James Dong * See the License for the specific language governing permissions 1529a84457aed4c45bc900998b5e11c03023264208James Dong * and limitations under the License. 1629a84457aed4c45bc900998b5e11c03023264208James Dong * ------------------------------------------------------------------- 1729a84457aed4c45bc900998b5e11c03023264208James Dong */ 1829a84457aed4c45bc900998b5e11c03023264208James Dong/** 1929a84457aed4c45bc900998b5e11c03023264208James DongThis file contains application function interfaces to the AVC encoder library 2029a84457aed4c45bc900998b5e11c03023264208James Dongand necessary type defitionitions and enumerations. 2129a84457aed4c45bc900998b5e11c03023264208James Dong@publishedAll 2229a84457aed4c45bc900998b5e11c03023264208James Dong*/ 2329a84457aed4c45bc900998b5e11c03023264208James Dong 2429a84457aed4c45bc900998b5e11c03023264208James Dong#ifndef AVCENC_INT_H_INCLUDED 2529a84457aed4c45bc900998b5e11c03023264208James Dong#define AVCENC_INT_H_INCLUDED 2629a84457aed4c45bc900998b5e11c03023264208James Dong 2729a84457aed4c45bc900998b5e11c03023264208James Dong#ifndef AVCINT_COMMON_H_INCLUDED 2829a84457aed4c45bc900998b5e11c03023264208James Dong#include "avcint_common.h" 2929a84457aed4c45bc900998b5e11c03023264208James Dong#endif 3029a84457aed4c45bc900998b5e11c03023264208James Dong#ifndef AVCENC_API_H_INCLUDED 3129a84457aed4c45bc900998b5e11c03023264208James Dong#include "avcenc_api.h" 3229a84457aed4c45bc900998b5e11c03023264208James Dong#endif 3329a84457aed4c45bc900998b5e11c03023264208James Dong 3429a84457aed4c45bc900998b5e11c03023264208James Dongtypedef float OsclFloat; 3529a84457aed4c45bc900998b5e11c03023264208James Dong 3629a84457aed4c45bc900998b5e11c03023264208James Dong/* Definition for the structures below */ 3729a84457aed4c45bc900998b5e11c03023264208James Dong#define DEFAULT_ATTR 0 /* default memory attribute */ 3829a84457aed4c45bc900998b5e11c03023264208James Dong#define MAX_INPUT_FRAME 30 /* some arbitrary number, it can be much higher than this. */ 3929a84457aed4c45bc900998b5e11c03023264208James Dong#define MAX_REF_FRAME 16 /* max size of the RefPicList0 and RefPicList1 */ 4029a84457aed4c45bc900998b5e11c03023264208James Dong#define MAX_REF_PIC_LIST 33 4129a84457aed4c45bc900998b5e11c03023264208James Dong 4229a84457aed4c45bc900998b5e11c03023264208James Dong#define MIN_QP 0 4329a84457aed4c45bc900998b5e11c03023264208James Dong#define MAX_QP 51 4429a84457aed4c45bc900998b5e11c03023264208James Dong#define SHIFT_QP 12 4529a84457aed4c45bc900998b5e11c03023264208James Dong#define LAMBDA_ACCURACY_BITS 16 4629a84457aed4c45bc900998b5e11c03023264208James Dong#define LAMBDA_FACTOR(lambda) ((int)((double)(1<<LAMBDA_ACCURACY_BITS)*lambda+0.5)) 4729a84457aed4c45bc900998b5e11c03023264208James Dong 4829a84457aed4c45bc900998b5e11c03023264208James Dong 4929a84457aed4c45bc900998b5e11c03023264208James Dong#define DISABLE_THRESHOLDING 0 5029a84457aed4c45bc900998b5e11c03023264208James Dong// for better R-D performance 5129a84457aed4c45bc900998b5e11c03023264208James Dong#define _LUMA_COEFF_COST_ 4 //!< threshold for luma coeffs 5229a84457aed4c45bc900998b5e11c03023264208James Dong#define _CHROMA_COEFF_COST_ 4 //!< threshold for chroma coeffs, used to be 7 5329a84457aed4c45bc900998b5e11c03023264208James Dong#define _LUMA_MB_COEFF_COST_ 5 //!< threshold for luma coeffs of inter Macroblocks 5429a84457aed4c45bc900998b5e11c03023264208James Dong#define _LUMA_8x8_COEFF_COST_ 5 //!< threshold for luma coeffs of 8x8 Inter Partition 5529a84457aed4c45bc900998b5e11c03023264208James Dong#define MAX_VALUE 999999 //!< used for start value for some variables 5629a84457aed4c45bc900998b5e11c03023264208James Dong 5729a84457aed4c45bc900998b5e11c03023264208James Dong#define WEIGHTED_COST(factor,bits) (((factor)*(bits))>>LAMBDA_ACCURACY_BITS) 5829a84457aed4c45bc900998b5e11c03023264208James Dong#define MV_COST(f,s,cx,cy,px,py) (WEIGHTED_COST(f,mvbits[((cx)<<(s))-px]+mvbits[((cy)<<(s))-py])) 5929a84457aed4c45bc900998b5e11c03023264208James Dong#define MV_COST_S(f,cx,cy,px,py) (WEIGHTED_COST(f,mvbits[cx-px]+mvbits[cy-py])) 6029a84457aed4c45bc900998b5e11c03023264208James Dong 6129a84457aed4c45bc900998b5e11c03023264208James Dong/* for sub-pel search and interpolation */ 6229a84457aed4c45bc900998b5e11c03023264208James Dong#define SUBPEL_PRED_BLK_SIZE 576 // 24x24 6329a84457aed4c45bc900998b5e11c03023264208James Dong#define REF_CENTER 75 6429a84457aed4c45bc900998b5e11c03023264208James Dong#define V2Q_H0Q 1 6529a84457aed4c45bc900998b5e11c03023264208James Dong#define V0Q_H2Q 2 6629a84457aed4c45bc900998b5e11c03023264208James Dong#define V2Q_H2Q 3 6729a84457aed4c45bc900998b5e11c03023264208James Dong 6829a84457aed4c45bc900998b5e11c03023264208James Dong/* 6929a84457aed4c45bc900998b5e11c03023264208James Dong#define V3Q_H0Q 1 7029a84457aed4c45bc900998b5e11c03023264208James Dong#define V3Q_H1Q 2 7129a84457aed4c45bc900998b5e11c03023264208James Dong#define V0Q_H1Q 3 7229a84457aed4c45bc900998b5e11c03023264208James Dong#define V1Q_H1Q 4 7329a84457aed4c45bc900998b5e11c03023264208James Dong#define V1Q_H0Q 5 7429a84457aed4c45bc900998b5e11c03023264208James Dong#define V1Q_H3Q 6 7529a84457aed4c45bc900998b5e11c03023264208James Dong#define V0Q_H3Q 7 7629a84457aed4c45bc900998b5e11c03023264208James Dong#define V3Q_H3Q 8 7729a84457aed4c45bc900998b5e11c03023264208James Dong#define V2Q_H3Q 9 7829a84457aed4c45bc900998b5e11c03023264208James Dong#define V2Q_H0Q 10 7929a84457aed4c45bc900998b5e11c03023264208James Dong#define V2Q_H1Q 11 8029a84457aed4c45bc900998b5e11c03023264208James Dong#define V2Q_H2Q 12 8129a84457aed4c45bc900998b5e11c03023264208James Dong#define V3Q_H2Q 13 8229a84457aed4c45bc900998b5e11c03023264208James Dong#define V0Q_H2Q 14 8329a84457aed4c45bc900998b5e11c03023264208James Dong#define V1Q_H2Q 15 8429a84457aed4c45bc900998b5e11c03023264208James Dong*/ 8529a84457aed4c45bc900998b5e11c03023264208James Dong 8629a84457aed4c45bc900998b5e11c03023264208James Dong 8729a84457aed4c45bc900998b5e11c03023264208James Dong#define DEFAULT_OVERRUN_BUFFER_SIZE 1000 8829a84457aed4c45bc900998b5e11c03023264208James Dong 8929a84457aed4c45bc900998b5e11c03023264208James Dong// associated with the above cost model 9029a84457aed4c45bc900998b5e11c03023264208James Dongconst uint8 COEFF_COST[2][16] = 9129a84457aed4c45bc900998b5e11c03023264208James Dong{ 9229a84457aed4c45bc900998b5e11c03023264208James Dong {3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 9329a84457aed4c45bc900998b5e11c03023264208James Dong {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9} 9429a84457aed4c45bc900998b5e11c03023264208James Dong}; 9529a84457aed4c45bc900998b5e11c03023264208James Dong 9629a84457aed4c45bc900998b5e11c03023264208James Dong 9729a84457aed4c45bc900998b5e11c03023264208James Dong 9829a84457aed4c45bc900998b5e11c03023264208James Dong//! convert from H.263 QP to H.264 quant given by: quant=pow(2,QP/6) 9929a84457aed4c45bc900998b5e11c03023264208James Dongconst int QP2QUANT[40] = 10029a84457aed4c45bc900998b5e11c03023264208James Dong{ 10129a84457aed4c45bc900998b5e11c03023264208James Dong 1, 1, 1, 1, 2, 2, 2, 2, 10229a84457aed4c45bc900998b5e11c03023264208James Dong 3, 3, 3, 4, 4, 4, 5, 6, 10329a84457aed4c45bc900998b5e11c03023264208James Dong 6, 7, 8, 9, 10, 11, 13, 14, 10429a84457aed4c45bc900998b5e11c03023264208James Dong 16, 18, 20, 23, 25, 29, 32, 36, 10529a84457aed4c45bc900998b5e11c03023264208James Dong 40, 45, 51, 57, 64, 72, 81, 91 10629a84457aed4c45bc900998b5e11c03023264208James Dong}; 10729a84457aed4c45bc900998b5e11c03023264208James Dong 10829a84457aed4c45bc900998b5e11c03023264208James Dong 10929a84457aed4c45bc900998b5e11c03023264208James Dong/** 11029a84457aed4c45bc900998b5e11c03023264208James DongThis enumeration keeps track of the internal status of the encoder whether it is doing 11129a84457aed4c45bc900998b5e11c03023264208James Dongsomething. The encoding flow follows the order in which these states are. 11229a84457aed4c45bc900998b5e11c03023264208James Dong@publishedAll 11329a84457aed4c45bc900998b5e11c03023264208James Dong*/ 11429a84457aed4c45bc900998b5e11c03023264208James Dongtypedef enum 11529a84457aed4c45bc900998b5e11c03023264208James Dong{ 11629a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_Initializing = 0, 11729a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_Encoding_SPS, 11829a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_Encoding_PPS, 11929a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_Analyzing_Frame, 12029a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_WaitingForBuffer, // pending state 12129a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_Encoding_Frame, 12229a84457aed4c45bc900998b5e11c03023264208James Dong} AVCEnc_State ; 12329a84457aed4c45bc900998b5e11c03023264208James Dong 12429a84457aed4c45bc900998b5e11c03023264208James Dong/** 12529a84457aed4c45bc900998b5e11c03023264208James DongBitstream structure contains bitstream related parameters such as the pointer 12629a84457aed4c45bc900998b5e11c03023264208James Dongto the buffer, the current byte position and bit position. The content of the 12729a84457aed4c45bc900998b5e11c03023264208James DongbitstreamBuffer will be in EBSP format as the emulation prevention codes are 12829a84457aed4c45bc900998b5e11c03023264208James Dongautomatically inserted as the RBSP is recorded. 12929a84457aed4c45bc900998b5e11c03023264208James Dong@publishedAll 13029a84457aed4c45bc900998b5e11c03023264208James Dong*/ 13129a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagEncBitstream 13229a84457aed4c45bc900998b5e11c03023264208James Dong{ 13329a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *bitstreamBuffer; /* pointer to buffer memory */ 13429a84457aed4c45bc900998b5e11c03023264208James Dong int buf_size; /* size of the buffer memory */ 13529a84457aed4c45bc900998b5e11c03023264208James Dong int write_pos; /* next position to write to bitstreamBuffer */ 13629a84457aed4c45bc900998b5e11c03023264208James Dong int count_zeros; /* count number of consecutive zero */ 13729a84457aed4c45bc900998b5e11c03023264208James Dong uint current_word; /* byte-swapped (MSB left) current word to write to buffer */ 13829a84457aed4c45bc900998b5e11c03023264208James Dong int bit_left; /* number of bit left in current_word */ 13929a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *overrunBuffer; /* extra output buffer to prevent current skip due to output buffer overrun*/ 14029a84457aed4c45bc900998b5e11c03023264208James Dong int oBSize; /* size of allocated overrun buffer */ 14129a84457aed4c45bc900998b5e11c03023264208James Dong void *encvid; /* pointer to the main object */ 14229a84457aed4c45bc900998b5e11c03023264208James Dong 14329a84457aed4c45bc900998b5e11c03023264208James Dong} AVCEncBitstream; 14429a84457aed4c45bc900998b5e11c03023264208James Dong 14529a84457aed4c45bc900998b5e11c03023264208James Dong/** 14629a84457aed4c45bc900998b5e11c03023264208James DongThis structure is used for rate control purpose and other performance related control 14729a84457aed4c45bc900998b5e11c03023264208James Dongvariables such as, RD cost, statistics, motion search stuffs, etc. 14829a84457aed4c45bc900998b5e11c03023264208James Dongshould be in this structure. 14929a84457aed4c45bc900998b5e11c03023264208James Dong@publishedAll 15029a84457aed4c45bc900998b5e11c03023264208James Dong*/ 15129a84457aed4c45bc900998b5e11c03023264208James Dong 15229a84457aed4c45bc900998b5e11c03023264208James Dong 15329a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagRDInfo 15429a84457aed4c45bc900998b5e11c03023264208James Dong{ 15529a84457aed4c45bc900998b5e11c03023264208James Dong int QP; 15629a84457aed4c45bc900998b5e11c03023264208James Dong int actual_bits; 15729a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat mad; 15829a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat R_D; 15929a84457aed4c45bc900998b5e11c03023264208James Dong} RDInfo; 16029a84457aed4c45bc900998b5e11c03023264208James Dong 16129a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagMultiPass 16229a84457aed4c45bc900998b5e11c03023264208James Dong{ 16329a84457aed4c45bc900998b5e11c03023264208James Dong /* multipass rate control data */ 16429a84457aed4c45bc900998b5e11c03023264208James Dong int target_bits; /* target bits for current frame, = rc->T */ 16529a84457aed4c45bc900998b5e11c03023264208James Dong int actual_bits; /* actual bits for current frame obtained after encoding, = rc->Rc*/ 16629a84457aed4c45bc900998b5e11c03023264208James Dong int QP; /* quantization level for current frame, = rc->Qc*/ 16729a84457aed4c45bc900998b5e11c03023264208James Dong int prev_QP; /* quantization level for previous frame */ 16829a84457aed4c45bc900998b5e11c03023264208James Dong int prev_prev_QP; /* quantization level for previous frame before last*/ 16929a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat mad; /* mad for current frame, = video->avgMAD*/ 17029a84457aed4c45bc900998b5e11c03023264208James Dong int bitrate; /* bitrate for current frame */ 17129a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat framerate; /* framerate for current frame*/ 17229a84457aed4c45bc900998b5e11c03023264208James Dong 17329a84457aed4c45bc900998b5e11c03023264208James Dong int nRe_Quantized; /* control variable for multipass encoding, */ 17429a84457aed4c45bc900998b5e11c03023264208James Dong /* 0 : first pass */ 17529a84457aed4c45bc900998b5e11c03023264208James Dong /* 1 : intermediate pass(quantization and VLC loop only) */ 17629a84457aed4c45bc900998b5e11c03023264208James Dong /* 2 : final pass(de-quantization, idct, etc) */ 17729a84457aed4c45bc900998b5e11c03023264208James Dong /* 3 : macroblock level rate control */ 17829a84457aed4c45bc900998b5e11c03023264208James Dong 17929a84457aed4c45bc900998b5e11c03023264208James Dong int encoded_frames; /* counter for all encoded frames */ 18029a84457aed4c45bc900998b5e11c03023264208James Dong int re_encoded_frames; /* counter for all multipass encoded frames*/ 18129a84457aed4c45bc900998b5e11c03023264208James Dong int re_encoded_times; /* counter for all times of multipass frame encoding */ 18229a84457aed4c45bc900998b5e11c03023264208James Dong 18329a84457aed4c45bc900998b5e11c03023264208James Dong /* Multiple frame prediction*/ 18429a84457aed4c45bc900998b5e11c03023264208James Dong RDInfo **pRDSamples; /* pRDSamples[30][32], 30->30fps, 32 -> 5 bit quantizer, 32 candidates*/ 18529a84457aed4c45bc900998b5e11c03023264208James Dong int framePos; /* specific position in previous multiple frames*/ 18629a84457aed4c45bc900998b5e11c03023264208James Dong int frameRange; /* number of overall previous multiple frames */ 18729a84457aed4c45bc900998b5e11c03023264208James Dong int samplesPerFrame[30]; /* number of samples per frame, 30->30fps */ 18829a84457aed4c45bc900998b5e11c03023264208James Dong 18929a84457aed4c45bc900998b5e11c03023264208James Dong /* Bit allocation for scene change frames and high motion frames */ 19029a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat sum_mad; 19129a84457aed4c45bc900998b5e11c03023264208James Dong int counter_BTsrc; /* BT = Bit Transfer, bit transfer from low motion frames or less complicatedly compressed frames */ 19229a84457aed4c45bc900998b5e11c03023264208James Dong int counter_BTdst; /* BT = Bit Transfer, bit transfer to scene change frames or high motion frames or more complicatedly compressed frames */ 19329a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat sum_QP; 19429a84457aed4c45bc900998b5e11c03023264208James Dong int diff_counter; /* diff_counter = -diff_counter_BTdst, or diff_counter_BTsrc */ 19529a84457aed4c45bc900998b5e11c03023264208James Dong 19629a84457aed4c45bc900998b5e11c03023264208James Dong /* For target bitrate or framerate update */ 19729a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat target_bits_per_frame; /* = C = bitrate/framerate */ 19829a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat target_bits_per_frame_prev; /* previous C */ 19929a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat aver_mad; /* so-far average mad could replace sum_mad */ 20029a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat aver_mad_prev; /* previous average mad */ 20129a84457aed4c45bc900998b5e11c03023264208James Dong int overlapped_win_size; /* transition period of time */ 20229a84457aed4c45bc900998b5e11c03023264208James Dong int encoded_frames_prev; /* previous encoded_frames */ 20329a84457aed4c45bc900998b5e11c03023264208James Dong} MultiPass; 20429a84457aed4c45bc900998b5e11c03023264208James Dong 20529a84457aed4c45bc900998b5e11c03023264208James Dong 20629a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagdataPointArray 20729a84457aed4c45bc900998b5e11c03023264208James Dong{ 20829a84457aed4c45bc900998b5e11c03023264208James Dong int Qp; 20929a84457aed4c45bc900998b5e11c03023264208James Dong int Rp; 21029a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat Mp; /* for MB-based RC */ 21129a84457aed4c45bc900998b5e11c03023264208James Dong struct tagdataPointArray *next; 21229a84457aed4c45bc900998b5e11c03023264208James Dong struct tagdataPointArray *prev; 21329a84457aed4c45bc900998b5e11c03023264208James Dong} dataPointArray; 21429a84457aed4c45bc900998b5e11c03023264208James Dong 21529a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagAVCRateControl 21629a84457aed4c45bc900998b5e11c03023264208James Dong{ 21729a84457aed4c45bc900998b5e11c03023264208James Dong 21829a84457aed4c45bc900998b5e11c03023264208James Dong /* these parameters are initialized by the users AVCEncParams */ 21929a84457aed4c45bc900998b5e11c03023264208James Dong /* bitrate-robustness tradeoff */ 22029a84457aed4c45bc900998b5e11c03023264208James Dong uint scdEnable; /* enable scene change detection */ 22129a84457aed4c45bc900998b5e11c03023264208James Dong int idrPeriod; /* IDR period in number of frames */ 22229a84457aed4c45bc900998b5e11c03023264208James Dong int intraMBRate; /* intra MB refresh rate per frame */ 22329a84457aed4c45bc900998b5e11c03023264208James Dong uint dpEnable; /* enable data partitioning */ 22429a84457aed4c45bc900998b5e11c03023264208James Dong 22529a84457aed4c45bc900998b5e11c03023264208James Dong /* quality-complexity tradeoff */ 22629a84457aed4c45bc900998b5e11c03023264208James Dong uint subPelEnable; /* enable quarter pel search */ 22729a84457aed4c45bc900998b5e11c03023264208James Dong int mvRange; /* motion vector search range in +/- pixel */ 22829a84457aed4c45bc900998b5e11c03023264208James Dong uint subMBEnable; /* enable sub MB prediction mode (4x4, 4x8, 8x4) */ 22929a84457aed4c45bc900998b5e11c03023264208James Dong uint rdOptEnable; /* enable RD-opt mode selection */ 23029a84457aed4c45bc900998b5e11c03023264208James Dong uint twoPass; /* flag for 2 pass encoding ( for future )*/ 23129a84457aed4c45bc900998b5e11c03023264208James Dong uint bidirPred; /* bi-directional prediction for B-frame. */ 23229a84457aed4c45bc900998b5e11c03023264208James Dong 23329a84457aed4c45bc900998b5e11c03023264208James Dong uint rcEnable; /* enable rate control, '1' on, '0' const QP */ 23429a84457aed4c45bc900998b5e11c03023264208James Dong int initQP; /* initial QP */ 23529a84457aed4c45bc900998b5e11c03023264208James Dong 23629a84457aed4c45bc900998b5e11c03023264208James Dong /* note the following 3 params are for HRD, these triplets can be a series 23729a84457aed4c45bc900998b5e11c03023264208James Dong of triplets as the generalized HRD allows. SEI message must be generated in this case. */ 23829a84457aed4c45bc900998b5e11c03023264208James Dong /* We no longer have to differentiate between CBR and VBR. The users to the 23929a84457aed4c45bc900998b5e11c03023264208James Dong AVC encoder lib will do the mapping from CBR/VBR to these parameters. */ 24029a84457aed4c45bc900998b5e11c03023264208James Dong int32 bitRate; /* target bit rate for the overall clip in bits/second*/ 24129a84457aed4c45bc900998b5e11c03023264208James Dong int32 cpbSize; /* coded picture buffer size in bytes */ 24229a84457aed4c45bc900998b5e11c03023264208James Dong int32 initDelayOffset; /* initial CBP removal delay in bits */ 24329a84457aed4c45bc900998b5e11c03023264208James Dong 24429a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat frame_rate; /* frame rate */ 24529a84457aed4c45bc900998b5e11c03023264208James Dong int srcInterval; /* source frame rate in msec */ 24629a84457aed4c45bc900998b5e11c03023264208James Dong int basicUnit; /* number of macroblocks per BU */ 24729a84457aed4c45bc900998b5e11c03023264208James Dong 24829a84457aed4c45bc900998b5e11c03023264208James Dong /* Then internal parameters for the operation */ 24929a84457aed4c45bc900998b5e11c03023264208James Dong uint first_frame; /* a flag for the first frame */ 25029a84457aed4c45bc900998b5e11c03023264208James Dong int lambda_mf; /* for example */ 25129a84457aed4c45bc900998b5e11c03023264208James Dong int totalSAD; /* SAD of current frame */ 25229a84457aed4c45bc900998b5e11c03023264208James Dong 25329a84457aed4c45bc900998b5e11c03023264208James Dong /*******************************************/ 25429a84457aed4c45bc900998b5e11c03023264208James Dong /* this part comes from MPEG4 rate control */ 25529a84457aed4c45bc900998b5e11c03023264208James Dong int alpha; /* weight for I frame */ 25629a84457aed4c45bc900998b5e11c03023264208James Dong int Rs; /*bit rate for the sequence (or segment) e.g., 24000 bits/sec */ 25729a84457aed4c45bc900998b5e11c03023264208James Dong int Rc; /*bits used for the current frame. It is the bit count obtained after encoding. */ 25829a84457aed4c45bc900998b5e11c03023264208James Dong int Rp; /*bits to be removed from the buffer per picture. */ 25929a84457aed4c45bc900998b5e11c03023264208James Dong /*? is this the average one, or just the bits coded for the previous frame */ 26029a84457aed4c45bc900998b5e11c03023264208James Dong int Rps; /*bit to be removed from buffer per src frame */ 26129a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat Ts; /*number of seconds for the sequence (or segment). e.g., 10 sec */ 26229a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat Ep; 26329a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat Ec; /*mean absolute difference for the current frame after motion compensation.*/ 26429a84457aed4c45bc900998b5e11c03023264208James Dong /*If the macroblock is intra coded, the original spatial pixel values are summed.*/ 26529a84457aed4c45bc900998b5e11c03023264208James Dong int Qc; /*quantization level used for the current frame. */ 26629a84457aed4c45bc900998b5e11c03023264208James Dong int Nr; /*number of P frames remaining for encoding.*/ 26729a84457aed4c45bc900998b5e11c03023264208James Dong int Rr; /*number of bits remaining for encoding this sequence (or segment).*/ 26829a84457aed4c45bc900998b5e11c03023264208James Dong int Rr_Old; 26929a84457aed4c45bc900998b5e11c03023264208James Dong int T; /*target bit to be used for the current frame.*/ 27029a84457aed4c45bc900998b5e11c03023264208James Dong int S; /*number of bits used for encoding the previous frame.*/ 27129a84457aed4c45bc900998b5e11c03023264208James Dong int Hc; /*header and motion vector bits used in the current frame. It includes all the information except to the residual information.*/ 27229a84457aed4c45bc900998b5e11c03023264208James Dong int Hp; /*header and motion vector bits used in the previous frame. It includes all the information except to the residual information.*/ 27329a84457aed4c45bc900998b5e11c03023264208James Dong int Ql; /*quantization level used in the previous frame */ 27429a84457aed4c45bc900998b5e11c03023264208James Dong int Bs; /*buffer size e.g., R/2 */ 27529a84457aed4c45bc900998b5e11c03023264208James Dong int B; /*current buffer level e.g., R/4 - start from the middle of the buffer */ 27629a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat X1; 27729a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat X2; 27829a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat X11; 27929a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat M; /*safe margin for the buffer */ 28029a84457aed4c45bc900998b5e11c03023264208James Dong OsclFloat smTick; /*ratio of src versus enc frame rate */ 28129a84457aed4c45bc900998b5e11c03023264208James Dong double remnant; /*remainder frame of src/enc frame for fine frame skipping */ 28229a84457aed4c45bc900998b5e11c03023264208James Dong int timeIncRes; /* vol->timeIncrementResolution */ 28329a84457aed4c45bc900998b5e11c03023264208James Dong 28429a84457aed4c45bc900998b5e11c03023264208James Dong dataPointArray *end; /*quantization levels for the past (20) frames */ 28529a84457aed4c45bc900998b5e11c03023264208James Dong 28629a84457aed4c45bc900998b5e11c03023264208James Dong int frameNumber; /* ranging from 0 to 20 nodes*/ 28729a84457aed4c45bc900998b5e11c03023264208James Dong int w; 28829a84457aed4c45bc900998b5e11c03023264208James Dong int Nr_Original; 28929a84457aed4c45bc900998b5e11c03023264208James Dong int Nr_Old, Nr_Old2; 29029a84457aed4c45bc900998b5e11c03023264208James Dong int skip_next_frame; 29129a84457aed4c45bc900998b5e11c03023264208James Dong int Qdep; /* smooth Q adjustment */ 29229a84457aed4c45bc900998b5e11c03023264208James Dong int VBR_Enabled; 29329a84457aed4c45bc900998b5e11c03023264208James Dong 29429a84457aed4c45bc900998b5e11c03023264208James Dong int totalFrameNumber; /* total coded frames, for debugging!!*/ 29529a84457aed4c45bc900998b5e11c03023264208James Dong 29629a84457aed4c45bc900998b5e11c03023264208James Dong char oFirstTime; 29729a84457aed4c45bc900998b5e11c03023264208James Dong 29829a84457aed4c45bc900998b5e11c03023264208James Dong int numFrameBits; /* keep track of number of bits of the current frame */ 29929a84457aed4c45bc900998b5e11c03023264208James Dong int NumberofHeaderBits; 30029a84457aed4c45bc900998b5e11c03023264208James Dong int NumberofTextureBits; 30129a84457aed4c45bc900998b5e11c03023264208James Dong int numMBHeaderBits; 30229a84457aed4c45bc900998b5e11c03023264208James Dong int numMBTextureBits; 30329a84457aed4c45bc900998b5e11c03023264208James Dong double *MADofMB; 30429a84457aed4c45bc900998b5e11c03023264208James Dong int32 bitsPerFrame; 30529a84457aed4c45bc900998b5e11c03023264208James Dong 30629a84457aed4c45bc900998b5e11c03023264208James Dong /* BX rate control, something like TMN8 rate control*/ 30729a84457aed4c45bc900998b5e11c03023264208James Dong 30829a84457aed4c45bc900998b5e11c03023264208James Dong MultiPass *pMP; 30929a84457aed4c45bc900998b5e11c03023264208James Dong 31029a84457aed4c45bc900998b5e11c03023264208James Dong int TMN_W; 31129a84457aed4c45bc900998b5e11c03023264208James Dong int TMN_TH; 31229a84457aed4c45bc900998b5e11c03023264208James Dong int VBV_fullness; 31329a84457aed4c45bc900998b5e11c03023264208James Dong int max_BitVariance_num; /* the number of the maximum bit variance within the given buffer with the unit of 10% of bitrate/framerate*/ 31429a84457aed4c45bc900998b5e11c03023264208James Dong int encoded_frames; /* counter for all encoded frames */ 31529a84457aed4c45bc900998b5e11c03023264208James Dong int low_bound; /* bound for underflow detection, usually low_bound=-Bs/2, but could be changed in H.263 mode */ 31629a84457aed4c45bc900998b5e11c03023264208James Dong int VBV_fullness_offset; /* offset of VBV_fullness, usually is zero, but can be changed in H.263 mode*/ 31729a84457aed4c45bc900998b5e11c03023264208James Dong /* End BX */ 31829a84457aed4c45bc900998b5e11c03023264208James Dong 31929a84457aed4c45bc900998b5e11c03023264208James Dong} AVCRateControl; 32029a84457aed4c45bc900998b5e11c03023264208James Dong 32129a84457aed4c45bc900998b5e11c03023264208James Dong 32229a84457aed4c45bc900998b5e11c03023264208James Dong/** 32329a84457aed4c45bc900998b5e11c03023264208James DongThis structure is for the motion vector information. */ 32429a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagMV 32529a84457aed4c45bc900998b5e11c03023264208James Dong{ 32629a84457aed4c45bc900998b5e11c03023264208James Dong int x; 32729a84457aed4c45bc900998b5e11c03023264208James Dong int y; 32829a84457aed4c45bc900998b5e11c03023264208James Dong uint sad; 32929a84457aed4c45bc900998b5e11c03023264208James Dong} AVCMV; 33029a84457aed4c45bc900998b5e11c03023264208James Dong 33129a84457aed4c45bc900998b5e11c03023264208James Dong/** 33229a84457aed4c45bc900998b5e11c03023264208James DongThis structure contains function pointers for different platform dependent implementation of 33329a84457aed4c45bc900998b5e11c03023264208James Dongfunctions. */ 33429a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagAVCEncFuncPtr 33529a84457aed4c45bc900998b5e11c03023264208James Dong{ 33629a84457aed4c45bc900998b5e11c03023264208James Dong 33729a84457aed4c45bc900998b5e11c03023264208James Dong int (*SAD_MB_HalfPel[4])(uint8*, uint8*, int, void *); 33829a84457aed4c45bc900998b5e11c03023264208James Dong int (*SAD_Macroblock)(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info); 33929a84457aed4c45bc900998b5e11c03023264208James Dong 34029a84457aed4c45bc900998b5e11c03023264208James Dong} AVCEncFuncPtr; 34129a84457aed4c45bc900998b5e11c03023264208James Dong 34229a84457aed4c45bc900998b5e11c03023264208James Dong/** 34329a84457aed4c45bc900998b5e11c03023264208James DongThis structure contains information necessary for correct padding. 34429a84457aed4c45bc900998b5e11c03023264208James Dong*/ 34529a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagPadInfo 34629a84457aed4c45bc900998b5e11c03023264208James Dong{ 34729a84457aed4c45bc900998b5e11c03023264208James Dong int i; 34829a84457aed4c45bc900998b5e11c03023264208James Dong int width; 34929a84457aed4c45bc900998b5e11c03023264208James Dong int j; 35029a84457aed4c45bc900998b5e11c03023264208James Dong int height; 35129a84457aed4c45bc900998b5e11c03023264208James Dong} AVCPadInfo; 35229a84457aed4c45bc900998b5e11c03023264208James Dong 35329a84457aed4c45bc900998b5e11c03023264208James Dong 35429a84457aed4c45bc900998b5e11c03023264208James Dong#ifdef HTFM 35529a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagHTFM_Stat 35629a84457aed4c45bc900998b5e11c03023264208James Dong{ 35729a84457aed4c45bc900998b5e11c03023264208James Dong int abs_dif_mad_avg; 35829a84457aed4c45bc900998b5e11c03023264208James Dong uint countbreak; 35929a84457aed4c45bc900998b5e11c03023264208James Dong int offsetArray[16]; 36029a84457aed4c45bc900998b5e11c03023264208James Dong int offsetRef[16]; 36129a84457aed4c45bc900998b5e11c03023264208James Dong} HTFM_Stat; 36229a84457aed4c45bc900998b5e11c03023264208James Dong#endif 36329a84457aed4c45bc900998b5e11c03023264208James Dong 36429a84457aed4c45bc900998b5e11c03023264208James Dong 36529a84457aed4c45bc900998b5e11c03023264208James Dong/** 36629a84457aed4c45bc900998b5e11c03023264208James DongThis structure is the main object for AVC encoder library providing access to all 36729a84457aed4c45bc900998b5e11c03023264208James Dongglobal variables. It is allocated at PVAVCInitEncoder and freed at PVAVCCleanUpEncoder. 36829a84457aed4c45bc900998b5e11c03023264208James Dong@publishedAll 36929a84457aed4c45bc900998b5e11c03023264208James Dong*/ 37029a84457aed4c45bc900998b5e11c03023264208James Dongtypedef struct tagEncObject 37129a84457aed4c45bc900998b5e11c03023264208James Dong{ 37229a84457aed4c45bc900998b5e11c03023264208James Dong 37329a84457aed4c45bc900998b5e11c03023264208James Dong AVCCommonObj *common; 37429a84457aed4c45bc900998b5e11c03023264208James Dong 37529a84457aed4c45bc900998b5e11c03023264208James Dong AVCEncBitstream *bitstream; /* for current NAL */ 37629a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *overrunBuffer; /* extra output buffer to prevent current skip due to output buffer overrun*/ 37729a84457aed4c45bc900998b5e11c03023264208James Dong int oBSize; /* size of allocated overrun buffer */ 37829a84457aed4c45bc900998b5e11c03023264208James Dong 37929a84457aed4c45bc900998b5e11c03023264208James Dong /* rate control */ 38029a84457aed4c45bc900998b5e11c03023264208James Dong AVCRateControl *rateCtrl; /* pointer to the rate control structure */ 38129a84457aed4c45bc900998b5e11c03023264208James Dong 38229a84457aed4c45bc900998b5e11c03023264208James Dong /* encoding operation */ 38329a84457aed4c45bc900998b5e11c03023264208James Dong AVCEnc_State enc_state; /* encoding state */ 38429a84457aed4c45bc900998b5e11c03023264208James Dong 38529a84457aed4c45bc900998b5e11c03023264208James Dong AVCFrameIO *currInput; /* pointer to the current input frame */ 38629a84457aed4c45bc900998b5e11c03023264208James Dong 38729a84457aed4c45bc900998b5e11c03023264208James Dong int currSliceGroup; /* currently encoded slice group id */ 38829a84457aed4c45bc900998b5e11c03023264208James Dong 38929a84457aed4c45bc900998b5e11c03023264208James Dong int level[24][16], run[24][16]; /* scratch memory */ 39029a84457aed4c45bc900998b5e11c03023264208James Dong int leveldc[16], rundc[16]; /* for DC component */ 39129a84457aed4c45bc900998b5e11c03023264208James Dong int levelcdc[16], runcdc[16]; /* for chroma DC component */ 39229a84457aed4c45bc900998b5e11c03023264208James Dong int numcoefcdc[2]; /* number of coefficient for chroma DC */ 39329a84457aed4c45bc900998b5e11c03023264208James Dong int numcoefdc; /* number of coefficients for DC component */ 39429a84457aed4c45bc900998b5e11c03023264208James Dong 39529a84457aed4c45bc900998b5e11c03023264208James Dong int qp_const; 39629a84457aed4c45bc900998b5e11c03023264208James Dong int qp_const_c; 39729a84457aed4c45bc900998b5e11c03023264208James Dong /********* intra prediction scratch memory **********************/ 39829a84457aed4c45bc900998b5e11c03023264208James Dong uint8 pred_i16[AVCNumI16PredMode][256]; /* save prediction for MB */ 39929a84457aed4c45bc900998b5e11c03023264208James Dong uint8 pred_i4[AVCNumI4PredMode][16]; /* save prediction for blk */ 40029a84457aed4c45bc900998b5e11c03023264208James Dong uint8 pred_ic[AVCNumIChromaMode][128]; /* for 2 chroma */ 40129a84457aed4c45bc900998b5e11c03023264208James Dong 40229a84457aed4c45bc900998b5e11c03023264208James Dong int mostProbableI4Mode[16]; /* in raster scan order */ 40329a84457aed4c45bc900998b5e11c03023264208James Dong /********* motion compensation related variables ****************/ 40429a84457aed4c45bc900998b5e11c03023264208James Dong AVCMV *mot16x16; /* Saved motion vectors for 16x16 block*/ 40529a84457aed4c45bc900998b5e11c03023264208James Dong AVCMV(*mot16x8)[2]; /* Saved motion vectors for 16x8 block*/ 40629a84457aed4c45bc900998b5e11c03023264208James Dong AVCMV(*mot8x16)[2]; /* Saved motion vectors for 8x16 block*/ 40729a84457aed4c45bc900998b5e11c03023264208James Dong AVCMV(*mot8x8)[4]; /* Saved motion vectors for 8x8 block*/ 40829a84457aed4c45bc900998b5e11c03023264208James Dong 40929a84457aed4c45bc900998b5e11c03023264208James Dong /********* subpel position **************************************/ 41029a84457aed4c45bc900998b5e11c03023264208James Dong uint32 subpel_pred[SUBPEL_PRED_BLK_SIZE/*<<2*/]; /* all 16 sub-pel positions */ 41129a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *hpel_cand[9]; /* pointer to half-pel position */ 41229a84457aed4c45bc900998b5e11c03023264208James Dong int best_hpel_pos; /* best position */ 41329a84457aed4c45bc900998b5e11c03023264208James Dong uint8 qpel_cand[8][24*16]; /* pointer to quarter-pel position */ 41429a84457aed4c45bc900998b5e11c03023264208James Dong int best_qpel_pos; 41529a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *bilin_base[9][4]; /* pointer to 4 position at top left of bilinear quarter-pel */ 41629a84457aed4c45bc900998b5e11c03023264208James Dong 41729a84457aed4c45bc900998b5e11c03023264208James Dong /* need for intra refresh rate */ 41829a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *intraSearch; /* Intra Array for MBs to be intra searched */ 41929a84457aed4c45bc900998b5e11c03023264208James Dong uint firstIntraRefreshMBIndx; /* keep track for intra refresh */ 42029a84457aed4c45bc900998b5e11c03023264208James Dong 42129a84457aed4c45bc900998b5e11c03023264208James Dong int i4_sad; /* temporary for i4 mode SAD */ 42229a84457aed4c45bc900998b5e11c03023264208James Dong int *min_cost; /* Minimum cost for the all MBs */ 42329a84457aed4c45bc900998b5e11c03023264208James Dong int lambda_mode; /* Lagrange parameter for mode selection */ 42429a84457aed4c45bc900998b5e11c03023264208James Dong int lambda_motion; /* Lagrange parameter for MV selection */ 42529a84457aed4c45bc900998b5e11c03023264208James Dong 42629a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *mvbits_array; /* Table for bits spent in the cost funciton */ 42729a84457aed4c45bc900998b5e11c03023264208James Dong uint8 *mvbits; /* An offset to the above array. */ 42829a84457aed4c45bc900998b5e11c03023264208James Dong 42929a84457aed4c45bc900998b5e11c03023264208James Dong /* to speedup the SAD calculation */ 43029a84457aed4c45bc900998b5e11c03023264208James Dong void *sad_extra_info; 43129a84457aed4c45bc900998b5e11c03023264208James Dong uint8 currYMB[256]; /* interleaved current macroblock in HTFM order */ 43229a84457aed4c45bc900998b5e11c03023264208James Dong 43329a84457aed4c45bc900998b5e11c03023264208James Dong#ifdef HTFM 43429a84457aed4c45bc900998b5e11c03023264208James Dong int nrmlz_th[48]; /* Threshold for fast SAD calculation using HTFM */ 43529a84457aed4c45bc900998b5e11c03023264208James Dong HTFM_Stat htfm_stat; /* For statistics collection */ 43629a84457aed4c45bc900998b5e11c03023264208James Dong#endif 43729a84457aed4c45bc900998b5e11c03023264208James Dong 43829a84457aed4c45bc900998b5e11c03023264208James Dong /* statistics */ 43929a84457aed4c45bc900998b5e11c03023264208James Dong int numIntraMB; /* keep track of number of intra MB */ 44029a84457aed4c45bc900998b5e11c03023264208James Dong 44129a84457aed4c45bc900998b5e11c03023264208James Dong /* encoding complexity control */ 44229a84457aed4c45bc900998b5e11c03023264208James Dong uint fullsearch_enable; /* flag to enable full-pel full-search */ 44329a84457aed4c45bc900998b5e11c03023264208James Dong 44429a84457aed4c45bc900998b5e11c03023264208James Dong /* misc.*/ 44529a84457aed4c45bc900998b5e11c03023264208James Dong bool outOfBandParamSet; /* flag to enable out-of-band param set */ 44629a84457aed4c45bc900998b5e11c03023264208James Dong 44729a84457aed4c45bc900998b5e11c03023264208James Dong AVCSeqParamSet extSPS; /* for external SPS */ 44829a84457aed4c45bc900998b5e11c03023264208James Dong AVCPicParamSet extPPS; /* for external PPS */ 44929a84457aed4c45bc900998b5e11c03023264208James Dong 45029a84457aed4c45bc900998b5e11c03023264208James Dong /* time control */ 45129a84457aed4c45bc900998b5e11c03023264208James Dong uint32 prevFrameNum; /* previous frame number starting from modTimeRef */ 45229a84457aed4c45bc900998b5e11c03023264208James Dong uint32 modTimeRef; /* Reference modTime update every I-Vop*/ 45329a84457aed4c45bc900998b5e11c03023264208James Dong uint32 wrapModTime; /* Offset to modTime Ref, rarely used */ 45429a84457aed4c45bc900998b5e11c03023264208James Dong 45529a84457aed4c45bc900998b5e11c03023264208James Dong uint prevProcFrameNum; /* previously processed frame number, could be skipped */ 45629a84457aed4c45bc900998b5e11c03023264208James Dong uint prevCodedFrameNum; /* previously encoded frame number */ 45729a84457aed4c45bc900998b5e11c03023264208James Dong /* POC related variables */ 45829a84457aed4c45bc900998b5e11c03023264208James Dong uint32 dispOrdPOCRef; /* reference POC is displayer order unit. */ 45929a84457aed4c45bc900998b5e11c03023264208James Dong 46029a84457aed4c45bc900998b5e11c03023264208James Dong /* Function pointers */ 46129a84457aed4c45bc900998b5e11c03023264208James Dong AVCEncFuncPtr *functionPointer; /* store pointers to platform specific functions */ 46229a84457aed4c45bc900998b5e11c03023264208James Dong 46329a84457aed4c45bc900998b5e11c03023264208James Dong /* Application control data */ 46429a84457aed4c45bc900998b5e11c03023264208James Dong AVCHandle *avcHandle; 46529a84457aed4c45bc900998b5e11c03023264208James Dong 46629a84457aed4c45bc900998b5e11c03023264208James Dong 46729a84457aed4c45bc900998b5e11c03023264208James Dong} AVCEncObject; 46829a84457aed4c45bc900998b5e11c03023264208James Dong 46929a84457aed4c45bc900998b5e11c03023264208James Dong 47029a84457aed4c45bc900998b5e11c03023264208James Dong#endif /*AVCENC_INT_H_INCLUDED*/ 47129a84457aed4c45bc900998b5e11c03023264208James Dong 472