Coverage Report

Created: 2025-07-11 06:43

/src/libavif/ext/aom/av1/encoder/lookahead.c
Line
Count
Source (jump to first uncovered line)
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 */
23
264k
static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24
264k
  int index = *idx;
25
264k
  struct lookahead_entry *buf = ctx->buf + index;
26
27
264k
  assert(index < ctx->max_sz);
28
264k
  if (++index >= ctx->max_sz) index -= ctx->max_sz;
29
264k
  *idx = index;
30
264k
  return buf;
31
264k
}
32
33
76.1k
void av1_lookahead_destroy(struct lookahead_ctx *ctx) {
34
76.1k
  if (ctx) {
35
76.1k
    if (ctx->buf) {
36
76.1k
      int i;
37
38
432k
      for (i = 0; i < ctx->max_sz; i++) aom_free_frame_buffer(&ctx->buf[i].img);
39
76.1k
      free(ctx->buf);
40
76.1k
    }
41
76.1k
    free(ctx);
42
76.1k
  }
43
76.1k
}
44
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
76.1k
    bool is_all_intra, bool alloc_pyramid) {
50
76.1k
  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
76.1k
  const uint8_t max_pre_frames = is_all_intra ? 0 : MAX_PRE_FRAMES;
58
59
  // Add the lags to depth and clamp
60
76.1k
  depth += num_lap_buffers;
61
76.1k
  depth = clamp(depth, 1, MAX_TOTAL_BUFFERS);
62
63
  // Allocate memory to keep previous source frames available.
64
76.1k
  depth += max_pre_frames;
65
66
  // Allocate the lookahead structures
67
76.1k
  struct lookahead_ctx *ctx = calloc(1, sizeof(*ctx));
68
76.1k
  if (ctx) {
69
76.1k
    unsigned int i;
70
76.1k
    ctx->max_sz = depth;
71
76.1k
    ctx->push_frame_count = 0;
72
76.1k
    ctx->max_pre_frames = max_pre_frames;
73
76.1k
    ctx->read_ctxs[ENCODE_STAGE].pop_sz = ctx->max_sz - ctx->max_pre_frames;
74
76.1k
    ctx->read_ctxs[ENCODE_STAGE].valid = 1;
75
76.1k
    if (num_lap_buffers) {
76
7.52k
      ctx->read_ctxs[LAP_STAGE].pop_sz = lag_in_frames;
77
7.52k
      ctx->read_ctxs[LAP_STAGE].valid = 1;
78
7.52k
    }
79
76.1k
    ctx->buf = calloc(depth, sizeof(*ctx->buf));
80
76.1k
    if (!ctx->buf) goto fail;
81
432k
    for (i = 0; i < depth; i++) {
82
356k
      if (aom_realloc_frame_buffer(
83
356k
              &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
84
356k
              use_highbitdepth, border_in_pixels, byte_alignment, NULL, NULL,
85
356k
              NULL, alloc_pyramid, 0)) {
86
0
        goto fail;
87
0
      }
88
356k
    }
89
76.1k
  }
90
76.1k
  return ctx;
91
0
fail:
92
0
  av1_lookahead_destroy(ctx);
93
0
  return NULL;
94
76.1k
}
95
96
0
int av1_lookahead_full(const struct lookahead_ctx *ctx) {
97
  // TODO(angiebird): Test this function.
98
0
  return ctx->read_ctxs[ENCODE_STAGE].sz >= ctx->read_ctxs[ENCODE_STAGE].pop_sz;
99
0
}
100
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
122k
                       bool alloc_pyramid, aom_enc_frame_flags_t flags) {
104
122k
  int width = src->y_crop_width;
105
122k
  int height = src->y_crop_height;
106
122k
  int uv_width = src->uv_crop_width;
107
122k
  int uv_height = src->uv_crop_height;
108
122k
  int subsampling_x = src->subsampling_x;
109
122k
  int subsampling_y = src->subsampling_y;
110
122k
  int larger_dimensions, new_dimensions;
111
112
122k
  assert(ctx->read_ctxs[ENCODE_STAGE].valid == 1);
113
122k
  if (ctx->read_ctxs[ENCODE_STAGE].sz + ctx->max_pre_frames > ctx->max_sz)
114
0
    return 1;
115
116
122k
  ctx->read_ctxs[ENCODE_STAGE].sz++;
117
122k
  if (ctx->read_ctxs[LAP_STAGE].valid) {
118
20.4k
    ctx->read_ctxs[LAP_STAGE].sz++;
119
20.4k
  }
120
121
122k
  struct lookahead_entry *buf = pop(ctx, &ctx->write_idx);
122
123
122k
  new_dimensions = width != buf->img.y_crop_width ||
124
122k
                   height != buf->img.y_crop_height ||
125
122k
                   uv_width != buf->img.uv_crop_width ||
126
122k
                   uv_height != buf->img.uv_crop_height;
127
122k
  larger_dimensions =
128
122k
      width > buf->img.y_crop_width || height > buf->img.y_crop_height ||
129
122k
      uv_width > buf->img.uv_crop_width || uv_height > buf->img.uv_crop_height;
130
122k
  assert(!larger_dimensions || new_dimensions);
131
132
122k
  if (larger_dimensions) {
133
0
    YV12_BUFFER_CONFIG new_img;
134
0
    memset(&new_img, 0, sizeof(new_img));
135
0
    if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
136
0
                               subsampling_y, use_highbitdepth,
137
0
                               AOM_BORDER_IN_PIXELS, 0, alloc_pyramid, 0))
138
0
      return 1;
139
0
    aom_free_frame_buffer(&buf->img);
140
0
    buf->img = new_img;
141
122k
  } else if (new_dimensions) {
142
0
    buf->img.y_width = src->y_width;
143
0
    buf->img.y_height = src->y_height;
144
0
    buf->img.uv_width = src->uv_width;
145
0
    buf->img.uv_height = src->uv_height;
146
0
    buf->img.y_crop_width = src->y_crop_width;
147
0
    buf->img.y_crop_height = src->y_crop_height;
148
0
    buf->img.uv_crop_width = src->uv_crop_width;
149
0
    buf->img.uv_crop_height = src->uv_crop_height;
150
0
    buf->img.subsampling_x = src->subsampling_x;
151
0
    buf->img.subsampling_y = src->subsampling_y;
152
0
  }
153
122k
  av1_copy_and_extend_frame(src, &buf->img);
154
155
122k
  buf->ts_start = ts_start;
156
122k
  buf->ts_end = ts_end;
157
122k
  buf->display_idx = ctx->push_frame_count;
158
122k
  buf->flags = flags;
159
122k
  ++ctx->push_frame_count;
160
122k
  aom_remove_metadata_from_frame_buffer(&buf->img);
161
122k
  if (src->metadata &&
162
122k
      aom_copy_metadata_to_frame_buffer(&buf->img, src->metadata)) {
163
0
    return 1;
164
0
  }
165
122k
  return 0;
166
122k
}
167
168
struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx, int drain,
169
142k
                                          COMPRESSOR_STAGE stage) {
170
142k
  struct lookahead_entry *buf = NULL;
171
142k
  if (ctx) {
172
142k
    struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
173
142k
    assert(read_ctx->valid == 1);
174
142k
    if (read_ctx->sz && (drain || read_ctx->sz == read_ctx->pop_sz)) {
175
142k
      buf = pop(ctx, &read_ctx->read_idx);
176
142k
      read_ctx->sz--;
177
142k
    }
178
142k
  }
179
142k
  return buf;
180
142k
}
181
182
struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx, int index,
183
926k
                                           COMPRESSOR_STAGE stage) {
184
926k
  struct lookahead_entry *buf = NULL;
185
926k
  if (ctx == NULL) {
186
0
    return buf;
187
0
  }
188
189
926k
  struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
190
926k
  assert(read_ctx->valid == 1);
191
926k
  if (index >= 0) {
192
    // Forward peek
193
867k
    if (index < read_ctx->sz) {
194
609k
      index += read_ctx->read_idx;
195
609k
      if (index >= ctx->max_sz) index -= ctx->max_sz;
196
609k
      buf = ctx->buf + index;
197
609k
    }
198
867k
  } else if (index < 0) {
199
    // Backward peek
200
58.8k
    if (-index <= ctx->max_pre_frames) {
201
58.8k
      index += (int)(read_ctx->read_idx);
202
58.8k
      if (index < 0) index += (int)(ctx->max_sz);
203
58.8k
      buf = ctx->buf + index;
204
58.8k
    }
205
58.8k
  }
206
207
926k
  return buf;
208
926k
}
209
210
unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx,
211
414k
                                 COMPRESSOR_STAGE stage) {
212
414k
  assert(ctx != NULL);
213
214
414k
  struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
215
414k
  assert(read_ctx->valid == 1);
216
414k
  return read_ctx->sz;
217
414k
}
218
219
401k
int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage) {
220
401k
  assert(ctx != NULL);
221
222
401k
  struct read_ctx *read_ctx = &ctx->read_ctxs[stage];
223
401k
  assert(read_ctx->valid == 1);
224
401k
  return read_ctx->pop_sz;
225
401k
}