Coverage Report

Created: 2026-06-10 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
1.11k
{
58
1.11k
  vvencYUVBuffer* yuvBuffer = (vvencYUVBuffer*)malloc(sizeof(vvencYUVBuffer));
59
1.11k
  if( nullptr == yuvBuffer )
60
0
  {
61
0
    return nullptr;
62
0
  }
63
64
1.11k
  vvenc_YUVBuffer_default( yuvBuffer );
65
1.11k
  return yuvBuffer;
66
1.11k
}
67
68
VVENC_DECL void vvenc_YUVBuffer_free(vvencYUVBuffer *yuvBuffer, bool freePicBuffer )
69
1.11k
{
70
1.11k
  if( yuvBuffer )
71
1.11k
  {
72
1.11k
    if( freePicBuffer && yuvBuffer->planes[0].ptr )
73
0
    {
74
0
      vvenc_YUVBuffer_free_buffer ( yuvBuffer );
75
0
    }
76
1.11k
    free(yuvBuffer);
77
1.11k
  }
78
1.11k
}
79
80
VVENC_DECL void vvenc_YUVBuffer_default(vvencYUVBuffer *yuvBuffer )
81
1.11k
{
82
1.11k
  if ( nullptr == yuvBuffer )
83
0
  {
84
0
    return;
85
0
  }
86
87
4.44k
  for( int i = 0; i < 3; i ++ )
88
3.33k
  {
89
3.33k
    yuvBuffer->planes[i].ptr     = NULL;
90
3.33k
    yuvBuffer->planes[i].width   = 0;
91
3.33k
    yuvBuffer->planes[i].height  = 0;
92
3.33k
    yuvBuffer->planes[i].stride  = 0;
93
3.33k
  }
94
1.11k
  yuvBuffer->sequenceNumber  = 0;
95
1.11k
  yuvBuffer->cts             = 0;
96
1.11k
  yuvBuffer->ctsValid        = false;
97
1.11k
}
98
99
100
VVENC_DECL void vvenc_YUVBuffer_alloc_buffer( vvencYUVBuffer *yuvBuffer, const vvencChromaFormat chFmt, const int frameWidth, const int frameHeight )
101
1.11k
{
102
1.11k
  if ( nullptr == yuvBuffer )
103
0
  {
104
0
    return;
105
0
  }
106
107
4.44k
  for ( int i = 0; i < 3; i++ )
108
3.33k
  {
109
3.33k
    vvencYUVPlane&    yuvPlane = yuvBuffer->planes[ i ];
110
3.33k
    yuvPlane.width  = vvenc_get_width_of_component ( chFmt, frameWidth,  i );
111
3.33k
    yuvPlane.height = vvenc_get_height_of_component( chFmt, frameHeight, i );
112
3.33k
    yuvPlane.stride = yuvPlane.width;
113
3.33k
    const int size  = yuvPlane.stride * yuvPlane.height;
114
3.33k
    yuvPlane.ptr    = ( size > 0 ) ? new int16_t[ size ] : nullptr;
115
3.33k
  }
116
1.11k
}
117
118
VVENC_DECL void vvenc_YUVBuffer_free_buffer( vvencYUVBuffer *yuvBuffer )
119
1.11k
{
120
1.11k
  if ( nullptr == yuvBuffer )
121
0
  {
122
0
    return;
123
0
  }
124
125
4.44k
  for ( int i = 0; i < 3; i++ )
126
3.33k
  {
127
3.33k
    delete [] yuvBuffer->planes[ i ].ptr;
128
3.33k
    yuvBuffer->planes[ i ].ptr = nullptr;
129
3.33k
  }
130
1.11k
}
131
132
133
VVENC_DECL vvencAccessUnit* vvenc_accessUnit_alloc()
134
2.22k
{
135
2.22k
  vvencAccessUnit* accessUnit = (vvencAccessUnit*)malloc(sizeof(vvencAccessUnit));
136
2.22k
  if( nullptr == accessUnit )
137
0
  {
138
0
    return nullptr;
139
0
  }
140
141
2.22k
  vvenc_accessUnit_default( accessUnit );
142
2.22k
  return accessUnit;
143
2.22k
}
144
145
VVENC_DECL void vvenc_accessUnit_free(vvencAccessUnit *accessUnit, bool freePayload  )
146
2.22k
{
147
2.22k
  if( accessUnit )
148
2.22k
  {
149
2.22k
    if( freePayload && accessUnit->payload )
150
2.22k
    {
151
2.22k
      vvenc_accessUnit_free_payload ( accessUnit );
152
2.22k
    }
153
2.22k
    free(accessUnit);
154
2.22k
  }
155
2.22k
}
156
157
VVENC_DECL void vvenc_accessUnit_alloc_payload(vvencAccessUnit *accessUnit, int payload_size )
158
2.22k
{
159
2.22k
  accessUnit->payload = (unsigned char*)malloc(sizeof(unsigned char) * payload_size );
160
2.22k
  if( nullptr == accessUnit->payload )
161
0
  {
162
0
    return;
163
0
  }
164
2.22k
  accessUnit->payloadSize = payload_size;
165
2.22k
  accessUnit->payloadUsedSize = 0;
166
2.22k
}
167
168
VVENC_DECL void vvenc_accessUnit_free_payload(vvencAccessUnit *accessUnit )
169
2.22k
{
170
2.22k
  if( accessUnit->payload )
171
2.22k
  {
172
2.22k
    free(accessUnit->payload);
173
2.22k
    accessUnit->payloadSize = 0;
174
2.22k
    accessUnit->payloadUsedSize = 0;
175
2.22k
  }
176
2.22k
}
177
178
VVENC_DECL void vvenc_accessUnit_reset(vvencAccessUnit *accessUnit )
179
4.44k
{
180
4.44k
  if( nullptr == accessUnit )
181
0
  {
182
0
    return;
183
0
  }
184
4.44k
  accessUnit->payloadUsedSize = 0;
185
4.44k
  accessUnit->cts             = 0;
186
4.44k
  accessUnit->dts             = 0;
187
4.44k
  accessUnit->ctsValid        = false;
188
4.44k
  accessUnit->dtsValid        = false;
189
4.44k
  accessUnit->rap             = false;
190
4.44k
  accessUnit->sliceType       = VVENC_NUMBER_OF_SLICE_TYPES;
191
4.44k
  accessUnit->refPic          = false;
192
4.44k
  accessUnit->temporalLayer   = 0;
193
4.44k
  accessUnit->poc             = 0;
194
4.44k
  accessUnit->status          = 0;
195
4.44k
  accessUnit->essentialBytes  = 0;
196
#if VVENC_USE_UNSTABLE_API
197
  accessUnit->userData        = nullptr;
198
#endif
199
200
4.44k
  memset( accessUnit->infoString, 0, sizeof( accessUnit->infoString ) );
201
4.44k
  accessUnit->infoString[0]   ='\0';
202
4.44k
}
203
204
VVENC_DECL void vvenc_accessUnit_default(vvencAccessUnit *accessUnit )
205
2.22k
{
206
2.22k
  if( nullptr == accessUnit )
207
0
  {
208
0
    return;
209
0
  }
210
2.22k
  accessUnit->payload         = NULL;
211
2.22k
  accessUnit->payloadSize     = 0;
212
2.22k
  vvenc_accessUnit_reset( accessUnit );
213
2.22k
}
214
215
VVENC_DECL vvencEncoder* vvenc_encoder_create()
216
1.11k
{
217
1.11k
  return (vvencEncoder*) new(std::nothrow) vvenc::VVEncImpl();
218
1.11k
}
219
220
221
VVENC_DECL int vvenc_encoder_open( vvencEncoder *enc, vvenc_config* config )
222
1.11k
{
223
1.11k
  auto e = (vvenc::VVEncImpl*)enc;
224
1.11k
  if (!e)
225
0
  {
226
0
    return VVENC_ERR_INITIALIZE;
227
0
  }
228
229
1.11k
  int ret = e->init( config );
230
1.11k
  if (ret != 0)
231
0
  {
232
    // Error initializing the decoder
233
0
    return VVENC_ERR_INITIALIZE;
234
0
  }
235
236
1.11k
  return VVENC_OK;
237
1.11k
}
238
239
VVENC_DECL int vvenc_encoder_close(vvencEncoder *enc)
240
1.11k
{
241
1.11k
  auto e = (vvenc::VVEncImpl*)enc;
242
1.11k
  if (!e)
243
0
  {
244
0
    return VVENC_ERR_INITIALIZE;
245
0
  }
246
247
1.11k
  int ret = e->uninit();
248
1.11k
  delete e;
249
250
1.11k
  return ret;
251
1.11k
}
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
2.22k
{
279
2.22k
  auto e = (vvenc::VVEncImpl*)enc;
280
2.22k
  if (!e)
281
0
  {
282
0
    return VVENC_ERR_INITIALIZE;
283
0
  }
284
285
2.22k
  return e->encode( YUVBuffer, accessUnit, encodeDone );
286
2.22k
}
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
3.33k
{
445
3.33k
  int w = frameWidth;
446
3.33k
  if ( compId > 0 )
447
2.22k
  {
448
2.22k
    switch ( chFmt )
449
2.22k
    {
450
0
      case VVENC_CHROMA_400: w = 0;      break;
451
2.22k
      case VVENC_CHROMA_420:
452
2.22k
      case VVENC_CHROMA_422: w = w >> 1; break;
453
0
      default: break;
454
2.22k
    }
455
2.22k
  }
456
3.33k
  return w;
457
3.33k
}
458
459
VVENC_DECL int vvenc_get_height_of_component( const vvencChromaFormat chFmt, const int frameHeight, const int compId )
460
3.33k
{
461
3.33k
  int h = frameHeight;
462
3.33k
  if ( compId > 0 )
463
2.22k
  {
464
2.22k
    switch ( chFmt )
465
2.22k
    {
466
0
      case VVENC_CHROMA_400: h = 0;      break;
467
2.22k
      case VVENC_CHROMA_420: h = h >> 1; break;
468
0
      case VVENC_CHROMA_422:
469
0
      default: break;
470
2.22k
    }
471
2.22k
  }
472
3.33k
  return h;
473
3.33k
}
474
475
VVENC_NAMESPACE_END