diff mbox series

[FFmpeg-devel,v15,16/16] fftools/ffmpeg: Use new frame-based subtitle encoding API

Message ID DM8P223MB0365E311AC0E804AF797D85EBA629@DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM
State Not Applicable, archived
Headers show
Series Untitled series #5317
Related show

Commit Message

Soft Works Nov. 25, 2021, 5:52 p.m. UTC
Signed-off-by: softworkz <softworkz@hotmail.com>
---
 fftools/ffmpeg.c | 60 ++++++++++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 30 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c697c12777..603b4c23e0 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -896,10 +896,9 @@  error:
 static void do_subtitle_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
 {
     const int subtitle_out_max_size = 1024 * 1024;
-    int subtitle_out_size, nb, i;
+    int nb, i;
     AVCodecContext *enc;
     AVPacket *pkt = ost->pkt;
-    AVSubtitle out_sub = { 0 };
     int64_t pts;
 
     if (!frame)
@@ -917,12 +916,14 @@  static void do_subtitle_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
     enc = ost->enc_ctx;
 
     if (!subtitle_out) {
-        subtitle_out = av_malloc(subtitle_out_max_size);
+        subtitle_out = av_mallocz(subtitle_out_max_size);
         if (!subtitle_out) {
             av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n");
             exit_program(1);
         }
     }
+    else
+        memset(subtitle_out, 0, subtitle_out_max_size);
 
     /* Note: DVB subtitle need one packet to draw them and one other
        packet to clear them */
@@ -947,44 +948,43 @@  static void do_subtitle_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
     frame->subtitle_end_time  -= frame->subtitle_start_time;
     frame->subtitle_start_time = 0;
 
-    av_frame_get_subtitle(&out_sub, frame);
-
     for (i = 0; i < nb; i++) {
-        const unsigned save_num_rects = out_sub.num_rects;
+        int ret, got_packet = 0;
+        const unsigned save_num_rects = frame->num_subtitle_areas;
+
+        pkt->data = subtitle_out;
+        pkt->size = subtitle_out_max_size;
 
         ost->frames_encoded++;
 
         if (i == 1)
-            out_sub.num_rects = 0;
+            frame->num_subtitle_areas = 0;
 
-        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out, subtitle_out_max_size, &out_sub);
+        ret = avcodec_encode_subtitle2(enc, pkt, frame, &got_packet);
 
         if (i == 1)
-            out_sub.num_rects = save_num_rects;
+            frame->num_subtitle_areas = save_num_rects;
 
-        if (subtitle_out_size < 0) {
-            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
-            exit_program(1);
-        }
-
-        //av_packet_unref(pkt);
-        pkt->data = subtitle_out;
-        pkt->size = subtitle_out_size;
-        pkt->pts  = av_rescale_q(frame->subtitle_pts, AV_TIME_BASE_Q, ost->mux_timebase);
-        pkt->duration = av_rescale_q(frame->subtitle_end_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
-        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 */
-            if (i == 0)
-                pkt->pts += av_rescale_q(frame->subtitle_start_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
-            else
-                pkt->pts += av_rescale_q(frame->subtitle_end_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed: %d\n", ret);
+            exit_program(ret);
+        }
+
+        if (got_packet) {
+            pkt->pts  = av_rescale_q(frame->subtitle_pts, AV_TIME_BASE_Q, ost->mux_timebase);
+            pkt->duration = av_rescale_q(frame->subtitle_end_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
+            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 */
+                if (i == 0)
+                    pkt->pts += av_rescale_q(frame->subtitle_start_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
+                else
+                    pkt->pts += av_rescale_q(frame->subtitle_end_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
+            }
+            pkt->dts = pkt->pts;
+            output_packet(of, pkt, ost, 0);
         }
-        pkt->dts = pkt->pts;
-        output_packet(of, pkt, ost, 0);
     }
-
-    avsubtitle_free(&out_sub);
 }
 
 static void do_video_out(OutputFile *of,