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.

235 lines
7.5KB

  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. typedef struct {
  32. uint16_t style_start;
  33. uint16_t style_end;
  34. uint8_t style_flag;
  35. } StyleBox;
  36. static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
  37. StyleBox **s, int style_entries)
  38. {
  39. int i = 0;
  40. int text_pos = 0;
  41. while (text < text_end) {
  42. for (i = 0; i < style_entries; i++) {
  43. if (s[i]->style_flag && text_pos == s[i]->style_end) {
  44. if (s[i]->style_flag & STYLE_FLAG_BOLD)
  45. av_bprintf(buf, "{\\b0}");
  46. if (s[i]->style_flag & STYLE_FLAG_ITALIC)
  47. av_bprintf(buf, "{\\i0}");
  48. if (s[i]->style_flag & STYLE_FLAG_UNDERLINE)
  49. av_bprintf(buf, "{\\u0}");
  50. }
  51. }
  52. for (i = 0; i < style_entries; i++) {
  53. if (s[i]->style_flag && text_pos == s[i]->style_start) {
  54. if (s[i]->style_flag & STYLE_FLAG_BOLD)
  55. av_bprintf(buf, "{\\b1}");
  56. if (s[i]->style_flag & STYLE_FLAG_ITALIC)
  57. av_bprintf(buf, "{\\i1}");
  58. if (s[i]->style_flag & STYLE_FLAG_UNDERLINE)
  59. av_bprintf(buf, "{\\u1}");
  60. }
  61. }
  62. switch (*text) {
  63. case '\r':
  64. break;
  65. case '\n':
  66. av_bprintf(buf, "\\N");
  67. break;
  68. default:
  69. av_bprint_chars(buf, *text, 1);
  70. break;
  71. }
  72. text++;
  73. text_pos++;
  74. }
  75. return 0;
  76. }
  77. static int mov_text_init(AVCodecContext *avctx) {
  78. /*
  79. * TODO: Handle the default text style.
  80. * NB: Most players ignore styles completely, with the result that
  81. * it's very common to find files where the default style is broken
  82. * and respecting it results in a worse experience than ignoring it.
  83. */
  84. return ff_ass_subtitle_header_default(avctx);
  85. }
  86. static int mov_text_decode_frame(AVCodecContext *avctx,
  87. void *data, int *got_sub_ptr, AVPacket *avpkt)
  88. {
  89. AVSubtitle *sub = data;
  90. int ret, ts_start, ts_end;
  91. AVBPrint buf;
  92. char *ptr = avpkt->data;
  93. char *end;
  94. //char *ptr_temp;
  95. int text_length, tsmb_type, style_entries;
  96. uint64_t tsmb_size, tracksize;
  97. StyleBox **s = {0, };
  98. StyleBox *s_temp;
  99. const uint8_t *tsmb;
  100. int count, i, size_var;
  101. if (!ptr || avpkt->size < 2)
  102. return AVERROR_INVALIDDATA;
  103. /*
  104. * A packet of size two with value zero is an empty subtitle
  105. * used to mark the end of the previous non-empty subtitle.
  106. * We can just drop them here as we have duration information
  107. * already. If the value is non-zero, then it's technically a
  108. * bad packet.
  109. */
  110. if (avpkt->size == 2)
  111. return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
  112. /*
  113. * The first two bytes of the packet are the length of the text string
  114. * In complex cases, there are style descriptors appended to the string
  115. * so we can't just assume the packet size is the string size.
  116. */
  117. text_length = AV_RB16(ptr);
  118. end = ptr + FFMIN(2 + text_length, avpkt->size);
  119. ptr += 2;
  120. ts_start = av_rescale_q(avpkt->pts,
  121. avctx->time_base,
  122. (AVRational){1,100});
  123. ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
  124. avctx->time_base,
  125. (AVRational){1,100});
  126. tsmb_size = 0;
  127. tracksize = 2 + text_length;
  128. // Note that the spec recommends lines be no longer than 2048 characters.
  129. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  130. if (text_length + 2 != avpkt->size) {
  131. while (tracksize + 8 <= avpkt->size) {
  132. // A box is a minimum of 8 bytes.
  133. tsmb = ptr + tracksize - 2;
  134. tsmb_size = AV_RB32(tsmb);
  135. tsmb += 4;
  136. tsmb_type = AV_RB32(tsmb);
  137. tsmb += 4;
  138. if (tsmb_size == 1) {
  139. if (tracksize + 16 > avpkt->size)
  140. break;
  141. tsmb_size = AV_RB64(tsmb);
  142. tsmb += 8;
  143. size_var = 18;
  144. } else
  145. size_var = 10;
  146. //size_var is equal to 10 or 18 depending on the size of box
  147. if (tracksize + tsmb_size > avpkt->size)
  148. break;
  149. if (tsmb_type == MKBETAG('s','t','y','l')) {
  150. if (tracksize + size_var > avpkt->size)
  151. break;
  152. style_entries = AV_RB16(tsmb);
  153. tsmb += 2;
  154. // A single style record is of length 12 bytes.
  155. if (tracksize + size_var + style_entries * 12 > avpkt->size)
  156. break;
  157. count = 0;
  158. for(i = 0; i < style_entries; i++) {
  159. s_temp = av_malloc(sizeof(*s_temp));
  160. if (!s_temp)
  161. goto error;
  162. s_temp->style_start = AV_RB16(tsmb);
  163. tsmb += 2;
  164. s_temp->style_end = AV_RB16(tsmb);
  165. tsmb += 2;
  166. // fontID = AV_RB16(tsmb);
  167. tsmb += 2;
  168. s_temp->style_flag = AV_RB8(tsmb);
  169. av_dynarray_add(&s, &count, s_temp);
  170. if(!s)
  171. goto error;
  172. //fontsize=AV_RB8(tsmb);
  173. tsmb += 2;
  174. // text-color-rgba
  175. tsmb += 4;
  176. }
  177. text_to_ass(&buf, ptr, end, s, style_entries);
  178. for(i = 0; i < count; i++) {
  179. av_freep(&s[i]);
  180. }
  181. av_freep(&s);
  182. }
  183. tracksize = tracksize + tsmb_size;
  184. }
  185. } else
  186. text_to_ass(&buf, ptr, end, NULL, 0);
  187. ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
  188. av_bprint_finalize(&buf, NULL);
  189. if (ret < 0)
  190. return ret;
  191. *got_sub_ptr = sub->num_rects > 0;
  192. return avpkt->size;
  193. error:
  194. for(i = 0; i < count; i++) {
  195. av_freep(&s[i]);
  196. }
  197. av_freep(&s);
  198. if (s_temp)
  199. av_freep(&s_temp);
  200. av_bprint_finalize(&buf, NULL);
  201. return AVERROR(ENOMEM);
  202. }
  203. AVCodec ff_movtext_decoder = {
  204. .name = "mov_text",
  205. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  206. .type = AVMEDIA_TYPE_SUBTITLE,
  207. .id = AV_CODEC_ID_MOV_TEXT,
  208. .init = mov_text_init,
  209. .decode = mov_text_decode_frame,
  210. };