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.
vp9_frameworker_lock_stats(VPxWorker * const worker)20 void vp9_frameworker_lock_stats(VPxWorker *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
vp9_frameworker_unlock_stats(VPxWorker * const worker)29 void vp9_frameworker_unlock_stats(VPxWorker *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
vp9_frameworker_signal_stats(VPxWorker * const worker)38 void vp9_frameworker_signal_stats(VPxWorker *const worker) {
39 #if CONFIG_MULTITHREAD
40 FrameWorkerData *const worker_data = worker->data1;
41
42 // TODO(hkuang): Fix the pthread_cond_broadcast in windows wrapper.
43 #if defined(_WIN32) && !HAVE_PTHREAD_H
44 pthread_cond_signal(&worker_data->stats_cond);
45 #else
46 pthread_cond_broadcast(&worker_data->stats_cond);
47 #endif
48
49 #else
50 (void)worker;
51 #endif
52 }
53
54 // This macro prevents thread_sanitizer from reporting known concurrent writes.
55 #if defined(__has_feature)
56 #if __has_feature(thread_sanitizer)
57 #define BUILDING_WITH_TSAN
58 #endif
59 #endif
60
61 // TODO(hkuang): Remove worker parameter as it is only used in debug code.
vp9_frameworker_wait(VPxWorker * const worker,RefCntBuffer * const ref_buf,int row)62 void vp9_frameworker_wait(VPxWorker *const worker, RefCntBuffer *const ref_buf,
63 int row) {
64 #if CONFIG_MULTITHREAD
65 if (!ref_buf)
66 return;
67
68 #ifndef BUILDING_WITH_TSAN
69 // The following line of code will get harmless tsan error but it is the key
70 // to get best performance.
71 if (ref_buf->row >= row && ref_buf->buf.corrupted != 1) return;
72 #endif
73
74 {
75 // Find the worker thread that owns the reference frame. If the reference
76 // frame has been fully decoded, it may not have owner.
77 VPxWorker *const ref_worker = ref_buf->frame_worker_owner;
78 FrameWorkerData *const ref_worker_data =
79 (FrameWorkerData *)ref_worker->data1;
80 const VP9Decoder *const pbi = ref_worker_data->pbi;
81
82 #ifdef DEBUG_THREAD
83 {
84 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
85 printf("%d %p worker is waiting for %d %p worker (%d) ref %d \r\n",
86 worker_data->worker_id, worker, ref_worker_data->worker_id,
87 ref_buf->frame_worker_owner, row, ref_buf->row);
88 }
89 #endif
90
91 vp9_frameworker_lock_stats(ref_worker);
92 while (ref_buf->row < row && pbi->cur_buf == ref_buf &&
93 ref_buf->buf.corrupted != 1) {
94 pthread_cond_wait(&ref_worker_data->stats_cond,
95 &ref_worker_data->stats_mutex);
96 }
97
98 if (ref_buf->buf.corrupted == 1) {
99 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
100 vp9_frameworker_unlock_stats(ref_worker);
101 vpx_internal_error(&worker_data->pbi->common.error,
102 VPX_CODEC_CORRUPT_FRAME,
103 "Worker %p failed to decode frame", worker);
104 }
105 vp9_frameworker_unlock_stats(ref_worker);
106 }
107 #else
108 (void)worker;
109 (void)ref_buf;
110 (void)row;
111 (void)ref_buf;
112 #endif // CONFIG_MULTITHREAD
113 }
114
vp9_frameworker_broadcast(RefCntBuffer * const buf,int row)115 void vp9_frameworker_broadcast(RefCntBuffer *const buf, int row) {
116 #if CONFIG_MULTITHREAD
117 VPxWorker *worker = buf->frame_worker_owner;
118
119 #ifdef DEBUG_THREAD
120 {
121 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
122 printf("%d %p worker decode to (%d) \r\n", worker_data->worker_id,
123 buf->frame_worker_owner, row);
124 }
125 #endif
126
127 vp9_frameworker_lock_stats(worker);
128 buf->row = row;
129 vp9_frameworker_signal_stats(worker);
130 vp9_frameworker_unlock_stats(worker);
131 #else
132 (void)buf;
133 (void)row;
134 #endif // CONFIG_MULTITHREAD
135 }
136
vp9_frameworker_copy_context(VPxWorker * const dst_worker,VPxWorker * const src_worker)137 void vp9_frameworker_copy_context(VPxWorker *const dst_worker,
138 VPxWorker *const src_worker) {
139 #if CONFIG_MULTITHREAD
140 FrameWorkerData *const src_worker_data = (FrameWorkerData *)src_worker->data1;
141 FrameWorkerData *const dst_worker_data = (FrameWorkerData *)dst_worker->data1;
142 VP9_COMMON *const src_cm = &src_worker_data->pbi->common;
143 VP9_COMMON *const dst_cm = &dst_worker_data->pbi->common;
144 int i;
145
146 // Wait until source frame's context is ready.
147 vp9_frameworker_lock_stats(src_worker);
148 while (!src_worker_data->frame_context_ready) {
149 pthread_cond_wait(&src_worker_data->stats_cond,
150 &src_worker_data->stats_mutex);
151 }
152
153 dst_cm->last_frame_seg_map = src_cm->seg.enabled ?
154 src_cm->current_frame_seg_map : src_cm->last_frame_seg_map;
155 dst_worker_data->pbi->need_resync = src_worker_data->pbi->need_resync;
156 vp9_frameworker_unlock_stats(src_worker);
157
158 dst_cm->bit_depth = src_cm->bit_depth;
159 #if CONFIG_VP9_HIGHBITDEPTH
160 dst_cm->use_highbitdepth = src_cm->use_highbitdepth;
161 #endif
162 dst_cm->prev_frame = src_cm->show_existing_frame ?
163 src_cm->prev_frame : src_cm->cur_frame;
164 dst_cm->last_width = !src_cm->show_existing_frame ?
165 src_cm->width : src_cm->last_width;
166 dst_cm->last_height = !src_cm->show_existing_frame ?
167 src_cm->height : src_cm->last_height;
168 dst_cm->subsampling_x = src_cm->subsampling_x;
169 dst_cm->subsampling_y = src_cm->subsampling_y;
170 dst_cm->frame_type = src_cm->frame_type;
171 dst_cm->last_show_frame = !src_cm->show_existing_frame ?
172 src_cm->show_frame : src_cm->last_show_frame;
173 for (i = 0; i < REF_FRAMES; ++i)
174 dst_cm->ref_frame_map[i] = src_cm->next_ref_frame_map[i];
175
176 memcpy(dst_cm->lf_info.lfthr, src_cm->lf_info.lfthr,
177 (MAX_LOOP_FILTER + 1) * sizeof(loop_filter_thresh));
178 dst_cm->lf.last_sharpness_level = src_cm->lf.sharpness_level;
179 dst_cm->lf.filter_level = src_cm->lf.filter_level;
180 memcpy(dst_cm->lf.ref_deltas, src_cm->lf.ref_deltas, MAX_REF_LF_DELTAS);
181 memcpy(dst_cm->lf.mode_deltas, src_cm->lf.mode_deltas, MAX_MODE_LF_DELTAS);
182 dst_cm->seg = src_cm->seg;
183 memcpy(dst_cm->frame_contexts, src_cm->frame_contexts,
184 FRAME_CONTEXTS * sizeof(dst_cm->frame_contexts[0]));
185 #else
186 (void) dst_worker;
187 (void) src_worker;
188 #endif // CONFIG_MULTITHREAD
189 }
190