vp9_dthread.c 6.4 KB

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