190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber/*
2f71323e297a928af368937089d3ed71239786f86Andreas Huber *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber *
4f71323e297a928af368937089d3ed71239786f86Andreas Huber *  Use of this source code is governed by a BSD-style license
5f71323e297a928af368937089d3ed71239786f86Andreas Huber *  that can be found in the LICENSE file in the root of the source
6f71323e297a928af368937089d3ed71239786f86Andreas Huber *  tree. An additional intellectual property rights grant can be found
7f71323e297a928af368937089d3ed71239786f86Andreas Huber *  in the file PATENTS.  All contributing project authors may
8f71323e297a928af368937089d3ed71239786f86Andreas Huber *  be found in the AUTHORS file in the root of the source tree.
990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber */
1090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
111b362b15af34006e6a11974088a46d42b903418eJohann#include "vpx_config.h"
12da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian#include "alloccommon.h"
1390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "blockd.h"
1490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "vpx_mem/vpx_mem.h"
1590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "onyxc_int.h"
1690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "findnearmv.h"
1790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "entropymode.h"
1890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber#include "systemdependent.h"
1990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
207bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid vp8_de_alloc_frame_buffers(VP8_COMMON *oci) {
217bc9febe8749e98a3812a0dc4380ceae75c29450Johann  int i;
227bc9febe8749e98a3812a0dc4380ceae75c29450Johann  for (i = 0; i < NUM_YV12_BUFFERS; ++i) {
237bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
247bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
25f71323e297a928af368937089d3ed71239786f86Andreas Huber
267bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
271b362b15af34006e6a11974088a46d42b903418eJohann#if CONFIG_POSTPROC
287bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
297bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (oci->post_proc_buffer_int_used) {
307bc9febe8749e98a3812a0dc4380ceae75c29450Johann    vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer_int);
317bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
327bc9febe8749e98a3812a0dc4380ceae75c29450Johann
337bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vpx_free(oci->pp_limits_buffer);
347bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->pp_limits_buffer = NULL;
351b362b15af34006e6a11974088a46d42b903418eJohann
367bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vpx_free(oci->postproc_state.generated_noise);
377bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->postproc_state.generated_noise = NULL;
381b362b15af34006e6a11974088a46d42b903418eJohann#endif
3990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
407bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vpx_free(oci->above_context);
417bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vpx_free(oci->mip);
421b362b15af34006e6a11974088a46d42b903418eJohann#if CONFIG_ERROR_CONCEALMENT
437bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vpx_free(oci->prev_mip);
447bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->prev_mip = NULL;
451b362b15af34006e6a11974088a46d42b903418eJohann#endif
4690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
477bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->above_context = NULL;
487bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mip = NULL;
4990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
5090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
517bc9febe8749e98a3812a0dc4380ceae75c29450Johannint vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height) {
527bc9febe8749e98a3812a0dc4380ceae75c29450Johann  int i;
5390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
547bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_de_alloc_frame_buffers(oci);
5590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
567bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /* our internal buffers are always multiples of 16 */
577bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if ((width & 0xf) != 0) width += 16 - (width & 0xf);
5890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
597bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if ((height & 0xf) != 0) height += 16 - (height & 0xf);
6090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
617bc9febe8749e98a3812a0dc4380ceae75c29450Johann  for (i = 0; i < NUM_YV12_BUFFERS; ++i) {
627bc9febe8749e98a3812a0dc4380ceae75c29450Johann    oci->fb_idx_ref_cnt[i] = 0;
637bc9febe8749e98a3812a0dc4380ceae75c29450Johann    oci->yv12_fb[i].flags = 0;
647bc9febe8749e98a3812a0dc4380ceae75c29450Johann    if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i], width, height,
657bc9febe8749e98a3812a0dc4380ceae75c29450Johann                                    VP8BORDERINPIXELS) < 0) {
667bc9febe8749e98a3812a0dc4380ceae75c29450Johann      goto allocation_fail;
6790d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    }
687bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
6990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
707bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->new_fb_idx = 0;
717bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->lst_fb_idx = 1;
727bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->gld_fb_idx = 2;
737bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->alt_fb_idx = 3;
7490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
757bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->fb_idx_ref_cnt[0] = 1;
767bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->fb_idx_ref_cnt[1] = 1;
777bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->fb_idx_ref_cnt[2] = 1;
787bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->fb_idx_ref_cnt[3] = 1;
7990d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
807bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame, width, 16,
817bc9febe8749e98a3812a0dc4380ceae75c29450Johann                                  VP8BORDERINPIXELS) < 0) {
827bc9febe8749e98a3812a0dc4380ceae75c29450Johann    goto allocation_fail;
837bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
8490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
857bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mb_rows = height >> 4;
867bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mb_cols = width >> 4;
877bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->MBs = oci->mb_rows * oci->mb_cols;
887bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mode_info_stride = oci->mb_cols + 1;
897bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mip =
907bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
9190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
927bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (!oci->mip) goto allocation_fail;
9390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
947bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mi = oci->mip + oci->mode_info_stride + 1;
9590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
967bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /* Allocation of previous mode info will be done in vp8_decode_frame()
977bc9febe8749e98a3812a0dc4380ceae75c29450Johann   * as it is a decoder only data */
9890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
997bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->above_context =
1007bc9febe8749e98a3812a0dc4380ceae75c29450Johann      vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * oci->mb_cols, 1);
10190d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1027bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (!oci->above_context) goto allocation_fail;
10390d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1041b362b15af34006e6a11974088a46d42b903418eJohann#if CONFIG_POSTPROC
1057bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height,
1067bc9febe8749e98a3812a0dc4380ceae75c29450Johann                                  VP8BORDERINPIXELS) < 0) {
1077bc9febe8749e98a3812a0dc4380ceae75c29450Johann    goto allocation_fail;
1087bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
1097bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1107bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->post_proc_buffer_int_used = 0;
1117bc9febe8749e98a3812a0dc4380ceae75c29450Johann  memset(&oci->postproc_state, 0, sizeof(oci->postproc_state));
1127bc9febe8749e98a3812a0dc4380ceae75c29450Johann  memset(oci->post_proc_buffer.buffer_alloc, 128,
1137bc9febe8749e98a3812a0dc4380ceae75c29450Johann         oci->post_proc_buffer.frame_size);
1147bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1157bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /* Allocate buffer to store post-processing filter coefficients.
1167bc9febe8749e98a3812a0dc4380ceae75c29450Johann   *
1177bc9febe8749e98a3812a0dc4380ceae75c29450Johann   * Note: Round up mb_cols to support SIMD reads
1187bc9febe8749e98a3812a0dc4380ceae75c29450Johann   */
1197bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->pp_limits_buffer = vpx_memalign(16, 24 * ((oci->mb_cols + 1) & ~1));
1207bc9febe8749e98a3812a0dc4380ceae75c29450Johann  if (!oci->pp_limits_buffer) goto allocation_fail;
1211b362b15af34006e6a11974088a46d42b903418eJohann#endif
12290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1237bc9febe8749e98a3812a0dc4380ceae75c29450Johann  return 0;
1241b362b15af34006e6a11974088a46d42b903418eJohann
1251b362b15af34006e6a11974088a46d42b903418eJohannallocation_fail:
1267bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_de_alloc_frame_buffers(oci);
1277bc9febe8749e98a3812a0dc4380ceae75c29450Johann  return 1;
12890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
1291b362b15af34006e6a11974088a46d42b903418eJohann
1307bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid vp8_setup_version(VP8_COMMON *cm) {
1317bc9febe8749e98a3812a0dc4380ceae75c29450Johann  switch (cm->version) {
13290d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    case 0:
1337bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->no_lpf = 0;
1347bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->filter_type = NORMAL_LOOPFILTER;
1357bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->use_bilinear_mc_filter = 0;
1367bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->full_pixel = 0;
1377bc9febe8749e98a3812a0dc4380ceae75c29450Johann      break;
13890d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    case 1:
1397bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->no_lpf = 0;
1407bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->filter_type = SIMPLE_LOOPFILTER;
1417bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->use_bilinear_mc_filter = 1;
1427bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->full_pixel = 0;
1437bc9febe8749e98a3812a0dc4380ceae75c29450Johann      break;
14490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    case 2:
1457bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->no_lpf = 1;
1467bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->filter_type = NORMAL_LOOPFILTER;
1477bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->use_bilinear_mc_filter = 1;
1487bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->full_pixel = 0;
1497bc9febe8749e98a3812a0dc4380ceae75c29450Johann      break;
15090d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    case 3:
1517bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->no_lpf = 1;
1527bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->filter_type = SIMPLE_LOOPFILTER;
1537bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->use_bilinear_mc_filter = 1;
1547bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->full_pixel = 1;
1557bc9febe8749e98a3812a0dc4380ceae75c29450Johann      break;
15690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber    default:
1577bc9febe8749e98a3812a0dc4380ceae75c29450Johann      /*4,5,6,7 are reserved for future use*/
1587bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->no_lpf = 0;
1597bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->filter_type = NORMAL_LOOPFILTER;
1607bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->use_bilinear_mc_filter = 0;
1617bc9febe8749e98a3812a0dc4380ceae75c29450Johann      cm->full_pixel = 0;
1627bc9febe8749e98a3812a0dc4380ceae75c29450Johann      break;
1637bc9febe8749e98a3812a0dc4380ceae75c29450Johann  }
16490d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
1657bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid vp8_create_common(VP8_COMMON *oci) {
1667bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_machine_specific_config(oci);
1677bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1687bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_init_mbmode_probs(oci);
1697bc9febe8749e98a3812a0dc4380ceae75c29450Johann  vp8_default_bmode_probs(oci->fc.bmode_prob);
1707bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1717bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->mb_no_coeff_skip = 1;
1727bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->no_lpf = 0;
1737bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->filter_type = NORMAL_LOOPFILTER;
1747bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->use_bilinear_mc_filter = 0;
1757bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->full_pixel = 0;
1767bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->multi_token_partition = ONE_PARTITION;
1777bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->clamp_type = RECON_CLAMP_REQUIRED;
1787bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1797bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /* Initialize reference frame sign bias structure to defaults */
1807bc9febe8749e98a3812a0dc4380ceae75c29450Johann  memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
1817bc9febe8749e98a3812a0dc4380ceae75c29450Johann
1827bc9febe8749e98a3812a0dc4380ceae75c29450Johann  /* Default disable buffer to buffer copying */
1837bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->copy_buffer_to_gf = 0;
1847bc9febe8749e98a3812a0dc4380ceae75c29450Johann  oci->copy_buffer_to_arf = 0;
18590d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber}
18690d3ed91ae9228e1c8bab561b6138d4cb8c1e4fdAndreas Huber
1877bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid vp8_remove_common(VP8_COMMON *oci) { vp8_de_alloc_frame_buffers(oci); }
188