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 "aom_scale/yv12config.h"
17 #include "av1/common/common.h"
18 #include "av1/encoder/encoder.h"
19 #include "av1/encoder/extend.h"
20 #include "av1/encoder/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 
av1_lookahead_destroy(struct lookahead_ctx * ctx)33 void av1_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++) aom_free_frame_buffer(&ctx->buf[i].img);
39       free(ctx->buf);
40     }
41     free(ctx);
42   }
43 }
44 
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)45 struct lookahead_ctx *av1_lookahead_init(
46     unsigned int width, unsigned int height, unsigned int subsampling_x,
47     unsigned int subsampling_y, int use_highbitdepth, unsigned int depth,
48     const int border_in_pixels, int byte_alignment, int num_lap_buffers) {
49   struct lookahead_ctx *ctx = NULL;
50   int lag_in_frames = AOMMAX(1, depth);
51 
52   // Add the lags to depth and clamp
53   depth += num_lap_buffers;
54   depth = clamp(depth, 1, MAX_TOTAL_BUFFERS);
55 
56   // Allocate memory to keep previous source frames available.
57   depth += MAX_PRE_FRAMES;
58 
59   // Allocate the lookahead structures
60   ctx = calloc(1, sizeof(*ctx));
61   if (ctx) {
62     unsigned int i;
63     ctx->max_sz = depth;
64     ctx->read_ctxs[ENCODE_STAGE].pop_sz = ctx->max_sz - MAX_PRE_FRAMES;
65     ctx->read_ctxs[ENCODE_STAGE].valid = 1;
66     if (num_lap_buffers) {
67       ctx->read_ctxs[LAP_STAGE].pop_sz = lag_in_frames;
68       ctx->read_ctxs[LAP_STAGE].valid = 1;
69     }
70     ctx->buf = calloc(depth, sizeof(*ctx->buf));
71     if (!ctx->buf) goto fail;
72     for (i = 0; i < depth; i++) {
73       aom_free_frame_buffer(&ctx->buf[i].img);
74       if (aom_realloc_frame_buffer(&ctx->buf[i].img, width, height,
75                                    subsampling_x, subsampling_y,
76                                    use_highbitdepth, border_in_pixels,
77                                    byte_alignment, NULL, NULL, NULL))
78         goto fail;
79     }
80   }
81   return ctx;
82 fail:
83   av1_lookahead_destroy(ctx);
84   return NULL;
85 }
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   int width = src->y_crop_width;
92   int height = src->y_crop_height;
93   int uv_width = src->uv_crop_width;
94   int uv_height = src->uv_crop_height;
95   int subsampling_x = src->subsampling_x;
96   int subsampling_y = src->subsampling_y;
97   int larger_dimensions, new_dimensions;
98 
99   assert(ctx->read_ctxs[ENCODE_STAGE].valid == 1);
100   if (ctx->read_ctxs[ENCODE_STAGE].sz + 1 + MAX_PRE_FRAMES > ctx->max_sz)
101     return 1;
102   ctx->read_ctxs[ENCODE_STAGE].sz++;
103   if (ctx->read_ctxs[LAP_STAGE].valid) {
104     ctx->read_ctxs[LAP_STAGE].sz++;
105   }
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 (larger_dimensions) {
118     YV12_BUFFER_CONFIG new_img;
119     memset(&new_img, 0, sizeof(new_img));
120     if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
121                                subsampling_y, use_highbitdepth,
122                                AOM_BORDER_IN_PIXELS, 0))
123       return 1;
124     aom_free_frame_buffer(&buf->img);
125     buf->img = new_img;
126   } else if (new_dimensions) {
127     buf->img.y_crop_width = src->y_crop_width;
128     buf->img.y_crop_height = src->y_crop_height;
129     buf->img.uv_crop_width = src->uv_crop_width;
130     buf->img.uv_crop_height = src->uv_crop_height;
131     buf->img.subsampling_x = src->subsampling_x;
132     buf->img.subsampling_y = src->subsampling_y;
133   }
134   // Partial copy not implemented yet
135   av1_copy_and_extend_frame(src, &buf->img);
136 
137   buf->ts_start = ts_start;
138   buf->ts_end = ts_end;
139   buf->flags = flags;
140   aom_remove_metadata_from_frame_buffer(&buf->img);
141   aom_copy_metadata_to_frame_buffer(&buf->img, src->metadata);
142   return 0;
143 }
144 
av1_lookahead_pop(struct lookahead_ctx * ctx,int drain,COMPRESSOR_STAGE stage)145 struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx, int drain,
146                                           COMPRESSOR_STAGE stage) {
147   struct lookahead_entry *buf = NULL;
148   if (ctx) {
149     struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
150     assert(read_ctx->valid == 1);
151     if (read_ctx->sz && (drain || read_ctx->sz == read_ctx->pop_sz)) {
152       buf = pop(ctx, &read_ctx->read_idx);
153       read_ctx->sz--;
154     }
155   }
156   return buf;
157 }
158 
av1_lookahead_peek(struct lookahead_ctx * ctx,int index,COMPRESSOR_STAGE stage)159 struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx, int index,
160                                            COMPRESSOR_STAGE stage) {
161   struct lookahead_entry *buf = NULL;
162   struct read_ctx *read_ctx = NULL;
163   if (ctx == NULL) {
164     return buf;
165   }
166 
167   read_ctx = &ctx->read_ctxs[stage];
168   assert(read_ctx->valid == 1);
169   if (index >= 0) {
170     // Forward peek
171     if (index < read_ctx->sz) {
172       index += read_ctx->read_idx;
173       if (index >= ctx->max_sz) index -= ctx->max_sz;
174       buf = ctx->buf + index;
175     }
176   } else if (index < 0) {
177     // Backward peek
178     if (-index <= MAX_PRE_FRAMES) {
179       index += (int)(read_ctx->read_idx);
180       if (index < 0) index += (int)(ctx->max_sz);
181       buf = ctx->buf + index;
182     }
183   }
184 
185   return buf;
186 }
187 
av1_lookahead_depth(struct lookahead_ctx * ctx,COMPRESSOR_STAGE stage)188 unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx,
189                                  COMPRESSOR_STAGE stage) {
190   struct read_ctx *read_ctx = NULL;
191   assert(ctx != NULL);
192 
193   read_ctx = &ctx->read_ctxs[stage];
194   assert(read_ctx->valid == 1);
195   return read_ctx->sz;
196 }
197 
av1_lookahead_pop_sz(struct lookahead_ctx * ctx,COMPRESSOR_STAGE stage)198 int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage) {
199   struct read_ctx *read_ctx = NULL;
200   assert(ctx != NULL);
201 
202   read_ctx = &ctx->read_ctxs[stage];
203   assert(read_ctx->valid == 1);
204   return read_ctx->pop_sz;
205 }
206