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.

533 lines
15KB

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