libavcodec/dvdsubdec.c
Go to the documentation of this file.
00001 /*
00002  * DVD subtitle decoding
00003  * Copyright (c) 2005 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avcodec.h"
00022 #include "get_bits.h"
00023 #include "dsputil.h"
00024 #include "libavutil/colorspace.h"
00025 
00026 //#define DEBUG
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) { /* Code for fill rest of line */
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             /* byte align */
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         // this configuration (full range, lowest to highest) in tests
00132         // seemed most common, so assume this
00133         {0xff},
00134         {0x00, 0xff},
00135         {0x00, 0x80, 0xff},
00136         {0x00, 0x55, 0xaa, 0xff},
00137     };
00138     uint8_t color_used[16];
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     memset(color_used, 0, 16);
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) {   /* HD subpicture with 4-byte offsets */
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                 /* menu subpicture */
00227                 is_menu = 1;
00228                 break;
00229             case 0x01:
00230                 /* set start date */
00231                 sub_header->start_display_time = (date << 10) / 90;
00232                 break;
00233             case 0x02:
00234                 /* set end date */
00235                 sub_header->end_display_time = (date << 10) / 90;
00236                 break;
00237             case 0x03:
00238                 /* set colormap */
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                 /* set alpha */
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                 /* HD set palette */
00290                 if ((buf_size - pos) < 768)
00291                     goto fail;
00292                 yuv_palette = buf + pos;
00293                 pos += 768;
00294                 break;
00295             case 0x84:
00296                 /* HD set contrast (alpha) */
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             /* decode the bitmap */
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             }
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 /* return 0 if empty rectangle, 1 if non empty */
00398 static int find_smallest_bounding_rectangle(AVSubtitle *s)
00399 {
00400     uint8_t transp_color[256];
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     memset(transp_color, 0, 256);
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         }
00545 
00546         data += pos;
00547         data += strspn(data, "\n\r");
00548     }
00549 
00550     if (ctx->has_palette) {
00551         int i;
00552         av_log(avctx, AV_LOG_DEBUG, "palette:");
00553         for(i=0;i<16;i++)
00554             av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
00555         av_log(avctx, AV_LOG_DEBUG, "\n");
00556     }
00557 
00558     av_free(dataorig);
00559     return 1;
00560 }
00561 
00562 AVCodec ff_dvdsub_decoder = {
00563     .name           = "dvdsub",
00564     .type           = AVMEDIA_TYPE_SUBTITLE,
00565     .id             = CODEC_ID_DVD_SUBTITLE,
00566     .priv_data_size = sizeof(DVDSubContext),
00567     .init           = dvdsub_init,
00568     .decode         = dvdsub_decode,
00569     .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
00570 };