• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,bool is_all_intra,int enable_global_motion)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     bool is_all_intra, int enable_global_motion) {
50   int lag_in_frames = AOMMAX(1, depth);
51 
52   // For all-intra frame encoding, previous source frames are not required.
53   // Hence max_pre_frames is set to 0 in this case. As previous source frames
54   // are accessed using a negative index to av1_lookahead_peek(), setting
55   // max_pre_frames to 0 will cause av1_lookahead_peek() to return NULL for a
56   // negative index.
57   const uint8_t max_pre_frames = is_all_intra ? 0 : MAX_PRE_FRAMES;
58 
59   // Add the lags to depth and clamp
60   depth += num_lap_buffers;
61   depth = clamp(depth, 1, MAX_TOTAL_BUFFERS);
62 
63   // Allocate memory to keep previous source frames available.
64   depth += max_pre_frames;
65 
66   // Allocate the lookahead structures
67   struct lookahead_ctx *ctx = calloc(1, sizeof(*ctx));
68   if (ctx) {
69     unsigned int i;
70     ctx->max_sz = depth;
71     ctx->push_frame_count = 0;
72     ctx->max_pre_frames = max_pre_frames;
73     ctx->read_ctxs[ENCODE_STAGE].pop_sz = ctx->max_sz - ctx->max_pre_frames;
74     ctx->read_ctxs[ENCODE_STAGE].valid = 1;
75     if (num_lap_buffers) {
76       ctx->read_ctxs[LAP_STAGE].pop_sz = lag_in_frames;
77       ctx->read_ctxs[LAP_STAGE].valid = 1;
78     }
79     ctx->buf = calloc(depth, sizeof(*ctx->buf));
80     if (!ctx->buf) goto fail;
81     for (i = 0; i < depth; i++) {
82       if (aom_realloc_frame_buffer(
83               &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
84               use_highbitdepth, border_in_pixels, byte_alignment, NULL, NULL,
85               NULL, enable_global_motion, 0)) {
86         goto fail;
87       }
88     }
89   }
90   return ctx;
91 fail:
92   av1_lookahead_destroy(ctx);
93   return NULL;
94 }
95 
av1_lookahead_full(const struct lookahead_ctx * ctx)96 int av1_lookahead_full(const struct lookahead_ctx *ctx) {
97   // TODO(angiebird): Test this function.
98   return ctx->read_ctxs[ENCODE_STAGE].sz >= ctx->read_ctxs[ENCODE_STAGE].pop_sz;
99 }
100 
av1_lookahead_push(struct lookahead_ctx * ctx,const YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,int use_highbitdepth,aom_enc_frame_flags_t flags)101 int av1_lookahead_push(struct lookahead_ctx *ctx, const YV12_BUFFER_CONFIG *src,
102                        int64_t ts_start, int64_t ts_end, int use_highbitdepth,
103                        aom_enc_frame_flags_t flags) {
104   int width = src->y_crop_width;
105   int height = src->y_crop_height;
106   int uv_width = src->uv_crop_width;
107   int uv_height = src->uv_crop_height;
108   int subsampling_x = src->subsampling_x;
109   int subsampling_y = src->subsampling_y;
110   int larger_dimensions, new_dimensions;
111 
112   assert(ctx->read_ctxs[ENCODE_STAGE].valid == 1);
113   if (ctx->read_ctxs[ENCODE_STAGE].sz + ctx->max_pre_frames > ctx->max_sz)
114     return 1;
115 
116   ctx->read_ctxs[ENCODE_STAGE].sz++;
117   if (ctx->read_ctxs[LAP_STAGE].valid) {
118     ctx->read_ctxs[LAP_STAGE].sz++;
119   }
120 
121   struct lookahead_entry *buf = pop(ctx, &ctx->write_idx);
122 
123   new_dimensions = width != buf->img.y_crop_width ||
124                    height != buf->img.y_crop_height ||
125                    uv_width != buf->img.uv_crop_width ||
126                    uv_height != buf->img.uv_crop_height;
127   larger_dimensions = width > buf->img.y_width || height > buf->img.y_height ||
128                       uv_width > buf->img.uv_width ||
129                       uv_height > buf->img.uv_height;
130   assert(!larger_dimensions || new_dimensions);
131 
132   if (larger_dimensions) {
133     YV12_BUFFER_CONFIG new_img;
134     memset(&new_img, 0, sizeof(new_img));
135     if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
136                                subsampling_y, use_highbitdepth,
137                                AOM_BORDER_IN_PIXELS, 0, 0))
138       return 1;
139     aom_free_frame_buffer(&buf->img);
140     buf->img = new_img;
141   } else if (new_dimensions) {
142     buf->img.y_crop_width = src->y_crop_width;
143     buf->img.y_crop_height = src->y_crop_height;
144     buf->img.uv_crop_width = src->uv_crop_width;
145     buf->img.uv_crop_height = src->uv_crop_height;
146     buf->img.subsampling_x = src->subsampling_x;
147     buf->img.subsampling_y = src->subsampling_y;
148   }
149   // Partial copy not implemented yet
150   av1_copy_and_extend_frame(src, &buf->img);
151 
152   buf->ts_start = ts_start;
153   buf->ts_end = ts_end;
154   buf->display_idx = ctx->push_frame_count;
155   buf->flags = flags;
156   ++ctx->push_frame_count;
157   aom_remove_metadata_from_frame_buffer(&buf->img);
158   if (src->metadata &&
159       aom_copy_metadata_to_frame_buffer(&buf->img, src->metadata)) {
160     return 1;
161   }
162   return 0;
163 }
164 
av1_lookahead_pop(struct lookahead_ctx * ctx,int drain,COMPRESSOR_STAGE stage)165 struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx, int drain,
166                                           COMPRESSOR_STAGE stage) {
167   struct lookahead_entry *buf = NULL;
168   if (ctx) {
169     struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
170     assert(read_ctx->valid == 1);
171     if (read_ctx->sz && (drain || read_ctx->sz == read_ctx->pop_sz)) {
172       buf = pop(ctx, &read_ctx->read_idx);
173       read_ctx->sz--;
174     }
175   }
176   return buf;
177 }
178 
av1_lookahead_peek(struct lookahead_ctx * ctx,int index,COMPRESSOR_STAGE stage)179 struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx, int index,
180                                            COMPRESSOR_STAGE stage) {
181   struct lookahead_entry *buf = NULL;
182   if (ctx == NULL) {
183     return buf;
184   }
185 
186   struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
187   assert(read_ctx->valid == 1);
188   if (index >= 0) {
189     // Forward peek
190     if (index < read_ctx->sz) {
191       index += read_ctx->read_idx;
192       if (index >= ctx->max_sz) index -= ctx->max_sz;
193       buf = ctx->buf + index;
194     }
195   } else if (index < 0) {
196     // Backward peek
197     if (-index <= ctx->max_pre_frames) {
198       index += (int)(read_ctx->read_idx);
199       if (index < 0) index += (int)(ctx->max_sz);
200       buf = ctx->buf + index;
201     }
202   }
203 
204   return buf;
205 }
206 
av1_lookahead_depth(struct lookahead_ctx * ctx,COMPRESSOR_STAGE stage)207 unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx,
208                                  COMPRESSOR_STAGE stage) {
209   assert(ctx != NULL);
210 
211   struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
212   assert(read_ctx->valid == 1);
213   return read_ctx->sz;
214 }
215 
av1_lookahead_pop_sz(struct lookahead_ctx * ctx,COMPRESSOR_STAGE stage)216 int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage) {
217   assert(ctx != NULL);
218 
219   struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
220   assert(read_ctx->valid == 1);
221   return read_ctx->pop_sz;
222 }
223