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.

126 lines
4.5KB

  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. * Raw subtitles decoder
  23. */
  24. #include "avcodec.h"
  25. #include "ass.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/opt.h"
  28. typedef struct {
  29. AVClass *class;
  30. char *linebreaks;
  31. int keep_ass_markup;
  32. } TextContext;
  33. #define OFFSET(x) offsetof(TextContext, x)
  34. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  35. static const AVOption options[] = {
  36. { "linebreaks", "Extra line breaks characters", OFFSET(linebreaks), AV_OPT_TYPE_STRING, {.str=NULL}, .flags=SD },
  37. { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags=SD },
  38. { NULL }
  39. };
  40. static const AVClass text_decoder_class = {
  41. .class_name = "text decoder",
  42. .item_name = av_default_item_name,
  43. .option = options,
  44. .version = LIBAVUTIL_VERSION_INT,
  45. };
  46. static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
  47. const char *p, const char *p_end)
  48. {
  49. const TextContext *text = avctx->priv_data;
  50. for (; p < p_end && *p; p++) {
  51. /* forced custom line breaks, not accounted as "normal" EOL */
  52. if (text->linebreaks && strchr(text->linebreaks, *p)) {
  53. av_bprintf(buf, "\\N");
  54. /* standard ASS escaping so random characters don't get mis-interpreted
  55. * as ASS */
  56. } else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
  57. av_bprintf(buf, "\\%c", *p);
  58. /* some packets might end abruptly (no \0 at the end, like for example
  59. * in some cases of demuxing from a classic video container), some
  60. * might be terminated with \n or \r\n which we have to remove (for
  61. * consistency with those who haven't), and we also have to deal with
  62. * evil cases such as \r at the end of the buffer (and no \0 terminated
  63. * character) */
  64. } else if (p[0] == '\n') {
  65. /* some stuff left so we can insert a line break */
  66. if (p < p_end - 1)
  67. av_bprintf(buf, "\\N");
  68. } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
  69. /* \r followed by a \n, we can skip it. We don't insert the \N yet
  70. * because we don't know if it is followed by more text */
  71. continue;
  72. /* finally, a sane character */
  73. } else {
  74. av_bprint_chars(buf, *p, 1);
  75. }
  76. }
  77. av_bprintf(buf, "\r\n");
  78. return 0;
  79. }
  80. static int text_decode_frame(AVCodecContext *avctx, void *data,
  81. int *got_sub_ptr, AVPacket *avpkt)
  82. {
  83. AVBPrint buf;
  84. AVSubtitle *sub = data;
  85. const char *ptr = avpkt->data;
  86. const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
  87. const int ts_duration = avpkt->duration != -1 ?
  88. av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
  89. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  90. if (ptr && avpkt->size > 0 && *ptr &&
  91. !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
  92. if (!av_bprint_is_complete(&buf)) {
  93. av_bprint_finalize(&buf, NULL);
  94. return AVERROR(ENOMEM);
  95. }
  96. ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
  97. }
  98. *got_sub_ptr = sub->num_rects > 0;
  99. av_bprint_finalize(&buf, NULL);
  100. return avpkt->size;
  101. }
  102. AVCodec ff_text_decoder = {
  103. .name = "text",
  104. .priv_data_size = sizeof(TextContext),
  105. .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
  106. .type = AVMEDIA_TYPE_SUBTITLE,
  107. .id = AV_CODEC_ID_TEXT,
  108. .decode = text_decode_frame,
  109. .init = ff_ass_subtitle_header_default,
  110. .priv_class = &text_decoder_class,
  111. };