Coverage Report

Created: 2026-07-16 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/dpb.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
#include "dpb.h"
22
#include "decctx.h"
23
#include <string.h>
24
#include <assert.h>
25
26
27
decoded_picture_buffer::~decoded_picture_buffer()
28
1.43k
{
29
1.43k
  for (size_t i=0;i<dpb.size();i++)
30
0
    delete dpb[i];
31
1.43k
}
32
33
34
void decoded_picture_buffer::log_dpb_content() const
35
0
{
36
0
  for (size_t i=0;i<dpb.size();i++) {
37
0
    loginfo(LogHighlevel, " DPB %d: POC=%d, ID=%d %s %s\n", i,
38
0
            dpb[i]->PicOrderCntVal,
39
0
            dpb[i]->get_ID(),
40
0
            dpb[i]->PicState == UnusedForReference ? "unused" :
41
0
            dpb[i]->PicState == UsedForShortTermReference ? "short-term" : "long-term",
42
0
            dpb[i]->PicOutputFlag ? "output" : "---");
43
0
  }
44
0
}
45
46
47
bool decoded_picture_buffer::has_free_dpb_picture(bool high_priority) const
48
2.58k
{
49
  // we will always adapt the buffer to insert high-priority images
50
2.58k
  if (high_priority) return true;
51
52
  // quick test to check for free slots
53
2.58k
  if (dpb.size() < max_images_in_DPB) return true;
54
55
  // scan for empty slots
56
0
  for (size_t i=0;i<dpb.size();i++) {
57
0
    if (dpb[i]->PicOutputFlag==false && dpb[i]->PicState == UnusedForReference) {
58
0
      return true;
59
0
    }
60
0
  }
61
62
0
  return false;
63
0
}
64
65
66
int decoded_picture_buffer::DPB_index_of_picture_with_POC(int poc, uint32_t currentID, bool preferLongTerm) const
67
0
{
68
0
  logdebug(LogHeaders,"DPB_index_of_picture_with_POC POC=%d\n",poc);
69
70
  //log_dpb_content(ctx);
71
  //loginfo(LogDPB,"searching for short-term reference POC=%d\n",poc);
72
73
0
  if (preferLongTerm) {
74
0
    for (size_t k=0;k<dpb.size();k++) {
75
0
      if (dpb[k]->PicOrderCntVal == poc &&
76
0
          dpb[k]->removed_at_picture_id > currentID &&
77
0
          dpb[k]->PicState == UsedForLongTermReference) {
78
0
        return k;
79
0
      }
80
0
    }
81
0
  }
82
83
0
  for (size_t k=0;k<dpb.size();k++) {
84
0
    if (dpb[k]->PicOrderCntVal == poc &&
85
0
        dpb[k]->removed_at_picture_id > currentID &&
86
0
        dpb[k]->PicState != UnusedForReference) {
87
0
      return k;
88
0
    }
89
0
  }
90
91
0
  return -1;
92
0
}
93
94
95
int decoded_picture_buffer::DPB_index_of_picture_with_LSB(int lsb, uint32_t currentID, bool preferLongTerm) const
96
0
{
97
0
  logdebug(LogHeaders,"get access to picture with LSB %d from DPB\n",lsb);
98
99
0
  if (preferLongTerm) {
100
0
    for (size_t k=0;k<dpb.size();k++) {
101
0
      if (dpb[k]->picture_order_cnt_lsb == lsb &&
102
0
          dpb[k]->removed_at_picture_id > currentID &&
103
0
          dpb[k]->PicState == UsedForLongTermReference) {
104
0
        return k;
105
0
      }
106
0
    }
107
0
  }
108
109
0
  for (size_t k=0;k<dpb.size();k++) {
110
0
    if (dpb[k]->picture_order_cnt_lsb == lsb &&
111
0
        dpb[k]->removed_at_picture_id > currentID &&
112
0
        dpb[k]->PicState != UnusedForReference) {
113
0
      return k;
114
0
    }
115
0
  }
116
117
0
  return -1;
118
0
}
119
120
121
int decoded_picture_buffer::DPB_index_of_picture_with_ID(uint32_t id) const
122
0
{
123
0
  logdebug(LogHeaders,"get access to picture with ID %d from DPB\n",id);
124
125
0
  for (size_t k=0;k<dpb.size();k++) {
126
0
    if (dpb[k]->get_ID() == id) {
127
0
      return k;
128
0
    }
129
0
  }
130
131
0
  return -1;
132
0
}
133
134
135
void decoded_picture_buffer::output_next_picture_in_reorder_buffer()
136
0
{
137
0
  assert(!reorder_output_queue.empty());
138
139
  // search for picture in reorder buffer with minimum POC
140
141
0
  int minPOC = reorder_output_queue[0]->PicOrderCntVal;
142
0
  int minIdx = 0;
143
0
  for (size_t i=1;i<reorder_output_queue.size();i++)
144
0
    {
145
0
      if (reorder_output_queue[i]->PicOrderCntVal < minPOC) {
146
0
        minPOC = reorder_output_queue[i]->PicOrderCntVal;
147
0
        minIdx = i;
148
0
      }
149
0
    }
150
151
152
  // put image into output queue
153
154
0
  image_output_queue.push_back(reorder_output_queue[minIdx]);
155
156
157
  // remove image from reorder buffer
158
159
0
  reorder_output_queue[minIdx] = reorder_output_queue.back();
160
0
  reorder_output_queue.pop_back();
161
0
}
162
163
164
bool decoded_picture_buffer::flush_reorder_buffer()
165
44.8k
{
166
  // return 'false' when there are no pictures in reorder buffer
167
44.8k
  if (reorder_output_queue.empty()) return false;
168
169
0
  while (!reorder_output_queue.empty()) {
170
0
    output_next_picture_in_reorder_buffer();
171
0
  }
172
173
0
  return true;
174
44.8k
}
175
176
177
void decoded_picture_buffer::clear()
178
0
{
179
0
  for (size_t i=0;i<dpb.size();i++) {
180
0
    if (dpb[i]->PicOutputFlag ||
181
0
        dpb[i]->PicState != UnusedForReference)
182
0
      {
183
0
        dpb[i]->PicOutputFlag = false;
184
0
        dpb[i]->PicState = UnusedForReference;
185
0
        dpb[i]->release();
186
0
      }
187
0
  }
188
189
0
  reorder_output_queue.clear();
190
0
  image_output_queue.clear();
191
0
}
192
193
194
int decoded_picture_buffer::new_image(std::shared_ptr<const seq_parameter_set> sps,
195
                                      decoder_context* decctx,
196
                                      de265_PTS pts, void* user_data, bool isOutputImage)
197
0
{
198
0
  loginfo(LogHeaders,"DPB::new_image\n");
199
0
  log_dpb_content();
200
201
  // --- search for a free slot in the DPB ---
202
203
0
  uint8_t free_image_buffer_idx = 0;
204
0
  uint8_t err = DE265_ERROR_IMAGE_BUFFER_FULL;
205
206
0
  for (size_t i=0;i<dpb.size();i++) {
207
0
    if (dpb[i]->can_be_released()) {
208
0
      dpb[i]->release(); /* TODO: this is surely not the best place to free the image, but
209
                            we have to do it here because releasing it in de265_release_image()
210
                            would break the API compatibility. */
211
212
0
      free_image_buffer_idx = i;
213
0
      err = DE265_OK;
214
0
      break;
215
0
    }
216
0
  }
217
218
219
  // Try to free a buffer at the end if the DPB got too large.
220
  /* This should also probably move to a better place as soon as the API allows for this. */
221
222
0
  if (dpb.size() > norm_images_in_DPB &&           // buffer too large
223
0
      free_image_buffer_idx != dpb.size()-1 &&     // last slot not reused in this alloc
224
0
      dpb.back()->can_be_released())               // last slot is free
225
0
    {
226
0
      delete dpb.back();
227
0
      dpb.pop_back();
228
0
    }
229
230
231
  // create a new image slot if no empty slot remaining
232
233
0
  if (err == DE265_ERROR_IMAGE_BUFFER_FULL) {
234
0
    size_t dpb_size = dpb.size();
235
0
    assert(dpb_size < 255);
236
237
0
    free_image_buffer_idx = static_cast<uint8_t>(dpb_size);
238
0
    dpb.push_back(new de265_image);
239
0
    err = DE265_OK;
240
0
  }
241
242
243
  // --- allocate new image ---
244
245
0
  if (err) {
246
0
    return -err;
247
0
  }
248
249
0
  de265_image* img = dpb[free_image_buffer_idx];
250
251
0
  int w = sps->pic_width_in_luma_samples;
252
0
  int h = sps->pic_height_in_luma_samples;
253
254
  // --- enforce maximum image size before allocating the image buffer ---
255
256
0
  uint32_t max_image_size_pixels = decctx->param_security_limits.max_image_size_pixels;
257
0
  if (max_image_size_pixels != 0 &&
258
0
      (uint64_t)w * h > max_image_size_pixels) {
259
0
    return -DE265_ERROR_IMAGE_SIZE_EXCEEDS_SECURITY_LIMIT;
260
0
  }
261
262
0
  enum de265_chroma chroma;
263
0
  switch (sps->chroma_format_idc) {
264
0
  case 0: chroma = de265_chroma_mono; break;
265
0
  case 1: chroma = de265_chroma_420;  break;
266
0
  case 2: chroma = de265_chroma_422;  break;
267
0
  case 3: chroma = de265_chroma_444;  break;
268
0
  default: chroma = de265_chroma_420; assert(0); break; // should never happen
269
0
  }
270
271
0
  de265_error error = img->alloc_image(w,h, chroma, sps, true, decctx, /*nullptr,*/ pts, user_data, isOutputImage);
272
0
  if (error) {
273
0
    return -error;
274
0
  }
275
276
0
  img->integrity = INTEGRITY_CORRECT;
277
278
0
  return free_image_buffer_idx;
279
0
}
280
281
282
void decoded_picture_buffer::pop_next_picture_in_output_queue()
283
0
{
284
0
  image_output_queue.pop_front();
285
286
287
0
  loginfo(LogDPB, "DPB output queue: ");
288
0
  for (size_t i=0;i<image_output_queue.size();i++) {
289
0
    loginfo(LogDPB, "*%d ", image_output_queue[i]->PicOrderCntVal);
290
0
  }
291
0
  loginfo(LogDPB,"*\n");
292
0
}
293
294
295
void decoded_picture_buffer::log_dpb_queues() const
296
0
{
297
0
    loginfo(LogDPB, "DPB reorder queue (after push): ");
298
0
    for (int i=0;i<num_pictures_in_reorder_buffer();i++) {
299
0
      loginfo(LogDPB, "*%d ", reorder_output_queue[i]->PicOrderCntVal);
300
0
    }
301
0
    loginfo(LogDPB,"*\n");
302
303
0
    loginfo(LogDPB, "DPB output queue (after push): ");
304
0
    for (int i=0;i<num_pictures_in_output_queue();i++) {
305
0
      loginfo(LogDPB, "*%d ", image_output_queue[i]->PicOrderCntVal);
306
0
    }
307
0
    loginfo(LogDPB,"*\n");
308
0
}