/src/vvenc/source/Lib/vvenc/vvenc.cpp
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | |
43 | | /** |
44 | | \file vvenc.cpp |
45 | | \brief This file contains the external interface of the vvenc SDK. |
46 | | */ |
47 | | |
48 | | #include "vvenc/vvenc.h" |
49 | | #include "vvenc/vvencCfg.h" |
50 | | #include "vvenc/version.h" |
51 | | |
52 | | #include "vvencimpl.h" |
53 | | |
54 | | VVENC_NAMESPACE_BEGIN |
55 | | |
56 | | VVENC_DECL vvencYUVBuffer* vvenc_YUVBuffer_alloc() |
57 | 0 | { |
58 | 0 | vvencYUVBuffer* yuvBuffer = (vvencYUVBuffer*)malloc(sizeof(vvencYUVBuffer)); |
59 | 0 | if( nullptr == yuvBuffer ) |
60 | 0 | { |
61 | 0 | return nullptr; |
62 | 0 | } |
63 | | |
64 | 0 | vvenc_YUVBuffer_default( yuvBuffer ); |
65 | 0 | return yuvBuffer; |
66 | 0 | } |
67 | | |
68 | | VVENC_DECL void vvenc_YUVBuffer_free(vvencYUVBuffer *yuvBuffer, bool freePicBuffer ) |
69 | 0 | { |
70 | 0 | if( yuvBuffer ) |
71 | 0 | { |
72 | 0 | if( freePicBuffer && yuvBuffer->planes[0].ptr ) |
73 | 0 | { |
74 | 0 | vvenc_YUVBuffer_free_buffer ( yuvBuffer ); |
75 | 0 | } |
76 | 0 | free(yuvBuffer); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | VVENC_DECL void vvenc_YUVBuffer_default(vvencYUVBuffer *yuvBuffer ) |
81 | 0 | { |
82 | 0 | if ( nullptr == yuvBuffer ) |
83 | 0 | { |
84 | 0 | return; |
85 | 0 | } |
86 | | |
87 | 0 | for( int i = 0; i < 3; i ++ ) |
88 | 0 | { |
89 | 0 | yuvBuffer->planes[i].ptr = NULL; |
90 | 0 | yuvBuffer->planes[i].width = 0; |
91 | 0 | yuvBuffer->planes[i].height = 0; |
92 | 0 | yuvBuffer->planes[i].stride = 0; |
93 | 0 | } |
94 | 0 | yuvBuffer->sequenceNumber = 0; |
95 | 0 | yuvBuffer->cts = 0; |
96 | 0 | yuvBuffer->ctsValid = false; |
97 | 0 | } |
98 | | |
99 | | |
100 | | VVENC_DECL void vvenc_YUVBuffer_alloc_buffer( vvencYUVBuffer *yuvBuffer, const vvencChromaFormat chFmt, const int frameWidth, const int frameHeight ) |
101 | 0 | { |
102 | 0 | if ( nullptr == yuvBuffer ) |
103 | 0 | { |
104 | 0 | return; |
105 | 0 | } |
106 | | |
107 | 0 | for ( int i = 0; i < 3; i++ ) |
108 | 0 | { |
109 | 0 | vvencYUVPlane& yuvPlane = yuvBuffer->planes[ i ]; |
110 | 0 | yuvPlane.width = vvenc_get_width_of_component ( chFmt, frameWidth, i ); |
111 | 0 | yuvPlane.height = vvenc_get_height_of_component( chFmt, frameHeight, i ); |
112 | 0 | yuvPlane.stride = yuvPlane.width; |
113 | 0 | const int size = yuvPlane.stride * yuvPlane.height; |
114 | 0 | yuvPlane.ptr = ( size > 0 ) ? new int16_t[ size ] : nullptr; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | VVENC_DECL void vvenc_YUVBuffer_free_buffer( vvencYUVBuffer *yuvBuffer ) |
119 | 0 | { |
120 | 0 | if ( nullptr == yuvBuffer ) |
121 | 0 | { |
122 | 0 | return; |
123 | 0 | } |
124 | | |
125 | 0 | for ( int i = 0; i < 3; i++ ) |
126 | 0 | { |
127 | 0 | delete [] yuvBuffer->planes[ i ].ptr; |
128 | 0 | yuvBuffer->planes[ i ].ptr = nullptr; |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | |
133 | | VVENC_DECL vvencAccessUnit* vvenc_accessUnit_alloc() |
134 | 0 | { |
135 | 0 | vvencAccessUnit* accessUnit = (vvencAccessUnit*)malloc(sizeof(vvencAccessUnit)); |
136 | 0 | if( nullptr == accessUnit ) |
137 | 0 | { |
138 | 0 | return nullptr; |
139 | 0 | } |
140 | | |
141 | 0 | vvenc_accessUnit_default( accessUnit ); |
142 | 0 | return accessUnit; |
143 | 0 | } |
144 | | |
145 | | VVENC_DECL void vvenc_accessUnit_free(vvencAccessUnit *accessUnit, bool freePayload ) |
146 | 0 | { |
147 | 0 | if( accessUnit ) |
148 | 0 | { |
149 | 0 | if( freePayload && accessUnit->payload ) |
150 | 0 | { |
151 | 0 | vvenc_accessUnit_free_payload ( accessUnit ); |
152 | 0 | } |
153 | 0 | free(accessUnit); |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | VVENC_DECL void vvenc_accessUnit_alloc_payload(vvencAccessUnit *accessUnit, int payload_size ) |
158 | 0 | { |
159 | 0 | accessUnit->payload = (unsigned char*)malloc(sizeof(unsigned char) * payload_size ); |
160 | 0 | if( nullptr == accessUnit->payload ) |
161 | 0 | { |
162 | 0 | return; |
163 | 0 | } |
164 | 0 | accessUnit->payloadSize = payload_size; |
165 | 0 | accessUnit->payloadUsedSize = 0; |
166 | 0 | } |
167 | | |
168 | | VVENC_DECL void vvenc_accessUnit_free_payload(vvencAccessUnit *accessUnit ) |
169 | 0 | { |
170 | 0 | if( accessUnit->payload ) |
171 | 0 | { |
172 | 0 | free(accessUnit->payload); |
173 | 0 | accessUnit->payloadSize = 0; |
174 | 0 | accessUnit->payloadUsedSize = 0; |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | VVENC_DECL void vvenc_accessUnit_reset(vvencAccessUnit *accessUnit ) |
179 | 0 | { |
180 | 0 | if( nullptr == accessUnit ) |
181 | 0 | { |
182 | 0 | return; |
183 | 0 | } |
184 | 0 | accessUnit->payloadUsedSize = 0; |
185 | 0 | accessUnit->cts = 0; |
186 | 0 | accessUnit->dts = 0; |
187 | 0 | accessUnit->ctsValid = false; |
188 | 0 | accessUnit->dtsValid = false; |
189 | 0 | accessUnit->rap = false; |
190 | 0 | accessUnit->sliceType = VVENC_NUMBER_OF_SLICE_TYPES; |
191 | 0 | accessUnit->refPic = false; |
192 | 0 | accessUnit->temporalLayer = 0; |
193 | 0 | accessUnit->poc = 0; |
194 | 0 | accessUnit->status = 0; |
195 | 0 | accessUnit->essentialBytes = 0; |
196 | | #if VVENC_USE_UNSTABLE_API |
197 | | accessUnit->userData = nullptr; |
198 | | #endif |
199 | |
|
200 | 0 | memset( accessUnit->infoString, 0, sizeof( accessUnit->infoString ) ); |
201 | 0 | accessUnit->infoString[0] ='\0'; |
202 | 0 | } |
203 | | |
204 | | VVENC_DECL void vvenc_accessUnit_default(vvencAccessUnit *accessUnit ) |
205 | 0 | { |
206 | 0 | if( nullptr == accessUnit ) |
207 | 0 | { |
208 | 0 | return; |
209 | 0 | } |
210 | 0 | accessUnit->payload = NULL; |
211 | 0 | accessUnit->payloadSize = 0; |
212 | 0 | vvenc_accessUnit_reset( accessUnit ); |
213 | 0 | } |
214 | | |
215 | | VVENC_DECL vvencEncoder* vvenc_encoder_create() |
216 | 0 | { |
217 | 0 | return (vvencEncoder*) new(std::nothrow) vvenc::VVEncImpl(); |
218 | 0 | } |
219 | | |
220 | | |
221 | | VVENC_DECL int vvenc_encoder_open( vvencEncoder *enc, vvenc_config* config ) |
222 | 0 | { |
223 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
224 | 0 | if (!e) |
225 | 0 | { |
226 | 0 | return VVENC_ERR_INITIALIZE; |
227 | 0 | } |
228 | | |
229 | 0 | int ret = e->init( config ); |
230 | 0 | if (ret != 0) |
231 | 0 | { |
232 | | // Error initializing the decoder |
233 | 0 | return VVENC_ERR_INITIALIZE; |
234 | 0 | } |
235 | | |
236 | 0 | return VVENC_OK; |
237 | 0 | } |
238 | | |
239 | | VVENC_DECL int vvenc_encoder_close(vvencEncoder *enc) |
240 | 0 | { |
241 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
242 | 0 | if (!e) |
243 | 0 | { |
244 | 0 | return VVENC_ERR_INITIALIZE; |
245 | 0 | } |
246 | | |
247 | 0 | int ret = e->uninit(); |
248 | 0 | delete e; |
249 | |
|
250 | 0 | return ret; |
251 | 0 | } |
252 | | |
253 | | VVENC_DECL int vvenc_encoder_set_RecYUVBufferCallback(vvencEncoder *enc, void * ctx, vvencRecYUVBufferCallback callback ) |
254 | 0 | { |
255 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
256 | 0 | if (!e) |
257 | 0 | { |
258 | 0 | return VVENC_ERR_INITIALIZE; |
259 | 0 | } |
260 | | |
261 | 0 | e->setRecYUVBufferCallback( ctx, callback ); |
262 | 0 | return VVENC_OK; |
263 | 0 | } |
264 | | |
265 | | VVENC_DECL int vvenc_init_pass( vvencEncoder *enc, int pass, const char * statsFName ) |
266 | 0 | { |
267 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
268 | 0 | if (!e) |
269 | 0 | { |
270 | 0 | return VVENC_ERR_INITIALIZE; |
271 | 0 | } |
272 | | |
273 | 0 | return e->initPass( pass, statsFName ); |
274 | 0 | } |
275 | | |
276 | | |
277 | | VVENC_DECL int vvenc_encode( vvencEncoder *enc, vvencYUVBuffer* YUVBuffer, vvencAccessUnit* accessUnit, bool* encodeDone ) |
278 | 0 | { |
279 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
280 | 0 | if (!e) |
281 | 0 | { |
282 | 0 | return VVENC_ERR_INITIALIZE; |
283 | 0 | } |
284 | | |
285 | 0 | return e->encode( YUVBuffer, accessUnit, encodeDone ); |
286 | 0 | } |
287 | | |
288 | | VVENC_DECL int vvenc_get_config( vvencEncoder *enc, vvenc_config* cfg ) |
289 | 0 | { |
290 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
291 | 0 | if (!e) |
292 | 0 | { |
293 | 0 | return VVENC_ERR_UNSPECIFIED; |
294 | 0 | } |
295 | | |
296 | 0 | return e->getConfig( *cfg ); |
297 | 0 | } |
298 | | |
299 | | |
300 | | VVENC_DECL int vvenc_reconfig( vvencEncoder *enc, const vvenc_config *cfg ) |
301 | 0 | { |
302 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
303 | 0 | if (!e) |
304 | 0 | { |
305 | 0 | return VVENC_ERR_UNSPECIFIED; |
306 | 0 | } |
307 | | |
308 | 0 | return e->reconfig( *cfg ); |
309 | 0 | } |
310 | | |
311 | | VVENC_DECL int vvenc_check_config( vvencEncoder *enc, const vvenc_config *cfg ) |
312 | 0 | { |
313 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
314 | 0 | if (!e) |
315 | 0 | { |
316 | 0 | return VVENC_ERR_UNSPECIFIED; |
317 | 0 | } |
318 | | |
319 | 0 | return e->checkConfig( *cfg ); |
320 | 0 | } |
321 | | |
322 | | VVENC_DECL int vvenc_get_headers(vvencEncoder *enc, vvencAccessUnit *accessUnit) |
323 | 0 | { |
324 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
325 | 0 | if (!e) |
326 | 0 | { |
327 | 0 | return VVENC_ERR_UNSPECIFIED; |
328 | 0 | } |
329 | | |
330 | 0 | return e->getParameterSets( accessUnit ); |
331 | 0 | } |
332 | | |
333 | | VVENC_DECL const char* vvenc_get_last_error( vvencEncoder *enc ) |
334 | 0 | { |
335 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
336 | 0 | if (!e) |
337 | 0 | { |
338 | 0 | return NULL; |
339 | 0 | } |
340 | | |
341 | 0 | return e->getLastError(); |
342 | 0 | } |
343 | | |
344 | | static std::string VVencDefaultInfo; |
345 | | |
346 | | VVENC_DECL const char* vvenc_get_enc_information( vvencEncoder *enc ) |
347 | 0 | { |
348 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
349 | 0 | if (!e) |
350 | 0 | { |
351 | 0 | VVencDefaultInfo.clear(); |
352 | 0 | VVencDefaultInfo = vvenc::VVEncImpl::createEncoderInfoStr(); |
353 | 0 | return VVencDefaultInfo.c_str(); |
354 | 0 | } |
355 | | |
356 | 0 | return e->getEncoderInfo(); |
357 | 0 | } |
358 | | |
359 | | VVENC_DECL int vvenc_get_num_lead_frames( vvencEncoder *enc ) |
360 | 0 | { |
361 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
362 | 0 | if (!e) |
363 | 0 | { |
364 | 0 | return VVENC_ERR_UNSPECIFIED; |
365 | 0 | } |
366 | | |
367 | 0 | return e->getNumLeadFrames(); |
368 | 0 | } |
369 | | |
370 | | VVENC_DECL int vvenc_get_num_trail_frames( vvencEncoder *enc ) |
371 | 0 | { |
372 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
373 | 0 | if (!e) |
374 | 0 | { |
375 | 0 | return VVENC_ERR_UNSPECIFIED; |
376 | 0 | } |
377 | | |
378 | 0 | return e->getNumTrailFrames(); |
379 | 0 | } |
380 | | |
381 | | VVENC_DECL int vvenc_print_summary( vvencEncoder *enc ) |
382 | 0 | { |
383 | 0 | auto e = (vvenc::VVEncImpl*)enc; |
384 | 0 | if (!e) |
385 | 0 | { |
386 | 0 | return VVENC_ERR_UNSPECIFIED; |
387 | 0 | } |
388 | | |
389 | 0 | return e->printSummary(); |
390 | 0 | } |
391 | | |
392 | | |
393 | | VVENC_DECL const char* vvenc_get_version() |
394 | 0 | { |
395 | 0 | return VVENC_VERSION; |
396 | 0 | } |
397 | | |
398 | | VVENC_DECL const char* vvenc_get_error_msg( int nRet ) |
399 | 0 | { |
400 | 0 | return vvenc::VVEncImpl::getErrorMsg( nRet ); |
401 | 0 | } |
402 | | |
403 | | |
404 | | VVENC_DECL int vvenc_set_logging_callback( void * ctx, vvencLoggingCallback callback ) |
405 | 0 | { |
406 | | // DEPRECATED |
407 | 0 | vvenc::VVEncImpl::registerMsgCbf ( ctx, callback ); |
408 | 0 | return VVENC_OK; |
409 | 0 | } |
410 | | |
411 | | |
412 | | VVENC_DECL const char* vvenc_set_SIMD_extension( const char* simdId ) |
413 | 0 | { |
414 | 0 | return vvenc::VVEncImpl::setSIMDExtension( simdId ); |
415 | 0 | } |
416 | | |
417 | | |
418 | | ///< checks if library has tracing supported enabled (see ENABLE_TRACING). |
419 | | VVENC_DECL bool vvenc_is_tracing_enabled() |
420 | 0 | { |
421 | | #if ENABLE_TRACING |
422 | | return true; |
423 | | #else |
424 | 0 | return false; |
425 | 0 | #endif |
426 | 0 | } |
427 | | |
428 | | static std::string VVencCompileInfo; |
429 | | |
430 | | // creates compile info string containing OS, Compiler and Bit-depth (e.g. 32 or 64 bit). |
431 | | VVENC_DECL const char* vvenc_get_compile_info_string() |
432 | 0 | { |
433 | 0 | VVencCompileInfo.clear(); |
434 | 0 | VVencCompileInfo = vvenc::VVEncImpl::getCompileInfoString(); |
435 | 0 | return VVencCompileInfo.c_str(); |
436 | 0 | } |
437 | | |
438 | | VVENC_DECL int vvenc_decode_bitstream( const char* FileName, const char* trcFile, const char* trcRule) |
439 | 0 | { |
440 | 0 | return vvenc::VVEncImpl::decodeBitstream( FileName, trcFile, trcRule ); |
441 | 0 | } |
442 | | |
443 | | VVENC_DECL int vvenc_get_width_of_component( const vvencChromaFormat chFmt, const int frameWidth, const int compId ) |
444 | 0 | { |
445 | 0 | int w = frameWidth; |
446 | 0 | if ( compId > 0 ) |
447 | 0 | { |
448 | 0 | switch ( chFmt ) |
449 | 0 | { |
450 | 0 | case VVENC_CHROMA_400: w = 0; break; |
451 | 0 | case VVENC_CHROMA_420: |
452 | 0 | case VVENC_CHROMA_422: w = w >> 1; break; |
453 | 0 | default: break; |
454 | 0 | } |
455 | 0 | } |
456 | 0 | return w; |
457 | 0 | } |
458 | | |
459 | | VVENC_DECL int vvenc_get_height_of_component( const vvencChromaFormat chFmt, const int frameHeight, const int compId ) |
460 | 0 | { |
461 | 0 | int h = frameHeight; |
462 | 0 | if ( compId > 0 ) |
463 | 0 | { |
464 | 0 | switch ( chFmt ) |
465 | 0 | { |
466 | 0 | case VVENC_CHROMA_400: h = 0; break; |
467 | 0 | case VVENC_CHROMA_420: h = h >> 1; break; |
468 | 0 | case VVENC_CHROMA_422: |
469 | 0 | default: break; |
470 | 0 | } |
471 | 0 | } |
472 | 0 | return h; |
473 | 0 | } |
474 | | |
475 | | VVENC_NAMESPACE_END |