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.

199 lines
6.7KB

  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;
  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. // Note that the spec recommends lines be no longer than 2048 characters.
  126. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  127. if (text_length + 2 != avpkt->size) {
  128. while (text_length + 2 + tsmb_size < avpkt->size) {
  129. tsmb = ptr + text_length + tsmb_size;
  130. tsmb_size = AV_RB32(tsmb);
  131. tsmb += 4;
  132. tsmb_type = AV_RB32(tsmb);
  133. tsmb += 4;
  134. if (tsmb_type == MKBETAG('s','t','y','l')) {
  135. style_entries = AV_RB16(tsmb);
  136. tsmb += 2;
  137. for(i = 0; i < style_entries; i++) {
  138. style_pos = av_malloc(4);
  139. *style_pos = AV_RB16(tsmb);
  140. index = i;
  141. av_dynarray_add(&style_start, &index, style_pos);
  142. tsmb += 2;
  143. style_pos = av_malloc(4);
  144. *style_pos = AV_RB16(tsmb);
  145. index = i;
  146. av_dynarray_add(&style_end, &index, style_pos);
  147. tsmb += 2;
  148. // fontID = AV_RB16(tsmb);
  149. tsmb += 2;
  150. flag = av_malloc(4);
  151. *flag = AV_RB8(tsmb);
  152. index = i;
  153. av_dynarray_add(&style_flags, &index, flag);
  154. //fontsize=AV_RB8(tsmb);
  155. tsmb += 2;
  156. // text-color-rgba
  157. tsmb += 4;
  158. }
  159. text_to_ass(&buf, ptr, end, style_start, style_end, style_flags, style_entries);
  160. av_freep(&style_start);
  161. av_freep(&style_end);
  162. av_freep(&style_flags);
  163. }
  164. }
  165. } else
  166. text_to_ass(&buf, ptr, end, NULL, NULL, 0, 0);
  167. ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
  168. av_bprint_finalize(&buf, NULL);
  169. if (ret < 0)
  170. return ret;
  171. *got_sub_ptr = sub->num_rects > 0;
  172. return avpkt->size;
  173. }
  174. AVCodec ff_movtext_decoder = {
  175. .name = "mov_text",
  176. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  177. .type = AVMEDIA_TYPE_SUBTITLE,
  178. .id = AV_CODEC_ID_MOV_TEXT,
  179. .init = mov_text_init,
  180. .decode = mov_text_decode_frame,
  181. };