summaryrefslogtreecommitdiff
path: root/vp8/common/implicit_segmentation.c
blob: 68bb31cca2f29984c9b26e5b1f3142ca10e1f588 (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
/*
 *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "vp8/common/onyxc_int.h"

#define MAX_REGIONS 24000
#ifndef NULL
#define NULL 0
#endif

#define min_mbs_in_region 3

// this linked list structure holds equivalences for connected
// component labeling
struct list_el {
   int label;
   int seg_value;
   int count;
   struct list_el * next;
};
typedef struct list_el item;

// connected colorsegments
typedef struct
{
  int min_x;
  int min_y;
  int max_x;
  int max_y;
  long long sum_x;
  long long sum_y;
  int pixels;
  int seg_value;
  int label;
} segment_info;


typedef enum
{
  SEGMENT_MODE,
  SEGMENT_MV,
  SEGMENT_REFFRAME,
  SEGMENT_SKIPPED
} SEGMENT_TYPE;


// this merges the two equivalence lists and
// then makes sure that every label points to the same
// equivalence list
void merge ( item *labels, int u, int v )
{
  item *a = labels[u].next;
  item *b = labels[v].next;
  item c;
  item *it = &c;
  int count;

  // check if they are already merged
  if(u==v || a==b)
    return;

  count = a->count + b->count;

  // merge 2 sorted linked lists.
  while ( a != NULL && b != NULL )
  {
    if ( a->label < b->label)
    {
      it->next = a;
      a = a->next;
    }
    else
    {
      it->next = b;
      b = b->next;
    }

    it = it->next;
  }

  if ( a == NULL )
    it->next = b;
  else
    it->next = a;

  it = c.next;

  // make sure every equivalence in the linked list points to this new ll
  while( it != NULL)
  {
    labels[it->label].next = c.next;
    it=it->next;
  }
  c.next->count = count;

}

void segment_via_mode_info( VP8_COMMON *oci, int how)
{
    MODE_INFO *mi = oci->mi;
    int i,j;
    int mb_index = 0;

    int label=1;
    int pitch = oci->mb_cols;

    // holds linked list equivalences
    // the max should probably be allocated at a higher level in oci
    item equivalences[MAX_REGIONS];
    int eq_ptr = 0;
    item labels[MAX_REGIONS];
    segment_info segments[MAX_REGIONS];
    int label_count = 1;
    int labeling[400*300];
    int *lp = labeling;

    label_count = 1;
    memset(labels,0,sizeof(labels));
    memset(segments,0,sizeof(segments));

    /* Go through each macroblock first pass labelling */
    for (i = 0; i < oci->mb_rows; i++,lp+=pitch)
    {
        for (j = 0; j < oci->mb_cols; j++)
        {
          // int above seg_value, left seg_value, this seg_value...
          int a=-1,l=-1,n=-1;

          // above label, left label
          int al=-1,ll=-1;
          if(i)
          {
            al=lp[j-pitch];
            a = labels[al].next->seg_value;
          }
          if(j)
          {
            ll=lp[j-1];
            l = labels[ll].next->seg_value;
          }

          // what setting are we going to do the implicit segmentation on
          switch (how)
          {
            case SEGMENT_MODE:
              n= mi[mb_index].mbmi.mode;
              break;
            case SEGMENT_MV:
              n = mi[mb_index].mbmi.mv.as_int;
              if(mi[mb_index].mbmi.ref_frame == INTRA_FRAME)
                 n=-9999999;
              break;
            case SEGMENT_REFFRAME:
              n = mi[mb_index].mbmi.ref_frame;
              break;
            case SEGMENT_SKIPPED:
              n = mi[mb_index].mbmi.mb_skip_coeff;
              break;
          }

          // above and left both have the same seg_value
          if(n==a&&n==l)
          {
            // pick the lowest label
            lp[j] = (al<ll?al:ll);
            labels[lp[j]].next->count++;

            // merge the above and left equivalencies
            merge( labels, al, ll );
          }
          // this matches above seg_value
          else if(n==a)
          {
              // give it the same label as above
              lp[j]=al;
              labels[al].next->count++;
          }
          // this matches left seg_value
          else if(n==l)
          {
              // give it the same label as above
              lp[j]=ll;
              labels[ll].next->count++;
          }
          else
          {
              // new label doesn't match either
              item *e = &labels[label];
              item *nl = &equivalences[eq_ptr++];
              lp[j]=label;
              nl->label = label;
              nl->next = 0;
              nl->seg_value = n;
              nl->count = 1;
              e->next = nl;
              label++;
          }
          mb_index++;
      }
      mb_index++;
    }
    lp = labeling;

    // give new labels to regions
    for(i=1;i<label;i++)
      if(labels[i].next->count >min_mbs_in_region  &&  labels[labels[i].next->label].label == 0 )
      {
        segment_info *cs= &segments[label_count];
        cs->label = label_count;
        labels[labels[i].next->label].label = label_count++;
        labels[labels[i].next->label].seg_value  = labels[i].next->seg_value;
        cs->seg_value = labels[labels[i].next->label].seg_value;
        cs->min_x = oci->mb_cols;
        cs->min_y = oci->mb_rows;
        cs->max_x = 0;
        cs->max_y = 0;
        cs->sum_x = 0;
        cs->sum_y = 0;
        cs->pixels= 0;

      }
    lp = labeling;

    // this is just to gather stats...
    for(i=0;i<oci->mb_rows;i++,lp+=pitch)
    {
      for(j=0;j<oci->mb_cols;j++)
      {
         segment_info *cs;
         int oldlab = labels[lp[j]].next->label;
         int lab = labels[oldlab].label;
         lp[j] = lab;

         cs= &segments[lab];

         cs->min_x = (j<cs->min_x?j:cs->min_x);
         cs->max_x = (j>cs->max_x?j:cs->max_x);
         cs->min_y = (i<cs->min_y?i:cs->min_y);
         cs->max_y = (i>cs->max_y?i:cs->max_y);
         cs->sum_x += j;
         cs->sum_y += i;
         cs->pixels ++;

         lp[j] = lab;
         mb_index++;
      }
      mb_index++;
    }

    {
      lp = labeling;
      printf("labelling \n");
      mb_index = 0;
      for(i=0;i<oci->mb_rows;i++,lp+=pitch)
      {
        for(j=0;j<oci->mb_cols;j++)
        {
          printf("%4d",lp[j]);
        }
        printf("            ");
        for(j=0;j<oci->mb_cols;j++,mb_index++)
        {
          //printf("%3d",mi[mb_index].mbmi.mode );
          printf("%4d:%4d",mi[mb_index].mbmi.mv.as_mv.row,mi[mb_index].mbmi.mv.as_mv.col );
        }
        printf("\n");
        ++mb_index;
      }
      printf("\n");
    }
}