FFmpeg
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 #include "libavutil/opt.h"
27 
28 typedef struct DialogueLine {
29  int readorder;
30  char *line;
31  struct DialogueLine *prev, *next;
32 } DialogueLine;
33 
34 typedef struct ASSContext {
35  const AVClass *class;
40  int ssa_mode;
42  uint8_t *trailer;
43  size_t trailer_size;
44 } ASSContext;
45 
47 {
48  ASSContext *ass = s->priv_data;
49  AVCodecParameters *par = s->streams[0]->codecpar;
50 
51  if (s->nb_streams != 1 || par->codec_id != AV_CODEC_ID_ASS) {
52  av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
53  return AVERROR(EINVAL);
54  }
55  avpriv_set_pts_info(s->streams[0], 64, 1, 100);
56  if (par->extradata_size > 0) {
57  size_t header_size = par->extradata_size;
58  uint8_t *trailer = strstr(par->extradata, "\n[Events]");
59 
60  if (trailer)
61  trailer = strstr(trailer, "Format:");
62  if (trailer)
63  trailer = strstr(trailer, "\n");
64 
65  if (trailer++) {
66  header_size = (trailer - par->extradata);
67  ass->trailer_size = par->extradata_size - header_size;
68  if (ass->trailer_size)
69  ass->trailer = trailer;
70  }
71 
72  avio_write(s->pb, par->extradata, header_size);
73  if (par->extradata[header_size - 1] != '\n')
74  avio_write(s->pb, "\r\n", 2);
75  ass->ssa_mode = !strstr(par->extradata, "\n[V4+ Styles]");
76  if (!strstr(par->extradata, "\n[Events]"))
77  avio_printf(s->pb, "[Events]\r\nFormat: %s, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
78  ass->ssa_mode ? "Marked" : "Layer");
79  }
80 
81  return 0;
82 }
83 
84 static void purge_dialogues(AVFormatContext *s, int force)
85 {
86  int n = 0;
87  ASSContext *ass = s->priv_data;
88  DialogueLine *dialogue = ass->dialogue_cache;
89 
90  while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
91  DialogueLine *next = dialogue->next;
92  if (dialogue->readorder != ass->expected_readorder) {
93  av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
94  ass->expected_readorder, dialogue->readorder);
95  ass->expected_readorder = dialogue->readorder;
96  }
97  avio_print(s->pb, "Dialogue: ", dialogue->line, "\r\n");
98  if (dialogue == ass->last_added_dialogue)
99  ass->last_added_dialogue = next;
100  av_freep(&dialogue->line);
101  av_free(dialogue);
102  if (next)
103  next->prev = NULL;
104  dialogue = ass->dialogue_cache = next;
105  ass->expected_readorder++;
106  n++;
107  }
108  ass->cache_size -= n;
109  if (n > 1)
110  av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
111  n, ass->cache_size, ass->expected_readorder);
112 }
113 
114 static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
115 {
116  DialogueLine *cur, *next = NULL, *prev = NULL;
117 
118  /* from the last added to the end of the list */
119  if (ass->last_added_dialogue) {
120  for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
121  if (cur->readorder > dialogue->readorder)
122  break;
123  prev = cur;
124  next = cur->next;
125  }
126  }
127 
128  /* from the beginning to the last one added */
129  if (!prev) {
130  next = ass->dialogue_cache;
131  for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
132  if (cur->readorder > dialogue->readorder)
133  break;
134  prev = cur;
135  next = cur->next;
136  }
137  }
138 
139  if (prev) {
140  prev->next = dialogue;
141  dialogue->prev = prev;
142  } else {
143  dialogue->prev = ass->dialogue_cache;
144  ass->dialogue_cache = dialogue;
145  }
146  if (next) {
147  next->prev = dialogue;
148  dialogue->next = next;
149  }
150  ass->cache_size++;
151  ass->last_added_dialogue = dialogue;
152 }
153 
155 {
156  ASSContext *ass = s->priv_data;
157 
158  long int layer;
159  char *p = pkt->data;
160  int64_t start = pkt->pts;
161  int64_t end = start + pkt->duration;
162  int hh1, mm1, ss1, ms1;
163  int hh2, mm2, ss2, ms2;
164  DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
165 
166  if (!dialogue)
167  return AVERROR(ENOMEM);
168 
169  dialogue->readorder = strtol(p, &p, 10);
170  if (dialogue->readorder < ass->expected_readorder)
171  av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
172  dialogue->readorder);
173  if (*p == ',')
174  p++;
175 
176  if (ass->ssa_mode && !strncmp(p, "Marked=", 7))
177  p += 7;
178 
179  layer = strtol(p, &p, 10);
180  if (*p == ',')
181  p++;
182  hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
183  hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
184  ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
185  ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
186  if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
187  if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
188 
189  dialogue->line = av_asprintf("%s%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
190  ass->ssa_mode ? "Marked=" : "",
191  layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
192  if (!dialogue->line) {
193  av_free(dialogue);
194  return AVERROR(ENOMEM);
195  }
196  insert_dialogue(ass, dialogue);
198 
199  return 0;
200 }
201 
203 {
204  ASSContext *ass = s->priv_data;
205 
206  purge_dialogues(s, 1);
207 
208  if (ass->trailer) {
209  avio_write(s->pb, ass->trailer, ass->trailer_size);
210  }
211 
212  return 0;
213 }
214 
215 #define OFFSET(x) offsetof(ASSContext, x)
216 #define E AV_OPT_FLAG_ENCODING_PARAM
217 static const AVOption options[] = {
218  { "ignore_readorder", "write events immediately, even if they're out-of-order", OFFSET(ignore_readorder), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
219  { NULL },
220 };
221 
222 static const AVClass ass_class = {
223  .class_name = "ass muxer",
224  .item_name = av_default_item_name,
225  .option = options,
226  .version = LIBAVUTIL_VERSION_INT,
227 };
228 
230  .name = "ass",
231  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
232  .mime_type = "text/x-ass",
233  .extensions = "ass,ssa",
234  .priv_data_size = sizeof(ASSContext),
235  .subtitle_codec = AV_CODEC_ID_ASS,
240  .priv_class = &ass_class,
241 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
AVOutputFormat::name
const char * name
Definition: avformat.h:496
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ASSContext::cache_size
int cache_size
Definition: assenc.c:39
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
av_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
insert_dialogue
static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
Definition: assenc.c:114
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
DialogueLine::readorder
int readorder
Definition: assenc.c:29
ASSContext::trailer_size
size_t trailer_size
Definition: assenc.c:43
ASSContext::ignore_readorder
int ignore_readorder
Definition: assenc.c:41
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:543
DialogueLine::line
char * line
Definition: assenc.c:30
ASSContext::trailer
uint8_t * trailer
Definition: assenc.c:42
purge_dialogues
static void purge_dialogues(AVFormatContext *s, int force)
Definition: assenc.c:84
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:257
DialogueLine
Definition: assenc.c:28
write_header
static int write_header(AVFormatContext *s)
Definition: assenc.c:46
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AVFormatContext
Format I/O context.
Definition: avformat.h:1097
DialogueLine::prev
struct DialogueLine * prev
Definition: assenc.c:31
internal.h
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ass_class
static const AVClass ass_class
Definition: assenc.c:222
avio_print
#define avio_print(s,...)
Write strings (const char *) to the context.
Definition: avio.h:523
ASSContext::ssa_mode
int ssa_mode
Definition: assenc.c:40
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
OFFSET
#define OFFSET(x)
Definition: assenc.c:215
ff_ass_muxer
const AVOutputFormat ff_ass_muxer
Definition: assenc.c:229
DialogueLine::next
struct DialogueLine * next
Definition: assenc.c:31
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
ASSContext::expected_readorder
int expected_readorder
Definition: assenc.c:36
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
write_packet
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:154
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:466
AVOutputFormat
Definition: avformat.h:495
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:477
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
avformat.h
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
E
#define E
Definition: assenc.c:216
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:1146
ASSContext
Definition: assdec.c:31
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
ASSContext::last_added_dialogue
DialogueLine * last_added_dialogue
Definition: assenc.c:38
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
avstring.h
options
static const AVOption options[]
Definition: assenc.c:217
int
int
Definition: ffmpeg_filter.c:156
ASSContext::dialogue_cache
DialogueLine * dialogue_cache
Definition: assenc.c:37
write_trailer
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:202