FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/textdec.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 17 22 77.3%
Branches: 4 10 40.0%

Line Branch Exec Source
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;
35 int keep_ass_markup;
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 89 static int text_decode_frame(AVCodecContext *avctx, void *data,
47 int *got_sub_ptr, AVPacket *avpkt)
48 {
49 89 int ret = 0;
50 AVBPrint buf;
51 89 AVSubtitle *sub = data;
52 89 const char *ptr = avpkt->data;
53 89 TextContext *text = avctx->priv_data;
54
55 89 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
56
3/6
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 89 times.
✗ Branch 5 not taken.
89 if (ptr && avpkt->size > 0 && *ptr) {
57 89 ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
58 89 ret = ff_ass_add_rect(sub, buf.str, text->readorder++, 0, NULL, NULL);
59 }
60 89 av_bprint_finalize(&buf, NULL);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (ret < 0)
62 return ret;
63 89 *got_sub_ptr = sub->num_rects > 0;
64 89 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 static const AVClass textsub_decoder_class = {
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),
86 .type = AVMEDIA_TYPE_SUBTITLE,
87 .id = AV_CODEC_ID_TEXT,
88 .decode = text_decode_frame,
89 .init = ff_ass_subtitle_header_default,
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 8 static int linebreak_init(AVCodecContext *avctx)
99 {
100 8 TextContext *text = avctx->priv_data;
101 8 text->linebreaks = "|";
102 8 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),
110 .type = AVMEDIA_TYPE_SUBTITLE,
111 .id = AV_CODEC_ID_VPLAYER,
112 .decode = text_decode_frame,
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),
125 .type = AVMEDIA_TYPE_SUBTITLE,
126 .id = AV_CODEC_ID_STL,
127 .decode = text_decode_frame,
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),
140 .type = AVMEDIA_TYPE_SUBTITLE,
141 .id = AV_CODEC_ID_PJS,
142 .decode = text_decode_frame,
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
151 const AVCodec ff_subviewer1_decoder = {
152 .name = "subviewer1",
153 .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
154 .priv_data_size = sizeof(TextContext),
155 .type = AVMEDIA_TYPE_SUBTITLE,
156 .id = AV_CODEC_ID_SUBVIEWER1,
157 .decode = text_decode_frame,
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 */
166