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