vp9_iface_common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2013 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. #ifndef VPX_VP9_VP9_IFACE_COMMON_H_
  11. #define VPX_VP9_VP9_IFACE_COMMON_H_
  12. #include "vpx_ports/mem.h"
  13. static void yuvconfig2image(vpx_image_t *img, const YV12_BUFFER_CONFIG *yv12,
  14. void *user_priv) {
  15. /** vpx_img_wrap() doesn't allow specifying independent strides for
  16. * the Y, U, and V planes, nor other alignment adjustments that
  17. * might be representable by a YV12_BUFFER_CONFIG, so we just
  18. * initialize all the fields.*/
  19. int bps;
  20. if (!yv12->subsampling_y) {
  21. if (!yv12->subsampling_x) {
  22. img->fmt = VPX_IMG_FMT_I444;
  23. bps = 24;
  24. } else {
  25. img->fmt = VPX_IMG_FMT_I422;
  26. bps = 16;
  27. }
  28. } else {
  29. if (!yv12->subsampling_x) {
  30. img->fmt = VPX_IMG_FMT_I440;
  31. bps = 16;
  32. } else {
  33. img->fmt = VPX_IMG_FMT_I420;
  34. bps = 12;
  35. }
  36. }
  37. img->cs = yv12->color_space;
  38. img->range = yv12->color_range;
  39. img->bit_depth = 8;
  40. img->w = yv12->y_stride;
  41. img->h = ALIGN_POWER_OF_TWO(yv12->y_height + 2 * VP9_ENC_BORDER_IN_PIXELS, 3);
  42. img->d_w = yv12->y_crop_width;
  43. img->d_h = yv12->y_crop_height;
  44. img->r_w = yv12->render_width;
  45. img->r_h = yv12->render_height;
  46. img->x_chroma_shift = yv12->subsampling_x;
  47. img->y_chroma_shift = yv12->subsampling_y;
  48. img->planes[VPX_PLANE_Y] = yv12->y_buffer;
  49. img->planes[VPX_PLANE_U] = yv12->u_buffer;
  50. img->planes[VPX_PLANE_V] = yv12->v_buffer;
  51. img->planes[VPX_PLANE_ALPHA] = NULL;
  52. img->stride[VPX_PLANE_Y] = yv12->y_stride;
  53. img->stride[VPX_PLANE_U] = yv12->uv_stride;
  54. img->stride[VPX_PLANE_V] = yv12->uv_stride;
  55. img->stride[VPX_PLANE_ALPHA] = yv12->y_stride;
  56. #if CONFIG_VP9_HIGHBITDEPTH
  57. if (yv12->flags & YV12_FLAG_HIGHBITDEPTH) {
  58. // vpx_image_t uses byte strides and a pointer to the first byte
  59. // of the image.
  60. img->fmt = (vpx_img_fmt_t)(img->fmt | VPX_IMG_FMT_HIGHBITDEPTH);
  61. img->bit_depth = yv12->bit_depth;
  62. img->planes[VPX_PLANE_Y] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->y_buffer);
  63. img->planes[VPX_PLANE_U] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->u_buffer);
  64. img->planes[VPX_PLANE_V] = (uint8_t *)CONVERT_TO_SHORTPTR(yv12->v_buffer);
  65. img->planes[VPX_PLANE_ALPHA] = NULL;
  66. img->stride[VPX_PLANE_Y] = 2 * yv12->y_stride;
  67. img->stride[VPX_PLANE_U] = 2 * yv12->uv_stride;
  68. img->stride[VPX_PLANE_V] = 2 * yv12->uv_stride;
  69. img->stride[VPX_PLANE_ALPHA] = 2 * yv12->y_stride;
  70. }
  71. #endif // CONFIG_VP9_HIGHBITDEPTH
  72. img->bps = bps;
  73. img->user_priv = user_priv;
  74. img->img_data = yv12->buffer_alloc;
  75. img->img_data_owner = 0;
  76. img->self_allocd = 0;
  77. }
  78. static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
  79. YV12_BUFFER_CONFIG *yv12) {
  80. yv12->y_buffer = img->planes[VPX_PLANE_Y];
  81. yv12->u_buffer = img->planes[VPX_PLANE_U];
  82. yv12->v_buffer = img->planes[VPX_PLANE_V];
  83. yv12->y_crop_width = img->d_w;
  84. yv12->y_crop_height = img->d_h;
  85. yv12->render_width = img->r_w;
  86. yv12->render_height = img->r_h;
  87. yv12->y_width = img->d_w;
  88. yv12->y_height = img->d_h;
  89. yv12->uv_width =
  90. img->x_chroma_shift == 1 ? (1 + yv12->y_width) / 2 : yv12->y_width;
  91. yv12->uv_height =
  92. img->y_chroma_shift == 1 ? (1 + yv12->y_height) / 2 : yv12->y_height;
  93. yv12->uv_crop_width = yv12->uv_width;
  94. yv12->uv_crop_height = yv12->uv_height;
  95. yv12->y_stride = img->stride[VPX_PLANE_Y];
  96. yv12->uv_stride = img->stride[VPX_PLANE_U];
  97. yv12->color_space = img->cs;
  98. yv12->color_range = img->range;
  99. #if CONFIG_VP9_HIGHBITDEPTH
  100. if (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
  101. // In vpx_image_t
  102. // planes point to uint8 address of start of data
  103. // stride counts uint8s to reach next row
  104. // In YV12_BUFFER_CONFIG
  105. // y_buffer, u_buffer, v_buffer point to uint16 address of data
  106. // stride and border counts in uint16s
  107. // This means that all the address calculations in the main body of code
  108. // should work correctly.
  109. // However, before we do any pixel operations we need to cast the address
  110. // to a uint16 ponter and double its value.
  111. yv12->y_buffer = CONVERT_TO_BYTEPTR(yv12->y_buffer);
  112. yv12->u_buffer = CONVERT_TO_BYTEPTR(yv12->u_buffer);
  113. yv12->v_buffer = CONVERT_TO_BYTEPTR(yv12->v_buffer);
  114. yv12->y_stride >>= 1;
  115. yv12->uv_stride >>= 1;
  116. yv12->flags = YV12_FLAG_HIGHBITDEPTH;
  117. } else {
  118. yv12->flags = 0;
  119. }
  120. yv12->border = (yv12->y_stride - img->w) / 2;
  121. #else
  122. yv12->border = (img->stride[VPX_PLANE_Y] - img->w) / 2;
  123. #endif // CONFIG_VP9_HIGHBITDEPTH
  124. yv12->subsampling_x = img->x_chroma_shift;
  125. yv12->subsampling_y = img->y_chroma_shift;
  126. return VPX_CODEC_OK;
  127. }
  128. static VP9_REFFRAME ref_frame_to_vp9_reframe(vpx_ref_frame_type_t frame) {
  129. switch (frame) {
  130. case VP8_LAST_FRAME: return VP9_LAST_FLAG;
  131. case VP8_GOLD_FRAME: return VP9_GOLD_FLAG;
  132. case VP8_ALTR_FRAME: return VP9_ALT_FLAG;
  133. }
  134. assert(0 && "Invalid Reference Frame");
  135. return VP9_LAST_FLAG;
  136. }
  137. #endif // VPX_VP9_VP9_IFACE_COMMON_H_