Remove build warnings and fixed svace issues
[platform/adaptation/ap_samsung/libomxil-e3250-v4l2.git] / tool / NV12T_converter.c
1 /*
2  *
3  * Copyright 2015 Samsung Electronics Co. LTD
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <memory.h>
22 #include <sys/stat.h>
23
24 /* 2D Configurable tiled memory access (TM)
25  * Return the linear address from tiled position (x, y) */
26 unsigned int Tile2D_To_Linear(unsigned int width, unsigned int height,
27                                                 unsigned int xpos, unsigned int ypos, int crFlag)
28 {
29   int  tileNumX;
30   int  tileX, tileY;
31   int  tileAddr;
32   int  offset;
33   int  addr;
34
35   width = ((width + 15) / 16)*16;
36   tileNumX = width / 16;
37
38   /* crFlag - 0: Y plane, 1: CbCr plane */
39   if (crFlag == 0)
40   {
41     tileX = xpos / 16;
42     tileY = ypos / 16;
43     tileAddr = tileY * tileNumX + tileX;
44     offset = (ypos & 15) * 16 + (xpos & 15);
45     addr = (tileAddr << 8) | offset;
46   }
47   else
48   {
49     tileX = xpos / 16;
50     tileY = ypos /  8;
51     tileAddr = tileY * tileNumX + tileX;
52     offset = (ypos & 7) * 16 + (xpos & 15);
53     addr = (tileAddr << 7) | offset;
54   }
55
56   return addr;
57 }
58
59 void Tile2D_To_YUV420(unsigned char *Y_plane, unsigned char *Cb_plane, unsigned char *Cr_plane,
60                         unsigned int y_addr, unsigned int c_addr, unsigned int width, unsigned int height)
61 {
62   int x, y, j, k, l;
63   int out_of_width, actual_width;
64   unsigned int base_addr, data;
65
66   printf("height = %d  width = %d y_addr= %x  c_addr = %x \n", height, width, y_addr, c_addr);
67
68   // y: 0, 16, 32, ...
69   for (y = 0; y < height; y += 16)
70   {
71     // x: 0, 16, 32, ...
72     for (x = 0; x < width; x += 16)
73     {
74       out_of_width = (x + 16) > width ? 1 : 0;
75       base_addr = y_addr + Tile2D_To_Linear(width, height, x, y, 0);
76
77       for (k = 0; (k < 16) && ((y + k) < height); k++)
78       {
79         actual_width = out_of_width ? ((width%4)?((width%16) / 4 + 1) : ((width%16) / 4)) : 4;
80         for (l = 0; l < actual_width; l++)
81         {
82           data = *((unsigned int*)(base_addr + 16*k + l*4));
83           for (j = 0; (j < 4) && (x + l*4 + j) < width; j++)
84           {
85             Y_plane[(y+k)*width + x + l*4 +j] = (data>>(8*j))&0xff;
86           }
87         }
88       }
89     }
90   }
91
92   for (y = 0; y < height/2; y += 8)
93   {
94     for (x = 0; x < width; x += 16)
95     {
96       out_of_width = (x + 16) > width ? 1 : 0;
97       base_addr = c_addr + Tile2D_To_Linear(width, height/2, x, y, 1);
98       for (k = 0; (k < 8) && ((y+k) < height/2); k++)
99       {
100         actual_width = out_of_width ? ((width%4) ? ((width%16) / 4 + 1) : ((width%16) / 4)) : 4;
101         for (l = 0; l < actual_width; l++)
102         {
103           data = *((unsigned int*)(base_addr + 16*k + l*4));
104           for (j = 0; (j < 2) && (x/2 + l*2 +j) < width/2; j++)
105           {
106             Cb_plane[(y+k)*width/2 + x/2 + l*2 +j] = (data>> (8*2*j))&0xff;
107             Cr_plane[(y+k)*width/2 + x/2 + l*2 +j] = (data>>(8*2*j+8))&0xff;
108           }
109         }
110       }
111     }
112   }
113 }
114
115 int main (int argc, char **argv)
116 {
117   int i = 0;
118   int j = 0;
119   int size = 0;
120
121   char filename[100]={0};
122   FILE *fp_in = NULL;
123   FILE *fp_out = NULL;
124
125   unsigned int width;
126   unsigned int height;
127   unsigned int num_frame;
128
129   struct stat input_file_info;
130
131   unsigned char* Y_plane = NULL;
132   unsigned char* Cb_plane = NULL;
133   unsigned char* Cr_plane = NULL;
134
135   unsigned int* c_addr = NULL;
136   unsigned int* y_addr = NULL;
137
138   if(argc != 4)
139   {
140     printf("Usage : NV12T_converter [file_path] width height ex)NV12T_converter out.yuv 1280 720\n");
141     return 0;
142   }
143
144   strcpy(filename, argv[1]);
145   width = atoi(argv[2]);
146   height = atoi(argv[3]);
147   printf("file path=%s, width = %d, height = %d \n", filename, width, height);
148
149   /**/
150   Y_plane = (unsigned char*)malloc(width * height);
151   Cb_plane = (unsigned char*)malloc(width * height);
152   Cr_plane = (unsigned char*)malloc(width * height);
153
154   c_addr = (unsigned int*)malloc(width * height);
155   y_addr = (unsigned int*)malloc(width * height);
156
157   fp_in = fopen(filename, "r");
158   if (fp_in == NULL)
159     printf ("Input file cannot open!! \n");
160
161   fp_out = fopen("output_linear.yuv", "ab");
162   if (fp_out == NULL)
163     printf ("Output file cannot open !! \n");
164
165   printf("file open success: %s \n", filename);
166
167   stat(filename, &input_file_info);
168   num_frame = input_file_info.st_size / (width*height*3/2);
169
170   printf("file size = %d, num_frame = %d \n", input_file_info.st_size, num_frame);
171
172   for (i=0; i < num_frame; i++)
173   {
174     memset(Y_plane, 0xff, width * height);
175     memset(Cb_plane, 0xff, width * height);
176     memset(Cr_plane, 0xff, width * height);
177     memset(c_addr, 0xff, width * height);
178     memset(y_addr, 0xff, width * height);
179
180     size = fread(y_addr, 1, width * height, fp_in);
181     printf("[fread] y_addr size = %d \n", size);
182     size = fread(c_addr, 1, (width * height)/2, fp_in);
183     printf("[fread] c_addr size = %d \n", size);
184     printf("[tiled_transcode] y_addr: 0x%x  c_addr: 0x%x \n", y_addr, c_addr);
185
186     Tile2D_To_YUV420(Y_plane, Cb_plane, Cr_plane, y_addr, c_addr, width, height);
187     printf (" Y_plane %02x %02x %02x %02x %02x \n", Y_plane[0], Y_plane[1], Y_plane[2], Y_plane[3], Y_plane[4]);
188
189     size = fwrite(Y_plane, 1, width * height, fp_out);
190     printf("write Y_plane size = %d \n", size);
191     size = fwrite(Cb_plane, 1, (width * height / 4), fp_out);
192     printf("write Cb_plane size = %d \n", size);
193     size = fwrite(Cr_plane, 1, (width * height / 4), fp_out);
194     printf("write Cr_plane size = %d \n", size);
195     printf("count = %d \n", i);
196   }
197
198   free(Y_plane);
199   free(Cb_plane);
200   free(Cr_plane);
201   free(c_addr);
202   free(y_addr);
203
204   fclose (fp_in);
205   fclose (fp_out);
206
207  return 0;
208 }