Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/dvbsubenc.c
Line
Count
Source (jump to first uncovered line)
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 "codec_internal.h"
24
#include "libavutil/colorspace.h"
25
26
typedef struct DVBSubtitleContext {
27
    int object_version;
28
} DVBSubtitleContext;
29
30
0
#define PUTBITS2(val)\
31
0
{\
32
0
    bitbuf |= (val) << bitcnt;\
33
0
    bitcnt -= 2;\
34
0
    if (bitcnt < 0) {\
35
0
        bitcnt = 6;\
36
0
        *q++ = bitbuf;\
37
0
        bitbuf = 0;\
38
0
    }\
39
0
}
40
41
static int dvb_encode_rle2(uint8_t **pq, int buf_size,
42
                           const uint8_t *bitmap, int linesize,
43
                           int w, int h)
44
0
{
45
0
    uint8_t *q, *line_begin;
46
0
    unsigned int bitbuf;
47
0
    int bitcnt;
48
0
    int x, y, len, x1, v, color;
49
50
0
    q = *pq;
51
52
0
    for(y = 0; y < h; y++) {
53
        // Worst case line is 3 bits per value + 4 bytes overhead
54
0
        if (buf_size * 8 < w * 3 + 32)
55
0
            return AVERROR_BUFFER_TOO_SMALL;
56
0
        line_begin = q;
57
0
        *q++ = 0x10;
58
0
        bitbuf = 0;
59
0
        bitcnt = 6;
60
61
0
        x = 0;
62
0
        while (x < w) {
63
0
            x1 = x;
64
0
            color = bitmap[x1++];
65
0
            while (x1 < w && bitmap[x1] == color)
66
0
                x1++;
67
0
            len = x1 - x;
68
0
            if (color == 0 && len == 2) {
69
0
                PUTBITS2(0);
70
0
                PUTBITS2(0);
71
0
                PUTBITS2(1);
72
0
            } else if (len >= 3 && len <= 10) {
73
0
                v = len - 3;
74
0
                PUTBITS2(0);
75
0
                PUTBITS2((v >> 2) | 2);
76
0
                PUTBITS2(v & 3);
77
0
                PUTBITS2(color);
78
0
            } else if (len >= 12 && len <= 27) {
79
0
                v = len - 12;
80
0
                PUTBITS2(0);
81
0
                PUTBITS2(0);
82
0
                PUTBITS2(2);
83
0
                PUTBITS2(v >> 2);
84
0
                PUTBITS2(v & 3);
85
0
                PUTBITS2(color);
86
0
            } else if (len >= 29) {
87
                /* length = 29 ... 284 */
88
0
                if (len > 284)
89
0
                    len = 284;
90
0
                v = len - 29;
91
0
                PUTBITS2(0);
92
0
                PUTBITS2(0);
93
0
                PUTBITS2(3);
94
0
                PUTBITS2((v >> 6));
95
0
                PUTBITS2((v >> 4) & 3);
96
0
                PUTBITS2((v >> 2) & 3);
97
0
                PUTBITS2(v & 3);
98
0
                PUTBITS2(color);
99
0
            } else {
100
0
                PUTBITS2(color);
101
0
                if (color == 0) {
102
0
                    PUTBITS2(1);
103
0
                }
104
0
                len = 1;
105
0
            }
106
0
            x += len;
107
0
        }
108
        /* end of line */
109
0
        PUTBITS2(0);
110
0
        PUTBITS2(0);
111
0
        PUTBITS2(0);
112
0
        if (bitcnt != 6) {
113
0
            *q++ = bitbuf;
114
0
        }
115
0
        *q++ = 0xf0;
116
0
        bitmap += linesize;
117
0
        buf_size -= q - line_begin;
118
0
    }
119
0
    len = q - *pq;
120
0
    *pq = q;
121
0
    return len;
122
0
}
123
124
0
#define PUTBITS4(val)\
125
0
{\
126
0
    bitbuf |= (val) << bitcnt;\
127
0
    bitcnt -= 4;\
128
0
    if (bitcnt < 0) {\
129
0
        bitcnt = 4;\
130
0
        *q++ = bitbuf;\
131
0
        bitbuf = 0;\
132
0
    }\
133
0
}
134
135
/* some DVB decoders only implement 4 bits/pixel */
136
static int dvb_encode_rle4(uint8_t **pq, int buf_size,
137
                           const uint8_t *bitmap, int linesize,
138
                           int w, int h)
139
0
{
140
0
    uint8_t *q, *line_begin;
141
0
    unsigned int bitbuf;
142
0
    int bitcnt;
143
0
    int x, y, len, x1, v, color;
144
145
0
    q = *pq;
146
147
0
    for(y = 0; y < h; y++) {
148
        // Worst case line is 6 bits per value, + 4 bytes overhead
149
0
        if (buf_size * 8 < w * 6 + 32)
150
0
            return AVERROR_BUFFER_TOO_SMALL;
151
0
        line_begin = q;
152
0
        *q++ = 0x11;
153
0
        bitbuf = 0;
154
0
        bitcnt = 4;
155
156
0
        x = 0;
157
0
        while (x < w) {
158
0
            x1 = x;
159
0
            color = bitmap[x1++];
160
0
            while (x1 < w && bitmap[x1] == color)
161
0
                x1++;
162
0
            len = x1 - x;
163
0
            if (color == 0 && len == 2) {
164
0
                PUTBITS4(0);
165
0
                PUTBITS4(0xd);
166
0
            } else if (color == 0 && (len >= 3 && len <= 9)) {
167
0
                PUTBITS4(0);
168
0
                PUTBITS4(len - 2);
169
0
            } else if (len >= 4 && len <= 7) {
170
0
                PUTBITS4(0);
171
0
                PUTBITS4(8 + len - 4);
172
0
                PUTBITS4(color);
173
0
            } else if (len >= 9 && len <= 24) {
174
0
                PUTBITS4(0);
175
0
                PUTBITS4(0xe);
176
0
                PUTBITS4(len - 9);
177
0
                PUTBITS4(color);
178
0
            } else if (len >= 25) {
179
0
                if (len > 280)
180
0
                    len = 280;
181
0
                v = len - 25;
182
0
                PUTBITS4(0);
183
0
                PUTBITS4(0xf);
184
0
                PUTBITS4(v >> 4);
185
0
                PUTBITS4(v & 0xf);
186
0
                PUTBITS4(color);
187
0
            } else {
188
0
                PUTBITS4(color);
189
0
                if (color == 0) {
190
0
                    PUTBITS4(0xc);
191
0
                }
192
0
                len = 1;
193
0
            }
194
0
            x += len;
195
0
        }
196
        /* end of line */
197
0
        PUTBITS4(0);
198
0
        PUTBITS4(0);
199
0
        if (bitcnt != 4) {
200
0
            *q++ = bitbuf;
201
0
        }
202
0
        *q++ = 0xf0;
203
0
        bitmap += linesize;
204
0
        buf_size -= q - line_begin;
205
0
    }
206
0
    len = q - *pq;
207
0
    *pq = q;
208
0
    return len;
209
0
}
210
211
static int dvb_encode_rle8(uint8_t **pq, int buf_size,
212
                           const uint8_t *bitmap, int linesize,
213
                           int w, int h)
214
0
{
215
0
    uint8_t *q, *line_begin;
216
0
    int x, y, len, x1, color;
217
218
0
    q = *pq;
219
220
0
    for (y = 0; y < h; y++) {
221
        // Worst case line is 12 bits per value, + 3 bytes overhead
222
0
        if (buf_size * 8 < w * 12 + 24)
223
0
            return AVERROR_BUFFER_TOO_SMALL;
224
0
        line_begin = q;
225
0
        *q++ = 0x12;
226
227
0
        x = 0;
228
0
        while (x < w) {
229
0
            x1 = x;
230
0
            color = bitmap[x1++];
231
0
            while (x1 < w && bitmap[x1] == color)
232
0
                x1++;
233
0
            len = x1 - x;
234
0
            if (len == 1 && color) {
235
                // 00000001 to 11111111           1 pixel in colour x
236
0
                *q++ = color;
237
0
            } else {
238
0
                if (color == 0x00) {
239
                    // 00000000 0LLLLLLL          L pixels (1-127) in colour 0 (L > 0)
240
0
                    len = FFMIN(len, 127);
241
0
                    *q++ = 0x00;
242
0
                    *q++ = len;
243
0
                } else if (len > 2) {
244
                    // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
245
0
                    len = FFMIN(len, 127);
246
0
                    *q++ = 0x00;
247
0
                    *q++ = 0x80+len;
248
0
                    *q++ = color;
249
0
                }
250
0
                else if (len == 2) {
251
0
                    *q++ = color;
252
0
                    *q++ = color;
253
0
                } else {
254
0
                    *q++ = color;
255
0
                    len = 1;
256
0
                }
257
0
            }
258
0
            x += len;
259
0
        }
260
        /* end of line */
261
        // 00000000 end of 8-bit/pixel_code_string
262
0
        *q++ = 0x00;
263
0
        *q++ = 0xf0;
264
0
        bitmap += linesize;
265
0
        buf_size -= q - line_begin;
266
0
    }
267
0
    len = q - *pq;
268
0
    *pq = q;
269
0
    return len;
270
0
}
271
272
static int dvbsub_encode(AVCodecContext *avctx, uint8_t *outbuf, int buf_size,
273
                         const AVSubtitle *h)
274
0
{
275
0
    DVBSubtitleContext *s = avctx->priv_data;
276
0
    uint8_t *q, *pseg_len;
277
0
    int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
278
279
280
0
    q = outbuf;
281
282
0
    page_id = 1;
283
284
0
    if (h->num_rects && !h->rects)
285
0
        return AVERROR(EINVAL);
286
287
0
    if (avctx->width > 0 && avctx->height > 0) {
288
0
        if (buf_size < 11)
289
0
            return AVERROR_BUFFER_TOO_SMALL;
290
        /* display definition segment */
291
0
        *q++ = 0x0f; /* sync_byte */
292
0
        *q++ = 0x14; /* segment_type */
293
0
        bytestream_put_be16(&q, page_id);
294
0
        pseg_len = q;
295
0
        q += 2; /* segment length */
296
0
        *q++ = 0x00; /* dds version number & display window flag */
297
0
        bytestream_put_be16(&q, avctx->width - 1); /* display width */
298
0
        bytestream_put_be16(&q, avctx->height - 1); /* display height */
299
0
        bytestream_put_be16(&pseg_len, q - pseg_len - 2);
300
0
        buf_size -= 11;
301
0
    }
302
303
    /* page composition segment */
304
305
0
    if (buf_size < 8 + h->num_rects * 6)
306
0
        return AVERROR_BUFFER_TOO_SMALL;
307
0
    *q++ = 0x0f; /* sync_byte */
308
0
    *q++ = 0x10; /* segment_type */
309
0
    bytestream_put_be16(&q, page_id);
310
0
    pseg_len = q;
311
0
    q += 2; /* segment length */
312
0
    *q++ = 30; /* page_timeout (seconds) */
313
0
    page_state = 2; /* mode change */
314
    /* page_version = 0 + page_state */
315
0
    *q++ = (s->object_version << 4) | (page_state << 2) | 3;
316
317
0
    for (region_id = 0; region_id < h->num_rects; region_id++) {
318
0
        *q++ = region_id;
319
0
        *q++ = 0xff; /* reserved */
320
0
        bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
321
0
        bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
322
0
    }
323
324
0
    bytestream_put_be16(&pseg_len, q - pseg_len - 2);
325
0
    buf_size -= 8 + h->num_rects * 6;
326
327
0
    if (h->num_rects) {
328
0
        for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
329
0
            if (buf_size < 6 + h->rects[clut_id]->nb_colors * 6)
330
0
                return AVERROR_BUFFER_TOO_SMALL;
331
332
            /* CLUT segment */
333
334
0
            if (h->rects[clut_id]->nb_colors <= 4) {
335
                /* 2 bpp, some decoders do not support it correctly */
336
0
                bpp_index = 0;
337
0
            } else if (h->rects[clut_id]->nb_colors <= 16) {
338
                /* 4 bpp, standard encoding */
339
0
                bpp_index = 1;
340
0
            } else if (h->rects[clut_id]->nb_colors <= 256) {
341
                /* 8 bpp, standard encoding */
342
0
                bpp_index = 2;
343
0
            } else {
344
0
                return AVERROR(EINVAL);
345
0
            }
346
347
348
            /* CLUT segment */
349
0
            *q++ = 0x0f; /* sync byte */
350
0
            *q++ = 0x12; /* CLUT definition segment */
351
0
            bytestream_put_be16(&q, page_id);
352
0
            pseg_len = q;
353
0
            q += 2; /* segment length */
354
0
            *q++ = clut_id;
355
0
            *q++ = (0 << 4) | 0xf; /* version = 0 */
356
357
0
            for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
358
0
                *q++ = i; /* clut_entry_id */
359
0
                *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
360
0
                {
361
0
                    int a, r, g, b;
362
0
                    uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
363
0
                    a = (x >> 24) & 0xff;
364
0
                    r = (x >> 16) & 0xff;
365
0
                    g = (x >>  8) & 0xff;
366
0
                    b = (x >>  0) & 0xff;
367
368
0
                    *q++ = RGB_TO_Y_CCIR(r, g, b);
369
0
                    *q++ = RGB_TO_V_CCIR(r, g, b, 0);
370
0
                    *q++ = RGB_TO_U_CCIR(r, g, b, 0);
371
0
                    *q++ = 255 - a;
372
0
                }
373
0
            }
374
375
0
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
376
0
            buf_size -= 6 + h->rects[clut_id]->nb_colors * 6;
377
0
        }
378
379
0
        if (buf_size < h->num_rects * 22)
380
0
            return AVERROR_BUFFER_TOO_SMALL;
381
0
        for (region_id = 0; region_id < h->num_rects; region_id++) {
382
383
            /* region composition segment */
384
385
0
            if (h->rects[region_id]->nb_colors <= 4) {
386
                /* 2 bpp, some decoders do not support it correctly */
387
0
                bpp_index = 0;
388
0
            } else if (h->rects[region_id]->nb_colors <= 16) {
389
                /* 4 bpp, standard encoding */
390
0
                bpp_index = 1;
391
0
            } else if (h->rects[region_id]->nb_colors <= 256) {
392
                /* 8 bpp, standard encoding */
393
0
                bpp_index = 2;
394
0
            } else {
395
0
                return AVERROR(EINVAL);
396
0
            }
397
398
0
            *q++ = 0x0f; /* sync_byte */
399
0
            *q++ = 0x11; /* segment_type */
400
0
            bytestream_put_be16(&q, page_id);
401
0
            pseg_len = q;
402
0
            q += 2; /* segment length */
403
0
            *q++ = region_id;
404
0
            *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
405
0
            bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
406
0
            bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
407
0
            *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
408
0
            *q++ = region_id; /* clut_id == region_id */
409
0
            *q++ = 0; /* 8 bit fill colors */
410
0
            *q++ = 0x03; /* 4 bit and 2 bit fill colors */
411
412
0
            bytestream_put_be16(&q, region_id); /* object_id == region_id */
413
0
            *q++ = (0 << 6) | (0 << 4);
414
0
            *q++ = 0;
415
0
            *q++ = 0xf0;
416
0
            *q++ = 0;
417
418
0
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
419
0
        }
420
0
        buf_size -= h->num_rects * 22;
421
422
0
        for (object_id = 0; object_id < h->num_rects; object_id++) {
423
0
            int (*dvb_encode_rle)(uint8_t **pq, int buf_size,
424
0
                                  const uint8_t *bitmap, int linesize,
425
0
                                  int w, int h);
426
427
0
            if (buf_size < 13)
428
0
                return AVERROR_BUFFER_TOO_SMALL;
429
430
            /* bpp_index maths */
431
0
            if (h->rects[object_id]->nb_colors <= 4) {
432
                /* 2 bpp, some decoders do not support it correctly */
433
0
                dvb_encode_rle = dvb_encode_rle2;
434
0
            } else if (h->rects[object_id]->nb_colors <= 16) {
435
                /* 4 bpp, standard encoding */
436
0
                dvb_encode_rle = dvb_encode_rle4;
437
0
            } else if (h->rects[object_id]->nb_colors <= 256) {
438
                /* 8 bpp, standard encoding */
439
0
                dvb_encode_rle = dvb_encode_rle8;
440
0
            } else {
441
0
                return AVERROR(EINVAL);
442
0
            }
443
444
            /* Object Data segment */
445
0
            *q++ = 0x0f; /* sync byte */
446
0
            *q++ = 0x13;
447
0
            bytestream_put_be16(&q, page_id);
448
0
            pseg_len = q;
449
0
            q += 2; /* segment length */
450
451
0
            bytestream_put_be16(&q, object_id);
452
0
            *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
453
                                                                       object_coding_method,
454
                                                                       non_modifying_color_flag */
455
0
            {
456
0
                uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
457
0
                int ret;
458
459
0
                ptop_field_len = q;
460
0
                q += 2;
461
0
                pbottom_field_len = q;
462
0
                q += 2;
463
0
                buf_size -= 13;
464
465
0
                top_ptr = q;
466
0
                ret = dvb_encode_rle(&q, buf_size,
467
0
                                     h->rects[object_id]->data[0],
468
0
                                     h->rects[object_id]->w * 2,
469
0
                                     h->rects[object_id]->w,
470
0
                                     h->rects[object_id]->h >> 1);
471
0
                if (ret < 0)
472
0
                    return ret;
473
0
                buf_size -= ret;
474
0
                bottom_ptr = q;
475
0
                ret = dvb_encode_rle(&q, buf_size,
476
0
                                     h->rects[object_id]->data[0] + h->rects[object_id]->w,
477
0
                                     h->rects[object_id]->w * 2,
478
0
                                     h->rects[object_id]->w,
479
0
                                     h->rects[object_id]->h >> 1);
480
0
                if (ret < 0)
481
0
                    return ret;
482
0
                buf_size -= ret;
483
484
0
                bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
485
0
                bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
486
0
            }
487
488
0
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
489
0
        }
490
0
    }
491
492
    /* end of display set segment */
493
494
0
    if (buf_size < 6)
495
0
        return AVERROR_BUFFER_TOO_SMALL;
496
0
    *q++ = 0x0f; /* sync_byte */
497
0
    *q++ = 0x80; /* segment_type */
498
0
    bytestream_put_be16(&q, page_id);
499
0
    pseg_len = q;
500
0
    q += 2; /* segment length */
501
502
0
    bytestream_put_be16(&pseg_len, q - pseg_len - 2);
503
0
    buf_size -= 6;
504
505
0
    s->object_version = (s->object_version + 1) & 0xf;
506
0
    return q - outbuf;
507
0
}
508
509
const FFCodec ff_dvbsub_encoder = {
510
    .p.name         = "dvbsub",
511
    CODEC_LONG_NAME("DVB subtitles"),
512
    .p.type         = AVMEDIA_TYPE_SUBTITLE,
513
    .p.id           = AV_CODEC_ID_DVB_SUBTITLE,
514
    .priv_data_size = sizeof(DVBSubtitleContext),
515
    FF_CODEC_ENCODE_SUB_CB(dvbsub_encode),
516
};