Propagate error from worker to main thread during lr MT This change sets a longjmp target in loop_restoration_row_worker() to propagate error from worker threads to the main thread during loop restoration multi-threading. It also ensures that other threads stop processing further if any of the threads encounter an error. Bug: aomedia:3276 Change-Id: Idce3fac77cd59960eed4c3eaf92df15d5935b817
diff --git a/av1/common/restoration.c b/av1/common/restoration.c index 4806975..18e3d1f 100644 --- a/av1/common/restoration.c +++ b/av1/common/restoration.c
@@ -20,6 +20,7 @@ #include "av1/common/av1_common_int.h" #include "av1/common/resize.h" #include "av1/common/restoration.h" +#include "av1/common/thread_common.h" #include "aom_dsp/aom_dsp_common.h" #include "aom_mem/aom_mem.h" @@ -1070,6 +1071,7 @@ RestorationInfo *rsi = &cm->rst_info[plane]; RestorationType rtype = rsi->frame_restoration_type; rsi->optimized_lr = optimized_lr; + lr_ctxt->ctxt[plane].rsi = rsi; if (rtype == RESTORE_NONE) { continue; @@ -1086,7 +1088,6 @@ RESTORATION_BORDER, highbd); FilterFrameCtxt *lr_plane_ctxt = &lr_ctxt->ctxt[plane]; - lr_plane_ctxt->rsi = rsi; lr_plane_ctxt->ss_x = is_uv && seq_params->subsampling_x; lr_plane_ctxt->ss_y = is_uv && seq_params->subsampling_y; lr_plane_ctxt->plane_w = plane_w; @@ -1152,7 +1153,11 @@ rest_unit_visitor_t on_rest_unit, int row_number, int unit_size, int hnum_rest_units, int vnum_rest_units, int plane, void *priv, int32_t *tmpbuf, RestorationLineBuffers *rlbs, sync_read_fn_t on_sync_read, - sync_write_fn_t on_sync_write, struct AV1LrSyncData *const lr_sync) { + sync_write_fn_t on_sync_write, struct AV1LrSyncData *const lr_sync, + struct aom_internal_error_info *error_info) { + // TODO(aomedia:3276): Pass error_info to the low-level functions as required + // in future to handle error propagation. + (void)error_info; const int ext_size = unit_size * 3 / 2; int x0 = 0, j = 0; while (x0 < plane_w) { @@ -1175,6 +1180,16 @@ // bottom-right sync on_sync_read(lr_sync, row_number + 2, j, plane); +#if CONFIG_MULTITHREAD + if (lr_sync && lr_sync->num_workers > 1) { + pthread_mutex_lock(lr_sync->job_mutex); + const bool lr_mt_exit = lr_sync->lr_mt_exit; + pthread_mutex_unlock(lr_sync->job_mutex); + // Exit in case any worker has encountered an error. + if (lr_mt_exit) return; + } +#endif + on_rest_unit(limits, unit_idx, priv, tmpbuf, rlbs); on_sync_write(lr_sync, row_number, j, hnum_rest_units, plane); @@ -1232,7 +1247,7 @@ av1_foreach_rest_unit_in_row(&limits, plane_w, on_rest_unit, i, unit_size, hnum_rest_units, vnum_rest_units, plane, priv, tmpbuf, rlbs, av1_lr_sync_read_dummy, - av1_lr_sync_write_dummy, NULL); + av1_lr_sync_write_dummy, NULL, cm->error); y0 += h; ++i;
diff --git a/av1/common/restoration.h b/av1/common/restoration.h index 48d9a9c..5f0e1e0 100644 --- a/av1/common/restoration.h +++ b/av1/common/restoration.h
@@ -451,7 +451,9 @@ rest_unit_visitor_t on_rest_unit, int row_number, int unit_size, int hnum_rest_units, int vnum_rest_units, int plane, void *priv, int32_t *tmpbuf, RestorationLineBuffers *rlbs, sync_read_fn_t on_sync_read, - sync_write_fn_t on_sync_write, struct AV1LrSyncData *const lr_sync); + sync_write_fn_t on_sync_write, struct AV1LrSyncData *const lr_sync, + struct aom_internal_error_info *error_info); + void av1_get_upsampled_plane_size(const struct AV1Common *cm, int is_uv, int *plane_w, int *plane_h); int av1_lr_count_units(int unit_size, int plane_size);
diff --git a/av1/common/thread_common.c b/av1/common/thread_common.c index c215ee7..b52ef0e 100644 --- a/av1/common/thread_common.c +++ b/av1/common/thread_common.c
@@ -617,6 +617,7 @@ } lr_sync->num_workers = num_workers; + lr_sync->lr_mt_exit = false; for (int j = 0; j < num_planes; j++) { CHECK_MEM_ERROR( @@ -760,7 +761,7 @@ #if CONFIG_MULTITHREAD pthread_mutex_lock(lr_sync->job_mutex); - if (lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) { + if (!lr_sync->lr_mt_exit && lr_sync->jobs_dequeued < lr_sync->jobs_enqueued) { cur_job_info = lr_sync->job_queue + lr_sync->jobs_dequeued; lr_sync->jobs_dequeued++; } @@ -773,6 +774,26 @@ return cur_job_info; } +static void set_loop_restoration_done(AV1LrSync *const lr_sync, + FilterFrameCtxt *const ctxt) { + for (int plane = 0; plane < MAX_MB_PLANE; ++plane) { + if (ctxt[plane].rsi->frame_restoration_type == RESTORE_NONE) continue; + int y0 = 0, row_number = 0; + const int unit_size = ctxt[plane].rsi->restoration_unit_size; + const int plane_h = ctxt[plane].plane_h; + const int ext_size = unit_size * 3 / 2; + const int hnum_rest_units = ctxt[plane].rsi->horz_units; + while (y0 < plane_h) { + const int remaining_h = plane_h - y0; + const int h = (remaining_h < ext_size) ? remaining_h : unit_size; + lr_sync_write(lr_sync, row_number, hnum_rest_units - 1, hnum_rest_units, + plane); + y0 += h; + ++row_number; + } + } +} + // Implement row loop restoration for each thread. static int loop_restoration_row_worker(void *arg1, void *arg2) { AV1LrSync *const lr_sync = (AV1LrSync *)arg1; @@ -782,6 +803,31 @@ int lr_unit_row; int plane; int plane_w; +#if CONFIG_MULTITHREAD + pthread_mutex_t *job_mutex_ = lr_sync->job_mutex; +#endif + struct aom_internal_error_info *const error_info = &lrworkerdata->error_info; + + // The jmp_buf is valid only for the duration of the function that calls + // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 + // before it returns. + if (setjmp(error_info->jmp)) { + error_info->setjmp = 0; +#if CONFIG_MULTITHREAD + pthread_mutex_lock(job_mutex_); + lr_sync->lr_mt_exit = true; + pthread_mutex_unlock(job_mutex_); +#endif + // In case of loop restoration multithreading, the worker on an even lr + // block row waits for the completion of the filtering of the top-right and + // bottom-right blocks. Hence, in case a thread (main/worker) encounters an + // error, update that filtering of every row in the frame is complete in + // order to avoid the dependent workers from waiting indefinitely. + set_loop_restoration_done(lr_sync, lr_ctxt->ctxt); + return 0; + } + error_info->setjmp = 1; + typedef void (*copy_fun)(const YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc, int hstart, int hend, int vstart, int vend); @@ -814,7 +860,7 @@ ctxt[plane].rsi->restoration_unit_size, ctxt[plane].rsi->horz_units, ctxt[plane].rsi->vert_units, plane, &ctxt[plane], lrworkerdata->rst_tmpbuf, lrworkerdata->rlbs, on_sync_read, - on_sync_write, lr_sync); + on_sync_write, lr_sync, error_info); copy_funs[plane](lr_ctxt->dst, lr_ctxt->frame, 0, plane_w, cur_job_info->v_copy_start, cur_job_info->v_copy_end); @@ -828,9 +874,35 @@ break; } } + error_info->setjmp = 0; return 1; } +static AOM_INLINE void sync_lr_workers(AVxWorker *const workers, + AV1_COMMON *const cm, int num_workers) { + const AVxWorkerInterface *const winterface = aom_get_worker_interface(); + int had_error = workers[0].had_error; + struct aom_internal_error_info error_info; + + // Read the error_info of main thread. + if (had_error) { + AVxWorker *const worker = &workers[0]; + error_info = ((LRWorkerData *)worker->data2)->error_info; + } + + // Wait till all rows are finished. + for (int i = num_workers - 1; i > 0; --i) { + AVxWorker *const worker = &workers[i]; + if (!winterface->sync(worker)) { + had_error = 1; + error_info = ((LRWorkerData *)worker->data2)->error_info; + } + } + if (had_error) + aom_internal_error(cm->error, error_info.error_code, "%s", + error_info.detail); +} + static void foreach_rest_unit_in_planes_mt(AV1LrStruct *lr_ctxt, AVxWorker *workers, int nworkers, AV1LrSync *lr_sync, AV1_COMMON *cm, @@ -880,6 +952,7 @@ worker->data2 = &lr_sync->lrworkerdata[i]; // Start loop restoration + worker->had_error = 0; if (i == 0) { winterface->execute(worker); } else { @@ -887,10 +960,7 @@ } } - // Wait till all rows are finished - for (i = 1; i < num_workers; ++i) { - winterface->sync(&workers[i]); - } + sync_lr_workers(workers, cm, num_workers); } void av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
diff --git a/av1/common/thread_common.h b/av1/common/thread_common.h index cfbdc2f..6d695e8 100644 --- a/av1/common/thread_common.h +++ b/av1/common/thread_common.h
@@ -75,6 +75,7 @@ void *rlbs; void *lr_ctxt; int do_extend_border; + struct aom_internal_error_info error_info; } LRWorkerData; // Looprestoration row synchronization @@ -102,6 +103,9 @@ AV1LrMTInfo *job_queue; int jobs_enqueued; int jobs_dequeued; + // Initialized to false, set to true by the worker thread that encounters + // an error in order to abort the processing of other worker threads. + bool lr_mt_exit; } AV1LrSync; typedef struct AV1CdefWorker {