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.

211 lines
7.1KB

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