Coverage Report

Created: 2024-09-06 07:53

/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
84.9k
static struct lookahead_entry *pop(struct lookahead_ctx *ctx, int *idx) {
24
84.9k
  int index = *idx;
25
84.9k
  struct lookahead_entry *buf = ctx->buf + index;
26
27
84.9k
  assert(index < ctx->max_sz);
28
84.9k
  if (++index >= ctx->max_sz) index -= ctx->max_sz;
29
84.9k
  *idx = index;
30
84.9k
  return buf;
31
84.9k
}
32
33
2.98k
void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
34
2.98k
  if (ctx) {
35
2.87k
    if (ctx->buf) {
36
2.87k
      int i;
37
38
77.5k
      for (i = 0; i < ctx->max_sz; i++) vpx_free_frame_buffer(&ctx->buf[i].img);
39
2.87k
      free(ctx->buf);
40
2.87k
    }
41
2.87k
    free(ctx);
42
2.87k
  }
43
2.98k
}
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
2.87k
                                         unsigned int depth) {
53
2.87k
  struct lookahead_ctx *ctx = NULL;
54
55
  // Clamp the lookahead queue depth
56
2.87k
  depth = clamp(depth, 1, MAX_LAG_BUFFERS);
57
58
  // Allocate memory to keep previous source frames available.
59
2.87k
  depth += MAX_PRE_FRAMES;
60
61
  // Allocate the lookahead structures
62
2.87k
  ctx = calloc(1, sizeof(*ctx));
63
2.87k
  if (ctx) {
64
2.87k
    const int legacy_byte_alignment = 0;
65
2.87k
    unsigned int i;
66
2.87k
    ctx->max_sz = depth;
67
2.87k
    ctx->buf = calloc(depth, sizeof(*ctx->buf));
68
2.87k
    ctx->next_show_idx = 0;
69
2.87k
    if (!ctx->buf) goto bail;
70
77.5k
    for (i = 0; i < depth; i++)
71
74.6k
      if (vpx_alloc_frame_buffer(
72
74.6k
              &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
73
74.6k
#if CONFIG_VP9_HIGHBITDEPTH
74
74.6k
              use_highbitdepth,
75
74.6k
#endif
76
74.6k
              VP9_ENC_BORDER_IN_PIXELS, legacy_byte_alignment))
77
0
        goto bail;
78
2.87k
  }
79
2.87k
  return ctx;
80
0
bail:
81
0
  vp9_lookahead_destroy(ctx);
82
0
  return NULL;
83
2.87k
}
84
85
42.7k
int vp9_lookahead_full(const struct lookahead_ctx *ctx) {
86
42.7k
  return ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz;
87
42.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
42.7k
                       vpx_enc_frame_flags_t flags) {
96
42.7k
  struct lookahead_entry *buf;
97
42.7k
  int width = src->y_crop_width;
98
42.7k
  int height = src->y_crop_height;
99
42.7k
  int uv_width = src->uv_crop_width;
100
42.7k
  int uv_height = src->uv_crop_height;
101
42.7k
  int subsampling_x = src->subsampling_x;
102
42.7k
  int subsampling_y = src->subsampling_y;
103
42.7k
  int larger_dimensions, new_dimensions;
104
#if !CONFIG_VP9_HIGHBITDEPTH
105
  (void)use_highbitdepth;
106
  assert(use_highbitdepth == 0);
107
#endif
108
109
42.7k
  if (vp9_lookahead_full(ctx)) return 1;
110
42.7k
  ctx->sz++;
111
42.7k
  buf = pop(ctx, &ctx->write_idx);
112
113
42.7k
  new_dimensions = width != buf->img.y_crop_width ||
114
42.7k
                   height != buf->img.y_crop_height ||
115
42.7k
                   uv_width != buf->img.uv_crop_width ||
116
42.7k
                   uv_height != buf->img.uv_crop_height;
117
42.7k
  larger_dimensions =
118
42.7k
      width > buf->img.y_crop_width || height > buf->img.y_crop_height ||
119
42.7k
      uv_width > buf->img.uv_crop_width || uv_height > buf->img.uv_crop_height;
120
42.7k
  assert(!larger_dimensions || new_dimensions);
121
122
42.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
42.7k
  } else if (new_dimensions) {
135
0
    buf->img.y_width = src->y_width;
136
0
    buf->img.y_height = src->y_height;
137
0
    buf->img.uv_width = src->uv_width;
138
0
    buf->img.uv_height = src->uv_height;
139
0
    buf->img.y_crop_width = src->y_crop_width;
140
0
    buf->img.y_crop_height = src->y_crop_height;
141
0
    buf->img.uv_crop_width = src->uv_crop_width;
142
0
    buf->img.uv_crop_height = src->uv_crop_height;
143
0
    buf->img.subsampling_x = src->subsampling_x;
144
0
    buf->img.subsampling_y = src->subsampling_y;
145
0
  }
146
42.7k
  vp9_copy_and_extend_frame(src, &buf->img);
147
148
42.7k
  buf->ts_start = ts_start;
149
42.7k
  buf->ts_end = ts_end;
150
42.7k
  buf->flags = flags;
151
42.7k
  buf->show_idx = ctx->next_show_idx;
152
42.7k
  ++ctx->next_show_idx;
153
42.7k
  return 0;
154
42.7k
}
155
156
struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
157
105k
                                          int drain) {
158
105k
  struct lookahead_entry *buf = NULL;
159
160
105k
  if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
161
42.2k
    buf = pop(ctx, &ctx->read_idx);
162
42.2k
    ctx->sz--;
163
42.2k
  }
164
105k
  return buf;
165
105k
}
166
167
struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
168
120k
                                           int index) {
169
120k
  struct lookahead_entry *buf = NULL;
170
171
120k
  if (index >= 0) {
172
    // Forward peek
173
41.6k
    if (index < ctx->sz) {
174
41.6k
      index += ctx->read_idx;
175
41.6k
      if (index >= ctx->max_sz) index -= ctx->max_sz;
176
41.6k
      buf = ctx->buf + index;
177
41.6k
    }
178
79.1k
  } else if (index < 0) {
179
    // Backward peek
180
79.1k
    if (-index <= MAX_PRE_FRAMES) {
181
79.1k
      index += ctx->read_idx;
182
79.1k
      if (index < 0) index += ctx->max_sz;
183
79.1k
      buf = ctx->buf + index;
184
79.1k
    }
185
79.1k
  }
186
187
120k
  return buf;
188
120k
}
189
190
39.1k
unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) { return ctx->sz; }