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.

117 lines
3.7KB

  1. /*
  2. * 3GPP TS 26.245 Timed Text decoder
  3. * Copyright (c) 2012 Philip Langdale <philipl@overt.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "ass.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/intreadwrite.h"
  27. static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end)
  28. {
  29. while (text < text_end) {
  30. switch (*text) {
  31. case '\r':
  32. break;
  33. case '\n':
  34. av_bprintf(buf, "\\N");
  35. break;
  36. default:
  37. av_bprint_chars(buf, *text, 1);
  38. break;
  39. }
  40. text++;
  41. }
  42. av_bprintf(buf, "\r\n");
  43. return 0;
  44. }
  45. static int mov_text_init(AVCodecContext *avctx) {
  46. /*
  47. * TODO: Handle the default text style.
  48. * NB: Most players ignore styles completely, with the result that
  49. * it's very common to find files where the default style is broken
  50. * and respecting it results in a worse experience than ignoring it.
  51. */
  52. return ff_ass_subtitle_header_default(avctx);
  53. }
  54. static int mov_text_decode_frame(AVCodecContext *avctx,
  55. void *data, int *got_sub_ptr, AVPacket *avpkt)
  56. {
  57. AVSubtitle *sub = data;
  58. int ts_start, ts_end;
  59. AVBPrint buf;
  60. const char *ptr = avpkt->data;
  61. const char *end;
  62. if (!ptr || avpkt->size < 2)
  63. return AVERROR_INVALIDDATA;
  64. /*
  65. * A packet of size two with value zero is an empty subtitle
  66. * used to mark the end of the previous non-empty subtitle.
  67. * We can just drop them here as we have duration information
  68. * already. If the value is non-zero, then it's technically a
  69. * bad packet.
  70. */
  71. if (avpkt->size == 2)
  72. return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
  73. /*
  74. * The first two bytes of the packet are the length of the text string
  75. * In complex cases, there are style descriptors appended to the string
  76. * so we can't just assume the packet size is the string size.
  77. */
  78. end = ptr + FFMAX(2 + AV_RB16(ptr), avpkt->size);
  79. ptr += 2;
  80. ts_start = av_rescale_q(avpkt->pts,
  81. avctx->time_base,
  82. (AVRational){1,100});
  83. ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
  84. avctx->time_base,
  85. (AVRational){1,100});
  86. // Note that the spec recommends lines be no longer than 2048 characters.
  87. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  88. text_to_ass(&buf, ptr, end);
  89. if (!av_bprint_is_complete(&buf))
  90. return AVERROR(ENOMEM);
  91. ff_ass_add_rect(sub, buf.str, ts_start, ts_end-ts_start, 0);
  92. *got_sub_ptr = sub->num_rects > 0;
  93. av_bprint_finalize(&buf, NULL);
  94. return avpkt->size;
  95. }
  96. AVCodec ff_movtext_decoder = {
  97. .name = "mov_text",
  98. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  99. .type = AVMEDIA_TYPE_SUBTITLE,
  100. .id = AV_CODEC_ID_MOV_TEXT,
  101. .init = mov_text_init,
  102. .decode = mov_text_decode_frame,
  103. };