/work/libde265/libde265/refpic.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 "refpic.h" |
22 | | #include "decctx.h" |
23 | | #include "util.h" |
24 | | |
25 | | #include <assert.h> |
26 | | #include <stdlib.h> |
27 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
28 | | # include <malloc.h> |
29 | | #elif defined(HAVE_ALLOCA_H) |
30 | | # include <alloca.h> |
31 | | #endif |
32 | | |
33 | | |
34 | | void ref_pic_set::reset() |
35 | 0 | { |
36 | 0 | NumNegativePics = 0; |
37 | 0 | NumPositivePics = 0; |
38 | 0 | NumDeltaPocs = 0; |
39 | 0 | NumPocTotalCurr_shortterm_only = 0; |
40 | |
|
41 | 0 | for (int i=0;i<MAX_NUM_REF_PICS;i++) { |
42 | 0 | DeltaPocS0[i] = 0; |
43 | 0 | DeltaPocS1[i] = 0; |
44 | |
|
45 | 0 | UsedByCurrPicS0[i] = 0; |
46 | 0 | UsedByCurrPicS1[i] = 0; |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | | |
51 | | void ref_pic_set::compute_derived_values() |
52 | 0 | { |
53 | 0 | NumPocTotalCurr_shortterm_only = 0; |
54 | |
|
55 | 0 | for (int i=0; i<NumNegativePics; i++) |
56 | 0 | if (UsedByCurrPicS0[i]) |
57 | 0 | NumPocTotalCurr_shortterm_only++; |
58 | |
|
59 | 0 | for (int i=0; i<NumPositivePics; i++) |
60 | 0 | if (UsedByCurrPicS1[i]) |
61 | 0 | NumPocTotalCurr_shortterm_only++; |
62 | |
|
63 | 0 | NumDeltaPocs = NumNegativePics + NumPositivePics; |
64 | | |
65 | | |
66 | | /* |
67 | | NOTE: this is done when reading the slice header. |
68 | | The value numPocTotalCurr is then stored in the slice header. |
69 | | |
70 | | for (int i = 0; i < num_long_term_sps + num_long_term_pics; i++ ) |
71 | | if( UsedByCurrPicLt[i] ) |
72 | | NumPocTotalCurr++ |
73 | | } |
74 | | */ |
75 | 0 | } |
76 | | |
77 | | |
78 | | /* A ref-pic-set is coded either coded |
79 | | - as a list of the relative POC deltas themselves, or |
80 | | - by shifting an existing ref-pic-set by some number of frames |
81 | | When shifting an existing set, the frame 0 is also shifted as an additional reference frame. |
82 | | When coding the ref-pic-sets in the SPS, prediction is always from the previous set. |
83 | | In the slice header, the ref-pic-set can use any previous set as reference. |
84 | | */ |
85 | | bool read_short_term_ref_pic_set(error_queue* errqueue, |
86 | | const seq_parameter_set* sps, |
87 | | bitreader* br, |
88 | | ref_pic_set* out_set, // where to store the read set |
89 | | int idxRps, // index of the set to be read |
90 | | const std::vector<ref_pic_set>& sets, // previously read sets |
91 | | bool sliceRefPicSet) // is this in the slice header? |
92 | 0 | { |
93 | | // --- is this set coded in prediction mode (not possible for the first set) |
94 | |
|
95 | 0 | char inter_ref_pic_set_prediction_flag; |
96 | |
|
97 | 0 | if (idxRps != 0) { |
98 | 0 | inter_ref_pic_set_prediction_flag = get_bits(br,1); |
99 | 0 | } |
100 | 0 | else { |
101 | 0 | inter_ref_pic_set_prediction_flag = 0; |
102 | 0 | } |
103 | | |
104 | | |
105 | |
|
106 | 0 | if (inter_ref_pic_set_prediction_flag) { |
107 | 0 | int vlc; |
108 | | |
109 | | /* Only for the last ref_pic_set (that's the one coded in the slice header), |
110 | | we can specify relative to which reference set we code the set. */ |
111 | |
|
112 | 0 | int delta_idx; |
113 | 0 | if (sliceRefPicSet) { // idxRps == num_short_term_ref_pic_sets) { |
114 | 0 | delta_idx = vlc = get_uvlc(br); |
115 | 0 | if (delta_idx==UVLC_ERROR) { |
116 | 0 | return false; |
117 | 0 | } |
118 | | |
119 | 0 | if (delta_idx>=idxRps) { |
120 | 0 | return false; |
121 | 0 | } |
122 | | |
123 | 0 | delta_idx++; |
124 | 0 | } else { |
125 | 0 | delta_idx = 1; |
126 | 0 | } |
127 | | |
128 | 0 | int RIdx = idxRps - delta_idx; // this is our source set, which we will modify |
129 | 0 | assert(RIdx>=0); |
130 | | |
131 | 0 | int delta_rps_sign = get_bits(br,1); |
132 | 0 | int abs_delta_rps = vlc = get_uvlc(br); |
133 | 0 | if (vlc==UVLC_ERROR) { return false; } |
134 | 0 | abs_delta_rps++; |
135 | 0 | int DeltaRPS = (delta_rps_sign ? -abs_delta_rps : abs_delta_rps); |
136 | | |
137 | | // bits are stored in this order: |
138 | | // - all bits for negative Pocs (forward), |
139 | | // - then all bits for positive Pocs (forward), |
140 | | // - then bits for '0', shifting of the current picture |
141 | | // in total, these are 'nDeltaPocsRIdx'+1 bits |
142 | |
|
143 | 0 | logtrace(LogHeaders,"predicted from %d with delta %d\n",RIdx,DeltaRPS); |
144 | |
|
145 | 0 | int nDeltaPocsRIdx= sets[RIdx].NumDeltaPocs; // size of source set |
146 | 0 | char *const used_by_curr_pic_flag = (char *)alloca((nDeltaPocsRIdx+1) * sizeof(char)); |
147 | 0 | char *const use_delta_flag = (char *)alloca((nDeltaPocsRIdx+1) * sizeof(char)); |
148 | |
|
149 | 0 | for (int j=0;j<=nDeltaPocsRIdx;j++) { |
150 | 0 | used_by_curr_pic_flag[j] = get_bits(br,1); |
151 | 0 | if (used_by_curr_pic_flag[j]) { |
152 | 0 | use_delta_flag[j] = 1; // if this frame is used, we also have to apply the delta |
153 | 0 | } else { |
154 | 0 | use_delta_flag[j] = get_bits(br,1); // otherwise, it is only optionally included |
155 | 0 | } |
156 | 0 | } |
157 | |
|
158 | 0 | logtrace(LogHeaders,"flags: "); |
159 | 0 | for (int j=0;j<=nDeltaPocsRIdx;j++) { |
160 | 0 | logtrace(LogHeaders,"%d ", use_delta_flag[j]); |
161 | 0 | } |
162 | 0 | logtrace(LogHeaders,"\n"); |
163 | |
|
164 | 0 | int nNegativeRIdx = sets[RIdx].NumNegativePics; |
165 | 0 | int nPositiveRIdx = sets[RIdx].NumPositivePics; |
166 | | |
167 | | // --- update list 0 (negative Poc) --- |
168 | | // Iterate through all Pocs in decreasing value order (positive reverse, 0, negative forward). |
169 | |
|
170 | 0 | int i=0; // target index |
171 | | |
172 | | // positive list |
173 | 0 | for (int j=nPositiveRIdx-1;j>=0;j--) { |
174 | 0 | assert(RIdx >= 0 && RIdx < sets.size()); |
175 | 0 | assert(j>=0 && j < MAX_NUM_REF_PICS); |
176 | | |
177 | 0 | int dPoc = sets[RIdx].DeltaPocS1[j] + DeltaRPS; // new delta |
178 | 0 | if (dPoc<0 && use_delta_flag[nNegativeRIdx+j]) { |
179 | 0 | if (i>= MAX_NUM_REF_PICS) { return false; } |
180 | | |
181 | 0 | out_set->DeltaPocS0[i] = dPoc; |
182 | 0 | out_set->UsedByCurrPicS0[i] = used_by_curr_pic_flag[nNegativeRIdx+j]; |
183 | 0 | i++; |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | // frame 0 |
188 | 0 | if (DeltaRPS<0 && use_delta_flag[nDeltaPocsRIdx]) { |
189 | 0 | if (i>= MAX_NUM_REF_PICS) { return false; } |
190 | | |
191 | 0 | out_set->DeltaPocS0[i] = DeltaRPS; |
192 | 0 | out_set->UsedByCurrPicS0[i] = used_by_curr_pic_flag[nDeltaPocsRIdx]; |
193 | 0 | i++; |
194 | 0 | } |
195 | | |
196 | | // negative list |
197 | 0 | for (int j=0;j<nNegativeRIdx;j++) { |
198 | 0 | int dPoc = sets[RIdx].DeltaPocS0[j] + DeltaRPS; |
199 | 0 | if (dPoc<0 && use_delta_flag[j]) { |
200 | 0 | if (i>= MAX_NUM_REF_PICS) { return false; } |
201 | | |
202 | 0 | out_set->DeltaPocS0[i] = dPoc; |
203 | 0 | out_set->UsedByCurrPicS0[i] = used_by_curr_pic_flag[j]; |
204 | 0 | i++; |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 0 | out_set->NumNegativePics = i; |
209 | | |
210 | | |
211 | | // --- update list 1 (positive Poc) --- |
212 | | // Iterate through all Pocs in increasing value order (negative reverse, 0, positive forward) |
213 | |
|
214 | 0 | i=0; // target index |
215 | | |
216 | | // negative list |
217 | 0 | for (int j=nNegativeRIdx-1;j>=0;j--) { |
218 | 0 | int dPoc = sets[RIdx].DeltaPocS0[j] + DeltaRPS; |
219 | 0 | if (dPoc>0 && use_delta_flag[j]) { |
220 | 0 | if (i>= MAX_NUM_REF_PICS) { return false; } |
221 | | |
222 | 0 | out_set->DeltaPocS1[i] = dPoc; |
223 | 0 | out_set->UsedByCurrPicS1[i] = used_by_curr_pic_flag[j]; |
224 | 0 | i++; |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | | // frame 0 |
229 | 0 | if (DeltaRPS>0 && use_delta_flag[nDeltaPocsRIdx]) { |
230 | 0 | if (i>= MAX_NUM_REF_PICS) { return false; } |
231 | | |
232 | 0 | out_set->DeltaPocS1[i] = DeltaRPS; |
233 | 0 | out_set->UsedByCurrPicS1[i] = used_by_curr_pic_flag[nDeltaPocsRIdx]; |
234 | 0 | i++; |
235 | 0 | } |
236 | | |
237 | | // positive list |
238 | 0 | for (int j=0;j<nPositiveRIdx;j++) { |
239 | 0 | int dPoc = sets[RIdx].DeltaPocS1[j] + DeltaRPS; |
240 | 0 | if (dPoc>0 && use_delta_flag[nNegativeRIdx+j]) { |
241 | 0 | if (i>= MAX_NUM_REF_PICS) { return false; } |
242 | | |
243 | 0 | out_set->DeltaPocS1[i] = dPoc; |
244 | 0 | out_set->UsedByCurrPicS1[i] = used_by_curr_pic_flag[nNegativeRIdx+j]; |
245 | 0 | i++; |
246 | 0 | } |
247 | 0 | } |
248 | | |
249 | 0 | out_set->NumPositivePics = i; |
250 | |
|
251 | 0 | } else { |
252 | | |
253 | | // --- first, read the number of past and future frames in this set --- |
254 | |
|
255 | 0 | int num_negative_pics = get_uvlc(br); |
256 | 0 | int num_positive_pics = get_uvlc(br); |
257 | |
|
258 | 0 | if (num_negative_pics == UVLC_ERROR || |
259 | 0 | num_positive_pics == UVLC_ERROR) { |
260 | | // invalid num-ref-pics value |
261 | 0 | errqueue->add_warning(DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED, false); |
262 | 0 | return false; |
263 | 0 | } |
264 | | |
265 | | // total number of reference pictures may not exceed buffer capacity |
266 | 0 | if (num_negative_pics + num_positive_pics > |
267 | 0 | sps->sps_max_dec_pic_buffering[ sps->sps_max_sub_layers-1 ]) { |
268 | |
|
269 | 0 | out_set->NumNegativePics = 0; |
270 | 0 | out_set->NumPositivePics = 0; |
271 | 0 | out_set->NumDeltaPocs = 0; |
272 | 0 | out_set->NumPocTotalCurr_shortterm_only = 0; |
273 | |
|
274 | 0 | errqueue->add_warning(DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED, false); |
275 | 0 | return false; |
276 | 0 | } |
277 | | |
278 | 0 | if (num_negative_pics > MAX_NUM_REF_PICS || |
279 | 0 | num_positive_pics > MAX_NUM_REF_PICS) { |
280 | 0 | errqueue->add_warning(DE265_WARNING_MAX_NUM_REF_PICS_EXCEEDED, false); |
281 | 0 | return false; |
282 | 0 | } |
283 | | |
284 | 0 | out_set->NumNegativePics = num_negative_pics; |
285 | 0 | out_set->NumPositivePics = num_positive_pics; |
286 | | |
287 | | // --- now, read the deltas between the reference frames to fill the lists --- |
288 | | |
289 | | // past frames |
290 | |
|
291 | 0 | int lastPocS=0; |
292 | 0 | for (int i=0;i<num_negative_pics;i++) { |
293 | 0 | int delta_poc_s0 = get_uvlc(br); |
294 | 0 | if (delta_poc_s0==UVLC_ERROR) { return false; } |
295 | 0 | delta_poc_s0++; |
296 | 0 | char used_by_curr_pic_s0_flag = get_bits(br,1); |
297 | |
|
298 | 0 | out_set->DeltaPocS0[i] = lastPocS - delta_poc_s0; |
299 | 0 | out_set->UsedByCurrPicS0[i] = used_by_curr_pic_s0_flag; |
300 | 0 | lastPocS = out_set->DeltaPocS0[i]; |
301 | 0 | } |
302 | | |
303 | | // future frames |
304 | | |
305 | 0 | lastPocS=0; |
306 | 0 | for (int i=0;i<num_positive_pics;i++) { |
307 | 0 | int delta_poc_s1 = get_uvlc(br); |
308 | 0 | if (delta_poc_s1==UVLC_ERROR) { return false; } |
309 | 0 | delta_poc_s1++; |
310 | 0 | char used_by_curr_pic_s1_flag = get_bits(br,1); |
311 | |
|
312 | 0 | out_set->DeltaPocS1[i] = lastPocS + delta_poc_s1; |
313 | 0 | out_set->UsedByCurrPicS1[i] = used_by_curr_pic_s1_flag; |
314 | 0 | lastPocS = out_set->DeltaPocS1[i]; |
315 | 0 | } |
316 | 0 | } |
317 | | |
318 | | |
319 | 0 | out_set->compute_derived_values(); |
320 | |
|
321 | 0 | return true; |
322 | 0 | } |
323 | | |
324 | | |
325 | | bool write_short_term_ref_pic_set_nopred(error_queue* errqueue, |
326 | | const seq_parameter_set* sps, |
327 | | CABAC_encoder& out, |
328 | | const ref_pic_set* in_set, // which set to write |
329 | | int idxRps, // index of the set to be written |
330 | | const std::vector<ref_pic_set>& sets, // previously read sets |
331 | | bool sliceRefPicSet) // is this in the slice header? |
332 | 0 | { |
333 | 0 | if (idxRps != 0) { |
334 | | // inter_ref_pic_set_prediction_flag |
335 | 0 | out.write_bit(0); |
336 | 0 | } |
337 | | |
338 | | |
339 | | // --- first, write the number of past and future frames in this set --- |
340 | |
|
341 | 0 | out.write_uvlc(in_set->NumNegativePics); |
342 | 0 | out.write_uvlc(in_set->NumPositivePics); |
343 | | |
344 | | // --- now, write the deltas between the reference frames to fill the lists --- |
345 | | |
346 | | // past frames |
347 | |
|
348 | 0 | int lastPocS=0; |
349 | 0 | for (int i=0;i<in_set->NumNegativePics;i++) { |
350 | 0 | int delta_poc_s0 = lastPocS - in_set->DeltaPocS0[i]; |
351 | 0 | char used_by_curr_pic_s0_flag = in_set->UsedByCurrPicS0[i]; |
352 | |
|
353 | 0 | assert(delta_poc_s0 >= 1); |
354 | 0 | out.write_uvlc(delta_poc_s0-1); |
355 | 0 | out.write_bit(used_by_curr_pic_s0_flag); |
356 | 0 | lastPocS = in_set->DeltaPocS0[i]; |
357 | 0 | } |
358 | | |
359 | | // future frames |
360 | | |
361 | 0 | lastPocS=0; |
362 | 0 | for (int i=0;i<in_set->NumPositivePics;i++) { |
363 | 0 | int delta_poc_s1 = in_set->DeltaPocS1[i] - lastPocS; |
364 | 0 | char used_by_curr_pic_s1_flag = in_set->UsedByCurrPicS1[i]; |
365 | |
|
366 | 0 | assert(delta_poc_s1 >= 1); |
367 | 0 | out.write_uvlc(delta_poc_s1-1); |
368 | 0 | out.write_bit(used_by_curr_pic_s1_flag); |
369 | 0 | lastPocS = in_set->DeltaPocS1[i]; |
370 | 0 | } |
371 | | |
372 | 0 | return true; |
373 | 0 | } |
374 | | |
375 | | |
376 | | bool write_short_term_ref_pic_set(error_queue* errqueue, |
377 | | const seq_parameter_set* sps, |
378 | | CABAC_encoder& out, |
379 | | const ref_pic_set* in_set, // which set to write |
380 | | int idxRps, // index of the set to be read |
381 | | const std::vector<ref_pic_set>& sets, // previously read sets |
382 | | bool sliceRefPicSet) // is this in the slice header? |
383 | 0 | { |
384 | 0 | return write_short_term_ref_pic_set_nopred(errqueue, sps, out, in_set, idxRps, sets, |
385 | 0 | sliceRefPicSet); |
386 | 0 | } |
387 | | |
388 | | |
389 | | void dump_short_term_ref_pic_set(const ref_pic_set* set, FILE* fh) |
390 | 0 | { |
391 | 0 | log2fh(fh,"NumDeltaPocs: %d [-:%d +:%d]\n", set->NumDeltaPocs, |
392 | 0 | set->NumNegativePics, set->NumPositivePics); |
393 | |
|
394 | 0 | log2fh(fh,"DeltaPocS0:"); |
395 | 0 | for (int i=0;i<set->NumNegativePics;i++) { |
396 | 0 | if (i) { log2fh(fh,","); } |
397 | 0 | log2fh(fh," %d/%d",set->DeltaPocS0[i],set->UsedByCurrPicS0[i]); |
398 | 0 | } |
399 | 0 | log2fh(fh,"\n"); |
400 | |
|
401 | 0 | log2fh(fh,"DeltaPocS1:"); |
402 | 0 | for (int i=0;i<set->NumPositivePics;i++) { |
403 | 0 | if (i) { log2fh(fh,","); } |
404 | 0 | log2fh(fh," %d/%d",set->DeltaPocS1[i],set->UsedByCurrPicS1[i]); |
405 | 0 | } |
406 | 0 | log2fh(fh,"\n"); |
407 | 0 | } |
408 | | |
409 | | |
410 | | void dump_compact_short_term_ref_pic_set(const ref_pic_set* set, int range, FILE* fh) |
411 | 0 | { |
412 | 0 | char *const log = (char *)alloca((range+1+range+1) * sizeof(char)); |
413 | 0 | log[2*range+1] = 0; |
414 | 0 | for (int i=0;i<2*range+1;i++) log[i]='.'; |
415 | 0 | log[range]='|'; |
416 | |
|
417 | 0 | for (int i=set->NumNegativePics-1;i>=0;i--) { |
418 | 0 | int n = set->DeltaPocS0[i]; |
419 | 0 | if (n>=-range && n<=range) { |
420 | 0 | if (set->UsedByCurrPicS0[i]) log[n+range] = 'X'; |
421 | 0 | else log[n+range] = 'o'; |
422 | 0 | } else { log2fh(fh,"*%d%c ",n, set->UsedByCurrPicS0[i] ? 'X':'o'); } |
423 | 0 | } |
424 | |
|
425 | 0 | for (int i=set->NumPositivePics-1;i>=0;i--) { |
426 | 0 | int n = set->DeltaPocS1[i]; |
427 | 0 | if (n>=-range && n<=range) { |
428 | 0 | if (set->UsedByCurrPicS1[i]) log[n+range] = 'X'; |
429 | 0 | else log[n+range] = 'o'; |
430 | 0 | } else { log2fh(fh,"*%d%c ",n, set->UsedByCurrPicS1[i] ? 'X':'o'); } |
431 | 0 | } |
432 | |
|
433 | 0 | log2fh(fh,"*%s\n",log); |
434 | 0 | } |