FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
assenc.c
Go to the documentation of this file.
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)
38  int ssa_mode;
39 }ASSContext;
40 
42 {
43  ASSContext *ass = s->priv_data;
44  AVCodecContext *avctx= s->streams[0]->codec;
45 
46  if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
47  avctx->codec_id != AV_CODEC_ID_ASS)) {
48  av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
49  return AVERROR(EINVAL);
50  }
51  ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
52  avpriv_set_pts_info(s->streams[0], 64, 1, 100);
53  if (avctx->extradata_size > 0) {
54  avio_write(s->pb, avctx->extradata, avctx->extradata_size);
55  if (avctx->extradata[avctx->extradata_size - 1] != '\n')
56  avio_write(s->pb, "\r\n", 2);
57  ass->ssa_mode = !strstr(avctx->extradata, "\n[V4+ Styles]");
58  if (!strstr(avctx->extradata, "\n[Events]"))
59  avio_printf(s->pb, "[Events]\r\nFormat: %s, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
60  ass->ssa_mode ? "Marked" : "Layer");
61  }
62  avio_flush(s->pb);
63 
64  return 0;
65 }
66 
67 static void purge_dialogues(AVFormatContext *s, int force)
68 {
69  int n = 0;
70  ASSContext *ass = s->priv_data;
71  DialogueLine *dialogue = ass->dialogue_cache;
72 
73  while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
74  DialogueLine *next = dialogue->next;
75  if (dialogue->readorder != ass->expected_readorder) {
76  av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
77  ass->expected_readorder, dialogue->readorder);
78  ass->expected_readorder = dialogue->readorder;
79  }
80  avio_printf(s->pb, "Dialogue: %s\r\n", dialogue->line);
81  if (dialogue == ass->last_added_dialogue)
82  ass->last_added_dialogue = next;
83  av_free(dialogue->line);
84  av_free(dialogue);
85  if (next)
86  next->prev = NULL;
87  dialogue = ass->dialogue_cache = next;
88  ass->expected_readorder++;
89  n++;
90  }
91  ass->cache_size -= n;
92  if (n > 1)
93  av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
94  n, ass->cache_size, ass->expected_readorder);
95 }
96 
97 static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
98 {
99  DialogueLine *cur, *next = NULL, *prev = NULL;
100 
101  /* from the last added to the end of the list */
102  if (ass->last_added_dialogue) {
103  for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
104  if (cur->readorder > dialogue->readorder)
105  break;
106  prev = cur;
107  next = cur->next;
108  }
109  }
110 
111  /* from the beginning to the last one added */
112  if (!prev) {
113  next = ass->dialogue_cache;
114  for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
115  if (cur->readorder > dialogue->readorder)
116  break;
117  prev = cur;
118  next = cur->next;
119  }
120  }
121 
122  if (prev) {
123  prev->next = dialogue;
124  dialogue->prev = prev;
125  } else {
126  dialogue->prev = ass->dialogue_cache;
127  ass->dialogue_cache = dialogue;
128  }
129  if (next) {
130  next->prev = dialogue;
131  dialogue->next = next;
132  }
133  ass->cache_size++;
134  ass->last_added_dialogue = dialogue;
135 }
136 
138 {
139  ASSContext *ass = s->priv_data;
140 
141  if (ass->write_ts) {
142  long int layer;
143  char *p = pkt->data;
144  int64_t start = pkt->pts;
145  int64_t end = start + pkt->duration;
146  int hh1, mm1, ss1, ms1;
147  int hh2, mm2, ss2, ms2;
148  DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
149 
150  if (!dialogue)
151  return AVERROR(ENOMEM);
152 
153  dialogue->readorder = strtol(p, &p, 10);
154  if (dialogue->readorder < ass->expected_readorder)
155  av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
156  dialogue->readorder);
157  if (*p == ',')
158  p++;
159 
160  if (ass->ssa_mode && !strncmp(p, "Marked=", 7))
161  p += 7;
162 
163  layer = strtol(p, &p, 10);
164  if (*p == ',')
165  p++;
166  hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
167  hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
168  ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
169  ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
170  if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
171  if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
172 
173  dialogue->line = av_asprintf("%s%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
174  ass->ssa_mode ? "Marked=" : "",
175  layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
176  if (!dialogue->line) {
177  av_free(dialogue);
178  return AVERROR(ENOMEM);
179  }
180  insert_dialogue(ass, dialogue);
181  purge_dialogues(s, 0);
182  } else {
183  avio_write(s->pb, pkt->data, pkt->size);
184  }
185 
186  return 0;
187 }
188 
190 {
191  purge_dialogues(s, 1);
192  return 0;
193 }
194 
196  .name = "ass",
197  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
198  .mime_type = "text/x-ssa",
199  .extensions = "ass,ssa",
200  .priv_data_size = sizeof(ASSContext),
201  .subtitle_codec = AV_CODEC_ID_SSA,
206 };