2010-05-18 11:58:33 -04:00
|
|
|
/*
|
2010-09-09 08:16:39 -04:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 11:58:33 -04:00
|
|
|
*
|
2010-06-18 12:39:21 -04:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 16:19:40 -04:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 12:39:21 -04:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 16:19:40 -04:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 11:58:33 -04:00
|
|
|
*/
|
|
|
|
|
2012-12-23 07:20:10 -08:00
|
|
|
#include "./vpx_config.h"
|
2010-05-18 11:58:33 -04:00
|
|
|
#include "vpx_mem/vpx_mem.h"
|
2013-07-03 10:54:50 -07:00
|
|
|
|
2013-06-05 20:56:37 -07:00
|
|
|
#include "vp9/common/vp9_blockd.h"
|
2012-11-28 10:41:40 -08:00
|
|
|
#include "vp9/common/vp9_entropymode.h"
|
|
|
|
#include "vp9/common/vp9_entropymv.h"
|
2013-06-05 20:56:37 -07:00
|
|
|
#include "vp9/common/vp9_onyxc_int.h"
|
2012-11-28 10:41:40 -08:00
|
|
|
#include "vp9/common/vp9_systemdependent.h"
|
2010-05-18 11:58:33 -04:00
|
|
|
|
2015-01-27 12:26:28 -08:00
|
|
|
// TODO(hkuang): Don't need to lock the whole pool after implementing atomic
|
|
|
|
// frame reference count.
|
|
|
|
void lock_buffer_pool(BufferPool *const pool) {
|
|
|
|
#if CONFIG_MULTITHREAD
|
|
|
|
pthread_mutex_lock(&pool->pool_mutex);
|
|
|
|
#else
|
|
|
|
(void)pool;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock_buffer_pool(BufferPool *const pool) {
|
|
|
|
#if CONFIG_MULTITHREAD
|
|
|
|
pthread_mutex_unlock(&pool->pool_mutex);
|
|
|
|
#else
|
|
|
|
(void)pool;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
|
|
|
|
const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
|
|
|
|
const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
|
|
|
|
|
2013-08-22 14:45:24 -07:00
|
|
|
cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
|
|
|
|
cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
|
2014-08-12 11:24:24 -07:00
|
|
|
cm->mi_stride = calc_mi_size(cm->mi_cols);
|
2013-10-01 11:54:10 -07:00
|
|
|
|
|
|
|
cm->mb_cols = (cm->mi_cols + 1) >> 1;
|
|
|
|
cm->mb_rows = (cm->mi_rows + 1) >> 1;
|
|
|
|
cm->MBs = cm->mb_rows * cm->mb_cols;
|
2013-06-05 20:56:37 -07:00
|
|
|
}
|
|
|
|
|
2015-01-27 12:26:28 -08:00
|
|
|
static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
|
|
|
|
cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
|
|
|
|
if (cm->seg_map_array[i] == NULL)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the index.
|
|
|
|
cm->seg_map_idx = 0;
|
|
|
|
cm->prev_seg_map_idx = 1;
|
|
|
|
|
|
|
|
cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
|
|
|
|
if (!cm->frame_parallel_decode)
|
|
|
|
cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_seg_map(VP9_COMMON *cm) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
|
|
|
|
vpx_free(cm->seg_map_array[i]);
|
|
|
|
cm->seg_map_array[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cm->current_frame_seg_map = NULL;
|
|
|
|
|
|
|
|
if (!cm->frame_parallel_decode) {
|
|
|
|
cm->last_frame_seg_map = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
void vp9_free_ref_frame_buffers(VP9_COMMON *cm) {
|
2015-01-27 12:26:28 -08:00
|
|
|
BufferPool *const pool = cm->buffer_pool;
|
2014-03-19 11:50:04 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FRAME_BUFFERS; ++i) {
|
2015-01-27 12:26:28 -08:00
|
|
|
if (pool->frame_bufs[i].ref_count > 0 &&
|
|
|
|
pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
|
|
|
|
pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
|
|
|
|
pool->frame_bufs[i].ref_count = 0;
|
2014-03-19 11:50:04 -07:00
|
|
|
}
|
2015-01-27 12:26:28 -08:00
|
|
|
vpx_free(pool->frame_bufs[i].mvs);
|
|
|
|
pool->frame_bufs[i].mvs = NULL;
|
|
|
|
vp9_free_frame_buffer(&pool->frame_bufs[i].buf);
|
2014-03-19 11:50:04 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 18:12:29 -08:00
|
|
|
#if CONFIG_VP9_POSTPROC
|
2014-03-19 11:50:04 -07:00
|
|
|
vp9_free_frame_buffer(&cm->post_proc_buffer);
|
2014-12-02 12:14:47 -08:00
|
|
|
vp9_free_frame_buffer(&cm->post_proc_buffer_int);
|
2014-12-10 18:12:29 -08:00
|
|
|
#endif
|
2014-06-16 16:22:28 -07:00
|
|
|
}
|
2014-03-19 11:50:04 -07:00
|
|
|
|
2014-06-16 16:22:28 -07:00
|
|
|
void vp9_free_context_buffers(VP9_COMMON *cm) {
|
2014-11-03 11:23:22 -08:00
|
|
|
cm->free_mi(cm);
|
2015-01-27 12:26:28 -08:00
|
|
|
free_seg_map(cm);
|
2014-03-24 18:32:46 -07:00
|
|
|
vpx_free(cm->above_context);
|
|
|
|
cm->above_context = NULL;
|
2014-03-20 16:08:54 -07:00
|
|
|
vpx_free(cm->above_seg_context);
|
|
|
|
cm->above_seg_context = NULL;
|
2014-03-19 11:50:04 -07:00
|
|
|
}
|
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
|
|
|
|
vp9_free_context_buffers(cm);
|
2013-11-14 14:55:49 -08:00
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
vp9_set_mb_mi(cm, width, height);
|
2014-11-03 11:23:22 -08:00
|
|
|
if (cm->alloc_mi(cm, cm->mi_stride * calc_mi_size(cm->mi_rows)))
|
2014-08-12 11:24:24 -07:00
|
|
|
goto fail;
|
2013-11-14 14:55:49 -08:00
|
|
|
|
2015-01-27 12:26:28 -08:00
|
|
|
// Create the segmentation map structure and set to 0.
|
|
|
|
free_seg_map(cm);
|
|
|
|
if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
|
|
|
|
goto fail;
|
2013-11-14 14:55:49 -08:00
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
|
|
|
|
2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
|
|
|
|
sizeof(*cm->above_context));
|
|
|
|
if (!cm->above_context) goto fail;
|
2014-03-24 18:32:46 -07:00
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
|
|
|
|
mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
|
|
|
|
if (!cm->above_seg_context) goto fail;
|
2014-03-20 16:08:54 -07:00
|
|
|
|
2013-11-14 14:55:49 -08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2014-06-16 16:22:28 -07:00
|
|
|
vp9_free_context_buffers(cm);
|
2013-11-14 14:55:49 -08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-16 16:22:28 -07:00
|
|
|
static void init_frame_bufs(VP9_COMMON *cm) {
|
2015-01-27 12:26:28 -08:00
|
|
|
BufferPool *const pool = cm->buffer_pool;
|
2014-06-16 16:22:28 -07:00
|
|
|
int i;
|
|
|
|
|
|
|
|
cm->new_fb_idx = FRAME_BUFFERS - 1;
|
2015-01-27 12:26:28 -08:00
|
|
|
pool->frame_bufs[cm->new_fb_idx].ref_count = 1;
|
2014-06-16 16:22:28 -07:00
|
|
|
|
|
|
|
for (i = 0; i < REF_FRAMES; ++i) {
|
|
|
|
cm->ref_frame_map[i] = i;
|
2015-01-27 12:26:28 -08:00
|
|
|
pool->frame_bufs[i].ref_count = 1;
|
2014-06-16 16:22:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
int vp9_alloc_ref_frame_buffers(VP9_COMMON *cm, int width, int height) {
|
2014-06-16 16:22:28 -07:00
|
|
|
int i;
|
2013-08-22 20:04:24 -07:00
|
|
|
const int ss_x = cm->subsampling_x;
|
|
|
|
const int ss_y = cm->subsampling_y;
|
2010-05-18 11:58:33 -04:00
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
vp9_free_ref_frame_buffers(cm);
|
2010-05-18 11:58:33 -04:00
|
|
|
|
2014-06-16 16:22:28 -07:00
|
|
|
for (i = 0; i < FRAME_BUFFERS; ++i) {
|
2015-01-27 12:26:28 -08:00
|
|
|
BufferPool *const pool = cm->buffer_pool;
|
|
|
|
pool->frame_bufs[i].ref_count = 0;
|
|
|
|
if (vp9_alloc_frame_buffer(&pool->frame_bufs[i].buf, width, height,
|
2014-08-26 12:35:15 -07:00
|
|
|
ss_x, ss_y,
|
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
cm->use_highbitdepth,
|
|
|
|
#endif
|
2014-12-15 12:00:09 -08:00
|
|
|
VP9_ENC_BORDER_IN_PIXELS,
|
|
|
|
cm->byte_alignment) < 0)
|
2013-06-05 20:56:37 -07:00
|
|
|
goto fail;
|
2015-01-27 12:26:28 -08:00
|
|
|
if (pool->frame_bufs[i].mvs == NULL) {
|
|
|
|
pool->frame_bufs[i].mvs =
|
2014-10-27 16:19:04 -07:00
|
|
|
(MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
|
2015-01-27 12:26:28 -08:00
|
|
|
sizeof(*pool->frame_bufs[i].mvs));
|
|
|
|
if (pool->frame_bufs[i].mvs == NULL)
|
2014-10-27 16:19:04 -07:00
|
|
|
goto fail;
|
|
|
|
|
2015-01-27 12:26:28 -08:00
|
|
|
pool->frame_bufs[i].mi_rows = cm->mi_rows;
|
|
|
|
pool->frame_bufs[i].mi_cols = cm->mi_cols;
|
2014-10-27 16:19:04 -07:00
|
|
|
}
|
2012-07-13 15:21:29 -07:00
|
|
|
}
|
2010-05-18 11:58:33 -04:00
|
|
|
|
2014-06-16 16:22:28 -07:00
|
|
|
init_frame_bufs(cm);
|
2010-05-18 11:58:33 -04:00
|
|
|
|
2014-12-09 17:27:52 -08:00
|
|
|
#if CONFIG_VP9_POSTPROC
|
2013-08-22 20:04:24 -07:00
|
|
|
if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
|
2014-08-26 12:35:15 -07:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
|
|
|
cm->use_highbitdepth,
|
|
|
|
#endif
|
2014-12-15 12:00:09 -08:00
|
|
|
VP9_ENC_BORDER_IN_PIXELS,
|
|
|
|
cm->byte_alignment) < 0)
|
2013-06-05 20:56:37 -07:00
|
|
|
goto fail;
|
2014-06-27 15:08:07 -07:00
|
|
|
#endif
|
2013-06-05 20:56:37 -07:00
|
|
|
|
2014-06-16 16:22:28 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2014-07-10 15:35:51 -07:00
|
|
|
vp9_free_ref_frame_buffers(cm);
|
2013-06-05 20:56:37 -07:00
|
|
|
return 1;
|
2010-05-18 11:58:33 -04:00
|
|
|
}
|
2013-01-15 06:43:35 -08:00
|
|
|
|
2013-08-22 20:04:24 -07:00
|
|
|
void vp9_remove_common(VP9_COMMON *cm) {
|
2014-07-10 15:35:51 -07:00
|
|
|
vp9_free_ref_frame_buffers(cm);
|
2014-06-16 16:22:28 -07:00
|
|
|
vp9_free_context_buffers(cm);
|
2014-10-29 18:38:18 -07:00
|
|
|
|
|
|
|
vpx_free(cm->fc);
|
|
|
|
cm->fc = NULL;
|
|
|
|
vpx_free(cm->frame_contexts);
|
|
|
|
cm->frame_contexts = NULL;
|
2010-05-18 11:58:33 -04:00
|
|
|
}
|
2013-04-30 11:14:27 -07:00
|
|
|
|
2014-07-10 15:35:51 -07:00
|
|
|
void vp9_init_context_buffers(VP9_COMMON *cm) {
|
2014-11-03 11:23:22 -08:00
|
|
|
cm->setup_mi(cm);
|
2015-01-27 12:26:28 -08:00
|
|
|
if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
|
2013-08-09 16:21:27 -07:00
|
|
|
vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
|
2013-04-30 11:14:27 -07:00
|
|
|
}
|
2015-01-27 12:26:28 -08:00
|
|
|
|
|
|
|
void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
|
|
|
|
// Swap indices.
|
|
|
|
const int tmp = cm->seg_map_idx;
|
|
|
|
cm->seg_map_idx = cm->prev_seg_map_idx;
|
|
|
|
cm->prev_seg_map_idx = tmp;
|
|
|
|
|
|
|
|
cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
|
|
|
|
cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
|
|
|
|
}
|