FFmpeg  4.4.9
microdvddec.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  * MicroDVD subtitle decoder
24  *
25  * Based on the specifications found here:
26  * https://trac.videolan.org/vlc/ticket/1825#comment:6
27  */
28 
29 #include "libavutil/avstring.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/bprint.h"
33 #include "avcodec.h"
34 #include "ass.h"
35 #include "decode.h"
36 
37 static int indexof(const char *s, int c)
38 {
39  char *f = strchr(s, c);
40  return f ? (f - s) : -1;
41 }
42 
43 struct microdvd_tag {
44  char key;
46  uint32_t data1;
47  uint32_t data2;
48  char *data_string;
50 };
51 
52 #define MICRODVD_PERSISTENT_OFF 0
53 #define MICRODVD_PERSISTENT_ON 1
54 #define MICRODVD_PERSISTENT_OPENED 2
55 
56 // Color, Font, Size, cHarset, stYle, Position, cOordinate
57 #define MICRODVD_TAGS "cfshyYpo"
58 
59 static void microdvd_set_tag(struct microdvd_tag *tags, struct microdvd_tag tag)
60 {
61  int tag_index = indexof(MICRODVD_TAGS, tag.key);
62 
63  if (tag_index < 0)
64  return;
65  memcpy(&tags[tag_index], &tag, sizeof(tag));
66 }
67 
68 // italic, bold, underline, strike-through
69 #define MICRODVD_STYLES "ibus"
70 
71 /* some samples have lines that start with a / indicating non persistent italic
72  * marker */
73 static char *check_for_italic_slash_marker(struct microdvd_tag *tags, char *s)
74 {
75  if (*s == '/') {
76  struct microdvd_tag tag = tags[indexof(MICRODVD_TAGS, 'y')];
77  tag.key = 'y';
78  tag.data1 |= 1 << 0 /* 'i' position in MICRODVD_STYLES */;
79  microdvd_set_tag(tags, tag);
80  s++;
81  }
82  return s;
83 }
84 
85 static char *microdvd_load_tags(struct microdvd_tag *tags, char *s)
86 {
88 
89  while (*s == '{') {
90  char *start = s;
91  char tag_char = *(s + 1);
92  struct microdvd_tag tag = {0};
93 
94  if (!tag_char || *(s + 2) != ':')
95  break;
96  s += 3;
97 
98  switch (tag_char) {
99 
100  /* Style */
101  case 'Y':
102  tag.persistent = MICRODVD_PERSISTENT_ON;
103  case 'y':
104  while (*s && *s != '}' && s - start < 256) {
105  int style_index = indexof(MICRODVD_STYLES, *s);
106 
107  if (style_index >= 0)
108  tag.data1 |= (1 << style_index);
109  s++;
110  }
111  if (*s != '}')
112  break;
113  /* We must distinguish persistent and non-persistent styles
114  * to handle this kind of style tags: {y:ib}{Y:us} */
115  tag.key = tag_char;
116  break;
117 
118  /* Color */
119  case 'C':
120  tag.persistent = MICRODVD_PERSISTENT_ON;
121  case 'c':
122  while (*s == '$' || *s == '#')
123  s++;
124  tag.data1 = strtol(s, &s, 16) & 0x00ffffff;
125  if (*s != '}')
126  break;
127  tag.key = 'c';
128  break;
129 
130  /* Font name */
131  case 'F':
132  tag.persistent = MICRODVD_PERSISTENT_ON;
133  case 'f': {
134  int len = indexof(s, '}');
135  if (len < 0)
136  break;
137  tag.data_string = s;
138  tag.data_string_len = len;
139  s += len;
140  tag.key = 'f';
141  break;
142  }
143 
144  /* Font size */
145  case 'S':
146  tag.persistent = MICRODVD_PERSISTENT_ON;
147  case 's':
148  tag.data1 = strtol(s, &s, 10);
149  if (*s != '}')
150  break;
151  tag.key = 's';
152  break;
153 
154  /* Charset */
155  case 'H': {
156  //TODO: not yet handled, just parsed.
157  int len = indexof(s, '}');
158  if (len < 0)
159  break;
160  tag.data_string = s;
161  tag.data_string_len = len;
162  s += len;
163  tag.key = 'h';
164  break;
165  }
166 
167  /* Position */
168  case 'P':
169  if (!*s)
170  break;
171  tag.persistent = MICRODVD_PERSISTENT_ON;
172  tag.data1 = (*s++ == '1');
173  if (*s != '}')
174  break;
175  tag.key = 'p';
176  break;
177 
178  /* Coordinates */
179  case 'o':
180  tag.persistent = MICRODVD_PERSISTENT_ON;
181  tag.data1 = strtol(s, &s, 10);
182  if (*s != ',')
183  break;
184  s++;
185  tag.data2 = strtol(s, &s, 10);
186  if (*s != '}')
187  break;
188  tag.key = 'o';
189  break;
190 
191  default: /* Unknown tag, we consider it's text */
192  break;
193  }
194 
195  if (tag.key == 0)
196  return start;
197 
198  microdvd_set_tag(tags, tag);
199  s++;
200  }
201  return check_for_italic_slash_marker(tags, s);
202 }
203 
204 static void microdvd_open_tags(AVBPrint *new_line, struct microdvd_tag *tags)
205 {
206  int i, sidx;
207  for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
209  continue;
210  switch (tags[i].key) {
211  case 'Y':
212  case 'y':
213  for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++)
214  if (tags[i].data1 & (1 << sidx))
215  av_bprintf(new_line, "{\\%c1}", MICRODVD_STYLES[sidx]);
216  break;
217 
218  case 'c':
219  av_bprintf(new_line, "{\\c&H%06"PRIX32"&}", tags[i].data1);
220  break;
221 
222  case 'f':
223  av_bprintf(new_line, "{\\fn%.*s}",
224  tags[i].data_string_len, tags[i].data_string);
225  break;
226 
227  case 's':
228  av_bprintf(new_line, "{\\fs%"PRId32"}", tags[i].data1);
229  break;
230 
231  case 'p':
232  if (tags[i].data1 == 0)
233  av_bprintf(new_line, "{\\an8}");
234  break;
235 
236  case 'o':
237  av_bprintf(new_line, "{\\pos(%"PRId32",%"PRId32")}",
238  tags[i].data1, tags[i].data2);
239  break;
240  }
241  if (tags[i].persistent == MICRODVD_PERSISTENT_ON)
243  }
244 }
245 
246 static void microdvd_close_no_persistent_tags(AVBPrint *new_line,
247  struct microdvd_tag *tags)
248 {
249  int i, sidx;
250 
251  for (i = sizeof(MICRODVD_TAGS) - 2; i >= 0; i--) {
252  if (tags[i].persistent != MICRODVD_PERSISTENT_OFF)
253  continue;
254  switch (tags[i].key) {
255 
256  case 'y':
257  for (sidx = sizeof(MICRODVD_STYLES) - 2; sidx >= 0; sidx--)
258  if (tags[i].data1 & (1 << sidx))
259  av_bprintf(new_line, "{\\%c0}", MICRODVD_STYLES[sidx]);
260  break;
261 
262  case 'c':
263  av_bprintf(new_line, "{\\c}");
264  break;
265 
266  case 'f':
267  av_bprintf(new_line, "{\\fn}");
268  break;
269 
270  case 's':
271  av_bprintf(new_line, "{\\fs}");
272  break;
273  }
274  tags[i].key = 0;
275  }
276 }
277 
279  void *data, int *got_sub_ptr, AVPacket *avpkt)
280 {
281  AVSubtitle *sub = data;
282  AVBPrint new_line;
283  char *line = (char *)ff_decode_subtitle_text(avctx, avpkt);
284  FFASSDecoderContext *s = avctx->priv_data;
285  struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
286 
287  if (avpkt->size <= 0)
288  return avpkt->size;
289  if (!line)
290  return AVERROR(ENOMEM);
291 
292  av_bprint_init(&new_line, 0, 2048);
293 
294  // subtitle content
295  while (*line) {
296 
297  // parse MicroDVD tags, and open them in ASS
298  line = microdvd_load_tags(tags, line);
299  microdvd_open_tags(&new_line, tags);
300 
301  // simple copy until EOL or forced carriage return
302  while (*line && *line != '|') {
303  av_bprint_chars(&new_line, *line, 1);
304  line++;
305  }
306 
307  // line split
308  if (*line == '|') {
309  microdvd_close_no_persistent_tags(&new_line, tags);
310  av_bprintf(&new_line, "\\N");
311  line++;
312  }
313  }
314  if (new_line.len) {
315  int ret = ff_ass_add_rect(sub, new_line.str, s->readorder++, 0, NULL, NULL);
316  av_bprint_finalize(&new_line, NULL);
317  if (ret < 0)
318  return ret;
319  }
320 
321  *got_sub_ptr = sub->num_rects > 0;
322  return avpkt->size;
323 }
324 
325 static int microdvd_init(AVCodecContext *avctx)
326 {
327  int i, sidx;
328  AVBPrint font_buf;
329  int font_size = ASS_DEFAULT_FONT_SIZE;
330  int color = ASS_DEFAULT_COLOR;
331  int bold = ASS_DEFAULT_BOLD;
332  int italic = ASS_DEFAULT_ITALIC;
333  int underline = ASS_DEFAULT_UNDERLINE;
334  int alignment = ASS_DEFAULT_ALIGNMENT;
335  struct microdvd_tag tags[sizeof(MICRODVD_TAGS) - 1] = {{0}};
336 
338  av_bprintf(&font_buf, "%s", ASS_DEFAULT_FONT);
339 
340  if (avctx->extradata) {
341  char *default_tags = av_strndup(avctx->extradata, avctx->extradata_size);
342 
343  if (!default_tags)
344  return AVERROR(ENOMEM);
345  microdvd_load_tags(tags, default_tags);
346  for (i = 0; i < sizeof(MICRODVD_TAGS) - 1; i++) {
347  switch (av_tolower(tags[i].key)) {
348  case 'y':
349  for (sidx = 0; sidx < sizeof(MICRODVD_STYLES) - 1; sidx++) {
350  if (tags[i].data1 & (1 << sidx)) {
351  switch (MICRODVD_STYLES[sidx]) {
352  case 'i': italic = 1; break;
353  case 'b': bold = 1; break;
354  case 'u': underline = 1; break;
355  }
356  }
357  }
358  break;
359 
360  case 'c': color = tags[i].data1; break;
361  case 's': font_size = tags[i].data1; break;
362  case 'p': alignment = 8; break;
363 
364  case 'f':
365  av_bprint_clear(&font_buf);
366  av_bprintf(&font_buf, "%.*s",
367  tags[i].data_string_len, tags[i].data_string);
368  break;
369  }
370  }
371  av_free(default_tags);
372  }
373  return ff_ass_subtitle_header(avctx, font_buf.str, font_size, color,
374  ASS_DEFAULT_BACK_COLOR, bold, italic,
375  underline, ASS_DEFAULT_BORDERSTYLE,
376  alignment);
377 }
378 
380  .name = "microdvd",
381  .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle"),
382  .type = AVMEDIA_TYPE_SUBTITLE,
383  .id = AV_CODEC_ID_MICRODVD,
384  .init = microdvd_init,
385  .decode = microdvd_decode_frame,
386  .flush = ff_ass_decoder_flush,
387  .priv_data_size = sizeof(FFASSDecoderContext),
388 };
int ff_ass_subtitle_header(AVCodecContext *avctx, const char *font, int font_size, int color, int back_color, int bold, int italic, int underline, int border_style, int alignment)
Generate a suitable AVCodecContext.subtitle_header for SUBTITLE_ASS.
Definition: ass.c:83
void ff_ass_decoder_flush(AVCodecContext *avctx)
Helper to flush a text subtitles decoder making use of the FFASSDecoderContext.
Definition: ass.c:141
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker)
Add an ASS dialog to a subtitle.
Definition: ass.c:118
#define ASS_DEFAULT_ALIGNMENT
Definition: ass.h:42
#define ASS_DEFAULT_FONT_SIZE
Definition: ass.h:36
#define ASS_DEFAULT_COLOR
Definition: ass.h:37
#define ASS_DEFAULT_BORDERSTYLE
Definition: ass.h:43
#define ASS_DEFAULT_BACK_COLOR
Definition: ass.h:38
#define ASS_DEFAULT_FONT
Definition: ass.h:35
#define ASS_DEFAULT_BOLD
Definition: ass.h:39
#define ASS_DEFAULT_UNDERLINE
Definition: ass.h:41
#define ASS_DEFAULT_ITALIC
Definition: ass.h:40
Libavcodec external API header.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:245
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:237
#define AV_BPRINT_SIZE_AUTOMATIC
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
#define NULL
Definition: coverity.c:32
const char * ff_decode_subtitle_text(AVCodecContext *avctx, const AVPacket *avpkt)
Get the data of a subtitle packet followed by a 0 byte.
Definition: decode.c:1034
static float sub(float src0, float src1)
@ AV_CODEC_ID_MICRODVD
Definition: codec_id.h:533
#define AVERROR(e)
Definition: error.h:43
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:265
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
static av_const int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:246
const char * key
int i
Definition: input.c:407
#define MICRODVD_STYLES
Definition: microdvddec.c:69
AVCodec ff_microdvd_decoder
Definition: microdvddec.c:379
static int indexof(const char *s, int c)
Definition: microdvddec.c:37
static void microdvd_open_tags(AVBPrint *new_line, struct microdvd_tag *tags)
Definition: microdvddec.c:204
static int microdvd_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *avpkt)
Definition: microdvddec.c:278
static char * check_for_italic_slash_marker(struct microdvd_tag *tags, char *s)
Definition: microdvddec.c:73
static void microdvd_close_no_persistent_tags(AVBPrint *new_line, struct microdvd_tag *tags)
Definition: microdvddec.c:246
#define MICRODVD_PERSISTENT_ON
Definition: microdvddec.c:53
static void microdvd_set_tag(struct microdvd_tag *tags, struct microdvd_tag tag)
Definition: microdvddec.c:59
static char * microdvd_load_tags(struct microdvd_tag *tags, char *s)
Definition: microdvddec.c:85
#define MICRODVD_PERSISTENT_OFF
Definition: microdvddec.c:52
static int microdvd_init(AVCodecContext *avctx)
Definition: microdvddec.c:325
#define MICRODVD_PERSISTENT_OPENED
Definition: microdvddec.c:54
#define MICRODVD_TAGS
Definition: microdvddec.c:57
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
Memory handling functions.
uint32_t tag
Definition: movenc.c:1611
const char data[16]
Definition: mxf.c:142
misc parsing utilities
main external API structure.
Definition: avcodec.h:536
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
int extradata_size
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
Definition: graph2dot.c:48
char * data_string
Definition: microdvddec.c:48
uint32_t data2
Definition: microdvddec.c:47
int data_string_len
Definition: microdvddec.c:49
uint32_t data1
Definition: microdvddec.c:46
#define av_free(p)
int len
static double c[64]