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