Ticket #9808: webvtt-unescape.diff

File webvtt-unescape.diff, 1.7 KB (added by tfischer, 4 years ago)

Unescaping '{' and '}'

  • libavcodec/webvttenc.c

    diff --git a/libavcodec/webvttenc.c b/libavcodec/webvttenc.c
    index 4369aacb74..39d4f7c28a 100644
    a b  
    2121 */
    2222
    2323#include <stdarg.h>
     24#include <strings.h>
    2425#include "avcodec.h"
    2526#include "libavutil/avstring.h"
    2627#include "libavutil/bprint.h"
     
    2829#include "ass.h"
    2930#include "codec_internal.h"
    3031
     32// Unescape what has been escaped in webvtt_event_to_ass
     33static const char *webvtt_unescape_ass_chars = "{}";
     34
    3135#define WEBVTT_STACK_SIZE 64
    3236typedef struct {
    3337    AVCodecContext *avctx;
    static const ASSCodesCallbacks webvtt_callbacks = {  
    155159    .end              = webvtt_end_cb,
    156160};
    157161
     162static int webvtt_unescape_ass(unsigned char *buf, int buflen)
     163{
     164    unsigned char *pfrom = buf, *pto = buf;
     165    int count = buflen;
     166
     167    while (count > 0 && *pfrom) {
     168        if (*pfrom == '\\' && *(pfrom+1) != '\0' && index(webvtt_unescape_ass_chars, *(pfrom+1)) != NULL) {
     169            ++pfrom;
     170            --buflen;
     171        }
     172        *pto = *pfrom;
     173        pto++;
     174        pfrom++;
     175        --count;
     176    }
     177
     178    *pto='\0';
     179
     180    return buflen;
     181}
     182
    158183static int webvtt_encode_frame(AVCodecContext *avctx,
    159184                               unsigned char *buf, int bufsize, const AVSubtitle *sub)
    160185{
    161186    WebVTTContext *s = avctx->priv_data;
    162187    ASSDialog *dialog;
    163     int i;
     188    int i, ret;
    164189
    165190    av_bprint_clear(&s->buffer);
    166191
    static int webvtt_encode_frame(AVCodecContext *avctx,  
    190215        return AVERROR_BUFFER_TOO_SMALL;
    191216    }
    192217    memcpy(buf, s->buffer.str, s->buffer.len);
     218    ret = webvtt_unescape_ass(buf, s->buffer.len);
    193219
    194     return s->buffer.len;
     220    return ret;
    195221}
    196222
    197223static int webvtt_encode_close(AVCodecContext *avctx)