2 * DVB subtitle encoding
3 * Copyright (c) 2005 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 int encode_dvb_subtitles(DVBSubtitleContext *s,
198 uint8_t *outbuf, const AVSubtitle *h)
200 uint8_t *q, *pseg_len;
201 int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
208 if (h->num_rects == 0 || h->rects == NULL)
211 *q++ = 0x00; /* subtitle_stream_id */
213 /* page composition segment */
215 *q++ = 0x0f; /* sync_byte */
216 *q++ = 0x10; /* segment_type */
217 bytestream_put_be16(&q, page_id);
219 q += 2; /* segment length */
220 *q++ = 30; /* page_timeout (seconds) */
222 page_state = 0; /* normal case */
224 page_state = 2; /* mode change */
225 /* page_version = 0 + page_state */
226 *q++ = (s->object_version << 4) | (page_state << 2) | 3;
228 for (region_id = 0; region_id < h->num_rects; region_id++) {
230 *q++ = 0xff; /* reserved */
231 bytestream_put_be16(&q, h->rects[region_id]->x); /* left pos */
232 bytestream_put_be16(&q, h->rects[region_id]->y); /* top pos */
235 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
237 if (!s->hide_state) {
238 for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
242 if (h->rects[clut_id]->nb_colors <= 4) {
243 /* 2 bpp, some decoders do not support it correctly */
245 } else if (h->rects[clut_id]->nb_colors <= 16) {
246 /* 4 bpp, standard encoding */
252 *q++ = 0x0f; /* sync byte */
253 *q++ = 0x12; /* CLUT definition segment */
254 bytestream_put_be16(&q, page_id);
256 q += 2; /* segment length */
258 *q++ = (0 << 4) | 0xf; /* version = 0 */
260 for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
261 *q++ = i; /* clut_entry_id */
262 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
265 uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
266 a = (x >> 24) & 0xff;
267 r = (x >> 16) & 0xff;
271 *q++ = RGB_TO_Y_CCIR(r, g, b);
272 *q++ = RGB_TO_V_CCIR(r, g, b, 0);
273 *q++ = RGB_TO_U_CCIR(r, g, b, 0);
278 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
282 for (region_id = 0; region_id < h->num_rects; region_id++) {
284 /* region composition segment */
286 if (h->rects[region_id]->nb_colors <= 4) {
287 /* 2 bpp, some decoders do not support it correctly */
289 } else if (h->rects[region_id]->nb_colors <= 16) {
290 /* 4 bpp, standard encoding */
296 *q++ = 0x0f; /* sync_byte */
297 *q++ = 0x11; /* segment_type */
298 bytestream_put_be16(&q, page_id);
300 q += 2; /* segment length */
302 *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
303 bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
304 bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
305 *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
306 *q++ = region_id; /* clut_id == region_id */
307 *q++ = 0; /* 8 bit fill colors */
308 *q++ = 0x03; /* 4 bit and 2 bit fill colors */
310 if (!s->hide_state) {
311 bytestream_put_be16(&q, region_id); /* object_id == region_id */
312 *q++ = (0 << 6) | (0 << 4);
318 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
321 if (!s->hide_state) {
323 for (object_id = 0; object_id < h->num_rects; object_id++) {
324 /* Object Data segment */
326 if (h->rects[object_id]->nb_colors <= 4) {
327 /* 2 bpp, some decoders do not support it correctly */
329 } else if (h->rects[object_id]->nb_colors <= 16) {
330 /* 4 bpp, standard encoding */
336 *q++ = 0x0f; /* sync byte */
338 bytestream_put_be16(&q, page_id);
340 q += 2; /* segment length */
342 bytestream_put_be16(&q, object_id);
343 *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
344 onject_coding_method,
345 non_modifying_color_flag */
347 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
348 void (*dvb_encode_rle)(uint8_t **pq,
349 const uint8_t *bitmap, int linesize,
353 pbottom_field_len = q;
357 dvb_encode_rle = dvb_encode_rle2;
359 dvb_encode_rle = dvb_encode_rle4;
362 dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
363 h->rects[object_id]->w, h->rects[object_id]->h >> 1);
365 dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
366 h->rects[object_id]->w * 2, h->rects[object_id]->w,
367 h->rects[object_id]->h >> 1);
369 bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
370 bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
373 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
377 /* end of display set segment */
379 *q++ = 0x0f; /* sync_byte */
380 *q++ = 0x80; /* segment_type */
381 bytestream_put_be16(&q, page_id);
383 q += 2; /* segment length */
385 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
387 *q++ = 0xff; /* end of PES data */
389 s->object_version = (s->object_version + 1) & 0xf;
390 s->hide_state = !s->hide_state;
394 static int dvbsub_encode(AVCodecContext *avctx,
395 unsigned char *buf, int buf_size,
396 const AVSubtitle *sub)
398 DVBSubtitleContext *s = avctx->priv_data;
401 ret = encode_dvb_subtitles(s, buf, sub);
405 AVCodec ff_dvbsub_encoder = {
407 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
408 .type = AVMEDIA_TYPE_SUBTITLE,
409 .id = AV_CODEC_ID_DVB_SUBTITLE,
410 .priv_data_size = sizeof(DVBSubtitleContext),
411 .encode_sub = dvbsub_encode,