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.

101 lines
3.1KB

  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. };
  38. static int webvtt_event_to_ass(AVBPrint *buf, const char *p)
  39. {
  40. int i, skip = 0;
  41. while (*p) {
  42. for (i = 0; i < FF_ARRAY_ELEMS(webvtt_tag_replace); i++) {
  43. const char *from = webvtt_tag_replace[i].from;
  44. const size_t len = strlen(from);
  45. if (!strncmp(p, from, len)) {
  46. av_bprintf(buf, "%s", webvtt_tag_replace[i].to);
  47. p += len;
  48. break;
  49. }
  50. }
  51. if (!*p)
  52. break;
  53. if (*p == '<')
  54. skip = 1;
  55. else if (*p == '>')
  56. skip = 0;
  57. else if (p[0] == '\n' && p[1])
  58. av_bprintf(buf, "\\N");
  59. else if (!skip && *p != '\r')
  60. av_bprint_chars(buf, *p, 1);
  61. p++;
  62. }
  63. av_bprintf(buf, "\r\n");
  64. return 0;
  65. }
  66. static int webvtt_decode_frame(AVCodecContext *avctx,
  67. void *data, int *got_sub_ptr, AVPacket *avpkt)
  68. {
  69. AVSubtitle *sub = data;
  70. const char *ptr = avpkt->data;
  71. AVBPrint buf;
  72. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  73. if (ptr && avpkt->size > 0 && !webvtt_event_to_ass(&buf, ptr)) {
  74. int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
  75. int ts_duration = avpkt->duration != -1 ?
  76. av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
  77. ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
  78. }
  79. *got_sub_ptr = sub->num_rects > 0;
  80. av_bprint_finalize(&buf, NULL);
  81. return avpkt->size;
  82. }
  83. AVCodec ff_webvtt_decoder = {
  84. .name = "webvtt",
  85. .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
  86. .type = AVMEDIA_TYPE_SUBTITLE,
  87. .id = AV_CODEC_ID_WEBVTT,
  88. .decode = webvtt_decode_frame,
  89. .init = ff_ass_subtitle_header_default,
  90. };