]> git.sesse.net Git - ffmpeg/blob - libavcodec/textdec.c
Merge commit '637606de2d2e0af0a9fa2f23f943765d7d7c5cd5'
[ffmpeg] / libavcodec / textdec.c
1 /*
2  * Copyright (c) 2012 Clément Bœsch
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Raw subtitles decoder
24  */
25
26 #include "avcodec.h"
27 #include "ass.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/opt.h"
30
31 typedef struct {
32     AVClass *class;
33     char *linebreaks;
34     int keep_ass_markup;
35 } TextContext;
36
37 #define OFFSET(x) offsetof(TextContext, x)
38 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
39 static const AVOption options[] = {
40     { "linebreaks",      "Extra line breaks characters",    OFFSET(linebreaks),      AV_OPT_TYPE_STRING, {.str=NULL},    .flags=SD },
41     { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT,    {.i64=0}, 0, 1, .flags=SD },
42     { NULL }
43 };
44
45 static const AVClass text_decoder_class = {
46     .class_name = "text decoder",
47     .item_name  = av_default_item_name,
48     .option     = options,
49     .version    = LIBAVUTIL_VERSION_INT,
50 };
51
52 static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
53                              const char *p, const char *p_end)
54 {
55     const TextContext *text = avctx->priv_data;
56
57     for (; p < p_end && *p; p++) {
58
59         /* forced custom line breaks, not accounted as "normal" EOL */
60         if (text->linebreaks && strchr(text->linebreaks, *p)) {
61             av_bprintf(buf, "\\N");
62
63         /* standard ASS escaping so random characters don't get mis-interpreted
64          * as ASS */
65         } else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
66             av_bprintf(buf, "\\%c", *p);
67
68         /* some packets might end abruptly (no \0 at the end, like for example
69          * in some cases of demuxing from a classic video container), some
70          * might be terminated with \n or \r\n which we have to remove (for
71          * consistency with those who haven't), and we also have to deal with
72          * evil cases such as \r at the end of the buffer (and no \0 terminated
73          * character) */
74         } else if (p[0] == '\n') {
75             /* some stuff left so we can insert a line break */
76             if (p < p_end - 1)
77                 av_bprintf(buf, "\\N");
78         } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
79             /* \r followed by a \n, we can skip it. We don't insert the \N yet
80              * because we don't know if it is followed by more text */
81             continue;
82
83         /* finally, a sane character */
84         } else {
85             av_bprint_chars(buf, *p, 1);
86         }
87     }
88     av_bprintf(buf, "\r\n");
89     return 0;
90 }
91
92 static int text_decode_frame(AVCodecContext *avctx, void *data,
93                              int *got_sub_ptr, AVPacket *avpkt)
94 {
95     AVBPrint buf;
96     AVSubtitle *sub = data;
97     const char *ptr = avpkt->data;
98     const int ts_start     = av_rescale_q(avpkt->pts,      avctx->time_base, (AVRational){1,100});
99     const int ts_duration  = avpkt->duration != -1 ?
100                              av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
101
102     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
103     if (ptr && avpkt->size > 0 && *ptr &&
104         !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
105         if (!av_bprint_is_complete(&buf)) {
106             av_bprint_finalize(&buf, NULL);
107             return AVERROR(ENOMEM);
108         }
109         ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
110     }
111     *got_sub_ptr = sub->num_rects > 0;
112     av_bprint_finalize(&buf, NULL);
113     return avpkt->size;
114 }
115
116 AVCodec ff_text_decoder = {
117     .name           = "text",
118     .priv_data_size = sizeof(TextContext),
119     .long_name      = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
120     .type           = AVMEDIA_TYPE_SUBTITLE,
121     .id             = AV_CODEC_ID_TEXT,
122     .decode         = text_decode_frame,
123     .init           = ff_ass_subtitle_header_default,
124     .priv_class     = &text_decoder_class,
125 };