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 "vp9/common/vp9_common.h"
17 #include "vp9/encoder/vp9_encoder.h"
18 #include "vp9/encoder/vp9_extend.h"
19 #include "vp9/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, int *idx) {
24 struct lookahead_entry *buf = ctx->buf + index;
26 assert(index < ctx->max_sz);
27 if (++index >= ctx->max_sz) index -= ctx->max_sz;
32 void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
37 for (i = 0; i < ctx->max_sz; i++) vpx_free_frame_buffer(&ctx->buf[i].img);
44 struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
46 unsigned int subsampling_x,
47 unsigned int subsampling_y,
48 #if CONFIG_VP9_HIGHBITDEPTH
52 struct lookahead_ctx *ctx = NULL;
54 // Clamp the lookahead queue depth
55 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
57 // Allocate memory to keep previous source frames available.
58 depth += MAX_PRE_FRAMES;
60 // Allocate the lookahead structures
61 ctx = calloc(1, sizeof(*ctx));
63 const int legacy_byte_alignment = 0;
66 ctx->buf = calloc(depth, sizeof(*ctx->buf));
67 if (!ctx->buf) goto bail;
68 for (i = 0; i < depth; i++)
69 if (vpx_alloc_frame_buffer(
70 &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
71 #if CONFIG_VP9_HIGHBITDEPTH
74 VP9_ENC_BORDER_IN_PIXELS, legacy_byte_alignment))
79 vp9_lookahead_destroy(ctx);
83 #define USE_PARTIAL_COPY 0
85 int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
86 int64_t ts_start, int64_t ts_end,
87 #if CONFIG_VP9_HIGHBITDEPTH
90 vpx_enc_frame_flags_t flags) {
91 struct lookahead_entry *buf;
93 int row, col, active_end;
94 int mb_rows = (src->y_height + 15) >> 4;
95 int mb_cols = (src->y_width + 15) >> 4;
97 int width = src->y_crop_width;
98 int height = src->y_crop_height;
99 int uv_width = src->uv_crop_width;
100 int uv_height = src->uv_crop_height;
101 int subsampling_x = src->subsampling_x;
102 int subsampling_y = src->subsampling_y;
103 int larger_dimensions, new_dimensions;
105 if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz) return 1;
107 buf = pop(ctx, &ctx->write_idx);
109 new_dimensions = width != buf->img.y_crop_width ||
110 height != buf->img.y_crop_height ||
111 uv_width != buf->img.uv_crop_width ||
112 uv_height != buf->img.uv_crop_height;
113 larger_dimensions = width > buf->img.y_width || height > buf->img.y_height ||
114 uv_width > buf->img.uv_width ||
115 uv_height > buf->img.uv_height;
116 assert(!larger_dimensions || new_dimensions);
119 // TODO(jkoleszar): This is disabled for now, as
120 // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
122 // Only do this partial copy if the following conditions are all met:
123 // 1. Lookahead queue has has size of 1.
124 // 2. Active map is provided.
125 // 3. This is not a key frame, golden nor altref frame.
126 if (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
127 for (row = 0; row < mb_rows; ++row) {
131 // Find the first active macroblock in this row.
132 for (; col < mb_cols; ++col) {
133 if (active_map[col]) break;
136 // No more active macroblock in this row.
137 if (col == mb_cols) break;
139 // Find the end of active region in this row.
142 for (; active_end < mb_cols; ++active_end) {
143 if (!active_map[active_end]) break;
146 // Only copy this active region.
147 vp9_copy_and_extend_frame_with_rect(src, &buf->img, row << 4, col << 4,
148 16, (active_end - col) << 4);
150 // Start again from the end of this active region.
154 active_map += mb_cols;
158 if (larger_dimensions) {
159 YV12_BUFFER_CONFIG new_img;
160 memset(&new_img, 0, sizeof(new_img));
161 if (vpx_alloc_frame_buffer(&new_img, width, height, subsampling_x,
163 #if CONFIG_VP9_HIGHBITDEPTH
166 VP9_ENC_BORDER_IN_PIXELS, 0))
168 vpx_free_frame_buffer(&buf->img);
170 } else if (new_dimensions) {
171 buf->img.y_crop_width = src->y_crop_width;
172 buf->img.y_crop_height = src->y_crop_height;
173 buf->img.uv_crop_width = src->uv_crop_width;
174 buf->img.uv_crop_height = src->uv_crop_height;
175 buf->img.subsampling_x = src->subsampling_x;
176 buf->img.subsampling_y = src->subsampling_y;
178 // Partial copy not implemented yet
179 vp9_copy_and_extend_frame(src, &buf->img);
184 buf->ts_start = ts_start;
185 buf->ts_end = ts_end;
190 struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
192 struct lookahead_entry *buf = NULL;
194 if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
195 buf = pop(ctx, &ctx->read_idx);
201 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
203 struct lookahead_entry *buf = NULL;
207 if (index < ctx->sz) {
208 index += ctx->read_idx;
209 if (index >= ctx->max_sz) index -= ctx->max_sz;
210 buf = ctx->buf + index;
212 } else if (index < 0) {
214 if (-index <= MAX_PRE_FRAMES) {
215 index += ctx->read_idx;
216 if (index < 0) index += ctx->max_sz;
217 buf = ctx->buf + index;
224 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }