You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

576 lines
18KB

  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. #include <stdarg.h>
  22. #include "avcodec.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.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. #define STYLE_FLAG_BOLD (1<<0)
  31. #define STYLE_FLAG_ITALIC (1<<1)
  32. #define STYLE_FLAG_UNDERLINE (1<<2)
  33. #define STYLE_RECORD_SIZE 12
  34. #define SIZE_ADD 10
  35. #define STYL_BOX (1<<0)
  36. #define HLIT_BOX (1<<1)
  37. #define HCLR_BOX (1<<2)
  38. #define DEFAULT_STYLE_FONT_ID 0x01
  39. #define DEFAULT_STYLE_FONTSIZE 0x12
  40. #define DEFAULT_STYLE_COLOR 0xffffffff
  41. #define DEFAULT_STYLE_FLAG 0x00
  42. #define BGR_TO_RGB(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff))
  43. #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
  44. typedef struct {
  45. uint16_t style_start;
  46. uint16_t style_end;
  47. uint8_t style_flag;
  48. uint16_t style_fontID;
  49. uint8_t style_fontsize;
  50. uint32_t style_color;
  51. } StyleBox;
  52. typedef struct {
  53. uint16_t start;
  54. uint16_t end;
  55. } HighlightBox;
  56. typedef struct {
  57. uint32_t color;
  58. } HilightcolorBox;
  59. typedef struct {
  60. AVCodecContext *avctx;
  61. ASSSplitContext *ass_ctx;
  62. ASSStyle *ass_dialog_style;
  63. AVBPrint buffer;
  64. StyleBox **style_attributes;
  65. StyleBox *style_attributes_temp;
  66. HighlightBox hlit;
  67. HilightcolorBox hclr;
  68. int count;
  69. uint8_t box_flags;
  70. StyleBox d;
  71. uint16_t text_pos;
  72. uint16_t byte_count;
  73. } MovTextContext;
  74. typedef struct {
  75. uint32_t type;
  76. void (*encode)(MovTextContext *s, uint32_t tsmb_type);
  77. } Box;
  78. static void mov_text_cleanup(MovTextContext *s)
  79. {
  80. int j;
  81. if (s->box_flags & STYL_BOX) {
  82. for (j = 0; j < s->count; j++) {
  83. av_freep(&s->style_attributes[j]);
  84. }
  85. av_freep(&s->style_attributes);
  86. }
  87. if (s->style_attributes_temp) {
  88. *s->style_attributes_temp = s->d;
  89. }
  90. }
  91. static void encode_styl(MovTextContext *s, uint32_t tsmb_type)
  92. {
  93. int j;
  94. uint32_t tsmb_size;
  95. uint16_t style_entries;
  96. if ((s->box_flags & STYL_BOX) && s->count) {
  97. tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD;
  98. tsmb_size = AV_RB32(&tsmb_size);
  99. style_entries = AV_RB16(&s->count);
  100. /*The above three attributes are hard coded for now
  101. but will come from ASS style in the future*/
  102. av_bprint_append_any(&s->buffer, &tsmb_size, 4);
  103. av_bprint_append_any(&s->buffer, &tsmb_type, 4);
  104. av_bprint_append_any(&s->buffer, &style_entries, 2);
  105. for (j = 0; j < s->count; j++) {
  106. uint16_t style_start, style_end, style_fontID;
  107. uint32_t style_color;
  108. style_start = AV_RB16(&s->style_attributes[j]->style_start);
  109. style_end = AV_RB16(&s->style_attributes[j]->style_end);
  110. style_color = AV_RB32(&s->style_attributes[j]->style_color);
  111. style_fontID = AV_RB16(&s->style_attributes[j]->style_fontID);
  112. av_bprint_append_any(&s->buffer, &style_start, 2);
  113. av_bprint_append_any(&s->buffer, &style_end, 2);
  114. av_bprint_append_any(&s->buffer, &style_fontID, 2);
  115. av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_flag, 1);
  116. av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_fontsize, 1);
  117. av_bprint_append_any(&s->buffer, &style_color, 4);
  118. }
  119. }
  120. mov_text_cleanup(s);
  121. }
  122. static void encode_hlit(MovTextContext *s, uint32_t tsmb_type)
  123. {
  124. uint32_t tsmb_size;
  125. uint16_t start, end;
  126. if (s->box_flags & HLIT_BOX) {
  127. tsmb_size = 12;
  128. tsmb_size = AV_RB32(&tsmb_size);
  129. start = AV_RB16(&s->hlit.start);
  130. end = AV_RB16(&s->hlit.end);
  131. av_bprint_append_any(&s->buffer, &tsmb_size, 4);
  132. av_bprint_append_any(&s->buffer, &tsmb_type, 4);
  133. av_bprint_append_any(&s->buffer, &start, 2);
  134. av_bprint_append_any(&s->buffer, &end, 2);
  135. }
  136. }
  137. static void encode_hclr(MovTextContext *s, uint32_t tsmb_type)
  138. {
  139. uint32_t tsmb_size, color;
  140. if (s->box_flags & HCLR_BOX) {
  141. tsmb_size = 12;
  142. tsmb_size = AV_RB32(&tsmb_size);
  143. color = AV_RB32(&s->hclr.color);
  144. av_bprint_append_any(&s->buffer, &tsmb_size, 4);
  145. av_bprint_append_any(&s->buffer, &tsmb_type, 4);
  146. av_bprint_append_any(&s->buffer, &color, 4);
  147. }
  148. }
  149. static const Box box_types[] = {
  150. { MKTAG('s','t','y','l'), encode_styl },
  151. { MKTAG('h','l','i','t'), encode_hlit },
  152. { MKTAG('h','c','l','r'), encode_hclr },
  153. };
  154. const static size_t box_count = FF_ARRAY_ELEMS(box_types);
  155. static av_cold int mov_text_encode_init(AVCodecContext *avctx)
  156. {
  157. /*
  158. * For now, we'll use a fixed default style. When we add styling
  159. * support, this will be generated from the ASS style.
  160. */
  161. static const uint8_t text_sample_entry[] = {
  162. 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
  163. 0x01, // int8_t horizontal-justification
  164. 0xFF, // int8_t vertical-justification
  165. 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
  166. // BoxRecord {
  167. 0x00, 0x00, // int16_t top
  168. 0x00, 0x00, // int16_t left
  169. 0x00, 0x00, // int16_t bottom
  170. 0x00, 0x00, // int16_t right
  171. // };
  172. // StyleRecord {
  173. 0x00, 0x00, // uint16_t startChar
  174. 0x00, 0x00, // uint16_t endChar
  175. 0x00, 0x01, // uint16_t font-ID
  176. 0x00, // uint8_t face-style-flags
  177. 0x12, // uint8_t font-size
  178. 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
  179. // };
  180. // FontTableBox {
  181. 0x00, 0x00, 0x00, 0x12, // uint32_t size
  182. 'f', 't', 'a', 'b', // uint8_t name[4]
  183. 0x00, 0x01, // uint16_t entry-count
  184. // FontRecord {
  185. 0x00, 0x01, // uint16_t font-ID
  186. 0x05, // uint8_t font-name-length
  187. 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
  188. // };
  189. // };
  190. };
  191. MovTextContext *s = avctx->priv_data;
  192. s->avctx = avctx;
  193. s->style_attributes_temp = av_mallocz(sizeof(*s->style_attributes_temp));
  194. if (!s->style_attributes_temp) {
  195. return AVERROR(ENOMEM);
  196. }
  197. avctx->extradata_size = sizeof text_sample_entry;
  198. avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  199. if (!avctx->extradata)
  200. return AVERROR(ENOMEM);
  201. av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  202. memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size);
  203. s->ass_ctx = ff_ass_split(avctx->subtitle_header);
  204. // TODO: Initialize from ASS style record
  205. s->d.style_fontID = DEFAULT_STYLE_FONT_ID;
  206. s->d.style_fontsize = DEFAULT_STYLE_FONTSIZE;
  207. s->d.style_color = DEFAULT_STYLE_COLOR;
  208. s->d.style_flag = DEFAULT_STYLE_FLAG;
  209. return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
  210. }
  211. // Start a new style box if needed
  212. static int mov_text_style_start(MovTextContext *s)
  213. {
  214. // there's an existing style entry
  215. if (s->style_attributes_temp->style_start == s->text_pos)
  216. // Still at same text pos, use same entry
  217. return 1;
  218. if (s->style_attributes_temp->style_flag != s->d.style_flag ||
  219. s->style_attributes_temp->style_color != s->d.style_color ||
  220. s->style_attributes_temp->style_fontsize != s->d.style_fontsize) {
  221. // last style != defaults, end the style entry and start a new one
  222. s->box_flags |= STYL_BOX;
  223. s->style_attributes_temp->style_end = s->text_pos;
  224. av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp);
  225. s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
  226. if (!s->style_attributes_temp) {
  227. mov_text_cleanup(s);
  228. av_bprint_clear(&s->buffer);
  229. s->box_flags &= ~STYL_BOX;
  230. return 0;
  231. }
  232. *s->style_attributes_temp = s->d;
  233. s->style_attributes_temp->style_start = s->text_pos;
  234. } else { // style entry matches defaults, drop entry
  235. *s->style_attributes_temp = s->d;
  236. s->style_attributes_temp->style_start = s->text_pos;
  237. }
  238. return 1;
  239. }
  240. static uint8_t mov_text_style_to_flag(const char style)
  241. {
  242. uint8_t style_flag = 0;
  243. switch (style){
  244. case 'b':
  245. style_flag = STYLE_FLAG_BOLD;
  246. break;
  247. case 'i':
  248. style_flag = STYLE_FLAG_ITALIC;
  249. break;
  250. case 'u':
  251. style_flag = STYLE_FLAG_UNDERLINE;
  252. break;
  253. }
  254. return style_flag;
  255. }
  256. static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
  257. {
  258. if (!s->style_attributes_temp ||
  259. !((s->style_attributes_temp->style_flag & style_flags) ^ style_flags)) {
  260. // setting flags that that are already set
  261. return;
  262. }
  263. if (mov_text_style_start(s))
  264. s->style_attributes_temp->style_flag |= style_flags;
  265. }
  266. static void mov_text_style_cb(void *priv, const char style, int close)
  267. {
  268. MovTextContext *s = priv;
  269. uint8_t style_flag = mov_text_style_to_flag(style);
  270. if (!s->style_attributes_temp ||
  271. !!(s->style_attributes_temp->style_flag & style_flag) != close) {
  272. // setting flag that is already set
  273. return;
  274. }
  275. if (mov_text_style_start(s)) {
  276. if (!close)
  277. s->style_attributes_temp->style_flag |= style_flag;
  278. else
  279. s->style_attributes_temp->style_flag &= ~style_flag;
  280. }
  281. }
  282. static void mov_text_color_set(MovTextContext *s, uint32_t color)
  283. {
  284. if (!s->style_attributes_temp ||
  285. (s->style_attributes_temp->style_color & 0xffffff00) == color) {
  286. // color hasn't changed
  287. return;
  288. }
  289. if (mov_text_style_start(s))
  290. s->style_attributes_temp->style_color = (color & 0xffffff00) |
  291. (s->style_attributes_temp->style_color & 0xff);
  292. }
  293. static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
  294. {
  295. MovTextContext *s = priv;
  296. color = BGR_TO_RGB(color) << 8;
  297. if (color_id == 1) { //primary color changes
  298. mov_text_color_set(s, color);
  299. } else if (color_id == 2) { //secondary color changes
  300. if (s->box_flags & HLIT_BOX) { //close tag
  301. s->hlit.end = s->text_pos;
  302. } else {
  303. s->box_flags |= HCLR_BOX;
  304. s->box_flags |= HLIT_BOX;
  305. s->hlit.start = s->text_pos;
  306. s->hclr.color = color | 0xFF; //set alpha value to FF
  307. }
  308. }
  309. /* If there are more than one secondary color changes in ASS, take start of
  310. first section and end of last section. Movtext allows only one
  311. highlight box per sample.
  312. */
  313. }
  314. static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
  315. {
  316. if (!s->style_attributes_temp ||
  317. (s->style_attributes_temp->style_color & 0xff) == alpha) {
  318. // color hasn't changed
  319. return;
  320. }
  321. if (mov_text_style_start(s))
  322. s->style_attributes_temp->style_color =
  323. (s->style_attributes_temp->style_color & 0xffffff00) | alpha;
  324. }
  325. static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
  326. {
  327. MovTextContext *s = priv;
  328. if (alpha_id == 1) // primary alpha changes
  329. mov_text_alpha_set(s, 255 - alpha);
  330. }
  331. static void mov_text_font_size_set(MovTextContext *s, int size)
  332. {
  333. if (!s->style_attributes_temp ||
  334. s->style_attributes_temp->style_fontsize == size) {
  335. // color hasn't changed
  336. return;
  337. }
  338. if (mov_text_style_start(s))
  339. s->style_attributes_temp->style_fontsize = size;
  340. }
  341. static void mov_text_font_size_cb(void *priv, int size)
  342. {
  343. mov_text_font_size_set((MovTextContext*)priv, size);
  344. }
  345. static void mov_text_end_cb(void *priv)
  346. {
  347. // End of text, close any open style record
  348. mov_text_style_start((MovTextContext*)priv);
  349. }
  350. static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
  351. {
  352. uint8_t style_flags, alpha;
  353. uint32_t color;
  354. if (style) {
  355. style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
  356. (!!style->italic * STYLE_FLAG_ITALIC) |
  357. (!!style->underline * STYLE_FLAG_UNDERLINE);
  358. mov_text_style_set(s, style_flags);
  359. color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8;
  360. mov_text_color_set(s, color);
  361. alpha = 255 - ((uint32_t)style->primary_color >> 24);
  362. mov_text_alpha_set(s, alpha);
  363. mov_text_font_size_set(s, style->font_size);
  364. } else {
  365. // End current style record, go back to defaults
  366. mov_text_style_start(s);
  367. }
  368. }
  369. static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
  370. {
  371. ASSStyle * style = ff_ass_style_get(s->ass_ctx, dialog->style);
  372. s->ass_dialog_style = style;
  373. mov_text_ass_style_set(s, style);
  374. }
  375. static void mov_text_cancel_overrides_cb(void *priv, const char * style_name)
  376. {
  377. MovTextContext *s = priv;
  378. ASSStyle * style;
  379. if (!style_name || !*style_name)
  380. style = s->ass_dialog_style;
  381. else
  382. style= ff_ass_style_get(s->ass_ctx, style_name);
  383. mov_text_ass_style_set(s, style);
  384. }
  385. static uint16_t utf8_strlen(const char *text, int len)
  386. {
  387. uint16_t i = 0, ret = 0;
  388. while (i < len) {
  389. char c = text[i];
  390. if ((c & 0x80) == 0)
  391. i += 1;
  392. else if ((c & 0xE0) == 0xC0)
  393. i += 2;
  394. else if ((c & 0xF0) == 0xE0)
  395. i += 3;
  396. else if ((c & 0xF8) == 0xF0)
  397. i += 4;
  398. else
  399. return 0;
  400. ret++;
  401. }
  402. return ret;
  403. }
  404. static void mov_text_text_cb(void *priv, const char *text, int len)
  405. {
  406. uint16_t utf8_len = utf8_strlen(text, len);
  407. MovTextContext *s = priv;
  408. av_bprint_append_data(&s->buffer, text, len);
  409. // If it's not utf-8, just use the byte length
  410. s->text_pos += utf8_len ? utf8_len : len;
  411. s->byte_count += len;
  412. }
  413. static void mov_text_new_line_cb(void *priv, int forced)
  414. {
  415. MovTextContext *s = priv;
  416. av_bprint_append_data(&s->buffer, "\n", 1);
  417. s->text_pos += 1;
  418. s->byte_count += 1;
  419. }
  420. static const ASSCodesCallbacks mov_text_callbacks = {
  421. .text = mov_text_text_cb,
  422. .new_line = mov_text_new_line_cb,
  423. .style = mov_text_style_cb,
  424. .color = mov_text_color_cb,
  425. .alpha = mov_text_alpha_cb,
  426. .font_size = mov_text_font_size_cb,
  427. .cancel_overrides = mov_text_cancel_overrides_cb,
  428. .end = mov_text_end_cb,
  429. };
  430. static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  431. int bufsize, const AVSubtitle *sub)
  432. {
  433. MovTextContext *s = avctx->priv_data;
  434. ASSDialog *dialog;
  435. int i, length;
  436. size_t j;
  437. s->byte_count = 0;
  438. s->text_pos = 0;
  439. s->count = 0;
  440. s->box_flags = 0;
  441. for (i = 0; i < sub->num_rects; i++) {
  442. const char *ass = sub->rects[i]->ass;
  443. if (sub->rects[i]->type != SUBTITLE_ASS) {
  444. av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
  445. return AVERROR(ENOSYS);
  446. }
  447. #if FF_API_ASS_TIMING
  448. if (!strncmp(ass, "Dialogue: ", 10)) {
  449. int num;
  450. dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num);
  451. for (; dialog && num--; dialog++) {
  452. mov_text_dialog(s, dialog);
  453. ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
  454. }
  455. } else {
  456. #endif
  457. dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
  458. if (!dialog)
  459. return AVERROR(ENOMEM);
  460. mov_text_dialog(s, dialog);
  461. ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
  462. ff_ass_free_dialog(&dialog);
  463. #if FF_API_ASS_TIMING
  464. }
  465. #endif
  466. for (j = 0; j < box_count; j++) {
  467. box_types[j].encode(s, box_types[j].type);
  468. }
  469. }
  470. AV_WB16(buf, s->byte_count);
  471. buf += 2;
  472. if (!av_bprint_is_complete(&s->buffer)) {
  473. length = AVERROR(ENOMEM);
  474. goto exit;
  475. }
  476. if (!s->buffer.len) {
  477. length = 0;
  478. goto exit;
  479. }
  480. if (s->buffer.len > bufsize - 3) {
  481. av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
  482. length = AVERROR(EINVAL);
  483. goto exit;
  484. }
  485. memcpy(buf, s->buffer.str, s->buffer.len);
  486. length = s->buffer.len + 2;
  487. exit:
  488. av_bprint_clear(&s->buffer);
  489. return length;
  490. }
  491. static int mov_text_encode_close(AVCodecContext *avctx)
  492. {
  493. MovTextContext *s = avctx->priv_data;
  494. ff_ass_split_free(s->ass_ctx);
  495. av_bprint_finalize(&s->buffer, NULL);
  496. return 0;
  497. }
  498. AVCodec ff_movtext_encoder = {
  499. .name = "mov_text",
  500. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  501. .type = AVMEDIA_TYPE_SUBTITLE,
  502. .id = AV_CODEC_ID_MOV_TEXT,
  503. .priv_data_size = sizeof(MovTextContext),
  504. .init = mov_text_encode_init,
  505. .encode_sub = mov_text_encode_frame,
  506. .close = mov_text_encode_close,
  507. };