Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/roqvideodec.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2003 The FFmpeg project
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * id RoQ Video Decoder by Dr. Tim Ferguson
24
 * For more information about the id RoQ format, visit:
25
 *   http://www.csse.monash.edu.au/~timf/
26
 */
27
28
#include "libavutil/avassert.h"
29
30
#include "avcodec.h"
31
#include "bytestream.h"
32
#include "codec_internal.h"
33
#include "decode.h"
34
#include "roqvideo.h"
35
36
static void roqvideo_decode_frame(RoqContext *ri, GetByteContext *gb)
37
152k
{
38
152k
    unsigned int chunk_id = 0, chunk_arg = 0;
39
152k
    unsigned long chunk_size = 0;
40
152k
    int i, j, k, nv1, nv2, vqflg = 0, vqflg_pos = -1;
41
152k
    int vqid, xpos, ypos, xp, yp, x, y, mx, my;
42
152k
    roq_qcell *qcell;
43
152k
    int64_t chunk_start;
44
45
1.09M
    while (bytestream2_get_bytes_left(gb) >= 8) {
46
943k
        chunk_id   = bytestream2_get_le16(gb);
47
943k
        chunk_size = bytestream2_get_le32(gb);
48
943k
        chunk_arg  = bytestream2_get_le16(gb);
49
50
943k
        if(chunk_id == RoQ_QUAD_VQ)
51
460
            break;
52
943k
        if(chunk_id == RoQ_QUAD_CODEBOOK) {
53
1.14k
            if((nv1 = chunk_arg >> 8) == 0)
54
313
                nv1 = 256;
55
1.14k
            if((nv2 = chunk_arg & 0xff) == 0 && nv1 * 6 < chunk_size)
56
310
                nv2 = 256;
57
182k
            for(i = 0; i < nv1; i++) {
58
181k
                ri->cb2x2[i].y[0] = bytestream2_get_byte(gb);
59
181k
                ri->cb2x2[i].y[1] = bytestream2_get_byte(gb);
60
181k
                ri->cb2x2[i].y[2] = bytestream2_get_byte(gb);
61
181k
                ri->cb2x2[i].y[3] = bytestream2_get_byte(gb);
62
181k
                ri->cb2x2[i].u    = bytestream2_get_byte(gb);
63
181k
                ri->cb2x2[i].v    = bytestream2_get_byte(gb);
64
181k
            }
65
138k
            for(i = 0; i < nv2; i++)
66
688k
                for(j = 0; j < 4; j++)
67
550k
                    ri->cb4x4[i].idx[j] = bytestream2_get_byte(gb);
68
1.14k
        }
69
943k
    }
70
71
152k
    chunk_start = bytestream2_tell(gb);
72
152k
    xpos = ypos = 0;
73
74
152k
    if (chunk_size > bytestream2_get_bytes_left(gb)) {
75
9.96k
        av_log(ri->logctx, AV_LOG_ERROR, "Chunk does not fit in input buffer\n");
76
9.96k
        chunk_size = bytestream2_get_bytes_left(gb);
77
9.96k
    }
78
79
1.96M
    while (bytestream2_tell(gb) < chunk_start + chunk_size) {
80
5.44M
        for (yp = ypos; yp < ypos + 16; yp += 8)
81
10.8M
            for (xp = xpos; xp < xpos + 16; xp += 8) {
82
7.25M
                if (bytestream2_tell(gb) >= chunk_start + chunk_size) {
83
4.63k
                    av_log(ri->logctx, AV_LOG_VERBOSE, "Chunk is too short\n");
84
4.63k
                    return;
85
4.63k
                }
86
7.25M
                if (vqflg_pos < 0) {
87
945k
                    vqflg = bytestream2_get_le16(gb);
88
945k
                    vqflg_pos = 7;
89
945k
                }
90
7.25M
                vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
91
7.25M
                vqflg_pos--;
92
93
7.25M
                switch(vqid) {
94
6.54M
                case RoQ_ID_MOT:
95
6.54M
                    break;
96
255k
                case RoQ_ID_FCC: {
97
255k
                    int byte = bytestream2_get_byte(gb);
98
255k
                    mx = 8 - (byte >> 4) - ((signed char) (chunk_arg >> 8));
99
255k
                    my = 8 - (byte & 0xf) - ((signed char) chunk_arg);
100
255k
                    ff_apply_motion_8x8(ri, xp, yp, mx, my);
101
255k
                    break;
102
0
                }
103
253k
                case RoQ_ID_SLD:
104
253k
                    qcell = ri->cb4x4 + bytestream2_get_byte(gb);
105
253k
                    ff_apply_vector_4x4(ri, xp,     yp,     ri->cb2x2 + qcell->idx[0]);
106
253k
                    ff_apply_vector_4x4(ri, xp + 4, yp,     ri->cb2x2 + qcell->idx[1]);
107
253k
                    ff_apply_vector_4x4(ri, xp,     yp + 4, ri->cb2x2 + qcell->idx[2]);
108
253k
                    ff_apply_vector_4x4(ri, xp + 4, yp + 4, ri->cb2x2 + qcell->idx[3]);
109
253k
                    break;
110
203k
                case RoQ_ID_CCC:
111
1.01M
                    for (k = 0; k < 4; k++) {
112
810k
                        x = xp; y = yp;
113
810k
                        if(k & 0x01) x += 4;
114
810k
                        if(k & 0x02) y += 4;
115
116
810k
                        if (bytestream2_tell(gb) >= chunk_start + chunk_size) {
117
2.90k
                            av_log(ri->logctx, AV_LOG_VERBOSE, "Chunk is too short\n");
118
2.90k
                            return;
119
2.90k
                        }
120
807k
                        if (vqflg_pos < 0) {
121
67.9k
                            vqflg = bytestream2_get_le16(gb);
122
67.9k
                            vqflg_pos = 7;
123
67.9k
                        }
124
807k
                        vqid = (vqflg >> (vqflg_pos * 2)) & 0x3;
125
807k
                        vqflg_pos--;
126
807k
                        switch(vqid) {
127
247k
                        case RoQ_ID_MOT:
128
247k
                            break;
129
101k
                        case RoQ_ID_FCC: {
130
101k
                            int byte = bytestream2_get_byte(gb);
131
101k
                            mx = 8 - (byte >> 4) - ((signed char) (chunk_arg >> 8));
132
101k
                            my = 8 - (byte & 0xf) - ((signed char) chunk_arg);
133
101k
                            ff_apply_motion_4x4(ri, x, y, mx, my);
134
101k
                            break;
135
0
                        }
136
122k
                        case RoQ_ID_SLD:
137
122k
                            qcell = ri->cb4x4 + bytestream2_get_byte(gb);
138
122k
                            ff_apply_vector_2x2(ri, x,     y,     ri->cb2x2 + qcell->idx[0]);
139
122k
                            ff_apply_vector_2x2(ri, x + 2, y,     ri->cb2x2 + qcell->idx[1]);
140
122k
                            ff_apply_vector_2x2(ri, x,     y + 2, ri->cb2x2 + qcell->idx[2]);
141
122k
                            ff_apply_vector_2x2(ri, x + 2, y + 2, ri->cb2x2 + qcell->idx[3]);
142
122k
                            break;
143
335k
                        case RoQ_ID_CCC:
144
335k
                            ff_apply_vector_2x2(ri, x,     y,     ri->cb2x2 + bytestream2_get_byte(gb));
145
335k
                            ff_apply_vector_2x2(ri, x + 2, y,     ri->cb2x2 + bytestream2_get_byte(gb));
146
335k
                            ff_apply_vector_2x2(ri, x,     y + 2, ri->cb2x2 + bytestream2_get_byte(gb));
147
335k
                            ff_apply_vector_2x2(ri, x + 2, y + 2, ri->cb2x2 + bytestream2_get_byte(gb));
148
335k
                            break;
149
807k
                        }
150
807k
                    }
151
200k
                    break;
152
200k
                default:
153
0
                    av_assert2(0);
154
7.25M
            }
155
7.25M
        }
156
157
1.81M
        xpos += 16;
158
1.81M
        if (xpos >= ri->width) {
159
444k
            xpos -= ri->width;
160
444k
            ypos += 16;
161
444k
        }
162
1.81M
        if(ypos >= ri->height)
163
297
            break;
164
1.81M
    }
165
152k
}
166
167
168
static av_cold int roq_decode_init(AVCodecContext *avctx)
169
1.02k
{
170
1.02k
    RoqContext *s = avctx->priv_data;
171
172
1.02k
    s->logctx = avctx;
173
174
1.02k
    if (avctx->width % 16 || avctx->height % 16) {
175
39
        avpriv_request_sample(avctx, "Dimensions not being a multiple of 16");
176
39
        return AVERROR_PATCHWELCOME;
177
39
    }
178
179
986
    s->width = avctx->width;
180
986
    s->height = avctx->height;
181
182
986
    s->last_frame    = av_frame_alloc();
183
986
    s->current_frame = av_frame_alloc();
184
986
    if (!s->current_frame || !s->last_frame)
185
0
        return AVERROR(ENOMEM);
186
187
986
    avctx->pix_fmt = AV_PIX_FMT_YUVJ444P;
188
986
    avctx->color_range = AVCOL_RANGE_JPEG;
189
190
986
    return 0;
191
986
}
192
193
static int roq_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
194
                            int *got_frame, AVPacket *avpkt)
195
1.41M
{
196
1.41M
    const uint8_t *buf = avpkt->data;
197
1.41M
    int buf_size = avpkt->size;
198
1.41M
    RoqContext *s = avctx->priv_data;
199
1.41M
    int copy = !s->current_frame->data[0] && s->last_frame->data[0];
200
1.41M
    GetByteContext gb;
201
1.41M
    int ret;
202
203
1.41M
    if ((ret = ff_reget_buffer(avctx, s->current_frame, 0)) < 0)
204
1.26M
        return ret;
205
206
152k
    if (copy) {
207
324
        ret = av_frame_copy(s->current_frame, s->last_frame);
208
324
        if (ret < 0)
209
0
            return ret;
210
324
    }
211
212
152k
    bytestream2_init(&gb, buf, buf_size);
213
152k
    roqvideo_decode_frame(s, &gb);
214
215
152k
    if ((ret = av_frame_ref(rframe, s->current_frame)) < 0)
216
0
        return ret;
217
152k
    *got_frame      = 1;
218
219
    /* shuffle frames */
220
152k
    FFSWAP(AVFrame *, s->current_frame, s->last_frame);
221
222
152k
    return buf_size;
223
152k
}
224
225
static av_cold int roq_decode_end(AVCodecContext *avctx)
226
1.02k
{
227
1.02k
    RoqContext *s = avctx->priv_data;
228
229
1.02k
    av_frame_free(&s->current_frame);
230
1.02k
    av_frame_free(&s->last_frame);
231
232
1.02k
    return 0;
233
1.02k
}
234
235
const FFCodec ff_roq_decoder = {
236
    .p.name         = "roqvideo",
237
    CODEC_LONG_NAME("id RoQ video"),
238
    .p.type         = AVMEDIA_TYPE_VIDEO,
239
    .p.id           = AV_CODEC_ID_ROQ,
240
    .priv_data_size = sizeof(RoqContext),
241
    .init           = roq_decode_init,
242
    .close          = roq_decode_end,
243
    FF_CODEC_DECODE_CB(roq_decode_frame),
244
    .p.capabilities = AV_CODEC_CAP_DR1,
245
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
246
};