aboutsummaryrefslogtreecommitdiff
path: root/examples/ultrahdr_app.cpp
blob: 20b9329614a281c275555ee9bf3de8539e12acd6 (plain)
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
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif

#include <string.h>

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>

#include "ultrahdr/ultrahdrcommon.h"
#include "ultrahdr/gainmapmath.h"
#include "ultrahdr/jpegr.h"

using namespace ultrahdr;

const float BT601YUVtoRGBMatrix[9] = {
    1, 0, 1.402, 1, (-0.202008 / 0.587), (-0.419198 / 0.587), 1.0, 1.772, 0.0};
const float BT709YUVtoRGBMatrix[9] = {
    1, 0, 1.5748, 1, (-0.13397432 / 0.7152), (-0.33480248 / 0.7152), 1.0, 1.8556, 0.0};
const float BT2020YUVtoRGBMatrix[9] = {
    1, 0, 1.4746, 1, (-0.11156702 / 0.6780), (-0.38737742 / 0.6780), 1, 1.8814, 0};

const float BT601RGBtoYUVMatrix[9] = {
    0.299,           0.587, 0.114, (-0.299 / 1.772), (-0.587 / 1.772), 0.5, 0.5, (-0.587 / 1.402),
    (-0.114 / 1.402)};
const float BT709RGBtoYUVMatrix[9] = {0.2126,
                                      0.7152,
                                      0.0722,
                                      (-0.2126 / 1.8556),
                                      (-0.7152 / 1.8556),
                                      0.5,
                                      0.5,
                                      (-0.7152 / 1.5748),
                                      (-0.0722 / 1.5748)};
const float BT2020RGBtoYUVMatrix[9] = {0.2627,
                                       0.6780,
                                       0.0593,
                                       (-0.2627 / 1.8814),
                                       (-0.6780 / 1.8814),
                                       0.5,
                                       0.5,
                                       (-0.6780 / 1.4746),
                                       (-0.0593 / 1.4746)};

int optind_s = 1;
int optopt_s = 0;
char* optarg_s = nullptr;

int getopt_s(int argc, char* const argv[], char* ostr) {
  if (optind_s >= argc) return -1;

  const char* arg = argv[optind_s];
  if (arg[0] != '-' || !arg[1]) {
    std::cerr << "invalid option " << arg << std::endl;
    return '?';
  }
  optopt_s = arg[1];
  char* oindex = strchr(ostr, optopt_s);
  if (!oindex) {
    std::cerr << "unsupported option " << arg << std::endl;
    return '?';
  }
  if (oindex[1] != ':') {
    optarg_s = nullptr;
    return optopt_s;
  }

  if (argc > ++optind_s) {
    optarg_s = (char*)argv[optind_s++];
  } else {
    std::cerr << "option " << arg << " requires an argument" << std::endl;
    optarg_s = nullptr;
    return '?';
  }
  return optopt_s;
}

// #define PROFILE_ENABLE 1
#ifdef _WIN32
class Profiler {
 public:
  void timerStart() { QueryPerformanceCounter(&mStartingTime); }

  void timerStop() { QueryPerformanceCounter(&mEndingTime); }

  int64_t elapsedTime() {
    LARGE_INTEGER frequency;
    LARGE_INTEGER elapsedMicroseconds;
    QueryPerformanceFrequency(&frequency);
    elapsedMicroseconds.QuadPart = mEndingTime.QuadPart - mStartingTime.QuadPart;
    return (double)elapsedMicroseconds.QuadPart / (double)frequency.QuadPart * 1000000;
  }

 private:
  LARGE_INTEGER mStartingTime;
  LARGE_INTEGER mEndingTime;
};
#else
class Profiler {
 public:
  void timerStart() { gettimeofday(&mStartingTime, nullptr); }

  void timerStop() { gettimeofday(&mEndingTime, nullptr); }

  int64_t elapsedTime() {
    struct timeval elapsedMicroseconds;
    elapsedMicroseconds.tv_sec = mEndingTime.tv_sec - mStartingTime.tv_sec;
    elapsedMicroseconds.tv_usec = mEndingTime.tv_usec - mStartingTime.tv_usec;
    return elapsedMicroseconds.tv_sec * 1000000 + elapsedMicroseconds.tv_usec;
  }

 private:
  struct timeval mStartingTime;
  struct timeval mEndingTime;
};
#endif

static bool loadFile(const char* filename, void*& result, int length) {
  std::ifstream ifd(filename, std::ios::binary | std::ios::ate);
  if (ifd.good()) {
    int size = ifd.tellg();
    if (size < length) {
      std::cerr << "requested to read " << length << " bytes from file : " << filename
                << ", file contains only " << size << " bytes" << std::endl;
      return false;
    }
    ifd.seekg(0, std::ios::beg);
    result = malloc(length);
    if (result == nullptr) {
      std::cerr << "failed to allocate memory to store contents of file : " << filename
                << std::endl;
      return false;
    }
    ifd.read(static_cast<char*>(result), length);
    return true;
  }
  std::cerr << "unable to open file : " << filename << std::endl;
  return false;
}

static bool writeFile(const char* filename, void*& result, int length) {
  std::ofstream ofd(filename, std::ios::binary);
  if (ofd.is_open()) {
    ofd.write(static_cast<char*>(result), length);
    return true;
  }
  std::cerr << "unable to write to file : " << filename << std::endl;
  return false;
}

class UltraHdrAppInput {
 public:
  UltraHdrAppInput(const char* p010File, const char* yuv420File, const char* yuv420JpegFile,
                   size_t width, size_t height,
                   ultrahdr_color_gamut p010Cg = ULTRAHDR_COLORGAMUT_BT709,
                   ultrahdr_color_gamut yuv420Cg = ULTRAHDR_COLORGAMUT_BT709,
                   ultrahdr_transfer_function tf = ULTRAHDR_TF_HLG, int quality = 100,
                   ultrahdr_output_format of = ULTRAHDR_OUTPUT_HDR_HLG)
      : mP010File(p010File),
        mYuv420File(yuv420File),
        mYuv420JpegFile(yuv420JpegFile),
        mJpegRFile(nullptr),
        mWidth(width),
        mHeight(height),
        mP010Cg(p010Cg),
        mYuv420Cg(yuv420Cg),
        mTf(tf),
        mQuality(quality),
        mOf(of),
        mMode(0){};

  UltraHdrAppInput(const char* jpegRFile, ultrahdr_output_format of = ULTRAHDR_OUTPUT_HDR_HLG)
      : mP010File(nullptr),
        mYuv420File(nullptr),
        mJpegRFile(jpegRFile),
        mWidth(0),
        mHeight(0),
        mP010Cg(ULTRAHDR_COLORGAMUT_UNSPECIFIED),
        mYuv420Cg(ULTRAHDR_COLORGAMUT_UNSPECIFIED),
        mTf(ULTRAHDR_TF_UNSPECIFIED),
        mQuality(100),
        mOf(of),
        mMode(1){};

  ~UltraHdrAppInput() {
    if (mRawP010Image.data) free(mRawP010Image.data);
    if (mRawP010Image.chroma_data) free(mRawP010Image.chroma_data);
    if (mRawRgba1010102Image.data) free(mRawRgba1010102Image.data);
    if (mRawRgba1010102Image.chroma_data) free(mRawRgba1010102Image.chroma_data);
    if (mRawYuv420Image.data) free(mRawYuv420Image.data);
    if (mRawYuv420Image.chroma_data) free(mRawYuv420Image.chroma_data);
    if (mRawRgba8888Image.data) free(mRawRgba8888Image.data);
    if (mRawRgba8888Image.chroma_data) free(mRawRgba8888Image.chroma_data);
    if (mJpegImgR.data) free(mJpegImgR.data);
    if (mDestImage.data) free(mDestImage.data);
    if (mDestImage.chroma_data) free(mDestImage.chroma_data);
    if (mDestYUV444Image.data) free(mDestYUV444Image.data);
    if (mDestYUV444Image.chroma_data) free(mDestYUV444Image.chroma_data);
  }

  bool fillJpegRImageHandle();
  bool fillP010ImageHandle();
  bool convertP010ToRGBImage();
  bool fillYuv420ImageHandle();
  bool fillYuv420JpegImageHandle();
  bool convertYuv420ToRGBImage();
  bool convertRgba8888ToYUV444Image();
  bool convertRgba1010102ToYUV444Image();
  bool encode();
  bool decode();
  void computeRGBHdrPSNR();
  void computeRGBSdrPSNR();
  void computeYUVHdrPSNR();
  void computeYUVSdrPSNR();

  const char* mP010File;
  const char* mYuv420File;
  const char* mYuv420JpegFile;
  const char* mJpegRFile;
  const int mWidth;
  const int mHeight;
  const ultrahdr_color_gamut mP010Cg;
  const ultrahdr_color_gamut mYuv420Cg;
  const ultrahdr_transfer_function mTf;
  const int mQuality;
  const ultrahdr_output_format mOf;
  const int mMode;
  jpegr_uncompressed_struct mRawP010Image{};
  jpegr_uncompressed_struct mRawRgba1010102Image{};
  jpegr_uncompressed_struct mRawYuv420Image{};
  jpegr_compressed_struct mYuv420JpegImage{};
  jpegr_uncompressed_struct mRawRgba8888Image{};
  jpegr_compressed_struct mJpegImgR{};
  jpegr_uncompressed_struct mDestImage{};
  jpegr_uncompressed_struct mDestYUV444Image{};
  double mPsnr[3]{};
};

bool UltraHdrAppInput::fillP010ImageHandle() {
  const int bpp = 2;
  int p010Size = mWidth * mHeight * bpp * 1.5;
  mRawP010Image.width = mWidth;
  mRawP010Image.height = mHeight;
  mRawP010Image.colorGamut = mP010Cg;
  return loadFile(mP010File, mRawP010Image.data, p010Size);
}

bool UltraHdrAppInput::fillYuv420ImageHandle() {
  int yuv420Size = mWidth * mHeight * 1.5;
  mRawYuv420Image.width = mWidth;
  mRawYuv420Image.height = mHeight;
  mRawYuv420Image.colorGamut = mYuv420Cg;
  return loadFile(mYuv420File, mRawYuv420Image.data, yuv420Size);
}

bool UltraHdrAppInput::fillYuv420JpegImageHandle() {
  std::ifstream ifd(mYuv420JpegFile, std::ios::binary | std::ios::ate);
  if (ifd.good()) {
    int size = ifd.tellg();
    mYuv420JpegImage.length = size;
    mYuv420JpegImage.maxLength = size;
    mYuv420JpegImage.data = nullptr;
    mYuv420JpegImage.colorGamut = mYuv420Cg;
    ifd.close();
    return loadFile(mYuv420JpegFile, mYuv420JpegImage.data, size);
  }
  return false;
}

bool UltraHdrAppInput::fillJpegRImageHandle() {
  std::ifstream ifd(mJpegRFile, std::ios::binary | std::ios::ate);
  if (ifd.good()) {
    int size = ifd.tellg();
    mJpegImgR.length = size;
    mJpegImgR.maxLength = size;
    mJpegImgR.data = nullptr;
    mJpegImgR.colorGamut = mYuv420Cg;
    ifd.close();
    return loadFile(mJpegRFile, mJpegImgR.data, size);
  }
  return false;
}

bool UltraHdrAppInput::encode() {
  if (!fillP010ImageHandle()) return false;
  if (mYuv420File != nullptr && !fillYuv420ImageHandle()) return false;
  if (mYuv420JpegFile != nullptr && !fillYuv420JpegImageHandle()) return false;

  mJpegImgR.maxLength = (std::max)(static_cast<size_t>(8 * 1024) /* min size 8kb */,
                                   mRawP010Image.width * mRawP010Image.height * 3 * 2);
  mJpegImgR.data = malloc(mJpegImgR.maxLength);
  if (mJpegImgR.data == nullptr) {
    std::cerr << "unable to allocate memory to store compressed image" << std::endl;
    return false;
  }

  JpegR jpegHdr;
  status_t status = JPEGR_UNKNOWN_ERROR;
#ifdef PROFILE_ENABLE
  const int profileCount = 10;
  Profiler profileEncode;
  profileEncode.timerStart();
  for (auto i = 0; i < profileCount; i++) {
#endif
    if (mYuv420File == nullptr && mYuv420JpegFile == nullptr) {  // api-0
      status = jpegHdr.encodeJPEGR(&mRawP010Image, mTf, &mJpegImgR, mQuality, nullptr);
      if (JPEGR_NO_ERROR != status) {
        std::cerr << "Encountered error during encodeJPEGR call, error code " << status
                  << std::endl;
        return false;
      }
    } else if (mYuv420File != nullptr && mYuv420JpegFile == nullptr) {  // api-1
      status =
          jpegHdr.encodeJPEGR(&mRawP010Image, &mRawYuv420Image, mTf, &mJpegImgR, mQuality, nullptr);
      if (JPEGR_NO_ERROR != status) {
        std::cerr << "Encountered error during encodeJPEGR call, error code " << status
                  << std::endl;
        return false;
      }
    } else if (mYuv420File != nullptr && mYuv420JpegFile != nullptr) {  // api-2
      status =
          jpegHdr.encodeJPEGR(&mRawP010Image, &mRawYuv420Image, &mYuv420JpegImage, mTf, &mJpegImgR);
      if (JPEGR_NO_ERROR != status) {
        std::cerr << "Encountered error during encodeJPEGR call, error code " << status
                  << std::endl;
        return false;
      }
    } else if (mYuv420File == nullptr && mYuv420JpegFile != nullptr) {  // api-3
      status = jpegHdr.encodeJPEGR(&mRawP010Image, &mYuv420JpegImage, mTf, &mJpegImgR);
      if (JPEGR_NO_ERROR != status) {
        std::cerr << "Encountered error during encodeJPEGR call, error code " << status
                  << std::endl;
        return false;
      }
    }
#ifdef PROFILE_ENABLE
  }
  profileEncode.timerStop();
  auto avgEncTime = profileEncode.elapsedTime() / (profileCount * 1000.f);
  printf("Average encode time for res %d x %d is %f ms \n", mWidth, mHeight, avgEncTime);
#endif
  writeFile("out.jpeg", mJpegImgR.data, mJpegImgR.length);
  return true;
}

bool UltraHdrAppInput::decode() {
  if (mMode == 1 && !fillJpegRImageHandle()) return false;
  std::vector<uint8_t> iccData(0);
  std::vector<uint8_t> exifData(0);
  jpegr_info_struct info{};
  JpegR jpegHdr;
  status_t status = jpegHdr.getJPEGRInfo(&mJpegImgR, &info);
  if (JPEGR_NO_ERROR == status) {
    size_t outSize = info.width * info.height * ((mOf == ULTRAHDR_OUTPUT_HDR_LINEAR) ? 8 : 4);
    mDestImage.data = malloc(outSize);
    if (mDestImage.data == nullptr) {
      std::cerr << "failed to allocate memory to store decoded output" << std::endl;
      return false;
    }
#ifdef PROFILE_ENABLE
    const int profileCount = 10;
    Profiler profileDecode;
    profileDecode.timerStart();
    for (auto i = 0; i < profileCount; i++) {
#endif
      status =
          jpegHdr.decodeJPEGR(&mJpegImgR, &mDestImage, FLT_MAX, nullptr, mOf, nullptr, nullptr);
      if (JPEGR_NO_ERROR != status) {
        std::cerr << "Encountered error during decodeJPEGR call, error code " << status
                  << std::endl;
        return false;
      }
#ifdef PROFILE_ENABLE
    }
    profileDecode.timerStop();
    auto avgDecTime = profileDecode.elapsedTime() / (profileCount * 1000.f);
    printf("Average decode time for res %ld x %ld is %f ms \n", info.width, info.height,
           avgDecTime);
#endif
    writeFile("outrgb.raw", mDestImage.data, outSize);
  } else {
    std::cerr << "Encountered error during getJPEGRInfo call, error code " << status << std::endl;
    return false;
  }
  return true;
}

bool UltraHdrAppInput::convertP010ToRGBImage() {
  const float* coeffs = BT2020YUVtoRGBMatrix;
  if (mP010Cg == ULTRAHDR_COLORGAMUT_BT709) {
    coeffs = BT709YUVtoRGBMatrix;
  } else if (mP010Cg == ULTRAHDR_COLORGAMUT_BT2100) {
    coeffs = BT2020YUVtoRGBMatrix;
  } else if (mP010Cg == ULTRAHDR_COLORGAMUT_P3) {
    coeffs = BT601YUVtoRGBMatrix;
  } else {
    std::cerr << "color matrix not present for gamut " << mP010Cg << " using BT2020Matrix"
              << std::endl;
  }

  mRawRgba1010102Image.data = malloc(mRawP010Image.width * mRawP010Image.height * 4);
  if (mRawRgba1010102Image.data == nullptr) {
    std::cerr << "failed to allocate memory to store Rgba1010102" << std::endl;
    return false;
  }
  mRawRgba1010102Image.width = mRawP010Image.width;
  mRawRgba1010102Image.height = mRawP010Image.height;
  mRawRgba1010102Image.colorGamut = mRawP010Image.colorGamut;
  uint32_t* rgbData = static_cast<uint32_t*>(mRawRgba1010102Image.data);
  uint16_t* y = static_cast<uint16_t*>(mRawP010Image.data);
  uint16_t* u = y + mRawP010Image.width * mRawP010Image.height;
  uint16_t* v = u + 1;

  for (size_t i = 0; i < mRawP010Image.height; i++) {
    for (size_t j = 0; j < mRawP010Image.width; j++) {
      float y0 = float(y[mRawP010Image.width * i + j] >> 6);
      float u0 = float(u[mRawP010Image.width * (i / 2) + (j / 2) * 2] >> 6);
      float v0 = float(v[mRawP010Image.width * (i / 2) + (j / 2) * 2] >> 6);

      y0 = CLIP3(y0, 64.0f, 940.0f);
      u0 = CLIP3(u0, 64.0f, 960.0f);
      v0 = CLIP3(v0, 64.0f, 960.0f);

      y0 = (y0 - 64.0f) / 876.0f;
      u0 = (u0 - 64.0f) / 896.0f - 0.5f;
      v0 = (v0 - 64.0f) / 896.0f - 0.5f;

      float r = coeffs[0] * y0 + coeffs[1] * u0 + coeffs[2] * v0;
      float g = coeffs[3] * y0 + coeffs[4] * u0 + coeffs[5] * v0;
      float b = coeffs[6] * y0 + coeffs[7] * u0 + coeffs[8] * v0;

      r = CLIP3(r * 1023.0f + 0.5f, 0.0f, 1023.0f);
      g = CLIP3(g * 1023.0f + 0.5f, 0.0f, 1023.0f);
      b = CLIP3(b * 1023.0f + 0.5f, 0.0f, 1023.0f);

      int32_t r0 = int32_t(r);
      int32_t g0 = int32_t(g);
      int32_t b0 = int32_t(b);
      *rgbData = (0x3ff & r0) | ((0x3ff & g0) << 10) | ((0x3ff & b0) << 20) |
                 (0x3 << 30);  // Set alpha to 1.0

      rgbData++;
    }
  }
  writeFile("inRgba1010102.raw", mRawRgba1010102Image.data,
            mRawP010Image.width * mRawP010Image.height * 4);
  return true;
}

bool UltraHdrAppInput::convertYuv420ToRGBImage() {
  mRawRgba8888Image.data = malloc(mRawYuv420Image.width * mRawYuv420Image.height * 4);
  if (mRawRgba8888Image.data == nullptr) {
    std::cerr << "failed to allocate memory to store rgba888" << std::endl;
    return false;
  }
  mRawRgba8888Image.width = mRawYuv420Image.width;
  mRawRgba8888Image.height = mRawYuv420Image.height;
  mRawRgba8888Image.colorGamut = mRawYuv420Image.colorGamut;
  uint32_t* rgbData = static_cast<uint32_t*>(mRawRgba8888Image.data);
  uint8_t* y = static_cast<uint8_t*>(mRawYuv420Image.data);
  uint8_t* u = y + (mRawYuv420Image.width * mRawYuv420Image.height);
  uint8_t* v = u + (mRawYuv420Image.width * mRawYuv420Image.height / 4);

  const float* coeffs = BT601YUVtoRGBMatrix;
  for (size_t i = 0; i < mRawYuv420Image.height; i++) {
    for (size_t j = 0; j < mRawYuv420Image.width; j++) {
      float y0 = float(y[mRawYuv420Image.width * i + j]);
      float u0 = float(u[mRawYuv420Image.width / 2 * (i / 2) + (j / 2)] - 128);
      float v0 = float(v[mRawYuv420Image.width / 2 * (i / 2) + (j / 2)] - 128);

      y0 /= 255.0f;
      u0 /= 255.0f;
      v0 /= 255.0f;

      float r = coeffs[0] * y0 + coeffs[1] * u0 + coeffs[2] * v0;
      float g = coeffs[3] * y0 + coeffs[4] * u0 + coeffs[5] * v0;
      float b = coeffs[6] * y0 + coeffs[7] * u0 + coeffs[8] * v0;

      r = r * 255.0f + 0.5f;
      g = g * 255.0f + 0.5f;
      b = b * 255.0f + 0.5f;

      r = CLIP3(r, 0.0f, 255.0f);
      g = CLIP3(g, 0.0f, 255.0f);
      b = CLIP3(b, 0.0f, 255.0f);

      int32_t r0 = int32_t(r);
      int32_t g0 = int32_t(g);
      int32_t b0 = int32_t(b);
      *rgbData = r0 | (g0 << 8) | (b0 << 16) | (255 << 24);  // Set alpha to 1.0

      rgbData++;
    }
  }
  writeFile("inRgba8888.raw", mRawRgba8888Image.data,
            mRawYuv420Image.width * mRawYuv420Image.height * 4);
  return true;
}

bool UltraHdrAppInput::convertRgba8888ToYUV444Image() {
  mDestYUV444Image.data = malloc(mDestImage.width * mDestImage.height * 3);
  if (mDestYUV444Image.data == nullptr) {
    std::cerr << "failed to allocate memory to store yuv444" << std::endl;
    return false;
  }
  mDestYUV444Image.width = mDestImage.width;
  mDestYUV444Image.height = mDestImage.height;
  mDestYUV444Image.colorGamut = mDestImage.colorGamut;

  uint32_t* rgbData = static_cast<uint32_t*>(mDestImage.data);

  uint8_t* yData = static_cast<uint8_t*>(mDestYUV444Image.data);
  uint8_t* uData = yData + (mDestYUV444Image.width * mDestYUV444Image.height);
  uint8_t* vData = uData + (mDestYUV444Image.width * mDestYUV444Image.height);

  const float* coeffs = BT601RGBtoYUVMatrix;
  for (size_t i = 0; i < mDestImage.height; i++) {
    for (size_t j = 0; j < mDestImage.width; j++) {
      float r0 = float(rgbData[mDestImage.width * i + j] & 0xff);
      float g0 = float((rgbData[mDestImage.width * i + j] >> 8) & 0xff);
      float b0 = float((rgbData[mDestImage.width * i + j] >> 16) & 0xff);

      r0 /= 255.0f;
      g0 /= 255.0f;
      b0 /= 255.0f;

      float y = coeffs[0] * r0 + coeffs[1] * g0 + coeffs[2] * b0;
      float u = coeffs[3] * r0 + coeffs[4] * g0 + coeffs[5] * b0;
      float v = coeffs[6] * r0 + coeffs[7] * g0 + coeffs[8] * b0;

      y = y * 255.0f + 0.5f;
      u = u * 255.0f + 0.5f + 128.0f;
      v = v * 255.0f + 0.5f + 128.0f;

      y = CLIP3(y, 0.0f, 255.0f);
      u = CLIP3(u, 0.0f, 255.0f);
      v = CLIP3(v, 0.0f, 255.0f);

      yData[mDestYUV444Image.width * i + j] = uint8_t(y);
      uData[mDestYUV444Image.width * i + j] = uint8_t(u);
      vData[mDestYUV444Image.width * i + j] = uint8_t(v);
    }
  }
  writeFile("outyuv444.yuv", mDestYUV444Image.data,
            mDestYUV444Image.width * mDestYUV444Image.height * 3);
  return true;
}

bool UltraHdrAppInput::convertRgba1010102ToYUV444Image() {
  const float* coeffs = BT2020RGBtoYUVMatrix;
  if (mP010Cg == ULTRAHDR_COLORGAMUT_BT709) {
    coeffs = BT709RGBtoYUVMatrix;
  } else if (mP010Cg == ULTRAHDR_COLORGAMUT_BT2100) {
    coeffs = BT2020RGBtoYUVMatrix;
  } else if (mP010Cg == ULTRAHDR_COLORGAMUT_P3) {
    coeffs = BT601RGBtoYUVMatrix;
  } else {
    std::cerr << "color matrix not present for gamut " << mP010Cg << " using BT2020Matrix"
              << std::endl;
  }

  mDestYUV444Image.data = malloc(mDestImage.width * mDestImage.height * 3 * 2);
  if (mDestYUV444Image.data == nullptr) {
    std::cerr << "failed to allocate memory to store yuv444" << std::endl;
    return false;
  }
  mDestYUV444Image.width = mDestImage.width;
  mDestYUV444Image.height = mDestImage.height;
  mDestYUV444Image.colorGamut = mDestImage.colorGamut;

  uint32_t* rgbData = static_cast<uint32_t*>(mDestImage.data);

  uint16_t* yData = static_cast<uint16_t*>(mDestYUV444Image.data);
  uint16_t* uData = yData + (mDestYUV444Image.width * mDestYUV444Image.height);
  uint16_t* vData = uData + (mDestYUV444Image.width * mDestYUV444Image.height);

  for (size_t i = 0; i < mDestImage.height; i++) {
    for (size_t j = 0; j < mDestImage.width; j++) {
      float r0 = float(rgbData[mDestImage.width * i + j] & 0x3ff);
      float g0 = float((rgbData[mDestImage.width * i + j] >> 10) & 0x3ff);
      float b0 = float((rgbData[mDestImage.width * i + j] >> 20) & 0x3ff);

      r0 /= 1023.0f;
      g0 /= 1023.0f;
      b0 /= 1023.0f;

      float y = coeffs[0] * r0 + coeffs[1] * g0 + coeffs[2] * b0;
      float u = coeffs[3] * r0 + coeffs[4] * g0 + coeffs[5] * b0;
      float v = coeffs[6] * r0 + coeffs[7] * g0 + coeffs[8] * b0;

      y = (y * 876.0f) + 64.0f + 0.5f;
      u = (u * 896.0f) + 64.0f + 512.0f + 0.5f;
      v = (v * 896.0f) + 64.0f + 512.0f + 0.5f;

      y = CLIP3(y, 64.0f, 940.0f);
      u = CLIP3(u, 64.0f, 960.0f);
      v = CLIP3(v, 64.0f, 960.0f);

      yData[mDestYUV444Image.width * i + j] = uint16_t(y);
      uData[mDestYUV444Image.width * i + j] = uint16_t(u);
      vData[mDestYUV444Image.width * i + j] = uint16_t(v);
    }
  }
  writeFile("outyuv444.yuv", mDestYUV444Image.data,
            mDestYUV444Image.width * mDestYUV444Image.height * 3 * 2);
  return true;
}

void UltraHdrAppInput::computeRGBHdrPSNR() {
  if (mOf == ULTRAHDR_OUTPUT_SDR || mOf == ULTRAHDR_OUTPUT_HDR_LINEAR) {
    std::cout << "psnr not supported for output format " << mOf << std::endl;
    return;
  }
  uint32_t* rgbDataSrc = static_cast<uint32_t*>(mRawRgba1010102Image.data);
  uint32_t* rgbDataDst = static_cast<uint32_t*>(mDestImage.data);
  if (rgbDataSrc == nullptr || rgbDataDst == nullptr) {
    std::cerr << "invalid src or dst pointer for psnr computation " << std::endl;
    return;
  }
  if ((mOf == ULTRAHDR_OUTPUT_HDR_PQ && mTf != ULTRAHDR_TF_PQ) ||
      (mOf == ULTRAHDR_OUTPUT_HDR_HLG && mTf != ULTRAHDR_TF_HLG)) {
    std::cout << "input transfer function and output format are not compatible, psnr results "
                 "may be unreliable"
              << std::endl;
  }
  uint64_t rSqError = 0, gSqError = 0, bSqError = 0;
  for (size_t i = 0; i < mRawP010Image.width * mRawP010Image.height; i++) {
    int rSrc = *rgbDataSrc & 0x3ff;
    int rDst = *rgbDataDst & 0x3ff;
    rSqError += (rSrc - rDst) * (rSrc - rDst);

    int gSrc = (*rgbDataSrc >> 10) & 0x3ff;
    int gDst = (*rgbDataDst >> 10) & 0x3ff;
    gSqError += (gSrc - gDst) * (gSrc - gDst);

    int bSrc = (*rgbDataSrc >> 20) & 0x3ff;
    int bDst = (*rgbDataDst >> 20) & 0x3ff;
    bSqError += (bSrc - bDst) * (bSrc - bDst);

    rgbDataSrc++;
    rgbDataDst++;
  }
  double meanSquareError = (double)rSqError / (mRawP010Image.width * mRawP010Image.height);
  mPsnr[0] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100;

  meanSquareError = (double)gSqError / (mRawP010Image.width * mRawP010Image.height);
  mPsnr[1] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100;

  meanSquareError = (double)bSqError / (mRawP010Image.width * mRawP010Image.height);
  mPsnr[2] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100;

  std::cout << "psnr r :: " << mPsnr[0] << " psnr g :: " << mPsnr[1] << " psnr b :: " << mPsnr[2]
            << std::endl;
}

void UltraHdrAppInput::computeRGBSdrPSNR() {
  if (mOf != ULTRAHDR_OUTPUT_SDR) {
    std::cout << "psnr not supported for output format " << mOf << std::endl;
    return;
  }
  uint32_t* rgbDataSrc = static_cast<uint32_t*>(mRawRgba8888Image.data);
  uint32_t* rgbDataDst = static_cast<uint32_t*>(mDestImage.data);
  if (rgbDataSrc == nullptr || rgbDataDst == nullptr) {
    std::cerr << "invalid src or dst pointer for psnr computation " << std::endl;
    return;
  }

  uint64_t rSqError = 0, gSqError = 0, bSqError = 0;
  for (size_t i = 0; i < mRawYuv420Image.width * mRawYuv420Image.height; i++) {
    int rSrc = *rgbDataSrc & 0xff;
    int rDst = *rgbDataDst & 0xff;
    rSqError += (rSrc - rDst) * (rSrc - rDst);

    int gSrc = (*rgbDataSrc >> 8) & 0xff;
    int gDst = (*rgbDataDst >> 8) & 0xff;
    gSqError += (gSrc - gDst) * (gSrc - gDst);

    int bSrc = (*rgbDataSrc >> 16) & 0xff;
    int bDst = (*rgbDataDst >> 16) & 0xff;
    bSqError += (bSrc - bDst) * (bSrc - bDst);

    rgbDataSrc++;
    rgbDataDst++;
  }
  double meanSquareError = (double)rSqError / (mRawYuv420Image.width * mRawYuv420Image.height);
  mPsnr[0] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100;

  meanSquareError = (double)gSqError / (mRawYuv420Image.width * mRawYuv420Image.height);
  mPsnr[1] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100;

  meanSquareError = (double)bSqError / (mRawYuv420Image.width * mRawYuv420Image.height);
  mPsnr[2] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100;

  std::cout << "psnr r :: " << mPsnr[0] << " psnr g :: " << mPsnr[1] << " psnr b :: " << mPsnr[2]
            << std::endl;
}

void UltraHdrAppInput::computeYUVHdrPSNR() {
  if (mOf == ULTRAHDR_OUTPUT_SDR || mOf == ULTRAHDR_OUTPUT_HDR_LINEAR) {
    std::cout << "psnr not supported for output format " << mOf << std::endl;
    return;
  }
  uint16_t* yuvDataSrc = static_cast<uint16_t*>(mRawP010Image.data);
  uint16_t* yuvDataDst = static_cast<uint16_t*>(mDestYUV444Image.data);
  if (yuvDataSrc == nullptr || yuvDataDst == nullptr) {
    std::cerr << "invalid src or dst pointer for psnr computation " << std::endl;
    return;
  }
  if ((mOf == ULTRAHDR_OUTPUT_HDR_PQ && mTf != ULTRAHDR_TF_PQ) ||
      (mOf == ULTRAHDR_OUTPUT_HDR_HLG && mTf != ULTRAHDR_TF_HLG)) {
    std::cout << "input transfer function and output format are not compatible, psnr results "
                 "may be unreliable"
              << std::endl;
  }

  uint16_t* yDataSrc = static_cast<uint16_t*>(mRawP010Image.data);
  uint16_t* uDataSrc = yDataSrc + (mRawP010Image.width * mRawP010Image.height);
  uint16_t* vDataSrc = uDataSrc + 1;

  uint16_t* yDataDst = static_cast<uint16_t*>(mDestYUV444Image.data);
  uint16_t* uDataDst = yDataDst + (mDestYUV444Image.width * mDestYUV444Image.height);
  uint16_t* vDataDst = uDataDst + (mDestYUV444Image.width * mDestYUV444Image.height);

  uint64_t ySqError = 0, uSqError = 0, vSqError = 0;
  for (size_t i = 0; i < mDestYUV444Image.height; i++) {
    for (size_t j = 0; j < mDestYUV444Image.width; j++) {
      int ySrc = (yDataSrc[mRawP010Image.width * i + j] >> 6) & 0x3ff;
      ySrc = CLIP3(ySrc, 64, 940);
      int yDst = yDataDst[mDestYUV444Image.width * i + j] & 0x3ff;
      ySqError += (ySrc - yDst) * (ySrc - yDst);

      if (i % 2 == 0 && j % 2 == 0) {
        int uSrc = (uDataSrc[mRawP010Image.width * (i / 2) + (j / 2) * 2] >> 6) & 0x3ff;
        uSrc = CLIP3(uSrc, 64, 960);
        int uDst = uDataDst[mDestYUV444Image.width * i + j] & 0x3ff;
        uDst += uDataDst[mDestYUV444Image.width * i + j + 1] & 0x3ff;
        uDst += uDataDst[mDestYUV444Image.width * (i + 1) + j + 1] & 0x3ff;
        uDst += uDataDst[mDestYUV444Image.width * (i + 1) + j + 1] & 0x3ff;
        uDst = (uDst + 2) >> 2;
        uSqError += (uSrc - uDst) * (uSrc - uDst);

        int vSrc = (vDataSrc[mRawP010Image.width * (i / 2) + (j / 2) * 2] >> 6) & 0x3ff;
        vSrc = CLIP3(vSrc, 64, 960);
        int vDst = vDataDst[mDestYUV444Image.width * i + j] & 0x3ff;
        vDst += vDataDst[mDestYUV444Image.width * i + j + 1] & 0x3ff;
        vDst += vDataDst[mDestYUV444Image.width * (i + 1) + j + 1] & 0x3ff;
        vDst += vDataDst[mDestYUV444Image.width * (i + 1) + j + 1] & 0x3ff;
        vDst = (vDst + 2) >> 2;
        vSqError += (vSrc - vDst) * (vSrc - vDst);
      }
    }
  }

  double meanSquareError = (double)ySqError / (mDestYUV444Image.width * mDestYUV444Image.height);
  mPsnr[0] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100;

  meanSquareError = (double)uSqError / (mDestYUV444Image.width * mDestYUV444Image.height / 4);
  mPsnr[1] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100;

  meanSquareError = (double)vSqError / (mDestYUV444Image.width * mDestYUV444Image.height / 4);
  mPsnr[2] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100;

  std::cout << "psnr y :: " << mPsnr[0] << " psnr u :: " << mPsnr[1] << " psnr v :: " << mPsnr[2]
            << std::endl;
}

void UltraHdrAppInput::computeYUVSdrPSNR() {
  if (mOf != ULTRAHDR_OUTPUT_SDR) {
    std::cout << "psnr not supported for output format " << mOf << std::endl;
    return;
  }

  uint8_t* yDataSrc = static_cast<uint8_t*>(mRawYuv420Image.data);
  uint8_t* uDataSrc = yDataSrc + (mRawYuv420Image.width * mRawYuv420Image.height);
  uint8_t* vDataSrc = uDataSrc + (mRawYuv420Image.width * mRawYuv420Image.height / 4);

  uint8_t* yDataDst = static_cast<uint8_t*>(mDestYUV444Image.data);
  uint8_t* uDataDst = yDataDst + (mDestYUV444Image.width * mDestYUV444Image.height);
  uint8_t* vDataDst = uDataDst + (mDestYUV444Image.width * mDestYUV444Image.height);

  uint64_t ySqError = 0, uSqError = 0, vSqError = 0;
  for (size_t i = 0; i < mDestYUV444Image.height; i++) {
    for (size_t j = 0; j < mDestYUV444Image.width; j++) {
      int ySrc = yDataSrc[mRawYuv420Image.width * i + j];
      int yDst = yDataDst[mDestYUV444Image.width * i + j];
      ySqError += (ySrc - yDst) * (ySrc - yDst);

      if (i % 2 == 0 && j % 2 == 0) {
        int uSrc = uDataSrc[mRawYuv420Image.width / 2 * (i / 2) + j / 2];
        int uDst = uDataDst[mDestYUV444Image.width * i + j];
        uDst += uDataDst[mDestYUV444Image.width * i + j + 1];
        uDst += uDataDst[mDestYUV444Image.width * (i + 1) + j];
        uDst += uDataDst[mDestYUV444Image.width * (i + 1) + j + 1];
        uDst = (uDst + 2) >> 2;
        uSqError += (uSrc - uDst) * (uSrc - uDst);

        int vSrc = vDataSrc[mRawYuv420Image.width / 2 * (i / 2) + j / 2];
        int vDst = vDataDst[mDestYUV444Image.width * i + j];
        vDst += vDataDst[mDestYUV444Image.width * i + j + 1];
        vDst += vDataDst[mDestYUV444Image.width * (i + 1) + j];
        vDst += vDataDst[mDestYUV444Image.width * (i + 1) + j + 1];
        vDst = (vDst + 2) >> 2;
        vSqError += (vSrc - vDst) * (vSrc - vDst);
      }
    }
  }
  double meanSquareError = (double)ySqError / (mDestYUV444Image.width * mDestYUV444Image.height);
  mPsnr[0] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100;

  meanSquareError = (double)uSqError / (mDestYUV444Image.width * mDestYUV444Image.height / 4);
  mPsnr[1] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100;

  meanSquareError = (double)vSqError / (mDestYUV444Image.width * mDestYUV444Image.height / 4);
  mPsnr[2] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100;

  std::cout << "psnr y :: " << mPsnr[0] << " psnr  u:: " << mPsnr[1] << " psnr v :: " << mPsnr[2]
            << std::endl;
}

static void usage(const char* name) {
  fprintf(stderr, "\n## ultra hdr demo application.\nUsage : %s \n", name);
  fprintf(stderr, "    -m    mode of operation. [0:encode, 1:decode] \n");
  fprintf(stderr, "\n## encoder options : \n");
  fprintf(stderr, "    -p    raw 10 bit input resource in p010 color format, mandatory. \n");
  fprintf(stderr,
          "    -y    raw 8 bit input resource in yuv420, optional. \n"
          "          if not provided tonemapping happens internally. \n");
  fprintf(stderr, "    -i    compressed 8 bit jpeg file path, optional \n");
  fprintf(stderr, "    -w    input file width, mandatory. \n");
  fprintf(stderr, "    -h    input file height, mandatory. \n");
  fprintf(stderr, "    -C    10 bit input color gamut, optional. [0:bt709, 1:p3, 2:bt2100] \n");
  fprintf(stderr, "    -c    8 bit input color gamut, optional. [0:bt709, 1:p3, 2:bt2100] \n");
  fprintf(stderr, "    -t    input transfer function, optional. [0:linear, 1:hlg, 2:pq] \n");
  fprintf(stderr,
          "    -q    quality factor to be used while encoding 8 bit image, optional. [0-100].\n"
          "          gain map image does not use this quality factor. \n"
          "          for now gain map image quality factor is not configurable. \n");
  fprintf(stderr, "    -e    compute psnr, optional. [0:no, 1:yes] \n");
  fprintf(stderr, "\n## decoder options : \n");
  fprintf(stderr, "    -j    ultra hdr input resource, mandatory in decode mode. \n");
  fprintf(stderr,
          "    -o    output transfer function, optional. [0:sdr, 1:hdr_linear, 2:hdr_pq, "
          "3:hdr_hlg] \n");
  fprintf(stderr, "\n## examples of usage :\n");
  fprintf(stderr, "\n## encode api-0 :\n");
  fprintf(stderr, "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -w 1920 -h 1080 -q 97\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -w 1920 -h 1080 -q 97 -C 2 -t 2\n");
  fprintf(stderr, "\n## encode api-1 :\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -y cosmat_1920x1080_420.yuv -w 1920 "
          "-h 1080 -q 97\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -y cosmat_1920x1080_420.yuv -w 1920 "
          "-h 1080 -q 97\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -y cosmat_1920x1080_420.yuv -w 1920 "
          "-h 1080 -q 97 -C 2 -c 1 -t 1\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -y cosmat_1920x1080_420.yuv -w 1920 "
          "-h 1080 -q 97 -C 2 -c 1 -t 1 -e 1\n");
  fprintf(stderr, "\n## encode api-2 :\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -y cosmat_1920x1080_420.yuv -i "
          "cosmat_1920x1080_420_8bit.jpg -w 1920 -h 1080 -t 1 -o 3 -e 1\n");
  fprintf(stderr, "\n## encode api-3 :\n");
  fprintf(stderr,
          "    ultrahdr_app -m 0 -p cosmat_1920x1080_p010.yuv -i cosmat_1920x1080_420_8bit.jpg -w "
          "1920 -h 1080 -t 1 -o 3 -e 1\n");
  fprintf(stderr, "\n## decode api :\n");
  fprintf(stderr, "    ultrahdr_app -m 1 -j cosmat_1920x1080_hdr.jpg \n");
  fprintf(stderr, "    ultrahdr_app -m 1 -j cosmat_1920x1080_hdr.jpg -o 2\n");
  fprintf(stderr, "\n");
}

int main(int argc, char* argv[]) {
  char opt_string[] = "p:y:i:w:h:C:c:t:q:o:m:j:e:";
  char *p010_file = nullptr, *yuv420_file = nullptr, *jpegr_file = nullptr,
       *yuv420_jpeg_file = nullptr;
  int width = 0, height = 0;
  ultrahdr_color_gamut p010Cg = ULTRAHDR_COLORGAMUT_BT709;
  ultrahdr_color_gamut yuv420Cg = ULTRAHDR_COLORGAMUT_BT709;
  ultrahdr_transfer_function tf = ULTRAHDR_TF_HLG;
  int quality = 100;
  ultrahdr_output_format of = ULTRAHDR_OUTPUT_HDR_HLG;
  int mode = 0;
  int compute_psnr = 0;
  int ch;
  while ((ch = getopt_s(argc, argv, opt_string)) != -1) {
    switch (ch) {
      case 'p':
        p010_file = optarg_s;
        break;
      case 'y':
        yuv420_file = optarg_s;
        break;
      case 'i':
        yuv420_jpeg_file = optarg_s;
        break;
      case 'w':
        width = atoi(optarg_s);
        break;
      case 'h':
        height = atoi(optarg_s);
        break;
      case 'C':
        p010Cg = static_cast<ultrahdr_color_gamut>(atoi(optarg_s));
        break;
      case 'c':
        yuv420Cg = static_cast<ultrahdr_color_gamut>(atoi(optarg_s));
        break;
      case 't':
        tf = static_cast<ultrahdr_transfer_function>(atoi(optarg_s));
        break;
      case 'q':
        quality = atoi(optarg_s);
        break;
      case 'o':
        of = static_cast<ultrahdr_output_format>(atoi(optarg_s));
        break;
      case 'm':
        mode = atoi(optarg_s);
        break;
      case 'j':
        jpegr_file = optarg_s;
        break;
      case 'e':
        compute_psnr = atoi(optarg_s);
        break;
      default:
        usage(argv[0]);
        return -1;
    }
  }
  if (mode == 0) {
    if (width <= 0 || height <= 0 || p010_file == nullptr) {
      usage(argv[0]);
      return -1;
    }
    UltraHdrAppInput appInput(p010_file, yuv420_file, yuv420_jpeg_file, width, height, p010Cg,
                              yuv420Cg, tf, quality, of);
    if (!appInput.encode()) return -1;
    if (compute_psnr == 1) {
      if (!appInput.decode()) return -1;
      if (of == ULTRAHDR_OUTPUT_SDR && yuv420_file != nullptr) {
        appInput.convertYuv420ToRGBImage();
        appInput.computeRGBSdrPSNR();
        appInput.convertRgba8888ToYUV444Image();
        appInput.computeYUVSdrPSNR();
      } else if (of == ULTRAHDR_OUTPUT_HDR_HLG || of == ULTRAHDR_OUTPUT_HDR_PQ) {
        appInput.convertP010ToRGBImage();
        appInput.computeRGBHdrPSNR();
        appInput.convertRgba1010102ToYUV444Image();
        appInput.computeYUVHdrPSNR();
      }
    }
  } else if (mode == 1) {
    if (jpegr_file == nullptr) {
      usage(argv[0]);
      return -1;
    }
    UltraHdrAppInput appInput(jpegr_file, of);
    if (!appInput.decode()) return -1;
  } else {
    std::cerr << "unrecognized input mode " << mode << std::endl;
    usage(argv[0]);
    return -1;
  }

  return 0;
}