Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/apputils/FileIOHelper.h
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
/** \file     FileIOHelper.h
43
    \brief    file I/O helper class (header)
44
*/
45
46
#pragma once
47
48
#include <cstdio>
49
#include <iostream>
50
#include <fstream>
51
#include <sstream>
52
#include <string>
53
#include <vector>
54
#include <algorithm>
55
#include <regex>
56
57
#include "vvenc/vvencCfg.h"
58
#include "vvenc/vvenc.h"
59
60
61
#if defined (_WIN32) || defined (WIN32) || defined (_WIN64) || defined (WIN64)
62
#include <io.h>
63
#include <fcntl.h>
64
#endif
65
66
//! \ingroup Interface
67
//! \{
68
69
struct vvencYUVBuffer;
70
71
namespace apputils {
72
73
typedef int16_t LPel;
74
75
// Standard input for the y4m and yuv readers, read in blocks of 64 KiB. libc++'s std::cin reads a pipe one byte at a
76
// time (a getc with its lock per byte, about 60 ms per 1080p picture), so every read of stdin goes through this one
77
// stream instead: bytes it has buffered are no longer in stdin, so no reader may use std::cin beside it.
78
class StdinBuf : public std::streambuf
79
{
80
public:
81
0
  StdinBuf() { setg( m_buf, m_buf, m_buf ); }
82
83
protected:
84
  int_type underflow() override
85
0
  {
86
0
    if( gptr() < egptr() )
87
0
    {
88
0
      return traits_type::to_int_type( *gptr() );
89
0
    }
90
0
    const size_t numRead = std::fread( m_buf, 1, sizeof( m_buf ), stdin );
91
0
    if( numRead == 0 )
92
0
    {
93
0
      return traits_type::eof();
94
0
    }
95
0
    setg( m_buf, m_buf, m_buf + numRead );
96
0
    return traits_type::to_int_type( *gptr() );
97
0
  }
98
99
private:
100
  char m_buf[1 << 16];
101
};
102
103
// inline (not static): one buffer for the whole program, whichever file calls it
104
inline std::istream& stdinStream()
105
0
{
106
0
  static StdinBuf     buf;
107
0
  static std::istream stream( &buf );
108
0
  return stream;
109
0
}
110
111
// ====================================================================================================================
112
113
class FileIOHelper
114
{
115
public:
116
117
  static std::string getFileExtension( std::string fileName )
118
0
  {
119
0
    std::string ext;
120
0
    if(fileName.find_last_of(".") != std::string::npos)
121
0
    {
122
0
      ext = fileName.substr(fileName.find_last_of(".")+1);
123
0
      std::transform( ext.begin(), ext.end(), ext.begin(), ::tolower );
124
0
    }
125
0
    return ext;
126
0
  }
127
128
129
0
  static bool isY4mInputFilename( std::string fileName ) {  return ( "y4m" == getFileExtension(fileName) ); }
130
  static bool isY4mHeaderAvailable( std::string fileName )
131
0
  {
132
0
    if( fileName == "-" ) return false;
133
0
    std::fstream cfHandle;
134
0
    cfHandle.open( fileName, std::ios::binary | std::ios::in );
135
0
    if( cfHandle.fail() ) return false;
136
137
0
    char line[10] = {0};
138
0
    std::streamsize headerlinesize = cfHandle.readsome( line, 10 );
139
0
    cfHandle.close();
140
141
0
    if( headerlinesize && memcmp( line, "YUV4MPEG2", 9 ) == 0 ) return true;
142
143
0
    return false;
144
0
  }
145
146
  static int parseY4mHeader( const std::string &fileName, vvenc_config& cfg, vvencChromaFormat& inputFileChromaFormat )
147
0
  {
148
0
    std::fstream cfHandle;
149
150
0
    if( fileName == "-" )
151
0
    {
152
  #if defined (_WIN32) || defined (WIN32) || defined (_WIN64) || defined (WIN64)
153
      if( _setmode( _fileno( stdin ), _O_BINARY ) == -1 )
154
      {
155
        return -1;
156
      }
157
  #endif
158
0
    }
159
0
    else
160
0
    {
161
0
      cfHandle.open( fileName, std::ios::binary | std::ios::in );
162
0
      if( cfHandle.fail() )
163
0
      {
164
0
        return -1;
165
0
      }
166
0
    }
167
168
0
    std::istream& inStream = ( fileName == "-" ) ? stdinStream() : cfHandle;
169
170
    // y4m header syntax example
171
    // YUV4MPEG2 W1920 H1080 F50:1 Ip A128:117 C420p10
172
173
    // init y4m defaults (if not given in header)
174
0
    cfg.m_inputBitDepth[0]  = 8;
175
0
    inputFileChromaFormat = VVENC_CHROMA_420;
176
177
0
    std::string headerline;
178
0
    getline(inStream, headerline);
179
0
    if( headerline.empty() ){ return -1; }
180
0
    std::transform( headerline.begin(), headerline.end(), headerline.begin(), ::toupper );
181
182
0
    std::regex reg("\\s+"); // tokenize at spaces
183
0
    std::sregex_token_iterator iter(headerline.begin(), headerline.end(), reg, -1);
184
0
    std::sregex_token_iterator end;
185
0
    std::vector<std::string> vec(iter, end);
186
187
0
    bool valid=false;
188
0
    int  infosFound = 0;
189
0
    for (auto &p : vec)
190
0
    {
191
0
      if( p == "YUV4MPEG2" ) // read file signature
192
0
      { valid = true; }
193
0
      else if( p[0] == 'W' ) // width
194
0
      {
195
0
        cfg.m_SourceWidth = atoi( p.substr( 1 ).c_str());
196
0
        infosFound++;
197
0
      }
198
0
      else if( p[0] == 'H' ) // height
199
0
      {
200
0
        cfg.m_SourceHeight = atoi( p.substr( 1 ).c_str());
201
0
        infosFound++;
202
0
      }
203
0
      else if( p[0] == 'F' )  // framerate,scale
204
0
      {
205
0
        size_t sep = p.find(":");
206
0
        if( sep == std::string::npos ) return -1;
207
0
        cfg.m_FrameRate  = atoi( p.substr( 1, sep-1 ).c_str());
208
0
        cfg.m_FrameScale = atoi( p.substr( sep+1 ).c_str());
209
0
        infosFound++;
210
0
      }
211
0
      else if( p[0] == 'A' ) // aspect ratio
212
0
      {
213
0
        size_t sep = p.find(":");
214
0
        if( sep == std::string::npos ) return -1;
215
0
        cfg.m_sarWidth  = atoi( p.substr( 1, sep-1 ).c_str());
216
0
        cfg.m_sarHeight = atoi( p.substr( sep+1 ).c_str());
217
0
      }
218
0
      else if( p[0] == 'C' ) // colorspace ( e.g. C420p10)
219
0
      {
220
0
        std::vector<std::string> ignores = {"JPEG", "MPEG2", "PALVD" }; // ignore some special cases
221
0
        for( auto &i : ignores )
222
0
        {
223
0
          auto n = p.find( i );
224
0
          if (n != std::string::npos) p.erase(n, i.length()); // remove from param string (e.g. 420PALVD)
225
0
        }
226
227
0
        size_t sep = p.find("P");
228
0
        std::string chromatype;
229
0
        if( sep != std::string::npos )
230
0
        {
231
0
          chromatype = ( p.substr( 1, sep-1 ).c_str());
232
0
          cfg.m_inputBitDepth[0] = atoi( p.substr( sep+1 ).c_str());
233
0
        }
234
0
        else
235
0
        {
236
0
          sep = p.find("CMONO");
237
0
          if( sep != std::string::npos )
238
0
          {
239
0
            chromatype = "400";
240
0
            if( p == "CMONO") cfg.m_inputBitDepth[0] = 8;
241
0
            else cfg.m_inputBitDepth[0] = atoi( p.substr( sep+5 ).c_str()); // e.g. mono10
242
0
          }
243
0
          else
244
0
          {
245
0
            chromatype = ( p.substr( 1 ).c_str());
246
0
            cfg.m_inputBitDepth[0] = 8;
247
0
          }
248
0
        }
249
250
0
        if( chromatype == "400" )      { inputFileChromaFormat =  VVENC_CHROMA_400; }
251
0
        else if( chromatype == "420" ) { inputFileChromaFormat =  VVENC_CHROMA_420; }
252
0
        else if( chromatype == "422" ) { inputFileChromaFormat =  VVENC_CHROMA_422; }
253
0
        else if( chromatype == "444" ) { inputFileChromaFormat =  VVENC_CHROMA_444; }
254
0
        else { return -1; } // unsupported chroma foramt}
255
0
      }
256
0
      else if( p[0] == 'I' ) // interlaced format (ignore it, because we cannot set it in any params
257
0
      {}
258
0
      else if( p[0] == 'X' ) // ignore comments
259
0
      {}
260
0
    }
261
262
0
    if( fileName != "-" )
263
0
    {
264
0
      cfHandle.close();
265
0
    }
266
    
267
0
    if ( infosFound < 3 )
268
0
    {
269
0
      valid = false;  
270
0
    }
271
272
0
    if( !valid ) return -1;
273
274
0
    return (int)headerline.length()+1;
275
0
  }
276
277
278
  static bool checkInputFile( std::string fileName, std::string& rcErrText )
279
0
  {
280
0
    if( fileName == "-" ) return true;
281
282
0
    std::stringstream css;
283
0
    std::fstream cfHandle;
284
0
    cfHandle.open( fileName, std::ios::binary | std::ios::in );
285
0
    if( cfHandle.fail() )
286
0
    {
287
0
      css <<"cannot open input file " << fileName;
288
0
      rcErrText = css.str();
289
0
      return false;
290
0
    }
291
292
0
    cfHandle.seekg( 0, std::ios::end );
293
0
    if ( 0 >= cfHandle.tellg() )
294
0
    {
295
0
      css <<"cannot determine valid input file size of file" << fileName;
296
0
      rcErrText = css.str();
297
0
      return false;
298
0
    }
299
0
    cfHandle.close();
300
301
0
    std::string ext = getFileExtension(fileName);
302
0
    std::vector<std::string> unsupportedList { "mp4", "mkv", "ts", "mpeg", "avi" } ;
303
0
    for (auto &e : unsupportedList)
304
0
    {
305
0
      if( e == ext )
306
0
      {
307
0
        css <<"unsupported input file format " << e << " detected. support only for RAW/YUV/Y4M input files.\n";
308
0
        rcErrText = css.str();
309
0
        return false;
310
0
      }
311
0
    }
312
0
    return true;
313
0
  }
314
315
  static bool checkBitstreamFile( std::string fileName, std::string& rcErrText )
316
0
  {
317
0
    if( fileName == "-" ) return true;
318
0
    std::stringstream css;
319
0
    std::string ext = getFileExtension(fileName);
320
0
    std::vector<std::string> unsupportedList { "mp4", "mkv", "ts", "mpeg", "avi" } ;
321
0
    for (auto &e : unsupportedList)
322
0
    {
323
0
      if( e == ext )
324
0
      {
325
0
        css <<"unsupported output file format detected. no support for " << e << " container. RAW ES (e.g. *.vvc, *.266) support only.\n" << fileName;
326
0
        rcErrText = css.str();
327
0
        return false;
328
0
      }
329
0
    }
330
331
0
    return true;
332
0
  }
333
  
334
  static bool verifyYuvPlane( vvencYUVPlane& yuvPlane, const int bitDepth )
335
0
  {
336
0
    const int stride = yuvPlane.stride;
337
0
    const int width  = yuvPlane.width;
338
0
    const int height = yuvPlane.height;
339
0
    LPel* dst        = yuvPlane.ptr;
340
0
  
341
0
    const LPel mask = ~( ( 1 << bitDepth ) - 1 );
342
0
  
343
0
    for ( int y = 0; y < height; y++, dst += stride )
344
0
    {
345
0
      for ( int x = 0; x < width; x++ )
346
0
      {
347
0
        if ( ( dst[ x ] & mask ) != 0 )
348
0
        {
349
0
          return false;
350
0
        }
351
0
      }
352
0
    }
353
0
  
354
0
    return true;
355
0
  }
356
  
357
  static bool readYuvPlane( std::istream&     fd,
358
                     vvencYUVPlane&           yuvPlane,
359
                     bool                     is16bit,
360
                     int                      fileBitDepth,
361
                     int                      packedYUVInput,
362
                     const int&               compID,
363
                     const vvencChromaFormat& inputChFmt,
364
                     const vvencChromaFormat& internChFmt
365
                   )
366
0
  {
367
0
    const int csx_file = ( (compID == 0) || (inputChFmt==VVENC_CHROMA_444) ) ? 0 : 1;
368
0
    const int csy_file = ( (compID == 0) || (inputChFmt!=VVENC_CHROMA_420) ) ? 0 : 1;
369
0
    const int csx_dest = ( (compID == 0) || (internChFmt==VVENC_CHROMA_444) ) ? 0 : 1;
370
0
    const int csy_dest = ( (compID == 0) || (internChFmt!=VVENC_CHROMA_420) ) ? 0 : 1;
371
0
  
372
0
    const int stride  = yuvPlane.stride;
373
0
    const int width   = yuvPlane.width;
374
0
    const int height  = yuvPlane.height;
375
0
    LPel* dst         = yuvPlane.ptr;
376
0
  
377
0
    const int fileStride = ( ( width  << csx_dest ) * ( is16bit ? 2 : 1 ) ) >> csx_file;
378
0
    const int fileHeight = ( ( height << csy_dest )                       ) >> csy_file;
379
0
  
380
0
    if ( compID != 0 && ( inputChFmt == VVENC_CHROMA_400 || internChFmt == VVENC_CHROMA_400 ) )
381
0
    {
382
0
      if ( internChFmt != VVENC_CHROMA_400 )
383
0
      {
384
0
        // set chrominance data to mid-range: (1<<(fileBitDepth-1))
385
0
        const LPel val = 1 << ( fileBitDepth - 1 );
386
0
        for (int y = 0; y < height; y++, dst+= stride)
387
0
        {
388
0
          for (int x = 0; x < width; x++)
389
0
          {
390
0
            dst[x] = val;
391
0
          }
392
0
        }
393
0
      }
394
0
  
395
0
      if ( inputChFmt != VVENC_CHROMA_400 )
396
0
      {
397
0
        fd.seekg( fileHeight * fileStride, std::ios::cur );
398
0
        if ( fd.eof() || fd.fail() )
399
0
        {
400
0
          return false;
401
0
        }
402
0
      }
403
0
    }
404
0
    else if ( packedYUVInput )
405
0
    {
406
0
      const int fileStride_packed = ( width * 5 / 4 );
407
0
      std::vector<uint8_t> bufVec( fileStride_packed );
408
0
  
409
0
      for( int y = 0; y < height; y++ )
410
0
      {
411
0
        uint8_t *buf = &( bufVec[0] );
412
0
  
413
0
        // read a new line
414
0
        fd.read( reinterpret_cast<char*>( buf ), fileStride_packed );
415
0
        if ( fd.eof() || fd.fail() )
416
0
        {
417
0
          return false;
418
0
        }
419
0
  
420
0
        for ( int x = 0; x < width; x += 4 )
421
0
        {
422
0
          int64_t iTemp = 0;
423
0
          unsigned char* pucTemp = reinterpret_cast< unsigned char* >( &iTemp );
424
0
          pucTemp[0] = buf[0];
425
0
          pucTemp[1] = buf[1];
426
0
          pucTemp[2] = buf[2];
427
0
          pucTemp[3] = buf[3];
428
0
          pucTemp[4] = buf[4];
429
0
  
430
0
          dst[x+0] = 0x03ff & (iTemp>>0);
431
0
          dst[x+1] = 0x03ff & (iTemp>>10);
432
0
          dst[x+2] = 0x03ff & (iTemp>>20);
433
0
          dst[x+3] = 0x03ff & (iTemp>>30);
434
0
          buf += 5;
435
0
        }
436
0
        dst += stride;
437
0
      }
438
0
    }
439
0
    else
440
0
    {
441
0
      std::vector<uint8_t> bufVec( fileStride );
442
0
      uint8_t *buf = &( bufVec[0] );
443
0
      const unsigned mask_y_file = ( 1 << csy_file ) - 1;
444
0
      const unsigned mask_y_dest = ( 1 << csy_dest ) - 1;
445
0
      for( int y444 = 0; y444 < ( height << csy_dest ); y444++ )
446
0
      {
447
0
        if ( ( y444 & mask_y_file ) == 0 )
448
0
        {
449
0
          // read a new line
450
0
          fd.read( reinterpret_cast<char*>( buf ), fileStride );
451
0
          if ( fd.eof() || fd.fail() )
452
0
          {
453
0
            return false;
454
0
          }
455
0
        }
456
0
  
457
0
        if ( ( y444 & mask_y_dest ) == 0 )
458
0
        {
459
0
          // process current destination line
460
0
          if ( csx_file == csx_dest )
461
0
          {
462
0
            // the same horizontal subsampling in file and buffer (every plane of VVenC's 4:2:0 and 4:0:0 buffers): an
463
0
            // index without a shift lets the compiler vectorize the widening, which the loops below, with a shift
464
0
            // known only at run time, prevent
465
0
            if ( ! is16bit )
466
0
            {
467
0
              for ( int x = 0; x < width; x++ )
468
0
              {
469
0
                dst[ x ] = buf[ x ];
470
0
              }
471
0
            }
472
0
            else
473
0
            {
474
0
              for ( int x = 0; x < width; x++ )
475
0
              {
476
0
                dst[ x ] = LPel( buf[ x * 2 + 0 ] ) | ( LPel( buf[ x * 2 + 1 ] ) << 8 );
477
0
              }
478
0
            }
479
0
          }
480
0
          else if ( csx_file < csx_dest )
481
0
          {
482
0
            // eg file is 444, dest is 422.
483
0
            const int sx = csx_dest - csx_file;
484
0
            if ( ! is16bit )
485
0
            {
486
0
              for ( int x = 0; x < width; x++ )
487
0
              {
488
0
                dst[ x ] = buf[ x << sx ];
489
0
              }
490
0
            }
491
0
            else
492
0
            {
493
0
              for ( int x = 0; x < width; x++ )
494
0
              {
495
0
                dst[ x ] = LPel( buf[ ( x << sx ) * 2 + 0 ] ) | ( LPel ( buf[ ( x << sx ) * 2 + 1 ] ) << 8 );
496
0
              }
497
0
            }
498
0
          }
499
0
          else
500
0
          {
501
0
            // eg file is 422, dest is 444.
502
0
            const int sx = csx_file - csx_dest;
503
0
            if ( ! is16bit )
504
0
            {
505
0
              for ( int x = 0; x < width; x++ )
506
0
              {
507
0
                dst[ x ] = buf[ x >> sx ];
508
0
              }
509
0
            }
510
0
            else
511
0
            {
512
0
              for ( int x = 0; x < width; x++ )
513
0
              {
514
0
                dst[ x ] = LPel( buf[ ( x >> sx ) * 2 + 0 ] ) | ( LPel( buf[ ( x >> sx ) * 2 + 1 ] ) << 8 );
515
0
              }
516
0
            }
517
0
          }
518
0
  
519
0
          dst += stride;
520
0
        }
521
0
      }
522
0
  
523
0
    }
524
0
    return true;
525
0
  }
526
  
527
  static bool writeYuvPlane( std::ostream&     fd,
528
                      const vvencYUVPlane&     yuvPlane,
529
                      bool                     is16bit,
530
                      int                      fileBitDepth,
531
                      int                      packedYUVOutputMode,
532
                      const int&               compID,
533
                      const vvencChromaFormat& internChFmt,
534
                      const vvencChromaFormat& outputChFmt
535
                    )
536
0
  {
537
0
    const int stride = yuvPlane.stride;
538
0
    const int width  = yuvPlane.width;
539
0
    const int height = yuvPlane.height;
540
0
    const LPel* src  = yuvPlane.ptr;
541
0
  
542
0
    const int csx_file = ( (compID == 0) || (outputChFmt==VVENC_CHROMA_444) ) ? 0 : 1;
543
0
    const int csy_file = ( (compID == 0) || (outputChFmt!=VVENC_CHROMA_420) ) ? 0 : 1;
544
0
    const int csx_src  = ( (compID == 0) || (internChFmt==VVENC_CHROMA_444) ) ? 0 : 1;
545
0
    const int csy_src  = ( (compID == 0) || (internChFmt!=VVENC_CHROMA_420) ) ? 0 : 1;
546
0
  
547
0
    const int  fileWidth  = ( width  << csx_src ) >> csx_file;
548
0
    const int  fileHeight = ( height << csy_src ) >> csy_file;
549
0
    const bool writePYUV  = ( packedYUVOutputMode > 0 ) && ( fileBitDepth == 10 || fileBitDepth == 12 ) && ( ( fileWidth & ( 1 + ( fileBitDepth & 3 ) ) ) == 0 );
550
0
    const int  fileStride = writePYUV ? ( ( width << csx_src ) * fileBitDepth ) >> ( csx_file + 3 ) : ( ( width << csx_src ) * ( is16bit ? 2 : 1 ) ) >> csx_file;
551
0
  
552
0
    std::vector<uint8_t> bufVec( fileStride );
553
0
    uint8_t *buf = &( bufVec[ 0 ] );
554
0
  
555
0
    if ( writePYUV )
556
0
    {
557
0
      const unsigned mask_y_file = (1 << csy_file) - 1;
558
0
      const unsigned mask_y_src  = (1 << csy_src ) - 1;
559
0
      const int      widthS_file = fileWidth >> (fileBitDepth == 12 ? 1 : 2);
560
0
  
561
0
      for ( int y444 = 0; y444 < ( height << csy_src ); y444++)
562
0
      {
563
0
        if ( ( y444 & mask_y_file ) == 0 )  // write a new line to file
564
0
        {
565
0
          if ( csx_file < csx_src )
566
0
          {
567
0
            // eg file is 444, source is 422.
568
0
            const int sx = csx_src - csx_file;
569
0
  
570
0
            if (fileBitDepth == 10)  // write 4 values into 5 bytes
571
0
            {
572
0
              for ( int x = 0; x < widthS_file; x++)
573
0
              {
574
0
                const unsigned src0 = src[(4*x  ) >> sx];
575
0
                const unsigned src1 = src[(4*x+1) >> sx];
576
0
                const unsigned src2 = src[(4*x+2) >> sx];
577
0
                const unsigned src3 = src[(4*x+3) >> sx];
578
0
  
579
0
                buf[5*x  ] = ((src0     ) & 0xff); // src0:76543210
580
0
                buf[5*x+1] = ((src1 << 2) & 0xfc) + ((src0 >> 8) & 0x03);
581
0
                buf[5*x+2] = ((src2 << 4) & 0xf0) + ((src1 >> 6) & 0x0f);
582
0
                buf[5*x+3] = ((src3 << 6) & 0xc0) + ((src2 >> 4) & 0x3f);
583
0
                buf[5*x+4] = ((src3 >> 2) & 0xff); // src3:98765432
584
0
              }
585
0
            }
586
0
            else if ( fileBitDepth == 12 ) //...2 values into 3 bytes
587
0
            {
588
0
              for ( int x = 0; x < widthS_file; x++ )
589
0
              {
590
0
                const unsigned src0 = src[(2*x  ) >> sx];
591
0
                const unsigned src1 = src[(2*x+1) >> sx];
592
0
  
593
0
                buf[3*x  ] = ((src0     ) & 0xff); // src0:76543210
594
0
                buf[3*x+1] = ((src1 << 4) & 0xf0) + ((src0 >> 8) & 0x0f);
595
0
                buf[3*x+2] = ((src1 >> 4) & 0xff); // src1:BA987654
596
0
              }
597
0
            }
598
0
          }
599
0
          else
600
0
          {
601
0
            // eg file is 422, source is 444.
602
0
            const int sx = csx_file - csx_src;
603
0
  
604
0
            if (fileBitDepth == 10)  // write 4 values into 5 bytes
605
0
            {
606
0
              for ( int x = 0; x < widthS_file; x++ )
607
0
              {
608
0
                const unsigned src0 = src[(4*x  ) << sx];
609
0
                const unsigned src1 = src[(4*x+1) << sx];
610
0
                const unsigned src2 = src[(4*x+2) << sx];
611
0
                const unsigned src3 = src[(4*x+3) << sx];
612
0
  
613
0
                buf[5*x  ] = ((src0     ) & 0xff); // src0:76543210
614
0
                buf[5*x+1] = ((src1 << 2) & 0xfc) + ((src0 >> 8) & 0x03);
615
0
                buf[5*x+2] = ((src2 << 4) & 0xf0) + ((src1 >> 6) & 0x0f);
616
0
                buf[5*x+3] = ((src3 << 6) & 0xc0) + ((src2 >> 4) & 0x3f);
617
0
                buf[5*x+4] = ((src3 >> 2) & 0xff); // src3:98765432
618
0
              }
619
0
            }
620
0
            else if ( fileBitDepth == 12 ) //...2 values into 3 bytes
621
0
            {
622
0
              for ( int x = 0; x < widthS_file; x++ )
623
0
              {
624
0
                const unsigned src0 = src[(2*x  ) << sx];
625
0
                const unsigned src1 = src[(2*x+1) << sx];
626
0
  
627
0
                buf[3*x  ] = ((src0     ) & 0xff); // src0:76543210
628
0
                buf[3*x+1] = ((src1 << 4) & 0xf0) + ((src0 >> 8) & 0x0f);
629
0
                buf[3*x+2] = ((src1 >> 4) & 0xff); // src1:BA987654
630
0
              }
631
0
            }
632
0
          }
633
0
  
634
0
          fd.write( reinterpret_cast<const char*>( buf ), fileStride );
635
0
          if ( fd.eof() || fd.fail() )
636
0
          {
637
0
            return false;
638
0
          }
639
0
        }
640
0
  
641
0
        if ( ( y444 & mask_y_src ) == 0)
642
0
        {
643
0
          src += stride;
644
0
        }
645
0
      }
646
0
    }
647
0
    // !writePYUV
648
0
    else if ( compID != 0 && ( outputChFmt == VVENC_CHROMA_400 || internChFmt == VVENC_CHROMA_400 ) )
649
0
    {
650
0
      if ( outputChFmt != VVENC_CHROMA_400 )
651
0
      {
652
0
        const LPel value = 1 << ( fileBitDepth - 1 );
653
0
  
654
0
        for ( int y = 0; y < fileHeight; y++ )
655
0
        {
656
0
          if ( ! is16bit )
657
0
          {
658
0
            uint8_t val( value );
659
0
            for ( int x = 0; x < fileWidth; x++ )
660
0
            {
661
0
              buf[x]=val;
662
0
            }
663
0
          }
664
0
          else
665
0
          {
666
0
            uint16_t val( value );
667
0
            for ( int x = 0; x < fileWidth; x++ )
668
0
            {
669
0
              buf[2*x  ]= (val>>0) & 0xff;
670
0
              buf[2*x+1]= (val>>8) & 0xff;
671
0
            }
672
0
          }
673
0
  
674
0
          fd.write( reinterpret_cast<const char*>( buf ), fileStride );
675
0
          if ( fd.eof() || fd.fail() )
676
0
          {
677
0
            return false;
678
0
          }
679
0
        }
680
0
      }
681
0
    }
682
0
    else
683
0
    {
684
0
      const unsigned mask_y_file = (1 << csy_file) - 1;
685
0
      const unsigned mask_y_src  = (1 << csy_src ) - 1;
686
0
  
687
0
      for ( int y444 = 0; y444 < ( height << csy_src ); y444++ )
688
0
      {
689
0
        if ( ( y444 & mask_y_file ) == 0 )
690
0
        {
691
0
          // write a new line
692
0
          if ( csx_file < csx_src )
693
0
          {
694
0
            // eg file is 444, source is 422.
695
0
            const int sx = csx_src - csx_file;
696
0
            if ( ! is16bit )
697
0
            {
698
0
              for ( int x = 0; x < fileWidth; x++ )
699
0
              {
700
0
                buf[x] = (uint8_t)(src[x>>sx]);
701
0
              }
702
0
            }
703
0
            else
704
0
            {
705
0
              for ( int x = 0; x < fileWidth; x++ )
706
0
              {
707
0
                buf[2*x  ] = (src[x>>sx]>>0) & 0xff;
708
0
                buf[2*x+1] = (src[x>>sx]>>8) & 0xff;
709
0
              }
710
0
            }
711
0
          }
712
0
          else
713
0
          {
714
0
            // eg file is 422, source is 444.
715
0
            const int sx = csx_file - csx_src;
716
0
            if ( ! is16bit )
717
0
            {
718
0
              for ( int x = 0; x < fileWidth; x++ )
719
0
              {
720
0
                buf[x] = (uint8_t)(src[x<<sx]);
721
0
              }
722
0
            }
723
0
            else
724
0
            {
725
0
              for ( int x = 0; x < fileWidth; x++ )
726
0
              {
727
0
                buf[2*x  ] = (src[x<<sx]>>0) & 0xff;
728
0
                buf[2*x+1] = (src[x<<sx]>>8) & 0xff;
729
0
              }
730
0
            }
731
0
          }
732
0
  
733
0
          fd.write( reinterpret_cast<const char*>( buf ), fileStride );
734
0
          if ( fd.eof() || fd.fail() )
735
0
          {
736
0
            return false;
737
0
          }
738
0
        }
739
0
  
740
0
        if ( ( y444 & mask_y_src ) == 0 )
741
0
        {
742
0
          src += stride;
743
0
        }
744
0
      }
745
0
    }
746
0
    return true;
747
0
  }
748
749
  static void scaleYuvPlane( vvencYUVPlane& yuvPlaneOut, const vvencYUVPlane& yuvPlaneIn, 
750
                             const int shiftBits, const LPel minVal, const LPel maxVal )
751
0
  {
752
0
    const int stride = yuvPlaneOut.stride;
753
0
    const int width  = yuvPlaneOut.width;
754
0
    const int height = yuvPlaneOut.height;
755
0
    LPel* dst        = yuvPlaneOut.ptr;
756
0
    const LPel* src    = yuvPlaneIn.ptr;
757
0
    const int instride = yuvPlaneIn.stride;
758
0
  
759
0
    if( 0 == shiftBits )
760
0
    {
761
0
      return;
762
0
    }
763
0
  
764
0
    if( shiftBits > 0 )
765
0
    {
766
0
      for( int y = 0; y < height; y++, dst += stride, src += instride )
767
0
      {
768
0
        for( int x = 0; x < width; x++)
769
0
        {
770
0
          dst[x] = src[x] << shiftBits;
771
0
        }
772
0
      }
773
0
    }
774
0
    else if ( shiftBits < 0 )
775
0
    {
776
0
      const int shiftbitsr =- shiftBits;
777
0
      const LPel rounding = 1 << ( shiftbitsr-1 );
778
0
  
779
0
      for( int y = 0; y < height; y++, dst += stride, src += instride )
780
0
      {
781
0
        for( int x = 0; x < width; x++)
782
0
        {
783
0
          LPel val = ( src[ x ] + rounding ) >> shiftbitsr;
784
0
          dst[ x ] = std::max<LPel>( minVal, std::min<LPel>( maxVal, val ) );
785
0
        }
786
0
      }
787
0
    }
788
0
  }
789
};
790
791
} // namespace apputils
792
793
//! \}
794