1/*
2 *  Copyright (c) 2010 The WebM 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 "vp8/common/common.h"
13#include "encodemv.h"
14#include "vp8/common/entropymode.h"
15#include "vp8/common/systemdependent.h"
16
17#include <math.h>
18
19#ifdef VP8_ENTROPY_STATS
20extern unsigned int active_section;
21#endif
22
23static void encode_mvcomponent(
24    vp8_writer *const w,
25    const int v,
26    const struct mv_context *mvc
27)
28{
29    const vp8_prob *p = mvc->prob;
30    const int x = v < 0 ? -v : v;
31
32    if (x < mvnum_short)     /* Small */
33    {
34        vp8_write(w, 0, p [mvpis_short]);
35        vp8_treed_write(w, vp8_small_mvtree, p + MVPshort, x, 3);
36
37        if (!x)
38            return;         /* no sign bit */
39    }
40    else                    /* Large */
41    {
42        int i = 0;
43
44        vp8_write(w, 1, p [mvpis_short]);
45
46        do
47            vp8_write(w, (x >> i) & 1, p [MVPbits + i]);
48
49        while (++i < 3);
50
51        i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
52
53        do
54            vp8_write(w, (x >> i) & 1, p [MVPbits + i]);
55
56        while (--i > 3);
57
58        if (x & 0xFFF0)
59            vp8_write(w, (x >> 3) & 1, p [MVPbits + 3]);
60    }
61
62    vp8_write(w, v < 0, p [MVPsign]);
63}
64#if 0
65static int max_mv_r = 0;
66static int max_mv_c = 0;
67#endif
68void vp8_encode_motion_vector(vp8_writer *w, const MV *mv, const MV_CONTEXT *mvc)
69{
70
71#if 0
72    {
73        if (abs(mv->row >> 1) > max_mv_r)
74        {
75            FILE *f = fopen("maxmv.stt", "a");
76            max_mv_r = abs(mv->row >> 1);
77            fprintf(f, "New Mv Row Max %6d\n", (mv->row >> 1));
78
79            if ((abs(mv->row) / 2) != max_mv_r)
80                fprintf(f, "MV Row conversion error %6d\n", abs(mv->row) / 2);
81
82            fclose(f);
83        }
84
85        if (abs(mv->col >> 1) > max_mv_c)
86        {
87            FILE *f = fopen("maxmv.stt", "a");
88            fprintf(f, "New Mv Col Max %6d\n", (mv->col >> 1));
89            max_mv_c = abs(mv->col >> 1);
90            fclose(f);
91        }
92    }
93#endif
94
95    encode_mvcomponent(w, mv->row >> 1, &mvc[0]);
96    encode_mvcomponent(w, mv->col >> 1, &mvc[1]);
97}
98
99
100static unsigned int cost_mvcomponent(const int v, const struct mv_context *mvc)
101{
102    const vp8_prob *p = mvc->prob;
103    const int x = v;
104    unsigned int cost;
105
106    if (x < mvnum_short)
107    {
108        cost = vp8_cost_zero(p [mvpis_short])
109               + vp8_treed_cost(vp8_small_mvtree, p + MVPshort, x, 3);
110
111        if (!x)
112            return cost;
113    }
114    else
115    {
116        int i = 0;
117        cost = vp8_cost_one(p [mvpis_short]);
118
119        do
120            cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1);
121
122        while (++i < 3);
123
124        i = mvlong_width - 1;  /* Skip bit 3, which is sometimes implicit */
125
126        do
127            cost += vp8_cost_bit(p [MVPbits + i], (x >> i) & 1);
128
129        while (--i > 3);
130
131        if (x & 0xFFF0)
132            cost += vp8_cost_bit(p [MVPbits + 3], (x >> 3) & 1);
133    }
134
135    return cost;   /* + vp8_cost_bit( p [MVPsign], v < 0); */
136}
137
138void vp8_build_component_cost_table(int *mvcost[2], const MV_CONTEXT *mvc, int mvc_flag[2])
139{
140    int i = 1;
141    unsigned int cost0 = 0;
142    unsigned int cost1 = 0;
143
144    vp8_clear_system_state();
145
146    i = 1;
147
148    if (mvc_flag[0])
149    {
150        mvcost [0] [0] = cost_mvcomponent(0, &mvc[0]);
151
152        do
153        {
154            cost0 = cost_mvcomponent(i, &mvc[0]);
155
156            mvcost [0] [i] = cost0 + vp8_cost_zero(mvc[0].prob[MVPsign]);
157            mvcost [0] [-i] = cost0 + vp8_cost_one(mvc[0].prob[MVPsign]);
158        }
159        while (++i <= mv_max);
160    }
161
162    i = 1;
163
164    if (mvc_flag[1])
165    {
166        mvcost [1] [0] = cost_mvcomponent(0, &mvc[1]);
167
168        do
169        {
170            cost1 = cost_mvcomponent(i, &mvc[1]);
171
172            mvcost [1] [i] = cost1 + vp8_cost_zero(mvc[1].prob[MVPsign]);
173            mvcost [1] [-i] = cost1 + vp8_cost_one(mvc[1].prob[MVPsign]);
174        }
175        while (++i <= mv_max);
176    }
177}
178
179
180/* Motion vector probability table update depends on benefit.
181 * Small correction allows for the fact that an update to an MV probability
182 * may have benefit in subsequent frames as well as the current one.
183 */
184#define MV_PROB_UPDATE_CORRECTION   -1
185
186
187static void calc_prob(vp8_prob *p, const unsigned int ct[2])
188{
189    const unsigned int tot = ct[0] + ct[1];
190
191    if (tot)
192    {
193        const vp8_prob x = ((ct[0] * 255) / tot) & -2;
194        *p = x ? x : 1;
195    }
196}
197
198static void update(
199    vp8_writer *const w,
200    const unsigned int ct[2],
201    vp8_prob *const cur_p,
202    const vp8_prob new_p,
203    const vp8_prob update_p,
204    int *updated
205)
206{
207    const int cur_b = vp8_cost_branch(ct, *cur_p);
208    const int new_b = vp8_cost_branch(ct, new_p);
209    const int cost = 7 + MV_PROB_UPDATE_CORRECTION + ((vp8_cost_one(update_p) - vp8_cost_zero(update_p) + 128) >> 8);
210
211    if (cur_b - new_b > cost)
212    {
213        *cur_p = new_p;
214        vp8_write(w, 1, update_p);
215        vp8_write_literal(w, new_p >> 1, 7);
216        *updated = 1;
217
218    }
219    else
220        vp8_write(w, 0, update_p);
221}
222
223static void write_component_probs(
224    vp8_writer *const w,
225    struct mv_context *cur_mvc,
226    const struct mv_context *default_mvc_,
227    const struct mv_context *update_mvc,
228    const unsigned int events [MVvals],
229    unsigned int rc,
230    int *updated
231)
232{
233    vp8_prob *Pcur = cur_mvc->prob;
234    const vp8_prob *default_mvc = default_mvc_->prob;
235    const vp8_prob *Pupdate = update_mvc->prob;
236    unsigned int is_short_ct[2], sign_ct[2];
237
238    unsigned int bit_ct [mvlong_width] [2];
239
240    unsigned int short_ct  [mvnum_short];
241    unsigned int short_bct [mvnum_short-1] [2];
242
243    vp8_prob Pnew [MVPcount];
244
245    (void) rc;
246    vp8_copy_array(Pnew, default_mvc, MVPcount);
247
248    vp8_zero(is_short_ct)
249    vp8_zero(sign_ct)
250    vp8_zero(bit_ct)
251    vp8_zero(short_ct)
252    vp8_zero(short_bct)
253
254
255    /* j=0 */
256    {
257        const int c = events [mv_max];
258
259        is_short_ct [0] += c;     /* Short vector */
260        short_ct [0] += c;       /* Magnitude distribution */
261    }
262
263    /* j: 1 ~ mv_max (1023) */
264    {
265        int j = 1;
266
267        do
268        {
269            const int c1 = events [mv_max + j];  /* positive */
270            const int c2 = events [mv_max - j];  /* negative */
271            const int c  = c1 + c2;
272            int a = j;
273
274            sign_ct [0] += c1;
275            sign_ct [1] += c2;
276
277            if (a < mvnum_short)
278            {
279                is_short_ct [0] += c;     /* Short vector */
280                short_ct [a] += c;       /* Magnitude distribution */
281            }
282            else
283            {
284                int k = mvlong_width - 1;
285                is_short_ct [1] += c;     /* Long vector */
286
287                /*  bit 3 not always encoded. */
288                do
289                    bit_ct [k] [(a >> k) & 1] += c;
290
291                while (--k >= 0);
292            }
293        }
294        while (++j <= mv_max);
295    }
296
297    calc_prob(Pnew + mvpis_short, is_short_ct);
298
299    calc_prob(Pnew + MVPsign, sign_ct);
300
301    {
302        vp8_prob p [mvnum_short - 1];    /* actually only need branch ct */
303        int j = 0;
304
305        vp8_tree_probs_from_distribution(
306            8, vp8_small_mvencodings, vp8_small_mvtree,
307            p, short_bct, short_ct,
308            256, 1
309        );
310
311        do
312            calc_prob(Pnew + MVPshort + j, short_bct[j]);
313
314        while (++j < mvnum_short - 1);
315    }
316
317    {
318        int j = 0;
319
320        do
321            calc_prob(Pnew + MVPbits + j, bit_ct[j]);
322
323        while (++j < mvlong_width);
324    }
325
326    update(w, is_short_ct, Pcur + mvpis_short, Pnew[mvpis_short], *Pupdate++, updated);
327
328    update(w, sign_ct, Pcur + MVPsign, Pnew[MVPsign], *Pupdate++, updated);
329
330    {
331        const vp8_prob *const new_p = Pnew + MVPshort;
332        vp8_prob *const cur_p = Pcur + MVPshort;
333
334        int j = 0;
335
336        do
337
338            update(w, short_bct[j], cur_p + j, new_p[j], *Pupdate++, updated);
339
340        while (++j < mvnum_short - 1);
341    }
342
343    {
344        const vp8_prob *const new_p = Pnew + MVPbits;
345        vp8_prob *const cur_p = Pcur + MVPbits;
346
347        int j = 0;
348
349        do
350
351            update(w, bit_ct[j], cur_p + j, new_p[j], *Pupdate++, updated);
352
353        while (++j < mvlong_width);
354    }
355}
356
357void vp8_write_mvprobs(VP8_COMP *cpi)
358{
359    vp8_writer *const w  = cpi->bc;
360    MV_CONTEXT *mvc = cpi->common.fc.mvc;
361    int flags[2] = {0, 0};
362#ifdef VP8_ENTROPY_STATS
363    active_section = 4;
364#endif
365    write_component_probs(
366        w, &mvc[0], &vp8_default_mv_context[0], &vp8_mv_update_probs[0],
367        cpi->mb.MVcount[0], 0, &flags[0]
368    );
369    write_component_probs(
370        w, &mvc[1], &vp8_default_mv_context[1], &vp8_mv_update_probs[1],
371        cpi->mb.MVcount[1], 1, &flags[1]
372    );
373
374    if (flags[0] || flags[1])
375        vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flags);
376
377#ifdef VP8_ENTROPY_STATS
378    active_section = 5;
379#endif
380}
381