Commit 355cea8d authored by Michael Niedermayer's avatar Michael Niedermayer

Merge remote-tracking branch 'cus/stable'

* cus/stable:
  libzvbi-teletextdec: propagate errors generated in page handler
  libzvbi-teletextdec: dont ignore memory allocation error silently
  libzvbi-teletextdec: output ass subtitles instead of plain text
  ass: move text_event_to_ass from textdec.c to ass.c and export it
  ass: fix error handling in ff_ass_add_subrect
  ass: factor out ff_ass_bprint_dialog
  libzvbi-teletextdec: use AVBPrint for whitespace cleanup
  libzvbi-teletextdec: use option constants instead of strings
  libzvbi-teletextdec: fix indentation after last commit
  libzvbi-teletextdec: support multiple teletext pages in a single packet
Merged-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parents 43bf4297 c0479f0f
...@@ -77,14 +77,11 @@ static void insert_ts(AVBPrint *buf, int ts) ...@@ -77,14 +77,11 @@ static void insert_ts(AVBPrint *buf, int ts)
} }
} }
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int ff_ass_bprint_dialog(AVBPrint *buf, const char *dialog,
int ts_start, int duration, int raw) int ts_start, int duration, int raw)
{ {
AVBPrint buf; int dlen;
int ret, dlen;
AVSubtitleRect **rects;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
if (!raw || raw == 2) { if (!raw || raw == 2) {
long int layer = 0; long int layer = 0;
...@@ -101,32 +98,92 @@ int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, ...@@ -101,32 +98,92 @@ int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
dialog++; dialog++;
} }
av_bprintf(&buf, "Dialogue: %ld,", layer); av_bprintf(buf, "Dialogue: %ld,", layer);
insert_ts(&buf, ts_start); insert_ts(buf, ts_start);
insert_ts(&buf, duration == -1 ? -1 : ts_start + duration); insert_ts(buf, duration == -1 ? -1 : ts_start + duration);
if (raw != 2) if (raw != 2)
av_bprintf(&buf, "Default,"); av_bprintf(buf, "Default,");
} }
dlen = strcspn(dialog, "\n"); dlen = strcspn(dialog, "\n");
dlen += dialog[dlen] == '\n'; dlen += dialog[dlen] == '\n';
av_bprintf(&buf, "%.*s", dlen, dialog); av_bprintf(buf, "%.*s", dlen, dialog);
if (raw == 2) if (raw == 2)
av_bprintf(&buf, "\r\n"); av_bprintf(buf, "\r\n");
return dlen;
}
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
int ts_start, int duration, int raw)
{
AVBPrint buf;
int ret, dlen;
AVSubtitleRect **rects;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
if ((ret = ff_ass_bprint_dialog(&buf, dialog, ts_start, duration, raw)) < 0)
goto err;
dlen = ret;
if (!av_bprint_is_complete(&buf)) if (!av_bprint_is_complete(&buf))
return AVERROR(ENOMEM); goto errnomem;
rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects)); rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
if (!rects) if (!rects)
return AVERROR(ENOMEM); goto errnomem;
sub->rects = rects; sub->rects = rects;
sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration); sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
rects[sub->num_rects] = av_mallocz(sizeof(*rects[0])); rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
rects[sub->num_rects]->type = SUBTITLE_ASS; rects[sub->num_rects]->type = SUBTITLE_ASS;
ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass); ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
if (ret < 0) if (ret < 0)
return ret; goto err;
sub->num_rects++; sub->num_rects++;
return dlen; return dlen;
errnomem:
ret = AVERROR(ENOMEM);
err:
av_bprint_finalize(&buf, NULL);
return ret;
}
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
const char *linebreaks, int keep_ass_markup)
{
const char *p_end = p + size;
for (; p < p_end && *p; p++) {
/* forced custom line breaks, not accounted as "normal" EOL */
if (linebreaks && strchr(linebreaks, *p)) {
av_bprintf(buf, "\\N");
/* standard ASS escaping so random characters don't get mis-interpreted
* as ASS */
} else if (!keep_ass_markup && strchr("{}\\", *p)) {
av_bprintf(buf, "\\%c", *p);
/* some packets might end abruptly (no \0 at the end, like for example
* in some cases of demuxing from a classic video container), some
* might be terminated with \n or \r\n which we have to remove (for
* consistency with those who haven't), and we also have to deal with
* evil cases such as \r at the end of the buffer (and no \0 terminated
* character) */
} else if (p[0] == '\n') {
/* some stuff left so we can insert a line break */
if (p < p_end - 1)
av_bprintf(buf, "\\N");
} else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
/* \r followed by a \n, we can skip it. We don't insert the \N yet
* because we don't know if it is followed by more text */
continue;
/* finally, a sane character */
} else {
av_bprint_chars(buf, *p, 1);
}
}
av_bprintf(buf, "\r\n");
} }
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#define AVCODEC_ASS_H #define AVCODEC_ASS_H
#include "avcodec.h" #include "avcodec.h"
#include "libavutil/bprint.h"
/** /**
* @name Default values for ASS style * @name Default values for ASS style
...@@ -90,4 +91,38 @@ int ff_ass_subtitle_header_default(AVCodecContext *avctx); ...@@ -90,4 +91,38 @@ int ff_ass_subtitle_header_default(AVCodecContext *avctx);
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
int ts_start, int duration, int raw); int ts_start, int duration, int raw);
/**
* Add an ASS dialog line to an AVBPrint buffer.
*
* @param buf pointer to an initialized AVBPrint buffer
* @param dialog ASS dialog to add to sub
* @param ts_start start timestamp for this dialog (in 1/100 second unit)
* @param duration duration for this dialog (in 1/100 second unit), can be -1
* to last until the end of the presentation
* @param raw when set to 2, it indicates that dialog contains an ASS
* dialog line as muxed in Matroska
* when set to 1, it indicates that dialog contains a whole SSA
* dialog line which should be copied as is.
* when set to 0, it indicates that dialog contains only the Text
* part of the ASS dialog line, the rest of the line
* will be generated.
* @return number of characters read from dialog. It can be less than the whole
* length of dialog, if dialog contains several lines of text.
* A negative value indicates an error.
*/
int ff_ass_bprint_dialog(AVBPrint *buf, const char *dialog,
int ts_start, int duration, int raw);
/**
* Escape a text subtitle using ASS syntax into an AVBPrint buffer.
* Newline characters will be escaped to \N.
*
* @param buf pointer to an initialized AVBPrint buffer
* @param p source text
* @param size size of the source text
* @param linebreaks additional newline chars, which will be escaped to \N
* @param keep_ass_markup braces and backslash will not be escaped if set
*/
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
const char *linebreaks, int keep_ass_markup);
#endif /* AVCODEC_ASS_H */ #endif /* AVCODEC_ASS_H */
This diff is collapsed.
...@@ -41,59 +41,20 @@ static const AVOption options[] = { ...@@ -41,59 +41,20 @@ static const AVOption options[] = {
{ NULL } { NULL }
}; };
static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
const char *p, const char *p_end)
{
const TextContext *text = avctx->priv_data;
for (; p < p_end && *p; p++) {
/* forced custom line breaks, not accounted as "normal" EOL */
if (text->linebreaks && strchr(text->linebreaks, *p)) {
av_bprintf(buf, "\\N");
/* standard ASS escaping so random characters don't get mis-interpreted
* as ASS */
} else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
av_bprintf(buf, "\\%c", *p);
/* some packets might end abruptly (no \0 at the end, like for example
* in some cases of demuxing from a classic video container), some
* might be terminated with \n or \r\n which we have to remove (for
* consistency with those who haven't), and we also have to deal with
* evil cases such as \r at the end of the buffer (and no \0 terminated
* character) */
} else if (p[0] == '\n') {
/* some stuff left so we can insert a line break */
if (p < p_end - 1)
av_bprintf(buf, "\\N");
} else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
/* \r followed by a \n, we can skip it. We don't insert the \N yet
* because we don't know if it is followed by more text */
continue;
/* finally, a sane character */
} else {
av_bprint_chars(buf, *p, 1);
}
}
av_bprintf(buf, "\r\n");
return 0;
}
static int text_decode_frame(AVCodecContext *avctx, void *data, static int text_decode_frame(AVCodecContext *avctx, void *data,
int *got_sub_ptr, AVPacket *avpkt) int *got_sub_ptr, AVPacket *avpkt)
{ {
AVBPrint buf; AVBPrint buf;
AVSubtitle *sub = data; AVSubtitle *sub = data;
const char *ptr = avpkt->data; const char *ptr = avpkt->data;
const TextContext *text = avctx->priv_data;
const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100}); const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
const int ts_duration = avpkt->duration != -1 ? const int ts_duration = avpkt->duration != -1 ?
av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1; av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
if (ptr && avpkt->size > 0 && *ptr && if (ptr && avpkt->size > 0 && *ptr) {
!text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) { ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
if (!av_bprint_is_complete(&buf)) { if (!av_bprint_is_complete(&buf)) {
av_bprint_finalize(&buf, NULL); av_bprint_finalize(&buf, NULL);
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment