2
0

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(VPxWorker *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(VPxWorker *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(VPxWorker *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(VPxWorker *const worker, RefCntBuffer *const ref_buf,
  54. int row) {
  55. #if CONFIG_MULTITHREAD
  56. if (!ref_buf) return;
  57. #ifndef BUILDING_WITH_TSAN
  58. // The following line of code will get harmless tsan error but it is the key
  59. // to get best performance.
  60. if (ref_buf->row >= row && ref_buf->buf.corrupted != 1) return;
  61. #endif
  62. {
  63. // Find the worker thread that owns the reference frame. If the reference
  64. // frame has been fully decoded, it may not have owner.
  65. VPxWorker *const ref_worker = ref_buf->frame_worker_owner;
  66. FrameWorkerData *const ref_worker_data =
  67. (FrameWorkerData *)ref_worker->data1;
  68. const VP9Decoder *const pbi = ref_worker_data->pbi;
  69. #ifdef DEBUG_THREAD
  70. {
  71. FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
  72. printf("%d %p worker is waiting for %d %p worker (%d) ref %d \r\n",
  73. worker_data->worker_id, worker, ref_worker_data->worker_id,
  74. ref_buf->frame_worker_owner, row, ref_buf->row);
  75. }
  76. #endif
  77. vp9_frameworker_lock_stats(ref_worker);
  78. while (ref_buf->row < row && pbi->cur_buf == ref_buf &&
  79. ref_buf->buf.corrupted != 1) {
  80. pthread_cond_wait(&ref_worker_data->stats_cond,
  81. &ref_worker_data->stats_mutex);
  82. }
  83. if (ref_buf->buf.corrupted == 1) {
  84. FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
  85. vp9_frameworker_unlock_stats(ref_worker);
  86. vpx_internal_error(&worker_data->pbi->common.error,
  87. VPX_CODEC_CORRUPT_FRAME,
  88. "Worker %p failed to decode frame", worker);
  89. }
  90. vp9_frameworker_unlock_stats(ref_worker);
  91. }
  92. #else
  93. (void)worker;
  94. (void)ref_buf;
  95. (void)row;
  96. (void)ref_buf;
  97. #endif // CONFIG_MULTITHREAD
  98. }
  99. void vp9_frameworker_broadcast(RefCntBuffer *const buf, int row) {
  100. #if CONFIG_MULTITHREAD
  101. VPxWorker *worker = buf->frame_worker_owner;
  102. #ifdef DEBUG_THREAD
  103. {
  104. FrameWorkerData *const worker_data = (FrameWorkerData *)worker->data1;
  105. printf("%d %p worker decode to (%d) \r\n", worker_data->worker_id,
  106. buf->frame_worker_owner, row);
  107. }
  108. #endif
  109. vp9_frameworker_lock_stats(worker);
  110. buf->row = row;
  111. vp9_frameworker_signal_stats(worker);
  112. vp9_frameworker_unlock_stats(worker);
  113. #else
  114. (void)buf;
  115. (void)row;
  116. #endif // CONFIG_MULTITHREAD
  117. }
  118. void vp9_frameworker_copy_context(VPxWorker *const dst_worker,
  119. VPxWorker *const src_worker) {
  120. #if CONFIG_MULTITHREAD
  121. FrameWorkerData *const src_worker_data = (FrameWorkerData *)src_worker->data1;
  122. FrameWorkerData *const dst_worker_data = (FrameWorkerData *)dst_worker->data1;
  123. VP9_COMMON *const src_cm = &src_worker_data->pbi->common;
  124. VP9_COMMON *const dst_cm = &dst_worker_data->pbi->common;
  125. int i;
  126. // Wait until source frame's context is ready.
  127. vp9_frameworker_lock_stats(src_worker);
  128. while (!src_worker_data->frame_context_ready) {
  129. pthread_cond_wait(&src_worker_data->stats_cond,
  130. &src_worker_data->stats_mutex);
  131. }
  132. dst_cm->last_frame_seg_map = src_cm->seg.enabled
  133. ? src_cm->current_frame_seg_map
  134. : 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 =
  142. src_cm->show_existing_frame ? src_cm->prev_frame : src_cm->cur_frame;
  143. dst_cm->last_width =
  144. !src_cm->show_existing_frame ? src_cm->width : src_cm->last_width;
  145. dst_cm->last_height =
  146. !src_cm->show_existing_frame ? 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
  152. : src_cm->last_show_frame;
  153. for (i = 0; i < REF_FRAMES; ++i)
  154. dst_cm->ref_frame_map[i] = src_cm->next_ref_frame_map[i];
  155. memcpy(dst_cm->lf_info.lfthr, src_cm->lf_info.lfthr,
  156. (MAX_LOOP_FILTER + 1) * sizeof(loop_filter_thresh));
  157. dst_cm->lf.last_sharpness_level = src_cm->lf.sharpness_level;
  158. dst_cm->lf.filter_level = src_cm->lf.filter_level;
  159. memcpy(dst_cm->lf.ref_deltas, src_cm->lf.ref_deltas, MAX_REF_LF_DELTAS);
  160. memcpy(dst_cm->lf.mode_deltas, src_cm->lf.mode_deltas, MAX_MODE_LF_DELTAS);
  161. dst_cm->seg = src_cm->seg;
  162. memcpy(dst_cm->frame_contexts, src_cm->frame_contexts,
  163. FRAME_CONTEXTS * sizeof(dst_cm->frame_contexts[0]));
  164. #else
  165. (void)dst_worker;
  166. (void)src_worker;
  167. #endif // CONFIG_MULTITHREAD
  168. }