GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/sunrastenc.c Lines: 75 109 68.8 %
Date: 2020-09-25 23:16:12 Branches: 30 58 51.7 %

Line Branch Exec Source
1
/*
2
 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
3
 * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
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
#include "libavutil/opt.h"
23
24
#include "avcodec.h"
25
#include "bytestream.h"
26
#include "internal.h"
27
#include "sunrast.h"
28
29
typedef struct SUNRASTContext {
30
    AVClass *class;
31
32
    PutByteContext p;
33
    int depth;      ///< depth of pixel
34
    int length;     ///< length (bytes) of image
35
    int type;       ///< type of file
36
    int maptype;    ///< type of colormap
37
    int maplength;  ///< length (bytes) of colormap
38
    int size;
39
} SUNRASTContext;
40
41
13
static void sunrast_image_write_header(AVCodecContext *avctx)
42
{
43
13
    SUNRASTContext *s = avctx->priv_data;
44
45
13
    bytestream2_put_be32u(&s->p, RAS_MAGIC);
46
13
    bytestream2_put_be32u(&s->p, avctx->width);
47
13
    bytestream2_put_be32u(&s->p, avctx->height);
48
13
    bytestream2_put_be32u(&s->p, s->depth);
49
13
    bytestream2_put_be32u(&s->p, s->length);
50
13
    bytestream2_put_be32u(&s->p, s->type);
51
13
    bytestream2_put_be32u(&s->p, s->maptype);
52
13
    bytestream2_put_be32u(&s->p, s->maplength);
53
13
}
54
55
13
static void sunrast_image_write_image(AVCodecContext *avctx,
56
                                      const uint8_t *pixels,
57
                                      const uint32_t *palette_data,
58
                                      int linesize)
59
{
60
13
    SUNRASTContext *s = avctx->priv_data;
61
    const uint8_t *ptr;
62
    int len, alen, x, y;
63
64
13
    if (s->maplength) {     // palettized
65
        PutByteContext pb_r, pb_g;
66
        int len = s->maplength / 3;
67
68
        pb_r = s->p;
69
        bytestream2_skip_p(&s->p, len);
70
        pb_g = s->p;
71
        bytestream2_skip_p(&s->p, len);
72
73
        for (x = 0; x < len; x++) {
74
            uint32_t pixel = palette_data[x];
75
76
            bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
77
            bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF);
78
            bytestream2_put_byteu(&s->p,  pixel        & 0xFF);
79
        }
80
    }
81
82
13
    len  = (s->depth * avctx->width + 7) >> 3;
83
13
    alen = len + (len & 1);
84
13
    ptr  = pixels;
85
86
13
     if (s->type == RT_BYTE_ENCODED) {
87
        uint8_t value, value2;
88
        int run;
89
90
13
        ptr = pixels;
91
92
#define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
93
94
13
        x = 0, y = 0;
95

13
        value2 = GET_VALUE;
96
3917847
        while (y < avctx->height) {
97
3917834
            run = 1;
98
3917834
            value = value2;
99
3917834
            x++;
100
3917834
            if (x >= alen) {
101
3718
                x = 0;
102
3718
                ptr += linesize, y++;
103
            }
104
105

3917834
            value2 = GET_VALUE;
106

3953664
            while (value2 == value && run < 256 && y < avctx->height) {
107
35830
                x++;
108
35830
                run++;
109
35830
                if (x >= alen) {
110
26
                    x = 0;
111
26
                    ptr += linesize, y++;
112
                }
113

35830
                value2 = GET_VALUE;
114
            }
115
116

3917834
            if (run > 2 || value == RLE_TRIGGER) {
117
1396
                bytestream2_put_byteu(&s->p, RLE_TRIGGER);
118
1396
                bytestream2_put_byteu(&s->p, run - 1);
119
1396
                if (run > 1)
120
1396
                    bytestream2_put_byteu(&s->p, value);
121
3916438
            } else if (run == 1) {
122
3883890
                bytestream2_put_byteu(&s->p, value);
123
            } else
124
32548
                bytestream2_put_be16u(&s->p, (value << 8) | value);
125
        }
126
127
        // update data length for header
128
13
        s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
129
    } else {
130
        for (y = 0; y < avctx->height; y++) {
131
            bytestream2_put_buffer(&s->p, ptr, len);
132
            if (len < alen)
133
                bytestream2_put_byteu(&s->p, 0);
134
            ptr += linesize;
135
        }
136
    }
137
13
}
138
139
1
static av_cold int sunrast_encode_init(AVCodecContext *avctx)
140
{
141
1
    SUNRASTContext *s = avctx->priv_data;
142
143
#if FF_API_CODER_TYPE
144
FF_DISABLE_DEPRECATION_WARNINGS
145
1
    switch (avctx->coder_type) {
146
1
    case FF_CODER_TYPE_RLE:
147
1
        s->type = RT_BYTE_ENCODED;
148
1
        break;
149
    case FF_CODER_TYPE_RAW:
150
        s->type = RT_STANDARD;
151
        break;
152
    default:
153
        av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
154
        return AVERROR(EINVAL);
155
    }
156
FF_ENABLE_DEPRECATION_WARNINGS
157

1
    if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
158
#endif
159
    // adjust boolean option to RT equivalent
160
    s->type++;
161
162
1
    s->maptype                    = RMT_NONE;
163
1
    s->maplength                  = 0;
164
165

1
    switch (avctx->pix_fmt) {
166
    case AV_PIX_FMT_MONOWHITE:
167
        s->depth = 1;
168
        break;
169
    case AV_PIX_FMT_PAL8 :
170
        s->maptype   = RMT_EQUAL_RGB;
171
        s->maplength = 3 * 256;
172
        /* fall-through */
173
    case AV_PIX_FMT_GRAY8:
174
        s->depth = 8;
175
        break;
176
1
    case AV_PIX_FMT_BGR24:
177
1
        s->depth = 24;
178
1
        break;
179
    default:
180
        return AVERROR_BUG;
181
    }
182
1
    s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
183
1
    s->size   = 32 + s->maplength + s->length * s->type;
184
185
1
    return 0;
186
}
187
188
13
static int sunrast_encode_frame(AVCodecContext *avctx,  AVPacket *avpkt,
189
                                const AVFrame *frame, int *got_packet_ptr)
190
{
191
13
    SUNRASTContext *s = avctx->priv_data;
192
    int ret;
193
194
13
    if ((ret = ff_alloc_packet2(avctx, avpkt, s->size, 0)) < 0)
195
        return ret;
196
197
13
    bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
198
13
    sunrast_image_write_header(avctx);
199
13
    sunrast_image_write_image(avctx, frame->data[0],
200
13
                              (const uint32_t *)frame->data[1],
201
                              frame->linesize[0]);
202
    // update data length in header after RLE
203
13
    if (s->type == RT_BYTE_ENCODED)
204
13
        AV_WB32(&avpkt->data[16], s->length);
205
206
13
    *got_packet_ptr = 1;
207
13
    avpkt->flags |= AV_PKT_FLAG_KEY;
208
13
    avpkt->size = bytestream2_tell_p(&s->p);
209
13
    return 0;
210
}
211
212
#define OFFSET(x) offsetof(SUNRASTContext, x)
213
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
214
static const AVOption options[] = {
215
    { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
216
217
    { NULL },
218
};
219
220
static const AVClass sunrast_class = {
221
    .class_name = "sunrast",
222
    .item_name  = av_default_item_name,
223
    .option     = options,
224
    .version    = LIBAVUTIL_VERSION_INT,
225
};
226
227
#if FF_API_CODER_TYPE
228
static const AVCodecDefault sunrast_defaults[] = {
229
     { "coder", "rle" },
230
     { NULL },
231
};
232
#endif
233
234
AVCodec ff_sunrast_encoder = {
235
    .name           = "sunrast",
236
    .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
237
    .type           = AVMEDIA_TYPE_VIDEO,
238
    .id             = AV_CODEC_ID_SUNRAST,
239
    .priv_data_size = sizeof(SUNRASTContext),
240
    .init           = sunrast_encode_init,
241
    .encode2        = sunrast_encode_frame,
242
#if FF_API_CODER_TYPE
243
    .defaults       = sunrast_defaults,
244
#endif
245
    .priv_class     = &sunrast_class,
246
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
247
                                                  AV_PIX_FMT_PAL8,
248
                                                  AV_PIX_FMT_GRAY8,
249
                                                  AV_PIX_FMT_MONOWHITE,
250
                                                  AV_PIX_FMT_NONE },
251
};