1 /*
2  *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "./vp9_rtcd.h"
12 #include "./vpx_dsp_rtcd.h"
13 #include "./vpx_scale_rtcd.h"
14 #include "vp9/common/vp9_blockd.h"
15 #include "vpx_dsp/vpx_filter.h"
16 #include "vpx_scale/yv12config.h"
17 
vp9_scale_and_extend_frame_c(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,INTERP_FILTER filter_type,int phase_scaler)18 void vp9_scale_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
19                                   YV12_BUFFER_CONFIG *dst,
20                                   INTERP_FILTER filter_type, int phase_scaler) {
21   const int src_w = src->y_crop_width;
22   const int src_h = src->y_crop_height;
23   const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
24                                    src->v_buffer };
25   const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
26   uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
27   const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
28   const InterpKernel *const kernel = vp9_filter_kernels[filter_type];
29   int x, y, i;
30 
31 #if HAVE_SSSE3 || HAVE_NEON
32   // TODO(linfengz): The 4:3 specialized C code is disabled by default since
33   // it's much slower than the general version which calls vpx_scaled_2d() even
34   // if vpx_scaled_2d() is not optimized. It will only be enabled as a reference
35   // for the platforms which have faster optimization.
36   if (4 * dst->y_crop_width == 3 * src_w &&
37       4 * dst->y_crop_height == 3 * src_h) {
38     // Specialize 4 to 3 scaling.
39     // Example pixel locations.
40     // (O: Original pixel. S: Scaled pixel. X: Overlapped pixel.)
41     //      phase_scaler = 0               |      phase_scaler = 8
42     //                                     |
43     //      X     O S   O   S O     X      |      O     O     O     O     O
44     //                                     |
45     //                                     |
46     //                                     |         S       S       S
47     //                                     |
48     //                                     |
49     //      O     O     O     O     O      |      O     O     O     O     O
50     //                                     |
51     //      S       S       S       S      |
52     //                                     |
53     //                                     |
54     //                                     |         S       S       S
55     //      O     O     O     O     O      |      O     O     O     O     O
56     //                                     |
57     //                                     |
58     //                                     |
59     //      S       S       S       S      |
60     //                                     |
61     //      O     O     O     O     O      |      O     O     O     O     O
62     //                                     |         S       S       S
63     //                                     |
64     //                                     |
65     //                                     |
66     //                                     |
67     //      X     O S   O   S O     X      |      O     O     O     O     O
68 
69     const int dst_ws[3] = { dst->y_crop_width, dst->uv_crop_width,
70                             dst->uv_crop_width };
71     const int dst_hs[3] = { dst->y_crop_height, dst->uv_crop_height,
72                             dst->uv_crop_height };
73     for (i = 0; i < MAX_MB_PLANE; ++i) {
74       const int dst_w = dst_ws[i];
75       const int dst_h = dst_hs[i];
76       const int src_stride = src_strides[i];
77       const int dst_stride = dst_strides[i];
78       for (y = 0; y < dst_h; y += 3) {
79         for (x = 0; x < dst_w; x += 3) {
80           const uint8_t *src_ptr = srcs[i] + 4 * y / 3 * src_stride + 4 * x / 3;
81           uint8_t *dst_ptr = dsts[i] + y * dst_stride + x;
82 
83           // Must call c function because its optimization doesn't support 3x3.
84           vpx_scaled_2d_c(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
85                           phase_scaler, 64 / 3, phase_scaler, 64 / 3, 3, 3);
86         }
87       }
88     }
89   } else
90 #endif
91   {
92     const int dst_w = dst->y_crop_width;
93     const int dst_h = dst->y_crop_height;
94     for (i = 0; i < MAX_MB_PLANE; ++i) {
95       const int factor = (i == 0 || i == 3 ? 1 : 2);
96       const int src_stride = src_strides[i];
97       const int dst_stride = dst_strides[i];
98       for (y = 0; y < dst_h; y += 16) {
99         const int y_q4 = y * (16 / factor) * src_h / dst_h + phase_scaler;
100         for (x = 0; x < dst_w; x += 16) {
101           const int x_q4 = x * (16 / factor) * src_w / dst_w + phase_scaler;
102           const uint8_t *src_ptr = srcs[i] +
103                                    (y / factor) * src_h / dst_h * src_stride +
104                                    (x / factor) * src_w / dst_w;
105           uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
106 
107           vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride, kernel,
108                         x_q4 & 0xf, 16 * src_w / dst_w, y_q4 & 0xf,
109                         16 * src_h / dst_h, 16 / factor, 16 / factor);
110         }
111       }
112     }
113   }
114 
115   vpx_extend_frame_borders(dst);
116 }
117