Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avcodec: insert threads dependent function calls into compile time conditions
@ 2023-01-24 16:07 Pavel Korotkevich
  2023-01-30 11:02 ` Pavel Korotkevich
  0 siblings, 1 reply; 2+ messages in thread
From: Pavel Korotkevich @ 2023-01-24 16:07 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: Pawday <pawday@mail.ru>
---
  libavcodec/avcodec.c | 44 +++++++++++++++++++++++++++++++-------------
  libavcodec/decode.c  | 10 +++++++---
  libavcodec/encode.c  | 19 +++++++++++++------
  libavcodec/h264dec.c | 14 +++++++-------
  4 files changed, 58 insertions(+), 29 deletions(-)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index a85d3c2309..1af8a5937f 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -38,9 +38,16 @@
  #include "codec_internal.h"
  #include "decode.h"
  #include "encode.h"
+
+#if CONFIG_FRAME_THREAD_ENCODER
  #include "frame_thread_encoder.h"
+#endif
+
  #include "internal.h"
+
+#if HAVE_THREADS
  #include "thread.h"
+#endif
   int avcodec_default_execute(AVCodecContext *c, int 
(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, 
int size)
  {
@@ -67,7 +74,7 @@ int avcodec_default_execute2(AVCodecContext *c, int 
(*func)(AVCodecContext *c2,
      emms_c();
      return 0;
  }
-
+#if HAVE_THREADS
  static AVMutex codec_mutex = AV_MUTEX_INITIALIZER;
   static void lock_avcodec(const FFCodec *codec)
@@ -81,6 +88,10 @@ static void unlock_avcodec(const FFCodec *codec)
      if (codec->caps_internal & FF_CODEC_CAP_NOT_INIT_THREADSAFE && 
codec->init)
          ff_mutex_unlock(&codec_mutex);
  }
+#else
+#define lock_avcodec(args)
+#define unlock_avcodec(args)
+#endif
   static int64_t get_bit_rate(AVCodecContext *ctx)
  {
@@ -297,8 +308,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
          ret = ff_decode_preinit(avctx);
      if (ret < 0)
          goto free_and_end;
-
-    if (HAVE_THREADS && !avci->frame_thread_encoder) {
+#if HAVE_THREADS
+    if (!avci->frame_thread_encoder) {
          /* Frame-threaded decoders call FFCodec.init for their child 
contexts. */
          lock_avcodec(codec2);
          ret = ff_thread_init(avctx);
@@ -307,8 +318,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
              goto free_and_end;
          }
      }
-    if (!HAVE_THREADS && !(codec2->caps_internal & 
FF_CODEC_CAP_AUTO_THREADS))
+#endif
+#if !HAVE_THREADS
+    if (!(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
          avctx->thread_count = 1;
+#endif
       if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
          avci->frame_thread_encoder) {
@@ -403,10 +417,13 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
      av_frame_unref(avci->buffer_frame);
      av_packet_unref(avci->buffer_pkt);
  -    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
-        ff_thread_flush(avctx);
-    else if (ffcodec(avctx->codec)->flush)
-        ffcodec(avctx->codec)->flush(avctx);
+    if (!(HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)) {
+        if (ffcodec(avctx->codec)->flush)
+            ffcodec(avctx->codec)->flush(avctx);
+    }
+#if HAVE_THREADS
+    else ff_thread_flush(avctx);
+#endif
  }
   void avsubtitle_free(AVSubtitle *sub)
@@ -440,13 +457,14 @@ av_cold int avcodec_close(AVCodecContext *avctx)
       if (avcodec_is_open(avctx)) {
          AVCodecInternal *avci = avctx->internal;
-
-        if (CONFIG_FRAME_THREAD_ENCODER &&
-            avci->frame_thread_encoder && avctx->thread_count > 1) {
+#if CONFIG_FRAME_THREAD_ENCODER
+        if (avci->frame_thread_encoder && avctx->thread_count > 1)
              ff_frame_thread_encoder_free(avctx);
-        }
-        if (HAVE_THREADS && avci->thread_ctx)
+#endif
+#if HAVE_THREADS
+        if (avci->thread_ctx)
              ff_thread_free(avctx);
+#endif
          if (avci->needs_close && ffcodec(avctx->codec)->close)
              ffcodec(avctx->codec)->close(avctx);
          avci->byte_buffer_size = 0;
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 6be2d3d6ed..2014c90d06 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -47,7 +47,10 @@
  #include "decode.h"
  #include "hwconfig.h"
  #include "internal.h"
+
+#if HAVE_THREADS
  #include "thread.h"
+#endif
   static int apply_param_change(AVCodecContext *avctx, const AVPacket 
*avpkt)
  {
@@ -301,9 +304,7 @@ static inline int 
decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,
       got_frame = 0;
  -    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
-        ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
-    } else {
+    if (!(HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)) {
          ret = codec->cb.decode(avctx, frame, &got_frame, pkt);
           if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
@@ -321,6 +322,9 @@ static inline int 
decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,
              }
          }
      }
+#if HAVE_THREADS
+    else ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
+#endif
      emms_c();
      actual_got_frame = got_frame;
  diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index fbe2c97cd6..35d5bd281c 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -29,7 +29,11 @@
  #include "avcodec.h"
  #include "codec_internal.h"
  #include "encode.h"
+
+#if CONFIG_FRAME_THREAD_ENCODER
  #include "frame_thread_encoder.h"
+#endif
+
  #include "internal.h"
   int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
@@ -270,16 +274,19 @@ static int encode_simple_internal(AVCodecContext 
*avctx, AVPacket *avpkt)
       av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);
  -    if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
-        /* This will unref frame. */
-        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, 
&got_packet);
-    else {
+    if (!(CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)) {
          ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
  #if FF_API_THREAD_SAFE_CALLBACKS
          if (frame)
              av_frame_unref(frame);
  #endif
      }
+#if CONFIG_FRAME_THREAD_ENCODER
+    else {
+        /* This will unref frame. */
+        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, 
&got_packet);
+    }
+#endif
       if (avci->draining && !got_packet)
          avci->draining_done = 1;
@@ -670,11 +677,11 @@ int ff_encode_preinit(AVCodecContext *avctx)
              return AVERROR(ENOMEM);
      }
  -    if (CONFIG_FRAME_THREAD_ENCODER) {
+#if CONFIG_FRAME_THREAD_ENCODER
          ret = ff_frame_thread_encoder_init(avctx);
          if (ret < 0)
              return ret;
-    }
+#endif
       return 0;
  }
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 6ede4e8c9f..4538974dab 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -933,13 +933,13 @@ static int finalize_frame(H264Context *h, AVFrame 
*dst, H264Picture *out, int *g
           *got_frame = 1;
  -        if (CONFIG_MPEGVIDEODEC) {
-            ff_print_debug_info2(h->avctx, dst, NULL,
-                                 out->mb_type,
-                                 out->qscale_table,
-                                 out->motion_val,
-                                 out->mb_width, out->mb_height, 
out->mb_stride, 1);
-        }
+#if CONFIG_MPEGVIDEODEC
+        ff_print_debug_info2(h->avctx, dst, NULL,
+                             out->mb_type,
+                             out->qscale_table,
+                             out->motion_val,
+                             out->mb_width, out->mb_height, 
out->mb_stride, 1);
+#endif
      }
       return 0;
-- 
2.39.0.windows.2

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avcodec: insert threads dependent function calls into compile time conditions
  2023-01-24 16:07 [FFmpeg-devel] [PATCH] avcodec: insert threads dependent function calls into compile time conditions Pavel Korotkevich
@ 2023-01-30 11:02 ` Pavel Korotkevich
  0 siblings, 0 replies; 2+ messages in thread
From: Pavel Korotkevich @ 2023-01-30 11:02 UTC (permalink / raw)
  To: ffmpeg-devel


---

  libavcodec/avcodec.c | 36 +++++++++++++++++++++++++-----------
  libavcodec/decode.c  |  9 ++++++---
  libavcodec/encode.c  | 19 +++++++++++++------
  libavcodec/h264dec.c | 14 +++++++-------
  4 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index efa76d2740..11c5bdb8d9 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -38,7 +38,11 @@
  #include "codec_internal.h"
  #include "decode.h"
  #include "encode.h"
+
+#if CONFIG_FRAME_THREAD_ENCODER
  #include "frame_thread_encoder.h"
+#endif
+
  #include "internal.h"
  #include "thread.h"

@@ -297,8 +301,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
          ret = ff_decode_preinit(avctx);
      if (ret < 0)
          goto free_and_end;
-
-    if (HAVE_THREADS && !avci->frame_thread_encoder) {
+#if HAVE_THREADS
+    if (!avci->frame_thread_encoder) {
          /* Frame-threaded decoders call FFCodec.init for their child 
contexts. */
          lock_avcodec(codec2);
          ret = ff_thread_init(avctx);
@@ -307,8 +311,12 @@ FF_ENABLE_DEPRECATION_WARNINGS
              goto free_and_end;
          }
      }
-    if (!HAVE_THREADS && !(codec2->caps_internal & 
FF_CODEC_CAP_AUTO_THREADS))
+#endif
+
+#if !HAVE_THREADS
+    if (!(codec2->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
          avctx->thread_count = 1;
+#endif

      if (!(avctx->active_thread_type & FF_THREAD_FRAME) ||
          avci->frame_thread_encoder) {
@@ -400,10 +408,15 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
      av_frame_unref(avci->buffer_frame);
      av_packet_unref(avci->buffer_pkt);

-    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
+    if (!(HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)) {
+        if (ffcodec(avctx->codec)->flush)
+            ffcodec(avctx->codec)->flush(avctx);
+    }
+#if HAVE_THREADS
+    else {
          ff_thread_flush(avctx);
-    else if (ffcodec(avctx->codec)->flush)
-        ffcodec(avctx->codec)->flush(avctx);
+    }
+#endif
  }

  void avsubtitle_free(AVSubtitle *sub)
@@ -437,13 +450,14 @@ av_cold int avcodec_close(AVCodecContext *avctx)

      if (avcodec_is_open(avctx)) {
          AVCodecInternal *avci = avctx->internal;
-
-        if (CONFIG_FRAME_THREAD_ENCODER &&
-            avci->frame_thread_encoder && avctx->thread_count > 1) {
+#if CONFIG_FRAME_THREAD_ENCODER
+        if (avci->frame_thread_encoder && avctx->thread_count > 1)
              ff_frame_thread_encoder_free(avctx);
-        }
-        if (HAVE_THREADS && avci->thread_ctx)
+#endif
+#if HAVE_THREADS
+        if (avci->thread_ctx)
              ff_thread_free(avctx);
+#endif
          if (avci->needs_close && ffcodec(avctx->codec)->close)
              ffcodec(avctx->codec)->close(avctx);
          avci->byte_buffer_size = 0;
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 0abc88737b..df93195414 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -279,9 +279,7 @@ static inline int 
decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,

      got_frame = 0;

-    if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) {
-        ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
-    } else {
+    if (!(HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)) {
          ret = codec->cb.decode(avctx, frame, &got_frame, pkt);

          if (!(codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
@@ -299,6 +297,11 @@ static inline int 
decode_simple_internal(AVCodecContext *avctx, AVFrame *frame,
              }
          }
      }
+#if HAVE_THREADS
+    else {
+        ret = ff_thread_decode_frame(avctx, frame, &got_frame, pkt);
+    }
+#endif
      emms_c();
      actual_got_frame = got_frame;

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index fbe2c97cd6..35d5bd281c 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -29,7 +29,11 @@
  #include "avcodec.h"
  #include "codec_internal.h"
  #include "encode.h"
+
+#if CONFIG_FRAME_THREAD_ENCODER
  #include "frame_thread_encoder.h"
+#endif
+
  #include "internal.h"

  int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
@@ -270,16 +274,19 @@ static int encode_simple_internal(AVCodecContext 
*avctx, AVPacket *avpkt)

      av_assert0(codec->cb_type == FF_CODEC_CB_TYPE_ENCODE);

-    if (CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)
-        /* This will unref frame. */
-        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, 
&got_packet);
-    else {
+    if (!(CONFIG_FRAME_THREAD_ENCODER && avci->frame_thread_encoder)) {
          ret = ff_encode_encode_cb(avctx, avpkt, frame, &got_packet);
  #if FF_API_THREAD_SAFE_CALLBACKS
          if (frame)
              av_frame_unref(frame);
  #endif
      }
+#if CONFIG_FRAME_THREAD_ENCODER
+    else {
+        /* This will unref frame. */
+        ret = ff_thread_video_encode_frame(avctx, avpkt, frame, 
&got_packet);
+    }
+#endif

      if (avci->draining && !got_packet)
          avci->draining_done = 1;
@@ -670,11 +677,11 @@ int ff_encode_preinit(AVCodecContext *avctx)
              return AVERROR(ENOMEM);
      }

-    if (CONFIG_FRAME_THREAD_ENCODER) {
+#if CONFIG_FRAME_THREAD_ENCODER
          ret = ff_frame_thread_encoder_init(avctx);
          if (ret < 0)
              return ret;
-    }
+#endif

      return 0;
  }
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 9f7b3782e8..b3f3f383c8 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -933,13 +933,13 @@ static int finalize_frame(H264Context *h, AVFrame 
*dst, H264Picture *out, int *g

          *got_frame = 1;

-        if (CONFIG_MPEGVIDEODEC) {
-            ff_print_debug_info2(h->avctx, dst, NULL,
-                                 out->mb_type,
-                                 out->qscale_table,
-                                 out->motion_val,
-                                 out->mb_width, out->mb_height, 
out->mb_stride, 1);
-        }
+#if CONFIG_MPEGVIDEODEC
+        ff_print_debug_info2(h->avctx, dst, NULL,
+                             out->mb_type,
+                             out->qscale_table,
+                             out->motion_val,
+                             out->mb_width, out->mb_height, 
out->mb_stride, 1);
+#endif
      }

      return 0;
-- 
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-30 11:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-24 16:07 [FFmpeg-devel] [PATCH] avcodec: insert threads dependent function calls into compile time conditions Pavel Korotkevich
2023-01-30 11:02 ` Pavel Korotkevich

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