diff mbox series

[FFmpeg-devel,2/3] fftools/ffmpeg_demux: factorize the bitstream filtering loop

Message ID 20240924144308.1196-2-jamrial@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/3] avcodec: add an LCEVC merger bsf | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

James Almer Sept. 24, 2024, 2:43 p.m. UTC
Will be needed by the following patch.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 fftools/ffmpeg_demux.c | 74 ++++++++++++++++++++++++------------------
 1 file changed, 43 insertions(+), 31 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 13aef15eab..108a4a94bf 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -537,6 +537,47 @@  static int do_send(Demuxer *d, DemuxStream *ds, AVPacket *pkt, unsigned flags,
     return 0;
 }
 
+static int demux_filter(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
+                        AVBSFContext *bsf, AVPacket *pkt, void *logctx)
+{
+    int ret;
+
+    if (pkt)
+        av_packet_rescale_ts(pkt, pkt->time_base, bsf->time_base_in);
+
+    ret = av_bsf_send_packet(bsf, pkt);
+    if (ret < 0) {
+        if (pkt)
+            av_packet_unref(pkt);
+        av_log(logctx, AV_LOG_ERROR, "Error submitting a packet for filtering: %s\n",
+               av_err2str(ret));
+        return ret;
+    }
+
+    while (1) {
+        ret = av_bsf_receive_packet(bsf, dt->pkt_bsf);
+        if (ret == AVERROR(EAGAIN))
+            return 0;
+        else if (ret < 0) {
+            if (ret != AVERROR_EOF)
+                av_log(logctx, AV_LOG_ERROR,
+                       "Error applying bitstream filters to a packet: %s\n",
+                       av_err2str(ret));
+            break;
+        }
+
+        dt->pkt_bsf->time_base = bsf->time_base_out;
+
+        ret = do_send(d, ds, dt->pkt_bsf, 0, "filtered");
+        if (ret < 0) {
+            av_packet_unref(dt->pkt_bsf);
+            break;
+        }
+    }
+
+    return ret;
+}
+
 static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
                       AVPacket *pkt, unsigned flags)
 {
@@ -565,38 +606,9 @@  static int demux_send(Demuxer *d, DemuxThreadContext *dt, DemuxStream *ds,
     }
 
     if (ds->bsf) {
-        if (pkt)
-            av_packet_rescale_ts(pkt, pkt->time_base, ds->bsf->time_base_in);
-
-        ret = av_bsf_send_packet(ds->bsf, pkt);
-        if (ret < 0) {
-            if (pkt)
-                av_packet_unref(pkt);
-            av_log(ds, AV_LOG_ERROR, "Error submitting a packet for filtering: %s\n",
-                   av_err2str(ret));
+        ret = demux_filter(d, dt, ds, ds->bsf, pkt, ds);
+        if (ret < 0)
             return ret;
-        }
-
-        while (1) {
-            ret = av_bsf_receive_packet(ds->bsf, dt->pkt_bsf);
-            if (ret == AVERROR(EAGAIN))
-                return 0;
-            else if (ret < 0) {
-                if (ret != AVERROR_EOF)
-                    av_log(ds, AV_LOG_ERROR,
-                           "Error applying bitstream filters to a packet: %s\n",
-                           av_err2str(ret));
-                return ret;
-            }
-
-            dt->pkt_bsf->time_base = ds->bsf->time_base_out;
-
-            ret = do_send(d, ds, dt->pkt_bsf, 0, "filtered");
-            if (ret < 0) {
-                av_packet_unref(dt->pkt_bsf);
-                return ret;
-            }
-        }
     } else {
         ret = do_send(d, ds, pkt, flags, "demuxed");
         if (ret < 0)