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.

492 lines
15KB

  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. AVBPrint buffer;
  63. StyleBox **style_attributes;
  64. StyleBox *style_attributes_temp;
  65. HighlightBox hlit;
  66. HilightcolorBox hclr;
  67. int count;
  68. uint8_t box_flags;
  69. StyleBox d;
  70. uint16_t text_pos;
  71. uint16_t byte_count;
  72. } MovTextContext;
  73. typedef struct {
  74. uint32_t type;
  75. void (*encode)(MovTextContext *s, uint32_t tsmb_type);
  76. } Box;
  77. static void mov_text_cleanup(MovTextContext *s)
  78. {
  79. int j;
  80. if (s->box_flags & STYL_BOX) {
  81. for (j = 0; j < s->count; j++) {
  82. av_freep(&s->style_attributes[j]);
  83. }
  84. av_freep(&s->style_attributes);
  85. }
  86. if (s->style_attributes_temp) {
  87. s->style_attributes_temp->style_flag = 0;
  88. s->style_attributes_temp->style_start = 0;
  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->d.style_color);
  111. style_fontID = AV_RB16(&s->d.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->d.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) {
  219. // last style != defaults, end the style entry and start a new one
  220. s->box_flags |= STYL_BOX;
  221. s->style_attributes_temp->style_end = s->text_pos;
  222. av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp);
  223. s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
  224. if (!s->style_attributes_temp) {
  225. mov_text_cleanup(s);
  226. av_bprint_clear(&s->buffer);
  227. s->box_flags &= ~STYL_BOX;
  228. return 0;
  229. }
  230. s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag;
  231. s->style_attributes_temp->style_start = s->text_pos;
  232. } else { // style entry matches defaults, drop entry
  233. s->style_attributes_temp->style_flag = 0;
  234. s->style_attributes_temp->style_start = s->text_pos;
  235. }
  236. return 1;
  237. }
  238. static uint8_t mov_text_style_to_flag(const char style)
  239. {
  240. uint8_t style_flag = 0;
  241. switch (style){
  242. case 'b':
  243. style_flag = STYLE_FLAG_BOLD;
  244. break;
  245. case 'i':
  246. style_flag = STYLE_FLAG_ITALIC;
  247. break;
  248. case 'u':
  249. style_flag = STYLE_FLAG_UNDERLINE;
  250. break;
  251. }
  252. return style_flag;
  253. }
  254. static void mov_text_style_set(MovTextContext *s, uint8_t style_flags)
  255. {
  256. if (!s->style_attributes_temp ||
  257. !((s->style_attributes_temp->style_flag & style_flags) ^ style_flags)) {
  258. // setting flags that that are already set
  259. return;
  260. }
  261. if (mov_text_style_start(s))
  262. s->style_attributes_temp->style_flag |= style_flags;
  263. }
  264. static void mov_text_style_cb(void *priv, const char style, int close)
  265. {
  266. MovTextContext *s = priv;
  267. uint8_t style_flag = mov_text_style_to_flag(style);
  268. if (!s->style_attributes_temp ||
  269. !!(s->style_attributes_temp->style_flag & style_flag) != close) {
  270. // setting flag that is already set
  271. return;
  272. }
  273. if (mov_text_style_start(s)) {
  274. if (!close)
  275. s->style_attributes_temp->style_flag |= style_flag;
  276. else
  277. s->style_attributes_temp->style_flag &= ~style_flag;
  278. }
  279. }
  280. static void mov_text_color_cb(void *priv, unsigned int color, unsigned int color_id)
  281. {
  282. MovTextContext *s = priv;
  283. color = BGR_TO_RGB(color) << 8;
  284. if (color_id == 2) { //secondary color changes
  285. if (s->box_flags & HLIT_BOX) { //close tag
  286. s->hlit.end = s->text_pos;
  287. } else {
  288. s->box_flags |= HCLR_BOX;
  289. s->box_flags |= HLIT_BOX;
  290. s->hlit.start = s->text_pos;
  291. s->hclr.color = color | 0xFF; //set alpha value to FF
  292. }
  293. }
  294. /* If there are more than one secondary color changes in ASS, take start of
  295. first section and end of last section. Movtext allows only one
  296. highlight box per sample.
  297. */
  298. }
  299. static void mov_text_end_cb(void *priv)
  300. {
  301. // End of text, close any open style record
  302. mov_text_style_start((MovTextContext*)priv);
  303. }
  304. static void mov_text_dialog(MovTextContext *s, ASSDialog *dialog)
  305. {
  306. ASSStyle * style = ff_ass_style_get(s->ass_ctx, dialog->style);
  307. uint8_t style_flags;
  308. if (style) {
  309. style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
  310. (!!style->italic * STYLE_FLAG_ITALIC) |
  311. (!!style->underline * STYLE_FLAG_UNDERLINE);
  312. mov_text_style_set(s, style_flags);
  313. }
  314. }
  315. static uint16_t utf8_strlen(const char *text, int len)
  316. {
  317. uint16_t i = 0, ret = 0;
  318. while (i < len) {
  319. char c = text[i];
  320. if ((c & 0x80) == 0)
  321. i += 1;
  322. else if ((c & 0xE0) == 0xC0)
  323. i += 2;
  324. else if ((c & 0xF0) == 0xE0)
  325. i += 3;
  326. else if ((c & 0xF8) == 0xF0)
  327. i += 4;
  328. else
  329. return 0;
  330. ret++;
  331. }
  332. return ret;
  333. }
  334. static void mov_text_text_cb(void *priv, const char *text, int len)
  335. {
  336. uint16_t utf8_len = utf8_strlen(text, len);
  337. MovTextContext *s = priv;
  338. av_bprint_append_data(&s->buffer, text, len);
  339. // If it's not utf-8, just use the byte length
  340. s->text_pos += utf8_len ? utf8_len : len;
  341. s->byte_count += len;
  342. }
  343. static void mov_text_new_line_cb(void *priv, int forced)
  344. {
  345. MovTextContext *s = priv;
  346. av_bprint_append_data(&s->buffer, "\n", 1);
  347. s->text_pos += 1;
  348. s->byte_count += 1;
  349. }
  350. static const ASSCodesCallbacks mov_text_callbacks = {
  351. .text = mov_text_text_cb,
  352. .new_line = mov_text_new_line_cb,
  353. .style = mov_text_style_cb,
  354. .color = mov_text_color_cb,
  355. .end = mov_text_end_cb,
  356. };
  357. static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  358. int bufsize, const AVSubtitle *sub)
  359. {
  360. MovTextContext *s = avctx->priv_data;
  361. ASSDialog *dialog;
  362. int i, length;
  363. size_t j;
  364. s->byte_count = 0;
  365. s->text_pos = 0;
  366. s->count = 0;
  367. s->box_flags = 0;
  368. for (i = 0; i < sub->num_rects; i++) {
  369. const char *ass = sub->rects[i]->ass;
  370. if (sub->rects[i]->type != SUBTITLE_ASS) {
  371. av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
  372. return AVERROR(ENOSYS);
  373. }
  374. #if FF_API_ASS_TIMING
  375. if (!strncmp(ass, "Dialogue: ", 10)) {
  376. int num;
  377. dialog = ff_ass_split_dialog(s->ass_ctx, ass, 0, &num);
  378. for (; dialog && num--; dialog++) {
  379. mov_text_dialog(s, dialog);
  380. ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
  381. }
  382. } else {
  383. #endif
  384. dialog = ff_ass_split_dialog2(s->ass_ctx, ass);
  385. if (!dialog)
  386. return AVERROR(ENOMEM);
  387. mov_text_dialog(s, dialog);
  388. ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
  389. ff_ass_free_dialog(&dialog);
  390. #if FF_API_ASS_TIMING
  391. }
  392. #endif
  393. for (j = 0; j < box_count; j++) {
  394. box_types[j].encode(s, box_types[j].type);
  395. }
  396. }
  397. AV_WB16(buf, s->byte_count);
  398. buf += 2;
  399. if (!av_bprint_is_complete(&s->buffer)) {
  400. length = AVERROR(ENOMEM);
  401. goto exit;
  402. }
  403. if (!s->buffer.len) {
  404. length = 0;
  405. goto exit;
  406. }
  407. if (s->buffer.len > bufsize - 3) {
  408. av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
  409. length = AVERROR(EINVAL);
  410. goto exit;
  411. }
  412. memcpy(buf, s->buffer.str, s->buffer.len);
  413. length = s->buffer.len + 2;
  414. exit:
  415. av_bprint_clear(&s->buffer);
  416. return length;
  417. }
  418. static int mov_text_encode_close(AVCodecContext *avctx)
  419. {
  420. MovTextContext *s = avctx->priv_data;
  421. ff_ass_split_free(s->ass_ctx);
  422. av_bprint_finalize(&s->buffer, NULL);
  423. return 0;
  424. }
  425. AVCodec ff_movtext_encoder = {
  426. .name = "mov_text",
  427. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  428. .type = AVMEDIA_TYPE_SUBTITLE,
  429. .id = AV_CODEC_ID_MOV_TEXT,
  430. .priv_data_size = sizeof(MovTextContext),
  431. .init = mov_text_encode_init,
  432. .encode_sub = mov_text_encode_frame,
  433. .close = mov_text_encode_close,
  434. };