aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/video_coding/codecs/vp8/realtime_temporal_layers.cc
blob: d22601358f290a663e6d1445d5d2c5efef0a6286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
*  Use of this source code is governed by a BSD-style license
*  that can be found in the LICENSE file in the root of the source
*  tree. An additional intellectual property rights grant can be found
*  in the file PATENTS.  All contributing project authors may
*  be found in the AUTHORS file in the root of the source tree.
*/

#include <stdlib.h>
#include <algorithm>

#include "vpx/vpx_encoder.h"
#include "vpx/vp8cx.h"
#include "webrtc/modules/video_coding/include/video_codec_interface.h"
#include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
#include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"

// This file implements logic to adapt the number of temporal layers based on
// input frame rate in order to avoid having the base layer being relaying at
// a below acceptable framerate.
namespace webrtc {
namespace {
enum {
  kTemporalUpdateLast = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
                        VP8_EFLAG_NO_REF_GF |
                        VP8_EFLAG_NO_REF_ARF,

  kTemporalUpdateGolden =
      VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST,

  kTemporalUpdateGoldenWithoutDependency =
      kTemporalUpdateGolden | VP8_EFLAG_NO_REF_GF,

  kTemporalUpdateAltref = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_LAST,

  kTemporalUpdateAltrefWithoutDependency =
      kTemporalUpdateAltref | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF,

  kTemporalUpdateNone = VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
                        VP8_EFLAG_NO_UPD_LAST |
                        VP8_EFLAG_NO_UPD_ENTROPY,

  kTemporalUpdateNoneNoRefAltref = kTemporalUpdateNone | VP8_EFLAG_NO_REF_ARF,

  kTemporalUpdateNoneNoRefGoldenRefAltRef =
      VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF |
      VP8_EFLAG_NO_UPD_LAST |
      VP8_EFLAG_NO_UPD_ENTROPY,

  kTemporalUpdateGoldenWithoutDependencyRefAltRef =
      VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST,

  kTemporalUpdateLastRefAltRef =
      VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF,

  kTemporalUpdateGoldenRefAltRef = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST,

  kTemporalUpdateLastAndGoldenRefAltRef =
      VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_REF_GF,

  kTemporalUpdateLastRefAll = VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF,
};

int CalculateNumberOfTemporalLayers(int current_temporal_layers,
                                    int input_fps) {
  if (input_fps >= 24) {
    return 3;
  }
  if (input_fps >= 20 && current_temporal_layers >= 3) {
    // Keep doing 3 temporal layers until we go below 20fps.
    return 3;
  }
  if (input_fps >= 10) {
    return 2;
  }
  if (input_fps > 8 && current_temporal_layers >= 2) {
    // keep doing 2 temporal layers until we go below 8fps
    return 2;
  }
  return 1;
}

class RealTimeTemporalLayers : public TemporalLayers {
 public:
  RealTimeTemporalLayers(int max_num_temporal_layers,
                         uint8_t initial_tl0_pic_idx)
      : temporal_layers_(1),
        max_temporal_layers_(max_num_temporal_layers),
        tl0_pic_idx_(initial_tl0_pic_idx),
        frame_counter_(static_cast<unsigned int>(-1)),
        timestamp_(0),
        last_base_layer_sync_(0),
        layer_ids_length_(0),
        layer_ids_(NULL),
        encode_flags_length_(0),
        encode_flags_(NULL) {
    assert(max_temporal_layers_ >= 1);
    assert(max_temporal_layers_ <= 3);
  }

  virtual ~RealTimeTemporalLayers() {}

  virtual bool ConfigureBitrates(int bitrate_kbit,
                                 int max_bitrate_kbit,
                                 int framerate,
                                 vpx_codec_enc_cfg_t* cfg) {
    temporal_layers_ =
        CalculateNumberOfTemporalLayers(temporal_layers_, framerate);
    temporal_layers_ = std::min(temporal_layers_, max_temporal_layers_);
    assert(temporal_layers_ >= 1 && temporal_layers_ <= 3);

    cfg->ts_number_layers = temporal_layers_;
    for (int tl = 0; tl < temporal_layers_; ++tl) {
      cfg->ts_target_bitrate[tl] =
          bitrate_kbit * kVp8LayerRateAlloction[temporal_layers_ - 1][tl];
    }

    switch (temporal_layers_) {
      case 1: {
        static const unsigned int layer_ids[] = {0u};
        layer_ids_ = layer_ids;
        layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids);

        static const int encode_flags[] = {kTemporalUpdateLastRefAll};
        encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids);
        encode_flags_ = encode_flags;

        cfg->ts_rate_decimator[0] = 1;
        cfg->ts_periodicity = layer_ids_length_;
      } break;

      case 2: {
        static const unsigned int layer_ids[] = {0u, 1u};
        layer_ids_ = layer_ids;
        layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids);

        static const int encode_flags[] = {
            kTemporalUpdateLastAndGoldenRefAltRef,
            kTemporalUpdateGoldenWithoutDependencyRefAltRef,
            kTemporalUpdateLastRefAltRef,
            kTemporalUpdateGoldenRefAltRef,
            kTemporalUpdateLastRefAltRef,
            kTemporalUpdateGoldenRefAltRef,
            kTemporalUpdateLastRefAltRef,
            kTemporalUpdateNone};
        encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids);
        encode_flags_ = encode_flags;

        cfg->ts_rate_decimator[0] = 2;
        cfg->ts_rate_decimator[1] = 1;
        cfg->ts_periodicity = layer_ids_length_;
      } break;

      case 3: {
        static const unsigned int layer_ids[] = {0u, 2u, 1u, 2u};
        layer_ids_ = layer_ids;
        layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids);

        static const int encode_flags[] = {
            kTemporalUpdateLastAndGoldenRefAltRef,
            kTemporalUpdateNoneNoRefGoldenRefAltRef,
            kTemporalUpdateGoldenWithoutDependencyRefAltRef,
            kTemporalUpdateNone,
            kTemporalUpdateLastRefAltRef,
            kTemporalUpdateNone,
            kTemporalUpdateGoldenRefAltRef,
            kTemporalUpdateNone};
        encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids);
        encode_flags_ = encode_flags;

        cfg->ts_rate_decimator[0] = 4;
        cfg->ts_rate_decimator[1] = 2;
        cfg->ts_rate_decimator[2] = 1;
        cfg->ts_periodicity = layer_ids_length_;
      } break;

      default:
        assert(false);
        return false;
    }
    memcpy(cfg->ts_layer_id, layer_ids_,
           sizeof(unsigned int) * layer_ids_length_);
    return true;
  }

  virtual int EncodeFlags(uint32_t timestamp) {
    frame_counter_++;
    return CurrentEncodeFlags();
  }

  int CurrentEncodeFlags() const {
    assert(encode_flags_length_ > 0 && encode_flags_ != NULL);
    int index = frame_counter_ % encode_flags_length_;
    assert(index >= 0 && index < encode_flags_length_);
    return encode_flags_[index];
  }

  virtual int CurrentLayerId() const {
    assert(layer_ids_length_ > 0 && layer_ids_ != NULL);
    int index = frame_counter_ % layer_ids_length_;
    assert(index >= 0 && index < layer_ids_length_);
    return layer_ids_[index];
  }

  virtual void PopulateCodecSpecific(bool base_layer_sync,
                                     CodecSpecificInfoVP8* vp8_info,
                                     uint32_t timestamp) {
    assert(temporal_layers_ > 0);

    if (temporal_layers_ == 1) {
      vp8_info->temporalIdx = kNoTemporalIdx;
      vp8_info->layerSync = false;
      vp8_info->tl0PicIdx = kNoTl0PicIdx;
    } else {
      if (base_layer_sync) {
        vp8_info->temporalIdx = 0;
        vp8_info->layerSync = true;
      } else {
        vp8_info->temporalIdx = CurrentLayerId();
        int temporal_reference = CurrentEncodeFlags();

        if (temporal_reference == kTemporalUpdateAltrefWithoutDependency ||
            temporal_reference == kTemporalUpdateGoldenWithoutDependency ||
            temporal_reference ==
                kTemporalUpdateGoldenWithoutDependencyRefAltRef ||
            temporal_reference == kTemporalUpdateNoneNoRefGoldenRefAltRef ||
            (temporal_reference == kTemporalUpdateNone &&
             temporal_layers_ == 4)) {
          vp8_info->layerSync = true;
        } else {
          vp8_info->layerSync = false;
        }
      }
      if (last_base_layer_sync_ && vp8_info->temporalIdx != 0) {
        // Regardless of pattern the frame after a base layer sync will always
        // be a layer sync.
        vp8_info->layerSync = true;
      }
      if (vp8_info->temporalIdx == 0 && timestamp != timestamp_) {
        timestamp_ = timestamp;
        tl0_pic_idx_++;
      }
      last_base_layer_sync_ = base_layer_sync;
      vp8_info->tl0PicIdx = tl0_pic_idx_;
    }
  }

  void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {}

  bool UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) override { return false; }

 private:
  int temporal_layers_;
  int max_temporal_layers_;

  int tl0_pic_idx_;
  unsigned int frame_counter_;
  uint32_t timestamp_;
  bool last_base_layer_sync_;

  // Pattern of temporal layer ids.
  int layer_ids_length_;
  const unsigned int* layer_ids_;

  // Pattern of encode flags.
  int encode_flags_length_;
  const int* encode_flags_;
};
}  // namespace

TemporalLayers* RealTimeTemporalLayersFactory::Create(
    int max_temporal_layers,
    uint8_t initial_tl0_pic_idx) const {
  return new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx);
}
}  // namespace webrtc