FFmpeg
movtextenc.c
Go to the documentation of this file.
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/opt.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/common.h"
28 #include "ass_split.h"
29 #include "ass.h"
30 #include "bytestream.h"
31 #include "codec_internal.h"
32 
33 #define STYLE_FLAG_BOLD (1<<0)
34 #define STYLE_FLAG_ITALIC (1<<1)
35 #define STYLE_FLAG_UNDERLINE (1<<2)
36 #define STYLE_RECORD_SIZE 12
37 #define SIZE_ADD 10
38 
39 #define STYL_BOX (1<<0)
40 #define HLIT_BOX (1<<1)
41 #define HCLR_BOX (1<<2)
42 
43 #define DEFAULT_STYLE_FONT_ID 0x01
44 #define DEFAULT_STYLE_FONTSIZE 0x12
45 #define DEFAULT_STYLE_COLOR 0xffffffff
46 #define DEFAULT_STYLE_FLAG 0x00
47 
48 #define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((uint32_t)(c) >> 16) & 0xff))
49 #define FONTSIZE_SCALE(s,fs) ((fs) * (s)->font_scale_factor + 0.5)
50 #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
51 
52 typedef struct {
53  uint16_t style_start;
54  uint16_t style_end;
55  uint8_t style_flag;
56  uint16_t style_fontID;
57  uint8_t style_fontsize;
58  uint32_t style_color;
59 } StyleBox;
60 
61 typedef struct {
62  uint16_t start;
63  uint16_t end;
64 } HighlightBox;
65 
66 typedef struct {
67  uint32_t color;
69 
70 typedef struct {
71  AVClass *class;
73 
77  unsigned count;
80  AVBPrint buffer;
83  uint8_t box_flags;
85  uint16_t text_pos;
86  char **fonts;
89  int frame_height;
91 
92 typedef struct {
93  void (*encode)(MovTextContext *s);
94 } Box;
95 
97 {
98  s->count = 0;
99  s->style_attributes_temp = s->d;
100 }
101 
103 {
104  if ((s->box_flags & STYL_BOX) && s->count) {
105  uint8_t buf[12], *p = buf;
106 
107  bytestream_put_be32(&p, s->count * STYLE_RECORD_SIZE + SIZE_ADD);
108  bytestream_put_be32(&p, MKBETAG('s','t','y','l'));
109  bytestream_put_be16(&p, s->count);
110  /*The above three attributes are hard coded for now
111  but will come from ASS style in the future*/
112  av_bprint_append_any(&s->buffer, buf, 10);
113  for (unsigned j = 0; j < s->count; j++) {
114  const StyleBox *style = &s->style_attributes[j];
115 
116  p = buf;
117  bytestream_put_be16(&p, style->style_start);
118  bytestream_put_be16(&p, style->style_end);
119  bytestream_put_be16(&p, style->style_fontID);
120  bytestream_put_byte(&p, style->style_flag);
121  bytestream_put_byte(&p, style->style_fontsize);
122  bytestream_put_be32(&p, style->style_color);
123 
124  av_bprint_append_any(&s->buffer, buf, 12);
125  }
126  }
128 }
129 
131 {
132  if (s->box_flags & HLIT_BOX) {
133  uint8_t buf[12], *p = buf;
134 
135  bytestream_put_be32(&p, 12);
136  bytestream_put_be32(&p, MKBETAG('h','l','i','t'));
137  bytestream_put_be16(&p, s->hlit.start);
138  bytestream_put_be16(&p, s->hlit.end);
139 
140  av_bprint_append_any(&s->buffer, buf, 12);
141  }
142 }
143 
145 {
146  if (s->box_flags & HCLR_BOX) {
147  uint8_t buf[12], *p = buf;
148 
149  bytestream_put_be32(&p, 12);
150  bytestream_put_be32(&p, MKBETAG('h','c','l','r'));
151  bytestream_put_be32(&p, s->hclr.color);
152 
153  av_bprint_append_any(&s->buffer, buf, 12);
154  }
155 }
156 
157 static const Box box_types[] = {
158  { encode_styl },
159  { encode_hlit },
160  { encode_hclr },
161 };
162 
163 const static size_t box_count = FF_ARRAY_ELEMS(box_types);
164 
166 {
167  MovTextContext *s = avctx->priv_data;
168 
169  ff_ass_split_free(s->ass_ctx);
170  av_freep(&s->style_attributes);
171  av_freep(&s->fonts);
172  return 0;
173 }
174 
176 {
177  ASS *ass;
178  ASSStyle *style;
179  int i, j;
180  uint32_t back_color = 0;
181  int font_names_total_len = 0;
182  MovTextContext *s = avctx->priv_data;
183  uint8_t buf[30], *p = buf;
184  int ret;
185 
186  av_bprint_init(&s->buffer, 0, INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE + 1);
187 
188  // 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
189  // 0x01, // int8_t horizontal-justification
190  // 0xFF, // int8_t vertical-justification
191  // 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
192  // BoxRecord {
193  // 0x00, 0x00, // int16_t top
194  // 0x00, 0x00, // int16_t left
195  // 0x00, 0x00, // int16_t bottom
196  // 0x00, 0x00, // int16_t right
197  // };
198  // StyleRecord {
199  // 0x00, 0x00, // uint16_t startChar
200  // 0x00, 0x00, // uint16_t endChar
201  // 0x00, 0x01, // uint16_t font-ID
202  // 0x00, // uint8_t face-style-flags
203  // 0x12, // uint8_t font-size
204  // 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
205  // };
206  // FontTableBox {
207  // 0x00, 0x00, 0x00, 0x12, // uint32_t size
208  // 'f', 't', 'a', 'b', // uint8_t name[4]
209  // 0x00, 0x01, // uint16_t entry-count
210  // FontRecord {
211  // 0x00, 0x01, // uint16_t font-ID
212  // 0x05, // uint8_t font-name-length
213  // 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
214  // };
215  // };
216 
217  // Populate sample description from ASS header
218  ass = (ASS*)s->ass_ctx;
219  // Compute font scaling factor based on (optionally) provided
220  // output video height and ASS script play_res_y
221  if (s->frame_height && ass->script_info.play_res_y)
222  s->font_scale_factor = (double)s->frame_height / ass->script_info.play_res_y;
223  else
224  s->font_scale_factor = 1;
225 
226  style = ff_ass_style_get(s->ass_ctx, "Default");
227  if (!style && ass->styles_count) {
228  style = &ass->styles[0];
229  }
230  s->d.style_fontID = DEFAULT_STYLE_FONT_ID;
231  s->d.style_fontsize = DEFAULT_STYLE_FONTSIZE;
232  s->d.style_color = DEFAULT_STYLE_COLOR;
233  s->d.style_flag = DEFAULT_STYLE_FLAG;
234  if (style) {
235  s->d.style_fontsize = FONTSIZE_SCALE(s, style->font_size);
236  s->d.style_color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8 |
237  255 - ((uint32_t)style->primary_color >> 24);
238  s->d.style_flag = (!!style->bold * STYLE_FLAG_BOLD) |
239  (!!style->italic * STYLE_FLAG_ITALIC) |
240  (!!style->underline * STYLE_FLAG_UNDERLINE);
241  back_color = (BGR_TO_RGB(style->back_color & 0xffffff) << 8) |
242  (255 - ((uint32_t)style->back_color >> 24));
243  }
244 
245  bytestream_put_be32(&p, 0); // displayFlags
246  bytestream_put_be16(&p, 0x01FF); // horizontal/vertical justification (2x int8_t)
247  bytestream_put_be32(&p, back_color);
248  bytestream_put_be64(&p, 0); // BoxRecord - 4xint16_t: top, left, bottom, right
249  // StyleRecord {
250  bytestream_put_be16(&p, s->d.style_start);
251  bytestream_put_be16(&p, s->d.style_end);
252  bytestream_put_be16(&p, s->d.style_fontID);
253  bytestream_put_byte(&p, s->d.style_flag);
254  bytestream_put_byte(&p, s->d.style_fontsize);
255  bytestream_put_be32(&p, s->d.style_color);
256  // };
257  av_bprint_append_any(&s->buffer, buf, 30);
258 
259  // Build font table
260  // We can't build a complete font table since that would require
261  // scanning all dialogs first. But we can at least fill in what
262  // is avaiable in the ASS header
263  if (style && ass->styles_count) {
264  // Find unique font names
265  if (style->font_name) {
266  av_dynarray_add(&s->fonts, &s->font_count, style->font_name);
267  font_names_total_len += strlen(style->font_name);
268  }
269  for (i = 0; i < ass->styles_count; i++) {
270  int found = 0;
271  if (!ass->styles[i].font_name)
272  continue;
273  for (j = 0; j < s->font_count; j++) {
274  if (!strcmp(s->fonts[j], ass->styles[i].font_name)) {
275  found = 1;
276  break;
277  }
278  }
279  if (!found) {
280  av_dynarray_add(&s->fonts, &s->font_count,
281  ass->styles[i].font_name);
282  font_names_total_len += strlen(ass->styles[i].font_name);
283  }
284  }
285  } else
286  av_dynarray_add(&s->fonts, &s->font_count, (char*)"Serif");
287 
288  // FontTableBox {
289  p = buf;
290  bytestream_put_be32(&p, SIZE_ADD + 3 * s->font_count + font_names_total_len); // Size
291  bytestream_put_be32(&p, MKBETAG('f','t','a','b'));
292  bytestream_put_be16(&p, s->font_count);
293 
294  av_bprint_append_any(&s->buffer, buf, 10);
295  // FontRecord {
296  for (i = 0; i < s->font_count; i++) {
297  size_t len = strlen(s->fonts[i]);
298 
299  p = buf;
300  bytestream_put_be16(&p, i + 1); //fontID
301  bytestream_put_byte(&p, len);
302 
303  av_bprint_append_any(&s->buffer, buf, 3);
304  av_bprint_append_any(&s->buffer, s->fonts[i], len);
305  }
306  // };
307  // };
308 
309  if (!av_bprint_is_complete(&s->buffer)) {
310  ret = AVERROR(ENOMEM);
311  goto fail;
312  }
313 
314  avctx->extradata_size = s->buffer.len;
316  if (!avctx->extradata) {
317  ret = AVERROR(ENOMEM);
318  goto fail;
319  }
320 
321  memcpy(avctx->extradata, s->buffer.str, avctx->extradata_size);
322  ret = 0;
323 fail:
324  av_bprint_finalize(&s->buffer, NULL);
325 
326  return ret;
327 }
328 
330 {
331  int ret;
332  MovTextContext *s = avctx->priv_data;
333  s->avctx = avctx;
334 
335  s->ass_ctx = ff_ass_split(avctx->subtitle_header);
336  if (!s->ass_ctx)
337  return AVERROR_INVALIDDATA;
339  if (ret < 0)
340  return ret;
341 
342  return 0;
343 }
344 
345 // Start a new style box if needed
347 {
348  // there's an existing style entry
349  if (s->style_attributes_temp.style_start == s->text_pos)
350  // Still at same text pos, use same entry
351  return 1;
352  if (s->style_attributes_temp.style_flag != s->d.style_flag ||
353  s->style_attributes_temp.style_color != s->d.style_color ||
354  s->style_attributes_temp.style_fontID != s->d.style_fontID ||
355  s->style_attributes_temp.style_fontsize != s->d.style_fontsize) {
356  StyleBox *tmp;
357 
358  // last style != defaults, end the style entry and start a new one
359  if (s->count + 1 > FFMIN(SIZE_MAX / sizeof(*s->style_attributes), UINT16_MAX) ||
360  !(tmp = av_fast_realloc(s->style_attributes,
361  &s->style_attributes_bytes_allocated,
362  (s->count + 1) * sizeof(*s->style_attributes)))) {
364  av_bprint_clear(&s->buffer);
365  s->box_flags &= ~STYL_BOX;
366  return 0;
367  }
368  s->style_attributes = tmp;
369  s->style_attributes_temp.style_end = s->text_pos;
370  s->style_attributes[s->count++] = s->style_attributes_temp;
371  s->box_flags |= STYL_BOX;
372  s->style_attributes_temp = s->d;
373  s->style_attributes_temp.style_start = s->text_pos;
374  } else { // style entry matches defaults, drop entry
375  s->style_attributes_temp = s->d;
376  s->style_attributes_temp.style_start = s->text_pos;
377  }
378  return 1;
379 }
380 
381 static uint8_t mov_text_style_to_flag(const char style)
382 {
383  uint8_t style_flag = 0;
384 
385  switch (style){
386  case 'b':
387  style_flag = STYLE_FLAG_BOLD;
388  break;
389  case 'i':
390  style_flag = STYLE_FLAG_ITALIC;
391  break;
392  case 'u':
393  style_flag = STYLE_FLAG_UNDERLINE;
394  break;
395  }
396  return style_flag;
397 }
398 
399 static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
400 {
401  if (!((s->style_attributes_temp.style_flag & style_flags) ^ style_flags)) {
402  // setting flags that that are already set
403  return;
404  }
405  if (mov_text_style_start(s))
406  s->style_attributes_temp.style_flag |= style_flags;
407 }
408 
409 static void mov_text_style_cb(void *priv, const char style, int close)
410 {
411  MovTextContext *s = priv;
412  uint8_t style_flag = mov_text_style_to_flag(style);
413 
414  if (!!(s->style_attributes_temp.style_flag & style_flag) != close) {
415  // setting flag that is already set
416  return;
417  }
418  if (mov_text_style_start(s)) {
419  if (!close)
420  s->style_attributes_temp.style_flag |= style_flag;
421  else
422  s->style_attributes_temp.style_flag &= ~style_flag;
423  }
424 }
425 
426 static void mov_text_color_set(MovTextContext *s, uint32_t color)
427 {
428  if ((s->style_attributes_temp.style_color & 0xffffff00) == color) {
429  // color hasn't changed
430  return;
431  }
432  if (mov_text_style_start(s))
433  s->style_attributes_temp.style_color = (color & 0xffffff00) |
434  (s->style_attributes_temp.style_color & 0xff);
435 }
436 
437 static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
438 {
439  MovTextContext *s = priv;
440 
441  color = BGR_TO_RGB(color) << 8;
442  if (color_id == 1) { //primary color changes
444  } else if (color_id == 2) { //secondary color changes
445  if (!(s->box_flags & HCLR_BOX))
446  // Highlight alpha not set yet, use current primary alpha
447  s->hclr.color = s->style_attributes_temp.style_color;
448  if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
449  s->box_flags |= HCLR_BOX;
450  s->box_flags |= HLIT_BOX;
451  s->hlit.start = s->text_pos;
452  s->hclr.color = color | (s->hclr.color & 0xFF);
453  }
454  else //close tag
455  s->hlit.end = s->text_pos;
456  /* If there are more than one secondary color changes in ASS,
457  take start of first section and end of last section. Movtext
458  allows only one highlight box per sample.
459  */
460  }
461  // Movtext does not support changes to other color_id (outline, background)
462 }
463 
464 static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
465 {
466  if ((s->style_attributes_temp.style_color & 0xff) == alpha) {
467  // color hasn't changed
468  return;
469  }
470  if (mov_text_style_start(s))
471  s->style_attributes_temp.style_color =
472  (s->style_attributes_temp.style_color & 0xffffff00) | alpha;
473 }
474 
475 static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
476 {
477  MovTextContext *s = priv;
478 
479  alpha = 255 - alpha;
480  if (alpha_id == 1) // primary alpha changes
482  else if (alpha_id == 2) { //secondary alpha changes
483  if (!(s->box_flags & HCLR_BOX))
484  // Highlight color not set yet, use current primary color
485  s->hclr.color = s->style_attributes_temp.style_color;
486  if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
487  s->box_flags |= HCLR_BOX;
488  s->box_flags |= HLIT_BOX;
489  s->hlit.start = s->text_pos;
490  s->hclr.color = (s->hclr.color & 0xffffff00) | alpha;
491  }
492  else //close tag
493  s->hlit.end = s->text_pos;
494  }
495  // Movtext does not support changes to other alpha_id (outline, background)
496 }
497 
498 static uint16_t find_font_id(MovTextContext *s, const char *name)
499 {
500  if (!name)
501  return 1;
502 
503  for (int i = 0; i < s->font_count; i++) {
504  if (!strcmp(name, s->fonts[i]))
505  return i + 1;
506  }
507  return 1;
508 }
509 
510 static void mov_text_font_name_set(MovTextContext *s, const char *name)
511 {
512  int fontID = find_font_id(s, name);
513  if (s->style_attributes_temp.style_fontID == fontID) {
514  // color hasn't changed
515  return;
516  }
517  if (mov_text_style_start(s))
518  s->style_attributes_temp.style_fontID = fontID;
519 }
520 
521 static void mov_text_font_name_cb(void *priv, const char *name)
522 {
524 }
525 
527 {
529  if (s->style_attributes_temp.style_fontsize == size) {
530  // color hasn't changed
531  return;
532  }
533  if (mov_text_style_start(s))
534  s->style_attributes_temp.style_fontsize = size;
535 }
536 
537 static void mov_text_font_size_cb(void *priv, int size)
538 {
540 }
541 
542 static void mov_text_end_cb(void *priv)
543 {
544  // End of text, close any open style record
546 }
547 
549 {
550  uint8_t style_flags, alpha;
551  uint32_t color;
552 
553  if (style) {
554  style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
555  (!!style->italic * STYLE_FLAG_ITALIC) |
556  (!!style->underline * STYLE_FLAG_UNDERLINE);
557  mov_text_style_set(s, style_flags);
558  color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8;
560  alpha = 255 - ((uint32_t)style->primary_color >> 24);
564  } else {
565  // End current style record, go back to defaults
567  }
568 }
569 
571 {
572  ASSStyle *style = ff_ass_style_get(s->ass_ctx, dialog->style);
573 
574  s->ass_dialog_style = style;
575  mov_text_ass_style_set(s, style);
576 }
577 
578 static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
579 {
580  MovTextContext *s = priv;
581  ASSStyle *style;
582 
583  if (!style_name || !*style_name)
584  style = s->ass_dialog_style;
585  else
586  style= ff_ass_style_get(s->ass_ctx, style_name);
587 
588  mov_text_ass_style_set(s, style);
589 }
590 
591 static unsigned utf8_strlen(const char *text, int len)
592 {
593  unsigned i = 0, ret = 0;
594  while (i < len) {
595  char c = text[i];
596  if ((c & 0x80) == 0)
597  i += 1;
598  else if ((c & 0xE0) == 0xC0)
599  i += 2;
600  else if ((c & 0xF0) == 0xE0)
601  i += 3;
602  else if ((c & 0xF8) == 0xF0)
603  i += 4;
604  else
605  return 0;
606  ret++;
607  }
608  return ret;
609 }
610 
611 static void mov_text_text_cb(void *priv, const char *text, int len)
612 {
613  unsigned utf8_len = utf8_strlen(text, len);
614  MovTextContext *s = priv;
615  av_bprint_append_data(&s->buffer, text, len);
616  // If it's not utf-8, just use the byte length
617  s->text_pos += utf8_len ? utf8_len : len;
618 }
619 
620 static void mov_text_new_line_cb(void *priv, int forced)
621 {
622  MovTextContext *s = priv;
623  s->text_pos += 1;
624  av_bprint_chars(&s->buffer, '\n', 1);
625 }
626 
629  .new_line = mov_text_new_line_cb,
630  .style = mov_text_style_cb,
631  .color = mov_text_color_cb,
632  .alpha = mov_text_alpha_cb,
633  .font_name = mov_text_font_name_cb,
634  .font_size = mov_text_font_size_cb,
635  .cancel_overrides = mov_text_cancel_overrides_cb,
636  .end = mov_text_end_cb,
637 };
638 
639 static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
640  int bufsize, const AVSubtitle *sub)
641 {
642  MovTextContext *s = avctx->priv_data;
643  ASSDialog *dialog;
644  int i, length;
645 
646  if (bufsize < 3)
647  goto too_small;
648 
649  s->text_pos = 0;
650  s->count = 0;
651  s->box_flags = 0;
652 
653  av_bprint_init_for_buffer(&s->buffer, buf + 2, bufsize - 2);
654  for (i = 0; i < sub->num_rects; i++) {
655  const char *ass = sub->rects[i]->ass;
656 
657  if (sub->rects[i]->type != SUBTITLE_ASS) {
658  av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
659  return AVERROR(EINVAL);
660  }
661 
662  dialog = ff_ass_split_dialog(s->ass_ctx, ass);
663  if (!dialog)
664  return AVERROR(ENOMEM);
665  mov_text_dialog(s, dialog);
667  ff_ass_free_dialog(&dialog);
668  }
669 
670  if (s->buffer.len > UINT16_MAX)
671  return AVERROR(ERANGE);
672  AV_WB16(buf, s->buffer.len);
673 
674  for (size_t j = 0; j < box_count; j++)
675  box_types[j].encode(s);
676 
677  if (!s->buffer.len)
678  return 0;
679 
680  if (!av_bprint_is_complete(&s->buffer)) {
681 too_small:
682  av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
684  }
685 
686  length = s->buffer.len + 2;
687 
688  return length;
689 }
690 
691 #define OFFSET(x) offsetof(MovTextContext, x)
692 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
693 static const AVOption options[] = {
694  { "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
695  { NULL },
696 };
697 
699  .class_name = "MOV text enoder",
700  .item_name = av_default_item_name,
701  .option = options,
702  .version = LIBAVUTIL_VERSION_INT,
703 };
704 
706  .p.name = "mov_text",
707  CODEC_LONG_NAME("3GPP Timed Text subtitle"),
708  .p.type = AVMEDIA_TYPE_SUBTITLE,
709  .p.id = AV_CODEC_ID_MOV_TEXT,
710  .priv_data_size = sizeof(MovTextContext),
711  .p.priv_class = &mov_text_encoder_class,
712  .init = mov_text_encode_init,
714  .close = mov_text_encode_close,
715  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
716 };
AVSubtitle
Definition: avcodec.h:2254
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
MovTextContext::avctx
AVCodecContext * avctx
Definition: movtextenc.c:72
StyleBox::style_start
uint16_t style_start
Definition: movtextenc.c:53
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
utf8_strlen
static unsigned utf8_strlen(const char *text, int len)
Definition: movtextenc.c:591
ASSCodesCallbacks
Set of callback functions corresponding to each override codes that can be encountered in a "Dialogue...
Definition: ass_split.h:138
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVSubtitle::rects
AVSubtitleRect ** rects
Definition: avcodec.h:2259
opt.h
mov_text_dialog
static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
Definition: movtextenc.c:570
color
Definition: vf_paletteuse.c:513
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
MovTextContext::d
StyleBox d
Definition: movtextenc.c:84
mov_text_cancel_overrides_cb
static void mov_text_cancel_overrides_cb(void *priv, const char *style_name)
Definition: movtextenc.c:578
AVSubtitle::num_rects
unsigned num_rects
Definition: avcodec.h:2258
StyleBox::style_fontID
uint16_t style_fontID
Definition: movtextenc.c:56
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
ff_ass_split_dialog
ASSDialog * ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf)
Split one ASS Dialogue line from a string buffer.
Definition: ass_split.c:433
AVOption
AVOption.
Definition: opt.h:429
mov_text_font_name_set
static void mov_text_font_name_set(MovTextContext *s, const char *name)
Definition: movtextenc.c:510
options
static const AVOption options[]
Definition: movtextenc.c:693
ASS::styles
ASSStyle * styles
array of split out styles
Definition: ass_split.h:92
ASSStyle::font_size
int font_size
font height
Definition: ass_split.h:42
FFCodec
Definition: codec_internal.h:127
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Definition: avcodec.h:1918
mov_text_encode_init
static av_cold int mov_text_encode_init(AVCodecContext *avctx)
Definition: movtextenc.c:329
HighlightBox
Definition: movtextdec.c:82
MovTextContext::font_scale_factor
double font_scale_factor
Definition: movtextenc.c:88
box_count
const static size_t box_count
Definition: movtextenc.c:163
ASSDialog::style
char * style
name of the ASSStyle to use with this dialog
Definition: ass_split.h:76
OFFSET
#define OFFSET(x)
Definition: movtextenc.c:691
mov_text_color_set
static void mov_text_color_set(MovTextContext *s, uint32_t color)
Definition: movtextenc.c:426
DEFAULT_STYLE_FONT_ID
#define DEFAULT_STYLE_FONT_ID
Definition: movtextenc.c:43
mov_text_encoder_class
static const AVClass mov_text_encoder_class
Definition: movtextenc.c:698
SUBTITLE_ASS
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition: avcodec.h:2222
ASSStyle::font_name
char * font_name
font face (case sensitive)
Definition: ass_split.h:41
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
av_bprint_init_for_buffer
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
Init a print buffer using a pre-existing buffer.
Definition: bprint.c:85
mov_text_font_size_set
static void mov_text_font_size_set(MovTextContext *s, int size)
Definition: movtextenc.c:526
Box
Definition: movtextdec.c:110
mov_text_alpha_set
static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
Definition: movtextenc.c:464
fail
#define fail()
Definition: checkasm.h:193
StyleBox
Definition: movtextdec.c:61
ass_split.h
mov_text_color_cb
static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
Definition: movtextenc.c:437
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
FONTSIZE_SCALE
#define FONTSIZE_SCALE(s, fs)
Definition: movtextenc.c:49
HilightcolorBox::color
uint32_t color
Definition: movtextenc.c:67
STYL_BOX
#define STYL_BOX
Definition: movtextenc.c:39
AVSubtitleRect::ass
char * ass
0 terminated ASS/SSA compatible event line.
Definition: avcodec.h:2251
ass.h
ff_ass_free_dialog
void ff_ass_free_dialog(ASSDialog **dialogp)
Free a dialogue obtained from ff_ass_split_dialog().
Definition: ass_split.c:421
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
mov_text_text_cb
static void mov_text_text_cb(void *priv, const char *text, int len)
Definition: movtextenc.c:611
mov_text_style_start
static int mov_text_style_start(MovTextContext *s)
Definition: movtextenc.c:346
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:538
box_types
static const Box box_types[]
Definition: movtextenc.c:157
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
mov_text_cleanup
static void mov_text_cleanup(MovTextContext *s)
Definition: movtextenc.c:96
STYLE_FLAG_UNDERLINE
#define STYLE_FLAG_UNDERLINE
Definition: movtextenc.c:35
MovTextContext::fonts
char ** fonts
Definition: movtextenc.c:86
FLAGS
#define FLAGS
Definition: movtextenc.c:692
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
MovTextContext::font_count
int font_count
Definition: movtextenc.c:87
if
if(ret)
Definition: filter_design.txt:179
ASSScriptInfo::play_res_y
int play_res_y
video height that ASS coords are referring to
Definition: ass_split.h:32
STYLE_FLAG_ITALIC
#define STYLE_FLAG_ITALIC
Definition: movtextenc.c:34
HLIT_BOX
#define HLIT_BOX
Definition: movtextenc.c:40
ASS
structure containing the whole split ASS data
Definition: ass_split.h:90
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
StyleBox::style_end
uint16_t style_end
Definition: movtextenc.c:54
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
MovTextContext::style_attributes
StyleBox * style_attributes
Definition: movtextenc.c:76
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
AV_CODEC_ID_MOV_TEXT
@ AV_CODEC_ID_MOV_TEXT
Definition: codec_id.h:562
ASSStyle::primary_color
int primary_color
color that a subtitle will normally appear in
Definition: ass_split.h:43
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
options
Definition: swscale.c:42
FF_CODEC_ENCODE_SUB_CB
#define FF_CODEC_ENCODE_SUB_CB(func)
Definition: codec_internal.h:323
ASSSplitContext
This struct can be casted to ASS to access to the split data.
Definition: ass_split.c:205
double
double
Definition: af_crystalizer.c:132
ff_ass_split
ASSSplitContext * ff_ass_split(const char *buf)
Split a full ASS file or a ASS header from a string buffer and store the split structure in a newly a...
Definition: ass_split.c:382
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ASSStyle
fields extracted from the [V4(+) Styles] section
Definition: ass_split.h:39
mov_text_style_to_flag
static uint8_t mov_text_style_to_flag(const char style)
Definition: movtextenc.c:381
ASSStyle::underline
int underline
whether text is underlined (1) or not (0)
Definition: ass_split.h:49
ff_ass_split_free
void ff_ass_split_free(ASSSplitContext *ctx)
Free all the memory allocated for an ASSSplitContext.
Definition: ass_split.c:470
DEFAULT_STYLE_COLOR
#define DEFAULT_STYLE_COLOR
Definition: movtextenc.c:45
av_dynarray_add
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
Definition: mem.c:329
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
codec_internal.h
ASSDialog::text
char * text
actual text which will be displayed as a subtitle, can include style override control codes (see ff_a...
Definition: ass_split.h:82
encode_styl
static void encode_styl(MovTextContext *s)
Definition: movtextenc.c:102
HighlightBox::end
uint16_t end
Definition: movtextenc.c:63
size
int size
Definition: twinvq_data.h:10344
color
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
mov_text_style_set
static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
Definition: movtextenc.c:399
ASSStyle::italic
int italic
whether text is italic (1) or not (0)
Definition: ass_split.h:48
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
MovTextContext::ass_dialog_style
ASSStyle * ass_dialog_style
Definition: movtextenc.c:75
AVSubtitleRect::type
enum AVSubtitleType type
Definition: avcodec.h:2242
encode
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:94
DEFAULT_STYLE_FONTSIZE
#define DEFAULT_STYLE_FONTSIZE
Definition: movtextenc.c:44
MovTextContext::style_attributes_temp
StyleBox style_attributes_temp
Definition: movtextenc.c:79
StyleBox::style_flag
uint8_t style_flag
Definition: movtextenc.c:55
mov_text_encode_close
static int mov_text_encode_close(AVCodecContext *avctx)
Definition: movtextenc.c:165
find_font_id
static uint16_t find_font_id(MovTextContext *s, const char *name)
Definition: movtextenc.c:498
mov_text_font_name_cb
static void mov_text_font_name_cb(void *priv, const char *name)
Definition: movtextenc.c:521
MovTextContext::count
unsigned count
Definition: movtextenc.c:77
ASSStyle::back_color
int back_color
color of the subtitle outline or shadow
Definition: ass_split.h:46
SIZE_ADD
#define SIZE_ADD
Definition: movtextenc.c:37
mov_text_callbacks
static const ASSCodesCallbacks mov_text_callbacks
Definition: movtextenc.c:627
ff_ass_style_get
ASSStyle * ff_ass_style_get(ASSSplitContext *ctx, const char *style)
Find an ASSStyle structure by its name.
Definition: ass_split.c:578
encode_sample_description
static int encode_sample_description(AVCodecContext *avctx)
Definition: movtextenc.c:175
STYLE_RECORD_SIZE
#define STYLE_RECORD_SIZE
Definition: movtextenc.c:36
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:537
STYLE_FLAG_BOLD
#define STYLE_FLAG_BOLD
Definition: movtextenc.c:33
common.h
StyleBox::style_color
uint32_t style_color
Definition: movtextenc.c:58
MovTextContext::hlit
HighlightBox hlit
Definition: movtextenc.c:81
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
mov_text_encode_frame
static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition: movtextenc.c:639
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
encode_hlit
static void encode_hlit(MovTextContext *s)
Definition: movtextenc.c:130
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
mov_text_new_line_cb
static void mov_text_new_line_cb(void *priv, int forced)
Definition: movtextenc.c:620
ASS::styles_count
int styles_count
number of ASSStyle in the styles array
Definition: ass_split.h:93
len
int len
Definition: vorbis_enc_data.h:426
mov_text_end_cb
static void mov_text_end_cb(void *priv)
Definition: movtextenc.c:542
avcodec.h
encode_hclr
static void encode_hclr(MovTextContext *s)
Definition: movtextenc.c:144
ret
ret
Definition: filter_design.txt:187
BGR_TO_RGB
#define BGR_TO_RGB(c)
Definition: movtextenc.c:48
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:80
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
mov_text_font_size_cb
static void mov_text_font_size_cb(void *priv, int size)
Definition: movtextenc.c:537
ASSDialog
fields extracted from the [Events] section
Definition: ass_split.h:71
MovTextContext::style_attributes_bytes_allocated
unsigned style_attributes_bytes_allocated
Definition: movtextenc.c:78
StyleBox::style_fontsize
uint8_t style_fontsize
Definition: movtextenc.c:57
av_bprint_append_any
#define av_bprint_append_any(buf, data, size)
Definition: movtextenc.c:50
MovTextContext::text_pos
uint16_t text_pos
Definition: movtextenc.c:85
HilightcolorBox
Definition: movtextdec.c:87
mem.h
mov_text_style_cb
static void mov_text_style_cb(void *priv, const char style, int close)
Definition: movtextenc.c:409
alpha
static const int16_t alpha[]
Definition: ilbcdata.h:55
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
mov_text_ass_style_set
static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
Definition: movtextenc.c:548
MovTextContext::hclr
HilightcolorBox hclr
Definition: movtextenc.c:82
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
bytestream.h
DEFAULT_STYLE_FLAG
#define DEFAULT_STYLE_FLAG
Definition: movtextenc.c:46
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_ass_split_override_codes
int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv, const char *buf)
Split override codes out of a ASS "Dialogue" Text field.
Definition: ass_split.c:483
ff_movtext_encoder
const FFCodec ff_movtext_encoder
Definition: movtextenc.c:705
av_bprint_chars
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:145
HighlightBox::start
uint16_t start
Definition: movtextenc.c:62
av_bprint_append_data
void av_bprint_append_data(AVBPrint *buf, const char *data, unsigned size)
Append data to a print buffer.
Definition: bprint.c:163
MovTextContext::ass_ctx
ASSSplitContext * ass_ctx
Definition: movtextenc.c:74
mov_text_alpha_cb
static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
Definition: movtextenc.c:475
MovTextContext::buffer
AVBPrint buffer
Definition: movtextenc.c:80
MovTextContext
Definition: movtextdec.c:95
ASSStyle::bold
int bold
whether text is bold (1) or not (0)
Definition: ass_split.h:47
ASSCodesCallbacks::text
void(* text)(void *priv, const char *text, int len)
Definition: ass_split.h:143
HCLR_BOX
#define HCLR_BOX
Definition: movtextenc.c:41
ASS::script_info
ASSScriptInfo script_info
general information about the SSA script
Definition: ass_split.h:91