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