File: scte27.c

package info (click to toggle)
vlc 3.0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 208,024 kB
  • sloc: ansic: 443,448; cpp: 111,223; objc: 36,399; sh: 6,737; makefile: 6,627; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (519 lines) | stat: -rw-r--r-- 17,119 bytes parent folder | download | duplicates (7)
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
/*****************************************************************************
 * scte27.c : SCTE-27 subtitles decoder
 *****************************************************************************
 * Copyright (C) Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
 * This program 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.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
#include <vlc_bits.h>

#include <assert.h>

/*****************************************************************************
 * Module descriptor.
 *****************************************************************************/
static int  Open (vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin ()
    set_description(N_("SCTE-27 decoder"))
    set_shortname(N_("SCTE-27"))
    set_capability( "spu decoder", 51)
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_SCODEC)
    set_callbacks(Open, Close)
vlc_module_end ()

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
struct decoder_sys_t {
    int     segment_id;
    int     segment_size;
    uint8_t *segment_buffer;
    vlc_tick_t segment_date;
};

typedef struct {
    uint8_t y, u, v;
    uint8_t alpha;
} scte27_color_t;

static const scte27_color_t scte27_color_transparent = {
    .y     = 0x00,
    .u     = 0x80,
    .v     = 0x80,
    .alpha = 0x00,
};

static scte27_color_t bs_read_color(bs_t *bs)
{
    scte27_color_t color;

    /* XXX it's unclear if a value of 0 in Y/U/V means a transparent pixel */
    color.y     = bs_read(bs, 5) << 3;
    color.alpha = bs_read1(bs) ? 0xff : 0x80;
    color.v     = bs_read(bs, 5) << 3;
    color.u     = bs_read(bs, 5) << 3;

    return color;
}

static inline void SetYUVPPixel(picture_t *picture, int x, int y, int value)
{
    picture->p->p_pixels[y * picture->p->i_pitch + x] = value;
}

static subpicture_region_t *DecodeSimpleBitmap(decoder_t *dec,
                                               const uint8_t *data, int size)
{
    VLC_UNUSED(dec);
    /* Parse the bitmap and its properties */
    bs_t bs;
    bs_init(&bs, data, size);

    bs_skip(&bs, 5);
    int is_framed = bs_read(&bs, 1);
    int outline_style = bs_read(&bs, 2);
    scte27_color_t character_color = bs_read_color(&bs);
    int top_h = bs_read(&bs, 12);
    int top_v = bs_read(&bs, 12);
    int bottom_h = bs_read(&bs, 12);
    int bottom_v = bs_read(&bs, 12);
    if (top_h >= bottom_h || top_v >= bottom_v)
        return NULL;
    int frame_top_h = top_h;
    int frame_top_v = top_v;
    int frame_bottom_h = bottom_h;
    int frame_bottom_v = bottom_v;
    scte27_color_t frame_color = scte27_color_transparent;
    if (is_framed) {
        frame_top_h = bs_read(&bs, 12);
        frame_top_v = bs_read(&bs, 12);
        frame_bottom_h = bs_read(&bs, 12);
        frame_bottom_v = bs_read(&bs, 12);
        frame_color = bs_read_color(&bs);
        if (frame_top_h > top_h ||
            frame_top_v > top_v ||
            frame_bottom_h < bottom_h ||
            frame_bottom_v < bottom_v)
            return NULL;
    }
    int outline_thickness = 0;
    scte27_color_t outline_color = scte27_color_transparent;
    int shadow_right = 0;
    int shadow_bottom = 0;
    scte27_color_t shadow_color = scte27_color_transparent;
    if (outline_style == 1) {
        bs_skip(&bs, 4);
        outline_thickness = bs_read(&bs, 4);
        outline_color = bs_read_color(&bs);
    } else if (outline_style == 2) {
        shadow_right = bs_read(&bs, 4);
        shadow_bottom = bs_read(&bs, 4);
        shadow_color = bs_read_color(&bs);
    } else if (outline_style == 3) {
        bs_skip(&bs, 24);
    }
    bs_skip(&bs, 16); // bitmap_compressed_length
    int bitmap_h = bottom_h - top_h;
    int bitmap_v = bottom_v - top_v;
    int bitmap_size = bitmap_h * bitmap_v;
    bool *bitmap = vlc_alloc(bitmap_size, sizeof(*bitmap));
    if (!bitmap)
        return NULL;
    for (int position = 0; position < bitmap_size;) {
        if (bs_eof(&bs)) {
            for (; position < bitmap_size; position++)
                bitmap[position] = false;
            break;
        }

        int run_on_length = 0;
        int run_off_length = 0;
        if (!bs_read1(&bs)) {
            if (!bs_read1(&bs)) {
                if (!bs_read1(&bs)) {
                    if (bs_read(&bs, 2) == 1) {
                        int next = __MIN((position / bitmap_h + 1) * bitmap_h,
                                         bitmap_size);
                        for (; position < next; position++)
                            bitmap[position] = false;
                    }
                } else {
                    run_on_length = 4;
                }
            } else {
                run_off_length = 6;
            }
        } else {
            run_on_length = 3;
            run_off_length = 5;
        }

        if (run_on_length > 0) {
            int run = bs_read(&bs, run_on_length);
            if (!run)
                run = 1 << run_on_length;
            for (; position < bitmap_size && run > 0; position++, run--)
                bitmap[position] = true;
        }
        if (run_off_length > 0) {
            int run = bs_read(&bs, run_off_length);
            if (!run)
                run = 1 << run_off_length;
            for (; position < bitmap_size && run > 0; position++, run--)
                bitmap[position] = false;
        }
    }

    /* Render the bitmap into a subpicture_region_t */

    /* Reserve the place for the style
     * FIXME It's unclear if it is needed or if the bitmap should already include
     * the needed margin (I think the samples I have do both). */
    int margin_h = 0;
    int margin_v = 0;
    if (outline_style == 1) {
        margin_h =
        margin_v = outline_thickness;
    } else if (outline_style == 2) {
        margin_h = shadow_right;
        margin_v = shadow_bottom;
    }
    frame_top_h -= margin_h;
    frame_top_v -= margin_v;
    frame_bottom_h += margin_h;
    frame_bottom_v += margin_v;

    const int frame_h = frame_bottom_h - frame_top_h;
    const int frame_v = frame_bottom_v - frame_top_v;
    const int bitmap_oh = top_h - frame_top_h;
    const int bitmap_ov = top_v - frame_top_v;

    enum {
        COLOR_FRAME,
        COLOR_CHARACTER,
        COLOR_OUTLINE,
        COLOR_SHADOW,
    };
    video_palette_t palette = {
        .i_entries = 4,
        .palette = {
            [COLOR_FRAME] = {
                frame_color.y,
                frame_color.u,
                frame_color.v,
                frame_color.alpha
            },
            [COLOR_CHARACTER] = {
                character_color.y,
                character_color.u,
                character_color.v,
                character_color.alpha
            },
            [COLOR_OUTLINE] = {
                outline_color.y,
                outline_color.u,
                outline_color.v,
                outline_color.alpha
            },
            [COLOR_SHADOW] = {
                shadow_color.y,
                shadow_color.u,
                shadow_color.v,
                shadow_color.alpha
            },
        },
    };
    video_format_t fmt = {
        .i_chroma = VLC_CODEC_YUVP,
        .i_width = frame_h,
        .i_visible_width = frame_h,
        .i_height = frame_v,
        .i_visible_height = frame_v,
        .i_sar_num = 0, /* Use video AR */
        .i_sar_den = 1,
        .p_palette = &palette,
    };
    subpicture_region_t *r = subpicture_region_New(&fmt);
    if (!r) {
        free(bitmap);
        return NULL;
    }
    r->i_x = frame_top_h;
    r->i_y = frame_top_v;

    /* Fill up with frame (background) color */
    for (int y = 0; y < frame_v; y++)
        memset(&r->p_picture->p->p_pixels[y * r->p_picture->p->i_pitch],
               COLOR_FRAME,
               frame_h);

    /* Draw the outline/shadow if requested */
    if (outline_style == 1) {
        /* Draw an outline
         * XXX simple but slow and of low quality (no anti-aliasing) */
        bool circle[16][16];
        for (int dy = 0; dy <= 15; dy++) {
            for (int dx = 0; dx <= 15; dx++)
                circle[dy][dx] = (dx > 0 || dy > 0) &&
                                 dx * dx + dy * dy <= outline_thickness * outline_thickness;
        }
        for (int by = 0; by < bitmap_v; by++) {
            for (int bx = 0; bx < bitmap_h; bx++) {
                if (!bitmap[by * bitmap_h + bx])
                    continue;
                for (int dy = 0; dy <= outline_thickness; dy++) {
                    for (int dx = 0; dx <= outline_thickness; dx++) {
                        if (circle[dy][dx]) {
                            SetYUVPPixel(r->p_picture,
                                         bx + bitmap_oh + dx, by + bitmap_ov + dy, COLOR_OUTLINE);
                            SetYUVPPixel(r->p_picture,
                                         bx + bitmap_oh - dx, by + bitmap_ov + dy, COLOR_OUTLINE);
                            SetYUVPPixel(r->p_picture,
                                         bx + bitmap_oh + dx, by + bitmap_ov - dy, COLOR_OUTLINE);
                            SetYUVPPixel(r->p_picture,
                                         bx + bitmap_oh - dx, by + bitmap_ov - dy, COLOR_OUTLINE);
                        }
                    }
                }
            }
        }
    } else if (outline_style == 2) {
        /* Draw a shadow by drawing the character shifted by shadow right/bottom */
        for (int by = 0; by < bitmap_v; by++) {
            for (int bx = 0; bx < bitmap_h; bx++) {
                if (bitmap[by * bitmap_h + bx])
                    SetYUVPPixel(r->p_picture,
                                 bx + bitmap_oh + shadow_right,
                                 by + bitmap_ov + shadow_bottom,
                                 COLOR_SHADOW);
            }
        }
    }

    /* Draw the character */
    for (int by = 0; by < bitmap_v; by++) {
        for (int bx = 0; bx < bitmap_h; bx++) {
            if (bitmap[by * bitmap_h + bx])
                SetYUVPPixel(r->p_picture,
                             bx + bitmap_oh, by + bitmap_ov, COLOR_CHARACTER);
        }
    }
    free(bitmap);
    return r;
}

static subpicture_t *DecodeSubtitleMessage(decoder_t *dec,
                                           const uint8_t *data, int size,
                                           vlc_tick_t date)
{
    if (size < 12)
        goto error;

    /* Parse the header */
    bool pre_clear_display = data[3] & 0x80;
    int  display_standard = data[3] & 0x1f;
    int subtitle_type = data[8] >> 4;
    int display_duration = ((data[8] & 0x07) << 8) | data[9];
    int block_length = GetWBE(&data[10]);

    size -= 12;
    data += 12;

    if (block_length > size)
        goto error;

    if (subtitle_type == 1) {
        subpicture_region_t *region = DecodeSimpleBitmap(dec, data, block_length);
        if (!region)
            goto error;
        subpicture_t *sub = decoder_NewSubpicture(dec, NULL);
        if (!sub) {
            subpicture_region_Delete(region);
            return NULL;
        }
        int frame_duration;
        switch (display_standard) {
        case 0:
            sub->i_original_picture_width  = 720;
            sub->i_original_picture_height = 480;
            frame_duration = 33367;
            break;
        case 1:
            sub->i_original_picture_width  = 720;
            sub->i_original_picture_height = 576;
            frame_duration = 40000;
            break;
        case 2:
            sub->i_original_picture_width  = 1280;
            sub->i_original_picture_height =  720;
            frame_duration = 16683;
            break;
        case 3:
            sub->i_original_picture_width  = 1920;
            sub->i_original_picture_height = 1080;
            frame_duration = 16683;
            break;
        default:
            msg_Warn(dec, "Unknown display standard");
            sub->i_original_picture_width  = 0;
            sub->i_original_picture_height = 0;
            frame_duration = 40000;
            break;
        }
        sub->b_absolute = true;
        if (!pre_clear_display)
            msg_Warn(dec, "SCTE-27 subtitles without pre_clear_display flag are not well supported");
        sub->b_ephemer = true;
        sub->i_start = date;
        sub->i_stop = date + display_duration * frame_duration;
        sub->p_region = region;

        return sub;
    } else {
        /* Reserved */
        return NULL;
    }

error:
    msg_Err(dec, "corrupted subtitle_message");
    return NULL;
}

static int Decode(decoder_t *dec, block_t *b)
{
    decoder_sys_t *sys = dec->p_sys;

    if (b == NULL ) /* No Drain */
        return VLCDEC_SUCCESS;

    if (b->i_flags & (BLOCK_FLAG_CORRUPTED))
        goto exit;

    while (b->i_buffer > 3) {
        const int table_id =  b->p_buffer[0];
        if (table_id != 0xc6) {
            //if (table_id != 0xff)
            //    msg_Err(dec, "Invalid SCTE-27 table id (0x%x)", table_id);
            break;
        }
        const int section_length = ((b->p_buffer[1] & 0xf) << 8) | b->p_buffer[2];
        if (section_length <= 1 + 4 || b->i_buffer < 3 + (unsigned)section_length) {
            msg_Err(dec, "Invalid SCTE-27 section length");
            break;
        }
        const int protocol_version = b->p_buffer[3] & 0x3f;
        if (protocol_version != 0) {
            msg_Err(dec, "Unsupported SCTE-27 protocol version (%d)", protocol_version);
            break;
        }
        const bool segmentation_overlay = b->p_buffer[3] & 0x40;

        subpicture_t *sub = NULL;
        if (segmentation_overlay) {
            if (section_length < 1 + 5 + 4)
                break;
            int id = GetWBE(&b->p_buffer[4]);
            int last = (b->p_buffer[6] << 4) | (b->p_buffer[7] >> 4);
            int index = ((b->p_buffer[7] & 0x0f) << 8) | b->p_buffer[8];
            if (index > last)
                break;
            if (index == 0) {
                sys->segment_id = id;
                sys->segment_size = 0;
                sys->segment_date = b->i_pts > VLC_TICK_INVALID ? b->i_pts : b->i_dts;
            } else {
                if (sys->segment_id != id || sys->segment_size <= 0) {
                    sys->segment_id = -1;
                    break;
                }
            }

            int segment_size = section_length - 1 - 5 - 4;

            sys->segment_buffer = xrealloc(sys->segment_buffer,
                                           sys->segment_size + segment_size);
            memcpy(&sys->segment_buffer[sys->segment_size],
                   &b->p_buffer[9], segment_size);
            sys->segment_size += segment_size;

            if (index == last) {
                sub = DecodeSubtitleMessage(dec,
                                            sys->segment_buffer,
                                            sys->segment_size,
                                            sys->segment_date);
                sys->segment_size = 0;
            }
        } else {
            sub = DecodeSubtitleMessage(dec,
                                        &b->p_buffer[4],
                                        section_length - 1 - 4,
                                        b->i_pts > VLC_TICK_INVALID ? b->i_pts : b->i_dts);
        }
        if (sub != NULL)
            decoder_QueueSub(dec, sub);

        b->i_buffer -= 3 + section_length;
        b->p_buffer += 3 + section_length;
        break;
    }

exit:
    block_Release(b);
    return VLCDEC_SUCCESS;
}

static int Open(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t *)object;

    if (dec->fmt_in.i_codec != VLC_CODEC_SCTE_27)
        return VLC_EGENERIC;

    decoder_sys_t *sys = dec->p_sys = malloc(sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;
    sys->segment_id = -1;
    sys->segment_size = 0;
    sys->segment_buffer = NULL;

    dec->pf_decode = Decode;
    dec->fmt_out.i_codec = VLC_CODEC_YUVP;

    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t *)object;
    decoder_sys_t *sys = dec->p_sys;

    free(sys->segment_buffer);
    free(sys);
}