aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/hdrenc.c
blob: 0da89c092ddfd065091dbc715d1e4bf0acd924b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Radiance HDR image format
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "bytestream.h"
#include "codec_internal.h"
#include "encode.h"

typedef struct HDREncContext {
    uint8_t *scanline;
} HDREncContext;

static av_cold int hdr_encode_init(AVCodecContext *avctx)
{
    HDREncContext *s = avctx->priv_data;

    s->scanline = av_calloc(avctx->width * 4, sizeof(*s->scanline));
    if (!s->scanline)
        return AVERROR(ENOMEM);

    return 0;
}

static av_cold int hdr_encode_close(AVCodecContext *avctx)
{
    HDREncContext *s = avctx->priv_data;

    av_freep(&s->scanline);

    return 0;
}

static void bytestream_put_str(uint8_t **buf, const char *const line)
{
    bytestream_put_buffer(buf, line, strlen(line));
}

static void float2rgbe(uint8_t *rgbe, float red, float green, float blue)
{
    float v;
    int e;

    v = FFMAX3(red, green, blue);

    if (v < 1e-32f) {
        rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
    } else {
        v = frexpf(v, &e) * 256.f / v;

        rgbe[0] = av_clip_uint8(red * v);
        rgbe[1] = av_clip_uint8(green * v);
        rgbe[2] = av_clip_uint8(blue * v);
        rgbe[3] = av_clip_uint8(e + 128);
    }
}

static void rle(uint8_t **buffer, const uint8_t *data, int width)
{
#define MIN_RLE 4
    int cur = 0;

    while (cur < width) {
        int run_count = 0, old_run_count = 0;
        int beg_run = cur;
        uint8_t buf[2];

        while (run_count < MIN_RLE && beg_run < width) {
            beg_run += run_count;
            old_run_count = run_count;
            run_count = 1;
            while ((beg_run + run_count < width) && (run_count < 127) &&
                   (data[beg_run * 4] == data[(beg_run + run_count) * 4]))
                run_count++;
        }

        if ((old_run_count > 1) && (old_run_count == beg_run - cur)) {
            buf[0] = 128 + old_run_count;
            buf[1] = data[cur * 4];
            bytestream_put_buffer(buffer, buf, sizeof(buf));
            cur = beg_run;
        }

        while (cur < beg_run) {
            int nonrun_count = FFMIN(128, beg_run - cur);
            buf[0] = nonrun_count;
            bytestream_put_byte(buffer, buf[0]);
            for (int n = 0; n < nonrun_count; n++)
                bytestream_put_byte(buffer, data[(cur + n) * 4]);
            cur += nonrun_count;
        }

        if (run_count >= MIN_RLE) {
            buf[0] = 128 + run_count;
            buf[1] = data[beg_run * 4];
            bytestream_put_buffer(buffer, buf, sizeof(buf));
            cur += run_count;
        }
    }
}

static int hdr_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                            const AVFrame *frame, int *got_packet)
{
    HDREncContext *s = avctx->priv_data;
    int64_t packet_size;
    uint8_t *buf;
    int ret;

    packet_size = avctx->width * avctx->height * 4LL + 1024LL;
    if ((ret = ff_get_encode_buffer(avctx, pkt, packet_size, 0)) < 0)
        return ret;

    buf = pkt->data;
    bytestream_put_str(&buf, "#?RADIANCE\n");
    bytestream_put_str(&buf, "SOFTWARE=lavc\n");
    ret = snprintf(buf, 32, "PIXASPECT=%f\n", av_q2d(av_inv_q(avctx->sample_aspect_ratio)));
    if (ret > 0)
        buf += ret;
    bytestream_put_str(&buf, "FORMAT=32-bit_rle_rgbe\n\n");
    ret = snprintf(buf, 32, "-Y %d +X %d\n", avctx->height, avctx->width);
    if (ret > 0)
        buf += ret;

    for (int y = 0; y < avctx->height; y++) {
        const float *red   = (const float *)(frame->data[2] + y * frame->linesize[2]);
        const float *green = (const float *)(frame->data[0] + y * frame->linesize[0]);
        const float *blue  = (const float *)(frame->data[1] + y * frame->linesize[1]);

        if (avctx->width < 8 || avctx->width > 0x7fff) {
            for (int x = 0; x < avctx->width; x++) {
                float2rgbe(buf, red[x], green[x], blue[x]);
                buf += 4;
            }
        } else {
            bytestream_put_byte(&buf, 2);
            bytestream_put_byte(&buf, 2);
            bytestream_put_byte(&buf, avctx->width >> 8);
            bytestream_put_byte(&buf, avctx->width & 0xFF);

            for (int x = 0; x < avctx->width; x++)
                float2rgbe(s->scanline + 4 * x, red[x], green[x], blue[x]);
            for (int p = 0; p < 4; p++)
                rle(&buf, s->scanline + p, avctx->width);
        }
    }

    pkt->flags |= AV_PKT_FLAG_KEY;

    av_shrink_packet(pkt, buf - pkt->data);

    *got_packet = 1;

    return 0;
}

const FFCodec ff_hdr_encoder = {
    .p.name         = "hdr",
    .p.long_name    = NULL_IF_CONFIG_SMALL("HDR (Radiance RGBE format) image"),
    .priv_data_size = sizeof(HDREncContext),
    .p.type         = AVMEDIA_TYPE_VIDEO,
    .p.id           = AV_CODEC_ID_RADIANCE_HDR,
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
    .init           = hdr_encode_init,
    FF_CODEC_ENCODE_CB(hdr_encode_frame),
    .close          = hdr_encode_close,
    .p.pix_fmts     = (const enum AVPixelFormat[]){
        AV_PIX_FMT_GBRPF32,
        AV_PIX_FMT_NONE
    },
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
};