yading@11
|
1 /*
|
yading@11
|
2 * SSA/ASS muxer
|
yading@11
|
3 * Copyright (c) 2008 Michael Niedermayer
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include "avformat.h"
|
yading@11
|
23 #include "internal.h"
|
yading@11
|
24
|
yading@11
|
25 typedef struct ASSContext{
|
yading@11
|
26 unsigned int extra_index;
|
yading@11
|
27 int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like)
|
yading@11
|
28 }ASSContext;
|
yading@11
|
29
|
yading@11
|
30 static int write_header(AVFormatContext *s)
|
yading@11
|
31 {
|
yading@11
|
32 ASSContext *ass = s->priv_data;
|
yading@11
|
33 AVCodecContext *avctx= s->streams[0]->codec;
|
yading@11
|
34 uint8_t *last= NULL;
|
yading@11
|
35
|
yading@11
|
36 if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
|
yading@11
|
37 avctx->codec_id != AV_CODEC_ID_ASS)) {
|
yading@11
|
38 av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
|
yading@11
|
39 return -1;
|
yading@11
|
40 }
|
yading@11
|
41 ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
|
yading@11
|
42 avpriv_set_pts_info(s->streams[0], 64, 1, 100);
|
yading@11
|
43
|
yading@11
|
44 while(ass->extra_index < avctx->extradata_size){
|
yading@11
|
45 uint8_t *p = avctx->extradata + ass->extra_index;
|
yading@11
|
46 uint8_t *end= strchr(p, '\n');
|
yading@11
|
47 if(!end) end= avctx->extradata + avctx->extradata_size;
|
yading@11
|
48 else end++;
|
yading@11
|
49
|
yading@11
|
50 avio_write(s->pb, p, end-p);
|
yading@11
|
51 ass->extra_index += end-p;
|
yading@11
|
52
|
yading@11
|
53 if(last && !memcmp(last, "[Events]", 8))
|
yading@11
|
54 break;
|
yading@11
|
55 last=p;
|
yading@11
|
56 }
|
yading@11
|
57
|
yading@11
|
58 avio_flush(s->pb);
|
yading@11
|
59
|
yading@11
|
60 return 0;
|
yading@11
|
61 }
|
yading@11
|
62
|
yading@11
|
63 static int write_packet(AVFormatContext *s, AVPacket *pkt)
|
yading@11
|
64 {
|
yading@11
|
65 ASSContext *ass = s->priv_data;
|
yading@11
|
66
|
yading@11
|
67 if (ass->write_ts) {
|
yading@11
|
68 long int layer;
|
yading@11
|
69 char *p;
|
yading@11
|
70 int64_t start = pkt->pts;
|
yading@11
|
71 int64_t end = start + pkt->duration;
|
yading@11
|
72 int hh1, mm1, ss1, ms1;
|
yading@11
|
73 int hh2, mm2, ss2, ms2;
|
yading@11
|
74
|
yading@11
|
75 p = pkt->data + strcspn(pkt->data, ",") + 1; // skip ReadOrder
|
yading@11
|
76 layer = strtol(p, &p, 10);
|
yading@11
|
77 if (*p == ',')
|
yading@11
|
78 p++;
|
yading@11
|
79 hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
|
yading@11
|
80 hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
|
yading@11
|
81 ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
|
yading@11
|
82 ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
|
yading@11
|
83 if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
|
yading@11
|
84 if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
|
yading@11
|
85 avio_printf(s->pb, "Dialogue: %ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
|
yading@11
|
86 layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
|
yading@11
|
87 } else {
|
yading@11
|
88 avio_write(s->pb, pkt->data, pkt->size);
|
yading@11
|
89 }
|
yading@11
|
90 return 0;
|
yading@11
|
91 }
|
yading@11
|
92
|
yading@11
|
93 static int write_trailer(AVFormatContext *s)
|
yading@11
|
94 {
|
yading@11
|
95 ASSContext *ass = s->priv_data;
|
yading@11
|
96 AVCodecContext *avctx= s->streams[0]->codec;
|
yading@11
|
97
|
yading@11
|
98 avio_write(s->pb, avctx->extradata + ass->extra_index,
|
yading@11
|
99 avctx->extradata_size - ass->extra_index);
|
yading@11
|
100
|
yading@11
|
101 return 0;
|
yading@11
|
102 }
|
yading@11
|
103
|
yading@11
|
104 AVOutputFormat ff_ass_muxer = {
|
yading@11
|
105 .name = "ass",
|
yading@11
|
106 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
|
yading@11
|
107 .mime_type = "text/x-ssa",
|
yading@11
|
108 .extensions = "ass,ssa",
|
yading@11
|
109 .priv_data_size = sizeof(ASSContext),
|
yading@11
|
110 .subtitle_codec = AV_CODEC_ID_SSA,
|
yading@11
|
111 .write_header = write_header,
|
yading@11
|
112 .write_packet = write_packet,
|
yading@11
|
113 .write_trailer = write_trailer,
|
yading@11
|
114 .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT,
|
yading@11
|
115 };
|