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