Line | Branch | Exec | 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/mem.h" | ||
25 | #include "libavutil/stereo3d.h" | ||
26 | |||
27 | #include "container_fifo.h" | ||
28 | #include "decode.h" | ||
29 | #include "hevc.h" | ||
30 | #include "hevcdec.h" | ||
31 | #include "progressframe.h" | ||
32 | #include "refstruct.h" | ||
33 | |||
34 | 393624 | void ff_hevc_unref_frame(HEVCFrame *frame, int flags) | |
35 | { | ||
36 | 393624 | frame->flags &= ~flags; | |
37 |
2/2✓ Branch 0 taken 335541 times.
✓ Branch 1 taken 58083 times.
|
393624 | if (!frame->flags) { |
38 | 335541 | ff_progress_frame_unref(&frame->tf); | |
39 | 335541 | av_frame_unref(frame->frame_grain); | |
40 | 335541 | frame->needs_fg = 0; | |
41 | |||
42 | 335541 | ff_refstruct_unref(&frame->pps); | |
43 | 335541 | ff_refstruct_unref(&frame->tab_mvf); | |
44 | |||
45 | 335541 | ff_refstruct_unref(&frame->rpl); | |
46 | 335541 | frame->nb_rpl_elems = 0; | |
47 | 335541 | ff_refstruct_unref(&frame->rpl_tab); | |
48 | 335541 | frame->refPicList = NULL; | |
49 | |||
50 | 335541 | ff_refstruct_unref(&frame->hwaccel_picture_private); | |
51 | } | ||
52 | 393624 | } | |
53 | |||
54 | 4681214 | const RefPicList *ff_hevc_get_ref_list(const HEVCFrame *ref, int x0, int y0) | |
55 | { | ||
56 | 4681214 | const HEVCSPS *sps = ref->pps->sps; | |
57 | 4681214 | int x_cb = x0 >> sps->log2_ctb_size; | |
58 | 4681214 | int y_cb = y0 >> sps->log2_ctb_size; | |
59 | 4681214 | int pic_width_cb = sps->ctb_width; | |
60 | 4681214 | int ctb_addr_ts = ref->pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb]; | |
61 | 4681214 | return &ref->rpl_tab[ctb_addr_ts]->refPicList[0]; | |
62 | } | ||
63 | |||
64 | 823 | void ff_hevc_clear_refs(HEVCLayerContext *l) | |
65 | { | ||
66 | int i; | ||
67 |
2/2✓ Branch 0 taken 26336 times.
✓ Branch 1 taken 823 times.
|
27159 | for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) |
68 | 26336 | ff_hevc_unref_frame(&l->DPB[i], | |
69 | HEVC_FRAME_FLAG_SHORT_REF | | ||
70 | HEVC_FRAME_FLAG_LONG_REF); | ||
71 | 823 | } | |
72 | |||
73 | 8 | void ff_hevc_flush_dpb(HEVCContext *s) | |
74 | { | ||
75 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
|
24 | for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) { |
76 | 16 | HEVCLayerContext *l = &s->layers[layer]; | |
77 |
2/2✓ Branch 0 taken 512 times.
✓ Branch 1 taken 16 times.
|
528 | for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) |
78 | 512 | ff_hevc_unref_frame(&l->DPB[i], ~0); | |
79 | } | ||
80 | 8 | } | |
81 | |||
82 | 10229 | static HEVCFrame *alloc_frame(HEVCContext *s, HEVCLayerContext *l) | |
83 | { | ||
84 | 10229 | const HEVCVPS *vps = l->sps->vps; | |
85 | 10229 | const int view_id = vps->view_id[s->cur_layer]; | |
86 | int i, j, ret; | ||
87 |
1/2✓ Branch 0 taken 35883 times.
✗ Branch 1 not taken.
|
35883 | for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) { |
88 | 35883 | HEVCFrame *frame = &l->DPB[i]; | |
89 |
2/2✓ Branch 0 taken 25654 times.
✓ Branch 1 taken 10229 times.
|
35883 | if (frame->f) |
90 | 25654 | continue; | |
91 | |||
92 | 10229 | ret = ff_progress_frame_alloc(s->avctx, &frame->tf); | |
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (ret < 0) |
94 | ✗ | return NULL; | |
95 | |||
96 | // Add LCEVC SEI metadata here, as it's needed in get_buffer() | ||
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (s->sei.common.lcevc.info) { |
98 | ✗ | HEVCSEILCEVC *lcevc = &s->sei.common.lcevc; | |
99 | ✗ | ret = ff_frame_new_side_data_from_buf(s->avctx, frame->tf.f, | |
100 | AV_FRAME_DATA_LCEVC, &lcevc->info); | ||
101 | ✗ | if (ret < 0) | |
102 | ✗ | goto fail; | |
103 | } | ||
104 | |||
105 | // add view ID side data if it's nontrivial | ||
106 |
3/4✓ Branch 0 taken 9670 times.
✓ Branch 1 taken 559 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9670 times.
|
10229 | if (vps->nb_layers > 1 || view_id) { |
107 | 559 | HEVCSEITDRDI *tdrdi = &s->sei.tdrdi; | |
108 | 559 | AVFrameSideData *sd = av_frame_side_data_new(&frame->f->side_data, | |
109 | 559 | &frame->f->nb_side_data, | |
110 | AV_FRAME_DATA_VIEW_ID, | ||
111 | sizeof(int), 0); | ||
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 559 times.
|
559 | if (!sd) |
113 | ✗ | goto fail; | |
114 | 559 | *(int*)sd->data = view_id; | |
115 | |||
116 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 537 times.
|
559 | if (tdrdi->num_ref_displays) { |
117 | AVStereo3D *stereo_3d; | ||
118 | |||
119 | 22 | stereo_3d = av_stereo3d_create_side_data(frame->f); | |
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!stereo_3d) |
121 | ✗ | goto fail; | |
122 | |||
123 | 22 | stereo_3d->type = AV_STEREO3D_FRAMESEQUENCE; | |
124 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
|
22 | if (tdrdi->left_view_id[0] == view_id) |
125 | 11 | stereo_3d->view = AV_STEREO3D_VIEW_LEFT; | |
126 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | else if (tdrdi->right_view_id[0] == view_id) |
127 | 11 | stereo_3d->view = AV_STEREO3D_VIEW_RIGHT; | |
128 | else | ||
129 | ✗ | stereo_3d->view = AV_STEREO3D_VIEW_UNSPEC; | |
130 | } | ||
131 | } | ||
132 | |||
133 | 10229 | ret = ff_progress_frame_get_buffer(s->avctx, &frame->tf, | |
134 | AV_GET_BUFFER_FLAG_REF); | ||
135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (ret < 0) |
136 | ✗ | return NULL; | |
137 | |||
138 | 10229 | frame->rpl = ff_refstruct_allocz(s->pkt.nb_nals * sizeof(*frame->rpl)); | |
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (!frame->rpl) |
140 | ✗ | goto fail; | |
141 | 10229 | frame->nb_rpl_elems = s->pkt.nb_nals; | |
142 | |||
143 | 10229 | frame->tab_mvf = ff_refstruct_pool_get(l->tab_mvf_pool); | |
144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (!frame->tab_mvf) |
145 | ✗ | goto fail; | |
146 | |||
147 | 10229 | frame->rpl_tab = ff_refstruct_pool_get(l->rpl_tab_pool); | |
148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (!frame->rpl_tab) |
149 | ✗ | goto fail; | |
150 | 10229 | frame->ctb_count = l->sps->ctb_width * l->sps->ctb_height; | |
151 |
2/2✓ Branch 0 taken 1648997 times.
✓ Branch 1 taken 10229 times.
|
1659226 | for (j = 0; j < frame->ctb_count; j++) |
152 | 1648997 | frame->rpl_tab[j] = frame->rpl; | |
153 | |||
154 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 10214 times.
|
10229 | if (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) |
155 | 15 | frame->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
156 |
2/2✓ Branch 0 taken 10214 times.
✓ Branch 1 taken 15 times.
|
10229 | if ((s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) || |
157 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10212 times.
|
10214 | (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD)) |
158 | 17 | frame->f->flags |= AV_FRAME_FLAG_INTERLACED; | |
159 | |||
160 | 10229 | ret = ff_hwaccel_frame_priv_alloc(s->avctx, &frame->hwaccel_picture_private); | |
161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10229 times.
|
10229 | if (ret < 0) |
162 | ✗ | goto fail; | |
163 | |||
164 | 10229 | frame->pps = ff_refstruct_ref_c(s->pps); | |
165 | |||
166 | 10229 | return frame; | |
167 | ✗ | fail: | |
168 | ✗ | ff_hevc_unref_frame(frame, ~0); | |
169 | ✗ | return NULL; | |
170 | } | ||
171 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n"); | |
172 | ✗ | return NULL; | |
173 | } | ||
174 | |||
175 | 10171 | int ff_hevc_set_new_ref(HEVCContext *s, HEVCLayerContext *l, int poc) | |
176 | { | ||
177 | HEVCFrame *ref; | ||
178 | int i; | ||
179 | |||
180 | /* check that this POC doesn't already exist */ | ||
181 |
2/2✓ Branch 0 taken 325472 times.
✓ Branch 1 taken 10171 times.
|
335643 | for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) { |
182 | 325472 | HEVCFrame *frame = &l->DPB[i]; | |
183 | |||
184 |
3/4✓ Branch 0 taken 47080 times.
✓ Branch 1 taken 278392 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47080 times.
|
325472 | if (frame->f && frame->poc == poc) { |
185 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n", | |
186 | poc); | ||
187 | ✗ | return AVERROR_INVALIDDATA; | |
188 | } | ||
189 | } | ||
190 | |||
191 | 10171 | ref = alloc_frame(s, l); | |
192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10171 times.
|
10171 | if (!ref) |
193 | ✗ | return AVERROR(ENOMEM); | |
194 | |||
195 | 10171 | s->cur_frame = ref; | |
196 | 10171 | l->cur_frame = ref; | |
197 | 10171 | s->collocated_ref = NULL; | |
198 | |||
199 |
3/4✓ Branch 0 taken 277 times.
✓ Branch 1 taken 9894 times.
✓ Branch 2 taken 277 times.
✗ Branch 3 not taken.
|
10171 | ref->base_layer_frame = (l != &s->layers[0] && s->layers[0].cur_frame) ? |
200 | 277 | s->layers[0].cur_frame - s->layers[0].DPB : -1; | |
201 | |||
202 |
2/2✓ Branch 0 taken 10165 times.
✓ Branch 1 taken 6 times.
|
10171 | if (s->sh.pic_output_flag) |
203 | 10165 | ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF; | |
204 | else | ||
205 | 6 | ref->flags = HEVC_FRAME_FLAG_SHORT_REF; | |
206 | |||
207 | 10171 | ref->poc = poc; | |
208 | 10171 | ref->f->crop_left = l->sps->output_window.left_offset; | |
209 | 10171 | ref->f->crop_right = l->sps->output_window.right_offset; | |
210 | 10171 | ref->f->crop_top = l->sps->output_window.top_offset; | |
211 | 10171 | ref->f->crop_bottom = l->sps->output_window.bottom_offset; | |
212 | |||
213 | 10171 | return 0; | |
214 | } | ||
215 | |||
216 | 10171 | static void unref_missing_refs(HEVCLayerContext *l) | |
217 | { | ||
218 |
2/2✓ Branch 0 taken 325472 times.
✓ Branch 1 taken 10171 times.
|
335643 | for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) { |
219 | 325472 | HEVCFrame *frame = &l->DPB[i]; | |
220 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 325435 times.
|
325472 | if (frame->flags & HEVC_FRAME_FLAG_UNAVAILABLE) { |
221 | 37 | ff_hevc_unref_frame(frame, ~0); | |
222 | } | ||
223 | } | ||
224 | 10171 | } | |
225 | |||
226 | 10899 | int ff_hevc_output_frames(HEVCContext *s, | |
227 | unsigned layers_active_decode, unsigned layers_active_output, | ||
228 | unsigned max_output, unsigned max_dpb, int discard) | ||
229 | { | ||
230 | 10035 | while (1) { | |
231 | 20934 | int nb_dpb[HEVC_VPS_MAX_LAYERS] = { 0 }; | |
232 | 20934 | int nb_output = 0; | |
233 | 20934 | int min_poc = INT_MAX; | |
234 | 20934 | int min_layer = -1; | |
235 | 20934 | int min_idx, ret = 0; | |
236 | |||
237 |
2/2✓ Branch 0 taken 41868 times.
✓ Branch 1 taken 20934 times.
|
62802 | for (int layer = 0; layer < FF_ARRAY_ELEMS(s->layers); layer++) { |
238 | 41868 | HEVCLayerContext *l = &s->layers[layer]; | |
239 | |||
240 |
2/2✓ Branch 0 taken 20206 times.
✓ Branch 1 taken 21662 times.
|
41868 | if (!(layers_active_decode & (1 << layer))) |
241 | 20206 | continue; | |
242 | |||
243 |
2/2✓ Branch 0 taken 693184 times.
✓ Branch 1 taken 21662 times.
|
714846 | for (int i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) { |
244 | 693184 | HEVCFrame *frame = &l->DPB[i]; | |
245 |
2/2✓ Branch 0 taken 56588 times.
✓ Branch 1 taken 636596 times.
|
693184 | if (frame->flags & HEVC_FRAME_FLAG_OUTPUT) { |
246 | // nb_output counts AUs with an output-pending frame | ||
247 | // in at least one layer | ||
248 |
2/2✓ Branch 0 taken 2307 times.
✓ Branch 1 taken 54281 times.
|
56588 | if (!(frame->base_layer_frame >= 0 && |
249 |
2/2✓ Branch 0 taken 289 times.
✓ Branch 1 taken 2018 times.
|
2307 | (s->layers[0].DPB[frame->base_layer_frame].flags & HEVC_FRAME_FLAG_OUTPUT))) |
250 | 54570 | nb_output++; | |
251 |
4/4✓ Branch 0 taken 39067 times.
✓ Branch 1 taken 17521 times.
✓ Branch 2 taken 12820 times.
✓ Branch 3 taken 26247 times.
|
56588 | if (min_layer < 0 || frame->poc < min_poc) { |
252 | 30341 | min_poc = frame->poc; | |
253 | 30341 | min_idx = i; | |
254 | 30341 | min_layer = layer; | |
255 | } | ||
256 | } | ||
257 | 693184 | nb_dpb[layer] += !!frame->flags; | |
258 | } | ||
259 | } | ||
260 | |||
261 |
4/4✓ Branch 0 taken 11348 times.
✓ Branch 1 taken 9586 times.
✓ Branch 2 taken 7935 times.
✓ Branch 3 taken 3413 times.
|
20934 | if (nb_output > max_output || |
262 | 7935 | (nb_output && | |
263 |
4/4✓ Branch 0 taken 7498 times.
✓ Branch 1 taken 437 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 7486 times.
|
7935 | (nb_dpb[0] > max_dpb || nb_dpb[1] > max_dpb))) { |
264 | 10035 | HEVCFrame *frame = &s->layers[min_layer].DPB[min_idx]; | |
265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10035 times.
|
10035 | AVFrame *f = frame->needs_fg ? frame->frame_grain : frame->f; |
266 |
3/4✓ Branch 0 taken 10006 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 10006 times.
✗ Branch 3 not taken.
|
10035 | int output = !discard && (layers_active_output & (1 << min_layer)); |
267 | |||
268 |
2/2✓ Branch 0 taken 10006 times.
✓ Branch 1 taken 29 times.
|
10035 | if (output) { |
269 | 10006 | f->pkt_dts = s->pkt_dts; | |
270 | 10006 | ret = ff_container_fifo_write(s->output_fifo, f); | |
271 | } | ||
272 | 10035 | ff_hevc_unref_frame(frame, HEVC_FRAME_FLAG_OUTPUT); | |
273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10035 times.
|
10035 | if (ret < 0) |
274 | 10899 | return ret; | |
275 | |||
276 |
2/2✓ Branch 0 taken 10006 times.
✓ Branch 1 taken 29 times.
|
10035 | av_log(s->avctx, AV_LOG_DEBUG, "%s frame with POC %d/%d.\n", |
277 | output ? "Output" : "Discarded", min_layer, frame->poc); | ||
278 | 10035 | continue; | |
279 | } | ||
280 | 10899 | return 0; | |
281 | } | ||
282 | } | ||
283 | |||
284 | 18983 | static int init_slice_rpl(HEVCContext *s) | |
285 | { | ||
286 | 18983 | HEVCFrame *frame = s->cur_frame; | |
287 | 18983 | int ctb_count = frame->ctb_count; | |
288 | 18983 | int ctb_addr_ts = s->pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr]; | |
289 | int i; | ||
290 | |||
291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18983 times.
|
18983 | if (s->slice_idx >= frame->nb_rpl_elems) |
292 | ✗ | return AVERROR_INVALIDDATA; | |
293 | |||
294 |
2/2✓ Branch 0 taken 2105122 times.
✓ Branch 1 taken 18983 times.
|
2124105 | for (i = ctb_addr_ts; i < ctb_count; i++) |
295 | 2105122 | frame->rpl_tab[i] = frame->rpl + s->slice_idx; | |
296 | |||
297 | 18983 | frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts]; | |
298 | |||
299 | 18983 | return 0; | |
300 | } | ||
301 | |||
302 | 18983 | int ff_hevc_slice_rpl(HEVCContext *s) | |
303 | { | ||
304 | 18983 | SliceHeader *sh = &s->sh; | |
305 | |||
306 |
2/2✓ Branch 0 taken 15146 times.
✓ Branch 1 taken 3837 times.
|
18983 | uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1; |
307 | uint8_t list_idx; | ||
308 | int i, j, ret; | ||
309 | |||
310 | 18983 | ret = init_slice_rpl(s); | |
311 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18983 times.
|
18983 | if (ret < 0) |
312 | ✗ | return ret; | |
313 | |||
314 | 18983 | if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs + | |
315 | 18983 | s->rps[LT_CURR].nb_refs + | |
316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18983 times.
|
18983 | s->rps[INTER_LAYER0].nb_refs + s->rps[INTER_LAYER1].nb_refs) && |
317 | ✗ | !s->pps->pps_curr_pic_ref_enabled_flag) { | |
318 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n"); | |
319 | ✗ | return AVERROR_INVALIDDATA; | |
320 | } | ||
321 | |||
322 |
2/2✓ Branch 0 taken 34129 times.
✓ Branch 1 taken 18983 times.
|
53112 | for (list_idx = 0; list_idx < nb_list; list_idx++) { |
323 | 34129 | RefPicList rpl_tmp = { { 0 } }; | |
324 | 34129 | RefPicList *rpl = &s->cur_frame->refPicList[list_idx]; | |
325 | |||
326 | /* The order of the elements is | ||
327 | * ST_CURR_BEF - INTER_LAYER0 - ST_CURR_AFT - LT_CURR - INTER_LAYER1 for the L0 and | ||
328 | * ST_CURR_AFT - INTER_LAYER1 - ST_CURR_BEF - LT_CURR - INTER_LAYER0 for the L1 */ | ||
329 | 102387 | int cand_lists[] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF, | |
330 |
2/2✓ Branch 0 taken 15146 times.
✓ Branch 1 taken 18983 times.
|
34129 | list_idx ? INTER_LAYER1 : INTER_LAYER0, |
331 | 34129 | list_idx ? ST_CURR_BEF : ST_CURR_AFT, | |
332 | LT_CURR, | ||
333 |
2/2✓ Branch 0 taken 15146 times.
✓ Branch 1 taken 18983 times.
|
34129 | list_idx ? INTER_LAYER0 : INTER_LAYER1 |
334 | }; | ||
335 | |||
336 | /* concatenate the candidate lists for the current frame */ | ||
337 |
2/2✓ Branch 0 taken 34147 times.
✓ Branch 1 taken 34129 times.
|
68276 | while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) { |
338 |
2/2✓ Branch 0 taken 170735 times.
✓ Branch 1 taken 34147 times.
|
204882 | for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) { |
339 | 170735 | RefPicList *rps = &s->rps[cand_lists[i]]; | |
340 |
3/4✓ Branch 0 taken 123299 times.
✓ Branch 1 taken 170735 times.
✓ Branch 2 taken 123299 times.
✗ Branch 3 not taken.
|
294034 | for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) { |
341 | 123299 | rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j]; | |
342 | 123299 | rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j]; | |
343 | // multiview inter-layer refs are treated as long-term here, | ||
344 | // cf. G.8.1.3 | ||
345 | 368857 | rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = cand_lists[i] == LT_CURR || | |
346 |
4/4✓ Branch 0 taken 122259 times.
✓ Branch 1 taken 1040 times.
✓ Branch 2 taken 121870 times.
✓ Branch 3 taken 389 times.
|
245169 | cand_lists[i] == INTER_LAYER0 || |
347 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 121870 times.
|
121870 | cand_lists[i] == INTER_LAYER1; |
348 | 123299 | rpl_tmp.nb_refs++; | |
349 | } | ||
350 | } | ||
351 | // Construct RefPicList0, RefPicList1 (8-8, 8-10) | ||
352 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 34147 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
34147 | if (s->pps->pps_curr_pic_ref_enabled_flag && rpl_tmp.nb_refs < HEVC_MAX_REFS) { |
353 | ✗ | rpl_tmp.list[rpl_tmp.nb_refs] = s->cur_frame->poc; | |
354 | ✗ | rpl_tmp.ref[rpl_tmp.nb_refs] = s->cur_frame; | |
355 | ✗ | rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = 1; | |
356 | ✗ | rpl_tmp.nb_refs++; | |
357 | } | ||
358 | } | ||
359 | |||
360 | /* reorder the references if necessary */ | ||
361 |
2/2✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 32229 times.
|
34129 | if (sh->rpl_modification_flag[list_idx]) { |
362 |
2/2✓ Branch 0 taken 4554 times.
✓ Branch 1 taken 1900 times.
|
6454 | for (i = 0; i < sh->nb_refs[list_idx]; i++) { |
363 | 4554 | int idx = sh->list_entry_lx[list_idx][i]; | |
364 | |||
365 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4554 times.
|
4554 | if (idx >= rpl_tmp.nb_refs) { |
366 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n"); | |
367 | ✗ | return AVERROR_INVALIDDATA; | |
368 | } | ||
369 | |||
370 | 4554 | rpl->list[i] = rpl_tmp.list[idx]; | |
371 | 4554 | rpl->ref[i] = rpl_tmp.ref[idx]; | |
372 | 4554 | rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx]; | |
373 | 4554 | rpl->nb_refs++; | |
374 | } | ||
375 | } else { | ||
376 | 32229 | memcpy(rpl, &rpl_tmp, sizeof(*rpl)); | |
377 | 32229 | rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]); | |
378 | } | ||
379 | |||
380 | // 8-9 | ||
381 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34129 times.
|
34129 | if (s->pps->pps_curr_pic_ref_enabled_flag && |
382 | ✗ | !sh->rpl_modification_flag[list_idx] && | |
383 | ✗ | rpl_tmp.nb_refs > sh->nb_refs[L0]) { | |
384 | ✗ | rpl->list[sh->nb_refs[L0] - 1] = s->cur_frame->poc; | |
385 | ✗ | rpl->ref[sh->nb_refs[L0] - 1] = s->cur_frame; | |
386 | } | ||
387 | |||
388 |
2/2✓ Branch 0 taken 18982 times.
✓ Branch 1 taken 15147 times.
|
34129 | if (sh->collocated_list == list_idx && |
389 |
1/2✓ Branch 0 taken 18982 times.
✗ Branch 1 not taken.
|
18982 | sh->collocated_ref_idx < rpl->nb_refs) |
390 | 18982 | s->collocated_ref = rpl->ref[sh->collocated_ref_idx]; | |
391 | } | ||
392 | |||
393 | 18983 | return 0; | |
394 | } | ||
395 | |||
396 | 36973 | static HEVCFrame *find_ref_idx(HEVCContext *s, HEVCLayerContext *l, | |
397 | int poc, uint8_t use_msb) | ||
398 | { | ||
399 |
2/2✓ Branch 0 taken 1513 times.
✓ Branch 1 taken 35460 times.
|
36973 | int mask = use_msb ? ~0 : (1 << l->sps->log2_max_poc_lsb) - 1; |
400 | int i; | ||
401 | |||
402 |
2/2✓ Branch 0 taken 151343 times.
✓ Branch 1 taken 58 times.
|
151401 | for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) { |
403 | 151343 | HEVCFrame *ref = &l->DPB[i]; | |
404 |
2/2✓ Branch 0 taken 146553 times.
✓ Branch 1 taken 4790 times.
|
151343 | if (ref->f) { |
405 |
5/6✓ Branch 0 taken 36915 times.
✓ Branch 1 taken 109638 times.
✓ Branch 2 taken 1512 times.
✓ Branch 3 taken 35403 times.
✓ Branch 4 taken 1512 times.
✗ Branch 5 not taken.
|
146553 | if ((ref->poc & mask) == poc && (use_msb || ref->poc != s->poc)) |
406 | 36915 | return ref; | |
407 | } | ||
408 | } | ||
409 | |||
410 |
7/8✓ Branch 0 taken 23 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 2 times.
|
58 | if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s)) |
411 | 20 | av_log(s->avctx, AV_LOG_ERROR, | |
412 | "Could not find ref with POC %d\n", poc); | ||
413 | 58 | return NULL; | |
414 | } | ||
415 | |||
416 | 352274 | static void mark_ref(HEVCFrame *frame, int flag) | |
417 | { | ||
418 | 352274 | frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF); | |
419 | 352274 | frame->flags |= flag; | |
420 | 352274 | } | |
421 | |||
422 | 58 | static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc) | |
423 | { | ||
424 | HEVCFrame *frame; | ||
425 | int i, y; | ||
426 | |||
427 | 58 | frame = alloc_frame(s, l); | |
428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (!frame) |
429 | ✗ | return NULL; | |
430 | |||
431 |
1/2✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 | if (!s->avctx->hwaccel) { |
432 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 20 times.
|
58 | if (!l->sps->pixel_shift) { |
433 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 38 times.
|
152 | for (i = 0; frame->f->data[i]; i++) |
434 | 114 | memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1), | |
435 | 114 | frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i])); | |
436 | } else { | ||
437 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
|
80 | for (i = 0; frame->f->data[i]; i++) |
438 |
2/2✓ Branch 0 taken 71424 times.
✓ Branch 1 taken 60 times.
|
71484 | for (y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) { |
439 | 71424 | uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i]; | |
440 | 71424 | AV_WN16(dst, 1 << (l->sps->bit_depth - 1)); | |
441 | 71424 | av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2); | |
442 | } | ||
443 | } | ||
444 | } | ||
445 | |||
446 | 58 | frame->poc = poc; | |
447 | 58 | frame->flags = HEVC_FRAME_FLAG_UNAVAILABLE; | |
448 | |||
449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (s->avctx->active_thread_type == FF_THREAD_FRAME) |
450 | ✗ | ff_progress_frame_report(&frame->tf, INT_MAX); | |
451 | |||
452 | 58 | return frame; | |
453 | } | ||
454 | |||
455 | /* add a reference with the given poc to the list and mark it as used in DPB */ | ||
456 | 36973 | static int add_candidate_ref(HEVCContext *s, HEVCLayerContext *l, | |
457 | RefPicList *list, | ||
458 | int poc, int ref_flag, uint8_t use_msb) | ||
459 | { | ||
460 | 36973 | HEVCFrame *ref = find_ref_idx(s, l, poc, use_msb); | |
461 | |||
462 |
2/4✓ Branch 0 taken 36973 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36973 times.
|
36973 | if (ref == s->cur_frame || list->nb_refs >= HEVC_MAX_REFS) |
463 | ✗ | return AVERROR_INVALIDDATA; | |
464 | |||
465 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 36915 times.
|
36973 | if (!ref) { |
466 | 58 | ref = generate_missing_ref(s, l, poc); | |
467 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (!ref) |
468 | ✗ | return AVERROR(ENOMEM); | |
469 | } | ||
470 | |||
471 | 36973 | list->list[list->nb_refs] = ref->poc; | |
472 | 36973 | list->ref[list->nb_refs] = ref; | |
473 | 36973 | list->nb_refs++; | |
474 | |||
475 | 36973 | mark_ref(ref, ref_flag); | |
476 | 36973 | return 0; | |
477 | } | ||
478 | |||
479 | 10171 | int ff_hevc_frame_rps(HEVCContext *s, HEVCLayerContext *l) | |
480 | { | ||
481 | 10171 | const ShortTermRPS *short_rps = s->sh.short_term_rps; | |
482 | 10171 | const LongTermRPS *long_rps = &s->sh.long_term_rps; | |
483 | 10171 | RefPicList *rps = s->rps; | |
484 | 10171 | int i, ret = 0; | |
485 | |||
486 | 10171 | unref_missing_refs(l); | |
487 | |||
488 | /* clear the reference flags on all frames except the current one */ | ||
489 |
2/2✓ Branch 0 taken 325472 times.
✓ Branch 1 taken 10171 times.
|
335643 | for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) { |
490 | 325472 | HEVCFrame *frame = &l->DPB[i]; | |
491 | |||
492 |
2/2✓ Branch 0 taken 10171 times.
✓ Branch 1 taken 315301 times.
|
325472 | if (frame == s->cur_frame) |
493 | 10171 | continue; | |
494 | |||
495 | 315301 | mark_ref(frame, 0); | |
496 | } | ||
497 | |||
498 |
2/2✓ Branch 0 taken 71197 times.
✓ Branch 1 taken 10171 times.
|
81368 | for (i = 0; i < NB_RPS_TYPE; i++) |
499 | 71197 | rps[i].nb_refs = 0; | |
500 | |||
501 |
2/2✓ Branch 0 taken 410 times.
✓ Branch 1 taken 9761 times.
|
10171 | if (!short_rps) |
502 | 410 | goto inter_layer; | |
503 | |||
504 | /* add the short refs */ | ||
505 |
2/2✓ Branch 0 taken 35180 times.
✓ Branch 1 taken 9761 times.
|
44941 | for (i = 0; i < short_rps->num_delta_pocs; i++) { |
506 | 35180 | int poc = s->poc + short_rps->delta_poc[i]; | |
507 | int list; | ||
508 | |||
509 |
2/2✓ Branch 0 taken 1766 times.
✓ Branch 1 taken 33414 times.
|
35180 | if (!(short_rps->used & (1 << i))) |
510 | 1766 | list = ST_FOLL; | |
511 |
2/2✓ Branch 0 taken 24150 times.
✓ Branch 1 taken 9264 times.
|
33414 | else if (i < short_rps->num_negative_pics) |
512 | 24150 | list = ST_CURR_BEF; | |
513 | else | ||
514 | 9264 | list = ST_CURR_AFT; | |
515 | |||
516 | 35180 | ret = add_candidate_ref(s, l, &rps[list], poc, | |
517 | HEVC_FRAME_FLAG_SHORT_REF, 1); | ||
518 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35180 times.
|
35180 | if (ret < 0) |
519 | ✗ | goto fail; | |
520 | } | ||
521 | |||
522 | /* add the long refs */ | ||
523 |
2/2✓ Branch 0 taken 1561 times.
✓ Branch 1 taken 9761 times.
|
11322 | for (i = 0; i < long_rps->nb_refs; i++) { |
524 | 1561 | int poc = long_rps->poc[i]; | |
525 |
2/2✓ Branch 0 taken 574 times.
✓ Branch 1 taken 987 times.
|
1561 | int list = long_rps->used[i] ? LT_CURR : LT_FOLL; |
526 | |||
527 | 1561 | ret = add_candidate_ref(s, l, &rps[list], poc, | |
528 | 1561 | HEVC_FRAME_FLAG_LONG_REF, long_rps->poc_msb_present[i]); | |
529 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1561 times.
|
1561 | if (ret < 0) |
530 | ✗ | goto fail; | |
531 | } | ||
532 | |||
533 | 9761 | inter_layer: | |
534 | /* add inter-layer refs */ | ||
535 |
2/2✓ Branch 0 taken 9939 times.
✓ Branch 1 taken 232 times.
|
10171 | if (s->sh.inter_layer_pred) { |
536 | 232 | HEVCLayerContext *l0 = &s->layers[0]; | |
537 | |||
538 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | av_assert0(l != l0); |
539 | |||
540 | /* Given the assumption of at most two layers, refPicSet0Flag is | ||
541 | * always 1, so only RefPicSetInterLayer0 can ever contain a frame. */ | ||
542 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | if (l0->cur_frame) { |
543 | // inter-layer refs are treated as short-term here, cf. F.8.1.6 | ||
544 | 232 | ret = add_candidate_ref(s, l0, &rps[INTER_LAYER0], l0->cur_frame->poc, | |
545 | HEVC_FRAME_FLAG_SHORT_REF, 1); | ||
546 |
1/2✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
|
232 | if (ret < 0) |
547 | ✗ | goto fail; | |
548 | } | ||
549 | } | ||
550 | |||
551 | 10171 | fail: | |
552 | /* release any frames that are now unused */ | ||
553 |
2/2✓ Branch 0 taken 325472 times.
✓ Branch 1 taken 10171 times.
|
335643 | for (i = 0; i < FF_ARRAY_ELEMS(l->DPB); i++) |
554 | 325472 | ff_hevc_unref_frame(&l->DPB[i], 0); | |
555 | |||
556 | 10171 | return ret; | |
557 | } | ||
558 | |||
559 | 19069 | int ff_hevc_frame_nb_refs(const SliceHeader *sh, const HEVCPPS *pps, | |
560 | unsigned layer_idx) | ||
561 | { | ||
562 | 19069 | int ret = 0; | |
563 | int i; | ||
564 | 19069 | const ShortTermRPS *rps = sh->short_term_rps; | |
565 | 19069 | const LongTermRPS *long_rps = &sh->long_term_rps; | |
566 | |||
567 |
2/2✓ Branch 0 taken 19062 times.
✓ Branch 1 taken 7 times.
|
19069 | if (rps) { |
568 |
2/2✓ Branch 0 taken 46380 times.
✓ Branch 1 taken 19062 times.
|
65442 | for (i = 0; i < rps->num_negative_pics; i++) |
569 | 46380 | ret += !!(rps->used & (1 << i)); | |
570 |
2/2✓ Branch 0 taken 21066 times.
✓ Branch 1 taken 19062 times.
|
40128 | for (; i < rps->num_delta_pocs; i++) |
571 | 21066 | ret += !!(rps->used & (1 << i)); | |
572 | } | ||
573 | |||
574 |
1/2✓ Branch 0 taken 19069 times.
✗ Branch 1 not taken.
|
19069 | if (long_rps) { |
575 |
2/2✓ Branch 0 taken 1421 times.
✓ Branch 1 taken 19069 times.
|
20490 | for (i = 0; i < long_rps->nb_refs; i++) |
576 | 1421 | ret += !!long_rps->used[i]; | |
577 | } | ||
578 | |||
579 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 18837 times.
|
19069 | if (sh->inter_layer_pred) { |
580 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232 times.
|
232 | av_assert0(pps->sps->vps->num_direct_ref_layers[layer_idx] < 2); |
581 | 232 | ret++; | |
582 | } | ||
583 | |||
584 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19069 times.
|
19069 | if (pps->pps_curr_pic_ref_enabled_flag) |
585 | ✗ | ret++; | |
586 | |||
587 | 19069 | return ret; | |
588 | } | ||
589 |