Fix issues in multi-threading

Using launch() interface on worker 0 has no effect
as it is not initialized in the encoder. Thus, use
execute() interface for main thread (worker 0) and
launch interface for worker threads in loop filter
and loop restoration multi-threading.

The loop filter fix is from Fyodor's change
(https://aomedia-review.googlesource.com/c/aom/+/127762)

Also, change worker 0 to be the main thread in decoder
to avoid issues in common modules like loop-filter
and loop restoration multi-threading.

Change-Id: I8bda061586baa76f94b03278dec6d3808b8d8691
diff --git a/av1/common/thread_common.c b/av1/common/thread_common.c
index 914256f..638dc4c 100644
--- a/av1/common/thread_common.c
+++ b/av1/common/thread_common.c
@@ -444,7 +444,7 @@
                   plane_start, plane_end);
 
   // Set up loopfilter thread data.
-  for (i = 0; i < num_workers; ++i) {
+  for (i = num_workers - 1; i >= 0; --i) {
     AVxWorker *const worker = &workers[i];
     LFWorkerData *const lf_data = &lf_sync->lfdata[i];
 
@@ -464,7 +464,7 @@
     loop_filter_data_reset(lf_data, frame, cm, xd);
 
     // Start loopfiltering
-    if (i == num_workers - 1) {
+    if (i == 0) {
       winterface->execute(worker);
     } else {
       winterface->launch(worker);
@@ -894,15 +894,15 @@
   enqueue_lr_jobs(lr_sync, lr_ctxt, cm);
 
   // Set up looprestoration thread data.
-  for (i = 0; i < num_workers; ++i) {
+  for (i = num_workers - 1; i >= 0; --i) {
     AVxWorker *const worker = &workers[i];
     lr_sync->lrworkerdata[i].lr_ctxt = (void *)lr_ctxt;
     worker->hook = loop_restoration_row_worker;
     worker->data1 = lr_sync;
     worker->data2 = &lr_sync->lrworkerdata[i];
 
-    // Start loopfiltering
-    if (i == num_workers - 1) {
+    // Start loop restoration
+    if (i == 0) {
       winterface->execute(worker);
     } else {
       winterface->launch(worker);
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 41114d7..f7eb103 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3432,14 +3432,14 @@
                                           int num_workers) {
   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
 
-  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
+  for (int worker_idx = num_workers - 1; worker_idx >= 0; --worker_idx) {
     AVxWorker *const worker = &pbi->tile_workers[worker_idx];
     DecWorkerData *const thread_data = (DecWorkerData *)worker->data1;
 
     thread_data->data_end = data_end;
 
     worker->had_error = 0;
-    if (worker_idx == num_workers - 1) {
+    if (worker_idx == 0) {
       winterface->execute(worker);
     } else {
       winterface->launch(worker);
@@ -3479,12 +3479,12 @@
 
       winterface->init(worker);
       worker->thread_name = "aom tile worker";
-      if (worker_idx < num_threads - 1 && !winterface->reset(worker)) {
+      if (worker_idx != 0 && !winterface->reset(worker)) {
         aom_internal_error(&cm->error, AOM_CODEC_ERROR,
                            "Tile decoder thread creation failed");
       }
 
-      if (worker_idx < num_threads - 1) {
+      if (worker_idx != 0) {
         // Allocate thread data.
         CHECK_MEM_ERROR(cm, thread_data->td,
                         aom_memalign(32, sizeof(*thread_data->td)));
@@ -3499,7 +3499,7 @@
   }
   const int use_highbd = cm->seq_params.use_highbitdepth;
   const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
-  for (worker_idx = 0; worker_idx < pbi->max_threads - 1; ++worker_idx) {
+  for (worker_idx = 1; worker_idx < pbi->max_threads; ++worker_idx) {
     DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
     if (thread_data->td->mc_buf_size != buf_size) {
       av1_free_mc_tmp_buf(thread_data->td);
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index b877961..1680734 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -187,7 +187,7 @@
   aom_free(pbi->lf_worker.data1);
 
   if (pbi->thread_data) {
-    for (int worker_idx = 0; worker_idx < pbi->max_threads - 1; worker_idx++) {
+    for (int worker_idx = 1; worker_idx < pbi->max_threads; worker_idx++) {
       DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
       av1_free_mc_tmp_buf(thread_data->td);
       aom_free(thread_data->td);