assenc.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. put_buffer(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. put_flush_packet(s->pb);
  46. return 0;
  47. }
  48. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  49. {
  50. put_buffer(s->pb, pkt->data, pkt->size);
  51. put_flush_packet(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. put_buffer(s->pb, avctx->extradata + ass->extra_index,
  59. avctx->extradata_size - ass->extra_index);
  60. put_flush_packet(s->pb);
  61. return 0;
  62. }
  63. AVOutputFormat ass_muxer = {
  64. "ass",
  65. NULL_IF_CONFIG_SMALL("SSA/ASS format"),
  66. NULL,
  67. "ass,ssa",
  68. sizeof(ASSContext),
  69. CODEC_ID_NONE,
  70. CODEC_ID_NONE,
  71. write_header,
  72. write_packet,
  73. write_trailer,
  74. .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS
  75. };