decoder.h 5.41 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
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.
Jingning Han's avatar
Jingning Han committed
10
11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
13
#ifndef AV1_DECODER_DECODER_H_
#define AV1_DECODER_DECODER_H_
Jingning Han's avatar
Jingning Han committed
14

Yaowu Xu's avatar
Yaowu Xu committed
15
#include "./aom_config.h"
Jingning Han's avatar
Jingning Han committed
16

Yaowu Xu's avatar
Yaowu Xu committed
17
#include "aom/aom_codec.h"
18
#include "aom_dsp/bitreader.h"
19
#include "aom_scale/yv12config.h"
Yaowu Xu's avatar
Yaowu Xu committed
20
#include "aom_util/aom_thread.h"
Jingning Han's avatar
Jingning Han committed
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
Jingning Han's avatar
Jingning Han committed
31
32
33
34
35
36

#ifdef __cplusplus
extern "C" {
#endif

typedef struct TileData {
Yaowu Xu's avatar
Yaowu Xu committed
37
38
  AV1_COMMON *cm;
  aom_reader bit_reader;
Jingning Han's avatar
Jingning Han committed
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]);
Jingning Han's avatar
Jingning Han committed
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;

Yaowu Xu's avatar
Yaowu Xu committed
54
typedef struct AV1Decoder {
Jingning Han's avatar
Jingning Han committed
55
56
  DECLARE_ALIGNED(16, MACROBLOCKD, mb);

Yaowu Xu's avatar
Yaowu Xu committed
57
  DECLARE_ALIGNED(16, AV1_COMMON, common);
Jingning Han's avatar
Jingning Han committed
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.
Jingning Han's avatar
Jingning Han committed
66

Yaowu Xu's avatar
Yaowu Xu committed
67
68
69
  AVxWorker *frame_worker_owner;  // frame_worker that owns this pbi.
  AVxWorker lf_worker;
  AVxWorker *tile_workers;
Jingning Han's avatar
Jingning Han committed
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];
Jingning Han's avatar
Jingning Han committed
77

Yaowu Xu's avatar
Yaowu Xu committed
78
  AV1LfSync lf_row_sync;
Jingning Han's avatar
Jingning Han committed
79

80
  int allow_lowbitdepth;
Jingning Han's avatar
Jingning Han committed
81
82
  int max_threads;
  int inv_tile_order;
83
  int need_resync;   // wait for key/intra-only frame.
Jingning Han's avatar
Jingning Han committed
84
  int hold_ref_buf;  // hold the reference buffer.
85
86
87
88

  int tile_size_bytes;
#if CONFIG_EXT_TILE
  int tile_col_size_bytes;
89
90
  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
91
#if CONFIG_ACCOUNTING
92
  int acct_enabled;
Michael Bebenita's avatar
Michael Bebenita committed
93
94
  Accounting accounting;
#endif
95
96
  size_t uncomp_hdr_size;       // Size of the uncompressed header
  size_t first_partition_size;  // Size of the compressed header
97
98
  int tg_size;                  // Number of tiles in the current tilegroup
  int tg_start;                 // First tile in the current tilegroup
99
  int tg_size_bit_offset;
100
101
102
103
#if CONFIG_INSPECTION
  aom_inspect_cb inspect_cb;
  void *inspect_ctx;
#endif
Yaowu Xu's avatar
Yaowu Xu committed
104
} AV1Decoder;
Jingning Han's avatar
Jingning Han committed
105

Yaowu Xu's avatar
Yaowu Xu committed
106
107
int av1_receive_compressed_data(struct AV1Decoder *pbi, size_t size,
                                const uint8_t **dest);
Jingning Han's avatar
Jingning Han committed
108

Yaowu Xu's avatar
Yaowu Xu committed
109
int av1_get_raw_frame(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *sd);
Jingning Han's avatar
Jingning Han committed
110

Yaowu Xu's avatar
Yaowu Xu committed
111
int av1_get_frame_to_show(struct AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame);
112

113
aom_codec_err_t av1_copy_reference_dec(struct AV1Decoder *pbi, int idx,
114
                                       YV12_BUFFER_CONFIG *sd);
Jingning Han's avatar
Jingning Han committed
115

116
aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
Yaowu Xu's avatar
Yaowu Xu committed
117
118
                                      YV12_BUFFER_CONFIG *sd);

119
// This function is exposed for use in tests
Yaowu Xu's avatar
Yaowu Xu committed
120
121
aom_codec_err_t av1_parse_superframe_index(const uint8_t *data, size_t data_sz,
                                           uint32_t sizes[8], int *count,
122
                                           int *index_size);
Jingning Han's avatar
Jingning Han committed
123

Yaowu Xu's avatar
Yaowu Xu committed
124
struct AV1Decoder *av1_decoder_create(BufferPool *const pool);
Jingning Han's avatar
Jingning Han committed
125

Yaowu Xu's avatar
Yaowu Xu committed
126
void av1_decoder_remove(struct AV1Decoder *pbi);
Jingning Han's avatar
Jingning Han committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

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);
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
143
static INLINE int dec_is_ref_frame_buf(AV1Decoder *const pbi,
144
                                       RefCntBuffer *frame_buf) {
Yaowu Xu's avatar
Yaowu Xu committed
145
  AV1_COMMON *const cm = &pbi->common;
146
  int i;
147
  for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
148
149
    RefBuffer *const ref_frame = &cm->frame_refs[i];
    if (ref_frame->idx == INVALID_IDX) continue;
150
    if (frame_buf == &cm->buffer_pool->frame_bufs[ref_frame->idx]) break;
151
  }
152
  return (i < INTER_REFS_PER_FRAME);
153
154
}

155
156
157
158
159
160
161
162
163
164
165
166
#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);
}

Jingning Han's avatar
Jingning Han committed
167
168
169
170
#ifdef __cplusplus
}  // extern "C"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
171
#endif  // AV1_DECODER_DECODER_H_