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