1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/**
19This file contains declarations of internal functions for AVC decoder library.
20@publishedAll
21*/
22#ifndef AVCENC_LIB_H_INCLUDED
23#define AVCENC_LIB_H_INCLUDED
24
25#ifndef AVCLIB_COMMON_H_INCLUDED
26#include "avclib_common.h"
27#endif
28#ifndef AVCENC_INT_H_INCLUDED
29#include "avcenc_int.h"
30#endif
31
32#ifdef __cplusplus
33extern "C"
34{
35#endif
36    /*------------- block.c -------------------------*/
37
38    /**
39    This function perform residue calculation, transform, quantize, inverse quantize,
40    inverse transform and residue compensation on a 4x4 block.
41    \param "encvid" "Pointer to AVCEncObject."
42    \param "blkidx"  "raster scan block index of the current 4x4 block."
43    \param "cur"    "Pointer to the reconstructed block."
44    \param "org"    "Pointer to the original block."
45    \param "coef_cost"  "Pointer to the coefficient cost to be filled in and returned."
46    \return "Number of non-zero coefficients."
47    */
48    int dct_luma(AVCEncObject *encvid, int blkidx, uint8 *cur, uint8 *org, int *coef_cost);
49
50    /**
51    This function performs IDCT on an INTER macroblock.
52    \param "video"  "Pointer to AVCCommonObj."
53    \param "curL"   "Pointer to the origin of the macroblock on the current frame."
54    \param "currMB" "Pointer to the AVCMacroblock structure."
55    \param "picPitch" "Pitch of the current frame."
56    \return "void".
57    */
58    void MBInterIdct(AVCCommonObj *video, uint8 *curL, AVCMacroblock *currMB, int picPitch);
59
60    /**
61    This function perform residue calculation, transform, quantize, inverse quantize,
62    inverse transform and residue compensation on a macroblock.
63    \param "encvid" "Pointer to AVCEncObject."
64    \param "curL"   "Pointer to the reconstructed MB."
65    \param "orgL"    "Pointer to the original MB."
66    \return "void"
67    */
68    void dct_luma_16x16(AVCEncObject *encvid, uint8 *curL, uint8 *orgL);
69
70    /**
71    This function perform residue calculation, transform, quantize, inverse quantize,
72    inverse transform and residue compensation for chroma components of an MB.
73    \param "encvid" "Pointer to AVCEncObject."
74    \param "curC"   "Pointer to the reconstructed MB."
75    \param "orgC"    "Pointer to the original MB."
76    \param "cr"     "Flag whether it is Cr or not."
77    \return "void"
78    */
79    void dct_chroma(AVCEncObject *encvid, uint8 *curC, uint8 *orgC, int cr);
80
81    /*----------- init.c ------------------*/
82    /**
83    This function interprets the encoding parameters provided by users in encParam.
84    The results are kept in AVCEncObject, AVCSeqParamSet, AVCPicParamSet and AVCSliceHeader.
85    \param "encvid"     "Pointer to AVCEncObject."
86    \param "encParam"   "Pointer to AVCEncParam."
87    \param "extSPS"     "External SPS template to be followed. NULL if not present."
88    \param "extPPS"     "External PPS template to be followed. NULL if not present."
89    \return "see AVCEnc_Status."
90    */
91    AVCEnc_Status  SetEncodeParam(AVCHandle *avcHandle, AVCEncParams *encParam,
92                                  void *extSPS, void *extPPS);
93
94    /**
95    This function verifies the encoding parameters whether they meet the set of supported
96    tool by a specific profile. If the profile is not set, it will just find the closest
97    profile instead of verifying it.
98    \param "video"  "Pointer to AVCEncObject."
99    \param "seqParam"   "Pointer to AVCSeqParamSet."
100    \param "picParam"   "Pointer to AVCPicParamSet."
101    \return "AVCENC_SUCCESS if success,
102            AVCENC_PROFILE_NOT_SUPPORTED if the specified profile
103                is not supported by this version of the library,
104            AVCENC_TOOLS_NOT_SUPPORTED if any of the specified encoding tools are
105            not supported by the user-selected profile."
106    */
107    AVCEnc_Status VerifyProfile(AVCEncObject *video, AVCSeqParamSet *seqParam, AVCPicParamSet *picParam);
108
109    /**
110    This function verifies the encoding parameters whether they meet the requirement
111    for a specific level. If the level is not set, it will just find the closest
112    level instead of verifying it.
113    \param "video"  "Pointer to AVCEncObject."
114    \param "seqParam"   "Pointer to AVCSeqParamSet."
115    \param "picParam"   "Pointer to AVCPicParamSet."
116    \return "AVCENC_SUCCESS if success,
117            AVCENC_LEVEL_NOT_SUPPORTED if the specified level
118                is not supported by this version of the library,
119            AVCENC_LEVEL_FAIL if any of the encoding parameters exceed
120            the range of the user-selected level."
121    */
122    AVCEnc_Status VerifyLevel(AVCEncObject *video, AVCSeqParamSet *seqParam, AVCPicParamSet *picParam);
123
124    /**
125    This funciton initializes the frame encoding by setting poc/frame_num related parameters. it
126    also performs motion estimation.
127    \param "encvid" "Pointer to the AVCEncObject."
128    \return "AVCENC_SUCCESS if success, AVCENC_NO_PICTURE if there is no input picture
129            in the queue to encode, AVCENC_POC_FAIL or AVCENC_CONSECUTIVE_NONREF for POC
130            related errors, AVCENC_NEW_IDR if new IDR is detected."
131    */
132    AVCEnc_Status InitFrame(AVCEncObject *encvid);
133
134    /**
135    This function initializes slice header related variables and other variables necessary
136    for decoding one slice.
137    \param "encvid" "Pointer to the AVCEncObject."
138    \return "AVCENC_SUCCESS if success."
139    */
140    AVCEnc_Status InitSlice(AVCEncObject *encvid);
141
142    /*----------- header.c ----------------*/
143    /**
144    This function performs bitstream encoding of the sequence parameter set NAL.
145    \param "encvid" "Pointer to the AVCEncObject."
146    \param "stream" "Pointer to AVCEncBitstream."
147    \return "AVCENC_SUCCESS if success or AVCENC_SPS_FAIL or others for unexpected failure which
148    should not occur. The SPS parameters should all be verified before this function is called."
149    */
150    AVCEnc_Status EncodeSPS(AVCEncObject *encvid, AVCEncBitstream *stream);
151
152    /**
153    This function encodes the VUI parameters into the sequence parameter set bitstream.
154    \param "stream" "Pointer to AVCEncBitstream."
155    \param "vui"    "Pointer to AVCVUIParams."
156    \return "nothing."
157    */
158    void EncodeVUI(AVCEncBitstream* stream, AVCVUIParams* vui);
159
160    /**
161    This function encodes HRD parameters into the sequence parameter set bitstream
162    \param "stream" "Pointer to AVCEncBitstream."
163    \param "hrd"    "Pointer to AVCHRDParams."
164    \return "nothing."
165    */
166    void EncodeHRD(AVCEncBitstream* stream, AVCHRDParams* hrd);
167
168
169    /**
170    This function performs bitstream encoding of the picture parameter set NAL.
171    \param "encvid" "Pointer to the AVCEncObject."
172    \param "stream" "Pointer to AVCEncBitstream."
173    \return "AVCENC_SUCCESS if success or AVCENC_PPS_FAIL or others for unexpected failure which
174    should not occur. The SPS parameters should all be verified before this function is called."
175    */
176    AVCEnc_Status EncodePPS(AVCEncObject *encvid, AVCEncBitstream *stream);
177
178    /**
179    This function encodes slice header information which has been initialized or fabricated
180    prior to entering this funciton.
181    \param "encvid" "Pointer to the AVCEncObject."
182    \param "stream" "Pointer to AVCEncBitstream."
183    \return "AVCENC_SUCCESS if success or bitstream fail statuses."
184    */
185    AVCEnc_Status EncodeSliceHeader(AVCEncObject *encvid, AVCEncBitstream *stream);
186
187    /**
188    This function encodes reference picture list reordering relted syntax.
189    \param "video" "Pointer to AVCCommonObj."
190    \param "stream" "Pointer to AVCEncBitstream."
191    \param "sliceHdr" "Pointer to AVCSliceHdr."
192    \param "slice_type" "Value of slice_type - 5 if greater than 5."
193    \return "AVCENC_SUCCESS for success and AVCENC_FAIL otherwise."
194    */
195    AVCEnc_Status ref_pic_list_reordering(AVCCommonObj *video, AVCEncBitstream *stream, AVCSliceHeader *sliceHdr, int slice_type);
196
197    /**
198    This function encodes dec_ref_pic_marking related syntax.
199    \param "video" "Pointer to AVCCommonObj."
200    \param "stream" "Pointer to AVCEncBitstream."
201    \param "sliceHdr" "Pointer to AVCSliceHdr."
202    \return "AVCENC_SUCCESS for success and AVCENC_FAIL otherwise."
203    */
204    AVCEnc_Status dec_ref_pic_marking(AVCCommonObj *video, AVCEncBitstream *stream, AVCSliceHeader *sliceHdr);
205
206    /**
207    This function initializes the POC related variables and the POC syntax to be encoded
208    to the slice header derived from the disp_order and is_reference flag of the original
209    input frame to be encoded.
210    \param "video"  "Pointer to the AVCEncObject."
211    \return "AVCENC_SUCCESS if success,
212            AVCENC_POC_FAIL if the poc type is undefined or
213            AVCENC_CONSECUTIVE_NONREF if there are consecutive non-reference frame for POC type 2."
214    */
215    AVCEnc_Status InitPOC(AVCEncObject *video);
216
217    /**
218    This function performs POC related operation after a picture is decoded.
219    \param "video" "Pointer to AVCCommonObj."
220    \return "AVCENC_SUCCESS"
221    */
222    AVCEnc_Status PostPOC(AVCCommonObj *video);
223
224    /*----------- bitstream_io.c ----------------*/
225    /**
226    This function initializes the bitstream structure with the information given by
227    the users.
228    \param "bitstream"  "Pointer to the AVCEncBitstream structure."
229    \param "buffer"     "Pointer to the unsigned char buffer for output."
230    \param "buf_size"   "The size of the buffer in bytes."
231    \param "overrunBuffer"  "Pointer to extra overrun buffer."
232    \param "oBSize"     "Size of overrun buffer in bytes."
233    \return "AVCENC_SUCCESS if success, AVCENC_BITSTREAM_INIT_FAIL if fail"
234    */
235    AVCEnc_Status BitstreamEncInit(AVCEncBitstream *bitstream, uint8 *buffer, int buf_size,
236                                   uint8 *overrunBuffer, int oBSize);
237
238    /**
239    This function writes the data from the cache into the bitstream buffer. It also adds the
240    emulation prevention code if necessary.
241    \param "stream"     "Pointer to the AVCEncBitstream structure."
242    \return "AVCENC_SUCCESS if success or AVCENC_BITSTREAM_BUFFER_FULL if fail."
243    */
244    AVCEnc_Status AVCBitstreamSaveWord(AVCEncBitstream *stream);
245
246    /**
247    This function writes the codeword into the cache which will eventually be written to
248    the bitstream buffer.
249    \param "stream"     "Pointer to the AVCEncBitstream structure."
250    \param "nBits"      "Number of bits in the codeword."
251    \param "code"       "The codeword."
252    \return "AVCENC_SUCCESS if success or AVCENC_BITSTREAM_BUFFER_FULL if fail."
253    */
254    AVCEnc_Status BitstreamWriteBits(AVCEncBitstream *stream, int nBits, uint code);
255
256    /**
257    This function writes one bit of data into the cache which will eventually be written
258    to the bitstream buffer.
259    \param "stream"     "Pointer to the AVCEncBitstream structure."
260    \param "code"       "The codeword."
261    \return "AVCENC_SUCCESS if success or AVCENC_BITSTREAM_BUFFER_FULL if fail."
262    */
263    AVCEnc_Status BitstreamWrite1Bit(AVCEncBitstream *stream, uint code);
264
265    /**
266    This function adds trailing bits to the bitstream and reports back the final EBSP size.
267    \param "stream"     "Pointer to the AVCEncBitstream structure."
268    \param "nal_size"   "Output the final NAL size."
269    \return "AVCENC_SUCCESS if success or AVCENC_BITSTREAM_BUFFER_FULL if fail."
270    */
271    AVCEnc_Status BitstreamTrailingBits(AVCEncBitstream *bitstream, uint *nal_size);
272
273    /**
274    This function checks whether the current bit position is byte-aligned or not.
275    \param "stream" "Pointer to the bitstream structure."
276    \return "true if byte-aligned, false otherwise."
277    */
278    bool byte_aligned(AVCEncBitstream *stream);
279
280
281    /**
282    This function checks the availability of overrun buffer and switches to use it when
283    normal bufffer is not big enough.
284    \param "stream" "Pointer to the bitstream structure."
285    \param "numExtraBytes" "Number of extra byte needed."
286    \return "AVCENC_SUCCESS or AVCENC_FAIL."
287    */
288    AVCEnc_Status AVCBitstreamUseOverrunBuffer(AVCEncBitstream* stream, int numExtraBytes);
289
290
291    /*-------------- intra_est.c ---------------*/
292
293    /** This function performs intra/inter decision based on ABE.
294    \param "encvid" "Pointer to AVCEncObject."
295    \param "min_cost"   "Best inter cost."
296    \param "curL"   "Pointer to the current MB origin in reconstructed frame."
297    \param "picPitch" "Pitch of the reconstructed frame."
298    \return "Boolean for intra mode."
299    */
300
301//bool IntraDecisionABE(AVCEncObject *encvid, int min_cost, uint8 *curL, int picPitch);
302    bool IntraDecision(int *min_cost, uint8 *cur, int pitch, bool ave);
303
304    /**
305    This function performs intra prediction mode search.
306    \param "encvid" "Pointer to AVCEncObject."
307    \param "mbnum"  "Current MB number."
308    \param "curL"   "Pointer to the current MB origin in reconstructed frame."
309    \param "picPitch" "Pitch of the reconstructed frame."
310    \return "void."
311    */
312    void MBIntraSearch(AVCEncObject *encvid, int mbnum, uint8 *curL, int picPitch);
313
314    /**
315    This function generates all the I16 prediction modes for an MB and keep it in
316    encvid->pred_i16.
317    \param "encvid" "Pointer to AVCEncObject."
318    \return "void"
319    */
320    void intrapred_luma_16x16(AVCEncObject *encvid);
321
322    /**
323    This function calculate the cost of all I16 modes and compare them to get the minimum.
324    \param "encvid" "Pointer to AVCEncObject."
325    \param "orgY"   "Pointer to the original luma MB."
326    \param "min_cost" "Pointer to the minimal cost so-far."
327    \return "void"
328    */
329    void find_cost_16x16(AVCEncObject *encvid, uint8 *orgY, int *min_cost);
330
331    /**
332    This function calculates the cost of each I16 mode.
333    \param "org"    "Pointer to the original luma MB."
334    \param "org_pitch" "Stride size of the original frame."
335    \param "pred"   "Pointer to the prediction values."
336    \param "min_cost" "Minimal cost so-far."
337    \return "Cost"
338    */
339
340    int cost_i16(uint8 *org, int org_pitch, uint8 *pred, int min_cost);
341
342    /**
343    This function generates all the I4 prediction modes and select the best one
344    for all the blocks inside a macroblock.It also calls dct_luma to generate the reconstructed
345    MB, and transform coefficients to be encoded.
346    \param "encvid" "Pointer to AVCEncObject."
347    \param "min_cost" "Pointer to the minimal cost so-far."
348    \return "void"
349    */
350    void mb_intra4x4_search(AVCEncObject *encvid, int *min_cost);
351
352    /**
353    This function calculates the most probable I4 mode of a given 4x4 block
354    from neighboring informationaccording to AVC/H.264 standard.
355    \param "video"  "Pointer to AVCCommonObj."
356    \param "blkidx" "The current block index."
357    \return "Most probable mode."
358    */
359    int FindMostProbableI4Mode(AVCCommonObj *video, int blkidx);
360
361    /**
362    This function is where a lot of actions take place in the 4x4 block level inside
363    mb_intra4x4_search.
364    \param "encvid" "Pointer to AVCEncObject."
365    \param "blkidx" "The current 4x4 block index."
366    \param "cur"    "Pointer to the reconstructed block."
367    \param "org"    "Pointer to the original block."
368    \return "Minimal cost, also set currMB->i4Mode"
369    */
370    int blk_intra4x4_search(AVCEncObject *encvid, int blkidx, uint8 *cur, uint8 *org);
371
372    /**
373    This function calculates the cost of a given I4 prediction mode.
374    \param "org"    "Pointer to the original block."
375    \param "org_pitch"  "Stride size of the original frame."
376    \param "pred"   "Pointer to the prediction block. (encvid->pred_i4)"
377    \param "cost"   "Pointer to the minimal cost (to be updated)."
378    \return "void"
379    */
380    void cost_i4(uint8 *org, int org_pitch, uint8 *pred, uint16 *cost);
381
382    /**
383    This function performs chroma intra search. Each mode is saved in encvid->pred_ic.
384    \param "encvid" "Pointer to AVCEncObject."
385    \return "void"
386    */
387    void chroma_intra_search(AVCEncObject *encvid);
388
389    /**
390    This function calculates the cost of a chroma prediction mode.
391    \param "orgCb"  "Pointer to the original Cb block."
392    \param "orgCr"  "Pointer to the original Cr block."
393    \param "org_pitch"  "Stride size of the original frame."
394    \param "pred"   "Pointer to the prediction block (encvid->pred_ic)"
395    \param "mincost"    "Minimal cost so far."
396    \return "Cost."
397    */
398
399    int SATDChroma(uint8 *orgCb, uint8 *orgCr, int org_pitch, uint8 *pred, int mincost);
400
401    /*-------------- motion_comp.c ---------------*/
402
403    /**
404    This is a main function to peform inter prediction.
405    \param "encvid"     "Pointer to AVCEncObject."
406    \param "video"      "Pointer to AVCCommonObj."
407    \return "void".
408    */
409    void AVCMBMotionComp(AVCEncObject *encvid, AVCCommonObj *video);
410
411
412    /**
413    This function is called for luma motion compensation.
414    \param "ref"    "Pointer to the origin of a reference luma."
415    \param "picwidth"   "Width of the picture."
416    \param "picheight"  "Height of the picture."
417    \param "x_pos"  "X-coordinate of the predicted block in quarter pel resolution."
418    \param "y_pos"  "Y-coordinate of the predicted block in quarter pel resolution."
419    \param "pred"   "Pointer to the output predicted block."
420    \param "pred_pitch" "Width of pred."
421    \param "blkwidth"   "Width of the current partition."
422    \param "blkheight"  "Height of the current partition."
423    \return "void"
424    */
425    void eLumaMotionComp(uint8 *ref, int picwidth, int picheight,
426                         int x_pos, int y_pos,
427                         uint8 *pred, int pred_pitch,
428                         int blkwidth, int blkheight);
429
430    void eFullPelMC(uint8 *in, int inwidth, uint8 *out, int outpitch,
431                    int blkwidth, int blkheight);
432
433    void eHorzInterp1MC(uint8 *in, int inpitch, uint8 *out, int outpitch,
434                        int blkwidth, int blkheight, int dx);
435
436    void eHorzInterp2MC(int *in, int inpitch, uint8 *out, int outpitch,
437                        int blkwidth, int blkheight, int dx);
438
439    void eHorzInterp3MC(uint8 *in, int inpitch, int *out, int outpitch,
440                        int blkwidth, int blkheight);
441
442    void eVertInterp1MC(uint8 *in, int inpitch, uint8 *out, int outpitch,
443                        int blkwidth, int blkheight, int dy);
444
445    void eVertInterp2MC(uint8 *in, int inpitch, int *out, int outpitch,
446                        int blkwidth, int blkheight);
447
448    void eVertInterp3MC(int *in, int inpitch, uint8 *out, int outpitch,
449                        int blkwidth, int blkheight, int dy);
450
451    void eDiagonalInterpMC(uint8 *in1, uint8 *in2, int inpitch,
452                           uint8 *out, int outpitch,
453                           int blkwidth, int blkheight);
454
455    void eChromaMotionComp(uint8 *ref, int picwidth, int picheight,
456                           int x_pos, int y_pos, uint8 *pred, int pred_pitch,
457                           int blkwidth, int blkheight);
458
459    void eChromaDiagonalMC_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
460                                uint8 *pOut, int predPitch, int blkwidth, int blkheight);
461
462    void eChromaHorizontalMC_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
463                                  uint8 *pOut, int predPitch, int blkwidth, int blkheight);
464
465    void eChromaVerticalMC_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
466                                uint8 *pOut, int predPitch, int blkwidth, int blkheight);
467
468    void eChromaFullMC_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
469                            uint8 *pOut, int predPitch, int blkwidth, int blkheight);
470
471    void eChromaVerticalMC2_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
472                                 uint8 *pOut, int predPitch, int blkwidth, int blkheight);
473
474    void eChromaHorizontalMC2_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
475                                   uint8 *pOut, int predPitch, int blkwidth, int blkheight);
476
477    void eChromaDiagonalMC2_SIMD(uint8 *pRef, int srcPitch, int dx, int dy,
478                                 uint8 *pOut, int predPitch, int blkwidth, int blkheight);
479
480
481    /*-------------- motion_est.c ---------------*/
482
483    /**
484    Allocate and initialize arrays necessary for motion search algorithm.
485    \param "envid" "Pointer to AVCEncObject."
486    \return "AVC_SUCCESS or AVC_MEMORY_FAIL."
487    */
488    AVCEnc_Status InitMotionSearchModule(AVCHandle *avcHandle);
489
490    /**
491    Clean up memory allocated in InitMotionSearchModule.
492    \param "envid" "Pointer to AVCEncObject."
493    \return "void."
494    */
495    void CleanMotionSearchModule(AVCHandle *avcHandle);
496
497
498    /**
499    This function performs motion estimation of all macroblocks in a frame during the InitFrame.
500    The goal is to find the best MB partition for inter and find out if intra search is needed for
501    any MBs. This intra MB tendency can be used for scene change detection.
502    \param "encvid" "Pointer to AVCEncObject."
503    \return "void"
504    */
505    void AVCMotionEstimation(AVCEncObject *encvid);
506
507    /**
508    This function performs repetitive edge padding to the reference picture by adding 16 pixels
509    around the luma and 8 pixels around the chromas.
510    \param "refPic" "Pointer to the reference picture."
511    \return "void"
512    */
513    void  AVCPaddingEdge(AVCPictureData *refPic);
514
515    /**
516    This function keeps track of intra refresh macroblock locations.
517    \param "encvid" "Pointer to the global array structure AVCEncObject."
518    \param "mblock" "Pointer to the array of AVCMacroblock structures."
519    \param "totalMB" "Total number of MBs in a frame."
520    \param "numRefresh" "Number of MB to be intra refresh in a single frame."
521    \return "void"
522    */
523    void AVCRasterIntraUpdate(AVCEncObject *encvid, AVCMacroblock *mblock, int totalMB, int numRefresh);
524
525#ifdef HTFM
526    void InitHTFM(VideoEncData *encvid, HTFM_Stat *htfm_stat, double *newvar, int *collect);
527    void UpdateHTFM(AVCEncObject *encvid, double *newvar, double *exp_lamda, HTFM_Stat *htfm_stat);
528    void CalcThreshold(double pf, double exp_lamda[], int nrmlz_th[]);
529    void    HTFMPrepareCurMB_AVC(AVCEncObject *encvid, HTFM_Stat *htfm_stat, uint8 *cur, int pitch);
530#endif
531
532    /**
533    This function reads the input MB into a smaller faster memory space to minimize the cache miss.
534    \param "encvid" "Pointer to the global AVCEncObject."
535    \param "cur"    "Pointer to the original input macroblock."
536    \param "pitch"  "Stride size of the input frame (luma)."
537    \return "void"
538    */
539    void    AVCPrepareCurMB(AVCEncObject *encvid, uint8 *cur, int pitch);
540
541    /**
542    Performs motion vector search for a macroblock.
543    \param "encvid" "Pointer to AVCEncObject structure."
544    \param "cur"    "Pointer to the current macroblock in the input frame."
545    \param "best_cand" "Array of best candidates (to be filled in and returned)."
546    \param "i0"     "X-coordinate of the macroblock."
547    \param "j0"     "Y-coordinate of the macroblock."
548    \param "type_pred" "Indicates the type of operations."
549    \param "FS_en"      "Flag for fullsearch enable."
550    \param "hp_guess"   "Guess for half-pel search."
551    \return "void"
552    */
553    void AVCMBMotionSearch(AVCEncObject *encvid, uint8 *cur, uint8 *best_cand[],
554                           int i0, int j0, int type_pred, int FS_en, int *hp_guess);
555
556//AVCEnc_Status AVCMBMotionSearch(AVCEncObject *encvid, AVCMacroblock *currMB, int mbNum,
557//                           int num_pass);
558
559    /**
560    Perform full-pel exhaustive search around the predicted MV.
561    \param "encvid" "Pointer to AVCEncObject structure."
562    \param "prev"   "Pointer to the reference frame."
563    \param "cur"    "Pointer to the input macroblock."
564    \param "imin"   "Pointer to minimal mv (x)."
565    \param "jmin"   "Pointer to minimal mv (y)."
566    \param "ilow, ihigh, jlow, jhigh"   "Lower bound on search range."
567    \param "cmvx, cmvy" "Predicted MV value."
568
569    \return "The cost function of the best candidate."
570    */
571    int AVCFullSearch(AVCEncObject *encvid, uint8 *prev, uint8 *cur,
572                      int *imin, int *jmin, int ilow, int ihigh, int jlow, int jhigh,
573                      int cmvx, int cmvy);
574
575    /**
576    Select candidates from neighboring blocks according to the type of the
577    prediction selection.
578    \param "mvx"    "Pointer to the candidate, x-coordinate."
579    \param "mvy"    "Pointer to the candidate, y-coordinate."
580    \param "num_can"    "Pointer to the number of candidates returned."
581    \param "imb"    "The MB index x-coordinate."
582    \param "jmb"    "The MB index y-coordinate."
583    \param "type_pred"  "Type of the prediction."
584    \param "cmvx, cmvy" "Pointer to predicted MV (modified version)."
585    \return "void."
586    */
587    void AVCCandidateSelection(int *mvx, int *mvy, int *num_can, int imb, int jmb,
588                               AVCEncObject *encvid, int type_pred, int *cmvx, int *cmvy);
589
590    /**
591    Utility function to move the values in the array dn according to the new
592    location to avoid redundant calculation.
593    \param "dn" "Array of integer of size 9."
594    \param "new_loc"    "New location index."
595    \return "void."
596    */
597    void AVCMoveNeighborSAD(int dn[], int new_loc);
598
599    /**
600    Find minimum index of dn.
601    \param "dn" "Array of integer of size 9."
602    \return "The index of dn with the smallest dn[] value."
603    */
604    int AVCFindMin(int dn[]);
605
606
607    /*------------- findhalfpel.c -------------------*/
608
609    /**
610    Search for the best half-pel resolution MV around the full-pel MV.
611    \param "encvid" "Pointer to the global AVCEncObject structure."
612    \param "cur"    "Pointer to the current macroblock."
613    \param "mot"    "Pointer to the AVCMV array of the frame."
614    \param "ncand"  "Pointer to the origin of the fullsearch result."
615    \param "xpos"   "The current MB position in x."
616    \param "ypos"   "The current MB position in y."
617    \param "hp_guess"   "Input to help speedup the search."
618    \param "cmvx, cmvy" "Predicted motion vector use for mvcost."
619    \return "Minimal cost (SATD) without MV cost. (for rate control purpose)"
620    */
621    int AVCFindHalfPelMB(AVCEncObject *encvid, uint8 *cur, AVCMV *mot, uint8 *ncand,
622                         int xpos, int ypos, int hp_guess, int cmvx, int cmvy);
623
624    /**
625    This function generates sub-pel pixels required to do subpel MV search.
626    \param "subpel_pred" "Pointer to 2-D array, each array for each position."
627    \param "ncand" "Pointer to the full-pel center position in ref frame."
628    \param "lx" "Pitch of the ref frame."
629    \return "void"
630     */
631    void GenerateHalfPelPred(uint8 *subpel_pred, uint8 *ncand, int lx);
632
633    /**
634    This function calculate vertical interpolation at half-point of size 4x17.
635    \param "dst" "Pointer to destination."
636    \param "ref" "Pointer to the starting reference pixel."
637    \return "void."
638    */
639    void VertInterpWClip(uint8 *dst, uint8 *ref);
640
641    /**
642    This function generates quarter-pel pixels around the best half-pel result
643    during the sub-pel MV search.
644    \param "bilin_base"  "Array of pointers to be used as basis for q-pel interp."
645    \param "qpel_pred"  "Array of pointers pointing to quarter-pel candidates."
646    \param "hpel_pos" "Best half-pel position at the center."
647    \return "void"
648    */
649    void GenerateQuartPelPred(uint8 **bilin_base, uint8 *qpel_pred, int hpel_pos);
650
651    /**
652    This function calculates the SATD of a subpel candidate.
653    \param "cand"   "Pointer to a candidate."
654    \param "cur"    "Pointer to the current block."
655    \param "dmin"   "Min-so-far SATD."
656    \return "Sum of Absolute Transformed Difference."
657    */
658    int SATD_MB(uint8 *cand, uint8 *cur, int dmin);
659
660    /*------------- rate_control.c -------------------*/
661
662    /** This function is a utility function. It returns average QP of the previously encoded frame.
663    \param "rateCtrl" "Pointer to AVCRateControl structure."
664    \return "Average QP."
665    */
666    int GetAvgFrameQP(AVCRateControl *rateCtrl);
667
668    /**
669    This function takes the timestamp of the input and determine whether it should be encoded
670    or skipped.
671    \param "encvid" "Pointer to the AVCEncObject structure."
672    \param "rateCtrl"   "Pointer to the AVCRateControl structure."
673    \param "modTime"    "The 32 bit timestamp of the input frame."
674    \param "frameNum"   "Pointer to the frame number if to be encoded."
675    \return "AVC_SUCCESS or else."
676    */
677    AVCEnc_Status RCDetermineFrameNum(AVCEncObject *encvid, AVCRateControl *rateCtrl, uint32 modTime, uint *frameNum);
678
679    /**
680    This function updates the buffer fullness when frames are dropped either by the
681    rate control algorithm or by the users to make sure that target bit rate is still met.
682    \param "video" "Pointer to the common object structure."
683    \param "rateCtrl" "Pointer to rate control structure."
684    \param "frameInc" "Difference of the current frame number and previous frame number."
685    \return "void."
686    */
687    void RCUpdateBuffer(AVCCommonObj *video, AVCRateControl *rateCtrl, int frameInc);
688
689    /**
690    This function initializes rate control module and allocates necessary bufferes to do the job.
691    \param "avcHandle" "Pointer to the encoder handle."
692    \return "AVCENC_SUCCESS or AVCENC_MEMORY_FAIL."
693    */
694    AVCEnc_Status InitRateControlModule(AVCHandle *avcHandle);
695
696    /**
697    This function frees buffers allocated in InitRateControlModule.
698    \param "avcHandle" "Pointer to the encoder handle."
699    \return "void."
700    */
701    void CleanupRateControlModule(AVCHandle *avcHandle);
702
703    /**
704    This function is called at the beginning of each GOP or the first IDR frame. It calculates
705    target bits for a GOP.
706    \param "encvid" "Pointer to the encoder object."
707    \return "void."
708    */
709    void RCInitGOP(AVCEncObject *encvid);
710
711    /**
712    This function calculates target bits for a particular frame.
713    \param "video"  "Pointer to the AVCEncObject structure."
714    \return "void"
715    */
716    void RCInitFrameQP(AVCEncObject *video);
717
718    /**
719    This function calculates QP for the upcoming frame or basic unit.
720    \param "encvid" "Pointer to the encoder object."
721    \param "rateCtrl" "Pointer to the rate control object."
722    \return "QP value ranging from 0-51."
723    */
724    int  RCCalculateQP(AVCEncObject *encvid, AVCRateControl *rateCtrl);
725
726    /**
727    This function translates the luma QP to chroma QP and calculates lambda based on QP.
728    \param "video"  "Pointer to the AVCEncObject structure."
729    \return "void"
730    */
731    void RCInitChromaQP(AVCEncObject *encvid);
732
733    /**
734    This function is called before encoding each macroblock.
735    \param "encvid" "Pointer to the encoder object."
736    \return "void."
737    */
738    void RCInitMBQP(AVCEncObject *encvid);
739
740    /**
741    This function updates bits usage stats after encoding an macroblock.
742    \param "video" "Pointer to AVCCommonObj."
743    \param "rateCtrl" "Pointer to AVCRateControl."
744    \param "num_header_bits" "Number of bits used for MB header."
745    \param "num_texture_bits" "Number of bits used for MB texture."
746    \return "void"
747    */
748    void RCPostMB(AVCCommonObj *video, AVCRateControl *rateCtrl, int num_header_bits, int num_texture_bits);
749
750    /**
751    This function calculates the difference between prediction and original MB.
752    \param "encvid" "Pointer to the encoder object."
753    \param "currMB" "Pointer to the current macroblock structure."
754    \param "orgL" "Pointer to the original MB."
755    \param "orgPitch" "Pointer to the original picture pitch."
756    \return "void."
757    */
758    void RCCalculateMAD(AVCEncObject *encvid, AVCMacroblock *currMB, uint8 *orgL, int orgPitch);
759
760    /**
761    Restore QP related parameters of previous MB when current MB is skipped.
762    \param "currMB" "Pointer to the current macroblock."
763    \param "video"  "Pointer to the common video structure."
764    \param "encvid" "Pointer to the global encoding structure."
765    \return "void"
766    */
767    void RCRestoreQP(AVCMacroblock *currMB, AVCCommonObj *video, AVCEncObject *encvid);
768
769    /**
770    This function is called after done with a frame.
771    \param "encvid" "Pointer to the encoder object."
772    \return "AVCENC_SUCCESS or AVCENC_SKIPPED_PICTURE when bufer overflow (need to discard current frame)."
773    */
774    AVCEnc_Status RCUpdateFrame(AVCEncObject *encvid);
775
776    /*--------- residual.c -------------------*/
777
778    /**
779    This function encodes the intra pcm data and fill it in the corresponding location
780    on the current picture.
781    \param "video"  "Pointer to AVCEncObject."
782    \return "AVCENC_SUCCESS if success, or else for bitstream errors."
783    */
784    AVCEnc_Status EncodeIntraPCM(AVCEncObject *video);
785
786    /**
787    This function performs CAVLC syntax encoding on the run and level information of the coefficients.
788    The level and run arrays are elements in AVCEncObject structure, populated by TransQuantZZ,
789    TransQuantIntraDC and TransQuantChromaDC functions.
790    \param "video"  "Pointer to AVCEncObject."
791    \param "type"   "One of AVCResidualType for a particular 4x4 block."
792    \param "bindx"  "Block index or number of nonzero coefficients for AVC_Intra16DC and AVC_ChromaDC mode."
793    \param "currMB" "Pointer to the current macroblock structure."
794    \return "AVCENC_SUCCESS for success."
795    \Note   "This function has 32-bit machine specific instruction!!!!"
796    */
797    AVCEnc_Status enc_residual_block(AVCEncObject *encvid, AVCResidualType type, int bindx, AVCMacroblock *currMB);
798
799
800    /*------------- sad.c ---------------------------*/
801
802
803    int AVCSAD_MB_HalfPel_Cxhyh(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
804    int AVCSAD_MB_HalfPel_Cyh(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
805    int AVCSAD_MB_HalfPel_Cxh(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
806    int AVCSAD_Macroblock_C(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
807
808#ifdef HTFM /*  3/2/1, Hypothesis Testing Fast Matching */
809    int AVCSAD_MB_HP_HTFM_Collectxhyh(uint8 *ref, uint8 *blk, int dmin_x, void *extra_info);
810    int AVCSAD_MB_HP_HTFM_Collectyh(uint8 *ref, uint8 *blk, int dmin_x, void *extra_info);
811    int AVCSAD_MB_HP_HTFM_Collectxh(uint8 *ref, uint8 *blk, int dmin_x, void *extra_info);
812    int AVCSAD_MB_HP_HTFMxhyh(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
813    int AVCSAD_MB_HP_HTFMyh(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
814    int AVCSAD_MB_HP_HTFMxh(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
815    int AVCSAD_MB_HTFM_Collect(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
816    int AVCSAD_MB_HTFM(uint8 *ref, uint8 *blk, int dmin_lx, void *extra_info);
817#endif
818
819
820    /*------------- slice.c -------------------------*/
821
822    /**
823    This function performs the main encoding loop for a slice.
824    \param "encvid" "Pointer to AVCEncObject."
825    \return "AVCENC_SUCCESS for success, AVCENC_PICTURE_READY for end-of-picture and
826             AVCENC_FAIL or AVCENC_SLICE_EMPTY otherwise."
827    */
828    AVCEnc_Status AVCEncodeSlice(AVCEncObject *encvid);
829
830    /**
831    This function performs the main encoding operation for one macroblock.
832    \param "video" "pointer to AVCEncObject."
833    \return "AVCENC_SUCCESS for success, or other bitstream related failure status."
834    */
835    AVCEnc_Status EncodeMB(AVCEncObject *video);
836
837    /**
838    This function calls prediction INTRA/INTER functions, transform,
839    quantization and zigzag scanning to get the run-level symbols.
840    \param "encvid" "pointer to AVCEncObject."
841    \param "curL"   "pointer to Luma component of the current frame.
842    \param "curCb"  "pointer to Cb component of the current frame.
843    \param "curCr"  "pointer to Cr component of the current frame.
844    \return "void for now."
845     */
846    void MBPredTransQuantZZ(AVCEncObject *encvid, uint8 *curL, uint8 *curCb, uint8 *curCr);
847
848    /**
849    This function copies the content of the prediction MB into the reconstructed YUV
850    frame directly.
851    \param "curL"   "Pointer to the destination Y component."
852    \param "curCb"  "Pointer to the destination Cb component."
853    \param "curCr"  "Pointer to the destination Cr component."
854    \param "predBlock"  "Pointer to the prediction MB."
855    \param "picWidth"   "The width of the frame."
856    \return "None."
857    */
858    void Copy_MB(uint8 *curL, uint8 *curCb, uint8 *curCr, uint8 *predBlock, int picWidth);
859
860    /**
861    This function encodes the mb_type, CBP, prediction mode, ref idx and MV.
862    \param "currMB" "Pointer to the current macroblock structure."
863    \param "video" "Pointer to the AVCEncObject structure."
864    \return "AVCENC_SUCCESS for success or else for fail."
865    */
866    AVCEnc_Status EncodeMBHeader(AVCMacroblock *currMB, AVCEncObject *video);
867
868    /**
869    This function finds the right mb_type for a macroblock given the mbMode, CBP,
870    NumPart, PredPartMode.
871    \param "currMB" "Pointer to the current macroblock structure."
872    \param "slice_type" "Value of the slice_type."
873    \return "mb_type."
874    */
875    uint InterpretMBType(AVCMacroblock *currMB, int slice_type);
876
877    /**
878    This function encodes the mb_pred part of the macroblock data.
879    \param "video"  "Pointer to the AVCCommonObj structure."
880    \param "currMB" "Pointer to the current macroblock structure."
881    \param "stream" "Pointer to the AVCEncBitstream structure."
882    \return "AVCENC_SUCCESS for success or bitstream fail status."
883    */
884    AVCEnc_Status mb_pred(AVCCommonObj *video, AVCMacroblock *currMB, AVCEncBitstream *stream);
885
886    /**
887    This function encodes the sub_mb_pred part of the macroblock data.
888    \param "video"  "Pointer to the AVCCommonObj structure."
889    \param "currMB" "Pointer to the current macroblock structure."
890    \param "stream" "Pointer to the AVCEncBitstream structure."
891    \return "AVCENC_SUCCESS for success or bitstream fail status."
892    */
893    AVCEnc_Status sub_mb_pred(AVCCommonObj *video, AVCMacroblock *currMB, AVCEncBitstream *stream);
894
895    /**
896    This function interprets the sub_mb_type and sets necessary information
897    when the slice type is AVC_P_SLICE.
898    in the macroblock structure.
899    \param "mblock" "Pointer to current AVCMacroblock."
900    \param "sub_mb_type" "From the syntax bitstream."
901    \return "void"
902    */
903    void InterpretSubMBTypeP(AVCMacroblock *mblock, uint *sub_mb_type);
904
905    /**
906    This function interprets the sub_mb_type and sets necessary information
907    when the slice type is AVC_B_SLICE.
908    in the macroblock structure.
909    \param "mblock" "Pointer to current AVCMacroblock."
910    \param "sub_mb_type" "From the syntax bitstream."
911    \return "void"
912    */
913    void InterpretSubMBTypeB(AVCMacroblock *mblock, uint *sub_mb_type);
914
915    /**
916    This function encodes intra 4x4 mode. It calculates the predicted I4x4 mode and the
917    remnant to be encoded.
918    \param "video"  "Pointer to AVCEncObject structure."
919    \param "currMB" "Pointer to the AVCMacroblock structure."
920    \param "stream" "Pointer to AVCEncBitstream sructure."
921    \return "AVCENC_SUCCESS for success."
922    */
923    AVCEnc_Status EncodeIntra4x4Mode(AVCCommonObj *video, AVCMacroblock *currMB, AVCEncBitstream *stream);
924
925    /*------------- vlc_encode.c -----------------------*/
926    /**
927    This function encodes and writes a value into an Exp-Golomb codeword.
928    \param "bitstream" "Pointer to AVCEncBitstream."
929    \param "codeNum" "Pointer to the value of the codeNum."
930    \return "AVCENC_SUCCESS for success or bitstream error messages for fail."
931    */
932    AVCEnc_Status ue_v(AVCEncBitstream *bitstream, uint codeNum);
933
934    /**
935    This function maps and encodes signed Exp-Golomb codes.
936    \param "bitstream" "Pointer to AVCEncBitstream."
937    \param "value"  "Pointer to syntax element value."
938    \return "AVCENC_SUCCESS or AVCENC_FAIL."
939    */
940    AVCEnc_Status  se_v(AVCEncBitstream *bitstream, int value);
941
942    /**
943    This function maps and encodes truncated Exp-Golomb codes.
944    \param "bitstream" "Pointer to AVCEncBitstream."
945    \param "value"  "Pointer to syntax element value."
946    \param "range"  "Range of the value as input to determine the algorithm."
947    \return "AVCENC_SUCCESS or AVCENC_FAIL."
948    */
949    AVCEnc_Status te_v(AVCEncBitstream *bitstream, uint value, uint range);
950
951    /**
952    This function creates Exp-Golomb codeword from codeNum.
953    \param "bitstream" "Pointer to AVCEncBitstream."
954    \param "codeNum" "Pointer to the codeNum value."
955    \return "AVCENC_SUCCESS for success or bitstream error messages for fail."
956    */
957    AVCEnc_Status SetEGBitstring(AVCEncBitstream *bitstream, uint codeNum);
958
959    /**
960    This function performs CAVLC encoding of the CBP (coded block pattern) of a macroblock
961    by calling ue_v() and then mapping the CBP to the corresponding VLC codeNum.
962    \param "currMB"  "Pointer to the current AVCMacroblock structure."
963    \param "stream"  "Pointer to the AVCEncBitstream."
964    \return "void"
965    */
966    AVCEnc_Status EncodeCBP(AVCMacroblock *currMB, AVCEncBitstream *stream);
967
968    /**
969    This function encodes trailing ones and total coefficient.
970    \param "stream" "Pointer to the AVCEncBitstream."
971    \param "TrailingOnes"   "The trailing one variable output."
972    \param "TotalCoeff" "The total coefficient variable output."
973    \param "nC" "Context for number of nonzero coefficient (prediction context)."
974    \return "AVCENC_SUCCESS for success or else for bitstream failure."
975    */
976    AVCEnc_Status ce_TotalCoeffTrailingOnes(AVCEncBitstream *stream, int TrailingOnes, int TotalCoeff, int nC);
977
978    /**
979    This function encodes trailing ones and total coefficient for chroma DC block.
980    \param "stream" "Pointer to the AVCEncBitstream."
981    \param "TrailingOnes"   "The trailing one variable output."
982    \param "TotalCoeff" "The total coefficient variable output."
983    \return "AVCENC_SUCCESS for success or else for bitstream failure."
984    */
985    AVCEnc_Status ce_TotalCoeffTrailingOnesChromaDC(AVCEncBitstream *stream, int TrailingOnes, int TotalCoeff);
986
987    /**
988    This function encodes total_zeros value as in Table 9-7 and 9-8.
989    \param "stream" "Pointer to the AVCEncBitstream."
990    \param "TotalZeros" "The total_zeros value."
991    \param "TotalCoeff" "The total coefficient variable output."
992    \return "AVCENC_SUCCESS for success or else for bitstream failure."
993    */
994    AVCEnc_Status ce_TotalZeros(AVCEncBitstream *stream, int total_zeros, int TotalCoeff);
995
996    /**
997    This function encodes total_zeros VLC syntax for chroma DC as in Table 9-9.
998    \param "stream" "Pointer to the AVCEncBitstream."
999    \param "TotalZeros" "The total_zeros value."
1000    \param "TotalCoeff" "The total coefficient variable output."
1001    \return "AVCENC_SUCCESS for success or else for bitstream failure."
1002    */
1003    AVCEnc_Status ce_TotalZerosChromaDC(AVCEncBitstream *stream, int total_zeros, int TotalCoeff);
1004
1005    /**
1006    This function encodes run_before VLC syntax as in Table 9-10.
1007    \param "stream" "Pointer to the AVCEncBitstream."
1008    \param "run_before" "The run_before value."
1009    \param "zerosLeft"  "The context for number of zeros left."
1010    \return "AVCENC_SUCCESS for success or else for bitstream failure."
1011    */
1012    AVCEnc_Status ce_RunBefore(AVCEncBitstream *stream, int run_before, int zerosLeft);
1013
1014#ifdef __cplusplus
1015}
1016#endif
1017
1018
1019#endif /* _AVCENC_LIB_H_ */
1020
1021