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.

189 lines
6.2KB

  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. const 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. { "keep_ass_markup", "Set if ASS tags must be escaped", OFFSET(keep_ass_markup), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags=SD },
  37. { NULL }
  38. };
  39. static int text_event_to_ass(const AVCodecContext *avctx, AVBPrint *buf,
  40. const char *p, const char *p_end)
  41. {
  42. const TextContext *text = avctx->priv_data;
  43. for (; p < p_end && *p; p++) {
  44. /* forced custom line breaks, not accounted as "normal" EOL */
  45. if (text->linebreaks && strchr(text->linebreaks, *p)) {
  46. av_bprintf(buf, "\\N");
  47. /* standard ASS escaping so random characters don't get mis-interpreted
  48. * as ASS */
  49. } else if (!text->keep_ass_markup && strchr("{}\\", *p)) {
  50. av_bprintf(buf, "\\%c", *p);
  51. /* some packets might end abruptly (no \0 at the end, like for example
  52. * in some cases of demuxing from a classic video container), some
  53. * might be terminated with \n or \r\n which we have to remove (for
  54. * consistency with those who haven't), and we also have to deal with
  55. * evil cases such as \r at the end of the buffer (and no \0 terminated
  56. * character) */
  57. } else if (p[0] == '\n') {
  58. /* some stuff left so we can insert a line break */
  59. if (p < p_end - 1)
  60. av_bprintf(buf, "\\N");
  61. } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
  62. /* \r followed by a \n, we can skip it. We don't insert the \N yet
  63. * because we don't know if it is followed by more text */
  64. continue;
  65. /* finally, a sane character */
  66. } else {
  67. av_bprint_chars(buf, *p, 1);
  68. }
  69. }
  70. av_bprintf(buf, "\r\n");
  71. return 0;
  72. }
  73. static int text_decode_frame(AVCodecContext *avctx, void *data,
  74. int *got_sub_ptr, AVPacket *avpkt)
  75. {
  76. AVBPrint buf;
  77. AVSubtitle *sub = data;
  78. const char *ptr = avpkt->data;
  79. const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
  80. const int ts_duration = avpkt->duration != -1 ?
  81. av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
  82. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  83. if (ptr && avpkt->size > 0 && *ptr &&
  84. !text_event_to_ass(avctx, &buf, ptr, ptr + avpkt->size)) {
  85. if (!av_bprint_is_complete(&buf)) {
  86. av_bprint_finalize(&buf, NULL);
  87. return AVERROR(ENOMEM);
  88. }
  89. ff_ass_add_rect(sub, buf.str, ts_start, ts_duration, 0);
  90. }
  91. *got_sub_ptr = sub->num_rects > 0;
  92. av_bprint_finalize(&buf, NULL);
  93. return avpkt->size;
  94. }
  95. #define DECLARE_CLASS(decname) static const AVClass decname ## _decoder_class = { \
  96. .class_name = #decname " decoder", \
  97. .item_name = av_default_item_name, \
  98. .option = decname ## _options, \
  99. .version = LIBAVUTIL_VERSION_INT, \
  100. }
  101. #if CONFIG_TEXT_DECODER
  102. #define text_options options
  103. DECLARE_CLASS(text);
  104. AVCodec ff_text_decoder = {
  105. .name = "text",
  106. .priv_data_size = sizeof(TextContext),
  107. .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
  108. .type = AVMEDIA_TYPE_SUBTITLE,
  109. .id = AV_CODEC_ID_TEXT,
  110. .decode = text_decode_frame,
  111. .init = ff_ass_subtitle_header_default,
  112. .priv_class = &text_decoder_class,
  113. };
  114. #endif
  115. #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER
  116. static int linebreak_init(AVCodecContext *avctx)
  117. {
  118. TextContext *text = avctx->priv_data;
  119. text->linebreaks = "|";
  120. return ff_ass_subtitle_header_default(avctx);
  121. }
  122. #if CONFIG_VPLAYER_DECODER
  123. #define vplayer_options options
  124. DECLARE_CLASS(vplayer);
  125. AVCodec ff_vplayer_decoder = {
  126. .name = "vplayer",
  127. .priv_data_size = sizeof(TextContext),
  128. .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
  129. .type = AVMEDIA_TYPE_SUBTITLE,
  130. .id = AV_CODEC_ID_VPLAYER,
  131. .decode = text_decode_frame,
  132. .init = linebreak_init,
  133. .priv_class = &vplayer_decoder_class,
  134. };
  135. #endif
  136. #if CONFIG_PJS_DECODER
  137. #define pjs_options options
  138. DECLARE_CLASS(pjs);
  139. AVCodec ff_pjs_decoder = {
  140. .name = "pjs",
  141. .priv_data_size = sizeof(TextContext),
  142. .long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"),
  143. .type = AVMEDIA_TYPE_SUBTITLE,
  144. .id = AV_CODEC_ID_PJS,
  145. .decode = text_decode_frame,
  146. .init = linebreak_init,
  147. .priv_class = &pjs_decoder_class,
  148. };
  149. #endif
  150. #if CONFIG_SUBVIEWER1_DECODER
  151. #define subviewer1_options options
  152. DECLARE_CLASS(subviewer1);
  153. AVCodec ff_subviewer1_decoder = {
  154. .name = "subviewer1",
  155. .priv_data_size = sizeof(TextContext),
  156. .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
  157. .type = AVMEDIA_TYPE_SUBTITLE,
  158. .id = AV_CODEC_ID_SUBVIEWER1,
  159. .decode = text_decode_frame,
  160. .init = linebreak_init,
  161. .priv_class = &subviewer1_decoder_class,
  162. };
  163. #endif
  164. #endif /* text subtitles with '|' line break */