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