2 * Copyright (c) 2011 The WebM project authors. All Rights Reserved.
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.
13 #include "./vpx_config.h"
15 #include "vp10/common/vp9_common.h"
17 #include "vp10/encoder/vp9_encoder.h"
18 #include "vp10/encoder/vp9_extend.h"
19 #include "vp10/encoder/vp9_lookahead.h"
21 /* Return the buffer at the given absolute index and increment the index */
22 static struct lookahead_entry *pop(struct lookahead_ctx *ctx,
24 unsigned int index = *idx;
25 struct lookahead_entry *buf = ctx->buf + index;
27 assert(index < ctx->max_sz);
28 if (++index >= ctx->max_sz)
35 void vp10_lookahead_destroy(struct lookahead_ctx *ctx) {
40 for (i = 0; i < ctx->max_sz; i++)
41 vp9_free_frame_buffer(&ctx->buf[i].img);
49 struct lookahead_ctx *vp10_lookahead_init(unsigned int width,
51 unsigned int subsampling_x,
52 unsigned int subsampling_y,
53 #if CONFIG_VP9_HIGHBITDEPTH
57 struct lookahead_ctx *ctx = NULL;
59 // Clamp the lookahead queue depth
60 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
62 // Allocate memory to keep previous source frames available.
63 depth += MAX_PRE_FRAMES;
65 // Allocate the lookahead structures
66 ctx = calloc(1, sizeof(*ctx));
68 const int legacy_byte_alignment = 0;
71 ctx->buf = calloc(depth, sizeof(*ctx->buf));
74 for (i = 0; i < depth; i++)
75 if (vp9_alloc_frame_buffer(&ctx->buf[i].img,
76 width, height, subsampling_x, subsampling_y,
77 #if CONFIG_VP9_HIGHBITDEPTH
80 VP9_ENC_BORDER_IN_PIXELS,
81 legacy_byte_alignment))
86 vp10_lookahead_destroy(ctx);
90 #define USE_PARTIAL_COPY 0
92 int vp10_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
93 int64_t ts_start, int64_t ts_end,
94 #if CONFIG_VP9_HIGHBITDEPTH
98 struct lookahead_entry *buf;
100 int row, col, active_end;
101 int mb_rows = (src->y_height + 15) >> 4;
102 int mb_cols = (src->y_width + 15) >> 4;
104 int width = src->y_crop_width;
105 int height = src->y_crop_height;
106 int uv_width = src->uv_crop_width;
107 int uv_height = src->uv_crop_height;
108 int subsampling_x = src->subsampling_x;
109 int subsampling_y = src->subsampling_y;
110 int larger_dimensions, new_dimensions;
112 if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz)
115 buf = pop(ctx, &ctx->write_idx);
117 new_dimensions = width != buf->img.y_crop_width ||
118 height != buf->img.y_crop_height ||
119 uv_width != buf->img.uv_crop_width ||
120 uv_height != buf->img.uv_crop_height;
121 larger_dimensions = width > buf->img.y_width ||
122 height > buf->img.y_height ||
123 uv_width > buf->img.uv_width ||
124 uv_height > buf->img.uv_height;
125 assert(!larger_dimensions || new_dimensions);
128 // TODO(jkoleszar): This is disabled for now, as
129 // vp10_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
131 // Only do this partial copy if the following conditions are all met:
132 // 1. Lookahead queue has has size of 1.
133 // 2. Active map is provided.
134 // 3. This is not a key frame, golden nor altref frame.
135 if (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
136 for (row = 0; row < mb_rows; ++row) {
140 // Find the first active macroblock in this row.
141 for (; col < mb_cols; ++col) {
146 // No more active macroblock in this row.
150 // Find the end of active region in this row.
153 for (; active_end < mb_cols; ++active_end) {
154 if (!active_map[active_end])
158 // Only copy this active region.
159 vp10_copy_and_extend_frame_with_rect(src, &buf->img,
162 (active_end - col) << 4);
164 // Start again from the end of this active region.
168 active_map += mb_cols;
172 if (larger_dimensions) {
173 YV12_BUFFER_CONFIG new_img;
174 memset(&new_img, 0, sizeof(new_img));
175 if (vp9_alloc_frame_buffer(&new_img,
176 width, height, subsampling_x, subsampling_y,
177 #if CONFIG_VP9_HIGHBITDEPTH
180 VP9_ENC_BORDER_IN_PIXELS,
183 vp9_free_frame_buffer(&buf->img);
185 } else if (new_dimensions) {
186 buf->img.y_crop_width = src->y_crop_width;
187 buf->img.y_crop_height = src->y_crop_height;
188 buf->img.uv_crop_width = src->uv_crop_width;
189 buf->img.uv_crop_height = src->uv_crop_height;
190 buf->img.subsampling_x = src->subsampling_x;
191 buf->img.subsampling_y = src->subsampling_y;
193 // Partial copy not implemented yet
194 vp10_copy_and_extend_frame(src, &buf->img);
199 buf->ts_start = ts_start;
200 buf->ts_end = ts_end;
206 struct lookahead_entry *vp10_lookahead_pop(struct lookahead_ctx *ctx,
208 struct lookahead_entry *buf = NULL;
210 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
211 buf = pop(ctx, &ctx->read_idx);
218 struct lookahead_entry *vp10_lookahead_peek(struct lookahead_ctx *ctx,
220 struct lookahead_entry *buf = NULL;
224 if (index < (int)ctx->sz) {
225 index += ctx->read_idx;
226 if (index >= (int)ctx->max_sz)
227 index -= ctx->max_sz;
228 buf = ctx->buf + index;
230 } else if (index < 0) {
232 if (-index <= MAX_PRE_FRAMES) {
233 index += ctx->read_idx;
235 index += ctx->max_sz;
236 buf = ctx->buf + index;
243 unsigned int vp10_lookahead_depth(struct lookahead_ctx *ctx) {