00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avcodec.h"
00022 #include "bytestream.h"
00023 #include "colorspace.h"
00024
00025 typedef struct DVBSubtitleContext {
00026 int hide_state;
00027 int object_version;
00028 } DVBSubtitleContext;
00029
00030 #define PUTBITS2(val)\
00031 {\
00032 bitbuf |= (val) << bitcnt;\
00033 bitcnt -= 2;\
00034 if (bitcnt < 0) {\
00035 bitcnt = 6;\
00036 *q++ = bitbuf;\
00037 bitbuf = 0;\
00038 }\
00039 }
00040
00041 static void dvb_encode_rle2(uint8_t **pq,
00042 const uint8_t *bitmap, int linesize,
00043 int w, int h)
00044 {
00045 uint8_t *q;
00046 unsigned int bitbuf;
00047 int bitcnt;
00048 int x, y, len, x1, v, color;
00049
00050 q = *pq;
00051
00052 for(y = 0; y < h; y++) {
00053 *q++ = 0x10;
00054 bitbuf = 0;
00055 bitcnt = 6;
00056
00057 x = 0;
00058 while (x < w) {
00059 x1 = x;
00060 color = bitmap[x1++];
00061 while (x1 < w && bitmap[x1] == color)
00062 x1++;
00063 len = x1 - x;
00064 if (color == 0 && len == 2) {
00065 PUTBITS2(0);
00066 PUTBITS2(0);
00067 PUTBITS2(1);
00068 } else if (len >= 3 && len <= 10) {
00069 v = len - 3;
00070 PUTBITS2(0);
00071 PUTBITS2((v >> 2) | 2);
00072 PUTBITS2(v & 3);
00073 PUTBITS2(color);
00074 } else if (len >= 12 && len <= 27) {
00075 v = len - 12;
00076 PUTBITS2(0);
00077 PUTBITS2(0);
00078 PUTBITS2(2);
00079 PUTBITS2(v >> 2);
00080 PUTBITS2(v & 3);
00081 PUTBITS2(color);
00082 } else if (len >= 29) {
00083
00084 if (len > 284)
00085 len = 284;
00086 v = len - 29;
00087 PUTBITS2(0);
00088 PUTBITS2(0);
00089 PUTBITS2(3);
00090 PUTBITS2((v >> 6));
00091 PUTBITS2((v >> 4) & 3);
00092 PUTBITS2((v >> 2) & 3);
00093 PUTBITS2(v & 3);
00094 PUTBITS2(color);
00095 } else {
00096 PUTBITS2(color);
00097 if (color == 0) {
00098 PUTBITS2(1);
00099 }
00100 len = 1;
00101 }
00102 x += len;
00103 }
00104
00105 PUTBITS2(0);
00106 PUTBITS2(0);
00107 PUTBITS2(0);
00108 if (bitcnt != 6) {
00109 *q++ = bitbuf;
00110 }
00111 *q++ = 0xf0;
00112 bitmap += linesize;
00113 }
00114 *pq = q;
00115 }
00116
00117 #define PUTBITS4(val)\
00118 {\
00119 bitbuf |= (val) << bitcnt;\
00120 bitcnt -= 4;\
00121 if (bitcnt < 0) {\
00122 bitcnt = 4;\
00123 *q++ = bitbuf;\
00124 bitbuf = 0;\
00125 }\
00126 }
00127
00128
00129 static void dvb_encode_rle4(uint8_t **pq,
00130 const uint8_t *bitmap, int linesize,
00131 int w, int h)
00132 {
00133 uint8_t *q;
00134 unsigned int bitbuf;
00135 int bitcnt;
00136 int x, y, len, x1, v, color;
00137
00138 q = *pq;
00139
00140 for(y = 0; y < h; y++) {
00141 *q++ = 0x11;
00142 bitbuf = 0;
00143 bitcnt = 4;
00144
00145 x = 0;
00146 while (x < w) {
00147 x1 = x;
00148 color = bitmap[x1++];
00149 while (x1 < w && bitmap[x1] == color)
00150 x1++;
00151 len = x1 - x;
00152 if (color == 0 && len == 2) {
00153 PUTBITS4(0);
00154 PUTBITS4(0xd);
00155 } else if (color == 0 && (len >= 3 && len <= 9)) {
00156 PUTBITS4(0);
00157 PUTBITS4(len - 2);
00158 } else if (len >= 4 && len <= 7) {
00159 PUTBITS4(0);
00160 PUTBITS4(8 + len - 4);
00161 PUTBITS4(color);
00162 } else if (len >= 9 && len <= 24) {
00163 PUTBITS4(0);
00164 PUTBITS4(0xe);
00165 PUTBITS4(len - 9);
00166 PUTBITS4(color);
00167 } else if (len >= 25) {
00168 if (len > 280)
00169 len = 280;
00170 v = len - 25;
00171 PUTBITS4(0);
00172 PUTBITS4(0xf);
00173 PUTBITS4(v >> 4);
00174 PUTBITS4(v & 0xf);
00175 PUTBITS4(color);
00176 } else {
00177 PUTBITS4(color);
00178 if (color == 0) {
00179 PUTBITS4(0xc);
00180 }
00181 len = 1;
00182 }
00183 x += len;
00184 }
00185
00186 PUTBITS4(0);
00187 PUTBITS4(0);
00188 if (bitcnt != 4) {
00189 *q++ = bitbuf;
00190 }
00191 *q++ = 0xf0;
00192 bitmap += linesize;
00193 }
00194 *pq = q;
00195 }
00196
00197 static int encode_dvb_subtitles(DVBSubtitleContext *s,
00198 uint8_t *outbuf, AVSubtitle *h)
00199 {
00200 uint8_t *q, *pseg_len;
00201 int page_id, region_id, clut_id, object_id, i, bpp_index, page_state;
00202
00203
00204 q = outbuf;
00205
00206 page_id = 1;
00207
00208 if (h->num_rects == 0 || h->rects == NULL)
00209 return -1;
00210
00211 *q++ = 0x00;
00212
00213
00214
00215 *q++ = 0x0f;
00216 *q++ = 0x10;
00217 bytestream_put_be16(&q, page_id);
00218 pseg_len = q;
00219 q += 2;
00220 *q++ = 30;
00221 if (s->hide_state)
00222 page_state = 0;
00223 else
00224 page_state = 2;
00225
00226 *q++ = s->object_version | (page_state << 2) | 3;
00227
00228 for (region_id = 0; region_id < h->num_rects; region_id++) {
00229 *q++ = region_id;
00230 *q++ = 0xff;
00231 bytestream_put_be16(&q, h->rects[region_id]->x);
00232 bytestream_put_be16(&q, h->rects[region_id]->y);
00233 }
00234
00235 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
00236
00237 if (!s->hide_state) {
00238 for (clut_id = 0; clut_id < h->num_rects; clut_id++) {
00239
00240
00241
00242 if (h->rects[clut_id]->nb_colors <= 4) {
00243
00244 bpp_index = 0;
00245 } else if (h->rects[clut_id]->nb_colors <= 16) {
00246
00247 bpp_index = 1;
00248 } else {
00249 return -1;
00250 }
00251
00252 *q++ = 0x0f;
00253 *q++ = 0x12;
00254 bytestream_put_be16(&q, page_id);
00255 pseg_len = q;
00256 q += 2;
00257 *q++ = clut_id;
00258 *q++ = (0 << 4) | 0xf;
00259
00260 for(i = 0; i < h->rects[clut_id]->nb_colors; i++) {
00261 *q++ = i;
00262 *q++ = (1 << (7 - bpp_index)) | (0xf << 1) | 1;
00263 {
00264 int a, r, g, b;
00265 uint32_t x= ((uint32_t*)h->rects[clut_id]->pict.data[1])[i];
00266 a = (x >> 24) & 0xff;
00267 r = (x >> 16) & 0xff;
00268 g = (x >> 8) & 0xff;
00269 b = (x >> 0) & 0xff;
00270
00271 *q++ = RGB_TO_Y_CCIR(r, g, b);
00272 *q++ = RGB_TO_V_CCIR(r, g, b, 0);
00273 *q++ = RGB_TO_U_CCIR(r, g, b, 0);
00274 *q++ = 255 - a;
00275 }
00276 }
00277
00278 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
00279 }
00280 }
00281
00282 for (region_id = 0; region_id < h->num_rects; region_id++) {
00283
00284
00285
00286 if (h->rects[region_id]->nb_colors <= 4) {
00287
00288 bpp_index = 0;
00289 } else if (h->rects[region_id]->nb_colors <= 16) {
00290
00291 bpp_index = 1;
00292 } else {
00293 return -1;
00294 }
00295
00296 *q++ = 0x0f;
00297 *q++ = 0x11;
00298 bytestream_put_be16(&q, page_id);
00299 pseg_len = q;
00300 q += 2;
00301 *q++ = region_id;
00302 *q++ = (s->object_version << 4) | (0 << 3) | 0x07;
00303 bytestream_put_be16(&q, h->rects[region_id]->w);
00304 bytestream_put_be16(&q, h->rects[region_id]->h);
00305 *q++ = ((1 + bpp_index) << 5) | ((1 + bpp_index) << 2) | 0x03;
00306 *q++ = region_id;
00307 *q++ = 0;
00308 *q++ = 0x03;
00309
00310 if (!s->hide_state) {
00311 bytestream_put_be16(&q, region_id);
00312 *q++ = (0 << 6) | (0 << 4);
00313 *q++ = 0;
00314 *q++ = 0xf0;
00315 *q++ = 0;
00316 }
00317
00318 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
00319 }
00320
00321 if (!s->hide_state) {
00322
00323 for (object_id = 0; object_id < h->num_rects; object_id++) {
00324
00325
00326 if (h->rects[object_id]->nb_colors <= 4) {
00327
00328 bpp_index = 0;
00329 } else if (h->rects[object_id]->nb_colors <= 16) {
00330
00331 bpp_index = 1;
00332 } else {
00333 return -1;
00334 }
00335
00336 *q++ = 0x0f;
00337 *q++ = 0x13;
00338 bytestream_put_be16(&q, page_id);
00339 pseg_len = q;
00340 q += 2;
00341
00342 bytestream_put_be16(&q, object_id);
00343 *q++ = (s->object_version << 4) | (0 << 2) | (0 << 1) | 1;
00344
00345
00346 {
00347 uint8_t *ptop_field_len, *pbottom_field_len, *top_ptr, *bottom_ptr;
00348 void (*dvb_encode_rle)(uint8_t **pq,
00349 const uint8_t *bitmap, int linesize,
00350 int w, int h);
00351 ptop_field_len = q;
00352 q += 2;
00353 pbottom_field_len = q;
00354 q += 2;
00355
00356 if (bpp_index == 0)
00357 dvb_encode_rle = dvb_encode_rle2;
00358 else
00359 dvb_encode_rle = dvb_encode_rle4;
00360
00361 top_ptr = q;
00362 dvb_encode_rle(&q, h->rects[object_id]->pict.data[0], h->rects[object_id]->w * 2,
00363 h->rects[object_id]->w, h->rects[object_id]->h >> 1);
00364 bottom_ptr = q;
00365 dvb_encode_rle(&q, h->rects[object_id]->pict.data[0] + h->rects[object_id]->w,
00366 h->rects[object_id]->w * 2, h->rects[object_id]->w,
00367 h->rects[object_id]->h >> 1);
00368
00369 bytestream_put_be16(&ptop_field_len, bottom_ptr - top_ptr);
00370 bytestream_put_be16(&pbottom_field_len, q - bottom_ptr);
00371 }
00372
00373 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
00374 }
00375 }
00376
00377
00378
00379 *q++ = 0x0f;
00380 *q++ = 0x80;
00381 bytestream_put_be16(&q, page_id);
00382 pseg_len = q;
00383 q += 2;
00384
00385 bytestream_put_be16(&pseg_len, q - pseg_len - 2);
00386
00387 *q++ = 0xff;
00388
00389 s->object_version = (s->object_version + 1) & 0xf;
00390 s->hide_state = !s->hide_state;
00391 return q - outbuf;
00392 }
00393
00394 static int dvbsub_encode(AVCodecContext *avctx,
00395 unsigned char *buf, int buf_size, void *data)
00396 {
00397 DVBSubtitleContext *s = avctx->priv_data;
00398 AVSubtitle *sub = data;
00399 int ret;
00400
00401 ret = encode_dvb_subtitles(s, buf, sub);
00402 return ret;
00403 }
00404
00405 AVCodec dvbsub_encoder = {
00406 "dvbsub",
00407 AVMEDIA_TYPE_SUBTITLE,
00408 CODEC_ID_DVB_SUBTITLE,
00409 sizeof(DVBSubtitleContext),
00410 NULL,
00411 dvbsub_encode,
00412 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
00413 };