GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/dvbsubenc.c Lines: 171 307 55.7 %
Date: 2020-09-25 14:59:26 Branches: 95 232 40.9 %

Line Branch Exec Source
1
/*
2
 * DVB subtitle encoding
3
 * Copyright (c) 2005 Fabrice Bellard
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 "libavutil/colorspace.h"
24
25
typedef struct DVBSubtitleContext {
26
    int object_version;
27
} DVBSubtitleContext;
28
29
#define PUTBITS2(val)\
30
{\
31
    bitbuf |= (val) << bitcnt;\
32
    bitcnt -= 2;\
33
    if (bitcnt < 0) {\
34
        bitcnt = 6;\
35
        *q++ = bitbuf;\
36
        bitbuf = 0;\
37
    }\
38
}
39
40
static int dvb_encode_rle2(uint8_t **pq, int buf_size,
41
                           const uint8_t *bitmap, int linesize,
42
                           int w, int h)
43
{
44
    uint8_t *q, *line_begin;
45
    unsigned int bitbuf;
46
    int bitcnt;
47
    int x, y, len, x1, v, color;
48
49
    q = *pq;
50
51
    for(y = 0; y < h; y++) {
52
        // Worst case line is 3 bits per value + 4 bytes overhead
53
        if (buf_size * 8 < w * 3 + 32)
54
            return AVERROR_BUFFER_TOO_SMALL;
55
        line_begin = q;
56
        *q++ = 0x10;
57
        bitbuf = 0;
58
        bitcnt = 6;
59
60
        x = 0;
61
        while (x < w) {
62
            x1 = x;
63
            color = bitmap[x1++];
64
            while (x1 < w && bitmap[x1] == color)
65
                x1++;
66
            len = x1 - x;
67
            if (color == 0 && len == 2) {
68
                PUTBITS2(0);
69
                PUTBITS2(0);
70
                PUTBITS2(1);
71
            } else if (len >= 3 && len <= 10) {
72
                v = len - 3;
73
                PUTBITS2(0);
74
                PUTBITS2((v >> 2) | 2);
75
                PUTBITS2(v & 3);
76
                PUTBITS2(color);
77
            } else if (len >= 12 && len <= 27) {
78
                v = len - 12;
79
                PUTBITS2(0);
80
                PUTBITS2(0);
81
                PUTBITS2(2);
82
                PUTBITS2(v >> 2);
83
                PUTBITS2(v & 3);
84
                PUTBITS2(color);
85
            } else if (len >= 29) {
86
                /* length = 29 ... 284 */
87
                if (len > 284)
88
                    len = 284;
89
                v = len - 29;
90
                PUTBITS2(0);
91
                PUTBITS2(0);
92
                PUTBITS2(3);
93
                PUTBITS2((v >> 6));
94
                PUTBITS2((v >> 4) & 3);
95
                PUTBITS2((v >> 2) & 3);
96
                PUTBITS2(v & 3);
97
                PUTBITS2(color);
98
            } else {
99
                PUTBITS2(color);
100
                if (color == 0) {
101
                    PUTBITS2(1);
102
                }
103
                len = 1;
104
            }
105
            x += len;
106
        }
107
        /* end of line */
108
        PUTBITS2(0);
109
        PUTBITS2(0);
110
        PUTBITS2(0);
111
        if (bitcnt != 6) {
112
            *q++ = bitbuf;
113
        }
114
        *q++ = 0xf0;
115
        bitmap += linesize;
116
        buf_size -= q - line_begin;
117
    }
118
    len = q - *pq;
119
    *pq = q;
120
    return len;
121
}
122
123
#define PUTBITS4(val)\
124
{\
125
    bitbuf |= (val) << bitcnt;\
126
    bitcnt -= 4;\
127
    if (bitcnt < 0) {\
128
        bitcnt = 4;\
129
        *q++ = bitbuf;\
130
        bitbuf = 0;\
131
    }\
132
}
133
134
/* some DVB decoders only implement 4 bits/pixel */
135
132
static int dvb_encode_rle4(uint8_t **pq, int buf_size,
136
                           const uint8_t *bitmap, int linesize,
137
                           int w, int h)
138
{
139
    uint8_t *q, *line_begin;
140
    unsigned int bitbuf;
141
    int bitcnt;
142
    int x, y, len, x1, v, color;
143
144
132
    q = *pq;
145
146
2508
    for(y = 0; y < h; y++) {
147
        // Worst case line is 6 bits per value, + 4 bytes overhead
148
2376
        if (buf_size * 8 < w * 6 + 32)
149
            return AVERROR_BUFFER_TOO_SMALL;
150
2376
        line_begin = q;
151
2376
        *q++ = 0x11;
152
2376
        bitbuf = 0;
153
2376
        bitcnt = 4;
154
155
2376
        x = 0;
156
256530
        while (x < w) {
157
254154
            x1 = x;
158
254154
            color = bitmap[x1++];
159

1992511
            while (x1 < w && bitmap[x1] == color)
160
1738357
                x1++;
161
254154
            len = x1 - x;
162

254154
            if (color == 0 && len == 2) {
163
                PUTBITS4(0);
164
                PUTBITS4(0xd);
165

254154
            } else if (color == 0 && (len >= 3 && len <= 9)) {
166
36
                PUTBITS4(0);
167
36
                PUTBITS4(len - 2);
168

254118
            } else if (len >= 4 && len <= 7) {
169
18199
                PUTBITS4(0);
170
18199
                PUTBITS4(8 + len - 4);
171
18199
                PUTBITS4(color);
172

235919
            } else if (len >= 9 && len <= 24) {
173
7926
                PUTBITS4(0);
174
7926
                PUTBITS4(0xe);
175
7926
                PUTBITS4(len - 9);
176
7926
                PUTBITS4(color);
177
227993
            } else if (len >= 25) {
178
9232
                if (len > 280)
179
1469
                    len = 280;
180
9232
                v = len - 25;
181
9232
                PUTBITS4(0);
182
9232
                PUTBITS4(0xf);
183
9232
                PUTBITS4(v >> 4);
184
9232
                PUTBITS4(v & 0xf);
185
9232
                PUTBITS4(color);
186
            } else {
187
218761
                PUTBITS4(color);
188
218761
                if (color == 0) {
189
                    PUTBITS4(0xc);
190
                }
191
218761
                len = 1;
192
            }
193
254154
            x += len;
194
        }
195
        /* end of line */
196
2376
        PUTBITS4(0);
197
2376
        PUTBITS4(0);
198
2376
        if (bitcnt != 4) {
199
1024
            *q++ = bitbuf;
200
        }
201
2376
        *q++ = 0xf0;
202
2376
        bitmap += linesize;
203
2376
        buf_size -= q - line_begin;
204
    }
205
132
    len = q - *pq;
206
132
    *pq = q;
207
132
    return len;
208
}
209
210
static int dvb_encode_rle8(uint8_t **pq, int buf_size,
211
                           const uint8_t *bitmap, int linesize,
212
                           int w, int h)
213
{
214
    uint8_t *q, *line_begin;
215
    int x, y, len, x1, color;
216
217
    q = *pq;
218
219
    for (y = 0; y < h; y++) {
220
        // Worst case line is 12 bits per value, + 3 bytes overhead
221
        if (buf_size * 8 < w * 12 + 24)
222
            return AVERROR_BUFFER_TOO_SMALL;
223
        line_begin = q;
224
        *q++ = 0x12;
225
226
        x = 0;
227
        while (x < w) {
228
            x1 = x;
229
            color = bitmap[x1++];
230
            while (x1 < w && bitmap[x1] == color)
231
                x1++;
232
            len = x1 - x;
233
            if (len == 1 && color) {
234
                // 00000001 to 11111111           1 pixel in colour x
235
                *q++ = color;
236
            } else {
237
                if (color == 0x00) {
238
                    // 00000000 0LLLLLLL          L pixels (1-127) in colour 0 (L > 0)
239
                    len = FFMIN(len, 127);
240
                    *q++ = 0x00;
241
                    *q++ = len;
242
                } else if (len > 2) {
243
                    // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
244
                    len = FFMIN(len, 127);
245
                    *q++ = 0x00;
246
                    *q++ = 0x80+len;
247
                    *q++ = color;
248
                }
249
                else if (len == 2) {
250
                    *q++ = color;
251
                    *q++ = color;
252
                } else {
253
                    *q++ = color;
254
                    len = 1;
255
                }
256
            }
257
            x += len;
258
        }
259
        /* end of line */
260
        // 00000000 end of 8-bit/pixel_code_string
261
        *q++ = 0x00;
262
        *q++ = 0xf0;
263
        bitmap += linesize;
264
        buf_size -= q - line_begin;
265
    }
266
    len = q - *pq;
267
    *pq = q;
268
    return len;
269
}
270
271
72
static int dvbsub_encode(AVCodecContext *avctx, uint8_t *outbuf, int buf_size,
272
                         const AVSubtitle *h)
273
{
274
72
    DVBSubtitleContext *s = avctx->priv_data;
275
    uint8_t *q, *pseg_len;
276
    int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
277
278
279
72
    q = outbuf;
280
281
72
    page_id = 1;
282
283

72
    if (h->num_rects && !h->rects)
284
        return AVERROR(EINVAL);
285
286

72
    if (avctx->width > 0 && avctx->height > 0) {
287
        if (buf_size < 11)
288
            return AVERROR_BUFFER_TOO_SMALL;
289
        /* display definition segment */
290
        *q++ = 0x0f; /* sync_byte */
291
        *q++ = 0x14; /* segment_type */
292
        bytestream_put_be16(&q, page_id);
293
        pseg_len = q;
294
        q += 2; /* segment length */
295
        *q++ = 0x00; /* dds version number & display window flag */
296
        bytestream_put_be16(&q, avctx->width - 1); /* display width */
297
        bytestream_put_be16(&q, avctx->height - 1); /* display height */
298
        bytestream_put_be16(&pseg_len, q - pseg_len - 2);
299
        buf_size -= 11;
300
    }
301
302
    /* page composition segment */
303
304
72
    if (buf_size < 8 + h->num_rects * 6)
305
        return AVERROR_BUFFER_TOO_SMALL;
306
72
    *q++ = 0x0f; /* sync_byte */
307
72
    *q++ = 0x10; /* segment_type */
308
72
    bytestream_put_be16(&q, page_id);
309
72
    pseg_len = q;
310
72
    q += 2; /* segment length */
311
72
    *q++ = 30; /* page_timeout (seconds) */
312
72
    page_state = 2; /* mode change */
313
    /* page_version = 0 + page_state */
314
72
    *q++ = (s->object_version << 4) | (page_state << 2) | 3;
315
316
138
    for (region_id = 0; region_id < h->num_rects; region_id++) {
317
66
        *q++ = region_id;
318
66
        *q++ = 0xff; /* reserved */
319
66
        bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
320
66
        bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
321
    }
322
323
72
    bytestream_put_be16(&pseg_len, q - pseg_len - 2);
324
72
    buf_size -= 8 + h->num_rects * 6;
325
326
72
    if (h->num_rects) {
327
102
        for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
328
66
            if (buf_size < 6 + h->rects[clut_id]->nb_colors * 6)
329
                return AVERROR_BUFFER_TOO_SMALL;
330
331
            /* CLUT segment */
332
333
66
            if (h->rects[clut_id]->nb_colors <= 4) {
334
                /* 2 bpp, some decoders do not support it correctly */
335
                bpp_index = 0;
336
66
            } else if (h->rects[clut_id]->nb_colors <= 16) {
337
                /* 4 bpp, standard encoding */
338
66
                bpp_index = 1;
339
            } else if (h->rects[clut_id]->nb_colors <= 256) {
340
                /* 8 bpp, standard encoding */
341
                bpp_index = 2;
342
            } else {
343
                return AVERROR(EINVAL);
344
            }
345
346
347
            /* CLUT segment */
348
66
            *q++ = 0x0f; /* sync byte */
349
66
            *q++ = 0x12; /* CLUT definition segment */
350
66
            bytestream_put_be16(&q, page_id);
351
66
            pseg_len = q;
352
66
            q += 2; /* segment length */
353
66
            *q++ = clut_id;
354
66
            *q++ = (0 << 4) | 0xf; /* version = 0 */
355
356
1122
            for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
357
1056
                *q++ = i; /* clut_entry_id */
358
1056
                *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
359
                {
360
                    int a, r, g, b;
361
1056
                    uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
362
1056
                    a = (x >> 24) & 0xff;
363
1056
                    r = (x >> 16) & 0xff;
364
1056
                    g = (x >>  8) & 0xff;
365
1056
                    b = (x >>  0) & 0xff;
366
367
1056
                    *q++ = RGB_TO_Y_CCIR(r, g, b);
368
1056
                    *q++ = RGB_TO_V_CCIR(r, g, b, 0);
369
1056
                    *q++ = RGB_TO_U_CCIR(r, g, b, 0);
370
1056
                    *q++ = 255 - a;
371
                }
372
            }
373
374
66
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
375
66
            buf_size -= 6 + h->rects[clut_id]->nb_colors * 6;
376
        }
377
378
36
        if (buf_size < h->num_rects * 22)
379
            return AVERROR_BUFFER_TOO_SMALL;
380
102
        for (region_id = 0; region_id < h->num_rects; region_id++) {
381
382
            /* region composition segment */
383
384
66
            if (h->rects[region_id]->nb_colors <= 4) {
385
                /* 2 bpp, some decoders do not support it correctly */
386
                bpp_index = 0;
387
66
            } else if (h->rects[region_id]->nb_colors <= 16) {
388
                /* 4 bpp, standard encoding */
389
66
                bpp_index = 1;
390
            } else if (h->rects[region_id]->nb_colors <= 256) {
391
                /* 8 bpp, standard encoding */
392
                bpp_index = 2;
393
            } else {
394
                return AVERROR(EINVAL);
395
            }
396
397
66
            *q++ = 0x0f; /* sync_byte */
398
66
            *q++ = 0x11; /* segment_type */
399
66
            bytestream_put_be16(&q, page_id);
400
66
            pseg_len = q;
401
66
            q += 2; /* segment length */
402
66
            *q++ = region_id;
403
66
            *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
404
66
            bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
405
66
            bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
406
66
            *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
407
66
            *q++ = region_id; /* clut_id == region_id */
408
66
            *q++ = 0; /* 8 bit fill colors */
409
66
            *q++ = 0x03; /* 4 bit and 2 bit fill colors */
410
411
66
            bytestream_put_be16(&q, region_id); /* object_id == region_id */
412
66
            *q++ = (0 << 6) | (0 << 4);
413
66
            *q++ = 0;
414
66
            *q++ = 0xf0;
415
66
            *q++ = 0;
416
417
66
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
418
        }
419
36
        buf_size -= h->num_rects * 22;
420
421
102
        for (object_id = 0; object_id < h->num_rects; object_id++) {
422
            int (*dvb_encode_rle)(uint8_t **pq, int buf_size,
423
                                  const uint8_t *bitmap, int linesize,
424
                                  int w, int h);
425
426
66
            if (buf_size < 13)
427
                return AVERROR_BUFFER_TOO_SMALL;
428
429
            /* bpp_index maths */
430
66
            if (h->rects[object_id]->nb_colors <= 4) {
431
                /* 2 bpp, some decoders do not support it correctly */
432
                dvb_encode_rle = dvb_encode_rle2;
433
66
            } else if (h->rects[object_id]->nb_colors <= 16) {
434
                /* 4 bpp, standard encoding */
435
66
                dvb_encode_rle = dvb_encode_rle4;
436
            } else if (h->rects[object_id]->nb_colors <= 256) {
437
                /* 8 bpp, standard encoding */
438
                dvb_encode_rle = dvb_encode_rle8;
439
            } else {
440
                return AVERROR(EINVAL);
441
            }
442
443
            /* Object Data segment */
444
66
            *q++ = 0x0f; /* sync byte */
445
66
            *q++ = 0x13;
446
66
            bytestream_put_be16(&q, page_id);
447
66
            pseg_len = q;
448
66
            q += 2; /* segment length */
449
450
66
            bytestream_put_be16(&q, object_id);
451
66
            *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
452
                                                                       object_coding_method,
453
                                                                       non_modifying_color_flag */
454
            {
455
                uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
456
                int ret;
457
458
66
                ptop_field_len = q;
459
66
                q += 2;
460
66
                pbottom_field_len = q;
461
66
                q += 2;
462
66
                buf_size -= 13;
463
464
66
                top_ptr = q;
465
66
                ret = dvb_encode_rle(&q, buf_size,
466
66
                                     h->rects[object_id]->data[0],
467
66
                                     h->rects[object_id]->w * 2,
468
66
                                     h->rects[object_id]->w,
469
66
                                     h->rects[object_id]->h >> 1);
470
66
                if (ret < 0)
471
                    return ret;
472
66
                buf_size -= ret;
473
66
                bottom_ptr = q;
474
66
                ret = dvb_encode_rle(&q, buf_size,
475
66
                                     h->rects[object_id]->data[0] + h->rects[object_id]->w,
476
66
                                     h->rects[object_id]->w * 2,
477
66
                                     h->rects[object_id]->w,
478
66
                                     h->rects[object_id]->h >> 1);
479
66
                if (ret < 0)
480
                    return ret;
481
66
                buf_size -= ret;
482
483
66
                bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
484
66
                bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
485
            }
486
487
66
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
488
        }
489
    }
490
491
    /* end of display set segment */
492
493
72
    if (buf_size < 6)
494
        return AVERROR_BUFFER_TOO_SMALL;
495
72
    *q++ = 0x0f; /* sync_byte */
496
72
    *q++ = 0x80; /* segment_type */
497
72
    bytestream_put_be16(&q, page_id);
498
72
    pseg_len = q;
499
72
    q += 2; /* segment length */
500
501
72
    bytestream_put_be16(&pseg_len, q - pseg_len - 2);
502
72
    buf_size -= 6;
503
504
72
    s->object_version = (s->object_version + 1) & 0xf;
505
72
    return q - outbuf;
506
}
507
508
AVCodec ff_dvbsub_encoder = {
509
    .name           = "dvbsub",
510
    .long_name      = NULL_IF_CONFIG_SMALL("DVB subtitles"),
511
    .type           = AVMEDIA_TYPE_SUBTITLE,
512
    .id             = AV_CODEC_ID_DVB_SUBTITLE,
513
    .priv_data_size = sizeof(DVBSubtitleContext),
514
    .encode_sub     = dvbsub_encode,
515
};