Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/rv30.c
Line
Count
Source
1
/*
2
 * RV30 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
 * RV30 decoder
25
 */
26
27
#include "avcodec.h"
28
#include "codec_internal.h"
29
#include "mpegutils.h"
30
#include "mpegvideo.h"
31
#include "mpegvideodec.h"
32
#include "golomb.h"
33
34
#include "rv34.h"
35
#include "rv30data.h"
36
37
38
static int rv30_parse_slice_header(RV34DecContext *r, GetBitContext *gb, SliceInfo *si)
39
217k
{
40
217k
    AVCodecContext *avctx = r->s.avctx;
41
217k
    int w = r->s.width, h = r->s.height;
42
217k
    int mb_size;
43
217k
    int rpr;
44
45
217k
    memset(si, 0, sizeof(SliceInfo));
46
217k
    if(get_bits(gb, 3))
47
3.35k
        return -1;
48
213k
    si->type = get_bits(gb, 2);
49
213k
    if(si->type == 1) si->type = 0;
50
213k
    if(get_bits1(gb))
51
374
        return -1;
52
213k
    si->quant = get_bits(gb, 5);
53
213k
    skip_bits1(gb);
54
213k
    si->pts = get_bits(gb, 13);
55
213k
    rpr = get_bits(gb, av_log2(r->max_rpr) + 1);
56
213k
    if(rpr){
57
46.9k
        if (rpr > r->max_rpr) {
58
1.99k
            av_log(avctx, AV_LOG_ERROR, "rpr too large\n");
59
1.99k
            return AVERROR_INVALIDDATA;
60
1.99k
        }
61
62
44.9k
        if (avctx->extradata_size < rpr * 2 + 8) {
63
372
            av_log(avctx, AV_LOG_ERROR,
64
372
                   "Insufficient extradata - need at least %d bytes, got %d\n",
65
372
                   8 + rpr * 2, avctx->extradata_size);
66
372
            return AVERROR(EINVAL);
67
372
        }
68
69
44.5k
        w = r->s.avctx->extradata[6 + rpr*2] << 2;
70
44.5k
        h = r->s.avctx->extradata[7 + rpr*2] << 2;
71
166k
    } else {
72
166k
        w = r->orig_width;
73
166k
        h = r->orig_height;
74
166k
    }
75
211k
    si->width  = w;
76
211k
    si->height = h;
77
211k
    mb_size = ((w + 15) >> 4) * ((h + 15) >> 4);
78
211k
    si->start = ff_rv34_get_start_offset(gb, mb_size);
79
211k
    skip_bits1(gb);
80
211k
    return 0;
81
213k
}
82
83
/**
84
 * Decode 4x4 intra types array.
85
 */
86
static int rv30_decode_intra_types(RV34DecContext *r, GetBitContext *gb, int8_t *dst)
87
99.1k
{
88
99.1k
    int i, j, k;
89
90
418k
    for(i = 0; i < 4; i++, dst += r->intra_types_stride - 4){
91
981k
        for(j = 0; j < 4; j+= 2){
92
661k
            unsigned code = get_interleaved_ue_golomb(gb) << 1;
93
661k
            if (code > 80U*2U) {
94
15.0k
                av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction code\n");
95
15.0k
                return -1;
96
15.0k
            }
97
1.93M
            for(k = 0; k < 2; k++){
98
1.28M
                int A = dst[-r->intra_types_stride] + 1;
99
1.28M
                int B = dst[-1] + 1;
100
1.28M
                *dst++ = rv30_itype_from_context[A * 90 + B * 9 + rv30_itype_code[code + k]];
101
1.28M
                if(dst[-1] == 9){
102
4.24k
                    av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction mode\n");
103
4.24k
                    return -1;
104
4.24k
                }
105
1.28M
            }
106
646k
        }
107
339k
    }
108
79.8k
    return 0;
109
99.1k
}
110
111
/**
112
 * Decode macroblock information.
113
 */
114
static int rv30_decode_mb_info(RV34DecContext *r)
115
2.28M
{
116
2.28M
    static const int rv30_p_types[6] = { RV34_MB_SKIP, RV34_MB_P_16x16, RV34_MB_P_8x8, -1, RV34_MB_TYPE_INTRA, RV34_MB_TYPE_INTRA16x16 };
117
2.28M
    static const int rv30_b_types[6] = { RV34_MB_SKIP, RV34_MB_B_DIRECT, RV34_MB_B_FORWARD, RV34_MB_B_BACKWARD, RV34_MB_TYPE_INTRA, RV34_MB_TYPE_INTRA16x16 };
118
2.28M
    MpegEncContext *s = &r->s;
119
2.28M
    GetBitContext *const gb = &r->gb;
120
2.28M
    unsigned code = get_interleaved_ue_golomb(gb);
121
122
2.28M
    if (code > 11) {
123
4.05k
        av_log(s->avctx, AV_LOG_ERROR, "Incorrect MB type code\n");
124
4.05k
        return -1;
125
4.05k
    }
126
2.28M
    if(code > 5){
127
19.4k
        av_log(s->avctx, AV_LOG_ERROR, "dquant needed\n");
128
19.4k
        code -= 6;
129
19.4k
    }
130
2.28M
    if(s->pict_type != AV_PICTURE_TYPE_B)
131
888k
        return rv30_p_types[code];
132
1.39M
    else
133
1.39M
        return rv30_b_types[code];
134
2.28M
}
135
136
static inline void rv30_weak_loop_filter(uint8_t *src, const int step,
137
                                         const int stride, const int lim)
138
4.66M
{
139
4.66M
    const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
140
4.66M
    int i, diff;
141
142
23.3M
    for(i = 0; i < 4; i++){
143
18.6M
        diff = ((src[-2*step] - src[1*step]) - (src[-1*step] - src[0*step])*4) >> 3;
144
18.6M
        diff = av_clip(diff, -lim, lim);
145
18.6M
        src[-1*step] = cm[src[-1*step] + diff];
146
18.6M
        src[ 0*step] = cm[src[ 0*step] - diff];
147
18.6M
        src += stride;
148
18.6M
    }
149
4.66M
}
150
151
static void rv30_loop_filter(RV34DecContext *r, int row)
152
418k
{
153
418k
    MpegEncContext *s = &r->s;
154
418k
    int mb_pos, mb_x;
155
418k
    int i, j, k;
156
418k
    uint8_t *Y, *C;
157
418k
    int loc_lim, cur_lim, left_lim = 0, top_lim = 0;
158
159
418k
    mb_pos = row * s->mb_stride;
160
5.20M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
161
4.78M
        int mbtype = s->cur_pic.mb_type[mb_pos];
162
4.78M
        if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
163
1.13M
            r->deblock_coefs[mb_pos] = 0xFFFF;
164
4.78M
        if(IS_INTRA(mbtype))
165
1.13M
            r->cbp_chroma[mb_pos] = 0xFF;
166
4.78M
    }
167
168
    /* all vertical edges are filtered first
169
     * and horizontal edges are filtered on the next iteration
170
     */
171
418k
    mb_pos = row * s->mb_stride;
172
5.20M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
173
4.78M
        cur_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos]];
174
4.78M
        if(mb_x)
175
4.36M
            left_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos - 1]];
176
23.9M
        for(j = 0; j < 16; j += 4){
177
19.1M
            Y = s->cur_pic.data[0] + mb_x*16 + (row*16 + j) * s->linesize + 4 * !mb_x;
178
94.0M
            for(i = !mb_x; i < 4; i++, Y += 4){
179
74.8M
                int ij = i + j;
180
74.8M
                loc_lim = 0;
181
74.8M
                if(r->deblock_coefs[mb_pos] & (1 << ij))
182
23.6M
                    loc_lim = cur_lim;
183
51.2M
                else if(!i && r->deblock_coefs[mb_pos - 1] & (1 << (ij + 3)))
184
126k
                    loc_lim = left_lim;
185
51.0M
                else if( i && r->deblock_coefs[mb_pos]     & (1 << (ij - 1)))
186
112k
                    loc_lim = cur_lim;
187
74.8M
                if(loc_lim)
188
1.60M
                    rv30_weak_loop_filter(Y, 1, s->linesize, loc_lim);
189
74.8M
            }
190
19.1M
        }
191
14.3M
        for(k = 0; k < 2; k++){
192
9.56M
            int cur_cbp, left_cbp = 0;
193
9.56M
            cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF;
194
9.56M
            if(mb_x)
195
8.73M
                left_cbp = (r->cbp_chroma[mb_pos - 1] >> (k*4)) & 0xF;
196
28.7M
            for(j = 0; j < 8; j += 4){
197
19.1M
                C = s->cur_pic.data[k + 1] + mb_x*8 + (row*8 + j) * s->uvlinesize + 4 * !mb_x;
198
55.7M
                for(i = !mb_x; i < 2; i++, C += 4){
199
36.6M
                    int ij = i + (j >> 1);
200
36.6M
                    loc_lim = 0;
201
36.6M
                    if (cur_cbp & (1 << ij))
202
11.5M
                        loc_lim = cur_lim;
203
25.0M
                    else if(!i && left_cbp & (1 << (ij + 1)))
204
121k
                        loc_lim = left_lim;
205
24.8M
                    else if( i && cur_cbp  & (1 << (ij - 1)))
206
4.34k
                        loc_lim = cur_lim;
207
36.6M
                    if(loc_lim)
208
671k
                        rv30_weak_loop_filter(C, 1, s->uvlinesize, loc_lim);
209
36.6M
                }
210
19.1M
            }
211
9.56M
        }
212
4.78M
    }
213
418k
    mb_pos = row * s->mb_stride;
214
5.20M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
215
4.78M
        cur_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos]];
216
4.78M
        if(row)
217
4.32M
            top_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos - s->mb_stride]];
218
23.4M
        for(j = 4*!row; j < 16; j += 4){
219
18.6M
            Y = s->cur_pic.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
220
93.4M
            for(i = 0; i < 4; i++, Y += 4){
221
74.7M
                int ij = i + j;
222
74.7M
                loc_lim = 0;
223
74.7M
                if(r->deblock_coefs[mb_pos] & (1 << ij))
224
22.8M
                    loc_lim = cur_lim;
225
51.8M
                else if(!j && r->deblock_coefs[mb_pos - s->mb_stride] & (1 << (ij + 12)))
226
478k
                    loc_lim = top_lim;
227
51.3M
                else if( j && r->deblock_coefs[mb_pos]                & (1 << (ij - 4)))
228
109k
                    loc_lim = cur_lim;
229
74.7M
                if(loc_lim)
230
1.66M
                    rv30_weak_loop_filter(Y, s->linesize, 1, loc_lim);
231
74.7M
            }
232
18.6M
        }
233
14.3M
        for(k = 0; k < 2; k++){
234
9.56M
            int cur_cbp, top_cbp = 0;
235
9.56M
            cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF;
236
9.56M
            if(row)
237
8.65M
                top_cbp = (r->cbp_chroma[mb_pos - s->mb_stride] >> (k*4)) & 0xF;
238
27.7M
            for(j = 4*!row; j < 8; j += 4){
239
18.2M
                C = s->cur_pic.data[k+1] + mb_x*8 + (row*8 + j) * s->uvlinesize;
240
54.6M
                for(i = 0; i < 2; i++, C += 4){
241
36.4M
                    int ij = i + (j >> 1);
242
36.4M
                    loc_lim = 0;
243
36.4M
                    if (r->cbp_chroma[mb_pos] & (1 << ij))
244
10.7M
                        loc_lim = cur_lim;
245
25.6M
                    else if(!j && top_cbp & (1 << (ij + 2)))
246
281k
                        loc_lim = top_lim;
247
25.3M
                    else if( j && cur_cbp & (1 << (ij - 2)))
248
4.24k
                        loc_lim = cur_lim;
249
36.4M
                    if(loc_lim)
250
720k
                        rv30_weak_loop_filter(C, s->uvlinesize, 1, loc_lim);
251
36.4M
                }
252
18.2M
            }
253
9.56M
        }
254
4.78M
    }
255
418k
}
256
257
/**
258
 * Initialize decoder.
259
 */
260
static av_cold int rv30_decode_init(AVCodecContext *avctx)
261
2.47k
{
262
2.47k
    RV34DecContext *r = avctx->priv_data;
263
2.47k
    int ret;
264
265
2.47k
    r->orig_width  = avctx->coded_width;
266
2.47k
    r->orig_height = avctx->coded_height;
267
268
2.47k
    if (avctx->extradata_size < 2) {
269
185
        av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
270
185
        return AVERROR(EINVAL);
271
185
    }
272
2.28k
    r->rv30 = 1;
273
2.28k
    if ((ret = ff_rv34_decode_init(avctx)) < 0)
274
0
        return ret;
275
276
2.28k
    r->max_rpr = avctx->extradata[1] & 7;
277
2.28k
    if(avctx->extradata_size < 2*r->max_rpr + 8){
278
859
        av_log(avctx, AV_LOG_WARNING, "Insufficient extradata - need at least %d bytes, got %d\n",
279
859
               2*r->max_rpr + 8, avctx->extradata_size);
280
859
    }
281
282
2.28k
    r->parse_slice_header = rv30_parse_slice_header;
283
2.28k
    r->decode_intra_types = rv30_decode_intra_types;
284
2.28k
    r->decode_mb_info     = rv30_decode_mb_info;
285
2.28k
    r->loop_filter        = rv30_loop_filter;
286
2.28k
    r->luma_dc_quant_i = rv30_luma_dc_quant;
287
2.28k
    r->luma_dc_quant_p = rv30_luma_dc_quant;
288
2.28k
    ff_rv30dsp_init(&r->rdsp);
289
2.28k
    return 0;
290
2.28k
}
291
292
const FFCodec ff_rv30_decoder = {
293
    .p.name                = "rv30",
294
    CODEC_LONG_NAME("RealVideo 3.0"),
295
    .p.type                = AVMEDIA_TYPE_VIDEO,
296
    .p.id                  = AV_CODEC_ID_RV30,
297
    .priv_data_size        = sizeof(RV34DecContext),
298
    .init                  = rv30_decode_init,
299
    .close                 = ff_rv34_decode_end,
300
    FF_CODEC_DECODE_CB(ff_rv34_decode_frame),
301
    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
302
                             AV_CODEC_CAP_FRAME_THREADS,
303
    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP,
304
    .flush                 = ff_mpeg_flush,
305
    UPDATE_THREAD_CONTEXT(ff_rv34_decode_update_thread_context),
306
};