~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/rv34.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 * RV30/40 decoder common data
25
25
 */
26
26
 
 
27
#include "libavutil/internal.h"
 
28
 
27
29
#include "avcodec.h"
28
30
#include "dsputil.h"
29
31
#include "mpegvideo.h"
30
32
#include "golomb.h"
 
33
#include "internal.h"
31
34
#include "mathops.h"
32
35
#include "rectangle.h"
 
36
#include "thread.h"
33
37
 
34
38
#include "rv34vlc.h"
35
39
#include "rv34data.h"
62
66
 
63
67
static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
64
68
 
 
69
static int rv34_decode_mv(RV34DecContext *r, int block_type);
 
70
 
65
71
/**
66
 
 * @defgroup vlc RV30/40 VLC generating functions
 
72
 * @name RV30/40 VLC generating functions
67
73
 * @{
68
74
 */
69
75
 
169
175
 
170
176
/** @} */ // vlc group
171
177
 
172
 
 
173
 
/**
174
 
 * @defgroup transform RV30/40 inverse transform functions
175
 
 * @{
176
 
 */
177
 
 
178
 
static av_always_inline void rv34_row_transform(int temp[16], DCTELEM *block)
179
 
{
180
 
    int i;
181
 
 
182
 
    for(i=0; i<4; i++){
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];
187
 
 
188
 
        temp[4*i+0]= z0+z3;
189
 
        temp[4*i+1]= z1+z2;
190
 
        temp[4*i+2]= z1-z2;
191
 
        temp[4*i+3]= z0-z3;
192
 
    }
193
 
}
194
 
 
195
 
/**
196
 
 * Real Video 3.0/4.0 inverse transform
197
 
 * Code is almost the same as in SVQ3, only scaling is different.
198
 
 */
199
 
static void rv34_inv_transform(DCTELEM *block){
200
 
    int temp[16];
201
 
    int i;
202
 
 
203
 
    rv34_row_transform(temp, block);
204
 
 
205
 
    for(i=0; i<4; i++){
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];
210
 
 
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;
215
 
    }
216
 
 
217
 
}
218
 
 
219
 
/**
220
 
 * RealVideo 3.0/4.0 inverse transform for DC block
221
 
 *
222
 
 * Code is almost the same as rv34_inv_transform()
223
 
 * but final coefficients are multiplied by 1.5 and have no rounding.
224
 
 */
225
 
static void rv34_inv_transform_noround(DCTELEM *block){
226
 
    int temp[16];
227
 
    int i;
228
 
 
229
 
    rv34_row_transform(temp, block);
230
 
 
231
 
    for(i=0; i<4; i++){
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];
236
 
 
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;
241
 
    }
242
 
 
243
 
}
244
 
 
245
 
/** @} */ // transform
246
 
 
247
 
 
248
 
/**
249
 
 * @defgroup block RV30/40 4x4 block decoding functions
 
178
/**
 
179
 * @name RV30/40 4x4 block decoding functions
250
180
 * @{
251
181
 */
252
182
 
286
216
/**
287
217
 * Get one coefficient value from the bistream and store it.
288
218
 */
289
 
static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc)
 
219
static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc, int q)
290
220
{
291
221
    if(coef){
292
222
        if(coef == esc){
299
229
        }
300
230
        if(get_bits1(gb))
301
231
            coef = -coef;
302
 
        *dst = coef;
 
232
        *dst = (coef*q + 8) >> 4;
303
233
    }
304
234
}
305
235
 
306
236
/**
307
237
 * Decode 2x2 subblock of coefficients.
308
238
 */
309
 
static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc)
310
 
{
311
 
    int coeffs[4];
312
 
 
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);
318
 
    if(is_block2){
319
 
        decode_coeff(dst+8, coeffs[1], 2, gb, vlc);
320
 
        decode_coeff(dst+1, coeffs[2], 2, gb, vlc);
321
 
    }else{
322
 
        decode_coeff(dst+1, coeffs[1], 2, gb, vlc);
323
 
        decode_coeff(dst+8, coeffs[2], 2, gb, vlc);
324
 
    }
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)
 
240
{
 
241
    int coeffs[4];
 
242
 
 
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);
 
248
    if(is_block2){
 
249
        decode_coeff(dst+8, coeffs[1], 2, gb, vlc, q);
 
250
        decode_coeff(dst+1, coeffs[2], 2, gb, vlc, q);
 
251
    }else{
 
252
        decode_coeff(dst+1, coeffs[1], 2, gb, vlc, q);
 
253
        decode_coeff(dst+8, coeffs[2], 2, gb, vlc, q);
 
254
    }
 
255
    decode_coeff(dst+9, coeffs[3], 2, gb, vlc, q);
 
256
}
 
257
 
 
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)
 
260
{
 
261
    int coeffs[4];
 
262
 
 
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);
 
268
    if(is_block2){
 
269
        decode_coeff(dst+8, coeffs[1], 2, gb, vlc, q_ac1);
 
270
        decode_coeff(dst+1, coeffs[2], 2, gb, vlc, q_ac1);
 
271
    }else{
 
272
        decode_coeff(dst+1, coeffs[1], 2, gb, vlc, q_ac1);
 
273
        decode_coeff(dst+8, coeffs[2], 2, gb, vlc, q_ac1);
 
274
    }
 
275
    decode_coeff(dst+9, coeffs[3], 2, gb, vlc, q_ac2);
326
276
}
327
277
 
328
278
/**
336
286
 *  o--o
337
287
 */
338
288
 
339
 
static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc)
 
289
static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2)
340
290
{
341
291
    int code, pattern;
342
292
 
345
295
    pattern = code & 0x7;
346
296
 
347
297
    code >>= 3;
348
 
    decode_subblock(dst, code, 0, gb, &rvlc->coefficient);
 
298
    decode_subblock3(dst, code, 0, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2);
349
299
 
350
300
    if(pattern & 4){
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);
353
303
    }
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);
357
307
    }
358
308
    if(pattern & 1){
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);
361
311
    }
362
312
 
363
313
}
364
314
 
365
315
/**
366
 
 * Dequantize ordinary 4x4 block.
367
 
 * @todo optimize
368
 
 */
369
 
static inline void rv34_dequant4x4(DCTELEM *block, int Qdc, int Q)
370
 
{
371
 
    int i, j;
372
 
 
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;
377
 
}
378
 
 
379
 
/**
380
 
 * Dequantize 4x4 block of DC values for 16x16 macroblock.
381
 
 * @todo optimize
382
 
 */
383
 
static inline void rv34_dequant4x4_16x16(DCTELEM *block, int Qdc, int Q)
384
 
{
385
 
    int i;
386
 
 
387
 
    for(i = 0; i < 3; i++)
388
 
         block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Qdc + 8) >> 4;
389
 
    for(; i < 16; i++)
390
 
         block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Q + 8) >> 4;
391
 
}
392
 
/** @} */ //block functions
393
 
 
394
 
 
395
 
/**
396
 
 * @defgroup rv3040_bitstream RV30/40 bitstream parsing
 
316
 * @name RV30/40 bitstream parsing
397
317
 * @{
398
318
 */
399
319
 
422
342
}
423
343
 
424
344
/**
425
 
 * Decode quantizer difference and return modified quantizer.
 
345
 * Decode macroblock header and return CBP in case of success, -1 otherwise.
426
346
 */
427
 
static inline int rv34_decode_dquant(GetBitContext *gb, int quant)
 
347
static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
428
348
{
429
 
    if(get_bits1(gb))
430
 
        return rv34_dquant_tab[get_bits1(gb)][quant];
431
 
    else
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;
 
352
    int i, t;
 
353
 
 
354
    if(!r->si.type){
 
355
        r->is16 = get_bits1(gb);
 
356
        if(!r->is16 && !r->rv30){
 
357
            if(!get_bits1(gb))
 
358
                av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
 
359
        }
 
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;
 
362
    }else{
 
363
        r->block_type = r->decode_mb_info(r);
 
364
        if(r->block_type == -1)
 
365
            return -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;
 
373
        }
 
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]));
 
378
            return 0;
 
379
        }
 
380
        r->chroma_vlc = 1;
 
381
        r->luma_vlc   = 0;
 
382
    }
 
383
    if(IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
 
384
        if(r->is16){
 
385
            t = get_bits(gb, 2);
 
386
            fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
 
387
            r->luma_vlc   = 2;
 
388
        }else{
 
389
            if(r->decode_intra_types(r, gb, intra_types) < 0)
 
390
                return -1;
 
391
            r->luma_vlc   = 1;
 
392
        }
 
393
        r->chroma_vlc = 0;
 
394
        r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
 
395
    }else{
 
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){
 
400
            r->is16 = 1;
 
401
            r->chroma_vlc = 1;
 
402
            r->luma_vlc   = 2;
 
403
            r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
 
404
        }
 
405
    }
 
406
 
 
407
    return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
433
408
}
434
409
 
435
410
/** @} */ //bitstream functions
436
411
 
437
412
/**
438
 
 * @defgroup mv motion vector related code (prediction, reconstruction, motion compensation)
 
413
 * @name motion vector related code (prediction, reconstruction, motion compensation)
439
414
 * @{
440
415
 */
441
416
 
470
445
        c_off = -1;
471
446
 
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];
475
450
    }
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];
479
454
    }else{
480
455
        B[0] = A[0];
481
456
        B[1] = A[1];
482
457
    }
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];
487
462
        }else{
488
463
            C[0] = A[0];
489
464
            C[1] = A[1];
490
465
        }
491
466
    }else{
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];
494
469
    }
495
470
    mx = mid_pred(A[0], B[0], C[0]);
496
471
    my = mid_pred(A[1], B[1], C[1]);
498
473
    my += r->dmv[dmv_no][1];
499
474
    for(j = 0; j < part_sizes_h[block_type]; j++){
500
475
        for(i = 0; i < part_sizes_w[block_type]; i++){
501
 
            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
502
 
            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
 
476
            s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
 
477
            s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
503
478
        }
504
479
    }
505
480
}
511
486
 */
512
487
static int calc_add_mv(RV34DecContext *r, int dir, int val)
513
488
{
514
 
    int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
515
 
    int dist = dir ? -GET_PTS_DIFF(r->next_pts, r->cur_pts) : GET_PTS_DIFF(r->cur_pts, r->last_pts);
516
 
    int mul;
 
489
    int mul = dir ? -r->weight2 : r->weight1;
517
490
 
518
 
    if(!refdist) return 0;
519
 
    mul = (dist << 14) / refdist;
520
491
    return (val * mul + 0x2000) >> 14;
521
492
}
522
493
 
554
525
    int i, j;
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];
558
529
 
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];
565
536
        has_A = 1;
566
537
    }
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];
570
541
        has_B = 1;
571
542
    }
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];
575
546
        has_C = 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];
579
550
        has_C = 1;
580
551
    }
581
552
 
586
557
 
587
558
    for(j = 0; j < 2; j++){
588
559
        for(i = 0; i < 2; i++){
589
 
            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
590
 
            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
 
560
            cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
 
561
            cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
591
562
        }
592
563
    }
593
564
    if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
594
 
        ZERO8x2(cur_pic->motion_val[!dir][mv_pos], s->b8_stride);
 
565
        ZERO8x2(cur_pic->f.motion_val[!dir][mv_pos], s->b8_stride);
595
566
    }
596
567
}
597
568
 
608
579
    int avail_index = avail_indexes[0];
609
580
 
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];
613
584
    }
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];
617
588
    }else{
618
589
        B[0] = A[0];
619
590
        B[1] = A[1];
620
591
    }
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];
625
596
        }else{
626
597
            C[0] = A[0];
627
598
            C[1] = A[1];
628
599
        }
629
600
    }else{
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];
632
603
    }
633
604
    mx = mid_pred(A[0], B[0], C[0]);
634
605
    my = mid_pred(A[1], B[1], C[1]);
637
608
    for(j = 0; j < 2; j++){
638
609
        for(i = 0; i < 2; i++){
639
610
            for(k = 0; k < 2; k++){
640
 
                s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
641
 
                s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
 
611
                s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
 
612
                s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
642
613
            }
643
614
        }
644
615
    }
664
635
static inline void rv34_mc(RV34DecContext *r, const int block_type,
665
636
                          const int xoff, const int yoff, int mv_off,
666
637
                          const int width, const int height, int dir,
667
 
                          const int thirdpel,
 
638
                          const int thirdpel, int weighted,
668
639
                          qpel_mc_func (*qpel_mc)[16],
669
640
                          h264_chroma_mc_func (*chroma_mc))
670
641
{
676
647
 
677
648
    if(thirdpel){
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];
689
660
    }else{
690
661
        int cx, cy;
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;
697
668
        umx = cx >> 2;
698
669
        umy = cy >> 2;
699
670
        uvmx = (cx & 3) << 1;
702
673
        if(uvmx == 6 && uvmy == 6)
703
674
            uvmx = uvmy = 4;
704
675
    }
 
676
 
 
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);
 
682
    }
 
683
 
705
684
    dxy = ly*4 + lx;
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;
719
699
 
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,
728
708
        srcU = uvbuf;
729
709
        srcV = uvbuf + 16;
730
710
    }
731
 
    Y = s->dest[0] + xoff      + yoff     *s->linesize;
732
 
    U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
733
 
    V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
 
711
    if(!weighted){
 
712
        Y = s->dest[0] + xoff      + yoff     *s->linesize;
 
713
        U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
 
714
        V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
 
715
    }else{
 
716
        Y = r->tmp_b_block_y [dir]     +  xoff     +  yoff    *s->linesize;
 
717
        U = r->tmp_b_block_uv[dir*2]   + (xoff>>1) + (yoff>>1)*s->uvlinesize;
 
718
        V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
 
719
    }
734
720
 
735
721
    if(block_type == RV34_MB_P_16x8){
736
722
        qpel_mc[1][dxy](Y, srcY, s->linesize);
751
737
                        const int xoff, const int yoff, int mv_off,
752
738
                        const int width, const int height, int dir)
753
739
{
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);
 
743
}
 
744
 
 
745
static void rv4_weight(RV34DecContext *r)
 
746
{
 
747
    r->rdsp.rv40_weight_pixels_tab[0](r->s.dest[0],
 
748
                                      r->tmp_b_block_y[0],
 
749
                                      r->tmp_b_block_y[1],
 
750
                                      r->weight1,
 
751
                                      r->weight2,
 
752
                                      r->s.linesize);
 
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],
 
756
                                      r->weight1,
 
757
                                      r->weight2,
 
758
                                      r->s.uvlinesize);
 
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],
 
762
                                      r->weight1,
 
763
                                      r->weight2,
 
764
                                      r->s.uvlinesize);
759
765
}
760
766
 
761
767
static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
762
768
{
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;
 
770
 
 
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);
 
774
    if(!weighted){
 
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);
 
778
    }else{
 
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);
 
782
        rv4_weight(r);
 
783
    }
773
784
}
774
785
 
775
786
static void rv34_mc_2mv_skip(RV34DecContext *r)
776
787
{
777
788
    int i, j;
 
789
    int weighted = !r->rv30 && r->weight1 != 8192;
 
790
 
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);
 
794
                     weighted,
 
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);
 
798
                     weighted,
 
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);
790
801
        }
 
802
    if(weighted)
 
803
        rv4_weight(r);
791
804
}
792
805
 
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);
817
830
        return 0;
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);
822
835
            break;
823
836
        }
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);
 
842
 
 
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);
830
847
        }else
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);
838
855
        else
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);
841
858
        break;
842
859
    case RV34_MB_P_16x16:
843
860
    case RV34_MB_P_MIX16x16:
885
902
/** @} */ // mv group
886
903
 
887
904
/**
888
 
 * @defgroup recons Macroblock reconstruction functions
 
905
 * @name Macroblock reconstruction functions
889
906
 * @{
890
907
 */
891
908
/** mapping of RV30/40 intra prediction types to standard H.264 types */
923
940
        if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
924
941
    }
925
942
    if(!right && up){
926
 
        topleft = dst[-stride + 3] * 0x01010101;
 
943
        topleft = dst[-stride + 3] * 0x01010101u;
927
944
        prev = (uint8_t*)&topleft;
928
945
    }
929
946
    r->h.pred4x4[itype](dst, prev, stride);
1027
1044
    }
1028
1045
}
1029
1046
 
1030
 
/** @} */ // recons group
1031
 
 
1032
 
/**
1033
 
 * @addtogroup bitstream
1034
 
 * Decode macroblock header and return CBP in case of success, -1 otherwise.
1035
 
 */
1036
 
static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
1037
 
{
1038
 
    MpegEncContext *s = &r->s;
1039
 
    GetBitContext *gb = &s->gb;
1040
 
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1041
 
    int i, t;
1042
 
 
1043
 
    if(!r->si.type){
1044
 
        r->is16 = get_bits1(gb);
1045
 
        if(!r->is16 && !r->rv30){
1046
 
            if(!get_bits1(gb))
1047
 
                av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
1048
 
        }
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;
1051
 
    }else{
1052
 
        r->block_type = r->decode_mb_info(r);
1053
 
        if(r->block_type == -1)
1054
 
            return -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;
1062
 
        }
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]));
1067
 
            return 0;
1068
 
        }
1069
 
        r->chroma_vlc = 1;
1070
 
        r->luma_vlc   = 0;
1071
 
    }
1072
 
    if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
1073
 
        if(r->is16){
1074
 
            t = get_bits(gb, 2);
1075
 
            fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
1076
 
            r->luma_vlc   = 2;
1077
 
        }else{
1078
 
            if(r->decode_intra_types(r, gb, intra_types) < 0)
1079
 
                return -1;
1080
 
            r->luma_vlc   = 1;
1081
 
        }
1082
 
        r->chroma_vlc = 0;
1083
 
        r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
1084
 
    }else{
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){
1089
 
            r->is16 = 1;
1090
 
            r->chroma_vlc = 1;
1091
 
            r->luma_vlc   = 2;
1092
 
            r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
1093
 
        }
1094
 
    }
1095
 
 
1096
 
    return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
1097
 
}
1098
 
 
1099
 
/**
1100
 
 * @addtogroup recons
1101
 
 * @{
1102
 
 */
1103
1047
/**
1104
1048
 * mask for retrieving all bits in coded block pattern
1105
1049
 * corresponding to one 8x8 block
1109
1053
#define U_CBP_MASK 0x0F0000
1110
1054
#define V_CBP_MASK 0xF00000
1111
1055
 
 
1056
/** @} */ // recons group
 
1057
 
1112
1058
 
1113
1059
static void rv34_apply_differences(RV34DecContext *r, int cbp)
1114
1060
{
1142
1088
    MpegEncContext *s = &r->s;
1143
1089
    int hmvmask = 0, vmvmask = 0, i, j;
1144
1090
    int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
1145
 
    int16_t (*motion_val)[2] = &s->current_picture_ptr->motion_val[0][midx];
 
1091
    int16_t (*motion_val)[2] = &s->current_picture_ptr->f.motion_val[0][midx];
1146
1092
    for(j = 0; j < 16; j += 8){
1147
1093
        for(i = 0; i < 2; i++){
1148
1094
            if(is_mv_diff_gt_3(motion_val + i, 1))
1172
1118
    MpegEncContext *s = &r->s;
1173
1119
    GetBitContext *gb = &s->gb;
1174
1120
    int cbp, cbp2;
 
1121
    int q_dc, q_ac;
1175
1122
    int i, blknum, blkoff;
1176
 
    DCTELEM block16[64];
 
1123
    LOCAL_ALIGNED_16(DCTELEM, block16, [64]);
1177
1124
    int luma_dc_quant;
1178
1125
    int dist;
1179
1126
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1184
1131
    dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1185
1132
    if(s->mb_x && dist)
1186
1133
        r->avail_cache[5] =
1187
 
        r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
 
1134
        r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
1188
1135
    if(dist >= s->mb_width)
1189
1136
        r->avail_cache[2] =
1190
 
        r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
 
1137
        r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
1191
1138
    if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
1192
 
        r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
 
1139
        r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
1193
1140
    if(s->mb_x && dist > s->mb_width)
1194
 
        r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
 
1141
        r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
1195
1142
 
1196
1143
    s->qscale = r->si.quant;
1197
1144
    cbp = cbp2 = rv34_decode_mb_header(r, intra_types);
1201
1148
        r->deblock_coefs[mb_pos] = 0xFFFF;
1202
1149
    else
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;
1205
1152
 
1206
1153
    if(cbp == -1)
1207
1154
        return -1;
1208
1155
 
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];
1210
1157
    if(r->is16){
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);
1215
1163
    }
1216
1164
 
 
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);
1221
1170
        if(cbp & 1)
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);
1227
1176
    }
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);
1237
1187
    }
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);
1240
1190
    else
1241
1191
        rv34_apply_differences(r, cbp2);
1252
1202
        return 1;
1253
1203
    if(r->s.mb_skip_run > 1)
1254
1204
        return 0;
1255
 
    bits = r->bits - get_bits_count(&s->gb);
 
1205
    bits = get_bits_left(&s->gb);
1256
1206
    if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits)))
1257
1207
        return 1;
1258
1208
    return 0;
1259
1209
}
1260
1210
 
1261
 
static inline int slice_compare(SliceInfo *si1, SliceInfo *si2)
1262
 
{
1263
 
    return si1->type   != si2->type  ||
1264
 
           si1->start  >= si2->start ||
1265
 
           si1->width  != si2->width ||
1266
 
           si1->height != si2->height||
1267
 
           si1->pts    != si2->pts;
1268
 
}
1269
 
 
1270
1211
static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
1271
1212
{
1272
1213
    MpegEncContext *s = &r->s;
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);
1300
1242
        }
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)
1303
1245
            return -1;
1304
1246
        ff_er_frame_start(s);
 
1247
        if (!r->tmp_b_block_base) {
 
1248
            int i;
 
1249
 
 
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;
 
1256
        }
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;
 
1261
        }else{
 
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);
 
1265
 
 
1266
            if(!refdist){
 
1267
                r->weight1 = r->weight2 = 8192;
 
1268
            }else{
 
1269
                r->weight1 = (dist0 << 14) / refdist;
 
1270
                r->weight2 = (dist1 << 14) / refdist;
 
1271
            }
1309
1272
        }
1310
1273
        s->mb_x = s->mb_y = 0;
 
1274
        ff_thread_finish_setup(s->avctx);
 
1275
    } else {
 
1276
        int slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
 
1277
 
 
1278
        if (slice_type != s->pict_type) {
 
1279
            av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
 
1280
            return AVERROR_INVALIDDATA;
 
1281
        }
1311
1282
    }
1312
1283
 
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;
1318
1288
 
1324
1294
    }
1325
1295
    memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1326
1296
    s->first_slice_line = 1;
1327
 
    s->resync_mb_x= s->mb_x;
1328
 
    s->resync_mb_y= s->mb_y;
 
1297
    s->resync_mb_x = s->mb_x;
 
1298
    s->resync_mb_y = s->mb_y;
1329
1299
 
1330
1300
    ff_init_block_index(s);
1331
1301
    while(!check_slice_end(r, s)) {
1333
1303
        s->dsp.clear_blocks(s->block[0]);
1334
1304
 
1335
1305
        if(rv34_decode_macroblock(r, r->intra_types + s->mb_x * 4 + 4) < 0){
1336
 
            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);
 
1306
            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
1337
1307
            return -1;
1338
1308
        }
1339
1309
        if (++s->mb_x == s->mb_width) {
1346
1316
 
1347
1317
            if(r->loop_filter && s->mb_y >= 2)
1348
1318
                r->loop_filter(r, s->mb_y - 2);
 
1319
 
 
1320
            if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
 
1321
                ff_thread_report_progress(&s->current_picture_ptr->f,
 
1322
                                          s->mb_y - 2, 0);
 
1323
 
1349
1324
        }
1350
1325
        if(s->mb_x == s->resync_mb_x)
1351
1326
            s->first_slice_line=0;
1352
1327
        s->mb_num_left--;
1353
1328
    }
1354
 
    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);
 
1329
    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
1355
1330
 
1356
1331
    return s->mb_y == s->mb_height;
1357
1332
}
1367
1342
    MpegEncContext *s = &r->s;
1368
1343
 
1369
1344
    MPV_decode_defaults(s);
1370
 
    s->avctx= avctx;
 
1345
    s->avctx      = avctx;
1371
1346
    s->out_format = FMT_H263;
1372
 
    s->codec_id= avctx->codec_id;
 
1347
    s->codec_id   = avctx->codec_id;
1373
1348
 
1374
 
    s->width = avctx->width;
 
1349
    s->width  = avctx->width;
1375
1350
    s->height = avctx->height;
1376
1351
 
1377
1352
    r->s.avctx = avctx;
1384
1359
    if (MPV_common_init(s) < 0)
1385
1360
        return -1;
1386
1361
 
1387
 
    ff_h264_pred_init(&r->h, CODEC_ID_RV40, 8);
 
1362
    ff_h264_pred_init(&r->h, CODEC_ID_RV40, 8, 1);
 
1363
 
 
1364
#if CONFIG_RV30_DECODER
 
1365
    if (avctx->codec_id == CODEC_ID_RV30)
 
1366
        ff_rv30dsp_init(&r->rdsp, &r->s.dsp);
 
1367
#endif
 
1368
#if CONFIG_RV40_DECODER
 
1369
    if (avctx->codec_id == CODEC_ID_RV40)
 
1370
        ff_rv40dsp_init(&r->rdsp, &r->s.dsp);
 
1371
#endif
1388
1372
 
1389
1373
    r->intra_types_stride = 4*s->mb_stride + 4;
1390
1374
    r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1402
1386
    return 0;
1403
1387
}
1404
1388
 
 
1389
int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
 
1390
{
 
1391
    RV34DecContext *r = avctx->priv_data;
 
1392
 
 
1393
    r->s.avctx = avctx;
 
1394
 
 
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));
 
1406
 
 
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);
 
1416
        }
 
1417
 
 
1418
        r->intra_types      = r->intra_types_hist + r->intra_types_stride * 4;
 
1419
        r->tmp_b_block_base = NULL;
 
1420
 
 
1421
        memset(r->mb_type, 0,  r->s.mb_stride * r->s.mb_height *
 
1422
               sizeof(*r->mb_type));
 
1423
 
 
1424
        MPV_common_init(&r->s);
 
1425
    }
 
1426
    return 0;
 
1427
}
 
1428
 
 
1429
int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
 
1430
{
 
1431
    RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
 
1432
    MpegEncContext * const s = &r->s, * const s1 = &r1->s;
 
1433
    int err;
 
1434
 
 
1435
    if (dst == src || !s1->context_initialized)
 
1436
        return 0;
 
1437
 
 
1438
    if ((err = ff_mpeg_update_thread_context(dst, src)))
 
1439
        return err;
 
1440
 
 
1441
    r->cur_pts  = r1->cur_pts;
 
1442
    r->last_pts = r1->last_pts;
 
1443
    r->next_pts = r1->next_pts;
 
1444
 
 
1445
    memset(&r->si, 0, sizeof(r->si));
 
1446
 
 
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;
 
1450
 
 
1451
    return 0;
 
1452
}
 
1453
 
1405
1454
static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
1406
1455
{
1407
1456
    if(avctx->slice_count) return avctx->slice_offset[n];
1427
1476
    if (buf_size == 0) {
1428
1477
        /* special case for last picture */
1429
1478
        if (s->low_delay==0 && s->next_picture_ptr) {
1430
 
            *pict= *(AVFrame*)s->next_picture_ptr;
1431
 
            s->next_picture_ptr= NULL;
 
1479
            *pict = *(AVFrame*)s->next_picture_ptr;
 
1480
            s->next_picture_ptr = NULL;
1432
1481
 
1433
1482
            *data_size = sizeof(AVFrame);
1434
1483
        }
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;
1442
1492
    }else
1443
1493
        slice_count = avctx->slice_count;
1444
1494
 
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");
1448
1499
        return -1;
1449
1500
    }
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");
1453
1504
        return -1;
1454
1505
    }
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)
1456
1507
        return -1;
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)
1460
 
        return buf_size;
 
1511
        return avpkt->size;
1461
1512
 
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);
1464
1515
        int size;
1465
1516
        if(i+1 == slice_count)
1466
 
            size= buf_size - offset;
 
1517
            size = buf_size - offset;
1467
1518
        else
1468
 
            size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
 
1519
            size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
1469
1520
 
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");
1472
1523
            break;
1473
1524
        }
1474
1525
 
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");
 
1531
                break;
 
1532
            }
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)
1483
1539
            }else
1484
1540
                r->si.end = si.start;
1485
1541
        }
 
1542
        if (size < 0 || size > buf_size - offset) {
 
1543
            av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
 
1544
            break;
 
1545
        }
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;
1488
1548
        if(last)
1489
1549
            break;
1490
1550
    }
1491
1551
 
1492
 
    if(last){
 
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;
1501
1564
        }
1502
1565
 
1503
1566
        if(s->last_picture_ptr || s->low_delay){
1504
1567
            *data_size = sizeof(AVFrame);
1505
1568
            ff_print_debug_info(s, pict);
1506
1569
        }
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...)
1508
1571
    }
1509
 
    return buf_size;
 
1572
    return avpkt->size;
1510
1573
}
1511
1574
 
1512
1575
av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
1517
1580
 
1518
1581
    av_freep(&r->intra_types_hist);
1519
1582
    r->intra_types = NULL;
 
1583
    av_freep(&r->tmp_b_block_base);
1520
1584
    av_freep(&r->mb_type);
1521
1585
    av_freep(&r->cbp_luma);
1522
1586
    av_freep(&r->cbp_chroma);