changeset 6844:dbebcd49317c streaming

avoid unnecessary conversions in TranscoderDataSource
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 22 May 2026 09:34:01 +0200
parents 749a14809ad1
children 4444d7994ba5
files OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp OrthancFramework/Sources/DataSource/TranscoderDataSource.h OrthancFramework/Sources/DicomParsing/IDicomTranscoder.cpp OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h OrthancFramework/Sources/MultiThreading/Mutex.h OrthancServer/Sources/ServerTranscoder.cpp
diffstat 6 files changed, 178 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp	Fri May 22 07:53:32 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp	Fri May 22 09:34:01 2026 +0200
@@ -53,30 +53,78 @@
   class TranscoderDataSource::Value : public IDynamicObject
   {
   private:
-    std::unique_ptr<ParsedDicomFile>  dicom_;
+    std::unique_ptr<ParsedDicomFile>  parsed_;
+    std::unique_ptr<std::string>      raw_;
     size_t                            size_;
 
   public:
-    Value(ParsedDicomFile* dicom,
-          size_t size) :
-      dicom_(dicom),
-      size_(size)
+    Value(IDicomTranscoder::DicomImage* transcoded)
     {
-      if (dicom == NULL)
+      std::unique_ptr<IDicomTranscoder::DicomImage> protection(transcoded);
+
+      if (transcoded == NULL)
       {
         throw OrthancException(ErrorCode_NullPointer);
       }
-    }
+
+      if (transcoded->HasParsed() &&
+          !transcoded->HasInternalBuffer())
+      {
+        parsed_.reset(transcoded->ReleaseAsParsedDicomFile());
 
-    ParsedDicomFile& GetContent() const
-    {
-      return *dicom_;
+        DcmFileFormat& f = parsed_->GetDcmtkObject();
+        size_ = f.calcElementLength(f.getDataset()->getOriginalXfer(), EET_ExplicitLength);
+      }
+      else if (!transcoded->HasParsed() &&
+               transcoded->HasInternalBuffer())
+      {
+        raw_.reset(transcoded->ReleaseInternalBuffer());
+        size_ = raw_->size();
+      }
+      else
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
 
     size_t GetSize() const
     {
       return size_;
     }
+
+    bool HasParsed() const
+    {
+      return parsed_.get() != NULL;
+    }
+
+    bool HasRawBuffer() const
+    {
+      return raw_.get() != NULL;
+    }
+
+    ParsedDicomFile& GetParsed() const
+    {
+      if (parsed_.get() == NULL)
+      {
+        throw OrthancException(ErrorCode_BadSequenceOfCalls);
+      }
+      else
+      {
+        return *parsed_;
+      }
+    }
+
+    const std::string& GetRawBuffer() const
+    {
+      if (raw_.get() == NULL)
+      {
+        throw OrthancException(ErrorCode_BadSequenceOfCalls);
+      }
+      else
+      {
+        return *raw_;
+      }
+    }
   };
 
 
@@ -141,7 +189,7 @@
       IDicomTranscoder::DicomImage source;
       source.SetExternalBuffer(range->GetData(), range->GetSize());
 
-      IDicomTranscoder::DicomImage target;
+      std::unique_ptr<IDicomTranscoder::DicomImage> target;
 
       std::set<DicomTransferSyntax> allowedSyntaxes;
       allowedSyntaxes.insert(targetSyntax_);
@@ -149,17 +197,16 @@
       bool success;
       if (hasLossyQuality_)
       {
-        success = transcoder.Transcode(target, source, allowedSyntaxes, mode_, lossyQuality_);
+        success = transcoder.Transcode(*target, source, allowedSyntaxes, mode_, lossyQuality_);
       }
       else
       {
-        success = transcoder.Transcode(target, source, allowedSyntaxes, mode_);
+        success = transcoder.Transcode(*target, source, allowedSyntaxes, mode_);
       }
 
       if (success)
       {
-        const size_t size = target.GetBufferSize();
-        return new Value(target.ReleaseAsParsedDicomFile(), size);
+        return new Value(target.release());
       }
       else
       {
@@ -204,9 +251,48 @@
   }
 
 
-  const ParsedDicomFile& TranscoderDataSource::Transcoded::Lock::GetDicom() const
+  TranscoderDataSource::Transcoded::LockAsParsed::LockAsParsed(Transcoded& that) :
+    lock_(that.mutex_)
   {
-    return dynamic_cast<const Value&>(*that_.value_).GetContent();
+    const Value& value = dynamic_cast<const Value&>(*that.value_);
+
+    if (value.HasParsed())
+    {
+      // Don't release the lock here
+      content_ = &value.GetParsed();
+    }
+    else
+    {
+      assert(value.HasRawBuffer());
+      parsed_.reset(new ParsedDicomFile(value.GetRawBuffer()));
+      lock_.Unlock();
+
+      content_ = parsed_.get();
+    }
+
+    assert(content_ != NULL);
+  }
+
+
+  TranscoderDataSource::Transcoded::LockAsBuffer::LockAsBuffer(Transcoded& that)
+  {
+    Mutex::ScopedLock lock(that.mutex_);
+
+    const Value& value = dynamic_cast<const Value&>(*that.value_);
+
+    if (value.HasParsed())
+    {
+      serialized_.reset(new std::string);
+      value.GetParsed().SaveToMemoryBuffer(*serialized_);
+      content_ = serialized_.get();
+    }
+    else
+    {
+      assert(value.HasRawBuffer());
+      content_ = &value.GetRawBuffer();
+    }
+
+    assert(content_ != NULL);
   }
 
 
--- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.h	Fri May 22 07:53:32 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.h	Fri May 22 09:34:01 2026 +0200
@@ -61,25 +61,46 @@
     public:
       explicit Transcoded(const boost::shared_ptr<IDynamicObject>& value);
 
-      /**
-       * Access to the DICOM value must be protected by a mutex, as it
-       * could be shared by multiple threads if caching is enabled in
-       * the DataSourceReader.
-       **/
-      class Lock : public boost::noncopyable
+      class LockAsParsed : public boost::noncopyable
       {
       private:
-        Transcoded&        that_;
-        Mutex::ScopedLock  lock_;
+        /**
+         * Access to the transcoded DICOM must be protected by a
+         * mutex, as it could be shared by multiple threads if caching
+         * is enabled in the DataSourceReader.
+         **/
+
+        Mutex::ScopedLock                 lock_;
+        std::unique_ptr<ParsedDicomFile>  parsed_;
+        const ParsedDicomFile*            content_;
 
       public:
-        explicit Lock(Transcoded& that) :
-          that_(that),
-          lock_(that.mutex_)
+        explicit LockAsParsed(Transcoded& that);
+
+        const ParsedDicomFile& GetContent() const
         {
+          return *content_;
         }
+      };
 
-        const ParsedDicomFile& GetDicom() const;
+      class LockAsBuffer : public boost::noncopyable
+      {
+      private:
+        /**
+         * Access to the transcoded DICOM must only be protected in
+         * the constructor, if serialization to the buffer is required.
+         **/
+
+        std::unique_ptr<std::string>  serialized_;
+        const std::string*            content_;
+
+      public:
+        explicit LockAsBuffer(Transcoded& that);
+
+        const std::string& GetContent() const
+        {
+          return *content_;
+        }
       };
     };
 
--- a/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.cpp	Fri May 22 07:53:32 2026 +0200
+++ b/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.cpp	Fri May 22 09:34:01 2026 +0200
@@ -426,4 +426,17 @@
       return buffer_->size();
     }
   }
+
+
+  std::string* IDicomTranscoder::DicomImage::ReleaseInternalBuffer()
+  {
+    if (buffer_.get() == NULL)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else
+    {
+      return buffer_.release();
+    }
+  }
 }
--- a/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h	Fri May 22 07:53:32 2026 +0200
+++ b/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h	Fri May 22 09:34:01 2026 +0200
@@ -91,6 +91,18 @@
       const void* GetBufferData();
 
       size_t GetBufferSize();
+
+      bool HasParsed() const
+      {
+        return parsed_.get() != NULL;
+      }
+
+      bool HasInternalBuffer() const
+      {
+        return buffer_.get() != NULL;
+      }
+
+      std::string* ReleaseInternalBuffer();
     };
 
 
--- a/OrthancFramework/Sources/MultiThreading/Mutex.h	Fri May 22 07:53:32 2026 +0200
+++ b/OrthancFramework/Sources/MultiThreading/Mutex.h	Fri May 22 09:34:01 2026 +0200
@@ -47,6 +47,10 @@
       explicit ScopedLock(Mutex& mutex)
       {
       }
+
+      void Unlock()
+      {
+      }
     };
   };
 
@@ -68,6 +72,11 @@
         lock_(mutex.mutex_)
       {
       }
+
+      void Unlock()
+      {
+        lock_.unlock();
+      }
     };
   };
 #endif
--- a/OrthancServer/Sources/ServerTranscoder.cpp	Fri May 22 07:53:32 2026 +0200
+++ b/OrthancServer/Sources/ServerTranscoder.cpp	Fri May 22 09:34:01 2026 +0200
@@ -201,6 +201,12 @@
                                                                      size_t size,
                                                                      unsigned int frameIndex)
   {
+    /**
+     * Contrarily to "ServerTranscoder::Transcode()", this method
+     * doesn't use "dcmtkTranscoder_", as "DecodeFrameBuiltin()" would
+     * have succeeded in this case.
+     **/
+
 #if ORTHANC_ENABLE_PLUGINS == 1
     if (HasPluginsTranscoder())
     {
@@ -272,9 +278,9 @@
       std::unique_ptr<TranscoderDataSource::Transcoded> transcoded(
         TranscoderDataSource::Transcode(*transcoderReader, attachment, DicomTransferSyntax_LittleEndianExplicit));
 
-      TranscoderDataSource::Transcoded::Lock lock(*transcoded);
+      TranscoderDataSource::Transcoded::LockAsParsed lock(*transcoded);
 
-      return DecodeFrameBuiltin(lock.GetDicom(), frameIndex);
+      return DecodeFrameBuiltin(lock.GetContent(), frameIndex);
     }
   }