rotate_argb.cc 7.54 KB
Newer Older
1
/*
2
 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 4 5 6
 *
 *  Use of this source code is governed by a BSD-style license
 *  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 9 10 11 12 13
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "libyuv/rotate.h"

#include "libyuv/convert.h"
14
#include "libyuv/cpu_id.h"
15
#include "libyuv/planar_functions.h"
16
#include "libyuv/row.h"
17 18 19 20 21 22 23 24

#ifdef __cplusplus
namespace libyuv {
extern "C" {
#endif

// ARGBScale has a function to copy pixels to a row, striding each source
// pixel by a constant.
Frank Barchard's avatar
Frank Barchard committed
25 26 27 28
#if !defined(LIBYUV_DISABLE_X86) &&                          \
    (defined(_M_IX86) ||                                     \
     (defined(__x86_64__) && !defined(__native_client__)) || \
     defined(__i386__))
29
#define HAS_SCALEARGBROWDOWNEVEN_SSE2
Frank Barchard's avatar
Frank Barchard committed
30 31 32 33 34
void ScaleARGBRowDownEven_SSE2(const uint8* src_ptr,
                               int src_stride,
                               int src_stepx,
                               uint8* dst_ptr,
                               int dst_width);
35
#endif
36
#if !defined(LIBYUV_DISABLE_NEON) && !defined(__native_client__) && \
37
    (defined(__ARM_NEON__) || defined(LIBYUV_NEON) || defined(__aarch64__))
38
#define HAS_SCALEARGBROWDOWNEVEN_NEON
Frank Barchard's avatar
Frank Barchard committed
39 40 41 42 43
void ScaleARGBRowDownEven_NEON(const uint8* src_ptr,
                               int src_stride,
                               int src_stepx,
                               uint8* dst_ptr,
                               int dst_width);
44 45
#endif

Frank Barchard's avatar
Frank Barchard committed
46 47 48 49 50
void ScaleARGBRowDownEven_C(const uint8* src_ptr,
                            int,
                            int src_stepx,
                            uint8* dst_ptr,
                            int dst_width);
51

Frank Barchard's avatar
Frank Barchard committed
52 53 54 55 56 57
static void ARGBTranspose(const uint8* src,
                          int src_stride,
                          uint8* dst,
                          int dst_stride,
                          int width,
                          int height) {
58
  int i;
59
  int src_pixel_step = src_stride >> 2;
60
  void (*ScaleARGBRowDownEven)(const uint8* src_ptr, int src_stride,
Frank Barchard's avatar
Frank Barchard committed
61 62
                               int src_step, uint8* dst_ptr, int dst_width) =
      ScaleARGBRowDownEven_C;
63
#if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
64
  if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(height, 4)) {  // Width of dest.
65 66
    ScaleARGBRowDownEven = ScaleARGBRowDownEven_SSE2;
  }
67 68
#endif
#if defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
69
  if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(height, 4)) {  // Width of dest.
70 71
    ScaleARGBRowDownEven = ScaleARGBRowDownEven_NEON;
  }
72 73
#endif

74
  for (i = 0; i < width; ++i) {  // column of source to row of dest.
75 76 77 78 79 80
    ScaleARGBRowDownEven(src, 0, src_pixel_step, dst, height);
    dst += dst_stride;
    src += 4;
  }
}

Frank Barchard's avatar
Frank Barchard committed
81 82 83 84 85 86
void ARGBRotate90(const uint8* src,
                  int src_stride,
                  uint8* dst,
                  int dst_stride,
                  int width,
                  int height) {
87
  // Rotate by 90 is a ARGBTranspose with the source read
88
  // from bottom to top. So set the source pointer to the end
89 90 91 92 93 94
  // of the buffer and flip the sign of the source stride.
  src += src_stride * (height - 1);
  src_stride = -src_stride;
  ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
}

Frank Barchard's avatar
Frank Barchard committed
95 96 97 98 99 100
void ARGBRotate270(const uint8* src,
                   int src_stride,
                   uint8* dst,
                   int dst_stride,
                   int width,
                   int height) {
101
  // Rotate by 270 is a ARGBTranspose with the destination written
102
  // from bottom to top. So set the destination pointer to the end
103 104 105 106 107 108
  // of the buffer and flip the sign of the destination stride.
  dst += dst_stride * (width - 1);
  dst_stride = -dst_stride;
  ARGBTranspose(src, src_stride, dst, dst_stride, width, height);
}

Frank Barchard's avatar
Frank Barchard committed
109 110 111 112 113 114
void ARGBRotate180(const uint8* src,
                   int src_stride,
                   uint8* dst,
                   int dst_stride,
                   int width,
                   int height) {
115 116 117 118 119 120
  // Swap first and last row and mirror the content. Uses a temporary row.
  align_buffer_64(row, width * 4);
  const uint8* src_bot = src + src_stride * (height - 1);
  uint8* dst_bot = dst + dst_stride * (height - 1);
  int half_height = (height + 1) >> 1;
  int y;
121 122
  void (*ARGBMirrorRow)(const uint8* src, uint8* dst, int width) =
      ARGBMirrorRow_C;
123
  void (*CopyRow)(const uint8* src, uint8* dst, int width) = CopyRow_C;
fbarchard@google.com's avatar
fbarchard@google.com committed
124 125 126 127 128 129
#if defined(HAS_ARGBMIRRORROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
    if (IS_ALIGNED(width, 4)) {
      ARGBMirrorRow = ARGBMirrorRow_NEON;
    }
130
  }
fbarchard@google.com's avatar
fbarchard@google.com committed
131
#endif
132 133 134
#if defined(HAS_ARGBMIRRORROW_SSE2)
  if (TestCpuFlag(kCpuHasSSE2)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
135
    if (IS_ALIGNED(width, 4)) {
136
      ARGBMirrorRow = ARGBMirrorRow_SSE2;
fbarchard@google.com's avatar
fbarchard@google.com committed
137
    }
fbarchard@google.com's avatar
fbarchard@google.com committed
138 139
  }
#endif
fbarchard@google.com's avatar
fbarchard@google.com committed
140 141 142 143 144 145
#if defined(HAS_ARGBMIRRORROW_AVX2)
  if (TestCpuFlag(kCpuHasAVX2)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
    if (IS_ALIGNED(width, 8)) {
      ARGBMirrorRow = ARGBMirrorRow_AVX2;
    }
146
  }
147
#endif
148 149 150 151 152 153 154 155
#if defined(HAS_ARGBMIRRORROW_MSA)
  if (TestCpuFlag(kCpuHasMSA)) {
    ARGBMirrorRow = ARGBMirrorRow_Any_MSA;
    if (IS_ALIGNED(width, 16)) {
      ARGBMirrorRow = ARGBMirrorRow_MSA;
    }
  }
#endif
156
#if defined(HAS_COPYROW_SSE2)
157 158
  if (TestCpuFlag(kCpuHasSSE2)) {
    CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
159
  }
160
#endif
161
#if defined(HAS_COPYROW_AVX)
162 163
  if (TestCpuFlag(kCpuHasAVX)) {
    CopyRow = IS_ALIGNED(width * 4, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
164 165
  }
#endif
166 167 168
#if defined(HAS_COPYROW_ERMS)
  if (TestCpuFlag(kCpuHasERMS)) {
    CopyRow = CopyRow_ERMS;
fbarchard@google.com's avatar
fbarchard@google.com committed
169 170
  }
#endif
171 172 173 174 175
#if defined(HAS_COPYROW_NEON)
  if (TestCpuFlag(kCpuHasNEON)) {
    CopyRow = IS_ALIGNED(width * 4, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
  }
#endif
176 177 178 179
#if defined(HAS_COPYROW_MIPS)
  if (TestCpuFlag(kCpuHasMIPS)) {
    CopyRow = CopyRow_MIPS;
  }
180
#endif
181

182
  // Odd height will harmlessly mirror the middle row twice.
183
  for (y = 0; y < half_height; ++y) {
Frank Barchard's avatar
Frank Barchard committed
184
    ARGBMirrorRow(src, row, width);      // Mirror first row into a buffer
185
    ARGBMirrorRow(src_bot, dst, width);  // Mirror last row into first row
Frank Barchard's avatar
Frank Barchard committed
186
    CopyRow(row, dst_bot, width * 4);    // Copy first mirrored row into last
187 188 189 190 191
    src += src_stride;
    dst += dst_stride;
    src_bot -= src_stride;
    dst_bot -= dst_stride;
  }
192
  free_aligned_buffer_64(row);
193 194
}

195
LIBYUV_API
Frank Barchard's avatar
Frank Barchard committed
196 197 198 199 200 201
int ARGBRotate(const uint8* src_argb,
               int src_stride_argb,
               uint8* dst_argb,
               int dst_stride_argb,
               int width,
               int height,
202
               enum RotationMode mode) {
203 204 205 206 207 208 209 210 211 212 213 214 215 216
  if (!src_argb || width <= 0 || height == 0 || !dst_argb) {
    return -1;
  }

  // Negative height means invert the image.
  if (height < 0) {
    height = -height;
    src_argb = src_argb + (height - 1) * src_stride_argb;
    src_stride_argb = -src_stride_argb;
  }

  switch (mode) {
    case kRotate0:
      // copy frame
Frank Barchard's avatar
Frank Barchard committed
217
      return ARGBCopy(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
218 219
                      width, height);
    case kRotate90:
Frank Barchard's avatar
Frank Barchard committed
220 221
      ARGBRotate90(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                   height);
222 223
      return 0;
    case kRotate270:
Frank Barchard's avatar
Frank Barchard committed
224 225
      ARGBRotate270(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                    height);
226 227
      return 0;
    case kRotate180:
Frank Barchard's avatar
Frank Barchard committed
228 229
      ARGBRotate180(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
                    height);
230 231 232 233 234 235 236 237 238 239 240
      return 0;
    default:
      break;
  }
  return -1;
}

#ifdef __cplusplus
}  // extern "C"
}  // namespace libyuv
#endif