Coverage Report

Created: 2025-09-08 07:52

/src/libde265/libde265/scan.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "scan.h"
22
23
static position scan0 = { 0,0 };
24
static position scan_h_1[ 2* 2], scan_v_1[ 2* 2], scan_d_1[ 2* 2];
25
static position scan_h_2[ 4* 4], scan_v_2[ 4* 4], scan_d_2[ 4* 4];
26
static position scan_h_3[ 8* 8], scan_v_3[ 8* 8], scan_d_3[ 8* 8];
27
static position scan_h_4[16*16], scan_v_4[16*16], scan_d_4[16*16];
28
static position scan_h_5[32*32], scan_v_5[32*32], scan_d_5[32*32];
29
30
static position* scan_h[7] = { &scan0,scan_h_1,scan_h_2,scan_h_3,scan_h_4,scan_h_5 };
31
static position* scan_v[7] = { &scan0,scan_v_1,scan_v_2,scan_v_3,scan_v_4,scan_v_5 };
32
static position* scan_d[7] = { &scan0,scan_d_1,scan_d_2,scan_d_3,scan_d_4,scan_d_5 };
33
34
static void init_scan_h(position* scan, int blkSize)
35
95.7k
{
36
95.7k
  int i=0;
37
1.28M
  for (int y=0;y<blkSize;y++)
38
27.3M
    for (int x=0;x<blkSize;x++)
39
26.1M
      {
40
26.1M
        scan[i].x = x;
41
26.1M
        scan[i].y = y;
42
26.1M
        i++;
43
26.1M
      }
44
95.7k
}
45
46
static void init_scan_v(position* scan, int blkSize)
47
95.7k
{
48
95.7k
  int i=0;
49
1.28M
  for (int x=0;x<blkSize;x++)
50
27.3M
    for (int y=0;y<blkSize;y++)
51
26.1M
      {
52
26.1M
        scan[i].x = x;
53
26.1M
        scan[i].y = y;
54
26.1M
        i++;
55
26.1M
      }
56
95.7k
}
57
58
static void init_scan_d(position* scan, int blkSize)
59
95.7k
{
60
95.7k
  int i=0;
61
95.7k
  int x=0,y=0;
62
63
2.27M
  do {
64
53.3M
    while (y>=0) {
65
51.0M
      if (x<blkSize && y<blkSize) {
66
26.1M
        scan[i].x = x;
67
26.1M
        scan[i].y = y;
68
26.1M
        i++;
69
26.1M
      }
70
51.0M
      y--;
71
51.0M
      x++;
72
51.0M
    }
73
74
2.27M
    y=x;
75
2.27M
    x=0;
76
2.27M
  } while (i < blkSize*blkSize);
77
95.7k
}
78
79
80
const position* get_scan_order(int log2BlockSize, int scanIdx)
81
176M
{
82
176M
  switch (scanIdx) {
83
69.3M
  case 0: return scan_d[log2BlockSize];
84
53.6M
  case 1: return scan_h[log2BlockSize];
85
53.0M
  case 2: return scan_v[log2BlockSize];
86
0
  default: return 0; // should never happen
87
176M
  }
88
176M
}
89
90
91
92
static scan_position scanpos_h_2[ 4* 4], scanpos_v_2[ 4* 4], scanpos_d_2[ 4* 4];
93
static scan_position scanpos_h_3[ 8* 8], scanpos_v_3[ 8* 8], scanpos_d_3[ 8* 8];
94
static scan_position scanpos_h_4[16*16], scanpos_v_4[16*16], scanpos_d_4[16*16];
95
static scan_position scanpos_h_5[32*32], scanpos_v_5[32*32], scanpos_d_5[32*32];
96
97
static scan_position* scanpos[3][6] =
98
  { { 0,0,scanpos_d_2,scanpos_d_3,scanpos_d_4,scanpos_d_5 },
99
    { 0,0,scanpos_h_2,scanpos_h_3,scanpos_h_4,scanpos_h_5 },
100
    { 0,0,scanpos_v_2,scanpos_v_3,scanpos_v_4,scanpos_v_5 } };
101
   
102
103
scan_position get_scan_position(int x,int y, int scanIdx, int log2BlkSize)
104
9.87M
{
105
9.87M
  return scanpos[scanIdx][log2BlkSize][ y*(1<<log2BlkSize) + x ];
106
9.87M
}
107
108
static void fill_scan_pos(scan_position* pos, int x,int y,int scanIdx, int log2TrafoSize)
109
78.1M
{
110
78.1M
  int lastScanPos = 16;
111
78.1M
  int lastSubBlock = (1<<(log2TrafoSize-2)) * (1<<(log2TrafoSize-2)) -1;
112
113
78.1M
  const position* ScanOrderSub = get_scan_order(log2TrafoSize-2, scanIdx);
114
78.1M
  const position* ScanOrderPos = get_scan_order(2, scanIdx);
115
116
78.1M
  int xC,yC;
117
32.1G
  do {
118
32.1G
    if (lastScanPos==0) {
119
1.96G
      lastScanPos=16;
120
1.96G
      lastSubBlock--;
121
1.96G
    }
122
32.1G
    lastScanPos--;
123
124
32.1G
    position S = ScanOrderSub[lastSubBlock];
125
32.1G
    xC = (S.x<<2) + ScanOrderPos[lastScanPos].x;
126
32.1G
    yC = (S.y<<2) + ScanOrderPos[lastScanPos].y;
127
128
32.1G
  } while ( (xC != x) || (yC != y));
129
130
78.1M
  pos->subBlock = lastSubBlock;
131
78.1M
  pos->scanPos  = lastScanPos;
132
78.1M
}
133
134
135
void init_scan_orders()
136
19.1k
{
137
114k
  for (int log2size=1;log2size<=5;log2size++)
138
95.7k
    {
139
95.7k
      init_scan_h(scan_h[log2size], 1<<log2size);
140
95.7k
      init_scan_v(scan_v[log2size], 1<<log2size);
141
95.7k
      init_scan_d(scan_d[log2size], 1<<log2size);
142
95.7k
    }
143
144
145
95.7k
  for (int log2size=2;log2size<=5;log2size++)
146
306k
    for (int scanIdx=0;scanIdx<3;scanIdx++)
147
3.67M
      for (int y=0;y<(1<<log2size);y++)
148
81.5M
        for (int x=0;x<(1<<log2size);x++)
149
78.1M
          {
150
78.1M
            fill_scan_pos(&scanpos[scanIdx][log2size][ y*(1<<log2size) + x ],x,y,scanIdx,log2size);
151
78.1M
          }
152
19.1k
}