+ if (!py_res)
+ di->decoder_state = SRD_ERR;
+
+ /*
+ * Make sure to unblock potentially pending srd_inst_decode()
+ * calls in application threads after the decode() method might
+ * have terminated, while it neither has processed sample data
+ * nor has terminated upon request. This happens e.g. when "need
+ * a samplerate to decode" exception is thrown.
+ */
+ g_mutex_lock(&di->data_mutex);
+ wanted_term = di->want_wait_terminate;
+ di->want_wait_terminate = TRUE;
+ di->handled_all_samples = TRUE;
+ g_cond_signal(&di->handled_all_samples_cond);
+ g_mutex_unlock(&di->data_mutex);
+
+ /*
+ * Check for the termination cause of the decode() method.
+ * Though this is mostly for information.
+ */
+ if (!py_res && wanted_term) {
+ /*
+ * Silently ignore errors upon return from decode() calls
+ * when termination was requested. Terminate the thread
+ * which executed this instance's decode() logic.
+ */
+ srd_dbg("%s: Thread done (!res, want_term).", di->inst_id);
+ PyErr_Clear();
+ PyGILState_Release(gstate);
+ return NULL;
+ }
+ if (!py_res) {
+ /*
+ * The decode() invocation terminated unexpectedly. Have
+ * the back trace printed, and terminate the thread which
+ * executed the decode() method.
+ */
+ srd_dbg("%s: decode() terminated unrequested.", di->inst_id);
+ srd_exception_catch("Protocol decoder instance %s: ", di->inst_id);
+ srd_dbg("%s: Thread done (!res, !want_term).", di->inst_id);
+ PyGILState_Release(gstate);
+ return NULL;
+ }
+
+ /*
+ * TODO: By design the decode() method is not supposed to terminate.
+ * Nevertheless we have the thread joined, and srd backend calls to
+ * decode() will re-start another thread transparently.
+ */
+ srd_dbg("%s: decode() terminated (req %d).", di->inst_id, wanted_term);
+ Py_DecRef(py_res);
+ PyErr_Clear();
+
+ PyGILState_Release(gstate);
+
+ srd_dbg("%s: Thread done (with res).", di->inst_id);
+
+ return NULL;
+}
+
+/**
+ * Decode a chunk of samples.
+ *
+ * The calls to this function must provide the samples that shall be
+ * used by the protocol decoder
+ * - in the correct order ([...]5, 6, 4, 7, 8[...] is a bug),
+ * - starting from sample zero (2, 3, 4, 5, 6[...] is a bug),
+ * - consecutively, with no gaps (0, 1, 2, 4, 5[...] is a bug).
+ *
+ * The start- and end-sample numbers are absolute sample numbers (relative
+ * to the start of the whole capture/file/stream), i.e. they are not relative
+ * sample numbers within the chunk specified by 'inbuf' and 'inbuflen'.
+ *
+ * Correct example (4096 samples total, 4 chunks @ 1024 samples each):
+ * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1);
+ * srd_inst_decode(di, 1024, 2048, inbuf, 1024, 1);
+ * srd_inst_decode(di, 2048, 3072, inbuf, 1024, 1);
+ * srd_inst_decode(di, 3072, 4096, inbuf, 1024, 1);
+ *
+ * The chunk size ('inbuflen') can be arbitrary and can differ between calls.
+ *
+ * Correct example (4096 samples total, 7 chunks @ various samples each):
+ * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1);
+ * srd_inst_decode(di, 1024, 1124, inbuf, 100, 1);
+ * srd_inst_decode(di, 1124, 1424, inbuf, 300, 1);
+ * srd_inst_decode(di, 1424, 1643, inbuf, 219, 1);
+ * srd_inst_decode(di, 1643, 2048, inbuf, 405, 1);
+ * srd_inst_decode(di, 2048, 3072, inbuf, 1024, 1);
+ * srd_inst_decode(di, 3072, 4096, inbuf, 1024, 1);
+ *
+ * INCORRECT example (4096 samples total, 4 chunks @ 1024 samples each, but
+ * the start- and end-samplenumbers are not absolute):
+ * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1);
+ * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1);
+ * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1);
+ * srd_inst_decode(di, 0, 1024, inbuf, 1024, 1);
+ *
+ * @param di The decoder instance to call. Must not be NULL.
+ * @param abs_start_samplenum The absolute starting sample number for the
+ * buffer's sample set, relative to the start of capture.
+ * @param abs_end_samplenum The absolute ending sample number for the
+ * buffer's sample set, relative to the start of capture.
+ * @param inbuf The buffer to decode. Must not be NULL.
+ * @param inbuflen Length of the buffer. Must be > 0.
+ * @param unitsize The number of bytes per sample. Must be > 0.
+ *
+ * @return SRD_OK upon success, a (negative) error code otherwise.
+ *
+ * @private
+ */
+SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
+ uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
+ const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
+{