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.

111 lines
3.3KB

  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. * WebVTT subtitle decoder
  23. * @see http://dev.w3.org/html5/webvtt/
  24. * @todo need to support extended markups and cue settings
  25. */
  26. #include "avcodec.h"
  27. #include "ass.h"
  28. #include "libavutil/bprint.h"
  29. static const struct {
  30. const char *from;
  31. const char *to;
  32. } webvtt_tag_replace[] = {
  33. {"<i>", "{\\i1}"}, {"</i>", "{\\i0}"},
  34. {"<b>", "{\\b1}"}, {"</b>", "{\\b0}"},
  35. {"<u>", "{\\u1}"}, {"</u>", "{\\u0}"},
  36. {"{", "\\{"}, {"}", "\\}"}, // escape to avoid ASS markup conflicts
  37. {"&gt;", ">"}, {"&lt;", "<"},
  38. {"&lrm;", ""}, {"&rlm;", ""}, // FIXME: properly honor bidi marks
  39. {"&amp;", "&"}, {"&nbsp;", "\\h"},
  40. };
  41. static int webvtt_event_to_ass(AVBPrint *buf, const char *p)
  42. {
  43. int i, again = 0, skip = 0;
  44. while (*p) {
  45. for (i = 0; i < FF_ARRAY_ELEMS(webvtt_tag_replace); i++) {
  46. const char *from = webvtt_tag_replace[i].from;
  47. const size_t len = strlen(from);
  48. if (!strncmp(p, from, len)) {
  49. av_bprintf(buf, "%s", webvtt_tag_replace[i].to);
  50. p += len;
  51. again = 1;
  52. break;
  53. }
  54. }
  55. if (!*p)
  56. break;
  57. if (again) {
  58. again = 0;
  59. skip = 0;
  60. continue;
  61. }
  62. if (*p == '<')
  63. skip = 1;
  64. else if (*p == '>')
  65. skip = 0;
  66. else if (p[0] == '\n' && p[1])
  67. av_bprintf(buf, "\\N");
  68. else if (!skip && *p != '\r')
  69. av_bprint_chars(buf, *p, 1);
  70. p++;
  71. }
  72. return 0;
  73. }
  74. static int webvtt_decode_frame(AVCodecContext *avctx,
  75. void *data, int *got_sub_ptr, AVPacket *avpkt)
  76. {
  77. int ret = 0;
  78. AVSubtitle *sub = data;
  79. const char *ptr = avpkt->data;
  80. FFASSDecoderContext *s = avctx->priv_data;
  81. AVBPrint buf;
  82. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  83. if (ptr && avpkt->size > 0 && !webvtt_event_to_ass(&buf, ptr))
  84. ret = ff_ass_add_rect(sub, buf.str, s->readorder++, 0, NULL, NULL);
  85. av_bprint_finalize(&buf, NULL);
  86. if (ret < 0)
  87. return ret;
  88. *got_sub_ptr = sub->num_rects > 0;
  89. return avpkt->size;
  90. }
  91. AVCodec ff_webvtt_decoder = {
  92. .name = "webvtt",
  93. .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
  94. .type = AVMEDIA_TYPE_SUBTITLE,
  95. .id = AV_CODEC_ID_WEBVTT,
  96. .decode = webvtt_decode_frame,
  97. .init = ff_ass_subtitle_header_default,
  98. .flush = ff_ass_decoder_flush,
  99. .priv_data_size = sizeof(FFASSDecoderContext),
  100. };