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.

150 lines
4.7KB

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