avcenc_api.h revision 29a84457aed4c45bc900998b5e11c03023264208
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 application function interfaces to the AVC encoder library
20and necessary type defitionitions and enumerations.
21@publishedAll
22*/
23
24#ifndef AVCENC_API_H_INCLUDED
25#define AVCENC_API_H_INCLUDED
26
27#ifndef AVCAPI_COMMON_H_INCLUDED
28#include "avcapi_common.h"
29#endif
30
31/**
32 This enumeration is used for the status returned from the library interface.
33*/
34typedef enum
35{
36    /**
37    Fail information, need to add more error code for more specific info
38    */
39    AVCENC_TRAILINGONES_FAIL = -35,
40    AVCENC_SLICE_EMPTY = -34,
41    AVCENC_POC_FAIL = -33,
42    AVCENC_CONSECUTIVE_NONREF = -32,
43    AVCENC_CABAC_FAIL = -31,
44    AVCENC_PRED_WEIGHT_TAB_FAIL = -30,
45    AVCENC_DEC_REF_PIC_MARK_FAIL = -29,
46    AVCENC_SPS_FAIL = -28,
47    AVCENC_BITSTREAM_BUFFER_FULL    = -27,
48    AVCENC_BITSTREAM_INIT_FAIL = -26,
49    AVCENC_CHROMA_QP_FAIL = -25,
50    AVCENC_INIT_QS_FAIL = -24,
51    AVCENC_INIT_QP_FAIL = -23,
52    AVCENC_WEIGHTED_BIPRED_FAIL = -22,
53    AVCENC_INVALID_INTRA_PERIOD = -21,
54    AVCENC_INVALID_CHANGE_RATE = -20,
55    AVCENC_INVALID_BETA_OFFSET = -19,
56    AVCENC_INVALID_ALPHA_OFFSET = -18,
57    AVCENC_INVALID_DEBLOCK_IDC = -17,
58    AVCENC_INVALID_REDUNDANT_PIC = -16,
59    AVCENC_INVALID_FRAMERATE = -15,
60    AVCENC_INVALID_NUM_SLICEGROUP = -14,
61    AVCENC_INVALID_POC_LSB = -13,
62    AVCENC_INVALID_NUM_REF = -12,
63    AVCENC_INVALID_FMO_TYPE = -11,
64    AVCENC_ENCPARAM_MEM_FAIL = -10,
65    AVCENC_LEVEL_NOT_SUPPORTED = -9,
66    AVCENC_LEVEL_FAIL = -8,
67    AVCENC_PROFILE_NOT_SUPPORTED = -7,
68    AVCENC_TOOLS_NOT_SUPPORTED = -6,
69    AVCENC_WRONG_STATE = -5,
70    AVCENC_UNINITIALIZED = -4,
71    AVCENC_ALREADY_INITIALIZED = -3,
72    AVCENC_NOT_SUPPORTED = -2,
73    AVCENC_MEMORY_FAIL = AVC_MEMORY_FAIL,
74    AVCENC_FAIL = AVC_FAIL,
75    /**
76    Generic success value
77    */
78    AVCENC_SUCCESS = AVC_SUCCESS,
79    AVCENC_PICTURE_READY = 2,
80    AVCENC_NEW_IDR = 3, /* upon getting this, users have to call PVAVCEncodeSPS and PVAVCEncodePPS to get a new SPS and PPS*/
81    AVCENC_SKIPPED_PICTURE = 4 /* continuable error message */
82
83} AVCEnc_Status;
84
85#define MAX_NUM_SLICE_GROUP  8      /* maximum for all the profiles */
86
87/**
88This structure contains the encoding parameters.
89*/
90typedef struct tagAVCEncParam
91{
92    /* if profile/level is set to zero, encoder will choose the closest one for you */
93    AVCProfile profile; /* profile of the bitstream to be compliant with*/
94    AVCLevel   level;   /* level of the bitstream to be compliant with*/
95
96    int width;      /* width of an input frame in pixel */
97    int height;     /* height of an input frame in pixel */
98
99    int poc_type; /* picture order count mode, 0,1 or 2 */
100    /* for poc_type == 0 */
101    uint log2_max_poc_lsb_minus_4; /* specify maximum value of POC Lsb, range 0..12*/
102    /* for poc_type == 1 */
103    uint delta_poc_zero_flag; /* delta POC always zero */
104    int offset_poc_non_ref; /* offset for non-reference pic */
105    int offset_top_bottom; /* offset between top and bottom field */
106    uint num_ref_in_cycle; /* number of reference frame in one cycle */
107    int *offset_poc_ref; /* array of offset for ref pic, dimension [num_ref_in_cycle] */
108
109    int num_ref_frame;  /* number of reference frame used */
110    int num_slice_group;  /* number of slice group */
111    int fmo_type;   /* 0: interleave, 1: dispersed, 2: foreground with left-over
112                    3: box-out, 4:raster scan, 5:wipe, 6:explicit */
113    /* for fmo_type == 0 */
114    uint run_length_minus1[MAX_NUM_SLICE_GROUP];   /* array of size num_slice_group, in round robin fasion */
115    /* fmo_type == 2*/
116    uint top_left[MAX_NUM_SLICE_GROUP-1];           /* array of co-ordinates of each slice_group */
117    uint bottom_right[MAX_NUM_SLICE_GROUP-1];       /* except the last one which is the background. */
118    /* fmo_type == 3,4,5 */
119    AVCFlag change_dir_flag;  /* slice group change direction flag */
120    uint change_rate_minus1;
121    /* fmo_type == 6 */
122    uint *slice_group; /* array of size MBWidth*MBHeight */
123
124    AVCFlag db_filter;  /* enable deblocking loop filter */
125    int disable_db_idc;  /* 0: filter everywhere, 1: no filter, 2: no filter across slice boundary */
126    int alpha_offset;   /* alpha offset range -6,...,6 */
127    int beta_offset;    /* beta offset range -6,...,6 */
128
129    AVCFlag constrained_intra_pred; /* constrained intra prediction flag */
130
131    AVCFlag auto_scd;   /* scene change detection on or off */
132    int idr_period; /* idr frame refresh rate in number of target encoded frame (no concept of actual time).*/
133    int intramb_refresh;    /* minimum number of intra MB per frame */
134    AVCFlag data_par;   /* enable data partitioning */
135
136    AVCFlag fullsearch; /* enable full-pel full-search mode */
137    int search_range;   /* search range for motion vector in (-search_range,+search_range) pixels */
138    AVCFlag sub_pel;    /* enable sub pel prediction */
139    AVCFlag submb_pred; /* enable sub MB partition mode */
140    AVCFlag rdopt_mode; /* RD optimal mode selection */
141    AVCFlag bidir_pred; /* enable bi-directional for B-slice, this flag forces the encoder to encode
142                        any frame with POC less than the previously encoded frame as a B-frame.
143                        If it's off, then such frames will remain P-frame. */
144
145    AVCFlag rate_control; /* rate control enable, on: RC on, off: constant QP */
146    int initQP;     /* initial QP */
147    uint32 bitrate;    /* target encoding bit rate in bits/second */
148    uint32 CPB_size;  /* coded picture buffer in number of bits */
149    uint32 init_CBP_removal_delay; /* initial CBP removal delay in msec */
150
151    uint32 frame_rate;  /* frame rate in the unit of frames per 1000 second */
152    /* note, frame rate is only needed by the rate control, AVC is timestamp agnostic. */
153
154    AVCFlag out_of_band_param_set; /* flag to set whether param sets are to be retrieved up front or not */
155
156    AVCFlag use_overrun_buffer;  /* do not throw away the frame if output buffer is not big enough.
157                                    copy excess bits to the overrun buffer */
158} AVCEncParams;
159
160
161/**
162This structure contains current frame encoding statistics for debugging purpose.
163*/
164typedef struct tagAVCEncFrameStats
165{
166    int avgFrameQP;   /* average frame QP */
167    int numIntraMBs;  /* number of intra MBs */
168    int numFalseAlarm;
169    int numMisDetected;
170    int numDetected;
171
172} AVCEncFrameStats;
173
174#ifdef __cplusplus
175extern "C"
176{
177#endif
178    /** THE FOLLOWINGS ARE APIS */
179    /**
180    This function initializes the encoder library. It verifies the validity of the
181    encoding parameters against the specified profile/level and the list of supported
182    tools by this library. It allocates necessary memories required to perform encoding.
183    For re-encoding application, if users want to setup encoder in a more precise way,
184    users can give the external SPS and PPS to the encoder to follow.
185    \param "avcHandle"  "Handle to the AVC encoder library object."
186    \param "encParam"   "Pointer to the encoding parameter structure."
187    \param "extSPS"     "External SPS used for re-encoding purpose. NULL if not present"
188    \param "extPPS"     "External PPS used for re-encoding purpose. NULL if not present"
189    \return "AVCENC_SUCCESS for success,
190             AVCENC_NOT_SUPPORTED for the use of unsupported tools,
191             AVCENC_MEMORY_FAIL for memory allocation failure,
192             AVCENC_FAIL for generic failure."
193    */
194    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncInitialize(AVCHandle *avcHandle, AVCEncParams *encParam, void* extSPS, void* extPPS);
195
196
197    /**
198    Since the output buffer size is not known prior to encoding a frame, users need to
199    allocate big enough buffer otherwise, that frame will be dropped. This function returns
200    the size of the output buffer to be allocated by the users that guarantees to hold one frame.
201    It follows the CPB spec for a particular level.  However, when the users set use_overrun_buffer
202    flag, this API is useless as excess output bits are saved in the overrun buffer waiting to be
203    copied out in small chunks, i.e. users can allocate any size of output buffer.
204    \param "avcHandle"  "Handle to the AVC encoder library object."
205    \param "size"   "Pointer to the size to be modified."
206    \return "AVCENC_SUCCESS for success, AVCENC_UNINITIALIZED when level is not known.
207    */
208
209    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncGetMaxOutputBufferSize(AVCHandle *avcHandle, int* size);
210
211    /**
212    Users call this function to provide an input structure to the encoder library which will keep
213    a list of input structures it receives in case the users call this function many time before
214    calling PVAVCEncodeSlice. The encoder library will encode them according to the frame_num order.
215    Users should not modify the content of a particular frame until this frame is encoded and
216    returned thru CBAVCEnc_ReturnInput() callback function.
217    \param "avcHandle"  "Handle to the AVC encoder library object."
218    \param "input"      "Pointer to the input structure."
219    \return "AVCENC_SUCCESS for success,
220            AVCENC_FAIL if the encoder is not in the right state to take a new input frame.
221            AVCENC_NEW_IDR for the detection or determination of a new IDR, with this status,
222            the returned NAL is an SPS NAL,
223            AVCENC_NO_PICTURE if the input frame coding timestamp is too early, users must
224            get next frame or adjust the coding timestamp."
225    */
226    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncSetInput(AVCHandle *avcHandle, AVCFrameIO *input);
227
228    /**
229    This function is called to encode a NAL unit which can be an SPS NAL, a PPS NAL or
230    a VCL (video coding layer) NAL which contains one slice of data. It could be a
231    fixed number of macroblocks, as specified in the encoder parameters set, or the
232    maximum number of macroblocks fitted into the given input argument "buffer". The
233    input frame is taken from the oldest unencoded input frame retrieved by users by
234    PVAVCEncGetInput API.
235    \param "avcHandle"  "Handle to the AVC encoder library object."
236    \param "buffer"     "Pointer to the output AVC bitstream buffer, the format will be EBSP,
237                         not RBSP."
238    \param "buf_nal_size"   "As input, the size of the buffer in bytes.
239                        This is the physical limitation of the buffer. As output, the size of the EBSP."
240    \param "nal_type"   "Pointer to the NAL type of the returned buffer."
241    \return "AVCENC_SUCCESS for success of encoding one slice,
242             AVCENC_PICTURE_READY for the completion of a frame encoding,
243             AVCENC_FAIL for failure (this should not occur, though)."
244    */
245    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncodeNAL(AVCHandle *avcHandle, uint8 *buffer, uint *buf_nal_size, int *nal_type);
246
247    /**
248    This function sniffs the nal_unit_type such that users can call corresponding APIs.
249    This function is identical to PVAVCDecGetNALType() in the decoder.
250    \param "bitstream"  "Pointer to the beginning of a NAL unit (start with forbidden_zero_bit, etc.)."
251    \param "size"       "size of the bitstream (NumBytesInNALunit + 1)."
252    \param "nal_unit_type" "Pointer to the return value of nal unit type."
253    \return "AVCENC_SUCCESS if success, AVCENC_FAIL otherwise."
254    */
255    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncGetNALType(uint8 *bitstream, int size, int *nal_type, int *nal_ref_idc);
256
257    /**
258    This function returns the pointer to internal overrun buffer. Users can call this to query
259    whether the overrun buffer has been used to encode the current NAL.
260    \param "avcHandle"  "Pointer to the handle."
261    \return "Pointer to overrun buffer if it is used, otherwise, NULL."
262    */
263    OSCL_IMPORT_REF uint8* PVAVCEncGetOverrunBuffer(AVCHandle* avcHandle);
264
265    /**
266    This function returns the reconstructed frame of the most recently encoded frame.
267    Note that this frame is not returned to the users yet. Users should only read the
268    content of this frame.
269    \param "avcHandle"  "Handle to the AVC encoder library object."
270    \param "output"     "Pointer to the input structure."
271    \return "AVCENC_SUCCESS for success, AVCENC_NO_PICTURE if no picture to be outputted."
272    */
273    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncGetRecon(AVCHandle *avcHandle, AVCFrameIO *recon);
274
275    /**
276    This function is used to return the recontructed frame back to the AVC encoder library
277    in order to be re-used for encoding operation. If users want the content of it to remain
278    unchanged for a long time, they should make a copy of it and release the memory back to
279    the encoder. The encoder relies on the id element in the AVCFrameIO structure,
280    thus users should not change the id value.
281    \param "avcHandle"  "Handle to the AVC decoder library object."
282    \param "output"      "Pointer to the AVCFrameIO structure."
283    \return "AVCENC_SUCCESS for success, AVCENC_FAIL for fail for id not found."
284    */
285    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncReleaseRecon(AVCHandle *avcHandle, AVCFrameIO *recon);
286
287    /**
288    This function performs clean up operation including memory deallocation.
289    The encoder will also clear the list of input structures it has not released.
290    This implies that users must keep track of the number of input structure they have allocated
291    and free them accordingly.
292    \param "avcHandle"  "Handle to the AVC encoder library object."
293    */
294    OSCL_IMPORT_REF void    PVAVCCleanUpEncoder(AVCHandle *avcHandle);
295
296    /**
297    This function extracts statistics of the current frame. If the encoder has not finished
298    with the current frame, the result is not accurate.
299    \param "avcHandle"  "Handle to the AVC encoder library object."
300    \param "avcStats"   "Pointer to AVCEncFrameStats structure."
301    \return "void."
302    */
303    void PVAVCEncGetFrameStats(AVCHandle *avcHandle, AVCEncFrameStats *avcStats);
304
305    /**
306    These functions are used for the modification of encoding parameters.
307    To be polished.
308    */
309    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncUpdateBitRate(AVCHandle *avcHandle, uint32 bitrate);
310    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncUpdateFrameRate(AVCHandle *avcHandle, uint32 num, uint32 denom);
311    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncUpdateIDRInterval(AVCHandle *avcHandle, int IDRInterval);
312    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncIDRRequest(AVCHandle *avcHandle);
313    OSCL_IMPORT_REF AVCEnc_Status PVAVCEncUpdateIMBRefresh(AVCHandle *avcHandle, int numMB);
314
315
316#ifdef __cplusplus
317}
318#endif
319#endif  /* _AVCENC_API_H_ */
320
321