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,int use_highbitdepth,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 #if CONFIG_VP9_HIGHBITDEPTH
54 int use_highbitdepth,
55 #endif
56 unsigned int depth) {
57 struct lookahead_ctx *ctx = NULL;
58
59 // Clamp the lookahead queue depth
60 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
61
62 // Allocate memory to keep previous source frames available.
63 depth += MAX_PRE_FRAMES;
64
65 // Allocate the lookahead structures
66 ctx = calloc(1, sizeof(*ctx));
67 if (ctx) {
68 unsigned int i;
69 ctx->max_sz = depth;
70 ctx->buf = calloc(depth, sizeof(*ctx->buf));
71 if (!ctx->buf)
72 goto bail;
73 for (i = 0; i < depth; i++)
74 if (vp9_alloc_frame_buffer(&ctx->buf[i].img,
75 width, height, subsampling_x, subsampling_y,
76 #if CONFIG_VP9_HIGHBITDEPTH
77 use_highbitdepth,
78 #endif
79 VP9_ENC_BORDER_IN_PIXELS))
80 goto bail;
81 }
82 return ctx;
83 bail:
84 vp9_lookahead_destroy(ctx);
85 return NULL;
86 }
87
88 #define USE_PARTIAL_COPY 0
89
vp9_lookahead_push(struct lookahead_ctx * ctx,YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,unsigned int flags)90 int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
91 int64_t ts_start, int64_t ts_end, unsigned int flags) {
92 struct lookahead_entry *buf;
93 #if USE_PARTIAL_COPY
94 int row, col, active_end;
95 int mb_rows = (src->y_height + 15) >> 4;
96 int mb_cols = (src->y_width + 15) >> 4;
97 #endif
98
99 if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz)
100 return 1;
101 ctx->sz++;
102 buf = pop(ctx, &ctx->write_idx);
103
104 #if USE_PARTIAL_COPY
105 // TODO(jkoleszar): This is disabled for now, as
106 // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
107
108 // Only do this partial copy if the following conditions are all met:
109 // 1. Lookahead queue has has size of 1.
110 // 2. Active map is provided.
111 // 3. This is not a key frame, golden nor altref frame.
112 if (ctx->max_sz == 1 && active_map && !flags) {
113 for (row = 0; row < mb_rows; ++row) {
114 col = 0;
115
116 while (1) {
117 // Find the first active macroblock in this row.
118 for (; col < mb_cols; ++col) {
119 if (active_map[col])
120 break;
121 }
122
123 // No more active macroblock in this row.
124 if (col == mb_cols)
125 break;
126
127 // Find the end of active region in this row.
128 active_end = col;
129
130 for (; active_end < mb_cols; ++active_end) {
131 if (!active_map[active_end])
132 break;
133 }
134
135 // Only copy this active region.
136 vp9_copy_and_extend_frame_with_rect(src, &buf->img,
137 row << 4,
138 col << 4, 16,
139 (active_end - col) << 4);
140
141 // Start again from the end of this active region.
142 col = active_end;
143 }
144
145 active_map += mb_cols;
146 }
147 } else {
148 vp9_copy_and_extend_frame(src, &buf->img);
149 }
150 #else
151 // Partial copy not implemented yet
152 vp9_copy_and_extend_frame(src, &buf->img);
153 #endif
154
155 buf->ts_start = ts_start;
156 buf->ts_end = ts_end;
157 buf->flags = flags;
158 return 0;
159 }
160
161
vp9_lookahead_pop(struct lookahead_ctx * ctx,int drain)162 struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
163 int drain) {
164 struct lookahead_entry *buf = NULL;
165
166 if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
167 buf = pop(ctx, &ctx->read_idx);
168 ctx->sz--;
169 }
170 return buf;
171 }
172
173
vp9_lookahead_peek(struct lookahead_ctx * ctx,int index)174 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
175 int index) {
176 struct lookahead_entry *buf = NULL;
177
178 if (index >= 0) {
179 // Forward peek
180 if (index < (int)ctx->sz) {
181 index += ctx->read_idx;
182 if (index >= (int)ctx->max_sz)
183 index -= ctx->max_sz;
184 buf = ctx->buf + index;
185 }
186 } else if (index < 0) {
187 // Backward peek
188 if (-index <= MAX_PRE_FRAMES) {
189 index += ctx->read_idx;
190 if (index < 0)
191 index += ctx->max_sz;
192 buf = ctx->buf + index;
193 }
194 }
195
196 return buf;
197 }
198
vp9_lookahead_depth(struct lookahead_ctx * ctx)199 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) {
200 return ctx->sz;
201 }
202