/*
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

#ifndef AOM_AV1_ENCODER_BITSTREAM_H_
#define AOM_AV1_ENCODER_BITSTREAM_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "av1/common/av1_common_int.h"
#include "av1/common/blockd.h"
#include "av1/common/enums.h"
#include "av1/encoder/level.h"
#include "aom_dsp/bitwriter.h"
#include "aom_util/aom_pthread.h"

struct aom_write_bit_buffer;
struct AV1_COMP;
struct ThreadData;

/*!\cond */

// Stores the location and size of a tile's data in the bitstream.  Used for
// later identifying identical tiles
typedef struct {
  uint8_t *data;
  size_t size;
} TileBufferEnc;

typedef struct {
  uint8_t *frame_header;
  size_t obu_header_byte_offset;
  size_t total_length;
} FrameHeaderInfo;

typedef struct {
  struct aom_write_bit_buffer *saved_wb;  // Bit stream buffer writer structure
  TileBufferEnc buf;     // Structure to hold bitstream buffer and size
  uint32_t *total_size;  // Size of the bitstream buffer for the tile in bytes
  uint8_t *dst;          // Base address of tile bitstream buffer
  uint8_t *tile_data_curr;   // Base address of tile-group bitstream buffer
  size_t tile_buf_size;      // Available bitstream buffer for the tile in bytes
  uint8_t obu_extn_header;   // Presence of OBU extension header
  uint32_t obu_header_size;  // Size of the OBU header
  int curr_tg_hdr_size;      // Size of the obu, tg, frame headers
  int tile_size_mi;          // Tile size in mi units
  int tile_row;              // Number of tile rows
  int tile_col;              // Number of tile columns
  int is_last_tile_in_tg;    // Flag to indicate last tile in a tile-group
  int new_tg;                // Flag to indicate starting of a new tile-group
} PackBSParams;

typedef struct {
  uint64_t abs_sum_level;
  uint16_t tile_idx;
} PackBSTileOrder;

// Pack bitstream data for pack bitstream multi-threading.
typedef struct {
#if CONFIG_MULTITHREAD
  // Mutex lock used while dispatching jobs.
  pthread_mutex_t *mutex_;
#endif
  // Tile order structure of pack bitstream multithreading.
  PackBSTileOrder pack_bs_tile_order[MAX_TILES];

  // Index of next job to be processed.
  int next_job_idx;
  // Initialized to false, set to true by the worker thread that encounters an
  // error in order to abort the processing of other worker threads.
  bool pack_bs_mt_exit;
} AV1EncPackBSSync;

/*!\endcond */

// Writes only the OBU Sequence Header payload, and returns the size of the
// payload written to 'dst'. This function does not write the OBU header, the
// optional extension, or the OBU size to 'dst'.
uint32_t av1_write_sequence_header_obu(const SequenceHeader *seq_params,
                                       uint8_t *const dst);

// Writes the OBU header byte, and the OBU header extension byte when
// 'obu_extension' is non-zero. Returns number of bytes written to 'dst'.
uint32_t av1_write_obu_header(AV1LevelParams *const level_params,
                              int *frame_header_count, OBU_TYPE obu_type,
                              int obu_extension, uint8_t *const dst);

int av1_write_uleb_obu_size(size_t obu_header_size, size_t obu_payload_size,
                            uint8_t *dest);

// Pack tile data in the bitstream with tile_group, frame
// and OBU header.
void av1_pack_tile_info(struct AV1_COMP *const cpi, struct ThreadData *const td,
                        PackBSParams *const pack_bs_params);

void av1_write_last_tile_info(
    struct AV1_COMP *const cpi, const FrameHeaderInfo *fh_info,
    struct aom_write_bit_buffer *saved_wb, size_t *curr_tg_data_size,
    uint8_t *curr_tg_start, uint32_t *const total_size,
    uint8_t **tile_data_start, int *const largest_tile_id,
    int *const is_first_tg, uint32_t obu_header_size, uint8_t obu_extn_header);

/*!\brief Pack the bitstream for one frame
 *
 * \ingroup high_level_algo
 * \callgraph
 */
int av1_pack_bitstream(struct AV1_COMP *const cpi, uint8_t *dst, size_t *size,
                       int *const largest_tile_id);

void av1_write_tx_type(const AV1_COMMON *const cm, const MACROBLOCKD *xd,
                       TX_TYPE tx_type, TX_SIZE tx_size, aom_writer *w);

void av1_reset_pack_bs_thread_data(struct ThreadData *const td);

void av1_accumulate_pack_bs_thread_data(struct AV1_COMP *const cpi,
                                        struct ThreadData const *td);

void av1_write_obu_tg_tile_headers(struct AV1_COMP *const cpi,
                                   MACROBLOCKD *const xd,
                                   PackBSParams *const pack_bs_params,
                                   const int tile_idx);

int av1_neg_interleave(int x, int ref, int max);
#ifdef __cplusplus
}  // extern "C"
#endif

#endif  // AOM_AV1_ENCODER_BITSTREAM_H_
