2 * DVB subtitle encoding
3 * Copyright (c) 2005 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "bytestream.h"
23 #include "libavutil/colorspace.h"
25 typedef struct DVBSubtitleContext {
30 #define PUTBITS2(val)\
32 bitbuf |= (val) << bitcnt;\
41 static void dvb_encode_rle2(uint8_t **pq,
42 const uint8_t *bitmap, int linesize,
48 int x, y, len, x1, v, color;
52 for(y = 0; y < h; y++) {
61 while (x1 < w && bitmap[x1] == color)
64 if (color == 0 && len == 2) {
68 } else if (len >= 3 && len <= 10) {
71 PUTBITS2((v >> 2) | 2);
74 } else if (len >= 12 && len <= 27) {
82 } else if (len >= 29) {
83 /* length = 29 ... 284 */
91 PUTBITS2((v >> 4) & 3);
92 PUTBITS2((v >> 2) & 3);
117 #define PUTBITS4(val)\
119 bitbuf |= (val) << bitcnt;\
128 /* some DVB decoders only implement 4 bits/pixel */
129 static void dvb_encode_rle4(uint8_t **pq,
130 const uint8_t *bitmap, int linesize,
136 int x, y, len, x1, v, color;
140 for(y = 0; y < h; y++) {
148 color = bitmap[x1++];
149 while (x1 < w && bitmap[x1] == color)
152 if (color == 0 && len == 2) {
155 } else if (color == 0 && (len >= 3 && len <= 9)) {
158 } else if (len >= 4 && len <= 7) {
160 PUTBITS4(8 + len - 4);
162 } else if (len >= 9 && len <= 24) {
167 } else if (len >= 25) {
197 static void dvb_encode_rle8(uint8_t **pq,
198 const uint8_t *bitmap, int linesize,
202 int x, y, len, x1, color;
206 for (y = 0; y < h; y++) {
212 color = bitmap[x1++];
213 while (x1 < w && bitmap[x1] == color)
216 if (len == 1 && color) {
217 // 00000001 to 11111111 1 pixel in colour x
221 // 00000000 0LLLLLLL L pixels (1-127) in colour 0 (L > 0)
222 len = FFMIN(len, 127);
225 } else if (len > 2) {
226 // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
227 len = FFMIN(len, 127);
243 // 00000000 00000000 end of 8-bit/pixel_code_string
251 static int encode_dvb_subtitles(DVBSubtitleContext *s,
252 uint8_t *outbuf, AVSubtitle *h)
254 uint8_t *q, *pseg_len;
255 int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
262 if (h->num_rects == 0 || h->rects == NULL)
265 *q++ = 0x00; /* subtitle_stream_id */
267 /* page composition segment */
269 *q++ = 0x0f; /* sync_byte */
270 *q++ = 0x10; /* segment_type */
271 bytestream_put_be16(&q, page_id);
273 q += 2; /* segment length */
274 *q++ = 30; /* page_timeout (seconds) */
276 page_state = 0; /* normal case */
278 page_state = 2; /* mode change */
279 /* page_version = 0 + page_state */
280 *q++ = (s->object_version << 4) | (page_state << 2) | 3;
282 for (region_id = 0; region_id < h->num_rects; region_id++) {
284 *q++ = 0xff; /* reserved */
285 bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
286 bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
289 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
291 if (!s->hide_state) {
292 for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
296 if (h->rects[clut_id]->nb_colors <= 4) {
297 /* 2 bpp, some decoders do not support it correctly */
299 } else if (h->rects[clut_id]->nb_colors <= 16) {
300 /* 4 bpp, standard encoding */
302 } else if (h->rects[clut_id]->nb_colors <= 256) {
303 /* 8 bpp, standard encoding */
311 *q++ = 0x0f; /* sync byte */
312 *q++ = 0x12; /* CLUT definition segment */
313 bytestream_put_be16(&q, page_id);
315 q += 2; /* segment length */
317 *q++ = (0 << 4) | 0xf; /* version = 0 */
319 for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
320 *q++ = i; /* clut_entry_id */
321 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
324 uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
325 a = (x >> 24) & 0xff;
326 r = (x >> 16) & 0xff;
330 *q++ = RGB_TO_Y_CCIR(r, g, b);
331 *q++ = RGB_TO_V_CCIR(r, g, b, 0);
332 *q++ = RGB_TO_U_CCIR(r, g, b, 0);
337 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
341 for (region_id = 0; region_id < h->num_rects; region_id++) {
343 /* region composition segment */
345 if (h->rects[region_id]->nb_colors <= 4) {
346 /* 2 bpp, some decoders do not support it correctly */
348 } else if (h->rects[region_id]->nb_colors <= 16) {
349 /* 4 bpp, standard encoding */
355 *q++ = 0x0f; /* sync_byte */
356 *q++ = 0x11; /* segment_type */
357 bytestream_put_be16(&q, page_id);
359 q += 2; /* segment length */
361 *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
362 bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
363 bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
364 *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
365 *q++ = region_id; /* clut_id == region_id */
366 *q++ = 0; /* 8 bit fill colors */
367 *q++ = 0x03; /* 4 bit and 2 bit fill colors */
369 if (!s->hide_state) {
370 bytestream_put_be16(&q, region_id); /* object_id == region_id */
371 *q++ = (0 << 6) | (0 << 4);
377 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
380 if (!s->hide_state) {
382 for (object_id = 0; object_id < h->num_rects; object_id++) {
383 void (*dvb_encode_rle)(uint8_t **pq,
384 const uint8_t *bitmap, int linesize,
387 /* bpp_index maths */
388 if (h->rects[object_id]->nb_colors <= 4) {
389 /* 2 bpp, some decoders do not support it correctly */
390 dvb_encode_rle = dvb_encode_rle2;
391 } else if (h->rects[object_id]->nb_colors <= 16) {
392 /* 4 bpp, standard encoding */
393 dvb_encode_rle = dvb_encode_rle4;
394 } else if (h->rects[object_id]->nb_colors <= 256) {
395 /* 8 bpp, standard encoding */
396 dvb_encode_rle = dvb_encode_rle8;
401 /* Object Data segment */
402 *q++ = 0x0f; /* sync byte */
404 bytestream_put_be16(&q, page_id);
406 q += 2; /* segment length */
408 bytestream_put_be16(&q, object_id);
409 *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
410 onject_coding_method,
411 non_modifying_color_flag */
413 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
417 pbottom_field_len = q;
421 dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
422 h->rects[object_id]->w, h->rects[object_id]->h >> 1);
424 dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
425 h->rects[object_id]->w * 2, h->rects[object_id]->w,
426 h->rects[object_id]->h >> 1);
428 bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
429 bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
432 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
436 /* end of display set segment */
438 *q++ = 0x0f; /* sync_byte */
439 *q++ = 0x80; /* segment_type */
440 bytestream_put_be16(&q, page_id);
442 q += 2; /* segment length */
444 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
446 *q++ = 0xff; /* end of PES data */
448 s->object_version = (s->object_version + 1) & 0xf;
449 s->hide_state = !s->hide_state;
453 static int dvbsub_encode(AVCodecContext *avctx,
454 unsigned char *buf, int buf_size, void *data)
456 DVBSubtitleContext *s = avctx->priv_data;
457 AVSubtitle *sub = data;
460 ret = encode_dvb_subtitles(s, buf, sub);
464 AVCodec ff_dvbsub_encoder = {
466 .type = AVMEDIA_TYPE_SUBTITLE,
467 .id = CODEC_ID_DVB_SUBTITLE,
468 .priv_data_size = sizeof(DVBSubtitleContext),
469 .encode = dvbsub_encode,
470 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),