libavcodec/assenc.c
Go to the documentation of this file.
1 /*
2  * SSA/ASS encoder
3  * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
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 <string.h>
23 
24 #include "avcodec.h"
25 #include "ass_split.h"
26 #include "ass.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 
31 typedef struct {
32  int id; ///< current event id, ReadOrder field
34 
36 {
37  avctx->extradata = av_malloc(avctx->subtitle_header_size + 1);
38  if (!avctx->extradata)
39  return AVERROR(ENOMEM);
40  memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size);
41  avctx->extradata_size = avctx->subtitle_header_size;
42  avctx->extradata[avctx->extradata_size] = 0;
43  return 0;
44 }
45 
46 static int ass_encode_frame(AVCodecContext *avctx,
47  unsigned char *buf, int bufsize,
48  const AVSubtitle *sub)
49 {
50  ASSEncodeContext *s = avctx->priv_data;
51  int i, len, total_len = 0;
52 
53  for (i=0; i<sub->num_rects; i++) {
54  char ass_line[2048];
55  const char *ass = sub->rects[i]->ass;
56 
57  if (sub->rects[i]->type != SUBTITLE_ASS) {
58  av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
59  return -1;
60  }
61 
62  if (strncmp(ass, "Dialogue: ", 10)) {
63  av_log(avctx, AV_LOG_ERROR, "AVSubtitle rectangle ass \"%s\""
64  " does not look like a SSA markup\n", ass);
65  return AVERROR_INVALIDDATA;
66  }
67 
68  if (avctx->codec->id == AV_CODEC_ID_ASS) {
69  long int layer;
70  char *p;
71 
72  if (i > 0) {
73  av_log(avctx, AV_LOG_ERROR, "ASS encoder supports only one "
74  "ASS rectangle field.\n");
75  return AVERROR_INVALIDDATA;
76  }
77 
78  ass += 10; // skip "Dialogue: "
79  /* parse Layer field. If it's a Marked field, the content
80  * will be "Marked=N" instead of the layer num, so we will
81  * have layer=0, which is fine. */
82  layer = strtol(ass, &p, 10);
83  if (*p) p += strcspn(p, ",") + 1; // skip layer or marked
84  if (*p) p += strcspn(p, ",") + 1; // skip start timestamp
85  if (*p) p += strcspn(p, ",") + 1; // skip end timestamp
86  snprintf(ass_line, sizeof(ass_line), "%d,%ld,%s", ++s->id, layer, p);
87  ass_line[strcspn(ass_line, "\r\n")] = 0;
88  ass = ass_line;
89  }
90  len = av_strlcpy(buf+total_len, ass, bufsize-total_len);
91 
92  if (len > bufsize-total_len-1) {
93  av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
94  return -1;
95  }
96 
97  total_len += len;
98  }
99 
100  return total_len;
101 }
102 
103 #if CONFIG_SSA_ENCODER
104 AVCodec ff_ssa_encoder = {
105  .name = "ssa",
106  .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
107  .type = AVMEDIA_TYPE_SUBTITLE,
108  .id = AV_CODEC_ID_SSA,
109  .init = ass_encode_init,
110  .encode_sub = ass_encode_frame,
111  .priv_data_size = sizeof(ASSEncodeContext),
112 };
113 #endif
114 
115 #if CONFIG_ASS_ENCODER
116 AVCodec ff_ass_encoder = {
117  .name = "ass",
118  .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
119  .type = AVMEDIA_TYPE_SUBTITLE,
120  .id = AV_CODEC_ID_ASS,
121  .init = ass_encode_init,
122  .encode_sub = ass_encode_frame,
123  .priv_data_size = sizeof(ASSEncodeContext),
124 };
125 #endif
const struct AVCodec * codec
const char * s
Definition: avisynth_c.h:668
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
memory handling functions
static int ass_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
AVSubtitleRect ** rects
#define av_cold
Definition: attributes.h:78
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static av_cold int ass_encode_init(AVCodecContext *avctx)
enum AVCodecID id
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:82
common internal API header
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
#define snprintf
Definition: snprintf.h:34
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
ASS as defined in Matroska.
Formatted text, the ass field must be set by the decoder and is authoritative.
int len
char * ass
0 terminated ASS/SSA compatible event line.
int id
current event id, ReadOrder field
enum AVSubtitleType type
uint8_t * subtitle_header
Header containing style information for text subtitles.