2 * DVB subtitle encoding for ffmpeg
3 * Copyright (c) 2005 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 typedef struct DVBSubtitleContext
{
26 #define PUTBITS2(val)\
28 bitbuf |= (val) << bitcnt;\
37 static void dvb_encode_rle2(uint8_t **pq
,
38 const uint8_t *bitmap
, int linesize
,
44 int x
, y
, len
, x1
, v
, color
;
48 for(y
= 0; y
< h
; y
++) {
57 while (x1
< w
&& bitmap
[x1
] == color
)
60 if (color
== 0 && len
== 2) {
64 } else if (len
>= 3 && len
<= 10) {
67 PUTBITS2((v
>> 2) | 2);
70 } else if (len
>= 12 && len
<= 27) {
78 } else if (len
>= 29) {
79 /* length = 29 ... 284 */
87 PUTBITS2((v
>> 4) & 3);
88 PUTBITS2((v
>> 2) & 3);
113 #define PUTBITS4(val)\
115 bitbuf |= (val) << bitcnt;\
124 /* some DVB decoders only implement 4 bits/pixel */
125 static void dvb_encode_rle4(uint8_t **pq
,
126 const uint8_t *bitmap
, int linesize
,
132 int x
, y
, len
, x1
, v
, color
;
136 for(y
= 0; y
< h
; y
++) {
144 color
= bitmap
[x1
++];
145 while (x1
< w
&& bitmap
[x1
] == color
)
148 if (color
== 0 && len
== 2) {
151 } else if (color
== 0 && (len
>= 3 && len
<= 9)) {
154 } else if (len
>= 4 && len
<= 7) {
156 PUTBITS4(8 + len
- 4);
158 } else if (len
>= 9 && len
<= 24) {
163 } else if (len
>= 25) {
194 #define ONE_HALF (1 << (SCALEBITS - 1))
195 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
197 #define RGB_TO_Y_CCIR(r, g, b) \
198 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
199 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
201 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
202 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
203 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
205 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
206 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
207 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
209 static inline void putbe16(uint8_t **pq
, uint16_t v
)
218 static int encode_dvb_subtitles(DVBSubtitleContext
*s
,
219 uint8_t *outbuf
, AVSubtitle
*h
)
221 uint8_t *q
, *pseg_len
;
222 int page_id
, region_id
, clut_id
, object_id
, i
, bpp_index
, page_state
;
229 if (h
->num_rects
== 0 || h
->rects
== NULL
)
232 *q
++ = 0x00; /* subtitle_stream_id */
234 /* page composition segment */
236 *q
++ = 0x0f; /* sync_byte */
237 *q
++ = 0x10; /* segment_type */
238 putbe16(&q
, page_id
);
240 q
+= 2; /* segment length */
241 *q
++ = 30; /* page_timeout (seconds) */
243 page_state
= 0; /* normal case */
245 page_state
= 2; /* mode change */
246 /* page_version = 0 + page_state */
247 *q
++ = s
->object_version
| (page_state
<< 2) | 3;
249 for (region_id
= 0; region_id
< h
->num_rects
; region_id
++) {
251 *q
++ = 0xff; /* reserved */
252 putbe16(&q
, h
->rects
[region_id
].x
); /* left pos */
253 putbe16(&q
, h
->rects
[region_id
].y
); /* top pos */
256 putbe16(&pseg_len
, q
- pseg_len
- 2);
258 if (!s
->hide_state
) {
259 for (clut_id
= 0; clut_id
< h
->num_rects
; clut_id
++) {
263 if (h
->rects
[clut_id
].nb_colors
<= 4) {
264 /* 2 bpp, some decoders do not support it correctly */
266 } else if (h
->rects
[clut_id
].nb_colors
<= 16) {
267 /* 4 bpp, standard encoding */
273 *q
++ = 0x0f; /* sync byte */
274 *q
++ = 0x12; /* CLUT definition segment */
275 putbe16(&q
, page_id
);
277 q
+= 2; /* segment length */
279 *q
++ = (0 << 4) | 0xf; /* version = 0 */
281 for(i
= 0; i
< h
->rects
[clut_id
].nb_colors
; i
++) {
282 *q
++ = i
; /* clut_entry_id */
283 *q
++ = (1 << (7 - bpp_index
)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
286 a
= (h
->rects
[clut_id
].rgba_palette
[i
] >> 24) & 0xff;
287 r
= (h
->rects
[clut_id
].rgba_palette
[i
] >> 16) & 0xff;
288 g
= (h
->rects
[clut_id
].rgba_palette
[i
] >> 8) & 0xff;
289 b
= (h
->rects
[clut_id
].rgba_palette
[i
] >> 0) & 0xff;
291 *q
++ = RGB_TO_Y_CCIR(r
, g
, b
);
292 *q
++ = RGB_TO_V_CCIR(r
, g
, b
, 0);
293 *q
++ = RGB_TO_U_CCIR(r
, g
, b
, 0);
298 putbe16(&pseg_len
, q
- pseg_len
- 2);
302 for (region_id
= 0; region_id
< h
->num_rects
; region_id
++) {
304 /* region composition segment */
306 if (h
->rects
[region_id
].nb_colors
<= 4) {
307 /* 2 bpp, some decoders do not support it correctly */
309 } else if (h
->rects
[region_id
].nb_colors
<= 16) {
310 /* 4 bpp, standard encoding */
316 *q
++ = 0x0f; /* sync_byte */
317 *q
++ = 0x11; /* segment_type */
318 putbe16(&q
, page_id
);
320 q
+= 2; /* segment length */
322 *q
++ = (s
->object_version
<< 4) | (0 << 3) | 0x07; /* version , no fill */
323 putbe16(&q
, h
->rects
[region_id
].w
); /* region width */
324 putbe16(&q
, h
->rects
[region_id
].h
); /* region height */
325 *q
++ = ((1 + bpp_index
) << 5) | ((1 + bpp_index
) << 2) | 0x03;
326 *q
++ = region_id
; /* clut_id == region_id */
327 *q
++ = 0; /* 8 bit fill colors */
328 *q
++ = 0x03; /* 4 bit and 2 bit fill colors */
330 if (!s
->hide_state
) {
331 putbe16(&q
, region_id
); /* object_id == region_id */
332 *q
++ = (0 << 6) | (0 << 4);
338 putbe16(&pseg_len
, q
- pseg_len
- 2);
341 if (!s
->hide_state
) {
343 for (object_id
= 0; object_id
< h
->num_rects
; object_id
++) {
344 /* Object Data segment */
346 if (h
->rects
[object_id
].nb_colors
<= 4) {
347 /* 2 bpp, some decoders do not support it correctly */
349 } else if (h
->rects
[object_id
].nb_colors
<= 16) {
350 /* 4 bpp, standard encoding */
356 *q
++ = 0x0f; /* sync byte */
358 putbe16(&q
, page_id
);
360 q
+= 2; /* segment length */
362 putbe16(&q
, object_id
);
363 *q
++ = (s
->object_version
<< 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
364 onject_coding_method,
365 non_modifying_color_flag */
367 uint8_t *ptop_field_len
, *pbottom_field_len
, *top_ptr
, *bottom_ptr
;
368 void (*dvb_encode_rle
)(uint8_t **pq
,
369 const uint8_t *bitmap
, int linesize
,
373 pbottom_field_len
= q
;
377 dvb_encode_rle
= dvb_encode_rle2
;
379 dvb_encode_rle
= dvb_encode_rle4
;
382 dvb_encode_rle(&q
, h
->rects
[object_id
].bitmap
, h
->rects
[object_id
].w
* 2,
383 h
->rects
[object_id
].w
, h
->rects
[object_id
].h
>> 1);
385 dvb_encode_rle(&q
, h
->rects
[object_id
].bitmap
+ h
->rects
[object_id
].w
,
386 h
->rects
[object_id
].w
* 2, h
->rects
[object_id
].w
,
387 h
->rects
[object_id
].h
>> 1);
389 putbe16(&ptop_field_len
, bottom_ptr
- top_ptr
);
390 putbe16(&pbottom_field_len
, q
- bottom_ptr
);
393 putbe16(&pseg_len
, q
- pseg_len
- 2);
397 /* end of display set segment */
399 *q
++ = 0x0f; /* sync_byte */
400 *q
++ = 0x80; /* segment_type */
401 putbe16(&q
, page_id
);
403 q
+= 2; /* segment length */
405 putbe16(&pseg_len
, q
- pseg_len
- 2);
407 *q
++ = 0xff; /* end of PES data */
409 s
->object_version
= (s
->object_version
+ 1) & 0xf;
410 s
->hide_state
= !s
->hide_state
;
414 static int dvbsub_init_decoder(AVCodecContext
*avctx
)
419 static int dvbsub_close_decoder(AVCodecContext
*avctx
)
424 static int dvbsub_encode(AVCodecContext
*avctx
,
425 unsigned char *buf
, int buf_size
, void *data
)
427 DVBSubtitleContext
*s
= avctx
->priv_data
;
428 AVSubtitle
*sub
= data
;
431 ret
= encode_dvb_subtitles(s
, buf
, sub
);
435 AVCodec dvbsub_encoder
= {
438 CODEC_ID_DVB_SUBTITLE
,
439 sizeof(DVBSubtitleContext
),
442 dvbsub_close_decoder
,