2 * DVB subtitle encoding for ffmpeg
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
23 typedef struct DVBSubtitleContext {
28 #define PUTBITS2(val)\
30 bitbuf |= (val) << bitcnt;\
39 static void dvb_encode_rle2(uint8_t **pq,
40 const uint8_t *bitmap, int linesize,
46 int x, y, len, x1, v, color;
50 for(y = 0; y < h; y++) {
59 while (x1 < w && bitmap[x1] == color)
62 if (color == 0 && len == 2) {
66 } else if (len >= 3 && len <= 10) {
69 PUTBITS2((v >> 2) | 2);
72 } else if (len >= 12 && len <= 27) {
80 } else if (len >= 29) {
81 /* length = 29 ... 284 */
89 PUTBITS2((v >> 4) & 3);
90 PUTBITS2((v >> 2) & 3);
115 #define PUTBITS4(val)\
117 bitbuf |= (val) << bitcnt;\
126 /* some DVB decoders only implement 4 bits/pixel */
127 static void dvb_encode_rle4(uint8_t **pq,
128 const uint8_t *bitmap, int linesize,
134 int x, y, len, x1, v, color;
138 for(y = 0; y < h; y++) {
146 color = bitmap[x1++];
147 while (x1 < w && bitmap[x1] == color)
150 if (color == 0 && len == 2) {
153 } else if (color == 0 && (len >= 3 && len <= 9)) {
156 } else if (len >= 4 && len <= 7) {
158 PUTBITS4(8 + len - 4);
160 } else if (len >= 9 && len <= 24) {
165 } else if (len >= 25) {
196 #define ONE_HALF (1 << (SCALEBITS - 1))
197 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
199 #define RGB_TO_Y_CCIR(r, g, b) \
200 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
201 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
203 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
204 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
205 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
207 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
208 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
209 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
211 static inline void putbe16(uint8_t **pq, uint16_t v)
220 static int encode_dvb_subtitles(DVBSubtitleContext *s,
221 uint8_t *outbuf, AVSubtitle *h)
223 uint8_t *q, *pseg_len;
224 int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
231 if (h->num_rects == 0 || h->rects == NULL)
234 *q++ = 0x00; /* subtitle_stream_id */
236 /* page composition segment */
238 *q++ = 0x0f; /* sync_byte */
239 *q++ = 0x10; /* segment_type */
240 putbe16(&q, page_id);
242 q += 2; /* segment length */
243 *q++ = 30; /* page_timeout (seconds) */
245 page_state = 0; /* normal case */
247 page_state = 2; /* mode change */
248 /* page_version = 0 + page_state */
249 *q++ = s->object_version | (page_state << 2) | 3;
251 for (region_id = 0; region_id < h->num_rects; region_id++) {
253 *q++ = 0xff; /* reserved */
254 putbe16(&q, h->rects[region_id].x); /* left pos */
255 putbe16(&q, h->rects[region_id].y); /* top pos */
258 putbe16(&pseg_len, q - pseg_len - 2);
260 if (!s->hide_state) {
261 for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
265 if (h->rects[clut_id].nb_colors <= 4) {
266 /* 2 bpp, some decoders do not support it correctly */
268 } else if (h->rects[clut_id].nb_colors <= 16) {
269 /* 4 bpp, standard encoding */
275 *q++ = 0x0f; /* sync byte */
276 *q++ = 0x12; /* CLUT definition segment */
277 putbe16(&q, page_id);
279 q += 2; /* segment length */
281 *q++ = (0 << 4) | 0xf; /* version = 0 */
283 for(i = 0; i < h->rects[clut_id].nb_colors; i++) {
284 *q++ = i; /* clut_entry_id */
285 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
288 a = (h->rects[clut_id].rgba_palette[i] >> 24) & 0xff;
289 r = (h->rects[clut_id].rgba_palette[i] >> 16) & 0xff;
290 g = (h->rects[clut_id].rgba_palette[i] >> 8) & 0xff;
291 b = (h->rects[clut_id].rgba_palette[i] >> 0) & 0xff;
293 *q++ = RGB_TO_Y_CCIR(r, g, b);
294 *q++ = RGB_TO_V_CCIR(r, g, b, 0);
295 *q++ = RGB_TO_U_CCIR(r, g, b, 0);
300 putbe16(&pseg_len, q - pseg_len - 2);
304 for (region_id = 0; region_id < h->num_rects; region_id++) {
306 /* region composition segment */
308 if (h->rects[region_id].nb_colors <= 4) {
309 /* 2 bpp, some decoders do not support it correctly */
311 } else if (h->rects[region_id].nb_colors <= 16) {
312 /* 4 bpp, standard encoding */
318 *q++ = 0x0f; /* sync_byte */
319 *q++ = 0x11; /* segment_type */
320 putbe16(&q, page_id);
322 q += 2; /* segment length */
324 *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
325 putbe16(&q, h->rects[region_id].w); /* region width */
326 putbe16(&q, h->rects[region_id].h); /* region height */
327 *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
328 *q++ = region_id; /* clut_id == region_id */
329 *q++ = 0; /* 8 bit fill colors */
330 *q++ = 0x03; /* 4 bit and 2 bit fill colors */
332 if (!s->hide_state) {
333 putbe16(&q, region_id); /* object_id == region_id */
334 *q++ = (0 << 6) | (0 << 4);
340 putbe16(&pseg_len, q - pseg_len - 2);
343 if (!s->hide_state) {
345 for (object_id = 0; object_id < h->num_rects; object_id++) {
346 /* Object Data segment */
348 if (h->rects[object_id].nb_colors <= 4) {
349 /* 2 bpp, some decoders do not support it correctly */
351 } else if (h->rects[object_id].nb_colors <= 16) {
352 /* 4 bpp, standard encoding */
358 *q++ = 0x0f; /* sync byte */
360 putbe16(&q, page_id);
362 q += 2; /* segment length */
364 putbe16(&q, object_id);
365 *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
366 onject_coding_method,
367 non_modifying_color_flag */
369 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
370 void (*dvb_encode_rle)(uint8_t **pq,
371 const uint8_t *bitmap, int linesize,
375 pbottom_field_len = q;
379 dvb_encode_rle = dvb_encode_rle2;
381 dvb_encode_rle = dvb_encode_rle4;
384 dvb_encode_rle(&q, h->rects[object_id].bitmap, h->rects[object_id].w * 2,
385 h->rects[object_id].w, h->rects[object_id].h >> 1);
387 dvb_encode_rle(&q, h->rects[object_id].bitmap + h->rects[object_id].w,
388 h->rects[object_id].w * 2, h->rects[object_id].w,
389 h->rects[object_id].h >> 1);
391 putbe16(&ptop_field_len, bottom_ptr - top_ptr);
392 putbe16(&pbottom_field_len, q - bottom_ptr);
395 putbe16(&pseg_len, q - pseg_len - 2);
399 /* end of display set segment */
401 *q++ = 0x0f; /* sync_byte */
402 *q++ = 0x80; /* segment_type */
403 putbe16(&q, page_id);
405 q += 2; /* segment length */
407 putbe16(&pseg_len, q - pseg_len - 2);
409 *q++ = 0xff; /* end of PES data */
411 s->object_version = (s->object_version + 1) & 0xf;
412 s->hide_state = !s->hide_state;
416 static int dvbsub_init_decoder(AVCodecContext *avctx)
421 static int dvbsub_close_decoder(AVCodecContext *avctx)
426 static int dvbsub_encode(AVCodecContext *avctx,
427 unsigned char *buf, int buf_size, void *data)
429 DVBSubtitleContext *s = avctx->priv_data;
430 AVSubtitle *sub = data;
433 ret = encode_dvb_subtitles(s, buf, sub);
437 AVCodec dvbsub_encoder = {
440 CODEC_ID_DVB_SUBTITLE,
441 sizeof(DVBSubtitleContext),
444 dvbsub_close_decoder,