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 #include <string.h>
13
14 #include "./vpx_config.h"
15
16 #include "vp9/common/vp9_common.h"
17
18 #include "vp9/encoder/vp9_encoder.h"
19 #include "vp9/encoder/vp9_extend.h"
20 #include "vp9/encoder/vp9_lookahead.h"
21
22 /* Return the buffer at the given absolute index and increment the index */
pop(struct lookahead_ctx * ctx,int * idx)23 static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24 int index = *idx;
25 struct lookahead_entry *buf = ctx->buf + index;
26
27 assert(index < ctx->max_sz);
28 if (++index >= ctx->max_sz) index -= ctx->max_sz;
29 *idx = index;
30 return buf;
31 }
32
vp9_lookahead_destroy(struct lookahead_ctx * ctx)33 void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
34 if (ctx) {
35 if (ctx->buf) {
36 int i;
37
38 for (i = 0; i < ctx->max_sz; i++) vpx_free_frame_buffer(&ctx->buf[i].img);
39 free(ctx->buf);
40 }
41 free(ctx);
42 }
43 }
44
vp9_lookahead_init(unsigned int width,unsigned int height,unsigned int subsampling_x,unsigned int subsampling_y,int use_highbitdepth,unsigned int depth)45 struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
46 unsigned int height,
47 unsigned int subsampling_x,
48 unsigned int subsampling_y,
49 #if CONFIG_VP9_HIGHBITDEPTH
50 int use_highbitdepth,
51 #endif
52 unsigned int depth) {
53 struct lookahead_ctx *ctx = NULL;
54
55 // Clamp the lookahead queue depth
56 depth = clamp(depth, 1, MAX_LAG_BUFFERS);
57
58 // Allocate memory to keep previous source frames available.
59 depth += MAX_PRE_FRAMES;
60
61 // Allocate the lookahead structures
62 ctx = calloc(1, sizeof(*ctx));
63 if (ctx) {
64 const int legacy_byte_alignment = 0;
65 unsigned int i;
66 ctx->max_sz = depth;
67 ctx->buf = calloc(depth, sizeof(*ctx->buf));
68 ctx->next_show_idx = 0;
69 if (!ctx->buf) goto bail;
70 for (i = 0; i < depth; i++)
71 if (vpx_alloc_frame_buffer(
72 &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
73 #if CONFIG_VP9_HIGHBITDEPTH
74 use_highbitdepth,
75 #endif
76 VP9_ENC_BORDER_IN_PIXELS, legacy_byte_alignment))
77 goto bail;
78 }
79 return ctx;
80 bail:
81 vp9_lookahead_destroy(ctx);
82 return NULL;
83 }
84
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 int width = src->y_crop_width;
98 int height = src->y_crop_height;
99 int uv_width = src->uv_crop_width;
100 int uv_height = src->uv_crop_height;
101 int subsampling_x = src->subsampling_x;
102 int subsampling_y = src->subsampling_y;
103 int larger_dimensions, new_dimensions;
104 #if !CONFIG_VP9_HIGHBITDEPTH
105 (void)use_highbitdepth;
106 assert(use_highbitdepth == 0);
107 #endif
108
109 if (vp9_lookahead_full(ctx)) return 1;
110 ctx->sz++;
111 buf = pop(ctx, &ctx->write_idx);
112
113 new_dimensions = width != buf->img.y_crop_width ||
114 height != buf->img.y_crop_height ||
115 uv_width != buf->img.uv_crop_width ||
116 uv_height != buf->img.uv_crop_height;
117 larger_dimensions =
118 width > buf->img.y_crop_width || height > buf->img.y_crop_height ||
119 uv_width > buf->img.uv_crop_width || uv_height > buf->img.uv_crop_height;
120 assert(!larger_dimensions || new_dimensions);
121
122 if (larger_dimensions) {
123 YV12_BUFFER_CONFIG new_img;
124 memset(&new_img, 0, sizeof(new_img));
125 if (vpx_alloc_frame_buffer(&new_img, width, height, subsampling_x,
126 subsampling_y,
127 #if CONFIG_VP9_HIGHBITDEPTH
128 use_highbitdepth,
129 #endif
130 VP9_ENC_BORDER_IN_PIXELS, 0))
131 return 1;
132 vpx_free_frame_buffer(&buf->img);
133 buf->img = new_img;
134 } else if (new_dimensions) {
135 buf->img.y_width = src->y_width;
136 buf->img.y_height = src->y_height;
137 buf->img.uv_width = src->uv_width;
138 buf->img.uv_height = src->uv_height;
139 buf->img.y_crop_width = src->y_crop_width;
140 buf->img.y_crop_height = src->y_crop_height;
141 buf->img.uv_crop_width = src->uv_crop_width;
142 buf->img.uv_crop_height = src->uv_crop_height;
143 buf->img.subsampling_x = src->subsampling_x;
144 buf->img.subsampling_y = src->subsampling_y;
145 }
146 vp9_copy_and_extend_frame(src, &buf->img);
147
148 buf->ts_start = ts_start;
149 buf->ts_end = ts_end;
150 buf->flags = flags;
151 buf->show_idx = ctx->next_show_idx;
152 ++ctx->next_show_idx;
153 return 0;
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 && 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
vp9_lookahead_peek(struct lookahead_ctx * ctx,int index)167 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
168 int index) {
169 struct lookahead_entry *buf = NULL;
170
171 if (index >= 0) {
172 // Forward peek
173 if (index < ctx->sz) {
174 index += ctx->read_idx;
175 if (index >= ctx->max_sz) index -= ctx->max_sz;
176 buf = ctx->buf + index;
177 }
178 } else if (index < 0) {
179 // Backward peek
180 if (-index <= MAX_PRE_FRAMES) {
181 index += ctx->read_idx;
182 if (index < 0) index += ctx->max_sz;
183 buf = ctx->buf + index;
184 }
185 }
186
187 return buf;
188 }
189
vp9_lookahead_depth(struct lookahead_ctx * ctx)190 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }
191