2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
11 #include "./vpx_config.h"
12 #include "vpx_mem/vpx_mem.h"
13 #include "vp10/common/reconinter.h"
14 #include "vp10/decoder/dthread.h"
15 #include "vp10/decoder/decoder.h"
17 // #define DEBUG_THREAD
19 // TODO(hkuang): Clean up all the #ifdef in this file.
20 void vp10_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);
29 void vp10_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);
38 void vp10_frameworker_signal_stats(VPxWorker *const worker) {
39 #if CONFIG_MULTITHREAD
40 FrameWorkerData *const worker_data = worker->data1;
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);
46 pthread_cond_broadcast(&worker_data->stats_cond);
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
61 // TODO(hkuang): Remove worker parameter as it is only used in debug code.
62 void vp10_frameworker_wait(VPxWorker *const worker, RefCntBuffer *const ref_buf,
64 #if CONFIG_MULTITHREAD
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;
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 VP10Decoder *const pbi = ref_worker_data->pbi;
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);
91 vp10_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);
98 if (ref_buf->buf.corrupted == 1) {
99 FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
100 vp10_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);
105 vp10_frameworker_unlock_stats(ref_worker);
112 #endif // CONFIG_MULTITHREAD
115 void vp10_frameworker_broadcast(RefCntBuffer *const buf, int row) {
116 #if CONFIG_MULTITHREAD
117 VPxWorker *worker = buf->frame_worker_owner;
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);
127 vp10_frameworker_lock_stats(worker);
129 vp10_frameworker_signal_stats(worker);
130 vp10_frameworker_unlock_stats(worker);
134 #endif // CONFIG_MULTITHREAD
137 void vp10_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 VP10_COMMON *const src_cm = &src_worker_data->pbi->common;
143 VP10_COMMON *const dst_cm = &dst_worker_data->pbi->common;
146 // Wait until source frame's context is ready.
147 vp10_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);
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 vp10_frameworker_unlock_stats(src_worker);
158 dst_cm->bit_depth = src_cm->bit_depth;
159 #if CONFIG_VP9_HIGHBITDEPTH
160 dst_cm->use_highbitdepth = src_cm->use_highbitdepth;
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];
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_FRAMES);
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]));
188 #endif // CONFIG_MULTITHREAD