lookahead.c 6.97 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11 12 13
 */
#include <assert.h>
#include <stdlib.h>

14
#include "./aom_config.h"
Jingning Han's avatar
Jingning Han committed
15

16
#include "av1/common/common.h"
Jingning Han's avatar
Jingning Han committed
17

18 19 20
#include "av1/encoder/encoder.h"
#include "av1/encoder/extend.h"
#include "av1/encoder/lookahead.h"
Jingning Han's avatar
Jingning Han committed
21 22

/* Return the buffer at the given absolute index and increment the index */
23
static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24
  int index = *idx;
Jingning Han's avatar
Jingning Han committed
25 26 27
  struct lookahead_entry *buf = ctx->buf + index;

  assert(index < ctx->max_sz);
28
  if (++index >= ctx->max_sz) index -= ctx->max_sz;
Jingning Han's avatar
Jingning Han committed
29 30 31 32
  *idx = index;
  return buf;
}

33
void av1_lookahead_destroy(struct lookahead_ctx *ctx) {
Jingning Han's avatar
Jingning Han committed
34 35
  if (ctx) {
    if (ctx->buf) {
36
      int i;
Jingning Han's avatar
Jingning Han committed
37

38
      for (i = 0; i < ctx->max_sz; i++) aom_free_frame_buffer(&ctx->buf[i].img);
Jingning Han's avatar
Jingning Han committed
39 40 41 42 43 44
      free(ctx->buf);
    }
    free(ctx);
  }
}

45 46 47 48
struct lookahead_ctx *av1_lookahead_init(unsigned int width,
                                         unsigned int height,
                                         unsigned int subsampling_x,
                                         unsigned int subsampling_y,
49
#if CONFIG_HIGHBITDEPTH
50
                                         int use_highbitdepth,
Jingning Han's avatar
Jingning Han committed
51
#endif
52
                                         unsigned int depth) {
Jingning Han's avatar
Jingning Han committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  struct lookahead_ctx *ctx = NULL;

  // Clamp the lookahead queue depth
  depth = clamp(depth, 1, MAX_LAG_BUFFERS);

  // Allocate memory to keep previous source frames available.
  depth += MAX_PRE_FRAMES;

  // Allocate the lookahead structures
  ctx = calloc(1, sizeof(*ctx));
  if (ctx) {
    const int legacy_byte_alignment = 0;
    unsigned int i;
    ctx->max_sz = depth;
    ctx->buf = calloc(depth, sizeof(*ctx->buf));
68
    if (!ctx->buf) goto bail;
Jingning Han's avatar
Jingning Han committed
69
    for (i = 0; i < depth; i++)
70 71
      if (aom_alloc_frame_buffer(&ctx->buf[i].img, width, height, subsampling_x,
                                 subsampling_y,
72
#if CONFIG_HIGHBITDEPTH
73
                                 use_highbitdepth,
Jingning Han's avatar
Jingning Han committed
74
#endif
75
                                 AOM_BORDER_IN_PIXELS, legacy_byte_alignment))
Jingning Han's avatar
Jingning Han committed
76 77 78
        goto bail;
  }
  return ctx;
79
bail:
80
  av1_lookahead_destroy(ctx);
Jingning Han's avatar
Jingning Han committed
81 82 83 84 85
  return NULL;
}

#define USE_PARTIAL_COPY 0

86 87
int av1_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
                       int64_t ts_start, int64_t ts_end,
88
#if CONFIG_HIGHBITDEPTH
89
                       int use_highbitdepth,
Jingning Han's avatar
Jingning Han committed
90
#endif
91
                       aom_enc_frame_flags_t flags) {
Jingning Han's avatar
Jingning Han committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105
  struct lookahead_entry *buf;
#if USE_PARTIAL_COPY
  int row, col, active_end;
  int mb_rows = (src->y_height + 15) >> 4;
  int mb_cols = (src->y_width + 15) >> 4;
#endif
  int width = src->y_crop_width;
  int height = src->y_crop_height;
  int uv_width = src->uv_crop_width;
  int uv_height = src->uv_crop_height;
  int subsampling_x = src->subsampling_x;
  int subsampling_y = src->subsampling_y;
  int larger_dimensions, new_dimensions;

106
  if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz) return 1;
Jingning Han's avatar
Jingning Han committed
107 108 109 110 111 112 113
  ctx->sz++;
  buf = pop(ctx, &ctx->write_idx);

  new_dimensions = width != buf->img.y_crop_width ||
                   height != buf->img.y_crop_height ||
                   uv_width != buf->img.uv_crop_width ||
                   uv_height != buf->img.uv_crop_height;
114
  larger_dimensions = width > buf->img.y_width || height > buf->img.y_height ||
Jingning Han's avatar
Jingning Han committed
115 116 117 118 119 120
                      uv_width > buf->img.uv_width ||
                      uv_height > buf->img.uv_height;
  assert(!larger_dimensions || new_dimensions);

#if USE_PARTIAL_COPY
  // TODO(jkoleszar): This is disabled for now, as
121
  // av1_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
Jingning Han's avatar
Jingning Han committed
122 123 124 125 126 127 128 129 130 131 132 133

  // Only do this partial copy if the following conditions are all met:
  // 1. Lookahead queue has has size of 1.
  // 2. Active map is provided.
  // 3. This is not a key frame, golden nor altref frame.
  if (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
    for (row = 0; row < mb_rows; ++row) {
      col = 0;

      while (1) {
        // Find the first active macroblock in this row.
        for (; col < mb_cols; ++col) {
134
          if (active_map[col]) break;
Jingning Han's avatar
Jingning Han committed
135 136 137
        }

        // No more active macroblock in this row.
138
        if (col == mb_cols) break;
Jingning Han's avatar
Jingning Han committed
139 140 141 142 143

        // Find the end of active region in this row.
        active_end = col;

        for (; active_end < mb_cols; ++active_end) {
144
          if (!active_map[active_end]) break;
Jingning Han's avatar
Jingning Han committed
145 146 147
        }

        // Only copy this active region.
148 149
        av1_copy_and_extend_frame_with_rect(src, &buf->img, row << 4, col << 4,
                                            16, (active_end - col) << 4);
Jingning Han's avatar
Jingning Han committed
150 151 152 153 154 155 156 157 158 159 160 161

        // Start again from the end of this active region.
        col = active_end;
      }

      active_map += mb_cols;
    }
  } else {
#endif
    if (larger_dimensions) {
      YV12_BUFFER_CONFIG new_img;
      memset(&new_img, 0, sizeof(new_img));
162
      if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
163
                                 subsampling_y,
164
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
165 166
                                 use_highbitdepth,
#endif
167
                                 AOM_BORDER_IN_PIXELS, 0))
168
        return 1;
169
      aom_free_frame_buffer(&buf->img);
Jingning Han's avatar
Jingning Han committed
170 171 172 173 174 175 176 177 178 179
      buf->img = new_img;
    } else if (new_dimensions) {
      buf->img.y_crop_width = src->y_crop_width;
      buf->img.y_crop_height = src->y_crop_height;
      buf->img.uv_crop_width = src->uv_crop_width;
      buf->img.uv_crop_height = src->uv_crop_height;
      buf->img.subsampling_x = src->subsampling_x;
      buf->img.subsampling_y = src->subsampling_y;
    }
    // Partial copy not implemented yet
180
    av1_copy_and_extend_frame(src, &buf->img);
Jingning Han's avatar
Jingning Han committed
181 182 183 184 185 186 187 188 189 190
#if USE_PARTIAL_COPY
  }
#endif

  buf->ts_start = ts_start;
  buf->ts_end = ts_end;
  buf->flags = flags;
  return 0;
}

191 192
struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx,
                                          int drain) {
Jingning Han's avatar
Jingning Han committed
193 194
  struct lookahead_entry *buf = NULL;

195
  if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
Jingning Han's avatar
Jingning Han committed
196 197 198 199 200 201
    buf = pop(ctx, &ctx->read_idx);
    ctx->sz--;
  }
  return buf;
}

202 203
struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx,
                                           int index) {
Jingning Han's avatar
Jingning Han committed
204 205 206 207
  struct lookahead_entry *buf = NULL;

  if (index >= 0) {
    // Forward peek
208
    if (index < ctx->sz) {
Jingning Han's avatar
Jingning Han committed
209
      index += ctx->read_idx;
210
      if (index >= ctx->max_sz) index -= ctx->max_sz;
Jingning Han's avatar
Jingning Han committed
211 212 213 214 215
      buf = ctx->buf + index;
    }
  } else if (index < 0) {
    // Backward peek
    if (-index <= MAX_PRE_FRAMES) {
216 217
      index += (int)(ctx->read_idx);
      if (index < 0) index += (int)(ctx->max_sz);
Jingning Han's avatar
Jingning Han committed
218 219 220 221 222 223 224
      buf = ctx->buf + index;
    }
  }

  return buf;
}

225
unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }