OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "./vpx_config.h" |
| 12 #include "vpx_mem/vpx_mem.h" |
| 13 #include "vp9/common/vp9_reconinter.h" |
| 14 #include "vp9/decoder/vp9_dthread.h" |
| 15 #include "vp9/decoder/vp9_decoder.h" |
| 16 |
| 17 // #define DEBUG_THREAD |
| 18 |
| 19 // TODO(hkuang): Clean up all the #ifdef in this file. |
| 20 void vp9_frameworker_lock_stats(VP9Worker *const worker) { |
| 21 #if CONFIG_MULTITHREAD |
| 22 FrameWorkerData *const worker_data = worker->data1; |
| 23 pthread_mutex_lock(&worker_data->stats_mutex); |
| 24 #else |
| 25 (void)worker; |
| 26 #endif |
| 27 } |
| 28 |
| 29 void vp9_frameworker_unlock_stats(VP9Worker *const worker) { |
| 30 #if CONFIG_MULTITHREAD |
| 31 FrameWorkerData *const worker_data = worker->data1; |
| 32 pthread_mutex_unlock(&worker_data->stats_mutex); |
| 33 #else |
| 34 (void)worker; |
| 35 #endif |
| 36 } |
| 37 |
| 38 void vp9_frameworker_signal_stats(VP9Worker *const worker) { |
| 39 #if CONFIG_MULTITHREAD |
| 40 FrameWorkerData *const worker_data = worker->data1; |
| 41 // TODO(hkuang): Investigate using broadcast or signal. |
| 42 pthread_cond_signal(&worker_data->stats_cond); |
| 43 #else |
| 44 (void)worker; |
| 45 #endif |
| 46 } |
| 47 |
| 48 // This macro prevents thread_sanitizer from reporting known concurrent writes. |
| 49 #if defined(__has_feature) |
| 50 #if __has_feature(thread_sanitizer) |
| 51 #define BUILDING_WITH_TSAN |
| 52 #endif |
| 53 #endif |
| 54 |
| 55 // TODO(hkuang): Remove worker parameter as it is only used in debug code. |
| 56 void vp9_frameworker_wait(VP9Worker *const worker, RefCntBuffer *const ref_buf, |
| 57 int row) { |
| 58 #if CONFIG_MULTITHREAD |
| 59 if (!ref_buf) |
| 60 return; |
| 61 |
| 62 #ifndef BUILDING_WITH_TSAN |
| 63 // The following line of code will get harmless tsan error but it is the key |
| 64 // to get best performance. |
| 65 if (ref_buf->row >= row && ref_buf->buf.corrupted != 1) return; |
| 66 #endif |
| 67 |
| 68 { |
| 69 // Find the worker thread that owns the reference frame. If the reference |
| 70 // frame has been fully decoded, it may not have owner. |
| 71 VP9Worker *const ref_worker = ref_buf->frame_worker_owner; |
| 72 FrameWorkerData *const ref_worker_data = |
| 73 (FrameWorkerData *)ref_worker->data1; |
| 74 const VP9Decoder *const pbi = ref_worker_data->pbi; |
| 75 |
| 76 #ifdef DEBUG_THREAD |
| 77 { |
| 78 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1; |
| 79 printf("%d %p worker is waiting for %d %p worker (%d) ref %d \r\n", |
| 80 worker_data->worker_id, worker, ref_worker_data->worker_id, |
| 81 ref_buf->frame_worker_owner, row, ref_buf->row); |
| 82 } |
| 83 #endif |
| 84 |
| 85 vp9_frameworker_lock_stats(ref_worker); |
| 86 while (ref_buf->row < row && pbi->cur_buf == ref_buf && |
| 87 ref_buf->buf.corrupted != 1) { |
| 88 pthread_cond_wait(&ref_worker_data->stats_cond, |
| 89 &ref_worker_data->stats_mutex); |
| 90 } |
| 91 |
| 92 if (ref_buf->buf.corrupted == 1) { |
| 93 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1; |
| 94 vp9_frameworker_unlock_stats(ref_worker); |
| 95 vpx_internal_error(&worker_data->pbi->common.error, |
| 96 VPX_CODEC_CORRUPT_FRAME, |
| 97 "Worker %p failed to decode frame", worker); |
| 98 } |
| 99 vp9_frameworker_unlock_stats(ref_worker); |
| 100 } |
| 101 #else |
| 102 (void)worker; |
| 103 (void)ref_buf; |
| 104 (void)row; |
| 105 (void)ref_buf; |
| 106 #endif // CONFIG_MULTITHREAD |
| 107 } |
| 108 |
| 109 void vp9_frameworker_broadcast(RefCntBuffer *const buf, int row) { |
| 110 #if CONFIG_MULTITHREAD |
| 111 VP9Worker *worker = buf->frame_worker_owner; |
| 112 |
| 113 #ifdef DEBUG_THREAD |
| 114 { |
| 115 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1; |
| 116 printf("%d %p worker decode to (%d) \r\n", worker_data->worker_id, |
| 117 buf->frame_worker_owner, row); |
| 118 } |
| 119 #endif |
| 120 |
| 121 vp9_frameworker_lock_stats(worker); |
| 122 buf->row = row; |
| 123 vp9_frameworker_signal_stats(worker); |
| 124 vp9_frameworker_unlock_stats(worker); |
| 125 #else |
| 126 (void)buf; |
| 127 (void)row; |
| 128 #endif // CONFIG_MULTITHREAD |
| 129 } |
| 130 |
| 131 void vp9_frameworker_copy_context(VP9Worker *const dst_worker, |
| 132 VP9Worker *const src_worker) { |
| 133 #if CONFIG_MULTITHREAD |
| 134 FrameWorkerData *const src_worker_data = (FrameWorkerData *)src_worker->data1; |
| 135 FrameWorkerData *const dst_worker_data = (FrameWorkerData *)dst_worker->data1; |
| 136 VP9_COMMON *const src_cm = &src_worker_data->pbi->common; |
| 137 VP9_COMMON *const dst_cm = &dst_worker_data->pbi->common; |
| 138 int i; |
| 139 |
| 140 // Wait until source frame's context is ready. |
| 141 vp9_frameworker_lock_stats(src_worker); |
| 142 while (!src_worker_data->frame_context_ready) { |
| 143 pthread_cond_wait(&src_worker_data->stats_cond, |
| 144 &src_worker_data->stats_mutex); |
| 145 } |
| 146 |
| 147 // src worker may have already finished decoding a frame and swapped the mi. |
| 148 // TODO(hkuang): Remove following code after implenment no ModeInfo decoding. |
| 149 if (src_worker_data->frame_decoded) { |
| 150 dst_cm->prev_mip = src_cm->prev_mip; |
| 151 dst_cm->prev_mi = src_cm->prev_mi; |
| 152 } else { |
| 153 dst_cm->prev_mip = src_cm->mip; |
| 154 dst_cm->prev_mi = src_cm->mi; |
| 155 } |
| 156 |
| 157 dst_cm->last_frame_seg_map = src_cm->seg.enabled ? |
| 158 src_cm->current_frame_seg_map : src_cm->last_frame_seg_map; |
| 159 dst_worker_data->pbi->need_resync = src_worker_data->pbi->need_resync; |
| 160 vp9_frameworker_unlock_stats(src_worker); |
| 161 |
| 162 dst_worker_data->pbi->prev_buf = |
| 163 src_worker_data->pbi->common.show_existing_frame ? |
| 164 NULL : src_worker_data->pbi->cur_buf; |
| 165 |
| 166 dst_cm->prev_frame = src_cm->show_existing_frame ? |
| 167 src_cm->prev_frame : src_cm->cur_frame; |
| 168 dst_cm->last_width = !src_cm->show_existing_frame ? |
| 169 src_cm->width : src_cm->last_width; |
| 170 dst_cm->last_height = !src_cm->show_existing_frame ? |
| 171 src_cm->height : src_cm->last_height; |
| 172 dst_cm->display_width = src_cm->display_width; |
| 173 dst_cm->display_height = src_cm->display_height; |
| 174 dst_cm->subsampling_x = src_cm->subsampling_x; |
| 175 dst_cm->subsampling_y = src_cm->subsampling_y; |
| 176 dst_cm->last_show_frame = !src_cm->show_existing_frame ? |
| 177 src_cm->show_frame : src_cm->last_show_frame; |
| 178 dst_cm->last_frame_type = src_cm->last_frame_type; |
| 179 dst_cm->frame_type = src_cm->frame_type; |
| 180 dst_cm->y_dc_delta_q = src_cm->y_dc_delta_q; |
| 181 dst_cm->uv_dc_delta_q = src_cm->uv_dc_delta_q; |
| 182 dst_cm->uv_ac_delta_q = src_cm->uv_ac_delta_q; |
| 183 dst_cm->base_qindex = src_cm->base_qindex; |
| 184 |
| 185 for (i = 0; i < REF_FRAMES; ++i) |
| 186 dst_cm->ref_frame_map[i] = src_cm->next_ref_frame_map[i]; |
| 187 |
| 188 memcpy(dst_cm->lf_info.lfthr, src_cm->lf_info.lfthr, |
| 189 (MAX_LOOP_FILTER + 1) * sizeof(loop_filter_thresh)); |
| 190 dst_cm->lf.last_sharpness_level = src_cm->lf.sharpness_level; |
| 191 dst_cm->lf.filter_level = src_cm->lf.filter_level; |
| 192 memcpy(dst_cm->lf.ref_deltas, src_cm->lf.ref_deltas, MAX_REF_LF_DELTAS); |
| 193 memcpy(dst_cm->lf.mode_deltas, src_cm->lf.mode_deltas, MAX_MODE_LF_DELTAS); |
| 194 dst_cm->seg = src_cm->seg; |
| 195 memcpy(dst_cm->frame_contexts, src_cm->frame_contexts, |
| 196 FRAME_CONTEXTS * sizeof(dst_cm->frame_contexts[0])); |
| 197 #else |
| 198 (void) dst_worker; |
| 199 (void) src_worker; |
| 200 #endif // CONFIG_MULTITHREAD |
| 201 } |
OLD | NEW |