This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yuv.cpp
369 lines (267 loc) · 9.09 KB
/
yuv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*****************************************************************************
* sdlyuvaddon.c : RGB to YUV conversion functions
*
*****************************************************************************
* Copyright (C) 2001 Xavier Servettaz <[email protected]>
*
* Authors: Xavier Servettaz <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "yuv.h"
/*
* Create an empty YUV surface of the appropriate depth
*/
SDL_YUVSurface * SDL_CreateYUVSurface (int width, int height)
{
/*****************************************************
* FIXME only YUV 4.2.0 surfs
* ***************************************************/
SDL_YUVSurface *surface;
Uint32 size;
Uint32 uv_size;
/* Allocate the surface */
surface = (SDL_YUVSurface *)malloc(sizeof(*surface));
if ( surface == NULL ) {
SDL_OutOfMemory();
return(NULL);
}
surface->w = width;
surface->h = height;
surface->pitch = width; //SDL_CalculatePitch(surface);
surface->y_data = NULL;
surface->u_data = NULL;
surface->v_data = NULL;
size = surface->h*surface->pitch;
uv_size = (surface->w /2 +1)*(surface->h/2 +1);//FIXME only 4.2.0
//fprintf(stderr," Ysize = %d UVsize = %d\n",size,uv_size);
/* Get the yuv datas */
if ( surface->w && surface->h ) {
/* Y data */
//fprintf(stderr, "ALLOCATING Y: size = %d\n",size);
surface->y_data = (Uint8 *) malloc(size);
if ( surface->y_data == NULL ) {
SDL_FreeYUVSurface(surface);
SDL_OutOfMemory();
return(NULL);
}
/* U data */
//fprintf(stderr, "ALLOCATING U: size = %d\n",uv_size);
surface->u_data = (Uint8 *)malloc(uv_size);
if ( surface->u_data == NULL ) {
SDL_FreeYUVSurface(surface);
SDL_OutOfMemory();
return(NULL);
}
/* V data */
//fprintf(stderr, "ALLOCATING V: size = %d\n",uv_size);
surface->v_data = (Uint8 *)malloc(uv_size);
if ( surface->v_data == NULL ) {
SDL_FreeYUVSurface(surface);
SDL_OutOfMemory();
return(NULL);
}
/* This is important for bitmaps */
memset(surface->y_data, 0, size);
memset(surface->u_data, 0, uv_size);
memset(surface->v_data, 0, uv_size);
}
//fprintf(stderr, "YUV SURFACE ALLOCATED\n");
//fprintf(stderr," y_data = %x\n",surface->y_data);
//fprintf(stderr," u_data = %x\n",surface->u_data);
//fprintf(stderr," v_data = %x\n",surface->v_data);
return(surface);
}
/*
* Free a surface created by the above function.
*/
void SDL_FreeYUVSurface (SDL_YUVSurface *surface)
{
/* Free anything that's not NULL */
if (surface == NULL) {
return;
}
free(surface->y_data);
free(surface->u_data);
free(surface->v_data);
free(surface);
}
/***************************************************************************/
int Get_YUV_From_Surface(SDL_Surface * image,SDL_YUVSurface * yuv_surface){
Uint32 x;
Uint32 y;
Uint32 offset;
Uint32 offset_uv;
Uint32 convert_start;
Uint32 convert_end;
Uint8* p_y;
Uint8* p_u;
Uint8* p_v;
Uint32 height;
Uint32 width;
height = image->h;
width = image->w;
offset = 0;
offset_uv = 0;
convert_start = 0;
convert_end = 0;
p_y = yuv_surface->y_data;
p_u = yuv_surface->u_data;
p_v = yuv_surface->v_data;
//fprintf(stderr," p_y = %x\n",p_y);
//fprintf(stderr," p_u = %x\n",p_u);
//fprintf(stderr," p_v = %x\n",p_v);
if (p_y == NULL || p_u == NULL || p_v == NULL){
//fprintf(stderr,"YUV : Out of memory\n");
return (-1);
}
/*mesure speed*/
convert_start = SDL_GetTicks();
for (y = 0 ; y < height; y ++) {
for (x = 0 ; x < width; x ++) {
/*TODO improve speed: precalculate Y,U and V from pixel value (without extracting RGB) and store it into a table)*/
/* Get RGB components for current pixel */
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 new_y;
Uint8 new_u;
Uint8 new_v;
Uint32 current_pixel;
Uint8 BitsPerPixel;
/*FIXME using 16 BPP surface only */
BitsPerPixel= image->format->BitsPerPixel;
//fprintf(stderr," BPP = %d\n",BitsPerPixel );
switch(BitsPerPixel)
{
case 8:
//Uint8 current_pixel;
current_pixel = *((Uint8 *)image->pixels + y*image->pitch + x);
break;
case 16:
//Uint16 current_pixel;
current_pixel = *((Uint16 *)image->pixels + y*image->pitch/2 + x);
break;
case 24:
//Test
current_pixel = *((Uint8 *)image->pixels + y*image->pitch + x *3);
//fprintf(stderr, "ERROR : 24 BPP Surfaces NOT supported\n");
break;
case 32:
//Uint32 current_pixel;
current_pixel = *((Uint32 *)image->pixels + y*image->pitch/4 + x);
break;
default:
//fprintf(stderr, "ERROR : Surface has invalide BPP\n");
break;
}
//Get RGB components from pixel
SDL_GetRGB(current_pixel, image->format , &r,&g, &b);
/*Calculate Y*/
new_y = (Uint8)(0.299*r+0.587*g+0.098*b);
/*copy pixel data to p_y:*/
memcpy(&p_y[offset] ,&new_y,sizeof(new_y));
/*YUV 4.2.0 conversion U and V have 1/2 Hor and 1/2 Vert frequency*/
if ((x%2 == 0) && (y%2 == 0))
{ //copy 1/4
/*Formula is hard to find so, don't blast it (have a look at yuv fourcc samples and take a look at the book (video-demystified.com))*/
new_u = (Uint8)(0.558*(b - new_y) +127);
new_v = (Uint8)(0.709*(r - new_y) +127);
/*Copy U and V into p_u and p_v*/
memcpy(&p_u[offset_uv] ,&new_u,sizeof(new_u));
memcpy(&p_v[offset_uv] ,&new_v,sizeof(new_v));
//fprintf(stdout, "Y = %d X = %d / last offset_uv = %d\n",y,x,offset_uv);
offset_uv ++;
}
offset ++;
}//for x
}//for y
convert_end = SDL_GetTicks();
//fprintf(stderr, "conversion time (millisec) = %d\n", convert_end - convert_start);
}
/************************************
*
* ConvertRGBtoYUV
*
* **********************************/
int ConvertRGBtoYUV ( SDL_Surface *image, SDL_YUVSurface * yuv_surface){
//SDL_YUVSurface * yuv_surface;
//Get YUV from surface
if (-1 == Get_YUV_From_Surface(image, yuv_surface))
return(-1);
//fprintf(stderr, "Fin RGBtoYUV \n");
return(1);//yuv_surface);
}
/*********************************
*
* copy source yuv surface to dest overlay
* Only the position is used in the dstrect (the width and height are ignored).
*
*
* **********************************/
int SDL_BlitOverlay(SDL_YUVSurface *src, SDL_Rect *srcrect, SDL_Overlay *dst, SDL_Rect *dstrect){
/*declarations*/
Uint8 * p_y_dst;
Uint8 * p_u_dst;
Uint8 * p_v_dst;
Uint8 line;
Uint32 start, end;
Sint32 min_size;
Sint32 min_h;
Uint32 copy_offset;
copy_offset = 0;
start = SDL_GetTicks();
//Calculate min pitch and height for copy size
min_size = (src->pitch <= dst->pitches[0] - dstrect->x) ?
src->pitch : dst->pitches[0]- dstrect->x;
min_h = (src->h <= dst->h - dstrect->y) ? src->h : dst->h - dstrect->y;
if ((min_size < 0)||(min_h <0)) //No need to continue
return(0);
//fprintf(stderr,"min h =%d\n",min_h);
/*FIXME only blit YUV 4.2.0 overlays*/
/*Calculate dest pointers*/
for (line = 0 ; line < min_h; line ++) {
/*copy ligne a ligne*/
p_y_dst = dst->pixels[0] + (dstrect->y + line)* dst->pitches[0] + dstrect->x;
memcpy(p_y_dst, //to
src->y_data + line * src->pitch, //from
min_size); //size
if (line%2 == 0){
Sint32 uv_size;
uv_size = (min_size -1)/2 +1; //FIXME
//image->w - 1 is the last pixel of the line
// / 2 : U and V have 1/2 freq FIXME 420 only
// +1 : size of last U (V)
p_v_dst = dst->pixels[1] + dst->pitches[1]* (copy_offset + dstrect->y /2) + (dstrect->x -1)/2 +1;
p_u_dst = dst->pixels[2] + dst->pitches[2]* (copy_offset + dstrect->y /2) + (dstrect->x -1)/2 +1;
// V
memcpy(p_v_dst, //to
src->v_data + copy_offset * ((src->pitch - 1)/2+1),//uv_size, //from
uv_size); //size
// U
memcpy(p_u_dst, //to
src->u_data + copy_offset * ((src->pitch - 1)/2+1),//uv_size, //from
uv_size); //size
copy_offset++;
}
}
//fprintf(stderr,"Blit: last copy_offset = %d\n", copy_offset);
end = SDL_GetTicks();
//fprintf(stderr,"Overlay Blit time (millisec) = %d\n", end - start);
return(0);
}