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 /* Align the buffer dimensions */
77 width = (width + 15) & ~15;
78 height = (height + 15) & ~15;
80 /* Allocate the lookahead structures */
81 ctx = calloc(1, sizeof(*ctx));
85 ctx->buf = calloc(depth, sizeof(*ctx->buf));
88 for(i=0; i<depth; i++)
89 if (vp8_yv12_alloc_frame_buffer(&ctx->buf[i].img, width, height, 16))
94 vp8_lookahead_destroy(ctx);
100 vp8_lookahead_push(struct lookahead_ctx *ctx,
101 YV12_BUFFER_CONFIG *src,
106 struct lookahead_entry* buf;
108 if(ctx->sz + 1 > ctx->max_sz)
111 buf = pop(ctx, &ctx->write_idx);
112 vp8_copy_and_extend_frame(src, &buf->img);
113 buf->ts_start = ts_start;
114 buf->ts_end = ts_end;
120 struct lookahead_entry*
121 vp8_lookahead_pop(struct lookahead_ctx *ctx,
124 struct lookahead_entry* buf = NULL;
126 if(ctx->sz && (drain || ctx->sz == ctx->max_sz))
128 buf = pop(ctx, &ctx->read_idx);
135 struct lookahead_entry*
136 vp8_lookahead_peek(struct lookahead_ctx *ctx,
139 struct lookahead_entry* buf = NULL;
141 assert(index < ctx->max_sz);
144 index += ctx->read_idx;
145 if(index >= ctx->max_sz)
146 index -= ctx->max_sz;
147 buf = ctx->buf + index;
154 vp8_lookahead_depth(struct lookahead_ctx *ctx)