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.

621 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_start) {
  338. style_active = 1;
  339. if (style->bold ^ m->d.bold)
  340. av_bprintf(buf, "{\\b%d}", style->bold);
  341. if (style->italic ^ m->d.italic)
  342. av_bprintf(buf, "{\\i%d}", style->italic);
  343. if (style->underline ^ m->d.underline)
  344. av_bprintf(buf, "{\\u%d}", style->underline);
  345. if (style->fontsize != m->d.fontsize)
  346. av_bprintf(buf, "{\\fs%d}", style->fontsize);
  347. if (style->style_fontID != m->d.fontID)
  348. for (i = 0; i < m->ftab_entries; i++) {
  349. if (style->style_fontID == m->ftab[i].fontID)
  350. av_bprintf(buf, "{\\fn%s}", m->ftab[i].font);
  351. }
  352. if (m->d.color != style->color) {
  353. color = style->color;
  354. av_bprintf(buf, "{\\1c&H%X&}", RGB_TO_BGR(color));
  355. }
  356. if (m->d.alpha != style->alpha)
  357. av_bprintf(buf, "{\\1a&H%02X&}", 255 - style->alpha);
  358. }
  359. if (text_pos == style->style_end) {
  360. if (style_active) {
  361. av_bprintf(buf, "{\\r}");
  362. style_active = 0;
  363. color = m->d.color;
  364. }
  365. entry++;
  366. }
  367. }
  368. if (m->box_flags & HLIT_BOX) {
  369. if (text_pos == m->h.hlit_start) {
  370. /* If hclr box is present, set the secondary color to the color
  371. * specified. Otherwise, set primary color to white and secondary
  372. * color to black. These colors will come from TextSampleModifier
  373. * boxes in future and inverse video technique for highlight will
  374. * be implemented.
  375. */
  376. if (m->box_flags & HCLR_BOX) {
  377. av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
  378. m->c.hlit_color[1], m->c.hlit_color[0]);
  379. } else {
  380. av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
  381. }
  382. }
  383. if (text_pos == m->h.hlit_end) {
  384. if (m->box_flags & HCLR_BOX) {
  385. av_bprintf(buf, "{\\2c&H%X&}", RGB_TO_BGR(m->d.color));
  386. } else {
  387. av_bprintf(buf, "{\\1c&H%X&}{\\2c&H%X&}",
  388. RGB_TO_BGR(color), RGB_TO_BGR(m->d.color));
  389. }
  390. }
  391. }
  392. len = get_utf8_length_at(text, text_end);
  393. if (len < 1) {
  394. av_log(avctx, AV_LOG_ERROR, "invalid UTF-8 byte in subtitle\n");
  395. len = 1;
  396. }
  397. for (i = 0; i < len; i++) {
  398. switch (*text) {
  399. case '\r':
  400. break;
  401. case '\n':
  402. av_bprintf(buf, "\\N");
  403. break;
  404. default:
  405. av_bprint_chars(buf, *text, 1);
  406. break;
  407. }
  408. text++;
  409. }
  410. text_pos++;
  411. }
  412. return 0;
  413. }
  414. static int mov_text_init(AVCodecContext *avctx) {
  415. /*
  416. * TODO: Handle the default text style.
  417. * NB: Most players ignore styles completely, with the result that
  418. * it's very common to find files where the default style is broken
  419. * and respecting it results in a worse experience than ignoring it.
  420. */
  421. int ret;
  422. MovTextContext *m = avctx->priv_data;
  423. ret = mov_text_tx3g(avctx, m);
  424. if (ret == 0) {
  425. if (!m->frame_width || !m->frame_height) {
  426. m->frame_width = ASS_DEFAULT_PLAYRESX;
  427. m->frame_height = ASS_DEFAULT_PLAYRESY;
  428. }
  429. return ff_ass_subtitle_header_full(avctx,
  430. m->frame_width, m->frame_height,
  431. m->d.font, m->d.fontsize,
  432. (255U - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
  433. (255U - m->d.alpha) << 24 | RGB_TO_BGR(m->d.color),
  434. (255U - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
  435. (255U - m->d.back_alpha) << 24 | RGB_TO_BGR(m->d.back_color),
  436. m->d.bold, m->d.italic, m->d.underline,
  437. ASS_DEFAULT_BORDERSTYLE, m->d.alignment);
  438. } else
  439. return ff_ass_subtitle_header_default(avctx);
  440. }
  441. static int mov_text_decode_frame(AVCodecContext *avctx,
  442. void *data, int *got_sub_ptr, AVPacket *avpkt)
  443. {
  444. AVSubtitle *sub = data;
  445. MovTextContext *m = avctx->priv_data;
  446. int ret;
  447. AVBPrint buf;
  448. char *ptr = avpkt->data;
  449. char *end;
  450. int text_length, tsmb_type, ret_tsmb;
  451. uint64_t tsmb_size;
  452. const uint8_t *tsmb;
  453. size_t i;
  454. if (!ptr || avpkt->size < 2)
  455. return AVERROR_INVALIDDATA;
  456. /*
  457. * A packet of size two with value zero is an empty subtitle
  458. * used to mark the end of the previous non-empty subtitle.
  459. * We can just drop them here as we have duration information
  460. * already. If the value is non-zero, then it's technically a
  461. * bad packet.
  462. */
  463. if (avpkt->size == 2)
  464. return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
  465. /*
  466. * The first two bytes of the packet are the length of the text string
  467. * In complex cases, there are style descriptors appended to the string
  468. * so we can't just assume the packet size is the string size.
  469. */
  470. text_length = AV_RB16(ptr);
  471. end = ptr + FFMIN(2 + text_length, avpkt->size);
  472. ptr += 2;
  473. mov_text_cleanup(m);
  474. tsmb_size = 0;
  475. m->tracksize = 2 + text_length;
  476. m->style_entries = 0;
  477. m->box_flags = 0;
  478. // Note that the spec recommends lines be no longer than 2048 characters.
  479. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  480. if (text_length + 2 != avpkt->size) {
  481. while (m->tracksize + 8 <= avpkt->size) {
  482. // A box is a minimum of 8 bytes.
  483. tsmb = ptr + m->tracksize - 2;
  484. tsmb_size = AV_RB32(tsmb);
  485. tsmb += 4;
  486. tsmb_type = AV_RB32(tsmb);
  487. tsmb += 4;
  488. if (tsmb_size == 1) {
  489. if (m->tracksize + 16 > avpkt->size)
  490. break;
  491. tsmb_size = AV_RB64(tsmb);
  492. tsmb += 8;
  493. m->size_var = 16;
  494. } else
  495. m->size_var = 8;
  496. //size_var is equal to 8 or 16 depending on the size of box
  497. if (tsmb_size == 0) {
  498. av_log(avctx, AV_LOG_ERROR, "tsmb_size is 0\n");
  499. return AVERROR_INVALIDDATA;
  500. }
  501. if (tsmb_size > avpkt->size - m->tracksize)
  502. break;
  503. for (i = 0; i < box_count; i++) {
  504. if (tsmb_type == box_types[i].type) {
  505. if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
  506. break;
  507. ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
  508. if (ret_tsmb == -1)
  509. break;
  510. }
  511. }
  512. m->tracksize = m->tracksize + tsmb_size;
  513. }
  514. text_to_ass(&buf, ptr, end, avctx);
  515. mov_text_cleanup(m);
  516. } else
  517. text_to_ass(&buf, ptr, end, avctx);
  518. ret = ff_ass_add_rect(sub, buf.str, m->readorder++, 0, NULL, NULL);
  519. av_bprint_finalize(&buf, NULL);
  520. if (ret < 0)
  521. return ret;
  522. *got_sub_ptr = sub->num_rects > 0;
  523. return avpkt->size;
  524. }
  525. static int mov_text_decode_close(AVCodecContext *avctx)
  526. {
  527. MovTextContext *m = avctx->priv_data;
  528. mov_text_cleanup_ftab(m);
  529. mov_text_cleanup(m);
  530. return 0;
  531. }
  532. static void mov_text_flush(AVCodecContext *avctx)
  533. {
  534. MovTextContext *m = avctx->priv_data;
  535. if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
  536. m->readorder = 0;
  537. }
  538. #define OFFSET(x) offsetof(MovTextContext, x)
  539. #define FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
  540. static const AVOption options[] = {
  541. { "width", "Frame width, usually video width", OFFSET(frame_width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  542. { "height", "Frame height, usually video height", OFFSET(frame_height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  543. { NULL },
  544. };
  545. static const AVClass mov_text_decoder_class = {
  546. .class_name = "MOV text decoder",
  547. .item_name = av_default_item_name,
  548. .option = options,
  549. .version = LIBAVUTIL_VERSION_INT,
  550. };
  551. AVCodec ff_movtext_decoder = {
  552. .name = "mov_text",
  553. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  554. .type = AVMEDIA_TYPE_SUBTITLE,
  555. .id = AV_CODEC_ID_MOV_TEXT,
  556. .priv_data_size = sizeof(MovTextContext),
  557. .priv_class = &mov_text_decoder_class,
  558. .init = mov_text_init,
  559. .decode = mov_text_decode_frame,
  560. .close = mov_text_decode_close,
  561. .flush = mov_text_flush,
  562. };