44
static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
45
const char *p, const char *p_end)
47
const TextContext *text = avctx->priv_data;
49
for (; p < p_end && *p; p++) {
51
/* forced custom line breaks, not accounted as "normal" EOL */
52
if (text->linebreaks && strchr(text->linebreaks, *p)) {
53
av_bprintf(buf, "\\N");
55
/* standard ASS escaping so random characters don't get mis-interpreted
57
} else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
58
av_bprintf(buf, "\\%c", *p);
60
/* some packets might end abruptly (no \0 at the end, like for example
61
* in some cases of demuxing from a classic video container), some
62
* might be terminated with \n or \r\n which we have to remove (for
63
* consistency with those who haven't), and we also have to deal with
64
* evil cases such as \r at the end of the buffer (and no \0 terminated
66
} else if (p[0] == '\n') {
67
/* some stuff left so we can insert a line break */
69
av_bprintf(buf, "\\N");
70
} else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
71
/* \r followed by a \n, we can skip it. We don't insert the \N yet
72
* because we don't know if it is followed by more text */
75
/* finally, a sane character */
77
av_bprint_chars(buf, *p, 1);
80
av_bprintf(buf, "\r\n");
84
44
static int text_decode_frame(AVCodecContext *avctx, void *data,
85
45
int *got_sub_ptr, AVPacket *avpkt)
88
48
AVSubtitle *sub = data;
89
49
const char *ptr = avpkt->data;
50
const TextContext *text = avctx->priv_data;
90
51
const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
91
52
const int ts_duration = avpkt->duration != -1 ?
92
53
av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
94
55
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
95
if (ptr && avpkt->size > 0 && *ptr &&
96
!text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
56
if (ptr && avpkt->size > 0 && *ptr) {
57
ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
97
58
if (!av_bprint_is_complete(&buf)) {
98
59
av_bprint_finalize(&buf, NULL);
99
60
return AVERROR(ENOMEM);