Search
lxdream.org :: lxdream/src/pvr2/yuv.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/pvr2/yuv.c
changeset 282:01e53698ff38
prev281:2019e605cb27
next284:808617ee7135
author nkeynes
date Sun Jan 14 11:43:00 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change First cut of YUV converter
file annotate diff log raw
1.1 --- a/src/pvr2/yuv.c Sun Jan 14 02:55:25 2007 +0000
1.2 +++ b/src/pvr2/yuv.c Sun Jan 14 11:43:00 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: yuv.c,v 1.1 2007-01-14 02:55:25 nkeynes Exp $
1.6 + * $Id: yuv.c,v 1.2 2007-01-14 11:43:00 nkeynes Exp $
1.7 *
1.8 * YUV420 and YUV422 decoding
1.9 *
1.10 @@ -16,46 +16,142 @@
1.11 * GNU General Public License for more details.
1.12 */
1.13 #include "dream.h"
1.14 +#include "asic.h"
1.15 +#include "pvr2/pvr2.h"
1.16
1.17 #define YUV420_BLOCK_SIZE 384
1.18 +#define YUV422_BLOCK_SIZE 512
1.19
1.20 -static inline uint16_t decode_yuv420_to_rgb565_pixel( uint8_t y, uint8_t u, uint8_t v )
1.21 +#define FORMAT_YUV420 0
1.22 +#define FORMAT_YUV422 1
1.23 +
1.24 +
1.25 +static int yuv_block_size[2] = { YUV420_BLOCK_SIZE, YUV422_BLOCK_SIZE };
1.26 +
1.27 +struct yuv_state {
1.28 + uint32_t target;
1.29 + int width;
1.30 + int height;
1.31 + int input_format;
1.32 + char data[512];
1.33 + int data_length;
1.34 + int x, y;
1.35 +} pvr2_yuv_state;
1.36 +
1.37 +/**
1.38 + * Transformation table for yuv420.
1.39 + */
1.40 +uint16_t yuv420_lut[512] = { 0, 128, 64, 129, 1, 130, 65, 131, 2, 132, 66, 133, 3, 134, 67, 135, 4, 192, 68, 193, 5, 194, 69, 195, 6, 196, 70, 197, 7, 198, 71, 199,
1.41 + 0, 136, 64, 137, 1, 138, 65, 139, 2, 140, 66, 141, 3, 142, 67, 143, 4, 200, 68, 201, 5, 202, 69, 203, 6, 204, 70, 205, 7, 206, 71, 207,
1.42 + 8, 144, 72, 145, 9, 146, 73, 147, 10, 148, 74, 149, 11, 150, 75, 151, 12, 208, 76, 209, 13, 210, 77, 211, 14, 212, 78, 213, 15, 214, 79, 215,
1.43 + 8, 152, 72, 153, 9, 154, 73, 155, 10, 156, 74, 157, 11, 158, 75, 159, 12, 216, 76, 217, 13, 218, 77, 219, 14, 220, 78, 221, 15, 222, 79, 223,
1.44 + 16, 160, 80, 161, 17, 162, 81, 163, 18, 164, 82, 165, 19, 166, 83, 167, 20, 224, 84, 225, 21, 226, 85, 227, 22, 228, 86, 229, 23, 230, 87, 231,
1.45 + 16, 168, 80, 169, 17, 170, 81, 171, 18, 172, 82, 173, 19, 174, 83, 175, 20, 232, 84, 233, 21, 234, 85, 235, 22, 236, 86, 237, 23, 238, 87, 239,
1.46 + 24, 176, 88, 177, 25, 178, 89, 179, 26, 180, 90, 181, 27, 182, 91, 183, 28, 240, 92, 241, 29, 242, 93, 243, 30, 244, 94, 245, 31, 246, 95, 247,
1.47 + 24, 184, 88, 185, 25, 186, 89, 187, 26, 188, 90, 189, 27, 190, 91, 191, 28, 248, 92, 249, 29, 250, 93, 251, 30, 252, 94, 253, 31, 254, 95, 255,
1.48 + 32, 256, 96, 257, 33, 258, 97, 259, 34, 260, 98, 261, 35, 262, 99, 263, 36, 320, 100, 321, 37, 322, 101, 323, 38, 324, 102, 325, 39, 326, 103, 327,
1.49 + 32, 264, 96, 265, 33, 266, 97, 267, 34, 268, 98, 269, 35, 270, 99, 271, 36, 328, 100, 329, 37, 330, 101, 331, 38, 332, 102, 333, 39, 334, 103, 335,
1.50 + 40, 272, 104, 273, 41, 274, 105, 275, 42, 276, 106, 277, 43, 278, 107, 279, 44, 336, 108, 337, 45, 338, 109, 339, 46, 340, 110, 341, 47, 342, 111, 343,
1.51 + 40, 280, 104, 281, 41, 282, 105, 283, 42, 284, 106, 285, 43, 286, 107, 287, 44, 344, 108, 345, 45, 346, 109, 347, 46, 348, 110, 349, 47, 350, 111, 351,
1.52 + 48, 288, 112, 289, 49, 290, 113, 291, 50, 292, 114, 293, 51, 294, 115, 295, 52, 352, 116, 353, 53, 354, 117, 355, 54, 356, 118, 357, 55, 358, 119, 359,
1.53 + 48, 296, 112, 297, 49, 298, 113, 299, 50, 300, 114, 301, 51, 302, 115, 303, 52, 360, 116, 361, 53, 362, 117, 363, 54, 364, 118, 365, 55, 366, 119, 367,
1.54 + 56, 304, 120, 305, 57, 306, 121, 307, 58, 308, 122, 309, 59, 310, 123, 311, 60, 368, 124, 369, 61, 370, 125, 371, 62, 372, 126, 373, 63, 374, 127, 375,
1.55 + 56, 312, 120, 313, 57, 314, 121, 315, 58, 316, 122, 317, 59, 318, 123, 319, 60, 376, 124, 377, 61, 378, 125, 379, 62, 380, 126, 381, 63, 382, 127, 383 };
1.56 +
1.57 +
1.58 +/**
1.59 + * Input is 8x8 U, 8x8 V, 8x8 Y00, 8x8 Y01, 8x8 Y10, 8x8 Y11, 8 bits each,
1.60 + * for a total of 384 bytes.
1.61 + * Output is UVYV = 32 bits = 2 horizontal pixels, 8x16 = 512 bytes
1.62 + */
1.63 +void pvr2_decode_yuv420( char *dest, char *src )
1.64 {
1.65 -
1.66 + int i;
1.67 + for( i=0; i<512; i++ ) {
1.68 + dest[i] = src[yuv420_lut[i]];
1.69 + }
1.70 +}
1.71 +
1.72 +void pvr2_decode_yuv422( char *dest, char *src )
1.73 +{
1.74
1.75 }
1.76
1.77 /**
1.78 - * Convert a single 16x16 yuv420 block to rgb565.
1.79 - * @param dest output memory location for this block
1.80 - * @param src start of source block
1.81 - * @param stride length of overall line in pixels (ie 16-bit words)
1.82 + * Process a single macroblock of YUV data and write it out to
1.83 + * texture vram.
1.84 */
1.85 -static void decode_yuv420_to_rgb565_block( uint16_t *dest, uint8_t *src, uint32_t stride )
1.86 +void pvr2_yuv_process_block( char *data )
1.87 {
1.88 - uint8_t *up = *src;
1.89 - uint8_t *vp = u + 64;
1.90 - uint8_t *yp = v + 64;
1.91 + char output[512];
1.92 +
1.93 + if( pvr2_yuv_state.input_format == FORMAT_YUV420 ) {
1.94 + pvr2_decode_yuv420( output, data );
1.95 + } else {
1.96 + pvr2_decode_yuv422( output, data );
1.97 + }
1.98
1.99 - for( int yb=0; yb<16; yb++ ) {
1.100 - for( int xb=0; xb<16; xb++ ) {
1.101 - uint8_t y = *yp++;
1.102 - uint8_t u = up[xb>>1 + (yb>>1)<<3];
1.103 - uint8_t v = vp[xb>>1 + (yb>>1)<<3];
1.104 - *dest++ = decode_yuv420_to_rgb565_block(y,u,v);
1.105 + uint32_t target = pvr2_yuv_state.target +
1.106 + (pvr2_yuv_state.y * pvr2_yuv_state.width * 512) +
1.107 + (pvr2_yuv_state.x * 32);
1.108 +
1.109 + pvr2_vram64_write_stride( target, output, 32, pvr2_yuv_state.width*32, 16 );
1.110 + if( ++pvr2_yuv_state.x >= pvr2_yuv_state.width ) {
1.111 + pvr2_yuv_state.x = 0;
1.112 + pvr2_yuv_state.y++;
1.113 + if( pvr2_yuv_state.y >= pvr2_yuv_state.height ) {
1.114 + asic_event( EVENT_PVR_YUV_DONE );
1.115 }
1.116 - dest = dest + stride - 16;
1.117 + }
1.118 +
1.119 + MMIO_WRITE( PVR2, YUV_COUNT, MMIO_READ( PVR2, YUV_COUNT ) + 1 );
1.120 +}
1.121 +
1.122 +/**
1.123 + * Receive data from the SH4, usually via DMA. This method is mainly responsible
1.124 + * for buffering the data into macroblock chunks and then passing it on to the
1.125 + * real processing
1.126 + */
1.127 +void pvr2_yuv_write( char *data, uint32_t length )
1.128 +{
1.129 + int block_size = yuv_block_size[pvr2_yuv_state.input_format];
1.130 +
1.131 + if( pvr2_yuv_state.data_length != 0 ) { /* Append to existing data */
1.132 + int tmp = MIN( length, block_size - pvr2_yuv_state.data_length );
1.133 + memcpy( pvr2_yuv_state.data + pvr2_yuv_state.data_length,
1.134 + data, tmp );
1.135 + pvr2_yuv_state.data_length += tmp;
1.136 + data += tmp;
1.137 + length -= tmp;
1.138 + if( pvr2_yuv_state.data_length == block_size ) {
1.139 + pvr2_yuv_process_block( pvr2_yuv_state.data );
1.140 + }
1.141 + }
1.142 +
1.143 + while( length >= block_size ) {
1.144 + pvr2_yuv_process_block( data );
1.145 + data += block_size;
1.146 + length -= block_size;
1.147 + }
1.148 +
1.149 + if( length != 0 ) { /* Save the left over data */
1.150 + memcpy( pvr2_yuv_state.data, data, length );
1.151 + pvr2_yuv_state.data_length = length;
1.152 }
1.153 }
1.154
1.155 -void decode_yuv420_to_rgb565( uint16_t *dest, uint8_t *src, int width, int height )
1.156 +void pvr2_yuv_init( uint32_t target, uint32_t config )
1.157 {
1.158 - uint16_t *p;
1.159 - for( int j=0; j<height; j++ ) {
1.160 - for( int i=0; i<width; i++ ) {
1.161 - p = dest + (j<<5)*width + i<<5;
1.162 - decode_yuv420_to_rgb565_block( p, src, width );
1.163 - src += YUV420_BLOCK_SIZE;
1.164 - }
1.165 + pvr2_yuv_state.target = target;
1.166 + pvr2_yuv_state.width = (config & 0x3f) + 1;
1.167 + pvr2_yuv_state.height = ((config>>8) & 0x3f) +1;
1.168 + pvr2_yuv_state.x = 0;
1.169 + pvr2_yuv_state.y = 0;
1.170 + pvr2_yuv_state.data_length = 0;
1.171 + pvr2_yuv_state.input_format = (config & 0x01000000) ? FORMAT_YUV420 : FORMAT_YUV422;
1.172 + if( config & 0x00010000 ) {
1.173 + pvr2_yuv_state.height *= pvr2_yuv_state.width;
1.174 + pvr2_yuv_state.width = 1;
1.175 }
1.176 + MMIO_WRITE( PVR2, YUV_COUNT, 0 );
1.177 }
.