swscale/arm/yuv2rgb: add ff_yuv420p_to_{argb,rgba,abgr,bgra}_neon_{16,32}
[ffmpeg.git] / libswscale / arm / swscale_unscaled.c
1 /*
2  * Copyright (C) 2013 Xiaolei Yu <dreifachstein@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "config.h"
22 #include "libswscale/swscale.h"
23 #include "libswscale/swscale_internal.h"
24 #include "libavutil/arm/cpu.h"
25
26 #if 0
27 extern void rgbx_to_nv12_neon_32(const uint8_t *src, uint8_t *y, uint8_t *chroma,
28                 int width, int height,
29                 int y_stride, int c_stride, int src_stride,
30                 int32_t coeff_tbl[9]);
31
32 extern void rgbx_to_nv12_neon_16(const uint8_t *src, uint8_t *y, uint8_t *chroma,
33                 int width, int height,
34                 int y_stride, int c_stride, int src_stride,
35                 int32_t coeff_tbl[9]);
36
37 static int rgbx_to_nv12_neon_32_wrapper(SwsContext *context, const uint8_t *src[],
38                         int srcStride[], int srcSliceY, int srcSliceH,
39                         uint8_t *dst[], int dstStride[]) {
40
41     rgbx_to_nv12_neon_32(src[0] + srcSliceY * srcStride[0],
42             dst[0] + srcSliceY * dstStride[0],
43             dst[1] + (srcSliceY / 2) * dstStride[1],
44             context->srcW, srcSliceH,
45             dstStride[0], dstStride[1], srcStride[0],
46             context->input_rgb2yuv_table);
47
48     return 0;
49 }
50
51 static int rgbx_to_nv12_neon_16_wrapper(SwsContext *context, const uint8_t *src[],
52                         int srcStride[], int srcSliceY, int srcSliceH,
53                         uint8_t *dst[], int dstStride[]) {
54
55     rgbx_to_nv12_neon_16(src[0] + srcSliceY * srcStride[0],
56             dst[0] + srcSliceY * dstStride[0],
57             dst[1] + (srcSliceY / 2) * dstStride[1],
58             context->srcW, srcSliceH,
59             dstStride[0], dstStride[1], srcStride[0],
60             context->input_rgb2yuv_table);
61
62     return 0;
63 }
64 #endif
65
66 #define YUV_TO_RGB_TABLE(precision)                                                         \
67         c->yuv2rgb_v2r_coeff / ((precision) == 16 ? 1 << 7 : 1),                            \
68         c->yuv2rgb_u2g_coeff / ((precision) == 16 ? 1 << 7 : 1),                            \
69         c->yuv2rgb_v2g_coeff / ((precision) == 16 ? 1 << 7 : 1),                            \
70         c->yuv2rgb_u2b_coeff / ((precision) == 16 ? 1 << 7 : 1),                            \
71
72 #define DECLARE_FF_YUV420P_TO_RGBX_FUNCS(ofmt, precision)                                   \
73 int ff_yuv420p_to_##ofmt##_neon_##precision(int w, int h,                                   \
74                                  uint8_t *dst, int linesize,                                \
75                                  const uint8_t *srcY, int linesizeY,                        \
76                                  const uint8_t *srcU, int linesizeU,                        \
77                                  const uint8_t *srcV, int linesizeV,                        \
78                                  const int16_t *table,                                      \
79                                  int y_offset,                                              \
80                                  int y_coeff);                                              \
81                                                                                             \
82 static int yuv420p_to_##ofmt##_neon_wrapper_##precision(SwsContext *c, const uint8_t *src[],\
83                                            int srcStride[], int srcSliceY, int srcSliceH,   \
84                                            uint8_t *dst[], int dstStride[]) {               \
85     const int16_t yuv2rgb_table[] = { YUV_TO_RGB_TABLE(precision) };                        \
86                                                                                             \
87     ff_yuv420p_to_##ofmt##_neon_##precision(c->srcW, srcSliceH,                             \
88                                  dst[0] + srcSliceY * dstStride[0], dstStride[0],           \
89                                  src[0], srcStride[0],                                      \
90                                  src[1], srcStride[1],                                      \
91                                  src[2], srcStride[2],                                      \
92                                  yuv2rgb_table,                                             \
93                                  c->yuv2rgb_y_offset >> 9,                                  \
94                                  c->yuv2rgb_y_coeff / ((precision) == 16 ? 1 << 7 : 1));    \
95                                                                                             \
96     return 0;                                                                               \
97 }                                                                                           \
98
99 #define DECLARE_FF_YUV420P_TO_ALL_RGBX_FUNCS(precision)                                     \
100 DECLARE_FF_YUV420P_TO_RGBX_FUNCS(argb, precision)                                           \
101 DECLARE_FF_YUV420P_TO_RGBX_FUNCS(rgba, precision)                                           \
102 DECLARE_FF_YUV420P_TO_RGBX_FUNCS(abgr, precision)                                           \
103 DECLARE_FF_YUV420P_TO_RGBX_FUNCS(bgra, precision)                                           \
104
105 #define DECLARE_FF_YUV420P_TO_ALL_RGBX_ALL_PRECISION_FUNCS                                  \
106 DECLARE_FF_YUV420P_TO_ALL_RGBX_FUNCS(16)                                                    \
107
108 DECLARE_FF_YUV420P_TO_ALL_RGBX_ALL_PRECISION_FUNCS
109
110 #define DECLARE_FF_NVX_TO_RGBX_FUNCS(ifmt, ofmt, precision)                                 \
111 int ff_##ifmt##_to_##ofmt##_neon_##precision(int w, int h,                                  \
112                                  uint8_t *dst, int linesize,                                \
113                                  const uint8_t *srcY, int linesizeY,                        \
114                                  const uint8_t *srcC, int linesizeC,                        \
115                                  const int16_t *table,                                      \
116                                  int y_offset,                                              \
117                                  int y_coeff);                                              \
118                                                                                             \
119 static int ifmt##_to_##ofmt##_neon_wrapper_##precision(SwsContext *c, const uint8_t *src[], \
120                                            int srcStride[], int srcSliceY, int srcSliceH,   \
121                                            uint8_t *dst[], int dstStride[]) {               \
122     const int16_t yuv2rgb_table[] = { YUV_TO_RGB_TABLE(precision) };                        \
123                                                                                             \
124     ff_##ifmt##_to_##ofmt##_neon_##precision(c->srcW, srcSliceH,                            \
125                                  dst[0] + srcSliceY * dstStride[0], dstStride[0],           \
126                                  src[0], srcStride[0], src[1], srcStride[1],                \
127                                  yuv2rgb_table,                                             \
128                                  c->yuv2rgb_y_offset >> 9,                                  \
129                                  c->yuv2rgb_y_coeff / ((precision) == 16 ? 1 << 7 : 1));    \
130                                                                                             \
131     return 0;                                                                               \
132 }                                                                                           \
133
134 #define DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nvx, precision)                                    \
135 DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, argb, precision)                                          \
136 DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, rgba, precision)                                          \
137 DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, abgr, precision)                                          \
138 DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, bgra, precision)                                          \
139
140 #define DECLARE_FF_NVX_TO_ALL_RGBX_ALL_PRECISION_FUNCS(nvx)                                 \
141 DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nvx, 16)                                                   \
142
143 DECLARE_FF_NVX_TO_ALL_RGBX_ALL_PRECISION_FUNCS(nv12)
144 DECLARE_FF_NVX_TO_ALL_RGBX_ALL_PRECISION_FUNCS(nv21)
145
146 /* We need a 16 pixel width alignment. This constraint can easily be removed
147  * for input reading but for the output which is 4-bytes per pixel (RGBA) the
148  * assembly might be writing as much as 4*15=60 extra bytes at the end of the
149  * line, which won't fit the 32-bytes buffer alignment. */
150 #define SET_FF_NVX_TO_RGBX_FUNC(ifmt, IFMT, ofmt, OFMT, accurate_rnd) do {                  \
151     if (c->srcFormat == AV_PIX_FMT_##IFMT                                                   \
152         && c->dstFormat == AV_PIX_FMT_##OFMT                                                \
153         && !(c->srcH & 1)                                                                   \
154         && !(c->srcW & 15)                                                                  \
155         && !accurate_rnd) {                                                                 \
156         c->swscale = ifmt##_to_##ofmt##_neon_wrapper_16;                                    \
157     }                                                                                       \
158 } while (0)
159
160 #define SET_FF_NVX_TO_ALL_RGBX_FUNC(nvx, NVX, accurate_rnd) do {                            \
161     SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, argb, ARGB, accurate_rnd);                            \
162     SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, rgba, RGBA, accurate_rnd);                            \
163     SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, abgr, ABGR, accurate_rnd);                            \
164     SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, bgra, BGRA, accurate_rnd);                            \
165 } while (0)
166
167 static void get_unscaled_swscale_neon(SwsContext *c) {
168     int accurate_rnd = c->flags & SWS_ACCURATE_RND;
169 #if 0
170     if (c->srcFormat == AV_PIX_FMT_RGBA
171             && c->dstFormat == AV_PIX_FMT_NV12
172             && (c->srcW >= 16)) {
173         c->swscale = accurate_rnd ? rgbx_to_nv12_neon_32_wrapper
174                         : rgbx_to_nv12_neon_16_wrapper;
175     }
176 #endif
177
178     SET_FF_NVX_TO_ALL_RGBX_FUNC(nv12, NV12, accurate_rnd);
179     SET_FF_NVX_TO_ALL_RGBX_FUNC(nv21, NV21, accurate_rnd);
180     SET_FF_NVX_TO_ALL_RGBX_FUNC(yuv420p, YUV420P, accurate_rnd);
181 }
182
183 void ff_get_unscaled_swscale_arm(SwsContext *c)
184 {
185     int cpu_flags = av_get_cpu_flags();
186     if (have_neon(cpu_flags))
187         get_unscaled_swscale_neon(c);
188 }