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 
13 #include "./vpx_config.h"
14 
15 #include "vp9/common/vp9_common.h"
16 
17 #include "vp9/encoder/vp9_encoder.h"
18 #include "vp9/encoder/vp9_extend.h"
19 #include "vp9/encoder/vp9_lookahead.h"
20 
21 /* Return the buffer at the given absolute index and increment the index */
pop(struct lookahead_ctx * ctx,unsigned int * idx)22 static struct lookahead_entry *pop(struct lookahead_ctx *ctx,
23                                    unsigned int *idx) {
24   unsigned int index = *idx;
25   struct lookahead_entry *buf = ctx->buf + index;
26 
27   assert(index < ctx->max_sz);
28   if (++index >= ctx->max_sz)
29     index -= ctx->max_sz;
30   *idx = index;
31   return buf;
32 }
33 
34 
vp9_lookahead_destroy(struct lookahead_ctx * ctx)35 void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
36   if (ctx) {
37     if (ctx->buf) {
38       unsigned int i;
39 
40       for (i = 0; i < ctx->max_sz; i++)
41         vpx_free_frame_buffer(&ctx->buf[i].img);
42       free(ctx->buf);
43     }
44     free(ctx);
45   }
46 }
47 
48 
vp9_lookahead_init(unsigned int width,unsigned int height,unsigned int subsampling_x,unsigned int subsampling_y,int use_highbitdepth,unsigned int depth)49 struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
50                                          unsigned int height,
51                                          unsigned int subsampling_x,
52                                          unsigned int subsampling_y,
53 #if CONFIG_VP9_HIGHBITDEPTH
54                                          int use_highbitdepth,
55 #endif
56                                          unsigned int depth) {
57   struct lookahead_ctx *ctx = NULL;
58 
59   // Clamp the lookahead queue depth
60   depth = clamp(depth, 1, MAX_LAG_BUFFERS);
61 
62   // Allocate memory to keep previous source frames available.
63   depth += MAX_PRE_FRAMES;
64 
65   // Allocate the lookahead structures
66   ctx = calloc(1, sizeof(*ctx));
67   if (ctx) {
68     const int legacy_byte_alignment = 0;
69     unsigned int i;
70     ctx->max_sz = depth;
71     ctx->buf = calloc(depth, sizeof(*ctx->buf));
72     if (!ctx->buf)
73       goto bail;
74     for (i = 0; i < depth; i++)
75       if (vpx_alloc_frame_buffer(&ctx->buf[i].img,
76                                  width, height, subsampling_x, subsampling_y,
77 #if CONFIG_VP9_HIGHBITDEPTH
78                                  use_highbitdepth,
79 #endif
80                                  VP9_ENC_BORDER_IN_PIXELS,
81                                  legacy_byte_alignment))
82         goto bail;
83   }
84   return ctx;
85  bail:
86   vp9_lookahead_destroy(ctx);
87   return NULL;
88 }
89 
90 #define USE_PARTIAL_COPY 0
91 
vp9_lookahead_push(struct lookahead_ctx * ctx,YV12_BUFFER_CONFIG * src,int64_t ts_start,int64_t ts_end,int use_highbitdepth,unsigned int flags)92 int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG   *src,
93                        int64_t ts_start, int64_t ts_end,
94 #if CONFIG_VP9_HIGHBITDEPTH
95                        int use_highbitdepth,
96 #endif
97                        unsigned int flags) {
98   struct lookahead_entry *buf;
99 #if USE_PARTIAL_COPY
100   int row, col, active_end;
101   int mb_rows = (src->y_height + 15) >> 4;
102   int mb_cols = (src->y_width + 15) >> 4;
103 #endif
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   if (ctx->sz + 1  + MAX_PRE_FRAMES > ctx->max_sz)
113     return 1;
114   ctx->sz++;
115   buf = pop(ctx, &ctx->write_idx);
116 
117   new_dimensions = width != buf->img.y_crop_width ||
118                    height != buf->img.y_crop_height ||
119                    uv_width != buf->img.uv_crop_width ||
120                    uv_height != buf->img.uv_crop_height;
121   larger_dimensions = width > buf->img.y_width ||
122                       height > buf->img.y_height ||
123                       uv_width > buf->img.uv_width ||
124                       uv_height > buf->img.uv_height;
125   assert(!larger_dimensions || new_dimensions);
126 
127 #if USE_PARTIAL_COPY
128   // TODO(jkoleszar): This is disabled for now, as
129   // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
130 
131   // Only do this partial copy if the following conditions are all met:
132   // 1. Lookahead queue has has size of 1.
133   // 2. Active map is provided.
134   // 3. This is not a key frame, golden nor altref frame.
135   if (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
136     for (row = 0; row < mb_rows; ++row) {
137       col = 0;
138 
139       while (1) {
140         // Find the first active macroblock in this row.
141         for (; col < mb_cols; ++col) {
142           if (active_map[col])
143             break;
144         }
145 
146         // No more active macroblock in this row.
147         if (col == mb_cols)
148           break;
149 
150         // Find the end of active region in this row.
151         active_end = col;
152 
153         for (; active_end < mb_cols; ++active_end) {
154           if (!active_map[active_end])
155             break;
156         }
157 
158         // Only copy this active region.
159         vp9_copy_and_extend_frame_with_rect(src, &buf->img,
160                                             row << 4,
161                                             col << 4, 16,
162                                             (active_end - col) << 4);
163 
164         // Start again from the end of this active region.
165         col = active_end;
166       }
167 
168       active_map += mb_cols;
169     }
170   } else {
171 #endif
172     if (larger_dimensions) {
173       YV12_BUFFER_CONFIG new_img;
174       memset(&new_img, 0, sizeof(new_img));
175       if (vpx_alloc_frame_buffer(&new_img,
176                                  width, height, subsampling_x, subsampling_y,
177 #if CONFIG_VP9_HIGHBITDEPTH
178                                  use_highbitdepth,
179 #endif
180                                  VP9_ENC_BORDER_IN_PIXELS,
181                                  0))
182           return 1;
183       vpx_free_frame_buffer(&buf->img);
184       buf->img = new_img;
185     } else if (new_dimensions) {
186       buf->img.y_crop_width = src->y_crop_width;
187       buf->img.y_crop_height = src->y_crop_height;
188       buf->img.uv_crop_width = src->uv_crop_width;
189       buf->img.uv_crop_height = src->uv_crop_height;
190       buf->img.subsampling_x = src->subsampling_x;
191       buf->img.subsampling_y = src->subsampling_y;
192     }
193     // Partial copy not implemented yet
194     vp9_copy_and_extend_frame(src, &buf->img);
195 #if USE_PARTIAL_COPY
196   }
197 #endif
198 
199   buf->ts_start = ts_start;
200   buf->ts_end = ts_end;
201   buf->flags = flags;
202   return 0;
203 }
204 
205 
vp9_lookahead_pop(struct lookahead_ctx * ctx,int drain)206 struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
207                                           int drain) {
208   struct lookahead_entry *buf = NULL;
209 
210   if (ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
211     buf = pop(ctx, &ctx->read_idx);
212     ctx->sz--;
213   }
214   return buf;
215 }
216 
217 
vp9_lookahead_peek(struct lookahead_ctx * ctx,int index)218 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
219                                            int index) {
220   struct lookahead_entry *buf = NULL;
221 
222   if (index >= 0) {
223     // Forward peek
224     if (index < (int)ctx->sz) {
225       index += ctx->read_idx;
226       if (index >= (int)ctx->max_sz)
227         index -= ctx->max_sz;
228       buf = ctx->buf + index;
229     }
230   } else if (index < 0) {
231     // Backward peek
232     if (-index <= MAX_PRE_FRAMES) {
233       index += ctx->read_idx;
234       if (index < 0)
235         index += ctx->max_sz;
236       buf = ctx->buf + index;
237     }
238   }
239 
240   return buf;
241 }
242 
vp9_lookahead_depth(struct lookahead_ctx * ctx)243 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) {
244   return ctx->sz;
245 }
246