1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2022, ITU/ISO/IEC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file TVideoIOYuv.cpp
\brief YUV file I/O class
*/
#include <cstdlib>
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <memory.h>
#include "TLibCommon/TComRom.h"
#include "TVideoIOYuv.h"
using namespace std;
// ====================================================================================================================
// Local Functions
// ====================================================================================================================
/**
* Scale all pixels in img depending upon sign of shiftbits by a factor of
* 2<sup>shiftbits</sup>.
*
* @param img pointer to image to be transformed
* @param stride distance between vertically adjacent pixels of img.
* @param width width of active area in img.
* @param height height of active area in img.
* @param shiftbits if zero, no operation performed
* if > 0, multiply by 2<sup>shiftbits</sup>, see scalePlane()
* if < 0, divide and round by 2<sup>shiftbits</sup> and clip,
* see invScalePlane().
* @param minval minimum clipping value when dividing.
* @param maxval maximum clipping value when dividing.
*/
static Void scalePlane(Pel* img, const UInt stride, const UInt width, const UInt height, Int shiftbits, Pel minval, Pel maxval)
{
if (shiftbits > 0)
{
for (UInt y = 0; y < height; y++, img+=stride)
{
for (UInt x = 0; x < width; x++)
{
img[x] <<= shiftbits;
}
}
}
else if (shiftbits < 0)
{
shiftbits=-shiftbits;
Pel rounding = 1 << (shiftbits-1);
for (UInt y = 0; y < height; y++, img+=stride)
{
for (UInt x = 0; x < width; x++)
{
img[x] = Clip3(minval, maxval, Pel((img[x] + rounding) >> shiftbits));
}
}
}
}
static Void
copyPlane(const TComPicYuv &src, const ComponentID srcPlane, TComPicYuv &dest, const ComponentID destPlane);
// ====================================================================================================================
// Public member functions
// ====================================================================================================================
/**
* Open file for reading/writing Y'CbCr frames.
*
* Frames read/written have bitdepth fileBitDepth, and are automatically
* formatted as 8 or 16 bit word values (see TVideoIOYuv::write()).
*
* Image data read or written is converted to/from internalBitDepth
* (See scalePlane(), TVideoIOYuv::read() and TVideoIOYuv::write() for
* further details).
*
* \param pchFile file name string
* \param bWriteMode file open mode: true=write, false=read
* \param fileBitDepth bit-depth array of input/output file data.
* \param MSBExtendedBitDepth
* \param internalBitDepth bit-depth array to scale image data to/from when reading/writing.
*/
Void TVideoIOYuv::open( const std::string &fileName, Bool bWriteMode, const Int fileBitDepth[MAX_NUM_CHANNEL_TYPE], const Int MSBExtendedBitDepth[MAX_NUM_CHANNEL_TYPE], const Int internalBitDepth[MAX_NUM_CHANNEL_TYPE] )
{
//NOTE: files cannot have bit depth greater than 16
for(UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
{
m_fileBitdepth [ch] = std::min<UInt>(fileBitDepth[ch], 16);
m_MSBExtendedBitDepth[ch] = MSBExtendedBitDepth[ch];
m_bitdepthShift [ch] = internalBitDepth[ch] - m_MSBExtendedBitDepth[ch];
if (m_fileBitdepth[ch] > 16)
{
if (bWriteMode)
{
std::cerr << "\nWARNING: Cannot write a yuv file of bit depth greater than 16 - output will be right-shifted down to 16-bit precision\n" << std::endl;
}
else
{
std::cerr << "\nERROR: Cannot read a yuv file of bit depth greater than 16\n" << std::endl;
exit(0);
}
}
}
if ( bWriteMode )
{
m_cHandle.open( fileName.c_str(), ios::binary | ios::out );
if( m_cHandle.fail() )
{
printf("\nfailed to write reconstructed YUV file\n");
exit(0);
}
}
else
{
m_cHandle.open( fileName.c_str(), ios::binary | ios::in );
if( m_cHandle.fail() )
{
printf("\nfailed to open Input YUV file\n");
exit(0);
}
}
return;
}
Void TVideoIOYuv::close()
{
m_cHandle.close();
}
Bool TVideoIOYuv::isEof()
{
return m_cHandle.eof();
}
Bool TVideoIOYuv::isFail()
{
return m_cHandle.fail();
}
/**
* Skip numFrames in input.
*
* This function correctly handles cases where the input file is not
* seekable, by consuming bytes.
*/
Void TVideoIOYuv::skipFrames(Int numFrames, UInt width, UInt height, ChromaFormat format)
{
if (numFrames==0)
{
return;
}
//------------------
//set the frame size according to the chroma format
streamoff frameSize = 0;
UInt wordsize=1; // default to 8-bit, unless a channel with more than 8-bits is detected.
for (UInt component = 0; component < getNumberValidComponents(format); component++)
{
ComponentID compID=ComponentID(component);
frameSize += (width >> getComponentScaleX(compID, format)) * (height >> getComponentScaleY(compID, format));
if (m_fileBitdepth[toChannelType(compID)] > 8)
{
wordsize=2;
}
}
frameSize *= wordsize;
//------------------
const streamoff offset = frameSize * numFrames;
/* attempt to seek */
if (!!m_cHandle.seekg(offset, ios::cur))
{
return; /* success */
}
m_cHandle.clear();
/* fall back to consuming the input */
TChar buf[512];
const streamoff offset_mod_bufsize = offset % sizeof(buf);
for (streamoff i = 0; i < offset - offset_mod_bufsize; i += sizeof(buf))
{
m_cHandle.read(buf, sizeof(buf));
}
m_cHandle.read(buf, offset_mod_bufsize);
}
/**
* Read width*height pixels from fd into dst, optionally
* padding the left and right edges by edge-extension. Input may be
* either 8bit or 16bit little-endian lsb-aligned words.
*
* @param dst destination image plane
* @param fd input file stream
* @param is16bit true if input file carries > 8bit data, false otherwise.
* @param stride444 distance between vertically adjacent pixels of dst.
* @param width444 width of active area in dst.
* @param height444 height of active area in dst.
* @param pad_x444 length of horizontal padding.
* @param pad_y444 length of vertical padding.
* @param compID chroma component
* @param destFormat chroma format of image
* @param fileFormat chroma format of file
* @param fileBitDepth component bit depth in file
* @return true for success, false in case of error
*/
static Bool readPlane(Pel* dst,
istream& fd,
Bool is16bit,
UInt stride444,
UInt width444,
UInt height444,
UInt pad_x444,
UInt pad_y444,
const ComponentID compID,
const ChromaFormat destFormat,
const ChromaFormat fileFormat,
const UInt fileBitDepth)
{
const UInt csx_file =getComponentScaleX(compID, fileFormat);
const UInt csy_file =getComponentScaleY(compID, fileFormat);
const UInt csx_dest =getComponentScaleX(compID, destFormat);
const UInt csy_dest =getComponentScaleY(compID, destFormat);
const UInt width_dest = width444 >>csx_dest;
const UInt height_dest = height444>>csy_dest;
const UInt pad_x_dest = pad_x444>>csx_dest;
const UInt pad_y_dest = pad_y444>>csy_dest;
const UInt stride_dest = stride444>>csx_dest;
const UInt full_width_dest = width_dest+pad_x_dest;
const UInt full_height_dest = height_dest+pad_y_dest;
const UInt stride_file = (width444 * (is16bit ? 2 : 1)) >> csx_file;
std::vector<UChar> bufVec(stride_file);
UChar *buf=&(bufVec[0]);
if (compID!=COMPONENT_Y && (fileFormat==CHROMA_400 || destFormat==CHROMA_400))
{
if (destFormat!=CHROMA_400)
{
// set chrominance data to mid-range: (1<<(fileBitDepth-1))
const Pel value=Pel(1<<(fileBitDepth-1));
for (UInt y = 0; y < full_height_dest; y++, dst+=stride_dest)
{
for (UInt x = 0; x < full_width_dest; x++)
{
dst[x] = value;
}
}
}
if (fileFormat!=CHROMA_400)
{
const UInt height_file = height444>>csy_file;
fd.seekg(height_file*stride_file, ios::cur);
if (fd.eof() || fd.fail() )
{
return false;
}
}
}
else
{
const UInt mask_y_file=(1<<csy_file)-1;
const UInt mask_y_dest=(1<<csy_dest)-1;
for(UInt y444=0; y444<height444; y444++)
{
if ((y444&mask_y_file)==0)
{
// read a new line
fd.read(reinterpret_cast<TChar*>(buf), stride_file);
if (fd.eof() || fd.fail() )
{
return false;
}
}
if ((y444&mask_y_dest)==0)
{
// process current destination line
if (csx_file < csx_dest)
{
// eg file is 444, dest is 422.
const UInt sx=csx_dest-csx_file;
if (!is16bit)
{
for (UInt x = 0; x < width_dest; x++)
{
dst[x] = buf[x<<sx];
}
}
else
{
for (UInt x = 0; x < width_dest; x++)
{
dst[x] = Pel(buf[(x<<sx)*2+0]) | (Pel(buf[(x<<sx)*2+1])<<8);
}
}
}
else
{
// eg file is 422, dest is 444.
const UInt sx=csx_file-csx_dest;
if (!is16bit)
{
for (UInt x = 0; x < width_dest; x++)
{
dst[x] = buf[x>>sx];
}
}
else
{
for (UInt x = 0; x < width_dest; x++)
{
dst[x] = Pel(buf[(x>>sx)*2+0]) | (Pel(buf[(x>>sx)*2+1])<<8);
}
}
}
// process right hand side padding
const Pel val=dst[width_dest-1];
for (UInt x = width_dest; x < full_width_dest; x++)
{
dst[x] = val;
}
dst += stride_dest;
}
}
// process lower padding
for (UInt y = height_dest; y < full_height_dest; y++, dst+=stride_dest)
{
for (UInt x = 0; x < full_width_dest; x++)
{
dst[x] = (dst - stride_dest)[x];
}
}
}
return true;
}
/**
* Write an image plane (width444*height444 pixels) from src into output stream fd.
*
* @param fd output file stream
* @param src source image
* @param is16bit true if input file carries > 8bit data, false otherwise.
* @param stride444 distance between vertically adjacent pixels of src.
* @param width444 width of active area in src.
* @param height444 height of active area in src.
* @param compID chroma component
* @param srcFormat chroma format of image
* @param fileFormat chroma format of file
* @param fileBitDepth component bit depth in file
* @return true for success, false in case of error
*/
static Bool writePlane(ostream& fd, Pel* src, Bool is16bit,
UInt stride444,
UInt width444, UInt height444,
const ComponentID compID,
const ChromaFormat srcFormat,
const ChromaFormat fileFormat,
const UInt fileBitDepth)
{
const UInt csx_file =getComponentScaleX(compID, fileFormat);
const UInt csy_file =getComponentScaleY(compID, fileFormat);
const UInt csx_src =getComponentScaleX(compID, srcFormat);
const UInt csy_src =getComponentScaleY(compID, srcFormat);
const UInt stride_src = stride444>>csx_src;
const UInt stride_file = (width444 * (is16bit ? 2 : 1)) >> csx_file;
const UInt width_file = width444 >>csx_file;
const UInt height_file = height444>>csy_file;
std::vector<UChar> bufVec(stride_file);
UChar *buf=&(bufVec[0]);
if (compID!=COMPONENT_Y && (fileFormat==CHROMA_400 || srcFormat==CHROMA_400))
{
if (fileFormat!=CHROMA_400)
{
const UInt value=1<<(fileBitDepth-1);
for(UInt y=0; y< height_file; y++)
{
if (!is16bit)
{
UChar val(value);
for (UInt x = 0; x < width_file; x++)
{
buf[x]=val;
}
}
else
{
UShort val(value);
for (UInt x = 0; x < width_file; x++)
{
buf[2*x+0]= (val>>0) & 0xff;
buf[2*x+1]= (val>>8) & 0xff;
}
}
fd.write(reinterpret_cast<const TChar*>(buf), stride_file);
if (fd.eof() || fd.fail() )
{
return false;
}
}
}
}
else
{
const UInt mask_y_file=(1<<csy_file)-1;
const UInt mask_y_src =(1<<csy_src )-1;
for(UInt y444=0; y444<height444; y444++)
{
if ((y444&mask_y_file)==0)
{
// write a new line
if (csx_file < csx_src)
{
// eg file is 444, source is 422.
const UInt sx=csx_src-csx_file;
if (!is16bit)
{
for (UInt x = 0; x < width_file; x++)
{
buf[x] = (UChar)(src[x>>sx]);
}
}
else
{
for (UInt x = 0; x < width_file; x++)
{
buf[2*x ] = (src[x>>sx]>>0) & 0xff;
buf[2*x+1] = (src[x>>sx]>>8) & 0xff;
}
}
}
else
{
// eg file is 422, src is 444.
const UInt sx=csx_file-csx_src;
if (!is16bit)
{
for (UInt x = 0; x < width_file; x++)
{
buf[x] = (UChar)(src[x<<sx]);
}
}
else
{
for (UInt x = 0; x < width_file; x++)
{
buf[2*x ] = (src[x<<sx]>>0) & 0xff;
buf[2*x+1] = (src[x<<sx]>>8) & 0xff;
}
}
}
fd.write(reinterpret_cast<const TChar*>(buf), stride_file);
if (fd.eof() || fd.fail() )
{
return false;
}
}
if ((y444&mask_y_src)==0)
{
src += stride_src;
}
}
}
return true;
}
static Bool writeField(ostream& fd, Pel* top, Pel* bottom, Bool is16bit,
UInt stride444,
UInt width444, UInt height444,
const ComponentID compID,
const ChromaFormat srcFormat,
const ChromaFormat fileFormat,
const UInt fileBitDepth, const Bool isTff)
{
const UInt csx_file =getComponentScaleX(compID, fileFormat);
const UInt csy_file =getComponentScaleY(compID, fileFormat);
const UInt csx_src =getComponentScaleX(compID, srcFormat);
const UInt csy_src =getComponentScaleY(compID, srcFormat);
const UInt stride_src = stride444>>csx_src;
const UInt stride_file = (width444 * (is16bit ? 2 : 1)) >> csx_file;
const UInt width_file = width444 >>csx_file;
const UInt height_file = height444>>csy_file;
std::vector<UChar> bufVec(stride_file * 2);
UChar *buf=&(bufVec[0]);
if (compID!=COMPONENT_Y && (fileFormat==CHROMA_400 || srcFormat==CHROMA_400))
{
if (fileFormat!=CHROMA_400)
{
const UInt value=1<<(fileBitDepth-1);
for(UInt y=0; y< height_file; y++)
{
for (UInt field = 0; field < 2; field++)
{
UChar *fieldBuffer = buf + (field * stride_file);
if (!is16bit)
{
UChar val(value);
for (UInt x = 0; x < width_file; x++)
{
fieldBuffer[x]=val;
}
}
else
{
UShort val(value);
for (UInt x = 0; x < width_file; x++)
{
fieldBuffer[2*x+0]= (val>>0) & 0xff;
fieldBuffer[2*x+1]= (val>>8) & 0xff;
}
}
}
fd.write(reinterpret_cast<const TChar*>(buf), (stride_file * 2));
if (fd.eof() || fd.fail() )
{
return false;
}
}
}
}
else
{
const UInt mask_y_file=(1<<csy_file)-1;
const UInt mask_y_src =(1<<csy_src )-1;
for(UInt y444=0; y444<height444; y444++)
{
if ((y444&mask_y_file)==0)
{
for (UInt field = 0; field < 2; field++)
{
UChar *fieldBuffer = buf + (field * stride_file);
Pel *src = (((field == 0) && isTff) || ((field == 1) && (!isTff))) ? top : bottom;
// write a new line
if (csx_file < csx_src)
{
// eg file is 444, source is 422.
const UInt sx=csx_src-csx_file;
if (!is16bit)
{
for (UInt x = 0; x < width_file; x++)
{
fieldBuffer[x] = (UChar)(src[x>>sx]);
}
}
else
{
for (UInt x = 0; x < width_file; x++)
{
fieldBuffer[2*x ] = (src[x>>sx]>>0) & 0xff;
fieldBuffer[2*x+1] = (src[x>>sx]>>8) & 0xff;
}
}
}
else
{
// eg file is 422, src is 444.
const UInt sx=csx_file-csx_src;
if (!is16bit)
{
for (UInt x = 0; x < width_file; x++)
{
fieldBuffer[x] = (UChar)(src[x<<sx]);
}
}
else
{
for (UInt x = 0; x < width_file; x++)
{
fieldBuffer[2*x ] = (src[x<<sx]>>0) & 0xff;
fieldBuffer[2*x+1] = (src[x<<sx]>>8) & 0xff;
}
}
}
}
fd.write(reinterpret_cast<const TChar*>(buf), (stride_file * 2));
if (fd.eof() || fd.fail() )
{
return false;
}
}
if ((y444&mask_y_src)==0)
{
top += stride_src;
bottom += stride_src;
}
}
}
return true;
}
/**
* Read one Y'CbCr frame, performing any required input scaling to change
* from the bitdepth of the input file to the internal bit-depth.
*
* If a bit-depth reduction is required, and internalBitdepth >= 8, then
* the input file is assumed to be ITU-R BT.601/709 compliant, and the
* resulting data is clipped to the appropriate legal range, as if the
* file had been provided at the lower-bitdepth compliant to Rec601/709.
*
* @param pPicYuvUser input picture YUV buffer class pointer
* @param pPicYuvTrueOrg
* @param ipcsc
* @param aiPad source padding size, aiPad[0] = horizontal, aiPad[1] = vertical
* @param format chroma format
* @return true for success, false in case of error
*/
Bool TVideoIOYuv::read ( TComPicYuv* pPicYuvUser, TComPicYuv* pPicYuvTrueOrg, const InputColourSpaceConversion ipcsc, Int aiPad[2], ChromaFormat format, const Bool bClipToRec709 )
{
// check end-of-file
if ( isEof() )
{
return false;
}
TComPicYuv *pPicYuv=pPicYuvTrueOrg;
if (format>=NUM_CHROMA_FORMAT)
{
format=pPicYuv->getChromaFormat();
}
Bool is16bit = false;
for(UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
{
if (m_fileBitdepth[ch] > 8)
{
is16bit=true;
}
}
const UInt stride444 = pPicYuv->getStride(COMPONENT_Y);
// compute actual YUV width & height excluding padding size
const UInt pad_h444 = aiPad[0];
const UInt pad_v444 = aiPad[1];
const UInt width_full444 = pPicYuv->getWidth(COMPONENT_Y);
const UInt height_full444 = pPicYuv->getHeight(COMPONENT_Y);
const UInt width444 = width_full444 - pad_h444;
const UInt height444 = height_full444 - pad_v444;
for(UInt comp=0; comp<MAX_NUM_COMPONENT; comp++)
{
const ComponentID compID = ComponentID(comp);
const ChannelType chType=toChannelType(compID);
const Int desired_bitdepth = m_MSBExtendedBitDepth[chType] + m_bitdepthShift[chType];
const Bool b709Compliance=(bClipToRec709) && (m_bitdepthShift[chType] < 0 && desired_bitdepth >= 8); /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
const Pel minval = b709Compliance? (( 1 << (desired_bitdepth - 8)) ) : 0;
const Pel maxval = b709Compliance? ((0xff << (desired_bitdepth - 8)) -1) : (1 << desired_bitdepth) - 1;
if (! readPlane(pPicYuv->getAddr(compID), m_cHandle, is16bit, stride444, width444, height444, pad_h444, pad_v444, compID, pPicYuv->getChromaFormat(), format, m_fileBitdepth[chType]))
{
return false;
}
if (compID < pPicYuv->getNumberValidComponents() )
{
const UInt csx=getComponentScaleX(compID, pPicYuv->getChromaFormat());
const UInt csy=getComponentScaleY(compID, pPicYuv->getChromaFormat());
scalePlane(pPicYuv->getAddr(compID), stride444>>csx, width_full444>>csx, height_full444>>csy, m_bitdepthShift[chType], minval, maxval);
}
}
if(pPicYuvUser)
{
ColourSpaceConvert(*pPicYuvTrueOrg, *pPicYuvUser, ipcsc, true);
}
return true;
}
/**
* Write one Y'CbCr frame. No bit-depth conversion is performed, pcPicYuv is
* assumed to be at TVideoIO::m_fileBitdepth depth.
*
* @param pPicYuvUser input picture YUV buffer class pointer
* @param ipCSC
* @param confLeft conformance window left border
* @param confRight conformance window right border
* @param confTop conformance window top border
* @param confBottom conformance window bottom border
* @param format chroma format
* @return true for success, false in case of error
*/
Bool TVideoIOYuv::write( TComPicYuv* pPicYuvUser, const InputColourSpaceConversion ipCSC, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format, const Bool bClipToRec709 )
{
TComPicYuv cPicYuvCSCd;
if (ipCSC!=IPCOLOURSPACE_UNCHANGED)
{
cPicYuvCSCd.createWithoutCUInfo(pPicYuvUser->getWidth(COMPONENT_Y), pPicYuvUser->getHeight(COMPONENT_Y), pPicYuvUser->getChromaFormat() );
ColourSpaceConvert(*pPicYuvUser, cPicYuvCSCd, ipCSC, false);
}
TComPicYuv *pPicYuv=(ipCSC==IPCOLOURSPACE_UNCHANGED) ? pPicYuvUser : &cPicYuvCSCd;
// compute actual YUV frame size excluding padding size
Bool is16bit = false;
Bool nonZeroBitDepthShift=false;
for(UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
{
if (m_fileBitdepth[ch] > 8)
{
is16bit=true;
}
if (m_bitdepthShift[ch] != 0)
{
nonZeroBitDepthShift=true;
}
}
TComPicYuv *dstPicYuv = NULL;
Bool retval = true;
if (format>=NUM_CHROMA_FORMAT)
{
format=pPicYuv->getChromaFormat();
}
if (nonZeroBitDepthShift)
{
dstPicYuv = new TComPicYuv;
dstPicYuv->createWithoutCUInfo( pPicYuv->getWidth(COMPONENT_Y), pPicYuv->getHeight(COMPONENT_Y), pPicYuv->getChromaFormat() );
for(UInt comp=0; comp<dstPicYuv->getNumberValidComponents(); comp++)
{
const ComponentID compID=ComponentID(comp);
const ChannelType ch=toChannelType(compID);
const Bool b709Compliance = bClipToRec709 && (-m_bitdepthShift[ch] < 0 && m_MSBExtendedBitDepth[ch] >= 8); /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
const Pel minval = b709Compliance? (( 1 << (m_MSBExtendedBitDepth[ch] - 8)) ) : 0;
const Pel maxval = b709Compliance? ((0xff << (m_MSBExtendedBitDepth[ch] - 8)) -1) : (1 << m_MSBExtendedBitDepth[ch]) - 1;
copyPlane(*pPicYuv, compID, *dstPicYuv, compID);
scalePlane(dstPicYuv->getAddr(compID), dstPicYuv->getStride(compID), dstPicYuv->getWidth(compID), dstPicYuv->getHeight(compID), -m_bitdepthShift[ch], minval, maxval);
}
}
else
{
dstPicYuv = pPicYuv;
}
const Int stride444 = dstPicYuv->getStride(COMPONENT_Y);
const UInt width444 = dstPicYuv->getWidth(COMPONENT_Y) - confLeft - confRight;
const UInt height444 = dstPicYuv->getHeight(COMPONENT_Y) - confTop - confBottom;
if ((width444 == 0) || (height444 == 0))
{
printf ("\nWarning: writing %d x %d luma sample output picture!", width444, height444);
}
for(UInt comp=0; retval && comp<dstPicYuv->getNumberValidComponents(); comp++)
{
const ComponentID compID = ComponentID(comp);
const ChannelType ch=toChannelType(compID);
const UInt csx = dstPicYuv->getComponentScaleX(compID);
const UInt csy = dstPicYuv->getComponentScaleY(compID);
const Int planeOffset = (confLeft>>csx) + (confTop>>csy) * dstPicYuv->getStride(compID);
if (! writePlane(m_cHandle, dstPicYuv->getAddr(compID) + planeOffset, is16bit, stride444, width444, height444, compID, dstPicYuv->getChromaFormat(), format, m_fileBitdepth[ch]))
{
retval=false;
}
}
if (nonZeroBitDepthShift)
{
dstPicYuv->destroy();
delete dstPicYuv;
}
cPicYuvCSCd.destroy();
return retval;
}
Bool TVideoIOYuv::write( TComPicYuv* pPicYuvUserTop, TComPicYuv* pPicYuvUserBottom, const InputColourSpaceConversion ipCSC, Int confLeft, Int confRight, Int confTop, Int confBottom, ChromaFormat format, const Bool isTff, const Bool bClipToRec709 )
{
TComPicYuv cPicYuvTopCSCd;
TComPicYuv cPicYuvBottomCSCd;
if (ipCSC!=IPCOLOURSPACE_UNCHANGED)
{
cPicYuvTopCSCd .createWithoutCUInfo(pPicYuvUserTop ->getWidth(COMPONENT_Y), pPicYuvUserTop ->getHeight(COMPONENT_Y), pPicYuvUserTop ->getChromaFormat() );
cPicYuvBottomCSCd.createWithoutCUInfo(pPicYuvUserBottom->getWidth(COMPONENT_Y), pPicYuvUserBottom->getHeight(COMPONENT_Y), pPicYuvUserBottom->getChromaFormat() );
ColourSpaceConvert(*pPicYuvUserTop, cPicYuvTopCSCd, ipCSC, false);
ColourSpaceConvert(*pPicYuvUserBottom, cPicYuvBottomCSCd, ipCSC, false);
}
TComPicYuv *pPicYuvTop = (ipCSC==IPCOLOURSPACE_UNCHANGED) ? pPicYuvUserTop : &cPicYuvTopCSCd;
TComPicYuv *pPicYuvBottom = (ipCSC==IPCOLOURSPACE_UNCHANGED) ? pPicYuvUserBottom : &cPicYuvBottomCSCd;
Bool is16bit = false;
Bool nonZeroBitDepthShift=false;
for(UInt ch=0; ch<MAX_NUM_CHANNEL_TYPE; ch++)
{
if (m_fileBitdepth[ch] > 8)
{
is16bit=true;
}
if (m_bitdepthShift[ch] != 0)
{
nonZeroBitDepthShift=true;
}
}
TComPicYuv *dstPicYuvTop = NULL;
TComPicYuv *dstPicYuvBottom = NULL;
for (UInt field = 0; field < 2; field++)
{
TComPicYuv *pPicYuv = (field == 0) ? pPicYuvTop : pPicYuvBottom;
if (format>=NUM_CHROMA_FORMAT)
{
format=pPicYuv->getChromaFormat();
}
TComPicYuv* &dstPicYuv = (field == 0) ? dstPicYuvTop : dstPicYuvBottom;
if (nonZeroBitDepthShift)
{
dstPicYuv = new TComPicYuv;
dstPicYuv->createWithoutCUInfo( pPicYuv->getWidth(COMPONENT_Y), pPicYuv->getHeight(COMPONENT_Y), pPicYuv->getChromaFormat() );
for(UInt comp=0; comp<dstPicYuv->getNumberValidComponents(); comp++)
{
const ComponentID compID=ComponentID(comp);
const ChannelType ch=toChannelType(compID);
const Bool b709Compliance=bClipToRec709 && (-m_bitdepthShift[ch] < 0 && m_MSBExtendedBitDepth[ch] >= 8); /* ITU-R BT.709 compliant clipping for converting say 10b to 8b */
const Pel minval = b709Compliance? (( 1 << (m_MSBExtendedBitDepth[ch] - 8)) ) : 0;
const Pel maxval = b709Compliance? ((0xff << (m_MSBExtendedBitDepth[ch] - 8)) -1) : (1 << m_MSBExtendedBitDepth[ch]) - 1;
copyPlane(*pPicYuv, compID, *dstPicYuv, compID);
scalePlane(dstPicYuv->getAddr(compID), dstPicYuv->getStride(compID), dstPicYuv->getWidth(compID), dstPicYuv->getHeight(compID), -m_bitdepthShift[ch], minval, maxval);
}
}
else
{
dstPicYuv = pPicYuv;
}
}
Bool retval = true;
assert(dstPicYuvTop->getNumberValidComponents() == dstPicYuvBottom->getNumberValidComponents());
assert(dstPicYuvTop->getChromaFormat() == dstPicYuvBottom->getChromaFormat() );
for(UInt comp=0; retval && comp<dstPicYuvTop->getNumberValidComponents(); comp++)
{
const ComponentID compID = ComponentID(comp);
const ChannelType ch=toChannelType(compID);
assert(dstPicYuvTop->getWidth (compID) == dstPicYuvBottom->getWidth (compID));
assert(dstPicYuvTop->getHeight (compID) == dstPicYuvBottom->getHeight (compID));
assert(dstPicYuvTop->getComponentScaleX(compID) == dstPicYuvBottom->getComponentScaleX(compID));
assert(dstPicYuvTop->getComponentScaleY(compID) == dstPicYuvBottom->getComponentScaleY(compID));
assert(dstPicYuvTop->getStride (compID) == dstPicYuvBottom->getStride (compID));
const UInt width444 = dstPicYuvTop->getWidth(COMPONENT_Y) - (confLeft + confRight);
const UInt height444 = dstPicYuvTop->getHeight(COMPONENT_Y) - (confTop + confBottom);
if ((width444 == 0) || (height444 == 0))
{
printf ("\nWarning: writing %d x %d luma sample output picture!", width444, height444);
}
const UInt csx = dstPicYuvTop->getComponentScaleX(compID);
const UInt csy = dstPicYuvTop->getComponentScaleY(compID);
const Int planeOffset = (confLeft>>csx) + ( confTop>>csy) * dstPicYuvTop->getStride(compID); //offset is for entire frame - round up for top field and down for bottom field
if (! writeField(m_cHandle,
(dstPicYuvTop ->getAddr(compID) + planeOffset),
(dstPicYuvBottom->getAddr(compID) + planeOffset),
is16bit,
dstPicYuvTop->getStride(COMPONENT_Y),
width444, height444, compID, dstPicYuvTop->getChromaFormat(), format, m_fileBitdepth[ch], isTff))
{
retval=false;
}
}
if (nonZeroBitDepthShift)
{
dstPicYuvTop->destroy();
dstPicYuvBottom->destroy();
delete dstPicYuvTop;
delete dstPicYuvBottom;
}
cPicYuvTopCSCd.destroy();
cPicYuvBottomCSCd.destroy();
return retval;
}
static Void
copyPlane(const TComPicYuv &src, const ComponentID srcPlane, TComPicYuv &dest, const ComponentID destPlane)
{
const UInt width=src.getWidth(srcPlane);
const UInt height=src.getHeight(srcPlane);
assert(dest.getWidth(destPlane) == width);
assert(dest.getHeight(destPlane) == height);
const Pel *pSrc=src.getAddr(srcPlane);
Pel *pDest=dest.getAddr(destPlane);
const UInt strideSrc=src.getStride(srcPlane);
const UInt strideDest=dest.getStride(destPlane);
for(UInt y=0; y<height; y++, pSrc+=strideSrc, pDest+=strideDest)
{
memcpy(pDest, pSrc, width*sizeof(Pel));
}
}
// static member
Void TVideoIOYuv::ColourSpaceConvert(const TComPicYuv &src, TComPicYuv &dest, const InputColourSpaceConversion conversion, Bool bIsForwards)
{
const ChromaFormat format=src.getChromaFormat();
const UInt numValidComp=src.getNumberValidComponents();
switch (conversion)
{
case IPCOLOURSPACE_YCbCrtoYYY:
if (format!=CHROMA_444)
{
// only 444 is handled.
assert(format==CHROMA_444);
exit(1);
}
{
for(UInt comp=0; comp<numValidComp; comp++)
{
copyPlane(src, ComponentID(bIsForwards?0:comp), dest, ComponentID(comp));
}
}
break;
case IPCOLOURSPACE_YCbCrtoYCrCb:
{
for(UInt comp=0; comp<numValidComp; comp++)
{
copyPlane(src, ComponentID(comp), dest, ComponentID((numValidComp-comp)%numValidComp));
}
}
break;
case IPCOLOURSPACE_RGBtoGBR:
{
if (format!=CHROMA_444)
{
// only 444 is handled.
assert(format==CHROMA_444);
exit(1);
}
// channel re-mapping
for(UInt comp=0; comp<numValidComp; comp++)
{
const ComponentID compIDsrc=ComponentID((comp+1)%numValidComp);
const ComponentID compIDdst=ComponentID(comp);
copyPlane(src, bIsForwards?compIDsrc:compIDdst, dest, bIsForwards?compIDdst:compIDsrc);
}
}
break;
case IPCOLOURSPACE_UNCHANGED:
default:
{
for(UInt comp=0; comp<numValidComp; comp++)
{
copyPlane(src, ComponentID(comp), dest, ComponentID(comp));
}
}
break;
}
}
|