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