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.

592 lines
17KB

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