3 * Copyright 2015 Samsung Electronics Co. LTD
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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)
35 width = ((width + 15) / 16)*16;
36 tileNumX = width / 16;
38 /* crFlag - 0: Y plane, 1: CbCr plane */
43 tileAddr = tileY * tileNumX + tileX;
44 offset = (ypos & 15) * 16 + (xpos & 15);
45 addr = (tileAddr << 8) | offset;
51 tileAddr = tileY * tileNumX + tileX;
52 offset = (ypos & 7) * 16 + (xpos & 15);
53 addr = (tileAddr << 7) | offset;
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)
63 int out_of_width, actual_width;
64 unsigned int base_addr, data;
66 printf("height = %d width = %d y_addr= %x c_addr = %x \n", height, width, y_addr, c_addr);
69 for (y = 0; y < height; y += 16)
72 for (x = 0; x < width; x += 16)
74 out_of_width = (x + 16) > width ? 1 : 0;
75 base_addr = y_addr + Tile2D_To_Linear(width, height, x, y, 0);
77 for (k = 0; (k < 16) && ((y + k) < height); k++)
79 actual_width = out_of_width ? ((width%4)?((width%16) / 4 + 1) : ((width%16) / 4)) : 4;
80 for (l = 0; l < actual_width; l++)
82 data = *((unsigned int*)(base_addr + 16*k + l*4));
83 for (j = 0; (j < 4) && (x + l*4 + j) < width; j++)
85 Y_plane[(y+k)*width + x + l*4 +j] = (data>>(8*j))&0xff;
92 for (y = 0; y < height/2; y += 8)
94 for (x = 0; x < width; x += 16)
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++)
100 actual_width = out_of_width ? ((width%4) ? ((width%16) / 4 + 1) : ((width%16) / 4)) : 4;
101 for (l = 0; l < actual_width; l++)
103 data = *((unsigned int*)(base_addr + 16*k + l*4));
104 for (j = 0; (j < 2) && (x/2 + l*2 +j) < width/2; j++)
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;
115 int main (int argc, char **argv)
121 char filename[100]={0};
127 unsigned int num_frame;
129 struct stat input_file_info;
131 unsigned char* Y_plane = NULL;
132 unsigned char* Cb_plane = NULL;
133 unsigned char* Cr_plane = NULL;
135 unsigned int* c_addr = NULL;
136 unsigned int* y_addr = NULL;
140 printf("Usage : NV12T_converter [file_path] width height ex)NV12T_converter out.yuv 1280 720\n");
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);
150 Y_plane = (unsigned char*)malloc(width * height);
151 Cb_plane = (unsigned char*)malloc(width * height);
152 Cr_plane = (unsigned char*)malloc(width * height);
154 c_addr = (unsigned int*)malloc(width * height);
155 y_addr = (unsigned int*)malloc(width * height);
157 fp_in = fopen(filename, "r");
159 printf ("Input file cannot open!! \n");
161 fp_out = fopen("output_linear.yuv", "ab");
163 printf ("Output file cannot open !! \n");
165 printf("file open success: %s \n", filename);
167 stat(filename, &input_file_info);
168 num_frame = input_file_info.st_size / (width*height*3/2);
170 printf("file size = %d, num_frame = %d \n", input_file_info.st_size, num_frame);
172 for (i=0; i < num_frame; i++)
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);
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);
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]);
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);