media/libvpx/vp9/encoder/vp9_lookahead.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp9/encoder/vp9_lookahead.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2011 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +#include <assert.h>
    1.14 +#include <stdlib.h>
    1.15 +
    1.16 +#include "./vpx_config.h"
    1.17 +#include "vp9/common/vp9_common.h"
    1.18 +#include "vp9/encoder/vp9_lookahead.h"
    1.19 +#include "vp9/common/vp9_extend.h"
    1.20 +
    1.21 +struct lookahead_ctx {
    1.22 +  unsigned int max_sz;         /* Absolute size of the queue */
    1.23 +  unsigned int sz;             /* Number of buffers currently in the queue */
    1.24 +  unsigned int read_idx;       /* Read index */
    1.25 +  unsigned int write_idx;      /* Write index */
    1.26 +  struct lookahead_entry *buf; /* Buffer list */
    1.27 +};
    1.28 +
    1.29 +
    1.30 +/* Return the buffer at the given absolute index and increment the index */
    1.31 +static struct lookahead_entry * pop(struct lookahead_ctx *ctx,
    1.32 +                                    unsigned int *idx) {
    1.33 +  unsigned int index = *idx;
    1.34 +  struct lookahead_entry *buf = ctx->buf + index;
    1.35 +
    1.36 +  assert(index < ctx->max_sz);
    1.37 +  if (++index >= ctx->max_sz)
    1.38 +    index -= ctx->max_sz;
    1.39 +  *idx = index;
    1.40 +  return buf;
    1.41 +}
    1.42 +
    1.43 +
    1.44 +void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
    1.45 +  if (ctx) {
    1.46 +    if (ctx->buf) {
    1.47 +      unsigned int i;
    1.48 +
    1.49 +      for (i = 0; i < ctx->max_sz; i++)
    1.50 +        vp9_free_frame_buffer(&ctx->buf[i].img);
    1.51 +      free(ctx->buf);
    1.52 +    }
    1.53 +    free(ctx);
    1.54 +  }
    1.55 +}
    1.56 +
    1.57 +
    1.58 +struct lookahead_ctx * vp9_lookahead_init(unsigned int width,
    1.59 +                                          unsigned int height,
    1.60 +                                          unsigned int subsampling_x,
    1.61 +                                          unsigned int subsampling_y,
    1.62 +                                          unsigned int depth) {
    1.63 +  struct lookahead_ctx *ctx = NULL;
    1.64 +
    1.65 +  // Clamp the lookahead queue depth
    1.66 +  depth = clamp(depth, 1, MAX_LAG_BUFFERS);
    1.67 +
    1.68 +  // Allocate the lookahead structures
    1.69 +  ctx = calloc(1, sizeof(*ctx));
    1.70 +  if (ctx) {
    1.71 +    unsigned int i;
    1.72 +    ctx->max_sz = depth;
    1.73 +    ctx->buf = calloc(depth, sizeof(*ctx->buf));
    1.74 +    if (!ctx->buf)
    1.75 +      goto bail;
    1.76 +    for (i = 0; i < depth; i++)
    1.77 +      if (vp9_alloc_frame_buffer(&ctx->buf[i].img,
    1.78 +                                 width, height, subsampling_x, subsampling_y,
    1.79 +                                 VP9BORDERINPIXELS))
    1.80 +        goto bail;
    1.81 +  }
    1.82 +  return ctx;
    1.83 + bail:
    1.84 +  vp9_lookahead_destroy(ctx);
    1.85 +  return NULL;
    1.86 +}
    1.87 +
    1.88 +#define USE_PARTIAL_COPY 0
    1.89 +
    1.90 +int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG   *src,
    1.91 +                       int64_t ts_start, int64_t ts_end, unsigned int flags,
    1.92 +                       unsigned char *active_map) {
    1.93 +  struct lookahead_entry *buf;
    1.94 +#if USE_PARTIAL_COPY
    1.95 +  int row, col, active_end;
    1.96 +  int mb_rows = (src->y_height + 15) >> 4;
    1.97 +  int mb_cols = (src->y_width + 15) >> 4;
    1.98 +#endif
    1.99 +
   1.100 +  if (ctx->sz + 1 > ctx->max_sz)
   1.101 +    return 1;
   1.102 +  ctx->sz++;
   1.103 +  buf = pop(ctx, &ctx->write_idx);
   1.104 +
   1.105 +#if USE_PARTIAL_COPY
   1.106 +  // TODO(jkoleszar): This is disabled for now, as
   1.107 +  // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
   1.108 +
   1.109 +  // Only do this partial copy if the following conditions are all met:
   1.110 +  // 1. Lookahead queue has has size of 1.
   1.111 +  // 2. Active map is provided.
   1.112 +  // 3. This is not a key frame, golden nor altref frame.
   1.113 +  if (ctx->max_sz == 1 && active_map && !flags) {
   1.114 +    for (row = 0; row < mb_rows; ++row) {
   1.115 +      col = 0;
   1.116 +
   1.117 +      while (1) {
   1.118 +        // Find the first active macroblock in this row.
   1.119 +        for (; col < mb_cols; ++col) {
   1.120 +          if (active_map[col])
   1.121 +            break;
   1.122 +        }
   1.123 +
   1.124 +        // No more active macroblock in this row.
   1.125 +        if (col == mb_cols)
   1.126 +          break;
   1.127 +
   1.128 +        // Find the end of active region in this row.
   1.129 +        active_end = col;
   1.130 +
   1.131 +        for (; active_end < mb_cols; ++active_end) {
   1.132 +          if (!active_map[active_end])
   1.133 +            break;
   1.134 +        }
   1.135 +
   1.136 +        // Only copy this active region.
   1.137 +        vp9_copy_and_extend_frame_with_rect(src, &buf->img,
   1.138 +                                            row << 4,
   1.139 +                                            col << 4, 16,
   1.140 +                                            (active_end - col) << 4);
   1.141 +
   1.142 +        // Start again from the end of this active region.
   1.143 +        col = active_end;
   1.144 +      }
   1.145 +
   1.146 +      active_map += mb_cols;
   1.147 +    }
   1.148 +  } else {
   1.149 +    vp9_copy_and_extend_frame(src, &buf->img);
   1.150 +  }
   1.151 +#else
   1.152 +  // Partial copy not implemented yet
   1.153 +  vp9_copy_and_extend_frame(src, &buf->img);
   1.154 +#endif
   1.155 +
   1.156 +  buf->ts_start = ts_start;
   1.157 +  buf->ts_end = ts_end;
   1.158 +  buf->flags = flags;
   1.159 +  return 0;
   1.160 +}
   1.161 +
   1.162 +
   1.163 +struct lookahead_entry * vp9_lookahead_pop(struct lookahead_ctx *ctx,
   1.164 +                                           int drain) {
   1.165 +  struct lookahead_entry *buf = NULL;
   1.166 +
   1.167 +  if (ctx->sz && (drain || ctx->sz == ctx->max_sz)) {
   1.168 +    buf = pop(ctx, &ctx->read_idx);
   1.169 +    ctx->sz--;
   1.170 +  }
   1.171 +  return buf;
   1.172 +}
   1.173 +
   1.174 +
   1.175 +struct lookahead_entry * vp9_lookahead_peek(struct lookahead_ctx *ctx,
   1.176 +                                            int index) {
   1.177 +  struct lookahead_entry *buf = NULL;
   1.178 +
   1.179 +  assert(index < (int)ctx->max_sz);
   1.180 +  if (index < (int)ctx->sz) {
   1.181 +    index += ctx->read_idx;
   1.182 +    if (index >= (int)ctx->max_sz)
   1.183 +      index -= ctx->max_sz;
   1.184 +    buf = ctx->buf + index;
   1.185 +  }
   1.186 +  return buf;
   1.187 +}
   1.188 +
   1.189 +unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) {
   1.190 +  return ctx->sz;
   1.191 +}

mercurial