1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11 #include <assert.h>
12 #include <stdlib.h>
13
14 #include "config/aom_config.h"
15
16 #include "av1/common/common.h"
17 #include "av1/encoder/encoder.h"
18 #include "av1/encoder/extend.h"
19 #include "av1/encoder/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
av1_lookahead_destroy(struct lookahead_ctx * ctx)32 void av1_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++) aom_free_frame_buffer(&ctx->buf[i].img);
38 free(ctx->buf);
39 }
40 free(ctx);
41 }
42 }
43
av1_lookahead_init(unsigned int width,unsigned int height,unsigned int subsampling_x,unsigned int subsampling_y,int use_highbitdepth,unsigned int depth,const int border_in_pixels,int is_scale)44 struct lookahead_ctx *av1_lookahead_init(
45 unsigned int width, unsigned int height, unsigned int subsampling_x,
46 unsigned int subsampling_y, int use_highbitdepth, unsigned int depth,
47 const int border_in_pixels, int is_scale) {
48 struct lookahead_ctx *ctx = NULL;
49
50 // Clamp the lookahead queue depth
51 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
52
53 // Allocate memory to keep previous source frames available.
54 depth += MAX_PRE_FRAMES;
55
56 // Allocate the lookahead structures
57 ctx = calloc(1, sizeof(*ctx));
58 if (ctx) {
59 const int legacy_byte_alignment = 0;
60 unsigned int i;
61 ctx->max_sz = depth;
62 ctx->buf = calloc(depth, sizeof(*ctx->buf));
63 if (!ctx->buf) goto bail;
64 for (i = 0; i < depth; i++)
65 if (is_scale) {
66 if (aom_alloc_frame_buffer(
67 &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
68 use_highbitdepth, border_in_pixels, legacy_byte_alignment))
69 goto bail;
70 } else {
71 aom_free_frame_buffer(&ctx->buf[i].img);
72 if (aom_realloc_lookahead_buffer(
73 &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
74 use_highbitdepth, AOM_ENC_LOOKAHEAD_BORDER,
75 legacy_byte_alignment, NULL, NULL, NULL))
76 goto bail;
77 }
78 }
79 return ctx;
80 bail:
81 av1_lookahead_destroy(ctx);
82 return NULL;
83 }
84
85 #define USE_PARTIAL_COPY 0
86
av1_lookahead_push(struct lookahead_ctx * ctx,YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,int use_highbitdepth,aom_enc_frame_flags_t flags)87 int av1_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
88 int64_t ts_start, int64_t ts_end, int use_highbitdepth,
89 aom_enc_frame_flags_t flags) {
90 struct lookahead_entry *buf;
91 #if USE_PARTIAL_COPY
92 int row, col, active_end;
93 int mb_rows = (src->y_height + 15) >> 4;
94 int mb_cols = (src->y_width + 15) >> 4;
95 #endif
96 int width = src->y_crop_width;
97 int height = src->y_crop_height;
98 int uv_width = src->uv_crop_width;
99 int uv_height = src->uv_crop_height;
100 int subsampling_x = src->subsampling_x;
101 int subsampling_y = src->subsampling_y;
102 int larger_dimensions, new_dimensions;
103
104 if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz) return 1;
105 ctx->sz++;
106 buf = pop(ctx, &ctx->write_idx);
107
108 new_dimensions = width != buf->img.y_crop_width ||
109 height != buf->img.y_crop_height ||
110 uv_width != buf->img.uv_crop_width ||
111 uv_height != buf->img.uv_crop_height;
112 larger_dimensions = width > buf->img.y_width || height > buf->img.y_height ||
113 uv_width > buf->img.uv_width ||
114 uv_height > buf->img.uv_height;
115 assert(!larger_dimensions || new_dimensions);
116
117 #if USE_PARTIAL_COPY
118 // TODO(jkoleszar): This is disabled for now, as
119 // av1_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
120
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 (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
126 for (row = 0; row < mb_rows; ++row) {
127 col = 0;
128
129 while (1) {
130 // Find the first active macroblock in this row.
131 for (; col < mb_cols; ++col) {
132 if (active_map[col]) break;
133 }
134
135 // No more active macroblock in this row.
136 if (col == mb_cols) break;
137
138 // Find the end of active region in this row.
139 active_end = col;
140
141 for (; active_end < mb_cols; ++active_end) {
142 if (!active_map[active_end]) break;
143 }
144
145 // Only copy this active region.
146 av1_copy_and_extend_frame_with_rect(src, &buf->img, row << 4, col << 4,
147 16, (active_end - col) << 4);
148
149 // Start again from the end of this active region.
150 col = active_end;
151 }
152
153 active_map += mb_cols;
154 }
155 } else {
156 #endif
157 if (larger_dimensions) {
158 YV12_BUFFER_CONFIG new_img;
159 memset(&new_img, 0, sizeof(new_img));
160 if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
161 subsampling_y, use_highbitdepth,
162 AOM_BORDER_IN_PIXELS, 0))
163 return 1;
164 aom_free_frame_buffer(&buf->img);
165 buf->img = new_img;
166 } else if (new_dimensions) {
167 buf->img.y_crop_width = src->y_crop_width;
168 buf->img.y_crop_height = src->y_crop_height;
169 buf->img.uv_crop_width = src->uv_crop_width;
170 buf->img.uv_crop_height = src->uv_crop_height;
171 buf->img.subsampling_x = src->subsampling_x;
172 buf->img.subsampling_y = src->subsampling_y;
173 }
174 // Partial copy not implemented yet
175 av1_copy_and_extend_frame(src, &buf->img);
176 #if USE_PARTIAL_COPY
177 }
178 #endif
179
180 buf->ts_start = ts_start;
181 buf->ts_end = ts_end;
182 buf->flags = flags;
183 return 0;
184 }
185
av1_lookahead_pop(struct lookahead_ctx * ctx,int drain)186 struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx,
187 int drain) {
188 struct lookahead_entry *buf = NULL;
189
190 if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
191 buf = pop(ctx, &ctx->read_idx);
192 ctx->sz--;
193 }
194 return buf;
195 }
196
av1_lookahead_peek(struct lookahead_ctx * ctx,int index)197 struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx,
198 int index) {
199 struct lookahead_entry *buf = NULL;
200
201 if (index >= 0) {
202 // Forward peek
203 if (index < ctx->sz) {
204 index += ctx->read_idx;
205 if (index >= ctx->max_sz) index -= ctx->max_sz;
206 buf = ctx->buf + index;
207 }
208 } else if (index < 0) {
209 // Backward peek
210 if (-index <= MAX_PRE_FRAMES) {
211 index += (int)(ctx->read_idx);
212 if (index < 0) index += (int)(ctx->max_sz);
213 buf = ctx->buf + index;
214 }
215 }
216
217 return buf;
218 }
219
av1_lookahead_depth(struct lookahead_ctx * ctx)220 unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }
221