Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/rv40.c
Line
Count
Source
1
/*
2
 * RV40 decoder
3
 * Copyright (c) 2007 Konstantin Shishkov
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
/**
23
 * @file
24
 * RV40 decoder
25
 */
26
27
#include "config.h"
28
29
#include "libavutil/imgutils.h"
30
#include "libavutil/thread.h"
31
32
#include "avcodec.h"
33
#include "codec_internal.h"
34
#include "mpegutils.h"
35
#include "mpegvideo.h"
36
#include "mpegvideodec.h"
37
#include "golomb.h"
38
39
#include "rv34.h"
40
#include "rv40vlc2.h"
41
#include "rv40data.h"
42
43
static VLCElem aic_top_vlc[23590];
44
static const VLCElem *aic_mode1_vlc[AIC_MODE1_NUM], *aic_mode2_vlc[AIC_MODE2_NUM];
45
static const VLCElem *ptype_vlc[NUM_PTYPE_VLCS],    *btype_vlc[NUM_BTYPE_VLCS];
46
47
static av_cold const VLCElem *rv40_init_table(VLCInitState *state, int nb_bits,
48
                                              int nb_codes, const uint8_t (*tab)[2])
49
95
{
50
95
    return ff_vlc_init_tables_from_lengths(state, nb_bits, nb_codes,
51
95
                                           &tab[0][1], 2, &tab[0][0], 2, 1,
52
95
                                           0, 0);
53
95
}
54
55
/**
56
 * Initialize all tables.
57
 */
58
static av_cold void rv40_init_tables(void)
59
1
{
60
1
    VLCInitState state = VLC_INIT_STATE(aic_top_vlc);
61
1
    int i;
62
63
1
    rv40_init_table(&state, AIC_TOP_BITS, AIC_TOP_SIZE,
64
1
                    rv40_aic_top_vlc_tab);
65
91
    for(i = 0; i < AIC_MODE1_NUM; i++){
66
        // Every tenth VLC table is empty
67
90
        if((i % 10) == 9) continue;
68
81
        aic_mode1_vlc[i] =
69
81
            rv40_init_table(&state, AIC_MODE1_BITS,
70
81
                            AIC_MODE1_SIZE, aic_mode1_vlc_tabs[i]);
71
81
    }
72
21
    for (unsigned i = 0; i < AIC_MODE2_NUM; i++){
73
20
        uint16_t syms[AIC_MODE2_SIZE];
74
75
1.64k
        for (int j = 0; j < AIC_MODE2_SIZE; j++) {
76
1.62k
            int first  = aic_mode2_vlc_syms[i][j] >> 4;
77
1.62k
            int second = aic_mode2_vlc_syms[i][j] & 0xF;
78
1.62k
            if (HAVE_BIGENDIAN)
79
0
                syms[j] = (first << 8) | second;
80
1.62k
            else
81
1.62k
                syms[j] = first | (second << 8);
82
1.62k
        }
83
20
        aic_mode2_vlc[i] =
84
20
            ff_vlc_init_tables_from_lengths(&state, AIC_MODE2_BITS, AIC_MODE2_SIZE,
85
20
                                            aic_mode2_vlc_bits[i], 1,
86
20
                                            syms, 2, 2, 0, 0);
87
20
    }
88
8
    for(i = 0; i < NUM_PTYPE_VLCS; i++){
89
7
        ptype_vlc[i] =
90
7
            rv40_init_table(&state, PTYPE_VLC_BITS, PTYPE_VLC_SIZE,
91
7
                            ptype_vlc_tabs[i]);
92
7
    }
93
7
    for(i = 0; i < NUM_BTYPE_VLCS; i++){
94
6
        btype_vlc[i] =
95
6
            rv40_init_table(&state, BTYPE_VLC_BITS, BTYPE_VLC_SIZE,
96
6
                            btype_vlc_tabs[i]);
97
6
    }
98
1
}
99
100
/**
101
 * Get stored dimension from bitstream.
102
 *
103
 * If the width/height is the standard one then it's coded as a 3-bit index.
104
 * Otherwise it is coded as escaped 8-bit portions.
105
 */
106
static int get_dimension(GetBitContext *gb, const int *dim)
107
380k
{
108
380k
    int t   = get_bits(gb, 3);
109
380k
    int val = dim[t];
110
380k
    if(val < 0)
111
9.95k
        val = dim[get_bits1(gb) - val];
112
380k
    if(!val){
113
742k
        do{
114
742k
            if (get_bits_left(gb) < 8)
115
457
                return AVERROR_INVALIDDATA;
116
742k
            t = get_bits(gb, 8);
117
742k
            val += t << 2;
118
742k
        }while(t == 0xFF);
119
14.6k
    }
120
380k
    return val;
121
380k
}
122
123
/**
124
 * Get encoded picture size - usually this is called from rv40_parse_slice_header.
125
 */
126
static void rv40_parse_picture_size(GetBitContext *gb, int *w, int *h)
127
190k
{
128
190k
    *w = get_dimension(gb, rv40_standard_widths);
129
190k
    *h = get_dimension(gb, rv40_standard_heights);
130
190k
}
131
132
static int rv40_parse_slice_header(RV34DecContext *r, GetBitContext *gb, SliceInfo *si)
133
203k
{
134
203k
    int w = r->s.width, h = r->s.height;
135
203k
    int mb_size;
136
203k
    int ret;
137
138
203k
    memset(si, 0, sizeof(SliceInfo));
139
203k
    if(get_bits1(gb))
140
2.91k
        return AVERROR_INVALIDDATA;
141
200k
    si->type = get_bits(gb, 2);
142
200k
    if(si->type == 1) si->type = 0;
143
200k
    si->quant = get_bits(gb, 5);
144
200k
    if(get_bits(gb, 2))
145
3.01k
        return AVERROR_INVALIDDATA;
146
197k
    si->vlc_set = get_bits(gb, 2);
147
197k
    skip_bits1(gb);
148
197k
    si->pts = get_bits(gb, 13);
149
197k
    if(!si->type || !get_bits1(gb))
150
190k
        rv40_parse_picture_size(gb, &w, &h);
151
197k
    if ((ret = av_image_check_size(w, h, 0, r->s.avctx)) < 0)
152
1.09k
        return ret;
153
196k
    si->width  = w;
154
196k
    si->height = h;
155
196k
    mb_size = ((w + 15) >> 4) * ((h + 15) >> 4);
156
196k
    si->start = ff_rv34_get_start_offset(gb, mb_size);
157
158
196k
    return 0;
159
197k
}
160
161
/**
162
 * Decode 4x4 intra types array.
163
 */
164
static int rv40_decode_intra_types(RV34DecContext *r, GetBitContext *gb, int8_t *dst)
165
1.88M
{
166
1.88M
    MpegEncContext *s = &r->s;
167
1.88M
    int i, j, k, v;
168
1.88M
    int A, B, C;
169
1.88M
    int pattern;
170
1.88M
    int8_t *ptr;
171
172
9.41M
    for(i = 0; i < 4; i++, dst += r->intra_types_stride){
173
7.53M
        if(!i && s->first_slice_line){
174
32.3k
            pattern = get_vlc2(gb, aic_top_vlc, AIC_TOP_BITS, 1);
175
32.3k
            dst[0] = (pattern >> 2) & 2;
176
32.3k
            dst[1] = (pattern >> 1) & 2;
177
32.3k
            dst[2] =  pattern       & 2;
178
32.3k
            dst[3] = (pattern << 1) & 2;
179
32.3k
            continue;
180
32.3k
        }
181
7.50M
        ptr = dst;
182
34.1M
        for(j = 0; j < 4; j++){
183
            /* Coefficients are read using VLC chosen by the prediction pattern
184
             * The first one (used for retrieving a pair of coefficients) is
185
             * constructed from the top, top right and left coefficients
186
             * The second one (used for retrieving only one coefficient) is
187
             * top + 10 * left.
188
             */
189
26.6M
            A = ptr[-r->intra_types_stride + 1]; // it won't be used for the last coefficient in a row
190
26.6M
            B = ptr[-r->intra_types_stride];
191
26.6M
            C = ptr[-1];
192
26.6M
            pattern = A + B * (1 << 4) + C * (1 << 8);
193
521M
            for(k = 0; k < MODE2_PATTERNS_NUM; k++)
194
498M
                if(pattern == rv40_aic_table_index[k])
195
3.53M
                    break;
196
26.6M
            if(j < 3 && k < MODE2_PATTERNS_NUM){ //pattern is found, decoding 2 coefficients
197
3.40M
                AV_WN16(ptr, get_vlc2(gb, aic_mode2_vlc[k], AIC_MODE2_BITS, 2));
198
3.40M
                ptr += 2;
199
3.40M
                j++;
200
23.2M
            }else{
201
23.2M
                if(B != -1 && C != -1)
202
22.4M
                    v = get_vlc2(gb, aic_mode1_vlc[B + C*10], AIC_MODE1_BITS, 1);
203
785k
                else{ // tricky decoding
204
785k
                    v = 0;
205
785k
                    switch(C){
206
785k
                    case -1: // code 0 -> 1, 1 -> 0
207
785k
                        if(B < 2)
208
778k
                            v = get_bits1(gb) ^ 1;
209
785k
                        break;
210
0
                    case  0:
211
0
                    case  2: // code 0 -> 2, 1 -> 0
212
0
                        v = (get_bits1(gb) ^ 1) << 1;
213
0
                        break;
214
785k
                    }
215
785k
                }
216
23.2M
                *ptr++ = v;
217
23.2M
            }
218
26.6M
        }
219
7.50M
    }
220
1.88M
    return 0;
221
1.88M
}
222
223
/**
224
 * Decode macroblock information.
225
 */
226
static int rv40_decode_mb_info(RV34DecContext *r)
227
3.68M
{
228
3.68M
    MpegEncContext *s = &r->s;
229
3.68M
    GetBitContext *const gb = &r->gb;
230
3.68M
    int q, i;
231
3.68M
    int prev_type = 0;
232
3.68M
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
233
234
3.68M
    if (!r->mb_skip_run) {
235
636k
        r->mb_skip_run = get_interleaved_ue_golomb(gb) + 1;
236
636k
        if (r->mb_skip_run > (unsigned)s->mb_num)
237
7.80k
            return -1;
238
636k
    }
239
240
3.67M
    if (--r->mb_skip_run)
241
3.05M
         return RV34_MB_SKIP;
242
243
627k
    if(r->avail_cache[6-4]){
244
596k
        int blocks[RV34_MB_TYPES] = {0};
245
596k
        int count = 0;
246
596k
        if(r->avail_cache[6-1])
247
552k
            blocks[r->mb_type[mb_pos - 1]]++;
248
596k
        blocks[r->mb_type[mb_pos - s->mb_stride]]++;
249
596k
        if(r->avail_cache[6-2])
250
554k
            blocks[r->mb_type[mb_pos - s->mb_stride + 1]]++;
251
596k
        if(r->avail_cache[6-5])
252
552k
            blocks[r->mb_type[mb_pos - s->mb_stride - 1]]++;
253
2.71M
        for(i = 0; i < RV34_MB_TYPES; i++){
254
2.69M
            if(blocks[i] > count){
255
684k
                count = blocks[i];
256
684k
                prev_type = i;
257
684k
                if(count>1)
258
574k
                    break;
259
684k
            }
260
2.69M
        }
261
596k
    } else if (r->avail_cache[6-1])
262
26.8k
        prev_type = r->mb_type[mb_pos - 1];
263
264
627k
    if(s->pict_type == AV_PICTURE_TYPE_P){
265
491k
        prev_type = block_num_to_ptype_vlc_num[prev_type];
266
491k
        q = get_vlc2(gb, ptype_vlc[prev_type], PTYPE_VLC_BITS, 1);
267
491k
        if(q < PBTYPE_ESCAPE)
268
476k
            return q;
269
14.3k
        q = get_vlc2(gb, ptype_vlc[prev_type], PTYPE_VLC_BITS, 1);
270
14.3k
        av_log(s->avctx, AV_LOG_ERROR, "Dquant for P-frame\n");
271
136k
    }else{
272
136k
        prev_type = block_num_to_btype_vlc_num[prev_type];
273
136k
        q = get_vlc2(gb, btype_vlc[prev_type], BTYPE_VLC_BITS, 1);
274
136k
        if(q < PBTYPE_ESCAPE)
275
132k
            return q;
276
3.90k
        q = get_vlc2(gb, btype_vlc[prev_type], BTYPE_VLC_BITS, 1);
277
3.90k
        av_log(s->avctx, AV_LOG_ERROR, "Dquant for B-frame\n");
278
3.90k
    }
279
18.2k
    return 0;
280
627k
}
281
282
enum RV40BlockPos{
283
    POS_CUR,
284
    POS_TOP,
285
    POS_LEFT,
286
    POS_BOTTOM,
287
};
288
289
657M
#define MASK_CUR          0x0001
290
12.1M
#define MASK_RIGHT        0x0008
291
143M
#define MASK_BOTTOM       0x0010
292
10.9M
#define MASK_TOP          0x1000
293
9.94M
#define MASK_Y_TOP_ROW    0x000F
294
6.55M
#define MASK_Y_LAST_ROW   0xF000
295
7.10M
#define MASK_Y_LEFT_COL   0x1111
296
6.55M
#define MASK_Y_RIGHT_COL  0x8888
297
6.77M
#define MASK_C_TOP_ROW    0x0003
298
13.1M
#define MASK_C_LAST_ROW   0x000C
299
14.2M
#define MASK_C_LEFT_COL   0x0005
300
13.1M
#define MASK_C_RIGHT_COL  0x000A
301
302
static const int neighbour_offs_x[4] = { 0,  0, -1, 0 };
303
static const int neighbour_offs_y[4] = { 0, -1,  0, 1 };
304
305
static void rv40_adaptive_loop_filter(RV34DSPContext *rdsp,
306
                                      uint8_t *src, int stride, int dmode,
307
                                      int lim_q1, int lim_p1,
308
                                      int alpha, int beta, int beta2,
309
                                      int chroma, int edge, int dir)
310
141M
{
311
141M
    int filter_p1, filter_q1;
312
141M
    int strong;
313
141M
    int lims;
314
315
141M
    strong = rdsp->rv40_loop_filter_strength[dir](src, stride, beta, beta2,
316
141M
                                                  edge, &filter_p1, &filter_q1);
317
318
141M
    lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;
319
320
141M
    if (strong) {
321
26.8M
        rdsp->rv40_strong_loop_filter[dir](src, stride, alpha,
322
26.8M
                                           lims, dmode, chroma);
323
114M
    } else if (filter_p1 & filter_q1) {
324
67.7M
        rdsp->rv40_weak_loop_filter[dir](src, stride, 1, 1, alpha, beta,
325
67.7M
                                         lims, lim_q1, lim_p1);
326
67.7M
    } else if (filter_p1 | filter_q1) {
327
4.71M
        rdsp->rv40_weak_loop_filter[dir](src, stride, filter_p1, filter_q1,
328
4.71M
                                         alpha, beta, lims >> 1, lim_q1 >> 1,
329
4.71M
                                         lim_p1 >> 1);
330
4.71M
    }
331
141M
}
332
333
/**
334
 * RV40 loop filtering function
335
 */
336
static void rv40_loop_filter(RV34DecContext *r, int row)
337
550k
{
338
550k
    MpegEncContext *s = &r->s;
339
550k
    int mb_pos, mb_x;
340
550k
    int i, j, k;
341
550k
    uint8_t *Y, *C;
342
550k
    int alpha, beta, betaY, betaC;
343
550k
    int q;
344
550k
    int mbtype[4];   ///< current macroblock and its neighbours types
345
    /**
346
     * flags indicating that macroblock can be filtered with strong filter
347
     * it is set only for intra coded MB and MB with DCs coded separately
348
     */
349
550k
    int mb_strong[4];
350
550k
    int clip[4];     ///< MB filter clipping value calculated from filtering strength
351
    /**
352
     * coded block patterns for luma part of current macroblock and its neighbours
353
     * Format:
354
     * LSB corresponds to the top left block,
355
     * each nibble represents one row of subblocks.
356
     */
357
550k
    int cbp[4];
358
    /**
359
     * coded block patterns for chroma part of current macroblock and its neighbours
360
     * Format is the same as for luma with two subblocks in a row.
361
     */
362
550k
    int uvcbp[4][2];
363
    /**
364
     * This mask represents the pattern of luma subblocks that should be filtered
365
     * in addition to the coded ones because they lie at the edge of
366
     * 8x8 block with different enough motion vectors
367
     */
368
550k
    unsigned mvmasks[4];
369
370
550k
    mb_pos = row * s->mb_stride;
371
7.10M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
372
6.55M
        int mbtype = s->cur_pic.mb_type[mb_pos];
373
6.55M
        if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
374
2.74M
            r->cbp_luma  [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF;
375
6.55M
        if(IS_INTRA(mbtype))
376
2.72M
            r->cbp_chroma[mb_pos] = 0xFF;
377
6.55M
    }
378
550k
    mb_pos = row * s->mb_stride;
379
7.10M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
380
6.55M
        int y_h_deblock, y_v_deblock;
381
6.55M
        int c_v_deblock[2], c_h_deblock[2];
382
6.55M
        int clip_left;
383
6.55M
        int avail[4];
384
6.55M
        unsigned y_to_deblock;
385
6.55M
        int c_to_deblock[2];
386
387
6.55M
        q = s->cur_pic.qscale_table[mb_pos];
388
6.55M
        alpha = rv40_alpha_tab[q];
389
6.55M
        beta  = rv40_beta_tab [q];
390
6.55M
        betaY = betaC = beta * 3;
391
6.55M
        if(s->width * s->height <= 176*144)
392
277k
            betaY += beta;
393
394
6.55M
        avail[0] = 1;
395
6.55M
        avail[1] = row;
396
6.55M
        avail[2] = mb_x;
397
6.55M
        avail[3] = row < s->mb_height - 1;
398
32.7M
        for(i = 0; i < 4; i++){
399
26.2M
            if(avail[i]){
400
25.0M
                int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride;
401
25.0M
                mvmasks[i] = r->deblock_coefs[pos];
402
25.0M
                mbtype [i] = s->cur_pic.mb_type[pos];
403
25.0M
                cbp    [i] = r->cbp_luma[pos];
404
25.0M
                uvcbp[i][0] = r->cbp_chroma[pos] & 0xF;
405
25.0M
                uvcbp[i][1] = r->cbp_chroma[pos] >> 4;
406
25.0M
            }else{
407
1.16M
                mvmasks[i] = 0;
408
1.16M
                mbtype [i] = mbtype[0];
409
1.16M
                cbp    [i] = 0;
410
1.16M
                uvcbp[i][0] = uvcbp[i][1] = 0;
411
1.16M
            }
412
26.2M
            mb_strong[i] = IS_INTRA(mbtype[i]) || IS_SEPARATE_DC(mbtype[i]);
413
26.2M
            clip[i] = rv40_filter_clip_tbl[mb_strong[i] + 1][q];
414
26.2M
        }
415
6.55M
        y_to_deblock =  mvmasks[POS_CUR]
416
6.55M
                     | (mvmasks[POS_BOTTOM] << 16);
417
        /* This pattern contains bits signalling that horizontal edges of
418
         * the current block can be filtered.
419
         * That happens when either of adjacent subblocks is coded or lies on
420
         * the edge of 8x8 blocks with motion vectors differing by more than
421
         * 3/4 pel in any component (any edge orientation for some reason).
422
         */
423
6.55M
        y_h_deblock =   y_to_deblock
424
6.55M
                    | ((cbp[POS_CUR]                           <<  4) & ~MASK_Y_TOP_ROW)
425
6.55M
                    | ((cbp[POS_TOP]        & MASK_Y_LAST_ROW) >> 12);
426
        /* This pattern contains bits signalling that vertical edges of
427
         * the current block can be filtered.
428
         * That happens when either of adjacent subblocks is coded or lies on
429
         * the edge of 8x8 blocks with motion vectors differing by more than
430
         * 3/4 pel in any component (any edge orientation for some reason).
431
         */
432
6.55M
        y_v_deblock =   y_to_deblock
433
6.55M
                    | ((cbp[POS_CUR]                      << 1) & ~MASK_Y_LEFT_COL)
434
6.55M
                    | ((cbp[POS_LEFT] & MASK_Y_RIGHT_COL) >> 3);
435
6.55M
        if(!mb_x)
436
550k
            y_v_deblock &= ~MASK_Y_LEFT_COL;
437
6.55M
        if(!row)
438
196k
            y_h_deblock &= ~MASK_Y_TOP_ROW;
439
6.55M
        if(row == s->mb_height - 1 || (mb_strong[POS_CUR] | mb_strong[POS_BOTTOM]))
440
3.19M
            y_h_deblock &= ~(MASK_Y_TOP_ROW << 16);
441
        /* Calculating chroma patterns is similar and easier since there is
442
         * no motion vector pattern for them.
443
         */
444
19.6M
        for(i = 0; i < 2; i++){
445
13.1M
            c_to_deblock[i] = (uvcbp[POS_BOTTOM][i] << 4) | uvcbp[POS_CUR][i];
446
13.1M
            c_v_deblock[i] =   c_to_deblock[i]
447
13.1M
                           | ((uvcbp[POS_CUR] [i]                       << 1) & ~MASK_C_LEFT_COL)
448
13.1M
                           | ((uvcbp[POS_LEFT][i]   & MASK_C_RIGHT_COL) >> 1);
449
13.1M
            c_h_deblock[i] =   c_to_deblock[i]
450
13.1M
                           | ((uvcbp[POS_TOP][i]    & MASK_C_LAST_ROW)  >> 2)
451
13.1M
                           |  (uvcbp[POS_CUR][i]                        << 2);
452
13.1M
            if(!mb_x)
453
1.10M
                c_v_deblock[i] &= ~MASK_C_LEFT_COL;
454
13.1M
            if(!row)
455
392k
                c_h_deblock[i] &= ~MASK_C_TOP_ROW;
456
13.1M
            if(row == s->mb_height - 1 || (mb_strong[POS_CUR] | mb_strong[POS_BOTTOM]))
457
6.38M
                c_h_deblock[i] &= ~(MASK_C_TOP_ROW << 4);
458
13.1M
        }
459
460
32.7M
        for(j = 0; j < 16; j += 4){
461
26.2M
            Y = s->cur_pic.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
462
131M
            for(i = 0; i < 4; i++, Y += 4){
463
104M
                int ij = i + j;
464
104M
                int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0;
465
104M
                int dither = j ? ij : i*4;
466
467
                // if bottom block is coded then we can filter its top edge
468
                // (or bottom edge of this block, which is the same)
469
104M
                if(y_h_deblock & (MASK_BOTTOM << ij)){
470
38.2M
                    rv40_adaptive_loop_filter(&r->rdsp, Y+4*s->linesize,
471
38.2M
                                              s->linesize, dither,
472
38.2M
                                              y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0,
473
38.2M
                                              clip_cur, alpha, beta, betaY,
474
38.2M
                                              0, 0, 0);
475
38.2M
                }
476
                // filter left block edge in ordinary mode (with low filtering strength)
477
104M
                if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] | mb_strong[POS_LEFT]))){
478
38.3M
                    if(!i)
479
2.01M
                        clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;
480
36.3M
                    else
481
36.3M
                        clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;
482
38.3M
                    rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
483
38.3M
                                              clip_cur,
484
38.3M
                                              clip_left,
485
38.3M
                                              alpha, beta, betaY, 0, 0, 1);
486
38.3M
                }
487
                // filter top edge of the current macroblock when filtering strength is high
488
104M
                if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] | mb_strong[POS_TOP])){
489
10.9M
                    rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
490
10.9M
                                       clip_cur,
491
10.9M
                                       mvmasks[POS_TOP] & (MASK_TOP << i) ? clip[POS_TOP] : 0,
492
10.9M
                                       alpha, beta, betaY, 0, 1, 0);
493
10.9M
                }
494
                // filter left block edge in edge mode (with high filtering strength)
495
104M
                if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] | mb_strong[POS_LEFT])){
496
10.1M
                    clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;
497
10.1M
                    rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
498
10.1M
                                       clip_cur,
499
10.1M
                                       clip_left,
500
10.1M
                                       alpha, beta, betaY, 0, 1, 1);
501
10.1M
                }
502
104M
            }
503
26.2M
        }
504
19.6M
        for(k = 0; k < 2; k++){
505
39.3M
            for(j = 0; j < 2; j++){
506
26.2M
                C = s->cur_pic.data[k + 1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize;
507
78.7M
                for(i = 0; i < 2; i++, C += 4){
508
52.4M
                    int ij = i + j*2;
509
52.4M
                    int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0;
510
52.4M
                    if(c_h_deblock[k] & (MASK_CUR << (ij+2))){
511
11.4M
                        int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0;
512
11.4M
                        rv40_adaptive_loop_filter(&r->rdsp, C+4*s->uvlinesize, s->uvlinesize, i*8,
513
11.4M
                                           clip_bot,
514
11.4M
                                           clip_cur,
515
11.4M
                                           alpha, beta, betaC, 1, 0, 0);
516
11.4M
                    }
517
52.4M
                    if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] | mb_strong[POS_LEFT]))){
518
11.4M
                        if(!i)
519
287k
                            clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;
520
11.1M
                        else
521
11.1M
                            clip_left = c_to_deblock[k]    & (MASK_CUR << (ij-1))  ? clip[POS_CUR]  : 0;
522
11.4M
                        rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,
523
11.4M
                                           clip_cur,
524
11.4M
                                           clip_left,
525
11.4M
                                           alpha, beta, betaC, 1, 0, 1);
526
11.4M
                    }
527
52.4M
                    if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] | mb_strong[POS_TOP])){
528
10.8M
                        int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0;
529
10.8M
                        rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, i*8,
530
10.8M
                                           clip_cur,
531
10.8M
                                           clip_top,
532
10.8M
                                           alpha, beta, betaC, 1, 1, 0);
533
10.8M
                    }
534
52.4M
                    if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] | mb_strong[POS_LEFT])){
535
10.0M
                        clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;
536
10.0M
                        rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,
537
10.0M
                                           clip_cur,
538
10.0M
                                           clip_left,
539
10.0M
                                           alpha, beta, betaC, 1, 1, 1);
540
10.0M
                    }
541
52.4M
                }
542
26.2M
            }
543
13.1M
        }
544
6.55M
    }
545
550k
}
546
547
/**
548
 * Initialize decoder.
549
 */
550
static av_cold int rv40_decode_init(AVCodecContext *avctx)
551
7.55k
{
552
7.55k
    static AVOnce init_static_once = AV_ONCE_INIT;
553
7.55k
    RV34DecContext *r = avctx->priv_data;
554
7.55k
    int ret;
555
556
7.55k
    r->rv30 = 0;
557
7.55k
    if ((ret = ff_rv34_decode_init(avctx)) < 0)
558
0
        return ret;
559
7.55k
    r->parse_slice_header = rv40_parse_slice_header;
560
7.55k
    r->decode_intra_types = rv40_decode_intra_types;
561
7.55k
    r->decode_mb_info     = rv40_decode_mb_info;
562
7.55k
    r->loop_filter        = rv40_loop_filter;
563
7.55k
    r->luma_dc_quant_i = rv40_luma_dc_quant[0];
564
7.55k
    r->luma_dc_quant_p = rv40_luma_dc_quant[1];
565
7.55k
    ff_rv40dsp_init(&r->rdsp);
566
7.55k
    ff_thread_once(&init_static_once, rv40_init_tables);
567
7.55k
    return 0;
568
7.55k
}
569
570
const FFCodec ff_rv40_decoder = {
571
    .p.name                = "rv40",
572
    CODEC_LONG_NAME("RealVideo 4.0"),
573
    .p.type                = AVMEDIA_TYPE_VIDEO,
574
    .p.id                  = AV_CODEC_ID_RV40,
575
    .priv_data_size        = sizeof(RV34DecContext),
576
    .init                  = rv40_decode_init,
577
    .close                 = ff_rv34_decode_end,
578
    FF_CODEC_DECODE_CB(ff_rv34_decode_frame),
579
    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
580
                             AV_CODEC_CAP_FRAME_THREADS,
581
    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP,
582
    .flush                 = ff_mpeg_flush,
583
    UPDATE_THREAD_CONTEXT(ff_rv34_decode_update_thread_context),
584
};