xref: /aosp_15_r20/external/libaom/av1/encoder/lookahead.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
12*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
15*77c1e3ccSAndroid Build Coastguard Worker 
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom_scale/yv12config.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encoder.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/extend.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/lookahead.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker /* Return the buffer at the given absolute index and increment the index */
pop(struct lookahead_ctx * ctx,int * idx)23*77c1e3ccSAndroid Build Coastguard Worker static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24*77c1e3ccSAndroid Build Coastguard Worker   int index = *idx;
25*77c1e3ccSAndroid Build Coastguard Worker   struct lookahead_entry *buf = ctx->buf + index;
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker   assert(index < ctx->max_sz);
28*77c1e3ccSAndroid Build Coastguard Worker   if (++index >= ctx->max_sz) index -= ctx->max_sz;
29*77c1e3ccSAndroid Build Coastguard Worker   *idx = index;
30*77c1e3ccSAndroid Build Coastguard Worker   return buf;
31*77c1e3ccSAndroid Build Coastguard Worker }
32*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_destroy(struct lookahead_ctx * ctx)33*77c1e3ccSAndroid Build Coastguard Worker void av1_lookahead_destroy(struct lookahead_ctx *ctx) {
34*77c1e3ccSAndroid Build Coastguard Worker   if (ctx) {
35*77c1e3ccSAndroid Build Coastguard Worker     if (ctx->buf) {
36*77c1e3ccSAndroid Build Coastguard Worker       int i;
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker       for (i = 0; i < ctx->max_sz; i++) aom_free_frame_buffer(&ctx->buf[i].img);
39*77c1e3ccSAndroid Build Coastguard Worker       free(ctx->buf);
40*77c1e3ccSAndroid Build Coastguard Worker     }
41*77c1e3ccSAndroid Build Coastguard Worker     free(ctx);
42*77c1e3ccSAndroid Build Coastguard Worker   }
43*77c1e3ccSAndroid Build Coastguard Worker }
44*77c1e3ccSAndroid Build Coastguard Worker 
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 byte_alignment,int num_lap_buffers,bool is_all_intra,bool alloc_pyramid)45*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_ctx *av1_lookahead_init(
46*77c1e3ccSAndroid Build Coastguard Worker     unsigned int width, unsigned int height, unsigned int subsampling_x,
47*77c1e3ccSAndroid Build Coastguard Worker     unsigned int subsampling_y, int use_highbitdepth, unsigned int depth,
48*77c1e3ccSAndroid Build Coastguard Worker     const int border_in_pixels, int byte_alignment, int num_lap_buffers,
49*77c1e3ccSAndroid Build Coastguard Worker     bool is_all_intra, bool alloc_pyramid) {
50*77c1e3ccSAndroid Build Coastguard Worker   int lag_in_frames = AOMMAX(1, depth);
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker   // For all-intra frame encoding, previous source frames are not required.
53*77c1e3ccSAndroid Build Coastguard Worker   // Hence max_pre_frames is set to 0 in this case. As previous source frames
54*77c1e3ccSAndroid Build Coastguard Worker   // are accessed using a negative index to av1_lookahead_peek(), setting
55*77c1e3ccSAndroid Build Coastguard Worker   // max_pre_frames to 0 will cause av1_lookahead_peek() to return NULL for a
56*77c1e3ccSAndroid Build Coastguard Worker   // negative index.
57*77c1e3ccSAndroid Build Coastguard Worker   const uint8_t max_pre_frames = is_all_intra ? 0 : MAX_PRE_FRAMES;
58*77c1e3ccSAndroid Build Coastguard Worker 
59*77c1e3ccSAndroid Build Coastguard Worker   // Add the lags to depth and clamp
60*77c1e3ccSAndroid Build Coastguard Worker   depth += num_lap_buffers;
61*77c1e3ccSAndroid Build Coastguard Worker   depth = clamp(depth, 1, MAX_TOTAL_BUFFERS);
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker   // Allocate memory to keep previous source frames available.
64*77c1e3ccSAndroid Build Coastguard Worker   depth += max_pre_frames;
65*77c1e3ccSAndroid Build Coastguard Worker 
66*77c1e3ccSAndroid Build Coastguard Worker   // Allocate the lookahead structures
67*77c1e3ccSAndroid Build Coastguard Worker   struct lookahead_ctx *ctx = calloc(1, sizeof(*ctx));
68*77c1e3ccSAndroid Build Coastguard Worker   if (ctx) {
69*77c1e3ccSAndroid Build Coastguard Worker     unsigned int i;
70*77c1e3ccSAndroid Build Coastguard Worker     ctx->max_sz = depth;
71*77c1e3ccSAndroid Build Coastguard Worker     ctx->push_frame_count = 0;
72*77c1e3ccSAndroid Build Coastguard Worker     ctx->max_pre_frames = max_pre_frames;
73*77c1e3ccSAndroid Build Coastguard Worker     ctx->read_ctxs[ENCODE_STAGE].pop_sz = ctx->max_sz - ctx->max_pre_frames;
74*77c1e3ccSAndroid Build Coastguard Worker     ctx->read_ctxs[ENCODE_STAGE].valid = 1;
75*77c1e3ccSAndroid Build Coastguard Worker     if (num_lap_buffers) {
76*77c1e3ccSAndroid Build Coastguard Worker       ctx->read_ctxs[LAP_STAGE].pop_sz = lag_in_frames;
77*77c1e3ccSAndroid Build Coastguard Worker       ctx->read_ctxs[LAP_STAGE].valid = 1;
78*77c1e3ccSAndroid Build Coastguard Worker     }
79*77c1e3ccSAndroid Build Coastguard Worker     ctx->buf = calloc(depth, sizeof(*ctx->buf));
80*77c1e3ccSAndroid Build Coastguard Worker     if (!ctx->buf) goto fail;
81*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < depth; i++) {
82*77c1e3ccSAndroid Build Coastguard Worker       if (aom_realloc_frame_buffer(
83*77c1e3ccSAndroid Build Coastguard Worker               &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
84*77c1e3ccSAndroid Build Coastguard Worker               use_highbitdepth, border_in_pixels, byte_alignment, NULL, NULL,
85*77c1e3ccSAndroid Build Coastguard Worker               NULL, alloc_pyramid, 0)) {
86*77c1e3ccSAndroid Build Coastguard Worker         goto fail;
87*77c1e3ccSAndroid Build Coastguard Worker       }
88*77c1e3ccSAndroid Build Coastguard Worker     }
89*77c1e3ccSAndroid Build Coastguard Worker   }
90*77c1e3ccSAndroid Build Coastguard Worker   return ctx;
91*77c1e3ccSAndroid Build Coastguard Worker fail:
92*77c1e3ccSAndroid Build Coastguard Worker   av1_lookahead_destroy(ctx);
93*77c1e3ccSAndroid Build Coastguard Worker   return NULL;
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_full(const struct lookahead_ctx * ctx)96*77c1e3ccSAndroid Build Coastguard Worker int av1_lookahead_full(const struct lookahead_ctx *ctx) {
97*77c1e3ccSAndroid Build Coastguard Worker   // TODO(angiebird): Test this function.
98*77c1e3ccSAndroid Build Coastguard Worker   return ctx->read_ctxs[ENCODE_STAGE].sz >= ctx->read_ctxs[ENCODE_STAGE].pop_sz;
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_push(struct lookahead_ctx * ctx,const YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,int use_highbitdepth,bool alloc_pyramid,aom_enc_frame_flags_t flags)101*77c1e3ccSAndroid Build Coastguard Worker int av1_lookahead_push(struct lookahead_ctx *ctx, const YV12_BUFFER_CONFIG *src,
102*77c1e3ccSAndroid Build Coastguard Worker                        int64_t ts_start, int64_t ts_end, int use_highbitdepth,
103*77c1e3ccSAndroid Build Coastguard Worker                        bool alloc_pyramid, aom_enc_frame_flags_t flags) {
104*77c1e3ccSAndroid Build Coastguard Worker   int width = src->y_crop_width;
105*77c1e3ccSAndroid Build Coastguard Worker   int height = src->y_crop_height;
106*77c1e3ccSAndroid Build Coastguard Worker   int uv_width = src->uv_crop_width;
107*77c1e3ccSAndroid Build Coastguard Worker   int uv_height = src->uv_crop_height;
108*77c1e3ccSAndroid Build Coastguard Worker   int subsampling_x = src->subsampling_x;
109*77c1e3ccSAndroid Build Coastguard Worker   int subsampling_y = src->subsampling_y;
110*77c1e3ccSAndroid Build Coastguard Worker   int larger_dimensions, new_dimensions;
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker   assert(ctx->read_ctxs[ENCODE_STAGE].valid == 1);
113*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->read_ctxs[ENCODE_STAGE].sz + ctx->max_pre_frames > ctx->max_sz)
114*77c1e3ccSAndroid Build Coastguard Worker     return 1;
115*77c1e3ccSAndroid Build Coastguard Worker 
116*77c1e3ccSAndroid Build Coastguard Worker   ctx->read_ctxs[ENCODE_STAGE].sz++;
117*77c1e3ccSAndroid Build Coastguard Worker   if (ctx->read_ctxs[LAP_STAGE].valid) {
118*77c1e3ccSAndroid Build Coastguard Worker     ctx->read_ctxs[LAP_STAGE].sz++;
119*77c1e3ccSAndroid Build Coastguard Worker   }
120*77c1e3ccSAndroid Build Coastguard Worker 
121*77c1e3ccSAndroid Build Coastguard Worker   struct lookahead_entry *buf = pop(ctx, &ctx->write_idx);
122*77c1e3ccSAndroid Build Coastguard Worker 
123*77c1e3ccSAndroid Build Coastguard Worker   new_dimensions = width != buf->img.y_crop_width ||
124*77c1e3ccSAndroid Build Coastguard Worker                    height != buf->img.y_crop_height ||
125*77c1e3ccSAndroid Build Coastguard Worker                    uv_width != buf->img.uv_crop_width ||
126*77c1e3ccSAndroid Build Coastguard Worker                    uv_height != buf->img.uv_crop_height;
127*77c1e3ccSAndroid Build Coastguard Worker   larger_dimensions =
128*77c1e3ccSAndroid Build Coastguard Worker       width > buf->img.y_crop_width || height > buf->img.y_crop_height ||
129*77c1e3ccSAndroid Build Coastguard Worker       uv_width > buf->img.uv_crop_width || uv_height > buf->img.uv_crop_height;
130*77c1e3ccSAndroid Build Coastguard Worker   assert(!larger_dimensions || new_dimensions);
131*77c1e3ccSAndroid Build Coastguard Worker 
132*77c1e3ccSAndroid Build Coastguard Worker   if (larger_dimensions) {
133*77c1e3ccSAndroid Build Coastguard Worker     YV12_BUFFER_CONFIG new_img;
134*77c1e3ccSAndroid Build Coastguard Worker     memset(&new_img, 0, sizeof(new_img));
135*77c1e3ccSAndroid Build Coastguard Worker     if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
136*77c1e3ccSAndroid Build Coastguard Worker                                subsampling_y, use_highbitdepth,
137*77c1e3ccSAndroid Build Coastguard Worker                                AOM_BORDER_IN_PIXELS, 0, alloc_pyramid, 0))
138*77c1e3ccSAndroid Build Coastguard Worker       return 1;
139*77c1e3ccSAndroid Build Coastguard Worker     aom_free_frame_buffer(&buf->img);
140*77c1e3ccSAndroid Build Coastguard Worker     buf->img = new_img;
141*77c1e3ccSAndroid Build Coastguard Worker   } else if (new_dimensions) {
142*77c1e3ccSAndroid Build Coastguard Worker     buf->img.y_width = src->y_width;
143*77c1e3ccSAndroid Build Coastguard Worker     buf->img.y_height = src->y_height;
144*77c1e3ccSAndroid Build Coastguard Worker     buf->img.uv_width = src->uv_width;
145*77c1e3ccSAndroid Build Coastguard Worker     buf->img.uv_height = src->uv_height;
146*77c1e3ccSAndroid Build Coastguard Worker     buf->img.y_crop_width = src->y_crop_width;
147*77c1e3ccSAndroid Build Coastguard Worker     buf->img.y_crop_height = src->y_crop_height;
148*77c1e3ccSAndroid Build Coastguard Worker     buf->img.uv_crop_width = src->uv_crop_width;
149*77c1e3ccSAndroid Build Coastguard Worker     buf->img.uv_crop_height = src->uv_crop_height;
150*77c1e3ccSAndroid Build Coastguard Worker     buf->img.subsampling_x = src->subsampling_x;
151*77c1e3ccSAndroid Build Coastguard Worker     buf->img.subsampling_y = src->subsampling_y;
152*77c1e3ccSAndroid Build Coastguard Worker   }
153*77c1e3ccSAndroid Build Coastguard Worker   av1_copy_and_extend_frame(src, &buf->img);
154*77c1e3ccSAndroid Build Coastguard Worker 
155*77c1e3ccSAndroid Build Coastguard Worker   buf->ts_start = ts_start;
156*77c1e3ccSAndroid Build Coastguard Worker   buf->ts_end = ts_end;
157*77c1e3ccSAndroid Build Coastguard Worker   buf->display_idx = ctx->push_frame_count;
158*77c1e3ccSAndroid Build Coastguard Worker   buf->flags = flags;
159*77c1e3ccSAndroid Build Coastguard Worker   ++ctx->push_frame_count;
160*77c1e3ccSAndroid Build Coastguard Worker   aom_remove_metadata_from_frame_buffer(&buf->img);
161*77c1e3ccSAndroid Build Coastguard Worker   if (src->metadata &&
162*77c1e3ccSAndroid Build Coastguard Worker       aom_copy_metadata_to_frame_buffer(&buf->img, src->metadata)) {
163*77c1e3ccSAndroid Build Coastguard Worker     return 1;
164*77c1e3ccSAndroid Build Coastguard Worker   }
165*77c1e3ccSAndroid Build Coastguard Worker   return 0;
166*77c1e3ccSAndroid Build Coastguard Worker }
167*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_pop(struct lookahead_ctx * ctx,int drain,COMPRESSOR_STAGE stage)168*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx, int drain,
169*77c1e3ccSAndroid Build Coastguard Worker                                           COMPRESSOR_STAGE stage) {
170*77c1e3ccSAndroid Build Coastguard Worker   struct lookahead_entry *buf = NULL;
171*77c1e3ccSAndroid Build Coastguard Worker   if (ctx) {
172*77c1e3ccSAndroid Build Coastguard Worker     struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
173*77c1e3ccSAndroid Build Coastguard Worker     assert(read_ctx->valid == 1);
174*77c1e3ccSAndroid Build Coastguard Worker     if (read_ctx->sz && (drain || read_ctx->sz == read_ctx->pop_sz)) {
175*77c1e3ccSAndroid Build Coastguard Worker       buf = pop(ctx, &read_ctx->read_idx);
176*77c1e3ccSAndroid Build Coastguard Worker       read_ctx->sz--;
177*77c1e3ccSAndroid Build Coastguard Worker     }
178*77c1e3ccSAndroid Build Coastguard Worker   }
179*77c1e3ccSAndroid Build Coastguard Worker   return buf;
180*77c1e3ccSAndroid Build Coastguard Worker }
181*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_peek(struct lookahead_ctx * ctx,int index,COMPRESSOR_STAGE stage)182*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx, int index,
183*77c1e3ccSAndroid Build Coastguard Worker                                            COMPRESSOR_STAGE stage) {
184*77c1e3ccSAndroid Build Coastguard Worker   struct lookahead_entry *buf = NULL;
185*77c1e3ccSAndroid Build Coastguard Worker   if (ctx == NULL) {
186*77c1e3ccSAndroid Build Coastguard Worker     return buf;
187*77c1e3ccSAndroid Build Coastguard Worker   }
188*77c1e3ccSAndroid Build Coastguard Worker 
189*77c1e3ccSAndroid Build Coastguard Worker   struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
190*77c1e3ccSAndroid Build Coastguard Worker   assert(read_ctx->valid == 1);
191*77c1e3ccSAndroid Build Coastguard Worker   if (index >= 0) {
192*77c1e3ccSAndroid Build Coastguard Worker     // Forward peek
193*77c1e3ccSAndroid Build Coastguard Worker     if (index < read_ctx->sz) {
194*77c1e3ccSAndroid Build Coastguard Worker       index += read_ctx->read_idx;
195*77c1e3ccSAndroid Build Coastguard Worker       if (index >= ctx->max_sz) index -= ctx->max_sz;
196*77c1e3ccSAndroid Build Coastguard Worker       buf = ctx->buf + index;
197*77c1e3ccSAndroid Build Coastguard Worker     }
198*77c1e3ccSAndroid Build Coastguard Worker   } else if (index < 0) {
199*77c1e3ccSAndroid Build Coastguard Worker     // Backward peek
200*77c1e3ccSAndroid Build Coastguard Worker     if (-index <= ctx->max_pre_frames) {
201*77c1e3ccSAndroid Build Coastguard Worker       index += (int)(read_ctx->read_idx);
202*77c1e3ccSAndroid Build Coastguard Worker       if (index < 0) index += (int)(ctx->max_sz);
203*77c1e3ccSAndroid Build Coastguard Worker       buf = ctx->buf + index;
204*77c1e3ccSAndroid Build Coastguard Worker     }
205*77c1e3ccSAndroid Build Coastguard Worker   }
206*77c1e3ccSAndroid Build Coastguard Worker 
207*77c1e3ccSAndroid Build Coastguard Worker   return buf;
208*77c1e3ccSAndroid Build Coastguard Worker }
209*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_depth(struct lookahead_ctx * ctx,COMPRESSOR_STAGE stage)210*77c1e3ccSAndroid Build Coastguard Worker unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx,
211*77c1e3ccSAndroid Build Coastguard Worker                                  COMPRESSOR_STAGE stage) {
212*77c1e3ccSAndroid Build Coastguard Worker   assert(ctx != NULL);
213*77c1e3ccSAndroid Build Coastguard Worker 
214*77c1e3ccSAndroid Build Coastguard Worker   struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
215*77c1e3ccSAndroid Build Coastguard Worker   assert(read_ctx->valid == 1);
216*77c1e3ccSAndroid Build Coastguard Worker   return read_ctx->sz;
217*77c1e3ccSAndroid Build Coastguard Worker }
218*77c1e3ccSAndroid Build Coastguard Worker 
av1_lookahead_pop_sz(struct lookahead_ctx * ctx,COMPRESSOR_STAGE stage)219*77c1e3ccSAndroid Build Coastguard Worker int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage) {
220*77c1e3ccSAndroid Build Coastguard Worker   assert(ctx != NULL);
221*77c1e3ccSAndroid Build Coastguard Worker 
222*77c1e3ccSAndroid Build Coastguard Worker   struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
223*77c1e3ccSAndroid Build Coastguard Worker   assert(read_ctx->valid == 1);
224*77c1e3ccSAndroid Build Coastguard Worker   return read_ctx->pop_sz;
225*77c1e3ccSAndroid Build Coastguard Worker }
226