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