summaryrefslogtreecommitdiffstats
path: root/libvo/vo_dxr3.c
blob: 0d332c1638d145bf9a0a6c960293f77daacdfc60 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* 
 * vo_dxr3.c - DXR3/H+ video out
 *
 * Copyright (C) 2001 David Holm <dholm@iname.com>
 *
 */

#include "fastmemcpy.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/em8300.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>

#include "config.h"
#include "video_out.h"
#include "video_out_internal.h"

#include "../postproc/rgb2rgb.h"
#ifdef HAVE_MMX
#include "mmx.h"
#endif

LIBVO_EXTERN (dxr3)

#ifdef USE_MP1E
#include <rte.h>
rte_context* mp1e_context = NULL;
rte_codec* mp1e_codec = NULL;
rte_buffer mp1e_buffer;
struct { uint16_t Y,U,V; } YUV_s;
#define RGBTOY(R,G,B) (uint16_t)( (0.257 * R) + (0.504 * G) + (0.098 * B) + 16 )
#define RGBTOU(R,G,B) (uint16_t)( -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128 )
#define RGBTOV(R,G,B) (uint16_t)( (0.439 * R) - (0.368 * G) - (0.071 * B) + 128 )
#define RGBTOYUV(R,G,B) YUV_s.Y = RGBTOY(R,G,B); \
			YUV_s.U = RGBTOU(R,G,B); \
			YUV_s.V = RGBTOV(R,G,B);
#endif

static unsigned char *picture_data[3];
static unsigned int picture_linesize[3];
static int v_width,v_height;
static int s_width,s_height;
static int c_width,c_height;
static int s_pos_x,s_pos_y;
static int d_pos_x,d_pos_y;
static int osd_w,osd_h;
static int img_format = 0;
static uint32_t palette[] =
    {
	0x00505050,
	0x00000000,
	0x00808080,
	0xffffffff,
	0x00005555,
	0x0000ff00,
	0x00cc2255,
	0x00cc0055,
	0x00404040,
	0x00202020,
	0x00b0b0b0,
	0x00d0d0d0,
	0x60606000,
	0x70707000,
	0x80808000,
	0x60606000,
    };
static int fd_control = -1;
static int fd_video = -1;
static int fd_spu = -1;
static int ioval = 0;

static vo_info_t vo_info = 
{
	"DXR3/H+ video out",
	"dxr3",
	"David Holm <dholm@iname.com>",
	""
};

#ifdef USE_MP1E
void write_dxr3( rte_context* context, void* data, size_t size, void* user_data )
{
    if( ioctl( fd_video, EM8300_IOCTL_VIDEO_SETPTS, &vo_pts ) < 0 )
	printf( "VO: [dxr3] Unable to set video PTS\n" );
    write( fd_video, data, size );
}
#endif

static uint32_t init(uint32_t scr_width, uint32_t scr_height, uint32_t width, uint32_t height, uint32_t fullscreen, char *title, uint32_t format)
{
    fd_control = open( "/dev/em8300", O_WRONLY );
    if( fd_control < 1 )
    {
	printf( "VO: [dxr3] Error opening /dev/em8300 for writing!\n" );
	return -1;
    }
    fd_video = open( "/dev/em8300_mv", O_WRONLY );
    if( fd_video < 0 )
    {
	printf( "VO: [dxr3] Error opening /dev/em8300_mv for writing!\n" );
	return -1;
    }
    else printf( "VO: [dxr3] Opened /dev/em8300_mv\n" );
    fd_spu = open( "/dev/em8300_sp", O_WRONLY );
    if( fd_spu < 0 )
    {
	printf( "VO: [dxr3] Error opening /dev/em8300_sp for writing!\n" );
	return -1;
    }

    /* Subpic code isn't working yet, don't set to ON 
       unless you are really sure what you are doing */
    ioval = EM8300_SPUMODE_OFF;
    if( ioctl( fd_control, EM8300_IOCTL_SET_SPUMODE, &ioval ) < 0 )
    {
	printf( "VO: [dxr3] Unable to set subpicture mode!\n" );
	return -1;
    }
    
    if( ioctl( fd_spu, EM8300_IOCTL_SPU_SETPALETTE, palette ) < 0 )
    {
	printf( "VO: [dxr3] Unable to set subpicture palette!\n" );
	return -1;
    }

    ioval = EM8300_PLAYMODE_PLAY;
    if( ioctl( fd_control, EM8300_IOCTL_SET_PLAYMODE, &ioval ) < 0 )
	printf( "VO: [dxr3] Unable to set playmode!\n" );
    
    close( fd_control );

    img_format = format;
    v_width = width;
    v_height = height;
    s_width = scr_width;
    s_height = scr_height;

    if( format == IMGFMT_YV12 || format == IMGFMT_YUY2 || format == IMGFMT_BGR24 )
    {
#ifdef USE_MP1E
	int size;
	enum rte_frame_rate frame_rate;

	if( !rte_init() )
	{
	    printf( "VO: [dxr3] Unable to initialize RTE!\n" );
	    return -1;
	}
	
        if(width<=352 && height<=288){
          c_width=352;
          c_height=288;
        } else
        if(width<=352 && height<=576){
          c_width=352;
          c_height=576;
        } else
        if(width<=480 && height<=576){
          c_width=480;
          c_height=576;
        } else
        if(width<=544 && height<=576){
          c_width=544;
          c_height=576;
        } else {
          c_width=704;
          c_height=576;
        }
	
	mp1e_context = rte_context_new( c_width, c_height, "mp1e", (void*)0xdeadbeef );
	rte_set_verbosity( mp1e_context, 0 );
	
	printf( "VO: [dxr3] %dx%d => %dx%d\n", v_width, v_height, c_width, c_height );

	if( !mp1e_context )
	{
	    printf( "VO: [dxr3] Unable to create context!\n" );
	    return -1;
	}
	
	if( !rte_set_format( mp1e_context, "mpeg1" ) )
	{
	    printf( "VO: [dxr3] Unable to set format\n" );
	    return -1;
	}

	rte_set_mode( mp1e_context, RTE_VIDEO );
	mp1e_codec = rte_codec_set( mp1e_context, RTE_STREAM_VIDEO, 0, "mpeg1-video" );

	if( vo_fps < 24.0 ) frame_rate = RTE_RATE_1;
	else if( vo_fps < 25.0 ) frame_rate = RTE_RATE_2;
	else if( vo_fps < 29.97 ) frame_rate = RTE_RATE_3;
	else if( vo_fps < 30.0 ) frame_rate = RTE_RATE_4;
	else if( vo_fps < 50.0 ) frame_rate = RTE_RATE_5;
	else if( vo_fps < 59.97 ) frame_rate = RTE_RATE_6;
	else if( vo_fps < 60.0 ) frame_rate = RTE_RATE_7;
	else if( vo_fps > 60.0 ) frame_rate = RTE_RATE_8;
	else frame_rate = RTE_RATE_NORATE;

        if( !rte_set_video_parameters( mp1e_context, RTE_YUV420, mp1e_context->width,
					mp1e_context->height, frame_rate,
					3e6, "I" ) )
        {
            printf( "VO: [dxr3] Unable to set RTE context!\n" );
	    rte_context_destroy( mp1e_context );
	    return -1;
	}
	
	rte_set_input( mp1e_context, RTE_VIDEO, RTE_PUSH, TRUE, NULL, NULL, NULL );
	rte_set_output( mp1e_context, (void*)write_dxr3, NULL, NULL );
	
	if( !rte_init_context( mp1e_context ) )
	{
	    printf( "VO: [dxr3] Unable to init RTE context!\n" );
	    rte_context_delete( mp1e_context );
	    return -1;
	}

        osd_w=scr_width;
        d_pos_x=(c_width-(int)scr_width)/2;
        if(d_pos_x<0)
	{
    	    s_pos_x=-d_pos_x;d_pos_x=0;
    	    osd_w=c_width;
        } else s_pos_x=0;
    
        osd_h=scr_height;
        d_pos_y=(c_height-(int)scr_height)/2;
        if(d_pos_y<0)
	{
    	    s_pos_y=-d_pos_y;d_pos_y=0;
    	    osd_h=c_height;
        } else s_pos_y=0;
    
        printf("VO: [dxr3] Position mapping: %d;%d => %d;%d\n",s_pos_x,s_pos_y,d_pos_x,d_pos_y);
                
        size = c_width*c_height;

        picture_data[0] = malloc((size * 3)/2);
	picture_data[1] = picture_data[0] + size;
	picture_data[2] = picture_data[1] + size / 4;
	picture_linesize[0] = c_width;
	picture_linesize[1] = c_width / 2;
	picture_linesize[2] = c_width / 2;

	// Set the border colorwou
	RGBTOYUV(0,0,0)
        memset( picture_data[0], YUV_s.Y, picture_linesize[0]*c_height );
        memset( picture_data[1], YUV_s.U, picture_linesize[1]*(c_height/2) );
        memset( picture_data[2], YUV_s.V, picture_linesize[2]*(c_height/2) );
	
	if( !rte_start_encoding( mp1e_context ) )
	{
	    printf( "VO: [dxr3] Unable to start mp1e encoding!\n" );
	    uninit();
	    return -1;
	}

	return 0;
#endif
	return -1;    
    }
    else if(format==IMGFMT_MPEGPES)
    {
	printf( "VO: [dxr3] Format: MPEG-PES (no conversion needed)\n" );
	return 0;
    }

    printf( "VO: [dxr3] Format: Unsupported\n" );
    return -1;
}

static const vo_info_t* get_info(void)
{
    return &vo_info;
}

static void draw_alpha(int x0, int y0, int w, int h, unsigned char* src, unsigned char *srca, int srcstride)
{
}

static void draw_osd(void)
{
}

static uint32_t draw_frame(uint8_t * src[])
{
    if( img_format == IMGFMT_MPEGPES )
    {
        int data_left;
	vo_mpegpes_t *p=(vo_mpegpes_t *)src[0];

	if( ioctl( fd_video, EM8300_IOCTL_VIDEO_SETPTS, &vo_pts ) < 0 )
	    printf( "VO: [dxr3] Unable to set video PTS\n" );

	data_left = p->size;
	while( data_left )
	    data_left -= write( fd_video, &((unsigned char*)p->data)[p->size-data_left], data_left );

	return 0;
    }
#ifdef USE_MP1E
    else if( img_format == IMGFMT_YUY2 )
    {
	int w=v_width,h=v_height;
	unsigned char *s,*dY,*dU,*dV;
	
        if(d_pos_x+w>picture_linesize[0]) w=picture_linesize[0]-d_pos_x;
        if(d_pos_y+h>c_height) h=c_height-d_pos_y;

	s = src[0]+s_pos_x+s_pos_y*(w*2);
	dY = picture_data[0]+d_pos_x+d_pos_y*picture_linesize[0];
	dU = picture_data[1]+(d_pos_x/2)+(d_pos_y/2)*picture_linesize[1];
	dV = picture_data[2]+(d_pos_x/2)+(d_pos_y/2)*picture_linesize[2];
	
	yuy2toyv12( s, dY, dU, dV, w, h, picture_linesize[0], picture_linesize[1], w*2 );
	
	mp1e_buffer.data = picture_data[0];
	mp1e_buffer.time = vo_pts/90000.0;
	mp1e_buffer.user_data = NULL;
	rte_push_video_buffer( mp1e_context, &mp1e_buffer );
	return 0;
    }
    else if( img_format == IMGFMT_BGR24 )
    {
	int x,y,w=v_width,h=v_height;
	unsigned char *s,*dY,*dU,*dV;
	
        if(d_pos_x+w>picture_linesize[0]) w=picture_linesize[0]-d_pos_x;
        if(d_pos_y+h>c_height) h=c_height-d_pos_y;

	s = src[0]+s_pos_y*(w*3);

	dY = picture_data[0]+d_pos_y*picture_linesize[0];
	dU = picture_data[1]+(d_pos_y/2)*picture_linesize[1];
	dV = picture_data[2]+(d_pos_y/2)*picture_linesize[2];
	
	for(y=0;y<h;y++)
	{
	    dY+=d_pos_x;
	    dU+=d_pos_x/4;
	    dV+=d_pos_x/4;
	    s+=s_pos_x;
	    for(x=0;x<w;x+=4)
	    {
		RGBTOYUV(s[2],s[1],s[0]);
		s+=3;
		*dY = YUV_s.Y;dY++;
//	The chrominance is shifted, ppl will have to settle with b&w for now ;)
//		*dU = YUV_s.U;dU++;
//		*dV = YUV_s.V;dV++;
		
		*dY = RGBTOY(s[2],s[1],s[0]);dY++;
		s+=3;
		*dY = RGBTOY(s[2],s[1],s[0]);dY++;
		s+=3;
		*dY = RGBTOY(s[2],s[1],s[0]);dY++;
		s+=3;
	    }
	    dY+=d_pos_x;
	    dU+=d_pos_x/4;
	    dV+=d_pos_x/4;
	    s+=s_pos_x;
	}
	
	mp1e_buffer.data = picture_data[0];
	mp1e_buffer.time = vo_pts/90000.0;
	mp1e_buffer.user_data = NULL;
	rte_push_video_buffer( mp1e_context, &mp1e_buffer );
	
	return 0;
    }
#endif

    return -1;
}

static void flip_page (void)
{
    if( img_format == IMGFMT_YV12 )
    {
	mp1e_buffer.data = picture_data[0];
	mp1e_buffer.time = vo_pts/90000.0;
	mp1e_buffer.user_data = NULL;
	rte_push_video_buffer( mp1e_context, &mp1e_buffer );
    }
}

static uint32_t draw_slice( uint8_t *srcimg[], int stride[], int w, int h, int x0, int y0 )
{
    if( img_format == IMGFMT_YV12 )
    {
#ifdef USE_MP1E
	int y;
	unsigned char *s,*s1;
	unsigned char *d,*d1;

	x0+=d_pos_x;
	y0+=d_pos_y;

        if(x0+w>picture_linesize[0]) w=picture_linesize[0]-x0;
        if(y0+h>c_height) h=c_height-y0;

        s=srcimg[0]+s_pos_x+s_pos_y*stride[0];
        d=picture_data[0]+x0+y0*picture_linesize[0];
	for(y=0;y<h;y++)
	{
	    memcpy(d,s,w);
	    s+=stride[0];
	    d+=picture_linesize[0];
	}

	w/=2;h/=2;x0/=2;y0/=2;
	
	s=srcimg[1]+s_pos_x+s_pos_y*stride[1];
	d=picture_data[1]+x0+y0*picture_linesize[1];
	s1=srcimg[2]+s_pos_x+s_pos_y*stride[2];
	d1=picture_data[2]+x0+y0*picture_linesize[2];
	for(y=0;y<h;y++)
	{
	    memcpy(d,s,w);
	    memcpy(d1,s1,w);
	    s+=stride[1];s1+=stride[2];
	    d+=picture_linesize[1];d1+=picture_linesize[2];
	}

	return 0;
#endif
    }

    return -1;
}


static uint32_t
query_format(uint32_t format)
{
    if(format==IMGFMT_MPEGPES) return 1;
#ifdef USE_MP1E
    if(format==IMGFMT_YV12) return 1;
    if(format==IMGFMT_YUY2) return 1;
    if(format==IMGFMT_BGR24) { printf( "VO: [dxr3] WARNING\tExperimental output, black&white only and very slow\n\t(will be inproved later, this format is rarely used)\n" ); return 1; }
#else
    if(format==IMGFMT_YV12) {printf("VO: [dxr3] You need to compile with mp1e rte to play this file! Read DOCS/DXR3\n" ); return 0;}
    if(format==IMGFMT_YUY2) {printf("VO: [dxr3] You need to compile with mp1e rte to play this file! Read DOCS/DXR3\n" ); return 0;}
    if(format==IMGFMT_BGR24) {printf("VO: [dxr3] You need to compile with mp1e rte to play this file! Read DOCS/DXR3\n" ); return 0;}
#endif
    else printf( "VO: [dxr3] Format unsupported, mail dholm@iname.com\n" );
    return 0;
}

static void uninit(void)
{
    printf( "VO: [dxr3] Uninitializing\n" );
    if( picture_data[0] ) free(picture_data[0]);
    if( fd_video ) close(fd_video);
    if( fd_spu ) close(fd_spu);
}


static void check_events(void)
{
}