Coverage Report

Created: 2025-11-16 07:20

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