-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathoapv_app_util.h
More file actions
853 lines (757 loc) · 23.3 KB
/
Copy pathoapv_app_util.h
File metadata and controls
853 lines (757 loc) · 23.3 KB
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
/*
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
* 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 copyright owner, 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 OWNER 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.
*/
#ifndef _OAPV_APP_UTIL_H_
#define _OAPV_APP_UTIL_H_
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#if LINUX
#include <signal.h>
#include <unistd.h>
#endif
#define VERBOSE_NONE 0
#define VERBOSE_ERROR 1
#define VERBOSE_SIMPLE 2
#define VERBOSE_FRAME 3
/* max image resolution supported by imgb (32K: 2x of 8192x4320) */
#define IMGB_MAX_W 16384
#define IMGB_MAX_H 8640
/* logging functions */
static void log_msg(char *filename, int line, const char *fmt, ...)
{
char str[1024] = { '\0' };
if(filename != NULL && line >= 0)
snprintf(str, sizeof(str), "[%s:%d] ", filename, line);
va_list args;
va_start(args, fmt);
vsnprintf(str + strlen(str), sizeof(str) - strlen(str), fmt, args);
va_end(args);
printf("%s", str);
}
static void log_line(char *pre)
{
int i, len;
char str[128] = { '\0' };
const int chars = 80;
for(i = 0; i < 3; i++) {
str[i] = '=';
}
str[i] = '\0';
len = (pre == NULL) ? 0 : (int)strlen(pre);
if(len > 0) {
snprintf(str + 3, sizeof(str) - 3, " %s ", pre);
len = (int)strlen(str);
}
for(i = len; i < chars; i++) {
str[i] = '=';
}
str[chars] = '\0';
printf("%s\n", str);
}
#if defined(__GNUC__)
#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define logerr(args...) \
{ \
if(op_verbose >= VERBOSE_ERROR) { \
log_msg(NULL, -1, args); \
} \
}
#define logv2(args...) \
{ \
if(op_verbose >= VERBOSE_SIMPLE) { \
log_msg(NULL, -1, args); \
} \
}
#define logv3(args...) \
{ \
if(op_verbose >= VERBOSE_FRAME) { \
log_msg(NULL, -1, args); \
} \
}
#else
#define __FILENAME__ \
(strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#define logerr(args, ...) \
{ \
if(op_verbose >= VERBOSE_ERROR) { \
log_msg(NULL, -1, args, __VA_ARGS__); \
} \
}
#define logv2(args, ...) \
{ \
if(op_verbose >= VERBOSE_SIMPLE) { \
log_msg(NULL, -1, args, __VA_ARGS__); \
} \
}
#define logv3(args, ...) \
{ \
if(op_verbose >= VERBOSE_FRAME) { \
log_msg(NULL, -1, args, __VA_ARGS__); \
} \
}
#endif
#define logv2_line(pre) \
{ \
if(op_verbose >= VERBOSE_SIMPLE) { \
log_line(pre); \
} \
}
#define logv3_line(pre) \
{ \
if(op_verbose >= VERBOSE_FRAME) { \
log_line(pre); \
} \
}
/* assert function */
#include <assert.h>
#define assert_r(x) \
{ \
if(!(x)) { \
assert(x); \
return; \
} \
}
#define assert_rv(x, r) \
{ \
if(!(x)) { \
assert(x); \
return (r); \
} \
}
#define assert_g(x, g) \
{ \
if(!(x)) { \
assert(x); \
goto g; \
} \
}
#define assert_gv(x, r, v, g) \
{ \
if(!(x)) { \
assert(x); \
(r) = (v); \
goto g; \
} \
}
static int op_verbose = VERBOSE_SIMPLE;
/* Clocks */
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
typedef DWORD oapv_clk_t;
#define OAPV_CLK_PER_SEC (1000)
#define OAPV_CLK_PER_MSEC (1)
#define OAPV_CLK_MAX ((oapv_clk_t)(-1))
#define oapv_clk_get() GetTickCount()
#elif __linux__ || __CYGWIN__ || __APPLE__
#include <time.h>
#include <sys/time.h>
typedef unsigned long oapv_clk_t;
#define OAPV_CLK_MAX ((oapv_clk_t)(-1))
#define OAPV_CLK_PER_SEC (10000)
#define OAPV_CLK_PER_MSEC (10)
static oapv_clk_t oapv_clk_get(void)
{
oapv_clk_t clk;
struct timeval t;
gettimeofday(&t, NULL);
clk = t.tv_sec * 10000L + t.tv_usec / 100L;
return clk;
}
#else
#error THIS PLATFORM CANNOT SUPPORT CLOCK
#endif
static __inline oapv_clk_t oapv_clk_diff(oapv_clk_t t1, oapv_clk_t t2)
{
return (((t2) >= (t1)) ? ((t2) - (t1)) : ((OAPV_CLK_MAX - (t1)) + (t2)));
}
static __inline oapv_clk_t oapv_clk_from(oapv_clk_t from)
{
oapv_clk_t now = oapv_clk_get();
return oapv_clk_diff(from, now);
}
static __inline oapv_clk_t oapv_clk_msec(oapv_clk_t clk)
{
return ((oapv_clk_t)((clk + (OAPV_CLK_PER_MSEC / 2)) / OAPV_CLK_PER_MSEC));
}
static __inline oapv_clk_t oapv_clk_sec(oapv_clk_t clk)
{
return ((oapv_clk_t)((clk + (OAPV_CLK_PER_SEC / 2)) / OAPV_CLK_PER_SEC));
}
#define CLIP_VAL(n, min, max) (((n) > (max)) ? (max) : (((n) < (min)) ? (min) : (n)))
#define ALIGN_VAL(val, align) ((((val) + (align) - 1) / (align)) * (align))
/* Function for atomic increment:
This function might need to modify according to O/S or CPU platform
*/
static int atomic_inc(volatile int *pcnt)
{
int ret;
ret = *pcnt;
ret++;
*pcnt = ret;
return ret;
}
/* Function for atomic decrement:
This function might need to modify according to O/S or CPU platform
*/
static int atomic_dec(volatile int *pcnt)
{
int ret;
ret = *pcnt;
ret--;
*pcnt = ret;
return ret;
}
/* Function to allocate memory for picture buffer:
This function might need to modify according to O/S or CPU platform
*/
static void *picbuf_alloc(int size)
{
return malloc(size);
}
/* Function to free memory allocated for picture buffer:
This function might need to modify according to O/S or CPU platform
*/
static void picbuf_free(void *p)
{
if(p) {
free(p);
}
}
static int imgb_addref(oapv_imgb_t *imgb)
{
assert_rv(imgb, OAPV_ERR_INVALID_ARGUMENT);
return atomic_inc(&imgb->refcnt);
}
static int imgb_getref(oapv_imgb_t *imgb)
{
assert_rv(imgb, OAPV_ERR_INVALID_ARGUMENT);
return imgb->refcnt;
}
static int imgb_release(oapv_imgb_t *imgb)
{
int refcnt, i;
assert_rv(imgb, OAPV_ERR_INVALID_ARGUMENT);
refcnt = atomic_dec(&imgb->refcnt);
if(refcnt == 0) {
for(i = 0; i < OAPV_MAX_CC; i++) {
if(imgb->baddr[i])
picbuf_free(imgb->baddr[i]);
}
free(imgb);
}
return refcnt;
}
oapv_imgb_t *imgb_create(int w, int h, int cs)
{
int i, bd;
oapv_imgb_t *imgb;
imgb = (oapv_imgb_t *)malloc(sizeof(oapv_imgb_t));
if(imgb == NULL)
goto ERR;
memset(imgb, 0, sizeof(oapv_imgb_t));
bd = OAPV_CS_GET_BYTE_DEPTH(cs); /* byte unit */
/* reject invalid or out-of-range resolution */
if(w <= 0 || h <= 0 || w > IMGB_MAX_W || h > IMGB_MAX_H || bd <= 0) {
logerr("ERR: invalid image parameter (w=%d, h=%d, byte-depth=%d)\n", w, h, bd);
goto ERR;
}
imgb->w[0] = w;
imgb->h[0] = h;
switch(OAPV_CS_GET_FORMAT(cs)) {
case OAPV_CF_YCBCR400:
imgb->w[1] = imgb->w[2] = w;
imgb->h[1] = imgb->h[2] = h;
imgb->np = 1;
break;
case OAPV_CF_YCBCR420:
imgb->w[1] = imgb->w[2] = (w + 1) >> 1;
imgb->h[1] = imgb->h[2] = (h + 1) >> 1;
imgb->np = 3;
break;
case OAPV_CF_YCBCR422:
imgb->w[1] = imgb->w[2] = (w + 1) >> 1;
imgb->h[1] = imgb->h[2] = h;
imgb->np = 3;
break;
case OAPV_CF_YCBCR444:
imgb->w[1] = imgb->w[2] = w;
imgb->h[1] = imgb->h[2] = h;
imgb->np = 3;
break;
case OAPV_CF_YCBCR4444:
imgb->w[1] = imgb->w[2] = imgb->w[3] = w;
imgb->h[1] = imgb->h[2] = imgb->h[3] = h;
imgb->np = 4;
break;
case OAPV_CF_PLANAR2:
imgb->w[1] = w;
imgb->h[1] = h;
imgb->np = 2;
break;
default:
logv3("unsupported color format\n");
goto ERR;
}
for(i = 0; i < imgb->np; i++) {
// width and height need to be aligned to macroblock size
imgb->aw[i] = ALIGN_VAL(imgb->w[i], OAPV_MB_W);
imgb->s[i] = imgb->aw[i] * bd;
imgb->ah[i] = ALIGN_VAL(imgb->h[i], OAPV_MB_H);
imgb->e[i] = imgb->ah[i];
imgb->bsize[i] = imgb->s[i] * imgb->e[i];
imgb->a[i] = imgb->baddr[i] = picbuf_alloc(imgb->bsize[i]);
assert_g(imgb->a[i] != NULL, ERR);
memset(imgb->a[i], 0, imgb->bsize[i]);
}
imgb->cs = cs;
imgb->addref = imgb_addref;
imgb->getref = imgb_getref;
imgb->release = imgb_release;
imgb->addref(imgb); /* increase reference count */
return imgb;
ERR:
logerr("ERR: cannot create image buffer\n");
if(imgb) {
for(int i = 0; i < OAPV_MAX_CC; i++) {
if(imgb->a[i])
picbuf_free(imgb->a[i]);
}
free(imgb);
}
return NULL;
}
static int imgb_read(FILE *fp, oapv_imgb_t *img, int width, int height, int is_y4m)
{
int f_w, f_h;
unsigned char *p8;
/* handling Y4M frame header */
char t_buf[10];
if(is_y4m) {
if(6 != fread(t_buf, 1, 6, fp))
return -1;
if(memcmp(t_buf, "FRAME", 5)) {
logerr("ERR: Loss of framing in Y4M input data\n");
return -1;
}
if(t_buf[5] != '\n') {
logerr("ERR: parsing Y4M frame header\n");
return -1;
}
}
/* reading YUV format */
int color_format = OAPV_CS_GET_FORMAT(img->cs);
int bit_depth = OAPV_CS_GET_BIT_DEPTH(img->cs);
int w_shift = (color_format == OAPV_CF_YCBCR420) || ((color_format == OAPV_CF_YCBCR422) || (color_format == OAPV_CF_PLANAR2)) ? 1 : 0;
int h_shift = color_format == OAPV_CF_YCBCR420 ? 1 : 0;
if(bit_depth == 8) {
f_w = width;
f_h = height;
}
else if(bit_depth >= 10 && bit_depth <= 16) {
f_w = width * sizeof(short);
f_h = height;
}
else {
logerr("ERR: unsupported bit-depth (%d)\n", bit_depth);
return -1;
}
p8 = (unsigned char *)img->a[0];
for(int j = 0; j < f_h; j++) {
if(fread(p8, 1, f_w, fp) != (unsigned)f_w) {
return -1;
}
p8 += img->s[0];
}
if(color_format == OAPV_CF_PLANAR2) {
p8 = (unsigned char *)img->a[1];
for(int j = 0; j < f_h; j++) {
if(fread(p8, 1, f_w, fp) != (unsigned)f_w) {
return -1;
}
p8 += img->s[1];
}
}
else if(color_format != OAPV_CF_YCBCR400) {
f_w = f_w >> w_shift;
f_h = f_h >> h_shift;
p8 = (unsigned char *)img->a[1];
for(int j = 0; j < f_h; j++) {
if(fread(p8, 1, f_w, fp) != (unsigned)f_w) {
return -1;
}
p8 += img->s[1];
}
p8 = (unsigned char *)img->a[2];
for(int j = 0; j < f_h; j++) {
if(fread(p8, 1, f_w, fp) != (unsigned)f_w) {
return -1;
}
p8 += img->s[2];
}
}
if(color_format == OAPV_CF_YCBCR4444) {
f_w = f_w >> w_shift;
f_h = f_h >> h_shift;
p8 = (unsigned char *)img->a[3];
for(int j = 0; j < f_h; j++) {
if(fread(p8, 1, f_w, fp) != (unsigned)f_w) {
return -1;
}
p8 += img->s[3];
}
}
return 0;
}
static int imgb_write(char *fname, oapv_imgb_t *imgb)
{
unsigned char *p8;
int i, j, bd;
int chroma_format, bit_depth;
FILE *fp;
if(imgb == NULL) {
logerr("ERR: null image buffer in image write\n");
return -1;
}
chroma_format = OAPV_CS_GET_FORMAT(imgb->cs);
bit_depth = OAPV_CS_GET_BIT_DEPTH(imgb->cs);
fp = fopen(fname, "ab");
if(fp == NULL) {
logerr("ERR: cannot open file = %s\n", fname);
return -1;
}
if(bit_depth == 8 && (chroma_format == OAPV_CF_YCBCR400 || chroma_format == OAPV_CF_YCBCR420 || chroma_format == OAPV_CF_YCBCR422 ||
chroma_format == OAPV_CF_YCBCR444 || chroma_format == OAPV_CF_YCBCR4444)) {
bd = 1;
}
else if(bit_depth >= 10 && bit_depth <= 16 && (chroma_format == OAPV_CF_YCBCR400 || chroma_format == OAPV_CF_YCBCR420 || chroma_format == OAPV_CF_YCBCR422 || chroma_format == OAPV_CF_YCBCR444 || chroma_format == OAPV_CF_YCBCR4444)) {
bd = 2;
}
else if(bit_depth >= 10 && chroma_format == OAPV_CF_PLANAR2) {
bd = 2;
}
else {
logerr("ERR: cannot support the color space\n");
fclose(fp);
return -1;
}
for(i = 0; i < imgb->np; i++) {
p8 = (unsigned char *)imgb->a[i] + (imgb->s[i] * imgb->y[i]) + (imgb->x[i] * bd);
for(j = 0; j < imgb->h[i]; j++) {
fwrite(p8, imgb->w[i] * bd, 1, fp);
p8 += imgb->s[i];
}
}
fclose(fp);
return 0;
}
// checks src/dst are compatible and both buffers hold the copy extent
static int imgb_cpy_is_valid(oapv_imgb_t *dst, oapv_imgb_t *src)
{
if(src == NULL || dst == NULL) {
logerr("ERR: null image buffer in image copy\n");
return 0;
}
if(src->np < 1 || src->np > OAPV_MAX_CC || dst->np != src->np) {
logerr("ERR: mismatched plane count in image copy\n");
return 0;
}
int src_bytes = OAPV_CS_GET_BYTE_DEPTH(src->cs);
int dst_bytes = OAPV_CS_GET_BYTE_DEPTH(dst->cs);
for(int i = 0; i < src->np; i++) {
int w = src->w[i] > src->aw[i] ? src->w[i] : src->aw[i];
int h = src->h[i] > src->ah[i] ? src->h[i] : src->ah[i];
if(src->a[i] == NULL || dst->a[i] == NULL || w < 0 || h < 0) {
logerr("ERR: invalid plane in image copy\n");
return 0;
}
if((long long)(h - 1) * src->s[i] + (long long)w * src_bytes > src->bsize[i] ||
(long long)(h - 1) * dst->s[i] + (long long)w * dst_bytes > dst->bsize[i]) {
logerr("ERR: buffer too small in image copy\n");
return 0;
}
}
return 1;
}
static void imgb_cpy_plane(oapv_imgb_t *dst, oapv_imgb_t *src)
{
int i, j;
unsigned char *s, *d;
int numbyte = OAPV_CS_GET_BYTE_DEPTH(src->cs);
for(i = 0; i < src->np; i++) {
s = (unsigned char *)src->a[i];
d = (unsigned char *)dst->a[i];
for(j = 0; j < src->ah[i]; j++) {
memcpy(d, s, numbyte * src->aw[i]);
s += src->s[i];
d += dst->s[i];
}
}
}
static void imgb_cpy_shift_left_8b(oapv_imgb_t *dst, oapv_imgb_t *src, int shift)
{
int i, j, k;
unsigned char *s;
short *d;
for(i = 0; i < dst->np; i++) {
s = (unsigned char *)src->a[i];
d = (short *)dst->a[i];
for(j = 0; j < src->ah[i]; j++) {
for(k = 0; k < src->aw[i]; k++) {
d[k] = (short)(s[k] << shift);
}
s = s + src->s[i];
d = (short *)(((unsigned char *)d) + dst->s[i]);
}
}
}
static void imgb_cpy_shift_right_8b(oapv_imgb_t *dst, oapv_imgb_t *src, int shift)
{
int i, j, k, t0, add;
short *s;
unsigned char *d;
if(shift)
add = 1 << (shift - 1);
else
add = 0;
for(i = 0; i < dst->np; i++) {
s = (short *)src->a[i];
d = (unsigned char *)dst->a[i];
for(j = 0; j < src->ah[i]; j++) {
for(k = 0; k < src->aw[i]; k++) {
t0 = ((s[k] + add) >> shift);
d[k] = (unsigned char)(CLIP_VAL(t0, 0, 255));
}
s = (short *)(((unsigned char *)s) + src->s[i]);
d = d + dst->s[i];
}
}
}
static void imgb_cpy_shift_left(oapv_imgb_t *dst, oapv_imgb_t *src, int shift)
{
int i, j, k;
unsigned short *s;
unsigned short *d;
for(i = 0; i < dst->np; i++) {
s = (unsigned short *)src->a[i];
d = (unsigned short *)dst->a[i];
for(j = 0; j < src->h[i]; j++) {
for(k = 0; k < src->w[i]; k++) {
d[k] = (unsigned short)(s[k] << shift);
}
s = (unsigned short *)(((unsigned char *)s) + src->s[i]);
d = (unsigned short *)(((unsigned char *)d) + dst->s[i]);
}
}
}
static void imgb_cpy_shift_right(oapv_imgb_t *dst, oapv_imgb_t *src, int shift)
{
int i, j, k, t0, add;
int clip_min = 0;
int clip_max = 0;
unsigned short *s;
unsigned short *d;
if(shift)
add = 1 << (shift - 1);
else
add = 0;
clip_max = (1 << (OAPV_CS_GET_BIT_DEPTH(dst->cs))) - 1;
for(i = 0; i < dst->np; i++) {
s = (unsigned short *)src->a[i];
d = (unsigned short *)dst->a[i];
for(j = 0; j < src->h[i]; j++) {
for(k = 0; k < src->w[i]; k++) {
t0 = ((s[k] + add) >> shift);
d[k] = (CLIP_VAL(t0, clip_min, clip_max));
}
s = (unsigned short *)(((unsigned char *)s) + src->s[i]);
d = (unsigned short *)(((unsigned char *)d) + dst->s[i]);
}
}
}
static void imgb_cpy(oapv_imgb_t *dst, oapv_imgb_t *src)
{
int i, bd_src, bd_dst;
if(!imgb_cpy_is_valid(dst, src)) return;
bd_src = OAPV_CS_GET_BIT_DEPTH(src->cs);
bd_dst = OAPV_CS_GET_BIT_DEPTH(dst->cs);
if(bd_src < 8 || bd_src > 16 || bd_dst < 8 || bd_dst > 16) {
logerr("ERR: unsupported bit depth in image copy\n");
return;
}
if(src->cs == dst->cs) {
imgb_cpy_plane(dst, src);
}
else if(bd_src == 8 && bd_dst > 8) {
imgb_cpy_shift_left_8b(dst, src, bd_dst - bd_src);
}
else if(bd_src > 8 && bd_dst == 8) {
imgb_cpy_shift_right_8b(dst, src, bd_src - bd_dst);
}
else if(bd_src < bd_dst) {
imgb_cpy_shift_left(dst, src, bd_dst - bd_src);
}
else if(bd_src > bd_dst) {
imgb_cpy_shift_right(dst, src, bd_src - bd_dst);
}
else {
logerr("ERROR: unsupported image copy\n");
return;
}
for(i = 0; i < OAPV_MAX_CC; i++) {
dst->x[i] = src->x[i];
dst->y[i] = src->y[i];
dst->w[i] = src->w[i];
dst->h[i] = src->h[i];
dst->ts[i] = src->ts[i];
}
}
void imgb_clear(oapv_imgb_t *imgb)
{
int i;
for(i = 0; i < imgb->np; i++) {
memset(imgb->a[i], 0, imgb->bsize[i]);
}
}
static void measure_psnr(oapv_imgb_t *org, oapv_imgb_t *rec, double psnr[4], int bit_depth)
{
double sum[4], mse[4];
if(bit_depth == 8) {
unsigned char *o, *r;
int i, j, k;
for(i = 0; i < org->np; i++) {
o = (unsigned char *)org->a[i];
r = (unsigned char *)rec->a[i];
sum[i] = 0;
for(j = 0; j < org->h[i]; j++) {
for(k = 0; k < org->w[i]; k++) {
sum[i] += (double)(o[k] - r[k]) * (o[k] - r[k]);
}
o += org->s[i];
r += rec->s[i];
}
mse[i] = sum[i] / (org->w[i] * org->h[i]);
psnr[i] = (mse[i] == 0.0) ? 100. : fabs(10 * log10(((255.f * 255.f) / mse[i])));
}
}
else {
/* more than 8bit, ex) 10bit */
unsigned short *o, *r;
int i, j, k;
int factor = 1 << (bit_depth - 8);
factor *= factor;
for(i = 0; i < org->np; i++) {
o = (unsigned short *)org->a[i];
r = (unsigned short *)rec->a[i];
sum[i] = 0;
for(j = 0; j < org->h[i]; j++) {
for(k = 0; k < org->w[i]; k++) {
if(OAPV_CS_GET_FORMAT(org->cs) == OAPV_CF_PLANAR2) {
sum[i] += (((int)o[k] - (int)r[k]) >> 6) * (((int)o[k] - (int)r[k]) >> 6);
}
else {
sum[i] += (double)(o[k] - r[k]) * (o[k] - r[k]);
}
}
o = (unsigned short *)((unsigned char *)o + org->s[i]);
r = (unsigned short *)((unsigned char *)r + rec->s[i]);
}
mse[i] = sum[i] / (org->w[i] * org->h[i]);
psnr[i] = (mse[i] == 0.0) ? 100. : fabs(10 * log10(((255.f * 255.f * factor) / mse[i])));
}
}
}
static int write_data(char *fname, unsigned char *data, int size)
{
FILE *fp;
fp = fopen(fname, "ab");
if(fp == NULL) {
logerr("ERR: cannot open the output file=%s\n", fname);
return -1;
}
fwrite(data, 1, size, fp);
fclose(fp);
return 0;
}
static int clear_data(char *fname)
{
FILE *fp;
fp = fopen(fname, "wb");
if(fp == NULL) {
logerr("ERR: cannot remove file (%s)\n", fname);
return -1;
}
fclose(fp);
return 0;
}
static unsigned char char_to_hex(char a)
{
unsigned char ret;
switch(a) {
case 'a':
case 'A':
ret = 10;
break;
case 'b':
case 'B':
ret = 11;
break;
case 'c':
case 'C':
ret = 12;
break;
case 'd':
case 'D':
ret = 13;
break;
case 'e':
case 'E':
ret = 14;
break;
case 'f':
case 'F':
ret = 15;
break;
default:
ret = (unsigned char)a - '0';
break;
}
return ret;
}
#endif /* _OAPV_APP_UTIL_H_ */