LCOV - code coverage report
Current view: top level - src/libavcodec - ass.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 45 75.6 %
Date: 2017-05-26 21:11:57 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  * SSA/ASS common functions
       3             :  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
       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 "avcodec.h"
      23             : #include "ass.h"
      24             : #include "libavutil/avassert.h"
      25             : #include "libavutil/avstring.h"
      26             : #include "libavutil/bprint.h"
      27             : #include "libavutil/common.h"
      28             : 
      29          56 : int ff_ass_subtitle_header(AVCodecContext *avctx,
      30             :                            const char *font, int font_size,
      31             :                            int color, int back_color,
      32             :                            int bold, int italic, int underline,
      33             :                            int border_style, int alignment)
      34             : {
      35         112 :     avctx->subtitle_header = av_asprintf(
      36             :              "[Script Info]\r\n"
      37             :              "; Script generated by FFmpeg/Lavc%s\r\n"
      38             :              "ScriptType: v4.00+\r\n"
      39             :              "PlayResX: %d\r\n"
      40             :              "PlayResY: %d\r\n"
      41             :              "\r\n"
      42             :              "[V4+ Styles]\r\n"
      43             : 
      44             :              /* ASSv4 header */
      45             :              "Format: Name, "
      46             :              "Fontname, Fontsize, "
      47             :              "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
      48             :              "Bold, Italic, Underline, StrikeOut, "
      49             :              "ScaleX, ScaleY, "
      50             :              "Spacing, Angle, "
      51             :              "BorderStyle, Outline, Shadow, "
      52             :              "Alignment, MarginL, MarginR, MarginV, "
      53             :              "Encoding\r\n"
      54             : 
      55             :              "Style: "
      56             :              "Default,"             /* Name */
      57             :              "%s,%d,"               /* Font{name,size} */
      58             :              "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
      59             :              "%d,%d,%d,0,"          /* Bold, Italic, Underline, StrikeOut */
      60             :              "100,100,"             /* Scale{X,Y} */
      61             :              "0,0,"                 /* Spacing, Angle */
      62             :              "%d,1,0,"              /* BorderStyle, Outline, Shadow */
      63             :              "%d,10,10,10,"         /* Alignment, Margin[LRV] */
      64             :              "0\r\n"                /* Encoding */
      65             : 
      66             :              "\r\n"
      67             :              "[Events]\r\n"
      68             :              "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
      69          56 :              !(avctx->flags & AV_CODEC_FLAG_BITEXACT) ? AV_STRINGIFY(LIBAVCODEC_VERSION) : "",
      70             :              ASS_DEFAULT_PLAYRESX, ASS_DEFAULT_PLAYRESY,
      71             :              font, font_size, color, color, back_color, back_color,
      72             :              -bold, -italic, -underline, border_style, alignment);
      73             : 
      74          56 :     if (!avctx->subtitle_header)
      75           0 :         return AVERROR(ENOMEM);
      76          56 :     avctx->subtitle_header_size = strlen(avctx->subtitle_header);
      77          56 :     return 0;
      78             : }
      79             : 
      80          39 : int ff_ass_subtitle_header_default(AVCodecContext *avctx)
      81             : {
      82          39 :     return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
      83             :                                ASS_DEFAULT_FONT_SIZE,
      84             :                                ASS_DEFAULT_COLOR,
      85             :                                ASS_DEFAULT_BACK_COLOR,
      86             :                                ASS_DEFAULT_BOLD,
      87             :                                ASS_DEFAULT_ITALIC,
      88             :                                ASS_DEFAULT_UNDERLINE,
      89             :                                ASS_DEFAULT_BORDERSTYLE,
      90             :                                ASS_DEFAULT_ALIGNMENT);
      91             : }
      92             : 
      93         531 : char *ff_ass_get_dialog(int readorder, int layer, const char *style,
      94             :                         const char *speaker, const char *text)
      95             : {
      96         531 :     return av_asprintf("%d,%d,%s,%s,0,0,0,,%s",
      97             :                        readorder, layer, style ? style : "Default",
      98             :                        speaker ? speaker : "", text);
      99             : }
     100             : 
     101         531 : int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
     102             :                     int readorder, int layer, const char *style,
     103             :                     const char *speaker)
     104             : {
     105             :     char *ass_str;
     106             :     AVSubtitleRect **rects;
     107             : 
     108         531 :     rects = av_realloc_array(sub->rects, (sub->num_rects+1), sizeof(*sub->rects));
     109         531 :     if (!rects)
     110           0 :         return AVERROR(ENOMEM);
     111         531 :     sub->rects = rects;
     112         531 :     rects[sub->num_rects]       = av_mallocz(sizeof(*rects[0]));
     113         531 :     if (!rects[sub->num_rects])
     114           0 :         return AVERROR(ENOMEM);
     115         531 :     rects[sub->num_rects]->type = SUBTITLE_ASS;
     116         531 :     ass_str = ff_ass_get_dialog(readorder, layer, style, speaker, dialog);
     117         531 :     if (!ass_str)
     118           0 :         return AVERROR(ENOMEM);
     119         531 :     rects[sub->num_rects]->ass = ass_str;
     120         531 :     sub->num_rects++;
     121         531 :     return 0;
     122             : }
     123             : 
     124           0 : void ff_ass_decoder_flush(AVCodecContext *avctx)
     125             : {
     126           0 :     FFASSDecoderContext *s = avctx->priv_data;
     127           0 :     if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
     128           0 :         s->readorder = 0;
     129           0 : }
     130             : 
     131          89 : void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
     132             :                              const char *linebreaks, int keep_ass_markup)
     133             : {
     134          89 :     const char *p_end = p + size;
     135             : 
     136        3249 :     for (; p < p_end && *p; p++) {
     137             : 
     138             :         /* forced custom line breaks, not accounted as "normal" EOL */
     139        3160 :         if (linebreaks && strchr(linebreaks, *p)) {
     140          18 :             av_bprintf(buf, "\\N");
     141             : 
     142             :         /* standard ASS escaping so random characters don't get mis-interpreted
     143             :          * as ASS */
     144        3142 :         } else if (!keep_ass_markup && strchr("{}\\", *p)) {
     145           0 :             av_bprintf(buf, "\\%c", *p);
     146             : 
     147             :         /* some packets might end abruptly (no \0 at the end, like for example
     148             :          * in some cases of demuxing from a classic video container), some
     149             :          * might be terminated with \n or \r\n which we have to remove (for
     150             :          * consistency with those who haven't), and we also have to deal with
     151             :          * evil cases such as \r at the end of the buffer (and no \0 terminated
     152             :          * character) */
     153        3142 :         } else if (p[0] == '\n') {
     154             :             /* some stuff left so we can insert a line break */
     155          35 :             if (p < p_end - 1)
     156          35 :                 av_bprintf(buf, "\\N");
     157        3107 :         } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
     158             :             /* \r followed by a \n, we can skip it. We don't insert the \N yet
     159             :              * because we don't know if it is followed by more text */
     160           0 :             continue;
     161             : 
     162             :         /* finally, a sane character */
     163             :         } else {
     164        3107 :             av_bprint_chars(buf, *p, 1);
     165             :         }
     166             :     }
     167          89 : }

Generated by: LCOV version 1.13