Fully order the pvq search candidates
authorDavid Michael Barr <b@rr-dav.id.au>
Tue, 6 Sep 2016 02:41:07 +0000 (11:41 +0900)
committerDavid Michael Barr <b@rr-dav.id.au>
Thu, 8 Dec 2016 23:26:43 +0000 (13:26 -1000)
For portable and stable sorting, break ties.
Large differences in output were observed between
AWCY and an OS X machine because of the platform
qsort implementation.

Signed-off-by: David Michael Barr <b@rr-dav.id.au>
src/pvq_encoder.c

index 299401e..1440fc2 100644 (file)
@@ -283,7 +283,9 @@ typedef struct {
 } pvq_search_item;
 
 int items_compare(pvq_search_item *a, pvq_search_item *b) {
-  return a->k - b->k;
+  /* Break ties in K with gain to ensure a stable sort.
+     Otherwise, the order depends on qsort implementation. */
+  return a->k == b->k ? a->gain - b->gain : a->k - b->k;
 }
 
 /** Perform PVQ quantization with prediction, trying several