00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "mpegvideo.h"
00030 #include "golomb.h"
00031 #include "rectangle.h"
00032
00033 #include "rv34vlc.h"
00034 #include "rv34data.h"
00035 #include "rv34.h"
00036
00037
00038
00040 static const int rv34_mb_type_to_lavc[12] = {
00041 MB_TYPE_INTRA,
00042 MB_TYPE_INTRA16x16,
00043 MB_TYPE_16x16 | MB_TYPE_L0,
00044 MB_TYPE_8x8 | MB_TYPE_L0,
00045 MB_TYPE_16x16 | MB_TYPE_L0,
00046 MB_TYPE_16x16 | MB_TYPE_L1,
00047 MB_TYPE_SKIP,
00048 MB_TYPE_DIRECT2 | MB_TYPE_16x16,
00049 MB_TYPE_16x8 | MB_TYPE_L0,
00050 MB_TYPE_8x16 | MB_TYPE_L0,
00051 MB_TYPE_16x16 | MB_TYPE_L0L1,
00052 MB_TYPE_16x16 | MB_TYPE_L0
00053 };
00054
00055
00056 static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
00057
00069 static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms)
00070 {
00071 int i;
00072 int counts[17] = {0}, codes[17];
00073 uint16_t cw[size], syms[size];
00074 uint8_t bits2[size];
00075 int maxbits = 0, realsize = 0;
00076
00077 for(i = 0; i < size; i++){
00078 if(bits[i]){
00079 bits2[realsize] = bits[i];
00080 syms[realsize] = insyms ? insyms[i] : i;
00081 realsize++;
00082 maxbits = FFMAX(maxbits, bits[i]);
00083 counts[bits[i]]++;
00084 }
00085 }
00086
00087 codes[0] = 0;
00088 for(i = 0; i < 16; i++)
00089 codes[i+1] = (codes[i] + counts[i]) << 1;
00090 for(i = 0; i < realsize; i++)
00091 cw[i] = codes[bits2[i]]++;
00092
00093 init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
00094 bits2, 1, 1,
00095 cw, 2, 2,
00096 syms, 2, 2, INIT_VLC_USE_STATIC);
00097 }
00098
00102 static void rv34_init_tables()
00103 {
00104 int i, j, k;
00105
00106 for(i = 0; i < NUM_INTRA_TABLES; i++){
00107 for(j = 0; j < 2; j++){
00108 rv34_gen_vlc(rv34_table_intra_cbppat [i][j], CBPPAT_VLC_SIZE, &intra_vlcs[i].cbppattern[j], NULL);
00109 rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].second_pattern[j], NULL);
00110 rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].third_pattern[j], NULL);
00111 for(k = 0; k < 4; k++)
00112 rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2], CBP_VLC_SIZE, &intra_vlcs[i].cbp[j][k], rv34_cbp_code);
00113 }
00114 for(j = 0; j < 4; j++)
00115 rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, &intra_vlcs[i].first_pattern[j], NULL);
00116 rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, &intra_vlcs[i].coefficient, NULL);
00117 }
00118
00119 for(i = 0; i < NUM_INTER_TABLES; i++){
00120 rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE, &inter_vlcs[i].cbppattern[0], NULL);
00121 for(j = 0; j < 4; j++)
00122 rv34_gen_vlc(rv34_inter_cbp[i][j], CBP_VLC_SIZE, &inter_vlcs[i].cbp[0][j], rv34_cbp_code);
00123 for(j = 0; j < 2; j++){
00124 rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE, &inter_vlcs[i].first_pattern[j], NULL);
00125 rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].second_pattern[j], NULL);
00126 rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].third_pattern[j], NULL);
00127 }
00128 rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE, &inter_vlcs[i].coefficient, NULL);
00129 }
00130 }
00131
00133
00134
00140 static av_always_inline void rv34_row_transform(int temp[16], DCTELEM *block)
00141 {
00142 int i;
00143
00144 for(i=0; i<4; i++){
00145 const int z0= 13*(block[i+8*0] + block[i+8*2]);
00146 const int z1= 13*(block[i+8*0] - block[i+8*2]);
00147 const int z2= 7* block[i+8*1] - 17*block[i+8*3];
00148 const int z3= 17* block[i+8*1] + 7*block[i+8*3];
00149
00150 temp[4*i+0]= z0+z3;
00151 temp[4*i+1]= z1+z2;
00152 temp[4*i+2]= z1-z2;
00153 temp[4*i+3]= z0-z3;
00154 }
00155 }
00156
00161 static void rv34_inv_transform(DCTELEM *block){
00162 int temp[16];
00163 int i;
00164
00165 rv34_row_transform(temp, block);
00166
00167 for(i=0; i<4; i++){
00168 const int z0= 13*(temp[4*0+i] + temp[4*2+i]) + 0x200;
00169 const int z1= 13*(temp[4*0+i] - temp[4*2+i]) + 0x200;
00170 const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
00171 const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
00172
00173 block[i*8+0]= (z0 + z3)>>10;
00174 block[i*8+1]= (z1 + z2)>>10;
00175 block[i*8+2]= (z1 - z2)>>10;
00176 block[i*8+3]= (z0 - z3)>>10;
00177 }
00178
00179 }
00180
00187 static void rv34_inv_transform_noround(DCTELEM *block){
00188 int temp[16];
00189 int i;
00190
00191 rv34_row_transform(temp, block);
00192
00193 for(i=0; i<4; i++){
00194 const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
00195 const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
00196 const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
00197 const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
00198
00199 block[i*8+0]= ((z0 + z3)*3)>>11;
00200 block[i*8+1]= ((z1 + z2)*3)>>11;
00201 block[i*8+2]= ((z1 - z2)*3)>>11;
00202 block[i*8+3]= ((z0 - z3)*3)>>11;
00203 }
00204
00205 }
00206
00208
00209
00218 static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table)
00219 {
00220 int pattern, code, cbp=0;
00221 int ones;
00222 static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
00223 static const int shifts[4] = { 0, 2, 8, 10 };
00224 int *curshift = shifts;
00225 int i, t, mask;
00226
00227 code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
00228 pattern = code & 0xF;
00229 code >>= 4;
00230
00231 ones = rv34_count_ones[pattern];
00232
00233 for(mask = 8; mask; mask >>= 1, curshift++){
00234 if(pattern & mask)
00235 cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
00236 }
00237
00238 for(i = 0; i < 4; i++){
00239 t = modulo_three_table[code][i];
00240 if(t == 1)
00241 cbp |= cbp_masks[get_bits1(gb)] << i;
00242 if(t == 2)
00243 cbp |= cbp_masks[2] << i;
00244 }
00245 return cbp;
00246 }
00247
00251 static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc)
00252 {
00253 if(coef){
00254 if(coef == esc){
00255 coef = get_vlc2(gb, vlc->table, 9, 2);
00256 if(coef > 23){
00257 coef -= 23;
00258 coef = 22 + ((1 << coef) | get_bits(gb, coef));
00259 }
00260 coef += esc;
00261 }
00262 if(get_bits1(gb))
00263 coef = -coef;
00264 *dst = coef;
00265 }
00266 }
00267
00271 static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc)
00272 {
00273 int coeffs[4];
00274
00275 coeffs[0] = modulo_three_table[code][0];
00276 coeffs[1] = modulo_three_table[code][1];
00277 coeffs[2] = modulo_three_table[code][2];
00278 coeffs[3] = modulo_three_table[code][3];
00279 decode_coeff(dst , coeffs[0], 3, gb, vlc);
00280 if(is_block2){
00281 decode_coeff(dst+8, coeffs[1], 2, gb, vlc);
00282 decode_coeff(dst+1, coeffs[2], 2, gb, vlc);
00283 }else{
00284 decode_coeff(dst+1, coeffs[1], 2, gb, vlc);
00285 decode_coeff(dst+8, coeffs[2], 2, gb, vlc);
00286 }
00287 decode_coeff(dst+9, coeffs[3], 2, gb, vlc);
00288 }
00289
00301 static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc)
00302 {
00303 int code, pattern;
00304
00305 code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
00306
00307 pattern = code & 0x7;
00308
00309 code >>= 3;
00310 decode_subblock(dst, code, 0, gb, &rvlc->coefficient);
00311
00312 if(pattern & 4){
00313 code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
00314 decode_subblock(dst + 2, code, 0, gb, &rvlc->coefficient);
00315 }
00316 if(pattern & 2){
00317 code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
00318 decode_subblock(dst + 8*2, code, 1, gb, &rvlc->coefficient);
00319 }
00320 if(pattern & 1){
00321 code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
00322 decode_subblock(dst + 8*2+2, code, 0, gb, &rvlc->coefficient);
00323 }
00324
00325 }
00326
00331 static inline void rv34_dequant4x4(DCTELEM *block, int Qdc, int Q)
00332 {
00333 int i, j;
00334
00335 block[0] = (block[0] * Qdc + 8) >> 4;
00336 for(i = 0; i < 4; i++)
00337 for(j = !i; j < 4; j++)
00338 block[j + i*8] = (block[j + i*8] * Q + 8) >> 4;
00339 }
00340
00345 static inline void rv34_dequant4x4_16x16(DCTELEM *block, int Qdc, int Q)
00346 {
00347 int i;
00348
00349 for(i = 0; i < 3; i++)
00350 block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Qdc + 8) >> 4;
00351 for(; i < 16; i++)
00352 block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Q + 8) >> 4;
00353 }
00355
00356
00366 int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
00367 {
00368 int i;
00369 for(i = 0; i < 5; i++)
00370 if(rv34_mb_max_sizes[i] > mb_size)
00371 break;
00372 return rv34_mb_bits_sizes[i];
00373 }
00374
00378 static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
00379 {
00380 if(mod == 2 && quant < 19) quant += 10;
00381 else if(mod && quant < 26) quant += 5;
00382 return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
00383 : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
00384 }
00385
00389 static inline int rv34_decode_dquant(GetBitContext *gb, int quant)
00390 {
00391 if(get_bits1(gb))
00392 return rv34_dquant_tab[get_bits1(gb)][quant];
00393 else
00394 return get_bits(gb, 5);
00395 }
00396
00398
00405 static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
00406
00408 static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
00409
00411 static const uint8_t avail_indexes[4] = { 5, 6, 9, 10 };
00412
00420 static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
00421 {
00422 MpegEncContext *s = &r->s;
00423 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00424 int A[2] = {0}, B[2], C[2];
00425 int i, j;
00426 int mx, my;
00427 int avail_index = avail_indexes[subblock_no];
00428 int c_off = part_sizes_w[block_type];
00429
00430 mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
00431 if(subblock_no == 3)
00432 c_off = -1;
00433
00434 if(r->avail_cache[avail_index - 1]){
00435 A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
00436 A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
00437 }
00438 if(r->avail_cache[avail_index - 4]){
00439 B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
00440 B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
00441 }else{
00442 B[0] = A[0];
00443 B[1] = A[1];
00444 }
00445 if(!r->avail_cache[avail_index - 4 + c_off]){
00446 if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){
00447 C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
00448 C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
00449 }else{
00450 C[0] = A[0];
00451 C[1] = A[1];
00452 }
00453 }else{
00454 C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
00455 C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
00456 }
00457 mx = mid_pred(A[0], B[0], C[0]);
00458 my = mid_pred(A[1], B[1], C[1]);
00459 mx += r->dmv[dmv_no][0];
00460 my += r->dmv[dmv_no][1];
00461 for(j = 0; j < part_sizes_h[block_type]; j++){
00462 for(i = 0; i < part_sizes_w[block_type]; i++){
00463 s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
00464 s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
00465 }
00466 }
00467 }
00468
00472 static int calc_add_mv(MpegEncContext *s, int dir, int component)
00473 {
00474 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00475 int sum;
00476
00477 sum = (s->next_picture_ptr->motion_val[0][mv_pos][component] +
00478 s->next_picture_ptr->motion_val[0][mv_pos + 1][component] +
00479 s->next_picture_ptr->motion_val[0][mv_pos + s->b8_stride][component] +
00480 s->next_picture_ptr->motion_val[0][mv_pos + s->b8_stride + 1][component]) >> 2;
00481 return dir ? -(sum >> 1) : ((sum + 1) >> 1);
00482 }
00483
00487 static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
00488 int A_avail, int B_avail, int C_avail,
00489 int *mx, int *my)
00490 {
00491 if(A_avail + B_avail + C_avail != 3){
00492 *mx = A[0] + B[0] + C[0];
00493 *my = A[1] + B[1] + C[1];
00494 if(A_avail + B_avail + C_avail == 2){
00495 *mx /= 2;
00496 *my /= 2;
00497 }
00498 }else{
00499 *mx = mid_pred(A[0], B[0], C[0]);
00500 *my = mid_pred(A[1], B[1], C[1]);
00501 }
00502 }
00503
00507 static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
00508 {
00509 MpegEncContext *s = &r->s;
00510 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00511 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
00512 int A[2], B[2], C[2];
00513 int has_A = 0, has_B = 0, has_C = 0;
00514 int mx, my;
00515 int i, j;
00516 Picture *cur_pic = s->current_picture_ptr;
00517 const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
00518 int type = cur_pic->mb_type[mb_pos];
00519
00520 memset(A, 0, sizeof(A));
00521 memset(B, 0, sizeof(B));
00522 memset(C, 0, sizeof(C));
00523 if((r->avail_cache[5-1] & type) & mask){
00524 A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
00525 A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
00526 has_A = 1;
00527 }
00528 if((r->avail_cache[5-4] & type) & mask){
00529 B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
00530 B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
00531 has_B = 1;
00532 }
00533 if((r->avail_cache[5-2] & type) & mask){
00534 C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
00535 C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
00536 has_C = 1;
00537 }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[5-5] & type) & mask){
00538 C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
00539 C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
00540 has_C = 1;
00541 }
00542
00543 rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
00544
00545 mx += r->dmv[dir][0];
00546 my += r->dmv[dir][1];
00547
00548 if(block_type == RV34_MB_B_DIRECT){
00549 mx += calc_add_mv(s, dir, 0);
00550 my += calc_add_mv(s, dir, 1);
00551 }
00552 for(j = 0; j < 2; j++){
00553 for(i = 0; i < 2; i++){
00554 cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
00555 cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
00556 }
00557 }
00558 if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD)
00559 fill_rectangle(cur_pic->motion_val[!dir][mv_pos], 2, 2, s->b8_stride, 0, 4);
00560 }
00561
00562 static const int chroma_coeffs[3] = { 8, 5, 3 };
00563
00575 static inline void rv34_mc(RV34DecContext *r, const int block_type,
00576 const int xoff, const int yoff, int mv_off,
00577 const int width, const int height, int dir,
00578 const int thirdpel,
00579 qpel_mc_func (*qpel_mc)[16],
00580 h264_chroma_mc_func (*chroma_mc))
00581 {
00582 MpegEncContext *s = &r->s;
00583 uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
00584 int dxy, mx, my, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
00585 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
00586 int is16x16 = 1;
00587
00588 if(thirdpel){
00589 mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
00590 my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
00591 lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
00592 ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
00593 uvmx = chroma_coeffs[(3*(mx&1) + lx) >> 1];
00594 uvmy = chroma_coeffs[(3*(my&1) + ly) >> 1];
00595 }else{
00596 mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
00597 my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
00598 lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
00599 ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
00600 uvmx = mx & 6;
00601 uvmy = my & 6;
00602 }
00603 dxy = ly*4 + lx;
00604 srcY = dir ? s->next_picture_ptr->data[0] : s->last_picture_ptr->data[0];
00605 srcU = dir ? s->next_picture_ptr->data[1] : s->last_picture_ptr->data[1];
00606 srcV = dir ? s->next_picture_ptr->data[2] : s->last_picture_ptr->data[2];
00607 src_x = s->mb_x * 16 + xoff + mx;
00608 src_y = s->mb_y * 16 + yoff + my;
00609 uvsrc_x = s->mb_x * 8 + (xoff >> 1) + (mx >> 1);
00610 uvsrc_y = s->mb_y * 8 + (yoff >> 1) + (my >> 1);
00611 srcY += src_y * s->linesize + src_x;
00612 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
00613 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
00614 if( (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 3
00615 || (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 3){
00616 uint8_t *uvbuf= s->edge_emu_buffer + 20 * s->linesize;
00617
00618 srcY -= 2 + 2*s->linesize;
00619 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+4, (height<<3)+4,
00620 src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
00621 srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
00622 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,
00623 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
00624 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,
00625 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
00626 srcU = uvbuf;
00627 srcV = uvbuf + 16;
00628 }
00629 Y = s->dest[0] + xoff + yoff *s->linesize;
00630 U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
00631 V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
00632
00633 if(block_type == RV34_MB_P_16x8){
00634 qpel_mc[1][dxy](Y, srcY, s->linesize);
00635 Y += 8;
00636 srcY += 8;
00637 }else if(block_type == RV34_MB_P_8x16){
00638 qpel_mc[1][dxy](Y, srcY, s->linesize);
00639 Y += 8 * s->linesize;
00640 srcY += 8 * s->linesize;
00641 }
00642 is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
00643 qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
00644 chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
00645 chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
00646 }
00647
00648 static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
00649 const int xoff, const int yoff, int mv_off,
00650 const int width, const int height, int dir)
00651 {
00652 rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30,
00653 r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
00654 : r->s.dsp.put_h264_qpel_pixels_tab,
00655 r->s.dsp.put_h264_chroma_pixels_tab);
00656 }
00657
00658 static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
00659 {
00660 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,
00661 r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
00662 : r->s.dsp.put_h264_qpel_pixels_tab,
00663 r->s.dsp.put_h264_chroma_pixels_tab);
00664 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,
00665 r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
00666 : r->s.dsp.avg_h264_qpel_pixels_tab,
00667 r->s.dsp.avg_h264_chroma_pixels_tab);
00668 }
00669
00671 static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
00672
00677 static int rv34_decode_mv(RV34DecContext *r, int block_type)
00678 {
00679 MpegEncContext *s = &r->s;
00680 GetBitContext *gb = &s->gb;
00681 int i;
00682
00683 memset(r->dmv, 0, sizeof(r->dmv));
00684 for(i = 0; i < num_mvs[block_type]; i++){
00685 r->dmv[i][0] = svq3_get_se_golomb(gb);
00686 r->dmv[i][1] = svq3_get_se_golomb(gb);
00687 }
00688 switch(block_type){
00689 case RV34_MB_TYPE_INTRA:
00690 case RV34_MB_TYPE_INTRA16x16:
00691 fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
00692 return 0;
00693 case RV34_MB_SKIP:
00694 if(s->pict_type == P_TYPE){
00695 fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
00696 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
00697 break;
00698 }
00699 case RV34_MB_B_DIRECT:
00700 rv34_pred_mv_b (r, RV34_MB_B_DIRECT, 0);
00701 rv34_pred_mv_b (r, RV34_MB_B_DIRECT, 1);
00702 rv34_mc_2mv (r, RV34_MB_B_DIRECT);
00703 break;
00704 case RV34_MB_P_16x16:
00705 case RV34_MB_P_MIX16x16:
00706 rv34_pred_mv(r, block_type, 0, 0);
00707 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
00708 break;
00709 case RV34_MB_B_FORWARD:
00710 case RV34_MB_B_BACKWARD:
00711 r->dmv[1][0] = r->dmv[0][0];
00712 r->dmv[1][1] = r->dmv[0][1];
00713 rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
00714 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
00715 break;
00716 case RV34_MB_P_16x8:
00717 case RV34_MB_P_8x16:
00718 rv34_pred_mv(r, block_type, 0, 0);
00719 rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
00720 if(block_type == RV34_MB_P_16x8){
00721 rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
00722 rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
00723 }
00724 if(block_type == RV34_MB_P_8x16){
00725 rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
00726 rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
00727 }
00728 break;
00729 case RV34_MB_B_BIDIR:
00730 rv34_pred_mv_b (r, block_type, 0);
00731 rv34_pred_mv_b (r, block_type, 1);
00732 rv34_mc_2mv (r, block_type);
00733 break;
00734 case RV34_MB_P_8x8:
00735 for(i=0;i< 4;i++){
00736 rv34_pred_mv(r, block_type, i, i);
00737 rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
00738 }
00739 break;
00740 }
00741
00742 return 0;
00743 }
00745
00751 static const int ittrans[9] = {
00752 DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
00753 VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
00754 };
00755
00757 static const int ittrans16[4] = {
00758 DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
00759 };
00760
00764 static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
00765 {
00766 uint8_t *prev = dst - stride + 4;
00767 uint32_t topleft;
00768
00769 if(!up && !left)
00770 itype = DC_128_PRED;
00771 else if(!up){
00772 if(itype == VERT_PRED) itype = HOR_PRED;
00773 if(itype == DC_PRED) itype = LEFT_DC_PRED;
00774 }else if(!left){
00775 if(itype == HOR_PRED) itype = VERT_PRED;
00776 if(itype == DC_PRED) itype = TOP_DC_PRED;
00777 if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
00778 }
00779 if(!down){
00780 if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
00781 if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
00782 if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
00783 }
00784 if(!right && up){
00785 topleft = dst[-stride + 3] * 0x01010101;
00786 prev = &topleft;
00787 }
00788 r->h.pred4x4[itype](dst, prev, stride);
00789 }
00790
00792 static void rv34_add_4x4_block(uint8_t *dst, int stride, DCTELEM block[64], int off)
00793 {
00794 int x, y;
00795 for(y = 0; y < 4; y++)
00796 for(x = 0; x < 4; x++)
00797 dst[x + y*stride] = av_clip_uint8(dst[x + y*stride] + block[off + x+y*8]);
00798 }
00799
00800 static inline int adjust_pred16(int itype, int up, int left)
00801 {
00802 if(!up && !left)
00803 itype = DC_128_PRED8x8;
00804 else if(!up){
00805 if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
00806 if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
00807 if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
00808 }else if(!left){
00809 if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
00810 if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
00811 if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
00812 }
00813 return itype;
00814 }
00815
00816 static void rv34_output_macroblock(RV34DecContext *r, int8_t *intra_types, int cbp, int is16)
00817 {
00818 MpegEncContext *s = &r->s;
00819 DSPContext *dsp = &s->dsp;
00820 int i, j;
00821 uint8_t *Y, *U, *V;
00822 int itype;
00823 int avail[6*8] = {0};
00824 int idx;
00825
00826
00827 if(r->avail_cache[0])
00828 avail[0] = 1;
00829 if(r->avail_cache[1])
00830 avail[1] = avail[2] = 1;
00831 if(r->avail_cache[2])
00832 avail[3] = avail[4] = 1;
00833 if(r->avail_cache[3])
00834 avail[5] = 1;
00835 if(r->avail_cache[4])
00836 avail[8] = avail[16] = 1;
00837 if(r->avail_cache[8])
00838 avail[24] = avail[32] = 1;
00839
00840 Y = s->dest[0];
00841 U = s->dest[1];
00842 V = s->dest[2];
00843 if(!is16){
00844 for(j = 0; j < 4; j++){
00845 idx = 9 + j*8;
00846 for(i = 0; i < 4; i++, cbp >>= 1, Y += 4, idx++){
00847 rv34_pred_4x4_block(r, Y, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
00848 avail[idx] = 1;
00849 if(cbp & 1)
00850 rv34_add_4x4_block(Y, s->linesize, s->block[(i>>1)+(j&2)], (i&1)*4+(j&1)*32);
00851 }
00852 Y += s->linesize * 4 - 4*4;
00853 intra_types += s->b4_stride;
00854 }
00855 intra_types -= s->b4_stride * 4;
00856 fill_rectangle(r->avail_cache + 5, 2, 2, 4, 0, 4);
00857 for(j = 0; j < 2; j++){
00858 idx = 5 + j*4;
00859 for(i = 0; i < 2; i++, cbp >>= 1, idx++){
00860 rv34_pred_4x4_block(r, U + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);
00861 rv34_pred_4x4_block(r, V + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);
00862 r->avail_cache[idx] = 1;
00863 if(cbp & 0x01)
00864 rv34_add_4x4_block(U + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[4], i*4+j*32);
00865 if(cbp & 0x10)
00866 rv34_add_4x4_block(V + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[5], i*4+j*32);
00867 }
00868 }
00869 }else{
00870 itype = ittrans16[intra_types[0]];
00871 itype = adjust_pred16(itype, r->avail_cache[5-4], r->avail_cache[5-1]);
00872 r->h.pred16x16[itype](Y, s->linesize);
00873 dsp->add_pixels_clamped(s->block[0], Y, s->current_picture.linesize[0]);
00874 dsp->add_pixels_clamped(s->block[1], Y + 8, s->current_picture.linesize[0]);
00875 Y += s->current_picture.linesize[0] * 8;
00876 dsp->add_pixels_clamped(s->block[2], Y, s->current_picture.linesize[0]);
00877 dsp->add_pixels_clamped(s->block[3], Y + 8, s->current_picture.linesize[0]);
00878
00879 itype = ittrans16[intra_types[0]];
00880 if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
00881 itype = adjust_pred16(itype, r->avail_cache[5-4], r->avail_cache[5-1]);
00882 r->h.pred8x8[itype](U, s->uvlinesize);
00883 dsp->add_pixels_clamped(s->block[4], U, s->uvlinesize);
00884 r->h.pred8x8[itype](V, s->uvlinesize);
00885 dsp->add_pixels_clamped(s->block[5], V, s->uvlinesize);
00886 }
00887 }
00888
00890
00895 static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
00896 {
00897 MpegEncContext *s = &r->s;
00898 GetBitContext *gb = &s->gb;
00899 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00900 int i, t;
00901
00902 if(!r->si.type){
00903 r->is16 = get_bits1(gb);
00904 if(!r->is16 && !r->rv30){
00905 if(!get_bits1(gb))
00906 av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
00907 }
00908 s->current_picture_ptr->mb_type[mb_pos] = r->is16 ? MB_TYPE_INTRA16x16 : MB_TYPE_INTRA;
00909 r->block_type = r->is16 ? RV34_MB_TYPE_INTRA16x16 : RV34_MB_TYPE_INTRA;
00910 }else{
00911 r->block_type = r->decode_mb_info(r);
00912 if(r->block_type == -1)
00913 return -1;
00914 s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
00915 r->mb_type[mb_pos] = r->block_type;
00916 if(r->block_type == RV34_MB_SKIP){
00917 if(s->pict_type == P_TYPE)
00918 r->mb_type[mb_pos] = RV34_MB_P_16x16;
00919 if(s->pict_type == B_TYPE)
00920 r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
00921 }
00922 r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
00923 rv34_decode_mv(r, r->block_type);
00924 if(r->block_type == RV34_MB_SKIP){
00925 fill_rectangle(intra_types, 4, 4, s->b4_stride, 0, sizeof(intra_types[0]));
00926 return 0;
00927 }
00928 r->chroma_vlc = 1;
00929 r->luma_vlc = 0;
00930 }
00931 if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
00932 if(r->is16){
00933 t = get_bits(gb, 2);
00934 fill_rectangle(intra_types, 4, 4, s->b4_stride, t, sizeof(intra_types[0]));
00935 r->luma_vlc = 2;
00936 }else{
00937 if(r->decode_intra_types(r, gb, intra_types) < 0)
00938 return -1;
00939 r->luma_vlc = 1;
00940 }
00941 r->chroma_vlc = 0;
00942 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
00943 }else{
00944 for(i = 0; i < 16; i++)
00945 intra_types[(i & 3) + (i>>2) * s->b4_stride] = 0;
00946 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
00947 if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
00948 r->is16 = 1;
00949 r->chroma_vlc = 1;
00950 r->luma_vlc = 2;
00951 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
00952 }
00953 }
00954
00955 return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
00956 }
00957
00966 #define LUMA_CBP_BLOCK_MASK 0x303
00967
00968 #define U_CBP_MASK 0x0F0000
00969 #define V_CBP_MASK 0xF00000
00970
00971
00972 static void rv34_apply_differences(RV34DecContext *r, int cbp)
00973 {
00974 static const int shifts[4] = { 0, 2, 8, 10 };
00975 MpegEncContext *s = &r->s;
00976 int i;
00977
00978 for(i = 0; i < 4; i++)
00979 if(cbp & (LUMA_CBP_BLOCK_MASK << shifts[i]))
00980 s->dsp.add_pixels_clamped(s->block[i], s->dest[0] + (i & 1)*8 + (i&2)*4*s->linesize, s->linesize);
00981 if(cbp & U_CBP_MASK)
00982 s->dsp.add_pixels_clamped(s->block[4], s->dest[1], s->uvlinesize);
00983 if(cbp & V_CBP_MASK)
00984 s->dsp.add_pixels_clamped(s->block[5], s->dest[2], s->uvlinesize);
00985 }
00986
00987 static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types)
00988 {
00989 MpegEncContext *s = &r->s;
00990 GetBitContext *gb = &s->gb;
00991 int cbp, cbp2;
00992 int i, blknum, blkoff;
00993 DCTELEM block16[64];
00994 int luma_dc_quant;
00995 int dist;
00996 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00997
00998
00999 memset(r->avail_cache, 0, sizeof(r->avail_cache));
01000 fill_rectangle(r->avail_cache + 5, 2, 2, 4, 1, 4);
01001 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
01002 if(s->mb_x && dist)
01003 r->avail_cache[4] =
01004 r->avail_cache[8] = s->current_picture_ptr->mb_type[mb_pos - 1];
01005 if(dist >= s->mb_width)
01006 r->avail_cache[1] =
01007 r->avail_cache[2] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
01008 if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
01009 r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
01010 if(s->mb_x && dist > s->mb_width)
01011 r->avail_cache[0] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
01012
01013 s->qscale = r->si.quant;
01014 cbp = cbp2 = rv34_decode_mb_header(r, intra_types);
01015 r->cbp_luma [s->mb_x + s->mb_y * s->mb_stride] = cbp;
01016 r->cbp_chroma[s->mb_x + s->mb_y * s->mb_stride] = cbp >> 16;
01017 s->current_picture.qscale_table[s->mb_x + s->mb_y * s->mb_stride] = s->qscale;
01018
01019 if(cbp == -1)
01020 return -1;
01021
01022 luma_dc_quant = r->si.type ? r->luma_dc_quant_p[s->qscale] : r->luma_dc_quant_i[s->qscale];
01023 if(r->is16){
01024 memset(block16, 0, sizeof(block16));
01025 rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0);
01026 rv34_dequant4x4_16x16(block16, rv34_qscale_tab[luma_dc_quant],rv34_qscale_tab[s->qscale]);
01027 rv34_inv_transform_noround(block16);
01028 }
01029
01030 for(i = 0; i < 16; i++, cbp >>= 1){
01031 if(!r->is16 && !(cbp & 1)) continue;
01032 blknum = ((i & 2) >> 1) + ((i & 8) >> 2);
01033 blkoff = ((i & 1) << 2) + ((i & 4) << 3);
01034 if(cbp & 1)
01035 rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->luma_vlc, 0);
01036 rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[luma_dc_quant],rv34_qscale_tab[s->qscale]);
01037 if(r->is16)
01038 s->block[blknum][blkoff] = block16[(i & 3) | ((i & 0xC) << 1)];
01039 rv34_inv_transform(s->block[blknum] + blkoff);
01040 }
01041 if(r->block_type == RV34_MB_P_MIX16x16)
01042 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
01043 for(; i < 24; i++, cbp >>= 1){
01044 if(!(cbp & 1)) continue;
01045 blknum = ((i & 4) >> 2) + 4;
01046 blkoff = ((i & 1) << 2) + ((i & 2) << 4);
01047 rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->chroma_vlc, 1);
01048 rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]],rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]]);
01049 rv34_inv_transform(s->block[blknum] + blkoff);
01050 }
01051 if(IS_INTRA(s->current_picture_ptr->mb_type[s->mb_x + s->mb_y*s->mb_stride]))
01052 rv34_output_macroblock(r, intra_types, cbp2, r->is16);
01053 else
01054 rv34_apply_differences(r, cbp2);
01055
01056 return 0;
01057 }
01058
01059 static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
01060 {
01061 int bits;
01062 if(s->mb_y >= s->mb_height)
01063 return 1;
01064 if(!s->mb_num_left)
01065 return 1;
01066 if(r->s.mb_skip_run > 1)
01067 return 0;
01068 bits = r->bits - get_bits_count(&s->gb);
01069 if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits)))
01070 return 1;
01071 return 0;
01072 }
01073
01074 static inline int slice_compare(SliceInfo *si1, SliceInfo *si2)
01075 {
01076 return si1->type != si2->type ||
01077 si1->start >= si2->start ||
01078 si1->width != si2->width ||
01079 si1->height != si2->height;
01080 }
01081
01082 static int rv34_decode_slice(RV34DecContext *r, int end, uint8_t* buf, int buf_size)
01083 {
01084 MpegEncContext *s = &r->s;
01085 GetBitContext *gb = &s->gb;
01086 int mb_pos;
01087 int res;
01088
01089 init_get_bits(&r->s.gb, buf, buf_size*8);
01090 res = r->parse_slice_header(r, gb, &r->si);
01091 if(res < 0){
01092 av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
01093 return -1;
01094 }
01095
01096 if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
01097 if(s->width != r->si.width || s->height != r->si.height){
01098 av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
01099 MPV_common_end(s);
01100 s->width = r->si.width;
01101 s->height = r->si.height;
01102 if(MPV_common_init(s) < 0)
01103 return -1;
01104 r->intra_types_hist = av_realloc(r->intra_types_hist, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
01105 r->intra_types = r->intra_types_hist + s->b4_stride * 4;
01106 r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
01107 r->cbp_luma = av_realloc(r->cbp_luma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
01108 r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
01109 }
01110 s->pict_type = r->si.type ? r->si.type : I_TYPE;
01111 if(MPV_frame_start(s, s->avctx) < 0)
01112 return -1;
01113 ff_er_frame_start(s);
01114 s->current_picture_ptr = &s->current_picture;
01115 s->mb_x = s->mb_y = 0;
01116 }
01117
01118 r->si.end = end;
01119 s->qscale = r->si.quant;
01120 r->bits = buf_size*8;
01121 s->mb_num_left = r->si.end - r->si.start;
01122 r->s.mb_skip_run = 0;
01123
01124 mb_pos = s->mb_x + s->mb_y * s->mb_width;
01125 if(r->si.start != mb_pos){
01126 av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
01127 s->mb_x = r->si.start % s->mb_width;
01128 s->mb_y = r->si.start / s->mb_width;
01129 }
01130 memset(r->intra_types_hist, -1, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
01131 s->first_slice_line = 1;
01132 s->resync_mb_x= s->mb_x;
01133 s->resync_mb_y= s->mb_y;
01134
01135 ff_init_block_index(s);
01136 while(!check_slice_end(r, s)) {
01137 ff_update_block_index(s);
01138 s->dsp.clear_blocks(s->block[0]);
01139
01140 if(rv34_decode_macroblock(r, r->intra_types + s->mb_x * 4 + 1) < 0){
01141 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
01142 return -1;
01143 }
01144 if (++s->mb_x == s->mb_width) {
01145 s->mb_x = 0;
01146 s->mb_y++;
01147 ff_init_block_index(s);
01148
01149 memmove(r->intra_types_hist, r->intra_types, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
01150 memset(r->intra_types, -1, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
01151 }
01152 if(s->mb_x == s->resync_mb_x)
01153 s->first_slice_line=0;
01154 s->mb_num_left--;
01155 }
01156 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
01157
01158 return (s->mb_y == s->mb_height);
01159 }
01160
01162
01166 int ff_rv34_decode_init(AVCodecContext *avctx)
01167 {
01168 RV34DecContext *r = avctx->priv_data;
01169 MpegEncContext *s = &r->s;
01170
01171 MPV_decode_defaults(s);
01172 s->avctx= avctx;
01173 s->out_format = FMT_H263;
01174 s->codec_id= avctx->codec_id;
01175
01176 s->width = avctx->width;
01177 s->height = avctx->height;
01178
01179 r->s.avctx = avctx;
01180 avctx->flags |= CODEC_FLAG_EMU_EDGE;
01181 r->s.flags |= CODEC_FLAG_EMU_EDGE;
01182 avctx->pix_fmt = PIX_FMT_YUV420P;
01183 avctx->has_b_frames = 1;
01184 s->low_delay = 0;
01185
01186 if (MPV_common_init(s) < 0)
01187 return -1;
01188
01189 ff_h264_pred_init(&r->h, CODEC_ID_RV40);
01190
01191 r->intra_types_hist = av_malloc(s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
01192 r->intra_types = r->intra_types_hist + s->b4_stride * 4;
01193
01194 r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
01195
01196 r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
01197 r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
01198
01199 if(!intra_vlcs[0].cbppattern[0].bits)
01200 rv34_init_tables();
01201
01202 return 0;
01203 }
01204
01205 static int get_slice_offset(AVCodecContext *avctx, uint8_t *buf, int n)
01206 {
01207 if(avctx->slice_count) return avctx->slice_offset[n];
01208 else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
01209 }
01210
01211 int ff_rv34_decode_frame(AVCodecContext *avctx,
01212 void *data, int *data_size,
01213 uint8_t *buf, int buf_size)
01214 {
01215 RV34DecContext *r = avctx->priv_data;
01216 MpegEncContext *s = &r->s;
01217 AVFrame *pict = data;
01218 SliceInfo si;
01219 int i;
01220 int slice_count;
01221 uint8_t *slices_hdr = NULL;
01222 int last = 0;
01223
01224
01225 if (buf_size == 0) {
01226
01227 if (s->low_delay==0 && s->next_picture_ptr) {
01228 *pict= *(AVFrame*)s->next_picture_ptr;
01229 s->next_picture_ptr= NULL;
01230
01231 *data_size = sizeof(AVFrame);
01232 }
01233 return 0;
01234 }
01235
01236 if(!avctx->slice_count){
01237 slice_count = (*buf++) + 1;
01238 slices_hdr = buf + 4;
01239 buf += 8 * slice_count;
01240 }else
01241 slice_count = avctx->slice_count;
01242
01243 for(i=0; i<slice_count; i++){
01244 int offset= get_slice_offset(avctx, slices_hdr, i);
01245 int size;
01246 if(i+1 == slice_count)
01247 size= buf_size - offset;
01248 else
01249 size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
01250
01251 r->si.end = s->mb_width * s->mb_height;
01252 if(i+1 < slice_count){
01253 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);
01254 if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
01255 if(i+2 < slice_count)
01256 size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
01257 else
01258 size = buf_size - offset;
01259 }else
01260 r->si.end = si.start;
01261 }
01262 last = rv34_decode_slice(r, r->si.end, buf + offset, size);
01263 s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
01264 if(last)
01265 break;
01266 }
01267
01268 if(last){
01269 if(r->loop_filter)
01270 r->loop_filter(r);
01271 ff_er_frame_end(s);
01272 MPV_frame_end(s);
01273 if (s->pict_type == B_TYPE || s->low_delay) {
01274 *pict= *(AVFrame*)s->current_picture_ptr;
01275 } else if (s->last_picture_ptr != NULL) {
01276 *pict= *(AVFrame*)s->last_picture_ptr;
01277 }
01278
01279 if(s->last_picture_ptr || s->low_delay){
01280 *data_size = sizeof(AVFrame);
01281 ff_print_debug_info(s, pict);
01282 }
01283 s->current_picture_ptr= NULL;
01284 }
01285 return buf_size;
01286 }
01287
01288 int ff_rv34_decode_end(AVCodecContext *avctx)
01289 {
01290 RV34DecContext *r = avctx->priv_data;
01291
01292 MPV_common_end(&r->s);
01293
01294 av_freep(&r->intra_types_hist);
01295 r->intra_types = NULL;
01296 av_freep(&r->mb_type);
01297
01298 return 0;
01299 }