GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/dvdsubenc.c Lines: 206 259 79.5 %
Date: 2020-09-28 00:47:38 Branches: 133 172 77.3 %

Line Branch Exec Source
1
/*
2
 * DVD subtitle encoding
3
 * Copyright (c) 2005 Wolfram Gloger
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
#include "avcodec.h"
22
#include "bytestream.h"
23
#include "internal.h"
24
#include "libavutil/avassert.h"
25
#include "libavutil/bprint.h"
26
#include "libavutil/imgutils.h"
27
#include "libavutil/opt.h"
28
29
typedef struct {
30
    AVClass *class;
31
    uint32_t global_palette[16];
32
    char *palette_str;
33
    int even_rows_fix;
34
} DVDSubtitleContext;
35
36
// ncnt is the nibble counter
37
#define PUTNIBBLE(val)\
38
do {\
39
    if (ncnt++ & 1)\
40
        *q++ = bitbuf | ((val) & 0x0f);\
41
    else\
42
        bitbuf = (val) << 4;\
43
} while(0)
44
45
86
static void dvd_encode_rle(uint8_t **pq,
46
                           const uint8_t *bitmap, int linesize,
47
                           int w, int h,
48
                           const int cmap[256])
49
{
50
    uint8_t *q;
51
86
    unsigned int bitbuf = 0;
52
    int ncnt;
53
    int x, y, len, color;
54
55
86
    q = *pq;
56
57
1649
    for (y = 0; y < h; ++y) {
58
1563
        ncnt = 0;
59
119769
        for(x = 0; x < w; x += len) {
60
118206
            color = bitmap[x];
61
473056
            for (len=1; x+len < w; ++len)
62
471493
                if (bitmap[x+len] != color)
63
116643
                    break;
64
118206
            color = cmap[color];
65
118206
            av_assert0(color < 4);
66
118206
            if (len < 0x04) {
67
94474
                PUTNIBBLE((len << 2)|color);
68
23732
            } else if (len < 0x10) {
69
21030
                PUTNIBBLE(len >> 2);
70
21030
                PUTNIBBLE((len << 2)|color);
71
2702
            } else if (len < 0x40) {
72
1843
                PUTNIBBLE(0);
73
1843
                PUTNIBBLE(len >> 2);
74
1843
                PUTNIBBLE((len << 2)|color);
75
859
            } else if (x+len == w) {
76
321
                PUTNIBBLE(0);
77
321
                PUTNIBBLE(0);
78
321
                PUTNIBBLE(0);
79
321
                PUTNIBBLE(color);
80
            } else {
81
538
                if (len > 0xff)
82
26
                    len = 0xff;
83
538
                PUTNIBBLE(0);
84
538
                PUTNIBBLE(len >> 6);
85
538
                PUTNIBBLE(len >> 2);
86
538
                PUTNIBBLE((len << 2)|color);
87
            }
88
        }
89
        /* end of line */
90
1563
        if (ncnt & 1)
91
739
            PUTNIBBLE(0);
92
1563
        bitmap += linesize;
93
    }
94
95
86
    *pq = q;
96
86
}
97
98
45795
static int color_distance(uint32_t a, uint32_t b)
99
{
100
45795
    int r = 0, d, i;
101
45795
    int alpha_a = 8, alpha_b = 8;
102
103
228975
    for (i = 24; i >= 0; i -= 8) {
104
183180
        d = alpha_a * (int)((a >> i) & 0xFF) -
105
183180
            alpha_b * (int)((b >> i) & 0xFF);
106
183180
        r += d * d;
107
183180
        alpha_a = a >> 28;
108
183180
        alpha_b = b >> 28;
109
    }
110
45795
    return r;
111
}
112
113
/**
114
 * Count colors used in a rectangle, quantizing alpha and grouping by
115
 * nearest global palette entry.
116
 */
117
43
static void count_colors(AVCodecContext *avctx, unsigned hits[33],
118
                         const AVSubtitleRect *r)
119
{
120
43
    DVDSubtitleContext *dvdc = avctx->priv_data;
121
43
    unsigned count[256] = { 0 };
122
43
    uint32_t *palette = (uint32_t *)r->data[1];
123
    uint32_t color;
124
43
    int x, y, i, j, match, d, best_d, av_uninit(best_j);
125
43
    uint8_t *p = r->data[0];
126
127
1606
    for (y = 0; y < r->h; y++) {
128
473710
        for (x = 0; x < r->w; x++)
129
472147
            count[*(p++)]++;
130
1563
        p += r->linesize[0] - r->w;
131
    }
132
11051
    for (i = 0; i < 256; i++) {
133
11008
        if (!count[i]) /* avoid useless search */
134
10836
            continue;
135
172
        color = palette[i];
136
        /* 0: transparent, 1-16: semi-transparent, 17-33 opaque */
137

172
        match = color < 0x33000000 ? 0 : color < 0xCC000000 ? 1 : 17;
138
172
        if (match) {
139
86
            best_d = INT_MAX;
140
1462
            for (j = 0; j < 16; j++) {
141
1376
                d = color_distance(0xFF000000 | color,
142
1376
                                   0xFF000000 | dvdc->global_palette[j]);
143
1376
                if (d < best_d) {
144
172
                    best_d = d;
145
172
                    best_j = j;
146
                }
147
            }
148
86
            match += best_j;
149
        }
150
172
        hits[match] += count[i];
151
    }
152
43
}
153
154
43
static void select_palette(AVCodecContext *avctx, int out_palette[4],
155
                           int out_alpha[4], unsigned hits[33])
156
{
157
43
    DVDSubtitleContext *dvdc = avctx->priv_data;
158
    int i, j, bright, mult;
159
    uint32_t color;
160
43
    int selected[4] = { 0 };
161
43
    uint32_t pseudopal[33] = { 0 };
162
43
    uint32_t refcolor[3] = { 0x00000000, 0xFFFFFFFF, 0xFF000000 };
163
164
    /* Bonus for transparent: if the rectangle fits tightly the text, the
165
       background color can be quite rare, but it would be ugly without it */
166
43
    hits[0] *= 16;
167
    /* Bonus for bright colors */
168
731
    for (i = 0; i < 16; i++) {
169
688
        if (!(hits[1 + i] + hits[17 + i]))
170
602
            continue; /* skip unused colors to gain time */
171
86
        color = dvdc->global_palette[i];
172
86
        bright = 0;
173
344
        for (j = 0; j < 3; j++, color >>= 8)
174

258
            bright += (color & 0xFF) < 0x40 || (color & 0xFF) >= 0xC0;
175
86
        mult = 2 + FFMIN(bright, 2);
176
86
        hits[ 1 + i] *= mult;
177
86
        hits[17 + i] *= mult;
178
    }
179
180
    /* Select four most frequent colors */
181
215
    for (i = 0; i < 4; i++) {
182
5848
        for (j = 0; j < 33; j++)
183
5676
            if (hits[j] > hits[selected[i]])
184
129
                selected[i] = j;
185
172
        hits[selected[i]] = 0;
186
    }
187
188
    /* Order the colors like in most DVDs:
189
       0: background, 1: foreground, 2: outline */
190
731
    for (i = 0; i < 16; i++) {
191
688
        pseudopal[ 1 + i] = 0x80000000 | dvdc->global_palette[i];
192
688
        pseudopal[17 + i] = 0xFF000000 | dvdc->global_palette[i];
193
    }
194
172
    for (i = 0; i < 3; i++) {
195
129
        int best_d = color_distance(refcolor[i], pseudopal[selected[i]]);
196
387
        for (j = i + 1; j < 4; j++) {
197
258
            int d = color_distance(refcolor[i], pseudopal[selected[j]]);
198
258
            if (d < best_d) {
199
                FFSWAP(int, selected[i], selected[j]);
200
                best_d = d;
201
            }
202
        }
203
    }
204
205
    /* Output */
206
215
    for (i = 0; i < 4; i++) {
207
172
        out_palette[i] = selected[i] ? (selected[i] - 1) & 0xF : 0;
208

172
        out_alpha  [i] = !selected[i] ? 0 : selected[i] < 17 ? 0x80 : 0xFF;
209
    }
210
43
}
211
212
43
static void build_color_map(AVCodecContext *avctx, int cmap[],
213
                            const uint32_t palette[],
214
                            const int out_palette[], unsigned int const out_alpha[])
215
{
216
43
    DVDSubtitleContext *dvdc = avctx->priv_data;
217
    int i, j, d, best_d;
218
    uint32_t pseudopal[4];
219
220
215
    for (i = 0; i < 4; i++)
221
172
        pseudopal[i] = (out_alpha[i] << 24) |
222
172
                       dvdc->global_palette[out_palette[i]];
223
11051
    for (i = 0; i < 256; i++) {
224
11008
        best_d = INT_MAX;
225
55040
        for (j = 0; j < 4; j++) {
226
44032
            d = color_distance(pseudopal[j], palette[i]);
227
44032
            if (d < best_d) {
228
11094
                cmap[i] = j;
229
11094
                best_d = d;
230
            }
231
        }
232
    }
233
43
}
234
235
static void copy_rectangle(AVSubtitleRect *dst, AVSubtitleRect *src, int cmap[])
236
{
237
    int x, y;
238
    uint8_t *p, *q;
239
240
    p = src->data[0];
241
    q = dst->data[0] + (src->x - dst->x) +
242
                            (src->y - dst->y) * dst->linesize[0];
243
    for (y = 0; y < src->h; y++) {
244
        for (x = 0; x < src->w; x++)
245
            *(q++) = cmap[*(p++)];
246
        p += src->linesize[0] - src->w;
247
        q += dst->linesize[0] - src->w;
248
    }
249
}
250
251
43
static int encode_dvd_subtitles(AVCodecContext *avctx,
252
                                uint8_t *outbuf, int outbuf_size,
253
                                const AVSubtitle *h)
254
{
255
43
    DVDSubtitleContext *dvdc = avctx->priv_data;
256
    uint8_t *q, *qq;
257
    int offset1, offset2;
258
43
    int i, rects = h->num_rects, ret;
259
43
    unsigned global_palette_hits[33] = { 0 };
260
    int cmap[256];
261
    int out_palette[4];
262
    int out_alpha[4];
263
    AVSubtitleRect vrect;
264
43
    uint8_t *vrect_data = NULL;
265
    int x2, y2;
266
43
    int forced = 0;
267
268

43
    if (rects == 0 || !h->rects)
269
        return AVERROR(EINVAL);
270
86
    for (i = 0; i < rects; i++)
271
43
        if (h->rects[i]->type != SUBTITLE_BITMAP) {
272
            av_log(avctx, AV_LOG_ERROR, "Bitmap subtitle required\n");
273
            return AVERROR(EINVAL);
274
        }
275
    /* Mark this subtitle forced if any of the rectangles is forced. */
276
86
    for (i = 0; i < rects; i++)
277
43
        if ((h->rects[i]->flags & AV_SUBTITLE_FLAG_FORCED) != 0) {
278
            forced = 1;
279
            break;
280
        }
281
282
#if FF_API_AVPICTURE
283
FF_DISABLE_DEPRECATION_WARNINGS
284
86
    for (i = 0; i < rects; i++)
285
43
        if (!h->rects[i]->data[0]) {
286
            AVSubtitleRect *rect = h->rects[i];
287
            int j;
288
            for (j = 0; j < 4; j++) {
289
                rect->data[j] = rect->pict.data[j];
290
                rect->linesize[j] = rect->pict.linesize[j];
291
            }
292
        }
293
FF_ENABLE_DEPRECATION_WARNINGS
294
#endif
295
296
43
    vrect = *h->rects[0];
297
298
43
    if (rects > 1) {
299
        /* DVD subtitles can have only one rectangle: build a virtual
300
           rectangle containing all actual rectangles.
301
           The data of the rectangles will be copied later, when the palette
302
           is decided, because the rectangles may have different palettes. */
303
        int xmin = h->rects[0]->x, xmax = xmin + h->rects[0]->w;
304
        int ymin = h->rects[0]->y, ymax = ymin + h->rects[0]->h;
305
        for (i = 1; i < rects; i++) {
306
            xmin = FFMIN(xmin, h->rects[i]->x);
307
            ymin = FFMIN(ymin, h->rects[i]->y);
308
            xmax = FFMAX(xmax, h->rects[i]->x + h->rects[i]->w);
309
            ymax = FFMAX(ymax, h->rects[i]->y + h->rects[i]->h);
310
        }
311
        vrect.x = xmin;
312
        vrect.y = ymin;
313
        vrect.w = xmax - xmin;
314
        vrect.h = ymax - ymin;
315
        if ((ret = av_image_check_size(vrect.w, vrect.h, 0, avctx)) < 0)
316
            return ret;
317
318
        /* Count pixels outside the virtual rectangle as transparent */
319
        global_palette_hits[0] = vrect.w * vrect.h;
320
        for (i = 0; i < rects; i++)
321
            global_palette_hits[0] -= h->rects[i]->w * h->rects[i]->h;
322
    }
323
324
86
    for (i = 0; i < rects; i++)
325
43
        count_colors(avctx, global_palette_hits, h->rects[i]);
326
43
    select_palette(avctx, out_palette, out_alpha, global_palette_hits);
327
328
43
    if (rects > 1) {
329
        if (!(vrect_data = av_calloc(vrect.w, vrect.h)))
330
            return AVERROR(ENOMEM);
331
        vrect.data    [0] = vrect_data;
332
        vrect.linesize[0] = vrect.w;
333
        for (i = 0; i < rects; i++) {
334
            build_color_map(avctx, cmap, (uint32_t *)h->rects[i]->data[1],
335
                            out_palette, out_alpha);
336
            copy_rectangle(&vrect, h->rects[i], cmap);
337
        }
338
        for (i = 0; i < 4; i++)
339
            cmap[i] = i;
340
    } else {
341
43
        build_color_map(avctx, cmap, (uint32_t *)h->rects[0]->data[1],
342
                        out_palette, out_alpha);
343
    }
344
345
43
    av_log(avctx, AV_LOG_DEBUG, "Selected palette:");
346
215
    for (i = 0; i < 4; i++)
347
172
        av_log(avctx, AV_LOG_DEBUG, " 0x%06"PRIx32"@@%02x (0x%x,0x%x)",
348
172
               dvdc->global_palette[out_palette[i]], out_alpha[i],
349
172
               out_palette[i], out_alpha[i] >> 4);
350
43
    av_log(avctx, AV_LOG_DEBUG, "\n");
351
352
    // encode data block
353
43
    q = outbuf + 4;
354
43
    offset1 = q - outbuf;
355
    // worst case memory requirement: 1 nibble per pixel..
356
43
    if ((q - outbuf) + vrect.w * vrect.h / 2 + 17 + 21 > outbuf_size) {
357
        av_log(NULL, AV_LOG_ERROR, "dvd_subtitle too big\n");
358
        ret = AVERROR_BUFFER_TOO_SMALL;
359
        goto fail;
360
    }
361
43
    dvd_encode_rle(&q, vrect.data[0], vrect.w * 2,
362
43
                   vrect.w, (vrect.h + 1) >> 1, cmap);
363
43
    offset2 = q - outbuf;
364
43
    dvd_encode_rle(&q, vrect.data[0] + vrect.w, vrect.w * 2,
365
43
                   vrect.w, vrect.h >> 1, cmap);
366
367

43
    if (dvdc->even_rows_fix && (vrect.h & 1)) {
368
        // Work-around for some players that want the height to be even.
369
        vrect.h++;
370
        *q++ = 0x00; // 0x00 0x00 == empty row, i.e. fully transparent
371
        *q++ = 0x00;
372
    }
373
374
    // set data packet size
375
43
    qq = outbuf + 2;
376
43
    bytestream_put_be16(&qq, q - outbuf);
377
378
    // send start display command
379
43
    bytestream_put_be16(&q, (h->start_display_time*90) >> 10);
380
43
    bytestream_put_be16(&q, (q - outbuf) /*- 2 */ + 8 + 12 + 2);
381
43
    *q++ = 0x03; // palette - 4 nibbles
382
43
    *q++ = (out_palette[3] << 4) | out_palette[2];
383
43
    *q++ = (out_palette[1] << 4) | out_palette[0];
384
43
    *q++ = 0x04; // alpha - 4 nibbles
385
43
    *q++ = (out_alpha[3] & 0xF0) | (out_alpha[2] >> 4);
386
43
    *q++ = (out_alpha[1] & 0xF0) | (out_alpha[0] >> 4);
387
388
    // 12 bytes per rect
389
43
    x2 = vrect.x + vrect.w - 1;
390
43
    y2 = vrect.y + vrect.h - 1;
391
392
43
    *q++ = 0x05;
393
    // x1 x2 -> 6 nibbles
394
43
    *q++ = vrect.x >> 4;
395
43
    *q++ = (vrect.x << 4) | ((x2 >> 8) & 0xf);
396
43
    *q++ = x2;
397
    // y1 y2 -> 6 nibbles
398
43
    *q++ = vrect.y >> 4;
399
43
    *q++ = (vrect.y << 4) | ((y2 >> 8) & 0xf);
400
43
    *q++ = y2;
401
402
43
    *q++ = 0x06;
403
    // offset1, offset2
404
43
    bytestream_put_be16(&q, offset1);
405
43
    bytestream_put_be16(&q, offset2);
406
407
43
    *q++ = forced ? 0x00 : 0x01; // start command
408
43
    *q++ = 0xff; // terminating command
409
410
    // send stop display command last
411
43
    bytestream_put_be16(&q, (h->end_display_time*90) >> 10);
412
43
    bytestream_put_be16(&q, (q - outbuf) - 2 /*+ 4*/);
413
43
    *q++ = 0x02; // set end
414
43
    *q++ = 0xff; // terminating command
415
416
43
    qq = outbuf;
417
43
    bytestream_put_be16(&qq, q - outbuf);
418
419
43
    av_log(NULL, AV_LOG_DEBUG, "subtitle_packet size=%"PTRDIFF_SPECIFIER"\n", q - outbuf);
420
43
    ret = q - outbuf;
421
422
43
fail:
423
43
    av_free(vrect_data);
424
43
    return ret;
425
}
426
427
1
static int dvdsub_init(AVCodecContext *avctx)
428
{
429
1
    DVDSubtitleContext *dvdc = avctx->priv_data;
430
    static const uint32_t default_palette[16] = {
431
        0x000000, 0x0000FF, 0x00FF00, 0xFF0000,
432
        0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFFFFF,
433
        0x808000, 0x8080FF, 0x800080, 0x80FF80,
434
        0x008080, 0xFF8080, 0x555555, 0xAAAAAA,
435
    };
436
    AVBPrint extradata;
437
    int i, ret;
438
439
    av_assert0(sizeof(dvdc->global_palette) == sizeof(default_palette));
440
1
    if (dvdc->palette_str) {
441
        ff_dvdsub_parse_palette(dvdc->global_palette, dvdc->palette_str);
442
    } else {
443
1
        memcpy(dvdc->global_palette, default_palette, sizeof(dvdc->global_palette));
444
    }
445
446
1
    av_bprint_init(&extradata, 0, AV_BPRINT_SIZE_AUTOMATIC);
447

1
    if (avctx->width && avctx->height)
448
1
        av_bprintf(&extradata, "size: %dx%d\n", avctx->width, avctx->height);
449
1
    av_bprintf(&extradata, "palette:");
450
17
    for (i = 0; i < 16; i++)
451
16
        av_bprintf(&extradata, " %06"PRIx32"%c",
452
16
                   dvdc->global_palette[i] & 0xFFFFFF, i < 15 ? ',' : '\n');
453
454
1
    ret = avpriv_bprint_to_extradata(avctx, &extradata);
455
1
    if (ret < 0)
456
        return ret;
457
458
1
    return 0;
459
}
460
461
43
static int dvdsub_encode(AVCodecContext *avctx,
462
                         unsigned char *buf, int buf_size,
463
                         const AVSubtitle *sub)
464
{
465
    //DVDSubtitleContext *s = avctx->priv_data;
466
    int ret;
467
468
43
    ret = encode_dvd_subtitles(avctx, buf, buf_size, sub);
469
43
    return ret;
470
}
471
472
#define OFFSET(x) offsetof(DVDSubtitleContext, x)
473
#define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM
474
static const AVOption options[] = {
475
    {"palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SE },
476
    {"even_rows_fix", "Make number of rows even (workaround for some players)", OFFSET(even_rows_fix), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SE},
477
    { NULL },
478
};
479
480
static const AVClass dvdsubenc_class = {
481
    .class_name = "VOBSUB subtitle encoder",
482
    .item_name  = av_default_item_name,
483
    .option     = options,
484
    .version    = LIBAVUTIL_VERSION_INT,
485
};
486
487
AVCodec ff_dvdsub_encoder = {
488
    .name           = "dvdsub",
489
    .long_name      = NULL_IF_CONFIG_SMALL("DVD subtitles"),
490
    .type           = AVMEDIA_TYPE_SUBTITLE,
491
    .id             = AV_CODEC_ID_DVD_SUBTITLE,
492
    .init           = dvdsub_init,
493
    .encode_sub     = dvdsub_encode,
494
    .priv_class     = &dvdsubenc_class,
495
    .priv_data_size = sizeof(DVDSubtitleContext),
496
};