xref: /utopia/UTPA2-700.0.x/modules/mfe/api/mfe/yuvgen.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2006-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // (�uMStar Confidential Information�v) by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 #include "mdrv_mfe_io.h"
96 #include "MFE_chip.h"
97 //#if defined(_MIPS_PLATFORM_) && defined(_MFE_T8_)
98 //#include <linux/string.h>
99 //#else
100 #include <stdio.h>
101 #include <string.h>
102 #include <sys/types.h>
103 #include "madp_ms_dprintf.h"
104 
105 #if defined(_MFE_BIG2_)&&defined(_MIPS_PLATFORM_)
106 #include "shellcfg.h"
107 #define printf diag_printf
108 #endif
109 #define U8 unsigned char
110 #define U32 unsigned long
111 
112 unsigned char *YUV = 0;    // yuv buffer start point
113 unsigned char *TILE = 0;    // yuv buffer start point
114 
115 void copy8pixel(unsigned char *p);
116 void YuvGen(unsigned char seed, int width, int height, volatile unsigned char *buff);
117 
118 #ifdef _MAIN_
119 
120 #define MFE_DEBUG //x
121 
122 FILE *fp;
123 void (*do_copy)(unsigned char *) ;
124 static unsigned char *ptrInTileFormat;
125 int debug = 0;
126 
127 int W = 720;
128 int H = 480;
129 
130 
131 #define YUV_LEN 50
132 
main(int argc,char * argv[])133 main(int argc, char *argv[])
134 {
135 
136 #if 0
137 	int i;
138 	YUV = (unsigned char *) malloc( W * H * 1.5 );
139 	TILE = (unsigned char *) malloc( W * H * 1.5 );
140 
141 	fp = fopen(argv[1], "w");
142 	if( fp == NULL || YUV == NULL || TILE == NULL ) exit(-1);
143 
144 	do_copy = copy8pixel;
145 
146 	for( i = 0 ; i < YUV_LEN ; i ++ ) {
147 		ptrInTileFormat = TILE;
148 		YuvGen( 100 +i*5, W, H, TILE );
149 		ConvertYuvFromTile(W,H,0, YUV);
150 		ConvertYuvFromTile(W,H,1, YUV);
151 		fwrite(YUV, W * H * 1.5 , 1 , fp);
152 	}
153 
154 	fclose(fp);
155 	free(YUV);
156 	free(TILE);
157 #else
158 	int i;
159 	FILE *fpr= NULL;
160 	YUV = (unsigned char *) malloc( W * H * 1.5 );
161 	TILE = (unsigned char *) malloc( W * H * 1.5 );
162 	fp = fopen(argv[1], "wb");
163 	fpr = fopen(argv[2], "rb");
164 	if( fpr == NULL || fp == NULL || YUV == NULL || TILE == NULL ) exit(-1);
165 
166 	do_copy = copy8pixel;
167 
168 	while( fread(TILE, W * H * 1.5, 1, fpr) > 0 ) {
169 		ptrInTileFormat = TILE;
170 		ConvertYuvFromTile(W,H,0, YUV);
171 		ConvertYuvFromTile(W,H,1, YUV);
172 		fwrite(YUV, W * H * 1.5 , 1 , fp);
173 
174 	}
175 
176 #endif
177 }
178 
179 
180 
181 
182 
copy8pixel(unsigned char * p)183 void copy8pixel(unsigned char *p)
184 {
185 	int i ;
186 	for(i = 0; i < 8; i ++ ) {
187 		*p++ = *ptrInTileFormat++;
188 	}
189 }
190 
191 
copy_row(unsigned char * p)192 void copy_row(unsigned char *p)
193 {
194 	unsigned char buff[512];
195 
196 	unsigned char tmpbuf[9];
197 
198 	int i,j;
199 
200 	if( fgets(buff,512,fp) != NULL )
201 	{
202 
203 		for( j = 0; j < 8 ; j ++ ) {
204 			unsigned char tmp = 0;
205 			for( i = 0; i < 8 ; i ++ ) {
206 				// unsigned char k = buff[j*8+i] - '0';
207 				unsigned char k = buff[(7-j)*8+i] - '0';
208 				if( k != 0 && k != 1 )  {
209 					printf("Err inupt\n");
210 					exit(-3);
211 				}
212 				tmp <<= 1;
213 				tmp |= k;
214 			}
215 			p[j] = tmp;
216 
217 			if( debug ) fprintf(stderr,"%4d",p[j]);
218 		}
219 		if( debug ) fprintf(stderr,"\r\n");
220 	}
221 	else {
222 		printf("read failure\n");
223 		exit(-4);
224 	}
225 }
226 
227 
228 // copy_row2 is used to verify the binary format
copy_row2(unsigned char * p)229 void copy_row2(unsigned char *p)
230 {
231 	if( fread(p, 8,1,fp) == 0 )  {
232 		printf("read error in fread\n");
233 		return ;
234 	}
235 }
236 
237 #endif
238 
ConvertYuvFromTile(int width,int height,int mode,unsigned char * in_yuv,unsigned char * out_yuv)239 void ConvertYuvFromTile(int width, int height, int mode, unsigned char *in_yuv, unsigned char *out_yuv)
240 {
241 
242 	int mbx, mby;
243 	int image_x, image_x2;
244 	int MB_in_width, MB_in_height;
245 	int k,l,lumstart,chrstart;
246 	unsigned char *y_point, *u_point, *v_point;
247 	unsigned char *y_point2;
248 	unsigned char *blk_point;
249 	int blk, row;
250 
251 	image_x = width;
252 
253 	MB_in_width  = width >> 4 ;
254 	MB_in_height = height >> 4 ;
255 
256 	if( mode == 0 ) {  // Y component
257 		for (mby = 0; mby < MB_in_height; mby++)
258 		{
259 			for (mbx = 0; mbx < MB_in_width; mbx++)
260 			{
261 				k = mby * image_x << 4 /* * MB_SIZE*/;
262 				l = mbx << 4/** MB_SIZE*/;
263 				lumstart =k+l;
264 
265 				y_point = out_yuv + lumstart;
266 				y_point2 = y_point + (image_x << 3) /** 8 */;
267 				for (blk = 0; blk < 4; blk++)
268 				{
269 					blk_point = (blk>>1)? y_point2 : y_point;
270 					blk_point += (blk&1)? 8/*B_SIZE*/ : 0;
271 
272 					for (row = 0; row < 8; row++)
273 					{
274 						// fwrite(blk_point, 8, 1, fp);
275 						//do_copy(blk_point); /* 8 points  */
276 						memcpy(blk_point, in_yuv, 8);
277 //                        ms_dprintk(DRV_L4,"0x%x 0x%x %d %d %d %d %d %d %d %d\n", (U32)blk_point, (U32)in_yuv, in_yuv[0], in_yuv[1]
278 //                            , in_yuv[2], in_yuv[3], blk_point[0], blk_point[1], blk_point[2], blk_point[3]);
279 						blk_point += image_x;
280                         in_yuv += 8;
281 					}
282 				}
283 			}
284 		}
285 	}
286 	else { /* U & V */
287 		image_x2 = image_x >> 1 ;
288 		for (mby = 0; mby < MB_in_height; mby++)
289 		{
290 			for (mbx = 0; mbx < MB_in_width; mbx++)
291 			{
292 				k= mby * image_x << 4 /* * MB_SIZE*/;
293 				l= mbx << 4   /** MB_SIZE*/;
294 				chrstart =(k>>2)+(l>>1);
295 
296 				u_point = out_yuv + width * height + chrstart;
297 				v_point = out_yuv + width * height + ( width >> 1 ) * ( height >> 1 )  + chrstart;
298 
299 				for (blk = 0; blk < 2; blk++) // U block then V block..
300 				{
301 					blk_point = ( blk == 0 ) ? u_point : v_point;
302 
303 					for (row = 0; row < 8; row++)
304 					{
305 						//for (col = 0; col < 8; col++)
306 						//{
307 							//mem_uv_point[col] = blk_point[col];
308 						//}
309 //						do_copy(blk_point);
310 						memcpy(blk_point, in_yuv, 8);
311 						blk_point += image_x2;
312                         in_yuv += 8;
313 					}
314 				}
315 			}
316 		}
317 	}
318 }
319 
320 
321 #if 0
322 main(int argc, char *argv[])
323 {
324 	FILE *fpout = NULL;
325 	int width;
326 	int height;
327 
328 	char buff[512];
329 	unsigned num[8];
330 
331 	int i = 0;
332 	int j = 0;
333 	int m = 0;
334 
335 	if( argc < 3 )  {
336 		printf("%s width height input_y input_c out.yuv\n",argv[0]);
337 		exit(-1);
338 	}
339 	width  = atoi(argv[1]);
340 	height = atoi(argv[2]);
341 
342 	fp = fopen(argv[3], "r");
343 	if( fp == NULL) exit(-1);
344 
345 	fpout = fopen(argv[5], "w");
346 	if( fpout == NULL) exit(-1);
347 
348 	yuv = ( unsigned char *) malloc( width * height * 1.5 ) ;
349 	if( yuv == NULL ) {
350 		printf("malloc fail");
351 		exit(-5);
352 	}
353 
354 	// do_copy = copy_row2;
355 	do_copy = copy_row;
356 
357     gogo(width,height,0);
358 	fclose(fp);
359 
360 	debug = 1;
361 	fopen( argv[4] , "rb");
362 	if( fp == NULL) {ms_dprintk(DRV_L1,"open %s failure\n",argv[4]); exit(-1); }
363     gogo(width,height,1);
364 	fclose(fp);
365 
366 	fwrite(yuv, width * height * 1.5 , 1 , fpout);
367 
368 	fclose(fpout);
369 	free(yuv);
370 }
371 #endif
372 
YuvGen(unsigned char seed,int width,int height,volatile unsigned char * buff)373 void YuvGen(unsigned char seed, int width, int height, volatile unsigned char *buff)
374 {
375 	int i, j,k,m,n;
376 
377 	int inc = 5 ;
378 	int MB_width = width >> 4;
379 	int MB_height = height >> 4 ;
380 
381 	ms_dprintf(ADP_L1,"YuvGen begin: MB_width: %d MB_height: %d seed:%d\n",MB_width, MB_height,seed);
382 
383     // Y component
384 
385 	for( i = 0; i < MB_height ; i ++ ) {
386 		for( j = 0; j < MB_width ; j ++ ) {
387 			for( m = 0; m < 4 ; m ++ ) {  // 4 block
388                 for( k = 0; k < 8 ; k ++ ) {
389                     for( n = 0; n < 8; n ++ )
390                         *buff++ = seed;
391                 }
392             }
393             if( inc ) {
394                 seed += inc ;
395                 if( seed >= 255 ) seed = 0;
396 			}
397 //			printf("buff addr: 0x%x block[%2d][%2d][%d]\n",(U32)buff,i,j,m);
398 		}
399 	}
400     //MFE_DEBUG(printf("UV component begin\n"));
401     // U V component
402     height <<= 1 ;
403     width <<= 1 ;
404 
405 	for( i = 0; i < MB_height ; i ++ ) {
406 		for( j = 0; j < MB_width ; j ++ ) {
407 			for( k = 0; k < 8 ; k ++ ) {    // U
408                 for( n = 0; n < 8; n ++ ) {
409                     //*buff++ = seed;
410                     *buff = seed;
411                     buff++;
412                 }
413 			}
414 			for( k = 0; k < 8 ; k ++ ) {    // V
415                 for( n = 0; n < 8; n ++ ) {
416                     //*buff++ = seed;
417                     *buff = seed;
418                     buff++;
419                 }
420 			}
421 			if( inc ) {
422 				seed += inc ;
423 				if( seed >= 255 ) seed = 0;
424 			}
425 		}
426 	}
427     printf("YuvGen Done. seed:%d\n",seed);
428 }
429 
430 
yuv2tile(unsigned char * output,int width,int height,unsigned char * buff,int Ypart)431 void yuv2tile(/*FILE *fp*/unsigned char *output, int width, int height, unsigned char *buff , int Ypart)
432 {
433     int mbx, mby;
434     int image_x, image_x2;
435     int MB_in_width, MB_in_height;
436     int k,l,lumstart,chrstart;
437     U8 *y_point, *u_point, *v_point;
438     U8 *y_point2;
439     U8 *blk_point;
440     volatile unsigned char *tmp_out=output;
441     int blk, row;
442 
443 
444     if (buff != 0)
445     {
446         image_x = width;
447         MB_in_width = image_x >>4/*/ MB_SIZE*/;
448         MB_in_height = height >> 4;
449 
450         if (Ypart == 1)
451         {
452             // dump Y plane first
453             for (mby = 0; mby < MB_in_height; mby++)
454             {
455                 for (mbx = 0; mbx < MB_in_width; mbx++)
456                 {
457                     k = mby * image_x << 4 /* * MB_SIZE*/;
458                     l = mbx << 4/** MB_SIZE*/;
459                     lumstart =k+l;
460 
461                     y_point = buff + lumstart;
462                     y_point2 = y_point + (image_x << 3) /** 8 */;
463 
464                     for (blk = 0; blk < 4; blk++)
465                     {
466                         blk_point = (blk>>1)? y_point2 : y_point;
467                         blk_point += (blk&1)? 8/*B_SIZE*/ : 0;
468 
469                         for (row = 0; row < 8; row++)
470                         {
471                             //for (col = 0; col < 8; col++)
472                             //{
473                             //	mem_y_point[col] = blk_point[col];
474                             //}
475 //                           ms_dprintk(DRV_L4,"in 0x%2x 0x%2x 0x%2x\n", blk_point[0], blk_point[1], blk_point[2]);
476                             memcpy((void *)tmp_out, (const void *)blk_point, 8);
477 //                            ms_dprintk(DRV_L4,"out 0x%2x 0x%2x 0x%2x\n", tmp_out[0], tmp_out[1], tmp_out[2]);
478                             tmp_out+=8;
479                             //fwrite(blk_point, 8, 1, fp);
480                             blk_point += image_x;
481                             //mem_y_point += 8;
482                         }
483                     }
484                 }
485             }
486         }
487         else
488         {
489             // dump UV plane
490             image_x2 = image_x >> 1;
491             for (mby = 0; mby < MB_in_height; mby++)
492             {
493                 for (mbx = 0; mbx < MB_in_width; mbx++)
494                 {
495                     k= mby * image_x <<4 /* * MB_SIZE*/;
496                     l= mbx <<4/** MB_SIZE*/;
497                     chrstart =(k>>2)+(l>>1);
498 
499                     u_point = buff + /*width * height +*/ chrstart;
500                     v_point = buff + /*width * height +*/ ( (width*height) >> 2 ) + chrstart;
501 
502                     for (blk = 0; blk < 2; blk++) // U block then V block..
503                     {
504                         blk_point = (blk==0)? u_point : v_point;
505 
506                         for (row = 0; row < 8; row++)
507                         {
508                             memcpy((void *)tmp_out, (const void *)blk_point, 8);
509                             tmp_out+=8;
510                             //fwrite(blk_point, 8, 1, fp);
511                             blk_point += image_x2;
512                         }
513                     }
514                 }
515             }
516         }
517     } else
518         ms_dprintf(ADP_L1,"input buffer in yuv2tile() is zero\n");
519 }
520