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.

622 lines
19KB

  1. /*
  2. * 3GPP TS 26.245 Timed Text decoder
  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 "avcodec.h"
  22. #include "ass.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mem.h"
  29. #define STYLE_FLAG_BOLD (1<<0)
  30. #define STYLE_FLAG_ITALIC (1<<1)
  31. #define STYLE_FLAG_UNDERLINE (1<<2)
  32. #define BOX_SIZE_INITIAL 40
  33. #define STYL_BOX (1<<0)
  34. #define HLIT_BOX (1<<1)
  35. #define HCLR_BOX (1<<2)
  36. #define TWRP_BOX (1<<3)
  37. #define BOTTOM_LEFT 1
  38. #define BOTTOM_CENTER 2
  39. #define BOTTOM_RIGHT 3
  40. #define MIDDLE_LEFT 4
  41. #define MIDDLE_CENTER 5
  42. #define MIDDLE_RIGHT 6
  43. #define TOP_LEFT 7
  44. #define TOP_CENTER 8
  45. #define TOP_RIGHT 9
  46. #define RGB_TO_BGR(c) (((c) & 0xff) << 16 | ((c) & 0xff00) | (((c) >> 16) & 0xff))
  47. typedef struct {
  48. uint16_t fontID;
  49. const char *font;
  50. uint8_t fontsize;
  51. int color;
  52. uint8_t alpha;
  53. int back_color;
  54. uint8_t back_alpha;
  55. uint8_t bold;
  56. uint8_t italic;
  57. uint8_t underline;
  58. int alignment;
  59. } MovTextDefault;
  60. typedef struct {
  61. uint16_t fontID;
  62. char *font;
  63. } FontRecord;
  64. typedef struct {
  65. uint16_t style_start;
  66. uint16_t style_end;
  67. uint8_t style_flag;
  68. uint8_t bold;
  69. uint8_t italic;
  70. uint8_t underline;
  71. int color;
  72. uint8_t alpha;
  73. uint8_t fontsize;
  74. uint16_t style_fontID;
  75. } StyleBox;
  76. typedef struct {
  77. uint16_t hlit_start;
  78. uint16_t hlit_end;
  79. } HighlightBox;
  80. typedef struct {
  81. uint8_t hlit_color[4];
  82. } HilightcolorBox;
  83. typedef struct {
  84. uint8_t wrap_flag;
  85. } TextWrapBox;
  86. typedef struct {
  87. AVClass *class;
  88. StyleBox *s;
  89. HighlightBox h;
  90. HilightcolorBox c;
  91. FontRecord *ftab;
  92. TextWrapBox w;
  93. MovTextDefault d;
  94. uint8_t box_flags;
  95. uint16_t style_entries, ftab_entries;
  96. uint64_t tracksize;
  97. int size_var;
  98. int readorder;
  99. int frame_width;
  100. int frame_height;
  101. } MovTextContext;
  102. typedef struct {
  103. uint32_t type;
  104. size_t base_size;
  105. int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
  106. } Box;
  107. static void mov_text_cleanup(MovTextContext *m)
  108. {
  109. if (m->box_flags & STYL_BOX) {
  110. av_freep(&m->s);
  111. m->style_entries = 0;
  112. }
  113. }
  114. static void mov_text_cleanup_ftab(MovTextContext *m)
  115. {
  116. for (unsigned i = 0; i < m->ftab_entries; i++)
  117. av_freep(&m->ftab[i].font);
  118. av_freep(&m->ftab);
  119. m->ftab_entries = 0;
  120. }
  121. static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
  122. {
  123. uint8_t *tx3g_ptr = avctx->extradata;
  124. int i, j = -1, font_length, remaining = avctx->extradata_size - BOX_SIZE_INITIAL;
  125. int8_t v_align, h_align;
  126. unsigned ftab_entries;
  127. StyleBox s_default;
  128. m->ftab_entries = 0;
  129. if (remaining < 0)
  130. return -1;
  131. // Display Flags
  132. tx3g_ptr += 4;
  133. // Alignment
  134. h_align = *tx3g_ptr++;
  135. v_align = *tx3g_ptr++;
  136. if (h_align == 0) {
  137. if (v_align == 0)
  138. m->d.alignment = TOP_LEFT;
  139. if (v_align == 1)
  140. m->d.alignment = MIDDLE_LEFT;
  141. if (v_align == -1)
  142. m->d.alignment = BOTTOM_LEFT;
  143. }
  144. if (h_align == 1) {
  145. if (v_align == 0)
  146. m->d.alignment = TOP_CENTER;
  147. if (v_align == 1)
  148. m->d.alignment = MIDDLE_CENTER;
  149. if (v_align == -1)
  150. m->d.alignment = BOTTOM_CENTER;
  151. }
  152. if (h_align == -1) {
  153. if (v_align == 0)
  154. m->d.alignment = TOP_RIGHT;
  155. if (v_align == 1)
  156. m->d.alignment = MIDDLE_RIGHT;
  157. if (v_align == -1)
  158. m->d.alignment = BOTTOM_RIGHT;
  159. }
  160. // Background Color
  161. m->d.back_color = AV_RB24(tx3g_ptr);
  162. tx3g_ptr += 3;
  163. m->d.back_alpha = AV_RB8(tx3g_ptr);
  164. tx3g_ptr += 1;
  165. // BoxRecord
  166. tx3g_ptr += 8;
  167. // StyleRecord
  168. tx3g_ptr += 4;
  169. // fontID
  170. m->d.fontID = AV_RB16(tx3g_ptr);
  171. tx3g_ptr += 2;
  172. // face-style-flags
  173. s_default.style_flag = *tx3g_ptr++;
  174. m->d.bold = !!(s_default.style_flag & STYLE_FLAG_BOLD);
  175. m->d.italic = !!(s_default.style_flag & STYLE_FLAG_ITALIC);
  176. m->d.underline = !!(s_default.style_flag & STYLE_FLAG_UNDERLINE);
  177. // fontsize
  178. m->d.fontsize = *tx3g_ptr++;
  179. // Primary color
  180. m->d.color = AV_RB24(tx3g_ptr);
  181. tx3g_ptr += 3;
  182. m->d.alpha = AV_RB8(tx3g_ptr);
  183. tx3g_ptr += 1;
  184. // FontRecord
  185. // FontRecord Size
  186. tx3g_ptr += 4;
  187. // ftab
  188. tx3g_ptr += 4;
  189. // In case of broken header, init default font
  190. m->d.font = ASS_DEFAULT_FONT;
  191. ftab_entries = AV_RB16(tx3g_ptr);
  192. if (!ftab_entries)
  193. return 0;
  194. remaining -= 3 * ftab_entries;
  195. if (remaining < 0)
  196. return AVERROR_INVALIDDATA;
  197. m->ftab = av_calloc(ftab_entries, sizeof(*m->ftab));
  198. if (!m->ftab)
  199. return AVERROR(ENOMEM);
  200. m->ftab_entries = ftab_entries;
  201. tx3g_ptr += 2;
  202. for (i = 0; i < m->ftab_entries; i++) {
  203. m->ftab[i].fontID = AV_RB16(tx3g_ptr);
  204. if (m->ftab[i].fontID == m->d.fontID)
  205. j = i;
  206. tx3g_ptr += 2;
  207. font_length = *tx3g_ptr++;
  208. remaining -= font_length;
  209. if (remaining < 0) {
  210. mov_text_cleanup_ftab(m);
  211. return -1;
  212. }
  213. m->ftab[i].font = av_malloc(font_length + 1);
  214. if (!m->ftab[i].font) {
  215. mov_text_cleanup_ftab(m);
  216. return AVERROR(ENOMEM);
  217. }
  218. memcpy(m->ftab[i].font, tx3g_ptr, font_length);
  219. m->ftab[i].font[font_length] = '\0';
  220. tx3g_ptr = tx3g_ptr + font_length;
  221. }
  222. if (j >= 0)
  223. m->d.font = m->ftab[j].font;
  224. return 0;
  225. }
  226. static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  227. {
  228. m->box_flags |= TWRP_BOX;
  229. m->w.wrap_flag = *tsmb++;
  230. return 0;
  231. }
  232. static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  233. {
  234. m->box_flags |= HLIT_BOX;
  235. m->h.hlit_start = AV_RB16(tsmb);
  236. tsmb += 2;
  237. m->h.hlit_end = AV_RB16(tsmb);
  238. tsmb += 2;
  239. return 0;
  240. }
  241. static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  242. {
  243. m->box_flags |= HCLR_BOX;
  244. memcpy(m->c.hlit_color, tsmb, 4);
  245. tsmb += 4;
  246. return 0;
  247. }
  248. static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  249. {
  250. int i;
  251. int style_entries = AV_RB16(tsmb);
  252. StyleBox *tmp;
  253. tsmb += 2;
  254. // A single style record is of length 12 bytes.
  255. if (m->tracksize + m->size_var + 2 + style_entries * 12 > avpkt->size)
  256. return -1;
  257. tmp = av_realloc_array(m->s, style_entries, sizeof(*m->s));
  258. if (!tmp)
  259. return AVERROR(ENOMEM);
  260. m->s = tmp;
  261. m->style_entries = style_entries;
  262. m->box_flags |= STYL_BOX;
  263. for(i = 0; i < m->style_entries; i++) {
  264. StyleBox *style = &m->s[i];
  265. style->style_start = AV_RB16(tsmb);
  266. tsmb += 2;
  267. style->style_end = AV_RB16(tsmb);
  268. if ( style->style_end < style->style_start
  269. || (i && style->style_start < m->s[i - 1].style_end)) {
  270. mov_text_cleanup(m);
  271. return AVERROR(ENOMEM);
  272. }
  273. tsmb += 2;
  274. if (style->style_start == style->style_end) {
  275. /* Skip this style as it applies to no character */
  276. tsmb += 8;
  277. m->style_entries--;
  278. i--;
  279. continue;
  280. }
  281. style->style_fontID = AV_RB16(tsmb);
  282. tsmb += 2;
  283. style->style_flag = AV_RB8(tsmb);
  284. style->bold = !!(style->style_flag & STYLE_FLAG_BOLD);
  285. style->italic = !!(style->style_flag & STYLE_FLAG_ITALIC);
  286. style->underline = !!(style->style_flag & STYLE_FLAG_UNDERLINE);
  287. tsmb++;
  288. style->fontsize = AV_RB8(tsmb);
  289. tsmb++;
  290. style->color = AV_RB24(tsmb);
  291. tsmb += 3;
  292. style->alpha = AV_RB8(tsmb);
  293. tsmb++;
  294. }
  295. return 0;
  296. }
  297. static const Box box_types[] = {
  298. { MKBETAG('s','t','y','l'), 2, decode_styl },
  299. { MKBETAG('h','l','i','t'), 4, decode_hlit },
  300. { MKBETAG('h','c','l','r'), 4, decode_hclr },
  301. { MKBETAG('t','w','r','p'), 1, decode_twrp }
  302. };
  303. const static size_t box_count = FF_ARRAY_ELEMS(box_types);
  304. // Return byte length of the UTF-8 sequence starting at text[0]. 0 on error.
  305. static int get_utf8_length_at(const char *text, const char *text_end)
  306. {
  307. const char *start = text;
  308. int err = 0;
  309. uint32_t c;
  310. GET_UTF8(c, text < text_end ? (uint8_t)*text++ : (err = 1, 0), goto error;);
  311. if (err)
  312. goto error;
  313. return text - start;
  314. error:
  315. return 0;
  316. }
  317. static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
  318. AVCodecContext *avctx)
  319. {
  320. MovTextContext *m = avctx->priv_data;
  321. int i = 0;
  322. int text_pos = 0;
  323. int style_active = 0;
  324. int entry = 0;
  325. int color = m->d.color;
  326. if (text < text_end && m->box_flags & TWRP_BOX) {
  327. if (m->w.wrap_flag == 1) {
  328. av_bprintf(buf, "{\\q1}"); /* End of line wrap */
  329. } else {
  330. av_bprintf(buf, "{\\q2}"); /* No wrap */
  331. }
  332. }
  333. while (text < text_end) {
  334. int len;
  335. if ((m->box_flags & STYL_BOX) && entry < m->style_entries) {
  336. const StyleBox *style = &m->s[entry];
  337. if (text_pos == style->style_end) {
  338. if (style_active) {
  339. av_bprintf(buf, "{\\r}");
  340. style_active = 0;
  341. color = m->d.color;
  342. }
  343. entry++;
  344. style++;
  345. }
  346. if (entry < m->style_entries && text_pos == style->style_start) {
  347. style_active = 1;
  348. if (style->bold ^ m->d.bold)
  349. av_bprintf(buf, "{\\b%d}", style->bold);
  350. if (style->italic ^ m->d.italic)
  351. av_bprintf(buf, "{\\i%d}", style->italic);
  352. if (style->underline ^ m->d.underline)
  353. av_bprintf(buf, "{\\u%d}", style->underline);
  354. if (style->fontsize != m->d.fontsize)
  355. av_bprintf(buf, "{\\fs%d}", style->fontsize);
  356. if (style->style_fontID != m->d.fontID)
  357. for (i = 0; i < m->ftab_entries; i++) {
  358. if (style->style_fontID == m->ftab[i].fontID)
  359. av_bprintf(buf, "{\\fn%s}", m->ftab[i].font);
  360. }
  361. if (m->d.color != style->color) {
  362. color = style->color;
  363. av_bprintf(buf, "{\\1c&H%X&}", RGB_TO_BGR(color));
  364. }
  365. if (m->d.alpha != style->alpha)
  366. av_bprintf(buf, "{\\1a&H%02X&}", 255 - style->alpha);
  367. }
  368. }
  369. if (m->box_flags & HLIT_BOX) {
  370. if (text_pos == m->h.hlit_start) {
  371. /* If hclr box is present, set the secondary color to the color
  372. * specified. Otherwise, set primary color to white and secondary
  373. * color to black. These colors will come from TextSampleModifier
  374. * boxes in future and inverse video technique for highlight will
  375. * be implemented.
  376. */
  377. if (m->box_flags & HCLR_BOX) {
  378. av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
  379. m->c.hlit_color[1], m->c.hlit_color[0]);
  380. } else {
  381. av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
  382. }
  383. }
  384. if (text_pos == m->h.hlit_end) {
  385. if (m->box_flags & HCLR_BOX) {
  386. av_bprintf(buf, "{\\2c&H%X&}", RGB_TO_BGR(m->d.color));
  387. } else {
  388. av_bprintf(buf, "{\\1c&H%X&}{\\2c&H%X&}",
  389. RGB_TO_BGR(color), RGB_TO_BGR(m->d.color));
  390. }
  391. }
  392. }
  393. len = get_utf8_length_at(text, text_end);
  394. if (len < 1) {
  395. av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n");
  396. len = 1;
  397. }
  398. for (i = 0; i < len; i++) {
  399. switch (*text) {
  400. case '\r':
  401. break;
  402. case '\n':
  403. av_bprintf(buf, "\\N");
  404. break;
  405. default:
  406. av_bprint_chars(buf, *text, 1);
  407. break;
  408. }
  409. text++;
  410. }
  411. text_pos++;
  412. }
  413. return 0;
  414. }
  415. static int mov_text_init(AVCodecContext *avctx) {
  416. /*
  417. * TODO: Handle the default text style.
  418. * NB: Most players ignore styles completely, with the result that
  419. * it's very common to find files where the default style is broken
  420. * and respecting it results in a worse experience than ignoring it.
  421. */
  422. int ret;
  423. MovTextContext *m = avctx->priv_data;
  424. ret = mov_text_tx3g(avctx, m);
  425. if (ret == 0) {
  426. if (!m->frame_width || !m->frame_height) {
  427. m->frame_width = ASS_DEFAULT_PLAYRESX;
  428. m->frame_height = ASS_DEFAULT_PLAYRESY;
  429. }
  430. return ff_ass_subtitle_header_full(avctx,
  431. m->frame_width, m->frame_height,
  432. m->d.font, m->d.fontsize,
  433. (255U - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
  434. (255U - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
  435. (255U - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
  436. (255U - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
  437. m->d.bold, m->d.italic, m->d.underline,
  438. ASS_DEFAULT_BORDERSTYLE, m->d.alignment);
  439. } else
  440. return ff_ass_subtitle_header_default(avctx);
  441. }
  442. static int mov_text_decode_frame(AVCodecContext *avctx,
  443. void *data, int *got_sub_ptr, AVPacket *avpkt)
  444. {
  445. AVSubtitle *sub = data;
  446. MovTextContext *m = avctx->priv_data;
  447. int ret;
  448. AVBPrint buf;
  449. char *ptr = avpkt->data;
  450. char *end;
  451. int text_length, tsmb_type, ret_tsmb;
  452. uint64_t tsmb_size;
  453. const uint8_t *tsmb;
  454. size_t i;
  455. if (!ptr || avpkt->size < 2)
  456. return AVERROR_INVALIDDATA;
  457. /*
  458. * A packet of size two with value zero is an empty subtitle
  459. * used to mark the end of the previous non-empty subtitle.
  460. * We can just drop them here as we have duration information
  461. * already. If the value is non-zero, then it's technically a
  462. * bad packet.
  463. */
  464. if (avpkt->size == 2)
  465. return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
  466. /*
  467. * The first two bytes of the packet are the length of the text string
  468. * In complex cases, there are style descriptors appended to the string
  469. * so we can't just assume the packet size is the string size.
  470. */
  471. text_length = AV_RB16(ptr);
  472. end = ptr + FFMIN(2 + text_length, avpkt->size);
  473. ptr += 2;
  474. mov_text_cleanup(m);
  475. tsmb_size = 0;
  476. m->tracksize = 2 + text_length;
  477. m->style_entries = 0;
  478. m->box_flags = 0;
  479. // Note that the spec recommends lines be no longer than 2048 characters.
  480. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  481. if (text_length + 2 != avpkt->size) {
  482. while (m->tracksize + 8 <= avpkt->size) {
  483. // A box is a minimum of 8 bytes.
  484. tsmb = ptr + m->tracksize - 2;
  485. tsmb_size = AV_RB32(tsmb);
  486. tsmb += 4;
  487. tsmb_type = AV_RB32(tsmb);
  488. tsmb += 4;
  489. if (tsmb_size == 1) {
  490. if (m->tracksize + 16 > avpkt->size)
  491. break;
  492. tsmb_size = AV_RB64(tsmb);
  493. tsmb += 8;
  494. m->size_var = 16;
  495. } else
  496. m->size_var = 8;
  497. //size_var is equal to 8 or 16 depending on the size of box
  498. if (tsmb_size == 0) {
  499. av_log(avctx, AV_LOG_ERROR, "tsmb_size is 0\n");
  500. return AVERROR_INVALIDDATA;
  501. }
  502. if (tsmb_size > avpkt->size - m->tracksize)
  503. break;
  504. for (i = 0; i < box_count; i++) {
  505. if (tsmb_type == box_types[i].type) {
  506. if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
  507. break;
  508. ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
  509. if (ret_tsmb == -1)
  510. break;
  511. }
  512. }
  513. m->tracksize = m->tracksize + tsmb_size;
  514. }
  515. text_to_ass(&buf, ptr, end, avctx);
  516. mov_text_cleanup(m);
  517. } else
  518. text_to_ass(&buf, ptr, end, avctx);
  519. ret = ff_ass_add_rect(sub, buf.str, m->readorder++, 0, NULL, NULL);
  520. av_bprint_finalize(&buf, NULL);
  521. if (ret < 0)
  522. return ret;
  523. *got_sub_ptr = sub->num_rects > 0;
  524. return avpkt->size;
  525. }
  526. static int mov_text_decode_close(AVCodecContext *avctx)
  527. {
  528. MovTextContext *m = avctx->priv_data;
  529. mov_text_cleanup_ftab(m);
  530. mov_text_cleanup(m);
  531. return 0;
  532. }
  533. static void mov_text_flush(AVCodecContext *avctx)
  534. {
  535. MovTextContext *m = avctx->priv_data;
  536. if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
  537. m->readorder = 0;
  538. }
  539. #define OFFSET(x) offsetof(MovTextContext, x)
  540. #define FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  541. static const AVOption options[] = {
  542. { "width", "Frame width, usually video width", OFFSET(frame_width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  543. { "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  544. { NULL },
  545. };
  546. static const AVClass mov_text_decoder_class = {
  547. .class_name = "MOV text decoder",
  548. .item_name = av_default_item_name,
  549. .option = options,
  550. .version = LIBAVUTIL_VERSION_INT,
  551. };
  552. AVCodec ff_movtext_decoder = {
  553. .name = "mov_text",
  554. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  555. .type = AVMEDIA_TYPE_SUBTITLE,
  556. .id = AV_CODEC_ID_MOV_TEXT,
  557. .priv_data_size = sizeof(MovTextContext),
  558. .priv_class = &mov_text_decoder_class,
  559. .init = mov_text_init,
  560. .decode = mov_text_decode_frame,
  561. .close = mov_text_decode_close,
  562. .flush = mov_text_flush,
  563. };