1 /*
2  *  Copyright (c) 2010 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 <assert.h>
12 #include "./vpx_config.h"
13 #include "./vpx_scale_rtcd.h"
14 #include "vpx/vpx_integer.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_ports/mem.h"
17 #include "vpx_scale/yv12config.h"
18 #if CONFIG_VP9_HIGHBITDEPTH
19 #include "vp9/common/vp9_common.h"
20 #endif
21 
extend_plane(uint8_t * const src,int src_stride,int width,int height,int extend_top,int extend_left,int extend_bottom,int extend_right)22 static void extend_plane(uint8_t *const src, int src_stride, int width,
23                          int height, int extend_top, int extend_left,
24                          int extend_bottom, int extend_right) {
25   int i;
26   const int linesize = extend_left + extend_right + width;
27 
28   /* copy the left and right most columns out */
29   uint8_t *src_ptr1 = src;
30   uint8_t *src_ptr2 = src + width - 1;
31   uint8_t *dst_ptr1 = src - extend_left;
32   uint8_t *dst_ptr2 = src + width;
33 
34   for (i = 0; i < height; ++i) {
35     memset(dst_ptr1, src_ptr1[0], extend_left);
36     memset(dst_ptr2, src_ptr2[0], extend_right);
37     src_ptr1 += src_stride;
38     src_ptr2 += src_stride;
39     dst_ptr1 += src_stride;
40     dst_ptr2 += src_stride;
41   }
42 
43   /* Now copy the top and bottom lines into each line of the respective
44    * borders
45    */
46   src_ptr1 = src - extend_left;
47   src_ptr2 = src + src_stride * (height - 1) - extend_left;
48   dst_ptr1 = src + src_stride * -extend_top - extend_left;
49   dst_ptr2 = src + src_stride * height - extend_left;
50 
51   for (i = 0; i < extend_top; ++i) {
52     memcpy(dst_ptr1, src_ptr1, linesize);
53     dst_ptr1 += src_stride;
54   }
55 
56   for (i = 0; i < extend_bottom; ++i) {
57     memcpy(dst_ptr2, src_ptr2, linesize);
58     dst_ptr2 += src_stride;
59   }
60 }
61 
62 #if CONFIG_VP9_HIGHBITDEPTH
extend_plane_high(uint8_t * const src8,int src_stride,int width,int height,int extend_top,int extend_left,int extend_bottom,int extend_right)63 static void extend_plane_high(uint8_t *const src8, int src_stride, int width,
64                               int height, int extend_top, int extend_left,
65                               int extend_bottom, int extend_right) {
66   int i;
67   const int linesize = extend_left + extend_right + width;
68   uint16_t *src = CONVERT_TO_SHORTPTR(src8);
69 
70   /* copy the left and right most columns out */
71   uint16_t *src_ptr1 = src;
72   uint16_t *src_ptr2 = src + width - 1;
73   uint16_t *dst_ptr1 = src - extend_left;
74   uint16_t *dst_ptr2 = src + width;
75 
76   for (i = 0; i < height; ++i) {
77     vpx_memset16(dst_ptr1, src_ptr1[0], extend_left);
78     vpx_memset16(dst_ptr2, src_ptr2[0], extend_right);
79     src_ptr1 += src_stride;
80     src_ptr2 += src_stride;
81     dst_ptr1 += src_stride;
82     dst_ptr2 += src_stride;
83   }
84 
85   /* Now copy the top and bottom lines into each line of the respective
86    * borders
87    */
88   src_ptr1 = src - extend_left;
89   src_ptr2 = src + src_stride * (height - 1) - extend_left;
90   dst_ptr1 = src + src_stride * -extend_top - extend_left;
91   dst_ptr2 = src + src_stride * height - extend_left;
92 
93   for (i = 0; i < extend_top; ++i) {
94     memcpy(dst_ptr1, src_ptr1, linesize * sizeof(uint16_t));
95     dst_ptr1 += src_stride;
96   }
97 
98   for (i = 0; i < extend_bottom; ++i) {
99     memcpy(dst_ptr2, src_ptr2, linesize * sizeof(uint16_t));
100     dst_ptr2 += src_stride;
101   }
102 }
103 #endif
104 
vp8_yv12_extend_frame_borders_c(YV12_BUFFER_CONFIG * ybf)105 void vp8_yv12_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
106   const int uv_border = ybf->border / 2;
107 
108   assert(ybf->border % 2 == 0);
109   assert(ybf->y_height - ybf->y_crop_height < 16);
110   assert(ybf->y_width - ybf->y_crop_width < 16);
111   assert(ybf->y_height - ybf->y_crop_height >= 0);
112   assert(ybf->y_width - ybf->y_crop_width >= 0);
113 
114   extend_plane(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
115                ybf->y_crop_height, ybf->border, ybf->border,
116                ybf->border + ybf->y_height - ybf->y_crop_height,
117                ybf->border + ybf->y_width - ybf->y_crop_width);
118 
119   extend_plane(ybf->u_buffer, ybf->uv_stride, ybf->uv_crop_width,
120                ybf->uv_crop_height, uv_border, uv_border,
121                uv_border + ybf->uv_height - ybf->uv_crop_height,
122                uv_border + ybf->uv_width - ybf->uv_crop_width);
123 
124   extend_plane(ybf->v_buffer, ybf->uv_stride, ybf->uv_crop_width,
125                ybf->uv_crop_height, uv_border, uv_border,
126                uv_border + ybf->uv_height - ybf->uv_crop_height,
127                uv_border + ybf->uv_width - ybf->uv_crop_width);
128 }
129 
130 #if CONFIG_VP9
extend_frame(YV12_BUFFER_CONFIG * const ybf,int ext_size)131 static void extend_frame(YV12_BUFFER_CONFIG *const ybf, int ext_size) {
132   const int c_w = ybf->uv_crop_width;
133   const int c_h = ybf->uv_crop_height;
134   const int ss_x = ybf->uv_width < ybf->y_width;
135   const int ss_y = ybf->uv_height < ybf->y_height;
136   const int c_et = ext_size >> ss_y;
137   const int c_el = ext_size >> ss_x;
138   const int c_eb = c_et + ybf->uv_height - ybf->uv_crop_height;
139   const int c_er = c_el + ybf->uv_width - ybf->uv_crop_width;
140 
141   assert(ybf->y_height - ybf->y_crop_height < 16);
142   assert(ybf->y_width - ybf->y_crop_width < 16);
143   assert(ybf->y_height - ybf->y_crop_height >= 0);
144   assert(ybf->y_width - ybf->y_crop_width >= 0);
145 
146 #if CONFIG_VP9_HIGHBITDEPTH
147   if (ybf->flags & YV12_FLAG_HIGHBITDEPTH) {
148     extend_plane_high(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
149                       ybf->y_crop_height, ext_size, ext_size,
150                       ext_size + ybf->y_height - ybf->y_crop_height,
151                       ext_size + ybf->y_width - ybf->y_crop_width);
152     extend_plane_high(ybf->u_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb,
153                       c_er);
154     extend_plane_high(ybf->v_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb,
155                       c_er);
156     return;
157   }
158 #endif
159   extend_plane(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
160                ybf->y_crop_height, ext_size, ext_size,
161                ext_size + ybf->y_height - ybf->y_crop_height,
162                ext_size + ybf->y_width - ybf->y_crop_width);
163 
164   extend_plane(ybf->u_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb, c_er);
165 
166   extend_plane(ybf->v_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb, c_er);
167 }
168 
vpx_extend_frame_borders_c(YV12_BUFFER_CONFIG * ybf)169 void vpx_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
170   extend_frame(ybf, ybf->border);
171 }
172 
vpx_extend_frame_inner_borders_c(YV12_BUFFER_CONFIG * ybf)173 void vpx_extend_frame_inner_borders_c(YV12_BUFFER_CONFIG *ybf) {
174   const int inner_bw = (ybf->border > VP9INNERBORDERINPIXELS)
175                            ? VP9INNERBORDERINPIXELS
176                            : ybf->border;
177   extend_frame(ybf, inner_bw);
178 }
179 
180 #if CONFIG_VP9_HIGHBITDEPTH
memcpy_short_addr(uint8_t * dst8,const uint8_t * src8,int num)181 static void memcpy_short_addr(uint8_t *dst8, const uint8_t *src8, int num) {
182   uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
183   uint16_t *src = CONVERT_TO_SHORTPTR(src8);
184   memcpy(dst, src, num * sizeof(uint16_t));
185 }
186 #endif  // CONFIG_VP9_HIGHBITDEPTH
187 #endif  // CONFIG_VP9
188 
189 // Copies the source image into the destination image and updates the
190 // destination's UMV borders.
191 // Note: The frames are assumed to be identical in size.
192 
vp8_yv12_copy_frame_c(const YV12_BUFFER_CONFIG * src_ybc,YV12_BUFFER_CONFIG * dst_ybc)193 void vp8_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_ybc,
194                            YV12_BUFFER_CONFIG *dst_ybc) {
195   int row;
196   const uint8_t *src = src_ybc->y_buffer;
197   uint8_t *dst = dst_ybc->y_buffer;
198 
199 #if 0
200   /* These assertions are valid in the codec, but the libvpx-tester uses
201    * this code slightly differently.
202    */
203   assert(src_ybc->y_width == dst_ybc->y_width);
204   assert(src_ybc->y_height == dst_ybc->y_height);
205 #endif
206 
207   for (row = 0; row < src_ybc->y_height; ++row) {
208     memcpy(dst, src, src_ybc->y_width);
209     src += src_ybc->y_stride;
210     dst += dst_ybc->y_stride;
211   }
212 
213   src = src_ybc->u_buffer;
214   dst = dst_ybc->u_buffer;
215 
216   for (row = 0; row < src_ybc->uv_height; ++row) {
217     memcpy(dst, src, src_ybc->uv_width);
218     src += src_ybc->uv_stride;
219     dst += dst_ybc->uv_stride;
220   }
221 
222   src = src_ybc->v_buffer;
223   dst = dst_ybc->v_buffer;
224 
225   for (row = 0; row < src_ybc->uv_height; ++row) {
226     memcpy(dst, src, src_ybc->uv_width);
227     src += src_ybc->uv_stride;
228     dst += dst_ybc->uv_stride;
229   }
230 
231   vp8_yv12_extend_frame_borders_c(dst_ybc);
232 }
233 
234 #if CONFIG_VP9
vpx_yv12_copy_frame_c(const YV12_BUFFER_CONFIG * src_ybc,YV12_BUFFER_CONFIG * dst_ybc)235 void vpx_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_ybc,
236                            YV12_BUFFER_CONFIG *dst_ybc) {
237   int row;
238   const uint8_t *src = src_ybc->y_buffer;
239   uint8_t *dst = dst_ybc->y_buffer;
240 
241 #if 0
242   /* These assertions are valid in the codec, but the libvpx-tester uses
243    * this code slightly differently.
244    */
245   assert(src_ybc->y_width == dst_ybc->y_width);
246   assert(src_ybc->y_height == dst_ybc->y_height);
247 #endif
248 
249 #if CONFIG_VP9_HIGHBITDEPTH
250   if (src_ybc->flags & YV12_FLAG_HIGHBITDEPTH) {
251     assert(dst_ybc->flags & YV12_FLAG_HIGHBITDEPTH);
252     for (row = 0; row < src_ybc->y_height; ++row) {
253       memcpy_short_addr(dst, src, src_ybc->y_width);
254       src += src_ybc->y_stride;
255       dst += dst_ybc->y_stride;
256     }
257 
258     src = src_ybc->u_buffer;
259     dst = dst_ybc->u_buffer;
260 
261     for (row = 0; row < src_ybc->uv_height; ++row) {
262       memcpy_short_addr(dst, src, src_ybc->uv_width);
263       src += src_ybc->uv_stride;
264       dst += dst_ybc->uv_stride;
265     }
266 
267     src = src_ybc->v_buffer;
268     dst = dst_ybc->v_buffer;
269 
270     for (row = 0; row < src_ybc->uv_height; ++row) {
271       memcpy_short_addr(dst, src, src_ybc->uv_width);
272       src += src_ybc->uv_stride;
273       dst += dst_ybc->uv_stride;
274     }
275 
276     vpx_extend_frame_borders_c(dst_ybc);
277     return;
278   } else {
279     assert(!(dst_ybc->flags & YV12_FLAG_HIGHBITDEPTH));
280   }
281 #endif
282 
283   for (row = 0; row < src_ybc->y_height; ++row) {
284     memcpy(dst, src, src_ybc->y_width);
285     src += src_ybc->y_stride;
286     dst += dst_ybc->y_stride;
287   }
288 
289   src = src_ybc->u_buffer;
290   dst = dst_ybc->u_buffer;
291 
292   for (row = 0; row < src_ybc->uv_height; ++row) {
293     memcpy(dst, src, src_ybc->uv_width);
294     src += src_ybc->uv_stride;
295     dst += dst_ybc->uv_stride;
296   }
297 
298   src = src_ybc->v_buffer;
299   dst = dst_ybc->v_buffer;
300 
301   for (row = 0; row < src_ybc->uv_height; ++row) {
302     memcpy(dst, src, src_ybc->uv_width);
303     src += src_ybc->uv_stride;
304     dst += dst_ybc->uv_stride;
305   }
306 
307   vpx_extend_frame_borders_c(dst_ybc);
308 }
309 #endif  // CONFIG_VP9
310 
vpx_yv12_copy_y_c(const YV12_BUFFER_CONFIG * src_ybc,YV12_BUFFER_CONFIG * dst_ybc)311 void vpx_yv12_copy_y_c(const YV12_BUFFER_CONFIG *src_ybc,
312                        YV12_BUFFER_CONFIG *dst_ybc) {
313   int row;
314   const uint8_t *src = src_ybc->y_buffer;
315   uint8_t *dst = dst_ybc->y_buffer;
316 
317 #if CONFIG_VP9_HIGHBITDEPTH
318   if (src_ybc->flags & YV12_FLAG_HIGHBITDEPTH) {
319     const uint16_t *src16 = CONVERT_TO_SHORTPTR(src);
320     uint16_t *dst16 = CONVERT_TO_SHORTPTR(dst);
321     for (row = 0; row < src_ybc->y_height; ++row) {
322       memcpy(dst16, src16, src_ybc->y_width * sizeof(uint16_t));
323       src16 += src_ybc->y_stride;
324       dst16 += dst_ybc->y_stride;
325     }
326     return;
327   }
328 #endif
329 
330   for (row = 0; row < src_ybc->y_height; ++row) {
331     memcpy(dst, src, src_ybc->y_width);
332     src += src_ybc->y_stride;
333     dst += dst_ybc->y_stride;
334   }
335 }
336