Merge "Detect toolchain based on gcc -dumpmachine"
[profile/ivi/libvpx.git] / vp8 / decoder / decodemv.c
1 /*
2  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license 
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may 
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11
12 #include "treereader.h"
13 #include "entropymv.h"
14 #include "entropymode.h"
15 #include "onyxd_int.h"
16 #include "findnearmv.h"
17 #include "demode.h"
18 #if CONFIG_DEBUG
19 #include <assert.h>
20 #endif
21
22 static int read_mvcomponent(vp8_reader *r, const MV_CONTEXT *mvc)
23 {
24     const vp8_prob *const p = (const vp8_prob *) mvc;
25     int x = 0;
26
27     if (vp8_read(r, p [mvpis_short]))  /* Large */
28     {
29         int i = 0;
30
31         do
32         {
33             x += vp8_read(r, p [MVPbits + i]) << i;
34         }
35         while (++i < 3);
36
37         i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
38
39         do
40         {
41             x += vp8_read(r, p [MVPbits + i]) << i;
42         }
43         while (--i > 3);
44
45         if (!(x & 0xFFF0)  ||  vp8_read(r, p [MVPbits + 3]))
46             x += 8;
47     }
48     else   /* small */
49         x = vp8_treed_read(r, vp8_small_mvtree, p + MVPshort);
50
51     if (x  &&  vp8_read(r, p [MVPsign]))
52         x = -x;
53
54     return x;
55 }
56
57 static void read_mv(vp8_reader *r, MV *mv, const MV_CONTEXT *mvc)
58 {
59     mv->row = (short)(read_mvcomponent(r,   mvc) << 1);
60     mv->col = (short)(read_mvcomponent(r, ++mvc) << 1);
61 }
62
63
64 static void read_mvcontexts(vp8_reader *bc, MV_CONTEXT *mvc)
65 {
66     int i = 0;
67
68     do
69     {
70         const vp8_prob *up = vp8_mv_update_probs[i].prob;
71         vp8_prob *p = (vp8_prob *)(mvc + i);
72         vp8_prob *const pstop = p + MVPcount;
73
74         do
75         {
76             if (vp8_read(bc, *up++))
77             {
78                 const vp8_prob x = (vp8_prob)vp8_read_literal(bc, 7);
79
80                 *p = x ? x << 1 : 1;
81             }
82         }
83         while (++p < pstop);
84     }
85     while (++i < 2);
86 }
87
88
89 static MB_PREDICTION_MODE read_mv_ref(vp8_reader *bc, const vp8_prob *p)
90 {
91     const int i = vp8_treed_read(bc, vp8_mv_ref_tree, p);
92
93     return (MB_PREDICTION_MODE)i;
94 }
95
96 static MB_PREDICTION_MODE sub_mv_ref(vp8_reader *bc, const vp8_prob *p)
97 {
98     const int i = vp8_treed_read(bc, vp8_sub_mv_ref_tree, p);
99
100     return (MB_PREDICTION_MODE)i;
101 }
102 unsigned int vp8_mv_cont_count[5][4] =
103 {
104     { 0, 0, 0, 0 },
105     { 0, 0, 0, 0 },
106     { 0, 0, 0, 0 },
107     { 0, 0, 0, 0 },
108     { 0, 0, 0, 0 }
109 };
110
111 void vp8_decode_mode_mvs(VP8D_COMP *pbi)
112 {
113     const MV Zero = { 0, 0};
114
115     VP8_COMMON *const pc = & pbi->common;
116     vp8_reader *const bc = & pbi->bc;
117
118     MODE_INFO *mi = pc->mi, *ms;
119     const int mis = pc->mode_info_stride;
120
121     MV_CONTEXT *const mvc = pc->fc.mvc;
122
123     int mb_row = -1;
124
125     vp8_prob prob_intra;
126     vp8_prob prob_last;
127     vp8_prob prob_gf;
128     vp8_prob prob_skip_false = 0;
129
130     if (pc->mb_no_coeff_skip)
131         prob_skip_false = (vp8_prob)vp8_read_literal(bc, 8);
132
133     prob_intra = (vp8_prob)vp8_read_literal(bc, 8);
134     prob_last  = (vp8_prob)vp8_read_literal(bc, 8);
135     prob_gf    = (vp8_prob)vp8_read_literal(bc, 8);
136
137     ms = pc->mi - 1;
138
139     if (vp8_read_bit(bc))
140     {
141         int i = 0;
142
143         do
144         {
145             pc->fc.ymode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
146         }
147         while (++i < 4);
148     }
149
150     if (vp8_read_bit(bc))
151     {
152         int i = 0;
153
154         do
155         {
156             pc->fc.uv_mode_prob[i] = (vp8_prob) vp8_read_literal(bc, 8);
157         }
158         while (++i < 3);
159     }
160
161     read_mvcontexts(bc, mvc);
162
163     while (++mb_row < pc->mb_rows)
164     {
165         int mb_col = -1;
166
167         while (++mb_col < pc->mb_cols)
168         {
169             MB_MODE_INFO *const mbmi = & mi->mbmi;
170             MV *const mv = & mbmi->mv.as_mv;
171             VP8_COMMON *const pc = &pbi->common;
172             MACROBLOCKD *xd = &pbi->mb;
173
174             mbmi->need_to_clamp_mvs = 0;
175             vp8dx_bool_decoder_fill(bc);
176
177             // Distance of Mb to the various image edges.
178             // These specified to 8th pel as they are always compared to MV values that are in 1/8th pel units
179             xd->mb_to_left_edge = -((mb_col * 16) << 3);
180             xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
181             xd->mb_to_top_edge = -((mb_row * 16)) << 3;
182             xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
183
184             // If required read in new segmentation data for this MB
185             if (pbi->mb.update_mb_segmentation_map)
186                 vp8_read_mb_features(bc, mbmi, &pbi->mb);
187
188             // Read the macroblock coeff skip flag if this feature is in use, else default to 0
189             if (pc->mb_no_coeff_skip)
190                 mbmi->mb_skip_coeff = vp8_read(bc, prob_skip_false);
191             else
192                 mbmi->mb_skip_coeff = 0;
193
194             mbmi->uv_mode = DC_PRED;
195
196             if ((mbmi->ref_frame = (MV_REFERENCE_FRAME) vp8_read(bc, prob_intra)))    /* inter MB */
197             {
198                 int rct[4];
199                 vp8_prob mv_ref_p [VP8_MVREFS-1];
200                 MV nearest, nearby, best_mv;
201
202                 if (vp8_read(bc, prob_last))
203                 {
204                     mbmi->ref_frame = (MV_REFERENCE_FRAME)((int)mbmi->ref_frame + (int)(1 + vp8_read(bc, prob_gf)));
205                 }
206
207                 vp8_find_near_mvs(xd, mi, &nearest, &nearby, &best_mv, rct, mbmi->ref_frame, pbi->common.ref_frame_sign_bias);
208
209                 vp8_mv_ref_probs(mv_ref_p, rct);
210
211                 switch (mbmi->mode = read_mv_ref(bc, mv_ref_p))
212                 {
213                 case SPLITMV:
214                 {
215                     const int s = mbmi->partitioning = vp8_treed_read(
216                                                            bc, vp8_mbsplit_tree, vp8_mbsplit_probs
217                                                        );
218                     const int num_p = vp8_mbsplit_count [s];
219                     const int *const  L = vp8_mbsplits [s];
220                     int j = 0;
221
222                     do  /* for each subset j */
223                     {
224                         B_MODE_INFO *const bmi = mbmi->partition_bmi + j;
225                         MV *const mv = & bmi->mv.as_mv;
226
227                         int k = -1;  /* first block in subset j */
228                         int mv_contz;
229
230                         while (j != L[++k])
231                             if (k >= 16)
232 #if CONFIG_DEBUG
233                                 assert(0);
234
235 #else
236                                 ;
237 #endif
238
239                         mv_contz = vp8_mv_cont(&(vp8_left_bmi(mi, k)->mv.as_mv), &(vp8_above_bmi(mi, k, mis)->mv.as_mv));
240
241                         switch (bmi->mode = (B_PREDICTION_MODE) sub_mv_ref(bc, vp8_sub_mv_ref_prob2 [mv_contz])) //pc->fc.sub_mv_ref_prob))
242                         {
243                         case NEW4X4:
244                             read_mv(bc, mv, (const MV_CONTEXT *) mvc);
245                             mv->row += best_mv.row;
246                             mv->col += best_mv.col;
247 #ifdef VPX_MODE_COUNT
248                             vp8_mv_cont_count[mv_contz][3]++;
249 #endif
250                             break;
251                         case LEFT4X4:
252                             *mv = vp8_left_bmi(mi, k)->mv.as_mv;
253 #ifdef VPX_MODE_COUNT
254                             vp8_mv_cont_count[mv_contz][0]++;
255 #endif
256                             break;
257                         case ABOVE4X4:
258                             *mv = vp8_above_bmi(mi, k, mis)->mv.as_mv;
259 #ifdef VPX_MODE_COUNT
260                             vp8_mv_cont_count[mv_contz][1]++;
261 #endif
262                             break;
263                         case ZERO4X4:
264                             *mv = Zero;
265 #ifdef VPX_MODE_COUNT
266                             vp8_mv_cont_count[mv_contz][2]++;
267 #endif
268                             break;
269                         default:
270                             break;
271                         }
272
273                         if (mv->col < xd->mb_to_left_edge
274                                       - LEFT_TOP_MARGIN
275                             || mv->col > xd->mb_to_right_edge
276                                          + RIGHT_BOTTOM_MARGIN
277                             || mv->row < xd->mb_to_top_edge
278                                          - LEFT_TOP_MARGIN
279                             || mv->row > xd->mb_to_bottom_edge
280                                          + RIGHT_BOTTOM_MARGIN
281                             )
282                             mbmi->need_to_clamp_mvs = 1;
283
284                         /* Fill (uniform) modes, mvs of jth subset.
285                            Must do it here because ensuing subsets can
286                            refer back to us via "left" or "above". */
287                         do
288                             if (j == L[k])
289                                 mi->bmi[k] = *bmi;
290
291                         while (++k < 16);
292                     }
293                     while (++j < num_p);
294                 }
295
296                 *mv = mi->bmi[15].mv.as_mv;
297
298                 break;  /* done with SPLITMV */
299
300                 case NEARMV:
301                     *mv = nearby;
302
303                     // Clip "next_nearest" so that it does not extend to far out of image
304                     if (mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
305                         mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
306                     else if (mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
307                         mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
308
309                     if (mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
310                         mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
311                     else if (mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
312                         mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
313
314                     goto propagate_mv;
315
316                 case NEARESTMV:
317                     *mv = nearest;
318
319                     // Clip "next_nearest" so that it does not extend to far out of image
320                     if (mv->col < (xd->mb_to_left_edge - LEFT_TOP_MARGIN))
321                         mv->col = xd->mb_to_left_edge - LEFT_TOP_MARGIN;
322                     else if (mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN)
323                         mv->col = xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN;
324
325                     if (mv->row < (xd->mb_to_top_edge - LEFT_TOP_MARGIN))
326                         mv->row = xd->mb_to_top_edge - LEFT_TOP_MARGIN;
327                     else if (mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN)
328                         mv->row = xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN;
329
330                     goto propagate_mv;
331
332                 case ZEROMV:
333                     *mv = Zero;
334                     goto propagate_mv;
335
336                 case NEWMV:
337                     read_mv(bc, mv, (const MV_CONTEXT *) mvc);
338                     mv->row += best_mv.row;
339                     mv->col += best_mv.col;
340
341                     /* Don't need to check this on NEARMV and NEARESTMV modes
342                      * since those modes clamp the MV. The NEWMV mode does not,
343                      * so signal to the prediction stage whether special
344                      * handling may be required.
345                      */
346                     if (mv->col < xd->mb_to_left_edge - LEFT_TOP_MARGIN
347                         || mv->col > xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN
348                         || mv->row < xd->mb_to_top_edge - LEFT_TOP_MARGIN
349                         || mv->row > xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN
350                         )
351                         mbmi->need_to_clamp_mvs = 1;
352
353                 propagate_mv:  /* same MV throughout */
354                     {
355                         //int i=0;
356                         //do
357                         //{
358                         //  mi->bmi[i].mv.as_mv = *mv;
359                         //}
360                         //while( ++i < 16);
361
362                         mi->bmi[0].mv.as_mv = *mv;
363                         mi->bmi[1].mv.as_mv = *mv;
364                         mi->bmi[2].mv.as_mv = *mv;
365                         mi->bmi[3].mv.as_mv = *mv;
366                         mi->bmi[4].mv.as_mv = *mv;
367                         mi->bmi[5].mv.as_mv = *mv;
368                         mi->bmi[6].mv.as_mv = *mv;
369                         mi->bmi[7].mv.as_mv = *mv;
370                         mi->bmi[8].mv.as_mv = *mv;
371                         mi->bmi[9].mv.as_mv = *mv;
372                         mi->bmi[10].mv.as_mv = *mv;
373                         mi->bmi[11].mv.as_mv = *mv;
374                         mi->bmi[12].mv.as_mv = *mv;
375                         mi->bmi[13].mv.as_mv = *mv;
376                         mi->bmi[14].mv.as_mv = *mv;
377                         mi->bmi[15].mv.as_mv = *mv;
378                     }
379
380                     break;
381
382                 default:;
383 #if CONFIG_DEBUG
384                     assert(0);
385 #endif
386                 }
387             }
388             else
389             {
390                 /* MB is intra coded */
391
392                 int j = 0;
393
394                 do
395                 {
396                     mi->bmi[j].mv.as_mv = Zero;
397                 }
398                 while (++j < 16);
399
400                 *mv = Zero;
401
402                 if ((mbmi->mode = (MB_PREDICTION_MODE) vp8_read_ymode(bc, pc->fc.ymode_prob)) == B_PRED)
403                 {
404                     int j = 0;
405
406                     do
407                     {
408                         mi->bmi[j].mode = (B_PREDICTION_MODE)vp8_read_bmode(bc, pc->fc.bmode_prob);
409                     }
410                     while (++j < 16);
411                 }
412
413                 mbmi->uv_mode = (MB_PREDICTION_MODE)vp8_read_uv_mode(bc, pc->fc.uv_mode_prob);
414             }
415
416             mi++;       // next macroblock
417         }
418
419         mi++;           // skip left predictor each row
420     }
421 }