summaryrefslogtreecommitdiffstats
path: root/xine_artsplugin/tools/thumbnail/videocreator.cpp
blob: 88f672927d5dcee2b5b63b7051c7844c731f4fb4 (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
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
370
371
372
373
374
375
376
/*  This file is part of the KDE libraries
    Copyright (C) 2002 Simon MacMullen
    Copyright (C) 2003 Ewald Snel <ewald@rambo.its.tudelft.nl>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

// $Id$

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <sys/time.h>

#include <qpixmap.h>
#include <qdialog.h>
#include <qfile.h>
#include <qimage.h>
#include <qpainter.h>
#include <qpaintdevice.h>

#include <iostream>

#include <kstandarddirs.h>
#include <kapplication.h>

#define XINE_ENABLE_EXPERIMENTAL_FEATURES	1

#include <xine.h>

#include "videocreator.h"
#include "videoscaler.h"

#define TIMEOUT		15	// 15 seconds
#define MAX_ATTEMPTS	25


// Global xine pointer
static xine_t		*xine_shared	 = NULL;
static pthread_mutex_t	 xine_mutex	 = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t	 xine_cond	 = PTHREAD_COND_INITIALIZER;
static int		 xineRefCount	 = 0;

static void xine_init_routine()
{
    char cfgFileName[272];

    xine_shared = (xine_t *)xine_new();

    snprintf( cfgFileName, 272, "%s/.xine/config", getenv( "HOME" ) );

    xine_config_load( xine_shared, (const char *)cfgFileName );

    xine_init( xine_shared );
}

static void *xine_timeout_routine( void * )
{
    pthread_mutex_lock( &xine_mutex );

    while (xine_shared != 0)
    {
	if (xineRefCount == 0)
	{
	    struct timespec ts;
	    struct timeval tv;

	    gettimeofday( &tv, 0 );

	    ts.tv_sec	= tv.tv_sec;
	    ts.tv_nsec	= tv.tv_usec * 1000;
	    ts.tv_sec  += TIMEOUT;

	    if (pthread_cond_timedwait( &xine_cond, &xine_mutex, &ts ) != 0 &&
		xineRefCount == 0)
	    {
		xine_exit( xine_shared );
		xine_shared = NULL;
		break;
	    }
	}
	else
	{
	    pthread_cond_wait( &xine_cond, &xine_mutex );
	}
    }
    pthread_mutex_unlock( &xine_mutex );

    return NULL;
}

static xine_t *xine_shared_init()
{
    pthread_mutex_lock( &xine_mutex );

    ++xineRefCount;

    if (xine_shared == 0)
    {
	pthread_t thread;

	xine_init_routine();

	if (pthread_create( &thread, NULL, xine_timeout_routine, NULL ) == 0)
	{
	    pthread_detach( thread );
	}
    }
    else
    {
	pthread_cond_signal( &xine_cond );
    }
    pthread_mutex_unlock( &xine_mutex );

    return xine_shared;
}

static void xine_shared_exit( xine_t * )
{
    pthread_mutex_lock( &xine_mutex );

    if (--xineRefCount == 0)
    {
	pthread_cond_signal( &xine_cond );
    }
    pthread_mutex_unlock( &xine_mutex );
}

static QImage createThumbnail( xine_video_frame_t *frame, int width, int height )
{
    unsigned char *base[3];
    unsigned int pitches[3];

    if ((frame->aspect_ratio * height) > width)
	height = (int)(.5 + (width / frame->aspect_ratio));
    else
	width = (int)(.5 + (height * frame->aspect_ratio));

    QImage image( width, height, 32 );

    if (frame->colorspace == XINE_IMGFMT_YV12)
    {
	int y_size, uv_size;

	pitches[0] = (frame->width + 7) & ~0x7;
	pitches[1] = (((frame->width + 1) / 2) + 7) & ~0x7;
	pitches[2] = pitches[1];

	y_size  = pitches[0] * frame->height;
	uv_size = pitches[1] * ((frame->height + 1) / 2);

	base[0] = frame->data;
	base[1] = base[0] + y_size + uv_size;
	base[2] = base[0] + y_size;

	scaleYuvToRgb32( frame->width, frame->height, base, pitches,
			 width, height, (unsigned int *)image.bits(),
			 image.bytesPerLine() );
    }
    else if (frame->colorspace == XINE_IMGFMT_YUY2)
    {
	pitches[0] = 2*((frame->width + 3) & ~0x3);
	base[0] = frame->data;

	scaleYuy2ToRgb32( frame->width, frame->height, base[0], pitches[0],
			  width, height, (unsigned int *)image.bits(),
			  image.bytesPerLine() );
    }
    return image;
}

// Return the variance of the brightness of the pixels
static double imageVariance( unsigned char *pixels, int pitch,
			     int width, int height, int step )
{
    double sigmaX = 0;
    double sigmaXSquared = 0;

    for (int y=0; y < height ; y++)
    {
	unsigned int uSigmaX = 0;
	unsigned int uSigmaXSquared = 0;

	for (int x=0, n=(width * step); x < n ; x+=step)
	{
	    int gray = pixels[x];

	    uSigmaX += gray;
	    uSigmaXSquared += gray * gray;
        }

	sigmaX += uSigmaX;
	sigmaXSquared += uSigmaXSquared;

	pixels += pitch;
    }

    unsigned int total = height * width;

    return sqrt( sigmaXSquared / total - (sigmaX / total) * (sigmaX / total) );
}

static bool findBestFrame( xine_video_port_t *vo_port, xine_video_frame_t *frame )
{
    xine_video_frame_t frames[2], *bestFrame = NULL;
    double variance, bestVariance = 0;

    for (int i=0, n=0; i < MAX_ATTEMPTS; i++)
    {
	xine_video_frame_t *cFrame = &frames[n];

	// Try to read next frame
	if (!xine_get_next_video_frame( vo_port, cFrame ))
	{
	    break;
	}

	variance = imageVariance( cFrame->data, ((cFrame->width + 7) & ~0x7),
				  cFrame->width, cFrame->height,
				 (cFrame->colorspace == XINE_IMGFMT_YV12) ? 1 : 2 );

	// Compare current frame to best frame
	if (bestFrame == NULL || variance > bestVariance)
	{
	    if (bestFrame != NULL)
	    {
		xine_free_video_frame( vo_port, bestFrame );
	    }

	    bestFrame = cFrame;
	    bestVariance = variance;

	    n = (1 - n);
	}
	else
	{
	    xine_free_video_frame( vo_port, cFrame );
	}

	// Stop searching if current frame is interesting enough
	if (variance > 40.0)
	{
	    break;
	}
    }

    // This should be the best frame to create a thumbnail from
    if (bestFrame != NULL)
    {
	*frame = *bestFrame;
    }
    return (bestFrame != NULL);
}


extern "C"
{
    ThumbCreator *new_creator()
    {
        return new VideoCreator;
    }
}

VideoCreator::VideoCreator()
{
}

VideoCreator::~VideoCreator()
{
}

bool VideoCreator::create(const QString &path, int width, int height, QImage &img)
{
    if (m_sprocketSmall.isNull())
    {
        QString pixmap = locate( "data", "videothumbnail/sprocket-small.png" );
        m_sprocketSmall = QPixmap(pixmap);
        pixmap = locate( "data", "videothumbnail/sprocket-medium.png" );
        m_sprocketMedium = QPixmap(pixmap);
        pixmap = locate( "data", "videothumbnail/sprocket-large.png" );
        m_sprocketLarge = QPixmap(pixmap);
    }

    // The long term plan is to seek to frame 1, create thumbnail, see if is is
    // interesting enough, if not seek to frame 2, then 4, then 8, etc.
    // "Interesting enough" means the variance of the pixel brightness is high. This
    // is because many videos fade up from black and a black rectangle is boring.
    //
    // But for the time being we can't seek so we just let it play for one second 
    // then take whatever we find.

    xine_t *xine = xine_shared_init();
    xine_audio_port_t *ao_port = xine_new_framegrab_audio_port( xine );
    xine_video_port_t *vo_port = xine_new_framegrab_video_port( xine );
    xine_stream_t *stream = xine_stream_new( xine, ao_port, vo_port );
    bool success = false;

    if (xine_open( stream, QFile::encodeName ( path ).data() ))
    {
	xine_video_frame_t frame;
	int length;

	// Find 'best' (or at least any) frame
	if (!xine_get_pos_length( stream, NULL, NULL, &length ) || length > 5000)
	{
	    if (xine_play( stream, 0, 4000 ))
	    {
		success = findBestFrame( vo_port, &frame );
	    }
	}
	if (!success)
	{
	    // Some codecs can't seek to start, but close/open works
	    xine_close( stream );
	    xine_open( stream, path.ascii() );

	    if (xine_play( stream, 0, 0 ))
	    {
		success = findBestFrame( vo_port, &frame );
	    }
	}

	// Create thumbnail image
	if (success)
	{
	    QPixmap pix( createThumbnail( &frame, width, height ) );
	    QPainter painter( &pix );
	    QPixmap sprocket;

	    if (pix.height() < 60)
		sprocket = m_sprocketSmall;
	    else if (pix.height() < 90)
		sprocket = m_sprocketMedium;
	    else
		sprocket = m_sprocketLarge;

	    for (int y = 0; y < pix.height() + sprocket.height(); y += sprocket.height()) {
		painter.drawPixmap( 0, y, sprocket );
	    }

	    img = pix.convertToImage();

	    xine_free_video_frame( vo_port, &frame );
	}

	xine_stop( stream );
    }

    xine_dispose( stream );
    xine_close_audio_driver( xine, ao_port );
    xine_close_video_driver( xine, vo_port );
    xine_shared_exit( xine );

    return (success);
}

ThumbCreator::Flags VideoCreator::flags() const
{
    return (ThumbCreator::Flags) (DrawFrame);
}

#include "videocreator.moc"