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 
75  .class_name = "text/vplayer/stl/pjs/subviewer1 decoder",
76  .item_name = av_default_item_name,
77  .option = options,
78  .version = LIBAVUTIL_VERSION_INT,
79 };
80 
81 #if CONFIG_TEXT_DECODER
82 const AVCodec ff_text_decoder = {
83  .name = "text",
84  .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
85  .priv_data_size = sizeof(TextContext),
87  .id = AV_CODEC_ID_TEXT,
90  .priv_class = &textsub_decoder_class,
91  .flush = text_flush,
92  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
93 };
94 #endif
95 
96 #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
97 
98 static int linebreak_init(AVCodecContext *avctx)
99 {
100  TextContext *text = avctx->priv_data;
101  text->linebreaks = "|";
102  return ff_ass_subtitle_header_default(avctx);
103 }
104 
105 #if CONFIG_VPLAYER_DECODER
106 const AVCodec ff_vplayer_decoder = {
107  .name = "vplayer",
108  .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
109  .priv_data_size = sizeof(TextContext),
111  .id = AV_CODEC_ID_VPLAYER,
113  .init = linebreak_init,
114  .priv_class = &textsub_decoder_class,
115  .flush = text_flush,
116  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
117 };
118 #endif
119 
120 #if CONFIG_STL_DECODER
121 const AVCodec ff_stl_decoder = {
122  .name = "stl",
123  .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
124  .priv_data_size = sizeof(TextContext),
126  .id = AV_CODEC_ID_STL,
128  .init = linebreak_init,
129  .priv_class = &textsub_decoder_class,
130  .flush = text_flush,
131  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
132 };
133 #endif
134 
135 #if CONFIG_PJS_DECODER
136 const AVCodec ff_pjs_decoder = {
137  .name = "pjs",
138  .long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"),
139  .priv_data_size = sizeof(TextContext),
141  .id = AV_CODEC_ID_PJS,
143  .init = linebreak_init,
144  .priv_class = &textsub_decoder_class,
145  .flush = text_flush,
146  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
147 };
148 #endif
149 
150 #if CONFIG_SUBVIEWER1_DECODER
152  .name = "subviewer1",
153  .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
154  .priv_data_size = sizeof(TextContext),
158  .init = linebreak_init,
159  .priv_class = &textsub_decoder_class,
160  .flush = text_flush,
161  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
162 };
163 #endif
164 
165 #endif /* text subtitles with '|' line break */
AVSubtitle
Definition: avcodec.h:2289
AVCodec
AVCodec.
Definition: codec.h:202
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:42
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:96
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:234
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:68
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
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:117
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
data
const char data[16]
Definition: mxf.c:143
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:536
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:147
options
static const AVOption options[]
Definition: textdec.c:41
ff_pjs_decoder
const AVCodec ff_pjs_decoder
TextContext
Definition: textdec.c:32
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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:470
AVPacket::size
int size
Definition: packet.h:374
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
ff_text_decoder
const AVCodec ff_text_decoder
AV_CODEC_ID_STL
@ AV_CODEC_ID_STL
Definition: codec_id.h:535
bprint.h
TextContext::linebreaks
const char * linebreaks
Definition: textdec.c:34
AV_CODEC_ID_PJS
@ AV_CODEC_ID_PJS
Definition: codec_id.h:542
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
textsub_decoder_class
static const AVClass textsub_decoder_class
Definition: textdec.c:74
AV_CODEC_ID_VPLAYER
@ AV_CODEC_ID_VPLAYER
Definition: codec_id.h:541
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
AV_CODEC_ID_TEXT
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:523
SD
#define SD
Definition: textdec.c:40
OFFSET
#define OFFSET(x)
Definition: textdec.c:39
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
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:327