vp9_lookahead.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2011 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 <assert.h>
  11. #include <stdlib.h>
  12. #include "./vpx_config.h"
  13. #include "vp9/common/vp9_common.h"
  14. #include "vp9/encoder/vp9_encoder.h"
  15. #include "vp9/encoder/vp9_extend.h"
  16. #include "vp9/encoder/vp9_lookahead.h"
  17. /* Return the buffer at the given absolute index and increment the index */
  18. static struct lookahead_entry *pop(struct lookahead_ctx *ctx,
  19. unsigned int *idx) {
  20. unsigned int index = *idx;
  21. struct lookahead_entry *buf = ctx->buf + index;
  22. assert(index < ctx->max_sz);
  23. if (++index >= ctx->max_sz)
  24. index -= ctx->max_sz;
  25. *idx = index;
  26. return buf;
  27. }
  28. void vp9_lookahead_destroy(struct lookahead_ctx *ctx) {
  29. if (ctx) {
  30. if (ctx->buf) {
  31. unsigned int i;
  32. for (i = 0; i < ctx->max_sz; i++)
  33. vp9_free_frame_buffer(&ctx->buf[i].img);
  34. free(ctx->buf);
  35. }
  36. free(ctx);
  37. }
  38. }
  39. struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
  40. unsigned int height,
  41. unsigned int subsampling_x,
  42. unsigned int subsampling_y,
  43. #if CONFIG_VP9_HIGHBITDEPTH
  44. int use_highbitdepth,
  45. #endif
  46. unsigned int depth) {
  47. struct lookahead_ctx *ctx = NULL;
  48. // Clamp the lookahead queue depth
  49. depth = clamp(depth, 1, MAX_LAG_BUFFERS);
  50. // Allocate memory to keep previous source frames available.
  51. depth += MAX_PRE_FRAMES;
  52. // Allocate the lookahead structures
  53. ctx = calloc(1, sizeof(*ctx));
  54. if (ctx) {
  55. const int legacy_byte_alignment = 0;
  56. unsigned int i;
  57. ctx->max_sz = depth;
  58. ctx->buf = calloc(depth, sizeof(*ctx->buf));
  59. if (!ctx->buf)
  60. goto bail;
  61. for (i = 0; i < depth; i++)
  62. if (vp9_alloc_frame_buffer(&ctx->buf[i].img,
  63. width, height, subsampling_x, subsampling_y,
  64. #if CONFIG_VP9_HIGHBITDEPTH
  65. use_highbitdepth,
  66. #endif
  67. VP9_ENC_BORDER_IN_PIXELS,
  68. legacy_byte_alignment))
  69. goto bail;
  70. }
  71. return ctx;
  72. bail:
  73. vp9_lookahead_destroy(ctx);
  74. return NULL;
  75. }
  76. #define USE_PARTIAL_COPY 0
  77. int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
  78. int64_t ts_start, int64_t ts_end,
  79. #if CONFIG_VP9_HIGHBITDEPTH
  80. int use_highbitdepth,
  81. #endif
  82. unsigned int flags) {
  83. struct lookahead_entry *buf;
  84. #if USE_PARTIAL_COPY
  85. int row, col, active_end;
  86. int mb_rows = (src->y_height + 15) >> 4;
  87. int mb_cols = (src->y_width + 15) >> 4;
  88. #endif
  89. int width = src->y_crop_width;
  90. int height = src->y_crop_height;
  91. int uv_width = src->uv_crop_width;
  92. int uv_height = src->uv_crop_height;
  93. int subsampling_x = src->subsampling_x;
  94. int subsampling_y = src->subsampling_y;
  95. int larger_dimensions, new_dimensions;
  96. if (ctx->sz + 1 + MAX_PRE_FRAMES > ctx->max_sz)
  97. return 1;
  98. ctx->sz++;
  99. buf = pop(ctx, &ctx->write_idx);
  100. new_dimensions = width != buf->img.y_crop_width ||
  101. height != buf->img.y_crop_height ||
  102. uv_width != buf->img.uv_crop_width ||
  103. uv_height != buf->img.uv_crop_height;
  104. larger_dimensions = width > buf->img.y_width ||
  105. height > buf->img.y_height ||
  106. uv_width > buf->img.uv_width ||
  107. uv_height > buf->img.uv_height;
  108. assert(!larger_dimensions || new_dimensions);
  109. #if USE_PARTIAL_COPY
  110. // TODO(jkoleszar): This is disabled for now, as
  111. // vp9_copy_and_extend_frame_with_rect is not subsampling/alpha aware.
  112. // Only do this partial copy if the following conditions are all met:
  113. // 1. Lookahead queue has has size of 1.
  114. // 2. Active map is provided.
  115. // 3. This is not a key frame, golden nor altref frame.
  116. if (!new_dimensions && ctx->max_sz == 1 && active_map && !flags) {
  117. for (row = 0; row < mb_rows; ++row) {
  118. col = 0;
  119. while (1) {
  120. // Find the first active macroblock in this row.
  121. for (; col < mb_cols; ++col) {
  122. if (active_map[col])
  123. break;
  124. }
  125. // No more active macroblock in this row.
  126. if (col == mb_cols)
  127. break;
  128. // Find the end of active region in this row.
  129. active_end = col;
  130. for (; active_end < mb_cols; ++active_end) {
  131. if (!active_map[active_end])
  132. break;
  133. }
  134. // Only copy this active region.
  135. vp9_copy_and_extend_frame_with_rect(src, &buf->img,
  136. row << 4,
  137. col << 4, 16,
  138. (active_end - col) << 4);
  139. // Start again from the end of this active region.
  140. col = active_end;
  141. }
  142. active_map += mb_cols;
  143. }
  144. } else {
  145. #endif
  146. if (larger_dimensions) {
  147. YV12_BUFFER_CONFIG new_img;
  148. memset(&new_img, 0, sizeof(new_img));
  149. if (vp9_alloc_frame_buffer(&new_img,
  150. width, height, subsampling_x, subsampling_y,
  151. #if CONFIG_VP9_HIGHBITDEPTH
  152. use_highbitdepth,
  153. #endif
  154. VP9_ENC_BORDER_IN_PIXELS,
  155. 0))
  156. return 1;
  157. vp9_free_frame_buffer(&buf->img);
  158. buf->img = new_img;
  159. } else if (new_dimensions) {
  160. buf->img.y_crop_width = src->y_crop_width;
  161. buf->img.y_crop_height = src->y_crop_height;
  162. buf->img.uv_crop_width = src->uv_crop_width;
  163. buf->img.uv_crop_height = src->uv_crop_height;
  164. buf->img.subsampling_x = src->subsampling_x;
  165. buf->img.subsampling_y = src->subsampling_y;
  166. }
  167. // Partial copy not implemented yet
  168. vp9_copy_and_extend_frame(src, &buf->img);
  169. #if USE_PARTIAL_COPY
  170. }
  171. #endif
  172. buf->ts_start = ts_start;
  173. buf->ts_end = ts_end;
  174. buf->flags = flags;
  175. return 0;
  176. }
  177. struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx,
  178. int drain) {
  179. struct lookahead_entry *buf = NULL;
  180. if (ctx && ctx->sz && (drain || ctx->sz == ctx->max_sz - MAX_PRE_FRAMES)) {
  181. buf = pop(ctx, &ctx->read_idx);
  182. ctx->sz--;
  183. }
  184. return buf;
  185. }
  186. struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
  187. int index) {
  188. struct lookahead_entry *buf = NULL;
  189. if (index >= 0) {
  190. // Forward peek
  191. if (index < (int)ctx->sz) {
  192. index += ctx->read_idx;
  193. if (index >= (int)ctx->max_sz)
  194. index -= ctx->max_sz;
  195. buf = ctx->buf + index;
  196. }
  197. } else if (index < 0) {
  198. // Backward peek
  199. if (-index <= MAX_PRE_FRAMES) {
  200. index += ctx->read_idx;
  201. if (index < 0)
  202. index += ctx->max_sz;
  203. buf = ctx->buf + index;
  204. }
  205. }
  206. return buf;
  207. }
  208. unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx) {
  209. return ctx->sz;
  210. }