1 /* 2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #include <assert.h> 13 #include <limits.h> 14 #include <math.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 19 #include "config/aom_config.h" 20 21 #include "aom_dsp/aom_dsp_common.h" 22 #include "aom_ports/mem.h" 23 #include "aom_scale/aom_scale.h" 24 #include "av1/common/common.h" 25 #include "av1/common/resize.h" 26 27 #include "config/aom_scale_rtcd.h" 28 29 // Filters for interpolation (0.5-band) - note this also filters integer pels. 30 static const InterpKernel filteredinterp_filters500[(1 << RS_SUBPEL_BITS)] = { 31 { -3, 0, 35, 64, 35, 0, -3, 0 }, { -3, 0, 34, 64, 36, 0, -3, 0 }, 32 { -3, -1, 34, 64, 36, 1, -3, 0 }, { -3, -1, 33, 64, 37, 1, -3, 0 }, 33 { -3, -1, 32, 64, 38, 1, -3, 0 }, { -3, -1, 31, 64, 39, 1, -3, 0 }, 34 { -3, -1, 31, 63, 39, 2, -3, 0 }, { -2, -2, 30, 63, 40, 2, -3, 0 }, 35 { -2, -2, 29, 63, 41, 2, -3, 0 }, { -2, -2, 29, 63, 41, 3, -4, 0 }, 36 { -2, -2, 28, 63, 42, 3, -4, 0 }, { -2, -2, 27, 63, 43, 3, -4, 0 }, 37 { -2, -3, 27, 63, 43, 4, -4, 0 }, { -2, -3, 26, 62, 44, 5, -4, 0 }, 38 { -2, -3, 25, 62, 45, 5, -4, 0 }, { -2, -3, 25, 62, 45, 5, -4, 0 }, 39 { -2, -3, 24, 62, 46, 5, -4, 0 }, { -2, -3, 23, 61, 47, 6, -4, 0 }, 40 { -2, -3, 23, 61, 47, 6, -4, 0 }, { -2, -3, 22, 61, 48, 7, -4, -1 }, 41 { -2, -3, 21, 60, 49, 7, -4, 0 }, { -1, -4, 20, 60, 49, 8, -4, 0 }, 42 { -1, -4, 20, 60, 50, 8, -4, -1 }, { -1, -4, 19, 59, 51, 9, -4, -1 }, 43 { -1, -4, 19, 59, 51, 9, -4, -1 }, { -1, -4, 18, 58, 52, 10, -4, -1 }, 44 { -1, -4, 17, 58, 52, 11, -4, -1 }, { -1, -4, 16, 58, 53, 11, -4, -1 }, 45 { -1, -4, 16, 57, 53, 12, -4, -1 }, { -1, -4, 15, 57, 54, 12, -4, -1 }, 46 { -1, -4, 15, 56, 54, 13, -4, -1 }, { -1, -4, 14, 56, 55, 13, -4, -1 }, 47 { -1, -4, 14, 55, 55, 14, -4, -1 }, { -1, -4, 13, 55, 56, 14, -4, -1 }, 48 { -1, -4, 13, 54, 56, 15, -4, -1 }, { -1, -4, 12, 54, 57, 15, -4, -1 }, 49 { -1, -4, 12, 53, 57, 16, -4, -1 }, { -1, -4, 11, 53, 58, 16, -4, -1 }, 50 { -1, -4, 11, 52, 58, 17, -4, -1 }, { -1, -4, 10, 52, 58, 18, -4, -1 }, 51 { -1, -4, 9, 51, 59, 19, -4, -1 }, { -1, -4, 9, 51, 59, 19, -4, -1 }, 52 { -1, -4, 8, 50, 60, 20, -4, -1 }, { 0, -4, 8, 49, 60, 20, -4, -1 }, 53 { 0, -4, 7, 49, 60, 21, -3, -2 }, { -1, -4, 7, 48, 61, 22, -3, -2 }, 54 { 0, -4, 6, 47, 61, 23, -3, -2 }, { 0, -4, 6, 47, 61, 23, -3, -2 }, 55 { 0, -4, 5, 46, 62, 24, -3, -2 }, { 0, -4, 5, 45, 62, 25, -3, -2 }, 56 { 0, -4, 5, 45, 62, 25, -3, -2 }, { 0, -4, 5, 44, 62, 26, -3, -2 }, 57 { 0, -4, 4, 43, 63, 27, -3, -2 }, { 0, -4, 3, 43, 63, 27, -2, -2 }, 58 { 0, -4, 3, 42, 63, 28, -2, -2 }, { 0, -4, 3, 41, 63, 29, -2, -2 }, 59 { 0, -3, 2, 41, 63, 29, -2, -2 }, { 0, -3, 2, 40, 63, 30, -2, -2 }, 60 { 0, -3, 2, 39, 63, 31, -1, -3 }, { 0, -3, 1, 39, 64, 31, -1, -3 }, 61 { 0, -3, 1, 38, 64, 32, -1, -3 }, { 0, -3, 1, 37, 64, 33, -1, -3 }, 62 { 0, -3, 1, 36, 64, 34, -1, -3 }, { 0, -3, 0, 36, 64, 34, 0, -3 }, 63 }; 64 65 // Filters for interpolation (0.625-band) - note this also filters integer pels. 66 static const InterpKernel filteredinterp_filters625[(1 << RS_SUBPEL_BITS)] = { 67 { -1, -8, 33, 80, 33, -8, -1, 0 }, { -1, -8, 31, 80, 34, -8, -1, 1 }, 68 { -1, -8, 30, 80, 35, -8, -1, 1 }, { -1, -8, 29, 80, 36, -7, -2, 1 }, 69 { -1, -8, 28, 80, 37, -7, -2, 1 }, { -1, -8, 27, 80, 38, -7, -2, 1 }, 70 { 0, -8, 26, 79, 39, -7, -2, 1 }, { 0, -8, 25, 79, 40, -7, -2, 1 }, 71 { 0, -8, 24, 79, 41, -7, -2, 1 }, { 0, -8, 23, 78, 42, -6, -2, 1 }, 72 { 0, -8, 22, 78, 43, -6, -2, 1 }, { 0, -8, 21, 78, 44, -6, -2, 1 }, 73 { 0, -8, 20, 78, 45, -5, -3, 1 }, { 0, -8, 19, 77, 47, -5, -3, 1 }, 74 { 0, -8, 18, 77, 48, -5, -3, 1 }, { 0, -8, 17, 77, 49, -5, -3, 1 }, 75 { 0, -8, 16, 76, 50, -4, -3, 1 }, { 0, -8, 15, 76, 51, -4, -3, 1 }, 76 { 0, -8, 15, 75, 52, -3, -4, 1 }, { 0, -7, 14, 74, 53, -3, -4, 1 }, 77 { 0, -7, 13, 74, 54, -3, -4, 1 }, { 0, -7, 12, 73, 55, -2, -4, 1 }, 78 { 0, -7, 11, 73, 56, -2, -4, 1 }, { 0, -7, 10, 72, 57, -1, -4, 1 }, 79 { 1, -7, 10, 71, 58, -1, -5, 1 }, { 0, -7, 9, 71, 59, 0, -5, 1 }, 80 { 1, -7, 8, 70, 60, 0, -5, 1 }, { 1, -7, 7, 69, 61, 1, -5, 1 }, 81 { 1, -6, 6, 68, 62, 1, -5, 1 }, { 0, -6, 6, 68, 62, 2, -5, 1 }, 82 { 1, -6, 5, 67, 63, 2, -5, 1 }, { 1, -6, 5, 66, 64, 3, -6, 1 }, 83 { 1, -6, 4, 65, 65, 4, -6, 1 }, { 1, -6, 3, 64, 66, 5, -6, 1 }, 84 { 1, -5, 2, 63, 67, 5, -6, 1 }, { 1, -5, 2, 62, 68, 6, -6, 0 }, 85 { 1, -5, 1, 62, 68, 6, -6, 1 }, { 1, -5, 1, 61, 69, 7, -7, 1 }, 86 { 1, -5, 0, 60, 70, 8, -7, 1 }, { 1, -5, 0, 59, 71, 9, -7, 0 }, 87 { 1, -5, -1, 58, 71, 10, -7, 1 }, { 1, -4, -1, 57, 72, 10, -7, 0 }, 88 { 1, -4, -2, 56, 73, 11, -7, 0 }, { 1, -4, -2, 55, 73, 12, -7, 0 }, 89 { 1, -4, -3, 54, 74, 13, -7, 0 }, { 1, -4, -3, 53, 74, 14, -7, 0 }, 90 { 1, -4, -3, 52, 75, 15, -8, 0 }, { 1, -3, -4, 51, 76, 15, -8, 0 }, 91 { 1, -3, -4, 50, 76, 16, -8, 0 }, { 1, -3, -5, 49, 77, 17, -8, 0 }, 92 { 1, -3, -5, 48, 77, 18, -8, 0 }, { 1, -3, -5, 47, 77, 19, -8, 0 }, 93 { 1, -3, -5, 45, 78, 20, -8, 0 }, { 1, -2, -6, 44, 78, 21, -8, 0 }, 94 { 1, -2, -6, 43, 78, 22, -8, 0 }, { 1, -2, -6, 42, 78, 23, -8, 0 }, 95 { 1, -2, -7, 41, 79, 24, -8, 0 }, { 1, -2, -7, 40, 79, 25, -8, 0 }, 96 { 1, -2, -7, 39, 79, 26, -8, 0 }, { 1, -2, -7, 38, 80, 27, -8, -1 }, 97 { 1, -2, -7, 37, 80, 28, -8, -1 }, { 1, -2, -7, 36, 80, 29, -8, -1 }, 98 { 1, -1, -8, 35, 80, 30, -8, -1 }, { 1, -1, -8, 34, 80, 31, -8, -1 }, 99 }; 100 101 // Filters for interpolation (0.75-band) - note this also filters integer pels. 102 static const InterpKernel filteredinterp_filters750[(1 << RS_SUBPEL_BITS)] = { 103 { 2, -11, 25, 96, 25, -11, 2, 0 }, { 2, -11, 24, 96, 26, -11, 2, 0 }, 104 { 2, -11, 22, 96, 28, -11, 2, 0 }, { 2, -10, 21, 96, 29, -12, 2, 0 }, 105 { 2, -10, 19, 96, 31, -12, 2, 0 }, { 2, -10, 18, 95, 32, -11, 2, 0 }, 106 { 2, -10, 17, 95, 34, -12, 2, 0 }, { 2, -9, 15, 95, 35, -12, 2, 0 }, 107 { 2, -9, 14, 94, 37, -12, 2, 0 }, { 2, -9, 13, 94, 38, -12, 2, 0 }, 108 { 2, -8, 12, 93, 40, -12, 1, 0 }, { 2, -8, 11, 93, 41, -12, 1, 0 }, 109 { 2, -8, 9, 92, 43, -12, 1, 1 }, { 2, -8, 8, 92, 44, -12, 1, 1 }, 110 { 2, -7, 7, 91, 46, -12, 1, 0 }, { 2, -7, 6, 90, 47, -12, 1, 1 }, 111 { 2, -7, 5, 90, 49, -12, 1, 0 }, { 2, -6, 4, 89, 50, -12, 1, 0 }, 112 { 2, -6, 3, 88, 52, -12, 0, 1 }, { 2, -6, 2, 87, 54, -12, 0, 1 }, 113 { 2, -5, 1, 86, 55, -12, 0, 1 }, { 2, -5, 0, 85, 57, -12, 0, 1 }, 114 { 2, -5, -1, 84, 58, -11, 0, 1 }, { 2, -5, -2, 83, 60, -11, 0, 1 }, 115 { 2, -4, -2, 82, 61, -11, -1, 1 }, { 1, -4, -3, 81, 63, -10, -1, 1 }, 116 { 2, -4, -4, 80, 64, -10, -1, 1 }, { 1, -4, -4, 79, 66, -10, -1, 1 }, 117 { 1, -3, -5, 77, 67, -9, -1, 1 }, { 1, -3, -6, 76, 69, -9, -1, 1 }, 118 { 1, -3, -6, 75, 70, -8, -2, 1 }, { 1, -2, -7, 74, 71, -8, -2, 1 }, 119 { 1, -2, -7, 72, 72, -7, -2, 1 }, { 1, -2, -8, 71, 74, -7, -2, 1 }, 120 { 1, -2, -8, 70, 75, -6, -3, 1 }, { 1, -1, -9, 69, 76, -6, -3, 1 }, 121 { 1, -1, -9, 67, 77, -5, -3, 1 }, { 1, -1, -10, 66, 79, -4, -4, 1 }, 122 { 1, -1, -10, 64, 80, -4, -4, 2 }, { 1, -1, -10, 63, 81, -3, -4, 1 }, 123 { 1, -1, -11, 61, 82, -2, -4, 2 }, { 1, 0, -11, 60, 83, -2, -5, 2 }, 124 { 1, 0, -11, 58, 84, -1, -5, 2 }, { 1, 0, -12, 57, 85, 0, -5, 2 }, 125 { 1, 0, -12, 55, 86, 1, -5, 2 }, { 1, 0, -12, 54, 87, 2, -6, 2 }, 126 { 1, 0, -12, 52, 88, 3, -6, 2 }, { 0, 1, -12, 50, 89, 4, -6, 2 }, 127 { 0, 1, -12, 49, 90, 5, -7, 2 }, { 1, 1, -12, 47, 90, 6, -7, 2 }, 128 { 0, 1, -12, 46, 91, 7, -7, 2 }, { 1, 1, -12, 44, 92, 8, -8, 2 }, 129 { 1, 1, -12, 43, 92, 9, -8, 2 }, { 0, 1, -12, 41, 93, 11, -8, 2 }, 130 { 0, 1, -12, 40, 93, 12, -8, 2 }, { 0, 2, -12, 38, 94, 13, -9, 2 }, 131 { 0, 2, -12, 37, 94, 14, -9, 2 }, { 0, 2, -12, 35, 95, 15, -9, 2 }, 132 { 0, 2, -12, 34, 95, 17, -10, 2 }, { 0, 2, -11, 32, 95, 18, -10, 2 }, 133 { 0, 2, -12, 31, 96, 19, -10, 2 }, { 0, 2, -12, 29, 96, 21, -10, 2 }, 134 { 0, 2, -11, 28, 96, 22, -11, 2 }, { 0, 2, -11, 26, 96, 24, -11, 2 }, 135 }; 136 137 // Filters for interpolation (0.875-band) - note this also filters integer pels. 138 static const InterpKernel filteredinterp_filters875[(1 << RS_SUBPEL_BITS)] = { 139 { 3, -8, 13, 112, 13, -8, 3, 0 }, { 2, -7, 12, 112, 15, -8, 3, -1 }, 140 { 3, -7, 10, 112, 17, -9, 3, -1 }, { 2, -6, 8, 112, 19, -9, 3, -1 }, 141 { 2, -6, 7, 112, 21, -10, 3, -1 }, { 2, -5, 6, 111, 22, -10, 3, -1 }, 142 { 2, -5, 4, 111, 24, -10, 3, -1 }, { 2, -4, 3, 110, 26, -11, 3, -1 }, 143 { 2, -4, 1, 110, 28, -11, 3, -1 }, { 2, -4, 0, 109, 30, -12, 4, -1 }, 144 { 1, -3, -1, 108, 32, -12, 4, -1 }, { 1, -3, -2, 108, 34, -13, 4, -1 }, 145 { 1, -2, -4, 107, 36, -13, 4, -1 }, { 1, -2, -5, 106, 38, -13, 4, -1 }, 146 { 1, -1, -6, 105, 40, -14, 4, -1 }, { 1, -1, -7, 104, 42, -14, 4, -1 }, 147 { 1, -1, -7, 103, 44, -15, 4, -1 }, { 1, 0, -8, 101, 46, -15, 4, -1 }, 148 { 1, 0, -9, 100, 48, -15, 4, -1 }, { 1, 0, -10, 99, 50, -15, 4, -1 }, 149 { 1, 1, -11, 97, 53, -16, 4, -1 }, { 0, 1, -11, 96, 55, -16, 4, -1 }, 150 { 0, 1, -12, 95, 57, -16, 4, -1 }, { 0, 2, -13, 93, 59, -16, 4, -1 }, 151 { 0, 2, -13, 91, 61, -16, 4, -1 }, { 0, 2, -14, 90, 63, -16, 4, -1 }, 152 { 0, 2, -14, 88, 65, -16, 4, -1 }, { 0, 2, -15, 86, 67, -16, 4, 0 }, 153 { 0, 3, -15, 84, 69, -17, 4, 0 }, { 0, 3, -16, 83, 71, -17, 4, 0 }, 154 { 0, 3, -16, 81, 73, -16, 3, 0 }, { 0, 3, -16, 79, 75, -16, 3, 0 }, 155 { 0, 3, -16, 77, 77, -16, 3, 0 }, { 0, 3, -16, 75, 79, -16, 3, 0 }, 156 { 0, 3, -16, 73, 81, -16, 3, 0 }, { 0, 4, -17, 71, 83, -16, 3, 0 }, 157 { 0, 4, -17, 69, 84, -15, 3, 0 }, { 0, 4, -16, 67, 86, -15, 2, 0 }, 158 { -1, 4, -16, 65, 88, -14, 2, 0 }, { -1, 4, -16, 63, 90, -14, 2, 0 }, 159 { -1, 4, -16, 61, 91, -13, 2, 0 }, { -1, 4, -16, 59, 93, -13, 2, 0 }, 160 { -1, 4, -16, 57, 95, -12, 1, 0 }, { -1, 4, -16, 55, 96, -11, 1, 0 }, 161 { -1, 4, -16, 53, 97, -11, 1, 1 }, { -1, 4, -15, 50, 99, -10, 0, 1 }, 162 { -1, 4, -15, 48, 100, -9, 0, 1 }, { -1, 4, -15, 46, 101, -8, 0, 1 }, 163 { -1, 4, -15, 44, 103, -7, -1, 1 }, { -1, 4, -14, 42, 104, -7, -1, 1 }, 164 { -1, 4, -14, 40, 105, -6, -1, 1 }, { -1, 4, -13, 38, 106, -5, -2, 1 }, 165 { -1, 4, -13, 36, 107, -4, -2, 1 }, { -1, 4, -13, 34, 108, -2, -3, 1 }, 166 { -1, 4, -12, 32, 108, -1, -3, 1 }, { -1, 4, -12, 30, 109, 0, -4, 2 }, 167 { -1, 3, -11, 28, 110, 1, -4, 2 }, { -1, 3, -11, 26, 110, 3, -4, 2 }, 168 { -1, 3, -10, 24, 111, 4, -5, 2 }, { -1, 3, -10, 22, 111, 6, -5, 2 }, 169 { -1, 3, -10, 21, 112, 7, -6, 2 }, { -1, 3, -9, 19, 112, 8, -6, 2 }, 170 { -1, 3, -9, 17, 112, 10, -7, 3 }, { -1, 3, -8, 15, 112, 12, -7, 2 }, 171 }; 172 173 const int16_t av1_resize_filter_normative[( 174 1 << RS_SUBPEL_BITS)][UPSCALE_NORMATIVE_TAPS] = { 175 #if UPSCALE_NORMATIVE_TAPS == 8 176 { 0, 0, 0, 128, 0, 0, 0, 0 }, { 0, 0, -1, 128, 2, -1, 0, 0 }, 177 { 0, 1, -3, 127, 4, -2, 1, 0 }, { 0, 1, -4, 127, 6, -3, 1, 0 }, 178 { 0, 2, -6, 126, 8, -3, 1, 0 }, { 0, 2, -7, 125, 11, -4, 1, 0 }, 179 { -1, 2, -8, 125, 13, -5, 2, 0 }, { -1, 3, -9, 124, 15, -6, 2, 0 }, 180 { -1, 3, -10, 123, 18, -6, 2, -1 }, { -1, 3, -11, 122, 20, -7, 3, -1 }, 181 { -1, 4, -12, 121, 22, -8, 3, -1 }, { -1, 4, -13, 120, 25, -9, 3, -1 }, 182 { -1, 4, -14, 118, 28, -9, 3, -1 }, { -1, 4, -15, 117, 30, -10, 4, -1 }, 183 { -1, 5, -16, 116, 32, -11, 4, -1 }, { -1, 5, -16, 114, 35, -12, 4, -1 }, 184 { -1, 5, -17, 112, 38, -12, 4, -1 }, { -1, 5, -18, 111, 40, -13, 5, -1 }, 185 { -1, 5, -18, 109, 43, -14, 5, -1 }, { -1, 6, -19, 107, 45, -14, 5, -1 }, 186 { -1, 6, -19, 105, 48, -15, 5, -1 }, { -1, 6, -19, 103, 51, -16, 5, -1 }, 187 { -1, 6, -20, 101, 53, -16, 6, -1 }, { -1, 6, -20, 99, 56, -17, 6, -1 }, 188 { -1, 6, -20, 97, 58, -17, 6, -1 }, { -1, 6, -20, 95, 61, -18, 6, -1 }, 189 { -2, 7, -20, 93, 64, -18, 6, -2 }, { -2, 7, -20, 91, 66, -19, 6, -1 }, 190 { -2, 7, -20, 88, 69, -19, 6, -1 }, { -2, 7, -20, 86, 71, -19, 6, -1 }, 191 { -2, 7, -20, 84, 74, -20, 7, -2 }, { -2, 7, -20, 81, 76, -20, 7, -1 }, 192 { -2, 7, -20, 79, 79, -20, 7, -2 }, { -1, 7, -20, 76, 81, -20, 7, -2 }, 193 { -2, 7, -20, 74, 84, -20, 7, -2 }, { -1, 6, -19, 71, 86, -20, 7, -2 }, 194 { -1, 6, -19, 69, 88, -20, 7, -2 }, { -1, 6, -19, 66, 91, -20, 7, -2 }, 195 { -2, 6, -18, 64, 93, -20, 7, -2 }, { -1, 6, -18, 61, 95, -20, 6, -1 }, 196 { -1, 6, -17, 58, 97, -20, 6, -1 }, { -1, 6, -17, 56, 99, -20, 6, -1 }, 197 { -1, 6, -16, 53, 101, -20, 6, -1 }, { -1, 5, -16, 51, 103, -19, 6, -1 }, 198 { -1, 5, -15, 48, 105, -19, 6, -1 }, { -1, 5, -14, 45, 107, -19, 6, -1 }, 199 { -1, 5, -14, 43, 109, -18, 5, -1 }, { -1, 5, -13, 40, 111, -18, 5, -1 }, 200 { -1, 4, -12, 38, 112, -17, 5, -1 }, { -1, 4, -12, 35, 114, -16, 5, -1 }, 201 { -1, 4, -11, 32, 116, -16, 5, -1 }, { -1, 4, -10, 30, 117, -15, 4, -1 }, 202 { -1, 3, -9, 28, 118, -14, 4, -1 }, { -1, 3, -9, 25, 120, -13, 4, -1 }, 203 { -1, 3, -8, 22, 121, -12, 4, -1 }, { -1, 3, -7, 20, 122, -11, 3, -1 }, 204 { -1, 2, -6, 18, 123, -10, 3, -1 }, { 0, 2, -6, 15, 124, -9, 3, -1 }, 205 { 0, 2, -5, 13, 125, -8, 2, -1 }, { 0, 1, -4, 11, 125, -7, 2, 0 }, 206 { 0, 1, -3, 8, 126, -6, 2, 0 }, { 0, 1, -3, 6, 127, -4, 1, 0 }, 207 { 0, 1, -2, 4, 127, -3, 1, 0 }, { 0, 0, -1, 2, 128, -1, 0, 0 }, 208 #else 209 #error "Invalid value of UPSCALE_NORMATIVE_TAPS" 210 #endif // UPSCALE_NORMATIVE_TAPS == 8 211 }; 212 213 // Filters for interpolation (full-band) - no filtering for integer pixels 214 #define filteredinterp_filters1000 av1_resize_filter_normative 215 216 // Filters for factor of 2 downsampling. 217 static const int16_t av1_down2_symeven_half_filter[] = { 56, 12, -3, -1 }; 218 static const int16_t av1_down2_symodd_half_filter[] = { 64, 35, 0, -3 }; 219 220 static const InterpKernel *choose_interp_filter(int in_length, int out_length) { 221 int out_length16 = out_length * 16; 222 if (out_length16 >= in_length * 16) 223 return filteredinterp_filters1000; 224 else if (out_length16 >= in_length * 13) 225 return filteredinterp_filters875; 226 else if (out_length16 >= in_length * 11) 227 return filteredinterp_filters750; 228 else if (out_length16 >= in_length * 9) 229 return filteredinterp_filters625; 230 else 231 return filteredinterp_filters500; 232 } 233 234 static void interpolate_core(const uint8_t *const input, int in_length, 235 uint8_t *output, int out_length, 236 const int16_t *interp_filters, int interp_taps) { 237 const int32_t delta = 238 (((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / 239 out_length; 240 const int32_t offset = 241 in_length > out_length 242 ? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) + 243 out_length / 2) / 244 out_length 245 : -(((int32_t)(out_length - in_length) 246 << (RS_SCALE_SUBPEL_BITS - 1)) + 247 out_length / 2) / 248 out_length; 249 uint8_t *optr = output; 250 int x, x1, x2, sum, k, int_pel, sub_pel; 251 int32_t y; 252 253 x = 0; 254 y = offset + RS_SCALE_EXTRA_OFF; 255 while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) { 256 x++; 257 y += delta; 258 } 259 x1 = x; 260 x = out_length - 1; 261 y = delta * x + offset + RS_SCALE_EXTRA_OFF; 262 while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >= 263 in_length) { 264 x--; 265 y -= delta; 266 } 267 x2 = x; 268 if (x1 > x2) { 269 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length; 270 ++x, y += delta) { 271 int_pel = y >> RS_SCALE_SUBPEL_BITS; 272 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 273 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 274 sum = 0; 275 for (k = 0; k < interp_taps; ++k) { 276 const int pk = int_pel - interp_taps / 2 + 1 + k; 277 sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)]; 278 } 279 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); 280 } 281 } else { 282 // Initial part. 283 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) { 284 int_pel = y >> RS_SCALE_SUBPEL_BITS; 285 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 286 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 287 sum = 0; 288 for (k = 0; k < interp_taps; ++k) 289 sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)]; 290 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); 291 } 292 // Middle part. 293 for (; x <= x2; ++x, y += delta) { 294 int_pel = y >> RS_SCALE_SUBPEL_BITS; 295 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 296 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 297 sum = 0; 298 for (k = 0; k < interp_taps; ++k) 299 sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k]; 300 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); 301 } 302 // End part. 303 for (; x < out_length; ++x, y += delta) { 304 int_pel = y >> RS_SCALE_SUBPEL_BITS; 305 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 306 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 307 sum = 0; 308 for (k = 0; k < interp_taps; ++k) 309 sum += filter[k] * 310 input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)]; 311 *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS)); 312 } 313 } 314 } 315 316 static void interpolate_core_double_prec(const double *const input, 317 int in_length, double *output, 318 int out_length, 319 const int16_t *interp_filters, 320 int interp_taps) { 321 const int32_t delta = 322 (((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / 323 out_length; 324 const int32_t offset = 325 in_length > out_length 326 ? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) + 327 out_length / 2) / 328 out_length 329 : -(((int32_t)(out_length - in_length) 330 << (RS_SCALE_SUBPEL_BITS - 1)) + 331 out_length / 2) / 332 out_length; 333 double *optr = output; 334 int x, x1, x2, k, int_pel, sub_pel; 335 double sum; 336 int32_t y; 337 338 x = 0; 339 y = offset + RS_SCALE_EXTRA_OFF; 340 while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) { 341 x++; 342 y += delta; 343 } 344 x1 = x; 345 x = out_length - 1; 346 y = delta * x + offset + RS_SCALE_EXTRA_OFF; 347 while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >= 348 in_length) { 349 x--; 350 y -= delta; 351 } 352 x2 = x; 353 if (x1 > x2) { 354 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length; 355 ++x, y += delta) { 356 int_pel = y >> RS_SCALE_SUBPEL_BITS; 357 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 358 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 359 sum = 0; 360 for (k = 0; k < interp_taps; ++k) { 361 const int pk = int_pel - interp_taps / 2 + 1 + k; 362 sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)]; 363 } 364 *optr++ = sum / (1 << FILTER_BITS); 365 } 366 } else { 367 // Initial part. 368 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) { 369 int_pel = y >> RS_SCALE_SUBPEL_BITS; 370 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 371 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 372 sum = 0; 373 for (k = 0; k < interp_taps; ++k) 374 sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)]; 375 *optr++ = sum / (1 << FILTER_BITS); 376 } 377 // Middle part. 378 for (; x <= x2; ++x, y += delta) { 379 int_pel = y >> RS_SCALE_SUBPEL_BITS; 380 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 381 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 382 sum = 0; 383 for (k = 0; k < interp_taps; ++k) 384 sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k]; 385 *optr++ = sum / (1 << FILTER_BITS); 386 } 387 // End part. 388 for (; x < out_length; ++x, y += delta) { 389 int_pel = y >> RS_SCALE_SUBPEL_BITS; 390 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 391 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 392 sum = 0; 393 for (k = 0; k < interp_taps; ++k) 394 sum += filter[k] * 395 input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)]; 396 *optr++ = sum / (1 << FILTER_BITS); 397 } 398 } 399 } 400 401 static void interpolate(const uint8_t *const input, int in_length, 402 uint8_t *output, int out_length) { 403 const InterpKernel *interp_filters = 404 choose_interp_filter(in_length, out_length); 405 406 interpolate_core(input, in_length, output, out_length, &interp_filters[0][0], 407 SUBPEL_TAPS); 408 } 409 410 static void interpolate_double_prec(const double *const input, int in_length, 411 double *output, int out_length) { 412 const InterpKernel *interp_filters = 413 choose_interp_filter(in_length, out_length); 414 415 interpolate_core_double_prec(input, in_length, output, out_length, 416 &interp_filters[0][0], SUBPEL_TAPS); 417 } 418 419 int32_t av1_get_upscale_convolve_step(int in_length, int out_length) { 420 return ((in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / out_length; 421 } 422 423 static int32_t get_upscale_convolve_x0(int in_length, int out_length, 424 int32_t x_step_qn) { 425 const int err = out_length * x_step_qn - (in_length << RS_SCALE_SUBPEL_BITS); 426 const int32_t x0 = 427 (-((out_length - in_length) << (RS_SCALE_SUBPEL_BITS - 1)) + 428 out_length / 2) / 429 out_length + 430 RS_SCALE_EXTRA_OFF - err / 2; 431 return (int32_t)((uint32_t)x0 & RS_SCALE_SUBPEL_MASK); 432 } 433 434 static void down2_symeven(const uint8_t *const input, int length, 435 uint8_t *output) { 436 // Actual filter len = 2 * filter_len_half. 437 const int16_t *filter = av1_down2_symeven_half_filter; 438 const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2; 439 int i, j; 440 uint8_t *optr = output; 441 int l1 = filter_len_half; 442 int l2 = (length - filter_len_half); 443 l1 += (l1 & 1); 444 l2 += (l2 & 1); 445 if (l1 > l2) { 446 // Short input length. 447 for (i = 0; i < length; i += 2) { 448 int sum = (1 << (FILTER_BITS - 1)); 449 for (j = 0; j < filter_len_half; ++j) { 450 sum += 451 (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + 1 + j, length - 1)]) * 452 filter[j]; 453 } 454 sum >>= FILTER_BITS; 455 *optr++ = clip_pixel(sum); 456 } 457 } else { 458 // Initial part. 459 for (i = 0; i < l1; i += 2) { 460 int sum = (1 << (FILTER_BITS - 1)); 461 for (j = 0; j < filter_len_half; ++j) { 462 sum += (input[AOMMAX(i - j, 0)] + input[i + 1 + j]) * filter[j]; 463 } 464 sum >>= FILTER_BITS; 465 *optr++ = clip_pixel(sum); 466 } 467 // Middle part. 468 for (; i < l2; i += 2) { 469 int sum = (1 << (FILTER_BITS - 1)); 470 for (j = 0; j < filter_len_half; ++j) { 471 sum += (input[i - j] + input[i + 1 + j]) * filter[j]; 472 } 473 sum >>= FILTER_BITS; 474 *optr++ = clip_pixel(sum); 475 } 476 // End part. 477 for (; i < length; i += 2) { 478 int sum = (1 << (FILTER_BITS - 1)); 479 for (j = 0; j < filter_len_half; ++j) { 480 sum += 481 (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j]; 482 } 483 sum >>= FILTER_BITS; 484 *optr++ = clip_pixel(sum); 485 } 486 } 487 } 488 489 static void down2_symodd(const uint8_t *const input, int length, 490 uint8_t *output) { 491 // Actual filter len = 2 * filter_len_half - 1. 492 const int16_t *filter = av1_down2_symodd_half_filter; 493 const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2; 494 int i, j; 495 uint8_t *optr = output; 496 int l1 = filter_len_half - 1; 497 int l2 = (length - filter_len_half + 1); 498 l1 += (l1 & 1); 499 l2 += (l2 & 1); 500 if (l1 > l2) { 501 // Short input length. 502 for (i = 0; i < length; i += 2) { 503 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 504 for (j = 1; j < filter_len_half; ++j) { 505 sum += (input[(i - j < 0 ? 0 : i - j)] + 506 input[(i + j >= length ? length - 1 : i + j)]) * 507 filter[j]; 508 } 509 sum >>= FILTER_BITS; 510 *optr++ = clip_pixel(sum); 511 } 512 } else { 513 // Initial part. 514 for (i = 0; i < l1; i += 2) { 515 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 516 for (j = 1; j < filter_len_half; ++j) { 517 sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j]; 518 } 519 sum >>= FILTER_BITS; 520 *optr++ = clip_pixel(sum); 521 } 522 // Middle part. 523 for (; i < l2; i += 2) { 524 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 525 for (j = 1; j < filter_len_half; ++j) { 526 sum += (input[i - j] + input[i + j]) * filter[j]; 527 } 528 sum >>= FILTER_BITS; 529 *optr++ = clip_pixel(sum); 530 } 531 // End part. 532 for (; i < length; i += 2) { 533 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 534 for (j = 1; j < filter_len_half; ++j) { 535 sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) * 536 filter[j]; 537 } 538 sum >>= FILTER_BITS; 539 *optr++ = clip_pixel(sum); 540 } 541 } 542 } 543 544 static int get_down2_length(int length, int steps) { 545 for (int s = 0; s < steps; ++s) length = (length + 1) >> 1; 546 return length; 547 } 548 549 static int get_down2_steps(int in_length, int out_length) { 550 int steps = 0; 551 int proj_in_length; 552 while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) { 553 ++steps; 554 in_length = proj_in_length; 555 if (in_length == 1) { 556 // Special case: we break because any further calls to get_down2_length() 557 // with be with length == 1, which return 1, resulting in an infinite 558 // loop. 559 break; 560 } 561 } 562 return steps; 563 } 564 565 static void resize_multistep(const uint8_t *const input, int length, 566 uint8_t *output, int olength, uint8_t *otmp) { 567 if (length == olength) { 568 memcpy(output, input, sizeof(output[0]) * length); 569 return; 570 } 571 const int steps = get_down2_steps(length, olength); 572 573 if (steps > 0) { 574 uint8_t *out = NULL; 575 int filteredlength = length; 576 577 assert(otmp != NULL); 578 uint8_t *otmp2 = otmp + get_down2_length(length, 1); 579 for (int s = 0; s < steps; ++s) { 580 const int proj_filteredlength = get_down2_length(filteredlength, 1); 581 const uint8_t *const in = (s == 0 ? input : out); 582 if (s == steps - 1 && proj_filteredlength == olength) 583 out = output; 584 else 585 out = (s & 1 ? otmp2 : otmp); 586 if (filteredlength & 1) 587 down2_symodd(in, filteredlength, out); 588 else 589 down2_symeven(in, filteredlength, out); 590 filteredlength = proj_filteredlength; 591 } 592 if (filteredlength != olength) { 593 interpolate(out, filteredlength, output, olength); 594 } 595 } else { 596 interpolate(input, length, output, olength); 597 } 598 } 599 600 static void upscale_multistep_double_prec(const double *const input, int length, 601 double *output, int olength) { 602 assert(length < olength); 603 interpolate_double_prec(input, length, output, olength); 604 } 605 606 static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) { 607 int i; 608 uint8_t *iptr = img; 609 uint8_t *aptr = arr; 610 for (i = 0; i < len; ++i, iptr += stride) { 611 *aptr++ = *iptr; 612 } 613 } 614 615 static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) { 616 int i; 617 uint8_t *iptr = img; 618 uint8_t *aptr = arr; 619 for (i = 0; i < len; ++i, iptr += stride) { 620 *iptr = *aptr++; 621 } 622 } 623 624 static void fill_col_to_arr_double_prec(double *img, int stride, int len, 625 double *arr) { 626 int i; 627 double *iptr = img; 628 double *aptr = arr; 629 for (i = 0; i < len; ++i, iptr += stride) { 630 *aptr++ = *iptr; 631 } 632 } 633 634 static void fill_arr_to_col_double_prec(double *img, int stride, int len, 635 double *arr) { 636 int i; 637 double *iptr = img; 638 double *aptr = arr; 639 for (i = 0; i < len; ++i, iptr += stride) { 640 *iptr = *aptr++; 641 } 642 } 643 644 void av1_resize_plane(const uint8_t *const input, int height, int width, 645 int in_stride, uint8_t *output, int height2, int width2, 646 int out_stride) { 647 int i; 648 uint8_t *intbuf = (uint8_t *)aom_malloc(sizeof(uint8_t) * width2 * height); 649 uint8_t *tmpbuf = 650 (uint8_t *)aom_malloc(sizeof(uint8_t) * AOMMAX(width, height)); 651 uint8_t *arrbuf = (uint8_t *)aom_malloc(sizeof(uint8_t) * height); 652 uint8_t *arrbuf2 = (uint8_t *)aom_malloc(sizeof(uint8_t) * height2); 653 if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL) 654 goto Error; 655 assert(width > 0); 656 assert(height > 0); 657 assert(width2 > 0); 658 assert(height2 > 0); 659 for (i = 0; i < height; ++i) 660 resize_multistep(input + in_stride * i, width, intbuf + width2 * i, width2, 661 tmpbuf); 662 for (i = 0; i < width2; ++i) { 663 fill_col_to_arr(intbuf + i, width2, height, arrbuf); 664 resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf); 665 fill_arr_to_col(output + i, out_stride, height2, arrbuf2); 666 } 667 668 Error: 669 aom_free(intbuf); 670 aom_free(tmpbuf); 671 aom_free(arrbuf); 672 aom_free(arrbuf2); 673 } 674 675 void av1_upscale_plane_double_prec(const double *const input, int height, 676 int width, int in_stride, double *output, 677 int height2, int width2, int out_stride) { 678 int i; 679 double *intbuf = (double *)aom_malloc(sizeof(double) * width2 * height); 680 double *arrbuf = (double *)aom_malloc(sizeof(double) * height); 681 double *arrbuf2 = (double *)aom_malloc(sizeof(double) * height2); 682 if (intbuf == NULL || arrbuf == NULL || arrbuf2 == NULL) goto Error; 683 assert(width > 0); 684 assert(height > 0); 685 assert(width2 > 0); 686 assert(height2 > 0); 687 for (i = 0; i < height; ++i) 688 upscale_multistep_double_prec(input + in_stride * i, width, 689 intbuf + width2 * i, width2); 690 for (i = 0; i < width2; ++i) { 691 fill_col_to_arr_double_prec(intbuf + i, width2, height, arrbuf); 692 upscale_multistep_double_prec(arrbuf, height, arrbuf2, height2); 693 fill_arr_to_col_double_prec(output + i, out_stride, height2, arrbuf2); 694 } 695 696 Error: 697 aom_free(intbuf); 698 aom_free(arrbuf); 699 aom_free(arrbuf2); 700 } 701 702 static void upscale_normative_rect(const uint8_t *const input, int height, 703 int width, int in_stride, uint8_t *output, 704 int height2, int width2, int out_stride, 705 int x_step_qn, int x0_qn, int pad_left, 706 int pad_right) { 707 assert(width > 0); 708 assert(height > 0); 709 assert(width2 > 0); 710 assert(height2 > 0); 711 assert(height2 == height); 712 713 // Extend the left/right pixels of the tile column if needed 714 // (either because we can't sample from other tiles, or because we're at 715 // a frame edge). 716 // Save the overwritten pixels into tmp_left and tmp_right. 717 // Note: Because we pass input-1 to av1_convolve_horiz_rs, we need one extra 718 // column of border pixels compared to what we'd naively think. 719 const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1; 720 uint8_t *tmp_left = 721 NULL; // Silence spurious "may be used uninitialized" warnings 722 uint8_t *tmp_right = NULL; 723 uint8_t *const in_tl = (uint8_t *)(input - border_cols); // Cast off 'const' 724 uint8_t *const in_tr = (uint8_t *)(input + width); 725 if (pad_left) { 726 tmp_left = (uint8_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height); 727 for (int i = 0; i < height; i++) { 728 memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_cols); 729 memset(in_tl + i * in_stride, input[i * in_stride], border_cols); 730 } 731 } 732 if (pad_right) { 733 tmp_right = 734 (uint8_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height); 735 for (int i = 0; i < height; i++) { 736 memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_cols); 737 memset(in_tr + i * in_stride, input[i * in_stride + width - 1], 738 border_cols); 739 } 740 } 741 742 av1_convolve_horiz_rs(input - 1, in_stride, output, out_stride, width2, 743 height2, &av1_resize_filter_normative[0][0], x0_qn, 744 x_step_qn); 745 746 // Restore the left/right border pixels 747 if (pad_left) { 748 for (int i = 0; i < height; i++) { 749 memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_cols); 750 } 751 aom_free(tmp_left); 752 } 753 if (pad_right) { 754 for (int i = 0; i < height; i++) { 755 memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_cols); 756 } 757 aom_free(tmp_right); 758 } 759 } 760 761 static void highbd_interpolate_core(const uint16_t *const input, int in_length, 762 uint16_t *output, int out_length, int bd, 763 const int16_t *interp_filters, 764 int interp_taps) { 765 const int32_t delta = 766 (((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) / 767 out_length; 768 const int32_t offset = 769 in_length > out_length 770 ? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) + 771 out_length / 2) / 772 out_length 773 : -(((int32_t)(out_length - in_length) 774 << (RS_SCALE_SUBPEL_BITS - 1)) + 775 out_length / 2) / 776 out_length; 777 uint16_t *optr = output; 778 int x, x1, x2, sum, k, int_pel, sub_pel; 779 int32_t y; 780 781 x = 0; 782 y = offset + RS_SCALE_EXTRA_OFF; 783 while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) { 784 x++; 785 y += delta; 786 } 787 x1 = x; 788 x = out_length - 1; 789 y = delta * x + offset + RS_SCALE_EXTRA_OFF; 790 while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >= 791 in_length) { 792 x--; 793 y -= delta; 794 } 795 x2 = x; 796 if (x1 > x2) { 797 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length; 798 ++x, y += delta) { 799 int_pel = y >> RS_SCALE_SUBPEL_BITS; 800 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 801 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 802 sum = 0; 803 for (k = 0; k < interp_taps; ++k) { 804 const int pk = int_pel - interp_taps / 2 + 1 + k; 805 sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)]; 806 } 807 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd); 808 } 809 } else { 810 // Initial part. 811 for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) { 812 int_pel = y >> RS_SCALE_SUBPEL_BITS; 813 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 814 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 815 sum = 0; 816 for (k = 0; k < interp_taps; ++k) 817 sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)]; 818 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd); 819 } 820 // Middle part. 821 for (; x <= x2; ++x, y += delta) { 822 int_pel = y >> RS_SCALE_SUBPEL_BITS; 823 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 824 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 825 sum = 0; 826 for (k = 0; k < interp_taps; ++k) 827 sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k]; 828 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd); 829 } 830 // End part. 831 for (; x < out_length; ++x, y += delta) { 832 int_pel = y >> RS_SCALE_SUBPEL_BITS; 833 sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK; 834 const int16_t *filter = &interp_filters[sub_pel * interp_taps]; 835 sum = 0; 836 for (k = 0; k < interp_taps; ++k) 837 sum += filter[k] * 838 input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)]; 839 *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd); 840 } 841 } 842 } 843 844 static void highbd_interpolate(const uint16_t *const input, int in_length, 845 uint16_t *output, int out_length, int bd) { 846 const InterpKernel *interp_filters = 847 choose_interp_filter(in_length, out_length); 848 849 highbd_interpolate_core(input, in_length, output, out_length, bd, 850 &interp_filters[0][0], SUBPEL_TAPS); 851 } 852 853 static void highbd_down2_symeven(const uint16_t *const input, int length, 854 uint16_t *output, int bd) { 855 // Actual filter len = 2 * filter_len_half. 856 static const int16_t *filter = av1_down2_symeven_half_filter; 857 const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2; 858 int i, j; 859 uint16_t *optr = output; 860 int l1 = filter_len_half; 861 int l2 = (length - filter_len_half); 862 l1 += (l1 & 1); 863 l2 += (l2 & 1); 864 if (l1 > l2) { 865 // Short input length. 866 for (i = 0; i < length; i += 2) { 867 int sum = (1 << (FILTER_BITS - 1)); 868 for (j = 0; j < filter_len_half; ++j) { 869 sum += 870 (input[AOMMAX(0, i - j)] + input[AOMMIN(i + 1 + j, length - 1)]) * 871 filter[j]; 872 } 873 sum >>= FILTER_BITS; 874 *optr++ = clip_pixel_highbd(sum, bd); 875 } 876 } else { 877 // Initial part. 878 for (i = 0; i < l1; i += 2) { 879 int sum = (1 << (FILTER_BITS - 1)); 880 for (j = 0; j < filter_len_half; ++j) { 881 sum += (input[AOMMAX(0, i - j)] + input[i + 1 + j]) * filter[j]; 882 } 883 sum >>= FILTER_BITS; 884 *optr++ = clip_pixel_highbd(sum, bd); 885 } 886 // Middle part. 887 for (; i < l2; i += 2) { 888 int sum = (1 << (FILTER_BITS - 1)); 889 for (j = 0; j < filter_len_half; ++j) { 890 sum += (input[i - j] + input[i + 1 + j]) * filter[j]; 891 } 892 sum >>= FILTER_BITS; 893 *optr++ = clip_pixel_highbd(sum, bd); 894 } 895 // End part. 896 for (; i < length; i += 2) { 897 int sum = (1 << (FILTER_BITS - 1)); 898 for (j = 0; j < filter_len_half; ++j) { 899 sum += 900 (input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j]; 901 } 902 sum >>= FILTER_BITS; 903 *optr++ = clip_pixel_highbd(sum, bd); 904 } 905 } 906 } 907 908 static void highbd_down2_symodd(const uint16_t *const input, int length, 909 uint16_t *output, int bd) { 910 // Actual filter len = 2 * filter_len_half - 1. 911 static const int16_t *filter = av1_down2_symodd_half_filter; 912 const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2; 913 int i, j; 914 uint16_t *optr = output; 915 int l1 = filter_len_half - 1; 916 int l2 = (length - filter_len_half + 1); 917 l1 += (l1 & 1); 918 l2 += (l2 & 1); 919 if (l1 > l2) { 920 // Short input length. 921 for (i = 0; i < length; i += 2) { 922 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 923 for (j = 1; j < filter_len_half; ++j) { 924 sum += (input[AOMMAX(i - j, 0)] + input[AOMMIN(i + j, length - 1)]) * 925 filter[j]; 926 } 927 sum >>= FILTER_BITS; 928 *optr++ = clip_pixel_highbd(sum, bd); 929 } 930 } else { 931 // Initial part. 932 for (i = 0; i < l1; i += 2) { 933 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 934 for (j = 1; j < filter_len_half; ++j) { 935 sum += (input[AOMMAX(i - j, 0)] + input[i + j]) * filter[j]; 936 } 937 sum >>= FILTER_BITS; 938 *optr++ = clip_pixel_highbd(sum, bd); 939 } 940 // Middle part. 941 for (; i < l2; i += 2) { 942 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 943 for (j = 1; j < filter_len_half; ++j) { 944 sum += (input[i - j] + input[i + j]) * filter[j]; 945 } 946 sum >>= FILTER_BITS; 947 *optr++ = clip_pixel_highbd(sum, bd); 948 } 949 // End part. 950 for (; i < length; i += 2) { 951 int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0]; 952 for (j = 1; j < filter_len_half; ++j) { 953 sum += (input[i - j] + input[AOMMIN(i + j, length - 1)]) * filter[j]; 954 } 955 sum >>= FILTER_BITS; 956 *optr++ = clip_pixel_highbd(sum, bd); 957 } 958 } 959 } 960 961 static void highbd_resize_multistep(const uint16_t *const input, int length, 962 uint16_t *output, int olength, 963 uint16_t *otmp, int bd) { 964 if (length == olength) { 965 memcpy(output, input, sizeof(output[0]) * length); 966 return; 967 } 968 const int steps = get_down2_steps(length, olength); 969 970 if (steps > 0) { 971 uint16_t *out = NULL; 972 int filteredlength = length; 973 974 assert(otmp != NULL); 975 uint16_t *otmp2 = otmp + get_down2_length(length, 1); 976 for (int s = 0; s < steps; ++s) { 977 const int proj_filteredlength = get_down2_length(filteredlength, 1); 978 const uint16_t *const in = (s == 0 ? input : out); 979 if (s == steps - 1 && proj_filteredlength == olength) 980 out = output; 981 else 982 out = (s & 1 ? otmp2 : otmp); 983 if (filteredlength & 1) 984 highbd_down2_symodd(in, filteredlength, out, bd); 985 else 986 highbd_down2_symeven(in, filteredlength, out, bd); 987 filteredlength = proj_filteredlength; 988 } 989 if (filteredlength != olength) { 990 highbd_interpolate(out, filteredlength, output, olength, bd); 991 } 992 } else { 993 highbd_interpolate(input, length, output, olength, bd); 994 } 995 } 996 997 static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len, 998 uint16_t *arr) { 999 int i; 1000 uint16_t *iptr = img; 1001 uint16_t *aptr = arr; 1002 for (i = 0; i < len; ++i, iptr += stride) { 1003 *aptr++ = *iptr; 1004 } 1005 } 1006 1007 static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len, 1008 uint16_t *arr) { 1009 int i; 1010 uint16_t *iptr = img; 1011 uint16_t *aptr = arr; 1012 for (i = 0; i < len; ++i, iptr += stride) { 1013 *iptr = *aptr++; 1014 } 1015 } 1016 1017 void av1_highbd_resize_plane(const uint8_t *const input, int height, int width, 1018 int in_stride, uint8_t *output, int height2, 1019 int width2, int out_stride, int bd) { 1020 int i; 1021 uint16_t *intbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * width2 * height); 1022 uint16_t *tmpbuf = 1023 (uint16_t *)aom_malloc(sizeof(uint16_t) * AOMMAX(width, height)); 1024 uint16_t *arrbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * height); 1025 uint16_t *arrbuf2 = (uint16_t *)aom_malloc(sizeof(uint16_t) * height2); 1026 if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL) 1027 goto Error; 1028 for (i = 0; i < height; ++i) { 1029 highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width, 1030 intbuf + width2 * i, width2, tmpbuf, bd); 1031 } 1032 for (i = 0; i < width2; ++i) { 1033 highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf); 1034 highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd); 1035 highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2, 1036 arrbuf2); 1037 } 1038 1039 Error: 1040 aom_free(intbuf); 1041 aom_free(tmpbuf); 1042 aom_free(arrbuf); 1043 aom_free(arrbuf2); 1044 } 1045 1046 static void highbd_upscale_normative_rect(const uint8_t *const input, 1047 int height, int width, int in_stride, 1048 uint8_t *output, int height2, 1049 int width2, int out_stride, 1050 int x_step_qn, int x0_qn, 1051 int pad_left, int pad_right, int bd) { 1052 assert(width > 0); 1053 assert(height > 0); 1054 assert(width2 > 0); 1055 assert(height2 > 0); 1056 assert(height2 == height); 1057 1058 // Extend the left/right pixels of the tile column if needed 1059 // (either because we can't sample from other tiles, or because we're at 1060 // a frame edge). 1061 // Save the overwritten pixels into tmp_left and tmp_right. 1062 // Note: Because we pass input-1 to av1_convolve_horiz_rs, we need one extra 1063 // column of border pixels compared to what we'd naively think. 1064 const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1; 1065 const int border_size = border_cols * sizeof(uint16_t); 1066 uint16_t *tmp_left = 1067 NULL; // Silence spurious "may be used uninitialized" warnings 1068 uint16_t *tmp_right = NULL; 1069 uint16_t *const input16 = CONVERT_TO_SHORTPTR(input); 1070 uint16_t *const in_tl = input16 - border_cols; 1071 uint16_t *const in_tr = input16 + width; 1072 if (pad_left) { 1073 tmp_left = (uint16_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height); 1074 for (int i = 0; i < height; i++) { 1075 memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_size); 1076 aom_memset16(in_tl + i * in_stride, input16[i * in_stride], border_cols); 1077 } 1078 } 1079 if (pad_right) { 1080 tmp_right = 1081 (uint16_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height); 1082 for (int i = 0; i < height; i++) { 1083 memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_size); 1084 aom_memset16(in_tr + i * in_stride, input16[i * in_stride + width - 1], 1085 border_cols); 1086 } 1087 } 1088 1089 av1_highbd_convolve_horiz_rs(CONVERT_TO_SHORTPTR(input - 1), in_stride, 1090 CONVERT_TO_SHORTPTR(output), out_stride, width2, 1091 height2, &av1_resize_filter_normative[0][0], 1092 x0_qn, x_step_qn, bd); 1093 1094 // Restore the left/right border pixels 1095 if (pad_left) { 1096 for (int i = 0; i < height; i++) { 1097 memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_size); 1098 } 1099 aom_free(tmp_left); 1100 } 1101 if (pad_right) { 1102 for (int i = 0; i < height; i++) { 1103 memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_size); 1104 } 1105 aom_free(tmp_right); 1106 } 1107 } 1108 1109 void av1_resize_frame420(const uint8_t *const y, int y_stride, 1110 const uint8_t *const u, const uint8_t *const v, 1111 int uv_stride, int height, int width, uint8_t *oy, 1112 int oy_stride, uint8_t *ou, uint8_t *ov, 1113 int ouv_stride, int oheight, int owidth) { 1114 av1_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride); 1115 av1_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2, 1116 owidth / 2, ouv_stride); 1117 av1_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2, 1118 owidth / 2, ouv_stride); 1119 } 1120 1121 void av1_resize_frame422(const uint8_t *const y, int y_stride, 1122 const uint8_t *const u, const uint8_t *const v, 1123 int uv_stride, int height, int width, uint8_t *oy, 1124 int oy_stride, uint8_t *ou, uint8_t *ov, 1125 int ouv_stride, int oheight, int owidth) { 1126 av1_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride); 1127 av1_resize_plane(u, height, width / 2, uv_stride, ou, oheight, owidth / 2, 1128 ouv_stride); 1129 av1_resize_plane(v, height, width / 2, uv_stride, ov, oheight, owidth / 2, 1130 ouv_stride); 1131 } 1132 1133 void av1_resize_frame444(const uint8_t *const y, int y_stride, 1134 const uint8_t *const u, const uint8_t *const v, 1135 int uv_stride, int height, int width, uint8_t *oy, 1136 int oy_stride, uint8_t *ou, uint8_t *ov, 1137 int ouv_stride, int oheight, int owidth) { 1138 av1_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride); 1139 av1_resize_plane(u, height, width, uv_stride, ou, oheight, owidth, 1140 ouv_stride); 1141 av1_resize_plane(v, height, width, uv_stride, ov, oheight, owidth, 1142 ouv_stride); 1143 } 1144 1145 void av1_highbd_resize_frame420(const uint8_t *const y, int y_stride, 1146 const uint8_t *const u, const uint8_t *const v, 1147 int uv_stride, int height, int width, 1148 uint8_t *oy, int oy_stride, uint8_t *ou, 1149 uint8_t *ov, int ouv_stride, int oheight, 1150 int owidth, int bd) { 1151 av1_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth, 1152 oy_stride, bd); 1153 av1_highbd_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2, 1154 owidth / 2, ouv_stride, bd); 1155 av1_highbd_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2, 1156 owidth / 2, ouv_stride, bd); 1157 } 1158 1159 void av1_highbd_resize_frame422(const uint8_t *const y, int y_stride, 1160 const uint8_t *const u, const uint8_t *const v, 1161 int uv_stride, int height, int width, 1162 uint8_t *oy, int oy_stride, uint8_t *ou, 1163 uint8_t *ov, int ouv_stride, int oheight, 1164 int owidth, int bd) { 1165 av1_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth, 1166 oy_stride, bd); 1167 av1_highbd_resize_plane(u, height, width / 2, uv_stride, ou, oheight, 1168 owidth / 2, ouv_stride, bd); 1169 av1_highbd_resize_plane(v, height, width / 2, uv_stride, ov, oheight, 1170 owidth / 2, ouv_stride, bd); 1171 } 1172 1173 void av1_highbd_resize_frame444(const uint8_t *const y, int y_stride, 1174 const uint8_t *const u, const uint8_t *const v, 1175 int uv_stride, int height, int width, 1176 uint8_t *oy, int oy_stride, uint8_t *ou, 1177 uint8_t *ov, int ouv_stride, int oheight, 1178 int owidth, int bd) { 1179 av1_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth, 1180 oy_stride, bd); 1181 av1_highbd_resize_plane(u, height, width, uv_stride, ou, oheight, owidth, 1182 ouv_stride, bd); 1183 av1_highbd_resize_plane(v, height, width, uv_stride, ov, oheight, owidth, 1184 ouv_stride, bd); 1185 } 1186 1187 void av1_resize_and_extend_frame(const YV12_BUFFER_CONFIG *src, 1188 YV12_BUFFER_CONFIG *dst, int bd, 1189 const int num_planes) { 1190 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with aom_image_t 1191 1192 // We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet 1193 // the static analysis warnings. 1194 for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); ++i) { 1195 const int is_uv = i > 0; 1196 if (src->flags & YV12_FLAG_HIGHBITDEPTH) 1197 av1_highbd_resize_plane(src->buffers[i], src->crop_heights[is_uv], 1198 src->crop_widths[is_uv], src->strides[is_uv], 1199 dst->buffers[i], dst->crop_heights[is_uv], 1200 dst->crop_widths[is_uv], dst->strides[is_uv], bd); 1201 else 1202 av1_resize_plane(src->buffers[i], src->crop_heights[is_uv], 1203 src->crop_widths[is_uv], src->strides[is_uv], 1204 dst->buffers[i], dst->crop_heights[is_uv], 1205 dst->crop_widths[is_uv], dst->strides[is_uv]); 1206 } 1207 aom_extend_frame_borders(dst, num_planes); 1208 } 1209 1210 void av1_upscale_normative_rows(const AV1_COMMON *cm, const uint8_t *src, 1211 int src_stride, uint8_t *dst, int dst_stride, 1212 int plane, int rows) { 1213 const int is_uv = (plane > 0); 1214 const int ss_x = is_uv && cm->seq_params.subsampling_x; 1215 const int downscaled_plane_width = ROUND_POWER_OF_TWO(cm->width, ss_x); 1216 const int upscaled_plane_width = 1217 ROUND_POWER_OF_TWO(cm->superres_upscaled_width, ss_x); 1218 const int superres_denom = cm->superres_scale_denominator; 1219 1220 TileInfo tile_col; 1221 const int32_t x_step_qn = av1_get_upscale_convolve_step( 1222 downscaled_plane_width, upscaled_plane_width); 1223 int32_t x0_qn = get_upscale_convolve_x0(downscaled_plane_width, 1224 upscaled_plane_width, x_step_qn); 1225 1226 for (int j = 0; j < cm->tile_cols; j++) { 1227 av1_tile_set_col(&tile_col, cm, j); 1228 // Determine the limits of this tile column in both the source 1229 // and destination images. 1230 // Note: The actual location which we start sampling from is 1231 // (downscaled_x0 - 1 + (x0_qn/2^14)), and this quantity increases 1232 // by exactly dst_width * (x_step_qn/2^14) pixels each iteration. 1233 const int downscaled_x0 = tile_col.mi_col_start << (MI_SIZE_LOG2 - ss_x); 1234 const int downscaled_x1 = tile_col.mi_col_end << (MI_SIZE_LOG2 - ss_x); 1235 const int src_width = downscaled_x1 - downscaled_x0; 1236 1237 const int upscaled_x0 = (downscaled_x0 * superres_denom) / SCALE_NUMERATOR; 1238 int upscaled_x1; 1239 if (j == cm->tile_cols - 1) { 1240 // Note that we can't just use AOMMIN here - due to rounding, 1241 // (downscaled_x1 * superres_denom) / SCALE_NUMERATOR may be less than 1242 // upscaled_plane_width. 1243 upscaled_x1 = upscaled_plane_width; 1244 } else { 1245 upscaled_x1 = (downscaled_x1 * superres_denom) / SCALE_NUMERATOR; 1246 } 1247 1248 const uint8_t *const src_ptr = src + downscaled_x0; 1249 uint8_t *const dst_ptr = dst + upscaled_x0; 1250 const int dst_width = upscaled_x1 - upscaled_x0; 1251 1252 const int pad_left = (j == 0); 1253 const int pad_right = (j == cm->tile_cols - 1); 1254 1255 if (cm->seq_params.use_highbitdepth) 1256 highbd_upscale_normative_rect(src_ptr, rows, src_width, src_stride, 1257 dst_ptr, rows, dst_width, dst_stride, 1258 x_step_qn, x0_qn, pad_left, pad_right, 1259 cm->seq_params.bit_depth); 1260 else 1261 upscale_normative_rect(src_ptr, rows, src_width, src_stride, dst_ptr, 1262 rows, dst_width, dst_stride, x_step_qn, x0_qn, 1263 pad_left, pad_right); 1264 1265 // Update the fractional pixel offset to prepare for the next tile column. 1266 x0_qn += (dst_width * x_step_qn) - (src_width << RS_SCALE_SUBPEL_BITS); 1267 } 1268 } 1269 1270 void av1_upscale_normative_and_extend_frame(const AV1_COMMON *cm, 1271 const YV12_BUFFER_CONFIG *src, 1272 YV12_BUFFER_CONFIG *dst) { 1273 const int num_planes = av1_num_planes(cm); 1274 for (int i = 0; i < num_planes; ++i) { 1275 const int is_uv = (i > 0); 1276 av1_upscale_normative_rows(cm, src->buffers[i], src->strides[is_uv], 1277 dst->buffers[i], dst->strides[is_uv], i, 1278 src->crop_heights[is_uv]); 1279 } 1280 1281 aom_extend_frame_borders(dst, num_planes); 1282 } 1283 1284 YV12_BUFFER_CONFIG *av1_scale_if_required(AV1_COMMON *cm, 1285 YV12_BUFFER_CONFIG *unscaled, 1286 YV12_BUFFER_CONFIG *scaled) { 1287 const int num_planes = av1_num_planes(cm); 1288 if (cm->width != unscaled->y_crop_width || 1289 cm->height != unscaled->y_crop_height) { 1290 av1_resize_and_extend_frame(unscaled, scaled, (int)cm->seq_params.bit_depth, 1291 num_planes); 1292 return scaled; 1293 } else { 1294 return unscaled; 1295 } 1296 } 1297 1298 // Calculates the scaled dimension given the original dimension and the scale 1299 // denominator. 1300 static void calculate_scaled_size_helper(int *dim, int denom) { 1301 if (denom != SCALE_NUMERATOR) { 1302 // We need to ensure the constraint in "Appendix A" of the spec: 1303 // * FrameWidth is greater than or equal to 16 1304 // * FrameHeight is greater than or equal to 16 1305 // For this, we clamp the downscaled dimension to at least 16. One 1306 // exception: if original dimension itself was < 16, then we keep the 1307 // downscaled dimension to be same as the original, to ensure that resizing 1308 // is valid. 1309 const int min_dim = AOMMIN(16, *dim); 1310 // Use this version if we need *dim to be even 1311 // *width = (*width * SCALE_NUMERATOR + denom) / (2 * denom); 1312 // *width <<= 1; 1313 *dim = (*dim * SCALE_NUMERATOR + denom / 2) / (denom); 1314 *dim = AOMMAX(*dim, min_dim); 1315 } 1316 } 1317 1318 void av1_calculate_scaled_size(int *width, int *height, int resize_denom) { 1319 calculate_scaled_size_helper(width, resize_denom); 1320 calculate_scaled_size_helper(height, resize_denom); 1321 } 1322 1323 void av1_calculate_scaled_superres_size(int *width, int *height, 1324 int superres_denom) { 1325 (void)height; 1326 calculate_scaled_size_helper(width, superres_denom); 1327 } 1328 1329 void av1_calculate_unscaled_superres_size(int *width, int *height, int denom) { 1330 if (denom != SCALE_NUMERATOR) { 1331 // Note: av1_calculate_scaled_superres_size() rounds *up* after division 1332 // when the resulting dimensions are odd. So here, we round *down*. 1333 *width = *width * denom / SCALE_NUMERATOR; 1334 (void)height; 1335 } 1336 } 1337 1338 // Copy only the config data from 'src' to 'dst'. 1339 static void copy_buffer_config(const YV12_BUFFER_CONFIG *const src, 1340 YV12_BUFFER_CONFIG *const dst) { 1341 dst->bit_depth = src->bit_depth; 1342 dst->color_primaries = src->color_primaries; 1343 dst->transfer_characteristics = src->transfer_characteristics; 1344 dst->matrix_coefficients = src->matrix_coefficients; 1345 dst->monochrome = src->monochrome; 1346 dst->chroma_sample_position = src->chroma_sample_position; 1347 dst->color_range = src->color_range; 1348 } 1349 1350 // TODO(afergs): Look for in-place upscaling 1351 // TODO(afergs): aom_ vs av1_ functions? Which can I use? 1352 // Upscale decoded image. 1353 void av1_superres_upscale(AV1_COMMON *cm, BufferPool *const pool) { 1354 const int num_planes = av1_num_planes(cm); 1355 if (!av1_superres_scaled(cm)) return; 1356 const SequenceHeader *const seq_params = &cm->seq_params; 1357 1358 YV12_BUFFER_CONFIG copy_buffer; 1359 memset(©_buffer, 0, sizeof(copy_buffer)); 1360 1361 YV12_BUFFER_CONFIG *const frame_to_show = &cm->cur_frame->buf; 1362 1363 const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, 3); 1364 if (aom_alloc_frame_buffer( 1365 ©_buffer, aligned_width, cm->height, seq_params->subsampling_x, 1366 seq_params->subsampling_y, seq_params->use_highbitdepth, 1367 AOM_BORDER_IN_PIXELS, cm->byte_alignment)) 1368 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR, 1369 "Failed to allocate copy buffer for superres upscaling"); 1370 1371 // Copy function assumes the frames are the same size. 1372 // Note that it does not copy YV12_BUFFER_CONFIG config data. 1373 aom_yv12_copy_frame(frame_to_show, ©_buffer, num_planes); 1374 1375 assert(copy_buffer.y_crop_width == aligned_width); 1376 assert(copy_buffer.y_crop_height == cm->height); 1377 1378 // Realloc the current frame buffer at a higher resolution in place. 1379 if (pool != NULL) { 1380 // Use callbacks if on the decoder. 1381 aom_codec_frame_buffer_t *fb = &cm->cur_frame->raw_frame_buffer; 1382 aom_release_frame_buffer_cb_fn_t release_fb_cb = pool->release_fb_cb; 1383 aom_get_frame_buffer_cb_fn_t cb = pool->get_fb_cb; 1384 void *cb_priv = pool->cb_priv; 1385 1386 // Realloc with callback does not release the frame buffer - release first. 1387 if (release_fb_cb(cb_priv, fb)) 1388 aom_internal_error( 1389 &cm->error, AOM_CODEC_MEM_ERROR, 1390 "Failed to free current frame buffer before superres upscaling"); 1391 1392 // aom_realloc_frame_buffer() leaves config data for frame_to_show intact 1393 if (aom_realloc_frame_buffer( 1394 frame_to_show, cm->superres_upscaled_width, 1395 cm->superres_upscaled_height, seq_params->subsampling_x, 1396 seq_params->subsampling_y, seq_params->use_highbitdepth, 1397 AOM_BORDER_IN_PIXELS, cm->byte_alignment, fb, cb, cb_priv)) 1398 aom_internal_error( 1399 &cm->error, AOM_CODEC_MEM_ERROR, 1400 "Failed to allocate current frame buffer for superres upscaling"); 1401 } else { 1402 // Make a copy of the config data for frame_to_show in copy_buffer 1403 copy_buffer_config(frame_to_show, ©_buffer); 1404 1405 // Don't use callbacks on the encoder. 1406 // aom_alloc_frame_buffer() clears the config data for frame_to_show 1407 if (aom_alloc_frame_buffer( 1408 frame_to_show, cm->superres_upscaled_width, 1409 cm->superres_upscaled_height, seq_params->subsampling_x, 1410 seq_params->subsampling_y, seq_params->use_highbitdepth, 1411 AOM_BORDER_IN_PIXELS, cm->byte_alignment)) 1412 aom_internal_error( 1413 &cm->error, AOM_CODEC_MEM_ERROR, 1414 "Failed to reallocate current frame buffer for superres upscaling"); 1415 1416 // Restore config data back to frame_to_show 1417 copy_buffer_config(©_buffer, frame_to_show); 1418 } 1419 // TODO(afergs): verify frame_to_show is correct after realloc 1420 // encoder: 1421 // decoder: 1422 1423 assert(frame_to_show->y_crop_width == cm->superres_upscaled_width); 1424 assert(frame_to_show->y_crop_height == cm->superres_upscaled_height); 1425 1426 // Scale up and back into frame_to_show. 1427 assert(frame_to_show->y_crop_width != cm->width); 1428 av1_upscale_normative_and_extend_frame(cm, ©_buffer, frame_to_show); 1429 1430 // Free the copy buffer 1431 aom_free_frame_buffer(©_buffer); 1432 } 1433