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