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 "codec_internal.h"
24 #include "libavutil/colorspace.h"
26 typedef struct DVBSubtitleContext {
30 #define PUTBITS2(val)\
32 bitbuf |= (val) << bitcnt;\
41 static int dvb_encode_rle2(uint8_t **pq, int buf_size,
42 const uint8_t *bitmap, int linesize,
45 uint8_t *q, *line_begin;
48 int x, y, len, x1, v, color;
52 for(y = 0; y < h; y++) {
53 // Worst case line is 3 bits per value + 4 bytes overhead
54 if (buf_size * 8 < w * 3 + 32)
55 return AVERROR_BUFFER_TOO_SMALL;
65 while (x1 < w && bitmap[x1] == color)
68 if (color == 0 && len == 2) {
72 } else if (len >= 3 && len <= 10) {
75 PUTBITS2((v >> 2) | 2);
78 } else if (len >= 12 && len <= 27) {
86 } else if (len >= 29) {
87 /* length = 29 ... 284 */
95 PUTBITS2((v >> 4) & 3);
96 PUTBITS2((v >> 2) & 3);
117 buf_size -= q - line_begin;
124 #define PUTBITS4(val)\
126 bitbuf |= (val) << bitcnt;\
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,
140 uint8_t *q, *line_begin;
143 int x, y, len, x1, v, color;
147 for(y = 0; y < h; y++) {
148 // Worst case line is 6 bits per value, + 4 bytes overhead
149 if (buf_size * 8 < w * 6 + 32)
150 return AVERROR_BUFFER_TOO_SMALL;
159 color = bitmap[x1++];
160 while (x1 < w && bitmap[x1] == color)
163 if (color == 0 && len == 2) {
166 } else if (color == 0 && (len >= 3 && len <= 9)) {
169 } else if (len >= 4 && len <= 7) {
171 PUTBITS4(8 + len - 4);
173 } else if (len >= 9 && len <= 24) {
178 } else if (len >= 25) {
204 buf_size -= q - line_begin;
211 static int dvb_encode_rle8(uint8_t **pq, int buf_size,
212 const uint8_t *bitmap, int linesize,
215 uint8_t *q, *line_begin;
216 int x, y, len, x1, color;
220 for (y = 0; y < h; y++) {
221 // Worst case line is 12 bits per value, + 3 bytes overhead
222 if (buf_size * 8 < w * 12 + 24)
223 return AVERROR_BUFFER_TOO_SMALL;
230 color = bitmap[x1++];
231 while (x1 < w && bitmap[x1] == color)
234 if (len == 1 && color) {
235 // 00000001 to 11111111 1 pixel in colour x
239 // 00000000 0LLLLLLL L pixels (1-127) in colour 0 (L > 0)
240 len = FFMIN(len, 127);
243 } else if (len > 2) {
244 // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
245 len = FFMIN(len, 127);
261 // 00000000 end of 8-bit/pixel_code_string
265 buf_size -= q - line_begin;
272 static int dvbsub_encode(AVCodecContext *avctx, uint8_t *outbuf, int buf_size,
275 DVBSubtitleContext *s = avctx->priv_data;
276 uint8_t *q, *pseg_len;
277 int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
284 if (h->num_rects && !h->rects)
285 return AVERROR(EINVAL);
287 if (avctx->width > 0 && avctx->height > 0) {
289 return AVERROR_BUFFER_TOO_SMALL;
290 /* display definition segment */
291 *q++ = 0x0f; /* sync_byte */
292 *q++ = 0x14; /* segment_type */
293 bytestream_put_be16(&q, page_id);
295 q += 2; /* segment length */
296 *q++ = 0x00; /* dds version number & display window flag */
297 bytestream_put_be16(&q, avctx->width - 1); /* display width */
298 bytestream_put_be16(&q, avctx->height - 1); /* display height */
299 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
303 /* page composition segment */
305 if (buf_size < 8 + h->num_rects * 6)
306 return AVERROR_BUFFER_TOO_SMALL;
307 *q++ = 0x0f; /* sync_byte */
308 *q++ = 0x10; /* segment_type */
309 bytestream_put_be16(&q, page_id);
311 q += 2; /* segment length */
312 *q++ = 30; /* page_timeout (seconds) */
313 page_state = 2; /* mode change */
314 /* page_version = 0 + page_state */
315 *q++ = (s->object_version << 4) | (page_state << 2) | 3;
317 for (region_id = 0; region_id < h->num_rects; region_id++) {
319 *q++ = 0xff; /* reserved */
320 bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
321 bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
324 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
325 buf_size -= 8 + h->num_rects * 6;
328 for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
329 if (buf_size < 6 + h->rects[clut_id]->nb_colors * 6)
330 return AVERROR_BUFFER_TOO_SMALL;
334 if (h->rects[clut_id]->nb_colors <= 4) {
335 /* 2 bpp, some decoders do not support it correctly */
337 } else if (h->rects[clut_id]->nb_colors <= 16) {
338 /* 4 bpp, standard encoding */
340 } else if (h->rects[clut_id]->nb_colors <= 256) {
341 /* 8 bpp, standard encoding */
344 return AVERROR(EINVAL);
349 *q++ = 0x0f; /* sync byte */
350 *q++ = 0x12; /* CLUT definition segment */
351 bytestream_put_be16(&q, page_id);
353 q += 2; /* segment length */
355 *q++ = (0 << 4) | 0xf; /* version = 0 */
357 for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
358 *q++ = i; /* clut_entry_id */
359 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
362 uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
363 a = (x >> 24) & 0xff;
364 r = (x >> 16) & 0xff;
368 *q++ = RGB_TO_Y_CCIR(r, g, b);
369 *q++ = RGB_TO_V_CCIR(r, g, b, 0);
370 *q++ = RGB_TO_U_CCIR(r, g, b, 0);
375 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
376 buf_size -= 6 + h->rects[clut_id]->nb_colors * 6;
379 if (buf_size < h->num_rects * 22)
380 return AVERROR_BUFFER_TOO_SMALL;
381 for (region_id = 0; region_id < h->num_rects; region_id++) {
383 /* region composition segment */
385 if (h->rects[region_id]->nb_colors <= 4) {
386 /* 2 bpp, some decoders do not support it correctly */
388 } else if (h->rects[region_id]->nb_colors <= 16) {
389 /* 4 bpp, standard encoding */
391 } else if (h->rects[region_id]->nb_colors <= 256) {
392 /* 8 bpp, standard encoding */
395 return AVERROR(EINVAL);
398 *q++ = 0x0f; /* sync_byte */
399 *q++ = 0x11; /* segment_type */
400 bytestream_put_be16(&q, page_id);
402 q += 2; /* segment length */
404 *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
405 bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
406 bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
407 *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
408 *q++ = region_id; /* clut_id == region_id */
409 *q++ = 0; /* 8 bit fill colors */
410 *q++ = 0x03; /* 4 bit and 2 bit fill colors */
412 bytestream_put_be16(&q, region_id); /* object_id == region_id */
413 *q++ = (0 << 6) | (0 << 4);
418 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
420 buf_size -= h->num_rects * 22;
422 for (object_id = 0; object_id < h->num_rects; object_id++) {
423 int (*dvb_encode_rle)(uint8_t **pq, int buf_size,
424 const uint8_t *bitmap, int linesize,
428 return AVERROR_BUFFER_TOO_SMALL;
430 /* bpp_index maths */
431 if (h->rects[object_id]->nb_colors <= 4) {
432 /* 2 bpp, some decoders do not support it correctly */
433 dvb_encode_rle = dvb_encode_rle2;
434 } else if (h->rects[object_id]->nb_colors <= 16) {
435 /* 4 bpp, standard encoding */
436 dvb_encode_rle = dvb_encode_rle4;
437 } else if (h->rects[object_id]->nb_colors <= 256) {
438 /* 8 bpp, standard encoding */
439 dvb_encode_rle = dvb_encode_rle8;
441 return AVERROR(EINVAL);
444 /* Object Data segment */
445 *q++ = 0x0f; /* sync byte */
447 bytestream_put_be16(&q, page_id);
449 q += 2; /* segment length */
451 bytestream_put_be16(&q, object_id);
452 *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
453 object_coding_method,
454 non_modifying_color_flag */
456 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
461 pbottom_field_len = q;
466 ret = dvb_encode_rle(&q, buf_size,
467 h->rects[object_id]->data[0],
468 h->rects[object_id]->w * 2,
469 h->rects[object_id]->w,
470 h->rects[object_id]->h >> 1);
475 ret = dvb_encode_rle(&q, buf_size,
476 h->rects[object_id]->data[0] + h->rects[object_id]->w,
477 h->rects[object_id]->w * 2,
478 h->rects[object_id]->w,
479 h->rects[object_id]->h >> 1);
484 bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
485 bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
488 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
492 /* end of display set segment */
495 return AVERROR_BUFFER_TOO_SMALL;
496 *q++ = 0x0f; /* sync_byte */
497 *q++ = 0x80; /* segment_type */
498 bytestream_put_be16(&q, page_id);
500 q += 2; /* segment length */
502 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
505 s->object_version = (s->object_version + 1) & 0xf;
509 const FFCodec ff_dvbsub_encoder = {
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),