• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * DVB subtitle encoding
3  * Copyright (c) 2005 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avcodec.h"
22 #include "bytestream.h"
23 #include "codec_internal.h"
24 #include "libavutil/colorspace.h"
25 
26 typedef struct DVBSubtitleContext {
27     int object_version;
28 } DVBSubtitleContext;
29 
30 #define PUTBITS2(val)\
31 {\
32     bitbuf |= (val) << bitcnt;\
33     bitcnt -= 2;\
34     if (bitcnt < 0) {\
35         bitcnt = 6;\
36         *q++ = bitbuf;\
37         bitbuf = 0;\
38     }\
39 }
40 
dvb_encode_rle2(uint8_t ** pq,int buf_size,const uint8_t * bitmap,int linesize,int w,int h)41 static int dvb_encode_rle2(uint8_t **pq, int buf_size,
42                            const uint8_t *bitmap, int linesize,
43                            int w, int h)
44 {
45     uint8_t *q, *line_begin;
46     unsigned int bitbuf;
47     int bitcnt;
48     int x, y, len, x1, v, color;
49 
50     q = *pq;
51 
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;
56         line_begin = q;
57         *q++ = 0x10;
58         bitbuf = 0;
59         bitcnt = 6;
60 
61         x = 0;
62         while (x < w) {
63             x1 = x;
64             color = bitmap[x1++];
65             while (x1 < w && bitmap[x1] == color)
66                 x1++;
67             len = x1 - x;
68             if (color == 0 && len == 2) {
69                 PUTBITS2(0);
70                 PUTBITS2(0);
71                 PUTBITS2(1);
72             } else if (len >= 3 && len <= 10) {
73                 v = len - 3;
74                 PUTBITS2(0);
75                 PUTBITS2((v >> 2) | 2);
76                 PUTBITS2(v & 3);
77                 PUTBITS2(color);
78             } else if (len >= 12 && len <= 27) {
79                 v = len - 12;
80                 PUTBITS2(0);
81                 PUTBITS2(0);
82                 PUTBITS2(2);
83                 PUTBITS2(v >> 2);
84                 PUTBITS2(v & 3);
85                 PUTBITS2(color);
86             } else if (len >= 29) {
87                 /* length = 29 ... 284 */
88                 if (len > 284)
89                     len = 284;
90                 v = len - 29;
91                 PUTBITS2(0);
92                 PUTBITS2(0);
93                 PUTBITS2(3);
94                 PUTBITS2((v >> 6));
95                 PUTBITS2((v >> 4) & 3);
96                 PUTBITS2((v >> 2) & 3);
97                 PUTBITS2(v & 3);
98                 PUTBITS2(color);
99             } else {
100                 PUTBITS2(color);
101                 if (color == 0) {
102                     PUTBITS2(1);
103                 }
104                 len = 1;
105             }
106             x += len;
107         }
108         /* end of line */
109         PUTBITS2(0);
110         PUTBITS2(0);
111         PUTBITS2(0);
112         if (bitcnt != 6) {
113             *q++ = bitbuf;
114         }
115         *q++ = 0xf0;
116         bitmap += linesize;
117         buf_size -= q - line_begin;
118     }
119     len = q - *pq;
120     *pq = q;
121     return len;
122 }
123 
124 #define PUTBITS4(val)\
125 {\
126     bitbuf |= (val) << bitcnt;\
127     bitcnt -= 4;\
128     if (bitcnt < 0) {\
129         bitcnt = 4;\
130         *q++ = bitbuf;\
131         bitbuf = 0;\
132     }\
133 }
134 
135 /* some DVB decoders only implement 4 bits/pixel */
dvb_encode_rle4(uint8_t ** pq,int buf_size,const uint8_t * bitmap,int linesize,int w,int h)136 static int dvb_encode_rle4(uint8_t **pq, int buf_size,
137                            const uint8_t *bitmap, int linesize,
138                            int w, int h)
139 {
140     uint8_t *q, *line_begin;
141     unsigned int bitbuf;
142     int bitcnt;
143     int x, y, len, x1, v, color;
144 
145     q = *pq;
146 
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;
151         line_begin = q;
152         *q++ = 0x11;
153         bitbuf = 0;
154         bitcnt = 4;
155 
156         x = 0;
157         while (x < w) {
158             x1 = x;
159             color = bitmap[x1++];
160             while (x1 < w && bitmap[x1] == color)
161                 x1++;
162             len = x1 - x;
163             if (color == 0 && len == 2) {
164                 PUTBITS4(0);
165                 PUTBITS4(0xd);
166             } else if (color == 0 && (len >= 3 && len <= 9)) {
167                 PUTBITS4(0);
168                 PUTBITS4(len - 2);
169             } else if (len >= 4 && len <= 7) {
170                 PUTBITS4(0);
171                 PUTBITS4(8 + len - 4);
172                 PUTBITS4(color);
173             } else if (len >= 9 && len <= 24) {
174                 PUTBITS4(0);
175                 PUTBITS4(0xe);
176                 PUTBITS4(len - 9);
177                 PUTBITS4(color);
178             } else if (len >= 25) {
179                 if (len > 280)
180                     len = 280;
181                 v = len - 25;
182                 PUTBITS4(0);
183                 PUTBITS4(0xf);
184                 PUTBITS4(v >> 4);
185                 PUTBITS4(v & 0xf);
186                 PUTBITS4(color);
187             } else {
188                 PUTBITS4(color);
189                 if (color == 0) {
190                     PUTBITS4(0xc);
191                 }
192                 len = 1;
193             }
194             x += len;
195         }
196         /* end of line */
197         PUTBITS4(0);
198         PUTBITS4(0);
199         if (bitcnt != 4) {
200             *q++ = bitbuf;
201         }
202         *q++ = 0xf0;
203         bitmap += linesize;
204         buf_size -= q - line_begin;
205     }
206     len = q - *pq;
207     *pq = q;
208     return len;
209 }
210 
dvb_encode_rle8(uint8_t ** pq,int buf_size,const uint8_t * bitmap,int linesize,int w,int h)211 static int dvb_encode_rle8(uint8_t **pq, int buf_size,
212                            const uint8_t *bitmap, int linesize,
213                            int w, int h)
214 {
215     uint8_t *q, *line_begin;
216     int x, y, len, x1, color;
217 
218     q = *pq;
219 
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;
224         line_begin = q;
225         *q++ = 0x12;
226 
227         x = 0;
228         while (x < w) {
229             x1 = x;
230             color = bitmap[x1++];
231             while (x1 < w && bitmap[x1] == color)
232                 x1++;
233             len = x1 - x;
234             if (len == 1 && color) {
235                 // 00000001 to 11111111           1 pixel in colour x
236                 *q++ = color;
237             } else {
238                 if (color == 0x00) {
239                     // 00000000 0LLLLLLL          L pixels (1-127) in colour 0 (L > 0)
240                     len = FFMIN(len, 127);
241                     *q++ = 0x00;
242                     *q++ = len;
243                 } else if (len > 2) {
244                     // 00000000 1LLLLLLL CCCCCCCC L pixels (3-127) in colour C (L > 2)
245                     len = FFMIN(len, 127);
246                     *q++ = 0x00;
247                     *q++ = 0x80+len;
248                     *q++ = color;
249                 }
250                 else if (len == 2) {
251                     *q++ = color;
252                     *q++ = color;
253                 } else {
254                     *q++ = color;
255                     len = 1;
256                 }
257             }
258             x += len;
259         }
260         /* end of line */
261         // 00000000 end of 8-bit/pixel_code_string
262         *q++ = 0x00;
263         *q++ = 0xf0;
264         bitmap += linesize;
265         buf_size -= q - line_begin;
266     }
267     len = q - *pq;
268     *pq = q;
269     return len;
270 }
271 
dvbsub_encode(AVCodecContext * avctx,uint8_t * outbuf,int buf_size,const AVSubtitle * h)272 static int dvbsub_encode(AVCodecContext *avctx, uint8_t *outbuf, int buf_size,
273                          const AVSubtitle *h)
274 {
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;
278 
279 
280     q = outbuf;
281 
282     page_id = 1;
283 
284     if (h->num_rects && !h->rects)
285         return AVERROR(EINVAL);
286 
287     if (avctx->width > 0 && avctx->height > 0) {
288         if (buf_size < 11)
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);
294         pseg_len = q;
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);
300         buf_size -= 11;
301     }
302 
303     /* page composition segment */
304 
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);
310     pseg_len = q;
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;
316 
317     for (region_id = 0; region_id < h->num_rects; region_id++) {
318         *q++ = 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 */
322     }
323 
324     bytestream_put_be16(&pseg_len, q - pseg_len - 2);
325     buf_size -= 8 + h->num_rects * 6;
326 
327     if (h->num_rects) {
328         for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
329             /* CLUT segment */
330 
331             if (h->rects[clut_id]->nb_colors <= 4U) {
332                 /* 2 bpp, some decoders do not support it correctly */
333                 bpp_index = 0;
334             } else if (h->rects[clut_id]->nb_colors <= 16U) {
335                 /* 4 bpp, standard encoding */
336                 bpp_index = 1;
337             } else if (h->rects[clut_id]->nb_colors <= 256U) {
338                 /* 8 bpp, standard encoding */
339                 bpp_index = 2;
340             } else {
341                 return AVERROR(EINVAL);
342             }
343 
344             if (buf_size < 6 + h->rects[clut_id]->nb_colors * 6)
345                 return AVERROR_BUFFER_TOO_SMALL;
346 
347             /* CLUT segment */
348             *q++ = 0x0f; /* sync byte */
349             *q++ = 0x12; /* CLUT definition segment */
350             bytestream_put_be16(&q, page_id);
351             pseg_len = q;
352             q += 2; /* segment length */
353             *q++ = clut_id;
354             *q++ = (0 << 4) | 0xf; /* version = 0 */
355 
356             for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
357                 *q++ = i; /* clut_entry_id */
358                 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1; /* 2 bits/pixel full range */
359                 {
360                     int a, r, g, b;
361                     uint32_t x= ((uint32_t*)h->rects[clut_id]->data[1])[i];
362                     a = (x >> 24) & 0xff;
363                     r = (x >> 16) & 0xff;
364                     g = (x >>  8) & 0xff;
365                     b = (x >>  0) & 0xff;
366 
367                     *q++ = RGB_TO_Y_CCIR(r, g, b);
368                     *q++ = RGB_TO_V_CCIR(r, g, b, 0);
369                     *q++ = RGB_TO_U_CCIR(r, g, b, 0);
370                     *q++ = 255 - a;
371                 }
372             }
373 
374             bytestream_put_be16(&pseg_len, q - pseg_len - 2);
375             buf_size -= 6 + h->rects[clut_id]->nb_colors * 6;
376         }
377 
378         if (buf_size < h->num_rects * 22)
379             return AVERROR_BUFFER_TOO_SMALL;
380         for (region_id = 0; region_id < h->num_rects; region_id++) {
381 
382             /* region composition segment */
383 
384             if (h->rects[region_id]->nb_colors <= 4) {
385                 /* 2 bpp, some decoders do not support it correctly */
386                 bpp_index = 0;
387             } else if (h->rects[region_id]->nb_colors <= 16) {
388                 /* 4 bpp, standard encoding */
389                 bpp_index = 1;
390             } else if (h->rects[region_id]->nb_colors <= 256) {
391                 /* 8 bpp, standard encoding */
392                 bpp_index = 2;
393             } else {
394                 return AVERROR(EINVAL);
395             }
396 
397             *q++ = 0x0f; /* sync_byte */
398             *q++ = 0x11; /* segment_type */
399             bytestream_put_be16(&q, page_id);
400             pseg_len = q;
401             q += 2; /* segment length */
402             *q++ = region_id;
403             *q++ = (s->object_version << 4) | (0 << 3) | 0x07; /* version , no fill */
404             bytestream_put_be16(&q, h->rects[region_id]->w); /* region width */
405             bytestream_put_be16(&q, h->rects[region_id]->h); /* region height */
406             *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
407             *q++ = region_id; /* clut_id == region_id */
408             *q++ = 0; /* 8 bit fill colors */
409             *q++ = 0x03; /* 4 bit and 2 bit fill colors */
410 
411             bytestream_put_be16(&q, region_id); /* object_id == region_id */
412             *q++ = (0 << 6) | (0 << 4);
413             *q++ = 0;
414             *q++ = 0xf0;
415             *q++ = 0;
416 
417             bytestream_put_be16(&pseg_len, q - pseg_len - 2);
418         }
419         buf_size -= h->num_rects * 22;
420 
421         for (object_id = 0; object_id < h->num_rects; object_id++) {
422             int (*dvb_encode_rle)(uint8_t **pq, int buf_size,
423                                   const uint8_t *bitmap, int linesize,
424                                   int w, int h);
425 
426             if (buf_size < 13)
427                 return AVERROR_BUFFER_TOO_SMALL;
428 
429             /* bpp_index maths */
430             if (h->rects[object_id]->nb_colors <= 4) {
431                 /* 2 bpp, some decoders do not support it correctly */
432                 dvb_encode_rle = dvb_encode_rle2;
433             } else if (h->rects[object_id]->nb_colors <= 16) {
434                 /* 4 bpp, standard encoding */
435                 dvb_encode_rle = dvb_encode_rle4;
436             } else if (h->rects[object_id]->nb_colors <= 256) {
437                 /* 8 bpp, standard encoding */
438                 dvb_encode_rle = dvb_encode_rle8;
439             } else {
440                 return AVERROR(EINVAL);
441             }
442 
443             /* Object Data segment */
444             *q++ = 0x0f; /* sync byte */
445             *q++ = 0x13;
446             bytestream_put_be16(&q, page_id);
447             pseg_len = q;
448             q += 2; /* segment length */
449 
450             bytestream_put_be16(&q, object_id);
451             *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1; /* version = 0,
452                                                                        object_coding_method,
453                                                                        non_modifying_color_flag */
454             {
455                 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
456                 int ret;
457 
458                 ptop_field_len = q;
459                 q += 2;
460                 pbottom_field_len = q;
461                 q += 2;
462                 buf_size -= 13;
463 
464                 top_ptr = q;
465                 ret = dvb_encode_rle(&q, buf_size,
466                                      h->rects[object_id]->data[0],
467                                      h->rects[object_id]->w * 2,
468                                      h->rects[object_id]->w,
469                                      h->rects[object_id]->h >> 1);
470                 if (ret < 0)
471                     return ret;
472                 buf_size -= ret;
473                 bottom_ptr = q;
474                 ret = dvb_encode_rle(&q, buf_size,
475                                      h->rects[object_id]->data[0] + h->rects[object_id]->w,
476                                      h->rects[object_id]->w * 2,
477                                      h->rects[object_id]->w,
478                                      h->rects[object_id]->h >> 1);
479                 if (ret < 0)
480                     return ret;
481                 buf_size -= ret;
482 
483                 bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
484                 bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
485             }
486 
487             bytestream_put_be16(&pseg_len, q - pseg_len - 2);
488         }
489     }
490 
491     /* end of display set segment */
492 
493     if (buf_size < 6)
494         return AVERROR_BUFFER_TOO_SMALL;
495     *q++ = 0x0f; /* sync_byte */
496     *q++ = 0x80; /* segment_type */
497     bytestream_put_be16(&q, page_id);
498     pseg_len = q;
499     q += 2; /* segment length */
500 
501     bytestream_put_be16(&pseg_len, q - pseg_len - 2);
502     buf_size -= 6;
503 
504     s->object_version = (s->object_version + 1) & 0xf;
505     return q - outbuf;
506 }
507 
508 const FFCodec ff_dvbsub_encoder = {
509     .p.name         = "dvbsub",
510     .p.long_name    = NULL_IF_CONFIG_SMALL("DVB subtitles"),
511     .p.type         = AVMEDIA_TYPE_SUBTITLE,
512     .p.id           = AV_CODEC_ID_DVB_SUBTITLE,
513     .priv_data_size = sizeof(DVBSubtitleContext),
514     FF_CODEC_ENCODE_SUB_CB(dvbsub_encode),
515 };
516