assenc.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SSA/ASS muxer
  3. * Copyright (c) 2008 Michael Niedermayer
  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 "avformat.h"
  22. typedef struct ASSContext{
  23. unsigned int extra_index;
  24. }ASSContext;
  25. static int write_header(AVFormatContext *s)
  26. {
  27. ASSContext *ass = s->priv_data;
  28. AVCodecContext *avctx= s->streams[0]->codec;
  29. uint8_t *last= NULL;
  30. if(s->nb_streams != 1 || avctx->codec_id != CODEC_ID_SSA){
  31. av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
  32. return -1;
  33. }
  34. while(ass->extra_index < avctx->extradata_size){
  35. uint8_t *p = avctx->extradata + ass->extra_index;
  36. uint8_t *end= strchr(p, '\n');
  37. if(!end) end= avctx->extradata + avctx->extradata_size;
  38. else end++;
  39. avio_write(s->pb, p, end-p);
  40. ass->extra_index += end-p;
  41. if(last && !memcmp(last, "[Events]", 8))
  42. break;
  43. last=p;
  44. }
  45. avio_flush(s->pb);
  46. return 0;
  47. }
  48. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  49. {
  50. avio_write(s->pb, pkt->data, pkt->size);
  51. avio_flush(s->pb);
  52. return 0;
  53. }
  54. static int write_trailer(AVFormatContext *s)
  55. {
  56. ASSContext *ass = s->priv_data;
  57. AVCodecContext *avctx= s->streams[0]->codec;
  58. avio_write(s->pb, avctx->extradata + ass->extra_index,
  59. avctx->extradata_size - ass->extra_index);
  60. avio_flush(s->pb);
  61. return 0;
  62. }
  63. AVOutputFormat ff_ass_muxer = {
  64. .name = "ass",
  65. .long_name = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle format"),
  66. .mime_type = "text/x-ssa",
  67. .extensions = "ass,ssa",
  68. .priv_data_size = sizeof(ASSContext),
  69. .subtitle_codec = CODEC_ID_SSA,
  70. .write_header = write_header,
  71. .write_packet = write_packet,
  72. .write_trailer = write_trailer,
  73. .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS,
  74. };