91a7aeea7957b6793b8d031ef365363aa19b32d7
[ffmpeg.git] / libswscale / bfin / yuv2rgb_bfin.c
1 /*
2  * Copyright (C) 2007 Marc Hoffman <marc.hoffman@analog.com>
3  *
4  * Blackfin video color space converter operations
5  * convert I420 YV12 to RGB in various formats
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <inttypes.h>
28 #include <assert.h>
29 #include <unistd.h>
30
31 #include "config.h"
32 #include "libswscale/rgb2rgb.h"
33 #include "libswscale/swscale.h"
34 #include "libswscale/swscale_internal.h"
35
36 #if defined(__FDPIC__) && CONFIG_SRAM
37 #define L1CODE __attribute__((l1_text))
38 #else
39 #define L1CODE
40 #endif
41
42 void ff_bfin_yuv2rgb555_line(uint8_t *Y, uint8_t *U, uint8_t *V, uint8_t *out,
43                              int w, uint32_t *coeffs) L1CODE;
44
45 void ff_bfin_yuv2rgb565_line(uint8_t *Y, uint8_t *U, uint8_t *V, uint8_t *out,
46                              int w, uint32_t *coeffs) L1CODE;
47
48 void ff_bfin_yuv2rgb24_line(uint8_t *Y, uint8_t *U, uint8_t *V, uint8_t *out,
49                             int w, uint32_t *coeffs) L1CODE;
50
51 typedef void (*ltransform)(uint8_t *Y, uint8_t *U, uint8_t *V, uint8_t *out,
52                            int w, uint32_t *coeffs);
53
54 static void bfin_prepare_coefficients(SwsContext *c, int rgb, int masks)
55 {
56     int oy;
57     oy = c->yOffset & 0xffff;
58     oy = oy >> 3;      // keep everything U8.0 for offset calculation
59
60     c->oc = 128 * 0x01010101U;
61     c->oy = oy * 0x01010101U;
62
63     /* copy 64bit vector coeffs down to 32bit vector coeffs */
64     c->cy   = c->yCoeff;
65     c->zero = 0;
66
67     if (rgb) {
68         c->crv = c->vrCoeff;
69         c->cbu = c->ubCoeff;
70         c->cgu = c->ugCoeff;
71         c->cgv = c->vgCoeff;
72     } else {
73         c->crv = c->ubCoeff;
74         c->cbu = c->vrCoeff;
75         c->cgu = c->vgCoeff;
76         c->cgv = c->ugCoeff;
77     }
78
79     if (masks == 555) {
80         c->rmask = 0x001f * 0x00010001U;
81         c->gmask = 0x03e0 * 0x00010001U;
82         c->bmask = 0x7c00 * 0x00010001U;
83     } else if (masks == 565) {
84         c->rmask = 0x001f * 0x00010001U;
85         c->gmask = 0x07e0 * 0x00010001U;
86         c->bmask = 0xf800 * 0x00010001U;
87     }
88 }
89
90 static int core_yuv420_rgb(SwsContext *c, uint8_t **in, int *instrides,
91                            int srcSliceY, int srcSliceH, uint8_t **oplanes,
92                            int *outstrides, ltransform lcscf,
93                            int rgb, int masks)
94 {
95     uint8_t *py, *pu, *pv, *op;
96     int w  = instrides[0];
97     int h2 = srcSliceH >> 1;
98     int i;
99
100     bfin_prepare_coefficients(c, rgb, masks);
101
102     py = in[0];
103     pu = in[1 + (1 ^ rgb)];
104     pv = in[1 + (0 ^ rgb)];
105
106     op = oplanes[0] + srcSliceY * outstrides[0];
107
108     for (i = 0; i < h2; i++) {
109         lcscf(py, pu, pv, op, w, &c->oy);
110
111         py += instrides[0];
112         op += outstrides[0];
113
114         lcscf(py, pu, pv, op, w, &c->oy);
115
116         py += instrides[0];
117         pu += instrides[1];
118         pv += instrides[2];
119         op += outstrides[0];
120     }
121
122     return srcSliceH;
123 }
124
125 static int bfin_yuv420_rgb555(SwsContext *c, uint8_t **in, int *instrides,
126                               int srcSliceY, int srcSliceH,
127                               uint8_t **oplanes, int *outstrides)
128 {
129     return core_yuv420_rgb(c, in, instrides, srcSliceY, srcSliceH, oplanes,
130                            outstrides, ff_bfin_yuv2rgb555_line, 1, 555);
131 }
132
133 static int bfin_yuv420_bgr555(SwsContext *c, uint8_t **in, int *instrides,
134                               int srcSliceY, int srcSliceH,
135                               uint8_t **oplanes, int *outstrides)
136 {
137     return core_yuv420_rgb(c, in, instrides, srcSliceY, srcSliceH, oplanes,
138                            outstrides, ff_bfin_yuv2rgb555_line, 0, 555);
139 }
140
141 static int bfin_yuv420_rgb24(SwsContext *c, uint8_t **in, int *instrides,
142                              int srcSliceY, int srcSliceH,
143                              uint8_t **oplanes, int *outstrides)
144 {
145     return core_yuv420_rgb(c, in, instrides, srcSliceY, srcSliceH, oplanes,
146                            outstrides, ff_bfin_yuv2rgb24_line, 1, 888);
147 }
148
149 static int bfin_yuv420_bgr24(SwsContext *c, uint8_t **in, int *instrides,
150                              int srcSliceY, int srcSliceH,
151                              uint8_t **oplanes, int *outstrides)
152 {
153     return core_yuv420_rgb(c, in, instrides, srcSliceY, srcSliceH, oplanes,
154                            outstrides, ff_bfin_yuv2rgb24_line, 0, 888);
155 }
156
157 static int bfin_yuv420_rgb565(SwsContext *c, uint8_t **in, int *instrides,
158                               int srcSliceY, int srcSliceH,
159                               uint8_t **oplanes, int *outstrides)
160 {
161     return core_yuv420_rgb(c, in, instrides, srcSliceY, srcSliceH, oplanes,
162                            outstrides, ff_bfin_yuv2rgb565_line, 1, 565);
163 }
164
165 static int bfin_yuv420_bgr565(SwsContext *c, uint8_t **in, int *instrides,
166                               int srcSliceY, int srcSliceH,
167                               uint8_t **oplanes, int *outstrides)
168 {
169     return core_yuv420_rgb(c, in, instrides, srcSliceY, srcSliceH, oplanes,
170                            outstrides, ff_bfin_yuv2rgb565_line, 0, 565);
171 }
172
173 SwsFunc ff_yuv2rgb_get_func_ptr_bfin(SwsContext *c)
174 {
175     SwsFunc f;
176
177     switch (c->dstFormat) {
178     case PIX_FMT_RGB555:
179         f = bfin_yuv420_rgb555;
180         break;
181     case PIX_FMT_BGR555:
182         f = bfin_yuv420_bgr555;
183         break;
184     case PIX_FMT_RGB565:
185         f = bfin_yuv420_rgb565;
186         break;
187     case PIX_FMT_BGR565:
188         f = bfin_yuv420_bgr565;
189         break;
190     case PIX_FMT_RGB24:
191         f = bfin_yuv420_rgb24;
192         break;
193     case PIX_FMT_BGR24:
194         f = bfin_yuv420_bgr24;
195         break;
196     default:
197         return 0;
198     }
199
200     av_log(c, AV_LOG_INFO, "BlackFin accelerated color space converter %s\n",
201            sws_format_name(c->dstFormat));
202
203     return f;
204 }