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.

206 lines
6.7KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * JACOsub subtitle decoder
  23. * @see http://unicorn.us.com/jacosub/jscripts.html
  24. */
  25. #include <time.h>
  26. #include "ass.h"
  27. #include "jacosub.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #undef time
  31. static int insert_text(AVBPrint *dst, const char *in, const char *arg)
  32. {
  33. av_bprintf(dst, "%s", arg);
  34. return 0;
  35. }
  36. static int insert_datetime(AVBPrint *dst, const char *in, const char *arg)
  37. {
  38. char buf[16] = {0};
  39. time_t now = time(0);
  40. struct tm ltime;
  41. #if HAVE_LOCALTIME_R
  42. localtime_r(&now, &ltime);
  43. #else
  44. ltime = *localtime(&now);
  45. #endif
  46. strftime(buf, sizeof(buf), arg, &ltime);
  47. av_bprintf(dst, "%s", buf);
  48. return 0;
  49. }
  50. static int insert_color(AVBPrint *dst, const char *in, const char *arg)
  51. {
  52. return 1; // skip id
  53. }
  54. static int insert_font(AVBPrint *dst, const char *in, const char *arg)
  55. {
  56. return 1; // skip id
  57. }
  58. static const struct {
  59. const char *from;
  60. const char *arg;
  61. int (*func)(AVBPrint *dst, const char *in, const char *arg);
  62. } ass_codes_map[] = {
  63. {"\\~", "~", insert_text}, // tilde doesn't need escaping
  64. {"~", "{\\h}", insert_text}, // hard space
  65. {"\\n", "\\N", insert_text}, // newline
  66. {"\\D", "%d %b %Y", insert_datetime}, // current date
  67. {"\\T", "%H:%M", insert_datetime}, // current time
  68. {"\\N", "{\\r}", insert_text}, // reset to default style
  69. {"\\I", "{\\i1}", insert_text}, // italic on
  70. {"\\i", "{\\i0}", insert_text}, // italic off
  71. {"\\B", "{\\b1}", insert_text}, // bold on
  72. {"\\b", "{\\b0}", insert_text}, // bold off
  73. {"\\U", "{\\u1}", insert_text}, // underline on
  74. {"\\u", "{\\u0}", insert_text}, // underline off
  75. {"\\C", "", insert_color}, // TODO: color
  76. {"\\F", "", insert_font}, // TODO: font
  77. };
  78. enum {
  79. ALIGN_VB = 1<<0, // vertical bottom, default
  80. ALIGN_VM = 1<<1, // vertical middle
  81. ALIGN_VT = 1<<2, // vertical top
  82. ALIGN_JC = 1<<3, // justify center, default
  83. ALIGN_JL = 1<<4, // justify left
  84. ALIGN_JR = 1<<5, // justify right
  85. };
  86. static void jacosub_to_ass(AVCodecContext *avctx, AVBPrint *dst, const char *src)
  87. {
  88. int i, valign = 0, halign = 0;
  89. char c = av_toupper(*src);
  90. char directives[128] = {0};
  91. /* extract the optional directives */
  92. if ((c >= 'A' && c <= 'Z') || c == '[') {
  93. char *p = directives;
  94. char *pend = directives + sizeof(directives) - 1;
  95. do *p++ = av_toupper(*src++);
  96. while (*src && !jss_whitespace(*src) && p < pend);
  97. *p = 0;
  98. src = jss_skip_whitespace(src);
  99. }
  100. /* handle directives (TODO: handle more of them, and more reliably) */
  101. if (strstr(directives, "VB")) valign = ALIGN_VB;
  102. else if (strstr(directives, "VM")) valign = ALIGN_VM;
  103. else if (strstr(directives, "VT")) valign = ALIGN_VT;
  104. if (strstr(directives, "JC")) halign = ALIGN_JC;
  105. else if (strstr(directives, "JL")) halign = ALIGN_JL;
  106. else if (strstr(directives, "JR")) halign = ALIGN_JR;
  107. if (valign || halign) {
  108. if (!valign) valign = ALIGN_VB;
  109. if (!halign) halign = ALIGN_JC;
  110. switch (valign | halign) {
  111. case ALIGN_VB | ALIGN_JL: av_bprintf(dst, "{\\an1}"); break; // bottom left
  112. case ALIGN_VB | ALIGN_JC: av_bprintf(dst, "{\\an2}"); break; // bottom center
  113. case ALIGN_VB | ALIGN_JR: av_bprintf(dst, "{\\an3}"); break; // bottom right
  114. case ALIGN_VM | ALIGN_JL: av_bprintf(dst, "{\\an4}"); break; // middle left
  115. case ALIGN_VM | ALIGN_JC: av_bprintf(dst, "{\\an5}"); break; // middle center
  116. case ALIGN_VM | ALIGN_JR: av_bprintf(dst, "{\\an6}"); break; // middle right
  117. case ALIGN_VT | ALIGN_JL: av_bprintf(dst, "{\\an7}"); break; // top left
  118. case ALIGN_VT | ALIGN_JC: av_bprintf(dst, "{\\an8}"); break; // top center
  119. case ALIGN_VT | ALIGN_JR: av_bprintf(dst, "{\\an9}"); break; // top right
  120. }
  121. }
  122. /* process timed line */
  123. while (*src && *src != '\n') {
  124. /* text continue on the next line */
  125. if (src[0] == '\\' && src[1] == '\n') {
  126. src += 2;
  127. while (jss_whitespace(*src))
  128. src++;
  129. continue;
  130. }
  131. /* special character codes */
  132. for (i = 0; i < FF_ARRAY_ELEMS(ass_codes_map); i++) {
  133. const char *from = ass_codes_map[i].from;
  134. const char *arg = ass_codes_map[i].arg;
  135. size_t codemap_len = strlen(from);
  136. if (!strncmp(src, from, codemap_len)) {
  137. src += codemap_len;
  138. src += ass_codes_map[i].func(dst, src, arg);
  139. break;
  140. }
  141. }
  142. /* simple char copy */
  143. if (i == FF_ARRAY_ELEMS(ass_codes_map))
  144. av_bprintf(dst, "%c", *src++);
  145. }
  146. av_bprintf(dst, "\r\n");
  147. }
  148. static int jacosub_decode_frame(AVCodecContext *avctx,
  149. void *data, int *got_sub_ptr, AVPacket *avpkt)
  150. {
  151. AVSubtitle *sub = data;
  152. const char *ptr = avpkt->data;
  153. if (avpkt->size <= 0)
  154. goto end;
  155. if (*ptr) {
  156. AVBPrint buffer;
  157. char *dec_sub;
  158. // skip timers
  159. ptr = jss_skip_whitespace(ptr);
  160. ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
  161. ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
  162. av_bprint_init(&buffer, JSS_MAX_LINESIZE, JSS_MAX_LINESIZE);
  163. jacosub_to_ass(avctx, &buffer, ptr);
  164. av_bprint_finalize(&buffer, &dec_sub);
  165. ff_ass_add_rect(sub, dec_sub, avpkt->pts, avpkt->duration, 0);
  166. av_free(dec_sub);
  167. }
  168. end:
  169. *got_sub_ptr = sub->num_rects > 0;
  170. return avpkt->size;
  171. }
  172. AVCodec ff_jacosub_decoder = {
  173. .name = "jacosub",
  174. .long_name = NULL_IF_CONFIG_SMALL("JACOsub subtitle"),
  175. .type = AVMEDIA_TYPE_SUBTITLE,
  176. .id = AV_CODEC_ID_JACOSUB,
  177. .init = ff_ass_subtitle_header_default,
  178. .decode = jacosub_decode_frame,
  179. };