FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/bprint.h"
29 #include "libavutil/opt.h"
30 
31 typedef struct {
32  AVClass *class;
33  const char *linebreaks;
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  { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags=SD },
41  { NULL }
42 };
43 
44 static int text_decode_frame(AVCodecContext *avctx, void *data,
45  int *got_sub_ptr, AVPacket *avpkt)
46 {
47  int ret = 0;
48  AVBPrint buf;
49  AVSubtitle *sub = data;
50  const char *ptr = avpkt->data;
51  const TextContext *text = avctx->priv_data;
52  const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
53  const int ts_duration = avpkt->duration != -1 ?
54  av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
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_bprint(sub, &buf, ts_start, ts_duration);
60  }
62  if (ret < 0)
63  return ret;
64  *got_sub_ptr = sub->num_rects > 0;
65  return avpkt->size;
66 }
67 
68 #define DECLARE_CLASS(decname) static const AVClass decname ## _decoder_class = { \
69  .class_name = #decname " decoder", \
70  .item_name = av_default_item_name, \
71  .option = decname ## _options, \
72  .version = LIBAVUTIL_VERSION_INT, \
73 }
74 
75 #if CONFIG_TEXT_DECODER
76 #define text_options options
77 DECLARE_CLASS(text);
78 
79 AVCodec ff_text_decoder = {
80  .name = "text",
81  .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
82  .priv_data_size = sizeof(TextContext),
84  .id = AV_CODEC_ID_TEXT,
87  .priv_class = &text_decoder_class,
88 };
89 #endif
90 
91 #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
92 
93 static int linebreak_init(AVCodecContext *avctx)
94 {
95  TextContext *text = avctx->priv_data;
96  text->linebreaks = "|";
97  return ff_ass_subtitle_header_default(avctx);
98 }
99 
100 #if CONFIG_VPLAYER_DECODER
101 #define vplayer_options options
102 DECLARE_CLASS(vplayer);
103 
104 AVCodec ff_vplayer_decoder = {
105  .name = "vplayer",
106  .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
107  .priv_data_size = sizeof(TextContext),
109  .id = AV_CODEC_ID_VPLAYER,
111  .init = linebreak_init,
112  .priv_class = &vplayer_decoder_class,
113 };
114 #endif
115 
116 #if CONFIG_STL_DECODER
117 #define stl_options options
119 
120 AVCodec ff_stl_decoder = {
121  .name = "stl",
122  .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
123  .priv_data_size = sizeof(TextContext),
125  .id = AV_CODEC_ID_STL,
127  .init = linebreak_init,
128  .priv_class = &stl_decoder_class,
129 };
130 #endif
131 
132 #if CONFIG_PJS_DECODER
133 #define pjs_options options
134 DECLARE_CLASS(pjs);
135 
136 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 = &pjs_decoder_class,
145 };
146 #endif
147 
148 #if CONFIG_SUBVIEWER1_DECODER
149 #define subviewer1_options options
150 DECLARE_CLASS(subviewer1);
151 
152 AVCodec ff_subviewer1_decoder = {
153  .name = "subviewer1",
154  .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
155  .priv_data_size = sizeof(TextContext),
159  .init = linebreak_init,
160  .priv_class = &subviewer1_decoder_class,
161 };
162 #endif
163 
164 #endif /* text subtitles with '|' line break */
#define NULL
Definition: coverity.c:32
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVCodec.
Definition: avcodec.h:3392
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
const char * linebreaks
Definition: textdec.c:33
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1661
int keep_ass_markup
Definition: textdec.c:34
AVOptions.
int ff_ass_subtitle_header_default(AVCodecContext *avctx)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS with default style.
Definition: ass.c:80
uint8_t * data
Definition: avcodec.h:1467
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define AV_BPRINT_SIZE_UNLIMITED
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
static const AVOption options[]
Definition: textdec.c:39
const char * name
Name of the codec implementation.
Definition: avcodec.h:3399
#define stl(l, p)
Definition: asm.h:76
Libavcodec external API header.
int ff_ass_add_rect_bprint(AVSubtitle *sub, AVBPrint *buf, int ts_start, int duration)
Same as ff_ass_add_rect, but taking an AVBPrint buffer instead of a string, and assuming raw=0...
Definition: ass.c:181
main external API structure.
Definition: avcodec.h:1532
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
#define DECLARE_CLASS(decname)
Definition: textdec.c:68
Describe the class of an AVClass context structure.
Definition: log.h:67
rational number numerator/denominator
Definition: rational.h:43
#define OFFSET(x)
Definition: textdec.c:37
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:572
#define SD
Definition: textdec.c:38
raw UTF-8 text
Definition: avcodec.h:508
void * priv_data
Definition: avcodec.h:1574
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:190
static int text_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt)
Definition: textdec.c:44
This structure stores compressed data.
Definition: avcodec.h:1444
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460