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 "av1/common/av1_common_int.h" 20 #include "av1/common/blockd.h" 21 #include "av1/common/enums.h" 22 #include "av1/encoder/level.h" 23 #include "aom_dsp/bitwriter.h" 24 25 struct aom_write_bit_buffer; 26 struct AV1_COMP; 27 struct ThreadData; 28 29 /*!\cond */ 30 31 // Stores the location and size of a tile's data in the bitstream. Used for 32 // later identifying identical tiles 33 typedef struct { 34 uint8_t *data; 35 size_t size; 36 } TileBufferEnc; 37 38 typedef struct { 39 uint8_t *frame_header; 40 size_t obu_header_byte_offset; 41 size_t total_length; 42 } FrameHeaderInfo; 43 44 typedef struct { 45 struct aom_write_bit_buffer *saved_wb; // Bit stream buffer writer structure 46 TileBufferEnc buf; // Structure to hold bitstream buffer and size 47 uint32_t *total_size; // Size of the bitstream buffer for the tile in bytes 48 uint8_t *dst; // Base address of tile bitstream buffer 49 uint8_t *tile_data_curr; // Base address of tile-group bitstream buffer 50 size_t tile_buf_size; // Available bitstream buffer for the tile in bytes 51 uint8_t obu_extn_header; // Presence of OBU extension header 52 uint32_t obu_header_size; // Size of the OBU header 53 int curr_tg_hdr_size; // Size of the obu, tg, frame headers 54 int tile_size_mi; // Tile size in mi units 55 int tile_row; // Number of tile rows 56 int tile_col; // Number of tile columns 57 int is_last_tile_in_tg; // Flag to indicate last tile in a tile-group 58 int new_tg; // Flag to indicate starting of a new tile-group 59 } PackBSParams; 60 61 typedef struct { 62 uint64_t abs_sum_level; 63 uint16_t tile_idx; 64 } PackBSTileOrder; 65 66 // Pack bitstream data for pack bitstream multi-threading. 67 typedef struct { 68 #if CONFIG_MULTITHREAD 69 // Mutex lock used while dispatching jobs. 70 pthread_mutex_t *mutex_; 71 #endif 72 // Tile order structure of pack bitstream multithreading. 73 PackBSTileOrder pack_bs_tile_order[MAX_TILES]; 74 75 // Index of next job to be processed. 76 int next_job_idx; 77 } AV1EncPackBSSync; 78 79 /*!\endcond */ 80 81 // Writes only the OBU Sequence Header payload, and returns the size of the 82 // payload written to 'dst'. This function does not write the OBU header, the 83 // optional extension, or the OBU size to 'dst'. 84 uint32_t av1_write_sequence_header_obu(const SequenceHeader *seq_params, 85 uint8_t *const dst); 86 87 // Writes the OBU header byte, and the OBU header extension byte when 88 // 'obu_extension' is non-zero. Returns number of bytes written to 'dst'. 89 uint32_t av1_write_obu_header(AV1LevelParams *const level_params, 90 int *frame_header_count, OBU_TYPE obu_type, 91 int obu_extension, uint8_t *const dst); 92 93 int av1_write_uleb_obu_size(size_t obu_header_size, size_t obu_payload_size, 94 uint8_t *dest); 95 96 // Pack tile data in the bitstream with tile_group, frame 97 // and OBU header. 98 void av1_pack_tile_info(struct AV1_COMP *const cpi, struct ThreadData *const td, 99 PackBSParams *const pack_bs_params); 100 101 void av1_write_last_tile_info( 102 struct AV1_COMP *const cpi, const FrameHeaderInfo *fh_info, 103 struct aom_write_bit_buffer *saved_wb, size_t *curr_tg_data_size, 104 uint8_t *curr_tg_start, uint32_t *const total_size, 105 uint8_t **tile_data_start, int *const largest_tile_id, 106 int *const is_first_tg, uint32_t obu_header_size, uint8_t obu_extn_header); 107 108 /*!\brief Pack the bitstream for one frame 109 * 110 * \ingroup high_level_algo 111 * \callgraph 112 */ 113 int av1_pack_bitstream(struct AV1_COMP *const cpi, uint8_t *dst, size_t *size, 114 int *const largest_tile_id); 115 116 void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd, 117 TX_TYPE tx_type, TX_SIZE tx_size, aom_writer *w); 118 119 void av1_reset_pack_bs_thread_data(struct ThreadData *const td); 120 121 void av1_accumulate_pack_bs_thread_data(struct AV1_COMP *const cpi, 122 struct ThreadData const *td); 123 124 void av1_write_obu_tg_tile_headers(struct AV1_COMP *const cpi, 125 MACROBLOCKD *const xd, 126 PackBSParams *const pack_bs_params, 127 const int tile_idx); 128 129 int av1_neg_interleave(int x, int ref, int max); 130 #ifdef __cplusplus 131 } // extern "C" 132 #endif 133 134 #endif // AOM_AV1_ENCODER_BITSTREAM_H_ 135