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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avcodec.h"
23 #include "libavutil/avstring.h"
24 
26 {
27  avctx->extradata = av_malloc(avctx->subtitle_header_size);
28  if (!avctx->extradata)
29  return AVERROR(ENOMEM);
30  memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size);
31  avctx->extradata_size = avctx->subtitle_header_size;
32  return 0;
33 }
34 
35 static int ass_encode_frame(AVCodecContext *avctx,
36  unsigned char *buf, int bufsize, void *data)
37 {
38  AVSubtitle *sub = data;
39  int i, len, total_len = 0;
40 
41  for (i=0; i<sub->num_rects; i++) {
42  if (sub->rects[i]->type != SUBTITLE_ASS) {
43  av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
44  return -1;
45  }
46 
47  len = av_strlcpy(buf+total_len, sub->rects[i]->ass, bufsize-total_len);
48 
49  if (len > bufsize-total_len-1) {
50  av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
51  return -1;
52  }
53 
54  total_len += len;
55  }
56 
57  return total_len;
58 }
59 
61  .name = "ass",
62  .long_name = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle"),
63  .type = AVMEDIA_TYPE_SUBTITLE,
64  .id = CODEC_ID_SSA,
65  .init = ass_encode_init,
66  .encode = ass_encode_frame,
67 };
unsigned num_rects
Definition: avcodec.h:3444
AVCodec.
Definition: avcodec.h:3189
static int ass_encode_frame(AVCodecContext *avctx, unsigned char *buf, int bufsize, void *data)
Definition: assenc.c:35
AVSubtitleRect ** rects
Definition: avcodec.h:3445
#define av_cold
Definition: attributes.h:71
int subtitle_header_size
Definition: avcodec.h:3075
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
const char data[16]
Definition: mxf.c:60
static av_cold int ass_encode_init(AVCodecContext *avctx)
Definition: assenc.c:25
#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:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
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:64
external API header
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition: avcodec.h:3413
main external API structure.
Definition: avcodec.h:1329
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int extradata_size
Definition: avcodec.h:1388
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:64
int len
char * ass
0 terminated ASS/SSA compatible event line.
Definition: avcodec.h:3437
enum AVSubtitleType type
Definition: avcodec.h:3428
AVCodec ff_ass_encoder
Definition: assenc.c:60
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:3074