You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.9KB

  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. #include "libavutil/common.h"
  19. #include "libavutil/opt.h"
  20. #include "bsf.h"
  21. #include "cbs.h"
  22. #include "cbs_bsf.h"
  23. #include "cbs_av1.h"
  24. typedef struct AV1MetadataContext {
  25. CBSBSFContext common;
  26. int td;
  27. int color_primaries;
  28. int transfer_characteristics;
  29. int matrix_coefficients;
  30. int color_range;
  31. int chroma_sample_position;
  32. AVRational tick_rate;
  33. int num_ticks_per_picture;
  34. int delete_padding;
  35. } AV1MetadataContext;
  36. static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
  37. AV1RawSequenceHeader *seq)
  38. {
  39. AV1MetadataContext *ctx = bsf->priv_data;
  40. AV1RawColorConfig *clc = &seq->color_config;
  41. AV1RawTimingInfo *tim = &seq->timing_info;
  42. if (ctx->color_primaries >= 0 ||
  43. ctx->transfer_characteristics >= 0 ||
  44. ctx->matrix_coefficients >= 0) {
  45. clc->color_description_present_flag = 1;
  46. if (ctx->color_primaries >= 0)
  47. clc->color_primaries = ctx->color_primaries;
  48. if (ctx->transfer_characteristics >= 0)
  49. clc->transfer_characteristics = ctx->transfer_characteristics;
  50. if (ctx->matrix_coefficients >= 0)
  51. clc->matrix_coefficients = ctx->matrix_coefficients;
  52. }
  53. if (ctx->color_range >= 0) {
  54. if (clc->color_primaries == AVCOL_PRI_BT709 &&
  55. clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
  56. clc->matrix_coefficients == AVCOL_SPC_RGB) {
  57. av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
  58. "on RGB streams encoded in BT.709 sRGB.\n");
  59. } else {
  60. clc->color_range = ctx->color_range;
  61. }
  62. }
  63. if (ctx->chroma_sample_position >= 0) {
  64. if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
  65. av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
  66. "can only be set for 4:2:0 streams.\n");
  67. } else {
  68. clc->chroma_sample_position = ctx->chroma_sample_position;
  69. }
  70. }
  71. if (ctx->tick_rate.num && ctx->tick_rate.den) {
  72. int num, den;
  73. av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
  74. UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
  75. tim->time_scale = num;
  76. tim->num_units_in_display_tick = den;
  77. seq->timing_info_present_flag = 1;
  78. if (ctx->num_ticks_per_picture > 0) {
  79. tim->equal_picture_interval = 1;
  80. tim->num_ticks_per_picture_minus_1 =
  81. ctx->num_ticks_per_picture - 1;
  82. }
  83. }
  84. return 0;
  85. }
  86. static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
  87. CodedBitstreamFragment *frag)
  88. {
  89. AV1MetadataContext *ctx = bsf->priv_data;
  90. AV1RawOBU td, *obu;
  91. int err, i;
  92. for (i = 0; i < frag->nb_units; i++) {
  93. if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
  94. obu = frag->units[i].content;
  95. err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
  96. if (err < 0)
  97. return err;
  98. }
  99. }
  100. // If a Temporal Delimiter is present, it must be the first OBU.
  101. if (frag->nb_units && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
  102. if (ctx->td == BSF_ELEMENT_REMOVE)
  103. ff_cbs_delete_unit(frag, 0);
  104. } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) {
  105. td = (AV1RawOBU) {
  106. .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
  107. };
  108. err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
  109. &td, NULL);
  110. if (err < 0) {
  111. av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
  112. return err;
  113. }
  114. }
  115. if (ctx->delete_padding) {
  116. for (i = frag->nb_units - 1; i >= 0; i--) {
  117. if (frag->units[i].type == AV1_OBU_PADDING)
  118. ff_cbs_delete_unit(frag, i);
  119. }
  120. }
  121. return 0;
  122. }
  123. static const CBSBSFType av1_metadata_type = {
  124. .codec_id = AV_CODEC_ID_AV1,
  125. .fragment_name = "temporal unit",
  126. .unit_name = "OBU",
  127. .update_fragment = &av1_metadata_update_fragment,
  128. };
  129. static int av1_metadata_init(AVBSFContext *bsf)
  130. {
  131. return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type);
  132. }
  133. #define OFFSET(x) offsetof(AV1MetadataContext, x)
  134. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
  135. static const AVOption av1_metadata_options[] = {
  136. BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
  137. td, FLAGS),
  138. { "color_primaries", "Set color primaries (section 6.4.2)",
  139. OFFSET(color_primaries), AV_OPT_TYPE_INT,
  140. { .i64 = -1 }, -1, 255, FLAGS },
  141. { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
  142. OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
  143. { .i64 = -1 }, -1, 255, FLAGS },
  144. { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
  145. OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
  146. { .i64 = -1 }, -1, 255, FLAGS },
  147. { "color_range", "Set color range flag (section 6.4.2)",
  148. OFFSET(color_range), AV_OPT_TYPE_INT,
  149. { .i64 = -1 }, -1, 1, FLAGS, "cr" },
  150. { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
  151. { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
  152. { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
  153. { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
  154. { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
  155. OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
  156. { .i64 = -1 }, -1, 3, FLAGS, "csp" },
  157. { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
  158. { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
  159. { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
  160. { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
  161. { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
  162. { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
  163. { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
  164. OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
  165. { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
  166. { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
  167. OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
  168. { .i64 = -1 }, -1, INT_MAX, FLAGS },
  169. { "delete_padding", "Delete all Padding OBUs",
  170. OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
  171. { .i64 = 0 }, 0, 1, FLAGS},
  172. { NULL }
  173. };
  174. static const AVClass av1_metadata_class = {
  175. .class_name = "av1_metadata_bsf",
  176. .item_name = av_default_item_name,
  177. .option = av1_metadata_options,
  178. .version = LIBAVUTIL_VERSION_INT,
  179. };
  180. static const enum AVCodecID av1_metadata_codec_ids[] = {
  181. AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
  182. };
  183. const AVBitStreamFilter ff_av1_metadata_bsf = {
  184. .name = "av1_metadata",
  185. .priv_data_size = sizeof(AV1MetadataContext),
  186. .priv_class = &av1_metadata_class,
  187. .init = &av1_metadata_init,
  188. .close = &ff_cbs_bsf_generic_close,
  189. .filter = &ff_cbs_bsf_generic_filter,
  190. .codec_ids = av1_metadata_codec_ids,
  191. };