FFmpeg
textdec.c
Go to the documentation of this file.
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 "internal.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/opt.h"
31 
32 typedef struct {
33  AVClass *class;
34  const char *linebreaks;
36  int readorder;
37 } TextContext;
38 
39 #define OFFSET(x) offsetof(TextContext, x)
40 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
41 static const AVOption options[] = {
42  { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags=SD },
43  { NULL }
44 };
45 
46 static int text_decode_frame(AVCodecContext *avctx, void *data,
47  int *got_sub_ptr, AVPacket *avpkt)
48 {
49  int ret = 0;
50  AVBPrint buf;
51  AVSubtitle *sub = data;
52  const char *ptr = avpkt->data;
53  TextContext *text = avctx->priv_data;
54 
56  if (ptr && avpkt->size > 0 && *ptr) {
57  ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
58  ret = ff_ass_add_rect(sub, buf.str, text->readorder++, 0, NULL, NULL);
59  }
60  av_bprint_finalize(&buf, NULL);
61  if (ret < 0)
62  return ret;
63  *got_sub_ptr = sub->num_rects > 0;
64  return avpkt->size;
65 }
66 
67 static void text_flush(AVCodecContext *avctx)
68 {
69  TextContext *text = avctx->priv_data;
70  if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
71  text->readorder = 0;
72 }
73 
74 #define DECLARE_CLASS(decname) static const AVClass decname ## _decoder_class = { \
75  .class_name = #decname " decoder", \
76  .item_name = av_default_item_name, \
77  .option = decname ## _options, \
78  .version = LIBAVUTIL_VERSION_INT, \
79 }
80 
81 #if CONFIG_TEXT_DECODER
82 #define text_options options
83 DECLARE_CLASS(text);
84 
85 const AVCodec ff_text_decoder = {
86  .name = "text",
87  .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
88  .priv_data_size = sizeof(TextContext),
90  .id = AV_CODEC_ID_TEXT,
93  .priv_class = &text_decoder_class,
94  .flush = text_flush,
95  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
96 };
97 #endif
98 
99 #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
100 
101 static int linebreak_init(AVCodecContext *avctx)
102 {
103  TextContext *text = avctx->priv_data;
104  text->linebreaks = "|";
105  return ff_ass_subtitle_header_default(avctx);
106 }
107 
108 #if CONFIG_VPLAYER_DECODER
109 #define vplayer_options options
110 DECLARE_CLASS(vplayer);
111 
112 const AVCodec ff_vplayer_decoder = {
113  .name = "vplayer",
114  .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
115  .priv_data_size = sizeof(TextContext),
117  .id = AV_CODEC_ID_VPLAYER,
119  .init = linebreak_init,
120  .priv_class = &vplayer_decoder_class,
121  .flush = text_flush,
122  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
123 };
124 #endif
125 
126 #if CONFIG_STL_DECODER
127 #define stl_options options
129 
130 const AVCodec ff_stl_decoder = {
131  .name = "stl",
132  .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
133  .priv_data_size = sizeof(TextContext),
135  .id = AV_CODEC_ID_STL,
137  .init = linebreak_init,
138  .priv_class = &stl_decoder_class,
139  .flush = text_flush,
140  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
141 };
142 #endif
143 
144 #if CONFIG_PJS_DECODER
145 #define pjs_options options
146 DECLARE_CLASS(pjs);
147 
148 const AVCodec ff_pjs_decoder = {
149  .name = "pjs",
150  .long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"),
151  .priv_data_size = sizeof(TextContext),
153  .id = AV_CODEC_ID_PJS,
155  .init = linebreak_init,
156  .priv_class = &pjs_decoder_class,
157  .flush = text_flush,
158  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
159 };
160 #endif
161 
162 #if CONFIG_SUBVIEWER1_DECODER
163 #define subviewer1_options options
164 DECLARE_CLASS(subviewer1);
165 
167  .name = "subviewer1",
168  .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
169  .priv_data_size = sizeof(TextContext),
173  .init = linebreak_init,
174  .priv_class = &subviewer1_decoder_class,
175  .flush = text_flush,
176  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
177 };
178 #endif
179 
180 #endif /* text subtitles with '|' line break */
AVSubtitle
Definition: avcodec.h:2393
AVCodec
AVCodec.
Definition: codec.h:197
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
opt.h
ff_ass_subtitle_header_default
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:97
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
TextContext::readorder
int readorder
Definition: textdec.c:36
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:32
text_decode_frame
static int text_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt)
Definition: textdec.c:46
ff_ass_add_rect
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker)
Add an ASS dialog to a subtitle.
Definition: ass.c:118
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:365
AVOption
AVOption.
Definition: opt.h:248
data
const char data[16]
Definition: mxf.c:142
text_flush
static void text_flush(AVCodecContext *avctx)
Definition: textdec.c:67
ff_stl_decoder
const AVCodec ff_stl_decoder
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_CODEC_ID_SUBVIEWER1
@ AV_CODEC_ID_SUBVIEWER1
Definition: codec_id.h:534
ass.h
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
ff_ass_bprint_text_event
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size, const char *linebreaks, int keep_ass_markup)
Escape a text subtitle using ASS syntax into an AVBPrint buffer.
Definition: ass.c:148
options
static const AVOption options[]
Definition: textdec.c:41
ff_pjs_decoder
const AVCodec ff_pjs_decoder
TextContext
Definition: textdec.c:32
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:592
NULL
#define NULL
Definition: coverity.c:32
ff_subviewer1_decoder
const AVCodec ff_subviewer1_decoder
ff_vplayer_decoder
const AVCodec ff_vplayer_decoder
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:588
AVPacket::size
int size
Definition: packet.h:366
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
TextContext::keep_ass_markup
int keep_ass_markup
Definition: textdec.c:35
stl
#define stl(l, p)
Definition: asm.h:76
ff_text_decoder
const AVCodec ff_text_decoder
AV_CODEC_ID_STL
@ AV_CODEC_ID_STL
Definition: codec_id.h:533
bprint.h
TextContext::linebreaks
const char * linebreaks
Definition: textdec.c:34
AV_CODEC_ID_PJS
@ AV_CODEC_ID_PJS
Definition: codec_id.h:540
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
AV_CODEC_ID_VPLAYER
@ AV_CODEC_ID_VPLAYER
Definition: codec_id.h:539
avcodec.h
ret
ret
Definition: filter_design.txt:187
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:521
SD
#define SD
Definition: textdec.c:40
OFFSET
#define OFFSET(x)
Definition: textdec.c:39
AVCodecContext
main external API structure.
Definition: avcodec.h:501
DECLARE_CLASS
#define DECLARE_CLASS(decname)
Definition: textdec.c:74
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:528
AVPacket
This structure stores compressed data.
Definition: packet.h:342
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
AV_CODEC_FLAG2_RO_FLUSH_NOOP
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
Definition: avcodec.h:365