decoder.h 5.91 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9
 * 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.
10 11
 */

12 13
#ifndef AV1_DECODER_DECODER_H_
#define AV1_DECODER_DECODER_H_
14

15
#include "./aom_config.h"
16

17
#include "aom/aom_codec.h"
18
#include "aom_dsp/bitreader.h"
19
#include "aom_scale/yv12config.h"
20
#include "aom_util/aom_thread.h"
21

22 23 24
#include "av1/common/thread_common.h"
#include "av1/common/onyxc_int.h"
#include "av1/decoder/dthread.h"
Michael Bebenita's avatar
Michael Bebenita committed
25
#if CONFIG_ACCOUNTING
26
#include "av1/decoder/accounting.h"
Michael Bebenita's avatar
Michael Bebenita committed
27
#endif
28 29 30
#if CONFIG_INSPECTION
#include "av1/decoder/inspection.h"
#endif
31 32 33 34 35 36

#ifdef __cplusplus
extern "C" {
#endif

typedef struct TileData {
37 38
  AV1_COMMON *cm;
  aom_reader bit_reader;
39 40
  DECLARE_ALIGNED(16, MACROBLOCKD, xd);
  /* dqcoeff are shared by all the planes. So planes must be decoded serially */
41
  DECLARE_ALIGNED(16, tran_low_t, dqcoeff[MAX_TX_SQUARE]);
42
  DECLARE_ALIGNED(16, FRAME_CONTEXT, tctx);
43
  DECLARE_ALIGNED(16, uint8_t, color_index_map[2][MAX_PALETTE_SQUARE]);
44 45
} TileData;

46 47 48
typedef struct TileBufferDec {
  const uint8_t *data;
  size_t size;
49 50
  const uint8_t *raw_data_end;  // The end of the raw tile buffer in the
                                // bit stream.
51
  int col;                      // only used with multi-threaded decoding
52 53
} TileBufferDec;

54
typedef struct AV1Decoder {
55 56
  DECLARE_ALIGNED(16, MACROBLOCKD, mb);

57
  DECLARE_ALIGNED(16, AV1_COMMON, common);
58 59 60 61 62 63 64

  int ready_for_new_data;

  int refresh_frame_flags;

  // TODO(hkuang): Combine this with cur_buf in macroblockd as they are
  // the same.
65
  RefCntBuffer *cur_buf;  //  Current decoding frame buffer.
66

67 68 69
  AVxWorker *frame_worker_owner;  // frame_worker that owns this pbi.
  AVxWorker lf_worker;
  AVxWorker *tile_workers;
70 71 72 73
  TileInfo *tile_worker_info;
  int num_tile_workers;

  TileData *tile_data;
74 75 76
  int allocated_tiles;

  TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
77

78
  AV1LfSync lf_row_sync;
79

80
  aom_decrypt_cb decrypt_cb;
81 82
  void *decrypt_state;

83
  int allow_lowbitdepth;
84 85
  int max_threads;
  int inv_tile_order;
86
  int need_resync;   // wait for key/intra-only frame.
87
  int hold_ref_buf;  // hold the reference buffer.
88 89 90 91

  int tile_size_bytes;
#if CONFIG_EXT_TILE
  int tile_col_size_bytes;
92 93
  int dec_tile_row, dec_tile_col;  // always -1 for non-VR tile encoding
#endif                             // CONFIG_EXT_TILE
Michael Bebenita's avatar
Michael Bebenita committed
94
#if CONFIG_ACCOUNTING
95
  int acct_enabled;
Michael Bebenita's avatar
Michael Bebenita committed
96 97
  Accounting accounting;
#endif
98 99
  size_t uncomp_hdr_size;       // Size of the uncompressed header
  size_t first_partition_size;  // Size of the compressed header
100 101
  int tg_size;                  // Number of tiles in the current tilegroup
  int tg_start;                 // First tile in the current tilegroup
102
  int tg_size_bit_offset;
103 104 105 106
#if CONFIG_INSPECTION
  aom_inspect_cb inspect_cb;
  void *inspect_ctx;
#endif
107
} AV1Decoder;
108

109 110
int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
                                const uint8_t **dest);
111

112
int av1_get_raw_frame(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *sd);
113

114
int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
115

116
aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
117
                                       YV12_BUFFER_CONFIG *sd);
118

119
aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
120 121 122
                                      YV12_BUFFER_CONFIG *sd);

static INLINE uint8_t read_marker(aom_decrypt_cb decrypt_cb,
123
                                  void *decrypt_state, const uint8_t *data) {
124 125 126 127 128 129 130 131 132 133
  if (decrypt_cb) {
    uint8_t marker;
    decrypt_cb(decrypt_state, data, &marker, 1);
    return marker;
  }
  return *data;
}

// This function is exposed for use in tests, as well as the inlined function
// "read_marker".
134 135
aom_codec_err_t av1_parse_superframe_index(const uint8_t *data, size_t data_sz,
                                           uint32_t sizes[8], int *count,
136
                                           int *index_size,
137 138
                                           aom_decrypt_cb decrypt_cb,
                                           void *decrypt_state);
139

140
struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
141

142
void av1_decoder_remove(struct AV1Decoder *pbi);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

static INLINE void decrease_ref_count(int idx, RefCntBuffer *const frame_bufs,
                                      BufferPool *const pool) {
  if (idx >= 0) {
    --frame_bufs[idx].ref_count;
    // A worker may only get a free framebuffer index when calling get_free_fb.
    // But the private buffer is not set up until finish decoding header.
    // So any error happens during decoding header, the frame_bufs will not
    // have valid priv buffer.
    if (frame_bufs[idx].ref_count == 0 &&
        frame_bufs[idx].raw_frame_buffer.priv) {
      pool->release_fb_cb(pool->cb_priv, &frame_bufs[idx].raw_frame_buffer);
    }
  }
}

159
static INLINE int dec_is_ref_frame_buf(AV1Decoder *const pbi,
160
                                       RefCntBuffer *frame_buf) {
161
  AV1_COMMON *const cm = &pbi->common;
162
  int i;
163
  for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
164 165
    RefBuffer *const ref_frame = &cm->frame_refs[i];
    if (ref_frame->idx == INVALID_IDX) continue;
166
    if (frame_buf == &cm->buffer_pool->frame_bufs[ref_frame->idx]) break;
167
  }
168
  return (i < INTER_REFS_PER_FRAME);
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182
#define ACCT_STR __func__
static INLINE int av1_read_uniform(aom_reader *r, int n) {
  const int l = get_unsigned_bits(n);
  const int m = (1 << l) - n;
  const int v = aom_read_literal(r, l - 1, ACCT_STR);
  assert(l != 0);
  if (v < m)
    return v;
  else
    return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
}

183 184 185 186
#ifdef __cplusplus
}  // extern "C"
#endif

187
#endif  // AV1_DECODER_DECODER_H_