xref: /aosp_15_r20/external/libaom/av1/encoder/bitstream.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, 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 #ifndef AOM_AV1_ENCODER_BITSTREAM_H_
13 #define AOM_AV1_ENCODER_BITSTREAM_H_
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "av1/common/av1_common_int.h"
24 #include "av1/common/blockd.h"
25 #include "av1/common/enums.h"
26 #include "av1/encoder/level.h"
27 #include "aom_dsp/bitwriter.h"
28 #include "aom_util/aom_pthread.h"
29 
30 struct aom_write_bit_buffer;
31 struct AV1_COMP;
32 struct ThreadData;
33 
34 /*!\cond */
35 
36 // Stores the location and size of a tile's data in the bitstream.  Used for
37 // later identifying identical tiles
38 typedef struct {
39   uint8_t *data;
40   size_t size;
41 } TileBufferEnc;
42 
43 typedef struct {
44   uint8_t *frame_header;
45   size_t obu_header_byte_offset;
46   size_t total_length;
47 } FrameHeaderInfo;
48 
49 typedef struct {
50   struct aom_write_bit_buffer *saved_wb;  // Bit stream buffer writer structure
51   TileBufferEnc buf;     // Structure to hold bitstream buffer and size
52   uint32_t *total_size;  // Size of the bitstream buffer for the tile in bytes
53   uint8_t *dst;          // Base address of tile bitstream buffer
54   uint8_t *tile_data_curr;   // Base address of tile-group bitstream buffer
55   size_t tile_buf_size;      // Available bitstream buffer for the tile in bytes
56   uint8_t obu_extn_header;   // Presence of OBU extension header
57   uint32_t obu_header_size;  // Size of the OBU header
58   int curr_tg_hdr_size;      // Size of the obu, tg, frame headers
59   int tile_size_mi;          // Tile size in mi units
60   int tile_row;              // Number of tile rows
61   int tile_col;              // Number of tile columns
62   int is_last_tile_in_tg;    // Flag to indicate last tile in a tile-group
63   int new_tg;                // Flag to indicate starting of a new tile-group
64 } PackBSParams;
65 
66 typedef struct {
67   uint64_t abs_sum_level;
68   uint16_t tile_idx;
69 } PackBSTileOrder;
70 
71 // Pack bitstream data for pack bitstream multi-threading.
72 typedef struct {
73 #if CONFIG_MULTITHREAD
74   // Mutex lock used while dispatching jobs.
75   pthread_mutex_t *mutex_;
76 #endif
77   // Tile order structure of pack bitstream multithreading.
78   PackBSTileOrder pack_bs_tile_order[MAX_TILES];
79 
80   // Index of next job to be processed.
81   int next_job_idx;
82   // Initialized to false, set to true by the worker thread that encounters an
83   // error in order to abort the processing of other worker threads.
84   bool pack_bs_mt_exit;
85 } AV1EncPackBSSync;
86 
87 /*!\endcond */
88 
89 // Writes only the OBU Sequence Header payload, and returns the size of the
90 // payload written to 'dst'. This function does not write the OBU header, the
91 // optional extension, or the OBU size to 'dst'.
92 uint32_t av1_write_sequence_header_obu(const SequenceHeader *seq_params,
93                                        uint8_t *const dst, size_t dst_size);
94 
95 // Writes the OBU header byte, and the OBU header extension byte when
96 // has_nonzero_operating_point_idc is true and the OBU is part of a frame.
97 // Returns number of bytes written to 'dst'.
98 uint32_t av1_write_obu_header(AV1LevelParams *const level_params,
99                               int *frame_header_count, OBU_TYPE obu_type,
100                               bool has_nonzero_operating_point_idc,
101                               int obu_extension, uint8_t *const dst);
102 
103 // Encodes obu_payload_size as a leb128 integer and writes it to the dest
104 // buffer. The output must fill the buffer exactly. Returns AOM_CODEC_OK on
105 // success, AOM_CODEC_ERROR on failure.
106 int av1_write_uleb_obu_size(size_t obu_payload_size, uint8_t *dest,
107                             size_t dest_size);
108 
109 // Deprecated. Use av1_write_uleb_obu_size() instead.
110 int av1_write_uleb_obu_size_unsafe(size_t obu_payload_size, uint8_t *dest);
111 
112 // Pack tile data in the bitstream with tile_group, frame
113 // and OBU header.
114 void av1_pack_tile_info(struct AV1_COMP *const cpi, struct ThreadData *const td,
115                         PackBSParams *const pack_bs_params);
116 
117 void av1_write_last_tile_info(
118     struct AV1_COMP *const cpi, const FrameHeaderInfo *fh_info,
119     struct aom_write_bit_buffer *saved_wb, size_t *curr_tg_data_size,
120     uint8_t *curr_tg_start, uint32_t *const total_size,
121     uint8_t **tile_data_start, int *const largest_tile_id,
122     int *const is_first_tg, uint32_t obu_header_size, uint8_t obu_extn_header);
123 
124 /*!\brief Pack the bitstream for one frame
125  *
126  * \ingroup high_level_algo
127  * \callgraph
128  */
129 int av1_pack_bitstream(struct AV1_COMP *const cpi, uint8_t *dst,
130                        size_t dst_size, size_t *size,
131                        int *const largest_tile_id);
132 
133 void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd,
134                        TX_TYPE tx_type, TX_SIZE tx_size, aom_writer *w);
135 
136 void av1_reset_pack_bs_thread_data(struct ThreadData *const td);
137 
138 void av1_accumulate_pack_bs_thread_data(struct AV1_COMP *const cpi,
139                                         struct ThreadData const *td);
140 
141 void av1_write_obu_tg_tile_headers(struct AV1_COMP *const cpi,
142                                    MACROBLOCKD *const xd,
143                                    PackBSParams *const pack_bs_params,
144                                    const int tile_idx);
145 
146 int av1_neg_interleave(int x, int ref, int max);
147 #ifdef __cplusplus
148 }  // extern "C"
149 #endif
150 
151 #endif  // AOM_AV1_ENCODER_BITSTREAM_H_
152