1 /*
2 * Copyright (c) 2011 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10 #include <assert.h>
11 #include <stdlib.h>
12
13 #include "./vpx_config.h"
14
15 #include "vp9/common/vp9_common.h"
16
17 #include "vp9/encoder/vp9_encoder.h"
18 #include "vp9/encoder/vp9_extend.h"
19 #include "vp9/encoder/vp9_lookahead.h"
20
21 /* Return the buffer at the given absolute index and increment the index */
pop(struct lookahead_ctx * ctx,unsigned int * idx)22 static struct lookahead_entry *pop(struct lookahead_ctx *ctx,
23 unsigned int *idx) {
24 unsigned int index = *idx;
25 struct lookahead_entry *buf = ctx->buf + index;
26
27 assert(index < ctx->max_sz);
28 if (++index >= ctx->max_sz)
29 index -= ctx->max_sz;
30 *idx = index;
31 return buf;
32 }
33
34
vp9_lookahead_destroy(struct lookahead_ctx * ctx)35 void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
36 if (ctx) {
37 if (ctx->buf) {
38 unsigned int i;
39
40 for (i = 0; i < ctx->max_sz; i++)
41 vp9_free_frame_buffer(&ctx->buf[i].img);
42 free(ctx->buf);
43 }
44 free(ctx);
45 }
46 }
47
48
vp9_lookahead_init(unsigned int width,unsigned int height,unsigned int subsampling_x,unsigned int subsampling_y,unsigned int depth)49 struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
50 unsigned int height,
51 unsigned int subsampling_x,
52 unsigned int subsampling_y,
53 unsigned int depth) {
54 struct lookahead_ctx *ctx = NULL;
55
56 // Clamp the lookahead queue depth
57 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
58
59 // Allocate memory to keep previous source frames available.
60 depth += MAX_PRE_FRAMES;
61
62 // Allocate the lookahead structures
63 ctx = calloc(1, sizeof(*ctx));
64 if (ctx) {
65 unsigned int i;
66 ctx->max_sz = depth;
67 ctx->buf = calloc(depth, sizeof(*ctx->buf));
68 if (!ctx->buf)
69 goto bail;
70 for (i = 0; i < depth; i++)
71 if (vp9_alloc_frame_buffer(&ctx->buf[i].img,
72 width, height, subsampling_x, subsampling_y,
73 VP9_ENC_BORDER_IN_PIXELS))
74 goto bail;
75 }
76 return ctx;
77 bail:
78 vp9_lookahead_destroy(ctx);
79 return NULL;
80 }
81
82 #define USE_PARTIAL_COPY 0
83
vp9_lookahead_push(struct lookahead_ctx * ctx,YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,unsigned int flags)84 int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
85 int64_t ts_start, int64_t ts_end, unsigned int flags) {
86 struct lookahead_entry *buf;
87 #if USE_PARTIAL_COPY
88 int row, col, active_end;
89 int mb_rows = (src->y_height + 15) >> 4;
90 int mb_cols = (src->y_width + 15) >> 4;
91 #endif
92
93 if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz)
94 return 1;
95 ctx->sz++;
96 buf = pop(ctx, &ctx->write_idx);
97
98 #if USE_PARTIAL_COPY
99 // TODO(jkoleszar): This is disabled for now, as
100 // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
101
102 // Only do this partial copy if the following conditions are all met:
103 // 1. Lookahead queue has has size of 1.
104 // 2. Active map is provided.
105 // 3. This is not a key frame, golden nor altref frame.
106 if (ctx->max_sz == 1 && active_map && !flags) {
107 for (row = 0; row < mb_rows; ++row) {
108 col = 0;
109
110 while (1) {
111 // Find the first active macroblock in this row.
112 for (; col < mb_cols; ++col) {
113 if (active_map[col])
114 break;
115 }
116
117 // No more active macroblock in this row.
118 if (col == mb_cols)
119 break;
120
121 // Find the end of active region in this row.
122 active_end = col;
123
124 for (; active_end < mb_cols; ++active_end) {
125 if (!active_map[active_end])
126 break;
127 }
128
129 // Only copy this active region.
130 vp9_copy_and_extend_frame_with_rect(src, &buf->img,
131 row << 4,
132 col << 4, 16,
133 (active_end - col) << 4);
134
135 // Start again from the end of this active region.
136 col = active_end;
137 }
138
139 active_map += mb_cols;
140 }
141 } else {
142 vp9_copy_and_extend_frame(src, &buf->img);
143 }
144 #else
145 // Partial copy not implemented yet
146 vp9_copy_and_extend_frame(src, &buf->img);
147 #endif
148
149 buf->ts_start = ts_start;
150 buf->ts_end = ts_end;
151 buf->flags = flags;
152 return 0;
153 }
154
155
vp9_lookahead_pop(struct lookahead_ctx * ctx,int drain)156 struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
157 int drain) {
158 struct lookahead_entry *buf = NULL;
159
160 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
161 buf = pop(ctx, &ctx->read_idx);
162 ctx->sz--;
163 }
164 return buf;
165 }
166
167
vp9_lookahead_peek(struct lookahead_ctx * ctx,int index)168 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
169 int index) {
170 struct lookahead_entry *buf = NULL;
171
172 if (index >= 0) {
173 // Forward peek
174 if (index < (int)ctx->sz) {
175 index += ctx->read_idx;
176 if (index >= (int)ctx->max_sz)
177 index -= ctx->max_sz;
178 buf = ctx->buf + index;
179 }
180 } else if (index < 0) {
181 // Backward peek
182 if (-index <= MAX_PRE_FRAMES) {
183 index += ctx->read_idx;
184 if (index < 0)
185 index += ctx->max_sz;
186 buf = ctx->buf + index;
187 }
188 }
189
190 return buf;
191 }
192
vp9_lookahead_depth(struct lookahead_ctx * ctx)193 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) {
194 return ctx->sz;
195 }
196