summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/xfree86/drivers/r128/radeon_video.c
blob: 4a453abdc1798b04cc0a8c3b243a8b7e3311b7f9 (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/r128/radeon_video.c,v 1.3 2000/10/23 21:16:49 tsi Exp $ */
/**************************************************************************

Copyright 2000 Stuart R. Anderson and Metro Link, Inc.

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/
/*
 * Authors:
 *   Stuart R. Anderson <anderson@metrolink.com>
 *
 * Credits:
 *
 *   This code is derived primarily from the GATOS Project run by Stea Greene.
 *   The initial version of this code was done by Vladimir Dergacheb.
 *
 *   This code was simplified from the GATOS code primarily because I didn't
 *   have the right hardware handy to test anything beyond simple overlays,
 *   and because I wanted to complete it in a short time frame that I had
 *   available.
 *
 *   My apologies to Vladimir as there is more good work in his code that
 *   should be brought forward.
 *
 *   RADEON ALERT !
 *   This is an extremely quick port to the Radeon, it hasn't been tested
 *   thoroughly, although it appears to work.
 */
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86Resources.h"
#include "xf86_ansic.h"
#include "compiler.h"
#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "xf86fbman.h"
#include "regionstr.h"
 
#include "xf86xv.h"
#include "xf86Cursor.h"
#include "Xv.h"
#include "xaalocal.h"
#include "dixstruct.h"
#include "fourcc.h"

#include "radeon.h"
#include "radeon_reg.h"

/*
 * For Debug
#define OUTREG(addr, val)   { xf86DrvMsgVerb(pScrn->scrnIndex,X_INFO,1,"OUTREG(%s,%x)\n",#addr,val) ;MMIO_OUT32(RADEONMMIO, addr, val);}
*/

#define OFF_DELAY       250  /* milliseconds */
#define FREE_DELAY      15000
 
#define OFF_TIMER       0x01
#define FREE_TIMER      0x02
#define CLIENT_VIDEO_ON 0x04
 
#define TIMER_MASK      (OFF_TIMER | FREE_TIMER)
 
#ifndef XvExtension
void RADEONInitVideo(ScreenPtr pScreen) {}
#else 
void RADEONInitVideo(ScreenPtr);
static XF86VideoAdaptorPtr RADEONSetupImageVideo(ScreenPtr);
static int RADEONSetPortAttribute(ScrnInfoPtr, Atom, INT32, pointer);
static int RADEONGetPortAttribute(ScrnInfoPtr, Atom ,INT32 *, pointer);

static void RADEONStopVideo(ScrnInfoPtr, pointer, Bool);
static void RADEONQueryBestSize(ScrnInfoPtr, Bool,
        short, short, short, short, unsigned int *, unsigned int *, pointer);
static int RADEONPutImage( ScrnInfoPtr,
        short, short, short, short, short, short, short, short,
        int, unsigned char*, short, short, Bool, RegionPtr, pointer);
static int RADEONQueryImageAttributes(ScrnInfoPtr,
        int, unsigned short *, unsigned short *,  int *, int *);

#define MAKE_ATOM(a) MakeAtom(a, sizeof(a) - 1, TRUE)

#define IMAGE_MAX_WIDTH         2048
#define IMAGE_MAX_HEIGHT        2048
#define Y_BUF_SIZE              (IMAGE_MAX_WIDTH * IMAGE_MAX_HEIGHT) 

static Atom xvColorKey;

typedef struct {
	int		videoStatus;
	unsigned char	brightness;
	unsigned char	contrast;

	RegionRec	clip;
	CARD32		colorKey;
	CARD8		overlay_pixel_size;
	CARD8		current_buffer;
	int		overlay_pad;
	CARD32		overlay_id;
	CARD32		overlay_width;

	CARD32		scale_cntl;
	CARD32		video_format;
	FBLinearPtr	linear;
	} RADEONPortPrivRec, *RADEONPortPrivPtr;

void RADEONInitVideo(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    XF86VideoAdaptorPtr *adaptors, *newAdaptors = NULL;
    XF86VideoAdaptorPtr newAdaptor = NULL;
    int num_adaptors;

    RADEONTRACE(("RADEONInitVideo called\n"));

    /* Determine if the card supports this */
    if (pScrn->bitsPerPixel != 8)
    {
	newAdaptor = RADEONSetupImageVideo(pScreen);
    }
    
    num_adaptors = xf86XVListGenericAdaptors(pScrn, &adaptors);

    if(newAdaptor) {
	if(!num_adaptors) {
	    num_adaptors = 1;
	    adaptors = &newAdaptor;
	} else {
	    newAdaptors =  /* need to free this someplace */
		xalloc((num_adaptors + 1) * sizeof(XF86VideoAdaptorPtr*));
	    if(newAdaptors) {
		memcpy(newAdaptors, adaptors, num_adaptors * 
					sizeof(XF86VideoAdaptorPtr));
		newAdaptors[num_adaptors] = newAdaptor;
		adaptors = newAdaptors;
		num_adaptors++;
	    }
	}
    }

    if(num_adaptors)
	xf86XVScreenInit(pScreen, adaptors, num_adaptors);

    if(newAdaptors)
	xfree(newAdaptors);
}

/* client libraries expect an encoding */
static XF86VideoEncodingRec DummyEncoding[1] =
{
 {
   0,
   "XV_IMAGE",
   IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT,
   {1, 1}
 }
};

#define NUM_FORMATS 3

static XF86VideoFormatRec Formats[NUM_FORMATS] = 
{
  {15, TrueColor},
  {16, TrueColor},
  {32, TrueColor}
};

#define NUM_ATTRIBUTES 1

static XF86AttributeRec Attributes[NUM_ATTRIBUTES] =
{
   {XvSettable | XvGettable, 0, (1<<24)-1, "XV_COLORKEY"},
};

#define NUM_IMAGES 3

static XF86ImageRec Images[NUM_IMAGES] =
{
	XVIMAGE_YUY2,
	XVIMAGE_YV12,
	XVIMAGE_UYVY
};

static void
RADEONResetVideo(ScrnInfoPtr pScrn)
{
    RADEONInfoPtr   info      = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONPortPrivPtr pPriv = info->adaptor->pPortPrivates[0].ptr;

    RADEONTRACE(("RADEONResetVideo called\n"));

    /* Initialize some of the HW here */
    OUTREG(RADEON_OV0_EXCLUSIVE_HORZ,0); /* disable exclusive mode */
    OUTREG(RADEON_OV0_VIDEO_KEY_MSK,0xffff);
    OUTREG(RADEON_OV0_KEY_CNTL, RADEON_GRAPHIC_KEY_FN_NE);
    OUTREG(RADEON_OV0_GRAPHICS_KEY_CLR,pPriv->colorKey);
    /* Only using one buffer for now
    OUTREG(RADEON_OV0_AUTO_FLIP_CNTL,pAPriv->Port[0].auto_flip_cntl);
    */
    switch(pScrn->depth){
    case 8:
	OUTREG(RADEON_OV0_GRAPHICS_KEY_MSK,0xff);
	break;
    case 15:
	OUTREG(RADEON_OV0_GRAPHICS_KEY_MSK,0x7fff);
	break;
    case 16:
	OUTREG(RADEON_OV0_GRAPHICS_KEY_MSK,0xffff);
	break;
    case 24:
	OUTREG(RADEON_OV0_GRAPHICS_KEY_MSK,0xffffff);
	break;
    case 32:
	OUTREG(RADEON_OV0_GRAPHICS_KEY_MSK,0xffffffff);
	break;
    }

    OUTREG(RADEON_OV0_REG_LOAD_CNTL,0x0);
    OUTREG(RADEON_OV0_DEINTERLACE_PATTERN,0xAAAAA);
    OUTREG(RADEON_OV0_P1_V_ACCUM_INIT,(2<<20)|1);
    OUTREG(RADEON_OV0_P23_V_ACCUM_INIT,(2<<20)|1);
    OUTREG(RADEON_OV0_P1_H_ACCUM_INIT,(3<<28));
    OUTREG(RADEON_OV0_P23_H_ACCUM_INIT,(2<<28));
    OUTREG(RADEON_OV0_STEP_BY,1|(1<<8));
    OUTREG(RADEON_OV0_FILTER_CNTL,0xf); /* use hardcoded coeff's */
    OUTREG(RADEON_OV0_FILTER_CNTL,0x0); /* use programmable coeff's */
    OUTREG(RADEON_OV0_FOUR_TAP_COEF_0   , 0x00002000);
    OUTREG(RADEON_OV0_FOUR_TAP_COEF_1   , 0x0D06200D);
    OUTREG(RADEON_OV0_FOUR_TAP_COEF_2   , 0x0D0A1C0D);
    OUTREG(RADEON_OV0_FOUR_TAP_COEF_3   , 0x0C0E1A0C);
    OUTREG(RADEON_OV0_FOUR_TAP_COEF_4   , 0x0C14140C);
    OUTREG(RADEON_OV0_COLOUR_CNTL,(1<<12)|(1<<20));
    OUTREG(RADEON_OV0_TEST,0);
    OUTREG(RADEON_OV0_SCALE_CNTL,pPriv->scale_cntl|pPriv->video_format);
    OUTREG(RADEON_CAP0_TRIG_CNTL,0);
}

static XF86VideoAdaptorPtr
RADEONSetupImageVideo(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    RADEONInfoPtr info = RADEONPTR(pScrn);
    XF86VideoAdaptorPtr adapt;
    RADEONPortPrivPtr pPriv;

    RADEONTRACE(("RADEONSetupImageVideo called\n"));
 
    if(!(adapt = xcalloc(1, sizeof(XF86VideoAdaptorRec) +
                            sizeof(RADEONPortPrivRec) +
                            sizeof(DevUnion))))
        return NULL;

    adapt->type = XvWindowMask | XvInputMask | XvImageMask;
    adapt->flags = VIDEO_OVERLAID_IMAGES | VIDEO_CLIP_TO_VIEWPORT;
    adapt->name = "RADEON Video Overlay";
    adapt->nEncodings = 1;
    adapt->pEncodings = DummyEncoding;
    adapt->nFormats = NUM_FORMATS;
    adapt->pFormats = Formats;
    adapt->nPorts = 1;
    adapt->pPortPrivates = (DevUnion*)(&adapt[1]);

    pPriv = (RADEONPortPrivPtr)(&adapt->pPortPrivates[1]);

    adapt->pPortPrivates[0].ptr = (pointer)(pPriv);
    adapt->pAttributes = Attributes;
    adapt->nImages = NUM_IMAGES;
    adapt->nAttributes = NUM_ATTRIBUTES;
    adapt->pImages = Images;
    adapt->PutVideo = NULL;
    adapt->PutStill = NULL;
    adapt->GetVideo = NULL;
    adapt->GetStill = NULL;
    adapt->StopVideo = RADEONStopVideo;
    adapt->SetPortAttribute = RADEONSetPortAttribute;
    adapt->GetPortAttribute = RADEONGetPortAttribute;
    adapt->QueryBestSize = RADEONQueryBestSize;
    adapt->PutImage = RADEONPutImage;
    adapt->QueryImageAttributes = RADEONQueryImageAttributes;

    /* gotta uninit this someplace */
    REGION_INIT(pScreen, &pPriv->clip, NullBox, 0);
 
    info->adaptor = adapt;

    pPriv->colorKey = 0x01; /* a touch of blue */
    pPriv->video_format = RADEON_SCALER_SOURCE_VYUY422;
    pPriv->scale_cntl = RADEON_SCALER_PRG_LOAD_START|RADEON_SCALER_DOUBLE_BUFFER;
    pPriv->scale_cntl|= RADEON_SCALER_SMART_SWITCH|RADEON_SCALER_PIX_EXPAND;
    pPriv->scale_cntl|= RADEON_SCALER_SMART_SWITCH;

    xvColorKey   = MAKE_ATOM("XV_COLORKEY");

    RADEONResetVideo(pScrn);

    return adapt;
}

static Bool
RegionsEqual(RegionPtr A, RegionPtr B)
{
    int *dataA, *dataB;
    int num;

    num = REGION_NUM_RECTS(A);
    if(num != REGION_NUM_RECTS(B))
	return FALSE;

    if((A->extents.x1 != B->extents.x1) ||
       (A->extents.x2 != B->extents.x2) ||
       (A->extents.y1 != B->extents.y1) ||
       (A->extents.y2 != B->extents.y2))
	return FALSE;

    dataA = (int*)REGION_RECTS(A);
    dataB = (int*)REGION_RECTS(B);

    while(num--) {
	if((dataA[0] != dataB[0]) || (dataA[1] != dataB[1]))
	   return FALSE;
	dataA += 2; 
	dataB += 2;
    }

    return TRUE;
}

static void
RADEONStopVideo(ScrnInfoPtr pScrn, pointer data, Bool exit)
{
    RADEONInfoPtr   info      = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONPortPrivPtr pPriv = (RADEONPortPrivPtr) data;

    RADEONTRACE(("RADEONStopVideo called\n"));

    REGION_EMPTY(pScrn->pScreen, &pPriv->clip);

    if(exit) {
	if(pPriv->videoStatus & CLIENT_VIDEO_ON) {
	    OUTREG(RADEON_OV0_SCALE_CNTL,pPriv->scale_cntl|pPriv->video_format);
	}
	if(pPriv->linear) {
            xf86FreeOffscreenLinear(pPriv->linear);
            pPriv->linear = NULL;
         }
     pPriv->videoStatus = 0;
  } else {
     if(pPriv->videoStatus & CLIENT_VIDEO_ON) {
     }
  }
}

static int
RADEONSetPortAttribute(
  ScrnInfoPtr pScrn,
  Atom attribute,
  INT32 value,
  pointer data
)
{
    RADEONInfoPtr   info      = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONPortPrivPtr pPriv = (RADEONPortPrivPtr) data;

    RADEONTRACE(("RADEONSetPortAttribute called\n"));

    if (attribute == xvColorKey) {
	pPriv->colorKey = value;
        OUTREG(RADEON_OV0_GRAPHICS_KEY_CLR,pPriv->colorKey);
	RADEONTRACE(("Setting ColorKey to %d\n", pPriv->colorKey));
	return Success;
    } 

    return Success;
}

static int
RADEONGetPortAttribute(
  ScrnInfoPtr pScrn,
  Atom attribute,
  INT32 *value,
  pointer data
)
{
    RADEONPortPrivPtr pPriv = (RADEONPortPrivPtr) data;

    RADEONTRACE(("RADEONGetPortAttribute called\n"));

    if (attribute == xvColorKey) {
	RADEONTRACE(("Getting ColorKey %d\n", pPriv->colorKey));
	*value = pPriv->colorKey;
	return Success;
    } 

    return Success;
}

static void
RADEONQueryBestSize(
  ScrnInfoPtr pScrn,
  Bool motion,
  short vid_w, short vid_h,
  short drw_w, short drw_h,
  unsigned int *p_w, unsigned int *p_h,
  pointer data
)
{
   RADEONTRACE(("RADEONQueryBestSize called\n"));
  *p_w = drw_w;
  *p_h = drw_h;
} 

static void
RADEONDisplayVideo(
    ScrnInfoPtr pScrn,
    int id,
    short width, short height,
    int dstPitch,  /* of chroma for 4:2:0 */
    int x1, int y1, int x2, int y2,
    BoxPtr dstBox,
    short src_w, short src_h,
    short drw_w, short drw_h,
    int fboffset
)
{
    RADEONInfoPtr   info      = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONPortPrivPtr pPriv = info->adaptor->pPortPrivates[0].ptr;
    int step_by, vert_inc, horz_inc;

    RADEONTRACE(("RADEONDisplayVideo called\n"));

    /* calculate step_by factor */
    step_by=src_w/(drw_w*2);
    switch(step_by){
	case 0:
	    OUTREG(RADEON_OV0_STEP_BY,0x101);
	    step_by=1;
	    break;
	case 1:
	    OUTREG(RADEON_OV0_STEP_BY,0x202);
	    step_by=2;
	    break;
	case 2:
	case 3:
	    OUTREG(RADEON_OV0_STEP_BY,0x303);
	    step_by=4;
	    break;
	default:
	    OUTREG(RADEON_OV0_STEP_BY,0x404);
	    step_by=8;
	    break;
    }

    vert_inc=(src_h<<12)/(drw_h);
    horz_inc=(src_w<<12)/(drw_w*step_by);

    OUTREG(RADEON_OV0_Y_X_START,((x1))|(y1<<16)|(1<<31));
    OUTREG(RADEON_OV0_Y_X_END,((x2))|((y2)<<16));
    OUTREG(RADEON_OV0_H_INC,(horz_inc)|((horz_inc<<15)));
    OUTREG(RADEON_OV0_V_INC,(vert_inc<<8));
    OUTREG(RADEON_OV0_P1_BLANK_LINES_AT_TOP,0xfff|((src_h-1)<<16));
    OUTREG(RADEON_OV0_P23_BLANK_LINES_AT_TOP,0xfff|((src_h-1)<<16));
    OUTREG(RADEON_OV0_VID_BUF_PITCH0_VALUE,width<<1);
    OUTREG(RADEON_OV0_VID_BUF_PITCH1_VALUE,width<<1);
    OUTREG(RADEON_OV0_P1_X_START_END,(src_w-1)|((x1&0xf)<<16));
    OUTREG(RADEON_OV0_P2_X_START_END,(src_w-1)|((x1&0xf)<<16));
    OUTREG(RADEON_OV0_P3_X_START_END,(src_w-1)|((x1&0xf)<<16));

    OUTREG(RADEON_OV0_VID_BUF0_BASE_ADRS,(fboffset)&(~0xf));
    OUTREG(RADEON_OV0_VID_BUF1_BASE_ADRS,(fboffset)&(~0xf));
    OUTREG(RADEON_OV0_VID_BUF2_BASE_ADRS,(fboffset)&(~0xf));

#if 0
    /* Enable this when double buffering is implemented */
    OUTREG(RADEON_OV0_VID_BUF3_BASE_ADRS,(fboffset2)&(~0xf));
    OUTREG(RADEON_OV0_VID_BUF4_BASE_ADRS,(fboffset2)&(~0xf));
    OUTREG(RADEON_OV0_VID_BUF5_BASE_ADRS,(fboffset2)&(~0xf));
#endif

    OUTREG(RADEON_OV0_SCALE_CNTL,pPriv->scale_cntl|RADEON_SCALER_ENABLE|pPriv->video_format);
}

static void
RADEONCopyData(
  unsigned char *src,
  unsigned char *dst,
  int srcPitch,
  int dstPitch,
  int h,
  int w
  )
{
    w <<= 1;
    while(h--) {
        memcpy(dst, src, w);
        src += srcPitch;
        dst += dstPitch;
    }
}

static void
RADEONCopyMungedData(
   unsigned char *src1,
   unsigned char *src2,
   unsigned char *src3,
   unsigned char *dst1,
   int srcPitch,
   int srcPitch2,
   int dstPitch,
   int h,
   int w
   )
{
   CARD32 *dst = (CARD32*)dst1;
   int i, j;

   dstPitch >>= 2;
   w >>= 1;

   for(j = 0; j < h; j++) {
        for(i = 0; i < w; i++) {
            dst[i] = src1[i << 1] | (src1[(i << 1) + 1] << 16) |
                     (src3[i] << 8) | (src2[i] << 24);
        }
        dst += dstPitch;
        src1 += srcPitch;
        if(j & 1) {
            src2 += srcPitch2;
            src3 += srcPitch2;
        }
   }
}

static FBLinearPtr
RADEONAllocateMemory(
  ScrnInfoPtr pScrn,
  FBLinearPtr linear,
  int size
)
{
   ScreenPtr pScreen;
   FBLinearPtr new_linear;

   RADEONTRACE(("RADEONAllocateMemory(%x,%d) called\n",linear,size));

   if(linear) {
	if(linear->size >= size)
	   return linear;

	if(xf86ResizeOffscreenLinear(linear, size))
	   return linear;

	xf86FreeOffscreenLinear(linear);
   }

   pScreen = screenInfo.screens[pScrn->scrnIndex];

   new_linear = xf86AllocateOffscreenLinear(pScreen, size, 4,
                                            NULL, NULL, NULL);

   if(!new_linear) {
        int max_size;

        xf86QueryLargestOffscreenLinear(pScreen, &max_size, 4, 
				       PRIORITY_EXTREME);

        if(max_size < size) return NULL;

        xf86PurgeUnlockedOffscreenAreas(pScreen);
        new_linear = xf86AllocateOffscreenLinear(pScreen, size, 4, 
                                                 NULL, NULL, NULL);
   } 

   RADEONTRACE(("returning %x(%x)\n",new_linear,new_linear->offset));

   return new_linear;
}

static int
RADEONPutImage(
  ScrnInfoPtr pScrn,
  short src_x, short src_y,
  short drw_x, short drw_y,
  short src_w, short src_h,
  short drw_w, short drw_h,
  int id, unsigned char* buf,
  short width, short height,
  Bool sync,
  RegionPtr clipBoxes, pointer data
)
{
    RADEONInfoPtr   info      = RADEONPTR(pScrn);
    unsigned char *RADEONMMIO = info->MMIO;
    RADEONPortPrivPtr pPriv = (RADEONPortPrivPtr)data;
    INT32 x1, x2, y1, y2;
    INT32 d_x,d_y,d_width,d_height;
    int srcPitch = 0, srcPitch2 = 0;
    int dstPitch = 0;
    int offset,offset2 = 0,offset3 = 0,fboffset;
    int top, left, npixels, nlines, size;
    BoxRec dstBox;
    CARD32 video_format;

    RADEONTRACE(("RADEONPutImage called\n"));

    switch(id) {
    case FOURCC_YV12:
    case FOURCC_UYVY:
	video_format=RADEON_SCALER_SOURCE_VYUY422;
	break;
    case FOURCC_YUY2:
	video_format=RADEON_SCALER_SOURCE_YVYU422;
	break;
    default:
	return BadValue;
    }

   /* Clip */
    d_x=drw_x;
    d_y=drw_y;
    d_width=drw_w;
    d_height=drw_h;
    if(drw_x<0){
            drw_w+=drw_x;
            drw_x=0;
            }
    if(drw_y<0){
            drw_h+=drw_y;
            drw_y=0;
            }
    if(drw_x+drw_w>pScrn->pScreen->width){
            drw_w=pScrn->pScreen->width-drw_x;
            }
    if(drw_y+drw_h>pScrn->pScreen->height){
            drw_h=pScrn->pScreen->height-drw_y;
            }
    if((drw_w<=0)||(drw_h<=0)){
            /* this should not happen,
                    since we are outside of visible screen,
                     but just in case */
            return Success;
            }

    x1 = src_x;
    x2 = src_x + src_w;
    y1 = src_y;
    y2 = src_y + src_h;

    dstPitch = width*info->CurrentLayout.pixel_bytes;
    srcPitch=width;

    switch(id) {
    case FOURCC_YV12:
         size =  width * height * 2; /* 16bpp */
         break;
    case FOURCC_UYVY:
    case FOURCC_YUY2:
    default:
         size = width * height * 1.5;
         break;
    }

    if(!(pPriv->linear = RADEONAllocateMemory(pScrn, pPriv->linear,size)))
        return BadAlloc;

    /* copy data */
    top = y1 >> 16;
    left = (x1 >> 16) & ~1;
    npixels = ((((x2 + 0xffff) >> 16) + 1) & ~1) - left;

    switch(id) {
    case FOURCC_YV12:
        srcPitch = (width + 3) & ~3;
	offset2 = srcPitch * height;
	srcPitch2 = ((width >> 1) + 3) & ~3;
	offset3 = (srcPitch2 * (height >> 1)) + offset2;
	nlines = ((((y2 + 0xffff) >> 16) + 1) & ~1) - top;
        break;
    case FOURCC_UYVY:
    case FOURCC_YUY2:
    default:
	buf += (top * srcPitch) + left;
	nlines = ((y2 + 0xffff) >> 16) - top;
	srcPitch = (width << 1);
        break;
    }
    nlines=src_h;
    npixels=src_w;
    if(npixels>width) npixels=width;
    if(nlines>height) nlines=height;

    /* adjust source rectangle */
    src_x+=((drw_x-d_x)*src_w)/d_width;
    src_y+=((drw_y-d_y)*src_h)/d_height;

    src_w=(src_w * drw_w)/d_width;
    src_h=(src_h * drw_h)/d_height;

    offset=(src_x+src_y*width)*info->CurrentLayout.pixel_bytes;
    fboffset=pPriv->linear->offset*info->CurrentLayout.pixel_bytes;

    if(!(INREG(RADEON_CRTC_STATUS)&2)){
	xf86DrvMsg(pScrn->scrnIndex,X_INFO,"too fast");
	return Success;
	}

    RADEONDisplayVideo(pScrn, id, width, height, dstPitch,
            drw_x, drw_y, drw_x+drw_w, drw_y+drw_h,
	    &dstBox, src_w, src_h, drw_w, drw_h,fboffset);

    /* update cliplist */
    if(!RegionsEqual(&pPriv->clip, clipBoxes)) {
        REGION_COPY(pScreen, &pPriv->clip, clipBoxes);
        /* draw these */
        XAAFillSolidRects(pScrn, pPriv->colorKey, GXcopy, ~0,
                                        REGION_NUM_RECTS(clipBoxes),
                                        REGION_RECTS(clipBoxes));
    }


    switch(id) {
    case FOURCC_YV12:
	RADEONCopyMungedData(buf + (top * srcPitch) + (left >> 1),
	    buf + offset2, buf + offset3, info->FB+fboffset, 
	    srcPitch, srcPitch2, dstPitch, nlines, npixels);
        break;
    case FOURCC_UYVY:
    case FOURCC_YUY2:
    default:
	RADEONCopyData(buf,info->FB+fboffset,srcPitch,dstPitch,
	    nlines,npixels);
         break;
    }

    pPriv->videoStatus = CLIENT_VIDEO_ON;
    return Success;
}

static int
RADEONQueryImageAttributes(
  ScrnInfoPtr pScrn,
  int id,
  unsigned short *w, unsigned short *h,
  int *pitches, int *offsets
)
{
    int size, tmp;
    RADEONTRACE(("RADEONQueryImageAtrributes called\n"));
 
    if(*w > IMAGE_MAX_WIDTH) *w = IMAGE_MAX_WIDTH;
    if(*h > IMAGE_MAX_HEIGHT) *h = IMAGE_MAX_HEIGHT;
 
    *w = (*w + 1) & ~1;
    if(offsets) offsets[0] = 0;
 
    switch(id) {
    case FOURCC_YV12:
        *h = (*h + 1) & ~1;
        size = (*w + 3) & ~3;
        if(pitches) pitches[0] = size;
        size *= *h;
        if(offsets) offsets[1] = size;
        tmp = ((*w >> 1) + 3) & ~3;
        if(pitches) pitches[1] = pitches[2] = tmp;
        tmp *= (*h >> 1);
        size += tmp;
        if(offsets) offsets[2] = size;
        size += tmp;
        break;
    case FOURCC_UYVY:
    case FOURCC_YUY2:
    default:
        size = *w << 1;
        if(pitches) pitches[0] = size;
        size *= *h;
        break;
    } 

    return size;
}
#endif