00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/avassert.h"
00029 #include "avcodec.h"
00030 #include "get_bits.h"
00031 #include "golomb.h"
00032 #include "cavs.h"
00033
00034 static const uint8_t mv_scan[4] = {
00035 MV_FWD_X0,MV_FWD_X1,
00036 MV_FWD_X2,MV_FWD_X3
00037 };
00038
00039 static const uint8_t cbp_tab[64][2] = {
00040 {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13},
00041 { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3},
00042 { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62},
00043 {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6},
00044 {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20},
00045 {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43},
00046 {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49},
00047 {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38}
00048 };
00049
00050 static const uint8_t scan3x3[4] = { 4, 5, 7, 8 };
00051
00052 static const uint8_t cavs_chroma_qp[64] = {
00053 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
00054 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
00055 32,33,34,35,36,37,38,39,40,41,42,42,43,43,44,44,
00056 45,45,46,46,47,47,48,48,48,49,49,49,50,50,50,51
00057 };
00058
00059 static const uint8_t dequant_shift[64] = {
00060 14,14,14,14,14,14,14,14,
00061 13,13,13,13,13,13,13,13,
00062 13,12,12,12,12,12,12,12,
00063 11,11,11,11,11,11,11,11,
00064 11,10,10,10,10,10,10,10,
00065 10, 9, 9, 9, 9, 9, 9, 9,
00066 9, 8, 8, 8, 8, 8, 8, 8,
00067 7, 7, 7, 7, 7, 7, 7, 7
00068 };
00069
00070 static const uint16_t dequant_mul[64] = {
00071 32768,36061,38968,42495,46341,50535,55437,60424,
00072 32932,35734,38968,42495,46177,50535,55109,59933,
00073 65535,35734,38968,42577,46341,50617,55027,60097,
00074 32809,35734,38968,42454,46382,50576,55109,60056,
00075 65535,35734,38968,42495,46320,50515,55109,60076,
00076 65535,35744,38968,42495,46341,50535,55099,60087,
00077 65535,35734,38973,42500,46341,50535,55109,60097,
00078 32771,35734,38965,42497,46341,50535,55109,60099
00079 };
00080
00081 #define EOB 0,0,0
00082
00083 static const struct dec_2dvlc intra_dec[7] = {
00084 {
00085 {
00086 { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1},
00087 { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1},
00088 { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1},
00089 { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 2, 1, 2},{ -2, 1, 2},
00090 { 1,12, 1},{ -1,12, 1},{ 1,13, 1},{ -1,13, 1},{ 1,14, 1},{ -1,14, 1},
00091 { 1,15, 1},{ -1,15, 1},{ 2, 2, 2},{ -2, 2, 2},{ 1,16, 1},{ -1,16, 1},
00092 { 1,17, 1},{ -1,17, 1},{ 3, 1, 3},{ -3, 1, 3},{ 1,18, 1},{ -1,18, 1},
00093 { 1,19, 1},{ -1,19, 1},{ 2, 3, 2},{ -2, 3, 2},{ 1,20, 1},{ -1,20, 1},
00094 { 1,21, 1},{ -1,21, 1},{ 2, 4, 2},{ -2, 4, 2},{ 1,22, 1},{ -1,22, 1},
00095 { 2, 5, 2},{ -2, 5, 2},{ 1,23, 1},{ -1,23, 1},{ EOB }
00096 },
00097
00098 { 0, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00099 2, 2, 2, 2, 2, 2, 2,-1,-1,-1},
00100 2,
00101 0,
00102 23,
00103 },{
00104 {
00105 { 1, 1, 0},{ -1, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 1},{ -2, 1, 1},
00106 { 1, 3, 0},{ -1, 3, 0},{ EOB },{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},
00107 { -1, 5, 0},{ 1, 6, 0},{ -1, 6, 0},{ 3, 1, 2},{ -3, 1, 2},{ 2, 2, 1},
00108 { -2, 2, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0},{ -1, 8, 0},{ 1, 9, 0},
00109 { -1, 9, 0},{ 2, 3, 1},{ -2, 3, 1},{ 4, 1, 2},{ -4, 1, 2},{ 1,10, 0},
00110 { -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 2, 4, 1},{ -2, 4, 1},{ 3, 2, 2},
00111 { -3, 2, 2},{ 1,12, 0},{ -1,12, 0},{ 2, 5, 1},{ -2, 5, 1},{ 5, 1, 3},
00112 { -5, 1, 3},{ 1,13, 0},{ -1,13, 0},{ 2, 6, 1},{ -2, 6, 1},{ 1,14, 0},
00113 { -1,14, 0},{ 2, 7, 1},{ -2, 7, 1},{ 2, 8, 1},{ -2, 8, 1},{ 3, 3, 2},
00114 { -3, 3, 2},{ 6, 1, 3},{ -6, 1, 3},{ 1,15, 0},{ -1,15, 0}
00115 },
00116
00117 { 0, 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,
00118 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00119 2,
00120 1,
00121 15,
00122 },{
00123 {
00124 { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0},{ -1, 2, 0},
00125 { 3, 1, 1},{ -3, 1, 1},{ EOB },{ 1, 3, 0},{ -1, 3, 0},{ 2, 2, 0},
00126 { -2, 2, 0},{ 4, 1, 1},{ -4, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 2},
00127 { -5, 1, 2},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 3, 0},
00128 { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 2},{ -6, 1, 2},{ 2, 4, 0},
00129 { -2, 4, 0},{ 1, 7, 0},{ -1, 7, 0},{ 4, 2, 1},{ -4, 2, 1},{ 7, 1, 2},
00130 { -7, 1, 2},{ 3, 3, 1},{ -3, 3, 1},{ 2, 5, 0},{ -2, 5, 0},{ 1, 8, 0},
00131 { -1, 8, 0},{ 2, 6, 0},{ -2, 6, 0},{ 8, 1, 3},{ -8, 1, 3},{ 1, 9, 0},
00132 { -1, 9, 0},{ 5, 2, 2},{ -5, 2, 2},{ 3, 4, 1},{ -3, 4, 1},{ 2, 7, 0},
00133 { -2, 7, 0},{ 9, 1, 3},{ -9, 1, 3},{ 1,10, 0},{ -1,10, 0}
00134 },
00135
00136 { 0,10, 6, 4, 4, 3, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,
00137 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00138 2,
00139 2,
00140 10,
00141 },{
00142 {
00143 { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},{ -3, 1, 0},
00144 { 1, 2, 0},{ -1, 2, 0},{ EOB },{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 1},
00145 { -5, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 3, 0},{ -1, 3, 0},{ 6, 1, 1},
00146 { -6, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 7, 1, 1},{ -7, 1, 1},{ 1, 4, 0},
00147 { -1, 4, 0},{ 8, 1, 2},{ -8, 1, 2},{ 2, 3, 0},{ -2, 3, 0},{ 4, 2, 0},
00148 { -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 9, 1, 2},{ -9, 1, 2},{ 5, 2, 1},
00149 { -5, 2, 1},{ 2, 4, 0},{ -2, 4, 0},{ 10, 1, 2},{-10, 1, 2},{ 3, 3, 0},
00150 { -3, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 11, 1, 3},{-11, 1, 3},{ 6, 2, 1},
00151 { -6, 2, 1},{ 1, 7, 0},{ -1, 7, 0},{ 2, 5, 0},{ -2, 5, 0},{ 3, 4, 0},
00152 { -3, 4, 0},{ 12, 1, 3},{-12, 1, 3},{ 4, 3, 0},{ -4, 3, 0}
00153 },
00154
00155 { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1 -1,-1,-1,-1,-1,-1,
00156 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00157 2,
00158 4,
00159 7,
00160 },{
00161 {
00162 { 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},{ -3, 1, 0},
00163 { EOB },{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
00164 { -6, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 1},
00165 { -8, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 1},{ -9, 1, 1},{ 10, 1, 1},
00166 {-10, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 2},
00167 {-11, 1, 2},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 2},{-12, 1, 2},{ 13, 1, 2},
00168 {-13, 1, 2},{ 5, 2, 0},{ -5, 2, 0},{ 1, 4, 0},{ -1, 4, 0},{ 2, 3, 0},
00169 { -2, 3, 0},{ 14, 1, 2},{-14, 1, 2},{ 6, 2, 0},{ -6, 2, 0},{ 15, 1, 2},
00170 {-15, 1, 2},{ 16, 1, 2},{-16, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 1, 5, 0},
00171 { -1, 5, 0},{ 7, 2, 0},{ -7, 2, 0},{ 17, 1, 2},{-17, 1, 2}
00172 },
00173
00174 { 0,18, 8, 4, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00175 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00176 2,
00177 7,
00178 5,
00179 },{
00180 {
00181 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00182 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
00183 { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 9, 1, 0},
00184 { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 11, 1, 1},
00185 {-11, 1, 1},{ 12, 1, 1},{-12, 1, 1},{ 13, 1, 1},{-13, 1, 1},{ 2, 2, 0},
00186 { -2, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 15, 1, 1},{-15, 1, 1},{ 3, 2, 0},
00187 { -3, 2, 0},{ 16, 1, 1},{-16, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 17, 1, 1},
00188 {-17, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 18, 1, 1},{-18, 1, 1},{ 5, 2, 0},
00189 { -5, 2, 0},{ 19, 1, 1},{-19, 1, 1},{ 20, 1, 1},{-20, 1, 1},{ 6, 2, 0},
00190 { -6, 2, 0},{ 21, 1, 1},{-21, 1, 1},{ 2, 3, 0},{ -2, 3, 0}
00191 },
00192
00193 { 0,22, 7, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00194 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00195 2,
00196 10,
00197 3,
00198 },{
00199 {
00200 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00201 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
00202 { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 9, 1, 0},
00203 { -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},{-11, 1, 0},{ 12, 1, 0},
00204 {-12, 1, 0},{ 13, 1, 0},{-13, 1, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
00205 {-15, 1, 0},{ 16, 1, 0},{-16, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 17, 1, 0},
00206 {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 19, 1, 0},{-19, 1, 0},{ 20, 1, 0},
00207 {-20, 1, 0},{ 21, 1, 0},{-21, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 22, 1, 0},
00208 {-22, 1, 0},{ 23, 1, 0},{-23, 1, 0},{ 24, 1, 0},{-24, 1, 0},{ 25, 1, 0},
00209 {-25, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 26, 1, 0},{-26, 1, 0}
00210 },
00211
00212 { 0,27, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00213 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00214 2,
00215 INT_MAX,
00216 2,
00217 }
00218 };
00219
00220 static const struct dec_2dvlc inter_dec[7] = {
00221 {
00222 {
00223 { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1},
00224 { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1},
00225 { 1, 7, 1},{ -1, 7, 1},{ 1, 8, 1},{ -1, 8, 1},{ 1, 9, 1},{ -1, 9, 1},
00226 { 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},{ 1,12, 1},{ -1,12, 1},
00227 { 1,13, 1},{ -1,13, 1},{ 2, 1, 2},{ -2, 1, 2},{ 1,14, 1},{ -1,14, 1},
00228 { 1,15, 1},{ -1,15, 1},{ 1,16, 1},{ -1,16, 1},{ 1,17, 1},{ -1,17, 1},
00229 { 1,18, 1},{ -1,18, 1},{ 1,19, 1},{ -1,19, 1},{ 3, 1, 3},{ -3, 1, 3},
00230 { 1,20, 1},{ -1,20, 1},{ 1,21, 1},{ -1,21, 1},{ 2, 2, 2},{ -2, 2, 2},
00231 { 1,22, 1},{ -1,22, 1},{ 1,23, 1},{ -1,23, 1},{ 1,24, 1},{ -1,24, 1},
00232 { 1,25, 1},{ -1,25, 1},{ 1,26, 1},{ -1,26, 1},{ EOB }
00233 },
00234
00235 { 0, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00236 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
00237 3,
00238 0,
00239 26
00240 },{
00241 {
00242 { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 1, 3, 0},
00243 { -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0},
00244 { -1, 6, 0},{ 2, 1, 1},{ -2, 1, 1},{ 1, 7, 0},{ -1, 7, 0},{ 1, 8, 0},
00245 { -1, 8, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 2, 2, 1},
00246 { -2, 2, 1},{ 1,11, 0},{ -1,11, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 1, 2},
00247 { -3, 1, 2},{ 1,13, 0},{ -1,13, 0},{ 1,14, 0},{ -1,14, 0},{ 2, 3, 1},
00248 { -2, 3, 1},{ 1,15, 0},{ -1,15, 0},{ 2, 4, 1},{ -2, 4, 1},{ 1,16, 0},
00249 { -1,16, 0},{ 2, 5, 1},{ -2, 5, 1},{ 1,17, 0},{ -1,17, 0},{ 4, 1, 3},
00250 { -4, 1, 3},{ 2, 6, 1},{ -2, 6, 1},{ 1,18, 0},{ -1,18, 0},{ 1,19, 0},
00251 { -1,19, 0},{ 2, 7, 1},{ -2, 7, 1},{ 3, 2, 2},{ -3, 2, 2}
00252 },
00253
00254 { 0, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00255 2, 2, 2,-1,-1,-1,-1,-1,-1,-1},
00256 2,
00257 1,
00258 19
00259 },{
00260 {
00261 { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 0},
00262 { -2, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 3, 1, 1},
00263 { -3, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 1, 6, 0},
00264 { -1, 6, 0},{ 1, 7, 0},{ -1, 7, 0},{ 2, 3, 0},{ -2, 3, 0},{ 4, 1, 2},
00265 { -4, 1, 2},{ 1, 8, 0},{ -1, 8, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 4, 0},
00266 { -2, 4, 0},{ 1, 9, 0},{ -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 5, 1, 2},
00267 { -5, 1, 2},{ 2, 5, 0},{ -2, 5, 0},{ 1,11, 0},{ -1,11, 0},{ 2, 6, 0},
00268 { -2, 6, 0},{ 1,12, 0},{ -1,12, 0},{ 3, 3, 1},{ -3, 3, 1},{ 6, 1, 2},
00269 { -6, 1, 2},{ 4, 2, 2},{ -4, 2, 2},{ 1,13, 0},{ -1,13, 0},{ 2, 7, 0},
00270 { -2, 7, 0},{ 3, 4, 1},{ -3, 4, 1},{ 1,14, 0},{ -1,14, 0}
00271 },
00272
00273 { 0, 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,-1,
00274 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00275 2,
00276 2,
00277 14
00278 },{
00279 {
00280 { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0},
00281 { -1, 2, 0},{ 3, 1, 0},{ -3, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 2, 2, 0},
00282 { -2, 2, 0},{ 4, 1, 1},{ -4, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 1},
00283 { -5, 1, 1},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 0},{ -3, 2, 0},{ 2, 3, 0},
00284 { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 1},{ -6, 1, 1},{ 2, 4, 0},
00285 { -2, 4, 0},{ 1, 7, 0},{ -1, 7, 0},{ 4, 2, 1},{ -4, 2, 1},{ 7, 1, 2},
00286 { -7, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 1, 8, 0},{ -1, 8, 0},{ 2, 5, 0},
00287 { -2, 5, 0},{ 8, 1, 2},{ -8, 1, 2},{ 1, 9, 0},{ -1, 9, 0},{ 3, 4, 0},
00288 { -3, 4, 0},{ 2, 6, 0},{ -2, 6, 0},{ 5, 2, 1},{ -5, 2, 1},{ 1,10, 0},
00289 { -1,10, 0},{ 9, 1, 2},{ -9, 1, 2},{ 4, 3, 1},{ -4, 3, 1}
00290 },
00291
00292 { 0,10, 6, 5, 4, 3, 3, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,
00293 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00294 2,
00295 3,
00296 10
00297 },{
00298 {
00299 { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00300 { -3, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},
00301 { -5, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 1, 3, 0},{ -1, 3, 0},{ 6, 1, 0},
00302 { -6, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 7, 1, 1},{ -7, 1, 1},{ 1, 4, 0},
00303 { -1, 4, 0},{ 8, 1, 1},{ -8, 1, 1},{ 2, 3, 0},{ -2, 3, 0},{ 4, 2, 0},
00304 { -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 9, 1, 1},{ -9, 1, 1},{ 5, 2, 0},
00305 { -5, 2, 0},{ 2, 4, 0},{ -2, 4, 0},{ 1, 6, 0},{ -1, 6, 0},{ 10, 1, 2},
00306 {-10, 1, 2},{ 3, 3, 0},{ -3, 3, 0},{ 11, 1, 2},{-11, 1, 2},{ 1, 7, 0},
00307 { -1, 7, 0},{ 6, 2, 0},{ -6, 2, 0},{ 3, 4, 0},{ -3, 4, 0},{ 2, 5, 0},
00308 { -2, 5, 0},{ 12, 1, 2},{-12, 1, 2},{ 4, 3, 0},{ -4, 3, 0}
00309 },
00310
00311 { 0,13, 7, 5, 4, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00312 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00313 2,
00314 6,
00315 7
00316 },{
00317 {
00318 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00319 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 1, 2, 0},
00320 { -1, 2, 0},{ 6, 1, 0},{ -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},
00321 { -8, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 9, 1, 0},{ -9, 1, 0},{ 1, 3, 0},
00322 { -1, 3, 0},{ 10, 1, 1},{-10, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 11, 1, 1},
00323 {-11, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 12, 1, 1},{-12, 1, 1},{ 1, 4, 0},
00324 { -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 13, 1, 1},{-13, 1, 1},{ 5, 2, 0},
00325 { -5, 2, 0},{ 14, 1, 1},{-14, 1, 1},{ 6, 2, 0},{ -6, 2, 0},{ 1, 5, 0},
00326 { -1, 5, 0},{ 15, 1, 1},{-15, 1, 1},{ 3, 3, 0},{ -3, 3, 0},{ 16, 1, 1},
00327 {-16, 1, 1},{ 2, 4, 0},{ -2, 4, 0},{ 7, 2, 0},{ -7, 2, 0}
00328 },
00329
00330 { 0,17, 8, 4, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00331 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00332 2,
00333 9,
00334 5
00335 },{
00336 {
00337 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00338 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
00339 { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 8, 1, 0},
00340 { -8, 1, 0},{ 9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},
00341 {-11, 1, 0},{ 12, 1, 0},{-12, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 13, 1, 0},
00342 {-13, 1, 0},{ 1, 3, 0},{ -1, 3, 0},{ 14, 1, 0},{-14, 1, 0},{ 15, 1, 0},
00343 {-15, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 16, 1, 0},{-16, 1, 0},{ 17, 1, 0},
00344 {-17, 1, 0},{ 18, 1, 0},{-18, 1, 0},{ 4, 2, 0},{ -4, 2, 0},{ 19, 1, 0},
00345 {-19, 1, 0},{ 20, 1, 0},{-20, 1, 0},{ 2, 3, 0},{ -2, 3, 0},{ 1, 4, 0},
00346 { -1, 4, 0},{ 5, 2, 0},{ -5, 2, 0},{ 21, 1, 0},{-21, 1, 0}
00347 },
00348
00349 { 0,22, 6, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00350 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00351 2,
00352 INT_MAX,
00353 4
00354 }
00355 };
00356
00357 static const struct dec_2dvlc chroma_dec[5] = {
00358 {
00359 {
00360 { 1, 1, 1},{ -1, 1, 1},{ 1, 2, 1},{ -1, 2, 1},{ 1, 3, 1},{ -1, 3, 1},
00361 { 1, 4, 1},{ -1, 4, 1},{ 1, 5, 1},{ -1, 5, 1},{ 1, 6, 1},{ -1, 6, 1},
00362 { 1, 7, 1},{ -1, 7, 1},{ 2, 1, 2},{ -2, 1, 2},{ 1, 8, 1},{ -1, 8, 1},
00363 { 1, 9, 1},{ -1, 9, 1},{ 1,10, 1},{ -1,10, 1},{ 1,11, 1},{ -1,11, 1},
00364 { 1,12, 1},{ -1,12, 1},{ 1,13, 1},{ -1,13, 1},{ 1,14, 1},{ -1,14, 1},
00365 { 1,15, 1},{ -1,15, 1},{ 3, 1, 3},{ -3, 1, 3},{ 1,16, 1},{ -1,16, 1},
00366 { 1,17, 1},{ -1,17, 1},{ 1,18, 1},{ -1,18, 1},{ 1,19, 1},{ -1,19, 1},
00367 { 1,20, 1},{ -1,20, 1},{ 1,21, 1},{ -1,21, 1},{ 1,22, 1},{ -1,22, 1},
00368 { 2, 2, 2},{ -2, 2, 2},{ 1,23, 1},{ -1,23, 1},{ 1,24, 1},{ -1,24, 1},
00369 { 1,25, 1},{ -1,25, 1},{ 4, 1, 3},{ -4, 1, 3},{ EOB }
00370 },
00371
00372 { 0, 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00373 2, 2, 2, 2, 2, 2, 2, 2, 2,-1},
00374 2,
00375 0,
00376 25
00377 },{
00378 {
00379 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 2, 1, 1},
00380 { -2, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 1, 4, 0},{ -1, 4, 0},{ 1, 5, 0},
00381 { -1, 5, 0},{ 1, 6, 0},{ -1, 6, 0},{ 3, 1, 2},{ -3, 1, 2},{ 1, 7, 0},
00382 { -1, 7, 0},{ 1, 8, 0},{ -1, 8, 0},{ 2, 2, 1},{ -2, 2, 1},{ 1, 9, 0},
00383 { -1, 9, 0},{ 1,10, 0},{ -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 4, 1, 2},
00384 { -4, 1, 2},{ 1,12, 0},{ -1,12, 0},{ 1,13, 0},{ -1,13, 0},{ 1,14, 0},
00385 { -1,14, 0},{ 2, 3, 1},{ -2, 3, 1},{ 1,15, 0},{ -1,15, 0},{ 2, 4, 1},
00386 { -2, 4, 1},{ 5, 1, 3},{ -5, 1, 3},{ 3, 2, 2},{ -3, 2, 2},{ 1,16, 0},
00387 { -1,16, 0},{ 1,17, 0},{ -1,17, 0},{ 1,18, 0},{ -1,18, 0},{ 2, 5, 1},
00388 { -2, 5, 1},{ 1,19, 0},{ -1,19, 0},{ 1,20, 0},{ -1,20, 0}
00389 },
00390
00391 { 0, 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
00392 2, 2, 2, 2,-1,-1,-1,-1,-1,-1},
00393 0,
00394 1,
00395 20
00396 },{
00397 {
00398 { 1, 1, 0},{ -1, 1, 0},{ EOB },{ 2, 1, 0},{ -2, 1, 0},{ 1, 2, 0},
00399 { -1, 2, 0},{ 3, 1, 1},{ -3, 1, 1},{ 1, 3, 0},{ -1, 3, 0},{ 4, 1, 1},
00400 { -4, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 1, 4, 0},{ -1, 4, 0},{ 5, 1, 2},
00401 { -5, 1, 2},{ 1, 5, 0},{ -1, 5, 0},{ 3, 2, 1},{ -3, 2, 1},{ 2, 3, 0},
00402 { -2, 3, 0},{ 1, 6, 0},{ -1, 6, 0},{ 6, 1, 2},{ -6, 1, 2},{ 1, 7, 0},
00403 { -1, 7, 0},{ 2, 4, 0},{ -2, 4, 0},{ 7, 1, 2},{ -7, 1, 2},{ 1, 8, 0},
00404 { -1, 8, 0},{ 4, 2, 1},{ -4, 2, 1},{ 1, 9, 0},{ -1, 9, 0},{ 3, 3, 1},
00405 { -3, 3, 1},{ 2, 5, 0},{ -2, 5, 0},{ 2, 6, 0},{ -2, 6, 0},{ 8, 1, 2},
00406 { -8, 1, 2},{ 1,10, 0},{ -1,10, 0},{ 1,11, 0},{ -1,11, 0},{ 9, 1, 2},
00407 { -9, 1, 2},{ 5, 2, 2},{ -5, 2, 2},{ 3, 4, 1},{ -3, 4, 1},
00408 },
00409
00410 { 0,10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,
00411 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00412 1,
00413 2,
00414 11
00415 },{
00416 {
00417 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00418 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 1, 2, 0},{ -1, 2, 0},{ 5, 1, 1},
00419 { -5, 1, 1},{ 2, 2, 0},{ -2, 2, 0},{ 6, 1, 1},{ -6, 1, 1},{ 1, 3, 0},
00420 { -1, 3, 0},{ 7, 1, 1},{ -7, 1, 1},{ 3, 2, 0},{ -3, 2, 0},{ 8, 1, 1},
00421 { -8, 1, 1},{ 1, 4, 0},{ -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 9, 1, 1},
00422 { -9, 1, 1},{ 4, 2, 0},{ -4, 2, 0},{ 1, 5, 0},{ -1, 5, 0},{ 10, 1, 1},
00423 {-10, 1, 1},{ 3, 3, 0},{ -3, 3, 0},{ 5, 2, 1},{ -5, 2, 1},{ 2, 4, 0},
00424 { -2, 4, 0},{ 11, 1, 1},{-11, 1, 1},{ 1, 6, 0},{ -1, 6, 0},{ 12, 1, 1},
00425 {-12, 1, 1},{ 1, 7, 0},{ -1, 7, 0},{ 6, 2, 1},{ -6, 2, 1},{ 13, 1, 1},
00426 {-13, 1, 1},{ 2, 5, 0},{ -2, 5, 0},{ 1, 8, 0},{ -1, 8, 0},
00427 },
00428
00429 { 0,14, 7, 4, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,
00430 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00431 1,
00432 4,
00433 8
00434 },{
00435 {
00436 { EOB },{ 1, 1, 0},{ -1, 1, 0},{ 2, 1, 0},{ -2, 1, 0},{ 3, 1, 0},
00437 { -3, 1, 0},{ 4, 1, 0},{ -4, 1, 0},{ 5, 1, 0},{ -5, 1, 0},{ 6, 1, 0},
00438 { -6, 1, 0},{ 7, 1, 0},{ -7, 1, 0},{ 8, 1, 0},{ -8, 1, 0},{ 1, 2, 0},
00439 { -1, 2, 0},{ 9, 1, 0},{ -9, 1, 0},{ 10, 1, 0},{-10, 1, 0},{ 11, 1, 0},
00440 {-11, 1, 0},{ 2, 2, 0},{ -2, 2, 0},{ 12, 1, 0},{-12, 1, 0},{ 13, 1, 0},
00441 {-13, 1, 0},{ 3, 2, 0},{ -3, 2, 0},{ 14, 1, 0},{-14, 1, 0},{ 1, 3, 0},
00442 { -1, 3, 0},{ 15, 1, 0},{-15, 1, 0},{ 4, 2, 0},{ -4, 2, 0},{ 16, 1, 0},
00443 {-16, 1, 0},{ 17, 1, 0},{-17, 1, 0},{ 5, 2, 0},{ -5, 2, 0},{ 1, 4, 0},
00444 { -1, 4, 0},{ 2, 3, 0},{ -2, 3, 0},{ 18, 1, 0},{-18, 1, 0},{ 6, 2, 0},
00445 { -6, 2, 0},{ 19, 1, 0},{-19, 1, 0},{ 1, 5, 0},{ -1, 5, 0},
00446 },
00447
00448 { 0,20, 7, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00449 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
00450 0,
00451 INT_MAX,
00452 5,
00453 }
00454 };
00455
00456 #undef EOB
00457
00458
00459
00460
00461
00462
00463
00464 static inline void store_mvs(AVSContext *h) {
00465 h->col_mv[h->mbidx*4 + 0] = h->mv[MV_FWD_X0];
00466 h->col_mv[h->mbidx*4 + 1] = h->mv[MV_FWD_X1];
00467 h->col_mv[h->mbidx*4 + 2] = h->mv[MV_FWD_X2];
00468 h->col_mv[h->mbidx*4 + 3] = h->mv[MV_FWD_X3];
00469 }
00470
00471 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
00472 cavs_vector *col_mv) {
00473 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
00474 int den = h->direct_den[col_mv->ref];
00475 int m = col_mv->x >> 31;
00476
00477 pmv_fw->dist = h->dist[1];
00478 pmv_bw->dist = h->dist[0];
00479 pmv_fw->ref = 1;
00480 pmv_bw->ref = 0;
00481
00482 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
00483 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
00484 m = col_mv->y >> 31;
00485 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
00486 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
00487 }
00488
00489 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
00490 cavs_vector *dst = src + MV_BWD_OFFS;
00491
00492
00493 dst->x = -((src->x * h->sym_factor + 256) >> 9);
00494 dst->y = -((src->y * h->sym_factor + 256) >> 9);
00495 dst->ref = 0;
00496 dst->dist = h->dist[0];
00497 set_mvs(dst, size);
00498 }
00499
00500
00501
00502
00503
00504
00505
00507 static inline int get_ue_code(GetBitContext *gb, int order) {
00508 if(order) {
00509 int ret = get_ue_golomb(gb) << order;
00510 return ret + get_bits(gb,order);
00511 }
00512 return get_ue_golomb(gb);
00513 }
00514
00515 static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
00516 DCTELEM *dst, int mul, int shift, int coeff_num) {
00517 int round = 1 << (shift - 1);
00518 int pos = -1;
00519 const uint8_t *scantab = h->scantable.permutated;
00520
00521
00522 while(--coeff_num >= 0){
00523 pos += run_buf[coeff_num];
00524 if(pos > 63) {
00525 av_log(h->s.avctx, AV_LOG_ERROR,
00526 "position out of block bounds at pic %d MB(%d,%d)\n",
00527 h->picture.poc, h->mbx, h->mby);
00528 return -1;
00529 }
00530 dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
00531 }
00532 return 0;
00533 }
00534
00544 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
00545 const struct dec_2dvlc *r, int esc_golomb_order,
00546 int qp, uint8_t *dst, int stride) {
00547 int i, esc_code, level, mask;
00548 unsigned int level_code, run;
00549 DCTELEM level_buf[65];
00550 uint8_t run_buf[65];
00551 DCTELEM *block = h->block;
00552
00553 for(i=0;i<65;i++) {
00554 level_code = get_ue_code(gb,r->golomb_order);
00555 if(level_code >= ESCAPE_CODE) {
00556 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
00557 if(run > 64)
00558 return -1;
00559 esc_code = get_ue_code(gb,esc_golomb_order);
00560 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
00561 while(level > r->inc_limit)
00562 r++;
00563 mask = -(level_code & 1);
00564 level = (level^mask) - mask;
00565 } else {
00566 level = r->rltab[level_code][0];
00567 if(!level)
00568 break;
00569 run = r->rltab[level_code][1];
00570 r += r->rltab[level_code][2];
00571 }
00572 level_buf[i] = level;
00573 run_buf[i] = run;
00574 }
00575 if (dequant(h, level_buf, run_buf, block, dequant_mul[qp],
00576 dequant_shift[qp], i))
00577 return -1;
00578 h->cdsp.cavs_idct8_add(dst,block,stride);
00579 h->s.dsp.clear_block(block);
00580 return 0;
00581 }
00582
00583
00584 static inline void decode_residual_chroma(AVSContext *h) {
00585 if(h->cbp & (1<<4))
00586 decode_residual_block(h, &h->s.gb, chroma_dec, 0,
00587 cavs_chroma_qp[h->qp], h->cu, h->c_stride);
00588 if(h->cbp & (1<<5))
00589 decode_residual_block(h, &h->s.gb, chroma_dec, 0,
00590 cavs_chroma_qp[h->qp], h->cv, h->c_stride);
00591 }
00592
00593 static inline int decode_residual_inter(AVSContext *h) {
00594 int block;
00595
00596
00597 int cbp= get_ue_golomb(&h->s.gb);
00598 if(cbp > 63U){
00599 av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
00600 return -1;
00601 }
00602 h->cbp = cbp_tab[cbp][1];
00603
00604
00605 if(h->cbp && !h->qp_fixed)
00606 h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
00607 for(block=0;block<4;block++)
00608 if(h->cbp & (1<<block))
00609 decode_residual_block(h, &h->s.gb, inter_dec, 0, h->qp,
00610 h->cy + h->luma_scan[block], h->l_stride);
00611 decode_residual_chroma(h);
00612
00613 return 0;
00614 }
00615
00616
00617
00618
00619
00620
00621
00622 static inline void set_mv_intra(AVSContext *h) {
00623 h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
00624 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
00625 h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
00626 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
00627 if(h->pic_type != AV_PICTURE_TYPE_B)
00628 h->col_type_base[h->mbidx] = I_8X8;
00629 }
00630
00631 static int decode_mb_i(AVSContext *h, int cbp_code) {
00632 GetBitContext *gb = &h->s.gb;
00633 unsigned pred_mode_uv;
00634 int block;
00635 uint8_t top[18];
00636 uint8_t *left = NULL;
00637 uint8_t *d;
00638
00639 ff_cavs_init_mb(h);
00640
00641
00642 for(block=0;block<4;block++) {
00643 int nA,nB,predpred;
00644 int pos = scan3x3[block];
00645
00646 nA = h->pred_mode_Y[pos-1];
00647 nB = h->pred_mode_Y[pos-3];
00648 predpred = FFMIN(nA,nB);
00649 if(predpred == NOT_AVAIL)
00650 predpred = INTRA_L_LP;
00651 if(!get_bits1(gb)){
00652 int rem_mode= get_bits(gb, 2);
00653 predpred = rem_mode + (rem_mode >= predpred);
00654 }
00655 h->pred_mode_Y[pos] = predpred;
00656 }
00657 pred_mode_uv = get_ue_golomb(gb);
00658 if(pred_mode_uv > 6) {
00659 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
00660 return -1;
00661 }
00662 ff_cavs_modify_mb_i(h, &pred_mode_uv);
00663
00664
00665 if(h->pic_type == AV_PICTURE_TYPE_I)
00666 cbp_code = get_ue_golomb(gb);
00667 if(cbp_code > 63U){
00668 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
00669 return -1;
00670 }
00671 h->cbp = cbp_tab[cbp_code][0];
00672 if(h->cbp && !h->qp_fixed)
00673 h->qp = (h->qp + get_se_golomb(gb)) & 63;
00674
00675
00676 for(block=0;block<4;block++) {
00677 d = h->cy + h->luma_scan[block];
00678 ff_cavs_load_intra_pred_luma(h, top, &left, block);
00679 h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
00680 (d, top, left, h->l_stride);
00681 if(h->cbp & (1<<block))
00682 decode_residual_block(h, gb, intra_dec, 1, h->qp, d, h->l_stride);
00683 }
00684
00685
00686 ff_cavs_load_intra_pred_chroma(h);
00687 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
00688 h->left_border_u, h->c_stride);
00689 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
00690 h->left_border_v, h->c_stride);
00691
00692 decode_residual_chroma(h);
00693 ff_cavs_filter(h,I_8X8);
00694 set_mv_intra(h);
00695 return 0;
00696 }
00697
00698 static inline void set_intra_mode_default(AVSContext *h) {
00699 if(h->stream_revision > 0) {
00700 h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
00701 h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
00702 } else {
00703 h->pred_mode_Y[3] = h->pred_mode_Y[6] = INTRA_L_LP;
00704 h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
00705 }
00706 }
00707
00708 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
00709 GetBitContext *gb = &h->s.gb;
00710 int ref[4];
00711
00712 ff_cavs_init_mb(h);
00713 switch(mb_type) {
00714 case P_SKIP:
00715 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
00716 break;
00717 case P_16X16:
00718 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00719 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
00720 break;
00721 case P_16X8:
00722 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00723 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
00724 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
00725 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
00726 break;
00727 case P_8X16:
00728 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00729 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
00730 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
00731 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
00732 break;
00733 case P_8X8:
00734 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00735 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
00736 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
00737 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
00738 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
00739 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
00740 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
00741 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
00742 }
00743 ff_cavs_inter(h, mb_type);
00744 set_intra_mode_default(h);
00745 store_mvs(h);
00746 if(mb_type != P_SKIP)
00747 decode_residual_inter(h);
00748 ff_cavs_filter(h,mb_type);
00749 h->col_type_base[h->mbidx] = mb_type;
00750 }
00751
00752 static int decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
00753 int block;
00754 enum cavs_sub_mb sub_type[4];
00755 int flags;
00756
00757 ff_cavs_init_mb(h);
00758
00759
00760 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
00761 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
00762 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
00763 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
00764 switch(mb_type) {
00765 case B_SKIP:
00766 case B_DIRECT:
00767 if(!h->col_type_base[h->mbidx]) {
00768
00769 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
00770 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
00771 } else
00772
00773 for(block=0;block<4;block++)
00774 mv_pred_direct(h,&h->mv[mv_scan[block]],
00775 &h->col_mv[h->mbidx*4 + block]);
00776 break;
00777 case B_FWD_16X16:
00778 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
00779 break;
00780 case B_SYM_16X16:
00781 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
00782 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
00783 break;
00784 case B_BWD_16X16:
00785 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
00786 break;
00787 case B_8X8:
00788 for(block=0;block<4;block++)
00789 sub_type[block] = get_bits(&h->s.gb,2);
00790 for(block=0;block<4;block++) {
00791 switch(sub_type[block]) {
00792 case B_SUB_DIRECT:
00793 if(!h->col_type_base[h->mbidx]) {
00794
00795 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
00796 MV_PRED_BSKIP, BLK_8X8, 1);
00797 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
00798 mv_scan[block]-3+MV_BWD_OFFS,
00799 MV_PRED_BSKIP, BLK_8X8, 0);
00800 } else
00801 mv_pred_direct(h,&h->mv[mv_scan[block]],
00802 &h->col_mv[h->mbidx*4 + block]);
00803 break;
00804 case B_SUB_FWD:
00805 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
00806 MV_PRED_MEDIAN, BLK_8X8, 1);
00807 break;
00808 case B_SUB_SYM:
00809 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
00810 MV_PRED_MEDIAN, BLK_8X8, 1);
00811 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
00812 break;
00813 }
00814 }
00815 for(block=0;block<4;block++) {
00816 if(sub_type[block] == B_SUB_BWD)
00817 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
00818 mv_scan[block]+MV_BWD_OFFS-3,
00819 MV_PRED_MEDIAN, BLK_8X8, 0);
00820 }
00821 break;
00822 default:
00823 if (mb_type <= B_SYM_16X16) {
00824 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mb_type %d in B frame\n", mb_type);
00825 return AVERROR_INVALIDDATA;
00826 }
00827 av_assert2(mb_type < B_8X8);
00828 flags = ff_cavs_partition_flags[mb_type];
00829 if(mb_type & 1) {
00830 if(flags & FWD0)
00831 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
00832 if(flags & SYM0)
00833 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
00834 if(flags & FWD1)
00835 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
00836 if(flags & SYM1)
00837 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
00838 if(flags & BWD0)
00839 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
00840 if(flags & BWD1)
00841 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
00842 } else {
00843 if(flags & FWD0)
00844 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
00845 if(flags & SYM0)
00846 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
00847 if(flags & FWD1)
00848 ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
00849 if(flags & SYM1)
00850 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
00851 if(flags & BWD0)
00852 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
00853 if(flags & BWD1)
00854 ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
00855 }
00856 }
00857 ff_cavs_inter(h, mb_type);
00858 set_intra_mode_default(h);
00859 if(mb_type != B_SKIP)
00860 decode_residual_inter(h);
00861 ff_cavs_filter(h,mb_type);
00862
00863 return 0;
00864 }
00865
00866
00867
00868
00869
00870
00871
00872 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
00873 if(h->stc > 0xAF)
00874 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
00875
00876 if (h->stc >= h->mb_height)
00877 return -1;
00878
00879 h->mby = h->stc;
00880 h->mbidx = h->mby*h->mb_width;
00881
00882
00883 h->flags &= ~(B_AVAIL|C_AVAIL);
00884 if((h->mby == 0) && (!h->qp_fixed)){
00885 h->qp_fixed = get_bits1(gb);
00886 h->qp = get_bits(gb,6);
00887 }
00888
00889 if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
00890 if(get_bits1(gb)) {
00891 av_log(h->s.avctx, AV_LOG_ERROR,
00892 "weighted prediction not yet supported\n");
00893 }
00894 return 0;
00895 }
00896
00897 static inline int check_for_slice(AVSContext *h) {
00898 GetBitContext *gb = &h->s.gb;
00899 int align;
00900
00901 if(h->mbx)
00902 return 0;
00903 align = (-get_bits_count(gb)) & 7;
00904
00905 if(!align && (show_bits(gb,8) == 0x80))
00906 align = 8;
00907 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
00908 skip_bits_long(gb,24+align);
00909 h->stc = get_bits(gb,8);
00910 if (h->stc >= h->mb_height)
00911 return 0;
00912 decode_slice_header(h,gb);
00913 return 1;
00914 }
00915 return 0;
00916 }
00917
00918
00919
00920
00921
00922
00923
00924 static int decode_pic(AVSContext *h) {
00925 MpegEncContext *s = &h->s;
00926 int skip_count = -1;
00927 enum cavs_mb mb_type;
00928
00929 if (!s->context_initialized) {
00930 if (ff_MPV_common_init(s) < 0)
00931 return -1;
00932 ff_init_scantable_permutation(s->dsp.idct_permutation,
00933 h->cdsp.idct_perm);
00934 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
00935 }
00936 skip_bits(&s->gb,16);
00937 if(h->stc == PIC_PB_START_CODE) {
00938 h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
00939 if(h->pic_type > AV_PICTURE_TYPE_B) {
00940 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
00941 return -1;
00942 }
00943
00944 if(!h->DPB[0].f.data[0] ||
00945 (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
00946 return -1;
00947 } else {
00948 h->pic_type = AV_PICTURE_TYPE_I;
00949 if(get_bits1(&s->gb))
00950 skip_bits(&s->gb,24);
00951
00952
00953 if (s->low_delay || !(show_bits(&s->gb,9) & 1))
00954 h->stream_revision = 1;
00955
00956 else if(show_bits(&s->gb,11) & 3)
00957 h->stream_revision = 1;
00958 if(h->stream_revision > 0)
00959 skip_bits(&s->gb,1);
00960 }
00961
00962 if(h->picture.f.data[0])
00963 s->avctx->release_buffer(s->avctx, &h->picture.f);
00964
00965 s->avctx->get_buffer(s->avctx, &h->picture.f);
00966 ff_cavs_init_pic(h);
00967 h->picture.poc = get_bits(&s->gb,8)*2;
00968
00969
00970 if(h->pic_type != AV_PICTURE_TYPE_B) {
00971 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
00972 } else {
00973 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
00974 }
00975 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
00976 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
00977 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
00978 if(h->pic_type == AV_PICTURE_TYPE_B) {
00979 h->sym_factor = h->dist[0]*h->scale_den[1];
00980 } else {
00981 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
00982 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
00983 }
00984
00985 if(s->low_delay)
00986 get_ue_golomb(&s->gb);
00987 h->progressive = get_bits1(&s->gb);
00988 h->pic_structure = 1;
00989 if(!h->progressive)
00990 h->pic_structure = get_bits1(&s->gb);
00991 if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
00992 skip_bits1(&s->gb);
00993 skip_bits1(&s->gb);
00994 skip_bits1(&s->gb);
00995 h->qp_fixed = get_bits1(&s->gb);
00996 h->qp = get_bits(&s->gb,6);
00997 if(h->pic_type == AV_PICTURE_TYPE_I) {
00998 if(!h->progressive && !h->pic_structure)
00999 skip_bits1(&s->gb);
01000 skip_bits(&s->gb,4);
01001 } else {
01002 if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
01003 h->ref_flag = get_bits1(&s->gb);
01004 skip_bits(&s->gb,4);
01005 h->skip_mode_flag = get_bits1(&s->gb);
01006 }
01007 h->loop_filter_disable = get_bits1(&s->gb);
01008 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
01009 h->alpha_offset = get_se_golomb(&s->gb);
01010 h->beta_offset = get_se_golomb(&s->gb);
01011 } else {
01012 h->alpha_offset = h->beta_offset = 0;
01013 }
01014 if(h->pic_type == AV_PICTURE_TYPE_I) {
01015 do {
01016 check_for_slice(h);
01017 decode_mb_i(h, 0);
01018 } while(ff_cavs_next_mb(h));
01019 } else if(h->pic_type == AV_PICTURE_TYPE_P) {
01020 do {
01021 if(check_for_slice(h))
01022 skip_count = -1;
01023 if(h->skip_mode_flag && (skip_count < 0))
01024 skip_count = get_ue_golomb(&s->gb);
01025 if(h->skip_mode_flag && skip_count--) {
01026 decode_mb_p(h,P_SKIP);
01027 } else {
01028 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
01029 if(mb_type > P_8X8)
01030 decode_mb_i(h, mb_type - P_8X8 - 1);
01031 else
01032 decode_mb_p(h,mb_type);
01033 }
01034 } while(ff_cavs_next_mb(h));
01035 } else {
01036 do {
01037 if(check_for_slice(h))
01038 skip_count = -1;
01039 if(h->skip_mode_flag && (skip_count < 0))
01040 skip_count = get_ue_golomb(&s->gb);
01041 if(h->skip_mode_flag && skip_count--) {
01042 decode_mb_b(h,B_SKIP);
01043 } else {
01044 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
01045 if(mb_type > B_8X8)
01046 decode_mb_i(h, mb_type - B_8X8 - 1);
01047 else
01048 decode_mb_b(h,mb_type);
01049 }
01050 } while(ff_cavs_next_mb(h));
01051 }
01052 if(h->pic_type != AV_PICTURE_TYPE_B) {
01053 if(h->DPB[1].f.data[0])
01054 s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
01055 h->DPB[1] = h->DPB[0];
01056 h->DPB[0] = h->picture;
01057 memset(&h->picture,0,sizeof(Picture));
01058 }
01059 return 0;
01060 }
01061
01062
01063
01064
01065
01066
01067
01068 static int decode_seq_header(AVSContext *h) {
01069 MpegEncContext *s = &h->s;
01070 int frame_rate_code;
01071 int width, height;
01072
01073 h->profile = get_bits(&s->gb,8);
01074 h->level = get_bits(&s->gb,8);
01075 skip_bits1(&s->gb);
01076 width = get_bits(&s->gb,14);
01077 height = get_bits(&s->gb,14);
01078 if ((s->width || s->height) && (s->width != width || s->height != height)) {
01079 av_log_missing_feature(s, "Width/height changing in CAVS is", 0);
01080 return -1;
01081 }
01082 if (width <= 0 || height <= 0) {
01083 av_log(s, AV_LOG_ERROR, "Dimensions invalid\n");
01084 return AVERROR_INVALIDDATA;
01085 }
01086 s->width = width;
01087 s->height = height;
01088 skip_bits(&s->gb,2);
01089 skip_bits(&s->gb,3);
01090 h->aspect_ratio = get_bits(&s->gb,4);
01091 frame_rate_code = get_bits(&s->gb,4);
01092 skip_bits(&s->gb,18);
01093 skip_bits1(&s->gb);
01094 skip_bits(&s->gb,12);
01095 s->low_delay = get_bits1(&s->gb);
01096 h->mb_width = (s->width + 15) >> 4;
01097 h->mb_height = (s->height + 15) >> 4;
01098 h->s.avctx->time_base.den = avpriv_frame_rate_tab[frame_rate_code].num;
01099 h->s.avctx->time_base.num = avpriv_frame_rate_tab[frame_rate_code].den;
01100 h->s.avctx->width = s->width;
01101 h->s.avctx->height = s->height;
01102 if(!h->top_qp)
01103 ff_cavs_init_top_lines(h);
01104 return 0;
01105 }
01106
01107 static void cavs_flush(AVCodecContext * avctx) {
01108 AVSContext *h = avctx->priv_data;
01109 h->got_keyframe = 0;
01110 }
01111
01112 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
01113 AVPacket *avpkt) {
01114 const uint8_t *buf = avpkt->data;
01115 int buf_size = avpkt->size;
01116 AVSContext *h = avctx->priv_data;
01117 MpegEncContext *s = &h->s;
01118 int input_size;
01119 const uint8_t *buf_end;
01120 const uint8_t *buf_ptr;
01121 AVFrame *picture = data;
01122 uint32_t stc = -1;
01123
01124 s->avctx = avctx;
01125
01126 if (buf_size == 0) {
01127 if (!s->low_delay && h->DPB[0].f.data[0]) {
01128 *data_size = sizeof(AVPicture);
01129 *picture = h->DPB[0].f;
01130 memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
01131 }
01132 return 0;
01133 }
01134
01135 buf_ptr = buf;
01136 buf_end = buf + buf_size;
01137 for(;;) {
01138 buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
01139 if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
01140 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
01141 input_size = (buf_end - buf_ptr)*8;
01142 switch(stc) {
01143 case CAVS_START_CODE:
01144 init_get_bits(&s->gb, buf_ptr, input_size);
01145 decode_seq_header(h);
01146 break;
01147 case PIC_I_START_CODE:
01148 if(!h->got_keyframe) {
01149 if(h->DPB[0].f.data[0])
01150 avctx->release_buffer(avctx, &h->DPB[0].f);
01151 if(h->DPB[1].f.data[0])
01152 avctx->release_buffer(avctx, &h->DPB[1].f);
01153 h->got_keyframe = 1;
01154 }
01155 case PIC_PB_START_CODE:
01156 *data_size = 0;
01157 if(!h->got_keyframe)
01158 break;
01159 if(!h->top_qp)
01160 break;
01161 init_get_bits(&s->gb, buf_ptr, input_size);
01162 h->stc = stc;
01163 if(decode_pic(h))
01164 break;
01165 *data_size = sizeof(AVPicture);
01166 if(h->pic_type != AV_PICTURE_TYPE_B) {
01167 if(h->DPB[1].f.data[0]) {
01168 *picture = h->DPB[1].f;
01169 } else {
01170 *data_size = 0;
01171 }
01172 } else
01173 *picture = h->picture.f;
01174 break;
01175 case EXT_START_CODE:
01176
01177 break;
01178 case USER_START_CODE:
01179
01180 break;
01181 default:
01182 if (stc <= SLICE_MAX_START_CODE) {
01183 init_get_bits(&s->gb, buf_ptr, input_size);
01184 decode_slice_header(h, &s->gb);
01185 }
01186 break;
01187 }
01188 }
01189 }
01190
01191 AVCodec ff_cavs_decoder = {
01192 .name = "cavs",
01193 .type = AVMEDIA_TYPE_VIDEO,
01194 .id = AV_CODEC_ID_CAVS,
01195 .priv_data_size = sizeof(AVSContext),
01196 .init = ff_cavs_init,
01197 .close = ff_cavs_end,
01198 .decode = cavs_decode_frame,
01199 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
01200 .flush = cavs_flush,
01201 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS (Audio Video Standard) (AVS1-P2, JiZhun profile)"),
01202 };