[FFmpeg-devel] [PATCH 1/3] add ASS encoder and decoder

Aurélien Jacobs aurel
Sun Aug 8 02:26:56 CEST 2010


From: Aurelien Jacobs <aurel at gnuage.org>

---
 libavcodec/Makefile    |    2 +
 libavcodec/allcodecs.c |    1 +
 libavcodec/assdec.c    |   47 +++++++++++++++++++++++++++++++++++++
 libavcodec/assenc.c    |   61 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+), 0 deletions(-)
 create mode 100644 libavcodec/assdec.c
 create mode 100644 libavcodec/assenc.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index e29666a..a8f2805 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -63,6 +63,8 @@ OBJS-$(CONFIG_AMV_DECODER)             += sp5xdec.o mjpegdec.o mjpeg.o
 OBJS-$(CONFIG_ANM_DECODER)             += anm.o
 OBJS-$(CONFIG_ANSI_DECODER)            += ansi.o cga_data.o
 OBJS-$(CONFIG_APE_DECODER)             += apedec.o
+OBJS-$(CONFIG_ASS_DECODER)             += assdec.o
+OBJS-$(CONFIG_ASS_ENCODER)             += assenc.o
 OBJS-$(CONFIG_ASV1_DECODER)            += asv1.o mpeg12data.o
 OBJS-$(CONFIG_ASV1_ENCODER)            += asv1.o mpeg12data.o
 OBJS-$(CONFIG_ASV2_DECODER)            += asv1.o mpeg12data.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 83f075a..5f38ee0 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -336,6 +336,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC  (ADPCM_YAMAHA, adpcm_yamaha);
 
     /* subtitles */
+    REGISTER_ENCDEC  (ASS, ass);
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
     REGISTER_DECODER (PGSSUB, pgssub);
diff --git a/libavcodec/assdec.c b/libavcodec/assdec.c
new file mode 100644
index 0000000..98878a1
--- /dev/null
+++ b/libavcodec/assdec.c
@@ -0,0 +1,47 @@
+/*
+ * SSA/ASS decoder
+ * Copyright (c) 2010  Aurelien Jacobs <aurel at gnuage.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+
+
+static int ass_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
+                            AVPacket *avpkt)
+{
+    if (avpkt->size > 0) {
+        AVSubtitle *sub = data;
+        memset(sub, 0, sizeof(*sub));
+        sub->rects = av_mallocz(sizeof(*sub->rects));
+        sub->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
+        sub->num_rects = 1;
+        sub->rects[0]->type = SUBTITLE_ASS;
+        sub->rects[0]->ass  = av_strdup(avpkt->data);
+        *data_size = 1;
+    }
+    return avpkt->size;
+}
+
+AVCodec ass_decoder = {
+    .name      = "ass",
+    .long_name = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle"),
+    .type      = AVMEDIA_TYPE_SUBTITLE,
+    .id        = CODEC_ID_SSA,
+    .decode    = ass_decode_frame,
+};
diff --git a/libavcodec/assenc.c b/libavcodec/assenc.c
new file mode 100644
index 0000000..d0ba462
--- /dev/null
+++ b/libavcodec/assenc.c
@@ -0,0 +1,61 @@
+/*
+ * SSA/ASS encoder
+ * Copyright (c) 2010  Aurelien Jacobs <aurel at gnuage.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+#include "libavutil/avstring.h"
+
+
+static int ass_encode_frame(AVCodecContext *avctx,
+                            unsigned char *buf, int bufsize, void *data)
+{
+    AVSubtitle *sub = data;
+    int len;
+
+    if (sub->num_rects == 0)
+        return 0;
+
+    /* TODO: support multiple rects */
+    if (sub->num_rects > 1)
+        av_log(avctx, AV_LOG_WARNING, "Only single rects supported "
+               "(%d in subtitle.)\n", sub->num_rects);
+
+    if (sub->rects[0]->type != SUBTITLE_ASS) {
+        av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
+        return -1;
+    }
+
+    len = av_strlcpy(buf, sub->rects[0]->ass, bufsize);
+
+    if (len > bufsize-1) {
+        av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
+        return -1;
+    }
+
+    return len;
+}
+
+AVCodec ass_encoder = {
+    .name      = "ass",
+    .long_name = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle"),
+    .type      = AVMEDIA_TYPE_SUBTITLE,
+    .id        = CODEC_ID_SSA,
+    .encode    = ass_encode_frame,
+};
-- 
1.7.1




More information about the ffmpeg-devel mailing list