Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 17/23] fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c
Date: Sat, 25 Mar 2023 20:15:23 +0100
Message-ID: <20230325191529.10578-17-anton@khirnov.net> (raw)
In-Reply-To: <20230325191529.10578-1-anton@khirnov.net>

---
 fftools/ffmpeg.c     | 86 ++------------------------------------------
 fftools/ffmpeg.h     |  3 ++
 fftools/ffmpeg_enc.c | 80 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 84 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0324e45afa..bc2a9efbc1 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -669,7 +669,7 @@ static void close_output_stream(OutputStream *ost)
         sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
 }
 
-static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
+int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
 {
     OutputFile *of = output_files[ost->file_index];
 
@@ -1027,88 +1027,6 @@ static void do_audio_out(OutputFile *of, OutputStream *ost,
         exit_program(1);
 }
 
-static void do_subtitle_out(OutputFile *of,
-                            OutputStream *ost,
-                            AVSubtitle *sub)
-{
-    int subtitle_out_max_size = 1024 * 1024;
-    int subtitle_out_size, nb, i, ret;
-    AVCodecContext *enc;
-    AVPacket *pkt = ost->pkt;
-    int64_t pts;
-
-    if (sub->pts == AV_NOPTS_VALUE) {
-        av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
-        if (exit_on_error)
-            exit_program(1);
-        return;
-    }
-
-    enc = ost->enc_ctx;
-
-    /* Note: DVB subtitle need one packet to draw them and one other
-       packet to clear them */
-    /* XXX: signal it in the codec context ? */
-    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
-        nb = 2;
-    else if (enc->codec_id == AV_CODEC_ID_ASS)
-        nb = FFMAX(sub->num_rects, 1);
-    else
-        nb = 1;
-
-    /* shift timestamp to honor -ss and make check_recording_time() work with -t */
-    pts = sub->pts;
-    if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
-        pts -= output_files[ost->file_index]->start_time;
-    for (i = 0; i < nb; i++) {
-        AVSubtitle local_sub = *sub;
-
-        if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
-            return;
-
-        ret = av_new_packet(pkt, subtitle_out_max_size);
-        if (ret < 0)
-            report_and_exit(AVERROR(ENOMEM));
-
-        local_sub.pts = pts;
-        // start_display_time is required to be 0
-        local_sub.pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
-        local_sub.end_display_time  -= sub->start_display_time;
-        local_sub.start_display_time = 0;
-
-        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
-            local_sub.num_rects = 0;
-        else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
-            local_sub.num_rects = 1;
-            local_sub.rects += i;
-        }
-
-        ost->frames_encoded++;
-
-        subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
-        if (subtitle_out_size < 0) {
-            av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
-            exit_program(1);
-        }
-
-        av_shrink_packet(pkt, subtitle_out_size);
-        pkt->time_base = ost->mux_timebase;
-        pkt->pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, pkt->time_base);
-        pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
-        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(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
-            else
-                pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
-        }
-        pkt->dts = pkt->pts;
-
-        of_output_packet(of, pkt, ost, 0);
-    }
-}
-
 /* Convert frame timestamps to the encoder timebase and decide how many times
  * should this (and possibly previous) frame be repeated in order to conform to
  * desired target framerate (if any).
@@ -2351,7 +2269,7 @@ static int process_subtitle(InputStream *ist, AVSubtitle *subtitle, int *got_out
             || ost->enc_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)
             continue;
 
-        do_subtitle_out(output_files[ost->file_index], ost, subtitle);
+        enc_subtitle(output_files[ost->file_index], ost, subtitle);
     }
 
 out:
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c1e2bbc300..ffb0ca33ac 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -811,6 +811,9 @@ AVBufferRef *hw_device_for_filter(void);
 int hwaccel_decode_init(AVCodecContext *avctx);
 
 int enc_open(OutputStream *ost, AVFrame *frame);
+void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub);
+
+int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb);
 
 /*
  * Initialize muxing state for the given stream, should be called
diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c
index a4660051a2..bcc560b9b7 100644
--- a/fftools/ffmpeg_enc.c
+++ b/fftools/ffmpeg_enc.c
@@ -347,3 +347,83 @@ int enc_open(OutputStream *ost, AVFrame *frame)
 
     return 0;
 }
+
+void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub)
+{
+    int subtitle_out_max_size = 1024 * 1024;
+    int subtitle_out_size, nb, i, ret;
+    AVCodecContext *enc;
+    AVPacket *pkt = ost->pkt;
+    int64_t pts;
+
+    if (sub->pts == AV_NOPTS_VALUE) {
+        av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
+        if (exit_on_error)
+            exit_program(1);
+        return;
+    }
+
+    enc = ost->enc_ctx;
+
+    /* Note: DVB subtitle need one packet to draw them and one other
+       packet to clear them */
+    /* XXX: signal it in the codec context ? */
+    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
+        nb = 2;
+    else if (enc->codec_id == AV_CODEC_ID_ASS)
+        nb = FFMAX(sub->num_rects, 1);
+    else
+        nb = 1;
+
+    /* shift timestamp to honor -ss and make check_recording_time() work with -t */
+    pts = sub->pts;
+    if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
+        pts -= output_files[ost->file_index]->start_time;
+    for (i = 0; i < nb; i++) {
+        AVSubtitle local_sub = *sub;
+
+        if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
+            return;
+
+        ret = av_new_packet(pkt, subtitle_out_max_size);
+        if (ret < 0)
+            report_and_exit(AVERROR(ENOMEM));
+
+        local_sub.pts = pts;
+        // start_display_time is required to be 0
+        local_sub.pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
+        local_sub.end_display_time  -= sub->start_display_time;
+        local_sub.start_display_time = 0;
+
+        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
+            local_sub.num_rects = 0;
+        else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
+            local_sub.num_rects = 1;
+            local_sub.rects += i;
+        }
+
+        ost->frames_encoded++;
+
+        subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
+        if (subtitle_out_size < 0) {
+            av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
+            exit_program(1);
+        }
+
+        av_shrink_packet(pkt, subtitle_out_size);
+        pkt->time_base = ost->mux_timebase;
+        pkt->pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, pkt->time_base);
+        pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
+        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(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
+            else
+                pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
+        }
+        pkt->dts = pkt->pts;
+
+        of_output_packet(of, pkt, ost, 0);
+    }
+}
-- 
2.39.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2023-03-25 19:17 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-25 19:15 [FFmpeg-devel] [PATCH 01/23] fftools/ffmpeg: drop InputStream.processing_needed Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 02/23] fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams() Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 03/23] fftools/sync_queue: use timebase from input frames/packets Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 04/23] fftools/sync_queue: document overall design Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 05/23] fftools/sync_queue: support operation with no limiting streams Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 06/23] fftools/sync_queue: make sure audio duration matches sample count Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 07/23] fftools/sync_queue: allow requesting a specific number of audio samples Anton Khirnov
2023-03-29 23:41   ` James Almer
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Anton Khirnov
2023-03-25 21:43   ` Michael Niedermayer
2023-03-27  5:15     ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: do not return finished streams from choose_output() Anton Khirnov
2023-03-29 17:59       ` Michael Niedermayer
2023-03-30  8:48         ` Anton Khirnov
2023-04-02 15:58           ` Michael Niedermayer
2023-04-03 10:09             ` [FFmpeg-devel] [PATCH] fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF Anton Khirnov
2023-04-05 22:33               ` Michael Niedermayer
2023-04-06  7:27                 ` Anton Khirnov
2023-03-29 18:08     ` [FFmpeg-devel] [PATCH 08/23] fftools/ffmpeg: use sync queues for enforcing audio frame size Michael Niedermayer
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 09/23] fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode() Anton Khirnov
2023-03-25 19:43   ` James Almer
2023-03-26  9:20     ` Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 10/23] fftools/ffmpeg: drop unnecessarily indirection Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 11/23] fftools/ffmpeg: use stack variables to shorten code Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 12/23] fftools/ffmpeg: move encoder initialization to init_output_stream_encode Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 13/23] fftools/ffmpeg: reindent after previous commit Anton Khirnov
2023-03-28 22:42   ` Michael Niedermayer
2023-03-29  0:16     ` Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 14/23] fftools/ffmpeg: move initializing encoders to a new file Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 15/23] fftools/ffmpeg: simplify output stream initialization call graph Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 16/23] fftools/ffmpeg: replace ff_dlog() with av_log() Anton Khirnov
2023-03-25 19:15 ` Anton Khirnov [this message]
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 18/23] fftools/ffmpeg: move audio/video encoding code to ffmpeg_enc.c Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 19/23] fftools/ffmpeg: add encoder private data Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 20/23] fftools/ffmpeg: stop including os_support.h Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 21/23] fftools/ffmpeg: clean up system header includes Anton Khirnov
2023-03-27  5:35   ` Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 22/23] fftools/ffmpeg: clean up local includes Anton Khirnov
2023-03-25 19:15 ` [FFmpeg-devel] [PATCH 23/23] fftools/ffmpeg_enc: factorize calling enc_init() Anton Khirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230325191529.10578-17-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git