Coverage Report

Created: 2026-02-14 06:59

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
265k
{
40
265k
    AVCodecContext *avctx = r->s.avctx;
41
265k
    int w = r->s.width, h = r->s.height;
42
265k
    int mb_size;
43
265k
    int rpr;
44
45
265k
    memset(si, 0, sizeof(SliceInfo));
46
265k
    if(get_bits(gb, 3))
47
3.86k
        return -1;
48
261k
    si->type = get_bits(gb, 2);
49
261k
    if(si->type == 1) si->type = 0;
50
261k
    if(get_bits1(gb))
51
325
        return -1;
52
261k
    si->quant = get_bits(gb, 5);
53
261k
    skip_bits1(gb);
54
261k
    si->pts = get_bits(gb, 13);
55
261k
    rpr = get_bits(gb, av_log2(r->max_rpr) + 1);
56
261k
    if(rpr){
57
42.9k
        if (rpr > r->max_rpr) {
58
13.1k
            av_log(avctx, AV_LOG_ERROR, "rpr too large\n");
59
13.1k
            return AVERROR_INVALIDDATA;
60
13.1k
        }
61
62
29.7k
        if (avctx->extradata_size < rpr * 2 + 8) {
63
324
            av_log(avctx, AV_LOG_ERROR,
64
324
                   "Insufficient extradata - need at least %d bytes, got %d\n",
65
324
                   8 + rpr * 2, avctx->extradata_size);
66
324
            return AVERROR(EINVAL);
67
324
        }
68
69
29.4k
        w = r->s.avctx->extradata[6 + rpr*2] << 2;
70
29.4k
        h = r->s.avctx->extradata[7 + rpr*2] << 2;
71
218k
    } else {
72
218k
        w = r->orig_width;
73
218k
        h = r->orig_height;
74
218k
    }
75
247k
    si->width  = w;
76
247k
    si->height = h;
77
247k
    mb_size = ((w + 15) >> 4) * ((h + 15) >> 4);
78
247k
    si->start = ff_rv34_get_start_offset(gb, mb_size);
79
247k
    skip_bits1(gb);
80
247k
    return 0;
81
261k
}
82
83
/**
84
 * Decode 4x4 intra types array.
85
 */
86
static int rv30_decode_intra_types(RV34DecContext *r, GetBitContext *gb, int8_t *dst)
87
102k
{
88
102k
    int i, j, k;
89
90
460k
    for(i = 0; i < 4; i++, dst += r->intra_types_stride - 4){
91
1.09M
        for(j = 0; j < 4; j+= 2){
92
733k
            unsigned code = get_interleaved_ue_golomb(gb) << 1;
93
733k
            if (code > 80U*2U) {
94
8.21k
                av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction code\n");
95
8.21k
                return -1;
96
8.21k
            }
97
2.16M
            for(k = 0; k < 2; k++){
98
1.44M
                int A = dst[-r->intra_types_stride] + 1;
99
1.44M
                int B = dst[-1] + 1;
100
1.44M
                *dst++ = rv30_itype_from_context[A * 90 + B * 9 + rv30_itype_code[code + k]];
101
1.44M
                if(dst[-1] == 9){
102
4.59k
                    av_log(r->s.avctx, AV_LOG_ERROR, "Incorrect intra prediction mode\n");
103
4.59k
                    return -1;
104
4.59k
                }
105
1.44M
            }
106
725k
        }
107
371k
    }
108
89.5k
    return 0;
109
102k
}
110
111
/**
112
 * Decode macroblock information.
113
 */
114
static int rv30_decode_mb_info(RV34DecContext *r)
115
2.45M
{
116
2.45M
    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.45M
    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.45M
    MpegEncContext *s = &r->s;
119
2.45M
    GetBitContext *const gb = &r->gb;
120
2.45M
    unsigned code = get_interleaved_ue_golomb(gb);
121
122
2.45M
    if (code > 11) {
123
7.20k
        av_log(s->avctx, AV_LOG_ERROR, "Incorrect MB type code\n");
124
7.20k
        return -1;
125
7.20k
    }
126
2.44M
    if(code > 5){
127
34.6k
        av_log(s->avctx, AV_LOG_ERROR, "dquant needed\n");
128
34.6k
        code -= 6;
129
34.6k
    }
130
2.44M
    if(s->pict_type != AV_PICTURE_TYPE_B)
131
816k
        return rv30_p_types[code];
132
1.62M
    else
133
1.62M
        return rv30_b_types[code];
134
2.44M
}
135
136
static inline void rv30_weak_loop_filter(uint8_t *src, const int step,
137
                                         const int stride, const int lim)
138
6.95M
{
139
6.95M
    const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
140
6.95M
    int i, diff;
141
142
34.7M
    for(i = 0; i < 4; i++){
143
27.8M
        diff = ((src[-2*step] - src[1*step]) - (src[-1*step] - src[0*step])*4) >> 3;
144
27.8M
        diff = av_clip(diff, -lim, lim);
145
27.8M
        src[-1*step] = cm[src[-1*step] + diff];
146
27.8M
        src[ 0*step] = cm[src[ 0*step] - diff];
147
27.8M
        src += stride;
148
27.8M
    }
149
6.95M
}
150
151
static void rv30_loop_filter(RV34DecContext *r, int row)
152
397k
{
153
397k
    MpegEncContext *s = &r->s;
154
397k
    int mb_pos, mb_x;
155
397k
    int i, j, k;
156
397k
    uint8_t *Y, *C;
157
397k
    int loc_lim, cur_lim, left_lim = 0, top_lim = 0;
158
159
397k
    mb_pos = row * s->mb_stride;
160
5.23M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
161
4.83M
        int mbtype = s->cur_pic.mb_type[mb_pos];
162
4.83M
        if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
163
1.24M
            r->deblock_coefs[mb_pos] = 0xFFFF;
164
4.83M
        if(IS_INTRA(mbtype))
165
1.24M
            r->cbp_chroma[mb_pos] = 0xFF;
166
4.83M
    }
167
168
    /* all vertical edges are filtered first
169
     * and horizontal edges are filtered on the next iteration
170
     */
171
397k
    mb_pos = row * s->mb_stride;
172
5.23M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
173
4.83M
        cur_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos]];
174
4.83M
        if(mb_x)
175
4.43M
            left_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos - 1]];
176
24.1M
        for(j = 0; j < 16; j += 4){
177
19.3M
            Y = s->cur_pic.data[0] + mb_x*16 + (row*16 + j) * s->linesize + 4 * !mb_x;
178
95.0M
            for(i = !mb_x; i < 4; i++, Y += 4){
179
75.7M
                int ij = i + j;
180
75.7M
                loc_lim = 0;
181
75.7M
                if(r->deblock_coefs[mb_pos] & (1 << ij))
182
25.3M
                    loc_lim = cur_lim;
183
50.4M
                else if(!i && r->deblock_coefs[mb_pos - 1] & (1 << (ij + 3)))
184
163k
                    loc_lim = left_lim;
185
50.2M
                else if( i && r->deblock_coefs[mb_pos]     & (1 << (ij - 1)))
186
113k
                    loc_lim = cur_lim;
187
75.7M
                if(loc_lim)
188
2.36M
                    rv30_weak_loop_filter(Y, 1, s->linesize, loc_lim);
189
75.7M
            }
190
19.3M
        }
191
14.5M
        for(k = 0; k < 2; k++){
192
9.66M
            int cur_cbp, left_cbp = 0;
193
9.66M
            cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF;
194
9.66M
            if(mb_x)
195
8.87M
                left_cbp = (r->cbp_chroma[mb_pos - 1] >> (k*4)) & 0xF;
196
29.0M
            for(j = 0; j < 8; j += 4){
197
19.3M
                C = s->cur_pic.data[k + 1] + mb_x*8 + (row*8 + j) * s->uvlinesize + 4 * !mb_x;
198
56.4M
                for(i = !mb_x; i < 2; i++, C += 4){
199
37.0M
                    int ij = i + (j >> 1);
200
37.0M
                    loc_lim = 0;
201
37.0M
                    if (cur_cbp & (1 << ij))
202
12.3M
                        loc_lim = cur_lim;
203
24.6M
                    else if(!i && left_cbp & (1 << (ij + 1)))
204
157k
                        loc_lim = left_lim;
205
24.5M
                    else if( i && cur_cbp  & (1 << (ij - 1)))
206
6.01k
                        loc_lim = cur_lim;
207
37.0M
                    if(loc_lim)
208
1.02M
                        rv30_weak_loop_filter(C, 1, s->uvlinesize, loc_lim);
209
37.0M
                }
210
19.3M
            }
211
9.66M
        }
212
4.83M
    }
213
397k
    mb_pos = row * s->mb_stride;
214
5.23M
    for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
215
4.83M
        cur_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos]];
216
4.83M
        if(row)
217
4.36M
            top_lim = rv30_loop_filt_lim[s->cur_pic.qscale_table[mb_pos - s->mb_stride]];
218
23.6M
        for(j = 4*!row; j < 16; j += 4){
219
18.8M
            Y = s->cur_pic.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
220
94.3M
            for(i = 0; i < 4; i++, Y += 4){
221
75.4M
                int ij = i + j;
222
75.4M
                loc_lim = 0;
223
75.4M
                if(r->deblock_coefs[mb_pos] & (1 << ij))
224
24.4M
                    loc_lim = cur_lim;
225
51.0M
                else if(!j && r->deblock_coefs[mb_pos - s->mb_stride] & (1 << (ij + 12)))
226
584k
                    loc_lim = top_lim;
227
50.4M
                else if( j && r->deblock_coefs[mb_pos]                & (1 << (ij - 4)))
228
124k
                    loc_lim = cur_lim;
229
75.4M
                if(loc_lim)
230
2.45M
                    rv30_weak_loop_filter(Y, s->linesize, 1, loc_lim);
231
75.4M
            }
232
18.8M
        }
233
14.5M
        for(k = 0; k < 2; k++){
234
9.66M
            int cur_cbp, top_cbp = 0;
235
9.66M
            cur_cbp = (r->cbp_chroma[mb_pos] >> (k*4)) & 0xF;
236
9.66M
            if(row)
237
8.72M
                top_cbp = (r->cbp_chroma[mb_pos - s->mb_stride] >> (k*4)) & 0xF;
238
28.0M
            for(j = 4*!row; j < 8; j += 4){
239
18.3M
                C = s->cur_pic.data[k+1] + mb_x*8 + (row*8 + j) * s->uvlinesize;
240
55.1M
                for(i = 0; i < 2; i++, C += 4){
241
36.7M
                    int ij = i + (j >> 1);
242
36.7M
                    loc_lim = 0;
243
36.7M
                    if (r->cbp_chroma[mb_pos] & (1 << ij))
244
11.4M
                        loc_lim = cur_lim;
245
25.3M
                    else if(!j && top_cbp & (1 << (ij + 2)))
246
361k
                        loc_lim = top_lim;
247
24.9M
                    else if( j && cur_cbp & (1 << (ij - 2)))
248
5.89k
                        loc_lim = cur_lim;
249
36.7M
                    if(loc_lim)
250
1.10M
                        rv30_weak_loop_filter(C, s->uvlinesize, 1, loc_lim);
251
36.7M
                }
252
18.3M
            }
253
9.66M
        }
254
4.83M
    }
255
397k
}
256
257
/**
258
 * Initialize decoder.
259
 */
260
static av_cold int rv30_decode_init(AVCodecContext *avctx)
261
2.51k
{
262
2.51k
    RV34DecContext *r = avctx->priv_data;
263
2.51k
    int ret;
264
265
2.51k
    r->orig_width  = avctx->coded_width;
266
2.51k
    r->orig_height = avctx->coded_height;
267
268
2.51k
    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.32k
    r->rv30 = 1;
273
2.32k
    if ((ret = ff_rv34_decode_init(avctx)) < 0)
274
0
        return ret;
275
276
2.32k
    r->max_rpr = avctx->extradata[1] & 7;
277
2.32k
    if(avctx->extradata_size < 2*r->max_rpr + 8){
278
766
        av_log(avctx, AV_LOG_WARNING, "Insufficient extradata - need at least %d bytes, got %d\n",
279
766
               2*r->max_rpr + 8, avctx->extradata_size);
280
766
    }
281
282
2.32k
    r->parse_slice_header = rv30_parse_slice_header;
283
2.32k
    r->decode_intra_types = rv30_decode_intra_types;
284
2.32k
    r->decode_mb_info     = rv30_decode_mb_info;
285
2.32k
    r->loop_filter        = rv30_loop_filter;
286
2.32k
    r->luma_dc_quant_i = rv30_luma_dc_quant;
287
2.32k
    r->luma_dc_quant_p = rv30_luma_dc_quant;
288
2.32k
    ff_rv30dsp_init(&r->rdsp);
289
2.32k
    return 0;
290
2.32k
}
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
};