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.

107 lines
3.4KB

  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 avpkt->size ? AVERROR_INVALIDDATA : 0;
  64. /*
  65. * The first two bytes of the packet are the length of the text string
  66. * In complex cases, there are style descriptors appended to the string
  67. * so we can't just assume the packet size is the string size.
  68. */
  69. end = ptr + FFMAX(2 + AV_RB16(ptr), avpkt->size);
  70. ptr += 2;
  71. ts_start = av_rescale_q(avpkt->pts,
  72. avctx->time_base,
  73. (AVRational){1,100});
  74. ts_end = av_rescale_q(avpkt->pts + avpkt->duration,
  75. avctx->time_base,
  76. (AVRational){1,100});
  77. // Note that the spec recommends lines be no longer than 2048 characters.
  78. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  79. text_to_ass(&buf, ptr, end);
  80. if (!av_bprint_is_complete(&buf))
  81. return AVERROR(ENOMEM);
  82. ff_ass_add_rect(sub, buf.str, ts_start, ts_end-ts_start, 0);
  83. *got_sub_ptr = sub->num_rects > 0;
  84. av_bprint_finalize(&buf, NULL);
  85. return avpkt->size;
  86. }
  87. AVCodec ff_movtext_decoder = {
  88. .name = "mov_text",
  89. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  90. .type = AVMEDIA_TYPE_SUBTITLE,
  91. .id = AV_CODEC_ID_MOV_TEXT,
  92. .init = mov_text_init,
  93. .decode = mov_text_decode_frame,
  94. };