/src/vlc/contrib/contrib-build/bpg/libavcodec/hevc_refs.c
Line | Count | Source |
1 | | /* |
2 | | * HEVC video decoder |
3 | | * |
4 | | * Copyright (C) 2012 - 2013 Guillaume Martres |
5 | | * Copyright (C) 2012 - 2013 Gildas Cocherel |
6 | | * |
7 | | * This file is part of FFmpeg. |
8 | | * |
9 | | * FFmpeg is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public |
11 | | * License as published by the Free Software Foundation; either |
12 | | * version 2.1 of the License, or (at your option) any later version. |
13 | | * |
14 | | * FFmpeg is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public |
20 | | * License along with FFmpeg; if not, write to the Free Software |
21 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | | */ |
23 | | |
24 | | #include "libavutil/pixdesc.h" |
25 | | |
26 | | #include "internal.h" |
27 | | #include "thread.h" |
28 | | #include "hevc.h" |
29 | | |
30 | | void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags) |
31 | 0 | { |
32 | | /* frame->frame can be NULL if context init failed */ |
33 | 0 | if (!frame->frame || !frame->frame->buf[0]) |
34 | 0 | return; |
35 | | |
36 | 0 | frame->flags &= ~flags; |
37 | 0 | if (!frame->flags) { |
38 | 0 | ff_thread_release_buffer(s->avctx, &frame->tf); |
39 | |
|
40 | 0 | #ifdef USE_PRED |
41 | 0 | av_buffer_unref(&frame->tab_mvf_buf); |
42 | 0 | frame->tab_mvf = NULL; |
43 | 0 | av_buffer_unref(&frame->rpl_buf); |
44 | 0 | av_buffer_unref(&frame->rpl_tab_buf); |
45 | 0 | frame->rpl_tab = NULL; |
46 | 0 | frame->refPicList = NULL; |
47 | 0 | #endif |
48 | |
|
49 | 0 | frame->collocated_ref = NULL; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | #ifdef USE_PRED |
54 | | RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *ref, int x0, int y0) |
55 | 0 | { |
56 | 0 | int x_cb = x0 >> s->sps->log2_ctb_size; |
57 | 0 | int y_cb = y0 >> s->sps->log2_ctb_size; |
58 | 0 | int pic_width_cb = s->sps->ctb_width; |
59 | 0 | int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb]; |
60 | 0 | return (RefPicList *)ref->rpl_tab[ctb_addr_ts]; |
61 | 0 | } |
62 | | #endif |
63 | | |
64 | | void ff_hevc_clear_refs(HEVCContext *s) |
65 | 0 | { |
66 | 0 | int i; |
67 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) |
68 | 0 | ff_hevc_unref_frame(s, &s->DPB[i], |
69 | 0 | HEVC_FRAME_FLAG_SHORT_REF | |
70 | 0 | HEVC_FRAME_FLAG_LONG_REF); |
71 | 0 | } |
72 | | |
73 | | void ff_hevc_flush_dpb(HEVCContext *s) |
74 | 0 | { |
75 | 0 | int i; |
76 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) |
77 | 0 | ff_hevc_unref_frame(s, &s->DPB[i], ~0); |
78 | 0 | } |
79 | | |
80 | | static HEVCFrame *alloc_frame(HEVCContext *s) |
81 | 0 | { |
82 | 0 | int i, j, ret; |
83 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
84 | 0 | HEVCFrame *frame = &s->DPB[i]; |
85 | 0 | if (frame->frame->buf[0]) |
86 | 0 | continue; |
87 | | |
88 | 0 | ret = ff_thread_get_buffer(s->avctx, &frame->tf, |
89 | 0 | AV_GET_BUFFER_FLAG_REF); |
90 | 0 | if (ret < 0) |
91 | 0 | return NULL; |
92 | | |
93 | 0 | frame->ctb_count = s->sps->ctb_width * s->sps->ctb_height; |
94 | 0 | #ifdef USE_PRED |
95 | 0 | frame->rpl_buf = av_buffer_allocz(s->nb_nals * sizeof(RefPicListTab)); |
96 | 0 | if (!frame->rpl_buf) |
97 | 0 | goto fail; |
98 | | |
99 | 0 | frame->tab_mvf_buf = av_buffer_pool_get(s->tab_mvf_pool); |
100 | 0 | if (!frame->tab_mvf_buf) |
101 | 0 | goto fail; |
102 | 0 | frame->tab_mvf = (MvField *)frame->tab_mvf_buf->data; |
103 | |
|
104 | 0 | frame->rpl_tab_buf = av_buffer_pool_get(s->rpl_tab_pool); |
105 | 0 | if (!frame->rpl_tab_buf) |
106 | 0 | goto fail; |
107 | 0 | frame->rpl_tab = (RefPicListTab **)frame->rpl_tab_buf->data; |
108 | 0 | for (j = 0; j < frame->ctb_count; j++) |
109 | 0 | frame->rpl_tab[j] = (RefPicListTab *)frame->rpl_buf->data; |
110 | 0 | #endif |
111 | |
|
112 | 0 | frame->frame->top_field_first = s->picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD; |
113 | 0 | frame->frame->interlaced_frame = (s->picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) || (s->picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD); |
114 | 0 | return frame; |
115 | 0 | fail: |
116 | 0 | ff_hevc_unref_frame(s, frame, ~0); |
117 | 0 | return NULL; |
118 | 0 | } |
119 | 0 | av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n"); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | | |
123 | | int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc) |
124 | 0 | { |
125 | 0 | HEVCFrame *ref; |
126 | 0 | int i; |
127 | | |
128 | | /* check that this POC doesn't already exist */ |
129 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
130 | 0 | HEVCFrame *frame = &s->DPB[i]; |
131 | |
|
132 | 0 | if (frame->frame->buf[0] && frame->sequence == s->seq_decode && |
133 | 0 | frame->poc == poc) { |
134 | 0 | av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n", |
135 | 0 | poc); |
136 | 0 | return AVERROR_INVALIDDATA; |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | 0 | ref = alloc_frame(s); |
141 | 0 | if (!ref) |
142 | 0 | return AVERROR(ENOMEM); |
143 | | |
144 | 0 | *frame = ref->frame; |
145 | 0 | s->ref = ref; |
146 | |
|
147 | 0 | if (s->sh.pic_output_flag) |
148 | 0 | ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF; |
149 | 0 | else |
150 | 0 | ref->flags = HEVC_FRAME_FLAG_SHORT_REF; |
151 | |
|
152 | 0 | ref->poc = poc; |
153 | 0 | ref->sequence = s->seq_decode; |
154 | 0 | ref->window = s->sps->output_window; |
155 | |
|
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | | int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush) |
160 | 0 | { |
161 | 0 | do { |
162 | 0 | int nb_output = 0; |
163 | 0 | int min_poc = INT_MAX; |
164 | 0 | int i, min_idx, ret; |
165 | |
|
166 | 0 | if (s->sh.no_output_of_prior_pics_flag == 1) { |
167 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
168 | 0 | HEVCFrame *frame = &s->DPB[i]; |
169 | 0 | if (!(frame->flags & HEVC_FRAME_FLAG_BUMPING) && frame->poc != s->poc && |
170 | 0 | frame->sequence == s->seq_output) { |
171 | 0 | ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT); |
172 | 0 | } |
173 | 0 | } |
174 | 0 | } |
175 | |
|
176 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
177 | 0 | HEVCFrame *frame = &s->DPB[i]; |
178 | 0 | if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) && |
179 | 0 | frame->sequence == s->seq_output) { |
180 | 0 | nb_output++; |
181 | 0 | if (frame->poc < min_poc) { |
182 | 0 | min_poc = frame->poc; |
183 | 0 | min_idx = i; |
184 | 0 | } |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | /* wait for more frames before output */ |
189 | 0 | if (!flush && s->seq_output == s->seq_decode && s->sps && |
190 | 0 | nb_output <= s->sps->temporal_layer[s->sps->max_sub_layers - 1].num_reorder_pics) |
191 | 0 | return 0; |
192 | | |
193 | 0 | if (nb_output) { |
194 | 0 | HEVCFrame *frame = &s->DPB[min_idx]; |
195 | 0 | AVFrame *src = frame->frame; |
196 | |
|
197 | 0 | ret = av_frame_ref(out, src); |
198 | 0 | if (frame->flags & HEVC_FRAME_FLAG_BUMPING) |
199 | 0 | ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_BUMPING); |
200 | 0 | else |
201 | 0 | ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT); |
202 | 0 | if (ret < 0) |
203 | 0 | return ret; |
204 | | |
205 | | #ifndef USE_MSPS |
206 | | { |
207 | | AVFrame *dst = out; |
208 | | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format); |
209 | | int pixel_shift = !!(desc->comp[0].depth_minus1 > 7); |
210 | | for (i = 0; i < 3; i++) { |
211 | | int hshift = (i > 0) ? desc->log2_chroma_w : 0; |
212 | | int vshift = (i > 0) ? desc->log2_chroma_h : 0; |
213 | | int off = ((frame->window.left_offset >> hshift) << pixel_shift) + |
214 | | (frame->window.top_offset >> vshift) * dst->linesize[i]; |
215 | | dst->data[i] += off; |
216 | | } |
217 | | av_log(s->avctx, AV_LOG_DEBUG, |
218 | | "Output frame with POC %d.\n", frame->poc); |
219 | | } |
220 | | #endif |
221 | 0 | return 1; |
222 | 0 | } |
223 | | |
224 | 0 | if (s->seq_output != s->seq_decode) |
225 | 0 | s->seq_output = (s->seq_output + 1) & 0xff; |
226 | 0 | else |
227 | 0 | break; |
228 | 0 | } while (1); |
229 | | |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | | #ifdef USE_PRED |
234 | | void ff_hevc_bump_frame(HEVCContext *s) |
235 | 0 | { |
236 | 0 | int dpb = 0; |
237 | 0 | int min_poc = INT_MAX; |
238 | 0 | int i; |
239 | |
|
240 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
241 | 0 | HEVCFrame *frame = &s->DPB[i]; |
242 | 0 | if ((frame->flags) && |
243 | 0 | frame->sequence == s->seq_output && |
244 | 0 | frame->poc != s->poc) { |
245 | 0 | dpb++; |
246 | 0 | } |
247 | 0 | } |
248 | |
|
249 | 0 | if (s->sps && dpb >= s->sps->temporal_layer[s->sps->max_sub_layers - 1].max_dec_pic_buffering) { |
250 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
251 | 0 | HEVCFrame *frame = &s->DPB[i]; |
252 | 0 | if ((frame->flags) && |
253 | 0 | frame->sequence == s->seq_output && |
254 | 0 | frame->poc != s->poc) { |
255 | 0 | if (frame->flags == HEVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) { |
256 | 0 | min_poc = frame->poc; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | |
|
261 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
262 | 0 | HEVCFrame *frame = &s->DPB[i]; |
263 | 0 | if (frame->flags & HEVC_FRAME_FLAG_OUTPUT && |
264 | 0 | frame->sequence == s->seq_output && |
265 | 0 | frame->poc <= min_poc) { |
266 | 0 | frame->flags |= HEVC_FRAME_FLAG_BUMPING; |
267 | 0 | } |
268 | 0 | } |
269 | |
|
270 | 0 | dpb--; |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | static int init_slice_rpl(HEVCContext *s) |
275 | 0 | { |
276 | 0 | HEVCFrame *frame = s->ref; |
277 | 0 | int ctb_count = frame->ctb_count; |
278 | 0 | int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr]; |
279 | 0 | int i; |
280 | |
|
281 | 0 | if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab)) |
282 | 0 | return AVERROR_INVALIDDATA; |
283 | | |
284 | 0 | for (i = ctb_addr_ts; i < ctb_count; i++) |
285 | 0 | frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx; |
286 | |
|
287 | 0 | frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts]; |
288 | |
|
289 | 0 | return 0; |
290 | 0 | } |
291 | | |
292 | | int ff_hevc_slice_rpl(HEVCContext *s) |
293 | 0 | { |
294 | 0 | SliceHeader *sh = &s->sh; |
295 | |
|
296 | 0 | uint8_t nb_list = sh->slice_type == B_SLICE ? 2 : 1; |
297 | 0 | uint8_t list_idx; |
298 | 0 | int i, j, ret; |
299 | |
|
300 | 0 | ret = init_slice_rpl(s); |
301 | 0 | if (ret < 0) |
302 | 0 | return ret; |
303 | | |
304 | 0 | if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs + |
305 | 0 | s->rps[LT_CURR].nb_refs)) { |
306 | 0 | av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n"); |
307 | 0 | return AVERROR_INVALIDDATA; |
308 | 0 | } |
309 | | |
310 | 0 | for (list_idx = 0; list_idx < nb_list; list_idx++) { |
311 | 0 | RefPicList rpl_tmp = { { 0 } }; |
312 | 0 | RefPicList *rpl = &s->ref->refPicList[list_idx]; |
313 | | |
314 | | /* The order of the elements is |
315 | | * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and |
316 | | * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */ |
317 | 0 | int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF, |
318 | 0 | list_idx ? ST_CURR_BEF : ST_CURR_AFT, |
319 | 0 | LT_CURR }; |
320 | | |
321 | | /* concatenate the candidate lists for the current frame */ |
322 | 0 | while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) { |
323 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) { |
324 | 0 | RefPicList *rps = &s->rps[cand_lists[i]]; |
325 | 0 | for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < MAX_REFS; j++) { |
326 | 0 | rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j]; |
327 | 0 | rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j]; |
328 | 0 | rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2; |
329 | 0 | rpl_tmp.nb_refs++; |
330 | 0 | } |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | | /* reorder the references if necessary */ |
335 | 0 | if (sh->rpl_modification_flag[list_idx]) { |
336 | 0 | for (i = 0; i < sh->nb_refs[list_idx]; i++) { |
337 | 0 | int idx = sh->list_entry_lx[list_idx][i]; |
338 | |
|
339 | 0 | if (idx >= rpl_tmp.nb_refs) { |
340 | 0 | av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n"); |
341 | 0 | return AVERROR_INVALIDDATA; |
342 | 0 | } |
343 | | |
344 | 0 | rpl->list[i] = rpl_tmp.list[idx]; |
345 | 0 | rpl->ref[i] = rpl_tmp.ref[idx]; |
346 | 0 | rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx]; |
347 | 0 | rpl->nb_refs++; |
348 | 0 | } |
349 | 0 | } else { |
350 | 0 | memcpy(rpl, &rpl_tmp, sizeof(*rpl)); |
351 | 0 | rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]); |
352 | 0 | } |
353 | | |
354 | 0 | if (sh->collocated_list == list_idx && |
355 | 0 | sh->collocated_ref_idx < rpl->nb_refs) |
356 | 0 | s->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx]; |
357 | 0 | } |
358 | | |
359 | 0 | return 0; |
360 | 0 | } |
361 | | |
362 | | static HEVCFrame *find_ref_idx(HEVCContext *s, int poc) |
363 | 0 | { |
364 | 0 | int i; |
365 | 0 | int LtMask = (1 << s->sps->log2_max_poc_lsb) - 1; |
366 | |
|
367 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
368 | 0 | HEVCFrame *ref = &s->DPB[i]; |
369 | 0 | if (ref->frame->buf[0] && (ref->sequence == s->seq_decode)) { |
370 | 0 | if ((ref->poc & LtMask) == poc) |
371 | 0 | return ref; |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
376 | 0 | HEVCFrame *ref = &s->DPB[i]; |
377 | 0 | if (ref->frame->buf[0] && ref->sequence == s->seq_decode) { |
378 | 0 | if (ref->poc == poc || (ref->poc & LtMask) == poc) |
379 | 0 | return ref; |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | 0 | av_log(s->avctx, AV_LOG_ERROR, |
384 | 0 | "Could not find ref with POC %d\n", poc); |
385 | 0 | return NULL; |
386 | 0 | } |
387 | | |
388 | | static void mark_ref(HEVCFrame *frame, int flag) |
389 | 0 | { |
390 | 0 | frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF); |
391 | 0 | frame->flags |= flag; |
392 | 0 | } |
393 | | |
394 | | static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc) |
395 | 0 | { |
396 | 0 | HEVCFrame *frame; |
397 | 0 | int i, x, y; |
398 | |
|
399 | 0 | frame = alloc_frame(s); |
400 | 0 | if (!frame) |
401 | 0 | return NULL; |
402 | | |
403 | 0 | if (!s->sps->pixel_shift) { |
404 | 0 | for (i = 0; frame->frame->buf[i]; i++) |
405 | 0 | memset(frame->frame->buf[i]->data, 1 << (s->sps->bit_depth - 1), |
406 | 0 | frame->frame->buf[i]->size); |
407 | 0 | } else { |
408 | 0 | for (i = 0; frame->frame->data[i]; i++) |
409 | 0 | for (y = 0; y < (s->sps->height >> s->sps->vshift[i]); y++) |
410 | 0 | for (x = 0; x < (s->sps->width >> s->sps->hshift[i]); x++) { |
411 | 0 | AV_WN16(frame->frame->data[i] + y * frame->frame->linesize[i] + 2 * x, |
412 | 0 | 1 << (s->sps->bit_depth - 1)); |
413 | 0 | } |
414 | 0 | } |
415 | |
|
416 | 0 | frame->poc = poc; |
417 | 0 | frame->sequence = s->seq_decode; |
418 | 0 | frame->flags = 0; |
419 | |
|
420 | 0 | if (s->threads_type == FF_THREAD_FRAME) |
421 | 0 | ff_thread_report_progress(&frame->tf, INT_MAX, 0); |
422 | |
|
423 | 0 | return frame; |
424 | 0 | } |
425 | | |
426 | | /* add a reference with the given poc to the list and mark it as used in DPB */ |
427 | | static int add_candidate_ref(HEVCContext *s, RefPicList *list, |
428 | | int poc, int ref_flag) |
429 | 0 | { |
430 | 0 | HEVCFrame *ref = find_ref_idx(s, poc); |
431 | |
|
432 | 0 | if (ref == s->ref) |
433 | 0 | return AVERROR_INVALIDDATA; |
434 | | |
435 | 0 | if (!ref) { |
436 | 0 | ref = generate_missing_ref(s, poc); |
437 | 0 | if (!ref) |
438 | 0 | return AVERROR(ENOMEM); |
439 | 0 | } |
440 | | |
441 | 0 | list->list[list->nb_refs] = ref->poc; |
442 | 0 | list->ref[list->nb_refs] = ref; |
443 | 0 | list->nb_refs++; |
444 | |
|
445 | 0 | mark_ref(ref, ref_flag); |
446 | 0 | return 0; |
447 | 0 | } |
448 | | |
449 | | int ff_hevc_frame_rps(HEVCContext *s) |
450 | 0 | { |
451 | 0 | const ShortTermRPS *short_rps = s->sh.short_term_rps; |
452 | 0 | const LongTermRPS *long_rps = &s->sh.long_term_rps; |
453 | 0 | RefPicList *rps = s->rps; |
454 | 0 | int i, ret; |
455 | |
|
456 | 0 | if (!short_rps) { |
457 | 0 | rps[0].nb_refs = rps[1].nb_refs = 0; |
458 | 0 | return 0; |
459 | 0 | } |
460 | | |
461 | | /* clear the reference flags on all frames except the current one */ |
462 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) { |
463 | 0 | HEVCFrame *frame = &s->DPB[i]; |
464 | |
|
465 | 0 | if (frame == s->ref) |
466 | 0 | continue; |
467 | | |
468 | 0 | mark_ref(frame, 0); |
469 | 0 | } |
470 | |
|
471 | 0 | for (i = 0; i < NB_RPS_TYPE; i++) |
472 | 0 | rps[i].nb_refs = 0; |
473 | | |
474 | | /* add the short refs */ |
475 | 0 | for (i = 0; i < short_rps->num_delta_pocs; i++) { |
476 | 0 | int poc = s->poc + short_rps->delta_poc[i]; |
477 | 0 | int list; |
478 | |
|
479 | 0 | if (!short_rps->used[i]) |
480 | 0 | list = ST_FOLL; |
481 | 0 | else if (i < short_rps->num_negative_pics) |
482 | 0 | list = ST_CURR_BEF; |
483 | 0 | else |
484 | 0 | list = ST_CURR_AFT; |
485 | |
|
486 | 0 | ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF); |
487 | 0 | if (ret < 0) |
488 | 0 | return ret; |
489 | 0 | } |
490 | | |
491 | | /* add the long refs */ |
492 | 0 | for (i = 0; i < long_rps->nb_refs; i++) { |
493 | 0 | int poc = long_rps->poc[i]; |
494 | 0 | int list = long_rps->used[i] ? LT_CURR : LT_FOLL; |
495 | |
|
496 | 0 | ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF); |
497 | 0 | if (ret < 0) |
498 | 0 | return ret; |
499 | 0 | } |
500 | | |
501 | | /* release any frames that are now unused */ |
502 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) |
503 | 0 | ff_hevc_unref_frame(s, &s->DPB[i], 0); |
504 | |
|
505 | 0 | return 0; |
506 | 0 | } |
507 | | |
508 | | int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb) |
509 | 0 | { |
510 | 0 | int max_poc_lsb = 1 << s->sps->log2_max_poc_lsb; |
511 | 0 | int prev_poc_lsb = s->pocTid0 % max_poc_lsb; |
512 | 0 | int prev_poc_msb = s->pocTid0 - prev_poc_lsb; |
513 | 0 | int poc_msb; |
514 | |
|
515 | 0 | if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2) |
516 | 0 | poc_msb = prev_poc_msb + max_poc_lsb; |
517 | 0 | else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2) |
518 | 0 | poc_msb = prev_poc_msb - max_poc_lsb; |
519 | 0 | else |
520 | 0 | poc_msb = prev_poc_msb; |
521 | | |
522 | | // For BLA picture types, POCmsb is set to 0. |
523 | 0 | if (s->nal_unit_type == NAL_BLA_W_LP || |
524 | 0 | s->nal_unit_type == NAL_BLA_W_RADL || |
525 | 0 | s->nal_unit_type == NAL_BLA_N_LP) |
526 | 0 | poc_msb = 0; |
527 | |
|
528 | 0 | return poc_msb + poc_lsb; |
529 | 0 | } |
530 | | |
531 | | int ff_hevc_frame_nb_refs(HEVCContext *s) |
532 | 0 | { |
533 | 0 | int ret = 0; |
534 | 0 | int i; |
535 | 0 | const ShortTermRPS *rps = s->sh.short_term_rps; |
536 | 0 | LongTermRPS *long_rps = &s->sh.long_term_rps; |
537 | |
|
538 | 0 | if (rps) { |
539 | 0 | for (i = 0; i < rps->num_negative_pics; i++) |
540 | 0 | ret += !!rps->used[i]; |
541 | 0 | for (; i < rps->num_delta_pocs; i++) |
542 | 0 | ret += !!rps->used[i]; |
543 | 0 | } |
544 | |
|
545 | 0 | if (long_rps) { |
546 | 0 | for (i = 0; i < long_rps->nb_refs; i++) |
547 | 0 | ret += !!long_rps->used[i]; |
548 | 0 | } |
549 | 0 | return ret; |
550 | 0 | } |
551 | | #endif |