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.

85 lines
2.6KB

  1. /*
  2. * SSA/ASS decoder
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.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 <string.h>
  22. #include "avcodec.h"
  23. #include "ass.h"
  24. #include "libavutil/internal.h"
  25. #include "libavutil/mem.h"
  26. static av_cold int ass_decode_init(AVCodecContext *avctx)
  27. {
  28. avctx->subtitle_header = av_malloc(avctx->extradata_size + 1);
  29. if (!avctx->subtitle_header)
  30. return AVERROR(ENOMEM);
  31. if (avctx->extradata_size)
  32. memcpy(avctx->subtitle_header, avctx->extradata, avctx->extradata_size);
  33. avctx->subtitle_header[avctx->extradata_size] = 0;
  34. avctx->subtitle_header_size = avctx->extradata_size;
  35. return 0;
  36. }
  37. static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
  38. AVPacket *avpkt)
  39. {
  40. AVSubtitle *sub = data;
  41. if (avpkt->size <= 0)
  42. return avpkt->size;
  43. sub->rects = av_malloc(sizeof(*sub->rects));
  44. if (!sub->rects)
  45. return AVERROR(ENOMEM);
  46. sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
  47. if (!sub->rects[0])
  48. return AVERROR(ENOMEM);
  49. sub->num_rects = 1;
  50. sub->rects[0]->type = SUBTITLE_ASS;
  51. sub->rects[0]->ass = av_strdup(avpkt->data);
  52. if (!sub->rects[0]->ass)
  53. return AVERROR(ENOMEM);
  54. *got_sub_ptr = 1;
  55. return avpkt->size;
  56. }
  57. #if CONFIG_SSA_DECODER
  58. AVCodec ff_ssa_decoder = {
  59. .name = "ssa",
  60. .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
  61. .type = AVMEDIA_TYPE_SUBTITLE,
  62. .id = AV_CODEC_ID_ASS,
  63. .init = ass_decode_init,
  64. .decode = ass_decode_frame,
  65. };
  66. #endif
  67. #if CONFIG_ASS_DECODER
  68. AVCodec ff_ass_decoder = {
  69. .name = "ass",
  70. .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
  71. .type = AVMEDIA_TYPE_SUBTITLE,
  72. .id = AV_CODEC_ID_ASS,
  73. .init = ass_decode_init,
  74. .decode = ass_decode_frame,
  75. };
  76. #endif