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