Coverage Report

Created: 2026-09-04 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/encoder/vp9_lookahead.c
Line
Count
Source
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
#include <string.h>
13
14
#include "./vpx_config.h"
15
16
#include "vp9/common/vp9_common.h"
17
18
#include "vp9/encoder/vp9_encoder.h"
19
#include "vp9/encoder/vp9_extend.h"
20
#include "vp9/encoder/vp9_lookahead.h"
21
22
/* Return the buffer at the given absolute index and increment the index */
23
843k
static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24
843k
  int index = *idx;
25
843k
  struct lookahead_entry *buf = ctx->buf + index;
26
27
843k
  assert(index < ctx->max_sz);
28
843k
  if (++index >= ctx->max_sz) index -= ctx->max_sz;
29
843k
  *idx = index;
30
843k
  return buf;
31
843k
}
32
33
10.6k
void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
34
10.6k
  if (ctx) {
35
10.6k
    if (ctx->buf) {
36
10.6k
      int i;
37
38
286k
      for (i = 0; i < ctx->max_sz; i++) vpx_free_frame_buffer(&ctx->buf[i].img);
39
10.6k
      free(ctx->buf);
40
10.6k
    }
41
10.6k
    free(ctx);
42
10.6k
  }
43
10.6k
}
44
45
struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
46
                                         unsigned int height,
47
                                         unsigned int subsampling_x,
48
                                         unsigned int subsampling_y,
49
#if CONFIG_VP9_HIGHBITDEPTH
50
                                         int use_highbitdepth,
51
#endif
52
10.6k
                                         unsigned int depth) {
53
10.6k
  struct lookahead_ctx *ctx = NULL;
54
55
  // Clamp the lookahead queue depth
56
10.6k
  depth = clamp(depth, 1, MAX_LAG_BUFFERS);
57
58
  // Allocate memory to keep previous source frames available.
59
10.6k
  depth += MAX_PRE_FRAMES;
60
61
  // Allocate the lookahead structures
62
10.6k
  ctx = calloc(1, sizeof(*ctx));
63
10.6k
  if (ctx) {
64
10.6k
    const int legacy_byte_alignment = 0;
65
10.6k
    unsigned int i;
66
10.6k
    ctx->max_sz = depth;
67
10.6k
    ctx->buf = calloc(depth, sizeof(*ctx->buf));
68
10.6k
    ctx->next_show_idx = 0;
69
10.6k
    if (!ctx->buf) goto bail;
70
286k
    for (i = 0; i < depth; i++)
71
276k
      if (vpx_alloc_frame_buffer(
72
276k
              &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
73
276k
#if CONFIG_VP9_HIGHBITDEPTH
74
276k
              use_highbitdepth,
75
276k
#endif
76
276k
              VP9_ENC_BORDER_IN_PIXELS, legacy_byte_alignment))
77
0
        goto bail;
78
10.6k
  }
79
10.6k
  return ctx;
80
0
bail:
81
0
  vp9_lookahead_destroy(ctx);
82
0
  return NULL;
83
10.6k
}
84
85
423k
int vp9_lookahead_full(const struct lookahead_ctx *ctx) {
86
423k
  return ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz;
87
423k
}
88
89
0
int vp9_lookahead_next_show_idx(const struct lookahead_ctx *ctx) {
90
0
  return ctx->next_show_idx;
91
0
}
92
93
int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
94
                       int64_t ts_start, int64_t ts_end, int use_highbitdepth,
95
423k
                       vpx_enc_frame_flags_t flags) {
96
423k
  struct lookahead_entry *buf;
97
423k
  int width = src->y_crop_width;
98
423k
  int height = src->y_crop_height;
99
423k
  int uv_width = src->uv_crop_width;
100
423k
  int uv_height = src->uv_crop_height;
101
423k
  int subsampling_x = src->subsampling_x;
102
423k
  int subsampling_y = src->subsampling_y;
103
423k
  int larger_dimensions, new_dimensions;
104
#if !CONFIG_VP9_HIGHBITDEPTH
105
  (void)use_highbitdepth;
106
  assert(use_highbitdepth == 0);
107
#endif
108
109
423k
  if (vp9_lookahead_full(ctx)) return 1;
110
423k
  ctx->sz++;
111
423k
  buf = pop(ctx, &ctx->write_idx);
112
113
423k
  new_dimensions = width != buf->img.y_crop_width ||
114
423k
                   height != buf->img.y_crop_height ||
115
423k
                   uv_width != buf->img.uv_crop_width ||
116
423k
                   uv_height != buf->img.uv_crop_height;
117
423k
  larger_dimensions =
118
423k
      width > buf->img.y_crop_width || height > buf->img.y_crop_height ||
119
423k
      uv_width > buf->img.uv_crop_width || uv_height > buf->img.uv_crop_height;
120
423k
  assert(!larger_dimensions || new_dimensions);
121
122
423k
  if (larger_dimensions) {
123
0
    YV12_BUFFER_CONFIG new_img;
124
0
    memset(&new_img, 0, sizeof(new_img));
125
0
    if (vpx_alloc_frame_buffer(&new_img, width, height, subsampling_x,
126
0
                               subsampling_y,
127
0
#if CONFIG_VP9_HIGHBITDEPTH
128
0
                               use_highbitdepth,
129
0
#endif
130
0
                               VP9_ENC_BORDER_IN_PIXELS, 0))
131
0
      return 1;
132
0
    vpx_free_frame_buffer(&buf->img);
133
0
    buf->img = new_img;
134
423k
  } else if (new_dimensions) {
135
0
    int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
136
0
    buf->img.y_width = src->y_width;
137
0
    buf->img.y_height = src->y_height;
138
0
    buf->img.uv_width = src->uv_width;
139
0
    buf->img.uv_height = src->uv_height;
140
0
    buf->img.y_crop_width = src->y_crop_width;
141
0
    buf->img.y_crop_height = src->y_crop_height;
142
0
    buf->img.uv_crop_width = src->uv_crop_width;
143
0
    buf->img.uv_crop_height = src->uv_crop_height;
144
0
    buf->img.subsampling_x = src->subsampling_x;
145
0
    buf->img.subsampling_y = src->subsampling_y;
146
    // Here the new width (src->y_crop_width) is <= the previous width
147
    // (since otherwise it would enter the "larger_dimensions" code), so
148
    // it is safe here to update the stride.
149
    // The stride setting is taken from vpx_alloc_frame_buffer().
150
0
    buf->img.y_stride =
151
0
        ALIGN_POWER_OF_TWO((aligned_width + 2 * buf->img.border), 5);
152
0
    buf->img.uv_stride = buf->img.y_stride >> subsampling_x;
153
0
  }
154
423k
  vp9_copy_and_extend_frame(src, &buf->img);
155
156
423k
  buf->ts_start = ts_start;
157
423k
  buf->ts_end = ts_end;
158
423k
  buf->flags = flags;
159
423k
  buf->show_idx = ctx->next_show_idx;
160
423k
  ++ctx->next_show_idx;
161
423k
  return 0;
162
423k
}
163
164
struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
165
860k
                                          int drain) {
166
860k
  struct lookahead_entry *buf = NULL;
167
168
860k
  if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
169
420k
    buf = pop(ctx, &ctx->read_idx);
170
420k
    ctx->sz--;
171
420k
  }
172
860k
  return buf;
173
860k
}
174
175
struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
176
1.47M
                                           int index) {
177
1.47M
  struct lookahead_entry *buf = NULL;
178
179
1.47M
  if (index >= 0) {
180
    // Forward peek
181
756k
    if (index < ctx->sz) {
182
753k
      index += ctx->read_idx;
183
753k
      if (index >= ctx->max_sz) index -= ctx->max_sz;
184
753k
      buf = ctx->buf + index;
185
753k
    }
186
756k
  } else if (index < 0) {
187
    // Backward peek
188
719k
    if (-index <= MAX_PRE_FRAMES) {
189
719k
      index += ctx->read_idx;
190
719k
      if (index < 0) index += ctx->max_sz;
191
719k
      buf = ctx->buf + index;
192
719k
    }
193
719k
  }
194
195
1.47M
  return buf;
196
1.47M
}
197
198
466k
unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }