• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/dvdsubdec.c

Go to the documentation of this file.
00001 /*
00002  * DVD subtitle decoding for ffmpeg
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 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         cr = *ycbcr++;
00038         cb = *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) { /* Code for fill rest of line */
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             /* byte align */
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     static const uint8_t level_map[4][4] = {
00124         // this configuration (full range, lowest to highest) in tests
00125         // seemed most common, so assume this
00126         {0xff},
00127         {0x00, 0xff},
00128         {0x00, 0x80, 0xff},
00129         {0x00, 0x55, 0xaa, 0xff},
00130     };
00131     uint8_t color_used[16];
00132     int nb_opaque_colors, i, level, j, r, g, b;
00133 
00134     for(i = 0; i < 4; i++)
00135         rgba_palette[i] = 0;
00136 
00137     memset(color_used, 0, 16);
00138     nb_opaque_colors = 0;
00139     for(i = 0; i < 4; i++) {
00140         if (alpha[i] != 0 && !color_used[colormap[i]]) {
00141             color_used[colormap[i]] = 1;
00142             nb_opaque_colors++;
00143         }
00144     }
00145 
00146     if (nb_opaque_colors == 0)
00147         return;
00148 
00149     j = 0;
00150     memset(color_used, 0, 16);
00151     for(i = 0; i < 4; i++) {
00152         if (alpha[i] != 0) {
00153             if (!color_used[colormap[i]])  {
00154                 level = level_map[nb_opaque_colors][j];
00155                 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
00156                 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
00157                 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
00158                 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
00159                 color_used[colormap[i]] = (i + 1);
00160                 j++;
00161             } else {
00162                 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
00163                                     ((alpha[i] * 17) << 24);
00164             }
00165         }
00166     }
00167 }
00168 
00169 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
00170 
00171 static int decode_dvd_subtitles(AVSubtitle *sub_header,
00172                                 const uint8_t *buf, int buf_size)
00173 {
00174     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
00175     int big_offsets, offset_size, is_8bit = 0;
00176     const uint8_t *yuv_palette = 0;
00177     uint8_t colormap[4], alpha[256];
00178     int date;
00179     int i;
00180     int is_menu = 0;
00181 
00182     if (buf_size < 10)
00183         return -1;
00184 
00185     if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
00186         big_offsets = 1;
00187         offset_size = 4;
00188         cmd_pos = 6;
00189     } else {
00190         big_offsets = 0;
00191         offset_size = 2;
00192         cmd_pos = 2;
00193     }
00194 
00195     cmd_pos = READ_OFFSET(buf + cmd_pos);
00196 
00197     while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
00198         date = AV_RB16(buf + cmd_pos);
00199         next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
00200         av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
00201                 cmd_pos, next_cmd_pos, date);
00202         pos = cmd_pos + 2 + offset_size;
00203         offset1 = -1;
00204         offset2 = -1;
00205         x1 = y1 = x2 = y2 = 0;
00206         while (pos < buf_size) {
00207             cmd = buf[pos++];
00208             av_dlog(NULL, "cmd=%02x\n", cmd);
00209             switch(cmd) {
00210             case 0x00:
00211                 /* menu subpicture */
00212                 is_menu = 1;
00213                 break;
00214             case 0x01:
00215                 /* set start date */
00216                 sub_header->start_display_time = (date << 10) / 90;
00217                 break;
00218             case 0x02:
00219                 /* set end date */
00220                 sub_header->end_display_time = (date << 10) / 90;
00221                 break;
00222             case 0x03:
00223                 /* set colormap */
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                 /* set alpha */
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             av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
00242                 break;
00243             case 0x05:
00244             case 0x85:
00245                 if ((buf_size - pos) < 6)
00246                     goto fail;
00247                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
00248                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
00249                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
00250                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
00251                 if (cmd & 0x80)
00252                     is_8bit = 1;
00253                 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
00254                 pos += 6;
00255                 break;
00256             case 0x06:
00257                 if ((buf_size - pos) < 4)
00258                     goto fail;
00259                 offset1 = AV_RB16(buf + pos);
00260                 offset2 = AV_RB16(buf + pos + 2);
00261                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
00262                 pos += 4;
00263                 break;
00264             case 0x86:
00265                 if ((buf_size - pos) < 8)
00266                     goto fail;
00267                 offset1 = AV_RB32(buf + pos);
00268                 offset2 = AV_RB32(buf + pos + 4);
00269                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
00270                 pos += 8;
00271                 break;
00272 
00273             case 0x83:
00274                 /* HD set palette */
00275                 if ((buf_size - pos) < 768)
00276                     goto fail;
00277                 yuv_palette = buf + pos;
00278                 pos += 768;
00279                 break;
00280             case 0x84:
00281                 /* HD set contrast (alpha) */
00282                 if ((buf_size - pos) < 256)
00283                     goto fail;
00284                 for (i = 0; i < 256; i++)
00285                     alpha[i] = 0xFF - buf[pos+i];
00286                 pos += 256;
00287                 break;
00288 
00289             case 0xff:
00290                 goto the_end;
00291             default:
00292                 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
00293                 goto the_end;
00294             }
00295         }
00296     the_end:
00297         if (offset1 >= 0) {
00298             int w, h;
00299             uint8_t *bitmap;
00300 
00301             /* decode the bitmap */
00302             w = x2 - x1 + 1;
00303             if (w < 0)
00304                 w = 0;
00305             h = y2 - y1;
00306             if (h < 0)
00307                 h = 0;
00308             if (w > 0 && h > 0) {
00309                 if (sub_header->rects != NULL) {
00310                     for (i = 0; i < sub_header->num_rects; i++) {
00311                         av_freep(&sub_header->rects[i]->pict.data[0]);
00312                         av_freep(&sub_header->rects[i]->pict.data[1]);
00313                         av_freep(&sub_header->rects[i]);
00314                     }
00315                     av_freep(&sub_header->rects);
00316                     sub_header->num_rects = 0;
00317                 }
00318 
00319                 bitmap = av_malloc(w * h);
00320                 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
00321                 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
00322                 sub_header->num_rects = 1;
00323                 sub_header->rects[0]->pict.data[0] = bitmap;
00324                 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
00325                            buf, offset1, buf_size, is_8bit);
00326                 decode_rle(bitmap + w, w * 2, w, h / 2,
00327                            buf, offset2, buf_size, is_8bit);
00328                 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
00329                 if (is_8bit) {
00330                     if (yuv_palette == 0)
00331                         goto fail;
00332                     sub_header->rects[0]->nb_colors = 256;
00333                     yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
00334                 } else {
00335                     sub_header->rects[0]->nb_colors = 4;
00336                     guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
00337                                   colormap, alpha, 0xffff00);
00338                 }
00339                 sub_header->rects[0]->x = x1;
00340                 sub_header->rects[0]->y = y1;
00341                 sub_header->rects[0]->w = w;
00342                 sub_header->rects[0]->h = h;
00343                 sub_header->rects[0]->type = SUBTITLE_BITMAP;
00344                 sub_header->rects[0]->pict.linesize[0] = w;
00345             }
00346         }
00347         if (next_cmd_pos == cmd_pos)
00348             break;
00349         cmd_pos = next_cmd_pos;
00350     }
00351     if (sub_header->num_rects > 0)
00352         return is_menu;
00353  fail:
00354     if (sub_header->rects != NULL) {
00355         for (i = 0; i < sub_header->num_rects; i++) {
00356             av_freep(&sub_header->rects[i]->pict.data[0]);
00357             av_freep(&sub_header->rects[i]->pict.data[1]);
00358             av_freep(&sub_header->rects[i]);
00359         }
00360         av_freep(&sub_header->rects);
00361         sub_header->num_rects = 0;
00362     }
00363     return -1;
00364 }
00365 
00366 static int is_transp(const uint8_t *buf, int pitch, int n,
00367                      const uint8_t *transp_color)
00368 {
00369     int i;
00370     for(i = 0; i < n; i++) {
00371         if (!transp_color[*buf])
00372             return 0;
00373         buf += pitch;
00374     }
00375     return 1;
00376 }
00377 
00378 /* return 0 if empty rectangle, 1 if non empty */
00379 static int find_smallest_bounding_rectangle(AVSubtitle *s)
00380 {
00381     uint8_t transp_color[256];
00382     int y1, y2, x1, x2, y, w, h, i;
00383     uint8_t *bitmap;
00384 
00385     if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
00386         return 0;
00387 
00388     memset(transp_color, 0, 256);
00389     for(i = 0; i < s->rects[0]->nb_colors; i++) {
00390         if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
00391             transp_color[i] = 1;
00392     }
00393     y1 = 0;
00394     while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
00395                                   1, s->rects[0]->w, transp_color))
00396         y1++;
00397     if (y1 == s->rects[0]->h) {
00398         av_freep(&s->rects[0]->pict.data[0]);
00399         s->rects[0]->w = s->rects[0]->h = 0;
00400         return 0;
00401     }
00402 
00403     y2 = s->rects[0]->h - 1;
00404     while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
00405                                s->rects[0]->w, transp_color))
00406         y2--;
00407     x1 = 0;
00408     while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
00409                                         s->rects[0]->h, transp_color))
00410         x1++;
00411     x2 = s->rects[0]->w - 1;
00412     while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
00413                                   transp_color))
00414         x2--;
00415     w = x2 - x1 + 1;
00416     h = y2 - y1 + 1;
00417     bitmap = av_malloc(w * h);
00418     if (!bitmap)
00419         return 1;
00420     for(y = 0; y < h; y++) {
00421         memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
00422     }
00423     av_freep(&s->rects[0]->pict.data[0]);
00424     s->rects[0]->pict.data[0] = bitmap;
00425     s->rects[0]->pict.linesize[0] = w;
00426     s->rects[0]->w = w;
00427     s->rects[0]->h = h;
00428     s->rects[0]->x += x1;
00429     s->rects[0]->y += y1;
00430     return 1;
00431 }
00432 
00433 #ifdef DEBUG
00434 #undef fprintf
00435 #undef perror
00436 #undef exit
00437 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
00438                      uint32_t *rgba_palette)
00439 {
00440     int x, y, v;
00441     FILE *f;
00442 
00443     f = fopen(filename, "w");
00444     if (!f) {
00445         perror(filename);
00446         exit(1);
00447     }
00448     fprintf(f, "P6\n"
00449             "%d %d\n"
00450             "%d\n",
00451             w, h, 255);
00452     for(y = 0; y < h; y++) {
00453         for(x = 0; x < w; x++) {
00454             v = rgba_palette[bitmap[y * w + x]];
00455             putc((v >> 16) & 0xff, f);
00456             putc((v >> 8) & 0xff, f);
00457             putc((v >> 0) & 0xff, f);
00458         }
00459     }
00460     fclose(f);
00461 }
00462 #endif
00463 
00464 static int dvdsub_decode(AVCodecContext *avctx,
00465                          void *data, int *data_size,
00466                          AVPacket *avpkt)
00467 {
00468     const uint8_t *buf = avpkt->data;
00469     int buf_size = avpkt->size;
00470     AVSubtitle *sub = data;
00471     int is_menu;
00472 
00473     is_menu = decode_dvd_subtitles(sub, buf, buf_size);
00474 
00475     if (is_menu < 0) {
00476     no_subtitle:
00477         *data_size = 0;
00478 
00479         return buf_size;
00480     }
00481     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
00482         goto no_subtitle;
00483 
00484 #if defined(DEBUG)
00485     av_dlog(NULL, "start=%d ms end =%d ms\n",
00486             sub->start_display_time,
00487             sub->end_display_time);
00488     ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
00489              sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
00490 #endif
00491 
00492     *data_size = 1;
00493     return buf_size;
00494 }
00495 
00496 AVCodec ff_dvdsub_decoder = {
00497     "dvdsub",
00498     AVMEDIA_TYPE_SUBTITLE,
00499     CODEC_ID_DVD_SUBTITLE,
00500     0,
00501     NULL,
00502     NULL,
00503     NULL,
00504     dvdsub_decode,
00505     .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
00506 };

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1