static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
{
    SAMIContext *sami = avctx->priv_data;
    int ret = 0;
    char *tag = NULL;
    char *dupsrc = av_strdup(src);
    char *p = dupsrc;
    AVBPrint *dst_content = &sami->encoded_content;
    AVBPrint *dst_source = &sami->encoded_source;
    av_bprint_clear(&sami->encoded_content);
    av_bprint_clear(&sami->content);
    av_bprint_clear(&sami->encoded_source);
    for (;;) {
        char *saveptr = NULL;
        int prev_chr_is_space = 0;
        AVBPrint *dst = &sami->content;
        /* parse & extract paragraph tag */
        p = av_stristr(p, "<P");
        if (!p)
            break;
        if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
            p++;
            continue;
        }
        if (dst->len) // add a separator with the previous paragraph if there was one
            av_bprintf(dst, "\\N");
        tag = av_strtok(p, ">", &saveptr);
        if (!tag || !saveptr)
            break;
        p = saveptr;
        /* check if the current paragraph is the "source" (speaker name) */
        if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
            dst = &sami->source;
            av_bprint_clear(dst);
        }
        /* if empty event -> skip subtitle */
        while (av_isspace(*p))
            p++;
        if (!strncmp(p, "&nbsp;", 6)) {
            ret = -1;
            goto end;
        }
        /* extract the text, stripping most of the tags */
        while (*p) {
            if (*p == '<') {
                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
                    break;
            }
            if (!av_strncasecmp(p, "<BR", 3)) {
                av_bprintf(dst, "\\N");
                p++;
                while (*p && *p != '>')
                    p++;
                if (!*p)
                    break;
                if (*p == '>')
                    p++;
                continue;
            }
            if (!av_isspace(*p))
                av_bprint_chars(dst, *p, 1);
            else if (!prev_chr_is_space)
                av_bprint_chars(dst, ' ', 1);
            prev_chr_is_space = av_isspace(*p);
            p++;
        }
    }
    av_bprint_clear(&sami->full);
    if (sami->source.len) {
        ret = ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
        if (ret < 0)
            goto end;
        av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
    }
    ret = ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
    if (ret < 0)
        goto end;
    av_bprintf(&sami->full, "%s", sami->encoded_content.str);
end:
    av_free(dupsrc);
    return ret;
}
static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
{
    SAMIContext *sami = avctx->priv_data;
    int ret = 0;
    char *tag = NULL;
    char *dupsrc = av_strdup(src);
    char *p = dupsrc;
    AVBPrint *dst_content = &sami->encoded_content;
    AVBPrint *dst_source = &sami->encoded_source;
    av_bprint_clear(&sami->encoded_content);
    av_bprint_clear(&sami->content);
    av_bprint_clear(&sami->encoded_source);
    for (;;) {
        char *saveptr = NULL;
        int prev_chr_is_space = 0;
        AVBPrint *dst = &sami->content;
        
        p = av_stristr(p, "<P");
        if (!p)
            break;
        if (p[2] != '>' && !av_isspace(p[2])) { 
            p++;
            continue;
        }
        if (dst->len) 
            av_bprintf(dst, "\\N");
        tag = av_strtok(p, ">", &saveptr);
        if (!tag || !saveptr)
            break;
        p = saveptr;
        
        if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
            dst = &sami->source;
            av_bprint_clear(dst);
        }
        
        while (av_isspace(*p))
            p++;
        if (!strncmp(p, "&nbsp;", 6)) {
            ret = -1;
            goto end;
        }
        
        while (*p) {
            if (*p == '<') {
                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
                    break;
            }
            if (!av_strncasecmp(p, "<BR", 3)) {
                av_bprintf(dst, "\\N");
                p++;
                while (*p && *p != '>')
                    p++;
                if (!*p)
                    break;
                if (*p == '>')
                    p++;
                continue;
            }
            if (!av_isspace(*p))
                av_bprint_chars(dst, *p, 1);
            else if (!prev_chr_is_space)
                av_bprint_chars(dst, ' ', 1);
            prev_chr_is_space = av_isspace(*p);
            p++;
        }
    }
    av_bprint_clear(&sami->full);
    if (sami->source.len) {
        ret = ff_htmlmarkup_to_ass(avctx, dst_source, sami->source.str);
        if (ret < 0)
            goto end;
        av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
    }
    ret = ff_htmlmarkup_to_ass(avctx, dst_content, sami->content.str);
    if (ret < 0)
        goto end;
    av_bprintf(&sami->full, "%s", sami->encoded_content.str);
end:
    av_free(dupsrc);
    return ret;
}
static int FUNC_0(AVCodecContext *VAR_0, const char *VAR_1)
{
    SAMIContext *sami = VAR_0->priv_data;
    int VAR_2 = 0;
    char *VAR_3 = NULL;
    char *VAR_4 = av_strdup(VAR_1);
    char *VAR_5 = VAR_4;
    AVBPrint *dst_content = &sami->encoded_content;
    AVBPrint *dst_source = &sami->encoded_source;
    av_bprint_clear(&sami->encoded_content);
    av_bprint_clear(&sami->content);
    av_bprint_clear(&sami->encoded_source);
    for (;;) {
        char *VAR_6 = NULL;
        int VAR_7 = 0;
        AVBPrint *dst = &sami->content;
        
        VAR_5 = av_stristr(VAR_5, "<P");
        if (!VAR_5)
            break;
        if (VAR_5[2] != '>' && !av_isspace(VAR_5[2])) { 
            VAR_5++;
            continue;
        }
        if (dst->len) 
            av_bprintf(dst, "\\N");
        VAR_3 = av_strtok(VAR_5, ">", &VAR_6);
        if (!VAR_3 || !VAR_6)
            break;
        VAR_5 = VAR_6;
        
        if (av_stristr(VAR_3, "ID=Source") || av_stristr(VAR_3, "ID=\"Source\"")) {
            dst = &sami->source;
            av_bprint_clear(dst);
        }
        
        while (av_isspace(*VAR_5))
            VAR_5++;
        if (!strncmp(VAR_5, "&nbsp;", 6)) {
            VAR_2 = -1;
            goto end;
        }
        
        while (*VAR_5) {
            if (*VAR_5 == '<') {
                if (!av_strncasecmp(VAR_5, "<P", 2) && (VAR_5[2] == '>' || av_isspace(VAR_5[2])))
                    break;
            }
            if (!av_strncasecmp(VAR_5, "<BR", 3)) {
                av_bprintf(dst, "\\N");
                VAR_5++;
                while (*VAR_5 && *VAR_5 != '>')
                    VAR_5++;
                if (!*VAR_5)
                    break;
                if (*VAR_5 == '>')
                    VAR_5++;
                continue;
            }
            if (!av_isspace(*VAR_5))
                av_bprint_chars(dst, *VAR_5, 1);
            else if (!VAR_7)
                av_bprint_chars(dst, ' ', 1);
            VAR_7 = av_isspace(*VAR_5);
            VAR_5++;
        }
    }
    av_bprint_clear(&sami->full);
    if (sami->source.len) {
        VAR_2 = ff_htmlmarkup_to_ass(VAR_0, dst_source, sami->source.str);
        if (VAR_2 < 0)
            goto end;
        av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str);
    }
    VAR_2 = ff_htmlmarkup_to_ass(VAR_0, dst_content, sami->content.str);
    if (VAR_2 < 0)
        goto end;
    av_bprintf(&sami->full, "%s", sami->encoded_content.str);
end:
    av_free(VAR_4);
    return VAR_2;
}
static int FUNC_0(AVCodecContext *VAR_0, const char *VAR_1)
{ SAMIContext *sami = VAR_0->priv_data; int VAR_2 = 0; char *VAR_3 = NULL; char *VAR_4 = av_strdup(VAR_1); char *VAR_5 = VAR_4; AVBPrint *dst_content = &sami->encoded_content; AVBPrint *dst_source = &sami->encoded_source; av_bprint_clear(&sami->encoded_content); av_bprint_clear(&sami->content); av_bprint_clear(&sami->encoded_source); for (;;) { char *VAR_6 = NULL; int VAR_7 = 0; AVBPrint *dst = &sami->content; VAR_5 = av_stristr(VAR_5, "<P"); if (!VAR_5)
break; if (VAR_5[2] != '>' && !av_isspace(VAR_5[2])) { VAR_5++; continue; } if (dst->len)
av_bprintf(dst, "\\N"); VAR_3 = av_strtok(VAR_5, ">", &VAR_6); if (!VAR_3 || !VAR_6)
break; VAR_5 = VAR_6; if (av_stristr(VAR_3, "ID=Source") || av_stristr(VAR_3, "ID=\"Source\"")) { dst = &sami->source; av_bprint_clear(dst); } while (av_isspace(*VAR_5))
VAR_5++; if (!strncmp(VAR_5, "&nbsp;", 6)) { VAR_2 = -1; goto end; } while (*VAR_5) { if (*VAR_5 == '<') { if (!av_strncasecmp(VAR_5, "<P", 2) && (VAR_5[2] == '>' || av_isspace(VAR_5[2])))
break; } if (!av_strncasecmp(VAR_5, "<BR", 3)) { av_bprintf(dst, "\\N"); VAR_5++; while (*VAR_5 && *VAR_5 != '>')
VAR_5++; if (!*VAR_5)
break; if (*VAR_5 == '>')
VAR_5++; continue; } if (!av_isspace(*VAR_5))
av_bprint_chars(dst, *VAR_5, 1); else if (!VAR_7)
av_bprint_chars(dst, ' ', 1); VAR_7 = av_isspace(*VAR_5); VAR_5++; } } av_bprint_clear(&sami->full); if (sami->source.len) { VAR_2 = ff_htmlmarkup_to_ass(VAR_0, dst_source, sami->source.str); if (VAR_2 < 0)
goto end; av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->encoded_source.str); } VAR_2 = ff_htmlmarkup_to_ass(VAR_0, dst_content, sami->content.str); if (VAR_2 < 0)
goto end; av_bprintf(&sami->full, "%s", sami->encoded_content.str); end:
av_free(VAR_4); return VAR_2; }