video_enc_params.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_VIDEO_ENC_PARAMS_H
  19. #define AVUTIL_VIDEO_ENC_PARAMS_H
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/frame.h"
  24. enum AVVideoEncParamsType {
  25. AV_VIDEO_ENC_PARAMS_NONE = -1,
  26. /**
  27. * VP9 stores:
  28. * - per-frame base (luma AC) quantizer index, exported as AVVideoEncParams.qp
  29. * - deltas for luma DC, chroma AC and chroma DC, exported in the
  30. * corresponding entries in AVVideoEncParams.delta_qp
  31. * - per-segment delta, exported as for each block as AVVideoBlockParams.delta_qp
  32. *
  33. * To compute the resulting quantizer index for a block:
  34. * - for luma AC, add the base qp and the per-block delta_qp, saturating to
  35. * unsigned 8-bit.
  36. * - for luma DC and chroma AC/DC, add the corresponding
  37. * AVVideoBlockParams.delta_qp to the luma AC index, again saturating to
  38. * unsigned 8-bit.
  39. */
  40. AV_VIDEO_ENC_PARAMS_VP9,
  41. /**
  42. * H.264 stores:
  43. * - in PPS (per-picture):
  44. * * initial QP_Y (luma) value, exported as AVVideoEncParams.qp
  45. * * delta(s) for chroma QP values (same for both, or each separately),
  46. * exported as in the corresponding entries in AVVideoEncParams.delta_qp
  47. * - per-slice QP delta, not exported directly, added to the per-MB value
  48. * - per-MB delta; not exported directly; the final per-MB quantizer
  49. * parameter - QP_Y - minus the value in AVVideoEncParams.qp is exported
  50. * as AVVideoBlockParams.qp_delta.
  51. */
  52. AV_VIDEO_ENC_PARAMS_H264,
  53. };
  54. /**
  55. * Video encoding parameters for a given frame. This struct is allocated along
  56. * with an optional array of per-block AVVideoBlockParams descriptors.
  57. * Must be allocated with av_video_enc_params_alloc().
  58. */
  59. typedef struct AVVideoEncParams {
  60. /**
  61. * Number of blocks in the array.
  62. *
  63. * May be 0, in which case no per-block information is present. In this case
  64. * the values of blocks_offset / block_size are unspecified and should not
  65. * be accessed.
  66. */
  67. unsigned int nb_blocks;
  68. /**
  69. * Offset in bytes from the beginning of this structure at which the array
  70. * of blocks starts.
  71. */
  72. size_t blocks_offset;
  73. /*
  74. * Size of each block in bytes. May not match sizeof(AVVideoBlockParams).
  75. */
  76. size_t block_size;
  77. /**
  78. * Type of the parameters (the codec they are used with).
  79. */
  80. enum AVVideoEncParamsType type;
  81. /**
  82. * Base quantisation parameter for the frame. The final quantiser for a
  83. * given block in a given plane is obtained from this value, possibly
  84. * combined with {@code delta_qp} and the per-block delta in a manner
  85. * documented for each type.
  86. */
  87. int32_t qp;
  88. /**
  89. * Quantisation parameter offset from the base (per-frame) qp for a given
  90. * plane (first index) and AC/DC coefficients (second index).
  91. */
  92. int32_t delta_qp[4][2];
  93. } AVVideoEncParams;
  94. /**
  95. * Data structure for storing block-level encoding information.
  96. * It is allocated as a part of AVVideoEncParams and should be retrieved with
  97. * av_video_enc_params_block().
  98. *
  99. * sizeof(AVVideoBlockParams) is not a part of the ABI and new fields may be
  100. * added to it.
  101. */
  102. typedef struct AVVideoBlockParams {
  103. /**
  104. * Distance in luma pixels from the top-left corner of the visible frame
  105. * to the top-left corner of the block.
  106. * Can be negative if top/right padding is present on the coded frame.
  107. */
  108. int src_x, src_y;
  109. /**
  110. * Width and height of the block in luma pixels.
  111. */
  112. int w, h;
  113. /**
  114. * Difference between this block's final quantization parameter and the
  115. * corresponding per-frame value.
  116. */
  117. int32_t delta_qp;
  118. } AVVideoBlockParams;
  119. /*
  120. * Get the block at the specified {@code idx}. Must be between 0 and nb_blocks.
  121. */
  122. static av_always_inline AVVideoBlockParams*
  123. av_video_enc_params_block(AVVideoEncParams *par, unsigned int idx)
  124. {
  125. av_assert0(idx < par->nb_blocks);
  126. return (AVVideoBlockParams *)((uint8_t *)par + par->blocks_offset +
  127. idx * par->block_size);
  128. }
  129. /**
  130. * Allocates memory for AVVideoEncParams of the given type, plus an array of
  131. * {@code nb_blocks} AVVideoBlockParams and initializes the variables. Can be
  132. * freed with a normal av_free() call.
  133. *
  134. * @param out_size if non-NULL, the size in bytes of the resulting data array is
  135. * written here.
  136. */
  137. AVVideoEncParams *av_video_enc_params_alloc(enum AVVideoEncParamsType type,
  138. unsigned int nb_blocks, size_t *out_size);
  139. /**
  140. * Allocates memory for AVEncodeInfoFrame plus an array of
  141. * {@code nb_blocks} AVEncodeInfoBlock in the given AVFrame {@code frame}
  142. * as AVFrameSideData of type AV_FRAME_DATA_VIDEO_ENC_PARAMS
  143. * and initializes the variables.
  144. */
  145. AVVideoEncParams*
  146. av_video_enc_params_create_side_data(AVFrame *frame, enum AVVideoEncParamsType type,
  147. unsigned int nb_blocks);
  148. #endif /* AVUTIL_VIDEO_ENC_PARAMS_H */