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.

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