summaryrefslogtreecommitdiff
path: root/packages/mplayer/files/vo_pxa.c
blob: 1488d14064d064900a8562d645931a1b585b7248 (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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/*
 * Video driver for PXA 27x Overlay 2, in conjunction with kernel driver
 * by Tim Chick <tim (DOT) chick (AT) csr (DOT) com>
 * (C) 2007
 */

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

#include "config.h"
#include "video_out.h"
#include "video_out_internal.h"
#include "sub.h"
#include "aspect.h"
#include "mp_msg.h"
#include "subopt-helper.h"

#include "vo_pxa.h"

static vo_info_t info = {
	"PXA 27x Framebuffer",
	"pxa",
	"Tim Chick <tim (DOT) chick (AT) csr (DOT) com>",
	"For Sharp Zaurus SL-C1000 etc"
};

LIBVO_EXTERN(pxa);

static pxa_priv_t st_pxa_priv;

/*****************************************************************************
 * preinit
 *
 * Preinitializes driver
 *   arg - currently it's vo_subdevice
 *   returns: zero on successful initialization, non-zero on error.
 *
 ****************************************************************************/
static int preinit(const char *vo_subdevice)
{
    pxa_priv_t *priv = &st_pxa_priv;
    int rc;
    
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: preinit() was called\n");

    /* fill pxa_priv_t information */
    memset(priv, 0, sizeof(*priv));
    priv->fd = -1;
    
    /* We need to open the base framebuffer device, to change and restore modes */
    priv->base_fd = open( "/dev/fb0", O_RDWR );
    
    if( priv->base_fd < 0 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: Could not open base framebuffer device\n");
        return -1;
    }
    
    /* Get the base fb var data, so we can restore if we change video modes */
    rc = ioctl( priv->base_fd, FBIOGET_VSCREENINFO, &(priv->base_orig_fb_var) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: FBIOGET_VSCREENINFO preinit base_fd failed %d\n",
                errno );
        
        /* If this failed, close down the FD so we don't try to set this again */
        close( priv->base_fd );
        priv->base_fd = -1;
        
        return -1;
    }
    
    return 0;
}


/*****************************************************************************
 * config
 *
 * Config the display driver.
 * params:
 *   src_width,srcheight: image source size
 *   dst_width,dst_height: size of the requested window size, just a hint
 *   fullscreen: flag, 0=windowd 1=fullscreen, just a hint
 *   title: window title, if available
 *   format: fourcc of pixel format
 * returns : zero on successful initialization, non-zero on error.
 *
 ****************************************************************************/
static int config(uint32_t src_width, uint32_t src_height,
                  uint32_t dst_width, uint32_t dst_height, uint32_t flags,
                  char *title, uint32_t format)
{
    pxa_priv_t *priv = &st_pxa_priv;
    int rc;
    int i;
    
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: config() src_width:%d, src_height:%d, dst_width:%d, dst_height:%d\n",
	   src_width, src_height, dst_width, dst_height);

    /* Check format */
    if( !vo_pxa_query_format(format) )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: unsupported fourcc for this driver: %x (%s)\n",
                format, vo_format_name(format) );
        goto err_out;
    }
    priv->format = format;
    
    /* Change resolution? */
    priv->vm = flags & VOFLAG_MODESWITCHING;
    if( priv->vm )
    {
        priv->my_fb_var = priv->base_orig_fb_var;
        
        /* Hard coded values suck, never mind */
        priv->my_fb_var.xres = 240;
        priv->my_fb_var.yres = 320;
        priv->my_fb_var.pixclock = 134617;
        priv->my_fb_var.left_margin = 20;
        priv->my_fb_var.right_margin = 46;
        priv->my_fb_var.upper_margin = 1;
        priv->my_fb_var.lower_margin = 0;
        priv->my_fb_var.hsync_len = 20;
        priv->my_fb_var.vsync_len = 2;
    
        rc = ioctl( priv->base_fd, FBIOPUT_VSCREENINFO, &(priv->my_fb_var) );
        
        if( rc == -1 )
        {
            mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() Set FBIOPUT_VSCREENINFO on base_fd failed %d\n",
                    errno );
            priv->vm = 0;
            goto err_out;
        }
        
        /* We need this sleep, to make the change in resolution actually happen, before we open the overlay */
        sleep(1);
    }
 
   
    /* Open up the overlay fbdev */
    priv->fd = open( "/dev/fb2", O_RDWR );

    if( priv->fd < 0 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: Could not open /dev/fb2: %d\n", errno );
        goto err_out;
    }
    
    /* Read in fb var data */
    rc = ioctl( priv->fd, FBIOGET_VSCREENINFO, &(priv->my_fb_var) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOGET_VSCREENINFO from fd failed %d\n",
                errno );
        goto err_out;
    }
    
    /* Store away the source dimensions, so we can place in centre of screen later in vm mode */
    priv->src_width = src_width;
    priv->src_height = src_height;
    
    /* Set up the buffer */
    if( priv->vm )
    {
        /* Ignore size, as the rest of the screen is toast. Use max size */
        priv->my_fb_var.xres = 240;
        priv->my_fb_var.yres = 320;

        /* Do we need to rotate? */
        if( priv->src_width > priv->src_height )
        {
            /* Yes */
            priv->rotate = 1;
        }

        priv->width  = 240;
        priv->height = 320;
    }
    else
    {
        priv->my_fb_var.xres = src_width;
        priv->my_fb_var.yres = src_height;
        priv->width = src_width;
        priv->height = src_height;
    }
    
    priv->my_fb_var.nonstd = ( 4 << 20)  /* Format YV12 */
                           | ( 0 <<  0)  /* x position */
                           | ( 0 << 10); /* y position */ 
    /* We have to set the bits per pixel to a valid value, even though it is
     * incorrect for YV12
     */
    priv->my_fb_var.bits_per_pixel = 16;
    
    rc = ioctl( priv->fd, FBIOPUT_VSCREENINFO, &(priv->my_fb_var) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOPUT_VSCREENINFO to fd failed: %d\n",
                errno );
        goto err_out;
    }
        
    /* Next get the fixed fbvars, so we can mmap the data for all 3 planes */
    rc = ioctl( priv->fd, FBIOGET_FSCREENINFO, &(priv->my_fb_fix) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOGET_FSCREENINFO from fd failed: %d\n", 
                errno );
        goto err_out;
    }
    
    priv->fb_mem_base = mmap( NULL, priv->my_fb_fix.smem_len, (PROT_READ | PROT_WRITE ),
                        MAP_SHARED,
                        priv->fd,
                        0 );

    if( priv->fb_mem_base == MAP_FAILED )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: mmap fd buffer failed: %d\n", errno );
        goto err_out;
    }

    /* Finally, find the offsets of each plane by getting the var data again */
    rc = ioctl( priv->fd, FBIOGET_VSCREENINFO, &(priv->my_fb_var) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOGET_VSCREENINFO from fd (2) failed %d\n", 
                errno );
        goto err_out;
    }
    
    /* Fill the overlay with black */
    memset( priv->fb_mem_base + priv->my_fb_var.red.offset, 16, priv->my_fb_var.red.length );
    memset( priv->fb_mem_base + priv->my_fb_var.green.offset, 128, priv->my_fb_var.green.length );
    memset( priv->fb_mem_base + priv->my_fb_var.blue.offset, 128, priv->my_fb_var.blue.length );
    
    /* Now open the OSD overlay - overlay 1, and fill with transparent */
    sleep( 1 );
    
    priv->overlay_fd = open( "/dev/fb1", O_RDWR );

    if( priv->overlay_fd < 0 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: Could not open /dev/fb1: %d\n", errno );
        goto err_out;
    }
    
    /* Read in fb var data */
    rc = ioctl( priv->overlay_fd, FBIOGET_VSCREENINFO, &(priv->osd_fb_var) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOGET_VSCREENINFO from overlay_fd failed; %d\n",
                errno );
        goto err_out;
    }

    priv->osd_fb_var.xres = priv->width;
    priv->osd_fb_var.yres = priv->height;
    priv->osd_fb_var.nonstd = ( 0 <<  0)  /* x position */
                            | ( 0 << 10); /* y position */
    /* Use 15 bit mode, with top bit transparency */
    priv->osd_fb_var.bits_per_pixel = 16;
    
    rc = ioctl( priv->overlay_fd, FBIOPUT_VSCREENINFO, &(priv->osd_fb_var) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOPUT_VSCREENINFO to overlay_fd failed: %d\n",
                errno );
        goto err_out;
    }
        
    /* Next get the fixed fbvars, so we can mmap the data */
    rc = ioctl( priv->overlay_fd, FBIOGET_FSCREENINFO, &(priv->osd_fb_fix) );
    
    if( rc == -1 )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: config() FBIOGET_FSCREENINFO from overlay_fd failed %d\n",
                errno );
        goto err_out;
    }
    
    priv->osd_mem_base = mmap( NULL, priv->osd_fb_fix.smem_len, (PROT_READ | PROT_WRITE ),
                               MAP_SHARED,
                               priv->overlay_fd,
                               0 );

    if( priv->osd_mem_base == MAP_FAILED )
    {
        mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: mmap osd_mem_base failed: %d\n", errno );
        goto err_out;
    }
    
    /* Fill the overlay with transparent */
    vo_pxa_clear_osd( priv->osd_mem_base, priv->osd_fb_fix.smem_len );

    /* We are good to go! */
    mp_msg( MSGT_VO, MSGL_V, "vo_pxa: Opened video overlay %d x %d fourcc %s\n",
            priv->my_fb_var.xres,
            priv->my_fb_var.yres,
            vo_format_name(format) );
    
    return 0;
    
    err_out:

    /* Don't do anything here for the moment */
    return -1;
}


/*****************************************************************************
 *
 * control
 *
 * Control display
 *
 ****************************************************************************/
static int control(uint32_t request, void *data, ...)
{
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: control %08x\n", request );
    
    switch( request )
    {
        case VOCTRL_QUERY_FORMAT:
            return( vo_pxa_query_format( *(uint32_t *)data ) );
            break;
    }
    
    return VO_NOTIMPL;
}


/*****************************************************************************
 *
 * draw_frame
 *
 * Display a new RGB/BGR frame of the video to the screen.
 * params:
 *   src[0] - pointer to the image
 *
 ****************************************************************************/
int draw_frame(uint8_t *src[])
{
    /* This is not implimented */
    mp_msg(MSGT_VO, MSGL_ERR, "vo_pxa: dummy draw_frame() was called\n");
    return -1;
}


/*****************************************************************************
 *
 * draw_slice
 *
 * Draw a planar YUV slice to the buffer:
 * params:
 *   src[3] = source image planes (Y,U,V)
 *   stride[3] = source image planes line widths (in bytes)
 *   w,h = width*height of area to be copied (in Y pixels)
 *   x,y = position at the destination image (in Y pixels)
 *
 ****************************************************************************/
int draw_slice(uint8_t *src[], int stride[], int w,int h, int x,int y)
{
    pxa_priv_t *priv = &st_pxa_priv;
    
    /* This routine is only display routine actually implimented */
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: draw_slice() w %d h %d x %d y %d stride %d %d %d\n",
           w, h, x, y, stride[0], stride[1], stride[2] );
    
    /* It would be faster to check if source and dest have same geometry and copy
     * whole block
     * For the moment we just copy a line at a time
     */

    /* In vm mode rotate if wider than long */
    if( priv->vm )
    {
        /* Do we nee to rotate? */
        if( priv->rotate )
        {
            /* Yes, rotated version */
            int dst_x_offset = 0;
            int dst_y_offset = 0;
            int src_x_offset = 0;
            int src_y_offset = 0;
        
            /* Figure out  dst offset */
            if( priv->src_width < 320 )
            {
                dst_x_offset = ( ( 320 -  priv->src_width ) / 2 );
                /* Make it a multiple of 16 */
                dst_x_offset &= ~(0xf);
            }
        
            if( priv->src_height < 240 )
            {
                dst_y_offset = ( ( 240 -  priv->src_height ) / 2 );
                /* Make it a multiple of 16 */
                dst_y_offset &= ~(0xf);
            }
        
            dst_x_offset += x;
            dst_y_offset += y;
        
            if( ( dst_x_offset >= 320 ) || ( dst_y_offset >= 240 ) )
            {
                /* Nothing to do - drawing off the screen! */
                return( 0 );
            }
        
            /* Limit to drawable area */
            if( ( w + dst_x_offset ) > 320 )
            {
                w = ( 320 - dst_x_offset );
            }
        
            if( ( h + dst_y_offset ) > 240 )
            {
                h = ( 240 - dst_y_offset );
            }
            
            /* And source offset */
            if( priv->src_width > 320 )
            {
                src_x_offset = ( ( priv->src_width - 320 ) / 2 );
                /* Make it a multiple of 16 */
                src_x_offset &= ~(0xf);
            }
        
            if( priv->src_height > 240 )
            {
                src_y_offset = ( ( priv->src_height - 240 ) / 2 );
                /* Make it a multiple of 16 */
                src_y_offset &= ~(0xf);
            }
            
        
            /* Y first */
            vo_pxa_copy_and_rotate( src[0] + src_x_offset + (src_y_offset * stride[0]), stride[0],
                                    priv->fb_mem_base + priv->my_fb_var.red.offset  + (240 * dst_x_offset) + (240 - dst_y_offset - h),
                                    w, h, 240 );
            /* Now U */
            vo_pxa_copy_and_rotate( src[1] + src_x_offset/2 + (src_y_offset/2 * stride[1]), stride[1],
                                    priv->fb_mem_base + priv->my_fb_var.green.offset + (120 * dst_x_offset/2) + (120 - dst_y_offset/2 - h/2),
                                    w/2, h/2, 120 );
            vo_pxa_copy_and_rotate( src[2] + src_x_offset/2 + (src_y_offset/2 * stride[2]), stride[2],
                                    priv->fb_mem_base + priv->my_fb_var.blue.offset + (120 * dst_x_offset/2) + (120 - dst_y_offset/2 - h/2),
                                    w/2, h/2, 120 );
        }
        else
        {
            /* Don't rotate */
            int i;
            uint8_t *my_src;
            uint8_t *dest;
            int dst_x_offset = 0;
            int dst_y_offset = 0;
            int src_x_offset = 0;
            int src_y_offset = 0;
        
            /* Figure out  dst offset */
            if( priv->src_width < 240 )
            {
                dst_x_offset = ( ( 240 -  priv->src_width ) / 2 );
                /* Make it a multiple of 16 */
                dst_x_offset &= ~(0xf);
            }
        
            if( priv->src_height < 320 )
            {
                dst_y_offset = ( ( 320 -  priv->src_height ) / 2 );
                /* Make it a multiple of 16 */
                dst_y_offset &= ~(0xf);
            }
        
            dst_x_offset += x;
            dst_y_offset += y;
        
            if( ( dst_x_offset >= 240 ) || ( dst_y_offset >= 320 ) )
            {
                /* Nothing to do - drawing off the screen! */
                return( 0 );
            }
        
            /* Limit to drawable area */
            if( ( w + dst_x_offset ) > 240 )
            {
                w = ( 240 - dst_x_offset );
            }
        
            if( ( h + dst_y_offset ) > 320 )
            {
                h = ( 320 - dst_y_offset );
            }

            /* And source offset */
            if( priv->src_width > 240 )
            {
                src_x_offset = ( ( priv->src_width - 240 ) / 2 );
                /* Make it a multiple of 16 */
                src_x_offset &= ~(0xf);
            }
        
            if( priv->src_height > 320 )
            {
                src_y_offset = ( ( priv->src_height - 320 ) / 2 );
                /* Make it a multiple of 16 */
                src_y_offset &= ~(0xf);
            }
            
            /* First Y */
            for( i = 0; i<h; i++ )
            {
                dest = priv->fb_mem_base + 
                    priv->my_fb_var.red.offset + 
                    ( (dst_y_offset+i) * priv->my_fb_fix.line_length ) +
                    dst_x_offset;
                my_src = src[0] + src_x_offset + (stride[0] * (i+src_y_offset));
                memcpy( dest, my_src, w );
            }

            /* Now U */
            for( i = 0; i<(h/2); i++ )
            {
                dest = priv->fb_mem_base + 
                    priv->my_fb_var.green.offset + 
                    ( ((dst_y_offset/2)+i) * (priv->my_fb_fix.line_length/2) ) +
                    dst_x_offset/2;
                my_src = src[1] + src_x_offset/2 + (stride[1] * (i+(src_y_offset/2)));
                memcpy( dest, my_src, w/2 );
            }
    
            /* Finaly V */
            for( i = 0; i<(h/2); i++ )
            {
                dest = priv->fb_mem_base + 
                    priv->my_fb_var.blue.offset + 
                    ( ((dst_y_offset/2)+i) * (priv->my_fb_fix.line_length/2) ) +
                    dst_x_offset/2;
                my_src = src[2] + src_x_offset/2 + (stride[2] * (i+(src_y_offset/2)));
                memcpy( dest, my_src, w/2 );
            }
            
        }
    }
    else
    {
        /* Not full screen mode */
        uint8_t *my_src;
        uint8_t *dest;
        size_t length;
        int i;
        
        /* It would be faster to check if source and dest have same geometry and copy
         * whole block
         * For the moment we just copy a line at a time
         */

        /* Limit area written to */
        if( x >= priv->my_fb_fix.line_length )
        {
            return 0;
        }
    
        if( w + x > priv->my_fb_fix.line_length )
        {
            w = priv->my_fb_fix.line_length - x;
        }
    
        if( y>= priv->my_fb_var.yres )
        {
            return 0;
        }
    
        if( h + y > priv->my_fb_var.yres )
        {
            h = priv->my_fb_var.yres - y;
        }
    
        /* First Y */
        for( i = 0; i<h; i++ )
        {
            dest = priv->fb_mem_base + 
                priv->my_fb_var.red.offset + 
                ( (y+i) * priv->my_fb_fix.line_length ) +
                x;
            my_src = src[0] + stride[0] * i;
            memcpy( dest, my_src, w );
        }

        /* Now U */
        for( i = 0; i<(h/2); i++ )
        {
            dest = priv->fb_mem_base + 
                priv->my_fb_var.green.offset + 
                ( ((y/2)+i) * (priv->my_fb_fix.line_length/2) ) +
                x;
            my_src = src[1] + stride[1] * i;
            memcpy( dest, my_src, w/2 );
        }
    
        /* Finaly V */
        for( i = 0; i<(h/2); i++ )
        {
            dest = priv->fb_mem_base + 
                priv->my_fb_var.blue.offset + 
                ( ((y/2)+i) * (priv->my_fb_fix.line_length/2) ) +
                x;
            my_src = src[2] + stride[2] * i;
            memcpy( dest, my_src, w/2 );
        }
    }
    return 0;
}

static void draw_osd(void)
{
    pxa_priv_t *priv = &st_pxa_priv;
    int osd_has_changed;

    /* This gets called every frame, so systems which do the OSD without a
     * seperate overlay can mix in the image. We need to find out if the osd
     * has actually been updated!
     */
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: draw_osd() was called\n");
    
    osd_has_changed = vo_update_osd( priv->width, priv->height);

    if(osd_has_changed)
    {
        int i;
        
        mp_msg(MSGT_VO, MSGL_V, "vo_pxa: Clear and update OSD\n");
        
        /* Fill with transparent */
        vo_pxa_clear_osd( priv->osd_mem_base, priv->osd_fb_fix.smem_len );
        
        priv->osd_cleared = 1;
        
        /* now update */
        if( priv->rotate )
        {
            vo_draw_text( priv->width, priv->height, vo_pxa_draw_alpha_with_rotate );
        }
        else
        {
            vo_draw_text( priv->width, priv->height, vo_pxa_draw_alpha );
        }
    }
}

/*****************************************************************************
 *
 * flip_page
 *
 * Blit/Flip buffer to the screen. Must be called after each frame!
 *
 *
 ****************************************************************************/
static void flip_page(void)
{
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: flip_page() was called\n");
}

/*****************************************************************************
 *
 * check_events
 *
 *
 ****************************************************************************/
static void check_events(void)
{
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: check_events() was called\n");
}

/*****************************************************************************
 *
 * uninit
 *
 *
 ****************************************************************************/
static void uninit(void)
{
    pxa_priv_t *priv = &st_pxa_priv;
    int rc;

    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: uninit() was called\n");
    
    if( priv->vm )
    {
        /* We need these sleeps, to make the change in resolution actually happen */
        sleep(1);

        /* Restore original resolution */
        if( priv->base_fd >= 0 )
        {
            rc = ioctl( priv->base_fd, FBIOPUT_VSCREENINFO, &(priv->base_orig_fb_var) );
    
            if( rc == -1 )
            {
                mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: uninit() FBIOPUT_VSCREENINFO to base_fd failed %d\n",
                        errno );
            }
        }
        /* We need these sleeps, to make the change in resolution actually happen */
        /* For some reason, if we change the reolution the overlay buffer never gets deleted? */
        sleep(1);
    }
    

    /* We need to force the overlays to be really disabled, otherwise they
     * will come back as zombies after suspend, resume
     * This trick seems to work, but will not be needed once kernel driver
     * is fixed
     */
    if( priv->fd >= 0 )
    {
        rc = ioctl( priv->fd, FBIOGET_VSCREENINFO, &(priv->my_fb_var) );

        if( rc == -1 )
        {
            mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: uninit() FBIOGET_VSCREENINFO from fd failed %d\n",
                    errno );
        }
	priv->my_fb_var.bits_per_pixel = 0;

        rc = ioctl( priv->fd, FBIOPUT_VSCREENINFO, &(priv->my_fb_var) );

        if( rc == -1 )
        {
            mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: uninit() FBIOPUT_VSCREENINFO from fd failed %d\n",
                    errno );
        }
    }

    if( priv->overlay_fd >= 0 )
    {
        rc = ioctl( priv->overlay_fd, FBIOGET_VSCREENINFO, &(priv->my_fb_var) );

        if( rc == -1 )
        {
            mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: uninit() FBIOGET_VSCREENINFO from overlay_fd failed %d\n",
                    errno );
        }
        priv->my_fb_var.bits_per_pixel = 0;

        rc = ioctl( priv->overlay_fd, FBIOPUT_VSCREENINFO, &(priv->my_fb_var) );

        if( rc == -1 )
        {
            mp_msg( MSGT_VO, MSGL_ERR, "vo_pxa: uninit() FBIOPUT_VSCREENINFO from overlay_fd failed %d\n",
                    errno );
        }
    }

    if( priv->base_fd >= 0 )
    {
        close( priv->base_fd );
        priv->base_fd = -1;
    }
}

/*****************************************************************************
 *
 * Internal functions, not part of mplayer API
 *
 ****************************************************************************/

static int vo_pxa_query_format( uint32_t format )
{
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: vo_pxa_query_format was called: %x (%s)\n",
	   format, vo_format_name(format));

    switch (format)
    {
        /* Planar YUV Formats */
        /* Warning! dropthrough */
        case IMGFMT_YV12:
        case IMGFMT_IYUV:
        case IMGFMT_I420:
            return( VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
                    | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD
                    | VFCAP_ACCEPT_STRIDE );
            break;
    }
        
    return 0;
}

static void vo_pxa_copy_and_rotate( uint8_t *src, int stride, uint8_t *dst, int w, int h, int dst_stride )
{
    int i,j;
    uint8_t *my_src, *my_dst;
    Vo_Pxa_Pixel_Data8 *img_dst_pixel_data8;                       
        
    /* Loop so writing consectuive data in rotated image */
    /* This produces some pretty good assembler - better than the handcoded stuff in w100 */
    for( j=0; j<w; j++ )
    {
        my_src = src + j + ( stride * (h - 1) );

        img_dst_pixel_data8 = (Vo_Pxa_Pixel_Data8 *)dst;
        
        /* Allow for src not multiple of 8 by running off the end a little. Should not matter */
        for( i=0; i<((h+7)/8); i++ )
        {
            register Vo_Pxa_Pixel_Data8 build_pixels;
                
            build_pixels.a = *my_src;
            my_src -= stride;
            build_pixels.a |= (*my_src<<8);
            my_src -= stride;
            build_pixels.a |= (*my_src<<16);
            my_src -= stride;
            build_pixels.a |= (*my_src<<24);
            my_src -= stride;

            build_pixels.b = *my_src;
            my_src -= stride;
            build_pixels.b |= (*my_src<<8);
            my_src -= stride;
            build_pixels.b |= (*my_src<<16);
            my_src -= stride;
            build_pixels.b |= (*my_src<<24);
            my_src -= stride;
                
            *img_dst_pixel_data8++ = build_pixels;
        }
        
        /* Allow source not as big as dest */
        dst += dst_stride;
    }   
}

static void vo_pxa_draw_alpha( int x, int y, int w, int h, unsigned char *src,
                               unsigned char *srca, int stride )
{
    /* Dump data into our 15bit buffer with transparency */
    pxa_priv_t *priv = &st_pxa_priv;
    int i,j;
    unsigned char *src_ptr = src;
    unsigned char *a_ptr = srca;
    unsigned short *out_ptr;
    
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: vo_pxa_draw_alpha() w %d y %d w %d h %d\n", x, y, w, h );
    
    /* We ignore the alpha channel, other than off or on */
    for( i=0; i<h; i++ )
    {
        out_ptr = priv->osd_mem_base + x + ( priv->width * ( y + i ) );
        src_ptr = src + ( i * stride );
        a_ptr = srca + ( i * stride );
            
        for( j=0; j<w; j++ )
        {
            /* The srca is a 0-255 transpaency level, where 0 is transparent.
             * We only support transparent on or off
             */
            if( *a_ptr++ )
            {
                unsigned int grey;
                /* The src is a greylevel from 0 - 255 */
                /* We may as well use this value */
                grey = *src_ptr++ >> 3;
                *out_ptr++ = grey | (grey << 5) | (grey<<10);                
            }
            else
            {                
                *out_ptr++ = 0x8000;
                src_ptr++;
            }
            
        }
    }
}

static void vo_pxa_draw_alpha_with_rotate( int x, int y, int w, int h, unsigned char *src,
                                           unsigned char *srca, int stride )
{
    /* Dump data into our 15bit buffer with transparency */
    pxa_priv_t *priv = &st_pxa_priv;
    int i,j;
    unsigned char *src_ptr = src;
    unsigned char *a_ptr = srca;
    unsigned short *out_ptr;
    
    mp_msg(MSGT_VO, MSGL_V, "vo_pxa: vo_pxa_draw_alpha_with_rotate() x %d y %d w %d h %d\n", x, y, w, h );
        
    if( x >= 320 )
    {
        /* Off the screen */
        return;
    }

    /* Limit to size of screen/memory */
    if( ( w + x ) > 320 )
    {
        w = 320 - x;
    }
    
    if( y >= 240 )
    {
        /* Off the screen */
        return;
    }
    
    /* Limit to size of screen/memory */
    if( ( y + h ) > 240 )
    {
        h = 240 - y;
    }
    
    
    /* We ignore the alpha channel, other than off or on */
    for( i=0; i<w; i++ )
    {
        out_ptr = priv->osd_mem_base + y + ( priv->width * ( x + i ) );
        src_ptr = src + i + ( stride * (h - 1));
        a_ptr = srca + i + ( stride * (h - 1));
            
        for( j=0; j<h; j++ )
        {
            /* The srca is a 0-255 transpaency level, where 0 is transparent.
             * We only support transparent on or off
             */
            if( *a_ptr )
            {
                unsigned int grey;
                /* The src is a greylevel from 0 - 255 */
                /* We may as well use this value */
                grey = *src_ptr >> 3;
                *out_ptr++ = grey | (grey << 5) | (grey<<10);                
            }
            else
            {                
                *out_ptr++ = 0x8000;
                src_ptr;
            }
            a_ptr -= stride;
            src_ptr -= stride;
        }
    }
}

static void vo_pxa_clear_osd( uint16_t *mem_base, int len )
{
    /* fill whole area with 0x8000 -> trsnaparent.
     * assume area is word aligned, and a mulitple of 16 bytes in length
     * However I tried I could not get the compiler to generate this.
     * It always wanted to  to do ldmia 4 words from stack followed by
     * stmia 4 words. This seems odd!
     */
    __asm__ __volatile__ (
        "mov        r4, %0         \n\t"
        "mov        r5, %1, lsr #4      \n\t"
        "subs       r5, r5, #1\n\t"
        "mov	r0, #0x80000000         \n\t"
        "orr	r0, r0, #0x00008000     \n\t"
        "mov	r1, r0                  \n\t"
        "mov	r2, r0                  \n\t"
        "mov	r3, r0                  \n\t"
        "1:					\n\t"
        "subs       r5, r5, #1\n\t"
        "stmia      r4!, {r0, r1, r2, r3} \n\t"
        "bne        1b \n\t"
        : 
        : "r"(mem_base), "r"(len)
        : "memory", "r0", "r1", "r2", "r3", "r4", "r5", "cc" );
}