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