vp9_lookahead.c 6.8 KB

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