]> git.sesse.net Git - ffmpeg/blob - libavformat/assenc.c
lavf/webm_dash: Fix incorrect bandwidth computation
[ffmpeg] / libavformat / assenc.c
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
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 typedef struct DialogueLine {
27     int readorder;
28     char *line;
29     struct DialogueLine *prev, *next;
30 } DialogueLine;
31
32 typedef struct ASSContext{
33     int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like)
34     int expected_readorder;
35     DialogueLine *dialogue_cache;
36     DialogueLine *last_added_dialogue;
37     int cache_size;
38 }ASSContext;
39
40 static int write_header(AVFormatContext *s)
41 {
42     ASSContext *ass = s->priv_data;
43     AVCodecContext *avctx= s->streams[0]->codec;
44
45     if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
46                                avctx->codec_id != AV_CODEC_ID_ASS)) {
47         av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
48         return AVERROR(EINVAL);
49     }
50     ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
51     avpriv_set_pts_info(s->streams[0], 64, 1, 100);
52     if (avctx->extradata_size > 0) {
53         avio_write(s->pb, avctx->extradata, avctx->extradata_size);
54         if (avctx->extradata[avctx->extradata_size - 1] != '\n')
55             avio_write(s->pb, "\r\n", 2);
56     }
57     if (!strstr(avctx->extradata, "\n[Events]"))
58         avio_printf(s->pb, "[Events]\r\nFormat: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text\r\n");
59     avio_flush(s->pb);
60
61     return 0;
62 }
63
64 static void purge_dialogues(AVFormatContext *s, int force)
65 {
66     int n = 0;
67     ASSContext *ass = s->priv_data;
68     DialogueLine *dialogue = ass->dialogue_cache;
69
70     while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
71         DialogueLine *next = dialogue->next;
72         if (dialogue->readorder != ass->expected_readorder) {
73             av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
74                    ass->expected_readorder, dialogue->readorder);
75             ass->expected_readorder = dialogue->readorder;
76         }
77         avio_printf(s->pb, "Dialogue: %s\r\n", dialogue->line);
78         if (dialogue == ass->last_added_dialogue)
79             ass->last_added_dialogue = next;
80         av_free(dialogue->line);
81         av_free(dialogue);
82         if (next)
83             next->prev = NULL;
84         dialogue = ass->dialogue_cache = next;
85         ass->expected_readorder++;
86         n++;
87     }
88     ass->cache_size -= n;
89     if (n > 1)
90         av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
91                n, ass->cache_size, ass->expected_readorder);
92 }
93
94 static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
95 {
96     DialogueLine *cur, *next = NULL, *prev = NULL;
97
98     /* from the last added to the end of the list */
99     if (ass->last_added_dialogue) {
100         for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
101             if (cur->readorder > dialogue->readorder)
102                 break;
103             prev = cur;
104             next = cur->next;
105         }
106     }
107
108     /* from the beginning to the last one added */
109     if (!prev) {
110         next = ass->dialogue_cache;
111         for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
112             if (cur->readorder > dialogue->readorder)
113                 break;
114             prev = cur;
115             next = cur->next;
116         }
117     }
118
119     if (prev) {
120         prev->next = dialogue;
121         dialogue->prev = prev;
122     } else {
123         dialogue->prev = ass->dialogue_cache;
124         ass->dialogue_cache = dialogue;
125     }
126     if (next) {
127         next->prev = dialogue;
128         dialogue->next = next;
129     }
130     ass->cache_size++;
131     ass->last_added_dialogue = dialogue;
132 }
133
134 static int write_packet(AVFormatContext *s, AVPacket *pkt)
135 {
136     ASSContext *ass = s->priv_data;
137
138     if (ass->write_ts) {
139         long int layer;
140         char *p = pkt->data;
141         int64_t start = pkt->pts;
142         int64_t end   = start + pkt->duration;
143         int hh1, mm1, ss1, ms1;
144         int hh2, mm2, ss2, ms2;
145         DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
146
147         if (!dialogue)
148             return AVERROR(ENOMEM);
149
150         dialogue->readorder = strtol(p, &p, 10);
151         if (dialogue->readorder < ass->expected_readorder)
152             av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
153                    dialogue->readorder);
154         if (*p == ',')
155             p++;
156
157         layer = strtol(p, &p, 10);
158         if (*p == ',')
159             p++;
160         hh1 = (int)(start / 360000);    mm1 = (int)(start / 6000) % 60;
161         hh2 = (int)(end   / 360000);    mm2 = (int)(end   / 6000) % 60;
162         ss1 = (int)(start / 100) % 60;  ms1 = (int)(start % 100);
163         ss2 = (int)(end   / 100) % 60;  ms2 = (int)(end   % 100);
164         if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
165         if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
166
167         dialogue->line = av_asprintf("%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
168                                      layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
169         if (!dialogue->line) {
170             av_free(dialogue);
171             return AVERROR(ENOMEM);
172         }
173         insert_dialogue(ass, dialogue);
174         purge_dialogues(s, 0);
175     } else {
176         avio_write(s->pb, pkt->data, pkt->size);
177     }
178
179     return 0;
180 }
181
182 static int write_trailer(AVFormatContext *s)
183 {
184     purge_dialogues(s, 1);
185     return 0;
186 }
187
188 AVOutputFormat ff_ass_muxer = {
189     .name           = "ass",
190     .long_name      = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
191     .mime_type      = "text/x-ssa",
192     .extensions     = "ass,ssa",
193     .priv_data_size = sizeof(ASSContext),
194     .subtitle_codec = AV_CODEC_ID_SSA,
195     .write_header   = write_header,
196     .write_packet   = write_packet,
197     .write_trailer  = write_trailer,
198     .flags          = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT,
199 };