summaryrefslogtreecommitdiffstats
path: root/xine_artsplugin/tools/thumbnail/videoscaler.cpp
blob: 2b4b49ad5e8ee64723845200fb946074796e3f91 (plain)
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
/*
   This file is part of KDE/aRts - xine integration
   Copyright (C) 2003 Ewald Snel <[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.

   inspired by code from the xine project

   Copyright (C) 2003 the xine project
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include <config.h>

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif

#include "videoscaler.h"

#define THUMBNAIL_BRIGHTNESS	+32
#define THUMBNAIL_CONTRAST	128
#define THUMBNAIL_SATURATION	128


// Colorspace conversion tables
static int tableLY[256];
static int tableRV[256], tableBU[256], tableGU[256], tableGV[256];
static int clipR[2240], clipG[2240], clipB[2240];

static pthread_once_t once_control = PTHREAD_ONCE_INIT;


static void init_once_routine()
{
    int cly = ( 76309 * THUMBNAIL_CONTRAST   + 64) / 128;
    int crv = (104597 * THUMBNAIL_SATURATION + 64) / 128;
    int cbu = (132201 * THUMBNAIL_SATURATION + 64) / 128;
    int cgu = ( 25675 * THUMBNAIL_SATURATION + 64) / 128;
    int cgv = ( 53279 * THUMBNAIL_SATURATION + 64) / 128;
    int i;

    for (i=0; i < 256; i++)
    {
	tableLY[i] = cly * (i + THUMBNAIL_BRIGHTNESS - 16) + (864 << 16) + 32768;
	tableRV[i] = crv * (i - 128);
	tableBU[i] = cbu * (i - 128);
	tableGU[i] = cgu * (i - 128);
	tableGV[i] = cgv * (i - 128);
    }
    for (i=0; i < 2240; i++)
    {
	int c = (i < 864) ? 0 : ((i > 1119) ? 255 : (i - 864));

	clipR[i] = c << 16;
	clipG[i] = c << 8;
	clipB[i] = c;
    }
}

static void yuvToRgb32( unsigned char *bufy, unsigned char *bufu, unsigned char *bufv,
			unsigned int *pixels, int width )
{
    for (int i=0; i < width; i++)
    {
	int l = tableLY[bufy[i]];

	pixels[i] = clipR[(l + tableRV[bufv[i]]) >> 16] |
		    clipG[(l - tableGU[bufu[i]] - tableGV[bufv[i]]) >> 16] |
		    clipB[(l + tableBU[bufu[i]]) >> 16];
    }
}

static inline void scaleLine( unsigned char *src[2], int width,
			      unsigned char *dst, int scaledWidth,
			      int scale, int weight, int step, int offset )
{
    int a, b, c, d;
    int x = (scale / 2) - 32768;
    unsigned char *p0 = (src[0] + offset);
    unsigned char *p1 = (src[1] + offset);

    weight >>= 8;

    if (scaledWidth > width)
    {
	/* Trailing pixels, no horizontal filtering */
	c  = scaledWidth - (((width << 16) - 32768 - (scale / 2)) / scale);
	a  = p0[(step * width) - step];
	b  = p1[(step * width) - step];
	a += (128 + (b - a) * weight) >> 8;
	scaledWidth -= c;
	memset( &dst[scaledWidth], a, c );

	/* Leading pixels, no horizontal filtering */
	c  = (32767 + (scale / 2)) / scale;
	a  = p0[0];
	b  = p1[0];
	a += (128 + (b - a) * weight) >> 8;
	scaledWidth -= c;
	memset( dst, a, c );

	/* Adjust source and destination */
	dst += c;
	x += (c * scale);
    }

    for (int i=0; i < scaledWidth; i++)
    {
	int xhi = (step == 1) ? (x >> 16)
			      : ((step == 2) ? (x >> 15) & ~0x1
					     : (x >> 14) & ~0x3);
	int xlo = (x & 0xFFFF) >> 8;

	/* Four nearest points for bilinear filtering */
	a = p0[xhi];
	b = p0[xhi + step];
	c = p1[xhi];\
	d = p1[xhi + step];

	/* Interpolate horizontally */
	a = (256 * a) + (b - a) * xlo;
	c = (256 * c) + (d - c) * xlo;

	/* Interpolate vertically and store bilinear filtered sample */
	*(dst++) = ((256 * a) + (c - a) * weight + 32768) >> 16;

	x += scale;
    }
}

void scaleYuvToRgb32( int width, int height,
		      unsigned char *base[3], unsigned int pitches[3],
		      int scaledWidth, int scaledHeight,
		      unsigned int *pixels, unsigned int bytesPerLine )
{
    int chromaWidth = (width + 1) / 2;
    int chromaHeight = (height + 1) / 2;
    int scaleX = (width << 16) / scaledWidth;
    int scaleY = (height << 16) / scaledHeight;
    int scaleCX = (scaleX / 2);
    int y = (scaleY / 2) - 32768;

    // Temporary line buffers (stack)
    unsigned char *bufy = (unsigned char *)alloca( scaledWidth );
    unsigned char *bufu = (unsigned char *)alloca( scaledWidth );
    unsigned char *bufv = (unsigned char *)alloca( scaledWidth );

    pthread_once( &once_control, init_once_routine );

    for (int i=0; i < scaledHeight; i++)
    {
	unsigned char *twoy[2], *twou[2], *twov[2];
	int y2 = (y / 2) - 32768;

	// Calculate luminance scanlines for bilinear filtered scaling
	if (y < 0)
	{
	    twoy[0] = twoy[1] = base[0];
	}
	else if (y >= ((height - 1) << 16))
	{
	    twoy[0] = twoy[1] = base[0] + (height - 1) * pitches[0];
	}
	else
	{
	    twoy[0] = base[0] + (y >> 16) * pitches[0];
	    twoy[1] = twoy[0] + pitches[0];
	}

	// Calculate chrominance scanlines for bilinear filtered scaling
	if (y2 < 0)
	{
	    twou[0] = twou[1] = base[1];
	    twov[0] = twov[1] = base[2];
	}
	else if (y2 >= ((chromaHeight - 1) << 16))
	{
	    twou[0] = twou[1] = base[1] + (chromaHeight - 1) * pitches[1];
	    twov[0] = twov[1] = base[2] + (chromaHeight - 1) * pitches[2];
	}
	else
	{
	    twou[0] = base[1] + (y2 >> 16) * pitches[1];
	    twov[0] = base[2] + (y2 >> 16) * pitches[2];
	    twou[1] = twou[0] + pitches[1];
	    twov[1] = twov[0] + pitches[2];
	}

	// Bilinear filtered scaling
	scaleLine( twoy, width, bufy, scaledWidth, scaleX, (y & 0xFFFF), 1, 0 );
	scaleLine( twou, chromaWidth, bufu, scaledWidth, scaleCX, (y2 & 0xFFFF), 1, 0 );
	scaleLine( twov, chromaWidth, bufv, scaledWidth, scaleCX, (y2 & 0xFFFF), 1, 0 );

	// YUV to RGB32 comnversion
	yuvToRgb32( bufy, bufu, bufv, pixels, scaledWidth );

	pixels = (unsigned int *)(((char *)pixels) + bytesPerLine);
	y += scaleY;
    }
}

void scaleYuy2ToRgb32( int width, int height,
                       unsigned char *base, unsigned int pitch,
                       int scaledWidth, int scaledHeight,
                       unsigned int *pixels, unsigned int bytesPerLine )
{
    int chromaWidth = (width + 1) / 2;
    int scaleX = (width << 16) / scaledWidth;
    int scaleY = (height << 16) / scaledHeight;
    int scaleCX = (scaleX / 2);
    int y = (scaleY / 2) - 32768;

    // Temporary line buffers (stack)
    unsigned char *bufy = (unsigned char *)alloca( scaledWidth );
    unsigned char *bufu = (unsigned char *)alloca( scaledWidth );
    unsigned char *bufv = (unsigned char *)alloca( scaledWidth );

    pthread_once( &once_control, init_once_routine );

    for (int i=0; i < scaledHeight; i++)
    {
	unsigned char *two[2];

	// Calculate scanlines for bilinear filtered scaling
	if (y < 0)
	{
	    two[0] = two[1] = base;
	}
	else if (y >= ((height - 1) << 16))
	{
	    two[0] = two[1] = base + (height - 1) * pitch;
	}
	else
	{
	    two[0] = base + (y >> 16) * pitch;
	    two[1] = two[0] + pitch;
	}

	// Bilinear filtered scaling
	scaleLine( two, width, bufy, scaledWidth, scaleX, (y & 0xFFFF), 2, 0 );
	scaleLine( two, chromaWidth, bufu, scaledWidth, scaleCX, (y & 0xFFFF), 4, 1 );
	scaleLine( two, chromaWidth, bufv, scaledWidth, scaleCX, (y & 0xFFFF), 4, 3 );

	// YUV to RGB32 comnversion
	yuvToRgb32( bufy, bufu, bufv, pixels, scaledWidth );

	pixels = (unsigned int *)(((char *)pixels) + bytesPerLine);
	y += scaleY;
    }
}