GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/jvdec.c Lines: 115 126 91.3 %
Date: 2019-11-18 18:00:01 Branches: 73 86 84.9 %

Line Branch Exec Source
1
/*
2
 * Bitmap Brothers JV video decoder
3
 * Copyright (c) 2011 Peter Ross <pross@xvid.org>
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
 * Bitmap Brothers JV video decoder
25
 * @author Peter Ross <pross@xvid.org>
26
 */
27
28
#include "libavutil/intreadwrite.h"
29
30
#include "avcodec.h"
31
#include "blockdsp.h"
32
#include "get_bits.h"
33
#include "internal.h"
34
35
typedef struct JvContext {
36
    BlockDSPContext bdsp;
37
    AVFrame   *frame;
38
    uint32_t   palette[AVPALETTE_COUNT];
39
    int        palette_has_changed;
40
} JvContext;
41
42
3
static av_cold int decode_init(AVCodecContext *avctx)
43
{
44
3
    JvContext *s = avctx->priv_data;
45
46

3
    if (!avctx->width || !avctx->height ||
47

3
        (avctx->width & 7) || (avctx->height & 7)) {
48
        av_log(avctx, AV_LOG_ERROR, "Invalid video dimensions: %dx%d\n",
49
               avctx->width, avctx->height);
50
        return AVERROR(EINVAL);
51
    }
52
53
3
    s->frame = av_frame_alloc();
54
3
    if (!s->frame)
55
        return AVERROR(ENOMEM);
56
57
3
    avctx->pix_fmt = AV_PIX_FMT_PAL8;
58
3
    ff_blockdsp_init(&s->bdsp, avctx);
59
3
    return 0;
60
}
61
62
/**
63
 * Decode 2x2 block
64
 */
65
14056
static inline void decode2x2(GetBitContext *gb, uint8_t *dst, int linesize)
66
{
67
    int i, j, v[2];
68
69

14056
    switch (get_bits(gb, 2)) {
70
348
    case 1:
71
348
        v[0] = get_bits(gb, 8);
72
1044
        for (j = 0; j < 2; j++)
73
696
            memset(dst + j * linesize, v[0], 2);
74
348
        break;
75
9984
    case 2:
76
9984
        v[0] = get_bits(gb, 8);
77
9984
        v[1] = get_bits(gb, 8);
78
29952
        for (j = 0; j < 2; j++)
79
59904
            for (i = 0; i < 2; i++)
80
39936
                dst[j * linesize + i] = v[get_bits1(gb)];
81
9984
        break;
82
51
    case 3:
83
153
        for (j = 0; j < 2; j++)
84
306
            for (i = 0; i < 2; i++)
85
204
                dst[j * linesize + i] = get_bits(gb, 8);
86
    }
87
14056
}
88
89
/**
90
 * Decode 4x4 block
91
 */
92
17028
static inline void decode4x4(GetBitContext *gb, uint8_t *dst, int linesize)
93
{
94
    int i, j, v[2];
95
96

17028
    switch (get_bits(gb, 2)) {
97
139
    case 1:
98
139
        v[0] = get_bits(gb, 8);
99
695
        for (j = 0; j < 4; j++)
100
556
            memset(dst + j * linesize, v[0], 4);
101
139
        break;
102
11062
    case 2:
103
11062
        v[0] = get_bits(gb, 8);
104
11062
        v[1] = get_bits(gb, 8);
105
33186
        for (j = 2; j >= 0; j -= 2) {
106
110620
            for (i = 0; i < 4; i++)
107
88496
                dst[j * linesize + i] = v[get_bits1(gb)];
108
110620
            for (i = 0; i < 4; i++)
109
88496
                dst[(j + 1) * linesize + i] = v[get_bits1(gb)];
110
        }
111
11062
        break;
112
3514
    case 3:
113
10542
        for (j = 0; j < 4; j += 2)
114
21084
            for (i = 0; i < 4; i += 2)
115
14056
                decode2x2(gb, dst + j * linesize + i, linesize);
116
    }
117
17028
}
118
119
/**
120
 * Decode 8x8 block
121
 */
122
6000
static inline void decode8x8(GetBitContext *gb, uint8_t *dst, int linesize,
123
                             BlockDSPContext *bdsp)
124
{
125
    int i, j, v[2];
126
127

6000
    switch (get_bits(gb, 2)) {
128
29
    case 1:
129
29
        v[0] = get_bits(gb, 8);
130
29
        bdsp->fill_block_tab[1](dst, v[0], linesize, 8);
131
29
        break;
132
218
    case 2:
133
218
        v[0] = get_bits(gb, 8);
134
218
        v[1] = get_bits(gb, 8);
135
1962
        for (j = 7; j >= 0; j--)
136
15696
            for (i = 0; i < 8; i++)
137
13952
                dst[j * linesize + i] = v[get_bits1(gb)];
138
218
        break;
139
4257
    case 3:
140
12771
        for (j = 0; j < 8; j += 4)
141
25542
            for (i = 0; i < 8; i += 4)
142
17028
                decode4x4(gb, dst + j * linesize + i, linesize);
143
    }
144
6000
}
145
146
9
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
147
                        AVPacket *avpkt)
148
{
149
9
    JvContext *s = avctx->priv_data;
150
9
    const uint8_t *buf = avpkt->data;
151
9
    const uint8_t *buf_end = buf + avpkt->size;
152
    int video_size, video_type, i, j, ret;
153
154
9
    if (avpkt->size < 6)
155
        return AVERROR_INVALIDDATA;
156
157
9
    video_size = AV_RL32(buf);
158
9
    video_type = buf[4];
159
9
    buf += 5;
160
161
9
    if (video_size) {
162

8
        if (video_size < 0 || video_size > avpkt->size - 5) {
163
1
            av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
164
1
            return AVERROR_INVALIDDATA;
165
        }
166
167

7
        if (video_type == 0 || video_type == 1) {
168
            GetBitContext gb;
169
6
            init_get_bits(&gb, buf, 8 * video_size);
170
171
6
            if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
172
                return ret;
173
174
6
            if (avctx->height/8 * (avctx->width/8) > 4 * video_size) {
175
                av_log(avctx, AV_LOG_ERROR, "Insufficient input data for dimensions\n");
176
                return AVERROR_INVALIDDATA;
177
            }
178
179
156
            for (j = 0; j < avctx->height; j += 8)
180
6150
                for (i = 0; i < avctx->width; i += 8)
181
6000
                    decode8x8(&gb,
182
6000
                              s->frame->data[0] + j * s->frame->linesize[0] + i,
183
6000
                              s->frame->linesize[0], &s->bdsp);
184
185
6
            buf += video_size;
186
1
        } else if (video_type == 2) {
187
1
            int v = *buf++;
188
189
1
            av_frame_unref(s->frame);
190
1
            if ((ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF)) < 0)
191
                return ret;
192
193
201
            for (j = 0; j < avctx->height; j++)
194
200
                memset(s->frame->data[0] + j * s->frame->linesize[0],
195
200
                       v, avctx->width);
196
        } else {
197
            av_log(avctx, AV_LOG_WARNING,
198
                   "unsupported frame type %i\n", video_type);
199
            return AVERROR_INVALIDDATA;
200
        }
201
    }
202
203
8
    if (buf_end - buf >= AVPALETTE_COUNT * 3) {
204
257
        for (i = 0; i < AVPALETTE_COUNT; i++) {
205
256
            uint32_t pal = AV_RB24(buf);
206
256
            s->palette[i] = 0xFFU << 24 | pal << 2 | ((pal >> 4) & 0x30303);
207
256
            buf += 3;
208
        }
209
1
        s->palette_has_changed = 1;
210
    }
211
212
8
    if (video_size) {
213
7
        s->frame->key_frame           = 1;
214
7
        s->frame->pict_type           = AV_PICTURE_TYPE_I;
215
7
        s->frame->palette_has_changed = s->palette_has_changed;
216
7
        s->palette_has_changed        = 0;
217
7
        memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
218
219
7
        if ((ret = av_frame_ref(data, s->frame)) < 0)
220
            return ret;
221
7
        *got_frame = 1;
222
    }
223
224
8
    return avpkt->size;
225
}
226
227
3
static av_cold int decode_close(AVCodecContext *avctx)
228
{
229
3
    JvContext *s = avctx->priv_data;
230
231
3
    av_frame_free(&s->frame);
232
233
3
    return 0;
234
}
235
236
AVCodec ff_jv_decoder = {
237
    .name           = "jv",
238
    .long_name      = NULL_IF_CONFIG_SMALL("Bitmap Brothers JV video"),
239
    .type           = AVMEDIA_TYPE_VIDEO,
240
    .id             = AV_CODEC_ID_JV,
241
    .priv_data_size = sizeof(JvContext),
242
    .init           = decode_init,
243
    .close          = decode_close,
244
    .decode         = decode_frame,
245
    .capabilities   = AV_CODEC_CAP_DR1,
246
};