annotate imgconvert.c @ 43:ab64a3fc62bf libavcodec

Portability and testing issues
author nickols_k
date Wed, 08 Aug 2001 16:26:51 +0000
parents 1d2077091e88
children 1d796bdb2c2a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * Misc image convertion routines
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2001 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include <string.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
23
17
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 0
diff changeset
24 #ifdef USE_FASTMEMCPY
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 0
diff changeset
25 #include "fastmemcpy.h"
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 0
diff changeset
26 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
27 /* XXX: totally non optimized */
986e461dc072 Initial revision
glantau
parents:
diff changeset
28
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 static void yuv422_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
986e461dc072 Initial revision
glantau
parents:
diff changeset
30 UINT8 *src, int width, int height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 int x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
33 UINT8 *p = src;
986e461dc072 Initial revision
glantau
parents:
diff changeset
34
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 for(y=0;y<height;y+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
36 for(x=0;x<width;x+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 lum[0] = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 cb[0] = p[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 lum[1] = p[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 cr[0] = p[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
41 p += 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 lum += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
43 cb++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 cr++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 for(x=0;x<width;x+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 lum[0] = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 lum[1] = p[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 p += 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 lum += 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
54
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 #define SCALEBITS 8
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 #define ONE_HALF (1 << (SCALEBITS - 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
986e461dc072 Initial revision
glantau
parents:
diff changeset
58
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 UINT8 *src, int width, int height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 int wrap, wrap3, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 int r, g, b, r1, g1, b1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 UINT8 *p;
986e461dc072 Initial revision
glantau
parents:
diff changeset
65
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 wrap = width;
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 wrap3 = width * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 p = src;
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 for(y=0;y<height;y+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 for(x=0;x<width;x+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 r = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 g = p[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
73 b = p[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 r1 = r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 g1 = g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 b1 = b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 r = p[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 g = p[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 b = p[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 r1 += r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 g1 += g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 b1 += b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 p += wrap3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 lum += wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
89
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 r = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 g = p[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 b = p[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 r1 += r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 g1 += g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 b1 += b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
96 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 r = p[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 g = p[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 b = p[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 r1 += r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 g1 += g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 b1 += b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
106
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
109 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
111
986e461dc072 Initial revision
glantau
parents:
diff changeset
112 cb++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 cr++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
114 p += -wrap3 + 2 * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 lum += -wrap + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 p += wrap3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 lum += wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
121
986e461dc072 Initial revision
glantau
parents:
diff changeset
122 static void bgr24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 UINT8 *src, int width, int height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 int wrap, wrap3, x, y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 int r, g, b, r1, g1, b1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 UINT8 *p;
986e461dc072 Initial revision
glantau
parents:
diff changeset
128
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 wrap = width;
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 wrap3 = width * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 p = src;
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 for(y=0;y<height;y+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 for(x=0;x<width;x+=2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 b = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 g = p[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 r = p[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 r1 = r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 g1 = g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 b1 = b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
140 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 b = p[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 g = p[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 r = p[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 r1 += r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 g1 += g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 b1 += b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
149 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 p += wrap3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 lum += wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 b = p[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 g = p[1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 r = p[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 r1 += r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 g1 += g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 b1 += b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 b = p[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 g = p[4];
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 r = p[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
164 r1 += r;
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 g1 += g;
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 b1 += b;
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
986e461dc072 Initial revision
glantau
parents:
diff changeset
169
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
174
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 cb++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 cr++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 p += -wrap3 + 2 * 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 lum += -wrap + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 p += wrap3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 lum += wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
184
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 int img_convert_to_yuv420(UINT8 *img_out, UINT8 *img,
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 int pix_fmt, int width, int height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
188 UINT8 *pict;
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 int size, size_out;
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 UINT8 *picture[3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
191
986e461dc072 Initial revision
glantau
parents:
diff changeset
192 pict = img_out;
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 size = width * height;
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 size_out = (size * 3) / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 picture[0] = pict;
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 picture[1] = pict + size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 picture[2] = picture[1] + (size / 4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
198
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 switch(pix_fmt) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 case PIX_FMT_YUV420P:
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 memcpy(pict, img, size_out);
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
203 case PIX_FMT_YUV422:
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 yuv422_to_yuv420p(picture[0], picture[1], picture[2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 img, width, height);
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 case PIX_FMT_RGB24:
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 rgb24_to_yuv420p(picture[0], picture[1], picture[2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 img, width, height);
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 case PIX_FMT_BGR24:
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 bgr24_to_yuv420p(picture[0], picture[1], picture[2],
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 img, width, height);
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 return size_out;
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 }