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 #ifndef _MP4ENC_API_H_ 19 #define _MP4ENC_API_H_ 20 21 #include <stdint.h> 22 #include <string.h> 23 24 #ifndef _PV_TYPES_ 25 #define _PV_TYPES_ 26 typedef uint8_t UChar; 27 typedef int8_t Char; 28 typedef unsigned int UInt; 29 typedef int Int; 30 typedef uint16_t UShort; 31 typedef int16_t Short; 32 typedef unsigned int Bool; 33 typedef uint32_t ULong; 34 35 #define PV_CODEC_INIT 0 36 #define PV_CODEC_STOP 1 37 #endif 38 39 #define PV_TRUE 1 40 #define PV_FALSE 0 41 42 typedef enum 43 { 44 SHORT_HEADER, 45 SHORT_HEADER_WITH_ERR_RES, 46 H263_MODE, 47 H263_MODE_WITH_ERR_RES, 48 DATA_PARTITIONING_MODE, 49 COMBINE_MODE_NO_ERR_RES, 50 COMBINE_MODE_WITH_ERR_RES 51 52 } MP4EncodingMode; 53 54 typedef enum 55 { 56 CONSTANT_Q, 57 CBR_1, 58 VBR_1, 59 CBR_2, 60 VBR_2, 61 CBR_LOWDELAY 62 } MP4RateControlType; 63 64 typedef enum 65 { 66 PASS1, 67 PASS2 68 } PassNum; 69 70 typedef enum 71 { 72 PV_OFF, 73 PV_ON 74 } ParamEncMode; 75 76 77 /* {SPL0, SPL1, SPL2, SPL3, CPL1, CPL2, CPL2, CPL2} , SPL0: Simple Profile@Level0 , CPL1: Core Profile@Level1 */ 78 /* {SSPL0, SSPL1, SSPL2, SSPL2, CSPL1, CSPL2, CSPL3, CSPL3} , SSPL0: Simple Scalable Profile@Level0, CPL1: Core Scalable Profile@Level1 */ 79 80 typedef enum 81 { 82 /* Non-scalable profile */ 83 SIMPLE_PROFILE_LEVEL0 = 0, 84 SIMPLE_PROFILE_LEVEL1, 85 SIMPLE_PROFILE_LEVEL2, 86 SIMPLE_PROFILE_LEVEL3, 87 CORE_PROFILE_LEVEL1, 88 CORE_PROFILE_LEVEL2, 89 90 /* Scalable profile */ 91 SIMPLE_SCALABLE_PROFILE_LEVEL0 = 6, 92 SIMPLE_SCALABLE_PROFILE_LEVEL1, 93 SIMPLE_SCALABLE_PROFILE_LEVEL2, 94 95 CORE_SCALABLE_PROFILE_LEVEL1 = 10, 96 CORE_SCALABLE_PROFILE_LEVEL2, 97 CORE_SCALABLE_PROFILE_LEVEL3 98 99 } ProfileLevelType; 100 101 102 typedef struct tagMP4HintTrack 103 { 104 UChar MTB; 105 UChar LayerID; 106 UChar CodeType; 107 UChar RefSelCode; 108 } MP4HintTrack; 109 110 typedef struct tagvideoEncControls 111 { 112 void *videoEncoderData; 113 Int videoEncoderInit; 114 } VideoEncControls; 115 116 117 typedef struct tagvideoEncFrameIO 118 { 119 UChar *yChan; /* pointer to Y */ 120 UChar *uChan; /* pointer to U */ 121 UChar *vChan; /* pointer to V */ 122 Int height; /* height for Y */ 123 Int pitch; /* stride for Y */ 124 ULong timestamp; /* modulo timestamp in millisecond*/ 125 126 } VideoEncFrameIO ; 127 128 /** 129 @brief Encoding options structure */ 130 typedef struct tagvideoEncOptions 131 { 132 /** @brief Sets the encoding mode, defined by the above enumaration. If there are conflicts between the encoding mode 133 * and subsequent encoding options, encoding mode take precedent over encoding options. */ 134 MP4EncodingMode encMode; 135 136 /** @brief Sets the number of bytes per packet, only used in DATA_PARTITIONING_MODE or COMBINE_MODE_WITH_ERR_RES mode. 137 * The resync marker will be inserted as often as the size of the packet.*/ 138 Int packetSize; 139 140 /** @brief Selects MPEG-4/H.263 profile and level, if specified other encoding options must conform with it. */ 141 ProfileLevelType profile_level; 142 143 /** @brief Enables reversible variable length code (RVLC) mode. Normally it is set to PV_OFF.*/ 144 ParamEncMode rvlcEnable; 145 146 /** @brief Set the frequency of GOB header interval */ 147 Int gobHeaderInterval; 148 149 /** @brief Sets the number of bitstream layers: 1 is base only: 2 is base + enhancement */ 150 Int numLayers; 151 152 /** @brief Sets the number of ticks per second used for timing information encoded in MPEG4 bitstream.*/ 153 Int timeIncRes; 154 155 /** @brief Sets the number of ticks in time increment resolution between 2 source frames (equivalent to source frame rate). */ 156 Int tickPerSrc; 157 158 /** @brief Specifies encoded heights in pixels, height[n] represents the n-th layer's height. */ 159 Int encHeight[2]; 160 161 /** @brief Specifies encoded widths in pixels, width[n] represents the n-th layer's width.*/ 162 Int encWidth[2]; 163 164 /** @brief Specifies target frame rates in frames per second, frameRate[n] represents the n-th layer's target frame rate.*/ 165 float encFrameRate[2]; 166 167 /** @brief Specifies target bit rates in bits per second unit, bitRate[n] represents the n-th layer's target bit rate. */ 168 Int bitRate[2]; 169 170 /** @brief Specifies default quantization parameters for I-Vop. Iquant[n] represents the n-th layer default quantization parameter. The default is Iquant[0]=12.*/ 171 Int iQuant[2]; 172 173 /** @brief Specifies default quantization parameters for P-Vop. Pquant[n] represents the n-th layer default quantization parameter. The default is Pquant[0]=10.*/ 174 Int pQuant[2]; 175 176 /** @brief specifies quantization mode (H263 mode or MPEG mode) of the encoded base and enhance layer (if any). 177 * In Simple and Simple Scalable profile, we use only H263 mode.*/ 178 Int quantType[2]; 179 180 /** @brief Sets rate control algorithm, one of (CONSTANT_Q, CBR_1, or VBR_1). 181 * CONSTANT_Q uses the default quantization values to encode the sequence. 182 * CBR_1 (constant bit rate) controls the output at a desired bit rate 183 * VBR_1 (variable bit rate) gives better picture quality at the expense of bit rate fluctuation 184 * Note: type=CONSTANT_Q produces sequences with arbitrary bit rate. 185 * type=CBR_1 produces sequences suitable for streaming. 186 * type=VBR_1 produces sequences suitable for download. */ 187 MP4RateControlType rcType; 188 189 /** @brief Sets the VBV buffer size (in the unit of second delay) used to prevent buffer overflow and underflow 190 * on the decoder side. This function is redundant to PVSetVBVSize. Either one of them is used at a time. */ 191 float vbvDelay; 192 193 /** @brief Specifies whether frame skipping is permitted or not. When rate control type is set to CONSTANT_Q 194 * frame skipping is automatically banned. In CBR_1 and VBR_1 rate control, frame skipping is allowed by default. 195 * However, users can force no frame skipping with this flag, but buffer constraint may be violated.*/ 196 ParamEncMode noFrameSkipped; 197 198 /** @brief Sets the maximum number of P-frames between two I-frames. I-frame mode is periodically forced 199 * if no I-frame is encoded after the specified period to add error resiliency and help resynchronize in case of errors. 200 * If scene change detection can add additional I-frame if new scenes are detected. 201 * intraPeriod is the I frame interval in terms of second. 202 * intraPeriod =0 indicates I-frame encoding only; 203 * intraPeriod = -1 indicates I-frame followed by all P-frames; (default) 204 * intraPeriod = N, indicates the number of P-frames between 2 I-frames.*/ 205 Int intraPeriod; 206 207 208 /** @brief Specifies the number Intra MBs to be refreshed in a P-frame. */ 209 Int numIntraMB; 210 211 /** 212 * @brief Specifies whether the scene change detection (SCD) is enabled or disabled. 213 * With SCD enable, when a new scene is detected, I-Vop mode will be used for the first frame of 214 * the new scene resulting in better picture quality. An insertion of an I-VOP resets the intraPeriod 215 * specified by the IntraPeriodAPI().*/ 216 ParamEncMode sceneDetect; 217 218 /** @brief Specifies the search range of motion estimation search. Larger value implies 219 * larger search range, better motion vector match, but more complexity. 220 * If searchRange=n, the motion vector search is in the range of [-n,n-1] pixels. 221 * If half-pel mode is on, the range is [-n, (n-1)+1/2] pixels. The default value is 16.*/ 222 Int searchRange; 223 224 /** @brief Turns on/off 8x8 block motion estimation and compensation. 225 * If on, four motion vectors may be used for motion estimation and compensation of a macroblock, 226 * otherwise one motion vector per macroblock is used. When the 8x8 MV is off, the total encoding complexity 227 * is less but the image quality is also worse. Therefore, it can be used in complexity limited environment.*/ 228 ParamEncMode mv8x8Enable; 229 230 231 /** @brief Set the threshold for using intra DC VLC. 232 * Value must range from 0-7.*/ 233 Int intraDCVlcTh; 234 235 /** @brief This flag turns on the use of AC prediction */ 236 Bool useACPred; 237 238 } VideoEncOptions; 239 240 #ifdef __cplusplus 241 extern "C" 242 { 243 #endif 244 245 246 /* API's */ 247 /* Always start with this one !!*/ 248 /** 249 * @brief Gets default encoding options. This way users only have to set relevant encoding options and leave the one 250 * they are unsure of. 251 * @encOption Pointer to VideoEncOption structure. 252 * @encUseCase This value determines the set of default encoding options, for example, different encoding options 253 * are assigned to streaming use-case as compared to download use-case. It can be project dependent too. 254 * @return true for correct operation; false if error happens 255 */ 256 OSCL_IMPORT_REF Bool PVGetDefaultEncOption(VideoEncOptions *encOption, Int encUseCase); 257 258 /** 259 * @brief Verifies the consistency of encoding parameters, allocates memory needed and set necessary internal variables. 260 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 261 * @return true for correct operation; false if error happens 262 */ 263 OSCL_IMPORT_REF Bool PVInitVideoEncoder(VideoEncControls *encCtrl, VideoEncOptions *encOption); 264 265 /* acquiring encoder info APIs */ 266 /** 267 * @brief This function returns VOL header. It has to be called before the frame is encoded. If so, 268 * then the VOL Header is passed back to the application. Then all frames that are encoded do not contain the VOL Header. 269 * If you do not call the API then the VOL Header is passed within the first frame that is encoded. 270 * The behavior is unknown if it is called after the first frame is encoded. It is mainly used for MP4 file format authoring. 271 * @param encCtrl is video encoder control structure that is always passed as input in all APIs. 272 * @param volHeader is the Buffer for VOL header. 273 * @param size is the size of VOL header in bytes. 274 * @param layer is the layer of the requested VOL header. 275 * @return true for correct operation; false if error happens. 276 */ 277 OSCL_IMPORT_REF Bool PVGetVolHeader(VideoEncControls *encCtrl, UChar *volHeader, Int *size, Int layer); 278 279 /** 280 * @brief This function returns the profile and level in H.263 coding when the encoding parameters are set 281 * @param encCtrl is video encoder control structure that is always passed as input in all APIs. 282 * @param profileID is the pointer of the profile ID. Right now we only support profile 0 283 * @param levelID is the pointer of the level ID that could be 10-70. 284 * @return true for correct operation; false if error happens. 285 */ 286 OSCL_IMPORT_REF Bool PVGetH263ProfileLevelID(VideoEncControls *encCtrl, Int *profileID, Int *levelID); 287 288 /** 289 * @brief This function returns the profile and level of MPEG4 when the encoding parameters are set 290 * @param encCtrl is video encoder control structure that is always passed as input in all APIs. 291 * @param profile_level is the pointer of the profile enumeration 292 * @param nLayer is the index of the layer of interest 293 * @return true for correct operation; false if error happens. 294 */ 295 OSCL_IMPORT_REF Bool PVGetMPEG4ProfileLevelID(VideoEncControls *encCtrl, Int *profile_level, Int nLayer); 296 297 /** 298 * @brief This function returns maximum frame size in bytes 299 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 300 * @param maxVideoFrameSize is the pointer of the maximum frame size 301 * @return true for correct operation; false if error happens 302 */ 303 OSCL_IMPORT_REF Bool PVGetMaxVideoFrameSize(VideoEncControls *encCtrl, Int *maxVideoFrameSize); 304 305 #ifndef LIMITED_API 306 /** 307 * @brief This function returns the total amount of memory (in bytes) allocated by the encoder library. 308 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 309 * @return true for correct operation; false if error happens 310 */ 311 OSCL_IMPORT_REF Int PVGetEncMemoryUsage(VideoEncControls *encCtrl); 312 313 /** 314 * @brief This function is used by PVAuthor to get the size of the VBV buffer. 315 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 316 * @param VBVSize is the pointer of The size of the VBV buffer in bytes. 317 * @return true for correct operation; false if error happens 318 */ 319 OSCL_IMPORT_REF Bool PVGetVBVSize(VideoEncControls *encCtrl, Int *VBVSize); 320 #endif 321 322 /** 323 * @brief This function encodes a frame in YUV 4:2:0 format from the *video_in input frame and put the result in YUV 324 * for reconstructed frame and bstream for MPEG4 bitstream. The application is required to allocate memory for 325 * bitstream buffer.The size of the input bitstream memory and the returned output buffer are specified in the 326 * size field. The encoded layer is specified by the nLayer field. If the current frame is not encoded, size=0 and nLayer=-1. 327 * Note: If the allocated buffer size is too small to fit a bitstream of a frame, then those extra bits will be left out 328 * which can cause syntactic error at the decoder side. 329 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 330 * @param vid_in is the pointer to VideoEncFrameIO structure containing the YUV input data 331 * @param vid_out is the pointer to VideoEncFrameIO structure containing the reconstructed YUV output data after encoding 332 * @param nextModTime is the timestamp encoder expects from the next input 333 * @param bstream is the pointer to MPEG4 bitstream buffer 334 * @param size is the size of bitstream buffer allocated (input) and size of the encoded bitstream (output). 335 * @param nLayer is the layer of the encoded frame either 0 for base or 1 for enhancement layer. The value -1 indicates skipped frame due to buffer overflow. 336 * @return true newfor correct operation; false if error happens 337 */ 338 OSCL_IMPORT_REF Bool PVEncodeVideoFrame(VideoEncControls *encCtrl, VideoEncFrameIO *vid_in, VideoEncFrameIO *vid_out, 339 ULong *nextModTime, UChar *bstream, Int *size, Int *nLayer); 340 341 342 /** 343 * @brief This function is used to query overrun buffer. It is used when PVEncodeVideoFrame.returns size that is 344 * larger than the input size. 345 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 346 * @return Pointer to the overrun buffer. NULL if overrun buffer is not used. 347 */ 348 OSCL_IMPORT_REF UChar* PVGetOverrunBuffer(VideoEncControls *encCtrl); 349 350 #ifndef NO_SLICE_ENCODE /* This set of APIs are not working. This functionality has been partially 351 replaced by the introduction of overrun buffer. */ 352 353 /* slice-based coding */ 354 /** 355 * @brief This function sets the input YUV frame and timestamp to be encoded by the slice-based encoding function PVEncodeSlice(). 356 * It also return the memory address the reconstructed frame will be copied to (in advance) and the coded layer number. 357 * The encoder library processes the timestamp and determine if this frame is to be encoded or not. If the current frame 358 * is not encoded, nLayer=-1. For frame-based motion estimation, the motion estimation of the entire frame is also performed 359 * in this function. For MB-based motion estimation, the motion vector is searched while coding each MB in PVEncodeSlice(). 360 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 361 * @param vid_in is the pointer to VideoEncFrameIO structure containing the YUV input data 362 * @param nextModTime is the timestamp encoder expects from the next input if this input is rejected and nLayer is set to -1. 363 * @param nLayer is the layer of the encoded frame either 0 for base or 1 for enhancement layer. The value -1 indicates skipped frame due to buffer overflow. 364 * @return true newfor correct operation; false if error happens 365 */ 366 OSCL_IMPORT_REF Bool PVEncodeFrameSet(VideoEncControls *encCtrl, VideoEncFrameIO *vid_in, ULong *nextModTime, Int *nLayer); 367 /** 368 * @brief This function encodes a GOB (short header mode) or a packet (data partitioning mode or combined mode with resync marker) 369 * and output the reconstructed frame and MPEG4 bitstream. The application is required to allocate memory for the bitstream buffer. 370 * The size of the input bitstream memory and the returned output buffer are specified in the size field. If the buffer size is 371 * smaller than the requested packet size, user has to call PVEncodeSlice again to get the rest of that pending packet before moving 372 * on to the next packet. For the combined mode without resync marker, the function returns when the buffer is full. 373 * The end-of-frame flag indicates the completion of the frame encoding. Next frame must be sent in with PVEncodeFrameSet(). 374 * At the end-of-frame, the next video input address and the next video modulo timestamp will be set. 375 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 376 * @param bstream is the pointer to MPEG4 bitstream buffer. 377 * @param size is the size of bitstream buffer allocated (input) and size of the encoded bitstream (output). 378 * @param endofFrame is a flag indicating the end-of-frame, '1'. Otherwise, '0'. When PVSetNoCurrentFrameSkip is OFF, 379 * end-of-frame '-1' indicates current frame bitstream must be disregarded. 380 * @param vid_out is the pointer to VideoEncFrameIO structure containing the reconstructed YUV output data after encoding 381 * @param nextModTime is the timestamp encoder expects from the next input 382 * @return true newfor correct operation; false if error happens 383 */ 384 OSCL_IMPORT_REF Bool PVEncodeSlice(VideoEncControls *encCtrl, UChar *bstream, Int *size, 385 Int *endofFrame, VideoEncFrameIO *vid_out, ULong *nextModTime); 386 #endif 387 388 /** 389 * @brief This function returns MP4 file format hint track information. 390 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 391 * @param info is the structure for MP4 hint track information 392 * @return true for correct operation; false if error happens 393 */ 394 OSCL_IMPORT_REF Bool PVGetHintTrack(VideoEncControls *encCtrl, MP4HintTrack *info); 395 396 #ifndef LIMITED_API 397 /** 398 * @brief updates target frame rates of the encoded base and enhance layer (if any) while encoding operation is ongoing. 399 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 400 * @param frameRate is the pointers to array of target frame rates in frames per second, 401 * frameRate[n] represents the n-th layer's target frame rate. 402 * @return true for correct operation; false if error happens 403 */ 404 OSCL_IMPORT_REF Bool PVUpdateEncFrameRate(VideoEncControls *encCtrl, float *frameRate); /* for 2-way */ 405 406 407 /** 408 * @brief updates target bit rates of the encoded base and enhance layer (if any) while encoding operation is ongoing. 409 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 410 * @param bitRate is the pointers to array of target bit rates in bits per second unit, 411 * bitRate[n] represents the n-th layer's target bit rate. 412 * @return true for correct operation; false if error happens 413 */ 414 OSCL_IMPORT_REF Bool PVUpdateBitRate(VideoEncControls *encCtrl, Int *bitRate); /* for 2-way */ 415 416 417 /** 418 * @brief updates the INTRA frame refresh interval while encoding operation is ongoing. 419 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 420 * @param aIFramePeriod is a new value of INTRA frame interval in the unit of number of coded frames. 421 * @return true for correct operation; false if error happens 422 */ 423 424 OSCL_IMPORT_REF Bool PVUpdateIFrameInterval(VideoEncControls *encCtrl, Int aIFramePeriod);/* for 2-way */ 425 426 /** 427 * @brief specifies the number Intra MBs to be refreshed 428 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 429 * @param numMB is the number of Intra MBs to be refreshed 430 * @return true for correct operation; false if error happens 431 */ 432 OSCL_IMPORT_REF Bool PVUpdateNumIntraMBRefresh(VideoEncControls *encCtrl, Int numMB); /* for 2-way */ 433 434 /** 435 * @brief This function is called whenever users want the next base frame to be encoded as an I-Vop. 436 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 437 * @return true for correct operation; false if error happens 438 */ 439 OSCL_IMPORT_REF Bool PVIFrameRequest(VideoEncControls *encCtrl); /* for 2-way */ 440 441 #endif // LIMITED_API 442 443 /* finishing encoder */ 444 /** 445 * @brief This function frees up all the memory allocated by the encoder library. 446 * @param encCtrl is video encoder control structure that is always passed as input in all APIs 447 * @return true for correct operation; false if error happens 448 */ 449 OSCL_IMPORT_REF Bool PVCleanUpVideoEncoder(VideoEncControls *encCtrl); 450 451 #ifdef __cplusplus 452 } 453 #endif 454 #endif /* _MP4ENC_API_H_ */ 455 456