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 "get_bits.h"
00023 #include "dsputil.h"
00024 #include "libavutil/colorspace.h"
00025 #include "libavutil/opt.h"
00026 #include "libavutil/imgutils.h"
00027
00028
00029
00030 typedef struct DVDSubContext
00031 {
00032 AVClass *class;
00033 uint32_t palette[16];
00034 char *palette_str;
00035 int has_palette;
00036 uint8_t colormap[4];
00037 uint8_t alpha[256];
00038 } DVDSubContext;
00039
00040 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
00041 {
00042 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00043 uint8_t r, g, b;
00044 int i, y, cb, cr;
00045 int r_add, g_add, b_add;
00046
00047 for (i = num_values; i > 0; i--) {
00048 y = *ycbcr++;
00049 cr = *ycbcr++;
00050 cb = *ycbcr++;
00051 YUV_TO_RGB1_CCIR(cb, cr);
00052 YUV_TO_RGB2_CCIR(r, g, b, y);
00053 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
00054 }
00055 }
00056
00057 static int decode_run_2bit(GetBitContext *gb, int *color)
00058 {
00059 unsigned int v, t;
00060
00061 v = 0;
00062 for (t = 1; v < t && t <= 0x40; t <<= 2)
00063 v = (v << 4) | get_bits(gb, 4);
00064 *color = v & 3;
00065 if (v < 4) {
00066 return INT_MAX;
00067 }
00068 return v >> 2;
00069 }
00070
00071 static int decode_run_8bit(GetBitContext *gb, int *color)
00072 {
00073 int len;
00074 int has_run = get_bits1(gb);
00075 if (get_bits1(gb))
00076 *color = get_bits(gb, 8);
00077 else
00078 *color = get_bits(gb, 2);
00079 if (has_run) {
00080 if (get_bits1(gb)) {
00081 len = get_bits(gb, 7);
00082 if (len == 0)
00083 len = INT_MAX;
00084 else
00085 len += 9;
00086 } else
00087 len = get_bits(gb, 3) + 2;
00088 } else
00089 len = 1;
00090 return len;
00091 }
00092
00093 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
00094 const uint8_t *buf, int start, int buf_size, int is_8bit)
00095 {
00096 GetBitContext gb;
00097 int bit_len;
00098 int x, y, len, color;
00099 uint8_t *d;
00100
00101 bit_len = (buf_size - start) * 8;
00102 init_get_bits(&gb, buf + start, bit_len);
00103
00104 x = 0;
00105 y = 0;
00106 d = bitmap;
00107 for(;;) {
00108 if (get_bits_count(&gb) > bit_len)
00109 return -1;
00110 if (is_8bit)
00111 len = decode_run_8bit(&gb, &color);
00112 else
00113 len = decode_run_2bit(&gb, &color);
00114 len = FFMIN(len, w - x);
00115 memset(d + x, color, len);
00116 x += len;
00117 if (x >= w) {
00118 y++;
00119 if (y >= h)
00120 break;
00121 d += linesize;
00122 x = 0;
00123
00124 align_get_bits(&gb);
00125 }
00126 }
00127 return 0;
00128 }
00129
00130 static void guess_palette(uint32_t *rgba_palette,
00131 DVDSubContext* ctx,
00132 uint32_t subtitle_color)
00133 {
00134 static const uint8_t level_map[4][4] = {
00135
00136
00137 {0xff},
00138 {0x00, 0xff},
00139 {0x00, 0x80, 0xff},
00140 {0x00, 0x55, 0xaa, 0xff},
00141 };
00142 uint8_t color_used[16] = { 0 };
00143 int nb_opaque_colors, i, level, j, r, g, b;
00144 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
00145
00146 if(ctx->has_palette) {
00147 for(i = 0; i < 4; i++)
00148 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
00149 | ((alpha[i] * 17) << 24);
00150 return;
00151 }
00152
00153 for(i = 0; i < 4; i++)
00154 rgba_palette[i] = 0;
00155
00156 nb_opaque_colors = 0;
00157 for(i = 0; i < 4; i++) {
00158 if (alpha[i] != 0 && !color_used[colormap[i]]) {
00159 color_used[colormap[i]] = 1;
00160 nb_opaque_colors++;
00161 }
00162 }
00163
00164 if (nb_opaque_colors == 0)
00165 return;
00166
00167 j = 0;
00168 memset(color_used, 0, 16);
00169 for(i = 0; i < 4; i++) {
00170 if (alpha[i] != 0) {
00171 if (!color_used[colormap[i]]) {
00172 level = level_map[nb_opaque_colors][j];
00173 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
00174 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
00175 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
00176 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
00177 color_used[colormap[i]] = (i + 1);
00178 j++;
00179 } else {
00180 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
00181 ((alpha[i] * 17) << 24);
00182 }
00183 }
00184 }
00185 }
00186
00187 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
00188
00189 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
00190 const uint8_t *buf, int buf_size)
00191 {
00192 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
00193 int big_offsets, offset_size, is_8bit = 0;
00194 const uint8_t *yuv_palette = 0;
00195 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
00196 int date;
00197 int i;
00198 int is_menu = 0;
00199
00200 if (buf_size < 10)
00201 return -1;
00202
00203 if (AV_RB16(buf) == 0) {
00204 big_offsets = 1;
00205 offset_size = 4;
00206 cmd_pos = 6;
00207 } else {
00208 big_offsets = 0;
00209 offset_size = 2;
00210 cmd_pos = 2;
00211 }
00212
00213 cmd_pos = READ_OFFSET(buf + cmd_pos);
00214
00215 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
00216 date = AV_RB16(buf + cmd_pos);
00217 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
00218 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
00219 cmd_pos, next_cmd_pos, date);
00220 pos = cmd_pos + 2 + offset_size;
00221 offset1 = -1;
00222 offset2 = -1;
00223 x1 = y1 = x2 = y2 = 0;
00224 while (pos < buf_size) {
00225 cmd = buf[pos++];
00226 av_dlog(NULL, "cmd=%02x\n", cmd);
00227 switch(cmd) {
00228 case 0x00:
00229
00230 is_menu = 1;
00231 break;
00232 case 0x01:
00233
00234 sub_header->start_display_time = (date << 10) / 90;
00235 break;
00236 case 0x02:
00237
00238 sub_header->end_display_time = (date << 10) / 90;
00239 break;
00240 case 0x03:
00241
00242 if ((buf_size - pos) < 2)
00243 goto fail;
00244 colormap[3] = buf[pos] >> 4;
00245 colormap[2] = buf[pos] & 0x0f;
00246 colormap[1] = buf[pos + 1] >> 4;
00247 colormap[0] = buf[pos + 1] & 0x0f;
00248 pos += 2;
00249 break;
00250 case 0x04:
00251
00252 if ((buf_size - pos) < 2)
00253 goto fail;
00254 alpha[3] = buf[pos] >> 4;
00255 alpha[2] = buf[pos] & 0x0f;
00256 alpha[1] = buf[pos + 1] >> 4;
00257 alpha[0] = buf[pos + 1] & 0x0f;
00258 pos += 2;
00259 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
00260 break;
00261 case 0x05:
00262 case 0x85:
00263 if ((buf_size - pos) < 6)
00264 goto fail;
00265 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
00266 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
00267 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
00268 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
00269 if (cmd & 0x80)
00270 is_8bit = 1;
00271 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
00272 pos += 6;
00273 break;
00274 case 0x06:
00275 if ((buf_size - pos) < 4)
00276 goto fail;
00277 offset1 = AV_RB16(buf + pos);
00278 offset2 = AV_RB16(buf + pos + 2);
00279 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
00280 pos += 4;
00281 break;
00282 case 0x86:
00283 if ((buf_size - pos) < 8)
00284 goto fail;
00285 offset1 = AV_RB32(buf + pos);
00286 offset2 = AV_RB32(buf + pos + 4);
00287 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
00288 pos += 8;
00289 break;
00290
00291 case 0x83:
00292
00293 if ((buf_size - pos) < 768)
00294 goto fail;
00295 yuv_palette = buf + pos;
00296 pos += 768;
00297 break;
00298 case 0x84:
00299
00300 if ((buf_size - pos) < 256)
00301 goto fail;
00302 for (i = 0; i < 256; i++)
00303 alpha[i] = 0xFF - buf[pos+i];
00304 pos += 256;
00305 break;
00306
00307 case 0xff:
00308 goto the_end;
00309 default:
00310 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
00311 goto the_end;
00312 }
00313 }
00314 the_end:
00315 if (offset1 >= 0) {
00316 int w, h;
00317 uint8_t *bitmap;
00318
00319
00320 w = x2 - x1 + 1;
00321 if (w < 0)
00322 w = 0;
00323 h = y2 - y1;
00324 if (h < 0)
00325 h = 0;
00326 if (w > 0 && h > 0) {
00327 if (sub_header->rects != NULL) {
00328 for (i = 0; i < sub_header->num_rects; i++) {
00329 av_freep(&sub_header->rects[i]->pict.data[0]);
00330 av_freep(&sub_header->rects[i]->pict.data[1]);
00331 av_freep(&sub_header->rects[i]);
00332 }
00333 av_freep(&sub_header->rects);
00334 sub_header->num_rects = 0;
00335 }
00336
00337 bitmap = av_malloc(w * h);
00338 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
00339 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
00340 sub_header->num_rects = 1;
00341 sub_header->rects[0]->pict.data[0] = bitmap;
00342 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
00343 buf, offset1, buf_size, is_8bit);
00344 decode_rle(bitmap + w, w * 2, w, h / 2,
00345 buf, offset2, buf_size, is_8bit);
00346 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00347 if (is_8bit) {
00348 if (yuv_palette == 0)
00349 goto fail;
00350 sub_header->rects[0]->nb_colors = 256;
00351 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
00352 } else {
00353 sub_header->rects[0]->nb_colors = 4;
00354 guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1], ctx,
00355 0xffff00);
00356 }
00357 sub_header->rects[0]->x = x1;
00358 sub_header->rects[0]->y = y1;
00359 sub_header->rects[0]->w = w;
00360 sub_header->rects[0]->h = h;
00361 sub_header->rects[0]->type = SUBTITLE_BITMAP;
00362 sub_header->rects[0]->pict.linesize[0] = w;
00363 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
00364 }
00365 }
00366 if (next_cmd_pos < cmd_pos) {
00367 av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
00368 break;
00369 }
00370 if (next_cmd_pos == cmd_pos)
00371 break;
00372 cmd_pos = next_cmd_pos;
00373 }
00374 if (sub_header->num_rects > 0)
00375 return is_menu;
00376 fail:
00377 if (sub_header->rects != NULL) {
00378 for (i = 0; i < sub_header->num_rects; i++) {
00379 av_freep(&sub_header->rects[i]->pict.data[0]);
00380 av_freep(&sub_header->rects[i]->pict.data[1]);
00381 av_freep(&sub_header->rects[i]);
00382 }
00383 av_freep(&sub_header->rects);
00384 sub_header->num_rects = 0;
00385 }
00386 return -1;
00387 }
00388
00389 static int is_transp(const uint8_t *buf, int pitch, int n,
00390 const uint8_t *transp_color)
00391 {
00392 int i;
00393 for(i = 0; i < n; i++) {
00394 if (!transp_color[*buf])
00395 return 0;
00396 buf += pitch;
00397 }
00398 return 1;
00399 }
00400
00401
00402 static int find_smallest_bounding_rectangle(AVSubtitle *s)
00403 {
00404 uint8_t transp_color[256] = { 0 };
00405 int y1, y2, x1, x2, y, w, h, i;
00406 uint8_t *bitmap;
00407
00408 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
00409 return 0;
00410
00411 for(i = 0; i < s->rects[0]->nb_colors; i++) {
00412 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
00413 transp_color[i] = 1;
00414 }
00415 y1 = 0;
00416 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
00417 1, s->rects[0]->w, transp_color))
00418 y1++;
00419 if (y1 == s->rects[0]->h) {
00420 av_freep(&s->rects[0]->pict.data[0]);
00421 s->rects[0]->w = s->rects[0]->h = 0;
00422 return 0;
00423 }
00424
00425 y2 = s->rects[0]->h - 1;
00426 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
00427 s->rects[0]->w, transp_color))
00428 y2--;
00429 x1 = 0;
00430 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
00431 s->rects[0]->h, transp_color))
00432 x1++;
00433 x2 = s->rects[0]->w - 1;
00434 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
00435 transp_color))
00436 x2--;
00437 w = x2 - x1 + 1;
00438 h = y2 - y1 + 1;
00439 bitmap = av_malloc(w * h);
00440 if (!bitmap)
00441 return 1;
00442 for(y = 0; y < h; y++) {
00443 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
00444 }
00445 av_freep(&s->rects[0]->pict.data[0]);
00446 s->rects[0]->pict.data[0] = bitmap;
00447 s->rects[0]->pict.linesize[0] = w;
00448 s->rects[0]->w = w;
00449 s->rects[0]->h = h;
00450 s->rects[0]->x += x1;
00451 s->rects[0]->y += y1;
00452 return 1;
00453 }
00454
00455 #ifdef DEBUG
00456 #undef fprintf
00457 #undef perror
00458 #undef exit
00459 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
00460 uint32_t *rgba_palette)
00461 {
00462 int x, y, v;
00463 FILE *f;
00464
00465 f = fopen(filename, "w");
00466 if (!f) {
00467 perror(filename);
00468 exit(1);
00469 }
00470 fprintf(f, "P6\n"
00471 "%d %d\n"
00472 "%d\n",
00473 w, h, 255);
00474 for(y = 0; y < h; y++) {
00475 for(x = 0; x < w; x++) {
00476 v = rgba_palette[bitmap[y * w + x]];
00477 putc((v >> 16) & 0xff, f);
00478 putc((v >> 8) & 0xff, f);
00479 putc((v >> 0) & 0xff, f);
00480 }
00481 }
00482 fclose(f);
00483 }
00484 #endif
00485
00486 static int dvdsub_decode(AVCodecContext *avctx,
00487 void *data, int *data_size,
00488 AVPacket *avpkt)
00489 {
00490 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
00491 const uint8_t *buf = avpkt->data;
00492 int buf_size = avpkt->size;
00493 AVSubtitle *sub = data;
00494 int is_menu;
00495
00496 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
00497
00498 if (is_menu < 0) {
00499 no_subtitle:
00500 *data_size = 0;
00501
00502 return buf_size;
00503 }
00504 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
00505 goto no_subtitle;
00506
00507 #if defined(DEBUG)
00508 av_dlog(NULL, "start=%d ms end =%d ms\n",
00509 sub->start_display_time,
00510 sub->end_display_time);
00511 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
00512 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
00513 #endif
00514
00515 *data_size = 1;
00516 return buf_size;
00517 }
00518
00519 static void parse_palette(DVDSubContext *ctx, char *p)
00520 {
00521 int i;
00522
00523 ctx->has_palette = 1;
00524 for(i=0;i<16;i++) {
00525 ctx->palette[i] = strtoul(p, &p, 16);
00526 while(*p == ',' || isspace(*p))
00527 p++;
00528 }
00529 }
00530
00531 static int dvdsub_parse_extradata(AVCodecContext *avctx)
00532 {
00533 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
00534 char *dataorig, *data;
00535
00536 if (!avctx->extradata || !avctx->extradata_size)
00537 return 1;
00538
00539 dataorig = data = av_malloc(avctx->extradata_size+1);
00540 if (!data)
00541 return AVERROR(ENOMEM);
00542 memcpy(data, avctx->extradata, avctx->extradata_size);
00543 data[avctx->extradata_size] = '\0';
00544
00545 for(;;) {
00546 int pos = strcspn(data, "\n\r");
00547 if (pos==0 && *data==0)
00548 break;
00549
00550 if (strncmp("palette:", data, 8) == 0) {
00551 parse_palette(ctx, data + 8);
00552 } else if (strncmp("size:", data, 5) == 0) {
00553 int w, h;
00554 if (sscanf(data + 5, "%dx%d", &w, &h) == 2 &&
00555 av_image_check_size(w, h, 0, avctx) >= 0)
00556 avcodec_set_dimensions(avctx, w, h);
00557 }
00558
00559 data += pos;
00560 data += strspn(data, "\n\r");
00561 }
00562
00563 av_free(dataorig);
00564 return 1;
00565 }
00566
00567 static int dvdsub_init(AVCodecContext *avctx)
00568 {
00569 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
00570 int ret;
00571
00572 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
00573 return ret;
00574
00575 if (ctx->palette_str)
00576 parse_palette(ctx, ctx->palette_str);
00577 if (ctx->has_palette) {
00578 int i;
00579 av_log(avctx, AV_LOG_DEBUG, "palette:");
00580 for(i=0;i<16;i++)
00581 av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
00582 av_log(avctx, AV_LOG_DEBUG, "\n");
00583 }
00584
00585 return 1;
00586 }
00587
00588 #define OFFSET(field) offsetof(DVDSubContext, field)
00589 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
00590 static const AVOption options[] = {
00591 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
00592 { NULL }
00593 };
00594 static const AVClass class = {
00595 .class_name = "dvdsubdec",
00596 .item_name = av_default_item_name,
00597 .option = options,
00598 .version = LIBAVUTIL_VERSION_INT,
00599 };
00600
00601 AVCodec ff_dvdsub_decoder = {
00602 .name = "dvdsub",
00603 .type = AVMEDIA_TYPE_SUBTITLE,
00604 .id = AV_CODEC_ID_DVD_SUBTITLE,
00605 .priv_data_size = sizeof(DVDSubContext),
00606 .init = dvdsub_init,
00607 .decode = dvdsub_decode,
00608 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
00609 .priv_class = &class,
00610 };