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 #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;
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  avio_flush(s->pb);
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_printf(s->pb, "Dialogue: %s\r\n", dialogue->line);
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  char *p = pkt->data;
161  int64_t start = pkt->pts;
162  int64_t end = start + pkt->duration;
163  int hh1, mm1, ss1, ms1;
164  int hh2, mm2, ss2, ms2;
165  DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
166 
167  if (!dialogue)
168  return AVERROR(ENOMEM);
169 
170  dialogue->readorder = strtol(p, &p, 10);
171  if (dialogue->readorder < ass->expected_readorder)
172  av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
173  dialogue->readorder);
174  if (*p == ',')
175  p++;
176 
177  if (ass->ssa_mode && !strncmp(p, "Marked=", 7))
178  p += 7;
179 
180  layer = strtol(p, &p, 10);
181  if (*p == ',')
182  p++;
183  hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
184  hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
185  ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
186  ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
187  if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
188  if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
189 
190  dialogue->line = av_asprintf("%s%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
191  ass->ssa_mode ? "Marked=" : "",
192  layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
193  if (!dialogue->line) {
194  av_free(dialogue);
195  return AVERROR(ENOMEM);
196  }
197  insert_dialogue(ass, dialogue);
199 
200  return 0;
201 }
202 
204 {
205  ASSContext *ass = s->priv_data;
206 
207  purge_dialogues(s, 1);
208 
209  if (ass->trailer) {
210  avio_write(s->pb, ass->trailer, ass->trailer_size);
211  }
212 
213  return 0;
214 }
215 
216 #define OFFSET(x) offsetof(ASSContext, x)
217 #define E AV_OPT_FLAG_ENCODING_PARAM
218 static const AVOption options[] = {
219  { "ignore_readorder", "write events immediately, even if they're out-of-order", OFFSET(ignore_readorder), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, E },
220  { NULL },
221 };
222 
223 static const AVClass ass_class = {
224  .class_name = "ass muxer",
225  .item_name = av_default_item_name,
226  .option = options,
227  .version = LIBAVUTIL_VERSION_INT,
228 };
229 
231  .name = "ass",
232  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
233  .mime_type = "text/x-ass",
234  .extensions = "ass,ssa",
235  .priv_data_size = sizeof(ASSContext),
236  .subtitle_codec = AV_CODEC_ID_ASS,
241  .priv_class = &ass_class,
242 };
static int write_header(AVFormatContext *s)
Definition: assenc.c:46
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
AVOption.
Definition: opt.h:245
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
void avpriv_set_pts_info(AVStream *s, 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:4405
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:155
static const AVClass ass_class
Definition: assenc.c:223
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3915
int cache_size
Definition: assenc.c:39
struct DialogueLine * next
Definition: assenc.c:31
size_t trailer_size
Definition: assenc.c:43
static AVPacket pkt
AVOutputFormat ff_ass_muxer
Definition: assenc.c:230
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:494
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3907
Format I/O context.
Definition: avformat.h:1319
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:72
uint8_t
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1595
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1387
int expected_readorder
Definition: assenc.c:36
uint8_t * data
Definition: avcodec.h:1577
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:187
#define av_log(a,...)
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:203
static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
Definition: assenc.c:115
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static void purge_dialogues(AVFormatContext *s, int force)
Definition: assenc.c:85
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3933
struct DialogueLine * prev
Definition: assenc.c:31
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1375
#define OFFSET(x)
Definition: assenc.c:216
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:207
uint8_t * trailer
Definition: assenc.c:42
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:483
const char * name
Definition: avformat.h:522
int n
Definition: avisynth_c.h:547
static const AVOption options[]
Definition: assenc.c:218
DialogueLine * dialogue_cache
Definition: assenc.c:37
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:484
#define E
Definition: assenc.c:217
AVIOContext * pb
I/O context.
Definition: avformat.h:1361
Describe the class of an AVClass context structure.
Definition: log.h:67
int readorder
Definition: assenc.c:29
static int flags
Definition: cpu.c:47
Main libavformat public API header.
char * line
Definition: assenc.c:30
int ignore_readorder
Definition: assenc.c:41
DialogueLine * last_added_dialogue
Definition: assenc.c:38
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1347
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3929
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
AVCodecParameters * codecpar
Definition: avformat.h:1006
This structure stores compressed data.
Definition: avcodec.h:1554
int ssa_mode
Definition: assenc.c:40
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1570
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2