Coverage Report

Created: 2022-08-24 06:11

/src/aom/av1/encoder/gop_structure.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2019, 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
12
#include <stdint.h>
13
14
#include "av1/common/blockd.h"
15
#include "config/aom_config.h"
16
#include "config/aom_scale_rtcd.h"
17
18
#include "aom/aom_codec.h"
19
#include "aom/aom_encoder.h"
20
21
#include "av1/common/av1_common_int.h"
22
23
#include "av1/encoder/encoder.h"
24
#include "av1/encoder/firstpass.h"
25
#include "av1/encoder/gop_structure.h"
26
27
#if CONFIG_FRAME_PARALLEL_ENCODE
28
// This function sets gf_group->frame_parallel_level for LF_UPDATE frames based
29
// on the value of parallel_frame_count.
30
static void set_frame_parallel_level(int *frame_parallel_level,
31
                                     int *parallel_frame_count,
32
                                     int max_parallel_frames) {
33
  assert(*parallel_frame_count > 0);
34
  // parallel_frame_count > 1 indicates subsequent frame(s) in the current
35
  // parallel encode set.
36
  *frame_parallel_level = 1 + (*parallel_frame_count > 1);
37
  // Update the count of no. of parallel frames.
38
  (*parallel_frame_count)++;
39
  if (*parallel_frame_count > max_parallel_frames) *parallel_frame_count = 1;
40
}
41
42
// This function sets gf_group->src_offset based on frame_parallel_level.
43
// Outputs are gf_group->src_offset and first_frame_index
44
static void set_src_offset(GF_GROUP *const gf_group, int *first_frame_index,
45
                           int cur_frame_idx, int frame_ind) {
46
  if (gf_group->frame_parallel_level[frame_ind] > 0) {
47
    if (gf_group->frame_parallel_level[frame_ind] == 1) {
48
      *first_frame_index = cur_frame_idx;
49
    }
50
51
    // Obtain the offset of the frame at frame_ind in the lookahead queue by
52
    // subtracting the display order hints of the current frame from the display
53
    // order hint of the first frame in parallel encoding set (at
54
    // first_frame_index).
55
    gf_group->src_offset[frame_ind] =
56
        (cur_frame_idx + gf_group->arf_src_offset[frame_ind]) -
57
        *first_frame_index;
58
  }
59
}
60
61
#if CONFIG_FRAME_PARALLEL_ENCODE_2
62
// Sets the GF_GROUP params for LF_UPDATE frames.
63
static AOM_INLINE void set_params_for_leaf_frames(
64
    const TWO_PASS *twopass, const TWO_PASS_FRAME *twopass_frame,
65
    const PRIMARY_RATE_CONTROL *p_rc, FRAME_INFO *frame_info,
66
    GF_GROUP *const gf_group, int *cur_frame_idx, int *frame_ind,
67
    int *parallel_frame_count, int max_parallel_frames,
68
    int do_frame_parallel_encode, int *first_frame_index, int *cur_disp_index,
69
    int layer_depth, int start, int end) {
70
  gf_group->update_type[*frame_ind] = LF_UPDATE;
71
  gf_group->arf_src_offset[*frame_ind] = 0;
72
  gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
73
  gf_group->layer_depth[*frame_ind] = MAX_ARF_LAYERS;
74
  gf_group->frame_type[*frame_ind] = INTER_FRAME;
75
  gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
76
  gf_group->max_layer_depth = AOMMAX(gf_group->max_layer_depth, layer_depth);
77
  gf_group->display_idx[*frame_ind] = (*cur_disp_index);
78
  gf_group->arf_boost[*frame_ind] =
79
      av1_calc_arf_boost(twopass, twopass_frame, p_rc, frame_info, start,
80
                         end - start, 0, NULL, NULL, 0);
81
  ++(*cur_disp_index);
82
83
  // Set the level of parallelism for the LF_UPDATE frame.
84
  if (do_frame_parallel_encode) {
85
    set_frame_parallel_level(&gf_group->frame_parallel_level[*frame_ind],
86
                             parallel_frame_count, max_parallel_frames);
87
    // Set LF_UPDATE frames as non-reference frames.
88
    gf_group->is_frame_non_ref[*frame_ind] = 1;
89
  }
90
  set_src_offset(gf_group, first_frame_index, *cur_frame_idx, *frame_ind);
91
92
  ++(*frame_ind);
93
  ++(*cur_frame_idx);
94
}
95
96
// Sets the GF_GROUP params for INTNL_OVERLAY_UPDATE frames.
97
static AOM_INLINE void set_params_for_intnl_overlay_frames(
98
    GF_GROUP *const gf_group, int *cur_frame_idx, int *frame_ind,
99
    int *first_frame_index, int *cur_disp_index, int layer_depth) {
100
  gf_group->update_type[*frame_ind] = INTNL_OVERLAY_UPDATE;
101
  gf_group->arf_src_offset[*frame_ind] = 0;
102
  gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
103
  gf_group->layer_depth[*frame_ind] = layer_depth;
104
  gf_group->frame_type[*frame_ind] = INTER_FRAME;
105
  gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
106
  gf_group->display_idx[*frame_ind] = (*cur_disp_index);
107
  ++(*cur_disp_index);
108
109
  set_src_offset(gf_group, first_frame_index, *cur_frame_idx, *frame_ind);
110
  ++(*frame_ind);
111
  ++(*cur_frame_idx);
112
}
113
114
// Sets the GF_GROUP params for INTNL_ARF_UPDATE frames.
115
static AOM_INLINE void set_params_for_internal_arfs(
116
    const TWO_PASS *twopass, const TWO_PASS_FRAME *twopass_frame,
117
    const PRIMARY_RATE_CONTROL *p_rc, FRAME_INFO *frame_info,
118
    GF_GROUP *const gf_group, int *cur_frame_idx, int *frame_ind,
119
    int *parallel_frame_count, int max_parallel_frames,
120
    int do_frame_parallel_encode, int *first_frame_index, int depth_thr,
121
    int *cur_disp_idx, int layer_depth, int arf_src_offset, int offset,
122
    int f_frames, int b_frames) {
123
  gf_group->update_type[*frame_ind] = INTNL_ARF_UPDATE;
124
  gf_group->arf_src_offset[*frame_ind] = arf_src_offset;
125
  gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
126
  gf_group->layer_depth[*frame_ind] = layer_depth;
127
  gf_group->frame_type[*frame_ind] = INTER_FRAME;
128
  gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
129
  gf_group->display_idx[*frame_ind] =
130
      (*cur_disp_idx) + gf_group->arf_src_offset[*frame_ind];
131
  gf_group->arf_boost[*frame_ind] =
132
      av1_calc_arf_boost(twopass, twopass_frame, p_rc, frame_info, offset,
133
                         f_frames, b_frames, NULL, NULL, 0);
134
135
  if (do_frame_parallel_encode) {
136
    if (depth_thr != INT_MAX) {
137
      assert(depth_thr == 3 || depth_thr == 4);
138
      assert(IMPLIES(depth_thr == 3, layer_depth == 4));
139
      assert(IMPLIES(depth_thr == 4, layer_depth == 5));
140
      // Set frame_parallel_level of the first frame in the given layer to 1.
141
      if (gf_group->layer_depth[(*frame_ind) - 1] != layer_depth) {
142
        gf_group->frame_parallel_level[*frame_ind] = 1;
143
      } else {
144
        // Set frame_parallel_level of the consecutive frame in the same given
145
        // layer to 2.
146
        assert(gf_group->frame_parallel_level[(*frame_ind) - 1] == 1);
147
        gf_group->frame_parallel_level[*frame_ind] = 2;
148
        // Store the display order hints of the past 2 INTNL_ARF_UPDATE
149
        // frames which would not have been displayed at the time of the encode
150
        // of current frame.
151
        gf_group->skip_frame_refresh[*frame_ind][0] =
152
            gf_group->display_idx[(*frame_ind) - 1];
153
        gf_group->skip_frame_refresh[*frame_ind][1] =
154
            gf_group->display_idx[(*frame_ind) - 2];
155
        // Set the display_idx of frame_parallel_level 1 frame in
156
        // gf_group->skip_frame_as_ref.
157
        gf_group->skip_frame_as_ref[*frame_ind] =
158
            gf_group->display_idx[(*frame_ind) - 1];
159
      }
160
    }
161
    // If max_parallel_frames is not exceeded and if the frame will not be
162
    // temporally filtered, encode the next internal ARF frame in parallel.
163
    if (*parallel_frame_count > 1 &&
164
        *parallel_frame_count <= max_parallel_frames) {
165
      if (gf_group->arf_src_offset[*frame_ind] < TF_LOOKAHEAD_IDX_THR)
166
        gf_group->frame_parallel_level[*frame_ind] = 2;
167
      *parallel_frame_count = 1;
168
    }
169
  }
170
  set_src_offset(gf_group, first_frame_index, *cur_frame_idx, *frame_ind);
171
  ++(*frame_ind);
172
}
173
174
// Set parameters for frames between 'start' and 'end' (excluding both).
175
static void set_multi_layer_params_for_fp(
176
    const TWO_PASS *twopass, const TWO_PASS_FRAME *twopass_frame,
177
    GF_GROUP *const gf_group, const PRIMARY_RATE_CONTROL *p_rc,
178
    RATE_CONTROL *rc, FRAME_INFO *frame_info, int start, int end,
179
    int *cur_frame_idx, int *frame_ind, int *parallel_frame_count,
180
    int max_parallel_frames, int do_frame_parallel_encode,
181
    int *first_frame_index, int depth_thr, int *cur_disp_idx, int layer_depth) {
182
  const int num_frames_to_process = end - start;
183
184
  // Either we are at the last level of the pyramid, or we don't have enough
185
  // frames between 'l' and 'r' to create one more level.
186
  if (layer_depth > gf_group->max_layer_depth_allowed ||
187
      num_frames_to_process < 3) {
188
    // Leaf nodes.
189
    while (start < end) {
190
      set_params_for_leaf_frames(twopass, twopass_frame, p_rc, frame_info,
191
                                 gf_group, cur_frame_idx, frame_ind,
192
                                 parallel_frame_count, max_parallel_frames,
193
                                 do_frame_parallel_encode, first_frame_index,
194
                                 cur_disp_idx, layer_depth, start, end);
195
      ++start;
196
    }
197
  } else {
198
    const int m = (start + end - 1) / 2;
199
200
    // Internal ARF.
201
    int arf_src_offset = m - start;
202
    set_params_for_internal_arfs(
203
        twopass, twopass_frame, p_rc, frame_info, gf_group, cur_frame_idx,
204
        frame_ind, parallel_frame_count, max_parallel_frames,
205
        do_frame_parallel_encode, first_frame_index, INT_MAX, cur_disp_idx,
206
        layer_depth, arf_src_offset, m, end - m, m - start);
207
208
    // If encode reordering is enabled, configure the multi-layers accordingly
209
    // and return. For e.g., the encode order for gf-interval 16 after
210
    // reordering would be 0-> 16-> 8-> 4-> 2-> 6-> 1-> 3-> 5-> 7-> 12-> 10->
211
    // 14-> 9-> 11-> 13-> 15.
212
    if (layer_depth >= depth_thr) {
213
      int m1 = (m + start - 1) / 2;
214
      int m2 = (m + 1 + end) / 2;
215
      int arf_src_offsets[2] = { m1 - start, m2 - start };
216
      // Parameters to compute arf_boost.
217
      int offset[2] = { m1, m2 };
218
      int f_frames[2] = { m - m1, end - m2 };
219
      int b_frames[2] = { m1 - start, m2 - (m + 1) };
220
221
      // Set GF_GROUP params for INTNL_ARF_UPDATE frames which are reordered.
222
      for (int i = 0; i < 2; i++) {
223
        set_params_for_internal_arfs(
224
            twopass, twopass_frame, p_rc, frame_info, gf_group, cur_frame_idx,
225
            frame_ind, parallel_frame_count, max_parallel_frames,
226
            do_frame_parallel_encode, first_frame_index, depth_thr,
227
            cur_disp_idx, layer_depth + 1, arf_src_offsets[i], offset[i],
228
            f_frames[i], b_frames[i]);
229
      }
230
231
      // Initialize the start and end indices to configure LF_UPDATE frames.
232
      int start_idx[4] = { start, m1 + 1, m + 1, end - 1 };
233
      int end_idx[4] = { m1, m, m2, end };
234
      int layer_depth_for_intnl_overlay[4] = { layer_depth + 1, layer_depth,
235
                                               layer_depth + 1, INVALID_IDX };
236
237
      // Set GF_GROUP params for the rest of LF_UPDATE and INTNL_OVERLAY_UPDATE
238
      // frames after reordering.
239
      for (int i = 0; i < 4; i++) {
240
        set_multi_layer_params_for_fp(
241
            twopass, twopass_frame, gf_group, p_rc, rc, frame_info,
242
            start_idx[i], end_idx[i], cur_frame_idx, frame_ind,
243
            parallel_frame_count, max_parallel_frames, do_frame_parallel_encode,
244
            first_frame_index, depth_thr, cur_disp_idx, layer_depth + 2);
245
        if (layer_depth_for_intnl_overlay[i] != INVALID_IDX)
246
          set_params_for_intnl_overlay_frames(
247
              gf_group, cur_frame_idx, frame_ind, first_frame_index,
248
              cur_disp_idx, layer_depth_for_intnl_overlay[i]);
249
      }
250
      return;
251
    }
252
253
    // Frames displayed before this internal ARF.
254
    set_multi_layer_params_for_fp(
255
        twopass, twopass_frame, gf_group, p_rc, rc, frame_info, start, m,
256
        cur_frame_idx, frame_ind, parallel_frame_count, max_parallel_frames,
257
        do_frame_parallel_encode, first_frame_index, depth_thr, cur_disp_idx,
258
        layer_depth + 1);
259
260
    // Overlay for internal ARF.
261
    set_params_for_intnl_overlay_frames(gf_group, cur_frame_idx, frame_ind,
262
                                        first_frame_index, cur_disp_idx,
263
                                        layer_depth);
264
265
    // Frames displayed after this internal ARF.
266
    set_multi_layer_params_for_fp(
267
        twopass, twopass_frame, gf_group, p_rc, rc, frame_info, m + 1, end,
268
        cur_frame_idx, frame_ind, parallel_frame_count, max_parallel_frames,
269
        do_frame_parallel_encode, first_frame_index, depth_thr, cur_disp_idx,
270
        layer_depth + 1);
271
  }
272
}
273
274
// Structure for bookkeeping start, end and display indices to configure
275
// INTNL_ARF_UPDATE frames.
276
typedef struct {
277
  int start;
278
  int end;
279
  int display_index;
280
} FRAME_REORDER_INFO;
281
282
// Updates the stats required to configure the GF_GROUP.
283
static AOM_INLINE void fill_arf_frame_stats(FRAME_REORDER_INFO *arf_frame_stats,
284
                                            int arf_frame_index,
285
                                            int display_idx, int start,
286
                                            int end) {
287
  arf_frame_stats[arf_frame_index].start = start;
288
  arf_frame_stats[arf_frame_index].end = end;
289
  arf_frame_stats[arf_frame_index].display_index = display_idx;
290
}
291
292
// Sets GF_GROUP params for INTNL_ARF_UPDATE frames. Also populates
293
// doh_gf_index_map and arf_frame_stats.
294
static AOM_INLINE void set_params_for_internal_arfs_in_gf14(
295
    GF_GROUP *const gf_group, FRAME_REORDER_INFO *arf_frame_stats,
296
    int *cur_frame_idx, int *cur_disp_idx, int *frame_ind,
297
    int *count_arf_frames, int *doh_gf_index_map, int start, int end,
298
    int layer_depth, int layer_with_parallel_encodes) {
299
  int index = (start + end - 1) / 2;
300
  gf_group->update_type[*frame_ind] = INTNL_ARF_UPDATE;
301
  gf_group->arf_src_offset[*frame_ind] = index - 1;
302
  gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
303
  gf_group->layer_depth[*frame_ind] = layer_depth;
304
  gf_group->frame_type[*frame_ind] = INTER_FRAME;
305
  gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
306
  gf_group->display_idx[*frame_ind] =
307
      (*cur_disp_idx) + gf_group->arf_src_offset[*frame_ind];
308
309
  // Update the display index of the current frame with its gf index.
310
  doh_gf_index_map[index] = *frame_ind;
311
  if (layer_with_parallel_encodes) {
312
    assert(layer_depth == 4);
313
    // Set frame_parallel_level of the first frame in the given layer depth
314
    // to 1.
315
    if (gf_group->layer_depth[(*frame_ind) - 1] != layer_depth) {
316
      gf_group->frame_parallel_level[*frame_ind] = 1;
317
    } else {
318
      // Set frame_parallel_level of the consecutive frame in the same given
319
      // layer depth to 2.
320
      assert(gf_group->frame_parallel_level[(*frame_ind) - 1] == 1);
321
      gf_group->frame_parallel_level[*frame_ind] = 2;
322
      // Set the display_idx of frame_parallel_level 1 frame in
323
      // gf_group->skip_frame_as_ref.
324
      gf_group->skip_frame_as_ref[*frame_ind] =
325
          gf_group->display_idx[(*frame_ind) - 1];
326
    }
327
  }
328
  ++(*frame_ind);
329
330
  // Update arf_frame_stats.
331
  fill_arf_frame_stats(arf_frame_stats, *count_arf_frames, index, start, end);
332
  ++(*count_arf_frames);
333
}
334
335
// Sets GF_GROUP params for all INTNL_ARF_UPDATE frames in the given layer
336
// dpeth.
337
static AOM_INLINE void set_params_for_cur_layer_frames(
338
    GF_GROUP *const gf_group, FRAME_REORDER_INFO *arf_frame_stats,
339
    int *cur_frame_idx, int *cur_disp_idx, int *frame_ind,
340
    int *count_arf_frames, int *doh_gf_index_map, int num_dir, int node_start,
341
    int node_end, int layer_depth) {
342
  assert(num_dir < 3);
343
  int start, end;
344
  // Iterate through the nodes in the previous layer depth.
345
  for (int i = node_start; i < node_end; i++) {
346
    // For each node, check if a frame can be coded as INTNL_ARF_UPDATE frame on
347
    // either direction.
348
    for (int dir = 0; dir < num_dir; dir++) {
349
      // Checks for a frame to the left of current node.
350
      if (dir == 0) {
351
        start = arf_frame_stats[i].start;
352
        end = arf_frame_stats[i].display_index;
353
      } else {
354
        // Checks for a frame to the right of current node.
355
        start = arf_frame_stats[i].display_index + 1;
356
        end = arf_frame_stats[i].end;
357
      }
358
      const int num_frames_to_process = end - start;
359
      // Checks if a frame can be coded as INTNL_ARF_UPDATE frame. If
360
      // num_frames_to_process is less than 3, then there are not enough frames
361
      // between 'start' and 'end' to create another level.
362
      if (num_frames_to_process >= 3) {
363
        // Flag to indicate the lower layer depths for which parallel encoding
364
        // is enabled. Currently enabled for layer 4 frames.
365
        int layer_with_parallel_encodes = layer_depth == 4;
366
        set_params_for_internal_arfs_in_gf14(
367
            gf_group, arf_frame_stats, cur_frame_idx, cur_disp_idx, frame_ind,
368
            count_arf_frames, doh_gf_index_map, start, end, layer_depth,
369
            layer_with_parallel_encodes);
370
      }
371
    }
372
  }
373
}
374
375
// Configures multi-layers of the GF_GROUP when consecutive encode of frames in
376
// the same layer depth is enbaled.
377
static AOM_INLINE void set_multi_layer_params_for_gf14(
378
    const TWO_PASS *twopass, const TWO_PASS_FRAME *twopass_frame,
379
    const PRIMARY_RATE_CONTROL *p_rc, FRAME_INFO *frame_info,
380
    GF_GROUP *const gf_group, FRAME_REORDER_INFO *arf_frame_stats,
381
    int *cur_frame_idx, int *frame_ind, int *count_arf_frames,
382
    int *doh_gf_index_map, int *parallel_frame_count, int *first_frame_index,
383
    int *cur_disp_index, int gf_interval, int layer_depth,
384
    int max_parallel_frames) {
385
  assert(layer_depth == 2);
386
  assert(gf_group->max_layer_depth_allowed >= 4);
387
  int layer, node_start, node_end = 0;
388
  // Maximum layer depth excluding LF_UPDATE frames is 4 since applicable only
389
  // for gf-interval 14.
390
  const int max_layer_depth = 4;
391
  // Iterate through each layer depth starting from 2 till 'max_layer_depth'.
392
  for (layer = layer_depth; layer <= max_layer_depth; layer++) {
393
    // 'node_start' and 'node_end' indicate the number of nodes from the
394
    // previous layer depth to be considered. It also corresponds to the indices
395
    // of arf_frame_stats.
396
    node_start = node_end;
397
    node_end = (*count_arf_frames);
398
    // 'num_dir' indicates the number of directions to traverse w.r.t. a given
399
    // node in order to choose an INTNL_ARF_UPDATE frame. Layer depth 2 would
400
    // have only one frame and hence needs to traverse only in the left
401
    // direction w.r.t the node in the previous layer.
402
    int num_dir = layer == 2 ? 1 : 2;
403
    set_params_for_cur_layer_frames(gf_group, arf_frame_stats, cur_frame_idx,
404
                                    cur_disp_index, frame_ind, count_arf_frames,
405
                                    doh_gf_index_map, num_dir, node_start,
406
                                    node_end, layer);
407
  }
408
409
  for (int i = 1; i < gf_interval; i++) {
410
    // Since doh_gf_index_map is already populated for all INTNL_ARF_UPDATE
411
    // frames in the GF_GROUP, any frame with INVALID_IDX would correspond to an
412
    // LF_UPDATE frame.
413
    if (doh_gf_index_map[i] == INVALID_IDX) {
414
      // LF_UPDATE frames.
415
      // TODO(Remya): Correct start and end parameters passed to
416
      // set_params_for_leaf_frames() once encode reordering for gf-interval 14
417
      // is enbaled for parallel encode of lower layer frames.
418
      set_params_for_leaf_frames(
419
          twopass, twopass_frame, p_rc, frame_info, gf_group, cur_frame_idx,
420
          frame_ind, parallel_frame_count, max_parallel_frames, 1,
421
          first_frame_index, cur_disp_index, layer, 0, 0);
422
    } else {
423
      // In order to obtain the layer depths of INTNL_OVERLAY_UPDATE frames, get
424
      // the gf index of corresponding INTNL_ARF_UPDATE frames.
425
      int intnl_arf_index = doh_gf_index_map[i];
426
      int ld = gf_group->layer_depth[intnl_arf_index];
427
      set_params_for_intnl_overlay_frames(gf_group, cur_frame_idx, frame_ind,
428
                                          first_frame_index, cur_disp_index,
429
                                          ld);
430
    }
431
  }
432
}
433
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
434
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
435
436
// Set parameters for frames between 'start' and 'end' (excluding both).
437
static void set_multi_layer_params(
438
    const TWO_PASS *twopass, const TWO_PASS_FRAME *twopass_frame,
439
    GF_GROUP *const gf_group, const PRIMARY_RATE_CONTROL *p_rc,
440
    RATE_CONTROL *rc, FRAME_INFO *frame_info, int start, int end,
441
    int *cur_frame_idx, int *frame_ind,
442
#if CONFIG_FRAME_PARALLEL_ENCODE
443
    int *parallel_frame_count, int max_parallel_frames,
444
    int do_frame_parallel_encode, int *first_frame_index,
445
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
446
0
    int layer_depth) {
447
0
  const int num_frames_to_process = end - start;
448
449
  // Either we are at the last level of the pyramid, or we don't have enough
450
  // frames between 'l' and 'r' to create one more level.
451
0
  if (layer_depth > gf_group->max_layer_depth_allowed ||
452
0
      num_frames_to_process < 3) {
453
    // Leaf nodes.
454
0
    while (start < end) {
455
0
      gf_group->update_type[*frame_ind] = LF_UPDATE;
456
0
      gf_group->arf_src_offset[*frame_ind] = 0;
457
0
      gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
458
0
      gf_group->layer_depth[*frame_ind] = MAX_ARF_LAYERS;
459
0
      gf_group->arf_boost[*frame_ind] =
460
0
          av1_calc_arf_boost(twopass, twopass_frame, p_rc, frame_info, start,
461
0
                             end - start, 0, NULL, NULL, 0);
462
0
      gf_group->frame_type[*frame_ind] = INTER_FRAME;
463
0
      gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
464
0
      gf_group->max_layer_depth =
465
0
          AOMMAX(gf_group->max_layer_depth, layer_depth);
466
#if CONFIG_FRAME_PARALLEL_ENCODE
467
      // Set the level of parallelism for the LF_UPDATE frame.
468
      if (do_frame_parallel_encode) {
469
        set_frame_parallel_level(&gf_group->frame_parallel_level[*frame_ind],
470
                                 parallel_frame_count, max_parallel_frames);
471
        // Set LF_UPDATE frames as non-reference frames.
472
        gf_group->is_frame_non_ref[*frame_ind] = 1;
473
      }
474
      set_src_offset(gf_group, first_frame_index, *cur_frame_idx, *frame_ind);
475
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
476
0
      ++(*frame_ind);
477
0
      ++(*cur_frame_idx);
478
0
      ++start;
479
0
    }
480
0
  } else {
481
0
    const int m = (start + end - 1) / 2;
482
483
    // Internal ARF.
484
0
    gf_group->update_type[*frame_ind] = INTNL_ARF_UPDATE;
485
0
    gf_group->arf_src_offset[*frame_ind] = m - start;
486
0
    gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
487
0
    gf_group->layer_depth[*frame_ind] = layer_depth;
488
0
    gf_group->frame_type[*frame_ind] = INTER_FRAME;
489
0
    gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
490
491
#if CONFIG_FRAME_PARALLEL_ENCODE
492
    if (do_frame_parallel_encode) {
493
      // If max_parallel_frames is not exceeded and if the frame will not be
494
      // temporally filtered, encode the next internal ARF frame in parallel.
495
      if (*parallel_frame_count > 1 &&
496
          *parallel_frame_count <= max_parallel_frames) {
497
        if (gf_group->arf_src_offset[*frame_ind] < TF_LOOKAHEAD_IDX_THR)
498
          gf_group->frame_parallel_level[*frame_ind] = 2;
499
        *parallel_frame_count = 1;
500
      }
501
    }
502
    set_src_offset(gf_group, first_frame_index, *cur_frame_idx, *frame_ind);
503
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
504
505
    // Get the boost factor for intermediate ARF frames.
506
0
    gf_group->arf_boost[*frame_ind] =
507
0
        av1_calc_arf_boost(twopass, twopass_frame, p_rc, frame_info, m, end - m,
508
0
                           m - start, NULL, NULL, 0);
509
0
    ++(*frame_ind);
510
511
    // Frames displayed before this internal ARF.
512
0
    set_multi_layer_params(twopass, twopass_frame, gf_group, p_rc, rc,
513
0
                           frame_info, start, m, cur_frame_idx, frame_ind,
514
#if CONFIG_FRAME_PARALLEL_ENCODE
515
                           parallel_frame_count, max_parallel_frames,
516
                           do_frame_parallel_encode, first_frame_index,
517
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
518
0
                           layer_depth + 1);
519
520
    // Overlay for internal ARF.
521
0
    gf_group->update_type[*frame_ind] = INTNL_OVERLAY_UPDATE;
522
0
    gf_group->arf_src_offset[*frame_ind] = 0;
523
0
    gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
524
0
    gf_group->arf_boost[*frame_ind] = 0;
525
0
    gf_group->layer_depth[*frame_ind] = layer_depth;
526
0
    gf_group->frame_type[*frame_ind] = INTER_FRAME;
527
0
    gf_group->refbuf_state[*frame_ind] = REFBUF_UPDATE;
528
529
#if CONFIG_FRAME_PARALLEL_ENCODE
530
    set_src_offset(gf_group, first_frame_index, *cur_frame_idx, *frame_ind);
531
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
532
0
    ++(*frame_ind);
533
0
    ++(*cur_frame_idx);
534
535
    // Frames displayed after this internal ARF.
536
0
    set_multi_layer_params(twopass, twopass_frame, gf_group, p_rc, rc,
537
0
                           frame_info, m + 1, end, cur_frame_idx, frame_ind,
538
#if CONFIG_FRAME_PARALLEL_ENCODE
539
                           parallel_frame_count, max_parallel_frames,
540
                           do_frame_parallel_encode, first_frame_index,
541
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
542
0
                           layer_depth + 1);
543
0
  }
544
0
}
545
546
static int construct_multi_layer_gf_structure(
547
    AV1_COMP *cpi, TWO_PASS *twopass, GF_GROUP *const gf_group,
548
    RATE_CONTROL *rc, FRAME_INFO *const frame_info, int baseline_gf_interval,
549
0
    FRAME_UPDATE_TYPE first_frame_update_type) {
550
0
  PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
551
  // TODO(angiebird): Why do we need "-1" here?
552
0
  const int gf_interval = baseline_gf_interval - 1;
553
0
  int frame_index = 0;
554
0
  int cur_frame_index = 0;
555
556
#if CONFIG_FRAME_PARALLEL_ENCODE
557
#if CONFIG_FRAME_PARALLEL_ENCODE_2
558
  // Set the display order hint for the first frame in the GF_GROUP.
559
  int cur_disp_index = (first_frame_update_type == KF_UPDATE)
560
                           ? 0
561
                           : cpi->common.current_frame.frame_number;
562
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
563
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
564
565
#if CONFIG_FRAME_PARALLEL_ENCODE
566
  // Initialize gf_group->frame_parallel_level and gf_group->is_frame_non_ref to
567
  // 0.
568
  memset(
569
      gf_group->frame_parallel_level, 0,
570
      sizeof(gf_group->frame_parallel_level[0]) * MAX_STATIC_GF_GROUP_LENGTH);
571
  memset(gf_group->is_frame_non_ref, 0,
572
         sizeof(gf_group->is_frame_non_ref[0]) * MAX_STATIC_GF_GROUP_LENGTH);
573
  memset(gf_group->src_offset, 0,
574
         sizeof(gf_group->src_offset[0]) * MAX_STATIC_GF_GROUP_LENGTH);
575
#if CONFIG_FRAME_PARALLEL_ENCODE_2
576
  // Initialize gf_group->skip_frame_refresh and gf_group->skip_frame_as_ref
577
  // with INVALID_IDX.
578
  memset(gf_group->skip_frame_refresh, INVALID_IDX,
579
         sizeof(gf_group->skip_frame_refresh[0][0]) *
580
             MAX_STATIC_GF_GROUP_LENGTH * REF_FRAMES);
581
  memset(gf_group->skip_frame_as_ref, INVALID_IDX,
582
         sizeof(gf_group->skip_frame_as_ref[0]) * MAX_STATIC_GF_GROUP_LENGTH);
583
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
584
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
585
586
0
  int kf_decomp = cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1;
587
  // This is a patch that fixes https://crbug.com/aomedia/3163
588
  // enable_keyframe_filtering > 1 will introduce an extra overlay frame at
589
  // key frame location. However when
590
  // baseline_gf_interval == MAX_STATIC_GF_GROUP_LENGTH, we can't
591
  // afford to have an extra overlay frame. Otherwise, the gf_group->size will
592
  // become MAX_STATIC_GF_GROUP_LENGTH + 1, which causes memory error.
593
  // A cheap solution is to turn of kf_decomp here.
594
  // TODO(angiebird): Find a systematic way to solve this issue.
595
0
  if (baseline_gf_interval == MAX_STATIC_GF_GROUP_LENGTH) {
596
0
    kf_decomp = 0;
597
0
  }
598
0
  if (first_frame_update_type == KF_UPDATE) {
599
0
    gf_group->update_type[frame_index] = kf_decomp ? ARF_UPDATE : KF_UPDATE;
600
0
    gf_group->arf_src_offset[frame_index] = 0;
601
0
    gf_group->cur_frame_idx[frame_index] = cur_frame_index;
602
0
    gf_group->layer_depth[frame_index] = 0;
603
0
    gf_group->frame_type[frame_index] = KEY_FRAME;
604
0
    gf_group->refbuf_state[frame_index] = REFBUF_RESET;
605
0
    gf_group->max_layer_depth = 0;
606
#if CONFIG_FRAME_PARALLEL_ENCODE
607
#if CONFIG_FRAME_PARALLEL_ENCODE_2
608
    gf_group->display_idx[frame_index] = cur_disp_index;
609
    if (!kf_decomp) cur_disp_index++;
610
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
611
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
612
0
    ++frame_index;
613
614
0
    if (kf_decomp) {
615
0
      gf_group->update_type[frame_index] = OVERLAY_UPDATE;
616
0
      gf_group->arf_src_offset[frame_index] = 0;
617
0
      gf_group->cur_frame_idx[frame_index] = cur_frame_index;
618
0
      gf_group->layer_depth[frame_index] = 0;
619
0
      gf_group->frame_type[frame_index] = INTER_FRAME;
620
0
      gf_group->refbuf_state[frame_index] = REFBUF_UPDATE;
621
0
      gf_group->max_layer_depth = 0;
622
#if CONFIG_FRAME_PARALLEL_ENCODE
623
#if CONFIG_FRAME_PARALLEL_ENCODE_2
624
      gf_group->display_idx[frame_index] = cur_disp_index;
625
      cur_disp_index++;
626
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
627
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
628
0
      ++frame_index;
629
0
    }
630
0
    cur_frame_index++;
631
0
  }
632
633
0
  if (first_frame_update_type == GF_UPDATE) {
634
0
    gf_group->update_type[frame_index] = GF_UPDATE;
635
0
    gf_group->arf_src_offset[frame_index] = 0;
636
0
    gf_group->cur_frame_idx[frame_index] = cur_frame_index;
637
0
    gf_group->layer_depth[frame_index] = 0;
638
0
    gf_group->frame_type[frame_index] = INTER_FRAME;
639
0
    gf_group->refbuf_state[frame_index] = REFBUF_UPDATE;
640
0
    gf_group->max_layer_depth = 0;
641
#if CONFIG_FRAME_PARALLEL_ENCODE
642
#if CONFIG_FRAME_PARALLEL_ENCODE_2
643
    gf_group->display_idx[frame_index] = cur_disp_index;
644
    cur_disp_index++;
645
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
646
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
647
0
    ++frame_index;
648
0
    ++cur_frame_index;
649
0
  }
650
651
  // ALTREF.
652
0
  const int use_altref = gf_group->max_layer_depth_allowed > 0;
653
0
  int is_fwd_kf = rc->frames_to_fwd_kf == gf_interval;
654
655
0
  if (use_altref) {
656
0
    gf_group->update_type[frame_index] = ARF_UPDATE;
657
0
    gf_group->arf_src_offset[frame_index] = gf_interval - cur_frame_index;
658
0
    gf_group->cur_frame_idx[frame_index] = cur_frame_index;
659
0
    gf_group->layer_depth[frame_index] = 1;
660
0
    gf_group->arf_boost[frame_index] = cpi->ppi->p_rc.gfu_boost;
661
0
    gf_group->frame_type[frame_index] = is_fwd_kf ? KEY_FRAME : INTER_FRAME;
662
0
    gf_group->refbuf_state[frame_index] = REFBUF_UPDATE;
663
0
    gf_group->max_layer_depth = 1;
664
0
    gf_group->arf_index = frame_index;
665
#if CONFIG_FRAME_PARALLEL_ENCODE
666
#if CONFIG_FRAME_PARALLEL_ENCODE_2
667
    gf_group->display_idx[frame_index] =
668
        cur_disp_index + gf_group->arf_src_offset[frame_index];
669
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
670
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
671
0
    ++frame_index;
672
0
  } else {
673
0
    gf_group->arf_index = -1;
674
0
  }
675
676
  // Flag to indicate if multi-layer configuration is complete.
677
0
  int is_multi_layer_configured = 0;
678
679
#if CONFIG_FRAME_PARALLEL_ENCODE
680
  // Running count of no. of frames that is part of a given parallel
681
  // encode set in a gf_group. Value of 1 indicates no parallel encode.
682
  int parallel_frame_count = 1;
683
  // Enable parallel encode of frames if gf_group has a multi-layer pyramid
684
  // structure with minimum 4 layers.
685
  int do_frame_parallel_encode = (cpi->ppi->num_fp_contexts > 1 && use_altref &&
686
                                  gf_group->max_layer_depth_allowed >= 4);
687
688
  int first_frame_index = cur_frame_index;
689
#if CONFIG_FRAME_PARALLEL_ENCODE_2
690
  if (do_frame_parallel_encode) {
691
    // construct_multi_layer_gf_structure() takes the input parameter
692
    // 'gf_interval' as p_rc->baseline_gf_interval - 1 . Below code computes the
693
    // actual GF_GROUP length by compensating for this offset.
694
    int actual_gf_length = ((first_frame_update_type == KF_UPDATE) ||
695
                            (first_frame_update_type == GF_UPDATE))
696
                               ? gf_interval
697
                               : gf_interval + 1;
698
699
    // In order to facilitate parallel encoding of frames in lower layer depths,
700
    // encode reordering is done. Currently encode reordering is enabled only
701
    // for gf-intervals 16 and 32. NOTE: Since the buffer holding the
702
    // reference frames is of size 8 (ref_frame_map[REF_FRAMES]), there is a
703
    // limitation on the number of hidden frames possible at any given point and
704
    // hence the reordering is enabled only for gf-intervals 16 and 32.
705
    // Disabling encode reordering for gf-interval 14 since some cross-frame
706
    // dependencies related to temporal filtering for FPMT is currently not
707
    // handled.
708
    int disable_gf14_reorder = 1;
709
    if (actual_gf_length == 14 && !disable_gf14_reorder) {
710
      // This array holds the gf index of INTNL_ARF_UPDATE frames in the slot
711
      // corresponding to their display order hint. This is used while
712
      // configuring the LF_UPDATE frames and INTNL_OVERLAY_UPDATE frames.
713
      int doh_gf_index_map[FIXED_GF_INTERVAL];
714
      // Initialize doh_gf_index_map with INVALID_IDX.
715
      memset(&doh_gf_index_map[0], INVALID_IDX,
716
             (sizeof(doh_gf_index_map[0]) * FIXED_GF_INTERVAL));
717
718
      FRAME_REORDER_INFO arf_frame_stats[REF_FRAMES - 1];
719
      // Store the stats corresponding to layer 1 frame.
720
      fill_arf_frame_stats(arf_frame_stats, 0, actual_gf_length, 1,
721
                           actual_gf_length);
722
      int count_arf_frames = 1;
723
724
      // Sets multi-layer params for gf-interval 14 to consecutively encode
725
      // frames in the same layer depth, i.e., encode order would be 0-> 14->
726
      // 7-> 3-> 10-> 5-> 12-> 1-> 2-> 4-> 6-> 8-> 9-> 11-> 13.
727
      // TODO(Remya): Set GF_GROUP param 'arf_boost' for all frames.
728
      set_multi_layer_params_for_gf14(
729
          twopass, &cpi->twopass_frame, p_rc, frame_info, gf_group,
730
          arf_frame_stats, &cur_frame_index, &frame_index, &count_arf_frames,
731
          doh_gf_index_map, &parallel_frame_count, &first_frame_index,
732
          &cur_disp_index, actual_gf_length, use_altref + 1,
733
          cpi->ppi->num_fp_contexts);
734
735
      // Set gf_group->skip_frame_refresh.
736
      for (int i = 0; i < actual_gf_length; i++) {
737
        int count = 0;
738
        if (gf_group->update_type[i] == INTNL_ARF_UPDATE) {
739
          for (int j = 0; j < i; j++) {
740
            // Store the display order hint of the frames which would not
741
            // have been displayed at the encode call of frame 'i'.
742
            if ((gf_group->display_idx[j] < gf_group->display_idx[i]) &&
743
                gf_group->update_type[j] == INTNL_ARF_UPDATE) {
744
              gf_group->skip_frame_refresh[i][count++] =
745
                  gf_group->display_idx[j];
746
            }
747
          }
748
        }
749
      }
750
    } else {
751
      // Set layer depth threshold for reordering as per the gf length.
752
      int depth_thr =
753
          (actual_gf_length == 16) ? 3 : (actual_gf_length == 32) ? 4 : INT_MAX;
754
755
      set_multi_layer_params_for_fp(
756
          twopass, &cpi->twopass_frame, gf_group, p_rc, rc, frame_info,
757
          cur_frame_index, gf_interval, &cur_frame_index, &frame_index,
758
          &parallel_frame_count, cpi->ppi->num_fp_contexts,
759
          do_frame_parallel_encode, &first_frame_index, depth_thr,
760
          &cur_disp_index, use_altref + 1);
761
    }
762
    is_multi_layer_configured = 1;
763
  }
764
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
765
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
766
767
  // Rest of the frames.
768
0
  if (!is_multi_layer_configured)
769
0
    set_multi_layer_params(twopass, &cpi->twopass_frame, gf_group, p_rc, rc,
770
0
                           frame_info, cur_frame_index, gf_interval,
771
0
                           &cur_frame_index, &frame_index,
772
#if CONFIG_FRAME_PARALLEL_ENCODE
773
                           &parallel_frame_count, cpi->ppi->num_fp_contexts,
774
                           do_frame_parallel_encode, &first_frame_index,
775
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
776
0
                           use_altref + 1);
777
778
0
  if (use_altref) {
779
0
    gf_group->update_type[frame_index] = OVERLAY_UPDATE;
780
0
    gf_group->arf_src_offset[frame_index] = 0;
781
0
    gf_group->cur_frame_idx[frame_index] = cur_frame_index;
782
0
    gf_group->layer_depth[frame_index] = MAX_ARF_LAYERS;
783
0
    gf_group->arf_boost[frame_index] = NORMAL_BOOST;
784
0
    gf_group->frame_type[frame_index] = INTER_FRAME;
785
0
    gf_group->refbuf_state[frame_index] =
786
0
        is_fwd_kf ? REFBUF_RESET : REFBUF_UPDATE;
787
#if CONFIG_FRAME_PARALLEL_ENCODE
788
#if CONFIG_FRAME_PARALLEL_ENCODE_2
789
    gf_group->display_idx[frame_index] = cur_disp_index;
790
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
791
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
792
0
    ++frame_index;
793
0
  } else {
794
0
    for (; cur_frame_index <= gf_interval; ++cur_frame_index) {
795
0
      gf_group->update_type[frame_index] = LF_UPDATE;
796
0
      gf_group->arf_src_offset[frame_index] = 0;
797
0
      gf_group->cur_frame_idx[frame_index] = cur_frame_index;
798
0
      gf_group->layer_depth[frame_index] = MAX_ARF_LAYERS;
799
0
      gf_group->arf_boost[frame_index] = NORMAL_BOOST;
800
0
      gf_group->frame_type[frame_index] = INTER_FRAME;
801
0
      gf_group->refbuf_state[frame_index] = REFBUF_UPDATE;
802
0
      gf_group->max_layer_depth = AOMMAX(gf_group->max_layer_depth, 2);
803
#if CONFIG_FRAME_PARALLEL_ENCODE
804
      set_src_offset(gf_group, &first_frame_index, cur_frame_index,
805
                     frame_index);
806
#if CONFIG_FRAME_PARALLEL_ENCODE_2
807
      gf_group->display_idx[frame_index] = cur_disp_index;
808
      cur_disp_index++;
809
#endif  // CONFIG_FRAME_PARALLEL_ENCODE_2
810
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
811
0
      ++frame_index;
812
0
    }
813
0
  }
814
#if CONFIG_FRAME_PARALLEL_ENCODE
815
  if (do_frame_parallel_encode) {
816
    // Iterate through the gf_group and reset frame_parallel_level to 0 in case
817
    // a frame is marked as frame_parallel_level 1 with no subsequent
818
    // frame_parallel_level 2 frame(s).
819
    int level1_frame_idx = INT_MAX;
820
    int level2_frame_count = 0;
821
    for (int frame_idx = 0; frame_idx < frame_index; frame_idx++) {
822
      if (gf_group->frame_parallel_level[frame_idx] == 1) {
823
        // Set frame_parallel_level to 0 if only one frame is present in a
824
        // parallel encode set.
825
        if (level1_frame_idx != INT_MAX && !level2_frame_count)
826
          gf_group->frame_parallel_level[level1_frame_idx] = 0;
827
        // Book-keep frame_idx of frame_parallel_level 1 frame and reset the
828
        // count of frame_parallel_level 2 frames in the corresponding parallel
829
        // encode set.
830
        level1_frame_idx = frame_idx;
831
        level2_frame_count = 0;
832
      }
833
      if (gf_group->frame_parallel_level[frame_idx] == 2) level2_frame_count++;
834
    }
835
    // If frame_parallel_level is set to 1 for the last LF_UPDATE
836
    // frame in the gf_group, reset it to zero since there are no subsequent
837
    // frames in the gf_group.
838
    if (gf_group->frame_parallel_level[frame_index - 2] == 1) {
839
      assert(gf_group->update_type[frame_index - 2] == LF_UPDATE);
840
      gf_group->frame_parallel_level[frame_index - 2] = 0;
841
    }
842
  }
843
#endif
844
845
0
  for (int gf_idx = frame_index; gf_idx < MAX_STATIC_GF_GROUP_LENGTH;
846
0
       ++gf_idx) {
847
0
    gf_group->update_type[gf_idx] = LF_UPDATE;
848
0
    gf_group->arf_src_offset[gf_idx] = 0;
849
0
    gf_group->cur_frame_idx[gf_idx] = gf_idx;
850
0
    gf_group->layer_depth[gf_idx] = MAX_ARF_LAYERS;
851
0
    gf_group->arf_boost[gf_idx] = NORMAL_BOOST;
852
0
    gf_group->frame_type[gf_idx] = INTER_FRAME;
853
0
    gf_group->refbuf_state[gf_idx] = REFBUF_UPDATE;
854
0
    gf_group->max_layer_depth = AOMMAX(gf_group->max_layer_depth, 2);
855
0
  }
856
857
0
  return frame_index;
858
0
}
859
860
0
static void set_ld_layer_depth(GF_GROUP *gf_group, int gop_length) {
861
0
  int log_gop_length = 0;
862
0
  while ((1 << log_gop_length) < gop_length) {
863
0
    ++log_gop_length;
864
0
  }
865
866
0
  for (int gf_index = 0; gf_index < gf_group->size; ++gf_index) {
867
0
    int count = 0;
868
    // Find the trailing zeros
869
0
    for (; count < MAX_ARF_LAYERS; ++count) {
870
0
      if ((gf_index >> count) & 0x01) break;
871
0
    }
872
0
    gf_group->layer_depth[gf_index] = AOMMAX(log_gop_length - count, 0);
873
0
  }
874
0
  gf_group->max_layer_depth = log_gop_length;
875
0
}
876
877
0
void av1_gop_setup_structure(AV1_COMP *cpi) {
878
0
  RATE_CONTROL *const rc = &cpi->rc;
879
0
  PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc;
880
0
  GF_GROUP *const gf_group = &cpi->ppi->gf_group;
881
0
  TWO_PASS *const twopass = &cpi->ppi->twopass;
882
0
  FRAME_INFO *const frame_info = &cpi->frame_info;
883
0
  const int key_frame = rc->frames_since_key == 0;
884
0
  FRAME_UPDATE_TYPE first_frame_update_type = ARF_UPDATE;
885
886
0
  if (key_frame)
887
0
    first_frame_update_type = KF_UPDATE;
888
0
  else if (!cpi->ppi->gf_state.arf_gf_boost_lst)
889
0
    first_frame_update_type = GF_UPDATE;
890
891
0
  gf_group->size = construct_multi_layer_gf_structure(
892
0
      cpi, twopass, gf_group, rc, frame_info, p_rc->baseline_gf_interval,
893
0
      first_frame_update_type);
894
895
0
  if (gf_group->max_layer_depth_allowed == 0)
896
0
    set_ld_layer_depth(gf_group, p_rc->baseline_gf_interval);
897
0
}
898
899
int av1_gop_check_forward_keyframe(const GF_GROUP *gf_group,
900
0
                                   int gf_frame_index) {
901
0
  return gf_group->frame_type[gf_frame_index] == KEY_FRAME &&
902
0
         gf_group->refbuf_state[gf_frame_index] == REFBUF_UPDATE;
903
0
}
904
905
0
int av1_gop_is_second_arf(const GF_GROUP *gf_group, int gf_frame_index) {
906
0
  const int arf_src_offset = gf_group->arf_src_offset[gf_frame_index];
907
  // TODO(angiebird): when gf_group->size == 32, it's possble to
908
  // have "two" second arf. Check if this is acceptable.
909
0
  if (gf_group->update_type[gf_frame_index] == INTNL_ARF_UPDATE &&
910
0
      arf_src_offset >= TF_LOOKAHEAD_IDX_THR) {
911
0
    return 1;
912
0
  }
913
0
  return 0;
914
0
}