From patchwork Sat Mar 25 19:15:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40829 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:7a30:b0:df:834d:2c1a with SMTP id t48csp225963pzh; Sat, 25 Mar 2023 12:17:43 -0700 (PDT) X-Google-Smtp-Source: AKy350bnjf5riNqZw5097jaY2o7FZAokC0nVqxKI+pmHato+iB3tNTaJQVklVKQDA9hN89UFAA/R X-Received: by 2002:aa7:c94c:0:b0:4fc:eee9:417 with SMTP id h12-20020aa7c94c000000b004fceee90417mr6474872edt.18.1679771863146; Sat, 25 Mar 2023 12:17:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679771863; cv=none; d=google.com; s=arc-20160816; b=dLsx/kP8zYQO7qepPJjrVy9iUnZZx4J/pw6nHQh3gPQTQxrqVfMFw6oFWAJ99qyIrX wG5Ee9p4UHWIgIlCttdnWYZ+oU2Kd2q+6vbUqM4gtaqW0Ku/6MBfwdi/edqLe3pzFokU BFoT2vhhHacDlgoZzWOZIAzmexMTTVBbeYm/cRYLz8ToeNOrsPUxOR0ieCTQLNL3d7Yz 6HtRSL4kWBoggoWNYT6rhZoV4CNQS3cW1OgipAo/BFK3OwZ6sG/UZ+6NQucM5ulSgN++ R/HFepymvkeOjdqHRiPg4aWk7EK0B0D6si1vxe66BcqAlmc/UU3ZXa7lFuXRLuDl5FNm 6LQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=JAes3NwA3lCHW08OG8bPs1siaoPtO1/FSawAamOGXUk=; b=xIu+pHrnWxtKEBrn30/QvWBwhYRX769QR/jZtB70kPzsl34XNk02fgzwpwCKXdz0lZ CiTdiD69ZM85et6GLTnF6OGL0vBXTO+Are70k7h3w1MAttccKEKW3220AuR+154SwDM6 SN0Pg/hiLAsntT3kmlzSyUiPlrMqHEl0FGTtYkBzdc4FZ13bxFbtsYE52VBKTACcY9id iJM7GKL2u4eV7KIHsNZditIAfzJj3D+78GSb8i/CCymc3xe4D6N+cI5PzNGRG+k4SA00 xHE75kuDo+SHaTQfdjIzNXPT8If/KsJj55fTZxDJgoWDaDYvs1mN1GSbjqARttvAjUpQ 9zew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j8-20020a50ed08000000b0050230e4b487si1881466eds.13.2023.03.25.12.17.42; Sat, 25 Mar 2023 12:17:43 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7401E68C9A3; Sat, 25 Mar 2023 21:16:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D8FB668C810 for ; Sat, 25 Mar 2023 21:16:10 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9EB1D2404EE for ; Sat, 25 Mar 2023 20:16:10 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id nWPGG_ocLiYs for ; Sat, 25 Mar 2023 20:16:09 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 493D9240D1A for ; Sat, 25 Mar 2023 20:16:01 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 1DAB23A0786 for ; Sat, 25 Mar 2023 20:15:55 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Mar 2023 20:15:23 +0100 Message-Id: <20230325191529.10578-17-anton@khirnov.net> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230325191529.10578-1-anton@khirnov.net> References: <20230325191529.10578-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 17/23] fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: vuE7ohcvKLHZ --- 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); + } +}