FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/movtextenc.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 201 339 59.3%
Functions: 21 28 75.0%
Branches: 62 158 39.2%

Line Branch Exec Source
1 /*
2 * 3GPP TS 26.245 Timed Text encoder
3 * Copyright (c) 2012 Philip Langdale <philipl@overt.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdarg.h>
23 #include "avcodec.h"
24 #include "libavutil/attributes.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/common.h"
29 #include "ass_split.h"
30 #include "ass.h"
31 #include "bytestream.h"
32 #include "codec_internal.h"
33
34 #define STYLE_FLAG_BOLD (1<<0)
35 #define STYLE_FLAG_ITALIC (1<<1)
36 #define STYLE_FLAG_UNDERLINE (1<<2)
37 #define STYLE_RECORD_SIZE 12
38 #define SIZE_ADD 10
39
40 #define STYL_BOX (1<<0)
41 #define HLIT_BOX (1<<1)
42 #define HCLR_BOX (1<<2)
43
44 #define DEFAULT_STYLE_FONT_ID 0x01
45 #define DEFAULT_STYLE_FONTSIZE 0x12
46 #define DEFAULT_STYLE_COLOR 0xffffffff
47 #define DEFAULT_STYLE_FLAG 0x00
48
49 #define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((uint32_t)(c) >> 16) & 0xff))
50 #define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5)
51 #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
52
53 typedef struct {
54 uint16_t style_start;
55 uint16_t style_end;
56 uint8_t style_flag;
57 uint16_t style_fontID;
58 uint8_t style_fontsize;
59 uint32_t style_color;
60 } StyleBox;
61
62 typedef struct {
63 uint16_t start;
64 uint16_t end;
65 } HighlightBox;
66
67 typedef struct {
68 uint32_t color;
69 } HilightcolorBox;
70
71 typedef struct {
72 AVClass *class;
73 AVCodecContext *avctx;
74
75 ASSSplitContext *ass_ctx;
76 ASSStyle *ass_dialog_style;
77 StyleBox *style_attributes;
78 unsigned count;
79 unsigned style_attributes_bytes_allocated;
80 StyleBox style_attributes_temp;
81 AVBPrint buffer;
82 HighlightBox hlit;
83 HilightcolorBox hclr;
84 uint8_t box_flags;
85 StyleBox d;
86 uint16_t text_pos;
87 char **fonts;
88 int font_count;
89 double font_scale_factor;
90 int frame_height;
91 } MovTextContext;
92
93 typedef struct {
94 void (*encode)(MovTextContext *s);
95 } Box;
96
97 3 static void mov_text_cleanup(MovTextContext *s)
98 {
99 3 s->count = 0;
100 3 s->style_attributes_temp = s->d;
101 3 }
102
103 3 static void encode_styl(MovTextContext *s)
104 {
105
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if ((s->box_flags & STYL_BOX) && s->count) {
106 uint8_t buf[12], *p = buf;
107
108 bytestream_put_be32(&p, s->count * STYLE_RECORD_SIZE + SIZE_ADD);
109 bytestream_put_be32(&p, MKBETAG('s','t','y','l'));
110 bytestream_put_be16(&p, s->count);
111 /*The above three attributes are hard coded for now
112 but will come from ASS style in the future*/
113 av_bprint_append_any(&s->buffer, buf, 10);
114 for (unsigned j = 0; j < s->count; j++) {
115 const StyleBox *style = &s->style_attributes[j];
116
117 p = buf;
118 bytestream_put_be16(&p, style->style_start);
119 bytestream_put_be16(&p, style->style_end);
120 bytestream_put_be16(&p, style->style_fontID);
121 bytestream_put_byte(&p, style->style_flag);
122 bytestream_put_byte(&p, style->style_fontsize);
123 bytestream_put_be32(&p, style->style_color);
124
125 av_bprint_append_any(&s->buffer, buf, 12);
126 }
127 }
128 3 mov_text_cleanup(s);
129 3 }
130
131 3 static void encode_hlit(MovTextContext *s)
132 {
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->box_flags & HLIT_BOX) {
134 uint8_t buf[12], *p = buf;
135
136 bytestream_put_be32(&p, 12);
137 bytestream_put_be32(&p, MKBETAG('h','l','i','t'));
138 bytestream_put_be16(&p, s->hlit.start);
139 bytestream_put_be16(&p, s->hlit.end);
140
141 av_bprint_append_any(&s->buffer, buf, 12);
142 }
143 3 }
144
145 3 static void encode_hclr(MovTextContext *s)
146 {
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->box_flags & HCLR_BOX) {
148 uint8_t buf[12], *p = buf;
149
150 bytestream_put_be32(&p, 12);
151 bytestream_put_be32(&p, MKBETAG('h','c','l','r'));
152 bytestream_put_be32(&p, s->hclr.color);
153
154 av_bprint_append_any(&s->buffer, buf, 12);
155 }
156 3 }
157
158 static const Box box_types[] = {
159 { encode_styl },
160 { encode_hlit },
161 { encode_hclr },
162 };
163
164 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
165
166 1 static av_cold int mov_text_encode_close(AVCodecContext *avctx)
167 {
168 1 MovTextContext *s = avctx->priv_data;
169
170 1 ff_ass_split_free(s->ass_ctx);
171 1 av_freep(&s->style_attributes);
172 1 av_freep(&s->fonts);
173 1 return 0;
174 }
175
176 1 static int encode_sample_description(AVCodecContext *avctx)
177 {
178 ASS *ass;
179 ASSStyle *style;
180 int i, j;
181 1 uint32_t back_color = 0;
182 1 int font_names_total_len = 0;
183 1 MovTextContext *s = avctx->priv_data;
184 1 uint8_t buf[30], *p = buf;
185 int ret;
186
187 1 av_bprint_init(&s->buffer, 0, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE + 1);
188
189 // 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
190 // 0x01, // int8_t horizontal-justification
191 // 0xFF, // int8_t vertical-justification
192 // 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
193 // BoxRecord {
194 // 0x00, 0x00, // int16_t top
195 // 0x00, 0x00, // int16_t left
196 // 0x00, 0x00, // int16_t bottom
197 // 0x00, 0x00, // int16_t right
198 // };
199 // StyleRecord {
200 // 0x00, 0x00, // uint16_t startChar
201 // 0x00, 0x00, // uint16_t endChar
202 // 0x00, 0x01, // uint16_t font-ID
203 // 0x00, // uint8_t face-style-flags
204 // 0x12, // uint8_t font-size
205 // 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
206 // };
207 // FontTableBox {
208 // 0x00, 0x00, 0x00, 0x12, // uint32_t size
209 // 'f', 't', 'a', 'b', // uint8_t name[4]
210 // 0x00, 0x01, // uint16_t entry-count
211 // FontRecord {
212 // 0x00, 0x01, // uint16_t font-ID
213 // 0x05, // uint8_t font-name-length
214 // 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
215 // };
216 // };
217
218 // Populate sample description from ASS header
219 1 ass = (ASS*)s->ass_ctx;
220 // Compute font scaling factor based on (optionally) provided
221 // output video height and ASS script play_res_y
222
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (s->frame_height && ass->script_info.play_res_y)
223 s->font_scale_factor = (double)s->frame_height / ass->script_info.play_res_y;
224 else
225 1 s->font_scale_factor = 1;
226
227 1 style = ff_ass_style_get(s->ass_ctx, "Default");
228
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (!style && ass->styles_count) {
229 style = &ass->styles[0];
230 }
231 1 s->d.style_fontID = DEFAULT_STYLE_FONT_ID;
232 1 s->d.style_fontsize = DEFAULT_STYLE_FONTSIZE;
233 1 s->d.style_color = DEFAULT_STYLE_COLOR;
234 1 s->d.style_flag = DEFAULT_STYLE_FLAG;
235
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (style) {
236 1 s->d.style_fontsize = FONTSIZE_SCALE(s, style->font_size);
237 1 s->d.style_color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8 |
238 1 255 - ((uint32_t)style->primary_color >> 24);
239 1 s->d.style_flag = (!!style->bold * STYLE_FLAG_BOLD) |
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
2 (!!style->italic * STYLE_FLAG_ITALIC) |
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 (!!style->underline * STYLE_FLAG_UNDERLINE);
242 1 back_color = (BGR_TO_RGB(style->back_color & 0xffffff) << 8) |
243 1 (255 - ((uint32_t)style->back_color >> 24));
244 }
245
246 1 bytestream_put_be32(&p, 0); // displayFlags
247 1 bytestream_put_be16(&p, 0x01FF); // horizontal/vertical justification (2x int8_t)
248 1 bytestream_put_be32(&p, back_color);
249 1 bytestream_put_be64(&p, 0); // BoxRecord - 4xint16_t: top, left, bottom, right
250 // StyleRecord {
251 1 bytestream_put_be16(&p, s->d.style_start);
252 1 bytestream_put_be16(&p, s->d.style_end);
253 1 bytestream_put_be16(&p, s->d.style_fontID);
254 1 bytestream_put_byte(&p, s->d.style_flag);
255 1 bytestream_put_byte(&p, s->d.style_fontsize);
256 1 bytestream_put_be32(&p, s->d.style_color);
257 // };
258 1 av_bprint_append_any(&s->buffer, buf, 30);
259
260 // Build font table
261 // We can't build a complete font table since that would require
262 // scanning all dialogs first. But we can at least fill in what
263 // is available in the ASS header
264
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (style && ass->styles_count) {
265 // Find unique font names
266
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (style->font_name) {
267 1 av_dynarray_add(&s->fonts, &s->font_count, style->font_name);
268 1 font_names_total_len += strlen(style->font_name);
269 }
270
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < ass->styles_count; i++) {
271 1 int found = 0;
272
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ass->styles[i].font_name)
273 continue;
274
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (j = 0; j < s->font_count; j++) {
275
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!strcmp(s->fonts[j], ass->styles[i].font_name)) {
276 1 found = 1;
277 1 break;
278 }
279 }
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!found) {
281 av_dynarray_add(&s->fonts, &s->font_count,
282 ass->styles[i].font_name);
283 font_names_total_len += strlen(ass->styles[i].font_name);
284 }
285 }
286 } else
287 av_dynarray_add(&s->fonts, &s->font_count, (char*)"Serif");
288
289 // FontTableBox {
290 1 p = buf;
291 1 bytestream_put_be32(&p, SIZE_ADD + 3 * s->font_count + font_names_total_len); // Size
292 1 bytestream_put_be32(&p, MKBETAG('f','t','a','b'));
293 1 bytestream_put_be16(&p, s->font_count);
294
295 1 av_bprint_append_any(&s->buffer, buf, 10);
296 // FontRecord {
297
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0; i < s->font_count; i++) {
298 1 size_t len = strlen(s->fonts[i]);
299
300 1 p = buf;
301 1 bytestream_put_be16(&p, i + 1); //fontID
302 1 bytestream_put_byte(&p, len);
303
304 1 av_bprint_append_any(&s->buffer, buf, 3);
305 1 av_bprint_append_any(&s->buffer, s->fonts[i], len);
306 }
307 // };
308 // };
309
310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!av_bprint_is_complete(&s->buffer)) {
311 ret = AVERROR(ENOMEM);
312 goto fail;
313 }
314
315 1 avctx->extradata_size = s->buffer.len;
316 1 avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!avctx->extradata) {
318 ret = AVERROR(ENOMEM);
319 goto fail;
320 }
321
322 1 memcpy(avctx->extradata, s->buffer.str, avctx->extradata_size);
323 1 ret = 0;
324 1 fail:
325 1 av_bprint_finalize(&s->buffer, NULL);
326
327 1 return ret;
328 }
329
330 1 static av_cold int mov_text_encode_init(AVCodecContext *avctx)
331 {
332 int ret;
333 1 MovTextContext *s = avctx->priv_data;
334 1 s->avctx = avctx;
335
336 1 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->ass_ctx)
338 return AVERROR_INVALIDDATA;
339 1 ret = encode_sample_description(avctx);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
341 return ret;
342
343 1 return 0;
344 }
345
346 // Start a new style box if needed
347 7 static int mov_text_style_start(MovTextContext *s)
348 {
349 // there's an existing style entry
350
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 if (s->style_attributes_temp.style_start == s->text_pos)
351 // Still at same text pos, use same entry
352 4 return 1;
353
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->style_attributes_temp.style_flag != s->d.style_flag ||
354
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 s->style_attributes_temp.style_color != s->d.style_color ||
355
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 s->style_attributes_temp.style_fontID != s->d.style_fontID ||
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 s->style_attributes_temp.style_fontsize != s->d.style_fontsize) {
357 StyleBox *tmp;
358
359 // last style != defaults, end the style entry and start a new one
360 if (s->count + 1 > FFMIN(SIZE_MAX / sizeof(*s->style_attributes), UINT16_MAX) ||
361 !(tmp = av_fast_realloc(s->style_attributes,
362 &s->style_attributes_bytes_allocated,
363 (s->count + 1) * sizeof(*s->style_attributes)))) {
364 mov_text_cleanup(s);
365 av_bprint_clear(&s->buffer);
366 s->box_flags &= ~STYL_BOX;
367 return 0;
368 }
369 s->style_attributes = tmp;
370 s->style_attributes_temp.style_end = s->text_pos;
371 s->style_attributes[s->count++] = s->style_attributes_temp;
372 s->box_flags |= STYL_BOX;
373 s->style_attributes_temp = s->d;
374 s->style_attributes_temp.style_start = s->text_pos;
375 } else { // style entry matches defaults, drop entry
376 3 s->style_attributes_temp = s->d;
377 3 s->style_attributes_temp.style_start = s->text_pos;
378 }
379 3 return 1;
380 }
381
382 static uint8_t mov_text_style_to_flag(const char style)
383 {
384 uint8_t style_flag = 0;
385
386 switch (style){
387 case 'b':
388 style_flag = STYLE_FLAG_BOLD;
389 break;
390 case 'i':
391 style_flag = STYLE_FLAG_ITALIC;
392 break;
393 case 'u':
394 style_flag = STYLE_FLAG_UNDERLINE;
395 break;
396 }
397 return style_flag;
398 }
399
400 3 static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
401 {
402
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!((s->style_attributes_temp.style_flag & style_flags) ^ style_flags)) {
403 // setting flags that that are already set
404 3 return;
405 }
406 if (mov_text_style_start(s))
407 s->style_attributes_temp.style_flag |= style_flags;
408 }
409
410 static void mov_text_style_cb(void *priv, const char style, int close)
411 {
412 MovTextContext *s = priv;
413 uint8_t style_flag = mov_text_style_to_flag(style);
414
415 if (!!(s->style_attributes_temp.style_flag & style_flag) != close) {
416 // setting flag that is already set
417 return;
418 }
419 if (mov_text_style_start(s)) {
420 if (!close)
421 s->style_attributes_temp.style_flag |= style_flag;
422 else
423 s->style_attributes_temp.style_flag &= ~style_flag;
424 }
425 }
426
427 3 static void mov_text_color_set(MovTextContext *s, uint32_t color)
428 {
429
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if ((s->style_attributes_temp.style_color & 0xffffff00) == color) {
430 // color hasn't changed
431 2 return;
432 }
433
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (mov_text_style_start(s))
434 1 s->style_attributes_temp.style_color = (color & 0xffffff00) |
435 1 (s->style_attributes_temp.style_color & 0xff);
436 }
437
438 static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
439 {
440 MovTextContext *s = priv;
441
442 color = BGR_TO_RGB(color) << 8;
443 if (color_id == 1) { //primary color changes
444 mov_text_color_set(s, color);
445 } else if (color_id == 2) { //secondary color changes
446 if (!(s->box_flags & HCLR_BOX))
447 // Highlight alpha not set yet, use current primary alpha
448 s->hclr.color = s->style_attributes_temp.style_color;
449 if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
450 s->box_flags |= HCLR_BOX;
451 s->box_flags |= HLIT_BOX;
452 s->hlit.start = s->text_pos;
453 s->hclr.color = color | (s->hclr.color & 0xFF);
454 }
455 else //close tag
456 s->hlit.end = s->text_pos;
457 /* If there are more than one secondary color changes in ASS,
458 take start of first section and end of last section. Movtext
459 allows only one highlight box per sample.
460 */
461 }
462 // Movtext does not support changes to other color_id (outline, background)
463 }
464
465 3 static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
466 {
467
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if ((s->style_attributes_temp.style_color & 0xff) == alpha) {
468 // color hasn't changed
469 2 return;
470 }
471
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (mov_text_style_start(s))
472 1 s->style_attributes_temp.style_color =
473 1 (s->style_attributes_temp.style_color & 0xffffff00) | alpha;
474 }
475
476 static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
477 {
478 MovTextContext *s = priv;
479
480 alpha = 255 - alpha;
481 if (alpha_id == 1) // primary alpha changes
482 mov_text_alpha_set(s, alpha);
483 else if (alpha_id == 2) { //secondary alpha changes
484 if (!(s->box_flags & HCLR_BOX))
485 // Highlight color not set yet, use current primary color
486 s->hclr.color = s->style_attributes_temp.style_color;
487 if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
488 s->box_flags |= HCLR_BOX;
489 s->box_flags |= HLIT_BOX;
490 s->hlit.start = s->text_pos;
491 s->hclr.color = (s->hclr.color & 0xffffff00) | alpha;
492 }
493 else //close tag
494 s->hlit.end = s->text_pos;
495 }
496 // Movtext does not support changes to other alpha_id (outline, background)
497 }
498
499 3 static uint16_t find_font_id(MovTextContext *s, const char *name)
500 {
501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!name)
502 return 1;
503
504
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (int i = 0; i < s->font_count; i++) {
505
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!strcmp(name, s->fonts[i]))
506 3 return i + 1;
507 }
508 return 1;
509 }
510
511 3 static void mov_text_font_name_set(MovTextContext *s, const char *name)
512 {
513 3 int fontID = find_font_id(s, name);
514
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (s->style_attributes_temp.style_fontID == fontID) {
515 // color hasn't changed
516 2 return;
517 }
518
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (mov_text_style_start(s))
519 1 s->style_attributes_temp.style_fontID = fontID;
520 }
521
522 static void mov_text_font_name_cb(void *priv, const char *name)
523 {
524 mov_text_font_name_set((MovTextContext*)priv, name);
525 }
526
527 3 static void mov_text_font_size_set(MovTextContext *s, int size)
528 {
529 3 size = FONTSIZE_SCALE(s, size);
530
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (s->style_attributes_temp.style_fontsize == size) {
531 // color hasn't changed
532 2 return;
533 }
534
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (mov_text_style_start(s))
535 1 s->style_attributes_temp.style_fontsize = size;
536 }
537
538 static void mov_text_font_size_cb(void *priv, int size)
539 {
540 mov_text_font_size_set((MovTextContext*)priv, size);
541 }
542
543 3 static void mov_text_end_cb(void *priv)
544 {
545 // End of text, close any open style record
546 3 mov_text_style_start((MovTextContext*)priv);
547 3 }
548
549 3 static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
550 {
551 uint8_t style_flags, alpha;
552 uint32_t color;
553
554
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (style) {
555 3 style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
6 (!!style->italic * STYLE_FLAG_ITALIC) |
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 (!!style->underline * STYLE_FLAG_UNDERLINE);
558 3 mov_text_style_set(s, style_flags);
559 3 color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8;
560 3 mov_text_color_set(s, color);
561 3 alpha = 255 - ((uint32_t)style->primary_color >> 24);
562 3 mov_text_alpha_set(s, alpha);
563 3 mov_text_font_size_set(s, style->font_size);
564 3 mov_text_font_name_set(s, style->font_name);
565 } else {
566 // End current style record, go back to defaults
567 mov_text_style_start(s);
568 }
569 3 }
570
571 3 static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
572 {
573 3 ASSStyle *style = ff_ass_style_get(s->ass_ctx, dialog->style);
574
575 3 s->ass_dialog_style = style;
576 3 mov_text_ass_style_set(s, style);
577 3 }
578
579 static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
580 {
581 MovTextContext *s = priv;
582 ASSStyle *style;
583
584 if (!style_name || !*style_name)
585 style = s->ass_dialog_style;
586 else
587 style= ff_ass_style_get(s->ass_ctx, style_name);
588
589 mov_text_ass_style_set(s, style);
590 }
591
592 5 static unsigned utf8_strlen(const char *text, int len)
593 {
594 5 unsigned i = 0, ret = 0;
595
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 5 times.
48 while (i < len) {
596 43 char c = text[i];
597
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if ((c & 0x80) == 0)
598 43 i += 1;
599 else if ((c & 0xE0) == 0xC0)
600 i += 2;
601 else if ((c & 0xF0) == 0xE0)
602 i += 3;
603 else if ((c & 0xF8) == 0xF0)
604 i += 4;
605 else
606 return 0;
607 43 ret++;
608 }
609 5 return ret;
610 }
611
612 5 static void mov_text_text_cb(void *priv, const char *text, int len)
613 {
614 5 unsigned utf8_len = utf8_strlen(text, len);
615 5 MovTextContext *s = priv;
616 5 av_bprint_append_data(&s->buffer, text, len);
617 // If it's not utf-8, just use the byte length
618
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 s->text_pos += utf8_len ? utf8_len : len;
619 5 }
620
621 2 static void mov_text_new_line_cb(void *priv, int forced)
622 {
623 2 MovTextContext *s = priv;
624 2 s->text_pos += 1;
625 2 av_bprint_chars(&s->buffer, '\n', 1);
626 2 }
627
628 static const ASSCodesCallbacks mov_text_callbacks = {
629 .text = mov_text_text_cb,
630 .new_line = mov_text_new_line_cb,
631 .style = mov_text_style_cb,
632 .color = mov_text_color_cb,
633 .alpha = mov_text_alpha_cb,
634 .font_name = mov_text_font_name_cb,
635 .font_size = mov_text_font_size_cb,
636 .cancel_overrides = mov_text_cancel_overrides_cb,
637 .end = mov_text_end_cb,
638 };
639
640 3 static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
641 int bufsize, const AVSubtitle *sub)
642 {
643 3 MovTextContext *s = avctx->priv_data;
644 ASSDialog *dialog;
645 int i, length;
646
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (bufsize < 3)
648 goto too_small;
649
650 3 s->text_pos = 0;
651 3 s->count = 0;
652 3 s->box_flags = 0;
653
654 3 av_bprint_init_for_buffer(&s->buffer, buf + 2, bufsize - 2);
655
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (i = 0; i < sub->num_rects; i++) {
656 3 const char *ass = sub->rects[i]->ass;
657
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (sub->rects[i]->type != SUBTITLE_ASS) {
659 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
660 return AVERROR(EINVAL);
661 }
662
663 3 dialog = ff_ass_split_dialog(s->ass_ctx, ass);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!dialog)
665 return AVERROR(ENOMEM);
666 3 mov_text_dialog(s, dialog);
667 3 ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
668 3 ff_ass_free_dialog(&dialog);
669 }
670
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->buffer.len > UINT16_MAX)
672 return AVERROR(ERANGE);
673 3 AV_WB16(buf, s->buffer.len);
674
675
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (size_t j = 0; j < box_count; j++)
676 9 box_types[j].encode(s);
677
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->buffer.len)
679 return 0;
680
681
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!av_bprint_is_complete(&s->buffer)) {
682 too_small:
683 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
684 return AVERROR_BUFFER_TOO_SMALL;
685 }
686
687 3 length = s->buffer.len + 2;
688
689 3 return length;
690 }
691
692 #define OFFSET(x) offsetof(MovTextContext, x)
693 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
694 static const AVOption options[] = {
695 { "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
696 { NULL },
697 };
698
699 static const AVClass mov_text_encoder_class = {
700 .class_name = "MOV text enoder",
701 .item_name = av_default_item_name,
702 .option = options,
703 .version = LIBAVUTIL_VERSION_INT,
704 };
705
706 const FFCodec ff_movtext_encoder = {
707 .p.name = "mov_text",
708 CODEC_LONG_NAME("3GPP Timed Text subtitle"),
709 .p.type = AVMEDIA_TYPE_SUBTITLE,
710 .p.id = AV_CODEC_ID_MOV_TEXT,
711 .priv_data_size = sizeof(MovTextContext),
712 .p.priv_class = &mov_text_encoder_class,
713 .init = mov_text_encode_init,
714 FF_CODEC_ENCODE_SUB_CB(mov_text_encode_frame),
715 .close = mov_text_encode_close,
716 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
717 };
718