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 "avio_internal.h"
25 #include "internal.h"
26 #include "mux.h"
27 
28 #include "libavutil/opt.h"
29 
30 typedef struct DialogueLine {
31  int readorder;
32  char *line;
33  struct DialogueLine *prev, *next;
34 } DialogueLine;
35 
36 typedef struct ASSContext {
37  const AVClass *class;
42  int ssa_mode;
44  uint8_t *trailer;
45  size_t trailer_size;
46 } ASSContext;
47 
49 {
50  ASSContext *ass = s->priv_data;
51  AVCodecParameters *par = s->streams[0]->codecpar;
52 
53  if (s->nb_streams != 1 || par->codec_id != AV_CODEC_ID_ASS) {
54  av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
55  return AVERROR(EINVAL);
56  }
57  avpriv_set_pts_info(s->streams[0], 64, 1, 100);
58  if (par->extradata_size > 0) {
59  size_t header_size = par->extradata_size;
60  uint8_t *trailer = strstr(par->extradata, "\n[Events]");
61 
62  if (trailer)
63  trailer = strstr(trailer, "Format:");
64  if (trailer)
65  trailer = strstr(trailer, "\n");
66 
67  if (trailer) {
68  header_size = (++trailer - par->extradata);
69  ass->trailer_size = par->extradata_size - header_size;
70  if (ass->trailer_size)
71  ass->trailer = trailer;
72  }
73 
74  ffio_write_lines(s->pb, par->extradata, header_size, NULL);
75 
76  ass->ssa_mode = !strstr(par->extradata, "\n[V4+ Styles]");
77  if (!strstr(par->extradata, "\n[Events]"))
78  avio_printf(s->pb, "[Events]\nFormat: %s, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n",
79  ass->ssa_mode ? "Marked" : "Layer");
80  }
81 
82  return 0;
83 }
84 
85 static void purge_dialogues(AVFormatContext *s, int force)
86 {
87  int n = 0;
88  ASSContext *ass = s->priv_data;
89  DialogueLine *dialogue = ass->dialogue_cache;
90 
91  while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
92  DialogueLine *next = dialogue->next;
93  if (dialogue->readorder != ass->expected_readorder) {
94  av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
95  ass->expected_readorder, dialogue->readorder);
96  ass->expected_readorder = dialogue->readorder;
97  }
98  avio_print(s->pb, "Dialogue: ", dialogue->line, "\n");
99  if (dialogue == ass->last_added_dialogue)
100  ass->last_added_dialogue = next;
101  av_freep(&dialogue->line);
102  av_free(dialogue);
103  if (next)
104  next->prev = NULL;
105  dialogue = ass->dialogue_cache = next;
106  ass->expected_readorder++;
107  n++;
108  }
109  ass->cache_size -= n;
110  if (n > 1)
111  av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
112  n, ass->cache_size, ass->expected_readorder);
113 }
114 
115 static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
116 {
117  DialogueLine *cur, *next = NULL, *prev = NULL;
118 
119  /* from the last added to the end of the list */
120  if (ass->last_added_dialogue) {
121  for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
122  if (cur->readorder > dialogue->readorder)
123  break;
124  prev = cur;
125  next = cur->next;
126  }
127  }
128 
129  /* from the beginning to the last one added */
130  if (!prev) {
131  next = ass->dialogue_cache;
132  for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
133  if (cur->readorder > dialogue->readorder)
134  break;
135  prev = cur;
136  next = cur->next;
137  }
138  }
139 
140  if (prev) {
141  prev->next = dialogue;
142  dialogue->prev = prev;
143  } else {
144  dialogue->prev = ass->dialogue_cache;
145  ass->dialogue_cache = dialogue;
146  }
147  if (next) {
148  next->prev = dialogue;
149  dialogue->next = next;
150  }
151  ass->cache_size++;
152  ass->last_added_dialogue = dialogue;
153 }
154 
156 {
157  ASSContext *ass = s->priv_data;
158 
159  long int layer;
160  int text_len;
161  char *p = pkt->data;
162  int64_t start = pkt->pts;
163  int64_t end = start + pkt->duration;
164  int hh1, mm1, ss1, ms1;
165  int hh2, mm2, ss2, ms2;
166  DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
167 
168  if (!dialogue)
169  return AVERROR(ENOMEM);
170 
171  dialogue->readorder = strtol(p, &p, 10);
172  if (dialogue->readorder < ass->expected_readorder)
173  av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
174  dialogue->readorder);
175  if (*p == ',')
176  p++;
177 
178  if (ass->ssa_mode && !strncmp(p, "Marked=", 7))
179  p += 7;
180 
181  layer = strtol(p, &p, 10);
182  if (*p == ',')
183  p++;
184  hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
185  hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
186  ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
187  ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
188  if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
189  if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
190 
191  text_len = strlen(p);
192  while (text_len > 0 && p[text_len - 1] == '\r' || p[text_len - 1] == '\n')
193  text_len--;
194 
195  dialogue->line = av_asprintf("%s%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%.*s",
196  ass->ssa_mode ? "Marked=" : "",
197  layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, text_len, p);
198  if (!dialogue->line) {
199  av_free(dialogue);
200  return AVERROR(ENOMEM);
201  }
202  insert_dialogue(ass, dialogue);
204 
205  return 0;
206 }
207 
209 {
210  ASSContext *ass = s->priv_data;
211 
212  purge_dialogues(s, 1);
213 
214  if (ass->trailer) {
215  ffio_write_lines(s->pb, ass->trailer, ass->trailer_size, NULL);
216  }
217 
218  return 0;
219 }
220 
221 #define OFFSET(x) offsetof(ASSContext, x)
222 #define E AV_OPT_FLAG_ENCODING_PARAM
223 static const AVOption options[] = {
224  { "ignore_readorder", "write events immediately, even if they're out-of-order", OFFSET(ignore_readorder), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
225  { NULL },
226 };
227 
228 static const AVClass ass_class = {
229  .class_name = "ass muxer",
230  .item_name = av_default_item_name,
231  .option = options,
232  .version = LIBAVUTIL_VERSION_INT,
233 };
234 
236  .p.name = "ass",
237  .p.long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
238  .p.mime_type = "text/x-ass",
239  .p.extensions = "ass,ssa",
240  .p.subtitle_codec = AV_CODEC_ID_ASS,
242  .p.priv_class = &ass_class,
243  .priv_data_size = sizeof(ASSContext),
247 };
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:69
AVOutputFormat::name
const char * name
Definition: avformat.h:511
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:41
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:480
av_asprintf
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:115
insert_dialogue
static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
Definition: assenc.c:115
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:251
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
DialogueLine::readorder
int readorder
Definition: assenc.c:31
ASSContext::trailer_size
size_t trailer_size
Definition: assenc.c:45
ASSContext::ignore_readorder
int ignore_readorder
Definition: assenc.c:43
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:36
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:574
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: avformat.c:830
DialogueLine::line
char * line
Definition: assenc.c:32
ASSContext::trailer
uint8_t * trailer
Definition: assenc.c:44
purge_dialogues
static void purge_dialogues(AVFormatContext *s, int force)
Definition: assenc.c:85
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:198
DialogueLine
Definition: assenc.c:30
write_header
static int write_header(AVFormatContext *s)
Definition: assenc.c:48
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:1202
DialogueLine::prev
struct DialogueLine * prev
Definition: assenc.c:33
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:228
avio_print
#define avio_print(s,...)
Write strings (const char *) to the context.
Definition: avio.h:556
ASSContext::ssa_mode
int ssa_mode
Definition: assenc.c:42
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFOutputFormat
Definition: mux.h:32
OFFSET
#define OFFSET(x)
Definition: assenc.c:221
ff_ass_muxer
const FFOutputFormat ff_ass_muxer
Definition: assenc.c:235
DialogueLine::next
struct DialogueLine * next
Definition: assenc.c:33
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
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:106
ASSContext::expected_readorder
int expected_readorder
Definition: assenc.c:38
write_packet
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:155
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:479
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
avio_internal.h
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:254
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:492
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
ffio_write_lines
void ffio_write_lines(AVIOContext *s, const unsigned char *buf, int size, const unsigned char *ending)
Write a sequence of text lines, converting line endings.
Definition: aviobuf.c:1007
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:222
ASSContext
Definition: assdec.c:30
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ASSContext::last_added_dialogue
DialogueLine * last_added_dialogue
Definition: assenc.c:40
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h
options
static const AVOption options[]
Definition: assenc.c:223
int
int
Definition: ffmpeg_filter.c:424
ASSContext::dialogue_cache
DialogueLine * dialogue_cache
Definition: assenc.c:39
write_trailer
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:208
mux.h