• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The libgav1 Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBGAV1_SRC_FRAME_SCRATCH_BUFFER_H_
18 #define LIBGAV1_SRC_FRAME_SCRATCH_BUFFER_H_
19 
20 #include <condition_variable>  // NOLINT (unapproved c++11 header)
21 #include <cstdint>
22 #include <memory>
23 #include <mutex>  // NOLINT (unapproved c++11 header)
24 
25 #include "src/loop_restoration_info.h"
26 #include "src/residual_buffer_pool.h"
27 #include "src/symbol_decoder_context.h"
28 #include "src/threading_strategy.h"
29 #include "src/tile_scratch_buffer.h"
30 #include "src/utils/array_2d.h"
31 #include "src/utils/block_parameters_holder.h"
32 #include "src/utils/compiler_attributes.h"
33 #include "src/utils/constants.h"
34 #include "src/utils/dynamic_buffer.h"
35 #include "src/utils/memory.h"
36 #include "src/utils/stack.h"
37 #include "src/utils/types.h"
38 #include "src/yuv_buffer.h"
39 
40 namespace libgav1 {
41 
42 // Buffer used to store the unfiltered pixels that are necessary for decoding
43 // the next superblock row (for the intra prediction process).
44 using IntraPredictionBuffer =
45     std::array<AlignedDynamicBuffer<uint8_t, kMaxAlignment>, kMaxPlanes>;
46 
47 // Buffer to facilitate decoding a frame. This struct is used only within
48 // DecoderImpl::DecodeTiles().
49 struct FrameScratchBuffer {
50   LoopRestorationInfo loop_restoration_info;
51   Array2D<int16_t> cdef_index;
52   Array2D<TransformSize> inter_transform_sizes;
53   BlockParametersHolder block_parameters_holder;
54   TemporalMotionField motion_field;
55   SymbolDecoderContext symbol_decoder_context;
56   std::unique_ptr<ResidualBufferPool> residual_buffer_pool;
57   // threaded_window_buffer will be subdivided by PostFilter into windows of
58   // width 512 pixels. Each row in the window is filtered by a worker thread.
59   // To avoid false sharing, each 512-pixel row processed by one thread should
60   // not share a cache line with a row processed by another thread. So we align
61   // threaded_window_buffer to the cache line size. In addition, it is faster to
62   // memcpy from an aligned buffer.
63   AlignedDynamicBuffer<uint8_t, kCacheLineSize> threaded_window_buffer;
64   // Buffer used to temporarily store the input row for applying SuperRes.
65   AlignedDynamicBuffer<uint8_t, 16> superres_line_buffer;
66   // Buffer used to store the deblocked pixels that are necessary for loop
67   // restoration. This buffer will store 4 rows for every 64x64 block (4 rows
68   // for every 32x32 for chroma with subsampling). The indices of the rows that
69   // are stored are specified in |kDeblockedRowsForLoopRestoration|.
70   YuvBuffer deblock_buffer;
71   // The size of this dynamic buffer is |tile_rows|.
72   DynamicBuffer<IntraPredictionBuffer> intra_prediction_buffers;
73   TileScratchBufferPool tile_scratch_buffer_pool;
74   ThreadingStrategy threading_strategy;
75   std::mutex superblock_row_mutex;
76   // The size of this buffer is the number of superblock rows.
77   // |superblock_row_progress[i]| is incremented whenever a tile finishes
78   // decoding superblock row at index i. If the count reaches tile_columns, then
79   // |superblock_row_progress_condvar[i]| is notified.
80   DynamicBuffer<int> superblock_row_progress
81       LIBGAV1_GUARDED_BY(superblock_row_mutex);
82   // The size of this buffer is the number of superblock rows. Used to wait for
83   // |superblock_row_progress[i]| to reach tile_columns.
84   DynamicBuffer<std::condition_variable> superblock_row_progress_condvar;
85   // Used to signal tile decoding failure in the combined multithreading mode.
86   bool tile_decoding_failed LIBGAV1_GUARDED_BY(superblock_row_mutex);
87 };
88 
89 class FrameScratchBufferPool {
90  public:
Get()91   std::unique_ptr<FrameScratchBuffer> Get() {
92     std::unique_lock<std::mutex> lock(mutex_);
93     if (!buffers_.Empty()) {
94       return buffers_.Pop();
95     }
96     lock.unlock();
97     std::unique_ptr<FrameScratchBuffer> scratch_buffer(new (std::nothrow)
98                                                            FrameScratchBuffer);
99     return scratch_buffer;
100   }
101 
Release(std::unique_ptr<FrameScratchBuffer> scratch_buffer)102   void Release(std::unique_ptr<FrameScratchBuffer> scratch_buffer) {
103     std::lock_guard<std::mutex> lock(mutex_);
104     buffers_.Push(std::move(scratch_buffer));
105   }
106 
107  private:
108   std::mutex mutex_;
109   Stack<std::unique_ptr<FrameScratchBuffer>, kMaxThreads> buffers_
110       LIBGAV1_GUARDED_BY(mutex_);
111 };
112 
113 }  // namespace libgav1
114 
115 #endif  // LIBGAV1_SRC_FRAME_SCRATCH_BUFFER_H_
116