File: subtitle_renderer.cpp

package info (click to toggle)
bino 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,636 kB
  • sloc: cpp: 21,288; sh: 4,849; makefile: 314; sed: 16
file content (585 lines) | stat: -rw-r--r-- 19,586 bytes parent folder | download | duplicates (2)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
 * This file is part of bino, a 3D video player.
 *
 * Copyright (C) 2011, 2012, 2015
 * Martin Lambers <marlam@marlam.de>
 * Joe <cuchac@email.cz>
 * Frédéric Devernay <Frederic.Devernay@inrialpes.fr>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <vector>
#include <limits>
#include <cstring>
#include <cstdio>
#include <stdint.h>

#include <GL/glew.h>

#if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
# include <windows.h>
#endif
#if defined __APPLE__
# include <unistd.h>
#endif

extern "C"
{
#include <ass/ass.h>
}

#include "base/dbg.h"
#include "base/exc.h"
#include "base/str.h"
#include "base/blb.h"
#include "base/msg.h"
#include "base/pth.h"

#include "base/gettext.h"
#define _(string) gettext(string)

#include "subtitle_renderer.h"


subtitle_renderer_initializer::subtitle_renderer_initializer(subtitle_renderer &renderer) :
    _subtitle_renderer(renderer)
{
}

void subtitle_renderer_initializer::run()
{
    _subtitle_renderer.init();
}


/* Rendering subtitles with LibASS is not thread-safe.
 *
 * We have multiple concurrent subtitle rendering threads if we are running from Equalizer
 * and the Equalizer configuration contains multiple channels per node (since each channel
 * has a video output, which in turn has a subtitle renderer).
 *
 * Everything works fine as long as every channel is connected to the same X11 display.
 * But if channels are connected to different X11 displays, the application crashes.
 * The culprit here seems to be the freetype library, as this library causes the same
 * trouble in other applications that are not related to LibASS.
 *
 * Anyway, to fix this we use one big global lock around LibASS calls.
 */
static mutex global_libass_mutex;

subtitle_renderer::subtitle_renderer() :
    _initializer(*this),
    _initialized(false),
    _fontconfig_conffile(NULL),
    _ass_library(NULL),
    _ass_renderer(NULL),
    _ass_track(NULL)
{
    _initializer.start();
}

subtitle_renderer::~subtitle_renderer()
{
    if (_initializer.running())
    {
        try
        {
            _initializer.finish();
        }
        catch (...)
        {
        }
    }
    if (_ass_track)
    {
        ass_free_track(_ass_track);
    }
    if (_ass_renderer)
    {
        ass_renderer_done(_ass_renderer);
    }
    if (_ass_library)
    {
        ass_library_done(_ass_library);
    }
    if (_fontconfig_conffile)
    {
        (void)std::remove(_fontconfig_conffile);
    }
}

static void libass_msg_callback(int level, const char *fmt, va_list args, void *)
{
    msg::level_t msg_levels[8] = { msg::ERR, msg::ERR, msg::WRN, msg::WRN, msg::WRN, msg::INF, msg::DBG, msg::DBG };
    msg::level_t l = (level < 0 || level > 7 ? msg::ERR : msg_levels[level]);
    std::string s = str::vasprintf(fmt, args);
    msg::msg(l, std::string("LibASS: ") + s);
}

const char *subtitle_renderer::get_fontconfig_conffile()
{
#if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __APPLE__
    /* Fontconfig annoyingly requires a configuration file, but there is no
     * default location for it on Windows or Mac OS X. So we just create a temporary one.
     * Note that if something goes wrong and we return NULL here, the application will
     * likely crash when trying to render a subtitle. */
    FILE *f;
# if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
    /* Note that due to lack of mkstemp() on Windows, this code has race conditions. */
    char tmppathname[MAX_PATH];
    static char tmpfilename[MAX_PATH];
    DWORD ret;
    ret = GetTempPath(MAX_PATH, tmppathname);
    if (ret > MAX_PATH || ret == 0)
        return NULL;
    if (GetTempFileName(tmppathname, "fonts_conf", 0, tmpfilename) == 0)
        return NULL;
    if (!(f = fopen(tmpfilename, "w")))
        return NULL;
# else /* __APPLE__ */
    static char tmpfilename[] = "/tmp/fontsXXXXXX.conf";
    int d = mkstemps(tmpfilename, 5);
    if (d == -1)
        return NULL;
    if (!(f = fdopen(d, "w")))
        return NULL;
# endif
    fprintf(f,
            "<?xml version=\"1.0\"?>\n"
            "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
            "<fontconfig>\n"
# if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
            "<dir>WINDOWSFONTDIR</dir>\n"
            "<dir>~/.fonts</dir>\n"
            "<cachedir>WINDOWSTEMPDIR_FONTCONFIG_CACHE</cachedir>\n"
            "<cachedir>~/.fontconfig</cachedir>\n"
# else /* __APPLE__ */
            "<dir>/usr/share/fonts</dir>\n"
            "<dir>/usr/X11/lib/X11/fonts</dir>\n"
            "<dir>/usr/X11/share/fonts</dir>\n"
            "<dir>/opt/X11/share/fonts</dir>\n"
            "<dir>/Library/Fonts</dir>\n"
            "<dir>/Network/Library/Fonts</dir>\n"
            "<dir>/System/Library/Fonts</dir>\n"
            "<dir>~/Library/Application Support/Bino/fonts</dir>\n"
            "<cachedir>/var/cache/fontconfig</cachedir>\n"
            "<cachedir>/usr/X11/var/cache/fontconfig</cachedir>\n"
            "<cachedir>/opt/X11/var/cache/fontconfig</cachedir>\n"
            "<cachedir>~/Library/Application Support/Bino/cache/fonts</cachedir>\n"
            "<cachedir>~/.fontconfig</cachedir>\n"
# endif
            "<config>\n"
            "<blank>\n"
            "<int>0x0020</int> <int>0x00A0</int> <int>0x00AD</int> <int>0x034F</int> <int>0x0600</int>\n"
            "<int>0x0601</int> <int>0x0602</int> <int>0x0603</int> <int>0x06DD</int> <int>0x070F</int>\n"
            "<int>0x115F</int> <int>0x1160</int> <int>0x1680</int> <int>0x17B4</int> <int>0x17B5</int>\n"
            "<int>0x180E</int> <int>0x2000</int> <int>0x2001</int> <int>0x2002</int> <int>0x2003</int>\n"
            "<int>0x2004</int> <int>0x2005</int> <int>0x2006</int> <int>0x2007</int> <int>0x2008</int>\n"
            "<int>0x2009</int> <int>0x200A</int> <int>0x200B</int> <int>0x200C</int> <int>0x200D</int>\n"
            "<int>0x200E</int> <int>0x200F</int> <int>0x2028</int> <int>0x2029</int> <int>0x202A</int>\n"
            "<int>0x202B</int> <int>0x202C</int> <int>0x202D</int> <int>0x202E</int> <int>0x202F</int>\n"
            "<int>0x205F</int> <int>0x2060</int> <int>0x2061</int> <int>0x2062</int> <int>0x2063</int>\n"
            "<int>0x206A</int> <int>0x206B</int> <int>0x206C</int> <int>0x206D</int> <int>0x206E</int>\n"
            "<int>0x206F</int> <int>0x2800</int> <int>0x3000</int> <int>0x3164</int> <int>0xFEFF</int>\n"
            "<int>0xFFA0</int> <int>0xFFF9</int> <int>0xFFFA</int> <int>0xFFFB</int>\n"
            "</blank>\n"
            "<rescan><int>30</int></rescan>\n"
            "</config>\n"
            "</fontconfig>\n");
    fclose(f);
    return tmpfilename;
#else
    /* Systems other than Windows and Mac OS are expected to have a default
     * fontconfig configuration file so that we can simply return NULL. */
    return NULL;
#endif
}

void subtitle_renderer::init()
{
    if (!_initialized)
    {
        try
        {
            global_libass_mutex.lock();

            ASS_Library *ass_library = ass_library_init();
            if (!ass_library)
            {
                throw exc(_("Cannot initialize LibASS."));
            }
            ass_set_message_cb(ass_library, libass_msg_callback, NULL);
            ass_set_extract_fonts(ass_library, 1);
            _ass_library = ass_library;

            ASS_Renderer *ass_renderer = ass_renderer_init(_ass_library);
            if (!ass_renderer)
            {
                throw exc(_("Cannot initialize LibASS renderer."));
            }
            ass_set_hinting(ass_renderer, ASS_HINTING_NATIVE);
            _fontconfig_conffile = get_fontconfig_conffile();
            ass_set_fonts(ass_renderer, NULL, "sans-serif", 1, _fontconfig_conffile, 1);
            _ass_renderer = ass_renderer;

            _initialized = true;
            global_libass_mutex.unlock();
        }
        catch (...)
        {
            // Either we threw this exception ourselves, or the thread was cancelled.
            // Note that if the thread was cancelled, we likely leak ressources.
            // But at least we avoid to deconstruct potentially inconsistent ASS_Library
            // and ASS_Renderer objects.
            global_libass_mutex.unlock();
            throw;
        }
    }
}

bool subtitle_renderer::is_initialized()
{
    if (_initializer.running())
    {
        return false;
    }
    else
    {
        // rethrow a possible exception if something went wrong
        _initializer.finish();
        return _initialized;
    }
}

bool subtitle_renderer::render_to_display_size(const subtitle_box &box) const
{
    return (box.format != subtitle_box::image);
}

bool subtitle_renderer::prerender(const subtitle_box &box, int64_t timestamp,
        const parameters &params,
        int width, int height, float pixel_aspect_ratio,
        int &bb_x, int &bb_y, int &bb_w, int &bb_h)
{
    assert(_initialized);
    bool r = false;
    _fmt = box.format;
    switch (_fmt)
    {
    case subtitle_box::text:
    case subtitle_box::ass:
        r = prerender_ass(box, timestamp, params, width, height, pixel_aspect_ratio);
        break;
    case subtitle_box::image:
        r = prerender_img(box);
        break;
    }
    bb_x = _bb_x;
    bb_y = _bb_y;
    bb_w = _bb_w;
    bb_h = _bb_h;
    return r;
}

void subtitle_renderer::render(uint32_t *bgra32_buffer)
{
    switch (_fmt)
    {
    case subtitle_box::text:
    case subtitle_box::ass:
        render_ass(bgra32_buffer);
        break;
    case subtitle_box::image:
        render_img(bgra32_buffer);
        break;
    }
}

void subtitle_renderer::blend_ass_image(const ASS_Image *img, uint32_t *buf)
{
    const unsigned int R = (img->color >> 24u) & 0xffu;
    const unsigned int G = (img->color >> 16u) & 0xffu;
    const unsigned int B = (img->color >>  8u) & 0xffu;
    const unsigned int A = 255u - (img->color & 0xffu);

    unsigned char *src = img->bitmap;
    for (int src_y = 0; src_y < img->h; src_y++)
    {
        int dst_y = src_y + img->dst_y - _bb_y;
        if (dst_y >= _bb_h)
        {
            break;
        }
        for (int src_x = 0; src_x < img->w; src_x++)
        {
            unsigned int a = src[src_x] * A / 255u;
            int dst_x = src_x + img->dst_x - _bb_x;
            if (dst_x >= _bb_w)
            {
                break;
            }
            uint32_t oldval = buf[dst_y * _bb_w + dst_x];
            // XXX: The BGRA layout used here may be wrong on big endian system
            uint32_t newval = std::min(a + (oldval >> 24u), 255u) << 24u
                | ((a * R + (255u - a) * ((oldval >> 16u) & 0xffu)) / 255u) << 16u
                | ((a * G + (255u - a) * ((oldval >>  8u) & 0xffu)) / 255u) << 8u
                | ((a * B + (255u - a) * ((oldval       ) & 0xffu)) / 255u);
            buf[dst_y * _bb_w + dst_x] = newval;
        }
        src += img->stride;
    }
}

void subtitle_renderer::set_ass_parameters(const parameters &params)
{
    std::vector<std::string> overrides;
    if (!params.subtitle_font_is_default())
    {
        overrides.push_back(std::string("Default.Fontname=") + params.subtitle_font());
    }
    if (!params.subtitle_size_is_default())
    {
        overrides.push_back(std::string("Default.Fontsize=") + str::from(params.subtitle_size()));
    }
    if (!params.subtitle_color_is_default())
    {
        unsigned int color = params.subtitle_color();
        unsigned int a = 255u - ((color >> 24u) & 0xffu);
        unsigned int r = (color >> 16u) & 0xffu;
        unsigned int g = (color >> 8u) & 0xffu;
        unsigned int b = color & 0xffu;
        std::string color_str = str::asprintf("&H%02x%02x%02x%02x", a, b, g, r);
        overrides.push_back(std::string("Default.PrimaryColour=") + color_str);
        overrides.push_back(std::string("Default.SecondaryColour=") + color_str);
    }
    if (!params.subtitle_shadow_is_default())
    {
        overrides.push_back(std::string("Default.Shadow=")
                + (params.subtitle_shadow() == 0 ? "0" : "3"));
    }
    const char *ass_overrides[overrides.size() + 1];
    for (size_t i = 0; i < overrides.size(); i++)
    {
        ass_overrides[i] = overrides[i].c_str();
    }
    ass_overrides[overrides.size()] = NULL;
    ass_set_style_overrides(_ass_library, const_cast<char **>(ass_overrides));
    ass_set_font_scale(_ass_renderer, (params.subtitle_scale() >= 0.0f ? params.subtitle_scale() : 1.0));
    ass_process_force_style(_ass_track);
}

bool subtitle_renderer::prerender_ass(const subtitle_box &box, int64_t timestamp,
        const parameters &params, int width, int height, float pixel_aspect_ratio)
{
    // Lock
    global_libass_mutex.lock();

    // Set basic parameters
    ass_set_frame_size(_ass_renderer, width, height);
    ass_set_aspect_ratio(_ass_renderer, 1.0, pixel_aspect_ratio);

    // Put subtitle data into ASS track
    if (_ass_track)
    {
        ass_free_track(_ass_track);
    }
    _ass_track = ass_new_track(_ass_library);
    if (!_ass_track)
    {
        global_libass_mutex.unlock();
        throw exc(_("Cannot initialize LibASS track."));
    }
    std::string conv_str = box.str;
    if (params.subtitle_encoding() != "")
    {
        try
        {
            conv_str = str::convert(box.str, params.subtitle_encoding(), "UTF-8");
        }
        catch (std::exception &e)
        {
            msg::err(_("Subtitle character set conversion failed: %s"), e.what());
            conv_str = std::string("Dialogue: 0,0:00:00.00,9:59:59.99,") + e.what();
        }
    }
    if (box.format == subtitle_box::ass)
    {
        ass_process_codec_private(_ass_track, const_cast<char *>(box.style.c_str()), box.style.length());
        ass_process_data(_ass_track, const_cast<char *>(conv_str.c_str()), conv_str.length());
    }
    else
    {
        // Set a default ASS style for text subtitles
        std::string style =
            "[Script Info]\n"
            "ScriptType: v4.00+\n"
            "\n"
            "[V4+ Styles]\n"
            "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, "
            "OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, "
            "Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n"
            "Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,1,1,0,2,10,10,10,0,0\n"
            "\n"
            "[Events]\n"
            "Format: Layer, Start, End, Text\n"
            "\n";
        ass_process_codec_private(_ass_track, const_cast<char *>(style.c_str()), style.length());
        // Convert text to ASS
        conv_str = str::replace(conv_str, "\r\n", "\\N");
        conv_str = str::replace(conv_str, "\n", "\\N");
        std::string str = "Dialogue: 0,0:00:00.00,9:59:59.99," + conv_str;
        ass_process_data(_ass_track, const_cast<char *>(str.c_str()), str.length());
    }
    set_ass_parameters(params);

    // Render subtitle
    int change_detected = 0;
    _ass_img = ass_render_frame(_ass_renderer, _ass_track, timestamp / 1000, &change_detected);

    // Unlock
    global_libass_mutex.unlock();

    // Determine bounding box
    int min_x = width;
    int max_x = -1;
    int min_y = height;
    int max_y = -1;
    ASS_Image *img = _ass_img;
    while (img && img->w > 0 && img->h > 0)
    {
        if (img->dst_x < min_x)
            min_x = img->dst_x;
        if (img->dst_x + img->w - 1 > max_x)
            max_x = img->dst_x + img->w - 1;
        if (img->dst_y < min_y)
            min_y = img->dst_y;
        if (img->dst_y + img->h - 1 > max_y)
            max_y = img->dst_y + img->h - 1;
        img = img->next;
    }
    if (max_x < 0)
    {
        _bb_x = 0;
        _bb_y = 0;
        _bb_w = 0;
        _bb_h = 0;
    }
    else
    {
        _bb_x = min_x;
        _bb_w = max_x - min_x + 1;
        _bb_y = min_y;
        _bb_h = max_y - min_y + 1;
    }
    return change_detected;
}

void subtitle_renderer::render_ass(uint32_t *bgra32_buffer)
{
    if (_bb_w > 0 && _bb_h > 0)
    {
        std::memset(bgra32_buffer, 0, _bb_w * _bb_h * sizeof(uint32_t));
        ASS_Image *img = _ass_img;
        while (img && img->w > 0 && img->h > 0)
        {
            blend_ass_image(img, bgra32_buffer);
            img = img->next;
        }
    }
}

bool subtitle_renderer::prerender_img(const subtitle_box &box)
{
    _img_box = &box;
    // Determine bounding box
    int min_x = std::numeric_limits<int>::max();
    int max_x = -1;
    int min_y = std::numeric_limits<int>::max();
    int max_y = -1;
    for (size_t i = 0; i < box.images.size(); i++)
    {
        const subtitle_box::image_t &img = box.images[i];
        if (img.x < min_x)
            min_x = img.x;
        if (img.x + img.w - 1 > max_x)
            max_x = img.x + img.w - 1;
        if (img.y < min_y)
            min_y = img.y;
        if (img.y + img.h - 1 > max_y)
            max_y = img.y + img.h - 1;
    }
    if (max_x < 0)
    {
        _bb_x = 0;
        _bb_y = 0;
        _bb_w = 0;
        _bb_h = 0;
    }
    else
    {
        _bb_x = min_x;
        _bb_w = max_x - min_x + 1;
        _bb_y = min_y;
        _bb_h = max_y - min_y + 1;
    }
    return true;
}

void subtitle_renderer::render_img(uint32_t *bgra32_buffer)
{
    if (_bb_w <= 0 || _bb_h <= 0)
    {
        return;
    }

    std::memset(bgra32_buffer, 0, _bb_w * _bb_h * sizeof(uint32_t));
    for (size_t i = 0; i < _img_box->images.size(); i++)
    {
        const subtitle_box::image_t &img = _img_box->images[i];
        const uint8_t *src = &(img.data[0]);
        for (int src_y = 0; src_y < img.h; src_y++)
        {
            int dst_y = src_y + img.y - _bb_y;
            if (dst_y >= _bb_h)
            {
                break;
            }
            for (int src_x = 0; src_x < img.w; src_x++)
            {
                int dst_x = src_x + img.x - _bb_x;
                if (dst_x >= _bb_w)
                {
                    break;
                }
                int palette_index = src[src_x];
                uint32_t palette_entry = reinterpret_cast<const uint32_t *>(&(img.palette[0]))[palette_index];
                unsigned int A = (palette_entry >> 24u);
                unsigned int R = (palette_entry >> 16u) & 0xffu;
                unsigned int G = (palette_entry >> 8u) & 0xffu;
                unsigned int B = palette_entry & 0xffu;
                uint32_t oldval = bgra32_buffer[dst_y * _bb_w + dst_x];
                // XXX: The BGRA layout used here may be wrong on big endian system
                uint32_t newval = std::min(A + (oldval >> 24u), 255u) << 24u
                    | ((A * R + (255u - A) * ((oldval >> 16u) & 0xffu)) / 255u) << 16u
                    | ((A * G + (255u - A) * ((oldval >>  8u) & 0xffu)) / 255u) << 8u
                    | ((A * B + (255u - A) * ((oldval       ) & 0xffu)) / 255u);
                bgra32_buffer[dst_y * _bb_w + dst_x] = newval;
            }
            src += img.linesize;
        }
    }
}