yading@11: /* yading@11: * SSA/ASS muxer yading@11: * Copyright (c) 2008 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: yading@11: typedef struct ASSContext{ yading@11: unsigned int extra_index; yading@11: int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like) yading@11: }ASSContext; yading@11: yading@11: static int write_header(AVFormatContext *s) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: AVCodecContext *avctx= s->streams[0]->codec; yading@11: uint8_t *last= NULL; yading@11: yading@11: if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA && yading@11: avctx->codec_id != AV_CODEC_ID_ASS)) { yading@11: av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n"); yading@11: return -1; yading@11: } yading@11: ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS; yading@11: avpriv_set_pts_info(s->streams[0], 64, 1, 100); yading@11: yading@11: while(ass->extra_index < avctx->extradata_size){ yading@11: uint8_t *p = avctx->extradata + ass->extra_index; yading@11: uint8_t *end= strchr(p, '\n'); yading@11: if(!end) end= avctx->extradata + avctx->extradata_size; yading@11: else end++; yading@11: yading@11: avio_write(s->pb, p, end-p); yading@11: ass->extra_index += end-p; yading@11: yading@11: if(last && !memcmp(last, "[Events]", 8)) yading@11: break; yading@11: last=p; yading@11: } yading@11: yading@11: avio_flush(s->pb); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int write_packet(AVFormatContext *s, AVPacket *pkt) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: yading@11: if (ass->write_ts) { yading@11: long int layer; yading@11: char *p; yading@11: int64_t start = pkt->pts; yading@11: int64_t end = start + pkt->duration; yading@11: int hh1, mm1, ss1, ms1; yading@11: int hh2, mm2, ss2, ms2; yading@11: yading@11: p = pkt->data + strcspn(pkt->data, ",") + 1; // skip ReadOrder yading@11: layer = strtol(p, &p, 10); yading@11: if (*p == ',') yading@11: p++; yading@11: hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60; yading@11: hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60; yading@11: ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100); yading@11: ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100); yading@11: if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99; yading@11: if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99; yading@11: avio_printf(s->pb, "Dialogue: %ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n", yading@11: layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p); yading@11: } else { yading@11: avio_write(s->pb, pkt->data, pkt->size); yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: static int write_trailer(AVFormatContext *s) yading@11: { yading@11: ASSContext *ass = s->priv_data; yading@11: AVCodecContext *avctx= s->streams[0]->codec; yading@11: yading@11: avio_write(s->pb, avctx->extradata + ass->extra_index, yading@11: avctx->extradata_size - ass->extra_index); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: AVOutputFormat ff_ass_muxer = { yading@11: .name = "ass", yading@11: .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"), yading@11: .mime_type = "text/x-ssa", yading@11: .extensions = "ass,ssa", yading@11: .priv_data_size = sizeof(ASSContext), yading@11: .subtitle_codec = AV_CODEC_ID_SSA, yading@11: .write_header = write_header, yading@11: .write_packet = write_packet, yading@11: .write_trailer = write_trailer, yading@11: .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT, yading@11: };