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