FFmpeg
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 "config_components.h"
23 
24 #include <string.h>
25 
26 #include "avcodec.h"
27 #include "ass.h"
28 #include "codec_internal.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mem.h"
32 
34 {
35  avctx->extradata = av_malloc(avctx->subtitle_header_size + 1);
36  if (!avctx->extradata)
37  return AVERROR(ENOMEM);
38  memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size);
39  avctx->extradata_size = avctx->subtitle_header_size;
40  avctx->extradata[avctx->extradata_size] = 0;
41  return 0;
42 }
43 
44 static int ass_encode_frame(AVCodecContext *avctx,
45  unsigned char *buf, int bufsize,
46  const AVSubtitle *sub)
47 {
48  int i, len, total_len = 0;
49 
50  for (i=0; i<sub->num_rects; i++) {
51  const char *ass = sub->rects[i]->ass;
52 
53  if (sub->rects[i]->type != SUBTITLE_ASS) {
54  av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
55  return AVERROR(EINVAL);
56  }
57 
58  len = av_strlcpy(buf+total_len, ass, bufsize-total_len);
59 
60  if (len > bufsize-total_len-1) {
61  av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
63  }
64 
65  total_len += len;
66  }
67 
68  return total_len;
69 }
70 
71 #if CONFIG_SSA_ENCODER
72 const FFCodec ff_ssa_encoder = {
73  .p.name = "ssa",
74  CODEC_LONG_NAME("ASS (Advanced SubStation Alpha) subtitle"),
75  .p.type = AVMEDIA_TYPE_SUBTITLE,
76  .p.id = AV_CODEC_ID_ASS,
77  .init = ass_encode_init,
79 };
80 #endif
81 
82 #if CONFIG_ASS_ENCODER
83 const FFCodec ff_ass_encoder = {
84  .p.name = "ass",
85  CODEC_LONG_NAME("ASS (Advanced SubStation Alpha) subtitle"),
86  .p.type = AVMEDIA_TYPE_SUBTITLE,
87  .p.id = AV_CODEC_ID_ASS,
88  .init = ass_encode_init,
90 };
91 #endif
AVSubtitle
Definition: avcodec.h:2330
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
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
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:31
FFCodec
Definition: codec_internal.h:127
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:1723
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
SUBTITLE_ASS
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition: avcodec.h:2297
AV_CODEC_ID_ASS
@ AV_CODEC_ID_ASS
Definition: codec_id.h:566
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
ass.h
ff_ass_encoder
const FFCodec ff_ass_encoder
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:528
ff_ssa_encoder
const FFCodec ff_ssa_encoder
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
AVCodecContext::subtitle_header_size
int subtitle_header_size
Definition: avcodec.h:1724
FF_CODEC_ENCODE_SUB_CB
#define FF_CODEC_ENCODE_SUB_CB(func)
Definition: codec_internal.h:318
codec_internal.h
ass_encode_frame
static int ass_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, const AVSubtitle *sub)
Definition: assenc.c:44
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:527
internal.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
len
int len
Definition: vorbis_enc_data.h:426
avcodec.h
AVCodecContext
main external API structure.
Definition: avcodec.h:426
ass_encode_init
static av_cold int ass_encode_init(AVCodecContext *avctx)
Definition: assenc.c:33
mem.h
av_strlcpy
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:86
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h