summaryrefslogtreecommitdiffstats
path: root/sub/img_convert.c
blob: 6eef4a01a37ff3d5a85a0b86b90ce905da5edd82 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * This file is part of mplayer.
 *
 * mplayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mplayer 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with mplayer.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <assert.h>

#include <libavutil/mem.h>
#include <libavutil/common.h>

#include "talloc.h"

#include "img_convert.h"
#include "sub.h"

struct osd_conv_cache {
    struct sub_bitmap part;
    // for osd_conv_cache_alloc_old_p() (SUBBITMAP_PLANAR)
    int allocated, stride;
    struct old_osd_planar bmp;
    // for osd_conv_cache_alloc_bmp() (various other formats)
    unsigned char *packed;
};

static int osd_conv_cache_destroy(void *p)
{
    struct osd_conv_cache *c = p;
    av_free(c->bmp.bitmap);
    av_free(c->bmp.alpha);
    return 0;
}

struct osd_conv_cache *osd_conv_cache_new(void)
{
    struct osd_conv_cache *c = talloc_zero(NULL, struct osd_conv_cache);
    talloc_set_destructor(c, &osd_conv_cache_destroy);
    return c;
}

// allocates/enlarges the alpha/bitmap buffer
static void osd_conv_cache_alloc_old_p(struct osd_conv_cache *c, int w, int h)
{
    assert(w > 0 && h > 0);
    c->stride = (w + 7) & (~7);
    int len = c->stride * h;
    if (c->allocated < len) {
        av_free(c->bmp.bitmap);
        av_free(c->bmp.alpha);
        c->allocated = len;
        c->bmp.bitmap = av_malloc(len);
        c->bmp.alpha  = av_malloc(len);
    }
    memset(c->bmp.bitmap, sub_bg_color, len);
    memset(c->bmp.alpha, sub_bg_alpha, len);
    c->part = (struct sub_bitmap) {
        .bitmap = &c->bmp,
        .stride = c->stride,
        .w = w, .h = h,
        .dw = w, .dh = h,
    };
}

static void osd_conv_cache_alloc_bmp(struct osd_conv_cache *c, int w, int h,
                                     int bpp)
{
    size_t size = talloc_get_size(c->packed);
    size_t new_size = w * bpp * h;
    if (new_size > size)
        c->packed = talloc_realloc(c, c->packed, unsigned char, new_size);
    c->part = (struct sub_bitmap) {
        .bitmap = c->packed,
        .stride = w * bpp,
        .w = w, .h = h,
        .dw = w, .dh = h,
    };
}

static void draw_alpha_ass_to_old(unsigned char *src, int src_w, int src_h,
                                  int src_stride, unsigned char *dst_a,
                                  unsigned char *dst_i, size_t dst_stride,
                                  int dst_x, int dst_y, uint32_t color)
{
    const unsigned int r = (color >> 24) & 0xff;
    const unsigned int g = (color >> 16) & 0xff;
    const unsigned int b = (color >>  8) & 0xff;
    const unsigned int a = 0xff - (color & 0xff);

    int gray = (r + g + b) / 3; // not correct

    dst_a += dst_y * dst_stride + dst_x;
    dst_i += dst_y * dst_stride + dst_x;

    int src_skip = src_stride - src_w;
    int dst_skip = dst_stride - src_w;

    for (int y = 0; y < src_h; y++) {
        for (int x = 0; x < src_w; x++) {
            unsigned char as = (*src * a) >> 8;
            unsigned char bs = (gray * as) >> 8;
            // to mplayer scale
            as = -as;

            unsigned char *a = dst_a;
            unsigned char *b = dst_i;

            // NOTE: many special cases, because alpha=0 means transparency,
            //       while alpha=1..255 is opaque..transparent
            if (as) {
                *b = ((*b * as) >> 8) + bs;
                if (*a) {
                    *a = (*a * as) >> 8;
                    if (*a < 1)
                        *a = 1;
                } else {
                    *a = as;
                }
            }

            dst_a++;
            dst_i++;
            src++;
        }
        dst_a += dst_skip;
        dst_i += dst_skip;
        src += src_skip;
    }
}

static void render_ass_to_old(unsigned char *a, unsigned char *i,
                              size_t stride, int x, int y,
                              struct sub_bitmaps *imgs)
{
    for (int n = 0; n < imgs->num_parts; n++) {
        struct sub_bitmap *p = &imgs->parts[n];
        draw_alpha_ass_to_old(p->bitmap, p->w, p->h, p->stride, a, i, stride,
                              x + p->x, y + p->y, p->libass.color);
    }
}

// SUBBITMAP_LIBASS -> SUBBITMAP_OLD_PLANAR
bool osd_conv_ass_to_old_p(struct osd_conv_cache *c, struct sub_bitmaps *imgs)
{
    struct sub_bitmaps src = *imgs;
    if (src.format != SUBBITMAP_LIBASS || src.scaled)
        return false;

    imgs->format = SUBBITMAP_OLD_PLANAR;
    imgs->num_parts = 0;
    imgs->parts = NULL;

    int x1, y1, x2, y2;
    if (!sub_bitmaps_bb(&src, &x1, &y1, &x2, &y2))
        return true;

    osd_conv_cache_alloc_old_p(c, x2 - x1, y2 - y1);

    render_ass_to_old(c->bmp.alpha, c->bmp.bitmap, c->stride, -x1, -y1, &src);

    c->part.x = x1;
    c->part.y = y1;

    imgs->parts = &c->part;
    imgs->num_parts = 1;
    return true;
}

// SUBBITMAP_OLD_PLANAR -> SUBBITMAP_RGBA
bool osd_conv_old_p_to_rgba(struct osd_conv_cache *c, struct sub_bitmaps *imgs)
{
    struct sub_bitmaps src = *imgs;
    if (src.format != SUBBITMAP_OLD_PLANAR || src.num_parts > 1)
        return false;

    imgs->format = SUBBITMAP_RGBA;
    imgs->num_parts = 0;
    imgs->parts = NULL;

    if (src.num_parts == 0)
        return true;

    struct sub_bitmap *s = &src.parts[0];
    struct old_osd_planar *p = s->bitmap;

    osd_conv_cache_alloc_bmp(c, s->w, s->h, 4);

    for (int y = 0; y < s->h; y++) {
        unsigned char *y_src = p->bitmap + s->stride * y;
        unsigned char *y_srca = p->alpha + s->stride * y;
        unsigned char *cur = c->packed + y * s->w * 4;
        for (int x = 0; x < s->w; x++) {
            // This is incorrect, as input is premultiplied alpha, but output
            // has to be non-premultiplied. However, this code is for
            // compatibility with spudec.c only, and DVD subtitles have
            // binary transparency only - the rendered result will be the same.
            cur[x*4+0] = cur[x*4+1] = cur[x*4+2] = y_src[x];
            cur[x*4+3] = -y_srca[x];
        }
    }

    c->part.x = s->x;
    c->part.y = s->y;

    imgs->parts = &c->part;
    imgs->num_parts = 1;
    return true;
}

// SUBBITMAP_OLD_PLANAR -> SUBBITMAP_OLD
bool osd_conv_old_p_to_old(struct osd_conv_cache *c, struct sub_bitmaps *imgs)
{
    struct sub_bitmaps src = *imgs;
    if (src.format != SUBBITMAP_OLD_PLANAR || src.num_parts > 1)
        return false;

    imgs->format = SUBBITMAP_OLD;
    imgs->num_parts = 0;
    imgs->parts = NULL;

    if (src.num_parts == 0)
        return true;

    struct sub_bitmap *s = &src.parts[0];
    struct old_osd_planar *p = s->bitmap;

    osd_conv_cache_alloc_bmp(c, s->w, s->h, 2);

    for (int y = 0; y < s->h; y++) {
        unsigned char *y_src = p->bitmap + s->stride * y;
        unsigned char *y_srca = p->alpha + s->stride * y;
        unsigned char *cur = c->packed + y * s->w * 2;
        for (int x = 0; x < s->w; x++) {
            cur[x*2+0] = y_src[x];
            cur[x*2+1] = -y_srca[x];
        }
    }

    c->part.x = s->x;
    c->part.y = s->y;

    imgs->parts = &c->part;
    imgs->num_parts = 1;
    return true;
}