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 "config_components.h"
27 
28 #include "avcodec.h"
29 #include "ass.h"
30 #include "codec_internal.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/opt.h"
33 
34 typedef struct {
35  AVClass *class;
36  const char *linebreaks;
38  int readorder;
39 } TextContext;
40 
41 #define OFFSET(x) offsetof(TextContext, x)
42 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
43 static const AVOption options[] = {
44  { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags=SD },
45  { NULL }
46 };
47 
49  int *got_sub_ptr, const AVPacket *avpkt)
50 {
51  int ret = 0;
52  AVBPrint buf;
53  const char *ptr = avpkt->data;
54  TextContext *text = avctx->priv_data;
55 
57  if (ptr && avpkt->size > 0 && *ptr) {
58  ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
59  ret = ff_ass_add_rect(sub, buf.str, text->readorder++, 0, NULL, NULL);
60  }
61  av_bprint_finalize(&buf, NULL);
62  if (ret < 0)
63  return ret;
64  *got_sub_ptr = sub->num_rects > 0;
65  return avpkt->size;
66 }
67 
68 static void text_flush(AVCodecContext *avctx)
69 {
70  TextContext *text = avctx->priv_data;
71  if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
72  text->readorder = 0;
73 }
74 
76  .class_name = "text/vplayer/stl/pjs/subviewer1 decoder",
77  .item_name = av_default_item_name,
78  .option = options,
79  .version = LIBAVUTIL_VERSION_INT,
80 };
81 
82 #if CONFIG_TEXT_DECODER
83 const FFCodec ff_text_decoder = {
84  .p.name = "text",
85  .p.long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
86  .priv_data_size = sizeof(TextContext),
87  .p.type = AVMEDIA_TYPE_SUBTITLE,
88  .p.id = AV_CODEC_ID_TEXT,
91  .p.priv_class = &textsub_decoder_class,
92  .flush = text_flush,
93  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
94 };
95 #endif
96 
97 #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
98 
99 static int linebreak_init(AVCodecContext *avctx)
100 {
101  TextContext *text = avctx->priv_data;
102  text->linebreaks = "|";
103  return ff_ass_subtitle_header_default(avctx);
104 }
105 
106 #if CONFIG_VPLAYER_DECODER
107 const FFCodec ff_vplayer_decoder = {
108  .p.name = "vplayer",
109  .p.long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
110  .priv_data_size = sizeof(TextContext),
111  .p.type = AVMEDIA_TYPE_SUBTITLE,
112  .p.id = AV_CODEC_ID_VPLAYER,
114  .init = linebreak_init,
115  .p.priv_class = &textsub_decoder_class,
116  .flush = text_flush,
117  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
118 };
119 #endif
120 
121 #if CONFIG_STL_DECODER
122 const FFCodec ff_stl_decoder = {
123  .p.name = "stl",
124  .p.long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
125  .priv_data_size = sizeof(TextContext),
126  .p.type = AVMEDIA_TYPE_SUBTITLE,
127  .p.id = AV_CODEC_ID_STL,
129  .init = linebreak_init,
130  .p.priv_class = &textsub_decoder_class,
131  .flush = text_flush,
132  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
133 };
134 #endif
135 
136 #if CONFIG_PJS_DECODER
137 const FFCodec ff_pjs_decoder = {
138  .p.name = "pjs",
139  .p.long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"),
140  .priv_data_size = sizeof(TextContext),
141  .p.type = AVMEDIA_TYPE_SUBTITLE,
142  .p.id = AV_CODEC_ID_PJS,
144  .init = linebreak_init,
145  .p.priv_class = &textsub_decoder_class,
146  .flush = text_flush,
147  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
148 };
149 #endif
150 
151 #if CONFIG_SUBVIEWER1_DECODER
153  .p.name = "subviewer1",
154  .p.long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
155  .priv_data_size = sizeof(TextContext),
156  .p.type = AVMEDIA_TYPE_SUBTITLE,
157  .p.id = AV_CODEC_ID_SUBVIEWER1,
159  .init = linebreak_init,
160  .p.priv_class = &textsub_decoder_class,
161  .flush = text_flush,
162  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
163 };
164 #endif
165 
166 #endif /* text subtitles with '|' line break */
AVSubtitle
Definition: avcodec.h:2305
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
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:235
TextContext::readorder
int readorder
Definition: textdec.c:38
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:31
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:157
AVPacket::data
uint8_t * data
Definition: packet.h:374
AVOption
AVOption.
Definition: opt.h:251
ff_text_decoder
const FFCodec ff_text_decoder
FFCodec
Definition: codec_internal.h:112
text_flush
static void text_flush(AVCodecContext *avctx)
Definition: textdec.c:68
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AV_CODEC_ID_SUBVIEWER1
@ AV_CODEC_ID_SUBVIEWER1
Definition: codec_id.h:542
ass.h
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:171
options
static const AVOption options[]
Definition: textdec.c:43
TextContext
Definition: textdec.c:34
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:237
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:476
AVPacket::size
int size
Definition: packet.h:375
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
codec_internal.h
TextContext::keep_ass_markup
int keep_ass_markup
Definition: textdec.c:37
ff_subviewer1_decoder
const FFCodec ff_subviewer1_decoder
AV_CODEC_ID_STL
@ AV_CODEC_ID_STL
Definition: codec_id.h:541
bprint.h
TextContext::linebreaks
const char * linebreaks
Definition: textdec.c:36
AV_CODEC_ID_PJS
@ AV_CODEC_ID_PJS
Definition: codec_id.h:548
text_decode_frame
static int text_decode_frame(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
Definition: textdec.c:48
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: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
textsub_decoder_class
static const AVClass textsub_decoder_class
Definition: textdec.c:75
AV_CODEC_ID_VPLAYER
@ AV_CODEC_ID_VPLAYER
Definition: codec_id.h:547
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:529
SD
#define SD
Definition: textdec.c:42
ff_stl_decoder
const FFCodec ff_stl_decoder
OFFSET
#define OFFSET(x)
Definition: textdec.c:41
AVCodecContext
main external API structure.
Definition: avcodec.h:389
FF_CODEC_DECODE_SUB_CB
#define FF_CODEC_DECODE_SUB_CB(func)
Definition: codec_internal.h:257
ff_vplayer_decoder
const FFCodec ff_vplayer_decoder
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
ff_pjs_decoder
const FFCodec ff_pjs_decoder
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:333