GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cavsdec.c Lines: 446 564 79.1 %
Date: 2019-11-18 18:00:01 Branches: 260 364 71.4 %

Line Branch Exec Source
1
/*
2
 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3
 * Copyright (c) 2006  Stefan Gehrer <stefan.gehrer@gmx.de>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23
 * @file
24
 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25
 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26
 */
27
28
#include "libavutil/avassert.h"
29
#include "avcodec.h"
30
#include "get_bits.h"
31
#include "golomb.h"
32
#include "cavs.h"
33
#include "internal.h"
34
#include "mpeg12data.h"
35
36
static const uint8_t mv_scan[4] = {
37
    MV_FWD_X0, MV_FWD_X1,
38
    MV_FWD_X2, MV_FWD_X3
39
};
40
41
static const uint8_t cbp_tab[64][2] = {
42
  { 63,  0 }, { 15, 15 }, { 31, 63 }, { 47, 31 }, {  0, 16 }, { 14, 32 }, { 13, 47 }, { 11, 13 },
43
  {  7, 14 }, {  5, 11 }, { 10, 12 }, {  8,  5 }, { 12, 10 }, { 61,  7 }, {  4, 48 }, { 55,  3 },
44
  {  1,  2 }, {  2,  8 }, { 59,  4 }, {  3,  1 }, { 62, 61 }, {  9, 55 }, {  6, 59 }, { 29, 62 },
45
  { 45, 29 }, { 51, 27 }, { 23, 23 }, { 39, 19 }, { 27, 30 }, { 46, 28 }, { 53,  9 }, { 30,  6 },
46
  { 43, 60 }, { 37, 21 }, { 60, 44 }, { 16, 26 }, { 21, 51 }, { 28, 35 }, { 19, 18 }, { 35, 20 },
47
  { 42, 24 }, { 26, 53 }, { 44, 17 }, { 32, 37 }, { 58, 39 }, { 24, 45 }, { 20, 58 }, { 17, 43 },
48
  { 18, 42 }, { 48, 46 }, { 22, 36 }, { 33, 33 }, { 25, 34 }, { 49, 40 }, { 40, 52 }, { 36, 49 },
49
  { 34, 50 }, { 50, 56 }, { 52, 25 }, { 54, 22 }, { 41, 54 }, { 56, 57 }, { 38, 41 }, { 57, 38 }
50
};
51
52
static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
53
54
static const uint8_t dequant_shift[64] = {
55
  14, 14, 14, 14, 14, 14, 14, 14,
56
  13, 13, 13, 13, 13, 13, 13, 13,
57
  13, 12, 12, 12, 12, 12, 12, 12,
58
  11, 11, 11, 11, 11, 11, 11, 11,
59
  11, 10, 10, 10, 10, 10, 10, 10,
60
  10,  9,  9,  9,  9,  9,  9,  9,
61
  9,   8,  8,  8,  8,  8,  8,  8,
62
  7,   7,  7,  7,  7,  7,  7,  7
63
};
64
65
static const uint16_t dequant_mul[64] = {
66
  32768, 36061, 38968, 42495, 46341, 50535, 55437, 60424,
67
  32932, 35734, 38968, 42495, 46177, 50535, 55109, 59933,
68
  65535, 35734, 38968, 42577, 46341, 50617, 55027, 60097,
69
  32809, 35734, 38968, 42454, 46382, 50576, 55109, 60056,
70
  65535, 35734, 38968, 42495, 46320, 50515, 55109, 60076,
71
  65535, 35744, 38968, 42495, 46341, 50535, 55099, 60087,
72
  65535, 35734, 38973, 42500, 46341, 50535, 55109, 60097,
73
  32771, 35734, 38965, 42497, 46341, 50535, 55109, 60099
74
};
75
76
#define EOB 0, 0, 0
77
78
static const struct dec_2dvlc intra_dec[7] = {
79
    {
80
        { //level / run / table_inc
81
            {  1,  1,  1 }, { -1,  1,  1 }, {  1,  2,  1 }, { -1,  2,  1 }, {  1,  3,  1 }, { -1,  3, 1 },
82
            {  1,  4,  1 }, { -1,  4,  1 }, {  1,  5,  1 }, { -1,  5,  1 }, {  1,  6,  1 }, { -1,  6, 1 },
83
            {  1,  7,  1 }, { -1,  7,  1 }, {  1,  8,  1 }, { -1,  8,  1 }, {  1,  9,  1 }, { -1,  9, 1 },
84
            {  1, 10,  1 }, { -1, 10,  1 }, {  1, 11,  1 }, { -1, 11,  1 }, {  2,  1,  2 }, { -2,  1, 2 },
85
            {  1, 12,  1 }, { -1, 12,  1 }, {  1, 13,  1 }, { -1, 13,  1 }, {  1, 14,  1 }, { -1, 14, 1 },
86
            {  1, 15,  1 }, { -1, 15,  1 }, {  2,  2,  2 }, { -2,  2,  2 }, {  1, 16,  1 }, { -1, 16, 1 },
87
            {  1, 17,  1 }, { -1, 17,  1 }, {  3,  1,  3 }, { -3,  1,  3 }, {  1, 18,  1 }, { -1, 18, 1 },
88
            {  1, 19,  1 }, { -1, 19,  1 }, {  2,  3,  2 }, { -2,  3,  2 }, {  1, 20,  1 }, { -1, 20, 1 },
89
            {  1, 21,  1 }, { -1, 21,  1 }, {  2,  4,  2 }, { -2,  4,  2 }, {  1, 22,  1 }, { -1, 22, 1 },
90
            {  2,  5,  2 }, { -2,  5,  2 }, {  1, 23,  1 }, { -1, 23,  1 }, {   EOB    }
91
        },
92
        //level_add
93
        { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1 },
94
        2, //golomb_order
95
        0, //inc_limit
96
        23, //max_run
97
    },
98
    {
99
        { //level / run
100
            {  1,  1,  0 }, { -1,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  2,  1,  1 }, { -2,  1,  1 },
101
            {  1,  3,  0 }, { -1,  3,  0 }, {     EOB    }, {  1,  4,  0 }, { -1,  4,  0 }, {  1,  5,  0 },
102
            { -1,  5,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  3,  1,  2 }, { -3,  1,  2 }, {  2,  2,  1 },
103
            { -2,  2,  1 }, {  1,  7,  0 }, { -1,  7,  0 }, {  1,  8,  0 }, { -1,  8,  0 }, {  1,  9,  0 },
104
            { -1,  9,  0 }, {  2,  3,  1 }, { -2,  3,  1 }, {  4,  1,  2 }, { -4,  1,  2 }, {  1, 10,  0 },
105
            { -1, 10,  0 }, {  1, 11,  0 }, { -1, 11,  0 }, {  2,  4,  1 }, { -2,  4,  1 }, {  3,  2,  2 },
106
            { -3,  2,  2 }, {  1, 12,  0 }, { -1, 12,  0 }, {  2,  5,  1 }, { -2,  5,  1 }, {  5,  1,  3 },
107
            { -5,  1,  3 }, {  1, 13,  0 }, { -1, 13,  0 }, {  2,  6,  1 }, { -2,  6,  1 }, {  1, 14,  0 },
108
            { -1, 14,  0 }, {  2,  7,  1 }, { -2,  7,  1 }, {  2,  8,  1 }, { -2,  8,  1 }, {  3,  3,  2 },
109
            { -3,  3,  2 }, {  6,  1,  3 }, { -6,  1,  3 }, {  1, 15,  0 }, { -1, 15,  0 }
110
        },
111
        //level_add
112
        { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
113
        2, //golomb_order
114
        1, //inc_limit
115
        15, //max_run
116
    },
117
    {
118
        { //level / run
119
            {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 },
120
            {  3,  1,  1 }, { -3,  1,  1 }, {     EOB    }, {  1,  3,  0 }, { -1,  3,  0 }, {  2,  2,  0 },
121
            { -2,  2,  0 }, {  4,  1,  1 }, { -4,  1,  1 }, {  1,  4,  0 }, { -1,  4,  0 }, {  5,  1,  2 },
122
            { -5,  1,  2 }, {  1,  5,  0 }, { -1,  5,  0 }, {  3,  2,  1 }, { -3,  2,  1 }, {  2,  3,  0 },
123
            { -2,  3,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  6,  1,  2 }, { -6,  1,  2 }, {  2,  4,  0 },
124
            { -2,  4,  0 }, {  1,  7,  0 }, { -1,  7,  0 }, {  4,  2,  1 }, { -4,  2,  1 }, {  7,  1,  2 },
125
            { -7,  1,  2 }, {  3,  3,  1 }, { -3,  3,  1 }, {  2,  5,  0 }, { -2,  5,  0 }, {  1,  8,  0 },
126
            { -1,  8,  0 }, {  2,  6,  0 }, { -2,  6,  0 }, {  8,  1,  3 }, { -8,  1,  3 }, {  1,  9,  0 },
127
            { -1,  9,  0 }, {  5,  2,  2 }, { -5,  2,  2 }, {  3,  4,  1 }, { -3,  4,  1 }, {  2,  7,  0 },
128
            { -2,  7,  0 }, {  9,  1,  3 }, { -9,  1,  3 }, {  1, 10,  0 }, { -1, 10,  0 }
129
        },
130
        //level_add
131
        { 0, 10, 6, 4, 4, 3, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
132
        2, //golomb_order
133
        2, //inc_limit
134
        10, //max_run
135
    },
136
    {
137
        { //level / run
138
            {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 }, { -3,  1,  0 },
139
            {  1,  2,  0 }, { -1,  2,  0 }, {     EOB    }, {  4,  1,  0 }, { -4,  1,  0 }, {  5,  1,  1 },
140
            { -5,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  1,  3,  0 }, { -1,  3,  0 }, {  6,  1,  1 },
141
            { -6,  1,  1 }, {  3,  2,  0 }, { -3,  2,  0 }, {  7,  1,  1 }, { -7,  1,  1 }, {  1,  4,  0 },
142
            { -1,  4,  0 }, {  8,  1,  2 }, { -8,  1,  2 }, {  2,  3,  0 }, { -2,  3,  0 }, {  4,  2,  0 },
143
            { -4,  2,  0 }, {  1,  5,  0 }, { -1,  5,  0 }, {  9,  1,  2 }, { -9,  1,  2 }, {  5,  2,  1 },
144
            { -5,  2,  1 }, {  2,  4,  0 }, { -2,  4,  0 }, { 10,  1,  2 }, {-10,  1,  2 }, {  3,  3,  0 },
145
            { -3,  3,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, { 11,  1,  3 }, {-11,  1,  3 }, {  6,  2,  1 },
146
            { -6,  2,  1 }, {  1,  7,  0 }, { -1,  7,  0 }, {  2,  5,  0 }, { -2,  5,  0 }, {  3,  4,  0 },
147
            { -3,  4,  0 }, { 12,  1,  3 }, {-12,  1,  3 }, {  4,  3,  0 }, { -4,  3,  0 }
148
         },
149
        //level_add
150
        { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
151
        2, //golomb_order
152
        4, //inc_limit
153
        7, //max_run
154
    },
155
    {
156
        { //level / run
157
            {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 }, { -3,  1,  0 },
158
            {     EOB    }, {  4,  1,  0 }, { -4,  1,  0 }, {  5,  1,  0 }, { -5,  1,  0 }, {  6,  1,  0 },
159
            { -6,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  7,  1,  0 }, { -7,  1,  0 }, {  8,  1,  1 },
160
            { -8,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  9,  1,  1 }, { -9,  1,  1 }, { 10,  1,  1 },
161
            {-10,  1,  1 }, {  1,  3,  0 }, { -1,  3,  0 }, {  3,  2,  0 }, { -3,  2,  0 }, { 11,  1,  2 },
162
            {-11,  1,  2 }, {  4,  2,  0 }, { -4,  2,  0 }, { 12,  1,  2 }, {-12,  1,  2 }, { 13,  1,  2 },
163
            {-13,  1,  2 }, {  5,  2,  0 }, { -5,  2,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  2,  3,  0 },
164
            { -2,  3,  0 }, { 14,  1,  2 }, {-14,  1,  2 }, {  6,  2,  0 }, { -6,  2,  0 }, { 15,  1,  2 },
165
            {-15,  1,  2 }, { 16,  1,  2 }, {-16,  1,  2 }, {  3,  3,  0 }, { -3,  3,  0 }, {  1,  5,  0 },
166
            { -1,  5,  0 }, {  7,  2,  0 }, { -7,  2,  0 }, { 17,  1,  2 }, {-17,  1,  2 }
167
        },
168
        //level_add
169
        { 0,18, 8, 4, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
170
        2, //golomb_order
171
        7, //inc_limit
172
        5, //max_run
173
    },
174
    {
175
        { //level / run
176
            {     EOB    }, {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 },
177
            { -3,  1,  0 }, {  4,  1,  0 }, { -4,  1,  0 }, {  5,  1,  0 }, { -5,  1,  0 }, {  6,  1,  0 },
178
            { -6,  1,  0 }, {  7,  1,  0 }, { -7,  1,  0 }, {  8,  1,  0 }, { -8,  1,  0 }, {  9,  1,  0 },
179
            { -9,  1,  0 }, { 10,  1,  0 }, {-10,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, { 11,  1,  1 },
180
            {-11,  1,  1 }, { 12,  1,  1 }, {-12,  1,  1 }, { 13,  1,  1 }, {-13,  1,  1 }, {  2,  2,  0 },
181
            { -2,  2,  0 }, { 14,  1,  1 }, {-14,  1,  1 }, { 15,  1,  1 }, {-15,  1,  1 }, {  3,  2,  0 },
182
            { -3,  2,  0 }, { 16,  1,  1 }, {-16,  1,  1 }, {  1,  3,  0 }, { -1,  3,  0 }, { 17,  1,  1 },
183
            {-17,  1,  1 }, {  4,  2,  0 }, { -4,  2,  0 }, { 18,  1,  1 }, {-18,  1,  1 }, {  5,  2,  0 },
184
            { -5,  2,  0 }, { 19,  1,  1 }, {-19,  1,  1 }, { 20,  1,  1 }, {-20,  1,  1 }, {  6,  2,  0 },
185
            { -6,  2,  0 }, { 21,  1,  1 }, {-21,  1,  1 }, {  2,  3,  0 }, { -2,  3,  0 }
186
        },
187
        //level_add
188
        { 0, 22, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
189
        2, //golomb_order
190
        10, //inc_limit
191
        3, //max_run
192
    },
193
    {
194
        { //level / run
195
            {     EOB    }, {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 },
196
            { -3,  1,  0 }, {  4,  1,  0 }, { -4,  1,  0 }, {  5,  1,  0 }, { -5,  1,  0 }, {  6,  1,  0 },
197
            { -6,  1,  0 }, {  7,  1,  0 }, { -7,  1,  0 }, {  8,  1,  0 }, { -8,  1,  0 }, {  9,  1,  0 },
198
            { -9,  1,  0 }, { 10,  1,  0 }, {-10,  1,  0 }, { 11,  1,  0 }, {-11,  1,  0 }, { 12,  1,  0 },
199
            {-12,  1,  0 }, { 13,  1,  0 }, {-13,  1,  0 }, { 14,  1,  0 }, {-14,  1,  0 }, { 15,  1,  0 },
200
            {-15,  1,  0 }, { 16,  1,  0 }, {-16,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, { 17,  1,  0 },
201
            {-17,  1,  0 }, { 18,  1,  0 }, {-18,  1,  0 }, { 19,  1,  0 }, {-19,  1,  0 }, { 20,  1,  0 },
202
            {-20,  1,  0 }, { 21,  1,  0 }, {-21,  1,  0 }, {  2,  2,  0 }, { -2,  2,  0 }, { 22,  1,  0 },
203
            {-22,  1,  0 }, { 23,  1,  0 }, {-23,  1,  0 }, { 24,  1,  0 }, {-24,  1,  0 }, { 25,  1,  0 },
204
            {-25,  1,  0 }, {  3,  2,  0 }, { -3,  2,  0 }, { 26,  1,  0 }, {-26,  1,  0 }
205
        },
206
        //level_add
207
        { 0, 27, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
208
        2, //golomb_order
209
        INT_MAX, //inc_limit
210
        2, //max_run
211
    }
212
};
213
214
static const struct dec_2dvlc inter_dec[7] = {
215
    {
216
        { //level / run
217
            {  1,  1,  1 }, { -1,  1,  1 }, {  1,  2,  1 }, { -1,  2,  1 }, {  1,  3,  1 }, { -1,  3,  1 },
218
            {  1,  4,  1 }, { -1,  4,  1 }, {  1,  5,  1 }, { -1,  5,  1 }, {  1,  6,  1 }, { -1,  6,  1 },
219
            {  1,  7,  1 }, { -1,  7,  1 }, {  1,  8,  1 }, { -1,  8,  1 }, {  1,  9,  1 }, { -1,  9,  1 },
220
            {  1, 10,  1 }, { -1, 10,  1 }, {  1, 11,  1 }, { -1, 11,  1 }, {  1, 12,  1 }, { -1, 12,  1 },
221
            {  1, 13,  1 }, { -1, 13,  1 }, {  2,  1,  2 }, { -2,  1,  2 }, {  1, 14,  1 }, { -1, 14,  1 },
222
            {  1, 15,  1 }, { -1, 15,  1 }, {  1, 16,  1 }, { -1, 16,  1 }, {  1, 17,  1 }, { -1, 17,  1 },
223
            {  1, 18,  1 }, { -1, 18,  1 }, {  1, 19,  1 }, { -1, 19,  1 }, {  3,  1,  3 }, { -3,  1,  3 },
224
            {  1, 20,  1 }, { -1, 20,  1 }, {  1, 21,  1 }, { -1, 21,  1 }, {  2,  2,  2 }, { -2,  2,  2 },
225
            {  1, 22,  1 }, { -1, 22,  1 }, {  1, 23,  1 }, { -1, 23,  1 }, {  1, 24,  1 }, { -1, 24,  1 },
226
            {  1, 25,  1 }, { -1, 25,  1 }, {  1, 26,  1 }, { -1, 26,  1 }, {   EOB    }
227
        },
228
        //level_add
229
        { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
230
        3, //golomb_order
231
        0, //inc_limit
232
        26 //max_run
233
    },
234
    {
235
        { //level / run
236
            {  1,  1,  0 }, { -1,  1,  0 }, {     EOB    }, {  1,  2,  0 }, { -1,  2,  0 }, {  1,  3,  0 },
237
            { -1,  3,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  1,  5,  0 }, { -1,  5,  0 }, {  1,  6,  0 },
238
            { -1,  6,  0 }, {  2,  1,  1 }, { -2,  1,  1 }, {  1,  7,  0 }, { -1,  7,  0 }, {  1,  8,  0 },
239
            { -1,  8,  0 }, {  1,  9,  0 }, { -1,  9,  0 }, {  1, 10,  0 }, { -1, 10,  0 }, {  2,  2,  1 },
240
            { -2,  2,  1 }, {  1, 11,  0 }, { -1, 11,  0 }, {  1, 12,  0 }, { -1, 12,  0 }, {  3,  1,  2 },
241
            { -3,  1,  2 }, {  1, 13,  0 }, { -1, 13,  0 }, {  1, 14,  0 }, { -1, 14,  0 }, {  2,  3,  1 },
242
            { -2,  3,  1 }, {  1, 15,  0 }, { -1, 15,  0 }, {  2,  4,  1 }, { -2,  4,  1 }, {  1, 16,  0 },
243
            { -1, 16,  0 }, {  2,  5,  1 }, { -2,  5,  1 }, {  1, 17,  0 }, { -1, 17,  0 }, {  4,  1,  3 },
244
            { -4,  1,  3 }, {  2,  6,  1 }, { -2,  6,  1 }, {  1, 18,  0 }, { -1, 18,  0 }, {  1, 19,  0 },
245
            { -1, 19,  0 }, {  2,  7,  1 }, { -2,  7,  1 }, {  3,  2,  2 }, { -3,  2,  2 }
246
        },
247
        //level_add
248
        { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1 },
249
        2, //golomb_order
250
        1, //inc_limit
251
        19 //max_run
252
    },
253
    {
254
        { //level / run
255
            {  1,  1,  0 }, { -1,  1,  0 }, {     EOB    }, {  1,  2,  0 }, { -1,  2,  0 }, {  2,  1,  0 },
256
            { -2,  1,  0 }, {  1,  3,  0 }, { -1,  3,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  3,  1,  1 },
257
            { -3,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  1,  5,  0 }, { -1,  5,  0 }, {  1,  6,  0 },
258
            { -1,  6,  0 }, {  1,  7,  0 }, { -1,  7,  0 }, {  2,  3,  0 }, { -2,  3,  0 }, {  4,  1,  2 },
259
            { -4,  1,  2 }, {  1,  8,  0 }, { -1,  8,  0 }, {  3,  2,  1 }, { -3,  2,  1 }, {  2,  4,  0 },
260
            { -2,  4,  0 }, {  1,  9,  0 }, { -1,  9,  0 }, {  1, 10,  0 }, { -1, 10,  0 }, {  5,  1,  2 },
261
            { -5,  1,  2 }, {  2,  5,  0 }, { -2,  5,  0 }, {  1, 11,  0 }, { -1, 11,  0 }, {  2,  6,  0 },
262
            { -2,  6,  0 }, {  1, 12,  0 }, { -1, 12,  0 }, {  3,  3,  1 }, { -3,  3,  1 }, {  6,  1,  2 },
263
            { -6,  1,  2 }, {  4,  2,  2 }, { -4,  2,  2 }, {  1, 13,  0 }, { -1, 13,  0 }, {  2,  7,  0 },
264
            { -2,  7,  0 }, {  3,  4,  1 }, { -3,  4,  1 }, {  1, 14,  0 }, { -1, 14,  0 }
265
        },
266
        //level_add
267
        { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
268
        2, //golomb_order
269
        2, //inc_limit
270
        14 //max_run
271
    },
272
    {
273
        { //level / run
274
            {  1,  1,  0 }, { -1,  1,  0 }, {     EOB    }, {  2,  1,  0 }, { -2,  1,  0 }, {  1,  2,  0 },
275
            { -1,  2,  0 }, {  3,  1,  0 }, { -3,  1,  0 }, {  1,  3,  0 }, { -1,  3,  0 }, {  2,  2,  0 },
276
            { -2,  2,  0 }, {  4,  1,  1 }, { -4,  1,  1 }, {  1,  4,  0 }, { -1,  4,  0 }, {  5,  1,  1 },
277
            { -5,  1,  1 }, {  1,  5,  0 }, { -1,  5,  0 }, {  3,  2,  0 }, { -3,  2,  0 }, {  2,  3,  0 },
278
            { -2,  3,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  6,  1,  1 }, { -6,  1,  1 }, {  2,  4,  0 },
279
            { -2,  4,  0 }, {  1,  7,  0 }, { -1,  7,  0 }, {  4,  2,  1 }, { -4,  2,  1 }, {  7,  1,  2 },
280
            { -7,  1,  2 }, {  3,  3,  0 }, { -3,  3,  0 }, {  1,  8,  0 }, { -1,  8,  0 }, {  2,  5,  0 },
281
            { -2,  5,  0 }, {  8,  1,  2 }, { -8,  1,  2 }, {  1,  9,  0 }, { -1,  9,  0 }, {  3,  4,  0 },
282
            { -3,  4,  0 }, {  2,  6,  0 }, { -2,  6,  0 }, {  5,  2,  1 }, { -5,  2,  1 }, {  1, 10,  0 },
283
            { -1, 10,  0 }, {  9,  1,  2 }, { -9,  1,  2 }, {  4,  3,  1 }, { -4,  3,  1 }
284
        },
285
        //level_add
286
        { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
287
        2, //golomb_order
288
        3, //inc_limit
289
        10 //max_run
290
    },
291
    {
292
        { //level / run
293
            {  1,  1,  0 }, { -1,  1,  0 }, {     EOB    }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 },
294
            { -3,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  4,  1,  0 }, { -4,  1,  0 }, {  5,  1,  0 },
295
            { -5,  1,  0 }, {  2,  2,  0 }, { -2,  2,  0 }, {  1,  3,  0 }, { -1,  3,  0 }, {  6,  1,  0 },
296
            { -6,  1,  0 }, {  3,  2,  0 }, { -3,  2,  0 }, {  7,  1,  1 }, { -7,  1,  1 }, {  1,  4,  0 },
297
            { -1,  4,  0 }, {  8,  1,  1 }, { -8,  1,  1 }, {  2,  3,  0 }, { -2,  3,  0 }, {  4,  2,  0 },
298
            { -4,  2,  0 }, {  1,  5,  0 }, { -1,  5,  0 }, {  9,  1,  1 }, { -9,  1,  1 }, {  5,  2,  0 },
299
            { -5,  2,  0 }, {  2,  4,  0 }, { -2,  4,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, { 10,  1,  2 },
300
            {-10,  1,  2 }, {  3,  3,  0 }, { -3,  3,  0 }, { 11,  1,  2 }, {-11,  1,  2 }, {  1,  7,  0 },
301
            { -1,  7,  0 }, {  6,  2,  0 }, { -6,  2,  0 }, {  3,  4,  0 }, { -3,  4,  0 }, {  2,  5,  0 },
302
            { -2,  5,  0 }, { 12,  1,  2 }, {-12,  1,  2 }, {  4,  3,  0 }, { -4,  3,  0 }
303
        },
304
        //level_add
305
        { 0, 13, 7, 5, 4, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
306
        2, //golomb_order
307
        6, //inc_limit
308
        7  //max_run
309
    },
310
    {
311
        { //level / run
312
            {      EOB    }, {  1,  1,  0 }, {  -1,  1,  0 }, {  2,  1,  0 }, {  -2,  1,  0 }, {  3,  1,  0 },
313
            {  -3,  1,  0 }, {  4,  1,  0 }, {  -4,  1,  0 }, {  5,  1,  0 }, {  -5,  1,  0 }, {  1,  2,  0 },
314
            {  -1,  2,  0 }, {  6,  1,  0 }, {  -6,  1,  0 }, {  7,  1,  0 }, {  -7,  1,  0 }, {  8,  1,  0 },
315
            {  -8,  1,  0 }, {  2,  2,  0 }, {  -2,  2,  0 }, {  9,  1,  0 }, {  -9,  1,  0 }, {  1,  3,  0 },
316
            {  -1,  3,  0 }, { 10,  1,  1 }, { -10,  1,  1 }, {  3,  2,  0 }, {  -3,  2,  0 }, { 11,  1,  1 },
317
            { -11,  1,  1 }, {  4,  2,  0 }, {  -4,  2,  0 }, { 12,  1,  1 }, { -12,  1,  1 }, {  1,  4,  0 },
318
            {  -1,  4,  0 }, {  2,  3,  0 }, {  -2,  3,  0 }, { 13,  1,  1 }, { -13,  1,  1 }, {  5,  2,  0 },
319
            {  -5,  2,  0 }, { 14,  1,  1 }, { -14,  1,  1 }, {  6,  2,  0 }, {  -6,  2,  0 }, {  1,  5,  0 },
320
            {  -1,  5,  0 }, { 15,  1,  1 }, { -15,  1,  1 }, {  3,  3,  0 }, {  -3,  3,  0 }, { 16,  1,  1 },
321
            { -16,  1,  1 }, {  2,  4,  0 }, {  -2,  4,  0 }, {  7,  2,  0 }, {  -7,  2,  0 }
322
        },
323
        //level_add
324
        { 0, 17, 8, 4, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
325
        2, //golomb_order
326
        9, //inc_limit
327
        5  //max_run
328
    },
329
    {
330
        { //level / run
331
            {      EOB    }, {  1,  1,  0 }, {  -1,  1,  0 }, {  2,  1,  0 }, {  -2,  1,  0 }, {   3,  1,  0 },
332
            {  -3,  1,  0 }, {  4,  1,  0 }, {  -4,  1,  0 }, {  5,  1,  0 }, {  -5,  1,  0 }, {   6,  1,  0 },
333
            {  -6,  1,  0 }, {  7,  1,  0 }, {  -7,  1,  0 }, {  1,  2,  0 }, {  -1,  2,  0 }, {   8,  1,  0 },
334
            {  -8,  1,  0 }, {  9,  1,  0 }, {  -9,  1,  0 }, { 10,  1,  0 }, { -10,  1,  0 }, {  11,  1,  0 },
335
            { -11,  1,  0 }, { 12,  1,  0 }, { -12,  1,  0 }, {  2,  2,  0 }, {  -2,  2,  0 }, {  13,  1,  0 },
336
            { -13,  1,  0 }, {  1,  3,  0 }, {  -1,  3,  0 }, { 14,  1,  0 }, { -14,  1,  0 }, {  15,  1,  0 },
337
            { -15,  1,  0 }, {  3,  2,  0 }, {  -3,  2,  0 }, { 16,  1,  0 }, { -16,  1,  0 }, {  17,  1,  0 },
338
            { -17,  1,  0 }, { 18,  1,  0 }, { -18,  1,  0 }, {  4,  2,  0 }, {  -4,  2,  0 }, {  19,  1,  0 },
339
            { -19,  1,  0 }, { 20,  1,  0 }, { -20,  1,  0 }, {  2,  3,  0 }, {  -2,  3,  0 }, {   1,  4,  0 },
340
            {  -1,  4,  0 }, {  5,  2,  0 }, {  -5,  2,  0 }, { 21,  1,  0 }, { -21,  1,  0 }
341
        },
342
        //level_add
343
        { 0, 22, 6, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
344
        2, //golomb_order
345
        INT_MAX, //inc_limit
346
        4 //max_run
347
    }
348
};
349
350
static const struct dec_2dvlc chroma_dec[5] = {
351
    {
352
        { //level / run
353
            {  1,  1,  1 }, { -1,  1,  1 }, {  1,  2,  1 }, { -1,  2,  1 }, {  1,  3,  1 }, { -1,  3,  1 },
354
            {  1,  4,  1 }, { -1,  4,  1 }, {  1,  5,  1 }, { -1,  5,  1 }, {  1,  6,  1 }, { -1,  6,  1 },
355
            {  1,  7,  1 }, { -1,  7,  1 }, {  2,  1,  2 }, { -2,  1,  2 }, {  1,  8,  1 }, { -1,  8,  1 },
356
            {  1,  9,  1 }, { -1,  9,  1 }, {  1, 10,  1 }, { -1, 10,  1 }, {  1, 11,  1 }, { -1, 11,  1 },
357
            {  1, 12,  1 }, { -1, 12,  1 }, {  1, 13,  1 }, { -1, 13,  1 }, {  1, 14,  1 }, { -1, 14,  1 },
358
            {  1, 15,  1 }, { -1, 15,  1 }, {  3,  1,  3 }, { -3,  1,  3 }, {  1, 16,  1 }, { -1, 16,  1 },
359
            {  1, 17,  1 }, { -1, 17,  1 }, {  1, 18,  1 }, { -1, 18,  1 }, {  1, 19,  1 }, { -1, 19,  1 },
360
            {  1, 20,  1 }, { -1, 20,  1 }, {  1, 21,  1 }, { -1, 21,  1 }, {  1, 22,  1 }, { -1, 22,  1 },
361
            {  2,  2,  2 }, { -2,  2,  2 }, {  1, 23,  1 }, { -1, 23,  1 }, {  1, 24,  1 }, { -1, 24,  1 },
362
            {  1, 25,  1 }, { -1, 25,  1 }, {  4,  1,  3 }, { -4,  1,  3 }, {   EOB    }
363
        },
364
        //level_add
365
        { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1 },
366
        2, //golomb_order
367
        0, //inc_limit
368
        25 //max_run
369
    },
370
    {
371
        { //level / run
372
            {     EOB    }, {  1,  1,  0 }, { -1,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  2,  1,  1 },
373
            { -2,  1,  1 }, {  1,  3,  0 }, { -1,  3,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  1,  5,  0 },
374
            { -1,  5,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  3,  1,  2 }, { -3,  1,  2 }, {  1,  7,  0 },
375
            { -1,  7,  0 }, {  1,  8,  0 }, { -1,  8,  0 }, {  2,  2,  1 }, { -2,  2,  1 }, {  1,  9,  0 },
376
            { -1,  9,  0 }, {  1, 10,  0 }, { -1, 10,  0 }, {  1, 11,  0 }, { -1, 11,  0 }, {  4,  1,  2 },
377
            { -4,  1,  2 }, {  1, 12,  0 }, { -1, 12,  0 }, {  1, 13,  0 }, { -1, 13,  0 }, {  1, 14,  0 },
378
            { -1, 14,  0 }, {  2,  3,  1 }, { -2,  3,  1 }, {  1, 15,  0 }, { -1, 15,  0 }, {  2,  4,  1 },
379
            { -2,  4,  1 }, {  5,  1,  3 }, { -5,  1,  3 }, {  3,  2,  2 }, { -3,  2,  2 }, {  1, 16,  0 },
380
            { -1, 16,  0 }, {  1, 17,  0 }, { -1, 17,  0 }, {  1, 18,  0 }, { -1, 18,  0 }, {  2,  5,  1 },
381
            { -2,  5,  1 }, {  1, 19,  0 }, { -1, 19,  0 }, {  1, 20,  0 }, { -1, 20,  0 }
382
        },
383
        //level_add
384
        { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1 },
385
        0, //golomb_order
386
        1, //inc_limit
387
        20 //max_run
388
    },
389
    {
390
        { //level / run
391
            {  1,  1,  0 }, { -1,  1,  0 }, {     EOB    }, {  2,  1,  0 }, { -2,  1,  0 }, {  1,  2,  0 },
392
            { -1,  2,  0 }, {  3,  1,  1 }, { -3,  1,  1 }, {  1,  3,  0 }, { -1,  3,  0 }, {  4,  1,  1 },
393
            { -4,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  1,  4,  0 }, { -1,  4,  0 }, {  5,  1,  2 },
394
            { -5,  1,  2 }, {  1,  5,  0 }, { -1,  5,  0 }, {  3,  2,  1 }, { -3,  2,  1 }, {  2,  3,  0 },
395
            { -2,  3,  0 }, {  1,  6,  0 }, { -1,  6,  0 }, {  6,  1,  2 }, { -6,  1,  2 }, {  1,  7,  0 },
396
            { -1,  7,  0 }, {  2,  4,  0 }, { -2,  4,  0 }, {  7,  1,  2 }, { -7,  1,  2 }, {  1,  8,  0 },
397
            { -1,  8,  0 }, {  4,  2,  1 }, { -4,  2,  1 }, {  1,  9,  0 }, { -1,  9,  0 }, {  3,  3,  1 },
398
            { -3,  3,  1 }, {  2,  5,  0 }, { -2,  5,  0 }, {  2,  6,  0 }, { -2,  6,  0 }, {  8,  1,  2 },
399
            { -8,  1,  2 }, {  1, 10,  0 }, { -1, 10,  0 }, {  1, 11,  0 }, { -1, 11,  0 }, {  9,  1,  2 },
400
            { -9,  1,  2 }, {  5,  2,  2 }, { -5,  2,  2 }, {  3,  4,  1 }, { -3,  4,  1 },
401
        },
402
        //level_add
403
        { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
404
        1, //golomb_order
405
        2, //inc_limit
406
        11 //max_run
407
    },
408
    {
409
        { //level / run
410
            {     EOB    }, {  1,  1,  0 }, { -1,  1,  0 }, {  2,  1,  0 }, { -2,  1,  0 }, {  3,  1,  0 },
411
            { -3,  1,  0 }, {  4,  1,  0 }, { -4,  1,  0 }, {  1,  2,  0 }, { -1,  2,  0 }, {  5,  1,  1 },
412
            { -5,  1,  1 }, {  2,  2,  0 }, { -2,  2,  0 }, {  6,  1,  1 }, { -6,  1,  1 }, {  1,  3,  0 },
413
            { -1,  3,  0 }, {  7,  1,  1 }, { -7,  1,  1 }, {  3,  2,  0 }, { -3,  2,  0 }, {  8,  1,  1 },
414
            { -8,  1,  1 }, {  1,  4,  0 }, { -1,  4,  0 }, {  2,  3,  0 }, { -2,  3,  0 }, {  9,  1,  1 },
415
            { -9,  1,  1 }, {  4,  2,  0 }, { -4,  2,  0 }, {  1,  5,  0 }, { -1,  5,  0 }, { 10,  1,  1 },
416
            {-10,  1,  1 }, {  3,  3,  0 }, { -3,  3,  0 }, {  5,  2,  1 }, { -5,  2,  1 }, {  2,  4,  0 },
417
            { -2,  4,  0 }, { 11,  1,  1 }, {-11,  1,  1 }, {  1,  6,  0 }, { -1,  6,  0 }, { 12,  1,  1 },
418
            {-12,  1,  1 }, {  1,  7,  0 }, { -1,  7,  0 }, {  6,  2,  1 }, { -6,  2,  1 }, { 13,  1,  1 },
419
            {-13,  1,  1 }, {  2,  5,  0 }, { -2,  5,  0 }, {  1,  8,  0 }, { -1,  8,  0 },
420
        },
421
        //level_add
422
        { 0, 14, 7, 4, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
423
        1, //golomb_order
424
        4, //inc_limit
425
        8  //max_run
426
    },
427
    {
428
        { //level / run
429
            {      EOB    }, {  1,  1,  0 }, {  -1,  1,  0 }, {  2,  1,  0 }, {  -2,  1,  0 }, {  3,  1,  0 },
430
            {  -3,  1,  0 }, {  4,  1,  0 }, {  -4,  1,  0 }, {  5,  1,  0 }, {  -5,  1,  0 }, {  6,  1,  0 },
431
            {  -6,  1,  0 }, {  7,  1,  0 }, {  -7,  1,  0 }, {  8,  1,  0 }, {  -8,  1,  0 }, {  1,  2,  0 },
432
            {  -1,  2,  0 }, {  9,  1,  0 }, {  -9,  1,  0 }, { 10,  1,  0 }, { -10,  1,  0 }, { 11,  1,  0 },
433
            { -11,  1,  0 }, {  2,  2,  0 }, {  -2,  2,  0 }, { 12,  1,  0 }, { -12,  1,  0 }, { 13,  1,  0 },
434
            { -13,  1,  0 }, {  3,  2,  0 }, {  -3,  2,  0 }, { 14,  1,  0 }, { -14,  1,  0 }, {  1,  3,  0 },
435
            {  -1,  3,  0 }, { 15,  1,  0 }, { -15,  1,  0 }, {  4,  2,  0 }, {  -4,  2,  0 }, { 16,  1,  0 },
436
            { -16,  1,  0 }, { 17,  1,  0 }, { -17,  1,  0 }, {  5,  2,  0 }, {  -5,  2,  0 }, {  1,  4,  0 },
437
            {  -1,  4,  0 }, {  2,  3,  0 }, {  -2,  3,  0 }, { 18,  1,  0 }, { -18,  1,  0 }, {  6,  2,  0 },
438
            {  -6,  2,  0 }, { 19,  1,  0 }, { -19,  1,  0 }, {  1,  5,  0 }, {  -1,  5,  0 },
439
        },
440
        //level_add
441
        { 0, 20, 7, 3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
442
        0, //golomb_order
443
        INT_MAX, //inc_limit
444
        5, //max_run
445
    }
446
};
447
448
#undef EOB
449
450
/*****************************************************************************
451
 *
452
 * motion vector prediction
453
 *
454
 ****************************************************************************/
455
456
83018
static inline void store_mvs(AVSContext *h)
457
{
458
83018
    h->col_mv[h->mbidx * 4 + 0] = h->mv[MV_FWD_X0];
459
83018
    h->col_mv[h->mbidx * 4 + 1] = h->mv[MV_FWD_X1];
460
83018
    h->col_mv[h->mbidx * 4 + 2] = h->mv[MV_FWD_X2];
461
83018
    h->col_mv[h->mbidx * 4 + 3] = h->mv[MV_FWD_X3];
462
83018
}
463
464
522204
static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
465
                                  cavs_vector *col_mv)
466
{
467
522204
    cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
468
522204
    unsigned den = h->direct_den[col_mv->ref];
469
522204
    int m = FF_SIGNBIT(col_mv->x);
470
471
522204
    pmv_fw->dist = h->dist[1];
472
522204
    pmv_bw->dist = h->dist[0];
473
522204
    pmv_fw->ref = 1;
474
522204
    pmv_bw->ref = 0;
475
    /* scale the co-located motion vector according to its temporal span */
476
522204
    pmv_fw->x =     (((den + (den * col_mv->x * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
477
522204
    pmv_bw->x = m - (((den + (den * col_mv->x * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
478
522204
    m = FF_SIGNBIT(col_mv->y);
479
522204
    pmv_fw->y =     (((den + (den * col_mv->y * pmv_fw->dist ^ m) - m - 1) >> 14) ^ m) - m;
480
522204
    pmv_bw->y = m - (((den + (den * col_mv->y * pmv_bw->dist ^ m) - m - 1) >> 14) ^ m);
481
522204
}
482
483
28257
static inline void mv_pred_sym(AVSContext *h, cavs_vector *src,
484
                               enum cavs_block size)
485
{
486
28257
    cavs_vector *dst = src + MV_BWD_OFFS;
487
488
    /* backward mv is the scaled and negated forward mv */
489
28257
    dst->x = -((src->x * h->sym_factor + 256) >> 9);
490
28257
    dst->y = -((src->y * h->sym_factor + 256) >> 9);
491
28257
    dst->ref = 0;
492
28257
    dst->dist = h->dist[0];
493
28257
    set_mvs(dst, size);
494
28257
}
495
496
/*****************************************************************************
497
 *
498
 * residual data decoding
499
 *
500
 ****************************************************************************/
501
502
/** kth-order exponential golomb code */
503
2762076
static inline int get_ue_code(GetBitContext *gb, int order)
504
{
505
2762076
    unsigned ret = get_ue_golomb(gb);
506
2762076
    if (ret >= ((1U<<31)>>order)) {
507
3
        av_log(NULL, AV_LOG_ERROR, "get_ue_code: value too larger\n");
508
3
        return AVERROR_INVALIDDATA;
509
    }
510
2762073
    if (order) {
511
2377209
        return (ret<<order) + get_bits(gb, order);
512
    }
513
384864
    return ret;
514
}
515
516
351600
static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
517
                          int16_t *dst, int mul, int shift, int coeff_num)
518
{
519
351600
    int round = 1 << (shift - 1);
520
351600
    int pos = -1;
521
351600
    const uint8_t *scantab = h->scantable.permutated;
522
523
    /* inverse scan and dequantization */
524
2689692
    while (--coeff_num >= 0) {
525
2338092
        pos += run_buf[coeff_num];
526
2338092
        if (pos > 63) {
527
            av_log(h->avctx, AV_LOG_ERROR,
528
                   "position out of block bounds at pic %d MB(%d,%d)\n",
529
                   h->cur.poc, h->mbx, h->mby);
530
            return AVERROR_INVALIDDATA;
531
        }
532
2338092
        dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
533
    }
534
351600
    return 0;
535
}
536
537
/**
538
 * decode coefficients from one 8x8 block, dequantize, inverse transform
539
 *  and add them to sample block
540
 * @param r pointer to 2D VLC table
541
 * @param esc_golomb_order escape codes are k-golomb with this order k
542
 * @param qp quantizer
543
 * @param dst location of sample block
544
 * @param stride line stride in frame buffer
545
 */
546
351603
static int decode_residual_block(AVSContext *h, GetBitContext *gb,
547
                                 const struct dec_2dvlc *r, int esc_golomb_order,
548
                                 int qp, uint8_t *dst, ptrdiff_t stride)
549
{
550
    int i, esc_code, level, mask, ret;
551
    unsigned int level_code, run;
552
    int16_t level_buf[65];
553
    uint8_t run_buf[65];
554
351603
    int16_t *block = h->block;
555
556
2689698
    for (i = 0; i < 65; i++) {
557
2689698
        level_code = get_ue_code(gb, r->golomb_order);
558
2689698
        if (level_code >= ESCAPE_CODE) {
559
72381
            run      = ((level_code - ESCAPE_CODE) >> 1) + 1;
560
72381
            if(run > 64) {
561
3
                av_log(h->avctx, AV_LOG_ERROR, "run %d is too large\n", run);
562
3
                return AVERROR_INVALIDDATA;
563
            }
564
72378
            esc_code = get_ue_code(gb, esc_golomb_order);
565

72378
            if (esc_code < 0 || esc_code > 32767) {
566
                av_log(h->avctx, AV_LOG_ERROR, "esc_code invalid\n");
567
                return AVERROR_INVALIDDATA;
568
            }
569
570
72378
            level    = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
571
140649
            while (level > r->inc_limit)
572
68271
                r++;
573
72378
            mask  = -(level_code & 1);
574
72378
            level = (level ^ mask) - mask;
575
        } else {
576
2617317
            level = r->rltab[level_code][0];
577
2617317
            if (!level) //end of block signal
578
351600
                break;
579
2265717
            run = r->rltab[level_code][1];
580
2265717
            r  += r->rltab[level_code][2];
581
        }
582
2338095
        level_buf[i] = level;
583
2338095
        run_buf[i]   = run;
584
    }
585
351600
    if ((ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
586
351600
                      dequant_shift[qp], i)) < 0)
587
        return ret;
588
351600
    h->cdsp.cavs_idct8_add(dst, block, stride);
589
351600
    h->bdsp.clear_block(block);
590
351600
    return 0;
591
}
592
593
594
169615
static inline int decode_residual_chroma(AVSContext *h)
595
{
596
169615
    if (h->cbp & (1 << 4)) {
597
62361
        int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
598
62361
                              ff_cavs_chroma_qp[h->qp], h->cu, h->c_stride);
599
62361
        if (ret < 0)
600
1
            return ret;
601
    }
602
169614
    if (h->cbp & (1 << 5)) {
603
69792
        int ret = decode_residual_block(h, &h->gb, chroma_dec, 0,
604
69792
                              ff_cavs_chroma_qp[h->qp], h->cv, h->c_stride);
605
69792
        if (ret < 0)
606
            return ret;
607
    }
608
169614
    return 0;
609
}
610
611
144271
static inline int decode_residual_inter(AVSContext *h)
612
{
613
    int block;
614
615
    /* get coded block pattern */
616
144271
    int cbp = get_ue_golomb(&h->gb);
617
144271
    if (cbp > 63U) {
618
        av_log(h->avctx, AV_LOG_ERROR, "illegal inter cbp %d\n", cbp);
619
        return AVERROR_INVALIDDATA;
620
    }
621
144271
    h->cbp = cbp_tab[cbp][1];
622
623
    /* get quantizer */
624

144271
    if (h->cbp && !h->qp_fixed)
625
        h->qp = (h->qp + (unsigned)get_se_golomb(&h->gb)) & 63;
626
721355
    for (block = 0; block < 4; block++)
627
577084
        if (h->cbp & (1 << block))
628
180859
            decode_residual_block(h, &h->gb, inter_dec, 0, h->qp,
629
180859
                                  h->cy + h->luma_scan[block], h->l_stride);
630
144271
    decode_residual_chroma(h);
631
632
144271
    return 0;
633
}
634
635
/*****************************************************************************
636
 *
637
 * macroblock level
638
 *
639
 ****************************************************************************/
640
641
25344
static inline void set_mv_intra(AVSContext *h)
642
{
643
25344
    h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
644
25344
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
645
25344
    h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
646
25344
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
647
25344
    if (h->cur.f->pict_type != AV_PICTURE_TYPE_B)
648
22282
        h->col_type_base[h->mbidx] = I_8X8;
649
25344
}
650
651
25344
static int decode_mb_i(AVSContext *h, int cbp_code)
652
{
653
25344
    GetBitContext *gb = &h->gb;
654
    unsigned pred_mode_uv;
655
    int block;
656
    uint8_t top[18];
657
25344
    uint8_t *left = NULL;
658
    uint8_t *d;
659
    int ret;
660
661
25344
    ff_cavs_init_mb(h);
662
663
    /* get intra prediction modes from stream */
664
126720
    for (block = 0; block < 4; block++) {
665
        int nA, nB, predpred;
666
101376
        int pos = scan3x3[block];
667
668
101376
        nA = h->pred_mode_Y[pos - 1];
669
101376
        nB = h->pred_mode_Y[pos - 3];
670
101376
        predpred = FFMIN(nA, nB);
671
101376
        if (predpred == NOT_AVAIL) // if either is not available
672
1877
            predpred = INTRA_L_LP;
673
101376
        if (!get_bits1(gb)) {
674
17093
            int rem_mode = get_bits(gb, 2);
675
17093
            predpred     = rem_mode + (rem_mode >= predpred);
676
        }
677
101376
        h->pred_mode_Y[pos] = predpred;
678
    }
679
25344
    pred_mode_uv = get_ue_golomb(gb);
680
25344
    if (pred_mode_uv > 6) {
681
        av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
682
        return AVERROR_INVALIDDATA;
683
    }
684
25344
    ff_cavs_modify_mb_i(h, &pred_mode_uv);
685
686
    /* get coded block pattern */
687
25344
    if (h->cur.f->pict_type == AV_PICTURE_TYPE_I)
688
14580
        cbp_code = get_ue_golomb(gb);
689
25344
    if (cbp_code > 63U) {
690
        av_log(h->avctx, AV_LOG_ERROR, "illegal intra cbp\n");
691
        return AVERROR_INVALIDDATA;
692
    }
693
25344
    h->cbp = cbp_tab[cbp_code][0];
694

25344
    if (h->cbp && !h->qp_fixed)
695
        h->qp = (h->qp + (unsigned)get_se_golomb(gb)) & 63; //qp_delta
696
697
    /* luma intra prediction interleaved with residual decode/transform/add */
698
126720
    for (block = 0; block < 4; block++) {
699
101376
        d = h->cy + h->luma_scan[block];
700
101376
        ff_cavs_load_intra_pred_luma(h, top, &left, block);
701
101376
        h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
702
            (d, top, left, h->l_stride);
703
101376
        if (h->cbp & (1<<block)) {
704
38591
            ret = decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
705
38591
            if (ret < 0)
706
                return ret;
707
        }
708
    }
709
710
    /* chroma intra prediction */
711
25344
    ff_cavs_load_intra_pred_chroma(h);
712
25344
    h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx * 10],
713
25344
                                  h->left_border_u, h->c_stride);
714
25344
    h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
715
25344
                                  h->left_border_v, h->c_stride);
716
717
25344
    ret = decode_residual_chroma(h);
718
25344
    if (ret < 0)
719
        return ret;
720
25344
    ff_cavs_filter(h, I_8X8);
721
25344
    set_mv_intra(h);
722
25344
    return 0;
723
}
724
725
272401
static inline void set_intra_mode_default(AVSContext *h)
726
{
727
272401
    if (h->stream_revision > 0) {
728
        h->pred_mode_Y[3] =  h->pred_mode_Y[6] = NOT_AVAIL;
729
        h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = NOT_AVAIL;
730
    } else {
731
272401
        h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
732
272401
        h->top_pred_Y[h->mbx * 2 + 0] = h->top_pred_Y[h->mbx * 2 + 1] = INTRA_L_LP;
733
    }
734
272401
}
735
736
83018
static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type)
737
{
738
83018
    GetBitContext *gb = &h->gb;
739
    int ref[4];
740
741
83018
    ff_cavs_init_mb(h);
742

83018
    switch (mb_type) {
743
20801
    case P_SKIP:
744
20801
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP,  BLK_16X16, 0);
745
20801
        break;
746
48877
    case P_16X16:
747
48877
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
748
48877
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, ref[0]);
749
48877
        break;
750
3399
    case P_16X8:
751
3399
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
752
3399
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
753
3399
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,    BLK_16X8, ref[0]);
754
3399
        ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,   BLK_16X8, ref[2]);
755
3399
        break;
756
3154
    case P_8X16:
757
3154
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
758
3154
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
759
3154
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,     BLK_8X16, ref[0]);
760
3154
        ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
761
3154
        break;
762
6787
    case P_8X8:
763
6787
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
764
6787
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
765
6787
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
766
6787
        ref[3] = h->ref_flag ? 0 : get_bits1(gb);
767
6787
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
768
6787
        ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
769
6787
        ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
770
6787
        ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
771
    }
772
83018
    ff_cavs_inter(h, mb_type);
773
83018
    set_intra_mode_default(h);
774
83018
    store_mvs(h);
775
83018
    if (mb_type != P_SKIP)
776
62217
        decode_residual_inter(h);
777
83018
    ff_cavs_filter(h, mb_type);
778
83018
    h->col_type_base[h->mbidx] = mb_type;
779
83018
}
780
781
189383
static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type)
782
{
783
    int block;
784
    enum cavs_sub_mb sub_type[4];
785
    int flags;
786
787
189383
    ff_cavs_init_mb(h);
788
789
    /* reset all MVs */
790
189383
    h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
791
189383
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
792
189383
    h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
793
189383
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
794

189383
    switch (mb_type) {
795
144973
    case B_SKIP:
796
    case B_DIRECT:
797
144973
        if (!h->col_type_base[h->mbidx]) {
798
            /* intra MB at co-location, do in-plane prediction */
799
14422
            ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
800
14422
            ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
801
        } else
802
            /* direct prediction from co-located P MB, block-wise */
803
652755
            for (block = 0; block < 4; block++)
804
522204
                mv_pred_direct(h, &h->mv[mv_scan[block]],
805
522204
                               &h->col_mv[h->mbidx * 4 + block]);
806
144973
        break;
807
7233
    case B_FWD_16X16:
808
7233
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
809
7233
        break;
810
3127
    case B_SYM_16X16:
811
3127
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
812
3127
        mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
813
3127
        break;
814
5750
    case B_BWD_16X16:
815
5750
        ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
816
5750
        break;
817
13716
    case B_8X8:
818
#define TMP_UNUSED_INX  7
819
13716
        flags = 0;
820
68580
        for (block = 0; block < 4; block++)
821
54864
            sub_type[block] = get_bits(&h->gb, 2);
822
68580
        for (block = 0; block < 4; block++) {
823

54864
            switch (sub_type[block]) {
824
            case B_SUB_DIRECT:
825
                if (!h->col_type_base[h->mbidx]) {
826
                    /* intra MB at co-location, do in-plane prediction */
827
                    if(flags==0) {
828
                        // if col-MB is a Intra MB, current Block size is 16x16.
829
                        // AVS standard section 9.9.1
830
                        if(block>0){
831
                            h->mv[TMP_UNUSED_INX              ] = h->mv[MV_FWD_X0              ];
832
                            h->mv[TMP_UNUSED_INX + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
833
                        }
834
                        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2,
835
                                   MV_PRED_BSKIP, BLK_8X8, 1);
836
                        ff_cavs_mv(h, MV_FWD_X0+MV_BWD_OFFS,
837
                                   MV_FWD_C2+MV_BWD_OFFS,
838
                                   MV_PRED_BSKIP, BLK_8X8, 0);
839
                        if(block>0) {
840
                            flags = mv_scan[block];
841
                            h->mv[flags              ] = h->mv[MV_FWD_X0              ];
842
                            h->mv[flags + MV_BWD_OFFS] = h->mv[MV_FWD_X0 + MV_BWD_OFFS];
843
                            h->mv[MV_FWD_X0              ] = h->mv[TMP_UNUSED_INX              ];
844
                            h->mv[MV_FWD_X0 + MV_BWD_OFFS] = h->mv[TMP_UNUSED_INX + MV_BWD_OFFS];
845
                        } else
846
                            flags = MV_FWD_X0;
847
                    } else {
848
                        h->mv[mv_scan[block]              ] = h->mv[flags              ];
849
                        h->mv[mv_scan[block] + MV_BWD_OFFS] = h->mv[flags + MV_BWD_OFFS];
850
                    }
851
                } else
852
                    mv_pred_direct(h, &h->mv[mv_scan[block]],
853
                                   &h->col_mv[h->mbidx * 4 + block]);
854
                break;
855
20044
            case B_SUB_FWD:
856
20044
                ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
857
                           MV_PRED_MEDIAN, BLK_8X8, 1);
858
20044
                break;
859
16522
            case B_SUB_SYM:
860
16522
                ff_cavs_mv(h, mv_scan[block], mv_scan[block] - 3,
861
                           MV_PRED_MEDIAN, BLK_8X8, 1);
862
16522
                mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
863
16522
                break;
864
            }
865
54864
        }
866
#undef TMP_UNUSED_INX
867
68580
        for (block = 0; block < 4; block++) {
868
54864
            if (sub_type[block] == B_SUB_BWD)
869
18298
                ff_cavs_mv(h, mv_scan[block] + MV_BWD_OFFS,
870
18298
                           mv_scan[block] + MV_BWD_OFFS - 3,
871
                           MV_PRED_MEDIAN, BLK_8X8, 0);
872
        }
873
13716
        break;
874
14584
    default:
875
14584
        if (mb_type <= B_SYM_16X16) {
876
            av_log(h->avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
877
            return AVERROR_INVALIDDATA;
878
        }
879
        av_assert2(mb_type < B_8X8);
880
14584
        flags = ff_cavs_partition_flags[mb_type];
881
14584
        if (mb_type & 1) { /* 16x8 macroblock types */
882
7810
            if (flags & FWD0)
883
5218
                ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
884
7810
            if (flags & SYM0)
885
2134
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
886
7810
            if (flags & FWD1)
887
5320
                ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
888
7810
            if (flags & SYM1)
889
2490
                mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
890
7810
            if (flags & BWD0)
891
2592
                ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
892
7810
            if (flags & BWD1)
893
2490
                ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
894
        } else {          /* 8x16 macroblock types */
895
6774
            if (flags & FWD0)
896
4531
                ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
897
6774
            if (flags & SYM0)
898
1972
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
899
6774
            if (flags & FWD1)
900
4491
                ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 1);
901
6774
            if (flags & SYM1)
902
2012
                mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
903
6774
            if (flags & BWD0)
904
2243
                ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
905
6774
            if (flags & BWD1)
906
2283
                ff_cavs_mv(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 0);
907
        }
908
    }
909
189383
    ff_cavs_inter(h, mb_type);
910
189383
    set_intra_mode_default(h);
911
189383
    if (mb_type != B_SKIP)
912
82054
        decode_residual_inter(h);
913
189383
    ff_cavs_filter(h, mb_type);
914
915
189383
    return 0;
916
}
917
918
/*****************************************************************************
919
 *
920
 * slice level
921
 *
922
 ****************************************************************************/
923
924
static inline int decode_slice_header(AVSContext *h, GetBitContext *gb)
925
{
926
    if (h->stc > 0xAF)
927
        av_log(h->avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
928
929
    if (h->stc >= h->mb_height) {
930
        av_log(h->avctx, AV_LOG_ERROR, "stc 0x%02x is too large\n", h->stc);
931
        return AVERROR_INVALIDDATA;
932
    }
933
934
    h->mby   = h->stc;
935
    h->mbidx = h->mby * h->mb_width;
936
937
    /* mark top macroblocks as unavailable */
938
    h->flags &= ~(B_AVAIL | C_AVAIL);
939
    if (!h->pic_qp_fixed) {
940
        h->qp_fixed = get_bits1(gb);
941
        h->qp       = get_bits(gb, 6);
942
    }
943
    /* inter frame or second slice can have weighting params */
944
    if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
945
        (!h->pic_structure && h->mby >= h->mb_width / 2))
946
        if (get_bits1(gb)) { //slice_weighting_flag
947
            av_log(h->avctx, AV_LOG_ERROR,
948
                   "weighted prediction not yet supported\n");
949
        }
950
    return 0;
951
}
952
953
297746
static inline int check_for_slice(AVSContext *h)
954
{
955
297746
    GetBitContext *gb = &h->gb;
956
    int align;
957
958
297746
    if (h->mbx)
959
291129
        return 0;
960
6617
    align = (-get_bits_count(gb)) & 7;
961
    /* check for stuffing byte */
962

6617
    if (!align && (show_bits(gb, 8) == 0x80))
963
3
        align = 8;
964
6617
    if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) == 0x000001) {
965
        skip_bits_long(gb, 24 + align);
966
        h->stc = get_bits(gb, 8);
967
        if (h->stc >= h->mb_height)
968
            return 0;
969
        decode_slice_header(h, gb);
970
        return 1;
971
    }
972
6617
    return 0;
973
}
974
975
/*****************************************************************************
976
 *
977
 * frame level
978
 *
979
 ****************************************************************************/
980
981
184
static int decode_pic(AVSContext *h)
982
{
983
    int ret;
984
184
    int skip_count    = -1;
985
    enum cavs_mb mb_type;
986
987
184
    if (!h->top_qp) {
988
        av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\n");
989
        return AVERROR_INVALIDDATA;
990
    }
991
992
184
    av_frame_unref(h->cur.f);
993
994
184
    skip_bits(&h->gb, 16);//bbv_dwlay
995
184
    if (h->stc == PIC_PB_START_CODE) {
996
175
        h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
997
175
        if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {
998
            av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\n");
999
            return AVERROR_INVALIDDATA;
1000
        }
1001
        /* make sure we have the reference frames we need */
1002
175
        if (!h->DPB[0].f->data[0] ||
1003

175
           (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))
1004
            return AVERROR_INVALIDDATA;
1005
    } else {
1006
9
        h->cur.f->pict_type = AV_PICTURE_TYPE_I;
1007
9
        if (get_bits1(&h->gb))
1008
            skip_bits(&h->gb, 24);//time_code
1009
        /* old sample clips were all progressive and no low_delay,
1010
           bump stream revision if detected otherwise */
1011

9
        if (h->low_delay || !(show_bits(&h->gb, 9) & 1))
1012
            h->stream_revision = 1;
1013
        /* similarly test top_field_first and repeat_first_field */
1014
9
        else if (show_bits(&h->gb, 11) & 3)
1015
            h->stream_revision = 1;
1016
9
        if (h->stream_revision > 0)
1017
            skip_bits(&h->gb, 1); //marker_bit
1018
    }
1019
1020
184
    ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
1021
                        0 : AV_GET_BUFFER_FLAG_REF);
1022
184
    if (ret < 0)
1023
        return ret;
1024
1025
184
    if (!h->edge_emu_buffer) {
1026
4
        int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
1027
4
        h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);
1028
4
        if (!h->edge_emu_buffer)
1029
            return AVERROR(ENOMEM);
1030
    }
1031
1032
184
    if ((ret = ff_cavs_init_pic(h)) < 0)
1033
        return ret;
1034
184
    h->cur.poc = get_bits(&h->gb, 8) * 2;
1035
1036
    /* get temporal distances and MV scaling factors */
1037
184
    if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1038
65
        h->dist[0] = (h->cur.poc - h->DPB[0].poc) & 511;
1039
    } else {
1040
119
        h->dist[0] = (h->DPB[0].poc  - h->cur.poc) & 511;
1041
    }
1042
184
    h->dist[1] = (h->cur.poc - h->DPB[1].poc) & 511;
1043
184
    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1044
184
    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1045
184
    if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
1046
119
        h->sym_factor = h->dist[0] * h->scale_den[1];
1047
119
        if (FFABS(h->sym_factor) > 32768) {
1048
            av_log(h->avctx, AV_LOG_ERROR, "sym_factor %d too large\n", h->sym_factor);
1049
            return AVERROR_INVALIDDATA;
1050
        }
1051
    } else {
1052
65
        h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;
1053
65
        h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;
1054
    }
1055
1056
184
    if (h->low_delay)
1057
        get_ue_golomb(&h->gb); //bbv_check_times
1058
184
    h->progressive   = get_bits1(&h->gb);
1059
184
    h->pic_structure = 1;
1060
184
    if (!h->progressive)
1061
        h->pic_structure = get_bits1(&h->gb);
1062

184
    if (!h->pic_structure && h->stc == PIC_PB_START_CODE)
1063
        skip_bits1(&h->gb);     //advanced_pred_mode_disable
1064
184
    skip_bits1(&h->gb);        //top_field_first
1065
184
    skip_bits1(&h->gb);        //repeat_first_field
1066
184
    h->pic_qp_fixed =
1067
184
    h->qp_fixed = get_bits1(&h->gb);
1068
184
    h->qp       = get_bits(&h->gb, 6);
1069
184
    if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1070

9
        if (!h->progressive && !h->pic_structure)
1071
            skip_bits1(&h->gb);//what is this?
1072
9
        skip_bits(&h->gb, 4);   //reserved bits
1073
    } else {
1074

175
        if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
1075
56
            h->ref_flag        = get_bits1(&h->gb);
1076
175
        skip_bits(&h->gb, 4);   //reserved bits
1077
175
        h->skip_mode_flag      = get_bits1(&h->gb);
1078
    }
1079
184
    h->loop_filter_disable     = get_bits1(&h->gb);
1080

184
    if (!h->loop_filter_disable && get_bits1(&h->gb)) {
1081
        h->alpha_offset        = get_se_golomb(&h->gb);
1082
        h->beta_offset         = get_se_golomb(&h->gb);
1083
        if (   h->alpha_offset < -64 || h->alpha_offset > 64
1084
            || h-> beta_offset < -64 || h-> beta_offset > 64) {
1085
            h->alpha_offset = h->beta_offset  = 0;
1086
            return AVERROR_INVALIDDATA;
1087
        }
1088
    } else {
1089
184
        h->alpha_offset = h->beta_offset  = 0;
1090
    }
1091
1092
184
    ret = 0;
1093
184
    if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {
1094
        do {
1095
14580
            check_for_slice(h);
1096
14580
            ret = decode_mb_i(h, 0);
1097
14580
            if (ret < 0)
1098
                break;
1099
14580
        } while (ff_cavs_next_mb(h));
1100
175
    } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {
1101
        do {
1102
90720
            if (check_for_slice(h))
1103
                skip_count = -1;
1104

90720
            if (h->skip_mode_flag && (skip_count < 0)) {
1105
69924
                if (get_bits_left(&h->gb) < 1) {
1106
                    ret = AVERROR_INVALIDDATA;
1107
                    break;
1108
                }
1109
69924
                skip_count = get_ue_golomb(&h->gb);
1110
            }
1111

90720
            if (h->skip_mode_flag && skip_count--) {
1112
20801
                decode_mb_p(h, P_SKIP);
1113
            } else {
1114
69919
                if (get_bits_left(&h->gb) < 1) {
1115
                    ret = AVERROR_INVALIDDATA;
1116
                    break;
1117
                }
1118
69919
                mb_type = get_ue_golomb(&h->gb) + P_SKIP + h->skip_mode_flag;
1119
69919
                if (mb_type > P_8X8)
1120
7702
                    ret = decode_mb_i(h, mb_type - P_8X8 - 1);
1121
                else
1122
62217
                    decode_mb_p(h, mb_type);
1123
            }
1124
90720
            if (ret < 0)
1125
                break;
1126
90720
        } while (ff_cavs_next_mb(h));
1127
    } else { /* AV_PICTURE_TYPE_B */
1128
        do {
1129
192446
            if (check_for_slice(h))
1130
                skip_count = -1;
1131

192446
            if (h->skip_mode_flag && (skip_count < 0)) {
1132
85154
                if (get_bits_left(&h->gb) < 1) {
1133
1
                    ret = AVERROR_INVALIDDATA;
1134
1
                    break;
1135
                }
1136
85153
                skip_count = get_ue_golomb(&h->gb);
1137
            }
1138

192445
            if (h->skip_mode_flag && skip_count--) {
1139
107329
                ret = decode_mb_b(h, B_SKIP);
1140
            } else {
1141
85116
                if (get_bits_left(&h->gb) < 1) {
1142
                    ret = AVERROR_INVALIDDATA;
1143
                    break;
1144
                }
1145
85116
                mb_type = get_ue_golomb(&h->gb) + B_SKIP + h->skip_mode_flag;
1146
85116
                if (mb_type > B_8X8)
1147
3062
                    ret = decode_mb_i(h, mb_type - B_8X8 - 1);
1148
                else
1149
82054
                    ret = decode_mb_b(h, mb_type);
1150
            }
1151
192445
            if (ret < 0)
1152
                break;
1153
192445
        } while (ff_cavs_next_mb(h));
1154
    }
1155
184
    emms_c();
1156

184
    if (ret >= 0 && h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1157
65
        av_frame_unref(h->DPB[1].f);
1158
65
        FFSWAP(AVSFrame, h->cur, h->DPB[1]);
1159
65
        FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
1160
    }
1161
184
    return ret;
1162
}
1163
1164
/*****************************************************************************
1165
 *
1166
 * headers and interface
1167
 *
1168
 ****************************************************************************/
1169
1170
9
static int decode_seq_header(AVSContext *h)
1171
{
1172
    int frame_rate_code;
1173
    int width, height;
1174
    int ret;
1175
1176
9
    h->profile = get_bits(&h->gb, 8);
1177
9
    h->level   = get_bits(&h->gb, 8);
1178
9
    skip_bits1(&h->gb); //progressive sequence
1179
1180
9
    width  = get_bits(&h->gb, 14);
1181
9
    height = get_bits(&h->gb, 14);
1182


9
    if ((h->width || h->height) && (h->width != width || h->height != height)) {
1183
        avpriv_report_missing_feature(h->avctx,
1184
                                      "Width/height changing in CAVS");
1185
        return AVERROR_PATCHWELCOME;
1186
    }
1187

9
    if (width <= 0 || height <= 0) {
1188
        av_log(h->avctx, AV_LOG_ERROR, "Dimensions invalid\n");
1189
        return AVERROR_INVALIDDATA;
1190
    }
1191
9
    skip_bits(&h->gb, 2); //chroma format
1192
9
    skip_bits(&h->gb, 3); //sample_precision
1193
9
    h->aspect_ratio = get_bits(&h->gb, 4);
1194
9
    frame_rate_code = get_bits(&h->gb, 4);
1195

9
    if (frame_rate_code == 0 || frame_rate_code > 13) {
1196
        av_log(h->avctx, AV_LOG_WARNING,
1197
               "frame_rate_code %d is invalid\n", frame_rate_code);
1198
        frame_rate_code = 1;
1199
    }
1200
1201
9
    skip_bits(&h->gb, 18); //bit_rate_lower
1202
9
    skip_bits1(&h->gb);    //marker_bit
1203
9
    skip_bits(&h->gb, 12); //bit_rate_upper
1204
9
    h->low_delay =  get_bits1(&h->gb);
1205
1206
9
    ret = ff_set_dimensions(h->avctx, width, height);
1207
9
    if (ret < 0)
1208
        return ret;
1209
1210
9
    h->width  = width;
1211
9
    h->height = height;
1212
9
    h->mb_width  = (h->width  + 15) >> 4;
1213
9
    h->mb_height = (h->height + 15) >> 4;
1214
9
    h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
1215
9
    if (!h->top_qp)
1216
4
        return ff_cavs_init_top_lines(h);
1217
5
    return 0;
1218
}
1219
1220
static void cavs_flush(AVCodecContext * avctx)
1221
{
1222
    AVSContext *h = avctx->priv_data;
1223
    h->got_keyframe = 0;
1224
}
1225
1226
187
static int cavs_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
1227
                             AVPacket *avpkt)
1228
{
1229
187
    AVSContext *h      = avctx->priv_data;
1230
187
    const uint8_t *buf = avpkt->data;
1231
187
    int buf_size       = avpkt->size;
1232
187
    uint32_t stc       = -1;
1233
    int input_size, ret;
1234
    const uint8_t *buf_end;
1235
    const uint8_t *buf_ptr;
1236
187
    int frame_start = 0;
1237
1238
187
    if (buf_size == 0) {
1239

3
        if (!h->low_delay && h->DPB[0].f->data[0]) {
1240
2
            *got_frame = 1;
1241
2
            av_frame_move_ref(data, h->DPB[0].f);
1242
        }
1243
3
        return 0;
1244
    }
1245
1246
184
    h->stc = 0;
1247
1248
184
    buf_ptr = buf;
1249
184
    buf_end = buf + buf_size;
1250
    for(;;) {
1251
385
        buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
1252

385
        if ((stc & 0xFFFFFE00) || buf_ptr == buf_end) {
1253
184
            if (!h->stc)
1254
                av_log(h->avctx, AV_LOG_WARNING, "no frame decoded\n");
1255
184
            return FFMAX(0, buf_ptr - buf);
1256
        }
1257
201
        input_size = (buf_end - buf_ptr) * 8;
1258

201
        switch (stc) {
1259
9
        case CAVS_START_CODE:
1260
9
            init_get_bits(&h->gb, buf_ptr, input_size);
1261
9
            decode_seq_header(h);
1262
9
            break;
1263
9
        case PIC_I_START_CODE:
1264
9
            if (!h->got_keyframe) {
1265
4
                av_frame_unref(h->DPB[0].f);
1266
4
                av_frame_unref(h->DPB[1].f);
1267
4
                h->got_keyframe = 1;
1268
            }
1269
        case PIC_PB_START_CODE:
1270
184
            if (frame_start > 1)
1271
                return AVERROR_INVALIDDATA;
1272
184
            frame_start ++;
1273
184
            if (*got_frame)
1274
                av_frame_unref(data);
1275
184
            *got_frame = 0;
1276
184
            if (!h->got_keyframe)
1277
                break;
1278
184
            init_get_bits(&h->gb, buf_ptr, input_size);
1279
184
            h->stc = stc;
1280
184
            if (decode_pic(h))
1281
1
                break;
1282
183
            *got_frame = 1;
1283
183
            if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
1284
65
                if (h->DPB[!h->low_delay].f->data[0]) {
1285
61
                    if ((ret = av_frame_ref(data, h->DPB[!h->low_delay].f)) < 0)
1286
                        return ret;
1287
                } else {
1288
4
                    *got_frame = 0;
1289
                }
1290
            } else {
1291
118
                av_frame_move_ref(data, h->cur.f);
1292
            }
1293
183
            break;
1294
4
        case EXT_START_CODE:
1295
            //mpeg_decode_extension(avctx, buf_ptr, input_size);
1296
4
            break;
1297
4
        case USER_START_CODE:
1298
            //mpeg_decode_user_data(avctx, buf_ptr, input_size);
1299
4
            break;
1300
        default:
1301
            if (stc <= SLICE_MAX_START_CODE) {
1302
                init_get_bits(&h->gb, buf_ptr, input_size);
1303
                decode_slice_header(h, &h->gb);
1304
            }
1305
            break;
1306
        }
1307
    }
1308
}
1309
1310
AVCodec ff_cavs_decoder = {
1311
    .name           = "cavs",
1312
    .long_name      = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
1313
    .type           = AVMEDIA_TYPE_VIDEO,
1314
    .id             = AV_CODEC_ID_CAVS,
1315
    .priv_data_size = sizeof(AVSContext),
1316
    .init           = ff_cavs_init,
1317
    .close          = ff_cavs_end,
1318
    .decode         = cavs_decode_frame,
1319
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1320
    .flush          = cavs_flush,
1321
};