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.
12 #include "vpx_config.h"
13 #include "lookahead.h"
14 #include "vp8/common/extend.h"
16 #define MAX_LAG_BUFFERS (CONFIG_REALTIME_ONLY? 1 : 25)
20 unsigned int max_sz; /* Absolute size of the queue */
21 unsigned int sz; /* Number of buffers currently in the queue */
22 unsigned int read_idx; /* Read index */
23 unsigned int write_idx; /* Write index */
24 struct lookahead_entry *buf; /* Buffer list */
28 /* Return the buffer at the given absolute index and increment the index */
29 static struct lookahead_entry *
30 pop(struct lookahead_ctx *ctx,
33 unsigned int index = *idx;
34 struct lookahead_entry *buf = ctx->buf + index;
36 assert(index < ctx->max_sz);
37 if(++index >= ctx->max_sz)
45 vp8_lookahead_destroy(struct lookahead_ctx *ctx)
53 for(i = 0; i < ctx->max_sz; i++)
54 vp8_yv12_de_alloc_frame_buffer(&ctx->buf[i].img);
63 vp8_lookahead_init(unsigned int width,
67 struct lookahead_ctx *ctx = NULL;
70 /* Clamp the lookahead queue depth */
73 else if(depth > MAX_LAG_BUFFERS)
74 depth = MAX_LAG_BUFFERS;
76 /* Keep last frame in lookahead buffer by increasing depth by 1.*/
79 /* Align the buffer dimensions */
80 width = (width + 15) & ~15;
81 height = (height + 15) & ~15;
83 /* Allocate the lookahead structures */
84 ctx = calloc(1, sizeof(*ctx));
88 ctx->buf = calloc(depth, sizeof(*ctx->buf));
91 for(i=0; i<depth; i++)
92 if (vp8_yv12_alloc_frame_buffer(&ctx->buf[i].img,
93 width, height, VP8BORDERINPIXELS))
98 vp8_lookahead_destroy(ctx);
104 vp8_lookahead_push(struct lookahead_ctx *ctx,
105 YV12_BUFFER_CONFIG *src,
109 unsigned char *active_map)
111 struct lookahead_entry* buf;
112 int row, col, active_end;
113 int mb_rows = (src->y_height + 15) >> 4;
114 int mb_cols = (src->y_width + 15) >> 4;
116 if(ctx->sz + 2 > ctx->max_sz)
119 buf = pop(ctx, &ctx->write_idx);
121 // Only do this partial copy if the following conditions are all met:
122 // 1. Lookahead queue has has size of 1.
123 // 2. Active map is provided.
124 // 3. This is not a key frame, golden nor altref frame.
125 if (ctx->max_sz == 1 && active_map && !flags)
127 for (row = 0; row < mb_rows; ++row)
133 // Find the first active macroblock in this row.
134 for (; col < mb_cols; ++col)
140 // No more active macroblock in this row.
144 // Find the end of active region in this row.
147 for (; active_end < mb_cols; ++active_end)
149 if (!active_map[active_end])
153 // Only copy this active region.
154 vp8_copy_and_extend_frame_with_rect(src, &buf->img,
157 (active_end - col) << 4);
159 // Start again from the end of this active region.
163 active_map += mb_cols;
168 vp8_copy_and_extend_frame(src, &buf->img);
170 buf->ts_start = ts_start;
171 buf->ts_end = ts_end;
177 struct lookahead_entry*
178 vp8_lookahead_pop(struct lookahead_ctx *ctx,
181 struct lookahead_entry* buf = NULL;
183 if(ctx->sz && (drain || ctx->sz == ctx->max_sz - 1))
185 buf = pop(ctx, &ctx->read_idx);
192 struct lookahead_entry*
193 vp8_lookahead_peek(struct lookahead_ctx *ctx,
197 struct lookahead_entry* buf = NULL;
199 if (direction == PEEK_FORWARD)
201 assert(index < ctx->max_sz - 1);
204 index += ctx->read_idx;
205 if(index >= ctx->max_sz)
206 index -= ctx->max_sz;
207 buf = ctx->buf + index;
210 else if (direction == PEEK_BACKWARD)
214 if(ctx->read_idx == 0)
215 index = ctx->max_sz - 1;
217 index = ctx->read_idx - index;
218 buf = ctx->buf + index;
226 vp8_lookahead_depth(struct lookahead_ctx *ctx)