dvbsub.c 14 KB
Newer Older
1
/*
2
 * DVB subtitle encoding
3
 * Copyright (c) 2005 Fabrice Bellard
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * 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
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

typedef struct DVBSubtitleContext {
    int object_version;
} DVBSubtitleContext;

#define PUTBITS2(val)\
{\
    bitbuf |= (val) << bitcnt;\
    bitcnt -= 2;\
    if (bitcnt < 0) {\
        bitcnt = 6;\
        *q++ = bitbuf;\
        bitbuf = 0;\
    }\
}

static void dvb_encode_rle2(uint8_t **pq,
                            const uint8_t *bitmap, int linesize,
                            int w, int h)
{
    uint8_t *q;
    unsigned int bitbuf;
    int bitcnt;
    int x, y, len, x1, v, color;

    q = *pq;
50

51 52 53 54
    for(y = 0; y < h; y++) {
        *q++ = 0x10;
        bitbuf = 0;
        bitcnt = 6;
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
        x = 0;
        while (x < w) {
            x1 = x;
            color = bitmap[x1++];
            while (x1 < w && bitmap[x1] == color)
                x1++;
            len = x1 - x;
            if (color == 0 && len == 2) {
                PUTBITS2(0);
                PUTBITS2(0);
                PUTBITS2(1);
            } else if (len >= 3 && len <= 10) {
                v = len - 3;
                PUTBITS2(0);
                PUTBITS2((v >> 2) | 2);
                PUTBITS2(v & 3);
                PUTBITS2(color);
            } else if (len >= 12 && len <= 27) {
                v = len - 12;
                PUTBITS2(0);
                PUTBITS2(0);
                PUTBITS2(2);
                PUTBITS2(v >> 2);
                PUTBITS2(v & 3);
                PUTBITS2(color);
            } else if (len >= 29) {
                /* length = 29 ... 284 */
                if (len > 284)
                    len = 284;
                v = len - 29;
                PUTBITS2(0);
                PUTBITS2(0);
                PUTBITS2(3);
                PUTBITS2((v >> 6));
                PUTBITS2((v >> 4) & 3);
                PUTBITS2((v >> 2) & 3);
                PUTBITS2(v & 3);
                PUTBITS2(color);
            } else {
                PUTBITS2(color);
                if (color == 0) {
                    PUTBITS2(1);
                }
                len = 1;
            }
            x += len;
        }
        /* end of line */
        PUTBITS2(0);
        PUTBITS2(0);
        PUTBITS2(0);
        if (bitcnt != 6) {
            *q++ = bitbuf;
        }
        *q++ = 0xf0;
        bitmap += linesize;
    }
    *pq = q;
}

#define PUTBITS4(val)\
{\
    bitbuf |= (val) << bitcnt;\
    bitcnt -= 4;\
    if (bitcnt < 0) {\
        bitcnt = 4;\
        *q++ = bitbuf;\
        bitbuf = 0;\
    }\
}

/* some DVB decoders only implement 4 bits/pixel */
static void dvb_encode_rle4(uint8_t **pq,
                            const uint8_t *bitmap, int linesize,
                            int w, int h)
{
    uint8_t *q;
    unsigned int bitbuf;
    int bitcnt;
    int x, y, len, x1, v, color;

    q = *pq;
138

139 140 141 142
    for(y = 0; y < h; y++) {
        *q++ = 0x11;
        bitbuf = 0;
        bitcnt = 4;
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
        x = 0;
        while (x < w) {
            x1 = x;
            color = bitmap[x1++];
            while (x1 < w && bitmap[x1] == color)
                x1++;
            len = x1 - x;
            if (color == 0 && len == 2) {
                PUTBITS4(0);
                PUTBITS4(0xd);
            } else if (color == 0 && (len >= 3 && len <= 9)) {
                PUTBITS4(0);
                PUTBITS4(len - 2);
            } else if (len >= 4 && len <= 7) {
                PUTBITS4(0);
                PUTBITS4(8 + len - 4);
                PUTBITS4(color);
            } else if (len >= 9 && len <= 24) {
                PUTBITS4(0);
                PUTBITS4(0xe);
                PUTBITS4(len - 9);
                PUTBITS4(color);
            } else if (len >= 25) {
                if (len > 280)
                    len = 280;
                v = len - 25;
                PUTBITS4(0);
                PUTBITS4(0xf);
                PUTBITS4(v >> 4);
                PUTBITS4(v & 0xf);
                PUTBITS4(color);
            } else {
                PUTBITS4(color);
                if (color == 0) {
                    PUTBITS4(0xc);
                }
                len = 1;
            }
            x += len;
        }
        /* end of line */
        PUTBITS4(0);
        PUTBITS4(0);
        if (bitcnt != 4) {
            *q++ = bitbuf;
        }
        *q++ = 0xf0;
        bitmap += linesize;
    }
    *pq = q;
}

196 197 198 199 200
static void dvb_encode_rle8(uint8_t **pq,
                            const uint8_t *bitmap, int linesize,
                            int w, int h)
{
    uint8_t *q;
201
    int x, y, len, x1, color;
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

    q = *pq;

    for (y = 0; y < h; y++) {
        *q++ = 0x12;

        x = 0;
        while (x < w) {
            x1 = x;
            color = bitmap[x1++];
            while (x1 < w && bitmap[x1] == color)
                x1++;
            len = x1 - x;
            if (len == 1 && color) {
                // 00000001 to 11111111           1 pixel in colour x
                *q++ = color;
            } else {
                if (color == 0x00) {
                    // 00000000 0LLLLLLL          L pixels (1-127) in colour 0 (L > 0)
                    len = FFMIN(len, 127);
                    *q++ = 0x00;
                    *q++ = len;
                } else if (len > 2) {
                    // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
                    len = FFMIN(len, 127);
                    *q++ = 0x00;
                    *q++ = 0x80+len;
                    *q++ = color;
                }
                else if (len == 2) {
                    *q++ = color;
                    *q++ = color;
                } else {
                    *q++ = color;
                    len = 1;
                }
            }
            x += len;
        }
        /* end of line */
242
        // 00000000 end of 8-bit/pixel_code_string
243
        *q++ = 0x00;
244
        *q++ = 0xf0;
245 246 247 248 249
        bitmap += linesize;
    }
    *pq = q;
}

250
static int encode_dvb_subtitles(DVBSubtitleContext *s,
251
                                uint8_t *outbuf, const AVSubtitle *h)
252 253 254 255 256 257 258 259
{
    uint8_t *q, *pseg_len;
    int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;


    q = outbuf;

    page_id = 1;
260

261
    if (h->num_rects && !h->rects)
262 263 264 265 266 267
        return -1;

    /* page composition segment */

    *q++ = 0x0f; /* sync_byte */
    *q++ = 0x10; /* segment_type */
268
    bytestream_put_be16(&q, page_id);
269 270 271
    pseg_len = q;
    q += 2; /* segment length */
    *q++ = 30; /* page_timeout (seconds) */
272
    page_state = 2; /* mode change */
273
    /* page_version = 0 + page_state */
274
    *q++ = (s->object_version << 4) | (page_state << 2) | 3;
275 276 277 278

    for (region_id = 0; region_id < h->num_rects; region_id++) {
        *q++ = region_id;
        *q++ = 0xff; /* reserved */
279 280
        bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
        bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
281
    }
282

283
    bytestream_put_be16(&pseg_len, q - pseg_len - 2);
284

285
    if (h->num_rects) {
286 287 288 289
        for (clut_id = 0; clut_id < h->num_rects; clut_id++) {

            /* CLUT segment */

290
            if (h->rects[clut_id]->nb_colors <= 4) {
291 292
                /* 2 bpp, some decoders do not support it correctly */
                bpp_index = 0;
293
            } else if (h->rects[clut_id]->nb_colors <= 16) {
294 295
                /* 4 bpp, standard encoding */
                bpp_index = 1;
296 297 298
            } else if (h->rects[clut_id]->nb_colors <= 256) {
                /* 8 bpp, standard encoding */
                bpp_index = 2;
299 300 301 302
            } else {
                return -1;
            }

JULIAN GARDNER's avatar
JULIAN GARDNER committed
303 304

            /* CLUT segment */
305 306
            *q++ = 0x0f; /* sync byte */
            *q++ = 0x12; /* CLUT definition segment */
307
            bytestream_put_be16(&q, page_id);
308 309 310 311 312
            pseg_len = q;
            q += 2; /* segment length */
            *q++ = clut_id;
            *q++ = (0 << 4) | 0xf; /* version = 0 */

313
            for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
314 315 316 317
                *q++ = i; /* clut_entry_id */
                *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
                {
                    int a, r, g, b;
318
                    uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
319 320 321 322
                    a = (x >> 24) & 0xff;
                    r = (x >> 16) & 0xff;
                    g = (x >>  8) & 0xff;
                    b = (x >>  0) & 0xff;
323 324 325 326 327 328 329 330

                    *q++ = RGB_TO_Y_CCIR(r, g, b);
                    *q++ = RGB_TO_V_CCIR(r, g, b, 0);
                    *q++ = RGB_TO_U_CCIR(r, g, b, 0);
                    *q++ = 255 - a;
                }
            }

331
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
332 333 334 335 336 337
        }
    }

    for (region_id = 0; region_id < h->num_rects; region_id++) {

        /* region composition segment */
338

339
        if (h->rects[region_id]->nb_colors <= 4) {
340 341
            /* 2 bpp, some decoders do not support it correctly */
            bpp_index = 0;
342
        } else if (h->rects[region_id]->nb_colors <= 16) {
343 344
            /* 4 bpp, standard encoding */
            bpp_index = 1;
345 346 347
        } else if (h->rects[region_id]->nb_colors <= 256) {
            /* 8 bpp, standard encoding */
            bpp_index = 2;
348 349 350 351 352 353
        } else {
            return -1;
        }

        *q++ = 0x0f; /* sync_byte */
        *q++ = 0x11; /* segment_type */
354
        bytestream_put_be16(&q, page_id);
355 356
        pseg_len = q;
        q += 2; /* segment length */
357 358
        *q++ = region_id;
        *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
359 360
        bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
        bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
361 362 363 364 365
        *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
        *q++ = region_id; /* clut_id == region_id */
        *q++ = 0; /* 8 bit fill colors */
        *q++ = 0x03; /* 4 bit and 2 bit fill colors */

366 367 368 369 370
        bytestream_put_be16(&q, region_id); /* object_id == region_id */
        *q++ = (0 << 6) | (0 << 4);
        *q++ = 0;
        *q++ = 0xf0;
        *q++ = 0;
371

372
        bytestream_put_be16(&pseg_len, q - pseg_len - 2);
373 374
    }

375
    if (h->num_rects) {
376

377
        for (object_id = 0; object_id < h->num_rects; object_id++) {
378 379 380
            void (*dvb_encode_rle)(uint8_t **pq,
                                    const uint8_t *bitmap, int linesize,
                                    int w, int h);
381

382
            /* bpp_index maths */
383
            if (h->rects[object_id]->nb_colors <= 4) {
384
                /* 2 bpp, some decoders do not support it correctly */
385
                dvb_encode_rle = dvb_encode_rle2;
386
            } else if (h->rects[object_id]->nb_colors <= 16) {
387
                /* 4 bpp, standard encoding */
388
                dvb_encode_rle = dvb_encode_rle4;
389 390 391
            } else if (h->rects[object_id]->nb_colors <= 256) {
                /* 8 bpp, standard encoding */
                dvb_encode_rle = dvb_encode_rle8;
392 393 394
            } else {
                return -1;
            }
395

JULIAN GARDNER's avatar
JULIAN GARDNER committed
396
            /* Object Data segment */
397 398
            *q++ = 0x0f; /* sync byte */
            *q++ = 0x13;
399
            bytestream_put_be16(&q, page_id);
400 401
            pseg_len = q;
            q += 2; /* segment length */
402

403
            bytestream_put_be16(&q, object_id);
404 405 406 407 408
            *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
                                                                       onject_coding_method,
                                                                       non_modifying_color_flag */
            {
                uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
409

410 411 412 413 414 415
                ptop_field_len = q;
                q += 2;
                pbottom_field_len = q;
                q += 2;

                top_ptr = q;
416
                dvb_encode_rle(&q, h->rects[object_id]->data[0], h->rects[object_id]->w * 2,
417
                                    h->rects[object_id]->w, h->rects[object_id]->h >> 1);
418
                bottom_ptr = q;
419
                dvb_encode_rle(&q, h->rects[object_id]->data[0] + h->rects[object_id]->w,
420 421
                                    h->rects[object_id]->w * 2, h->rects[object_id]->w,
                                    h->rects[object_id]->h >> 1);
422

423 424
                bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
                bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
425 426
            }

427
            bytestream_put_be16(&pseg_len, q - pseg_len - 2);
428
        }
429 430 431 432 433 434
    }

    /* end of display set segment */

    *q++ = 0x0f; /* sync_byte */
    *q++ = 0x80; /* segment_type */
435
    bytestream_put_be16(&q, page_id);
436 437 438
    pseg_len = q;
    q += 2; /* segment length */

439
    bytestream_put_be16(&pseg_len, q - pseg_len - 2);
440 441 442 443 444 445

    s->object_version = (s->object_version + 1) & 0xf;
    return q - outbuf;
}

static int dvbsub_encode(AVCodecContext *avctx,
446 447
                         unsigned char *buf, int buf_size,
                         const AVSubtitle *sub)
448 449 450 451 452 453 454 455
{
    DVBSubtitleContext *s = avctx->priv_data;
    int ret;

    ret = encode_dvb_subtitles(s, buf, sub);
    return ret;
}

456
AVCodec ff_dvbsub_encoder = {
457
    .name           = "dvbsub",
458
    .long_name      = NULL_IF_CONFIG_SMALL("DVB subtitles"),
459
    .type           = AVMEDIA_TYPE_SUBTITLE,
460
    .id             = AV_CODEC_ID_DVB_SUBTITLE,
461
    .priv_data_size = sizeof(DVBSubtitleContext),
462
    .encode_sub     = dvbsub_encode,
463
};