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.

165 lines
5.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. * 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_decode_frame(AVCodecContext *avctx, void *data,
  40. int *got_sub_ptr, AVPacket *avpkt)
  41. {
  42. int ret = 0;
  43. AVBPrint buf;
  44. AVSubtitle *sub = data;
  45. const char *ptr = avpkt->data;
  46. const TextContext *text = avctx->priv_data;
  47. const int ts_start = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
  48. const int ts_duration = avpkt->duration != -1 ?
  49. av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
  50. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  51. if (ptr && avpkt->size > 0 && *ptr) {
  52. ff_ass_bprint_text_event(&buf, ptr, avpkt->size, text->linebreaks, text->keep_ass_markup);
  53. ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_duration);
  54. }
  55. av_bprint_finalize(&buf, NULL);
  56. if (ret < 0)
  57. return ret;
  58. *got_sub_ptr = sub->num_rects > 0;
  59. return avpkt->size;
  60. }
  61. #define DECLARE_CLASS(decname) static const AVClass decname ## _decoder_class = { \
  62. .class_name = #decname " decoder", \
  63. .item_name = av_default_item_name, \
  64. .option = decname ## _options, \
  65. .version = LIBAVUTIL_VERSION_INT, \
  66. }
  67. #if CONFIG_TEXT_DECODER
  68. #define text_options options
  69. DECLARE_CLASS(text);
  70. AVCodec ff_text_decoder = {
  71. .name = "text",
  72. .long_name = NULL_IF_CONFIG_SMALL("Raw text subtitle"),
  73. .priv_data_size = sizeof(TextContext),
  74. .type = AVMEDIA_TYPE_SUBTITLE,
  75. .id = AV_CODEC_ID_TEXT,
  76. .decode = text_decode_frame,
  77. .init = ff_ass_subtitle_header_default,
  78. .priv_class = &text_decoder_class,
  79. };
  80. #endif
  81. #if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_SUBVIEWER1_DECODER || CONFIG_STL_DECODER
  82. static int linebreak_init(AVCodecContext *avctx)
  83. {
  84. TextContext *text = avctx->priv_data;
  85. text->linebreaks = "|";
  86. return ff_ass_subtitle_header_default(avctx);
  87. }
  88. #if CONFIG_VPLAYER_DECODER
  89. #define vplayer_options options
  90. DECLARE_CLASS(vplayer);
  91. AVCodec ff_vplayer_decoder = {
  92. .name = "vplayer",
  93. .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
  94. .priv_data_size = sizeof(TextContext),
  95. .type = AVMEDIA_TYPE_SUBTITLE,
  96. .id = AV_CODEC_ID_VPLAYER,
  97. .decode = text_decode_frame,
  98. .init = linebreak_init,
  99. .priv_class = &vplayer_decoder_class,
  100. };
  101. #endif
  102. #if CONFIG_STL_DECODER
  103. #define stl_options options
  104. DECLARE_CLASS(stl);
  105. AVCodec ff_stl_decoder = {
  106. .name = "stl",
  107. .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
  108. .priv_data_size = sizeof(TextContext),
  109. .type = AVMEDIA_TYPE_SUBTITLE,
  110. .id = AV_CODEC_ID_STL,
  111. .decode = text_decode_frame,
  112. .init = linebreak_init,
  113. .priv_class = &stl_decoder_class,
  114. };
  115. #endif
  116. #if CONFIG_PJS_DECODER
  117. #define pjs_options options
  118. DECLARE_CLASS(pjs);
  119. AVCodec ff_pjs_decoder = {
  120. .name = "pjs",
  121. .long_name = NULL_IF_CONFIG_SMALL("PJS subtitle"),
  122. .priv_data_size = sizeof(TextContext),
  123. .type = AVMEDIA_TYPE_SUBTITLE,
  124. .id = AV_CODEC_ID_PJS,
  125. .decode = text_decode_frame,
  126. .init = linebreak_init,
  127. .priv_class = &pjs_decoder_class,
  128. };
  129. #endif
  130. #if CONFIG_SUBVIEWER1_DECODER
  131. #define subviewer1_options options
  132. DECLARE_CLASS(subviewer1);
  133. AVCodec ff_subviewer1_decoder = {
  134. .name = "subviewer1",
  135. .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"),
  136. .priv_data_size = sizeof(TextContext),
  137. .type = AVMEDIA_TYPE_SUBTITLE,
  138. .id = AV_CODEC_ID_SUBVIEWER1,
  139. .decode = text_decode_frame,
  140. .init = linebreak_init,
  141. .priv_class = &subviewer1_decoder_class,
  142. };
  143. #endif
  144. #endif /* text subtitles with '|' line break */