FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/textdec.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 16 21 76.2%
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 "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;
37 int keep_ass_markup;
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
48 132 static int text_decode_frame(AVCodecContext *avctx, AVSubtitle *sub,
49 int *got_sub_ptr, const AVPacket *avpkt)
50 {
51 132 int ret = 0;
52 AVBPrint buf;
53 132 const char *ptr = avpkt->data;
54 132 TextContext *text = avctx->priv_data;
55
56 132 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
57
3/6
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 132 times.
✗ Branch 5 not taken.
132 if (ptr && avpkt->size > 0 && *ptr) {
58 132 ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
59 132 ret = ff_ass_add_rect(sub, buf.str, text->readorder++, 0, NULL, NULL);
60 }
61 132 av_bprint_finalize(&buf, NULL);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (ret < 0)
63 return ret;
64 132 *got_sub_ptr = sub->num_rects > 0;
65 132 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
75 static const AVClass textsub_decoder_class = {
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 CODEC_LONG_NAME("Raw text subtitle"),
86 .priv_data_size = sizeof(TextContext),
87 .p.type = AVMEDIA_TYPE_SUBTITLE,
88 .p.id = AV_CODEC_ID_TEXT,
89 FF_CODEC_DECODE_SUB_CB(text_decode_frame),
90 .init = ff_ass_subtitle_header_default,
91 .p.priv_class = &textsub_decoder_class,
92 .flush = text_flush,
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 FFCodec ff_vplayer_decoder = {
107 .p.name = "vplayer",
108 CODEC_LONG_NAME("VPlayer subtitle"),
109 .priv_data_size = sizeof(TextContext),
110 .p.type = AVMEDIA_TYPE_SUBTITLE,
111 .p.id = AV_CODEC_ID_VPLAYER,
112 FF_CODEC_DECODE_SUB_CB(text_decode_frame),
113 .init = linebreak_init,
114 .p.priv_class = &textsub_decoder_class,
115 .flush = text_flush,
116 };
117 #endif
118
119 #if CONFIG_STL_DECODER
120 const FFCodec ff_stl_decoder = {
121 .p.name = "stl",
122 CODEC_LONG_NAME("Spruce subtitle format"),
123 .priv_data_size = sizeof(TextContext),
124 .p.type = AVMEDIA_TYPE_SUBTITLE,
125 .p.id = AV_CODEC_ID_STL,
126 FF_CODEC_DECODE_SUB_CB(text_decode_frame),
127 .init = linebreak_init,
128 .p.priv_class = &textsub_decoder_class,
129 .flush = text_flush,
130 };
131 #endif
132
133 #if CONFIG_PJS_DECODER
134 const FFCodec ff_pjs_decoder = {
135 .p.name = "pjs",
136 CODEC_LONG_NAME("PJS subtitle"),
137 .priv_data_size = sizeof(TextContext),
138 .p.type = AVMEDIA_TYPE_SUBTITLE,
139 .p.id = AV_CODEC_ID_PJS,
140 FF_CODEC_DECODE_SUB_CB(text_decode_frame),
141 .init = linebreak_init,
142 .p.priv_class = &textsub_decoder_class,
143 .flush = text_flush,
144 };
145 #endif
146
147 #if CONFIG_SUBVIEWER1_DECODER
148 const FFCodec ff_subviewer1_decoder = {
149 .p.name = "subviewer1",
150 CODEC_LONG_NAME("SubViewer1 subtitle"),
151 .priv_data_size = sizeof(TextContext),
152 .p.type = AVMEDIA_TYPE_SUBTITLE,
153 .p.id = AV_CODEC_ID_SUBVIEWER1,
154 FF_CODEC_DECODE_SUB_CB(text_decode_frame),
155 .init = linebreak_init,
156 .p.priv_class = &textsub_decoder_class,
157 .flush = text_flush,
158 };
159 #endif
160
161 #endif /* text subtitles with '|' line break */
162