vp9_alloccommon.c 5.22 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


12
#include "./vpx_config.h"
13
#include "vp9/common/vp9_blockd.h"
John Koleszar's avatar
John Koleszar committed
14
#include "vpx_mem/vpx_mem.h"
15
16
17
18
19
#include "vp9/common/vp9_onyxc_int.h"
#include "vp9/common/vp9_findnearmv.h"
#include "vp9/common/vp9_entropymode.h"
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_systemdependent.h"
John Koleszar's avatar
John Koleszar committed
20
21


Dmitry Kovalev's avatar
Dmitry Kovalev committed
22
23
void vp9_update_mode_info_border(VP9_COMMON *cpi, MODE_INFO *mi) {
  const int stride = cpi->mode_info_stride;
John Koleszar's avatar
John Koleszar committed
24
25
26
  int i;

  // Clear down top border row
Dmitry Kovalev's avatar
Dmitry Kovalev committed
27
  vpx_memset(mi, 0, sizeof(MODE_INFO) * stride);
John Koleszar's avatar
John Koleszar committed
28
29

  // Clear left border column
Dmitry Kovalev's avatar
Dmitry Kovalev committed
30
31
  for (i = 1; i < cpi->mb_rows + 1; i++)
    vpx_memset(&mi[i * stride], 0, sizeof(MODE_INFO));
32
}
33

34
void vp9_update_mode_info_in_image(VP9_COMMON *cpi, MODE_INFO *mi) {
John Koleszar's avatar
John Koleszar committed
35
36
37
38
39
40
  int i, j;

  // For each in image mode_info element set the in image flag to 1
  for (i = 0; i < cpi->mb_rows; i++) {
    for (j = 0; j < cpi->mb_cols; j++) {
      mi->mbmi.mb_in_image = 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
41
      mi++;  // Next element in the row
John Koleszar's avatar
John Koleszar committed
42
    }
John Koleszar's avatar
John Koleszar committed
43

Dmitry Kovalev's avatar
Dmitry Kovalev committed
44
    mi++;  // Step over border element at start of next row
John Koleszar's avatar
John Koleszar committed
45
  }
John Koleszar's avatar
John Koleszar committed
46
}
47

Dmitry Kovalev's avatar
Dmitry Kovalev committed
48
void vp9_free_frame_buffers(VP9_COMMON *oci) {
John Koleszar's avatar
John Koleszar committed
49
  int i;
50

John Koleszar's avatar
John Koleszar committed
51
52
  for (i = 0; i < NUM_YV12_BUFFERS; i++)
    vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
53

John Koleszar's avatar
John Koleszar committed
54
55
  vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
  vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
John Koleszar's avatar
John Koleszar committed
56

John Koleszar's avatar
John Koleszar committed
57
58
59
  vpx_free(oci->above_context);
  vpx_free(oci->mip);
  vpx_free(oci->prev_mip);
John Koleszar's avatar
John Koleszar committed
60

John Koleszar's avatar
John Koleszar committed
61
62
63
  oci->above_context = 0;
  oci->mip = 0;
  oci->prev_mip = 0;
John Koleszar's avatar
John Koleszar committed
64
65
66

}

67
int vp9_alloc_frame_buffers(VP9_COMMON *oci, int width, int height) {
John Koleszar's avatar
John Koleszar committed
68
  int i;
69

Dmitry Kovalev's avatar
Dmitry Kovalev committed
70
71
72
  // Our internal buffers are always multiples of 16
  const int aligned_width = multiple16(width);
  const int aligned_height = multiple16(height);
John Koleszar's avatar
John Koleszar committed
73

Dmitry Kovalev's avatar
Dmitry Kovalev committed
74
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
75

John Koleszar's avatar
John Koleszar committed
76
77
  for (i = 0; i < NUM_YV12_BUFFERS; i++) {
    oci->fb_idx_ref_cnt[i] = 0;
John Koleszar's avatar
John Koleszar committed
78
79
    if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i], width, height,
                                    VP9BORDERINPIXELS) < 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
80
      vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
81
      return 1;
John Koleszar's avatar
John Koleszar committed
82
    }
John Koleszar's avatar
John Koleszar committed
83
  }
John Koleszar's avatar
John Koleszar committed
84

85
86
87
  oci->new_fb_idx = NUM_YV12_BUFFERS - 1;
  oci->fb_idx_ref_cnt[oci->new_fb_idx] = 1;

88
  for (i = 0; i < ALLOWED_REFS_PER_FRAME; i++)
89
    oci->active_ref_idx[i] = i;
John Koleszar's avatar
John Koleszar committed
90

91
92
93
94
  for (i = 0; i < NUM_REF_FRAMES; i++) {
    oci->ref_frame_map[i] = i;
    oci->fb_idx_ref_cnt[i] = 1;
  }
John Koleszar's avatar
John Koleszar committed
95

John Koleszar's avatar
John Koleszar committed
96
97
  if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame, width, 16,
                                  VP9BORDERINPIXELS) < 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
98
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
99
100
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
101

John Koleszar's avatar
John Koleszar committed
102
103
  if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height,
                                  VP9BORDERINPIXELS) < 0) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
104
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
105
106
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
107

John Koleszar's avatar
John Koleszar committed
108
109
  oci->mb_rows = aligned_height >> 4;
  oci->mb_cols = aligned_width >> 4;
John Koleszar's avatar
John Koleszar committed
110
111
112
  oci->MBs = oci->mb_rows * oci->mb_cols;
  oci->mode_info_stride = oci->mb_cols + 1;
  oci->mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
John Koleszar's avatar
John Koleszar committed
113

John Koleszar's avatar
John Koleszar committed
114
  if (!oci->mip) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
115
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
116
117
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
118

John Koleszar's avatar
John Koleszar committed
119
  oci->mi = oci->mip + oci->mode_info_stride + 1;
John Koleszar's avatar
John Koleszar committed
120

John Koleszar's avatar
John Koleszar committed
121
  /* allocate memory for last frame MODE_INFO array */
Paul Wilkins's avatar
Paul Wilkins committed
122

John Koleszar's avatar
John Koleszar committed
123
  oci->prev_mip = vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
124

John Koleszar's avatar
John Koleszar committed
125
  if (!oci->prev_mip) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
126
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
127
128
    return 1;
  }
129

John Koleszar's avatar
John Koleszar committed
130
  oci->prev_mi = oci->prev_mip + oci->mode_info_stride + 1;
John Koleszar's avatar
John Koleszar committed
131

132
133
  oci->above_context =
    vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * (3 + oci->mb_cols), 1);
John Koleszar's avatar
John Koleszar committed
134

John Koleszar's avatar
John Koleszar committed
135
  if (!oci->above_context) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
136
    vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
137
138
    return 1;
  }
John Koleszar's avatar
John Koleszar committed
139

John Koleszar's avatar
John Koleszar committed
140
  return 0;
John Koleszar's avatar
John Koleszar committed
141
}
142

143
void vp9_setup_version(VP9_COMMON *cm) {
John Koleszar's avatar
John Koleszar committed
144
145
146
147
148
149
150
151
152
  if (cm->version & 0x4) {
    if (!CONFIG_EXPERIMENTAL)
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                         "Bitstream was created by an experimental "
                         "encoder");
    cm->experimental = 1;
  }

  switch (cm->version & 0x3) {
John Koleszar's avatar
John Koleszar committed
153
    case 0:
John Koleszar's avatar
John Koleszar committed
154
155
156
157
      cm->no_lpf = 0;
      cm->filter_type = NORMAL_LOOPFILTER;
      cm->use_bilinear_mc_filter = 0;
      break;
John Koleszar's avatar
John Koleszar committed
158
    case 1:
John Koleszar's avatar
John Koleszar committed
159
160
161
162
      cm->no_lpf = 0;
      cm->filter_type = SIMPLE_LOOPFILTER;
      cm->use_bilinear_mc_filter = 1;
      break;
John Koleszar's avatar
John Koleszar committed
163
    case 2:
Paul Wilkins's avatar
Paul Wilkins committed
164
    case 3:
John Koleszar's avatar
John Koleszar committed
165
166
167
168
169
      cm->no_lpf = 1;
      cm->filter_type = NORMAL_LOOPFILTER;
      cm->use_bilinear_mc_filter = 1;
      break;
  }
John Koleszar's avatar
John Koleszar committed
170
}
171
void vp9_create_common(VP9_COMMON *oci) {
172
  vp9_machine_specific_config(oci);
John Koleszar's avatar
John Koleszar committed
173

174
  vp9_init_mbmode_probs(oci);
John Koleszar's avatar
John Koleszar committed
175

176
  vp9_default_bmode_probs(oci->fc.bmode_prob);
John Koleszar's avatar
John Koleszar committed
177
178
179
180
181
182
183
184
185

  oci->txfm_mode = ONLY_4X4;
  oci->comp_pred_mode = HYBRID_PREDICTION;
  oci->no_lpf = 0;
  oci->filter_type = NORMAL_LOOPFILTER;
  oci->use_bilinear_mc_filter = 0;
  oci->clr_type = REG_YUV;
  oci->clamp_type = RECON_CLAMP_REQUIRED;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
186
  // Initialize reference frame sign bias structure to defaults
John Koleszar's avatar
John Koleszar committed
187
188
189
  vpx_memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));

  oci->kf_ymode_probs_update = 0;
John Koleszar's avatar
John Koleszar committed
190
191
}

192
void vp9_remove_common(VP9_COMMON *oci) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
193
  vp9_free_frame_buffers(oci);
John Koleszar's avatar
John Koleszar committed
194
195
}

196
197
198
199
void vp9_initialize_common() {
  vp9_coef_tree_initialize();
  vp9_entropy_mode_init();
  vp9_entropy_mv_init();
John Koleszar's avatar
John Koleszar committed
200
}