1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
From: ihy123 <aladinandreyy@gmail.com>
Date: Mon, 18 Aug 2025 03:32:22 +0300
Subject: ip/ffmpeg: improve readability
Previously ffmpeg_read()'s while loop was kinda leaking into
ffmpeg_get_frame(), now it doesn't.
---
ip/ffmpeg.c | 36 ++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)
diff --git a/ip/ffmpeg.c b/ip/ffmpeg.c
index dd9061a..fc74895 100644
--- a/ip/ffmpeg.c
+++ b/ip/ffmpeg.c
@@ -337,30 +337,32 @@ static void ffmpeg_skip_frame_part(struct ffmpeg_private *priv)
/*
* return:
* <0 - error
- * 0 - retry
+ * 0 - eof
* >0 - ok
*/
static int ffmpeg_get_frame(struct ffmpeg_private *priv)
{
- int res = avcodec_receive_frame(priv->codec_ctx, priv->frame);
+ int res;
+retry:
+ res = avcodec_receive_frame(priv->codec_ctx, priv->frame);
if (res == AVERROR(EAGAIN)) {
av_packet_unref(priv->pkt);
res = av_read_frame(priv->format_ctx, priv->pkt);
if (res < 0)
- return res;
+ goto err;
if (priv->pkt->stream_index != priv->stream_index)
- return 0;
+ goto retry;
priv->curr_size += priv->pkt->size;
priv->curr_duration += priv->pkt->duration;
res = avcodec_send_packet(priv->codec_ctx, priv->pkt);
if (res == 0 || res == AVERROR(EAGAIN))
- return 0;
+ goto retry;
}
if (res < 0)
- return res;
+ goto err;
if (priv->seek_ts > 0) {
priv->skip_samples = ffmpeg_calc_skip_samples(priv);
@@ -371,9 +373,14 @@ static int ffmpeg_get_frame(struct ffmpeg_private *priv)
if (priv->skip_samples > 0) {
ffmpeg_skip_frame_part(priv);
if (priv->frame->nb_samples == 0)
- return 0;
+ goto retry;
}
return 1;
+err:
+ if (res == AVERROR_EOF)
+ return 0;
+ d_print("%s\n", ffmpeg_errmsg(res));
+ return -IP_ERROR_INTERNAL;
}
static int ffmpeg_convert_frame(struct ffmpeg_private *priv)
@@ -386,8 +393,10 @@ static int ffmpeg_convert_frame(struct ffmpeg_private *priv)
if (res >= 0) {
priv->swr_frame->nb_samples = res;
priv->swr_frame_start = 0;
+ return res;
}
- return res;
+ d_print("%s\n", ffmpeg_errmsg(res));
+ return -IP_ERROR_INTERNAL;
}
static int ffmpeg_read(struct input_plugin_data *ip_data, char *buffer, int count)
@@ -401,16 +410,14 @@ static int ffmpeg_read(struct input_plugin_data *ip_data, char *buffer, int coun
while (count) {
if (priv->swr_frame->nb_samples == 0) {
res = ffmpeg_get_frame(priv);
- if (res == AVERROR_EOF)
+ if (res == 0)
break;
- else if (res == 0)
- continue;
else if (res < 0)
- goto err;
+ return res;
res = ffmpeg_convert_frame(priv);
if (res < 0)
- goto err;
+ return res;
}
int copy_frames = min_i(count, priv->swr_frame->nb_samples);
@@ -424,9 +431,6 @@ static int ffmpeg_read(struct input_plugin_data *ip_data, char *buffer, int coun
written += copy_bytes;
}
return written;
-err:
- d_print("%s\n", ffmpeg_errmsg(res));
- return -IP_ERROR_INTERNAL;
}
static int ffmpeg_seek(struct input_plugin_data *ip_data, double offset)
|