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->format = 0;
00179 sub_header->start_display_time = 0;
00180 sub_header->end_display_time = 0;
00181
00182 if (AV_RB16(buf) == 0) {
00183 big_offsets = 1;
00184 offset_size = 4;
00185 cmd_pos = 6;
00186 } else {
00187 big_offsets = 0;
00188 offset_size = 2;
00189 cmd_pos = 2;
00190 }
00191
00192 cmd_pos = READ_OFFSET(buf + cmd_pos);
00193
00194 while ((cmd_pos + 2 + offset_size) < buf_size) {
00195 date = AV_RB16(buf + cmd_pos);
00196 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
00197 #ifdef DEBUG
00198 av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
00199 cmd_pos, next_cmd_pos, date);
00200 #endif
00201 pos = cmd_pos + 2 + offset_size;
00202 offset1 = -1;
00203 offset2 = -1;
00204 x1 = y1 = x2 = y2 = 0;
00205 while (pos < buf_size) {
00206 cmd = buf[pos++];
00207 #ifdef DEBUG
00208 av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
00209 #endif
00210 switch(cmd) {
00211 case 0x00:
00212
00213 is_menu = 1;
00214 break;
00215 case 0x01:
00216
00217 sub_header->start_display_time = (date << 10) / 90;
00218 break;
00219 case 0x02:
00220
00221 sub_header->end_display_time = (date << 10) / 90;
00222 break;
00223 case 0x03:
00224
00225 if ((buf_size - pos) < 2)
00226 goto fail;
00227 colormap[3] = buf[pos] >> 4;
00228 colormap[2] = buf[pos] & 0x0f;
00229 colormap[1] = buf[pos + 1] >> 4;
00230 colormap[0] = buf[pos + 1] & 0x0f;
00231 pos += 2;
00232 break;
00233 case 0x04:
00234
00235 if ((buf_size - pos) < 2)
00236 goto fail;
00237 alpha[3] = buf[pos] >> 4;
00238 alpha[2] = buf[pos] & 0x0f;
00239 alpha[1] = buf[pos + 1] >> 4;
00240 alpha[0] = buf[pos + 1] & 0x0f;
00241 pos += 2;
00242 #ifdef DEBUG
00243 av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
00244 #endif
00245 break;
00246 case 0x05:
00247 case 0x85:
00248 if ((buf_size - pos) < 6)
00249 goto fail;
00250 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
00251 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
00252 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
00253 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
00254 if (cmd & 0x80)
00255 is_8bit = 1;
00256 #ifdef DEBUG
00257 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
00258 x1, x2, y1, y2);
00259 #endif
00260 pos += 6;
00261 break;
00262 case 0x06:
00263 if ((buf_size - pos) < 4)
00264 goto fail;
00265 offset1 = AV_RB16(buf + pos);
00266 offset2 = AV_RB16(buf + pos + 2);
00267 #ifdef DEBUG
00268 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
00269 #endif
00270 pos += 4;
00271 break;
00272 case 0x86:
00273 if ((buf_size - pos) < 8)
00274 goto fail;
00275 offset1 = AV_RB32(buf + pos);
00276 offset2 = AV_RB32(buf + pos + 4);
00277 #ifdef DEBUG
00278 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
00279 #endif
00280 pos += 8;
00281 break;
00282
00283 case 0x83:
00284
00285 if ((buf_size - pos) < 768)
00286 goto fail;
00287 yuv_palette = buf + pos;
00288 pos += 768;
00289 break;
00290 case 0x84:
00291
00292 if ((buf_size - pos) < 256)
00293 goto fail;
00294 for (i = 0; i < 256; i++)
00295 alpha[i] = 0xFF - buf[pos+i];
00296 pos += 256;
00297 break;
00298
00299 case 0xff:
00300 goto the_end;
00301 default:
00302 #ifdef DEBUG
00303 av_log(NULL, AV_LOG_INFO, "unrecognised subpicture command 0x%x\n", cmd);
00304 #endif
00305 goto the_end;
00306 }
00307 }
00308 the_end:
00309 if (offset1 >= 0) {
00310 int w, h;
00311 uint8_t *bitmap;
00312
00313
00314 w = x2 - x1 + 1;
00315 if (w < 0)
00316 w = 0;
00317 h = y2 - y1;
00318 if (h < 0)
00319 h = 0;
00320 if (w > 0 && h > 0) {
00321 if (sub_header->rects != NULL) {
00322 for (i = 0; i < sub_header->num_rects; i++) {
00323 av_freep(&sub_header->rects[i]->pict.data[0]);
00324 av_freep(&sub_header->rects[i]->pict.data[1]);
00325 av_freep(&sub_header->rects[i]);
00326 }
00327 av_freep(&sub_header->rects);
00328 sub_header->num_rects = 0;
00329 }
00330
00331 bitmap = av_malloc(w * h);
00332 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
00333 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
00334 sub_header->num_rects = 1;
00335 sub_header->rects[0]->pict.data[0] = bitmap;
00336 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
00337 buf, offset1, buf_size, is_8bit);
00338 decode_rle(bitmap + w, w * 2, w, h / 2,
00339 buf, offset2, buf_size, is_8bit);
00340 if (is_8bit) {
00341 if (yuv_palette == 0)
00342 goto fail;
00343 sub_header->rects[0]->pict.data[1] = av_malloc(256 * 4);
00344 sub_header->rects[0]->nb_colors = 256;
00345 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
00346 } else {
00347 sub_header->rects[0]->pict.data[1] = av_malloc(4 * 4);
00348 sub_header->rects[0]->nb_colors = 4;
00349 guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
00350 colormap, alpha, 0xffff00);
00351 }
00352 sub_header->rects[0]->x = x1;
00353 sub_header->rects[0]->y = y1;
00354 sub_header->rects[0]->w = w;
00355 sub_header->rects[0]->h = h;
00356 sub_header->rects[0]->pict.linesize[0] = w;
00357 }
00358 }
00359 if (next_cmd_pos == cmd_pos)
00360 break;
00361 cmd_pos = next_cmd_pos;
00362 }
00363 if (sub_header->num_rects > 0)
00364 return is_menu;
00365 fail:
00366 if (sub_header->rects != NULL) {
00367 for (i = 0; i < sub_header->num_rects; i++) {
00368 av_freep(&sub_header->rects[i]->pict.data[0]);
00369 av_freep(&sub_header->rects[i]->pict.data[1]);
00370 av_freep(&sub_header->rects[i]);
00371 }
00372 av_freep(&sub_header->rects);
00373 sub_header->num_rects = 0;
00374 }
00375 return -1;
00376 }
00377
00378 static int is_transp(const uint8_t *buf, int pitch, int n,
00379 const uint8_t *transp_color)
00380 {
00381 int i;
00382 for(i = 0; i < n; i++) {
00383 if (!transp_color[*buf])
00384 return 0;
00385 buf += pitch;
00386 }
00387 return 1;
00388 }
00389
00390
00391 static int find_smallest_bounding_rectangle(AVSubtitle *s)
00392 {
00393 uint8_t transp_color[256];
00394 int y1, y2, x1, x2, y, w, h, i;
00395 uint8_t *bitmap;
00396
00397 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
00398 return 0;
00399
00400 memset(transp_color, 0, 256);
00401 for(i = 0; i < s->rects[0]->nb_colors; i++) {
00402 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
00403 transp_color[i] = 1;
00404 }
00405 y1 = 0;
00406 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
00407 1, s->rects[0]->w, transp_color))
00408 y1++;
00409 if (y1 == s->rects[0]->h) {
00410 av_freep(&s->rects[0]->pict.data[0]);
00411 s->rects[0]->w = s->rects[0]->h = 0;
00412 return 0;
00413 }
00414
00415 y2 = s->rects[0]->h - 1;
00416 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
00417 s->rects[0]->w, transp_color))
00418 y2--;
00419 x1 = 0;
00420 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
00421 s->rects[0]->h, transp_color))
00422 x1++;
00423 x2 = s->rects[0]->w - 1;
00424 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
00425 transp_color))
00426 x2--;
00427 w = x2 - x1 + 1;
00428 h = y2 - y1 + 1;
00429 bitmap = av_malloc(w * h);
00430 if (!bitmap)
00431 return 1;
00432 for(y = 0; y < h; y++) {
00433 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
00434 }
00435 av_freep(&s->rects[0]->pict.data[0]);
00436 s->rects[0]->pict.data[0] = bitmap;
00437 s->rects[0]->pict.linesize[0] = w;
00438 s->rects[0]->w = w;
00439 s->rects[0]->h = h;
00440 s->rects[0]->x += x1;
00441 s->rects[0]->y += y1;
00442 return 1;
00443 }
00444
00445 #ifdef DEBUG
00446 #undef fprintf
00447 #undef perror
00448 #undef exit
00449 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
00450 uint32_t *rgba_palette)
00451 {
00452 int x, y, v;
00453 FILE *f;
00454
00455 f = fopen(filename, "w");
00456 if (!f) {
00457 perror(filename);
00458 exit(1);
00459 }
00460 fprintf(f, "P6\n"
00461 "%d %d\n"
00462 "%d\n",
00463 w, h, 255);
00464 for(y = 0; y < h; y++) {
00465 for(x = 0; x < w; x++) {
00466 v = rgba_palette[bitmap[y * w + x]];
00467 putc((v >> 16) & 0xff, f);
00468 putc((v >> 8) & 0xff, f);
00469 putc((v >> 0) & 0xff, f);
00470 }
00471 }
00472 fclose(f);
00473 }
00474 #endif
00475
00476 static int dvdsub_decode(AVCodecContext *avctx,
00477 void *data, int *data_size,
00478 const uint8_t *buf, int buf_size)
00479 {
00480 AVSubtitle *sub = (void *)data;
00481 int is_menu;
00482
00483 is_menu = decode_dvd_subtitles(sub, buf, buf_size);
00484
00485 if (is_menu < 0) {
00486 no_subtitle:
00487 *data_size = 0;
00488
00489 return buf_size;
00490 }
00491 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
00492 goto no_subtitle;
00493
00494 #if defined(DEBUG)
00495 av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
00496 sub->start_display_time,
00497 sub->end_display_time);
00498 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
00499 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
00500 #endif
00501
00502 *data_size = 1;
00503 return buf_size;
00504 }
00505
00506 AVCodec dvdsub_decoder = {
00507 "dvdsub",
00508 CODEC_TYPE_SUBTITLE,
00509 CODEC_ID_DVD_SUBTITLE,
00510 0,
00511 NULL,
00512 NULL,
00513 NULL,
00514 dvdsub_decode,
00515 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
00516 };