Home | History | Annotate | Download | only in common
      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 #include "vpx_config.h"
     12 #include "alloccommon.h"
     13 #include "blockd.h"
     14 #include "vpx_mem/vpx_mem.h"
     15 #include "onyxc_int.h"
     16 #include "findnearmv.h"
     17 #include "entropymode.h"
     18 #include "systemdependent.h"
     19 
     20 void vp8_de_alloc_frame_buffers(VP8_COMMON *oci) {
     21   int i;
     22   for (i = 0; i < NUM_YV12_BUFFERS; ++i) {
     23     vp8_yv12_de_alloc_frame_buffer(&oci->yv12_fb[i]);
     24   }
     25 
     26   vp8_yv12_de_alloc_frame_buffer(&oci->temp_scale_frame);
     27 #if CONFIG_POSTPROC
     28   vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer);
     29   if (oci->post_proc_buffer_int_used) {
     30     vp8_yv12_de_alloc_frame_buffer(&oci->post_proc_buffer_int);
     31   }
     32 
     33   vpx_free(oci->pp_limits_buffer);
     34   oci->pp_limits_buffer = NULL;
     35 
     36   vpx_free(oci->postproc_state.generated_noise);
     37   oci->postproc_state.generated_noise = NULL;
     38 #endif
     39 
     40   vpx_free(oci->above_context);
     41   vpx_free(oci->mip);
     42 #if CONFIG_ERROR_CONCEALMENT
     43   vpx_free(oci->prev_mip);
     44   oci->prev_mip = NULL;
     45 #endif
     46 
     47   oci->above_context = NULL;
     48   oci->mip = NULL;
     49 }
     50 
     51 int vp8_alloc_frame_buffers(VP8_COMMON *oci, int width, int height) {
     52   int i;
     53 
     54   vp8_de_alloc_frame_buffers(oci);
     55 
     56   /* our internal buffers are always multiples of 16 */
     57   if ((width & 0xf) != 0) width += 16 - (width & 0xf);
     58 
     59   if ((height & 0xf) != 0) height += 16 - (height & 0xf);
     60 
     61   for (i = 0; i < NUM_YV12_BUFFERS; ++i) {
     62     oci->fb_idx_ref_cnt[i] = 0;
     63     oci->yv12_fb[i].flags = 0;
     64     if (vp8_yv12_alloc_frame_buffer(&oci->yv12_fb[i], width, height,
     65                                     VP8BORDERINPIXELS) < 0) {
     66       goto allocation_fail;
     67     }
     68   }
     69 
     70   oci->new_fb_idx = 0;
     71   oci->lst_fb_idx = 1;
     72   oci->gld_fb_idx = 2;
     73   oci->alt_fb_idx = 3;
     74 
     75   oci->fb_idx_ref_cnt[0] = 1;
     76   oci->fb_idx_ref_cnt[1] = 1;
     77   oci->fb_idx_ref_cnt[2] = 1;
     78   oci->fb_idx_ref_cnt[3] = 1;
     79 
     80   if (vp8_yv12_alloc_frame_buffer(&oci->temp_scale_frame, width, 16,
     81                                   VP8BORDERINPIXELS) < 0) {
     82     goto allocation_fail;
     83   }
     84 
     85   oci->mb_rows = height >> 4;
     86   oci->mb_cols = width >> 4;
     87   oci->MBs = oci->mb_rows * oci->mb_cols;
     88   oci->mode_info_stride = oci->mb_cols + 1;
     89   oci->mip =
     90       vpx_calloc((oci->mb_cols + 1) * (oci->mb_rows + 1), sizeof(MODE_INFO));
     91 
     92   if (!oci->mip) goto allocation_fail;
     93 
     94   oci->mi = oci->mip + oci->mode_info_stride + 1;
     95 
     96   /* Allocation of previous mode info will be done in vp8_decode_frame()
     97    * as it is a decoder only data */
     98 
     99   oci->above_context =
    100       vpx_calloc(sizeof(ENTROPY_CONTEXT_PLANES) * oci->mb_cols, 1);
    101 
    102   if (!oci->above_context) goto allocation_fail;
    103 
    104 #if CONFIG_POSTPROC
    105   if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer, width, height,
    106                                   VP8BORDERINPIXELS) < 0) {
    107     goto allocation_fail;
    108   }
    109 
    110   oci->post_proc_buffer_int_used = 0;
    111   memset(&oci->postproc_state, 0, sizeof(oci->postproc_state));
    112   memset(oci->post_proc_buffer.buffer_alloc, 128,
    113          oci->post_proc_buffer.frame_size);
    114 
    115   /* Allocate buffer to store post-processing filter coefficients.
    116    *
    117    * Note: Round up mb_cols to support SIMD reads
    118    */
    119   oci->pp_limits_buffer = vpx_memalign(16, 24 * ((oci->mb_cols + 1) & ~1));
    120   if (!oci->pp_limits_buffer) goto allocation_fail;
    121 #endif
    122 
    123   return 0;
    124 
    125 allocation_fail:
    126   vp8_de_alloc_frame_buffers(oci);
    127   return 1;
    128 }
    129 
    130 void vp8_setup_version(VP8_COMMON *cm) {
    131   switch (cm->version) {
    132     case 0:
    133       cm->no_lpf = 0;
    134       cm->filter_type = NORMAL_LOOPFILTER;
    135       cm->use_bilinear_mc_filter = 0;
    136       cm->full_pixel = 0;
    137       break;
    138     case 1:
    139       cm->no_lpf = 0;
    140       cm->filter_type = SIMPLE_LOOPFILTER;
    141       cm->use_bilinear_mc_filter = 1;
    142       cm->full_pixel = 0;
    143       break;
    144     case 2:
    145       cm->no_lpf = 1;
    146       cm->filter_type = NORMAL_LOOPFILTER;
    147       cm->use_bilinear_mc_filter = 1;
    148       cm->full_pixel = 0;
    149       break;
    150     case 3:
    151       cm->no_lpf = 1;
    152       cm->filter_type = SIMPLE_LOOPFILTER;
    153       cm->use_bilinear_mc_filter = 1;
    154       cm->full_pixel = 1;
    155       break;
    156     default:
    157       /*4,5,6,7 are reserved for future use*/
    158       cm->no_lpf = 0;
    159       cm->filter_type = NORMAL_LOOPFILTER;
    160       cm->use_bilinear_mc_filter = 0;
    161       cm->full_pixel = 0;
    162       break;
    163   }
    164 }
    165 void vp8_create_common(VP8_COMMON *oci) {
    166   vp8_machine_specific_config(oci);
    167 
    168   vp8_init_mbmode_probs(oci);
    169   vp8_default_bmode_probs(oci->fc.bmode_prob);
    170 
    171   oci->mb_no_coeff_skip = 1;
    172   oci->no_lpf = 0;
    173   oci->filter_type = NORMAL_LOOPFILTER;
    174   oci->use_bilinear_mc_filter = 0;
    175   oci->full_pixel = 0;
    176   oci->multi_token_partition = ONE_PARTITION;
    177   oci->clamp_type = RECON_CLAMP_REQUIRED;
    178 
    179   /* Initialize reference frame sign bias structure to defaults */
    180   memset(oci->ref_frame_sign_bias, 0, sizeof(oci->ref_frame_sign_bias));
    181 
    182   /* Default disable buffer to buffer copying */
    183   oci->copy_buffer_to_gf = 0;
    184   oci->copy_buffer_to_arf = 0;
    185 }
    186 
    187 void vp8_remove_common(VP8_COMMON *oci) { vp8_de_alloc_frame_buffers(oci); }
    188