Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp9/encoder/vp9_lookahead.c
Line
Count
Source (jump to first uncovered line)
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
106k
static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24
106k
  int index = *idx;
25
106k
  struct lookahead_entry *buf = ctx->buf + index;
26
27
106k
  assert(index < ctx->max_sz);
28
106k
  if (++index >= ctx->max_sz) index -= ctx->max_sz;
29
106k
  *idx = index;
30
106k
  return buf;
31
106k
}
32
33
3.99k
void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
34
3.99k
  if (ctx) {
35
3.87k
    if (ctx->buf) {
36
3.87k
      int i;
37
38
104k
      for (i = 0; i < ctx->max_sz; i++) vpx_free_frame_buffer(&ctx->buf[i].img);
39
3.87k
      free(ctx->buf);
40
3.87k
    }
41
3.87k
    free(ctx);
42
3.87k
  }
43
3.99k
}
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
3.87k
                                         unsigned int depth) {
53
3.87k
  struct lookahead_ctx *ctx = NULL;
54
55
  // Clamp the lookahead queue depth
56
3.87k
  depth = clamp(depth, 1, MAX_LAG_BUFFERS);
57
58
  // Allocate memory to keep previous source frames available.
59
3.87k
  depth += MAX_PRE_FRAMES;
60
61
  // Allocate the lookahead structures
62
3.87k
  ctx = calloc(1, sizeof(*ctx));
63
3.87k
  if (ctx) {
64
3.87k
    const int legacy_byte_alignment = 0;
65
3.87k
    unsigned int i;
66
3.87k
    ctx->max_sz = depth;
67
3.87k
    ctx->buf = calloc(depth, sizeof(*ctx->buf));
68
3.87k
    ctx->next_show_idx = 0;
69
3.87k
    if (!ctx->buf) goto bail;
70
104k
    for (i = 0; i < depth; i++)
71
100k
      if (vpx_alloc_frame_buffer(
72
100k
              &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
73
100k
#if CONFIG_VP9_HIGHBITDEPTH
74
100k
              use_highbitdepth,
75
100k
#endif
76
100k
              VP9_ENC_BORDER_IN_PIXELS, legacy_byte_alignment))
77
0
        goto bail;
78
3.87k
  }
79
3.87k
  return ctx;
80
0
bail:
81
0
  vp9_lookahead_destroy(ctx);
82
0
  return NULL;
83
3.87k
}
84
85
53.7k
int vp9_lookahead_full(const struct lookahead_ctx *ctx) {
86
53.7k
  return ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz;
87
53.7k
}
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
53.7k
                       vpx_enc_frame_flags_t flags) {
96
53.7k
  struct lookahead_entry *buf;
97
53.7k
  int width = src->y_crop_width;
98
53.7k
  int height = src->y_crop_height;
99
53.7k
  int uv_width = src->uv_crop_width;
100
53.7k
  int uv_height = src->uv_crop_height;
101
53.7k
  int subsampling_x = src->subsampling_x;
102
53.7k
  int subsampling_y = src->subsampling_y;
103
53.7k
  int larger_dimensions, new_dimensions;
104
#if !CONFIG_VP9_HIGHBITDEPTH
105
  (void)use_highbitdepth;
106
  assert(use_highbitdepth == 0);
107
#endif
108
109
53.7k
  if (vp9_lookahead_full(ctx)) return 1;
110
53.7k
  ctx->sz++;
111
53.7k
  buf = pop(ctx, &ctx->write_idx);
112
113
53.7k
  new_dimensions = width != buf->img.y_crop_width ||
114
53.7k
                   height != buf->img.y_crop_height ||
115
53.7k
                   uv_width != buf->img.uv_crop_width ||
116
53.7k
                   uv_height != buf->img.uv_crop_height;
117
53.7k
  larger_dimensions =
118
53.7k
      width > buf->img.y_crop_width || height > buf->img.y_crop_height ||
119
53.7k
      uv_width > buf->img.uv_crop_width || uv_height > buf->img.uv_crop_height;
120
53.7k
  assert(!larger_dimensions || new_dimensions);
121
122
53.7k
  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
53.7k
  } 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
53.7k
  vp9_copy_and_extend_frame(src, &buf->img);
155
156
53.7k
  buf->ts_start = ts_start;
157
53.7k
  buf->ts_end = ts_end;
158
53.7k
  buf->flags = flags;
159
53.7k
  buf->show_idx = ctx->next_show_idx;
160
53.7k
  ++ctx->next_show_idx;
161
53.7k
  return 0;
162
53.7k
}
163
164
struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
165
133k
                                          int drain) {
166
133k
  struct lookahead_entry *buf = NULL;
167
168
133k
  if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
169
53.1k
    buf = pop(ctx, &ctx->read_idx);
170
53.1k
    ctx->sz--;
171
53.1k
  }
172
133k
  return buf;
173
133k
}
174
175
struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
176
161k
                                           int index) {
177
161k
  struct lookahead_entry *buf = NULL;
178
179
161k
  if (index >= 0) {
180
    // Forward peek
181
59.8k
    if (index < ctx->sz) {
182
59.8k
      index += ctx->read_idx;
183
59.8k
      if (index >= ctx->max_sz) index -= ctx->max_sz;
184
59.8k
      buf = ctx->buf + index;
185
59.8k
    }
186
101k
  } else if (index < 0) {
187
    // Backward peek
188
101k
    if (-index <= MAX_PRE_FRAMES) {
189
101k
      index += ctx->read_idx;
190
101k
      if (index < 0) index += ctx->max_sz;
191
101k
      buf = ctx->buf + index;
192
101k
    }
193
101k
  }
194
195
161k
  return buf;
196
161k
}
197
198
55.6k
unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }