LCOV - code coverage report
Current view: top level - libavcodec - htmlsubtitles.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 132 142 93.0 %
Date: 2017-12-16 21:16:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
       3             :  * Copyright (c) 2017  Clément Bœsch <u@pkh.me>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/avassert.h"
      23             : #include "libavutil/avstring.h"
      24             : #include "libavutil/common.h"
      25             : #include "libavutil/parseutils.h"
      26             : #include "htmlsubtitles.h"
      27             : 
      28          57 : static int html_color_parse(void *log_ctx, const char *str)
      29             : {
      30             :     uint8_t rgba[4];
      31          57 :     int nb_sharps = 0;
      32         136 :     while (str[nb_sharps] == '#')
      33          22 :         nb_sharps++;
      34          57 :     str += FFMAX(0, nb_sharps - 1);
      35          57 :     if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 0)
      36           0 :         return -1;
      37          57 :     return rgba[0] | rgba[1] << 8 | rgba[2] << 16;
      38             : }
      39             : 
      40         429 : static void rstrip_spaces_buf(AVBPrint *buf)
      41             : {
      42         429 :     if (av_bprint_is_complete(buf))
      43         975 :         while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
      44         117 :             buf->str[--buf->len] = 0;
      45         429 : }
      46             : 
      47             : /* skip all {\xxx} substrings except for {\an%d}
      48             :    and all microdvd like styles such as {Y:xxx} */
      49          48 : static void handle_open_brace(AVBPrint *dst, const char **inp, int *an, int *closing_brace_missing)
      50             : {
      51          48 :     int len = 0;
      52          48 :     const char *in = *inp;
      53             : 
      54          48 :     *an += sscanf(in, "{\\an%*1u}%n", &len) >= 0 && len > 0;
      55             : 
      56          48 :     if (!*closing_brace_missing) {
      57          48 :         if (   (*an != 1 && in[1] == '\\')
      58          42 :             || (in[1] && strchr("CcFfoPSsYy", in[1]) && in[2] == ':')) {
      59           9 :             char *bracep = strchr(in+2, '}');
      60           9 :             if (bracep) {
      61           9 :                 *inp = bracep;
      62          18 :                 return;
      63             :             } else
      64           0 :                 *closing_brace_missing = 1;
      65             :         }
      66             :     }
      67             : 
      68          39 :     av_bprint_chars(dst, *in, 1);
      69             : }
      70             : 
      71             : struct font_tag {
      72             :     char face[128];
      73             :     int size;
      74             :     uint32_t color;
      75             : };
      76             : 
      77             : /*
      78             :  * The general politic of the convert is to mask unsupported tags or formatting
      79             :  * errors (but still alert the user/subtitles writer with an error/warning)
      80             :  * without dropping any actual text content for the final user.
      81             :  */
      82         226 : int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
      83             : {
      84             :     char *param, buffer[128];
      85         226 :     int len, tag_close, sptr = 0, line_start = 1, an = 0, end = 0;
      86         226 :     int closing_brace_missing = 0;
      87             :     int i, likely_a_tag;
      88             : 
      89             :     /*
      90             :      * state stack is only present for fonts since they are the only tags where
      91             :      * the state is not binary. Here is a typical use case:
      92             :      *
      93             :      *   <font color="red" size=10>
      94             :      *     red 10
      95             :      *     <font size=50> RED AND BIG </font>
      96             :      *     red 10 again
      97             :      *   </font>
      98             :      *
      99             :      * On the other hand, using the state system for all the tags should be
     100             :      * avoided because it breaks wrongly nested tags such as:
     101             :      *
     102             :      *   <b> foo <i> bar </b> bla </i>
     103             :      *
     104             :      * We don't want to break here; instead, we will treat all these tags as
     105             :      * binary state markers. Basically, "<b>" will activate bold, and "</b>"
     106             :      * will deactivate it, whatever the current state.
     107             :      *
     108             :      * This will also prevents cases where we have a random closing tag
     109             :      * remaining after the opening one was dropped. Yes, this happens and we
     110             :      * still don't want to print a "</b>" at the end of the dialog event.
     111             :      */
     112             :     struct font_tag stack[16];
     113             : 
     114         226 :     memset(&stack[0], 0, sizeof(stack[0]));
     115             : 
     116       16613 :     for (; !end && *in; in++) {
     117       16387 :         switch (*in) {
     118           0 :         case '\r':
     119           0 :             break;
     120         203 :         case '\n':
     121         203 :             if (line_start) {
     122           0 :                 end = 1;
     123           0 :                 break;
     124             :             }
     125         203 :             rstrip_spaces_buf(dst);
     126         203 :             av_bprintf(dst, "\\N");
     127         203 :             line_start = 1;
     128         203 :             break;
     129        2243 :         case ' ':
     130        2243 :             if (!line_start)
     131        2213 :                 av_bprint_chars(dst, *in, 1);
     132        2243 :             break;
     133          48 :         case '{':
     134          48 :             handle_open_brace(dst, &in, &an, &closing_brace_missing);
     135          48 :             break;
     136         422 :         case '<':
     137             :             /*
     138             :              * "<<" are likely latin guillemets in ASCII or some kind of random
     139             :              * style effect; see sub/badsyntax.srt in the FATE samples
     140             :              * directory for real test cases.
     141             :              */
     142             : 
     143         422 :             likely_a_tag = 1;
     144         449 :             for (i = 0; in[1] == '<'; i++) {
     145          27 :                 av_bprint_chars(dst, '<', 1);
     146          27 :                 likely_a_tag = 0;
     147          27 :                 in++;
     148             :             }
     149             : 
     150         422 :             tag_close = in[1] == '/';
     151         422 :             if (tag_close)
     152         192 :                 likely_a_tag = 1;
     153             : 
     154         422 :             av_assert0(in[0] == '<');
     155             : 
     156         422 :             len = 0;
     157             : 
     158         829 :             if (sscanf(in+tag_close+1, "%127[^<>]>%n", buffer, &len) >= 1 && len > 0) {
     159         407 :                 const int skip = len + tag_close;
     160         407 :                 const char *tagname = buffer;
     161         819 :                 while (*tagname == ' ') {
     162           5 :                     likely_a_tag = 0;
     163           5 :                     tagname++;
     164             :                 }
     165         407 :                 if ((param = strchr(tagname, ' ')))
     166         103 :                     *param++ = 0;
     167             : 
     168             :                 /* Check if this is likely a tag */
     169             : #define LIKELY_A_TAG_CHAR(x) (((x) >= '0' && (x) <= '9') || \
     170             :                               ((x) >= 'a' && (x) <= 'z') || \
     171             :                               ((x) >= 'A' && (x) <= 'Z') || \
     172             :                                (x) == '_' || (x) == '/')
     173        1876 :                 for (i = 0; tagname[i]; i++) {
     174        1475 :                     if (!LIKELY_A_TAG_CHAR(tagname[i])) {
     175           6 :                         likely_a_tag = 0;
     176           6 :                         break;
     177             :                     }
     178             :                 }
     179             : 
     180         407 :                 if (!av_strcasecmp(tagname, "font")) {
     181         270 :                     if (tag_close && sptr > 0) {
     182          90 :                         struct font_tag *cur_tag  = &stack[sptr--];
     183          90 :                         struct font_tag *last_tag = &stack[sptr];
     184             : 
     185          90 :                         if (cur_tag->size) {
     186          48 :                             if (!last_tag->size)
     187          42 :                                 av_bprintf(dst, "{\\fs}");
     188           6 :                             else if (last_tag->size != cur_tag->size)
     189           3 :                                 av_bprintf(dst, "{\\fs%d}", last_tag->size);
     190             :                         }
     191             : 
     192          90 :                         if (cur_tag->color & 0xff000000) {
     193          76 :                             if (!(last_tag->color & 0xff000000))
     194          53 :                                 av_bprintf(dst, "{\\c}");
     195          23 :                             else if (last_tag->color != cur_tag->color)
     196           4 :                                 av_bprintf(dst, "{\\c&H%"PRIX32"&}", last_tag->color & 0xffffff);
     197             :                         }
     198             : 
     199          90 :                         if (cur_tag->face[0]) {
     200           3 :                             if (!last_tag->face[0])
     201           3 :                                 av_bprintf(dst, "{\\fn}");
     202           0 :                             else if (strcmp(last_tag->face, cur_tag->face))
     203           0 :                                 av_bprintf(dst, "{\\fn%s}", last_tag->face);
     204             :                         }
     205          90 :                     } else if (!tag_close && sptr < FF_ARRAY_ELEMS(stack) - 1) {
     206          90 :                         struct font_tag *new_tag = &stack[sptr + 1];
     207             : 
     208          90 :                         *new_tag = stack[sptr++];
     209             : 
     210         286 :                         while (param) {
     211         106 :                             if (!av_strncasecmp(param, "size=", 5)) {
     212          45 :                                 param += 5 + (param[5] == '"');
     213          45 :                                 if (sscanf(param, "%u", &new_tag->size) == 1)
     214          45 :                                     av_bprintf(dst, "{\\fs%u}", new_tag->size);
     215          61 :                             } else if (!av_strncasecmp(param, "color=", 6)) {
     216             :                                 int color;
     217          57 :                                 param += 6 + (param[6] == '"');
     218          57 :                                 color = html_color_parse(log_ctx, param);
     219          57 :                                 if (color >= 0) {
     220          57 :                                     new_tag->color = 0xff000000 | color;
     221          57 :                                     av_bprintf(dst, "{\\c&H%"PRIX32"&}", new_tag->color & 0xffffff);
     222             :                                 }
     223           4 :                             } else if (!av_strncasecmp(param, "face=", 5)) {
     224           3 :                                 param += 5 + (param[5] == '"');
     225           3 :                                 len = strcspn(param,
     226           3 :                                               param[-1] == '"' ? "\"" :" ");
     227           6 :                                 av_strlcpy(new_tag->face, param,
     228           6 :                                            FFMIN(sizeof(new_tag->face), len+1));
     229           3 :                                 param += len;
     230           3 :                                 av_bprintf(dst, "{\\fn%s}", new_tag->face);
     231             :                             }
     232         106 :                             if ((param = strchr(param, ' ')))
     233          16 :                                 param++;
     234             :                         }
     235             :                     }
     236         180 :                     in += skip;
     237         227 :                 } else if (tagname[0] && !tagname[1] && strchr("bisu", av_tolower(tagname[0]))) {
     238         116 :                     av_bprintf(dst, "{\\%c%d}", (char)av_tolower(tagname[0]), !tag_close);
     239         116 :                     in += skip;
     240         117 :                 } else if (!av_strncasecmp(tagname, "br", 2) &&
     241           9 :                            (!tagname[2] || (tagname[2] == '/' && !tagname[3]))) {
     242           4 :                     av_bprintf(dst, "\\N");
     243           4 :                     in += skip;
     244         107 :                 } else if (likely_a_tag) {
     245          96 :                     if (!tag_close) // warn only once
     246          51 :                         av_log(log_ctx, AV_LOG_WARNING, "Unrecognized tag %s\n", tagname);
     247          96 :                     in += skip;
     248             :                 } else {
     249          11 :                     av_bprint_chars(dst, '<', 1);
     250             :                 }
     251             :             } else {
     252          15 :                 av_bprint_chars(dst, *in, 1);
     253             :             }
     254         422 :             break;
     255       13471 :         default:
     256       13471 :             av_bprint_chars(dst, *in, 1);
     257       13471 :             break;
     258             :         }
     259       16387 :         if (*in != ' ' && *in != '\r' && *in != '\n')
     260       13941 :             line_start = 0;
     261             :     }
     262             : 
     263         226 :     if (!av_bprint_is_complete(dst))
     264           0 :         return AVERROR(ENOMEM);
     265             : 
     266         452 :     while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
     267           0 :         dst->len -= 2;
     268         226 :     dst->str[dst->len] = 0;
     269         226 :     rstrip_spaces_buf(dst);
     270             : 
     271         226 :     return 0;
     272             : }

Generated by: LCOV version 1.13