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.

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