This source file includes following definitions.
- text_to_ass
- mov_text_init
- mov_text_decode_frame
#include "avcodec.h"
#include "ass.h"
#include "libavutil/avstring.h"
#include "libavutil/common.h"
#include "libavutil/bprint.h"
#include "libavutil/intreadwrite.h"
static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end)
{
while (text < text_end) {
switch (*text) {
case '\r':
break;
case '\n':
av_bprintf(buf, "\\N");
break;
default:
av_bprint_chars(buf, *text, 1);
break;
}
text++;
}
return 0;
}
static int mov_text_init(AVCodecContext *avctx) {
return ff_ass_subtitle_header_default(avctx);
}
static int mov_text_decode_frame(AVCodecContext *avctx,
void *data, int *got_sub_ptr, AVPacket *avpkt)
{
AVSubtitle *sub = data;
int ret, ts_start, ts_end;
AVBPrint buf;
const char *ptr = avpkt->data;
const char *end;
if (!ptr || avpkt->size < 2)
return AVERROR_INVALIDDATA;
if (avpkt->size == 2)
return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
end = ptr + FFMIN(2 + AV_RB16(ptr), avpkt->size);
ptr += 2;
ts_start = av_rescale_q(avpkt->pts,
avctx->time_base,
(AVRational){1,100});
ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
avctx->time_base,
(AVRational){1,100});
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
text_to_ass(&buf, ptr, end);
ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end-ts_start);
av_bprint_finalize(&buf, NULL);
if (ret < 0)
return ret;
*got_sub_ptr = sub->num_rects > 0;
return avpkt->size;
}
AVCodec ff_movtext_decoder = {
.name = "mov_text",
.long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
.type = AVMEDIA_TYPE_SUBTITLE,
.id = AV_CODEC_ID_MOV_TEXT,
.init = mov_text_init,
.decode = mov_text_decode_frame,
};