00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/internal.h"
00028
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "mpegvideo.h"
00032 #include "golomb.h"
00033 #include "internal.h"
00034 #include "mathops.h"
00035 #include "rectangle.h"
00036 #include "thread.h"
00037
00038 #include "rv34vlc.h"
00039 #include "rv34data.h"
00040 #include "rv34.h"
00041
00042
00043
00044 static inline void ZERO8x2(void* dst, int stride)
00045 {
00046 fill_rectangle(dst, 1, 2, stride, 0, 4);
00047 fill_rectangle(((uint8_t*)(dst))+4, 1, 2, stride, 0, 4);
00048 }
00049
00051 static const int rv34_mb_type_to_lavc[12] = {
00052 MB_TYPE_INTRA,
00053 MB_TYPE_INTRA16x16 | MB_TYPE_SEPARATE_DC,
00054 MB_TYPE_16x16 | MB_TYPE_L0,
00055 MB_TYPE_8x8 | MB_TYPE_L0,
00056 MB_TYPE_16x16 | MB_TYPE_L0,
00057 MB_TYPE_16x16 | MB_TYPE_L1,
00058 MB_TYPE_SKIP,
00059 MB_TYPE_DIRECT2 | MB_TYPE_16x16,
00060 MB_TYPE_16x8 | MB_TYPE_L0,
00061 MB_TYPE_8x16 | MB_TYPE_L0,
00062 MB_TYPE_16x16 | MB_TYPE_L0L1,
00063 MB_TYPE_16x16 | MB_TYPE_L0 | MB_TYPE_SEPARATE_DC
00064 };
00065
00066
00067 static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
00068
00069 static int rv34_decode_mv(RV34DecContext *r, int block_type);
00070
00076 static const int table_offs[] = {
00077 0, 1818, 3622, 4144, 4698, 5234, 5804, 5868, 5900, 5932,
00078 5996, 6252, 6316, 6348, 6380, 7674, 8944, 10274, 11668, 12250,
00079 14060, 15846, 16372, 16962, 17512, 18148, 18180, 18212, 18244, 18308,
00080 18564, 18628, 18660, 18692, 20036, 21314, 22648, 23968, 24614, 26384,
00081 28190, 28736, 29366, 29938, 30608, 30640, 30672, 30704, 30768, 31024,
00082 31088, 31120, 31184, 32570, 33898, 35236, 36644, 37286, 39020, 40802,
00083 41368, 42052, 42692, 43348, 43380, 43412, 43444, 43476, 43604, 43668,
00084 43700, 43732, 45100, 46430, 47778, 49160, 49802, 51550, 53340, 53972,
00085 54648, 55348, 55994, 56122, 56154, 56186, 56218, 56346, 56410, 56442,
00086 56474, 57878, 59290, 60636, 62036, 62682, 64460, 64524, 64588, 64716,
00087 64844, 66076, 67466, 67978, 68542, 69064, 69648, 70296, 72010, 72074,
00088 72138, 72202, 72330, 73572, 74936, 75454, 76030, 76566, 77176, 77822,
00089 79582, 79646, 79678, 79742, 79870, 81180, 82536, 83064, 83672, 84242,
00090 84934, 85576, 87384, 87448, 87480, 87544, 87672, 88982, 90340, 90902,
00091 91598, 92182, 92846, 93488, 95246, 95278, 95310, 95374, 95502, 96878,
00092 98266, 98848, 99542, 100234, 100884, 101524, 103320, 103352, 103384, 103416,
00093 103480, 104874, 106222, 106910, 107584, 108258, 108902, 109544, 111366, 111398,
00094 111430, 111462, 111494, 112878, 114320, 114988, 115660, 116310, 116950, 117592
00095 };
00096
00097 static VLC_TYPE table_data[117592][2];
00098
00107 static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms,
00108 const int num)
00109 {
00110 int i;
00111 int counts[17] = {0}, codes[17];
00112 uint16_t cw[MAX_VLC_SIZE], syms[MAX_VLC_SIZE];
00113 uint8_t bits2[MAX_VLC_SIZE];
00114 int maxbits = 0, realsize = 0;
00115
00116 for(i = 0; i < size; i++){
00117 if(bits[i]){
00118 bits2[realsize] = bits[i];
00119 syms[realsize] = insyms ? insyms[i] : i;
00120 realsize++;
00121 maxbits = FFMAX(maxbits, bits[i]);
00122 counts[bits[i]]++;
00123 }
00124 }
00125
00126 codes[0] = 0;
00127 for(i = 0; i < 16; i++)
00128 codes[i+1] = (codes[i] + counts[i]) << 1;
00129 for(i = 0; i < realsize; i++)
00130 cw[i] = codes[bits2[i]]++;
00131
00132 vlc->table = &table_data[table_offs[num]];
00133 vlc->table_allocated = table_offs[num + 1] - table_offs[num];
00134 ff_init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
00135 bits2, 1, 1,
00136 cw, 2, 2,
00137 syms, 2, 2, INIT_VLC_USE_NEW_STATIC);
00138 }
00139
00143 static av_cold void rv34_init_tables(void)
00144 {
00145 int i, j, k;
00146
00147 for(i = 0; i < NUM_INTRA_TABLES; i++){
00148 for(j = 0; j < 2; j++){
00149 rv34_gen_vlc(rv34_table_intra_cbppat [i][j], CBPPAT_VLC_SIZE, &intra_vlcs[i].cbppattern[j], NULL, 19*i + 0 + j);
00150 rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].second_pattern[j], NULL, 19*i + 2 + j);
00151 rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].third_pattern[j], NULL, 19*i + 4 + j);
00152 for(k = 0; k < 4; k++){
00153 rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2], CBP_VLC_SIZE, &intra_vlcs[i].cbp[j][k], rv34_cbp_code, 19*i + 6 + j*4 + k);
00154 }
00155 }
00156 for(j = 0; j < 4; j++){
00157 rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, &intra_vlcs[i].first_pattern[j], NULL, 19*i + 14 + j);
00158 }
00159 rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, &intra_vlcs[i].coefficient, NULL, 19*i + 18);
00160 }
00161
00162 for(i = 0; i < NUM_INTER_TABLES; i++){
00163 rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE, &inter_vlcs[i].cbppattern[0], NULL, i*12 + 95);
00164 for(j = 0; j < 4; j++){
00165 rv34_gen_vlc(rv34_inter_cbp[i][j], CBP_VLC_SIZE, &inter_vlcs[i].cbp[0][j], rv34_cbp_code, i*12 + 96 + j);
00166 }
00167 for(j = 0; j < 2; j++){
00168 rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE, &inter_vlcs[i].first_pattern[j], NULL, i*12 + 100 + j);
00169 rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].second_pattern[j], NULL, i*12 + 102 + j);
00170 rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].third_pattern[j], NULL, i*12 + 104 + j);
00171 }
00172 rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE, &inter_vlcs[i].coefficient, NULL, i*12 + 106);
00173 }
00174 }
00175
00177
00186 static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table)
00187 {
00188 int pattern, code, cbp=0;
00189 int ones;
00190 static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
00191 static const int shifts[4] = { 0, 2, 8, 10 };
00192 const int *curshift = shifts;
00193 int i, t, mask;
00194
00195 code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
00196 pattern = code & 0xF;
00197 code >>= 4;
00198
00199 ones = rv34_count_ones[pattern];
00200
00201 for(mask = 8; mask; mask >>= 1, curshift++){
00202 if(pattern & mask)
00203 cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
00204 }
00205
00206 for(i = 0; i < 4; i++){
00207 t = (modulo_three_table[code] >> (6 - 2*i)) & 3;
00208 if(t == 1)
00209 cbp |= cbp_masks[get_bits1(gb)] << i;
00210 if(t == 2)
00211 cbp |= cbp_masks[2] << i;
00212 }
00213 return cbp;
00214 }
00215
00219 static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc, int q)
00220 {
00221 if(coef){
00222 if(coef == esc){
00223 coef = get_vlc2(gb, vlc->table, 9, 2);
00224 if(coef > 23){
00225 coef -= 23;
00226 coef = 22 + ((1 << coef) | get_bits(gb, coef));
00227 }
00228 coef += esc;
00229 }
00230 if(get_bits1(gb))
00231 coef = -coef;
00232 *dst = (coef*q + 8) >> 4;
00233 }
00234 }
00235
00239 static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q)
00240 {
00241 int flags = modulo_three_table[code];
00242
00243 decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q);
00244 if(is_block2){
00245 decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
00246 decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q);
00247 }else{
00248 decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q);
00249 decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q);
00250 }
00251 decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
00252 }
00253
00257 static inline void decode_subblock1(DCTELEM *dst, int code, GetBitContext *gb, VLC *vlc, int q)
00258 {
00259 int coeff = modulo_three_table[code] >> 6;
00260 decode_coeff(dst, coeff, 3, gb, vlc, q);
00261 }
00262
00263 static inline void decode_subblock3(DCTELEM *dst, int code, GetBitContext *gb, VLC *vlc,
00264 int q_dc, int q_ac1, int q_ac2)
00265 {
00266 int flags = modulo_three_table[code];
00267
00268 decode_coeff(dst+0*4+0, (flags >> 6) , 3, gb, vlc, q_dc);
00269 decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
00270 decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
00271 decode_coeff(dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q_ac2);
00272 }
00273
00285 static int rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2)
00286 {
00287 int code, pattern, has_ac = 1;
00288
00289 code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
00290
00291 pattern = code & 0x7;
00292
00293 code >>= 3;
00294
00295 if (modulo_three_table[code] & 0x3F) {
00296 decode_subblock3(dst, code, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2);
00297 } else {
00298 decode_subblock1(dst, code, gb, &rvlc->coefficient, q_dc);
00299 if (!pattern)
00300 return 0;
00301 has_ac = 0;
00302 }
00303
00304 if(pattern & 4){
00305 code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
00306 decode_subblock(dst + 4*0+2, code, 0, gb, &rvlc->coefficient, q_ac2);
00307 }
00308 if(pattern & 2){
00309 code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
00310 decode_subblock(dst + 4*2+0, code, 1, gb, &rvlc->coefficient, q_ac2);
00311 }
00312 if(pattern & 1){
00313 code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
00314 decode_subblock(dst + 4*2+2, code, 0, gb, &rvlc->coefficient, q_ac2);
00315 }
00316 return has_ac | pattern;
00317 }
00318
00328 int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
00329 {
00330 int i;
00331 for(i = 0; i < 5; i++)
00332 if(rv34_mb_max_sizes[i] >= mb_size - 1)
00333 break;
00334 return rv34_mb_bits_sizes[i];
00335 }
00336
00340 static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
00341 {
00342 if(mod == 2 && quant < 19) quant += 10;
00343 else if(mod && quant < 26) quant += 5;
00344 return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
00345 : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
00346 }
00347
00351 static int rv34_decode_intra_mb_header(RV34DecContext *r, int8_t *intra_types)
00352 {
00353 MpegEncContext *s = &r->s;
00354 GetBitContext *gb = &s->gb;
00355 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00356 int t;
00357
00358 r->is16 = get_bits1(gb);
00359 if(r->is16){
00360 s->current_picture_ptr->f.mb_type[mb_pos] = MB_TYPE_INTRA16x16;
00361 r->block_type = RV34_MB_TYPE_INTRA16x16;
00362 t = get_bits(gb, 2);
00363 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
00364 r->luma_vlc = 2;
00365 }else{
00366 if(!r->rv30){
00367 if(!get_bits1(gb))
00368 av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
00369 }
00370 s->current_picture_ptr->f.mb_type[mb_pos] = MB_TYPE_INTRA;
00371 r->block_type = RV34_MB_TYPE_INTRA;
00372 if(r->decode_intra_types(r, gb, intra_types) < 0)
00373 return -1;
00374 r->luma_vlc = 1;
00375 }
00376
00377 r->chroma_vlc = 0;
00378 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
00379
00380 return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
00381 }
00382
00386 static int rv34_decode_inter_mb_header(RV34DecContext *r, int8_t *intra_types)
00387 {
00388 MpegEncContext *s = &r->s;
00389 GetBitContext *gb = &s->gb;
00390 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00391 int i, t;
00392
00393 r->block_type = r->decode_mb_info(r);
00394 if(r->block_type == -1)
00395 return -1;
00396 s->current_picture_ptr->f.mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
00397 r->mb_type[mb_pos] = r->block_type;
00398 if(r->block_type == RV34_MB_SKIP){
00399 if(s->pict_type == AV_PICTURE_TYPE_P)
00400 r->mb_type[mb_pos] = RV34_MB_P_16x16;
00401 if(s->pict_type == AV_PICTURE_TYPE_B)
00402 r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
00403 }
00404 r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->f.mb_type[mb_pos]);
00405 rv34_decode_mv(r, r->block_type);
00406 if(r->block_type == RV34_MB_SKIP){
00407 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
00408 return 0;
00409 }
00410 r->chroma_vlc = 1;
00411 r->luma_vlc = 0;
00412
00413 if(IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
00414 if(r->is16){
00415 t = get_bits(gb, 2);
00416 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
00417 r->luma_vlc = 2;
00418 }else{
00419 if(r->decode_intra_types(r, gb, intra_types) < 0)
00420 return -1;
00421 r->luma_vlc = 1;
00422 }
00423 r->chroma_vlc = 0;
00424 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
00425 }else{
00426 for(i = 0; i < 16; i++)
00427 intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
00428 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
00429 if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
00430 r->is16 = 1;
00431 r->chroma_vlc = 1;
00432 r->luma_vlc = 2;
00433 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
00434 }
00435 }
00436
00437 return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
00438 }
00439
00441
00448 static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
00449
00451 static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
00452
00454 static const uint8_t avail_indexes[4] = { 6, 7, 10, 11 };
00455
00463 static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
00464 {
00465 MpegEncContext *s = &r->s;
00466 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00467 int A[2] = {0}, B[2], C[2];
00468 int i, j;
00469 int mx, my;
00470 int* avail = r->avail_cache + avail_indexes[subblock_no];
00471 int c_off = part_sizes_w[block_type];
00472
00473 mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
00474 if(subblock_no == 3)
00475 c_off = -1;
00476
00477 if(avail[-1]){
00478 A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][0];
00479 A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][1];
00480 }
00481 if(avail[-4]){
00482 B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][0];
00483 B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][1];
00484 }else{
00485 B[0] = A[0];
00486 B[1] = A[1];
00487 }
00488 if(!avail[c_off-4]){
00489 if(avail[-4] && (avail[-1] || r->rv30)){
00490 C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][0];
00491 C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][1];
00492 }else{
00493 C[0] = A[0];
00494 C[1] = A[1];
00495 }
00496 }else{
00497 C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][0];
00498 C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][1];
00499 }
00500 mx = mid_pred(A[0], B[0], C[0]);
00501 my = mid_pred(A[1], B[1], C[1]);
00502 mx += r->dmv[dmv_no][0];
00503 my += r->dmv[dmv_no][1];
00504 for(j = 0; j < part_sizes_h[block_type]; j++){
00505 for(i = 0; i < part_sizes_w[block_type]; i++){
00506 s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
00507 s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
00508 }
00509 }
00510 }
00511
00512 #define GET_PTS_DIFF(a, b) ((a - b + 8192) & 0x1FFF)
00513
00517 static int calc_add_mv(RV34DecContext *r, int dir, int val)
00518 {
00519 int mul = dir ? -r->mv_weight2 : r->mv_weight1;
00520
00521 return (val * mul + 0x2000) >> 14;
00522 }
00523
00527 static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
00528 int A_avail, int B_avail, int C_avail,
00529 int *mx, int *my)
00530 {
00531 if(A_avail + B_avail + C_avail != 3){
00532 *mx = A[0] + B[0] + C[0];
00533 *my = A[1] + B[1] + C[1];
00534 if(A_avail + B_avail + C_avail == 2){
00535 *mx /= 2;
00536 *my /= 2;
00537 }
00538 }else{
00539 *mx = mid_pred(A[0], B[0], C[0]);
00540 *my = mid_pred(A[1], B[1], C[1]);
00541 }
00542 }
00543
00547 static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
00548 {
00549 MpegEncContext *s = &r->s;
00550 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00551 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00552 int A[2] = { 0 }, B[2] = { 0 }, C[2] = { 0 };
00553 int has_A = 0, has_B = 0, has_C = 0;
00554 int mx, my;
00555 int i, j;
00556 Picture *cur_pic = s->current_picture_ptr;
00557 const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
00558 int type = cur_pic->f.mb_type[mb_pos];
00559
00560 if((r->avail_cache[6-1] & type) & mask){
00561 A[0] = cur_pic->f.motion_val[dir][mv_pos - 1][0];
00562 A[1] = cur_pic->f.motion_val[dir][mv_pos - 1][1];
00563 has_A = 1;
00564 }
00565 if((r->avail_cache[6-4] & type) & mask){
00566 B[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][0];
00567 B[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][1];
00568 has_B = 1;
00569 }
00570 if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
00571 C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][0];
00572 C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][1];
00573 has_C = 1;
00574 }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
00575 C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][0];
00576 C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][1];
00577 has_C = 1;
00578 }
00579
00580 rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
00581
00582 mx += r->dmv[dir][0];
00583 my += r->dmv[dir][1];
00584
00585 for(j = 0; j < 2; j++){
00586 for(i = 0; i < 2; i++){
00587 cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
00588 cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
00589 }
00590 }
00591 if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
00592 ZERO8x2(cur_pic->f.motion_val[!dir][mv_pos], s->b8_stride);
00593 }
00594 }
00595
00599 static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
00600 {
00601 MpegEncContext *s = &r->s;
00602 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00603 int A[2] = {0}, B[2], C[2];
00604 int i, j, k;
00605 int mx, my;
00606 int* avail = r->avail_cache + avail_indexes[0];
00607
00608 if(avail[-1]){
00609 A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][0];
00610 A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][1];
00611 }
00612 if(avail[-4]){
00613 B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][0];
00614 B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][1];
00615 }else{
00616 B[0] = A[0];
00617 B[1] = A[1];
00618 }
00619 if(!avail[-4 + 2]){
00620 if(avail[-4] && (avail[-1])){
00621 C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][0];
00622 C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][1];
00623 }else{
00624 C[0] = A[0];
00625 C[1] = A[1];
00626 }
00627 }else{
00628 C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][0];
00629 C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][1];
00630 }
00631 mx = mid_pred(A[0], B[0], C[0]);
00632 my = mid_pred(A[1], B[1], C[1]);
00633 mx += r->dmv[0][0];
00634 my += r->dmv[0][1];
00635 for(j = 0; j < 2; j++){
00636 for(i = 0; i < 2; i++){
00637 for(k = 0; k < 2; k++){
00638 s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
00639 s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
00640 }
00641 }
00642 }
00643 }
00644
00645 static const int chroma_coeffs[3] = { 0, 3, 5 };
00646
00662 static inline void rv34_mc(RV34DecContext *r, const int block_type,
00663 const int xoff, const int yoff, int mv_off,
00664 const int width, const int height, int dir,
00665 const int thirdpel, int weighted,
00666 qpel_mc_func (*qpel_mc)[16],
00667 h264_chroma_mc_func (*chroma_mc))
00668 {
00669 MpegEncContext *s = &r->s;
00670 uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
00671 int dxy, mx, my, umx, umy, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
00672 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
00673 int is16x16 = 1;
00674
00675 if(thirdpel){
00676 int chroma_mx, chroma_my;
00677 mx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
00678 my = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
00679 lx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
00680 ly = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
00681 chroma_mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
00682 chroma_my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
00683 umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
00684 umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
00685 uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
00686 uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
00687 }else{
00688 int cx, cy;
00689 mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] >> 2;
00690 my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] >> 2;
00691 lx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] & 3;
00692 ly = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] & 3;
00693 cx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
00694 cy = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
00695 umx = cx >> 2;
00696 umy = cy >> 2;
00697 uvmx = (cx & 3) << 1;
00698 uvmy = (cy & 3) << 1;
00699
00700 if(uvmx == 6 && uvmy == 6)
00701 uvmx = uvmy = 4;
00702 }
00703
00704 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
00705
00706 int mb_row = s->mb_y + ((yoff + my + 5 + 8 * height) >> 4);
00707 AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f;
00708 ff_thread_await_progress(f, mb_row, 0);
00709 }
00710
00711 dxy = ly*4 + lx;
00712 srcY = dir ? s->next_picture_ptr->f.data[0] : s->last_picture_ptr->f.data[0];
00713 srcU = dir ? s->next_picture_ptr->f.data[1] : s->last_picture_ptr->f.data[1];
00714 srcV = dir ? s->next_picture_ptr->f.data[2] : s->last_picture_ptr->f.data[2];
00715 src_x = s->mb_x * 16 + xoff + mx;
00716 src_y = s->mb_y * 16 + yoff + my;
00717 uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
00718 uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
00719 srcY += src_y * s->linesize + src_x;
00720 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
00721 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
00722 if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
00723 (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
00724 (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
00725 uint8_t *uvbuf = s->edge_emu_buffer + 22 * s->linesize;
00726
00727 srcY -= 2 + 2*s->linesize;
00728 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+6, (height<<3)+6,
00729 src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
00730 srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
00731 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,
00732 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
00733 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,
00734 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
00735 srcU = uvbuf;
00736 srcV = uvbuf + 16;
00737 }
00738 if(!weighted){
00739 Y = s->dest[0] + xoff + yoff *s->linesize;
00740 U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
00741 V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
00742 }else{
00743 Y = r->tmp_b_block_y [dir] + xoff + yoff *s->linesize;
00744 U = r->tmp_b_block_uv[dir*2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
00745 V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
00746 }
00747
00748 if(block_type == RV34_MB_P_16x8){
00749 qpel_mc[1][dxy](Y, srcY, s->linesize);
00750 Y += 8;
00751 srcY += 8;
00752 }else if(block_type == RV34_MB_P_8x16){
00753 qpel_mc[1][dxy](Y, srcY, s->linesize);
00754 Y += 8 * s->linesize;
00755 srcY += 8 * s->linesize;
00756 }
00757 is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
00758 qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
00759 chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
00760 chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
00761 }
00762
00763 static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
00764 const int xoff, const int yoff, int mv_off,
00765 const int width, const int height, int dir)
00766 {
00767 rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
00768 r->rdsp.put_pixels_tab,
00769 r->rdsp.put_chroma_pixels_tab);
00770 }
00771
00772 static void rv4_weight(RV34DecContext *r)
00773 {
00774 r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][0](r->s.dest[0],
00775 r->tmp_b_block_y[0],
00776 r->tmp_b_block_y[1],
00777 r->weight1,
00778 r->weight2,
00779 r->s.linesize);
00780 r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[1],
00781 r->tmp_b_block_uv[0],
00782 r->tmp_b_block_uv[2],
00783 r->weight1,
00784 r->weight2,
00785 r->s.uvlinesize);
00786 r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[2],
00787 r->tmp_b_block_uv[1],
00788 r->tmp_b_block_uv[3],
00789 r->weight1,
00790 r->weight2,
00791 r->s.uvlinesize);
00792 }
00793
00794 static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
00795 {
00796 int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
00797
00798 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
00799 r->rdsp.put_pixels_tab,
00800 r->rdsp.put_chroma_pixels_tab);
00801 if(!weighted){
00802 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
00803 r->rdsp.avg_pixels_tab,
00804 r->rdsp.avg_chroma_pixels_tab);
00805 }else{
00806 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
00807 r->rdsp.put_pixels_tab,
00808 r->rdsp.put_chroma_pixels_tab);
00809 rv4_weight(r);
00810 }
00811 }
00812
00813 static void rv34_mc_2mv_skip(RV34DecContext *r)
00814 {
00815 int i, j;
00816 int weighted = !r->rv30 && r->weight1 != 8192;
00817
00818 for(j = 0; j < 2; j++)
00819 for(i = 0; i < 2; i++){
00820 rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
00821 weighted,
00822 r->rdsp.put_pixels_tab,
00823 r->rdsp.put_chroma_pixels_tab);
00824 rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
00825 weighted,
00826 weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
00827 weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
00828 }
00829 if(weighted)
00830 rv4_weight(r);
00831 }
00832
00834 static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
00835
00840 static int rv34_decode_mv(RV34DecContext *r, int block_type)
00841 {
00842 MpegEncContext *s = &r->s;
00843 GetBitContext *gb = &s->gb;
00844 int i, j, k, l;
00845 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00846 int next_bt;
00847
00848 memset(r->dmv, 0, sizeof(r->dmv));
00849 for(i = 0; i < num_mvs[block_type]; i++){
00850 r->dmv[i][0] = svq3_get_se_golomb(gb);
00851 r->dmv[i][1] = svq3_get_se_golomb(gb);
00852 }
00853 switch(block_type){
00854 case RV34_MB_TYPE_INTRA:
00855 case RV34_MB_TYPE_INTRA16x16:
00856 ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
00857 return 0;
00858 case RV34_MB_SKIP:
00859 if(s->pict_type == AV_PICTURE_TYPE_P){
00860 ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
00861 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
00862 break;
00863 }
00864 case RV34_MB_B_DIRECT:
00865
00866
00867 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
00868 ff_thread_await_progress(&s->next_picture_ptr->f, FFMAX(0, s->mb_y-1), 0);
00869
00870 next_bt = s->next_picture_ptr->f.mb_type[s->mb_x + s->mb_y * s->mb_stride];
00871 if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
00872 ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
00873 ZERO8x2(s->current_picture_ptr->f.motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
00874 }else
00875 for(j = 0; j < 2; j++)
00876 for(i = 0; i < 2; i++)
00877 for(k = 0; k < 2; k++)
00878 for(l = 0; l < 2; l++)
00879 s->current_picture_ptr->f.motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][k]);
00880 if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt)))
00881 rv34_mc_2mv(r, block_type);
00882 else
00883 rv34_mc_2mv_skip(r);
00884 ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
00885 break;
00886 case RV34_MB_P_16x16:
00887 case RV34_MB_P_MIX16x16:
00888 rv34_pred_mv(r, block_type, 0, 0);
00889 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
00890 break;
00891 case RV34_MB_B_FORWARD:
00892 case RV34_MB_B_BACKWARD:
00893 r->dmv[1][0] = r->dmv[0][0];
00894 r->dmv[1][1] = r->dmv[0][1];
00895 if(r->rv30)
00896 rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
00897 else
00898 rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
00899 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
00900 break;
00901 case RV34_MB_P_16x8:
00902 case RV34_MB_P_8x16:
00903 rv34_pred_mv(r, block_type, 0, 0);
00904 rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
00905 if(block_type == RV34_MB_P_16x8){
00906 rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
00907 rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
00908 }
00909 if(block_type == RV34_MB_P_8x16){
00910 rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
00911 rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
00912 }
00913 break;
00914 case RV34_MB_B_BIDIR:
00915 rv34_pred_mv_b (r, block_type, 0);
00916 rv34_pred_mv_b (r, block_type, 1);
00917 rv34_mc_2mv (r, block_type);
00918 break;
00919 case RV34_MB_P_8x8:
00920 for(i=0;i< 4;i++){
00921 rv34_pred_mv(r, block_type, i, i);
00922 rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
00923 }
00924 break;
00925 }
00926
00927 return 0;
00928 }
00930
00936 static const int ittrans[9] = {
00937 DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
00938 VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
00939 };
00940
00942 static const int ittrans16[4] = {
00943 DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
00944 };
00945
00949 static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
00950 {
00951 uint8_t *prev = dst - stride + 4;
00952 uint32_t topleft;
00953
00954 if(!up && !left)
00955 itype = DC_128_PRED;
00956 else if(!up){
00957 if(itype == VERT_PRED) itype = HOR_PRED;
00958 if(itype == DC_PRED) itype = LEFT_DC_PRED;
00959 }else if(!left){
00960 if(itype == HOR_PRED) itype = VERT_PRED;
00961 if(itype == DC_PRED) itype = TOP_DC_PRED;
00962 if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
00963 }
00964 if(!down){
00965 if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
00966 if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
00967 if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
00968 }
00969 if(!right && up){
00970 topleft = dst[-stride + 3] * 0x01010101u;
00971 prev = (uint8_t*)&topleft;
00972 }
00973 r->h.pred4x4[itype](dst, prev, stride);
00974 }
00975
00976 static inline int adjust_pred16(int itype, int up, int left)
00977 {
00978 if(!up && !left)
00979 itype = DC_128_PRED8x8;
00980 else if(!up){
00981 if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
00982 if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
00983 if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
00984 }else if(!left){
00985 if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
00986 if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
00987 if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
00988 }
00989 return itype;
00990 }
00991
00992 static inline void rv34_process_block(RV34DecContext *r,
00993 uint8_t *pdst, int stride,
00994 int fc, int sc, int q_dc, int q_ac)
00995 {
00996 MpegEncContext *s = &r->s;
00997 DCTELEM *ptr = s->block[0];
00998 int has_ac = rv34_decode_block(ptr, &s->gb, r->cur_vlcs,
00999 fc, sc, q_dc, q_ac, q_ac);
01000 if(has_ac){
01001 r->rdsp.rv34_idct_add(pdst, stride, ptr);
01002 }else{
01003 r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
01004 ptr[0] = 0;
01005 }
01006 }
01007
01008 static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
01009 {
01010 LOCAL_ALIGNED_16(DCTELEM, block16, [16]);
01011 MpegEncContext *s = &r->s;
01012 GetBitContext *gb = &s->gb;
01013 int q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
01014 q_ac = rv34_qscale_tab[s->qscale];
01015 uint8_t *dst = s->dest[0];
01016 DCTELEM *ptr = s->block[0];
01017 int i, j, itype, has_ac;
01018
01019 memset(block16, 0, 16 * sizeof(*block16));
01020
01021 has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
01022 if(has_ac)
01023 r->rdsp.rv34_inv_transform(block16);
01024 else
01025 r->rdsp.rv34_inv_transform_dc(block16);
01026
01027 itype = ittrans16[intra_types[0]];
01028 itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
01029 r->h.pred16x16[itype](dst, s->linesize);
01030
01031 for(j = 0; j < 4; j++){
01032 for(i = 0; i < 4; i++, cbp >>= 1){
01033 int dc = block16[i + j*4];
01034
01035 if(cbp & 1){
01036 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
01037 }else
01038 has_ac = 0;
01039
01040 if(has_ac){
01041 ptr[0] = dc;
01042 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
01043 }else
01044 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
01045 }
01046
01047 dst += 4*s->linesize;
01048 }
01049
01050 itype = ittrans16[intra_types[0]];
01051 if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
01052 itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
01053
01054 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
01055 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
01056
01057 for(j = 1; j < 3; j++){
01058 dst = s->dest[j];
01059 r->h.pred8x8[itype](dst, s->uvlinesize);
01060 for(i = 0; i < 4; i++, cbp >>= 1){
01061 uint8_t *pdst;
01062 if(!(cbp & 1)) continue;
01063 pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
01064
01065 rv34_process_block(r, pdst, s->uvlinesize,
01066 r->chroma_vlc, 1, q_dc, q_ac);
01067 }
01068 }
01069 }
01070
01071 static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
01072 {
01073 MpegEncContext *s = &r->s;
01074 uint8_t *dst = s->dest[0];
01075 int avail[6*8] = {0};
01076 int i, j, k;
01077 int idx, q_ac, q_dc;
01078
01079
01080 if(r->avail_cache[1])
01081 avail[0] = 1;
01082 if(r->avail_cache[2])
01083 avail[1] = avail[2] = 1;
01084 if(r->avail_cache[3])
01085 avail[3] = avail[4] = 1;
01086 if(r->avail_cache[4])
01087 avail[5] = 1;
01088 if(r->avail_cache[5])
01089 avail[8] = avail[16] = 1;
01090 if(r->avail_cache[9])
01091 avail[24] = avail[32] = 1;
01092
01093 q_ac = rv34_qscale_tab[s->qscale];
01094 for(j = 0; j < 4; j++){
01095 idx = 9 + j*8;
01096 for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
01097 rv34_pred_4x4_block(r, dst, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
01098 avail[idx] = 1;
01099 if(!(cbp & 1)) continue;
01100
01101 rv34_process_block(r, dst, s->linesize,
01102 r->luma_vlc, 0, q_ac, q_ac);
01103 }
01104 dst += s->linesize * 4 - 4*4;
01105 intra_types += r->intra_types_stride;
01106 }
01107
01108 intra_types -= r->intra_types_stride * 4;
01109
01110 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
01111 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
01112
01113 for(k = 0; k < 2; k++){
01114 dst = s->dest[1+k];
01115 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
01116
01117 for(j = 0; j < 2; j++){
01118 int* acache = r->avail_cache + 6 + j*4;
01119 for(i = 0; i < 2; i++, cbp >>= 1, acache++){
01120 int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
01121 rv34_pred_4x4_block(r, dst+4*i, s->uvlinesize, itype, acache[-4], acache[-1], !i && !j, acache[-3]);
01122 acache[0] = 1;
01123
01124 if(!(cbp&1)) continue;
01125
01126 rv34_process_block(r, dst + 4*i, s->uvlinesize,
01127 r->chroma_vlc, 1, q_dc, q_ac);
01128 }
01129
01130 dst += 4*s->uvlinesize;
01131 }
01132 }
01133 }
01134
01135 static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
01136 {
01137 int d;
01138 d = motion_val[0][0] - motion_val[-step][0];
01139 if(d < -3 || d > 3)
01140 return 1;
01141 d = motion_val[0][1] - motion_val[-step][1];
01142 if(d < -3 || d > 3)
01143 return 1;
01144 return 0;
01145 }
01146
01147 static int rv34_set_deblock_coef(RV34DecContext *r)
01148 {
01149 MpegEncContext *s = &r->s;
01150 int hmvmask = 0, vmvmask = 0, i, j;
01151 int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
01152 int16_t (*motion_val)[2] = &s->current_picture_ptr->f.motion_val[0][midx];
01153 for(j = 0; j < 16; j += 8){
01154 for(i = 0; i < 2; i++){
01155 if(is_mv_diff_gt_3(motion_val + i, 1))
01156 vmvmask |= 0x11 << (j + i*2);
01157 if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
01158 hmvmask |= 0x03 << (j + i*2);
01159 }
01160 motion_val += s->b8_stride;
01161 }
01162 if(s->first_slice_line)
01163 hmvmask &= ~0x000F;
01164 if(!s->mb_x)
01165 vmvmask &= ~0x1111;
01166 if(r->rv30){
01167 vmvmask |= (vmvmask & 0x4444) >> 1;
01168 hmvmask |= (hmvmask & 0x0F00) >> 4;
01169 if(s->mb_x)
01170 r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
01171 if(!s->first_slice_line)
01172 r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
01173 }
01174 return hmvmask | vmvmask;
01175 }
01176
01177 static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
01178 {
01179 MpegEncContext *s = &r->s;
01180 GetBitContext *gb = &s->gb;
01181 uint8_t *dst = s->dest[0];
01182 DCTELEM *ptr = s->block[0];
01183 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
01184 int cbp, cbp2;
01185 int q_dc, q_ac, has_ac;
01186 int i, j;
01187 int dist;
01188
01189
01190 memset(r->avail_cache, 0, sizeof(r->avail_cache));
01191 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
01192 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
01193 if(s->mb_x && dist)
01194 r->avail_cache[5] =
01195 r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
01196 if(dist >= s->mb_width)
01197 r->avail_cache[2] =
01198 r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
01199 if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
01200 r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
01201 if(s->mb_x && dist > s->mb_width)
01202 r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
01203
01204 s->qscale = r->si.quant;
01205 cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
01206 r->cbp_luma [mb_pos] = cbp;
01207 r->cbp_chroma[mb_pos] = cbp >> 16;
01208 r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
01209 s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
01210
01211 if(cbp == -1)
01212 return -1;
01213
01214 if (IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
01215 if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
01216 else rv34_output_intra(r, intra_types, cbp);
01217 return 0;
01218 }
01219
01220 if(r->is16){
01221
01222 LOCAL_ALIGNED_16(DCTELEM, block16, [16]);
01223 memset(block16, 0, 16 * sizeof(*block16));
01224 q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
01225 q_ac = rv34_qscale_tab[s->qscale];
01226 if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac))
01227 r->rdsp.rv34_inv_transform(block16);
01228 else
01229 r->rdsp.rv34_inv_transform_dc(block16);
01230
01231 q_ac = rv34_qscale_tab[s->qscale];
01232
01233 for(j = 0; j < 4; j++){
01234 for(i = 0; i < 4; i++, cbp >>= 1){
01235 int dc = block16[i + j*4];
01236
01237 if(cbp & 1){
01238 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
01239 }else
01240 has_ac = 0;
01241
01242 if(has_ac){
01243 ptr[0] = dc;
01244 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
01245 }else
01246 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
01247 }
01248
01249 dst += 4*s->linesize;
01250 }
01251
01252 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
01253 }else{
01254 q_ac = rv34_qscale_tab[s->qscale];
01255
01256 for(j = 0; j < 4; j++){
01257 for(i = 0; i < 4; i++, cbp >>= 1){
01258 if(!(cbp & 1)) continue;
01259
01260 rv34_process_block(r, dst + 4*i, s->linesize,
01261 r->luma_vlc, 0, q_ac, q_ac);
01262 }
01263 dst += 4*s->linesize;
01264 }
01265 }
01266
01267 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
01268 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
01269
01270 for(j = 1; j < 3; j++){
01271 dst = s->dest[j];
01272 for(i = 0; i < 4; i++, cbp >>= 1){
01273 uint8_t *pdst;
01274 if(!(cbp & 1)) continue;
01275 pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
01276
01277 rv34_process_block(r, pdst, s->uvlinesize,
01278 r->chroma_vlc, 1, q_dc, q_ac);
01279 }
01280 }
01281
01282 return 0;
01283 }
01284
01285 static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
01286 {
01287 MpegEncContext *s = &r->s;
01288 int cbp, dist;
01289 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
01290
01291
01292 memset(r->avail_cache, 0, sizeof(r->avail_cache));
01293 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
01294 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
01295 if(s->mb_x && dist)
01296 r->avail_cache[5] =
01297 r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
01298 if(dist >= s->mb_width)
01299 r->avail_cache[2] =
01300 r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
01301 if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
01302 r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
01303 if(s->mb_x && dist > s->mb_width)
01304 r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
01305
01306 s->qscale = r->si.quant;
01307 cbp = rv34_decode_intra_mb_header(r, intra_types);
01308 r->cbp_luma [mb_pos] = cbp;
01309 r->cbp_chroma[mb_pos] = cbp >> 16;
01310 r->deblock_coefs[mb_pos] = 0xFFFF;
01311 s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
01312
01313 if(cbp == -1)
01314 return -1;
01315
01316 if(r->is16){
01317 rv34_output_i16x16(r, intra_types, cbp);
01318 return 0;
01319 }
01320
01321 rv34_output_intra(r, intra_types, cbp);
01322 return 0;
01323 }
01324
01325 static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
01326 {
01327 int bits;
01328 if(s->mb_y >= s->mb_height)
01329 return 1;
01330 if(!s->mb_num_left)
01331 return 1;
01332 if(r->s.mb_skip_run > 1)
01333 return 0;
01334 bits = get_bits_left(&s->gb);
01335 if(bits <= 0 || (bits < 8 && !show_bits(&s->gb, bits)))
01336 return 1;
01337 return 0;
01338 }
01339
01340
01341 static void rv34_decoder_free(RV34DecContext *r)
01342 {
01343 av_freep(&r->intra_types_hist);
01344 r->intra_types = NULL;
01345 av_freep(&r->tmp_b_block_base);
01346 av_freep(&r->mb_type);
01347 av_freep(&r->cbp_luma);
01348 av_freep(&r->cbp_chroma);
01349 av_freep(&r->deblock_coefs);
01350 }
01351
01352
01353 static int rv34_decoder_alloc(RV34DecContext *r)
01354 {
01355 r->intra_types_stride = r->s.mb_width * 4 + 4;
01356
01357 r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height *
01358 sizeof(*r->cbp_chroma));
01359 r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height *
01360 sizeof(*r->cbp_luma));
01361 r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height *
01362 sizeof(*r->deblock_coefs));
01363 r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
01364 sizeof(*r->intra_types_hist));
01365 r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height *
01366 sizeof(*r->mb_type));
01367
01368 if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs &&
01369 r->intra_types_hist && r->mb_type)) {
01370 rv34_decoder_free(r);
01371 return AVERROR(ENOMEM);
01372 }
01373
01374 r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
01375
01376 return 0;
01377 }
01378
01379
01380 static int rv34_decoder_realloc(RV34DecContext *r)
01381 {
01382 rv34_decoder_free(r);
01383 return rv34_decoder_alloc(r);
01384 }
01385
01386
01387 static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
01388 {
01389 MpegEncContext *s = &r->s;
01390 GetBitContext *gb = &s->gb;
01391 int mb_pos, slice_type;
01392 int res;
01393
01394 init_get_bits(&r->s.gb, buf, buf_size*8);
01395 res = r->parse_slice_header(r, gb, &r->si);
01396 if(res < 0){
01397 av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
01398 return -1;
01399 }
01400
01401 slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
01402 if (slice_type != s->pict_type) {
01403 av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
01404 return AVERROR_INVALIDDATA;
01405 }
01406 if (s->width != r->si.width || s->height != r->si.height) {
01407 av_log(s->avctx, AV_LOG_ERROR, "Size mismatch\n");
01408 return AVERROR_INVALIDDATA;
01409 }
01410
01411 r->si.end = end;
01412 s->qscale = r->si.quant;
01413 s->mb_num_left = r->si.end - r->si.start;
01414 r->s.mb_skip_run = 0;
01415
01416 mb_pos = s->mb_x + s->mb_y * s->mb_width;
01417 if(r->si.start != mb_pos){
01418 av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
01419 s->mb_x = r->si.start % s->mb_width;
01420 s->mb_y = r->si.start / s->mb_width;
01421 }
01422 memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
01423 s->first_slice_line = 1;
01424 s->resync_mb_x = s->mb_x;
01425 s->resync_mb_y = s->mb_y;
01426
01427 ff_init_block_index(s);
01428 while(!check_slice_end(r, s)) {
01429 ff_update_block_index(s);
01430
01431 if(r->si.type)
01432 res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
01433 else
01434 res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
01435 if(res < 0){
01436 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
01437 return -1;
01438 }
01439 if (++s->mb_x == s->mb_width) {
01440 s->mb_x = 0;
01441 s->mb_y++;
01442 ff_init_block_index(s);
01443
01444 memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
01445 memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
01446
01447 if(r->loop_filter && s->mb_y >= 2)
01448 r->loop_filter(r, s->mb_y - 2);
01449
01450 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
01451 ff_thread_report_progress(&s->current_picture_ptr->f,
01452 s->mb_y - 2, 0);
01453
01454 }
01455 if(s->mb_x == s->resync_mb_x)
01456 s->first_slice_line=0;
01457 s->mb_num_left--;
01458 }
01459 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
01460
01461 return s->mb_y == s->mb_height;
01462 }
01463
01465
01469 av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
01470 {
01471 RV34DecContext *r = avctx->priv_data;
01472 MpegEncContext *s = &r->s;
01473 int ret;
01474
01475 ff_MPV_decode_defaults(s);
01476 s->avctx = avctx;
01477 s->out_format = FMT_H263;
01478 s->codec_id = avctx->codec_id;
01479
01480 s->width = avctx->width;
01481 s->height = avctx->height;
01482
01483 r->s.avctx = avctx;
01484 avctx->flags |= CODEC_FLAG_EMU_EDGE;
01485 r->s.flags |= CODEC_FLAG_EMU_EDGE;
01486 avctx->pix_fmt = PIX_FMT_YUV420P;
01487 avctx->has_b_frames = 1;
01488 s->low_delay = 0;
01489
01490 if ((ret = ff_MPV_common_init(s)) < 0)
01491 return ret;
01492
01493 ff_h264_pred_init(&r->h, AV_CODEC_ID_RV40, 8, 1);
01494
01495 #if CONFIG_RV30_DECODER
01496 if (avctx->codec_id == AV_CODEC_ID_RV30)
01497 ff_rv30dsp_init(&r->rdsp, &r->s.dsp);
01498 #endif
01499 #if CONFIG_RV40_DECODER
01500 if (avctx->codec_id == AV_CODEC_ID_RV40)
01501 ff_rv40dsp_init(&r->rdsp, &r->s.dsp);
01502 #endif
01503
01504 if ((ret = rv34_decoder_alloc(r)) < 0)
01505 return ret;
01506
01507 if(!intra_vlcs[0].cbppattern[0].bits)
01508 rv34_init_tables();
01509
01510 return 0;
01511 }
01512
01513 int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
01514 {
01515 int err;
01516 RV34DecContext *r = avctx->priv_data;
01517
01518 r->s.avctx = avctx;
01519
01520 if (avctx->internal->is_copy) {
01521 r->tmp_b_block_base = NULL;
01522 if ((err = ff_MPV_common_init(&r->s)) < 0)
01523 return err;
01524 if ((err = rv34_decoder_alloc(r)) < 0)
01525 return err;
01526 }
01527 return 0;
01528 }
01529
01530 int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
01531 {
01532 RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
01533 MpegEncContext * const s = &r->s, * const s1 = &r1->s;
01534 int err;
01535
01536 if (dst == src || !s1->context_initialized)
01537 return 0;
01538
01539 if (s->height != s1->height || s->width != s1->width) {
01540 s->height = s1->height;
01541 s->width = s1->width;
01542 if ((err = ff_MPV_common_frame_size_change(s)) < 0)
01543 return err;
01544 if ((err = rv34_decoder_realloc(r)) < 0)
01545 return err;
01546 }
01547
01548 if ((err = ff_mpeg_update_thread_context(dst, src)))
01549 return err;
01550
01551 r->cur_pts = r1->cur_pts;
01552 r->last_pts = r1->last_pts;
01553 r->next_pts = r1->next_pts;
01554
01555 memset(&r->si, 0, sizeof(r->si));
01556
01557 return 0;
01558 }
01559
01560 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
01561 {
01562 if(avctx->slice_count) return avctx->slice_offset[n];
01563 else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
01564 }
01565
01566 static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
01567 {
01568 RV34DecContext *r = avctx->priv_data;
01569 MpegEncContext *s = &r->s;
01570 int got_picture = 0;
01571
01572 ff_er_frame_end(s);
01573 ff_MPV_frame_end(s);
01574 s->mb_num_left = 0;
01575
01576 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
01577 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
01578
01579 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
01580 *pict = s->current_picture_ptr->f;
01581 got_picture = 1;
01582 } else if (s->last_picture_ptr != NULL) {
01583 *pict = s->last_picture_ptr->f;
01584 got_picture = 1;
01585 }
01586 if (got_picture)
01587 ff_print_debug_info(s, pict);
01588
01589 return got_picture;
01590 }
01591
01592 int ff_rv34_decode_frame(AVCodecContext *avctx,
01593 void *data, int *got_picture_ptr,
01594 AVPacket *avpkt)
01595 {
01596 const uint8_t *buf = avpkt->data;
01597 int buf_size = avpkt->size;
01598 RV34DecContext *r = avctx->priv_data;
01599 MpegEncContext *s = &r->s;
01600 AVFrame *pict = data;
01601 SliceInfo si;
01602 int i;
01603 int slice_count;
01604 const uint8_t *slices_hdr = NULL;
01605 int last = 0;
01606
01607
01608 if (buf_size == 0) {
01609
01610 if (s->low_delay==0 && s->next_picture_ptr) {
01611 *pict = s->next_picture_ptr->f;
01612 s->next_picture_ptr = NULL;
01613
01614 *got_picture_ptr = 1;
01615 }
01616 return 0;
01617 }
01618
01619 if(!avctx->slice_count){
01620 slice_count = (*buf++) + 1;
01621 slices_hdr = buf + 4;
01622 buf += 8 * slice_count;
01623 buf_size -= 1 + 8 * slice_count;
01624 }else
01625 slice_count = avctx->slice_count;
01626
01627
01628 if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
01629 get_slice_offset(avctx, slices_hdr, 0) > buf_size){
01630 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
01631 return AVERROR_INVALIDDATA;
01632 }
01633 init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
01634 if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
01635 av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
01636 return AVERROR_INVALIDDATA;
01637 }
01638 if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) &&
01639 si.type == AV_PICTURE_TYPE_B) {
01640 av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
01641 "reference data.\n");
01642 return AVERROR_INVALIDDATA;
01643 }
01644 if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
01645 || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
01646 || avctx->skip_frame >= AVDISCARD_ALL)
01647 return avpkt->size;
01648
01649
01650 if (si.start == 0) {
01651 if (s->mb_num_left > 0) {
01652 av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.\n",
01653 s->mb_num_left);
01654 ff_er_frame_end(s);
01655 ff_MPV_frame_end(s);
01656 }
01657
01658 if (s->width != si.width || s->height != si.height) {
01659 int err;
01660
01661 av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
01662 si.width, si.height);
01663
01664 s->width = si.width;
01665 s->height = si.height;
01666 avcodec_set_dimensions(s->avctx, s->width, s->height);
01667 if ((err = ff_MPV_common_frame_size_change(s)) < 0)
01668 return err;
01669 if ((err = rv34_decoder_realloc(r)) < 0)
01670 return err;
01671 }
01672 s->pict_type = si.type ? si.type : AV_PICTURE_TYPE_I;
01673 if (ff_MPV_frame_start(s, s->avctx) < 0)
01674 return -1;
01675 ff_er_frame_start(s);
01676 if (!r->tmp_b_block_base) {
01677 int i;
01678
01679 r->tmp_b_block_base = av_malloc(s->linesize * 48);
01680 for (i = 0; i < 2; i++)
01681 r->tmp_b_block_y[i] = r->tmp_b_block_base
01682 + i * 16 * s->linesize;
01683 for (i = 0; i < 4; i++)
01684 r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
01685 + (i >> 1) * 8 * s->uvlinesize
01686 + (i & 1) * 16;
01687 }
01688 r->cur_pts = si.pts;
01689 if (s->pict_type != AV_PICTURE_TYPE_B) {
01690 r->last_pts = r->next_pts;
01691 r->next_pts = r->cur_pts;
01692 } else {
01693 int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
01694 int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
01695 int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
01696
01697 if(!refdist){
01698 r->mv_weight1 = r->mv_weight2 = r->weight1 = r->weight2 = 8192;
01699 r->scaled_weight = 0;
01700 }else{
01701 r->mv_weight1 = (dist0 << 14) / refdist;
01702 r->mv_weight2 = (dist1 << 14) / refdist;
01703 if((r->mv_weight1|r->mv_weight2) & 511){
01704 r->weight1 = r->mv_weight1;
01705 r->weight2 = r->mv_weight2;
01706 r->scaled_weight = 0;
01707 }else{
01708 r->weight1 = r->mv_weight1 >> 9;
01709 r->weight2 = r->mv_weight2 >> 9;
01710 r->scaled_weight = 1;
01711 }
01712 }
01713 }
01714 s->mb_x = s->mb_y = 0;
01715 ff_thread_finish_setup(s->avctx);
01716 } else if (HAVE_THREADS &&
01717 (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
01718 av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames in frame "
01719 "multithreading mode (start MB is %d).\n", si.start);
01720 return AVERROR_INVALIDDATA;
01721 }
01722
01723 for(i = 0; i < slice_count; i++){
01724 int offset = get_slice_offset(avctx, slices_hdr, i);
01725 int size;
01726 if(i+1 == slice_count)
01727 size = buf_size - offset;
01728 else
01729 size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
01730
01731 if(offset < 0 || offset > buf_size){
01732 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
01733 break;
01734 }
01735
01736 r->si.end = s->mb_width * s->mb_height;
01737 s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
01738
01739 if(i+1 < slice_count){
01740 if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||
01741 get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {
01742 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
01743 break;
01744 }
01745 init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, i+1), (buf_size-get_slice_offset(avctx, slices_hdr, i+1))*8);
01746 if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
01747 if(i+2 < slice_count)
01748 size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
01749 else
01750 size = buf_size - offset;
01751 }else
01752 r->si.end = si.start;
01753 }
01754 if (size < 0 || size > buf_size - offset) {
01755 av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
01756 break;
01757 }
01758 last = rv34_decode_slice(r, r->si.end, buf + offset, size);
01759 if(last)
01760 break;
01761 }
01762
01763 if (s->current_picture_ptr) {
01764 if (last) {
01765 if(r->loop_filter)
01766 r->loop_filter(r, s->mb_height - 1);
01767
01768 *got_picture_ptr = finish_frame(avctx, pict);
01769 } else if (HAVE_THREADS &&
01770 (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
01771 av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n");
01772
01773
01774 ff_er_frame_end(s);
01775 ff_MPV_frame_end(s);
01776 s->mb_num_left = 0;
01777 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
01778 return AVERROR_INVALIDDATA;
01779 }
01780 }
01781
01782 return avpkt->size;
01783 }
01784
01785 av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
01786 {
01787 RV34DecContext *r = avctx->priv_data;
01788
01789 ff_MPV_common_end(&r->s);
01790 rv34_decoder_free(r);
01791
01792 return 0;
01793 }