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/vpx_integer.h"
14#include "vpx_mem/vpx_mem.h"
15#include "vpx_scale/yv12config.h"
16
17static void extend_plane(uint8_t *const src, int src_stride,
18                         int width, int height,
19                         int extend_top, int extend_left,
20                         int extend_bottom, int extend_right) {
21  int i;
22  const int linesize = extend_left + extend_right + width;
23
24  /* copy the left and right most columns out */
25  uint8_t *src_ptr1 = src;
26  uint8_t *src_ptr2 = src + width - 1;
27  uint8_t *dst_ptr1 = src - extend_left;
28  uint8_t *dst_ptr2 = src + width;
29
30  for (i = 0; i < height; ++i) {
31    vpx_memset(dst_ptr1, src_ptr1[0], extend_left);
32    vpx_memset(dst_ptr2, src_ptr2[0], extend_right);
33    src_ptr1 += src_stride;
34    src_ptr2 += src_stride;
35    dst_ptr1 += src_stride;
36    dst_ptr2 += src_stride;
37  }
38
39  /* Now copy the top and bottom lines into each line of the respective
40   * borders
41   */
42  src_ptr1 = src - extend_left;
43  src_ptr2 = src + src_stride * (height - 1) - extend_left;
44  dst_ptr1 = src + src_stride * -extend_top - extend_left;
45  dst_ptr2 = src + src_stride * height - extend_left;
46
47  for (i = 0; i < extend_top; ++i) {
48    vpx_memcpy(dst_ptr1, src_ptr1, linesize);
49    dst_ptr1 += src_stride;
50  }
51
52  for (i = 0; i < extend_bottom; ++i) {
53    vpx_memcpy(dst_ptr2, src_ptr2, linesize);
54    dst_ptr2 += src_stride;
55  }
56}
57
58void vp8_yv12_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
59  assert(ybf->y_height - ybf->y_crop_height < 16);
60  assert(ybf->y_width - ybf->y_crop_width < 16);
61  assert(ybf->y_height - ybf->y_crop_height >= 0);
62  assert(ybf->y_width - ybf->y_crop_width >= 0);
63
64  extend_plane(ybf->y_buffer, ybf->y_stride,
65               ybf->y_crop_width, ybf->y_crop_height,
66               ybf->border, ybf->border,
67               ybf->border + ybf->y_height - ybf->y_crop_height,
68               ybf->border + ybf->y_width - ybf->y_crop_width);
69
70  extend_plane(ybf->u_buffer, ybf->uv_stride,
71               (ybf->y_crop_width + 1) / 2, (ybf->y_crop_height + 1) / 2,
72               ybf->border / 2, ybf->border / 2,
73               (ybf->border + ybf->y_height - ybf->y_crop_height + 1) / 2,
74               (ybf->border + ybf->y_width - ybf->y_crop_width + 1) / 2);
75
76  extend_plane(ybf->v_buffer, ybf->uv_stride,
77               (ybf->y_crop_width + 1) / 2, (ybf->y_crop_height + 1) / 2,
78               ybf->border / 2, ybf->border / 2,
79               (ybf->border + ybf->y_height - ybf->y_crop_height + 1) / 2,
80               (ybf->border + ybf->y_width - ybf->y_crop_width + 1) / 2);
81}
82
83#if CONFIG_VP9
84static void extend_frame(YV12_BUFFER_CONFIG *const ybf,
85                         int subsampling_x, int subsampling_y,
86                         int ext_size) {
87  const int c_w = (ybf->y_crop_width + subsampling_x) >> subsampling_x;
88  const int c_h = (ybf->y_crop_height + subsampling_y) >> subsampling_y;
89  const int c_et = ext_size >> subsampling_y;
90  const int c_el = ext_size >> subsampling_x;
91  const int c_eb = (ext_size + ybf->y_height - ybf->y_crop_height +
92                    subsampling_y) >> subsampling_y;
93  const int c_er = (ext_size + ybf->y_width - ybf->y_crop_width +
94                    subsampling_x) >> subsampling_x;
95
96  assert(ybf->y_height - ybf->y_crop_height < 16);
97  assert(ybf->y_width - ybf->y_crop_width < 16);
98  assert(ybf->y_height - ybf->y_crop_height >= 0);
99  assert(ybf->y_width - ybf->y_crop_width >= 0);
100
101  extend_plane(ybf->y_buffer, ybf->y_stride,
102               ybf->y_crop_width, ybf->y_crop_height,
103               ext_size, ext_size,
104               ext_size + ybf->y_height - ybf->y_crop_height,
105               ext_size + ybf->y_width - ybf->y_crop_width);
106
107  extend_plane(ybf->u_buffer, ybf->uv_stride,
108               c_w, c_h, c_et, c_el, c_eb, c_er);
109
110  extend_plane(ybf->v_buffer, ybf->uv_stride,
111               c_w, c_h, c_et, c_el, c_eb, c_er);
112}
113
114void vp9_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf,
115                                int subsampling_x, int subsampling_y) {
116  extend_frame(ybf, subsampling_x, subsampling_y, ybf->border);
117}
118
119void vp9_extend_frame_inner_borders_c(YV12_BUFFER_CONFIG *ybf,
120                                      int subsampling_x, int subsampling_y) {
121  const int inner_bw = (ybf->border > VP9INNERBORDERINPIXELS) ?
122                       VP9INNERBORDERINPIXELS : ybf->border;
123  extend_frame(ybf, subsampling_x, subsampling_y, inner_bw);
124}
125#endif  // CONFIG_VP9
126
127// Copies the source image into the destination image and updates the
128// destination's UMV borders.
129// Note: The frames are assumed to be identical in size.
130void vp8_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_ybc,
131                           YV12_BUFFER_CONFIG *dst_ybc) {
132  int row;
133  const uint8_t *src = src_ybc->y_buffer;
134  uint8_t *dst = dst_ybc->y_buffer;
135
136#if 0
137  /* These assertions are valid in the codec, but the libvpx-tester uses
138   * this code slightly differently.
139   */
140  assert(src_ybc->y_width == dst_ybc->y_width);
141  assert(src_ybc->y_height == dst_ybc->y_height);
142#endif
143
144  for (row = 0; row < src_ybc->y_height; ++row) {
145    vpx_memcpy(dst, src, src_ybc->y_width);
146    src += src_ybc->y_stride;
147    dst += dst_ybc->y_stride;
148  }
149
150  src = src_ybc->u_buffer;
151  dst = dst_ybc->u_buffer;
152
153  for (row = 0; row < src_ybc->uv_height; ++row) {
154    vpx_memcpy(dst, src, src_ybc->uv_width);
155    src += src_ybc->uv_stride;
156    dst += dst_ybc->uv_stride;
157  }
158
159  src = src_ybc->v_buffer;
160  dst = dst_ybc->v_buffer;
161
162  for (row = 0; row < src_ybc->uv_height; ++row) {
163    vpx_memcpy(dst, src, src_ybc->uv_width);
164    src += src_ybc->uv_stride;
165    dst += dst_ybc->uv_stride;
166  }
167
168  vp8_yv12_extend_frame_borders_c(dst_ybc);
169}
170
171void vpx_yv12_copy_y_c(const YV12_BUFFER_CONFIG *src_ybc,
172                       YV12_BUFFER_CONFIG *dst_ybc) {
173  int row;
174  const uint8_t *src = src_ybc->y_buffer;
175  uint8_t *dst = dst_ybc->y_buffer;
176
177  for (row = 0; row < src_ybc->y_height; ++row) {
178    vpx_memcpy(dst, src, src_ybc->y_width);
179    src += src_ybc->y_stride;
180    dst += dst_ybc->y_stride;
181  }
182}
183