/work/libde265/libde265/motion.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 "motion.h" |
22 | | #include "decctx.h" |
23 | | #include "util.h" |
24 | | #include "dpb.h" |
25 | | |
26 | | #include <assert.h> |
27 | | |
28 | | |
29 | | #include <sys/types.h> |
30 | | #include <signal.h> |
31 | | #include <string.h> |
32 | | |
33 | | #if defined(_MSC_VER) || defined(__MINGW32__) |
34 | | # include <malloc.h> |
35 | | #elif defined(HAVE_ALLOCA_H) |
36 | | # include <alloca.h> |
37 | | #endif |
38 | | |
39 | | |
40 | 0 | #define MAX_CU_SIZE 64 |
41 | | |
42 | | |
43 | | static int extra_before[4] = { 0,3,3,2 }; |
44 | | static int extra_after [4] = { 0,3,4,4 }; |
45 | | |
46 | | |
47 | | |
48 | | // Luma sample interpolation process (8.5.3.3.3.2). |
49 | | // inter_t is the type of the intermediate prediction samples (see |
50 | | // generate_inter_prediction_samples_plane below). |
51 | | template <class pixel_t, class inter_t> |
52 | | void mc_luma(const base_context* ctx, |
53 | | const seq_parameter_set* sps, int mv_x, int mv_y, |
54 | | int xP,int yP, |
55 | | inter_t* out, int out_stride, |
56 | | const pixel_t* ref, ptrdiff_t ref_stride, |
57 | | int nPbW, int nPbH, int bitDepth_L) |
58 | 0 | { |
59 | 0 | int xFracL = mv_x & 3; |
60 | 0 | int yFracL = mv_y & 3; |
61 | |
|
62 | 0 | int xIntOffsL = xP + (mv_x>>2); |
63 | 0 | int yIntOffsL = yP + (mv_y>>2); |
64 | | |
65 | | //const int shift1 = std::min(4, sps->BitDepth_Y-8); |
66 | | //const int shift2 = 6; |
67 | 0 | const int shift3 = std::max(2, 14 - sps->BitDepth_Y); |
68 | |
|
69 | 0 | int w = sps->pic_width_in_luma_samples; |
70 | 0 | int h = sps->pic_height_in_luma_samples; |
71 | |
|
72 | 0 | ALIGNED_16(inter_t) mcbuffer[MAX_CU_SIZE * (MAX_CU_SIZE+7)]; |
73 | |
|
74 | 0 | if (xFracL==0 && yFracL==0) { |
75 | |
|
76 | 0 | if (xIntOffsL >= 0 && yIntOffsL >= 0 && |
77 | 0 | nPbW+xIntOffsL <= w && nPbH+yIntOffsL <= h) { |
78 | |
|
79 | 0 | ctx->acceleration.put_hevc_qpel(out, out_stride, |
80 | 0 | &ref[yIntOffsL*ref_stride + xIntOffsL], |
81 | 0 | ref_stride /* sizeof(pixel_t)*/, |
82 | 0 | nPbW,nPbH, mcbuffer, 0,0, bitDepth_L); |
83 | 0 | } |
84 | 0 | else { |
85 | 0 | for (int y=0;y<nPbH;y++) |
86 | 0 | for (int x=0;x<nPbW;x++) { |
87 | |
|
88 | 0 | int xA = Clip3(0,w-1,x + xIntOffsL); |
89 | 0 | int yA = Clip3(0,h-1,y + yIntOffsL); |
90 | |
|
91 | 0 | out[y*out_stride+x] = ref[ xA + yA*ref_stride ] << shift3; |
92 | 0 | } |
93 | 0 | } |
94 | |
|
95 | | #ifdef DE265_LOG_TRACE |
96 | | logtrace(LogMotion,"---MC luma %d %d = direct---\n",xFracL,yFracL); |
97 | | |
98 | | for (int y=0;y<nPbH;y++) { |
99 | | for (int x=0;x<nPbW;x++) { |
100 | | |
101 | | int xA = Clip3(0,w-1,x + xIntOffsL); |
102 | | int yA = Clip3(0,h-1,y + yIntOffsL); |
103 | | |
104 | | logtrace(LogMotion,"%02x ", ref[ xA + yA*ref_stride ]); |
105 | | } |
106 | | logtrace(LogMotion,"\n"); |
107 | | } |
108 | | |
109 | | logtrace(LogMotion," -> \n"); |
110 | | |
111 | | for (int y=0;y<nPbH;y++) { |
112 | | for (int x=0;x<nPbW;x++) { |
113 | | |
114 | | logtrace(LogMotion,"%02x ",out[y*out_stride+x] >> 6); // 6 will be used when summing predictions |
115 | | } |
116 | | logtrace(LogMotion,"\n"); |
117 | | } |
118 | | #endif |
119 | 0 | } |
120 | 0 | else { |
121 | 0 | int extra_left = extra_before[xFracL]; |
122 | 0 | int extra_right = extra_after [xFracL]; |
123 | 0 | int extra_top = extra_before[yFracL]; |
124 | 0 | int extra_bottom = extra_after [yFracL]; |
125 | | |
126 | | //int nPbW_extra = extra_left + nPbW + extra_right; |
127 | | //int nPbH_extra = extra_top + nPbH + extra_bottom; |
128 | | |
129 | |
|
130 | 0 | pixel_t padbuf[(MAX_CU_SIZE+16)*(MAX_CU_SIZE+7)]; |
131 | |
|
132 | 0 | const pixel_t* src_ptr; |
133 | 0 | ptrdiff_t src_stride; |
134 | |
|
135 | 0 | if (-extra_left + xIntOffsL >= 0 && |
136 | 0 | -extra_top + yIntOffsL >= 0 && |
137 | 0 | nPbW+extra_right + xIntOffsL < w && |
138 | 0 | nPbH+extra_bottom + yIntOffsL < h) { |
139 | 0 | src_ptr = &ref[xIntOffsL + yIntOffsL*ref_stride]; |
140 | 0 | src_stride = ref_stride; |
141 | 0 | } |
142 | 0 | else { |
143 | | // Extend fill width to a multiple of 16 so that SIMD over-reads |
144 | | // in qpel interpolation hit valid (edge-clamped) data. |
145 | 0 | int fill_width = ((extra_left + nPbW + extra_right + 15) & ~15); |
146 | 0 | if (fill_width > MAX_CU_SIZE+16) fill_width = MAX_CU_SIZE+16; |
147 | |
|
148 | 0 | for (int y=-extra_top;y<nPbH+extra_bottom;y++) { |
149 | 0 | for (int x=-extra_left;x<fill_width - extra_left;x++) { |
150 | |
|
151 | 0 | int xA = Clip3(0,w-1,x + xIntOffsL); |
152 | 0 | int yA = Clip3(0,h-1,y + yIntOffsL); |
153 | |
|
154 | 0 | padbuf[x+extra_left + (y+extra_top)*(MAX_CU_SIZE+16)] = ref[ xA + yA*ref_stride ]; |
155 | 0 | } |
156 | 0 | } |
157 | |
|
158 | 0 | src_ptr = &padbuf[extra_top*(MAX_CU_SIZE+16) + extra_left]; |
159 | 0 | src_stride = MAX_CU_SIZE+16; |
160 | 0 | } |
161 | |
|
162 | 0 | ctx->acceleration.put_hevc_qpel(out, out_stride, |
163 | 0 | src_ptr, src_stride /* sizeof(pixel_t) */, |
164 | 0 | nPbW,nPbH, mcbuffer, xFracL,yFracL, bitDepth_L); |
165 | | |
166 | |
|
167 | 0 | logtrace(LogMotion,"---V---\n"); |
168 | 0 | for (int y=0;y<nPbH;y++) { |
169 | 0 | for (int x=0;x<nPbW;x++) { |
170 | 0 | logtrace(LogMotion,"%04x ",out[x+y*out_stride]); |
171 | 0 | } |
172 | 0 | logtrace(LogMotion,"\n"); |
173 | 0 | } |
174 | 0 | } |
175 | 0 | } Unexecuted instantiation: void mc_luma<unsigned short, short>(base_context const*, seq_parameter_set const*, int, int, int, int, short*, int, unsigned short const*, long, int, int, int) Unexecuted instantiation: void mc_luma<unsigned char, short>(base_context const*, seq_parameter_set const*, int, int, int, int, short*, int, unsigned char const*, long, int, int, int) Unexecuted instantiation: void mc_luma<unsigned short, int>(base_context const*, seq_parameter_set const*, int, int, int, int, int*, int, unsigned short const*, long, int, int, int) Unexecuted instantiation: void mc_luma<unsigned char, int>(base_context const*, seq_parameter_set const*, int, int, int, int, int*, int, unsigned char const*, long, int, int, int) |
176 | | |
177 | | |
178 | | |
179 | | // Chroma sample interpolation process (8.5.3.3.3.3). |
180 | | template <class pixel_t, class inter_t> |
181 | | void mc_chroma(const base_context* ctx, |
182 | | const seq_parameter_set* sps, |
183 | | int mv_x, int mv_y, |
184 | | int xP,int yP, |
185 | | inter_t* out, int out_stride, |
186 | | const pixel_t* ref, ptrdiff_t ref_stride, |
187 | | int nPbWC, int nPbHC, int bit_depth_C) |
188 | 0 | { |
189 | | //const int shift1 = std::min(4, sps->BitDepth_C-8); |
190 | | //const int shift2 = 6; |
191 | 0 | const int shift3 = std::max(2, 14 - sps->BitDepth_C); |
192 | |
|
193 | 0 | int wC = sps->pic_width_in_luma_samples /sps->SubWidthC; |
194 | 0 | int hC = sps->pic_height_in_luma_samples/sps->SubHeightC; |
195 | |
|
196 | 0 | mv_x *= 2 / sps->SubWidthC; |
197 | 0 | mv_y *= 2 / sps->SubHeightC; |
198 | |
|
199 | 0 | int xFracC = mv_x & 7; |
200 | 0 | int yFracC = mv_y & 7; |
201 | |
|
202 | 0 | int xIntOffsC = xP/sps->SubWidthC + (mv_x>>3); |
203 | 0 | int yIntOffsC = yP/sps->SubHeightC + (mv_y>>3); |
204 | |
|
205 | 0 | ALIGNED_32(inter_t mcbuffer[MAX_CU_SIZE*(MAX_CU_SIZE+7)]); |
206 | |
|
207 | 0 | if (xFracC == 0 && yFracC == 0) { |
208 | 0 | if (xIntOffsC>=0 && nPbWC+xIntOffsC<=wC && |
209 | 0 | yIntOffsC>=0 && nPbHC+yIntOffsC<=hC) { |
210 | 0 | ctx->acceleration.put_hevc_epel(out, out_stride, |
211 | 0 | &ref[xIntOffsC + yIntOffsC*ref_stride], ref_stride, |
212 | 0 | nPbWC,nPbHC, 0,0, nullptr, bit_depth_C); |
213 | 0 | } |
214 | 0 | else |
215 | 0 | { |
216 | 0 | for (int y=0;y<nPbHC;y++) |
217 | 0 | for (int x=0;x<nPbWC;x++) { |
218 | |
|
219 | 0 | int xB = Clip3(0,wC-1,x + xIntOffsC); |
220 | 0 | int yB = Clip3(0,hC-1,y + yIntOffsC); |
221 | |
|
222 | 0 | out[y*out_stride+x] = ref[ xB + yB*ref_stride ] << shift3; |
223 | 0 | } |
224 | 0 | } |
225 | 0 | } |
226 | 0 | else { |
227 | 0 | pixel_t padbuf[(MAX_CU_SIZE+16)*(MAX_CU_SIZE+3)]; |
228 | |
|
229 | 0 | const pixel_t* src_ptr; |
230 | 0 | ptrdiff_t src_stride; |
231 | |
|
232 | 0 | int extra_top = 1; |
233 | 0 | int extra_left = 1; |
234 | 0 | int extra_right = 2; |
235 | 0 | int extra_bottom = 2; |
236 | |
|
237 | 0 | if (xIntOffsC>=1 && nPbWC+xIntOffsC<=wC-2 && |
238 | 0 | yIntOffsC>=1 && nPbHC+yIntOffsC<=hC-2) { |
239 | 0 | src_ptr = &ref[xIntOffsC + yIntOffsC*ref_stride]; |
240 | 0 | src_stride = ref_stride; |
241 | 0 | } |
242 | 0 | else { |
243 | | // Extend fill width to a multiple of 16 so that SIMD over-reads |
244 | | // in epel interpolation hit valid (edge-clamped) data. |
245 | 0 | int fill_width = ((extra_left + nPbWC + extra_right + 15) & ~15); |
246 | 0 | if (fill_width > MAX_CU_SIZE+16) fill_width = MAX_CU_SIZE+16; |
247 | |
|
248 | 0 | for (int y=-extra_top;y<nPbHC+extra_bottom;y++) { |
249 | 0 | for (int x=-extra_left;x<fill_width - extra_left;x++) { |
250 | |
|
251 | 0 | int xA = Clip3(0,wC-1,x + xIntOffsC); |
252 | 0 | int yA = Clip3(0,hC-1,y + yIntOffsC); |
253 | |
|
254 | 0 | padbuf[x+extra_left + (y+extra_top)*(MAX_CU_SIZE+16)] = ref[ xA + yA*ref_stride ]; |
255 | 0 | } |
256 | 0 | } |
257 | |
|
258 | 0 | src_ptr = &padbuf[extra_left + extra_top*(MAX_CU_SIZE+16)]; |
259 | 0 | src_stride = MAX_CU_SIZE+16; |
260 | 0 | } |
261 | | |
262 | |
|
263 | 0 | if (xFracC && yFracC) { |
264 | 0 | ctx->acceleration.put_hevc_epel_hv(out, out_stride, |
265 | 0 | src_ptr, src_stride, |
266 | 0 | nPbWC,nPbHC, xFracC,yFracC, mcbuffer, bit_depth_C); |
267 | 0 | } |
268 | 0 | else if (xFracC) { |
269 | 0 | ctx->acceleration.put_hevc_epel_h(out, out_stride, |
270 | 0 | src_ptr, src_stride, |
271 | 0 | nPbWC,nPbHC, xFracC,yFracC, mcbuffer, bit_depth_C); |
272 | 0 | } |
273 | 0 | else if (yFracC) { |
274 | 0 | ctx->acceleration.put_hevc_epel_v(out, out_stride, |
275 | 0 | src_ptr, src_stride, |
276 | 0 | nPbWC,nPbHC, xFracC,yFracC, mcbuffer, bit_depth_C); |
277 | 0 | } |
278 | 0 | else { |
279 | 0 | assert(false); // full-pel shifts are handled above |
280 | 0 | } |
281 | 0 | } |
282 | 0 | } Unexecuted instantiation: void mc_chroma<unsigned short, short>(base_context const*, seq_parameter_set const*, int, int, int, int, short*, int, unsigned short const*, long, int, int, int) Unexecuted instantiation: void mc_chroma<unsigned char, short>(base_context const*, seq_parameter_set const*, int, int, int, int, short*, int, unsigned char const*, long, int, int, int) Unexecuted instantiation: void mc_chroma<unsigned short, int>(base_context const*, seq_parameter_set const*, int, int, int, int, int*, int, unsigned short const*, long, int, int, int) Unexecuted instantiation: void mc_chroma<unsigned char, int>(base_context const*, seq_parameter_set const*, int, int, int, int, int*, int, unsigned char const*, long, int, int, int) |
283 | | |
284 | | |
285 | | |
286 | | // Fractional sample interpolation (8.5.3.3.3) and weighted sample prediction |
287 | | // (8.5.3.3.4) for one colour plane. |
288 | | // |
289 | | // inter_t is the type of the intermediate prediction samples predSamplesLX. |
290 | | // The spec keeps them at max(14, BitDepth+2) bits plus the overshoot of the |
291 | | // interpolation filters, which fits into int16_t only up to |
292 | | // MC_MAX_BIT_DEPTH_INT16. Above that, int32_t is used (see acceleration.h). |
293 | | // |
294 | | // refPic[l] is NULL when list l is not used or when its reference picture is |
295 | | // unusable. The caller has already reported the latter; the prediction is then |
296 | | // filled with mid-grey. |
297 | | // |
298 | | // Forced inline: called once per colour plane from a hot loop, and inlining lets |
299 | | // the compiler set up the prediction sample buffer once per PB instead of once |
300 | | // per plane. |
301 | | template <class inter_t> |
302 | | static LIBDE265_ALWAYS_INLINE void generate_inter_prediction_samples_plane(base_context* ctx, |
303 | | const slice_segment_header* shdr, |
304 | | de265_image* img, |
305 | | int cIdx, |
306 | | int xP,int yP, |
307 | | int nCS, int nPbW,int nPbH, |
308 | | const PBMotion* vi, |
309 | | const int predFlag[2], |
310 | | const de265_image* const refPic[2]) |
311 | 0 | { |
312 | 0 | const pic_parameter_set* pps = shdr->pps.get(); |
313 | 0 | const seq_parameter_set* sps = pps->sps.get(); |
314 | |
|
315 | 0 | const int bit_depth = sps->get_bit_depth(cIdx); |
316 | |
|
317 | 0 | const int SubWidthC = (cIdx==0 ? 1 : sps->SubWidthC); |
318 | 0 | const int SubHeightC = (cIdx==0 ? 1 : sps->SubHeightC); |
319 | 0 | const int w = nPbW / SubWidthC; |
320 | 0 | const int h = nPbH / SubHeightC; |
321 | |
|
322 | 0 | void* pixels = img->get_image_plane_at_pos_any_depth(cIdx, xP/SubWidthC, yP/SubHeightC); |
323 | 0 | const ptrdiff_t stride = img->get_image_stride(cIdx); |
324 | | |
325 | | // TODO: must predSamples stride really be nCS or can it be something smaller like nPbW? |
326 | 0 | ALIGNED_16(inter_t) predSamples[2 /* LX */][MAX_CU_SIZE* MAX_CU_SIZE]; |
327 | | |
328 | | |
329 | | // --- fractional sample interpolation (8.5.3.3.3) --- |
330 | |
|
331 | 0 | for (int l=0;l<2;l++) { |
332 | 0 | if (!predFlag[l]) continue; |
333 | | |
334 | 0 | if (!refPic[l]) { |
335 | | // Fill with mid-grey in intermediate precision: (1 << (bit_depth-1)) << shift3. |
336 | 0 | const inter_t fill = inter_t(1) << (bit_depth-1 + std::max(2, 14-bit_depth)); |
337 | |
|
338 | 0 | for (int y=0;y<h;y++) |
339 | 0 | for (int x=0;x<w;x++) |
340 | 0 | predSamples[l][y*nCS+x] = fill; |
341 | |
|
342 | 0 | continue; |
343 | 0 | } |
344 | | |
345 | 0 | if (cIdx==0) { |
346 | 0 | if (img->high_bit_depth(0)) { |
347 | 0 | mc_luma(ctx, sps, vi->mv[l].x, vi->mv[l].y, xP,yP, |
348 | 0 | predSamples[l],nCS, |
349 | 0 | (const uint16_t*)refPic[l]->get_image_plane(0), |
350 | 0 | refPic[l]->get_luma_stride(), nPbW,nPbH, bit_depth); |
351 | 0 | } |
352 | 0 | else { |
353 | 0 | mc_luma(ctx, sps, vi->mv[l].x, vi->mv[l].y, xP,yP, |
354 | 0 | predSamples[l],nCS, |
355 | 0 | (const uint8_t*)refPic[l]->get_image_plane(0), |
356 | 0 | refPic[l]->get_luma_stride(), nPbW,nPbH, bit_depth); |
357 | 0 | } |
358 | 0 | } |
359 | 0 | else { |
360 | 0 | if (img->high_bit_depth(cIdx)) { |
361 | 0 | mc_chroma(ctx, sps, vi->mv[l].x, vi->mv[l].y, xP,yP, |
362 | 0 | predSamples[l],nCS, |
363 | 0 | (const uint16_t*)refPic[l]->get_image_plane(cIdx), |
364 | 0 | refPic[l]->get_chroma_stride(), w,h, bit_depth); |
365 | 0 | } |
366 | 0 | else { |
367 | 0 | mc_chroma(ctx, sps, vi->mv[l].x, vi->mv[l].y, xP,yP, |
368 | 0 | predSamples[l],nCS, |
369 | 0 | (const uint8_t*)refPic[l]->get_image_plane(cIdx), |
370 | 0 | refPic[l]->get_chroma_stride(), w,h, bit_depth); |
371 | 0 | } |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | | |
376 | | // --- weighted sample prediction (8.5.3.3.4) --- |
377 | |
|
378 | 0 | const bool weightedPredFlag = (shdr->slice_type == SLICE_TYPE_P ? |
379 | 0 | pps->weighted_pred_flag : pps->weighted_bipred_flag); |
380 | | |
381 | | // explicit weighted prediction parameters (8.5.3.3.4.3) |
382 | |
|
383 | 0 | const int shift1 = std::max(2, 14-bit_depth); |
384 | 0 | const int log2WD = (cIdx==0 ? shdr->luma_log2_weight_denom : shdr->ChromaLog2WeightDenom) + shift1; |
385 | 0 | const int offsetShift = (cIdx==0 ? sps->WpOffsetBdShiftY : sps->WpOffsetBdShiftC); |
386 | |
|
387 | 0 | auto weight = [&](int l) -> int { |
388 | 0 | const int refIdx = vi->refIdx[l]; |
389 | 0 | return (cIdx==0 ? shdr->LumaWeight[l][refIdx] : shdr->ChromaWeight[l][refIdx][cIdx-1]); |
390 | 0 | }; Unexecuted instantiation: motion.cc:generate_inter_prediction_samples_plane<short>(base_context*, slice_segment_header const*, de265_image*, int, int, int, int, int, int, PBMotion const*, int const*, de265_image const* const*)::{lambda(int)#1}::operator()(int) constUnexecuted instantiation: motion.cc:generate_inter_prediction_samples_plane<int>(base_context*, slice_segment_header const*, de265_image*, int, int, int, int, int, int, PBMotion const*, int const*, de265_image const* const*)::{lambda(int)#1}::operator()(int) const |
391 | |
|
392 | 0 | auto offset = [&](int l) -> int { |
393 | 0 | const int refIdx = vi->refIdx[l]; |
394 | 0 | const int o = (cIdx==0 ? shdr->luma_offset[l][refIdx] : shdr->ChromaOffset[l][refIdx][cIdx-1]); |
395 | 0 | return o * (1<<offsetShift); |
396 | 0 | }; Unexecuted instantiation: motion.cc:generate_inter_prediction_samples_plane<short>(base_context*, slice_segment_header const*, de265_image*, int, int, int, int, int, int, PBMotion const*, int const*, de265_image const* const*)::{lambda(int)#2}::operator()(int) constUnexecuted instantiation: motion.cc:generate_inter_prediction_samples_plane<int>(base_context*, slice_segment_header const*, de265_image*, int, int, int, int, int, int, PBMotion const*, int const*, de265_image const* const*)::{lambda(int)#2}::operator()(int) const |
397 | | |
398 | |
|
399 | 0 | if (predFlag[0] && predFlag[1]) { |
400 | 0 | if (!weightedPredFlag) { |
401 | 0 | ctx->acceleration.put_weighted_pred_avg(pixels, stride, |
402 | 0 | predSamples[0], predSamples[1], nCS, |
403 | 0 | w,h, bit_depth); |
404 | 0 | } |
405 | 0 | else { |
406 | 0 | logtrace(LogMotion,"weighted-BI-0 [%d] %d %d %d %dx%d\n", vi->refIdx[0], log2WD-6,weight(0),offset(0),w,h); |
407 | 0 | logtrace(LogMotion,"weighted-BI-1 [%d] %d %d %d %dx%d\n", vi->refIdx[1], log2WD-6,weight(1),offset(1),w,h); |
408 | |
|
409 | 0 | ctx->acceleration.put_weighted_bipred(pixels, stride, |
410 | 0 | predSamples[0], predSamples[1], nCS, |
411 | 0 | w,h, |
412 | 0 | weight(0),offset(0), |
413 | 0 | weight(1),offset(1), |
414 | 0 | log2WD, bit_depth); |
415 | 0 | } |
416 | 0 | } |
417 | 0 | else { |
418 | 0 | const int l = (predFlag[0] ? 0 : 1); // the caller ensures that one list is used |
419 | |
|
420 | 0 | if (!weightedPredFlag) { |
421 | 0 | ctx->acceleration.put_unweighted_pred(pixels, stride, |
422 | 0 | predSamples[l], nCS, |
423 | 0 | w,h, bit_depth); |
424 | 0 | } |
425 | 0 | else { |
426 | 0 | logtrace(LogMotion,"weighted-L%d [%d] %d %d %d %dx%d\n", l, vi->refIdx[l], log2WD-6,weight(l),offset(l),w,h); |
427 | |
|
428 | 0 | ctx->acceleration.put_weighted_pred(pixels, stride, |
429 | 0 | predSamples[l], nCS, |
430 | 0 | w,h, |
431 | 0 | weight(l),offset(l), |
432 | 0 | log2WD, bit_depth); |
433 | 0 | } |
434 | 0 | } |
435 | 0 | } Unexecuted instantiation: motion.cc:void generate_inter_prediction_samples_plane<short>(base_context*, slice_segment_header const*, de265_image*, int, int, int, int, int, int, PBMotion const*, int const*, de265_image const* const*) Unexecuted instantiation: motion.cc:void generate_inter_prediction_samples_plane<int>(base_context*, slice_segment_header const*, de265_image*, int, int, int, int, int, int, PBMotion const*, int const*, de265_image const* const*) |
436 | | |
437 | | |
438 | | |
439 | | // Decoding process for inter prediction samples (8.5.3.3). |
440 | | // NOTE: for full-pel shifts, we can introduce a fast path, simply copying without shifts |
441 | | void generate_inter_prediction_samples(base_context* ctx, |
442 | | const slice_segment_header* shdr, |
443 | | de265_image* img, |
444 | | int xC,int yC, |
445 | | int xB,int yB, |
446 | | int nCS, int nPbW,int nPbH, |
447 | | const PBMotion* vi) |
448 | 0 | { |
449 | 0 | const int xP = xC+xB; |
450 | 0 | const int yP = yC+yB; |
451 | |
|
452 | 0 | const pic_parameter_set* pps = shdr->pps.get(); |
453 | 0 | const seq_parameter_set* sps = pps->sps.get(); |
454 | |
|
455 | 0 | if (sps->BitDepth_Y != img->get_bit_depth(0) || |
456 | 0 | sps->BitDepth_C != img->get_bit_depth(1)) { |
457 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
458 | 0 | ctx->add_warning(DE265_WARNING_BIT_DEPTH_OF_CURRENT_IMAGE_DOES_NOT_MATCH_SPS, false); |
459 | 0 | return; |
460 | 0 | } |
461 | | |
462 | 0 | if (sps->chroma_format_idc != img->get_chroma_format()) { |
463 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
464 | 0 | ctx->add_warning(DE265_WARNING_CHROMA_OF_CURRENT_IMAGE_DOES_NOT_MATCH_SPS, false); |
465 | 0 | return; |
466 | 0 | } |
467 | | |
468 | | |
469 | 0 | int predFlag[2]; |
470 | 0 | predFlag[0] = vi->predFlag[0]; |
471 | 0 | predFlag[1] = vi->predFlag[1]; |
472 | | |
473 | | // Some encoders use bi-prediction with two identical MVs onto the same picture. |
474 | | // Identify this case and use only one MV. This is not possible with explicit |
475 | | // weighted prediction, where the two lists may have different weights/offsets. |
476 | |
|
477 | 0 | const bool weightedPredFlag = (shdr->slice_type == SLICE_TYPE_P ? |
478 | 0 | pps->weighted_pred_flag : pps->weighted_bipred_flag); |
479 | |
|
480 | 0 | if (!weightedPredFlag) { |
481 | 0 | if (predFlag[0] && predFlag[1]) { |
482 | 0 | if (vi->mv[0].x == vi->mv[1].x && |
483 | 0 | vi->mv[0].y == vi->mv[1].y && |
484 | 0 | shdr->RefPicList[0][vi->refIdx[0]] == |
485 | 0 | shdr->RefPicList[1][vi->refIdx[1]]) { |
486 | 0 | predFlag[1] = 0; |
487 | 0 | } |
488 | 0 | } |
489 | 0 | } |
490 | |
|
491 | 0 | logtrace(LogMotion,"predFlags (modified): %d %d\n", predFlag[0], predFlag[1]); |
492 | |
|
493 | 0 | if (!predFlag[0] && !predFlag[1]) { |
494 | | // TODO: check why it can actually happen that both predFlags[] are false. |
495 | | // For now, we ignore this and continue decoding. |
496 | |
|
497 | 0 | ctx->add_warning(DE265_WARNING_BOTH_PREDFLAGS_ZERO, false); |
498 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
499 | 0 | return; |
500 | 0 | } |
501 | | |
502 | | |
503 | | // --- reference picture selection (8.5.3.3.2) --- |
504 | | |
505 | | // refPic[l] stays NULL when the reference picture cannot be used. |
506 | | |
507 | 0 | const de265_image* refPic[2] = { nullptr, nullptr }; |
508 | |
|
509 | 0 | for (int l=0;l<2;l++) { |
510 | 0 | if (!predFlag[l]) continue; |
511 | | |
512 | 0 | const de265_image* ref = ctx->get_image(shdr->RefPicList[l][vi->refIdx[l]]); |
513 | |
|
514 | 0 | logtrace(LogMotion, "refIdx: %d -> dpb[%d]\n", vi->refIdx[l], shdr->RefPicList[l][vi->refIdx[l]]); |
515 | |
|
516 | 0 | if (!ref || ref->PicState == UnusedForReference) { |
517 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
518 | 0 | ctx->add_warning(DE265_WARNING_NONEXISTING_REFERENCE_PICTURE_ACCESSED, false); |
519 | 0 | } |
520 | 0 | else if (ref->get_width(0) != sps->pic_width_in_luma_samples || |
521 | 0 | ref->get_height(0) != sps->pic_height_in_luma_samples) { |
522 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
523 | 0 | ctx->add_warning(DE265_WARNING_REFERENCE_IMAGE_SIZE_DOES_NOT_MATCH_SPS, false); |
524 | 0 | } |
525 | 0 | else if (img->get_bit_depth(0) != ref->get_bit_depth(0) || |
526 | 0 | img->get_bit_depth(1) != ref->get_bit_depth(1)) { |
527 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
528 | 0 | ctx->add_warning(DE265_WARNING_REFERENCE_IMAGE_BIT_DEPTH_DOES_NOT_MATCH, false); |
529 | 0 | } |
530 | 0 | else if (img->get_chroma_format() != ref->get_chroma_format()) { |
531 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
532 | 0 | ctx->add_warning(DE265_WARNING_REFERENCE_IMAGE_CHROMA_FORMAT_DOES_NOT_MATCH, false); |
533 | 0 | } |
534 | 0 | else { |
535 | 0 | logtrace(LogMotion,"do MC: L%d,MV=%d;%d RefPOC=%d\n", |
536 | 0 | l,vi->mv[l].x,vi->mv[l].y,ref->PicOrderCntVal); |
537 | |
|
538 | 0 | refPic[l] = ref; |
539 | 0 | } |
540 | 0 | } |
541 | | |
542 | | |
543 | | // --- prediction, per colour plane --- |
544 | | |
545 | | // Up to MC_MAX_BIT_DEPTH_INT16, the intermediate prediction samples fit |
546 | | // into int16_t. Above that, int32_t is needed. |
547 | |
|
548 | 0 | const int nPlanes = (img->get_chroma_format() == de265_chroma_mono ? 1 : 3); |
549 | |
|
550 | 0 | for (int cIdx=0;cIdx<nPlanes;cIdx++) { |
551 | 0 | if (sps->get_bit_depth(cIdx) <= MC_MAX_BIT_DEPTH_INT16) { |
552 | 0 | generate_inter_prediction_samples_plane<int16_t>(ctx,shdr,img, cIdx, xP,yP, nCS,nPbW,nPbH, vi, predFlag, refPic); |
553 | 0 | } |
554 | 0 | else { |
555 | 0 | generate_inter_prediction_samples_plane<int32_t>(ctx,shdr,img, cIdx, xP,yP, nCS,nPbW,nPbH, vi, predFlag, refPic); |
556 | 0 | } |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | #ifdef DE265_LOG_TRACE |
561 | | void logmvcand(const PBMotion& p) |
562 | | { |
563 | | for (int v=0;v<2;v++) { |
564 | | if (p.predFlag[v]) { |
565 | | logtrace(LogMotion," %d: %s %d;%d ref=%d\n", v, p.predFlag[v] ? "yes":"no ", |
566 | | p.mv[v].x,p.mv[v].y, p.refIdx[v]); |
567 | | } else { |
568 | | logtrace(LogMotion," %d: %s --;-- ref=--\n", v, p.predFlag[v] ? "yes":"no "); |
569 | | } |
570 | | } |
571 | | } |
572 | | #else |
573 | | #define logmvcand(p) |
574 | | #endif |
575 | | |
576 | | |
577 | | bool PBMotion::operator==(const PBMotion& b) const |
578 | 0 | { |
579 | 0 | const PBMotion& a = *this; |
580 | | |
581 | | // TODO: is this really correct? no check for predFlag? Standard says so... (p.127) |
582 | |
|
583 | 0 | for (int i=0;i<2;i++) { |
584 | 0 | if (a.predFlag[i] != b.predFlag[i]) return false; |
585 | | |
586 | 0 | if (a.predFlag[i]) { |
587 | 0 | if (a.mv[i].x != b.mv[i].x) return false; |
588 | 0 | if (a.mv[i].y != b.mv[i].y) return false; |
589 | 0 | if (a.refIdx[i] != b.refIdx[i]) return false; |
590 | 0 | } |
591 | 0 | } |
592 | | |
593 | 0 | return true; |
594 | 0 | } |
595 | | |
596 | | |
597 | | class MotionVectorAccess_de265_image : public MotionVectorAccess |
598 | | { |
599 | | public: |
600 | 0 | MotionVectorAccess_de265_image(const de265_image* i) : img(i) { } |
601 | | |
602 | 0 | enum PartMode get_PartMode(int x,int y) const override { return img->get_PartMode(x,y); } |
603 | 0 | const PBMotion& get_mv_info(int x,int y) const override { return img->get_mv_info(x,y); } |
604 | | |
605 | | private: |
606 | | const de265_image* img; |
607 | | }; |
608 | | |
609 | | |
610 | | |
611 | | /* |
612 | | +--+ +--+--+ |
613 | | |B2| |B1|B0| |
614 | | +--+----------------+--+--+ |
615 | | | | |
616 | | | | |
617 | | | | |
618 | | | | |
619 | | | PB | |
620 | | | | |
621 | | | | |
622 | | +--+ | |
623 | | |A1| | |
624 | | +--+-------------------+ |
625 | | |A0| |
626 | | +--+ |
627 | | */ |
628 | | |
629 | | |
630 | | // 8.5.3.1.2 |
631 | | // TODO: check: can we fill the candidate list directly in this function and omit to copy later |
632 | | /* |
633 | | xC/yC: CB position |
634 | | nCS: CB size (probably modified because of singleMCLFlag) |
635 | | xP/yP: PB position (absolute) (probably modified because of singleMCLFlag) |
636 | | singleMCLFlag |
637 | | nPbW/nPbH: PB size |
638 | | partIdx |
639 | | out_cand: merging candidate vectors |
640 | | |
641 | | Add these candidates: |
642 | | - A1 |
643 | | - B1 (if != A1) |
644 | | - B0 (if != B1) |
645 | | - A0 (if != A1) |
646 | | - B2 (if != A1 and != B1) |
647 | | |
648 | | A maximum of 4 candidates are generated. |
649 | | |
650 | | Note 1: For a CB split into two PBs, it does not make sense to merge the |
651 | | second part to the parameters of the first part, since then, we could use 2Nx2N |
652 | | right away. -> Exclude this candidate. |
653 | | */ |
654 | | int derive_spatial_merging_candidates(//const de265_image* img, |
655 | | const MotionVectorAccess& mvaccess, |
656 | | const de265_image* img, |
657 | | int xC, int yC, int nCS, int xP, int yP, |
658 | | uint8_t singleMCLFlag, |
659 | | int nPbW, int nPbH, |
660 | | int partIdx, |
661 | | PBMotion* out_cand, |
662 | | int maxCandidates) |
663 | 0 | { |
664 | 0 | const pic_parameter_set* pps = &img->get_pps(); |
665 | 0 | const int log2_parallel_merge_level = pps->log2_parallel_merge_level; |
666 | |
|
667 | 0 | enum PartMode PartMode = mvaccess.get_PartMode(xC,yC); |
668 | | |
669 | | /* |
670 | | const int A0 = SpatialMergingCandidates::PRED_A0; |
671 | | const int A1 = SpatialMergingCandidates::PRED_A1; |
672 | | const int B0 = SpatialMergingCandidates::PRED_B0; |
673 | | const int B1 = SpatialMergingCandidates::PRED_B1; |
674 | | const int B2 = SpatialMergingCandidates::PRED_B2; |
675 | | */ |
676 | | |
677 | | // --- A1 --- |
678 | | |
679 | | // a pixel within A1 (bottom right of A1) |
680 | 0 | int xA1 = xP-1; |
681 | 0 | int yA1 = yP+nPbH-1; |
682 | |
|
683 | 0 | bool availableA1; |
684 | 0 | int idxA1; |
685 | |
|
686 | 0 | int computed_candidates = 0; |
687 | | |
688 | | // check if candidate is in same motion-estimation region (MER) -> discard |
689 | 0 | if ((xP>>log2_parallel_merge_level) == (xA1>>log2_parallel_merge_level) && |
690 | 0 | (yP>>log2_parallel_merge_level) == (yA1>>log2_parallel_merge_level)) { |
691 | 0 | availableA1 = false; |
692 | 0 | logtrace(LogMotion,"spatial merging candidate A1: below parallel merge level\n"); |
693 | 0 | } |
694 | | // redundant candidate? (Note 1) -> discard |
695 | 0 | else if (// !singleMCLFlag && automatically true when partIdx==1 |
696 | 0 | partIdx==1 && |
697 | 0 | (PartMode==PART_Nx2N || |
698 | 0 | PartMode==PART_nLx2N || |
699 | 0 | PartMode==PART_nRx2N)) { |
700 | 0 | availableA1 = false; |
701 | 0 | logtrace(LogMotion,"spatial merging candidate A1: second part ignore\n"); |
702 | 0 | } |
703 | | // MV available in A1 |
704 | 0 | else { |
705 | 0 | availableA1 = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xA1,yA1); |
706 | 0 | if (!availableA1) logtrace(LogMotion,"spatial merging candidate A1: unavailable\n"); |
707 | 0 | } |
708 | |
|
709 | 0 | if (availableA1) { |
710 | 0 | idxA1 = computed_candidates++; |
711 | 0 | out_cand[idxA1] = mvaccess.get_mv_info(xA1,yA1); |
712 | |
|
713 | 0 | logtrace(LogMotion,"spatial merging candidate A1:\n"); |
714 | 0 | logmvcand(out_cand[idxA1]); |
715 | 0 | } |
716 | |
|
717 | 0 | if (computed_candidates>=maxCandidates) return computed_candidates; |
718 | | |
719 | | |
720 | | // --- B1 --- |
721 | | |
722 | 0 | int xB1 = xP+nPbW-1; |
723 | 0 | int yB1 = yP-1; |
724 | |
|
725 | 0 | bool availableB1; |
726 | 0 | int idxB1; |
727 | | |
728 | | // same MER -> discard |
729 | 0 | if ((xP>>log2_parallel_merge_level) == (xB1>>log2_parallel_merge_level) && |
730 | 0 | (yP>>log2_parallel_merge_level) == (yB1>>log2_parallel_merge_level)) { |
731 | 0 | availableB1 = false; |
732 | 0 | logtrace(LogMotion,"spatial merging candidate B1: below parallel merge level\n"); |
733 | 0 | } |
734 | | // redundant candidate (Note 1) -> discard |
735 | 0 | else if (// !singleMCLFlag && automatically true when partIdx==1 |
736 | 0 | partIdx==1 && |
737 | 0 | (PartMode==PART_2NxN || |
738 | 0 | PartMode==PART_2NxnU || |
739 | 0 | PartMode==PART_2NxnD)) { |
740 | 0 | availableB1 = false; |
741 | 0 | logtrace(LogMotion,"spatial merging candidate B1: second part ignore\n"); |
742 | 0 | } |
743 | | // MV available in B1 |
744 | 0 | else { |
745 | 0 | availableB1 = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xB1,yB1); |
746 | 0 | if (!availableB1) logtrace(LogMotion,"spatial merging candidate B1: unavailable\n"); |
747 | 0 | } |
748 | |
|
749 | 0 | if (availableB1) { |
750 | 0 | const PBMotion& b1 = img->get_mv_info(xB1,yB1); |
751 | | |
752 | | // B1 == A1 -> discard B1 |
753 | 0 | if (availableA1 && out_cand[idxA1] == b1) { |
754 | 0 | idxB1 = idxA1; |
755 | 0 | logtrace(LogMotion,"spatial merging candidate B1: redundant to A1\n"); |
756 | 0 | } |
757 | 0 | else { |
758 | 0 | idxB1 = computed_candidates++; |
759 | 0 | out_cand[idxB1] = b1; |
760 | |
|
761 | 0 | logtrace(LogMotion,"spatial merging candidate B1:\n"); |
762 | 0 | logmvcand(out_cand[idxB1]); |
763 | 0 | } |
764 | 0 | } |
765 | |
|
766 | 0 | if (computed_candidates>=maxCandidates) return computed_candidates; |
767 | | |
768 | | |
769 | | // --- B0 --- |
770 | | |
771 | 0 | int xB0 = xP+nPbW; |
772 | 0 | int yB0 = yP-1; |
773 | |
|
774 | 0 | bool availableB0; |
775 | 0 | int idxB0; |
776 | |
|
777 | 0 | if ((xP>>log2_parallel_merge_level) == (xB0>>log2_parallel_merge_level) && |
778 | 0 | (yP>>log2_parallel_merge_level) == (yB0>>log2_parallel_merge_level)) { |
779 | 0 | availableB0 = false; |
780 | 0 | logtrace(LogMotion,"spatial merging candidate B0: below parallel merge level\n"); |
781 | 0 | } |
782 | 0 | else { |
783 | 0 | availableB0 = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xB0,yB0); |
784 | 0 | if (!availableB0) logtrace(LogMotion,"spatial merging candidate B0: unavailable\n"); |
785 | 0 | } |
786 | |
|
787 | 0 | if (availableB0) { |
788 | 0 | const PBMotion& b0 = img->get_mv_info(xB0,yB0); |
789 | | |
790 | | // B0 == B1 -> discard B0 |
791 | 0 | if (availableB1 && out_cand[idxB1]==b0) { |
792 | 0 | idxB0 = idxB1; |
793 | 0 | logtrace(LogMotion,"spatial merging candidate B0: redundant to B1\n"); |
794 | 0 | } |
795 | 0 | else { |
796 | 0 | idxB0 = computed_candidates++; |
797 | 0 | out_cand[idxB0] = b0; |
798 | 0 | logtrace(LogMotion,"spatial merging candidate B0:\n"); |
799 | 0 | logmvcand(out_cand[idxB0]); |
800 | 0 | } |
801 | 0 | } |
802 | |
|
803 | 0 | if (computed_candidates>=maxCandidates) return computed_candidates; |
804 | | |
805 | | |
806 | | // --- A0 --- |
807 | | |
808 | 0 | int xA0 = xP-1; |
809 | 0 | int yA0 = yP+nPbH; |
810 | |
|
811 | 0 | bool availableA0; |
812 | 0 | int idxA0; |
813 | |
|
814 | 0 | if ((xP>>log2_parallel_merge_level) == (xA0>>log2_parallel_merge_level) && |
815 | 0 | (yP>>log2_parallel_merge_level) == (yA0>>log2_parallel_merge_level)) { |
816 | 0 | availableA0 = false; |
817 | 0 | logtrace(LogMotion,"spatial merging candidate A0: below parallel merge level\n"); |
818 | 0 | } |
819 | 0 | else { |
820 | 0 | availableA0 = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xA0,yA0); |
821 | 0 | if (!availableA0) logtrace(LogMotion,"spatial merging candidate A0: unavailable\n"); |
822 | 0 | } |
823 | |
|
824 | 0 | if (availableA0) { |
825 | 0 | const PBMotion& a0 = img->get_mv_info(xA0,yA0); |
826 | | |
827 | | // A0 == A1 -> discard A0 |
828 | 0 | if (availableA1 && out_cand[idxA1]==a0) { |
829 | 0 | idxA0 = idxA1; |
830 | 0 | logtrace(LogMotion,"spatial merging candidate A0: redundant to A1\n"); |
831 | 0 | } |
832 | 0 | else { |
833 | 0 | idxA0 = computed_candidates++; |
834 | 0 | out_cand[idxA0] = a0; |
835 | 0 | logtrace(LogMotion,"spatial merging candidate A0:\n"); |
836 | 0 | logmvcand(out_cand[idxA0]); |
837 | 0 | } |
838 | 0 | } |
839 | |
|
840 | 0 | if (computed_candidates>=maxCandidates) return computed_candidates; |
841 | | |
842 | | |
843 | | // --- B2 --- |
844 | | |
845 | 0 | int xB2 = xP-1; |
846 | 0 | int yB2 = yP-1; |
847 | |
|
848 | 0 | bool availableB2; |
849 | 0 | int idxB2; |
850 | | |
851 | | // if we already have four candidates, do not consider B2 anymore |
852 | 0 | if (computed_candidates==4) { |
853 | 0 | availableB2 = false; |
854 | 0 | logtrace(LogMotion,"spatial merging candidate B2: ignore\n"); |
855 | 0 | } |
856 | 0 | else if ((xP>>log2_parallel_merge_level) == (xB2>>log2_parallel_merge_level) && |
857 | 0 | (yP>>log2_parallel_merge_level) == (yB2>>log2_parallel_merge_level)) { |
858 | 0 | availableB2 = false; |
859 | 0 | logtrace(LogMotion,"spatial merging candidate B2: below parallel merge level\n"); |
860 | 0 | } |
861 | 0 | else { |
862 | 0 | availableB2 = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xB2,yB2); |
863 | 0 | if (!availableB2) logtrace(LogMotion,"spatial merging candidate B2: unavailable\n"); |
864 | 0 | } |
865 | |
|
866 | 0 | if (availableB2) { |
867 | 0 | const PBMotion& b2 = img->get_mv_info(xB2,yB2); |
868 | | |
869 | | // B2 == B1 -> discard B2 |
870 | 0 | if (availableB1 && out_cand[idxB1]==b2) { |
871 | 0 | idxB2 = idxB1; |
872 | 0 | logtrace(LogMotion,"spatial merging candidate B2: redundant to B1\n"); |
873 | 0 | } |
874 | | // B2 == A1 -> discard B2 |
875 | 0 | else if (availableA1 && out_cand[idxA1]==b2) { |
876 | 0 | idxB2 = idxA1; |
877 | 0 | logtrace(LogMotion,"spatial merging candidate B2: redundant to A1\n"); |
878 | 0 | } |
879 | 0 | else { |
880 | 0 | idxB2 = computed_candidates++; |
881 | 0 | out_cand[idxB2] = b2; |
882 | 0 | logtrace(LogMotion,"spatial merging candidate B2:\n"); |
883 | 0 | logmvcand(out_cand[idxB2]); |
884 | 0 | } |
885 | 0 | } |
886 | |
|
887 | 0 | return computed_candidates; |
888 | 0 | } |
889 | | |
890 | | |
891 | | // 8.5.3.1.4 |
892 | | void derive_zero_motion_vector_candidates(const slice_segment_header* shdr, |
893 | | PBMotion* out_mergeCandList, |
894 | | int* inout_numCurrMergeCand, |
895 | | int maxCandidates) |
896 | 0 | { |
897 | 0 | logtrace(LogMotion,"derive_zero_motion_vector_candidates\n"); |
898 | |
|
899 | 0 | int numRefIdx; |
900 | |
|
901 | 0 | if (shdr->slice_type==SLICE_TYPE_P) { |
902 | 0 | numRefIdx = shdr->num_ref_idx_l0_active; |
903 | 0 | } |
904 | 0 | else { |
905 | 0 | numRefIdx = std::min(shdr->num_ref_idx_l0_active, |
906 | 0 | shdr->num_ref_idx_l1_active); |
907 | 0 | } |
908 | | |
909 | | |
910 | | //int numInputMergeCand = *inout_numMergeCand; |
911 | 0 | int zeroIdx = 0; |
912 | |
|
913 | 0 | while (*inout_numCurrMergeCand < maxCandidates) { |
914 | | // 1. |
915 | |
|
916 | 0 | logtrace(LogMotion,"zeroIdx:%d numRefIdx:%d\n", zeroIdx, numRefIdx); |
917 | |
|
918 | 0 | PBMotion* newCand = &out_mergeCandList[*inout_numCurrMergeCand]; |
919 | |
|
920 | 0 | const int refIdx = (zeroIdx < numRefIdx) ? zeroIdx : 0; |
921 | |
|
922 | 0 | if (shdr->slice_type==SLICE_TYPE_P) { |
923 | 0 | newCand->refIdx[0] = refIdx; |
924 | 0 | newCand->refIdx[1] = 0; |
925 | 0 | newCand->predFlag[0] = 1; |
926 | 0 | newCand->predFlag[1] = 0; |
927 | 0 | } |
928 | 0 | else { |
929 | 0 | newCand->refIdx[0] = refIdx; |
930 | 0 | newCand->refIdx[1] = refIdx; |
931 | 0 | newCand->predFlag[0] = 1; |
932 | 0 | newCand->predFlag[1] = 1; |
933 | 0 | } |
934 | |
|
935 | 0 | newCand->mv[0].x = 0; |
936 | 0 | newCand->mv[0].y = 0; |
937 | 0 | newCand->mv[1].x = 0; |
938 | 0 | newCand->mv[1].y = 0; |
939 | |
|
940 | 0 | (*inout_numCurrMergeCand)++; |
941 | | |
942 | | // 2. |
943 | |
|
944 | 0 | zeroIdx++; |
945 | 0 | } |
946 | 0 | } |
947 | | |
948 | | |
949 | | bool scale_mv(MotionVector* out_mv, MotionVector mv, int colDist, int currDist) |
950 | 0 | { |
951 | 0 | int td = Clip3(-128,127, colDist); |
952 | 0 | int tb = Clip3(-128,127, currDist); |
953 | |
|
954 | 0 | if (td==0) { |
955 | 0 | *out_mv = mv; |
956 | 0 | return false; |
957 | 0 | } |
958 | 0 | else { |
959 | 0 | int tx = (16384 + (std::abs(td)>>1)) / td; |
960 | 0 | int distScaleFactor = Clip3(-4096,4095, (tb*tx+32)>>6); |
961 | 0 | out_mv->x = Clip3(-32768,32767, |
962 | 0 | Sign(distScaleFactor*mv.x)*((std::abs(distScaleFactor*mv.x)+127)>>8)); |
963 | 0 | out_mv->y = Clip3(-32768,32767, |
964 | 0 | Sign(distScaleFactor*mv.y)*((std::abs(distScaleFactor*mv.y)+127)>>8)); |
965 | 0 | return true; |
966 | 0 | } |
967 | 0 | } |
968 | | |
969 | | |
970 | | // (L1003) 8.5.3.2.8 |
971 | | |
972 | | void derive_collocated_motion_vectors(base_context* ctx, |
973 | | de265_image* img, |
974 | | const slice_segment_header* shdr, |
975 | | int xP,int yP, |
976 | | int colPic, |
977 | | int xColPb,int yColPb, |
978 | | int refIdxLX, // (always 0 for merge mode) |
979 | | int X, |
980 | | MotionVector* out_mvLXCol, |
981 | | uint8_t* out_availableFlagLXCol) |
982 | 0 | { |
983 | 0 | logtrace(LogMotion,"derive_collocated_motion_vectors %d;%d\n",xP,yP); |
984 | | |
985 | | |
986 | | // get collocated image and the prediction mode at the collocated position |
987 | |
|
988 | 0 | assert(ctx->has_image(colPic)); |
989 | 0 | const de265_image* colImg = ctx->get_image(colPic); |
990 | | |
991 | | // check for access outside image area |
992 | |
|
993 | 0 | if (xColPb >= colImg->get_width() || |
994 | 0 | yColPb >= colImg->get_height()) { |
995 | 0 | ctx->add_warning(DE265_WARNING_COLLOCATED_MOTION_VECTOR_OUTSIDE_IMAGE_AREA, false); |
996 | 0 | *out_availableFlagLXCol = 0; |
997 | 0 | return; |
998 | 0 | } |
999 | | |
1000 | 0 | enum PredMode predMode = colImg->get_pred_mode(xColPb,yColPb); |
1001 | | |
1002 | | |
1003 | | // collocated block is Intra -> no collocated MV |
1004 | |
|
1005 | 0 | if (predMode == MODE_INTRA) { |
1006 | 0 | out_mvLXCol->x = 0; |
1007 | 0 | out_mvLXCol->y = 0; |
1008 | 0 | *out_availableFlagLXCol = 0; |
1009 | 0 | return; |
1010 | 0 | } |
1011 | | |
1012 | | |
1013 | 0 | logtrace(LogMotion,"colPic:%d (POC=%d) X:%d refIdxLX:%d refpiclist:%d\n", |
1014 | 0 | colPic, |
1015 | 0 | colImg->PicOrderCntVal, |
1016 | 0 | X,refIdxLX,shdr->RefPicList[X][refIdxLX]); |
1017 | | |
1018 | | |
1019 | | // collocated reference image is unavailable -> no collocated MV |
1020 | |
|
1021 | 0 | if (colImg->integrity == INTEGRITY_UNAVAILABLE_REFERENCE) { |
1022 | 0 | out_mvLXCol->x = 0; |
1023 | 0 | out_mvLXCol->y = 0; |
1024 | 0 | *out_availableFlagLXCol = 0; |
1025 | 0 | return; |
1026 | 0 | } |
1027 | | |
1028 | | |
1029 | | // get the collocated MV |
1030 | | |
1031 | 0 | const PBMotion& mvi = colImg->get_mv_info(xColPb,yColPb); |
1032 | 0 | int listCol; |
1033 | 0 | int refIdxCol; |
1034 | 0 | MotionVector mvCol; |
1035 | |
|
1036 | 0 | logtrace(LogMotion,"read MVI %d;%d:\n",xColPb,yColPb); |
1037 | 0 | logmvcand(mvi); |
1038 | | |
1039 | | |
1040 | | // collocated MV uses only L1 -> use L1 |
1041 | 0 | if (mvi.predFlag[0]==0) { |
1042 | 0 | mvCol = mvi.mv[1]; |
1043 | 0 | refIdxCol = mvi.refIdx[1]; |
1044 | 0 | listCol = 1; |
1045 | 0 | } |
1046 | | // collocated MV uses only L0 -> use L0 |
1047 | 0 | else if (mvi.predFlag[1]==0) { |
1048 | 0 | mvCol = mvi.mv[0]; |
1049 | 0 | refIdxCol = mvi.refIdx[0]; |
1050 | 0 | listCol = 0; |
1051 | 0 | } |
1052 | | // collocated MV uses L0 and L1 |
1053 | 0 | else { |
1054 | 0 | bool allRefFramesBeforeCurrentFrame = true; |
1055 | |
|
1056 | 0 | const int currentPOC = img->PicOrderCntVal; |
1057 | | |
1058 | | // all reference POCs earlier than current POC (list 1) |
1059 | | // Test L1 first, because there is a higher change to find a future reference frame. |
1060 | |
|
1061 | 0 | for (int rIdx=0; rIdx<shdr->num_ref_idx_l1_active && allRefFramesBeforeCurrentFrame; rIdx++) |
1062 | 0 | { |
1063 | 0 | const de265_image* refimg = ctx->get_image(shdr->RefPicList[1][rIdx]); |
1064 | 0 | int refPOC = refimg->PicOrderCntVal; |
1065 | |
|
1066 | 0 | if (refPOC > currentPOC) { |
1067 | 0 | allRefFramesBeforeCurrentFrame = false; |
1068 | 0 | } |
1069 | 0 | } |
1070 | | |
1071 | | // all reference POCs earlier than current POC (list 0) |
1072 | |
|
1073 | 0 | for (int rIdx=0; rIdx<shdr->num_ref_idx_l0_active && allRefFramesBeforeCurrentFrame; rIdx++) |
1074 | 0 | { |
1075 | 0 | const de265_image* refimg = ctx->get_image(shdr->RefPicList[0][rIdx]); |
1076 | 0 | int refPOC = refimg->PicOrderCntVal; |
1077 | |
|
1078 | 0 | if (refPOC > currentPOC) { |
1079 | 0 | allRefFramesBeforeCurrentFrame = false; |
1080 | 0 | } |
1081 | 0 | } |
1082 | | |
1083 | | |
1084 | | /* TODO: What is the rationale behind this ??? |
1085 | | |
1086 | | My guess: |
1087 | | when there are images before the current frame (most probably in L0) and images after |
1088 | | the current frame (most probably in L1), we take the reference in the opposite |
1089 | | direction than where the collocated frame is positioned in the hope that the distance |
1090 | | to the current frame will be smaller and thus give a better prediction. |
1091 | | |
1092 | | If all references point into the past, we cannot say much about the temporal order or |
1093 | | L0,L1 and thus take over both parts. |
1094 | | */ |
1095 | |
|
1096 | 0 | if (allRefFramesBeforeCurrentFrame) { |
1097 | 0 | mvCol = mvi.mv[X]; |
1098 | 0 | refIdxCol = mvi.refIdx[X]; |
1099 | 0 | listCol = X; |
1100 | 0 | } |
1101 | 0 | else { |
1102 | 0 | int N = shdr->collocated_from_l0_flag; |
1103 | 0 | mvCol = mvi.mv[N]; |
1104 | 0 | refIdxCol = mvi.refIdx[N]; |
1105 | 0 | listCol = N; |
1106 | 0 | } |
1107 | 0 | } |
1108 | | |
1109 | | |
1110 | |
|
1111 | 0 | uint16_t slice_hdr_idx = colImg->get_SliceHeaderIndex(xColPb,yColPb); |
1112 | 0 | if (slice_hdr_idx >= colImg->slices.size()) { |
1113 | 0 | ctx->add_warning(DE265_WARNING_INVALID_SLICE_HEADER_INDEX_ACCESS, false); |
1114 | |
|
1115 | 0 | *out_availableFlagLXCol = 0; |
1116 | 0 | out_mvLXCol->x = 0; |
1117 | 0 | out_mvLXCol->y = 0; |
1118 | 0 | return; |
1119 | 0 | } |
1120 | | |
1121 | 0 | const slice_segment_header* colShdr = colImg->slices[ colImg->get_SliceHeaderIndex(xColPb,yColPb) ]; |
1122 | |
|
1123 | 0 | if (shdr->LongTermRefPic[X][refIdxLX] != |
1124 | 0 | colShdr->LongTermRefPic[listCol][refIdxCol]) { |
1125 | 0 | *out_availableFlagLXCol = 0; |
1126 | 0 | out_mvLXCol->x = 0; |
1127 | 0 | out_mvLXCol->y = 0; |
1128 | 0 | } |
1129 | 0 | else { |
1130 | 0 | *out_availableFlagLXCol = 1; |
1131 | |
|
1132 | 0 | const bool isLongTerm = shdr->LongTermRefPic[X][refIdxLX]; |
1133 | |
|
1134 | 0 | int colDist = colImg->PicOrderCntVal - colShdr->RefPicList_POC[listCol][refIdxCol]; |
1135 | 0 | int currDist = img->PicOrderCntVal - shdr->RefPicList_POC[X][refIdxLX]; |
1136 | |
|
1137 | 0 | logtrace(LogMotion,"COLPOCDIFF %d %d [%d %d / %d %d]\n",colDist, currDist, |
1138 | 0 | colImg->PicOrderCntVal, colShdr->RefPicList_POC[listCol][refIdxCol], |
1139 | 0 | img->PicOrderCntVal, shdr->RefPicList_POC[X][refIdxLX] |
1140 | 0 | ); |
1141 | |
|
1142 | 0 | if (isLongTerm || colDist == currDist) { |
1143 | 0 | *out_mvLXCol = mvCol; |
1144 | 0 | } |
1145 | 0 | else { |
1146 | 0 | if (!scale_mv(out_mvLXCol, mvCol, colDist, currDist)) { |
1147 | 0 | ctx->add_warning(DE265_WARNING_INCORRECT_MOTION_VECTOR_SCALING, false); |
1148 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
1149 | 0 | } |
1150 | |
|
1151 | 0 | logtrace(LogMotion,"scale: %d;%d to %d;%d\n", |
1152 | 0 | mvCol.x,mvCol.y, out_mvLXCol->x,out_mvLXCol->y); |
1153 | 0 | } |
1154 | 0 | } |
1155 | 0 | } |
1156 | | |
1157 | | |
1158 | | // 8.5.3.1.7 |
1159 | | void derive_temporal_luma_vector_prediction(base_context* ctx, |
1160 | | de265_image* img, |
1161 | | const slice_segment_header* shdr, |
1162 | | int xP,int yP, |
1163 | | int nPbW,int nPbH, |
1164 | | int refIdxL, |
1165 | | int X, // which MV (L0/L1) to get |
1166 | | MotionVector* out_mvLXCol, |
1167 | | uint8_t* out_availableFlagLXCol) |
1168 | 0 | { |
1169 | | // --- no temporal MVP -> exit --- |
1170 | |
|
1171 | 0 | if (shdr->slice_temporal_mvp_enabled_flag == 0) { |
1172 | 0 | out_mvLXCol->x = 0; |
1173 | 0 | out_mvLXCol->y = 0; |
1174 | 0 | *out_availableFlagLXCol = 0; |
1175 | 0 | return; |
1176 | 0 | } |
1177 | | |
1178 | | |
1179 | | // --- find collocated reference image --- |
1180 | | |
1181 | 0 | int Log2CtbSizeY = img->get_sps().Log2CtbSizeY; |
1182 | |
|
1183 | 0 | int colPic; // TODO: this is the same for the whole slice. We can precompute it. |
1184 | |
|
1185 | 0 | if (shdr->slice_type == SLICE_TYPE_B && |
1186 | 0 | shdr->collocated_from_l0_flag == 0) |
1187 | 0 | { |
1188 | 0 | logtrace(LogMotion,"collocated L1 ref_idx=%d\n",shdr->collocated_ref_idx); |
1189 | |
|
1190 | 0 | colPic = shdr->RefPicList[1][ shdr->collocated_ref_idx ]; |
1191 | 0 | } |
1192 | 0 | else |
1193 | 0 | { |
1194 | 0 | logtrace(LogMotion,"collocated L0 ref_idx=%d\n",shdr->collocated_ref_idx); |
1195 | |
|
1196 | 0 | colPic = shdr->RefPicList[0][ shdr->collocated_ref_idx ]; |
1197 | 0 | } |
1198 | | |
1199 | | |
1200 | | // check whether collocated reference picture exists |
1201 | |
|
1202 | 0 | if (!ctx->has_image(colPic)) { |
1203 | 0 | out_mvLXCol->x = 0; |
1204 | 0 | out_mvLXCol->y = 0; |
1205 | 0 | *out_availableFlagLXCol = 0; |
1206 | |
|
1207 | 0 | ctx->add_warning(DE265_WARNING_NONEXISTING_REFERENCE_PICTURE_ACCESSED, false); |
1208 | 0 | return; |
1209 | 0 | } |
1210 | | |
1211 | | |
1212 | | // --- get collocated MV either at bottom-right corner or from center of PB --- |
1213 | | |
1214 | 0 | int xColPb,yColPb; |
1215 | 0 | int yColBr = yP + nPbH; // bottom right collocated motion vector position |
1216 | 0 | int xColBr = xP + nPbW; |
1217 | | |
1218 | | /* If neighboring pixel at bottom-right corner is in the same CTB-row and inside the image, |
1219 | | use this (reduced down to 16 pixels resolution) as collocated MV position. |
1220 | | |
1221 | | Note: see 2014, Sze, Sect. 5.2.1.2 why candidate C0 is excluded when on another CTB-row. |
1222 | | This is to reduce the memory bandwidth requirements. |
1223 | | */ |
1224 | 0 | if ((yP>>Log2CtbSizeY) == (yColBr>>Log2CtbSizeY) && |
1225 | 0 | xColBr < img->get_sps().pic_width_in_luma_samples && |
1226 | 0 | yColBr < img->get_sps().pic_height_in_luma_samples) |
1227 | 0 | { |
1228 | 0 | xColPb = xColBr & ~0x0F; // reduce resolution of collocated motion-vectors to 16 pixels grid |
1229 | 0 | yColPb = yColBr & ~0x0F; |
1230 | |
|
1231 | 0 | derive_collocated_motion_vectors(ctx,img,shdr, xP,yP, colPic, xColPb,yColPb, refIdxL, X, |
1232 | 0 | out_mvLXCol, out_availableFlagLXCol); |
1233 | 0 | } |
1234 | 0 | else |
1235 | 0 | { |
1236 | 0 | out_mvLXCol->x = 0; |
1237 | 0 | out_mvLXCol->y = 0; |
1238 | 0 | *out_availableFlagLXCol = 0; |
1239 | 0 | } |
1240 | | |
1241 | |
|
1242 | 0 | if (*out_availableFlagLXCol==0) { |
1243 | |
|
1244 | 0 | int xColCtr = xP+(nPbW>>1); |
1245 | 0 | int yColCtr = yP+(nPbH>>1); |
1246 | |
|
1247 | 0 | xColPb = xColCtr & ~0x0F; // reduce resolution of collocated motion-vectors to 16 pixels grid |
1248 | 0 | yColPb = yColCtr & ~0x0F; |
1249 | |
|
1250 | 0 | derive_collocated_motion_vectors(ctx,img,shdr, xP,yP, colPic, xColPb,yColPb, refIdxL, X, |
1251 | 0 | out_mvLXCol, out_availableFlagLXCol); |
1252 | 0 | } |
1253 | 0 | } |
1254 | | |
1255 | | |
1256 | | static int table_8_19[2][12] = { |
1257 | | { 0,1,0,2,1,2,0,3,1,3,2,3 }, |
1258 | | { 1,0,2,0,2,1,3,0,3,1,3,2 } |
1259 | | }; |
1260 | | |
1261 | | // 8.5.3.1.3 |
1262 | | /* Note (TODO): during decoding, we know which of the candidates we will select. |
1263 | | + Hence, we do not really have to generate the other ones... |
1264 | | + */ |
1265 | | void derive_combined_bipredictive_merging_candidates(const base_context* ctx, |
1266 | | const slice_segment_header* shdr, |
1267 | | PBMotion* inout_mergeCandList, |
1268 | | int* inout_numMergeCand, |
1269 | | int maxCandidates) |
1270 | 0 | { |
1271 | 0 | if (*inout_numMergeCand>1 && *inout_numMergeCand < maxCandidates) { |
1272 | 0 | int numOrigMergeCand = *inout_numMergeCand; |
1273 | |
|
1274 | 0 | int numInputMergeCand = *inout_numMergeCand; |
1275 | 0 | int combIdx = 0; |
1276 | 0 | uint8_t combStop = false; |
1277 | |
|
1278 | 0 | while (!combStop) { |
1279 | 0 | int l0CandIdx = table_8_19[0][combIdx]; |
1280 | 0 | int l1CandIdx = table_8_19[1][combIdx]; |
1281 | |
|
1282 | 0 | if (l0CandIdx >= numInputMergeCand || |
1283 | 0 | l1CandIdx >= numInputMergeCand) { |
1284 | 0 | assert(false); // bitstream error -> TODO: conceal error |
1285 | 0 | } |
1286 | | |
1287 | 0 | PBMotion& l0Cand = inout_mergeCandList[l0CandIdx]; |
1288 | 0 | PBMotion& l1Cand = inout_mergeCandList[l1CandIdx]; |
1289 | |
|
1290 | 0 | logtrace(LogMotion,"add bipredictive merging candidate (combIdx:%d)\n",combIdx); |
1291 | 0 | logtrace(LogMotion,"l0Cand:\n"); logmvcand(l0Cand); |
1292 | 0 | logtrace(LogMotion,"l1Cand:\n"); logmvcand(l1Cand); |
1293 | |
|
1294 | 0 | const de265_image* img0 = l0Cand.predFlag[0] ? ctx->get_image(shdr->RefPicList[0][l0Cand.refIdx[0]]) : nullptr; |
1295 | 0 | const de265_image* img1 = l1Cand.predFlag[1] ? ctx->get_image(shdr->RefPicList[1][l1Cand.refIdx[1]]) : nullptr; |
1296 | |
|
1297 | 0 | if (l0Cand.predFlag[0] && !img0) { |
1298 | 0 | return; // TODO error |
1299 | 0 | } |
1300 | | |
1301 | 0 | if (l1Cand.predFlag[1] && !img1) { |
1302 | 0 | return; // TODO error |
1303 | 0 | } |
1304 | | |
1305 | 0 | if (l0Cand.predFlag[0] && l1Cand.predFlag[1] && |
1306 | 0 | (img0->PicOrderCntVal != img1->PicOrderCntVal || |
1307 | 0 | l0Cand.mv[0].x != l1Cand.mv[1].x || |
1308 | 0 | l0Cand.mv[0].y != l1Cand.mv[1].y)) { |
1309 | 0 | PBMotion& p = inout_mergeCandList[ *inout_numMergeCand ]; |
1310 | 0 | p.refIdx[0] = l0Cand.refIdx[0]; |
1311 | 0 | p.refIdx[1] = l1Cand.refIdx[1]; |
1312 | 0 | p.predFlag[0] = l0Cand.predFlag[0]; |
1313 | 0 | p.predFlag[1] = l1Cand.predFlag[1]; |
1314 | 0 | p.mv[0] = l0Cand.mv[0]; |
1315 | 0 | p.mv[1] = l1Cand.mv[1]; |
1316 | 0 | (*inout_numMergeCand)++; |
1317 | |
|
1318 | 0 | logtrace(LogMotion,"result:\n"); |
1319 | 0 | logmvcand(p); |
1320 | 0 | } |
1321 | |
|
1322 | 0 | combIdx++; |
1323 | 0 | if (combIdx == numOrigMergeCand*(numOrigMergeCand-1) || |
1324 | 0 | *inout_numMergeCand == maxCandidates) { |
1325 | 0 | combStop = true; |
1326 | 0 | } |
1327 | 0 | } |
1328 | 0 | } |
1329 | 0 | } |
1330 | | |
1331 | | |
1332 | | // 8.5.3.1.1 |
1333 | | |
1334 | | void get_merge_candidate_list_without_step_9(base_context* ctx, |
1335 | | const slice_segment_header* shdr, |
1336 | | const MotionVectorAccess& mvaccess, |
1337 | | de265_image* img, |
1338 | | int xC,int yC, int xP,int yP, |
1339 | | int nCS, int nPbW,int nPbH, int partIdx, |
1340 | | int max_merge_idx, |
1341 | | PBMotion* mergeCandList) |
1342 | 0 | { |
1343 | | |
1344 | | //int xOrigP = xP; |
1345 | | //int yOrigP = yP; |
1346 | | //int nOrigPbW = nPbW; |
1347 | | //int nOrigPbH = nPbH; |
1348 | |
|
1349 | 0 | int singleMCLFlag; // single merge-candidate-list (MCL) flag |
1350 | | |
1351 | | /* Use single MCL for CBs of size 8x8, except when parallel-merge-level is at 4x4. |
1352 | | Without this flag, PBs smaller than 8x8 would not receive as much merging candidates. |
1353 | | Having additional candidates might have these advantages: |
1354 | | - coding MVs for these small PBs is expensive, and |
1355 | | - since the PBs are not far away from a proper (neighboring) merging candidate, |
1356 | | the quality of the candidates will still be good. |
1357 | | */ |
1358 | 0 | singleMCLFlag = (img->get_pps().log2_parallel_merge_level > 2 && nCS==8); |
1359 | |
|
1360 | 0 | if (singleMCLFlag) { |
1361 | 0 | xP=xC; |
1362 | 0 | yP=yC; |
1363 | 0 | nPbW=nCS; |
1364 | 0 | nPbH=nCS; |
1365 | 0 | partIdx=0; |
1366 | 0 | } |
1367 | |
|
1368 | 0 | int maxCandidates = max_merge_idx+1; |
1369 | | //MotionVectorSpec mergeCandList[5]; |
1370 | 0 | int numMergeCand=0; |
1371 | | |
1372 | | // --- spatial merge candidates |
1373 | |
|
1374 | 0 | numMergeCand = derive_spatial_merging_candidates(mvaccess, |
1375 | 0 | img, xC,yC, nCS, xP,yP, singleMCLFlag, |
1376 | 0 | nPbW,nPbH,partIdx, mergeCandList, |
1377 | 0 | maxCandidates); |
1378 | | |
1379 | | // --- collocated merge candidate |
1380 | 0 | if (numMergeCand < maxCandidates) { |
1381 | 0 | int refIdxCol[2] = { 0,0 }; |
1382 | |
|
1383 | 0 | MotionVector mvCol[2]; |
1384 | 0 | uint8_t predFlagLCol[2]; |
1385 | 0 | derive_temporal_luma_vector_prediction(ctx,img,shdr, xP,yP,nPbW,nPbH, |
1386 | 0 | refIdxCol[0],0, &mvCol[0], |
1387 | 0 | &predFlagLCol[0]); |
1388 | |
|
1389 | 0 | uint8_t availableFlagCol = predFlagLCol[0]; |
1390 | 0 | predFlagLCol[1] = 0; |
1391 | |
|
1392 | 0 | if (shdr->slice_type == SLICE_TYPE_B) { |
1393 | 0 | derive_temporal_luma_vector_prediction(ctx,img,shdr, |
1394 | 0 | xP,yP,nPbW,nPbH, refIdxCol[1],1, &mvCol[1], |
1395 | 0 | &predFlagLCol[1]); |
1396 | 0 | availableFlagCol |= predFlagLCol[1]; |
1397 | 0 | } |
1398 | | |
1399 | |
|
1400 | 0 | if (availableFlagCol) { |
1401 | 0 | PBMotion* colVec = &mergeCandList[numMergeCand++]; |
1402 | |
|
1403 | 0 | colVec->mv[0] = mvCol[0]; |
1404 | 0 | colVec->mv[1] = mvCol[1]; |
1405 | 0 | colVec->predFlag[0] = predFlagLCol[0]; |
1406 | 0 | colVec->predFlag[1] = predFlagLCol[1]; |
1407 | 0 | colVec->refIdx[0] = refIdxCol[0]; |
1408 | 0 | colVec->refIdx[1] = refIdxCol[1]; |
1409 | 0 | } |
1410 | 0 | } |
1411 | | |
1412 | | |
1413 | | // --- bipredictive merge candidates --- |
1414 | |
|
1415 | 0 | if (shdr->slice_type == SLICE_TYPE_B) { |
1416 | 0 | derive_combined_bipredictive_merging_candidates(ctx, shdr, |
1417 | 0 | mergeCandList, &numMergeCand, maxCandidates); |
1418 | 0 | } |
1419 | | |
1420 | | |
1421 | | // --- zero-vector merge candidates --- |
1422 | |
|
1423 | 0 | derive_zero_motion_vector_candidates(shdr, mergeCandList, &numMergeCand, maxCandidates); |
1424 | | |
1425 | |
|
1426 | 0 | logtrace(LogMotion,"mergeCandList:\n"); |
1427 | 0 | for (int i=0;i<shdr->MaxNumMergeCand;i++) |
1428 | 0 | { |
1429 | | //logtrace(LogMotion, " %d:%s\n", i, i==merge_idx ? " SELECTED":""); |
1430 | 0 | logmvcand(mergeCandList[i]); |
1431 | 0 | } |
1432 | 0 | } |
1433 | | |
1434 | | |
1435 | | |
1436 | | void get_merge_candidate_list(base_context* ctx, |
1437 | | const slice_segment_header* shdr, |
1438 | | de265_image* img, |
1439 | | int xC,int yC, int xP,int yP, |
1440 | | int nCS, int nPbW,int nPbH, int partIdx, |
1441 | | PBMotion* mergeCandList) |
1442 | 0 | { |
1443 | 0 | int max_merge_idx = 5-shdr->five_minus_max_num_merge_cand -1; |
1444 | |
|
1445 | 0 | get_merge_candidate_list_without_step_9(ctx, shdr, |
1446 | 0 | MotionVectorAccess_de265_image(img), img, |
1447 | 0 | xC,yC,xP,yP,nCS,nPbW,nPbH, partIdx, |
1448 | 0 | max_merge_idx, mergeCandList); |
1449 | | |
1450 | | // 9. for encoder: modify all merge candidates |
1451 | |
|
1452 | 0 | for (int i=0;i<=max_merge_idx;i++) { |
1453 | 0 | if (mergeCandList[i].predFlag[0] && |
1454 | 0 | mergeCandList[i].predFlag[1] && |
1455 | 0 | nPbW+nPbH==12) |
1456 | 0 | { |
1457 | 0 | mergeCandList[i].refIdx[1] = 0; |
1458 | 0 | mergeCandList[i].predFlag[1] = 0; |
1459 | 0 | } |
1460 | 0 | } |
1461 | 0 | } |
1462 | | |
1463 | | |
1464 | | void derive_luma_motion_merge_mode(base_context* ctx, |
1465 | | const slice_segment_header* shdr, |
1466 | | de265_image* img, |
1467 | | int xC,int yC, int xP,int yP, |
1468 | | int nCS, int nPbW,int nPbH, int partIdx, |
1469 | | int merge_idx, |
1470 | | PBMotion* out_vi) |
1471 | 0 | { |
1472 | 0 | PBMotion mergeCandList[5]; |
1473 | |
|
1474 | 0 | get_merge_candidate_list_without_step_9(ctx, shdr, |
1475 | 0 | MotionVectorAccess_de265_image(img), img, |
1476 | 0 | xC,yC,xP,yP,nCS,nPbW,nPbH, partIdx, |
1477 | 0 | merge_idx, mergeCandList); |
1478 | | |
1479 | |
|
1480 | 0 | *out_vi = mergeCandList[merge_idx]; |
1481 | | |
1482 | | // 8.5.3.1.1 / 9. |
1483 | |
|
1484 | 0 | if (out_vi->predFlag[0] && out_vi->predFlag[1] && nPbW+nPbH==12) { |
1485 | 0 | out_vi->refIdx[1] = 0; |
1486 | 0 | out_vi->predFlag[1] = 0; |
1487 | 0 | } |
1488 | 0 | } |
1489 | | |
1490 | | |
1491 | | // 8.5.3.1.6 |
1492 | | void derive_spatial_luma_vector_prediction(base_context* ctx, |
1493 | | de265_image* img, |
1494 | | const slice_segment_header* shdr, |
1495 | | int xC,int yC,int nCS,int xP,int yP, |
1496 | | int nPbW,int nPbH, int X, |
1497 | | int refIdxLX, int partIdx, |
1498 | | uint8_t out_availableFlagLXN[2], |
1499 | | MotionVector out_mvLXN[2]) |
1500 | 0 | { |
1501 | 0 | if (refIdxLX >= MAX_NUM_REF_PICS) { |
1502 | 0 | ctx->add_warning(DE265_WARNING_INCORRECT_MOTION_VECTOR_SCALING, false); |
1503 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
1504 | |
|
1505 | 0 | out_availableFlagLXN[0] = false; |
1506 | 0 | out_availableFlagLXN[1] = false; |
1507 | 0 | out_mvLXN[0] = MotionVector(); |
1508 | 0 | out_mvLXN[1] = MotionVector(); |
1509 | 0 | return; |
1510 | 0 | } |
1511 | | |
1512 | 0 | int isScaledFlagLX = 0; |
1513 | |
|
1514 | 0 | const int A=0; |
1515 | 0 | const int B=1; |
1516 | |
|
1517 | 0 | out_availableFlagLXN[A] = 0; |
1518 | 0 | out_availableFlagLXN[B] = 0; |
1519 | | |
1520 | | |
1521 | | // --- A --- |
1522 | | |
1523 | | // 1. |
1524 | |
|
1525 | 0 | int xA[2], yA[2]; |
1526 | 0 | xA[0] = xP-1; |
1527 | 0 | yA[0] = yP + nPbH; |
1528 | 0 | xA[1] = xA[0]; |
1529 | 0 | yA[1] = yA[0]-1; |
1530 | | |
1531 | | // 2. |
1532 | |
|
1533 | 0 | out_availableFlagLXN[A] = 0; |
1534 | 0 | out_mvLXN[A].x = 0; |
1535 | 0 | out_mvLXN[A].y = 0; |
1536 | | |
1537 | | // 3. / 4. |
1538 | |
|
1539 | 0 | bool availableA[2]; |
1540 | 0 | availableA[0] = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xA[0],yA[0]); |
1541 | 0 | availableA[1] = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xA[1],yA[1]); |
1542 | | |
1543 | | // 5. |
1544 | |
|
1545 | 0 | if (availableA[0] || availableA[1]) { |
1546 | 0 | isScaledFlagLX = 1; |
1547 | 0 | } |
1548 | | |
1549 | | // 6. test A0 and A1 (Ak) |
1550 | |
|
1551 | 0 | int refIdxA=-1; |
1552 | | |
1553 | | // the POC we want to reference in this PB |
1554 | 0 | const de265_image* tmpimg = ctx->get_image(shdr->RefPicList[X][ refIdxLX ]); |
1555 | 0 | if (tmpimg==nullptr) { return; } |
1556 | 0 | const int referenced_POC = tmpimg->PicOrderCntVal; |
1557 | |
|
1558 | 0 | for (int k=0;k<=1;k++) { |
1559 | |
|
1560 | 0 | if (availableA[k] && |
1561 | 0 | out_availableFlagLXN[A]==0 && // no A?-predictor so far |
1562 | 0 | img->get_pred_mode(xA[k],yA[k]) != MODE_INTRA) { |
1563 | |
|
1564 | 0 | int Y=1-X; |
1565 | |
|
1566 | 0 | const PBMotion& vi = img->get_mv_info(xA[k],yA[k]); |
1567 | 0 | logtrace(LogMotion,"MVP A%d=\n",k); |
1568 | 0 | logmvcand(vi); |
1569 | |
|
1570 | 0 | const de265_image* imgX = nullptr; |
1571 | 0 | if (vi.predFlag[X]) { |
1572 | 0 | imgX = ctx->get_image(shdr->RefPicList[X][ vi.refIdx[X] ]); |
1573 | 0 | } |
1574 | |
|
1575 | 0 | const de265_image* imgY = nullptr; |
1576 | 0 | if (vi.predFlag[Y]) { |
1577 | 0 | imgY = ctx->get_image(shdr->RefPicList[Y][ vi.refIdx[Y] ]); |
1578 | 0 | } |
1579 | | |
1580 | | // check whether the predictor X is available and references the same POC |
1581 | 0 | if (vi.predFlag[X] && imgX && imgX->PicOrderCntVal == referenced_POC) { |
1582 | |
|
1583 | 0 | logtrace(LogMotion,"take A%d/L%d as A candidate with same POC\n",k,X); |
1584 | |
|
1585 | 0 | out_availableFlagLXN[A]=1; |
1586 | 0 | out_mvLXN[A] = vi.mv[X]; |
1587 | 0 | refIdxA = vi.refIdx[X]; |
1588 | 0 | } |
1589 | | // check whether the other predictor (Y) is available and references the same POC |
1590 | 0 | else if (vi.predFlag[Y] && imgY && imgY->PicOrderCntVal == referenced_POC) { |
1591 | |
|
1592 | 0 | logtrace(LogMotion,"take A%d/L%d as A candidate with same POC\n",k,Y); |
1593 | |
|
1594 | 0 | out_availableFlagLXN[A]=1; |
1595 | 0 | out_mvLXN[A] = vi.mv[Y]; |
1596 | 0 | refIdxA = vi.refIdx[Y]; |
1597 | 0 | } |
1598 | 0 | } |
1599 | 0 | } |
1600 | | |
1601 | | // 7. If there is no predictor referencing the same POC, we take any other reference as |
1602 | | // long as it is the same type of reference (long-term / short-term) |
1603 | |
|
1604 | 0 | for (int k=0 ; k<=1 && out_availableFlagLXN[A]==0 ; k++) { |
1605 | 0 | int refPicList=-1; |
1606 | |
|
1607 | 0 | if (availableA[k] && |
1608 | | // TODO: we could remove this call by storing the result of the similar computation above |
1609 | 0 | img->get_pred_mode(xA[k],yA[k]) != MODE_INTRA) { |
1610 | |
|
1611 | 0 | int Y=1-X; |
1612 | |
|
1613 | 0 | const PBMotion& vi = img->get_mv_info(xA[k],yA[k]); |
1614 | 0 | if (vi.predFlag[X]==1 && |
1615 | 0 | shdr->LongTermRefPic[X][refIdxLX] == shdr->LongTermRefPic[X][ vi.refIdx[X] ]) { |
1616 | |
|
1617 | 0 | logtrace(LogMotion,"take A%D/L%d as A candidate with different POCs\n",k,X); |
1618 | |
|
1619 | 0 | out_availableFlagLXN[A]=1; |
1620 | 0 | out_mvLXN[A] = vi.mv[X]; |
1621 | 0 | refIdxA = vi.refIdx[X]; |
1622 | 0 | refPicList = X; |
1623 | 0 | } |
1624 | 0 | else if (vi.predFlag[Y]==1 && |
1625 | 0 | shdr->LongTermRefPic[X][refIdxLX] == shdr->LongTermRefPic[Y][ vi.refIdx[Y] ]) { |
1626 | |
|
1627 | 0 | logtrace(LogMotion,"take A%d/L%d as A candidate with different POCs\n",k,Y); |
1628 | |
|
1629 | 0 | out_availableFlagLXN[A]=1; |
1630 | 0 | out_mvLXN[A] = vi.mv[Y]; |
1631 | 0 | refIdxA = vi.refIdx[Y]; |
1632 | 0 | refPicList = Y; |
1633 | 0 | } |
1634 | 0 | } |
1635 | |
|
1636 | 0 | if (out_availableFlagLXN[A]==1) { |
1637 | 0 | if (refIdxA<0) { |
1638 | 0 | out_availableFlagLXN[0] = out_availableFlagLXN[1] = false; |
1639 | 0 | return; // error |
1640 | 0 | } |
1641 | | |
1642 | 0 | assert(refIdxA>=0); |
1643 | 0 | assert(refPicList>=0); |
1644 | | |
1645 | 0 | const de265_image* refPicA = ctx->get_image(shdr->RefPicList[refPicList][refIdxA ]); |
1646 | |
|
1647 | | #ifdef DE265_LOG_TRACE |
1648 | | const de265_image* refPicX = ctx->get_image(shdr->RefPicList[X][refIdxLX]); |
1649 | | #endif |
1650 | | |
1651 | | //int picStateA = shdr->RefPicList_PicState[refPicList][refIdxA ]; |
1652 | | //int picStateX = shdr->RefPicList_PicState[X ][refIdxLX]; |
1653 | |
|
1654 | 0 | int isLongTermA = shdr->LongTermRefPic[refPicList][refIdxA ]; |
1655 | 0 | int isLongTermX = shdr->LongTermRefPic[X ][refIdxLX]; |
1656 | |
|
1657 | 0 | logtrace(LogMotion,"scale MVP A: A-POC:%d X-POC:%d\n", |
1658 | 0 | refPicA->PicOrderCntVal,refPicX->PicOrderCntVal); |
1659 | |
|
1660 | 0 | if (!isLongTermA && !isLongTermX) |
1661 | | /* |
1662 | | if (picStateA == UsedForShortTermReference && |
1663 | | picStateX == UsedForShortTermReference) |
1664 | | */ |
1665 | 0 | { |
1666 | 0 | int distA = img->PicOrderCntVal - refPicA->PicOrderCntVal; |
1667 | 0 | int distX = img->PicOrderCntVal - referenced_POC; |
1668 | |
|
1669 | 0 | if (!scale_mv(&out_mvLXN[A], out_mvLXN[A], distA, distX)) { |
1670 | 0 | ctx->add_warning(DE265_WARNING_INCORRECT_MOTION_VECTOR_SCALING, false); |
1671 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
1672 | 0 | } |
1673 | 0 | } |
1674 | 0 | } |
1675 | 0 | } |
1676 | | |
1677 | | // --- B --- |
1678 | | |
1679 | | // 1. |
1680 | | |
1681 | 0 | int xB[3], yB[3]; |
1682 | 0 | xB[0] = xP+nPbW; |
1683 | 0 | yB[0] = yP-1; |
1684 | 0 | xB[1] = xB[0]-1; |
1685 | 0 | yB[1] = yP-1; |
1686 | 0 | xB[2] = xP-1; |
1687 | 0 | yB[2] = yP-1; |
1688 | | |
1689 | | // 2. |
1690 | |
|
1691 | 0 | out_availableFlagLXN[B] = 0; |
1692 | 0 | out_mvLXN[B].x = 0; |
1693 | 0 | out_mvLXN[B].y = 0; |
1694 | | |
1695 | | // 3. test B0,B1,B2 (Bk) |
1696 | |
|
1697 | 0 | int refIdxB=-1; |
1698 | |
|
1699 | 0 | bool availableB[3]; |
1700 | 0 | for (int k=0;k<3;k++) { |
1701 | 0 | availableB[k] = img->available_pred_blk(xC,yC, nCS, xP,yP, nPbW,nPbH,partIdx, xB[k],yB[k]); |
1702 | |
|
1703 | 0 | if (availableB[k] && out_availableFlagLXN[B]==0) { |
1704 | |
|
1705 | 0 | int Y=1-X; |
1706 | |
|
1707 | 0 | const PBMotion& vi = img->get_mv_info(xB[k],yB[k]); |
1708 | 0 | logtrace(LogMotion,"MVP B%d=\n",k); |
1709 | 0 | logmvcand(vi); |
1710 | |
|
1711 | 0 | const de265_image* imgX = nullptr; |
1712 | 0 | if (vi.predFlag[X]) { |
1713 | 0 | imgX = ctx->get_image(shdr->RefPicList[X][ vi.refIdx[X] ]); |
1714 | 0 | } |
1715 | |
|
1716 | 0 | const de265_image* imgY = nullptr; |
1717 | 0 | if (vi.predFlag[Y]) { |
1718 | 0 | imgY = ctx->get_image(shdr->RefPicList[Y][ vi.refIdx[Y] ]); |
1719 | 0 | } |
1720 | |
|
1721 | 0 | if (vi.predFlag[X] && imgX && imgX->PicOrderCntVal == referenced_POC) { |
1722 | 0 | logtrace(LogMotion,"a) take B%d/L%d as B candidate with same POC\n",k,X); |
1723 | |
|
1724 | 0 | out_availableFlagLXN[B]=1; |
1725 | 0 | out_mvLXN[B] = vi.mv[X]; |
1726 | 0 | refIdxB = vi.refIdx[X]; |
1727 | 0 | } |
1728 | 0 | else if (vi.predFlag[Y] && imgY && imgY->PicOrderCntVal == referenced_POC) { |
1729 | 0 | logtrace(LogMotion,"b) take B%d/L%d as B candidate with same POC\n",k,Y); |
1730 | |
|
1731 | 0 | out_availableFlagLXN[B]=1; |
1732 | 0 | out_mvLXN[B] = vi.mv[Y]; |
1733 | 0 | refIdxB = vi.refIdx[Y]; |
1734 | 0 | } |
1735 | 0 | } |
1736 | 0 | } |
1737 | | |
1738 | | // 4. |
1739 | |
|
1740 | 0 | if (isScaledFlagLX==0 && // no A predictor, |
1741 | 0 | out_availableFlagLXN[B]) // but an unscaled B predictor |
1742 | 0 | { |
1743 | | // use unscaled B predictor as A predictor |
1744 | |
|
1745 | 0 | logtrace(LogMotion,"copy the same-POC B candidate as additional A candidate\n"); |
1746 | |
|
1747 | 0 | out_availableFlagLXN[A]=1; |
1748 | 0 | out_mvLXN[A] = out_mvLXN[B]; |
1749 | 0 | refIdxA = refIdxB; |
1750 | 0 | } |
1751 | | |
1752 | | // 5. |
1753 | | |
1754 | | // If no A predictor, we output the unscaled B as the A predictor (above) |
1755 | | // and also add a scaled B predictor here. |
1756 | | // If there is (probably) an A predictor, no differing-POC B predictor is generated. |
1757 | 0 | if (isScaledFlagLX==0) { |
1758 | 0 | out_availableFlagLXN[B]=0; |
1759 | |
|
1760 | 0 | for (int k=0 ; k<=2 && out_availableFlagLXN[B]==0 ; k++) { |
1761 | 0 | int refPicList=-1; |
1762 | |
|
1763 | 0 | if (availableB[k]) { |
1764 | 0 | int Y=1-X; |
1765 | |
|
1766 | 0 | const PBMotion& vi = img->get_mv_info(xB[k],yB[k]); |
1767 | |
|
1768 | 0 | if (vi.predFlag[X]==1 && |
1769 | 0 | shdr->LongTermRefPic[X][refIdxLX] == shdr->LongTermRefPic[X][ vi.refIdx[X] ]) { |
1770 | 0 | out_availableFlagLXN[B]=1; |
1771 | 0 | out_mvLXN[B] = vi.mv[X]; |
1772 | 0 | refIdxB = vi.refIdx[X]; |
1773 | 0 | refPicList = X; |
1774 | 0 | } |
1775 | 0 | else if (vi.predFlag[Y]==1 && |
1776 | 0 | shdr->LongTermRefPic[X][refIdxLX] == shdr->LongTermRefPic[Y][ vi.refIdx[Y] ]) { |
1777 | 0 | out_availableFlagLXN[B]=1; |
1778 | 0 | out_mvLXN[B] = vi.mv[Y]; |
1779 | 0 | refIdxB = vi.refIdx[Y]; |
1780 | 0 | refPicList = Y; |
1781 | 0 | } |
1782 | 0 | } |
1783 | |
|
1784 | 0 | if (out_availableFlagLXN[B]==1) { |
1785 | 0 | if (refIdxB<0) { |
1786 | 0 | out_availableFlagLXN[0] = out_availableFlagLXN[1] = false; |
1787 | 0 | return; // error |
1788 | 0 | } |
1789 | | |
1790 | 0 | assert(refPicList>=0); |
1791 | 0 | assert(refIdxB>=0); |
1792 | | |
1793 | 0 | const de265_image* refPicB=ctx->get_image(shdr->RefPicList[refPicList][refIdxB ]); |
1794 | 0 | const de265_image* refPicX=ctx->get_image(shdr->RefPicList[X ][refIdxLX]); |
1795 | |
|
1796 | 0 | int isLongTermB = shdr->LongTermRefPic[refPicList][refIdxB ]; |
1797 | 0 | int isLongTermX = shdr->LongTermRefPic[X ][refIdxLX]; |
1798 | |
|
1799 | 0 | if (refPicB==nullptr || refPicX==nullptr) { |
1800 | 0 | img->decctx->add_warning(DE265_WARNING_NONEXISTING_REFERENCE_PICTURE_ACCESSED,false); |
1801 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
1802 | 0 | } |
1803 | 0 | else if (refPicB->PicOrderCntVal != refPicX->PicOrderCntVal && |
1804 | 0 | !isLongTermB && !isLongTermX) { |
1805 | 0 | int distB = img->PicOrderCntVal - refPicB->PicOrderCntVal; |
1806 | 0 | int distX = img->PicOrderCntVal - referenced_POC; |
1807 | |
|
1808 | 0 | logtrace(LogMotion,"scale MVP B: B-POC:%d X-POC:%d\n",refPicB->PicOrderCntVal,refPicX->PicOrderCntVal); |
1809 | |
|
1810 | 0 | if (!scale_mv(&out_mvLXN[B], out_mvLXN[B], distB, distX)) { |
1811 | 0 | ctx->add_warning(DE265_WARNING_INCORRECT_MOTION_VECTOR_SCALING, false); |
1812 | 0 | img->integrity = INTEGRITY_DECODING_ERRORS; |
1813 | 0 | } |
1814 | 0 | } |
1815 | 0 | } |
1816 | 0 | } |
1817 | 0 | } |
1818 | 0 | } |
1819 | | |
1820 | | |
1821 | | // 8.5.3.1.5 |
1822 | | void fill_luma_motion_vector_predictors(base_context* ctx, |
1823 | | const slice_segment_header* shdr, |
1824 | | de265_image* img, |
1825 | | int xC,int yC,int nCS,int xP,int yP, |
1826 | | int nPbW,int nPbH, int l, |
1827 | | int refIdx, int partIdx, |
1828 | | MotionVector out_mvpList[2]) |
1829 | 0 | { |
1830 | | // 8.5.3.1.6: derive two spatial vector predictors A (0) and B (1) |
1831 | |
|
1832 | 0 | uint8_t availableFlagLXN[2]; |
1833 | 0 | MotionVector mvLXN[2]; |
1834 | |
|
1835 | 0 | derive_spatial_luma_vector_prediction(ctx, img, shdr, xC,yC, nCS, xP,yP, |
1836 | 0 | nPbW,nPbH, l, refIdx, partIdx, |
1837 | 0 | availableFlagLXN, mvLXN); |
1838 | | |
1839 | | // 8.5.3.1.7: if we only have one spatial vector or both spatial vectors are the same, |
1840 | | // derive a temporal predictor |
1841 | |
|
1842 | 0 | uint8_t availableFlagLXCol; |
1843 | 0 | MotionVector mvLXCol; |
1844 | | |
1845 | |
|
1846 | 0 | if (availableFlagLXN[0] && |
1847 | 0 | availableFlagLXN[1] && |
1848 | 0 | (mvLXN[0].x != mvLXN[1].x || mvLXN[0].y != mvLXN[1].y)) { |
1849 | 0 | availableFlagLXCol = 0; |
1850 | 0 | } |
1851 | 0 | else { |
1852 | 0 | derive_temporal_luma_vector_prediction(ctx, img, shdr, |
1853 | 0 | xP,yP, nPbW,nPbH, refIdx,l, |
1854 | 0 | &mvLXCol, &availableFlagLXCol); |
1855 | 0 | } |
1856 | | |
1857 | | |
1858 | | // --- build candidate vector list with exactly two entries --- |
1859 | |
|
1860 | 0 | int numMVPCandLX=0; |
1861 | | |
1862 | | // spatial predictor A |
1863 | |
|
1864 | 0 | if (availableFlagLXN[0]) |
1865 | 0 | { |
1866 | 0 | out_mvpList[numMVPCandLX++] = mvLXN[0]; |
1867 | 0 | } |
1868 | | |
1869 | | // spatial predictor B (if not same as A) |
1870 | |
|
1871 | 0 | if (availableFlagLXN[1] && |
1872 | 0 | (!availableFlagLXN[0] || // in case A in not available, but mvLXA initialized to same as mvLXB |
1873 | 0 | (mvLXN[0].x != mvLXN[1].x || mvLXN[0].y != mvLXN[1].y))) |
1874 | 0 | { |
1875 | 0 | out_mvpList[numMVPCandLX++] = mvLXN[1]; |
1876 | 0 | } |
1877 | | |
1878 | | // temporal predictor |
1879 | |
|
1880 | 0 | if (availableFlagLXCol) |
1881 | 0 | { |
1882 | 0 | out_mvpList[numMVPCandLX++] = mvLXCol; |
1883 | 0 | } |
1884 | | |
1885 | | // fill with zero predictors |
1886 | |
|
1887 | 0 | while (numMVPCandLX<2) { |
1888 | 0 | out_mvpList[numMVPCandLX].x = 0; |
1889 | 0 | out_mvpList[numMVPCandLX].y = 0; |
1890 | 0 | numMVPCandLX++; |
1891 | 0 | } |
1892 | | |
1893 | |
|
1894 | 0 | assert(numMVPCandLX==2); |
1895 | 0 | } |
1896 | | |
1897 | | |
1898 | | MotionVector luma_motion_vector_prediction(base_context* ctx, |
1899 | | const slice_segment_header* shdr, |
1900 | | de265_image* img, |
1901 | | const PBMotionCoding& motion, |
1902 | | int xC,int yC,int nCS,int xP,int yP, |
1903 | | int nPbW,int nPbH, int l, |
1904 | | int refIdx, int partIdx) |
1905 | 0 | { |
1906 | 0 | MotionVector mvpList[2]; |
1907 | |
|
1908 | 0 | fill_luma_motion_vector_predictors(ctx, shdr, img, |
1909 | 0 | xC,yC,nCS,xP,yP, |
1910 | 0 | nPbW, nPbH, l, refIdx, partIdx, |
1911 | 0 | mvpList); |
1912 | | |
1913 | | // select predictor according to mvp_lX_flag |
1914 | |
|
1915 | 0 | return mvpList[ l ? motion.mvp_l1_flag : motion.mvp_l0_flag ]; |
1916 | 0 | } |
1917 | | |
1918 | | |
1919 | | #if DE265_LOG_TRACE |
1920 | | void logMV(int x0,int y0,int nPbW,int nPbH, const char* mode,const PBMotion* mv) |
1921 | | { |
1922 | | int pred0 = mv->predFlag[0]; |
1923 | | int pred1 = mv->predFlag[1]; |
1924 | | |
1925 | | logtrace(LogMotion, |
1926 | | "*MV %d;%d [%d;%d] %s: (%d) %d;%d @%d (%d) %d;%d @%d\n", x0,y0,nPbW,nPbH,mode, |
1927 | | pred0, |
1928 | | pred0 ? mv->mv[0].x : 0,pred0 ? mv->mv[0].y : 0, pred0 ? mv->refIdx[0] : 0, |
1929 | | pred1, |
1930 | | pred1 ? mv->mv[1].x : 0,pred1 ? mv->mv[1].y : 0, pred1 ? mv->refIdx[1] : 0); |
1931 | | } |
1932 | | #else |
1933 | | #define logMV(x0,y0,nPbW,nPbH,mode,mv) |
1934 | | #endif |
1935 | | |
1936 | | |
1937 | | |
1938 | | // 8.5.3.1 |
1939 | | void motion_vectors_and_ref_indices(base_context* ctx, |
1940 | | const slice_segment_header* shdr, |
1941 | | de265_image* img, |
1942 | | const PBMotionCoding& motion, |
1943 | | int xC,int yC, int xB,int yB, int nCS, int nPbW,int nPbH, |
1944 | | int partIdx, |
1945 | | PBMotion* out_vi) |
1946 | 0 | { |
1947 | | //slice_segment_header* shdr = tctx->shdr; |
1948 | |
|
1949 | 0 | int xP = xC+xB; |
1950 | 0 | int yP = yC+yB; |
1951 | |
|
1952 | 0 | enum PredMode predMode = img->get_pred_mode(xC,yC); |
1953 | |
|
1954 | 0 | if (predMode == MODE_SKIP || |
1955 | 0 | (predMode == MODE_INTER && motion.merge_flag)) |
1956 | 0 | { |
1957 | 0 | derive_luma_motion_merge_mode(ctx,shdr,img, |
1958 | 0 | xC,yC, xP,yP, nCS,nPbW,nPbH, partIdx, |
1959 | 0 | motion.merge_idx, out_vi); |
1960 | |
|
1961 | 0 | logMV(xP,yP,nPbW,nPbH, "merge_mode", out_vi); |
1962 | 0 | } |
1963 | 0 | else { |
1964 | 0 | int mvdL[2][2]; |
1965 | 0 | MotionVector mvpL[2]; |
1966 | |
|
1967 | 0 | for (int l=0;l<2;l++) { |
1968 | | // 1. |
1969 | |
|
1970 | 0 | enum InterPredIdc inter_pred_idc = (enum InterPredIdc)motion.inter_pred_idc; |
1971 | |
|
1972 | 0 | if (inter_pred_idc == PRED_BI || |
1973 | 0 | (inter_pred_idc == PRED_L0 && l==0) || |
1974 | 0 | (inter_pred_idc == PRED_L1 && l==1)) { |
1975 | 0 | out_vi->refIdx[l] = motion.refIdx[l]; |
1976 | 0 | out_vi->predFlag[l] = 1; |
1977 | 0 | } |
1978 | 0 | else { |
1979 | 0 | out_vi->refIdx[l] = 0; |
1980 | 0 | out_vi->predFlag[l] = 0; |
1981 | 0 | } |
1982 | | |
1983 | | // 2. |
1984 | |
|
1985 | 0 | mvdL[l][0] = motion.mvd[l][0]; |
1986 | 0 | mvdL[l][1] = motion.mvd[l][1]; |
1987 | | |
1988 | |
|
1989 | 0 | if (out_vi->predFlag[l]) { |
1990 | | // 3. |
1991 | |
|
1992 | 0 | mvpL[l] = luma_motion_vector_prediction(ctx,shdr,img,motion, |
1993 | 0 | xC,yC,nCS,xP,yP, nPbW,nPbH, l, |
1994 | 0 | out_vi->refIdx[l], partIdx); |
1995 | | |
1996 | | // 4. |
1997 | |
|
1998 | 0 | int32_t x = (mvpL[l].x + mvdL[l][0] + 0x10000) & 0xFFFF; |
1999 | 0 | int32_t y = (mvpL[l].y + mvdL[l][1] + 0x10000) & 0xFFFF; |
2000 | |
|
2001 | 0 | out_vi->mv[l].x = (x>=0x8000) ? x-0x10000 : x; |
2002 | 0 | out_vi->mv[l].y = (y>=0x8000) ? y-0x10000 : y; |
2003 | 0 | } |
2004 | 0 | } |
2005 | |
|
2006 | 0 | logMV(xP,yP,nPbW,nPbH, "mvp", out_vi); |
2007 | 0 | } |
2008 | 0 | } |
2009 | | |
2010 | | |
2011 | | // 8.5.3 |
2012 | | |
2013 | | /* xC/yC : CB position |
2014 | | xB/yB : position offset of the PB |
2015 | | nPbW/nPbH : size of PB |
2016 | | nCS : CB size |
2017 | | */ |
2018 | | void decode_prediction_unit(base_context* ctx, |
2019 | | const slice_segment_header* shdr, |
2020 | | de265_image* img, |
2021 | | const PBMotionCoding& motion, |
2022 | | int xC,int yC, int xB,int yB, int nCS, int nPbW,int nPbH, int partIdx) |
2023 | 0 | { |
2024 | 0 | logtrace(LogMotion,"decode_prediction_unit POC=%d %d;%d %dx%d\n", |
2025 | 0 | img->PicOrderCntVal, xC+xB,yC+yB, nPbW,nPbH); |
2026 | | |
2027 | | //slice_segment_header* shdr = tctx->shdr; |
2028 | | |
2029 | | // 1. |
2030 | |
|
2031 | 0 | PBMotion vi; |
2032 | 0 | motion_vectors_and_ref_indices(ctx, shdr, img, motion, |
2033 | 0 | xC,yC, xB,yB, nCS, nPbW,nPbH, partIdx, &vi); |
2034 | | |
2035 | | // 2. |
2036 | |
|
2037 | 0 | generate_inter_prediction_samples(ctx,shdr, img, xC,yC, xB,yB, nCS, nPbW,nPbH, &vi); |
2038 | | |
2039 | |
|
2040 | 0 | img->set_mv_info(xC+xB,yC+yB,nPbW,nPbH, vi); |
2041 | 0 | } |