From da0bb45158c4c5c89c136a9bcb3cdea373d46612 Mon Sep 17 00:00:00 2001
From: Sebastian <sebastian@baldr.asgard>
Date: Wed, 23 Oct 2019 22:20:44 +0300
Subject: [PATCH] output libde265 warnings

---
 python/ftl/__init__.py  |  2 +-
 python/ftl/ftlstream.py | 18 +++++++++++++-----
 python/ftl/libde265.py  | 38 +++++++++++++++++++++++++++++---------
 3 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/python/ftl/__init__.py b/python/ftl/__init__.py
index 963e374bd..136f6fe7d 100644
--- a/python/ftl/__init__.py
+++ b/python/ftl/__init__.py
@@ -1 +1 @@
-from . ftlstream import FTLStream
\ No newline at end of file
+from . ftlstream import FTLStream
diff --git a/python/ftl/ftlstream.py b/python/ftl/ftlstream.py
index 2d6674e80..ef8a32aba 100644
--- a/python/ftl/ftlstream.py
+++ b/python/ftl/ftlstream.py
@@ -6,7 +6,7 @@ import struct
 
 from enum import IntEnum
 from collections import namedtuple
-from . libde265 import Decoder
+from . import libde265
 
 try:
     import cv2 as cv
@@ -191,7 +191,6 @@ class FTLStream:
         
         todo: fix endianess
         '''
-
         calibration = struct.unpack("@ddddIIdddd", p.data[:(4*8+2*4+4*8)])
         self._calibration[sp.streamID] = _Camera._make(calibration)
 
@@ -216,16 +215,25 @@ class FTLStream:
         k = (sp.streamID, sp.channel)
         
         if k not in self._decoders:
-            self._decoders[k] = Decoder(_definition_t[p.definition])
+            self._decoders[k] = libde265.Decoder(_definition_t[p.definition])
         
         decoder = self._decoders[k]
         decoder.push_data(p.data)
+        try:
+            decoder.decode()
+
+        except libde265.WaitingForInput:
+            pass
     
     def _decode_hevc(self):
         for stream, decoder in self._decoders.items():
-            decoder.decode()
+            try:
+                decoder.decode()
+
+            except libde265.WaitingForInput:
+                pass
+
             img = decoder.get_next_picture()
-            
             if img is not None:
                 self._frames[stream] = _ycrcb2rgb(img)
 
diff --git a/python/ftl/libde265.py b/python/ftl/libde265.py
index d35a35842..5cab9f0cc 100644
--- a/python/ftl/libde265.py
+++ b/python/ftl/libde265.py
@@ -20,6 +20,8 @@ except ImportError:
         # order: 0 nn, 1 bilinear, 3 bicubic
         return (resize_skimage(img, size, order=3, mode="constant", cval=0) * 255).astype(np.uint8)
 
+from warnings import warn
+
 import ctypes
 from enum import IntEnum
 
@@ -35,7 +37,7 @@ if _threads is None:
 
 # error codes copied from header (de265.h)
 
-class libde265error(IntEnum):
+class _libde265error(IntEnum):
     DE265_OK = 0
     DE265_ERROR_NO_SUCH_FILE=1
     DE265_ERROR_COEFFICIENT_OUT_OF_IMAGE_BOUNDS=4
@@ -86,6 +88,10 @@ libde265 = ctypes.cdll.LoadLibrary("libde265.so.0")
 
 libde265.de265_get_error_text.argtypes = [ctypes.c_void_p]
 libde265.de265_get_error_text.restype = ctypes.c_char_p
+
+libde265.de265_get_warning.argtypes = [ctypes.c_void_p]
+libde265.de265_get_warning.restype = ctypes.c_int
+
 libde265.de265_get_version_number_major.restype = ctypes.c_uint32
 libde265.de265_get_version_number_minor.restype = ctypes.c_uint32
 
@@ -136,8 +142,6 @@ libde265.de265_get_image_plane.restype = ctypes.POINTER(ctypes.c_char)
 libde265.de265_get_number_of_input_bytes_pending.argtypes = [ctypes.c_void_p]
 libde265.de265_get_number_of_input_bytes_pending.restype = ctypes.c_int
 
-import time
-
 class libde265Error(Exception):
     def __init__(self, code):
         super(libde265Error, self).__init__(
@@ -152,7 +156,10 @@ class Decoder:
         self._more = ctypes.c_int()
         self._out_stride = ctypes.c_int()
         self._ctx = libde265.de265_new_decoder()
+        self._supress_warnings = False
+
         err = libde265.de265_start_worker_threads(self._ctx, threads)
+
         if err:
             raise libde265Error(err)
         
@@ -180,16 +187,27 @@ class Decoder:
         
         return res
 
+    def _warning(self):
+        if self._supress_warnings:
+            return
+        
+        code = libde265.de265_get_warning(self._ctx)
+        
+        if code != _libde265error.DE265_OK:
+            msg = libde265.de265_get_error_text(code).decode("ascii")
+            warn(msg)
+
     def decode(self):
         err = libde265.de265_decode(self._ctx, self._more)
         
-        # should use custom
         if err:
-            if err == libde265error.DE265_ERROR_WAITING_FOR_INPUT_DATA:
+            if err == _libde265error.DE265_ERROR_WAITING_FOR_INPUT_DATA:
                 raise WaitingForInput(err)
 
             raise libde265Error(err)
         
+        self._warning()
+
         return self._more.value != 0
     
     def flush_data(self):
@@ -197,7 +215,7 @@ class Decoder:
         
         if err:
             raise libde265Error(err)
-
+    
     def push_data(self, data):
         if not isinstance(data, bytes):
             raise ValueError("expected bytes")
@@ -206,7 +224,7 @@ class Decoder:
         
         if err:
             raise libde265Error(err)
-            
+    
     def push_end_of_frame(self):
         err = libde265.de265_push_end_of_frame(self._ctx)
         
@@ -229,11 +247,12 @@ class Decoder:
         '''
 
         de265_image = libde265.de265_get_next_picture(self._ctx)
-        
+
         if not de265_image:
             return None
         
         res = self._copy_image(de265_image)
+
         libde265.de265_release_next_picture(self._ctx)
 
         return res
@@ -243,11 +262,12 @@ class Decoder:
 
     def peek_next_picture(self):
         de265_image = libde265.de265_peek_next_picture(self._ctx)
-        
+
         if not de265_image:
             return None
         
         res = self._copy_image(de265_image)
+
         libde265.de265_release_next_picture(self._ctx)
 
         return res
-- 
GitLab