00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "contributors.h"
00017
00018 #include <math.h>
00019 #include <time.h>
00020 #include <sys/timeb.h>
00021
00022 #include "global.h"
00023 #include "input.h"
00024 #include "report.h"
00025 #include "img_io.h"
00026
00027
00028 unsigned char *buf = NULL;
00029 unsigned char *ibuf = NULL;
00030
00031 void buf2img_basic ( imgpel** imgX, unsigned char* buf, int size_x, int size_y, int o_size_x, int o_size_y, int symbol_size_in_bytes, int bitshift);
00032 void buf2img_endian ( imgpel** imgX, unsigned char* buf, int size_x, int size_y, int o_size_x, int o_size_y, int symbol_size_in_bytes, int bitshift);
00033 void buf2img_bitshift ( imgpel** imgX, unsigned char* buf, int size_x, int size_y, int o_size_x, int o_size_y, int symbol_size_in_bytes, int bitshift);
00034 void (*buf2img) ( imgpel** imgX, unsigned char* buf, int size_x, int size_y, int o_size_x, int o_size_y, int symbol_size_in_bytes, int bitshift);
00035 void fillPlane ( imgpel** imgX, int nVal, int size_x, int size_y);
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 void initInput(FrameFormat *source, FrameFormat *output)
00047 {
00048 if (source->bit_depth[0] == output->bit_depth[0] && source->bit_depth[1] == output->bit_depth[1])
00049 {
00050 if (( sizeof(char) != sizeof (imgpel)) && testEndian())
00051 buf2img = buf2img_endian;
00052 else
00053 buf2img = buf2img_basic;
00054 }
00055 else
00056 buf2img = buf2img_bitshift;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 int testEndian(void)
00070 {
00071 short s;
00072 byte *p;
00073
00074 p=(byte*)&s;
00075
00076 s=1;
00077
00078 return (*p==0);
00079 }
00080
00081 #if (DEBUG_BITDEPTH)
00082
00083
00084
00085
00086
00087
00088 static void MaskMSBs (imgpel** imgX, int mask, int width, int height)
00089 {
00090 int i,j;
00091
00092 for (j=0; j < height; j++)
00093 {
00094 for (i=0; i < width; i++)
00095 {
00096 imgX[j][i]=(imgpel) (imgX[j][i] & mask);
00097 }
00098 }
00099 }
00100 #endif
00101
00102
00103
00104
00105
00106
00107
00108 void fillPlane ( imgpel** imgX,
00109 int nVal,
00110 int size_x,
00111 int size_y
00112 )
00113 {
00114 int j, i;
00115
00116 if (sizeof(imgpel) == sizeof(char))
00117 {
00118 memset(&imgX[0][0], nVal, size_y * size_x);
00119 }
00120 else
00121 {
00122 for (j = 0; j < size_y; j++)
00123 {
00124 for (i = 0; i < size_x; i++)
00125 {
00126 imgX[j][i] = nVal;
00127 }
00128 }
00129 }
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 static void deinterleave ( unsigned char** input,
00139 unsigned char** output,
00140 FrameFormat *source,
00141 int symbol_size_in_bytes
00142 )
00143 {
00144
00145 unsigned char *icmp0 = *input;
00146
00147 unsigned char *ocmp0 = *output;
00148
00149 unsigned char *ocmp1 = ocmp0 + symbol_size_in_bytes * source->size_cmp[0];
00150 unsigned char *ocmp2 = ocmp1 + symbol_size_in_bytes * source->size_cmp[1];
00151
00152 int i;
00153
00154 if (source->yuv_format == YUV420)
00155 {
00156 for (i = 0; i < source->size_cmp[1]; i++)
00157 {
00158 memcpy(ocmp1, icmp0, symbol_size_in_bytes);
00159 ocmp1 += symbol_size_in_bytes;
00160 icmp0 += symbol_size_in_bytes;
00161 memcpy(ocmp0, icmp0, 2 * symbol_size_in_bytes);
00162 ocmp0 += 2 * symbol_size_in_bytes;
00163 icmp0 += 2 * symbol_size_in_bytes;
00164 memcpy(ocmp2, icmp0, symbol_size_in_bytes);
00165 ocmp2 += symbol_size_in_bytes;
00166 icmp0 += symbol_size_in_bytes;
00167 memcpy(ocmp0, icmp0, 2 * symbol_size_in_bytes);
00168 ocmp0 += 2 * symbol_size_in_bytes;
00169 icmp0 += 2 * symbol_size_in_bytes;
00170 }
00171
00172
00173 icmp0 = *input;
00174 *input = *output;
00175 *output = icmp0;
00176 }
00177 if (source->yuv_format == YUV422)
00178 {
00179 for (i = 0; i < source->size_cmp[1]; i++)
00180 {
00181
00182 memcpy(ocmp0, icmp0, symbol_size_in_bytes);
00183 ocmp0 += symbol_size_in_bytes;
00184 icmp0 += symbol_size_in_bytes;
00185
00186 memcpy(ocmp1, icmp0, symbol_size_in_bytes);
00187 ocmp1 += symbol_size_in_bytes;
00188 icmp0 += symbol_size_in_bytes;
00189
00190 memcpy(ocmp0, icmp0, symbol_size_in_bytes);
00191 ocmp0 += symbol_size_in_bytes;
00192 icmp0 += symbol_size_in_bytes;
00193
00194 memcpy(ocmp2, icmp0, symbol_size_in_bytes);
00195 ocmp2 += symbol_size_in_bytes;
00196 icmp0 += symbol_size_in_bytes;
00197 }
00198
00199
00200 icmp0 = *input;
00201 *input = *output;
00202 *output = icmp0;
00203 }
00204 else if (source->yuv_format == YUV444)
00205 {
00206 for (i = 0; i < source->size_cmp[0]; i++)
00207 {
00208 memcpy(ocmp0, icmp0, symbol_size_in_bytes);
00209 ocmp0 += symbol_size_in_bytes;
00210 icmp0 += symbol_size_in_bytes;
00211 memcpy(ocmp1, icmp0, symbol_size_in_bytes);
00212 ocmp1 += symbol_size_in_bytes;
00213 icmp0 += symbol_size_in_bytes;
00214 memcpy(ocmp2, icmp0, symbol_size_in_bytes);
00215 ocmp2 += symbol_size_in_bytes;
00216 icmp0 += symbol_size_in_bytes;
00217 }
00218
00219 icmp0 = *input;
00220 *input = *output;
00221 *output = icmp0;
00222 }
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 void buf2img_bitshift ( imgpel** imgX,
00232 unsigned char* buf,
00233 int size_x,
00234 int size_y,
00235 int o_size_x,
00236 int o_size_y,
00237 int symbol_size_in_bytes,
00238 int bitshift
00239 )
00240 {
00241 int i,j;
00242
00243 unsigned short tmp16, ui16;
00244 unsigned long tmp32, ui32;
00245
00246 if (((symbol_size_in_bytes << 3) - bitshift) > (sizeof(imgpel)<< 3))
00247 {
00248 error ("Source picture has higher bit depth than imgpel data type. \nPlease recompile with larger data type for imgpel.", 500);
00249 }
00250
00251 if (testEndian())
00252 {
00253 if (size_x != o_size_x || size_y != o_size_y)
00254 {
00255 error ("Rescaling not supported in big endian architectures. ", 500);
00256 }
00257
00258
00259 switch (symbol_size_in_bytes)
00260 {
00261 case 1:
00262 {
00263 for(j = 0; j < o_size_y; j++)
00264 for(i = 0; i < o_size_x; i++)
00265 {
00266 imgX[j][i]= (imgpel) rshift_rnd(buf[i + j*size_x], bitshift);
00267 }
00268 break;
00269 }
00270 case 2:
00271 {
00272 for(j = 0; j < o_size_y; j++)
00273 for(i = 0; i < o_size_x; i++)
00274 {
00275 memcpy(&tmp16, buf+((i+j*size_x)*2), 2);
00276 ui16 = (tmp16 >> 8) | ((tmp16&0xFF)<<8);
00277 imgX[j][i] = (imgpel) rshift_rnd(ui16, bitshift);
00278 }
00279 break;
00280 }
00281 case 4:
00282 {
00283 for(j = 0; j < o_size_y; j++)
00284 for(i = 0; i < o_size_x; i++)
00285 {
00286 memcpy(&tmp32, buf+((i+j*size_x)*4), 4);
00287 ui32 = ((tmp32&0xFF00)<<8) | ((tmp32&0xFF)<<24) | ((tmp32&0xFF0000)>>8) | ((tmp32&0xFF000000)>>24);
00288 imgX[j][i] = (imgpel) rshift_rnd(ui32, bitshift);
00289 }
00290 }
00291 default:
00292 {
00293 error ("reading only from formats of 8, 16 or 32 bit allowed on big endian architecture", 500);
00294 break;
00295 }
00296 }
00297 }
00298 else
00299 {
00300
00301 int j_pos;
00302 if (size_x == o_size_x && size_y == o_size_y)
00303 {
00304 for (j = 0; j < o_size_y; j++)
00305 {
00306 j_pos = j*size_x;
00307 for (i = 0; i < o_size_x; i++)
00308 {
00309 ui16=0;
00310 memcpy(&(ui16), buf + ((i + j_pos) * symbol_size_in_bytes), symbol_size_in_bytes);
00311 imgX[j][i] = (imgpel) rshift_rnd(ui16,bitshift);
00312 }
00313 }
00314 }
00315 else
00316 {
00317 int iminwidth = imin(size_x, o_size_x);
00318 int iminheight = imin(size_y, o_size_y);
00319 int dst_offset_x = 0, dst_offset_y = 0;
00320 int offset_x = 0, offset_y = 0;
00321
00322
00323 if ( o_size_x >= size_x )
00324 dst_offset_x = ( o_size_x - size_x ) >> 1;
00325
00326 if (o_size_y >= size_y)
00327 dst_offset_y = ( o_size_y - size_y ) >> 1;
00328
00329
00330
00331 iminwidth = ( (offset_x + iminwidth ) > size_x ) ? (size_x - offset_x) : iminwidth;
00332 iminheight = ( (offset_y + iminheight) > size_y ) ? (size_y - offset_y) : iminheight;
00333
00334 iminwidth = ( (dst_offset_x + iminwidth ) > o_size_x ) ? (o_size_x - dst_offset_x) : iminwidth;
00335 iminheight = ( (dst_offset_y + iminheight) > o_size_y ) ? (o_size_y - dst_offset_y) : iminheight;
00336
00337 for (j=0; j < iminheight; j++)
00338 {
00339 j_pos = (j + offset_y) * size_x + offset_x;
00340 for (i=0; i < iminwidth; i++)
00341 {
00342 ui16=0;
00343 memcpy(&(ui16), buf + ((i + j_pos) * symbol_size_in_bytes), symbol_size_in_bytes);
00344 imgX[j + dst_offset_y][i + dst_offset_x] = (imgpel) rshift_rnd(ui16,bitshift);
00345 }
00346 }
00347 }
00348 }
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358 void buf2img_basic ( imgpel** imgX,
00359 unsigned char* buf,
00360 int size_x,
00361 int size_y,
00362 int o_size_x,
00363 int o_size_y,
00364 int symbol_size_in_bytes,
00365 int dummy
00366 )
00367 {
00368 int i,j;
00369 unsigned char* temp_buf = buf;
00370
00371 if (symbol_size_in_bytes> sizeof(imgpel))
00372 {
00373 error ("Source picture has higher bit depth than imgpel data type. \nPlease recompile with larger data type for imgpel.", 500);
00374 }
00375
00376 if (( sizeof (imgpel) == symbol_size_in_bytes))
00377 {
00378
00379 if (size_x == o_size_x && size_y == o_size_y)
00380 memcpy(&imgX[0][0], temp_buf, size_x * size_y * sizeof(imgpel));
00381 else
00382 {
00383 int iminwidth = imin(size_x, o_size_x);
00384 int iminheight = imin(size_y, o_size_y);
00385 int dst_offset_x = 0, dst_offset_y = 0;
00386 int offset_x = 0, offset_y = 0;
00387
00388
00389 if ( o_size_x >= size_x )
00390 dst_offset_x = ( o_size_x - size_x ) >> 1;
00391
00392 if (o_size_y >= size_y)
00393 dst_offset_y = ( o_size_y - size_y ) >> 1;
00394
00395
00396
00397 iminwidth = ( (offset_x + iminwidth ) > size_x ) ? (size_x - offset_x) : iminwidth;
00398 iminheight = ( (offset_y + iminheight) > size_y ) ? (size_y - offset_y) : iminheight;
00399
00400 iminwidth = ( (dst_offset_x + iminwidth ) > o_size_x ) ? (o_size_x - dst_offset_x) : iminwidth;
00401 iminheight = ( (dst_offset_y + iminheight) > o_size_y ) ? (o_size_y - dst_offset_y) : iminheight;
00402
00403 for (i=0; i<iminheight;i++) {
00404 memcpy(&imgX[i + dst_offset_y][dst_offset_x], &(temp_buf[(i + offset_y) * size_x + offset_x]), iminwidth * sizeof(imgpel));
00405 }
00406 }
00407 }
00408 else
00409 {
00410 int j_pos;
00411 unsigned short ui16;
00412 if (size_x == o_size_x && size_y == o_size_y)
00413 {
00414 for (j=0; j < o_size_y; j++)
00415 {
00416 j_pos = j * size_x;
00417 for (i=0; i < o_size_x; i++)
00418 {
00419 ui16=0;
00420 memcpy(&(ui16), buf + ((i + j_pos) * symbol_size_in_bytes), symbol_size_in_bytes);
00421 imgX[j][i]= (imgpel) ui16;
00422 }
00423 }
00424 }
00425 else
00426 {
00427 int iminwidth = imin(size_x, o_size_x);
00428 int iminheight = imin(size_y, o_size_y);
00429 int dst_offset_x = 0, dst_offset_y = 0;
00430 int offset_x = 0, offset_y = 0;
00431
00432
00433 if ( o_size_x >= size_x )
00434 dst_offset_x = ( o_size_x - size_x ) >> 1;
00435
00436 if (o_size_y >= size_y)
00437 dst_offset_y = ( o_size_y - size_y ) >> 1;
00438
00439
00440
00441 iminwidth = ( (offset_x + iminwidth ) > size_x ) ? (size_x - offset_x) : iminwidth;
00442 iminheight = ( (offset_y + iminheight) > size_y ) ? (size_y - offset_y) : iminheight;
00443
00444 iminwidth = ( (dst_offset_x + iminwidth ) > o_size_x ) ? (o_size_x - dst_offset_x) : iminwidth;
00445 iminheight = ( (dst_offset_y + iminheight) > o_size_y ) ? (o_size_y - dst_offset_y) : iminheight;
00446
00447 for (j = 0; j < iminheight; j++) {
00448 memcpy(&imgX[j + dst_offset_y][dst_offset_x], &(temp_buf[(j + offset_y) * size_x + offset_x]), iminwidth * sizeof(imgpel));
00449 }
00450 for (j=0; j < iminheight; j++)
00451 {
00452 j_pos = (j + offset_y) * size_x + offset_x;
00453 for (i=0; i < iminwidth; i++)
00454 {
00455 ui16 = 0;
00456 memcpy(&(ui16), buf + ((i + j_pos) * symbol_size_in_bytes), symbol_size_in_bytes);
00457 imgX[j + dst_offset_y][i + dst_offset_x]= (imgpel) ui16;
00458 }
00459 }
00460 }
00461 }
00462 }
00463
00464
00465
00466
00467
00468
00469
00470 void buf2img_endian ( imgpel** imgX,
00471 unsigned char* buf,
00472 int size_x,
00473 int size_y,
00474 int o_size_x,
00475 int o_size_y,
00476 int symbol_size_in_bytes,
00477 int dummy
00478 )
00479 {
00480 int i,j;
00481
00482 unsigned short tmp16, ui16;
00483 unsigned long tmp32, ui32;
00484
00485 if (symbol_size_in_bytes > sizeof(imgpel))
00486 {
00487 error ("Source picture has higher bit depth than imgpel data type. \nPlease recompile with larger data type for imgpel.", 500);
00488 }
00489
00490 if (size_x != o_size_x || size_y != o_size_y)
00491 {
00492 error ("Rescaling not supported in big endian architectures. ", 500);
00493 }
00494
00495
00496 switch (symbol_size_in_bytes)
00497 {
00498 case 1:
00499 {
00500 for(j=0;j<size_y;j++)
00501 {
00502 for(i=0;i<size_x;i++)
00503 {
00504 imgX[j][i]= (imgpel) buf[i + j*size_x];
00505 }
00506 }
00507 break;
00508 }
00509 case 2:
00510 {
00511 for(j=0;j<size_y;j++)
00512 {
00513 for(i=0;i<size_x;i++)
00514 {
00515 memcpy(&tmp16, buf+((i+j*size_x)*2), 2);
00516 ui16 = (tmp16 >> 8) | ((tmp16&0xFF)<<8);
00517 imgX[j][i] = (imgpel) ui16;
00518 }
00519 }
00520 break;
00521 }
00522 case 4:
00523 {
00524 for(j=0;j<size_y;j++)
00525 {
00526 for(i=0;i<size_x;i++)
00527 {
00528 memcpy(&tmp32, buf+((i+j*size_x)*4), 4);
00529 ui32 = ((tmp32&0xFF00)<<8) | ((tmp32&0xFF)<<24) | ((tmp32&0xFF0000)>>8) | ((tmp32&0xFF000000)>>24);
00530 imgX[j][i] = (imgpel) ui32;
00531 }
00532 }
00533 break;
00534 }
00535 default:
00536 {
00537 error ("reading only from formats of 8, 16 or 32 bit allowed on big endian architecture", 500);
00538 break;
00539 }
00540 }
00541 }
00542
00543
00544
00545
00546
00547
00548
00549
00550 void AllocateFrameMemory (ImageParameters *img, InputParameters *params, int size)
00551 {
00552 if (NULL == (buf = malloc (size * (img->pic_unit_size_on_disk >> 3))))
00553 no_mem_exit("AllocateFrameMemory: buf");
00554 if (params->input_file1.is_interleaved)
00555 {
00556 if (NULL == (ibuf = malloc (size * (img->pic_unit_size_on_disk >> 3))))
00557 no_mem_exit("AllocateFrameMemory: ibuf");
00558 }
00559 }
00560
00561
00562
00563
00564
00565
00566
00567
00568 void DeleteFrameMemory (void)
00569 {
00570 if (buf != NULL)
00571 free (buf);
00572 if (ibuf != NULL)
00573 free (ibuf);
00574 }
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 void ReadOneFrame (VideoDataFile *input_file, int FrameNoInFile, int HeaderSize, FrameFormat *source, FrameFormat *output, imgpel **pImage[3])
00596 {
00597 unsigned int symbol_size_in_bytes = img->pic_unit_size_on_disk >> 3;
00598
00599 const int imgsize_y = source->width * source->height;
00600 const int imgsize_uv = source->width_cr * source->height_cr;
00601
00602 const int bytes_y = imgsize_y * symbol_size_in_bytes;
00603 const int bytes_uv = imgsize_uv * symbol_size_in_bytes;
00604 int bit_scale;
00605
00606 Boolean rgb_input = (Boolean) (params->rgb_input_flag == CM_RGB && params->source.yuv_format == YUV444);
00607
00608 if (input_file->is_concatenated == 0)
00609 {
00610 if (input_file->vdtype == VIDEO_TIFF)
00611 ReadTIFFImage (input_file, FrameNoInFile, source, buf);
00612 else
00613 ReadFrameSeparate (input_file, FrameNoInFile, HeaderSize, source, buf);
00614 }
00615 else
00616 {
00617 ReadFrameConcatenated (input_file, FrameNoInFile, HeaderSize, source, buf);
00618 }
00619
00620
00621 if (input_file->is_interleaved)
00622 {
00623 deinterleave ( &buf, &ibuf, source, symbol_size_in_bytes);
00624 }
00625
00626 bit_scale = source->bit_depth[0] - output->bit_depth[0];
00627
00628 if(rgb_input)
00629 buf2img(pImage[0], buf + bytes_y, source->width, source->height, output->width, output->height, symbol_size_in_bytes, bit_scale);
00630 else
00631 buf2img(pImage[0], buf, source->width, source->height, output->width, output->height, symbol_size_in_bytes, bit_scale);
00632
00633 #if (DEBUG_BITDEPTH)
00634 MaskMSBs(pImage[0], ((1 << output->bit_depth[0]) - 1), output->width, output->height);
00635 #endif
00636
00637 if (img->yuv_format != YUV400)
00638 {
00639 bit_scale = source->bit_depth[1] - output->bit_depth[1];
00640 #if (ALLOW_GRAYSCALE)
00641 if (!params->grayscale)
00642 #endif
00643 {
00644 if(rgb_input)
00645 buf2img(pImage[1], buf + bytes_y + bytes_uv, source->width_cr, source->height_cr, output->width_cr, output->height_cr, symbol_size_in_bytes, bit_scale);
00646 else
00647 buf2img(pImage[1], buf + bytes_y, source->width_cr, source->height_cr, output->width_cr, output->height_cr, symbol_size_in_bytes, bit_scale);
00648
00649 bit_scale = source->bit_depth[2] - output->bit_depth[2];
00650 if(rgb_input)
00651 buf2img(pImage[2], buf, source->width_cr, source->height_cr, output->width_cr, output->height_cr, symbol_size_in_bytes, bit_scale);
00652 else
00653 buf2img(pImage[2], buf + bytes_y + bytes_uv, source->width_cr, source->height_cr, output->width_cr, output->height_cr, symbol_size_in_bytes, bit_scale);
00654 }
00655 #if (DEBUG_BITDEPTH)
00656 MaskMSBs(pImage[1], ((1 << output->bit_depth[1]) - 1), output->width_cr, output->height_cr);
00657 MaskMSBs(pImage[2], ((1 << output->bit_depth[2]) - 1), output->width_cr, output->height_cr);
00658 #endif
00659 }
00660 }
00661