dovi_meta.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2020 Vacing Fang <vacingfang@tencent.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * DOVI configuration
  23. */
  24. #ifndef AVUTIL_DOVI_META_H
  25. #define AVUTIL_DOVI_META_H
  26. #include <stdint.h>
  27. #include <stddef.h>
  28. #include "rational.h"
  29. /*
  30. * DOVI configuration
  31. * ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2.1.2
  32. dolby-vision-bitstreams-in-mpeg-2-transport-stream-multiplex-v1.2
  33. * @code
  34. * uint8_t dv_version_major, the major version number that the stream complies with
  35. * uint8_t dv_version_minor, the minor version number that the stream complies with
  36. * uint8_t dv_profile, the Dolby Vision profile
  37. * uint8_t dv_level, the Dolby Vision level
  38. * uint8_t rpu_present_flag
  39. * uint8_t el_present_flag
  40. * uint8_t bl_present_flag
  41. * uint8_t dv_bl_signal_compatibility_id
  42. * @endcode
  43. *
  44. * @note The struct must be allocated with av_dovi_alloc() and
  45. * its size is not a part of the public ABI.
  46. */
  47. typedef struct AVDOVIDecoderConfigurationRecord {
  48. uint8_t dv_version_major;
  49. uint8_t dv_version_minor;
  50. uint8_t dv_profile;
  51. uint8_t dv_level;
  52. uint8_t rpu_present_flag;
  53. uint8_t el_present_flag;
  54. uint8_t bl_present_flag;
  55. uint8_t dv_bl_signal_compatibility_id;
  56. } AVDOVIDecoderConfigurationRecord;
  57. /**
  58. * Allocate a AVDOVIDecoderConfigurationRecord structure and initialize its
  59. * fields to default values.
  60. *
  61. * @return the newly allocated struct or NULL on failure
  62. */
  63. AVDOVIDecoderConfigurationRecord *av_dovi_alloc(size_t *size);
  64. /**
  65. * Dolby Vision RPU data header.
  66. *
  67. * @note sizeof(AVDOVIRpuDataHeader) is not part of the public ABI.
  68. */
  69. typedef struct AVDOVIRpuDataHeader {
  70. uint8_t rpu_type;
  71. uint16_t rpu_format;
  72. uint8_t vdr_rpu_profile;
  73. uint8_t vdr_rpu_level;
  74. uint8_t chroma_resampling_explicit_filter_flag;
  75. uint8_t coef_data_type; /* informative, lavc always converts to fixed */
  76. uint8_t coef_log2_denom;
  77. uint8_t vdr_rpu_normalized_idc;
  78. uint8_t bl_video_full_range_flag;
  79. uint8_t bl_bit_depth; /* [8, 16] */
  80. uint8_t el_bit_depth; /* [8, 16] */
  81. uint8_t vdr_bit_depth; /* [8, 16] */
  82. uint8_t spatial_resampling_filter_flag;
  83. uint8_t el_spatial_resampling_filter_flag;
  84. uint8_t disable_residual_flag;
  85. } AVDOVIRpuDataHeader;
  86. enum AVDOVIMappingMethod {
  87. AV_DOVI_MAPPING_POLYNOMIAL = 0,
  88. AV_DOVI_MAPPING_MMR = 1,
  89. };
  90. /**
  91. * Coefficients of a piece-wise function. The pieces of the function span the
  92. * value ranges between two adjacent pivot values.
  93. */
  94. #define AV_DOVI_MAX_PIECES 8
  95. typedef struct AVDOVIReshapingCurve {
  96. uint8_t num_pivots; /* [2, 9] */
  97. uint16_t pivots[AV_DOVI_MAX_PIECES + 1]; /* sorted ascending */
  98. enum AVDOVIMappingMethod mapping_idc[AV_DOVI_MAX_PIECES];
  99. /* AV_DOVI_MAPPING_POLYNOMIAL */
  100. uint8_t poly_order[AV_DOVI_MAX_PIECES]; /* [1, 2] */
  101. int64_t poly_coef[AV_DOVI_MAX_PIECES][3]; /* x^0, x^1, x^2 */
  102. /* AV_DOVI_MAPPING_MMR */
  103. uint8_t mmr_order[AV_DOVI_MAX_PIECES]; /* [1, 3] */
  104. int64_t mmr_constant[AV_DOVI_MAX_PIECES];
  105. int64_t mmr_coef[AV_DOVI_MAX_PIECES][3/* order - 1 */][7];
  106. } AVDOVIReshapingCurve;
  107. enum AVDOVINLQMethod {
  108. AV_DOVI_NLQ_NONE = -1,
  109. AV_DOVI_NLQ_LINEAR_DZ = 0,
  110. };
  111. /**
  112. * Coefficients of the non-linear inverse quantization. For the interpretation
  113. * of these, see ETSI GS CCM 001.
  114. */
  115. typedef struct AVDOVINLQParams {
  116. uint16_t nlq_offset;
  117. uint64_t vdr_in_max;
  118. /* AV_DOVI_NLQ_LINEAR_DZ */
  119. uint64_t linear_deadzone_slope;
  120. uint64_t linear_deadzone_threshold;
  121. } AVDOVINLQParams;
  122. /**
  123. * Dolby Vision RPU data mapping parameters.
  124. *
  125. * @note sizeof(AVDOVIDataMapping) is not part of the public ABI.
  126. */
  127. typedef struct AVDOVIDataMapping {
  128. uint8_t vdr_rpu_id;
  129. uint8_t mapping_color_space;
  130. uint8_t mapping_chroma_format_idc;
  131. AVDOVIReshapingCurve curves[3]; /* per component */
  132. /* Non-linear inverse quantization */
  133. enum AVDOVINLQMethod nlq_method_idc;
  134. uint32_t num_x_partitions;
  135. uint32_t num_y_partitions;
  136. AVDOVINLQParams nlq[3]; /* per component */
  137. } AVDOVIDataMapping;
  138. /**
  139. * Dolby Vision RPU colorspace metadata parameters.
  140. *
  141. * @note sizeof(AVDOVIColorMetadata) is not part of the public ABI.
  142. */
  143. typedef struct AVDOVIColorMetadata {
  144. uint8_t dm_metadata_id;
  145. uint8_t scene_refresh_flag;
  146. /**
  147. * Coefficients of the custom Dolby Vision IPT-PQ matrices. These are to be
  148. * used instead of the matrices indicated by the frame's colorspace tags.
  149. * The output of rgb_to_lms_matrix is to be fed into a BT.2020 LMS->RGB
  150. * matrix based on a Hunt-Pointer-Estevez transform, but without any
  151. * crosstalk. (See the definition of the ICtCp colorspace for more
  152. * information.)
  153. */
  154. AVRational ycc_to_rgb_matrix[9]; /* before PQ linearization */
  155. AVRational ycc_to_rgb_offset[3]; /* input offset of neutral value */
  156. AVRational rgb_to_lms_matrix[9]; /* after PQ linearization */
  157. /**
  158. * Extra signal metadata (see Dolby patents for more info).
  159. */
  160. uint16_t signal_eotf;
  161. uint16_t signal_eotf_param0;
  162. uint16_t signal_eotf_param1;
  163. uint32_t signal_eotf_param2;
  164. uint8_t signal_bit_depth;
  165. uint8_t signal_color_space;
  166. uint8_t signal_chroma_format;
  167. uint8_t signal_full_range_flag; /* [0, 3] */
  168. uint16_t source_min_pq;
  169. uint16_t source_max_pq;
  170. uint16_t source_diagonal;
  171. } AVDOVIColorMetadata;
  172. /**
  173. * Combined struct representing a combination of header, mapping and color
  174. * metadata, for attaching to frames as side data.
  175. *
  176. * @note The struct must be allocated with av_dovi_metadata_alloc() and
  177. * its size is not a part of the public ABI.
  178. */
  179. typedef struct AVDOVIMetadata {
  180. /**
  181. * Offset in bytes from the beginning of this structure at which the
  182. * respective structs start.
  183. */
  184. size_t header_offset; /* AVDOVIRpuDataHeader */
  185. size_t mapping_offset; /* AVDOVIDataMapping */
  186. size_t color_offset; /* AVDOVIColorMetadata */
  187. } AVDOVIMetadata;
  188. static av_always_inline AVDOVIRpuDataHeader *
  189. av_dovi_get_header(const AVDOVIMetadata *data)
  190. {
  191. return (AVDOVIRpuDataHeader *)((uint8_t *) data + data->header_offset);
  192. }
  193. static av_always_inline AVDOVIDataMapping *
  194. av_dovi_get_mapping(const AVDOVIMetadata *data)
  195. {
  196. return (AVDOVIDataMapping *)((uint8_t *) data + data->mapping_offset);
  197. }
  198. static av_always_inline AVDOVIColorMetadata *
  199. av_dovi_get_color(const AVDOVIMetadata *data)
  200. {
  201. return (AVDOVIColorMetadata *)((uint8_t *) data + data->color_offset);
  202. }
  203. /**
  204. * Allocate an AVDOVIMetadata structure and initialize its
  205. * fields to default values.
  206. *
  207. * @param size If this parameter is non-NULL, the size in bytes of the
  208. * allocated struct will be written here on success
  209. *
  210. * @return the newly allocated struct or NULL on failure
  211. */
  212. AVDOVIMetadata *av_dovi_metadata_alloc(size_t *size);
  213. #endif /* AVUTIL_DOVI_META_H */