170
176
/** @} */ // vlc group
174
* @defgroup transform RV30/40 inverse transform functions
178
static av_always_inline void rv34_row_transform(int temp[16], DCTELEM *block)
183
const int z0= 13*(block[i+8*0] + block[i+8*2]);
184
const int z1= 13*(block[i+8*0] - block[i+8*2]);
185
const int z2= 7* block[i+8*1] - 17*block[i+8*3];
186
const int z3= 17* block[i+8*1] + 7*block[i+8*3];
196
* Real Video 3.0/4.0 inverse transform
197
* Code is almost the same as in SVQ3, only scaling is different.
199
static void rv34_inv_transform(DCTELEM *block){
203
rv34_row_transform(temp, block);
206
const int z0= 13*(temp[4*0+i] + temp[4*2+i]) + 0x200;
207
const int z1= 13*(temp[4*0+i] - temp[4*2+i]) + 0x200;
208
const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
209
const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
211
block[i*8+0]= (z0 + z3)>>10;
212
block[i*8+1]= (z1 + z2)>>10;
213
block[i*8+2]= (z1 - z2)>>10;
214
block[i*8+3]= (z0 - z3)>>10;
220
* RealVideo 3.0/4.0 inverse transform for DC block
222
* Code is almost the same as rv34_inv_transform()
223
* but final coefficients are multiplied by 1.5 and have no rounding.
225
static void rv34_inv_transform_noround(DCTELEM *block){
229
rv34_row_transform(temp, block);
232
const int z0= 13*(temp[4*0+i] + temp[4*2+i]);
233
const int z1= 13*(temp[4*0+i] - temp[4*2+i]);
234
const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];
235
const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];
237
block[i*8+0]= ((z0 + z3)*3)>>11;
238
block[i*8+1]= ((z1 + z2)*3)>>11;
239
block[i*8+2]= ((z1 - z2)*3)>>11;
240
block[i*8+3]= ((z0 - z3)*3)>>11;
245
/** @} */ // transform
249
* @defgroup block RV30/40 4x4 block decoding functions
179
* @name RV30/40 4x4 block decoding functions
300
230
if(get_bits1(gb))
232
*dst = (coef*q + 8) >> 4;
307
237
* Decode 2x2 subblock of coefficients.
309
static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc)
313
coeffs[0] = modulo_three_table[code][0];
314
coeffs[1] = modulo_three_table[code][1];
315
coeffs[2] = modulo_three_table[code][2];
316
coeffs[3] = modulo_three_table[code][3];
317
decode_coeff(dst , coeffs[0], 3, gb, vlc);
319
decode_coeff(dst+8, coeffs[1], 2, gb, vlc);
320
decode_coeff(dst+1, coeffs[2], 2, gb, vlc);
322
decode_coeff(dst+1, coeffs[1], 2, gb, vlc);
323
decode_coeff(dst+8, coeffs[2], 2, gb, vlc);
325
decode_coeff(dst+9, coeffs[3], 2, gb, vlc);
239
static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q)
243
coeffs[0] = modulo_three_table[code][0];
244
coeffs[1] = modulo_three_table[code][1];
245
coeffs[2] = modulo_three_table[code][2];
246
coeffs[3] = modulo_three_table[code][3];
247
decode_coeff(dst , coeffs[0], 3, gb, vlc, q);
249
decode_coeff(dst+8, coeffs[1], 2, gb, vlc, q);
250
decode_coeff(dst+1, coeffs[2], 2, gb, vlc, q);
252
decode_coeff(dst+1, coeffs[1], 2, gb, vlc, q);
253
decode_coeff(dst+8, coeffs[2], 2, gb, vlc, q);
255
decode_coeff(dst+9, coeffs[3], 2, gb, vlc, q);
258
static inline void decode_subblock3(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc,
259
int q_dc, int q_ac1, int q_ac2)
263
coeffs[0] = modulo_three_table[code][0];
264
coeffs[1] = modulo_three_table[code][1];
265
coeffs[2] = modulo_three_table[code][2];
266
coeffs[3] = modulo_three_table[code][3];
267
decode_coeff(dst , coeffs[0], 3, gb, vlc, q_dc);
269
decode_coeff(dst+8, coeffs[1], 2, gb, vlc, q_ac1);
270
decode_coeff(dst+1, coeffs[2], 2, gb, vlc, q_ac1);
272
decode_coeff(dst+1, coeffs[1], 2, gb, vlc, q_ac1);
273
decode_coeff(dst+8, coeffs[2], 2, gb, vlc, q_ac1);
275
decode_coeff(dst+9, coeffs[3], 2, gb, vlc, q_ac2);
345
295
pattern = code & 0x7;
348
decode_subblock(dst, code, 0, gb, &rvlc->coefficient);
298
decode_subblock3(dst, code, 0, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2);
351
301
code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
352
decode_subblock(dst + 2, code, 0, gb, &rvlc->coefficient);
302
decode_subblock(dst + 2, code, 0, gb, &rvlc->coefficient, q_ac2);
354
304
if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
355
305
code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
356
decode_subblock(dst + 8*2, code, 1, gb, &rvlc->coefficient);
306
decode_subblock(dst + 8*2, code, 1, gb, &rvlc->coefficient, q_ac2);
359
309
code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
360
decode_subblock(dst + 8*2+2, code, 0, gb, &rvlc->coefficient);
310
decode_subblock(dst + 8*2+2, code, 0, gb, &rvlc->coefficient, q_ac2);
366
* Dequantize ordinary 4x4 block.
369
static inline void rv34_dequant4x4(DCTELEM *block, int Qdc, int Q)
373
block[0] = (block[0] * Qdc + 8) >> 4;
374
for(i = 0; i < 4; i++)
375
for(j = !i; j < 4; j++)
376
block[j + i*8] = (block[j + i*8] * Q + 8) >> 4;
380
* Dequantize 4x4 block of DC values for 16x16 macroblock.
383
static inline void rv34_dequant4x4_16x16(DCTELEM *block, int Qdc, int Q)
387
for(i = 0; i < 3; i++)
388
block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Qdc + 8) >> 4;
390
block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Q + 8) >> 4;
392
/** @} */ //block functions
396
* @defgroup rv3040_bitstream RV30/40 bitstream parsing
316
* @name RV30/40 bitstream parsing
425
* Decode quantizer difference and return modified quantizer.
345
* Decode macroblock header and return CBP in case of success, -1 otherwise.
427
static inline int rv34_decode_dquant(GetBitContext *gb, int quant)
347
static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
430
return rv34_dquant_tab[get_bits1(gb)][quant];
432
return get_bits(gb, 5);
349
MpegEncContext *s = &r->s;
350
GetBitContext *gb = &s->gb;
351
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
355
r->is16 = get_bits1(gb);
356
if(!r->is16 && !r->rv30){
358
av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
360
s->current_picture_ptr->f.mb_type[mb_pos] = r->is16 ? MB_TYPE_INTRA16x16 : MB_TYPE_INTRA;
361
r->block_type = r->is16 ? RV34_MB_TYPE_INTRA16x16 : RV34_MB_TYPE_INTRA;
363
r->block_type = r->decode_mb_info(r);
364
if(r->block_type == -1)
366
s->current_picture_ptr->f.mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
367
r->mb_type[mb_pos] = r->block_type;
368
if(r->block_type == RV34_MB_SKIP){
369
if(s->pict_type == AV_PICTURE_TYPE_P)
370
r->mb_type[mb_pos] = RV34_MB_P_16x16;
371
if(s->pict_type == AV_PICTURE_TYPE_B)
372
r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
374
r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->f.mb_type[mb_pos]);
375
rv34_decode_mv(r, r->block_type);
376
if(r->block_type == RV34_MB_SKIP){
377
fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
383
if(IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
386
fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
389
if(r->decode_intra_types(r, gb, intra_types) < 0)
394
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
396
for(i = 0; i < 16; i++)
397
intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
398
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
399
if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
403
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
407
return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
435
410
/** @} */ //bitstream functions
438
* @defgroup mv motion vector related code (prediction, reconstruction, motion compensation)
413
* @name motion vector related code (prediction, reconstruction, motion compensation)
472
447
if(r->avail_cache[avail_index - 1]){
473
A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
474
A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
448
A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][0];
449
A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][1];
476
451
if(r->avail_cache[avail_index - 4]){
477
B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
478
B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
452
B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][0];
453
B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][1];
483
458
if(!r->avail_cache[avail_index - 4 + c_off]){
484
459
if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){
485
C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
486
C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
460
C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][0];
461
C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][1];
492
C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
493
C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
467
C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][0];
468
C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][1];
495
470
mx = mid_pred(A[0], B[0], C[0]);
496
471
my = mid_pred(A[1], B[1], C[1]);
555
526
Picture *cur_pic = s->current_picture_ptr;
556
527
const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
557
int type = cur_pic->mb_type[mb_pos];
528
int type = cur_pic->f.mb_type[mb_pos];
559
530
memset(A, 0, sizeof(A));
560
531
memset(B, 0, sizeof(B));
561
532
memset(C, 0, sizeof(C));
562
533
if((r->avail_cache[6-1] & type) & mask){
563
A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
564
A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
534
A[0] = cur_pic->f.motion_val[dir][mv_pos - 1][0];
535
A[1] = cur_pic->f.motion_val[dir][mv_pos - 1][1];
567
538
if((r->avail_cache[6-4] & type) & mask){
568
B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
569
B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
539
B[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][0];
540
B[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][1];
572
543
if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
573
C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
574
C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
544
C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][0];
545
C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][1];
576
547
}else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
577
C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
578
C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
548
C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][0];
549
C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][1];
608
579
int avail_index = avail_indexes[0];
610
581
if(r->avail_cache[avail_index - 1]){
611
A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
612
A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
582
A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][0];
583
A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][1];
614
585
if(r->avail_cache[avail_index - 4]){
615
B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
616
B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
586
B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][0];
587
B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][1];
621
592
if(!r->avail_cache[avail_index - 4 + 2]){
622
593
if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1])){
623
C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
624
C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
594
C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][0];
595
C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][1];
630
C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+2][0];
631
C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+2][1];
601
C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][0];
602
C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][1];
633
604
mx = mid_pred(A[0], B[0], C[0]);
634
605
my = mid_pred(A[1], B[1], C[1]);
678
649
int chroma_mx, chroma_my;
679
mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
680
my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
681
lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
682
ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
683
chroma_mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + 1) >> 1;
684
chroma_my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + 1) >> 1;
650
mx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
651
my = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
652
lx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
653
ly = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
654
chroma_mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
655
chroma_my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
685
656
umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
686
657
umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
687
658
uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
688
659
uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
691
mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
692
my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
693
lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
694
ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
695
cx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
696
cy = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
662
mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] >> 2;
663
my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] >> 2;
664
lx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] & 3;
665
ly = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] & 3;
666
cx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
667
cy = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
699
670
uvmx = (cx & 3) << 1;
702
673
if(uvmx == 6 && uvmy == 6)
677
if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
678
/* wait for the referenced mb row to be finished */
679
int mb_row = FFMIN(s->mb_height - 1, s->mb_y + ((yoff + my + 21) >> 4));
680
AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f;
681
ff_thread_await_progress(f, mb_row, 0);
706
srcY = dir ? s->next_picture_ptr->data[0] : s->last_picture_ptr->data[0];
707
srcU = dir ? s->next_picture_ptr->data[1] : s->last_picture_ptr->data[1];
708
srcV = dir ? s->next_picture_ptr->data[2] : s->last_picture_ptr->data[2];
685
srcY = dir ? s->next_picture_ptr->f.data[0] : s->last_picture_ptr->f.data[0];
686
srcU = dir ? s->next_picture_ptr->f.data[1] : s->last_picture_ptr->f.data[1];
687
srcV = dir ? s->next_picture_ptr->f.data[2] : s->last_picture_ptr->f.data[2];
709
688
src_x = s->mb_x * 16 + xoff + mx;
710
689
src_y = s->mb_y * 16 + yoff + my;
711
690
uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
713
692
srcY += src_y * s->linesize + src_x;
714
693
srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
715
694
srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
716
if( (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4
717
|| (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4){
718
uint8_t *uvbuf= s->edge_emu_buffer + 22 * s->linesize;
695
if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
696
(unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
697
(unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
698
uint8_t *uvbuf = s->edge_emu_buffer + 22 * s->linesize;
720
700
srcY -= 2 + 2*s->linesize;
721
701
s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+6, (height<<3)+6,
751
737
const int xoff, const int yoff, int mv_off,
752
738
const int width, const int height, int dir)
754
rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30,
755
r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
756
: r->s.dsp.put_rv40_qpel_pixels_tab,
757
r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
758
: r->s.dsp.put_rv40_chroma_pixels_tab);
740
rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
741
r->rdsp.put_pixels_tab,
742
r->rdsp.put_chroma_pixels_tab);
745
static void rv4_weight(RV34DecContext *r)
747
r->rdsp.rv40_weight_pixels_tab[0](r->s.dest[0],
753
r->rdsp.rv40_weight_pixels_tab[1](r->s.dest[1],
754
r->tmp_b_block_uv[0],
755
r->tmp_b_block_uv[2],
759
r->rdsp.rv40_weight_pixels_tab[1](r->s.dest[2],
760
r->tmp_b_block_uv[1],
761
r->tmp_b_block_uv[3],
761
767
static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
763
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,
764
r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
765
: r->s.dsp.put_rv40_qpel_pixels_tab,
766
r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
767
: r->s.dsp.put_rv40_chroma_pixels_tab);
768
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,
769
r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
770
: r->s.dsp.avg_rv40_qpel_pixels_tab,
771
r->rv30 ? r->s.dsp.avg_h264_chroma_pixels_tab
772
: r->s.dsp.avg_rv40_chroma_pixels_tab);
769
int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
771
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
772
r->rdsp.put_pixels_tab,
773
r->rdsp.put_chroma_pixels_tab);
775
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
776
r->rdsp.avg_pixels_tab,
777
r->rdsp.avg_chroma_pixels_tab);
779
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
780
r->rdsp.put_pixels_tab,
781
r->rdsp.put_chroma_pixels_tab);
775
786
static void rv34_mc_2mv_skip(RV34DecContext *r)
789
int weighted = !r->rv30 && r->weight1 != 8192;
778
791
for(j = 0; j < 2; j++)
779
792
for(i = 0; i < 2; i++){
780
793
rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
781
r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
782
: r->s.dsp.put_rv40_qpel_pixels_tab,
783
r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
784
: r->s.dsp.put_rv40_chroma_pixels_tab);
795
r->rdsp.put_pixels_tab,
796
r->rdsp.put_chroma_pixels_tab);
785
797
rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
786
r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
787
: r->s.dsp.avg_rv40_qpel_pixels_tab,
788
r->rv30 ? r->s.dsp.avg_h264_chroma_pixels_tab
789
: r->s.dsp.avg_rv40_chroma_pixels_tab);
799
weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
800
weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
793
806
/** number of motion vectors in each macroblock type */
813
826
switch(block_type){
814
827
case RV34_MB_TYPE_INTRA:
815
828
case RV34_MB_TYPE_INTRA16x16:
816
ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
829
ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
818
831
case RV34_MB_SKIP:
819
832
if(s->pict_type == AV_PICTURE_TYPE_P){
820
ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
833
ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
821
834
rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
824
837
case RV34_MB_B_DIRECT:
825
838
//surprisingly, it uses motion scheme from next reference frame
826
next_bt = s->next_picture_ptr->mb_type[s->mb_x + s->mb_y * s->mb_stride];
839
/* wait for the current mb row to be finished */
840
if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
841
ff_thread_await_progress(&s->next_picture_ptr->f, s->mb_y - 1, 0);
843
next_bt = s->next_picture_ptr->f.mb_type[s->mb_x + s->mb_y * s->mb_stride];
827
844
if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
828
ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
829
ZERO8x2(s->current_picture_ptr->motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
845
ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
846
ZERO8x2(s->current_picture_ptr->f.motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
831
848
for(j = 0; j < 2; j++)
832
849
for(i = 0; i < 2; i++)
833
850
for(k = 0; k < 2; k++)
834
851
for(l = 0; l < 2; l++)
835
s->current_picture_ptr->motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][k]);
852
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]);
836
853
if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
837
854
rv34_mc_2mv(r, block_type);
839
856
rv34_mc_2mv_skip(r);
840
ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
857
ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
842
859
case RV34_MB_P_16x16:
843
860
case RV34_MB_P_MIX16x16:
1030
/** @} */ // recons group
1033
* @addtogroup bitstream
1034
* Decode macroblock header and return CBP in case of success, -1 otherwise.
1036
static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
1038
MpegEncContext *s = &r->s;
1039
GetBitContext *gb = &s->gb;
1040
int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1044
r->is16 = get_bits1(gb);
1045
if(!r->is16 && !r->rv30){
1047
av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
1049
s->current_picture_ptr->mb_type[mb_pos] = r->is16 ? MB_TYPE_INTRA16x16 : MB_TYPE_INTRA;
1050
r->block_type = r->is16 ? RV34_MB_TYPE_INTRA16x16 : RV34_MB_TYPE_INTRA;
1052
r->block_type = r->decode_mb_info(r);
1053
if(r->block_type == -1)
1055
s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
1056
r->mb_type[mb_pos] = r->block_type;
1057
if(r->block_type == RV34_MB_SKIP){
1058
if(s->pict_type == AV_PICTURE_TYPE_P)
1059
r->mb_type[mb_pos] = RV34_MB_P_16x16;
1060
if(s->pict_type == AV_PICTURE_TYPE_B)
1061
r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
1063
r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
1064
rv34_decode_mv(r, r->block_type);
1065
if(r->block_type == RV34_MB_SKIP){
1066
fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
1072
if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
1074
t = get_bits(gb, 2);
1075
fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
1078
if(r->decode_intra_types(r, gb, intra_types) < 0)
1083
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
1085
for(i = 0; i < 16; i++)
1086
intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
1087
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
1088
if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
1092
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
1096
return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
1100
* @addtogroup recons
1104
1048
* mask for retrieving all bits in coded block pattern
1105
1049
* corresponding to one 8x8 block
1201
1148
r->deblock_coefs[mb_pos] = 0xFFFF;
1203
1150
r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
1204
s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
1151
s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
1209
1156
luma_dc_quant = r->block_type == RV34_MB_P_MIX16x16 ? r->luma_dc_quant_p[s->qscale] : r->luma_dc_quant_i[s->qscale];
1211
memset(block16, 0, sizeof(block16));
1212
rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0);
1213
rv34_dequant4x4_16x16(block16, rv34_qscale_tab[luma_dc_quant],rv34_qscale_tab[s->qscale]);
1214
rv34_inv_transform_noround(block16);
1158
q_dc = rv34_qscale_tab[luma_dc_quant];
1159
q_ac = rv34_qscale_tab[s->qscale];
1160
memset(block16, 0, 64 * sizeof(*block16));
1161
rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
1162
r->rdsp.rv34_inv_transform_tab[1](block16);
1165
q_ac = rv34_qscale_tab[s->qscale];
1217
1166
for(i = 0; i < 16; i++, cbp >>= 1){
1218
1167
if(!r->is16 && !(cbp & 1)) continue;
1219
1168
blknum = ((i & 2) >> 1) + ((i & 8) >> 2);
1220
1169
blkoff = ((i & 1) << 2) + ((i & 4) << 3);
1222
rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->luma_vlc, 0);
1223
rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[s->qscale],rv34_qscale_tab[s->qscale]);
1171
rv34_decode_block(s->block[blknum] + blkoff, gb,
1172
r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1224
1173
if(r->is16) //FIXME: optimize
1225
1174
s->block[blknum][blkoff] = block16[(i & 3) | ((i & 0xC) << 1)];
1226
rv34_inv_transform(s->block[blknum] + blkoff);
1175
r->rdsp.rv34_inv_transform_tab[0](s->block[blknum] + blkoff);
1228
1177
if(r->block_type == RV34_MB_P_MIX16x16)
1229
1178
r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
1179
q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1180
q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1230
1181
for(; i < 24; i++, cbp >>= 1){
1231
1182
if(!(cbp & 1)) continue;
1232
1183
blknum = ((i & 4) >> 2) + 4;
1233
1184
blkoff = ((i & 1) << 2) + ((i & 2) << 4);
1234
rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->chroma_vlc, 1);
1235
rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]],rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]]);
1236
rv34_inv_transform(s->block[blknum] + blkoff);
1185
rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->chroma_vlc, 1, q_dc, q_ac, q_ac);
1186
r->rdsp.rv34_inv_transform_tab[0](s->block[blknum] + blkoff);
1238
if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos]))
1188
if (IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos]))
1239
1189
rv34_output_macroblock(r, intra_types, cbp2, r->is16);
1241
1191
rv34_apply_differences(r, cbp2);
1297
1238
r->cbp_luma = av_realloc(r->cbp_luma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_luma));
1298
1239
r->cbp_chroma = av_realloc(r->cbp_chroma, r->s.mb_stride * r->s.mb_height * sizeof(*r->cbp_chroma));
1299
1240
r->deblock_coefs = av_realloc(r->deblock_coefs, r->s.mb_stride * r->s.mb_height * sizeof(*r->deblock_coefs));
1241
av_freep(&r->tmp_b_block_base);
1301
1243
s->pict_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
1302
1244
if(MPV_frame_start(s, s->avctx) < 0)
1304
1246
ff_er_frame_start(s);
1247
if (!r->tmp_b_block_base) {
1250
r->tmp_b_block_base = av_malloc(s->linesize * 48);
1251
for (i = 0; i < 2; i++)
1252
r->tmp_b_block_y[i] = r->tmp_b_block_base + i * 16 * s->linesize;
1253
for (i = 0; i < 4; i++)
1254
r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
1255
+ (i >> 1) * 8 * s->uvlinesize + (i & 1) * 16;
1305
1257
r->cur_pts = r->si.pts;
1306
1258
if(s->pict_type != AV_PICTURE_TYPE_B){
1307
1259
r->last_pts = r->next_pts;
1308
1260
r->next_pts = r->cur_pts;
1262
int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
1263
int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
1264
int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
1267
r->weight1 = r->weight2 = 8192;
1269
r->weight1 = (dist0 << 14) / refdist;
1270
r->weight2 = (dist1 << 14) / refdist;
1310
1273
s->mb_x = s->mb_y = 0;
1274
ff_thread_finish_setup(s->avctx);
1276
int slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
1278
if (slice_type != s->pict_type) {
1279
av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
1280
return AVERROR_INVALIDDATA;
1313
1284
r->si.end = end;
1314
1285
s->qscale = r->si.quant;
1315
r->bits = buf_size*8;
1316
1286
s->mb_num_left = r->si.end - r->si.start;
1317
1287
r->s.mb_skip_run = 0;
1389
int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
1391
RV34DecContext *r = avctx->priv_data;
1395
if (avctx->internal->is_copy) {
1396
r->cbp_chroma = av_malloc(r->s.mb_stride * r->s.mb_height *
1397
sizeof(*r->cbp_chroma));
1398
r->cbp_luma = av_malloc(r->s.mb_stride * r->s.mb_height *
1399
sizeof(*r->cbp_luma));
1400
r->deblock_coefs = av_malloc(r->s.mb_stride * r->s.mb_height *
1401
sizeof(*r->deblock_coefs));
1402
r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
1403
sizeof(*r->intra_types_hist));
1404
r->mb_type = av_malloc(r->s.mb_stride * r->s.mb_height *
1405
sizeof(*r->mb_type));
1407
if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs &&
1408
r->intra_types_hist && r->mb_type)) {
1409
av_freep(&r->cbp_chroma);
1410
av_freep(&r->cbp_luma);
1411
av_freep(&r->deblock_coefs);
1412
av_freep(&r->intra_types_hist);
1413
av_freep(&r->mb_type);
1414
r->intra_types = NULL;
1415
return AVERROR(ENOMEM);
1418
r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
1419
r->tmp_b_block_base = NULL;
1421
memset(r->mb_type, 0, r->s.mb_stride * r->s.mb_height *
1422
sizeof(*r->mb_type));
1424
MPV_common_init(&r->s);
1429
int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1431
RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
1432
MpegEncContext * const s = &r->s, * const s1 = &r1->s;
1435
if (dst == src || !s1->context_initialized)
1438
if ((err = ff_mpeg_update_thread_context(dst, src)))
1441
r->cur_pts = r1->cur_pts;
1442
r->last_pts = r1->last_pts;
1443
r->next_pts = r1->next_pts;
1445
memset(&r->si, 0, sizeof(r->si));
1447
/* necessary since it is it the condition checked for in decode_slice
1448
* to call MPV_frame_start. cmp. comment at the end of decode_frame */
1449
s->current_picture_ptr = NULL;
1405
1454
static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
1407
1456
if(avctx->slice_count) return avctx->slice_offset[n];
1439
1488
slice_count = (*buf++) + 1;
1440
1489
slices_hdr = buf + 4;
1441
1490
buf += 8 * slice_count;
1491
buf_size -= 1 + 8 * slice_count;
1443
1493
slice_count = avctx->slice_count;
1445
1495
//parse first slice header to check whether this frame can be decoded
1446
if(get_slice_offset(avctx, slices_hdr, 0) > buf_size){
1447
av_log(avctx, AV_LOG_ERROR, "Slice offset is greater than frame size\n");
1496
if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
1497
get_slice_offset(avctx, slices_hdr, 0) > buf_size){
1498
av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1450
init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), buf_size-get_slice_offset(avctx, slices_hdr, 0));
1501
init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
1451
1502
if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
1452
1503
av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
1455
if((!s->last_picture_ptr || !s->last_picture_ptr->data[0]) && si.type == AV_PICTURE_TYPE_B)
1506
if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) && si.type == AV_PICTURE_TYPE_B)
1457
1508
if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
1458
1509
|| (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
1459
1510
|| avctx->skip_frame >= AVDISCARD_ALL)
1462
for(i=0; i<slice_count; i++){
1463
int offset= get_slice_offset(avctx, slices_hdr, i);
1513
for(i = 0; i < slice_count; i++){
1514
int offset = get_slice_offset(avctx, slices_hdr, i);
1465
1516
if(i+1 == slice_count)
1466
size= buf_size - offset;
1517
size = buf_size - offset;
1468
size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
1519
size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
1470
if(offset > buf_size){
1471
av_log(avctx, AV_LOG_ERROR, "Slice offset is greater than frame size\n");
1521
if(offset < 0 || offset > buf_size){
1522
av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1475
1526
r->si.end = s->mb_width * s->mb_height;
1476
1527
if(i+1 < slice_count){
1528
if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||
1529
get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {
1530
av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1477
1533
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);
1478
1534
if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
1479
1535
if(i+2 < slice_count)
1484
1540
r->si.end = si.start;
1542
if (size < 0 || size > buf_size - offset) {
1543
av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
1486
1546
last = rv34_decode_slice(r, r->si.end, buf + offset, size);
1487
1547
s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
1552
if(last && s->current_picture_ptr){
1493
1553
if(r->loop_filter)
1494
1554
r->loop_filter(r, s->mb_height - 1);
1555
if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
1556
ff_thread_report_progress(&s->current_picture_ptr->f,
1557
s->mb_height - 1, 0);
1495
1558
ff_er_frame_end(s);
1496
1559
MPV_frame_end(s);
1497
1560
if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1498
*pict= *(AVFrame*)s->current_picture_ptr;
1561
*pict = *(AVFrame*)s->current_picture_ptr;
1499
1562
} else if (s->last_picture_ptr != NULL) {
1500
*pict= *(AVFrame*)s->last_picture_ptr;
1563
*pict = *(AVFrame*)s->last_picture_ptr;
1503
1566
if(s->last_picture_ptr || s->low_delay){
1504
1567
*data_size = sizeof(AVFrame);
1505
1568
ff_print_debug_info(s, pict);
1507
s->current_picture_ptr= NULL; //so we can detect if frame_end wasnt called (find some nicer solution...)
1570
s->current_picture_ptr = NULL; //so we can detect if frame_end wasnt called (find some nicer solution...)
1512
1575
av_cold int ff_rv34_decode_end(AVCodecContext *avctx)