yv12extend.c 5.87 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
 */

11
#include <assert.h>
John Koleszar's avatar
John Koleszar committed
12
#include "./vpx_config.h"
13
#include "vpx/vpx_integer.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vpx_mem/vpx_mem.h"
15
#include "vpx_scale/yv12config.h"
John Koleszar's avatar
John Koleszar committed
16

17
static void extend_plane(uint8_t *const src, int src_stride,
18
19
20
                         int width, int height,
                         int extend_top, int extend_left,
                         int extend_bottom, int extend_right) {
John Koleszar's avatar
John Koleszar committed
21
  int i;
22
  const int linesize = extend_left + extend_right + width;
John Koleszar's avatar
John Koleszar committed
23
24

  /* copy the left and right most columns out */
25
26
27
28
29
30
31
32
33
34
35
36
  uint8_t *src_ptr1 = src;
  uint8_t *src_ptr2 = src + width - 1;
  uint8_t *dst_ptr1 = src - extend_left;
  uint8_t *dst_ptr2 = src + width;

  for (i = 0; i < height; ++i) {
    vpx_memset(dst_ptr1, src_ptr1[0], extend_left);
    vpx_memset(dst_ptr2, src_ptr2[0], extend_right);
    src_ptr1 += src_stride;
    src_ptr2 += src_stride;
    dst_ptr1 += src_stride;
    dst_ptr2 += src_stride;
John Koleszar's avatar
John Koleszar committed
37
38
  }

John Koleszar's avatar
John Koleszar committed
39
40
41
  /* Now copy the top and bottom lines into each line of the respective
   * borders
   */
42
43
44
45
46
47
48
49
  src_ptr1 = src - extend_left;
  src_ptr2 = src + src_stride * (height - 1) - extend_left;
  dst_ptr1 = src + src_stride * -extend_top - extend_left;
  dst_ptr2 = src + src_stride * height - extend_left;

  for (i = 0; i < extend_top; ++i) {
    vpx_memcpy(dst_ptr1, src_ptr1, linesize);
    dst_ptr1 += src_stride;
John Koleszar's avatar
John Koleszar committed
50
51
  }

52
53
54
  for (i = 0; i < extend_bottom; ++i) {
    vpx_memcpy(dst_ptr2, src_ptr2, linesize);
    dst_ptr2 += src_stride;
John Koleszar's avatar
John Koleszar committed
55
  }
John Koleszar's avatar
John Koleszar committed
56
57
}

58
void vp8_yv12_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
John Koleszar's avatar
John Koleszar committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  assert(ybf->y_height - ybf->y_crop_height < 16);
  assert(ybf->y_width - ybf->y_crop_width < 16);
  assert(ybf->y_height - ybf->y_crop_height >= 0);
  assert(ybf->y_width - ybf->y_crop_width >= 0);

  extend_plane(ybf->y_buffer, ybf->y_stride,
               ybf->y_crop_width, ybf->y_crop_height,
               ybf->border, ybf->border,
               ybf->border + ybf->y_height - ybf->y_crop_height,
               ybf->border + ybf->y_width - ybf->y_crop_width);

  extend_plane(ybf->u_buffer, ybf->uv_stride,
               (ybf->y_crop_width + 1) / 2, (ybf->y_crop_height + 1) / 2,
               ybf->border / 2, ybf->border / 2,
               (ybf->border + ybf->y_height - ybf->y_crop_height + 1) / 2,
               (ybf->border + ybf->y_width - ybf->y_crop_width + 1) / 2);

  extend_plane(ybf->v_buffer, ybf->uv_stride,
               (ybf->y_crop_width + 1) / 2, (ybf->y_crop_height + 1) / 2,
               ybf->border / 2, ybf->border / 2,
               (ybf->border + ybf->y_height - ybf->y_crop_height + 1) / 2,
               (ybf->border + ybf->y_width - ybf->y_crop_width + 1) / 2);
John Koleszar's avatar
John Koleszar committed
81
82
}

John Koleszar's avatar
John Koleszar committed
83
#if CONFIG_VP9
84
static void extend_frame(YV12_BUFFER_CONFIG *const ybf, int ext_size) {
85
86
87
88
89
90
91
  const int c_w = ybf->uv_crop_width;
  const int c_h = ybf->uv_crop_height;
  const int c_ext_size = ext_size >> 1;
  const int c_et = c_ext_size;
  const int c_el = c_ext_size;
  const int c_eb = c_ext_size + ybf->uv_height - ybf->uv_crop_height;
  const int c_er = c_ext_size + ybf->uv_width - ybf->uv_crop_width;
John Koleszar's avatar
John Koleszar committed
92
93
94
95
96
97
98
99

  assert(ybf->y_height - ybf->y_crop_height < 16);
  assert(ybf->y_width - ybf->y_crop_width < 16);
  assert(ybf->y_height - ybf->y_crop_height >= 0);
  assert(ybf->y_width - ybf->y_crop_width >= 0);

  extend_plane(ybf->y_buffer, ybf->y_stride,
               ybf->y_crop_width, ybf->y_crop_height,
100
101
102
               ext_size, ext_size,
               ext_size + ybf->y_height - ybf->y_crop_height,
               ext_size + ybf->y_width - ybf->y_crop_width);
John Koleszar's avatar
John Koleszar committed
103
104
105
106
107
108
109

  extend_plane(ybf->u_buffer, ybf->uv_stride,
               c_w, c_h, c_et, c_el, c_eb, c_er);

  extend_plane(ybf->v_buffer, ybf->uv_stride,
               c_w, c_h, c_et, c_el, c_eb, c_er);
}
110

111
112
void vp9_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
  extend_frame(ybf, ybf->border);
113
114
}

115
void vp9_extend_frame_inner_borders_c(YV12_BUFFER_CONFIG *ybf) {
116
  const int inner_bw = (ybf->border > VP9INNERBORDERINPIXELS) ?
James Zern's avatar
James Zern committed
117
                       VP9INNERBORDERINPIXELS : ybf->border;
118
  extend_frame(ybf, inner_bw);
119
}
120
#endif  // CONFIG_VP9
John Koleszar's avatar
John Koleszar committed
121

122
123
124
// Copies the source image into the destination image and updates the
// destination's UMV borders.
// Note: The frames are assumed to be identical in size.
125
void vp8_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_ybc,
126
                           YV12_BUFFER_CONFIG *dst_ybc) {
John Koleszar's avatar
John Koleszar committed
127
  int row;
128
129
  const uint8_t *src = src_ybc->y_buffer;
  uint8_t *dst = dst_ybc->y_buffer;
John Koleszar's avatar
John Koleszar committed
130

John Koleszar's avatar
John Koleszar committed
131
132
133
134
#if 0
  /* These assertions are valid in the codec, but the libvpx-tester uses
   * this code slightly differently.
   */
135
136
  assert(src_ybc->y_width == dst_ybc->y_width);
  assert(src_ybc->y_height == dst_ybc->y_height);
John Koleszar's avatar
John Koleszar committed
137
#endif
138

139
140
141
142
  for (row = 0; row < src_ybc->y_height; ++row) {
    vpx_memcpy(dst, src, src_ybc->y_width);
    src += src_ybc->y_stride;
    dst += dst_ybc->y_stride;
John Koleszar's avatar
John Koleszar committed
143
144
  }

145
146
  src = src_ybc->u_buffer;
  dst = dst_ybc->u_buffer;
John Koleszar's avatar
John Koleszar committed
147

148
149
150
151
  for (row = 0; row < src_ybc->uv_height; ++row) {
    vpx_memcpy(dst, src, src_ybc->uv_width);
    src += src_ybc->uv_stride;
    dst += dst_ybc->uv_stride;
John Koleszar's avatar
John Koleszar committed
152
153
  }

154
155
  src = src_ybc->v_buffer;
  dst = dst_ybc->v_buffer;
John Koleszar's avatar
John Koleszar committed
156

157
158
159
160
  for (row = 0; row < src_ybc->uv_height; ++row) {
    vpx_memcpy(dst, src, src_ybc->uv_width);
    src += src_ybc->uv_stride;
    dst += dst_ybc->uv_stride;
John Koleszar's avatar
John Koleszar committed
161
162
  }

John Koleszar's avatar
John Koleszar committed
163
  vp8_yv12_extend_frame_borders_c(dst_ybc);
John Koleszar's avatar
John Koleszar committed
164
165
}

166
void vpx_yv12_copy_y_c(const YV12_BUFFER_CONFIG *src_ybc,
John Koleszar's avatar
John Koleszar committed
167
                       YV12_BUFFER_CONFIG *dst_ybc) {
John Koleszar's avatar
John Koleszar committed
168
  int row;
169
170
  const uint8_t *src = src_ybc->y_buffer;
  uint8_t *dst = dst_ybc->y_buffer;
John Koleszar's avatar
John Koleszar committed
171

172
173
174
175
  for (row = 0; row < src_ybc->y_height; ++row) {
    vpx_memcpy(dst, src, src_ybc->y_width);
    src += src_ybc->y_stride;
    dst += dst_ybc->y_stride;
John Koleszar's avatar
John Koleszar committed
176
  }
John Koleszar's avatar
John Koleszar committed
177
}