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.

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