[FFmpeg-devel] [PATCH] avcodec/samidec: process more of the SAMI tags

Clément Bœsch u at pkh.me
Sun Mar 15 14:33:38 CET 2015


On Sun, Mar 15, 2015 at 05:28:15PM +0900, Won-woo Choi wrote:
> Made some changes to samidec so that it can process SAMI tags.
> Processes <B> and <FONT>(font face, color).
> ---
>  libavcodec/samidec.c | 37 +++++++++++++++++++++++++++++++++++--
>  1 file changed, 35 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/samidec.c b/libavcodec/samidec.c
> index 47850e2..b47204d 100644
> --- a/libavcodec/samidec.c
> +++ b/libavcodec/samidec.c
> @@ -25,6 +25,7 @@
>   */
>  
>  #include "ass.h"
> +#include "libavformat/subtitles.h"
>  #include "libavutil/avstring.h"
>  #include "libavutil/bprint.h"
>  
> @@ -77,13 +78,45 @@ static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
>              goto end;
>          }
>  
> -        /* extract the text, stripping most of the tags */
> +        /* extract the text, processing some of the tags */
>          while (*p) {
> +            int bold = 0;
>              if (*p == '<') {
> -                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
> +                if ( !av_strncasecmp(p, "<P", 2) &&
> +                     (p[2] == '>' || av_isspace(p[2])) )
>                      break;
> +                if ( !av_strncasecmp(p, "<B", 2) &&
> +                     (p[2] == '>' || av_isspace(p[2])) ) {
> +                    bold = 1;
> +                    av_bprintf(dst, "{\\b1}");
> +                }
> +                if (!av_strncasecmp(p, "</B>", 4)) {
> +                    bold = 0;
> +                    av_bprintf(dst, "{\\b0}");
> +                }
>                  if (!av_strncasecmp(p, "<BR", 3))
>                      av_bprintf(dst, "\\N");
> +                if (!av_strncasecmp(p, "<FONT", 5)) {
> +                    const char *p_color = ff_smil_get_attr_ptr(p, "Color");
> +                    const char *p_face = ff_smil_get_attr_ptr(p, "Face");
> +                    const char *p_face_end = p_face;
> +
> +                    if (p_color) {
> +                        /* ASS color code order is BGR, opposite to SAMI */
> +                        av_bprintf(dst, "{\\c&H%.2s%.2s%.2s&}",
> +                                   p_color+4, p_color+2, p_color);
> +                    }
> +                    if (p_face) {
> +                        while (*p_face_end != '\"' && *p_face_end != ' ')
> +                            p_face_end++;
> +                        av_bprintf(dst, "{\\fn%.*s}",
> +                                   (int)(p_face_end - p_face), p_face);
> +                    }
> +                }
> +                if (!av_strncasecmp(p, "</FONT>", 7)) {
> +                    av_bprintf(dst, "{\\r}");
> +                    if (bold) av_bprintf(dst, "{\\b1}");
> +                }
>                  p++;
>                  while (*p && *p != '>')
>                      p++;

While this might work as a temporary workaround, it is not correct. For
example, it won't properly handle nested tags. It doesn't handle the HTML
color names either, and will generate a mess out of it. 

Someone I know is working on refactoring this code with the HTML-like tag
parsing from SubRip, which is the correct way of dealing with that issue.
I hope it will get done soon. If not, feel free to ping in a month or so.

Regards,

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20150315/cc04e3af/attachment.asc>


More information about the ffmpeg-devel mailing list