[FFmpeg-devel] [PATCH v3] libavcodec/utils: add avcodec_encode_subtitle2 and mark subtitle packets with FLAG_KEY

Aman Gupta ffmpeg at tmm1.net
Wed May 28 02:21:49 CEST 2014


Took a stab at implementing avcodec_encode_subtitle2 which takes an AVPacket argument
and sets AV_PKT_FLAG_KEY on the generated packets.

Signed-off-by: Aman Gupta <ffmpeg at tmm1.net>
---
 configure            |  1 +
 ffmpeg.c             | 30 +++++++++++++-----------------
 libavcodec/avcodec.h |  1 +
 libavcodec/utils.c   | 17 +++++++++++++++++
 4 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/configure b/configure
index a261fd6..b346f9d 100755
--- a/configure
+++ b/configure
@@ -4896,6 +4896,7 @@ enabled neon_clobber_test &&
                   -Wl,--wrap,avcodec_encode_audio2      \
                   -Wl,--wrap,avcodec_encode_video2      \
                   -Wl,--wrap,avcodec_encode_subtitle    \
+                  -Wl,--wrap,avcodec_encode_subtitle2   \
                   -Wl,--wrap,swr_convert                \
                   -Wl,--wrap,avresample_convert ||
     disable neon_clobber_test
diff --git a/ffmpeg.c b/ffmpeg.c
index a0e2be2..0b02e72 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -131,8 +131,6 @@ static int64_t decode_error_stat[2];
 static int current_time;
 AVIOContext *progress_avio = NULL;
 
-static uint8_t *subtitle_out;
-
 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
 
 InputStream **input_streams = NULL;
@@ -452,8 +450,6 @@ static void ffmpeg_cleanup(int ret)
     }
     av_freep(&filtergraphs);
 
-    av_freep(&subtitle_out);
-
     /* close files */
     for (i = 0; i < nb_output_files; i++) {
         OutputFile *of = output_files[i];
@@ -763,8 +759,7 @@ static void do_subtitle_out(AVFormatContext *s,
                             InputStream *ist,
                             AVSubtitle *sub)
 {
-    int subtitle_out_max_size = 1024 * 1024;
-    int subtitle_out_size, nb, i;
+    int ret, nb, i;
     AVCodecContext *enc;
     AVPacket pkt;
     int64_t pts;
@@ -778,10 +773,6 @@ static void do_subtitle_out(AVFormatContext *s,
 
     enc = ost->st->codec;
 
-    if (!subtitle_out) {
-        subtitle_out = av_malloc(subtitle_out_max_size);
-    }
-
     /* Note: DVB subtitle need one packet to draw them and one other
        packet to clear them */
     /* XXX: signal it in the codec context ? */
@@ -809,18 +800,19 @@ static void do_subtitle_out(AVFormatContext *s,
 
         ost->frames_encoded++;
 
-        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
-                                                    subtitle_out_max_size, sub);
-        if (subtitle_out_size < 0) {
+        av_init_packet(&pkt);
+        pkt.data = NULL;
+        pkt.size = 0;
+        ret = avcodec_encode_subtitle2(enc, &pkt, sub);
+        if (ret < 0) {
             av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
             exit_program(1);
         }
 
-        av_init_packet(&pkt);
-        pkt.data = subtitle_out;
-        pkt.size = subtitle_out_size;
         pkt.pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
         pkt.duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->st->time_base);
+        pkt.dts = pkt.pts;
+
         if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
             /* XXX: the pts correction is handled here. Maybe handling
                it in the codec would be better */
@@ -829,8 +821,12 @@ static void do_subtitle_out(AVFormatContext *s,
             else
                 pkt.pts += 90 * sub->end_display_time;
         }
-        pkt.dts = pkt.pts;
+
         write_frame(s, &pkt, ost);
+        if (pkt.data) {
+            av_freep(pkt.data);
+            pkt.data = NULL;
+        }
     }
 }
 
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 5bff631..d5c89ba 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -4500,6 +4500,7 @@ int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt,
 int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
                             const AVSubtitle *sub);
 
+int avcodec_encode_subtitle2(AVCodecContext *avctx, AVPacket *avpkt, const AVSubtitle *sub);
 
 /**
  * @}
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 75e07dc..01c9843 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -2071,6 +2071,23 @@ int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
     return ret;
 }
 
+int avcodec_encode_subtitle2(AVCodecContext *avctx, AVPacket *avpkt, const AVSubtitle *sub)
+{
+    int ret;
+    int subtitle_out_max_size = 1024 * 1024;
+    uint8_t *buf = av_malloc(subtitle_out_max_size);
+
+    ret = avcodec_encode_subtitle(avctx, buf, subtitle_out_max_size, sub);
+    if (ret < 0)
+        return ret;
+
+    avpkt->flags = AV_PKT_FLAG_KEY;
+    avpkt->data = buf;
+    avpkt->size = ret;
+
+    return ret;
+}
+
 /**
  * Attempt to guess proper monotonic timestamps for decoded video frames
  * which might have incorrect times. Input timestamps may wrap around, in
-- 
1.9.1



More information about the ffmpeg-devel mailing list