1 /*
2  *  Copyright (c) 2013 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 #include <math.h>
12 
13 #include "vpx_ports/mem.h"
14 #include "vpx_ports/system_state.h"
15 
16 #include "vp9/encoder/vp9_aq_360.h"
17 #include "vp9/encoder/vp9_aq_variance.h"
18 
19 #include "vp9/common/vp9_seg_common.h"
20 
21 #include "vp9/encoder/vp9_ratectrl.h"
22 #include "vp9/encoder/vp9_rd.h"
23 #include "vp9/encoder/vp9_segmentation.h"
24 
25 static const double rate_ratio[MAX_SEGMENTS] = { 1.0, 0.75, 0.6, 0.5,
26                                                  0.4, 0.3,  0.25 };
27 
28 // Sets segment id 0 for the equatorial region, 1 for temperate region
29 // and 2 for the polar regions
vp9_360aq_segment_id(int mi_row,int mi_rows)30 unsigned int vp9_360aq_segment_id(int mi_row, int mi_rows) {
31   if (mi_row < mi_rows / 8 || mi_row > mi_rows - mi_rows / 8)
32     return 2;
33   else if (mi_row < mi_rows / 4 || mi_row > mi_rows - mi_rows / 4)
34     return 1;
35   else
36     return 0;
37 }
38 
vp9_360aq_frame_setup(VP9_COMP * cpi)39 void vp9_360aq_frame_setup(VP9_COMP *cpi) {
40   VP9_COMMON *cm = &cpi->common;
41   struct segmentation *seg = &cm->seg;
42   int i;
43 
44   if (frame_is_intra_only(cm) || cpi->force_update_segmentation ||
45       cm->error_resilient_mode) {
46     vp9_enable_segmentation(seg);
47     vp9_clearall_segfeatures(seg);
48 
49     seg->abs_delta = SEGMENT_DELTADATA;
50 
51     vpx_clear_system_state();
52 
53     for (i = 0; i < MAX_SEGMENTS; ++i) {
54       int qindex_delta =
55           vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
56                                      rate_ratio[i], cm->bit_depth);
57 
58       // We don't allow qindex 0 in a segment if the base value is not 0.
59       // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment
60       // Q delta is sometimes applied without going back around the rd loop.
61       // This could lead to an illegal combination of partition size and q.
62       if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
63         qindex_delta = -cm->base_qindex + 1;
64       }
65 
66       // No need to enable SEG_LVL_ALT_Q for this segment.
67       if (rate_ratio[i] == 1.0) {
68         continue;
69       }
70 
71       vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta);
72       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
73     }
74   }
75 }
76