[FFmpeg-devel] [PATCH 2/2] Move AVSubtitle from lavc to lavu

Clément Bœsch u at pkh.me
Sun Dec 14 16:47:44 CET 2014


---
 doc/APIchanges       |   5 +++
 libavcodec/avcodec.h |  63 +++---------------------------
 libavcodec/utils.c   |  29 +++-----------
 libavcodec/version.h |   3 ++
 libavutil/Makefile   |   2 +
 libavutil/subtitle.c |  62 +++++++++++++++++++++++++++++
 libavutil/subtitle.h | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 191 insertions(+), 80 deletions(-)
 create mode 100644 libavutil/subtitle.c
 create mode 100644 libavutil/subtitle.h

diff --git a/doc/APIchanges b/doc/APIchanges
index ba7eae1..989794a 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,11 @@ libavutil:     2014-08-09
 
 API changes, most recent first:
 
+2014-12-xx - xxxxxxx - lavc 56.16.100 / lavu 54.17.100 - lavc/avcodec.h lavu/picture.h
+  Move AVSubtitle definition from libavcodec to libavutil and add
+  av_subtitle_*() functions which should be used instead of stack allocation
+  and the now deprecated avsubtitle_free().
+
 2014-12-xx - xxxxxxx - lavc 56.15.100 / lavu 54.16.100 - lavc/avcodec.h lavu/picture.h
   Move AVPicture definition from libavcodec to libavutil. This should be
   transparent API and ABI wise.
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 2c1918d..6ded06b 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -40,6 +40,7 @@
 #include "libavutil/picture.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/rational.h"
+#include "libavutil/subtitle.h"
 
 #include "version.h"
 
@@ -3149,8 +3150,6 @@ typedef struct AVProfile {
 
 typedef struct AVCodecDefault AVCodecDefault;
 
-struct AVSubtitle;
-
 /**
  * AVCodec.
  */
@@ -3401,61 +3400,6 @@ typedef struct AVHWAccel {
  * @}
  */
 
-enum AVSubtitleType {
-    SUBTITLE_NONE,
-
-    SUBTITLE_BITMAP,                ///< A bitmap, pict will be set
-
-    /**
-     * Plain text, the text field must be set by the decoder and is
-     * authoritative. ass and pict fields may contain approximations.
-     */
-    SUBTITLE_TEXT,
-
-    /**
-     * Formatted text, the ass field must be set by the decoder and is
-     * authoritative. pict and text fields may contain approximations.
-     */
-    SUBTITLE_ASS,
-};
-
-#define AV_SUBTITLE_FLAG_FORCED 0x00000001
-
-typedef struct AVSubtitleRect {
-    int x;         ///< top left corner  of pict, undefined when pict is not set
-    int y;         ///< top left corner  of pict, undefined when pict is not set
-    int w;         ///< width            of pict, undefined when pict is not set
-    int h;         ///< height           of pict, undefined when pict is not set
-    int nb_colors; ///< number of colors in pict, undefined when pict is not set
-
-    /**
-     * data+linesize for the bitmap of this subtitle.
-     * can be set for text/ass as well once they are rendered
-     */
-    AVPicture pict;
-    enum AVSubtitleType type;
-
-    char *text;                     ///< 0 terminated plain UTF-8 text
-
-    /**
-     * 0 terminated ASS/SSA compatible event line.
-     * The presentation of this is unaffected by the other values in this
-     * struct.
-     */
-    char *ass;
-
-    int flags;
-} AVSubtitleRect;
-
-typedef struct AVSubtitle {
-    uint16_t format; /* 0 = graphics */
-    uint32_t start_display_time; /* relative to packet pts, in ms */
-    uint32_t end_display_time; /* relative to packet pts, in ms */
-    unsigned num_rects;
-    AVSubtitleRect **rects;
-    int64_t pts;    ///< Same as packet pts, in AV_TIME_BASE
-} AVSubtitle;
-
 /**
  * If c is NULL, returns the first registered codec,
  * if c is non-NULL, returns the next registered codec after c,
@@ -3652,12 +3596,17 @@ int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **op
  */
 int avcodec_close(AVCodecContext *avctx);
 
+#if FF_API_AVSUBTITLE
 /**
  * Free all allocated data in the given subtitle struct.
+ * @deprecated use av_subtitle_*() functions (the immediate equivalent of
+ *             avsubtitle_free() is av_subtitle_clear())
  *
  * @param sub AVSubtitle to free.
  */
+attribute_deprecated
 void avsubtitle_free(AVSubtitle *sub);
+#endif
 
 /**
  * @}
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index db79b67..dc0c1ec 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -39,6 +39,7 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/samplefmt.h"
+#include "libavutil/subtitle.h"
 #include "libavutil/dict.h"
 #include "avcodec.h"
 #include "libavutil/opt.h"
@@ -1270,12 +1271,6 @@ int av_codec_get_max_lowres(const AVCodec *codec)
     return codec->max_lowres;
 }
 
-static void get_subtitle_defaults(AVSubtitle *sub)
-{
-    memset(sub, 0, sizeof(*sub));
-    sub->pts = AV_NOPTS_VALUE;
-}
-
 static int get_bit_rate(AVCodecContext *ctx)
 {
     int bit_rate;
@@ -2706,7 +2701,7 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
     }
 
     *got_sub_ptr = 0;
-    get_subtitle_defaults(sub);
+    av_subtitle_get_defaults(sub);
 
     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
         AVPacket pkt_recoded;
@@ -2750,7 +2745,7 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
                     av_log(avctx, AV_LOG_ERROR,
                            "Invalid UTF-8 in decoded subtitles text; "
                            "maybe missing -sub_charenc option\n");
-                    avsubtitle_free(sub);
+                    av_subtitle_clear(sub);
                     return AVERROR_INVALIDDATA;
                 }
             }
@@ -2782,24 +2777,12 @@ int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
     return ret;
 }
 
+#if FF_API_AVSUBTITLE
 void avsubtitle_free(AVSubtitle *sub)
 {
-    int i;
-
-    for (i = 0; i < sub->num_rects; i++) {
-        av_freep(&sub->rects[i]->pict.data[0]);
-        av_freep(&sub->rects[i]->pict.data[1]);
-        av_freep(&sub->rects[i]->pict.data[2]);
-        av_freep(&sub->rects[i]->pict.data[3]);
-        av_freep(&sub->rects[i]->text);
-        av_freep(&sub->rects[i]->ass);
-        av_freep(&sub->rects[i]);
-    }
-
-    av_freep(&sub->rects);
-
-    memset(sub, 0, sizeof(AVSubtitle));
+    av_subtitle_clear(sub);
 }
+#endif
 
 av_cold int avcodec_close(AVCodecContext *avctx)
 {
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 6610e8c..7c611c5 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -184,5 +184,8 @@
 #ifndef FF_API_MPV_OPT
 #define FF_API_MPV_OPT           (LIBAVCODEC_VERSION_MAJOR < 59)
 #endif
+#ifndef FF_API_AVSUBTITLE
+#define FF_API_AVSUBTITLE        (LIBAVCODEC_VERSION_MAJOR < 59)
+#endif
 
 #endif /* AVCODEC_VERSION_H */
diff --git a/libavutil/Makefile b/libavutil/Makefile
index ad703db..2998b08 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -56,6 +56,7 @@ HEADERS = adler32.h                                                     \
           sha.h                                                         \
           sha512.h                                                      \
           stereo3d.h                                                    \
+          subtitle.h                                                    \
           threadmessage.h                                               \
           time.h                                                        \
           timecode.h                                                    \
@@ -124,6 +125,7 @@ OBJS = adler32.o                                                        \
        sha.o                                                            \
        sha512.o                                                         \
        stereo3d.o                                                       \
+       subtitle.o                                                       \
        threadmessage.o                                                  \
        time.o                                                           \
        timecode.o                                                       \
diff --git a/libavutil/subtitle.c b/libavutil/subtitle.c
new file mode 100644
index 0000000..25217af
--- /dev/null
+++ b/libavutil/subtitle.c
@@ -0,0 +1,62 @@
+/*
+ * 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 "subtitle.h"
+
+void av_subtitle_get_defaults(AVSubtitle *sub)
+{
+    memset(sub, 0, sizeof(*sub));
+    sub->pts = AV_NOPTS_VALUE;
+}
+
+AVSubtitle *av_subtitle_alloc(void)
+{
+    AVSubtitle *sub = av_mallocz(sizeof(*sub));
+
+    if (!sub)
+        return NULL;
+    av_subtitle_get_defaults(sub);
+    return sub;
+}
+
+void av_subtitle_clear(AVSubtitle *sub)
+{
+    int i;
+
+    for (i = 0; i < sub->num_rects; i++) {
+        av_freep(&sub->rects[i]->pict.data[0]);
+        av_freep(&sub->rects[i]->pict.data[1]);
+        av_freep(&sub->rects[i]->pict.data[2]);
+        av_freep(&sub->rects[i]->pict.data[3]);
+        av_freep(&sub->rects[i]->text);
+        av_freep(&sub->rects[i]->ass);
+        av_freep(&sub->rects[i]);
+    }
+
+    av_freep(&sub->rects);
+
+    memset(sub, 0, sizeof(AVSubtitle));
+}
+
+void av_subtitle_free(AVSubtitle **sub)
+{
+    if (!sub || !*sub)
+        return;
+    av_subtitle_clear(*sub);
+    av_freep(sub);
+}
diff --git a/libavutil/subtitle.h b/libavutil/subtitle.h
new file mode 100644
index 0000000..6d205cd
--- /dev/null
+++ b/libavutil/subtitle.h
@@ -0,0 +1,107 @@
+/*
+ * 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
+ */
+
+#ifndef AVUTIL_SUBTITLE_H
+#define AVUTIL_SUBTITLE_H
+
+#include <stdint.h>
+
+#include "picture.h"
+
+enum AVSubtitleType {
+    SUBTITLE_NONE,
+
+    SUBTITLE_BITMAP,                ///< A bitmap, pict will be set
+
+    /**
+     * Plain text, the text field must be set by the decoder and is
+     * authoritative. ass and pict fields may contain approximations.
+     */
+    SUBTITLE_TEXT,
+
+    /**
+     * Formatted text, the ass field must be set by the decoder and is
+     * authoritative. pict and text fields may contain approximations.
+     */
+    SUBTITLE_ASS,
+};
+
+#define AV_SUBTITLE_FLAG_FORCED 0x00000001
+
+typedef struct AVSubtitleRect {
+    int x;         ///< top left corner  of pict, undefined when pict is not set
+    int y;         ///< top left corner  of pict, undefined when pict is not set
+    int w;         ///< width            of pict, undefined when pict is not set
+    int h;         ///< height           of pict, undefined when pict is not set
+    int nb_colors; ///< number of colors in pict, undefined when pict is not set
+
+    /**
+     * data+linesize for the bitmap of this subtitle.
+     * can be set for text/ass as well once they are rendered
+     */
+    AVPicture pict;
+    enum AVSubtitleType type;
+
+    char *text;                     ///< 0 terminated plain UTF-8 text
+
+    /**
+     * 0 terminated ASS/SSA compatible event line.
+     * The presentation of this is unaffected by the other values in this
+     * struct.
+     */
+    char *ass;
+
+    int flags;
+} AVSubtitleRect;
+
+typedef struct AVSubtitle {
+    uint16_t format; /* 0 = graphics */
+    uint32_t start_display_time; /* relative to packet pts, in ms */
+    uint32_t end_display_time; /* relative to packet pts, in ms */
+    unsigned num_rects;
+    AVSubtitleRect **rects;
+    int64_t pts;    ///< Same as packet pts, in AV_TIME_BASE
+} AVSubtitle;
+
+/**
+ * Allocate an AVSubtitle and set its fields to default values.  The resulting
+ * struct must be freed using av_subtitle_free().
+ *
+ * @return An AVSubtitle filled with default values or NULL on failure.
+ */
+AVSubtitle *av_subtitle_alloc(void);
+
+/**
+ * Reset all the fields to their defaults. This is not useful for most users.
+ */
+void av_subtitle_get_defaults(AVSubtitle *sub);
+
+/**
+ * Free any dynamically allocated objects in the specified AVSubtitle, e.g.
+ * rects.
+ */
+void av_subtitle_clear(AVSubtitle *sub);
+
+/**
+ * Call av_subtitle_clear() and free the subtitle.
+ *
+ * @param sub subtitle to be freed. The pointer will be set to NULL.
+ */
+void av_subtitle_free(AVSubtitle **sub);
+
+#endif
-- 
2.1.3



More information about the ffmpeg-devel mailing list