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,int * idx)22 static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
23 int index = *idx;
24 struct lookahead_entry *buf = ctx->buf + index;
25
26 assert(index < ctx->max_sz);
27 if (++index >= ctx->max_sz) index -= ctx->max_sz;
28 *idx = index;
29 return buf;
30 }
31
vp9_lookahead_destroy(struct lookahead_ctx * ctx)32 void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
33 if (ctx) {
34 if (ctx->buf) {
35 int i;
36
37 for (i = 0; i < ctx->max_sz; i++) vpx_free_frame_buffer(&ctx->buf[i].img);
38 free(ctx->buf);
39 }
40 free(ctx);
41 }
42 }
43
vp9_lookahead_init(unsigned int width,unsigned int height,unsigned int subsampling_x,unsigned int subsampling_y,int use_highbitdepth,unsigned int depth)44 struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
45 unsigned int height,
46 unsigned int subsampling_x,
47 unsigned int subsampling_y,
48 #if CONFIG_VP9_HIGHBITDEPTH
49 int use_highbitdepth,
50 #endif
51 unsigned int depth) {
52 struct lookahead_ctx *ctx = NULL;
53
54 // Clamp the lookahead queue depth
55 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
56
57 // Allocate memory to keep previous source frames available.
58 depth += MAX_PRE_FRAMES;
59
60 // Allocate the lookahead structures
61 ctx = calloc(1, sizeof(*ctx));
62 if (ctx) {
63 const int legacy_byte_alignment = 0;
64 unsigned int i;
65 ctx->max_sz = depth;
66 ctx->buf = calloc(depth, sizeof(*ctx->buf));
67 ctx->next_show_idx = 0;
68 if (!ctx->buf) goto bail;
69 for (i = 0; i < depth; i++)
70 if (vpx_alloc_frame_buffer(
71 &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
72 #if CONFIG_VP9_HIGHBITDEPTH
73 use_highbitdepth,
74 #endif
75 VP9_ENC_BORDER_IN_PIXELS, legacy_byte_alignment))
76 goto bail;
77 }
78 return ctx;
79 bail:
80 vp9_lookahead_destroy(ctx);
81 return NULL;
82 }
83
84 #define USE_PARTIAL_COPY 0
vp9_lookahead_full(const struct lookahead_ctx * ctx)85 int vp9_lookahead_full(const struct lookahead_ctx *ctx) {
86 return ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz;
87 }
88
vp9_lookahead_next_show_idx(const struct lookahead_ctx * ctx)89 int vp9_lookahead_next_show_idx(const struct lookahead_ctx *ctx) {
90 return ctx->next_show_idx;
91 }
92
vp9_lookahead_push(struct lookahead_ctx * ctx,YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,int use_highbitdepth,vpx_enc_frame_flags_t flags)93 int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
94 int64_t ts_start, int64_t ts_end, int use_highbitdepth,
95 vpx_enc_frame_flags_t flags) {
96 struct lookahead_entry *buf;
97 #if USE_PARTIAL_COPY
98 int row, col, active_end;
99 int mb_rows = (src->y_height + 15) >> 4;
100 int mb_cols = (src->y_width + 15) >> 4;
101 #endif
102 int width = src->y_crop_width;
103 int height = src->y_crop_height;
104 int uv_width = src->uv_crop_width;
105 int uv_height = src->uv_crop_height;
106 int subsampling_x = src->subsampling_x;
107 int subsampling_y = src->subsampling_y;
108 int larger_dimensions, new_dimensions;
109 #if !CONFIG_VP9_HIGHBITDEPTH
110 (void)use_highbitdepth;
111 assert(use_highbitdepth == 0);
112 #endif
113
114 if (vp9_lookahead_full(ctx)) return 1;
115 ctx->sz++;
116 buf = pop(ctx, &ctx->write_idx);
117
118 new_dimensions = width != buf->img.y_crop_width ||
119 height != buf->img.y_crop_height ||
120 uv_width != buf->img.uv_crop_width ||
121 uv_height != buf->img.uv_crop_height;
122 larger_dimensions = width > buf->img.y_width || height > buf->img.y_height ||
123 uv_width > buf->img.uv_width ||
124 uv_height > buf->img.uv_height;
125 assert(!larger_dimensions || new_dimensions);
126
127 #if USE_PARTIAL_COPY
128 // TODO(jkoleszar): This is disabled for now, as
129 // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
130
131 // Only do this partial copy if the following conditions are all met:
132 // 1. Lookahead queue has has size of 1.
133 // 2. Active map is provided.
134 // 3. This is not a key frame, golden nor altref frame.
135 if (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
136 for (row = 0; row < mb_rows; ++row) {
137 col = 0;
138
139 while (1) {
140 // Find the first active macroblock in this row.
141 for (; col < mb_cols; ++col) {
142 if (active_map[col]) break;
143 }
144
145 // No more active macroblock in this row.
146 if (col == mb_cols) break;
147
148 // Find the end of active region in this row.
149 active_end = col;
150
151 for (; active_end < mb_cols; ++active_end) {
152 if (!active_map[active_end]) break;
153 }
154
155 // Only copy this active region.
156 vp9_copy_and_extend_frame_with_rect(src, &buf->img, row << 4, col << 4,
157 16, (active_end - col) << 4);
158
159 // Start again from the end of this active region.
160 col = active_end;
161 }
162
163 active_map += mb_cols;
164 }
165 } else {
166 #endif
167 if (larger_dimensions) {
168 YV12_BUFFER_CONFIG new_img;
169 memset(&new_img, 0, sizeof(new_img));
170 if (vpx_alloc_frame_buffer(&new_img, width, height, subsampling_x,
171 subsampling_y,
172 #if CONFIG_VP9_HIGHBITDEPTH
173 use_highbitdepth,
174 #endif
175 VP9_ENC_BORDER_IN_PIXELS, 0))
176 return 1;
177 vpx_free_frame_buffer(&buf->img);
178 buf->img = new_img;
179 } else if (new_dimensions) {
180 buf->img.y_crop_width = src->y_crop_width;
181 buf->img.y_crop_height = src->y_crop_height;
182 buf->img.uv_crop_width = src->uv_crop_width;
183 buf->img.uv_crop_height = src->uv_crop_height;
184 buf->img.subsampling_x = src->subsampling_x;
185 buf->img.subsampling_y = src->subsampling_y;
186 }
187 // Partial copy not implemented yet
188 vp9_copy_and_extend_frame(src, &buf->img);
189 #if USE_PARTIAL_COPY
190 }
191 #endif
192
193 buf->ts_start = ts_start;
194 buf->ts_end = ts_end;
195 buf->flags = flags;
196 buf->show_idx = ctx->next_show_idx;
197 ++ctx->next_show_idx;
198 return 0;
199 }
200
vp9_lookahead_pop(struct lookahead_ctx * ctx,int drain)201 struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
202 int drain) {
203 struct lookahead_entry *buf = NULL;
204
205 if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
206 buf = pop(ctx, &ctx->read_idx);
207 ctx->sz--;
208 }
209 return buf;
210 }
211
vp9_lookahead_peek(struct lookahead_ctx * ctx,int index)212 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
213 int index) {
214 struct lookahead_entry *buf = NULL;
215
216 if (index >= 0) {
217 // Forward peek
218 if (index < ctx->sz) {
219 index += ctx->read_idx;
220 if (index >= ctx->max_sz) index -= ctx->max_sz;
221 buf = ctx->buf + index;
222 }
223 } else if (index < 0) {
224 // Backward peek
225 if (-index <= MAX_PRE_FRAMES) {
226 index += ctx->read_idx;
227 if (index < 0) index += ctx->max_sz;
228 buf = ctx->buf + index;
229 }
230 }
231
232 return buf;
233 }
234
vp9_lookahead_depth(struct lookahead_ctx * ctx)235 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }
236