Coverage Report

Created: 2026-09-13 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/de265.cc
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#define DEBUG_INSERT_STREAM_ERRORS 0
22
23
24
#include "de265.h"
25
#include "decctx.h"
26
#include "util.h"
27
#include "scan.h"
28
#include "image.h"
29
#include "sei.h"
30
31
#include <assert.h>
32
#include <string.h>
33
#include <stdlib.h>
34
#include <mutex>
35
36
37
// TODO: should be in some vps.c related header
38
de265_error read_vps(decoder_context* ctx, bitreader* reader, video_parameter_set* vps);
39
40
extern "C" {
41
LIBDE265_API const char *de265_get_version(void)
42
0
{
43
0
    return (LIBDE265_VERSION);
44
0
}
45
46
LIBDE265_API uint32_t de265_get_version_number(void)
47
0
{
48
0
    return (LIBDE265_NUMERIC_VERSION);
49
0
}
50
51
static uint8_t bcd2dec(uint8_t v)
52
0
{
53
0
  return (v>>4) * 10 + (v & 0x0F);
54
0
}
55
56
LIBDE265_API int de265_get_version_number_major(void)
57
0
{
58
0
  return bcd2dec(((LIBDE265_NUMERIC_VERSION)>>24) & 0xFF);
59
0
}
60
61
LIBDE265_API int de265_get_version_number_minor(void)
62
0
{
63
0
  return bcd2dec(((LIBDE265_NUMERIC_VERSION)>>16) & 0xFF);
64
0
}
65
66
LIBDE265_API int de265_get_version_number_maintenance(void)
67
0
{
68
0
  return bcd2dec(((LIBDE265_NUMERIC_VERSION)>>8) & 0xFF);
69
0
}
70
71
72
LIBDE265_API const char* de265_get_error_text(de265_error err)
73
0
{
74
0
  switch (err) {
75
0
  case DE265_OK: return "no error";
76
0
  case DE265_ERROR_NO_SUCH_FILE: return "no such file";
77
    //case DE265_ERROR_NO_STARTCODE: return "no startcode found";
78
    //case DE265_ERROR_EOF: return "end of file";
79
0
  case DE265_ERROR_COEFFICIENT_OUT_OF_IMAGE_BOUNDS: return "coefficient out of image bounds";
80
0
  case DE265_ERROR_CHECKSUM_MISMATCH: return "image checksum mismatch";
81
0
  case DE265_ERROR_CTB_OUTSIDE_IMAGE_AREA: return "CTB outside of image area";
82
0
  case DE265_ERROR_OUT_OF_MEMORY: return "out of memory";
83
0
  case DE265_ERROR_CODED_PARAMETER_OUT_OF_RANGE: return "coded parameter out of range";
84
0
  case DE265_ERROR_IMAGE_BUFFER_FULL: return "DPB/output queue full";
85
0
  case DE265_ERROR_IMAGE_SIZE_EXCEEDS_SECURITY_LIMIT: return "image size exceeds security limit";
86
0
  case DE265_ERROR_NAL_SIZE_EXCEEDS_SECURITY_LIMIT: return "NAL unit size exceeds security limit";
87
0
  case DE265_ERROR_INVALID_ARGUMENT: return "invalid argument";
88
0
  case DE265_ERROR_CANNOT_START_THREADPOOL: return "cannot start decoding threads";
89
0
  case DE265_ERROR_LIBRARY_INITIALIZATION_FAILED: return "global library initialization failed";
90
0
  case DE265_ERROR_LIBRARY_NOT_INITIALIZED: return "cannot free library data (not initialized";
91
92
  //case DE265_ERROR_MAX_THREAD_CONTEXTS_EXCEEDED:
93
  //  return "internal error: maximum number of thread contexts exceeded";
94
  //case DE265_ERROR_MAX_NUMBER_OF_SLICES_EXCEEDED:
95
  //  return "internal error: maximum number of slices exceeded";
96
0
  case DE265_ERROR_NOT_IMPLEMENTED_YET:
97
0
    return "unimplemented decoder feature";
98
    //case DE265_ERROR_SCALING_LIST_NOT_IMPLEMENTED:
99
    //return "scaling list not implemented";
100
101
0
  case DE265_ERROR_WAITING_FOR_INPUT_DATA:
102
0
    return "no more input data, decoder stalled";
103
0
  case DE265_ERROR_CANNOT_PROCESS_SEI:
104
0
    return "SEI data cannot be processed";
105
0
  case DE265_ERROR_PARAMETER_PARSING:
106
0
    return "command-line parameter error";
107
0
  case DE265_ERROR_NO_INITIAL_SLICE_HEADER:
108
0
    return "first slice missing, cannot decode dependent slice";
109
0
  case DE265_ERROR_PREMATURE_END_OF_SLICE:
110
0
    return "premature end of slice data";
111
0
  case DE265_ERROR_UNSPECIFIED_DECODING_ERROR:
112
0
    return "unspecified decoding error";
113
114
0
  case DE265_WARNING_NO_WPP_CANNOT_USE_MULTITHREADING:
115
0
    return "Cannot run decoder multi-threaded because stream does not support WPP";
116
0
  case DE265_WARNING_WARNING_BUFFER_FULL:
117
0
    return "Too many warnings queued";
118
0
  case DE265_WARNING_PREMATURE_END_OF_SLICE_SEGMENT:
119
0
    return "Premature end of slice segment";
120
0
  case DE265_WARNING_INCORRECT_ENTRY_POINT_OFFSET:
121
0
    return "Incorrect entry-point offsets";
122
0
  case DE265_WARNING_CTB_OUTSIDE_IMAGE_AREA:
123
0
    return "CTB outside of image area (concealing stream error...)";
124
0
  case DE265_WARNING_SPS_HEADER_INVALID:
125
0
    return "sps header invalid";
126
0
  case DE265_WARNING_PPS_HEADER_INVALID:
127
0
    return "pps header invalid";
128
0
  case DE265_WARNING_SLICEHEADER_INVALID:
129
0
    return "slice header invalid";
130
0
  case DE265_WARNING_INCORRECT_MOTION_VECTOR_SCALING:
131
0
    return "impossible motion vector scaling";
132
0
  case DE265_WARNING_NONEXISTING_PPS_REFERENCED:
133
0
    return "non-existing PPS referenced";
134
0
  case DE265_WARNING_NONEXISTING_SPS_REFERENCED:
135
0
    return "non-existing SPS referenced";
136
0
  case DE265_WARNING_BOTH_PREDFLAGS_ZERO:
137
0
    return "both predFlags[] are zero in MC";
138
0
  case DE265_WARNING_NONEXISTING_REFERENCE_PICTURE_ACCESSED:
139
0
    return "non-existing reference picture accessed";
140
0
  case DE265_WARNING_NUMMVP_NOT_EQUAL_TO_NUMMVQ:
141
0
    return "numMV_P != numMV_Q in deblocking";
142
0
  case DE265_WARNING_NUMBER_OF_SHORT_TERM_REF_PIC_SETS_OUT_OF_RANGE:
143
0
    return "number of short-term ref-pic-sets out of range";
144
0
  case DE265_WARNING_SHORT_TERM_REF_PIC_SET_OUT_OF_RANGE:
145
0
    return "short-term ref-pic-set index out of range";
146
0
  case DE265_WARNING_FAULTY_REFERENCE_PICTURE_LIST:
147
0
    return "faulty reference picture list";
148
0
  case DE265_WARNING_EOSS_BIT_NOT_SET:
149
0
    return "end_of_sub_stream_one_bit not set to 1 when it should be";
150
0
  case DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED:
151
0
    return "maximum number of reference pictures exceeded";
152
0
  case DE265_WARNING_INVALID_CHROMA_FORMAT:
153
0
    return "invalid chroma format in SPS header";
154
0
  case DE265_WARNING_SLICE_SEGMENT_ADDRESS_INVALID:
155
0
    return "slice segment address invalid";
156
0
  case DE265_WARNING_DEPENDENT_SLICE_WITH_ADDRESS_ZERO:
157
0
    return "dependent slice with address 0";
158
0
  case DE265_WARNING_NUMBER_OF_THREADS_LIMITED_TO_MAXIMUM:
159
0
    return "number of threads limited to maximum amount";
160
0
  case DE265_NON_EXISTING_LT_REFERENCE_CANDIDATE_IN_SLICE_HEADER:
161
0
    return "non-existing long-term reference candidate specified in slice header";
162
0
  case DE265_WARNING_CANNOT_APPLY_SAO_OUT_OF_MEMORY:
163
0
    return "cannot apply SAO because we ran out of memory";
164
0
  case DE265_WARNING_SPS_MISSING_CANNOT_DECODE_SEI:
165
0
    return "SPS header missing, cannot decode SEI";
166
0
  case DE265_WARNING_COLLOCATED_MOTION_VECTOR_OUTSIDE_IMAGE_AREA:
167
0
    return "collocated motion-vector is outside image area";
168
0
  case DE265_WARNING_PCM_BITDEPTH_TOO_LARGE:
169
0
    return "PCM bit-depth too large";
170
0
  case DE265_WARNING_REFERENCE_IMAGE_BIT_DEPTH_DOES_NOT_MATCH:
171
0
    return "Bit-depth of reference image does not match current image";
172
0
  case DE265_WARNING_REFERENCE_IMAGE_SIZE_DOES_NOT_MATCH_SPS:
173
0
    return "Size of reference image does not match current size in SPS";
174
0
  case DE265_WARNING_CHROMA_OF_CURRENT_IMAGE_DOES_NOT_MATCH_SPS:
175
0
    return "Chroma format of current image does not match chroma in SPS";
176
0
  case DE265_WARNING_BIT_DEPTH_OF_CURRENT_IMAGE_DOES_NOT_MATCH_SPS:
177
0
    return "Bit-depth of current image does not match SPS";
178
0
  case DE265_WARNING_REFERENCE_IMAGE_CHROMA_FORMAT_DOES_NOT_MATCH:
179
0
    return "Chroma format of reference image does not match current image";
180
0
  case DE265_WARNING_INVALID_SLICE_HEADER_INDEX_ACCESS:
181
0
    return "Access with invalid slice header index";
182
0
  case DE265_WARNING_INVALID_TU_BLOCK_SPLIT:
183
0
    return "Transform block split below minimum transform size";
184
0
  case DE265_WARNING_RICE_PARAMETER_OUT_OF_RANGE:
185
0
    return "Rice parameter or StatCoeff out of range, clamped";
186
0
  case DE265_WARNING_MAX_NUMBER_OF_SEI_MESSAGES_EXCEEDED:
187
0
    return "number of SEI messages exceeds security limit, dropped";
188
0
  case DE265_WARNING_SLICE_SEGMENT_ADDRESS_NOT_INCREASING:
189
0
    return "slice segment address not increasing within picture, slice segment dropped";
190
191
0
  default: return "unknown error";
192
0
  }
193
0
}
194
195
LIBDE265_API int de265_isOK(de265_error err)
196
0
{
197
0
  return err == DE265_OK || err >= 1000;
198
0
}
199
200
201
202
static int de265_init_count;
203
204
static std::mutex& de265_init_mutex()
205
2
{
206
2
  static std::mutex de265_init_mutex;
207
2
  return de265_init_mutex;
208
2
}
209
210
211
LIBDE265_API de265_error de265_init()
212
2
{
213
2
  std::lock_guard<std::mutex> lock(de265_init_mutex());
214
215
2
  de265_init_count++;
216
217
2
  if (de265_init_count > 1) {
218
    // we are not the first -> already initialized
219
220
0
    return DE265_OK;
221
0
  }
222
223
224
  // do initializations
225
226
2
  init_scan_orders();
227
2
  pps_scan_cache_init();
228
229
2
  if (!alloc_and_init_significant_coeff_ctxIdx_lookupTable()) {
230
0
    pps_scan_cache_free();
231
0
    de265_init_count--;
232
0
    return DE265_ERROR_LIBRARY_INITIALIZATION_FAILED;
233
0
  }
234
235
2
  return DE265_OK;
236
2
}
237
238
LIBDE265_API de265_error de265_free()
239
0
{
240
0
  std::lock_guard<std::mutex> lock(de265_init_mutex());
241
242
0
  if (de265_init_count<=0) {
243
0
    return DE265_ERROR_LIBRARY_NOT_INITIALIZED;
244
0
  }
245
246
0
  de265_init_count--;
247
248
0
  if (de265_init_count==0) {
249
0
    free_significant_coeff_ctxIdx_lookupTable();
250
0
    pps_scan_cache_free();
251
0
  }
252
253
0
  return DE265_OK;
254
0
}
255
256
257
LIBDE265_API de265_decoder_context* de265_new_decoder()
258
0
{
259
0
  de265_error init_err = de265_init();
260
0
  if (init_err != DE265_OK) {
261
0
    return nullptr;
262
0
  }
263
264
0
  decoder_context* ctx = new decoder_context;
265
0
  if (!ctx) {
266
0
    de265_free();
267
0
    return nullptr;
268
0
  }
269
270
0
  return reinterpret_cast<de265_decoder_context*>(ctx);
271
0
}
272
273
274
LIBDE265_API de265_error de265_free_decoder(de265_decoder_context* de265ctx)
275
0
{
276
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
277
278
0
  ctx->stop_thread_pool();
279
280
0
  delete ctx;
281
282
0
  return de265_free();
283
0
}
284
285
286
LIBDE265_API de265_error de265_start_worker_threads(de265_decoder_context* de265ctx, int number_of_threads)
287
0
{
288
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
289
290
0
  if (number_of_threads > MAX_THREADS) {
291
0
    number_of_threads = MAX_THREADS;
292
0
  }
293
294
0
  if (number_of_threads>0) {
295
0
    de265_error err = ctx->start_thread_pool(number_of_threads);
296
0
    if (de265_isOK(err)) {
297
0
      err = DE265_OK;
298
0
    }
299
0
    return err;
300
0
  }
301
0
  else {
302
0
    return DE265_OK;
303
0
  }
304
0
}
305
306
307
#ifndef LIBDE265_DISABLE_DEPRECATED
308
LIBDE265_API de265_error de265_decode_data(de265_decoder_context* de265ctx,
309
                                           const void* data8, int len)
310
0
{
311
  //decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
312
0
  de265_error err;
313
0
  if (len > 0) {
314
0
    err = de265_push_data(de265ctx, data8, len, 0, nullptr);
315
0
  } else {
316
0
    err = de265_flush_data(de265ctx);
317
0
  }
318
0
  if (err != DE265_OK) {
319
0
    return err;
320
0
  }
321
322
0
  int more = 0;
323
0
  do {
324
0
    err = de265_decode(de265ctx, &more);
325
0
    if (err != DE265_OK) {
326
0
        more = 0;
327
0
    }
328
329
0
    switch (err) {
330
0
    case DE265_ERROR_WAITING_FOR_INPUT_DATA:
331
      // ignore error (didn't exist in 0.4 and before)
332
0
      err = DE265_OK;
333
0
      break;
334
0
    default:
335
0
      break;
336
0
    }
337
0
  } while (more);
338
0
  return err;
339
0
}
340
#endif
341
342
#if 0
343
static void dumpdata(const void* data, int len)
344
{
345
  for (int i=0;i<len;i++) {
346
    printf("%02x ", ((uint8_t*)data)[i]);
347
  }
348
  printf("\n");
349
}
350
#endif
351
352
353
LIBDE265_API de265_error de265_push_data(de265_decoder_context* de265ctx,
354
                                         const void* data8, int len,
355
                                         de265_PTS pts, void* user_data)
356
0
{
357
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
358
0
  const uint8_t* data = reinterpret_cast<const uint8_t*>(data8);
359
360
  //printf("push data (size %d)\n",len);
361
  //dumpdata(data8,16);
362
363
0
  return ctx->nal_parser.push_data(data,len,pts,user_data);
364
0
}
365
366
367
LIBDE265_API de265_error de265_push_NAL(de265_decoder_context* de265ctx,
368
                                        const void* data8, int len,
369
                                        de265_PTS pts, void* user_data)
370
0
{
371
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
372
0
  const uint8_t* data = reinterpret_cast<const uint8_t*>(data8);
373
374
  //printf("push NAL (size %d)\n",len);
375
  //dumpdata(data8,16);
376
377
0
  return ctx->nal_parser.push_NAL(data,len,pts,user_data);
378
0
}
379
380
381
LIBDE265_API de265_error de265_decode(de265_decoder_context* de265ctx, int* more)
382
0
{
383
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
384
385
0
  return ctx->decode(more);
386
0
}
387
388
389
LIBDE265_API void        de265_push_end_of_NAL(de265_decoder_context* de265ctx)
390
0
{
391
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
392
393
0
  ctx->nal_parser.flush_data();
394
0
}
395
396
397
LIBDE265_API void        de265_push_end_of_frame(de265_decoder_context* de265ctx)
398
0
{
399
0
  de265_push_end_of_NAL(de265ctx);
400
401
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
402
0
  ctx->nal_parser.mark_end_of_frame();
403
0
}
404
405
406
LIBDE265_API de265_error de265_flush_data(de265_decoder_context* de265ctx)
407
0
{
408
0
  de265_push_end_of_NAL(de265ctx);
409
410
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
411
412
0
  ctx->nal_parser.flush_data();
413
0
  ctx->nal_parser.mark_end_of_stream();
414
415
0
  return DE265_OK;
416
0
}
417
418
419
LIBDE265_API void de265_reset(de265_decoder_context* de265ctx)
420
0
{
421
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
422
423
  //printf("--- reset ---\n");
424
425
0
  ctx->reset();
426
0
}
427
428
429
LIBDE265_API const struct de265_image* de265_get_next_picture(de265_decoder_context* de265ctx)
430
0
{
431
0
  const struct de265_image* img = de265_peek_next_picture(de265ctx);
432
0
  if (img) {
433
0
    de265_release_next_picture(de265ctx);
434
0
  }
435
436
0
  return img;
437
0
}
438
439
440
LIBDE265_API const struct de265_image* de265_peek_next_picture(de265_decoder_context* de265ctx)
441
0
{
442
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
443
444
0
  if (ctx->num_pictures_in_output_queue()>0) {
445
0
    de265_image* img = ctx->get_next_picture_in_output_queue();
446
0
    return img;
447
0
  }
448
0
  else {
449
0
    return nullptr;
450
0
  }
451
0
}
452
453
454
LIBDE265_API void de265_release_next_picture(de265_decoder_context* de265ctx)
455
0
{
456
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
457
458
  // no active output picture -> ignore release request
459
460
0
  if (ctx->num_pictures_in_output_queue()==0) { return; }
461
462
0
  de265_image* next_image = ctx->get_next_picture_in_output_queue();
463
464
0
  loginfo(LogDPB, "release DPB with POC=%d\n",next_image->PicOrderCntVal);
465
466
0
  next_image->PicOutputFlag = false;
467
468
  // TODO: actually, we want to release it here, but we cannot without breaking API
469
  // compatibility, because get_next_picture calls this immediately. Hence, we release
470
  // images while scanning for available slots in the DPB.
471
  // if (next_image->can_be_released()) { next_image->release(); }
472
473
  // pop output queue
474
475
0
  ctx->pop_next_picture_in_output_queue();
476
0
}
477
478
479
480
LIBDE265_API int  de265_get_highest_TID(de265_decoder_context* de265ctx)
481
0
{
482
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
483
0
  return ctx->get_highest_TID();
484
0
}
485
486
LIBDE265_API int  de265_get_current_TID(de265_decoder_context* de265ctx)
487
0
{
488
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
489
0
  return ctx->get_current_TID();
490
0
}
491
492
LIBDE265_API void de265_set_limit_TID(de265_decoder_context* de265ctx,int max_tid)
493
0
{
494
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
495
0
  ctx->set_limit_TID(max_tid);
496
0
}
497
498
LIBDE265_API void de265_set_framerate_ratio(de265_decoder_context* de265ctx,int percent)
499
0
{
500
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
501
0
  ctx->set_framerate_ratio(percent);
502
0
}
503
504
LIBDE265_API int  de265_change_framerate(de265_decoder_context* de265ctx,int more)
505
0
{
506
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
507
0
  return ctx->change_framerate(more);
508
0
}
509
510
511
LIBDE265_API de265_error de265_get_warning(de265_decoder_context* de265ctx)
512
0
{
513
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
514
515
0
  return ctx->get_warning();
516
0
}
517
518
LIBDE265_API void de265_set_parameter_bool(de265_decoder_context* de265ctx, enum de265_param param, int value)
519
0
{
520
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
521
522
0
  switch (param)
523
0
    {
524
0
    case DE265_DECODER_PARAM_BOOL_SEI_CHECK_HASH:
525
0
      ctx->param_sei_check_hash = !!value;
526
0
      break;
527
528
0
    case DE265_DECODER_PARAM_SUPPRESS_FAULTY_PICTURES:
529
0
      ctx->param_suppress_faulty_pictures = !!value;
530
0
      break;
531
532
0
    case DE265_DECODER_PARAM_DISABLE_DEBLOCKING:
533
0
      ctx->param_disable_deblocking = !!value;
534
0
      break;
535
536
0
    case DE265_DECODER_PARAM_DISABLE_SAO:
537
0
      ctx->param_disable_sao = !!value;
538
0
      break;
539
540
      /*
541
    case DE265_DECODER_PARAM_DISABLE_MC_RESIDUAL_IDCT:
542
      ctx->param_disable_mc_residual_idct = !!value;
543
      break;
544
545
    case DE265_DECODER_PARAM_DISABLE_INTRA_RESIDUAL_IDCT:
546
      ctx->param_disable_intra_residual_idct = !!value;
547
      break;
548
      */
549
550
0
    default:
551
0
      assert(false);
552
0
      break;
553
0
    }
554
0
}
555
556
557
LIBDE265_API void de265_set_parameter_int(de265_decoder_context* de265ctx, enum de265_param param, int value)
558
0
{
559
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
560
561
0
  switch (param)
562
0
    {
563
0
    case DE265_DECODER_PARAM_DUMP_SPS_HEADERS:
564
0
      ctx->param_sps_headers_fd = value;
565
0
      break;
566
567
0
    case DE265_DECODER_PARAM_DUMP_VPS_HEADERS:
568
0
      ctx->param_vps_headers_fd = value;
569
0
      break;
570
571
0
    case DE265_DECODER_PARAM_DUMP_PPS_HEADERS:
572
0
      ctx->param_pps_headers_fd = value;
573
0
      break;
574
575
0
    case DE265_DECODER_PARAM_DUMP_SLICE_HEADERS:
576
0
      ctx->param_slice_headers_fd = value;
577
0
      break;
578
579
0
    case DE265_DECODER_PARAM_ACCELERATION_CODE:
580
0
      ctx->set_acceleration_functions(static_cast<enum de265_acceleration>(value));
581
0
      break;
582
583
0
    default:
584
0
      assert(false);
585
0
      break;
586
0
    }
587
0
}
588
589
590
591
592
LIBDE265_API int de265_get_parameter_bool(de265_decoder_context* de265ctx, enum de265_param param)
593
0
{
594
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
595
596
0
  switch (param)
597
0
    {
598
0
    case DE265_DECODER_PARAM_BOOL_SEI_CHECK_HASH:
599
0
      return ctx->param_sei_check_hash;
600
601
0
    case DE265_DECODER_PARAM_SUPPRESS_FAULTY_PICTURES:
602
0
      return ctx->param_suppress_faulty_pictures;
603
604
0
    case DE265_DECODER_PARAM_DISABLE_DEBLOCKING:
605
0
      return ctx->param_disable_deblocking;
606
607
0
    case DE265_DECODER_PARAM_DISABLE_SAO:
608
0
      return ctx->param_disable_sao;
609
610
      /*
611
    case DE265_DECODER_PARAM_DISABLE_MC_RESIDUAL_IDCT:
612
      return ctx->param_disable_mc_residual_idct;
613
614
    case DE265_DECODER_PARAM_DISABLE_INTRA_RESIDUAL_IDCT:
615
      return ctx->param_disable_intra_residual_idct;
616
      */
617
618
0
    default:
619
0
      assert(false);
620
0
      return false;
621
0
    }
622
0
}
623
624
625
static const de265_security_limits disabled_security_limits = {
626
  1,    // version
627
  0,    // max_image_size_pixels
628
  0,    // max_NAL_size_bytes
629
  0     // max_SEI_messages
630
};
631
632
633
LIBDE265_API de265_security_limits* de265_get_security_limits(de265_decoder_context* de265ctx)
634
0
{
635
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
636
0
  return &ctx->param_security_limits;
637
0
}
638
639
640
// Copy security limits field by field, but only those fields that exist in both
641
// structs, i.e. up to min(dst->version, src->version). This keeps copying safe
642
// when 'src' was compiled against a different (older or newer) header version
643
// than 'dst'. Fields not covered by the common version keep their value in 'dst'.
644
static void copy_security_limits(de265_security_limits* dst, const de265_security_limits* src)
645
0
{
646
0
  uint8_t version = (dst->version < src->version) ? dst->version : src->version;
647
648
0
  if (version >= 1) {
649
0
    dst->max_image_size_pixels = src->max_image_size_pixels;
650
0
    dst->max_NAL_size_bytes    = src->max_NAL_size_bytes;
651
0
    dst->max_SEI_messages      = src->max_SEI_messages;
652
0
  }
653
0
}
654
655
656
LIBDE265_API void de265_set_security_limits(de265_decoder_context* de265ctx,
657
                                            const de265_security_limits* limits)
658
0
{
659
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
660
0
  if (limits) {
661
0
    copy_security_limits(&ctx->param_security_limits, limits);
662
0
  }
663
0
}
664
665
666
LIBDE265_API const de265_security_limits* de265_get_disabled_security_limits()
667
0
{
668
0
  return &disabled_security_limits;
669
0
}
670
671
672
LIBDE265_API int de265_get_number_of_input_bytes_pending(de265_decoder_context* de265ctx)
673
0
{
674
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
675
676
0
  return ctx->nal_parser.bytes_in_input_queue();
677
0
}
678
679
680
LIBDE265_API int de265_get_number_of_NAL_units_pending(de265_decoder_context* de265ctx)
681
0
{
682
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
683
684
0
  return ctx->nal_parser.number_of_NAL_units_pending();
685
0
}
686
687
688
LIBDE265_API int de265_get_image_width(const struct de265_image* img,int channel)
689
0
{
690
0
  switch (channel) {
691
0
  case 0:
692
0
    return img->width_confwin;
693
0
  case 1:
694
0
  case 2:
695
0
    return img->chroma_width_confwin;
696
0
  default:
697
0
    return 0;
698
0
  }
699
0
}
700
701
LIBDE265_API int de265_get_image_height(const struct de265_image* img,int channel)
702
0
{
703
0
  switch (channel) {
704
0
  case 0:
705
0
    return img->height_confwin;
706
0
  case 1:
707
0
  case 2:
708
0
    return img->chroma_height_confwin;
709
0
  default:
710
0
    return 0;
711
0
  }
712
0
}
713
714
LIBDE265_API int de265_get_bits_per_pixel(const struct de265_image* img,int channel)
715
0
{
716
0
  switch (channel) {
717
0
  case 0:
718
0
    return img->get_sps().BitDepth_Y;
719
0
  case 1:
720
0
  case 2:
721
0
    return img->get_sps().BitDepth_C;
722
0
  default:
723
0
    return 0;
724
0
  }
725
0
}
726
727
LIBDE265_API enum de265_chroma de265_get_chroma_format(const struct de265_image* img)
728
0
{
729
0
  return img->get_chroma_format();
730
0
}
731
732
LIBDE265_API const uint8_t* de265_get_image_plane(const de265_image* img, int channel, int* stride)
733
0
{
734
0
  assert(channel>=0 && channel <= 2);
735
736
0
  uint8_t* data = img->pixels_confwin[channel];
737
738
0
  if (stride) *stride = static_cast<int>(img->get_image_stride(channel) * ((de265_get_bits_per_pixel(img, channel)+7) / 8));
739
740
0
  return data;
741
0
}
742
743
LIBDE265_API void *de265_get_image_plane_user_data(const struct de265_image* img, int channel)
744
0
{
745
0
  assert(channel>=0 && channel <= 2);
746
747
0
  return img->plane_user_data[channel];
748
0
}
749
750
LIBDE265_API void de265_set_image_plane(de265_image* img, int cIdx, void* mem, int stride, void *userdata)
751
0
{
752
  // The internal "stride" is the number of pixels per line.
753
0
  stride = stride / ((de265_get_bits_per_pixel(img, cIdx)+7) / 8);
754
0
  img->set_image_plane(cIdx, static_cast<uint8_t*>(mem), stride, userdata);
755
0
}
756
757
LIBDE265_API void de265_set_image_allocation_functions(de265_decoder_context* de265ctx,
758
                                                       de265_image_allocation* allocfunc,
759
                                                       void* userdata)
760
0
{
761
0
  decoder_context* ctx = reinterpret_cast<decoder_context*>(de265ctx);
762
763
0
  ctx->set_image_allocation_functions(allocfunc, userdata);
764
0
}
765
766
LIBDE265_API const struct de265_image_allocation *de265_get_default_image_allocation_functions(void)
767
0
{
768
0
  return &de265_image::default_image_allocation;
769
0
}
770
771
LIBDE265_API de265_PTS de265_get_image_PTS(const struct de265_image* img)
772
0
{
773
0
  return img->pts;
774
0
}
775
776
LIBDE265_API void* de265_get_image_user_data(const struct de265_image* img)
777
0
{
778
0
  return img->user_data;
779
0
}
780
781
LIBDE265_API void de265_set_image_user_data(struct de265_image* img, void *user_data)
782
0
{
783
0
  img->user_data = user_data;
784
0
}
785
786
LIBDE265_API void de265_get_image_NAL_header(const struct de265_image* img,
787
                                             int* nal_unit_type,
788
                                             const char** nal_unit_name,
789
                                             int* nuh_layer_id,
790
                                             int* nuh_temporal_id)
791
0
{
792
0
  if (nal_unit_type)   *nal_unit_type   = img->nal_hdr.nal_unit_type;
793
0
  if (nal_unit_name)   *nal_unit_name   = get_NAL_name(img->nal_hdr.nal_unit_type);
794
0
  if (nuh_layer_id)    *nuh_layer_id    = img->nal_hdr.nuh_layer_id;
795
0
  if (nuh_temporal_id) *nuh_temporal_id = img->nal_hdr.nuh_temporal_id;
796
0
}
797
798
LIBDE265_API int de265_get_image_full_range_flag(const struct de265_image* img)
799
0
{
800
0
  return img->get_sps().vui.video_full_range_flag;
801
0
}
802
803
LIBDE265_API int de265_get_image_colour_primaries(const struct de265_image* img)
804
0
{
805
0
  return img->get_sps().vui.colour_primaries;
806
0
}
807
808
LIBDE265_API int de265_get_image_transfer_characteristics(const struct de265_image* img)
809
0
{
810
0
  return img->get_sps().vui.transfer_characteristics;
811
0
}
812
813
LIBDE265_API int de265_get_image_matrix_coefficients(const struct de265_image* img)
814
0
{
815
0
  return img->get_sps().vui.matrix_coeffs;
816
0
}
817
818
}