h266_metadata.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "bsf_internal.h"
  22. #include "cbs.h"
  23. #include "cbs_bsf.h"
  24. #include "cbs_h266.h"
  25. #include "vvc.h"
  26. #define IS_H266_SLICE(nut) (nut <= VVC_RASL_NUT || (nut >= VVC_IDR_W_RADL && nut <= VVC_GDR_NUT))
  27. typedef struct H266MetadataContext {
  28. CBSBSFContext common;
  29. H266RawAUD aud_nal;
  30. int aud;
  31. } H266MetadataContext;
  32. static int h266_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
  33. CodedBitstreamFragment *pu)
  34. {
  35. H266MetadataContext *ctx = bsf->priv_data;
  36. int err, i;
  37. // If an AUD is present, it must be the first NAL unit.
  38. if (pu->nb_units && pu->units[0].type == VVC_AUD_NUT) {
  39. if (ctx->aud == BSF_ELEMENT_REMOVE)
  40. ff_cbs_delete_unit(pu, 0);
  41. } else if ( pkt && ctx->aud == BSF_ELEMENT_INSERT) {
  42. const H266RawSlice *first_slice = NULL;
  43. const H266RawPictureHeader *ph = NULL;
  44. H266RawAUD *aud = &ctx->aud_nal;
  45. int pic_type = 0, temporal_id = 8, layer_id = 0;
  46. for (i = 0; i < pu->nb_units; i++) {
  47. const H266RawNALUnitHeader *nal = pu->units[i].content;
  48. if (!nal)
  49. continue;
  50. if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
  51. temporal_id = nal->nuh_temporal_id_plus1 - 1;
  52. if ( nal->nal_unit_type == VVC_PH_NUT ) {
  53. const H266RawPH *header = pu->units[i].content;
  54. ph = &header->ph_picture_header;
  55. } else if (IS_H266_SLICE(nal->nal_unit_type)) {
  56. const H266RawSlice *slice = pu->units[i].content;
  57. layer_id = nal->nuh_layer_id;
  58. if (slice->header.sh_slice_type == VVC_SLICE_TYPE_B &&
  59. pic_type < 2)
  60. pic_type = 2;
  61. if (slice->header.sh_slice_type == VVC_SLICE_TYPE_P &&
  62. pic_type < 1)
  63. pic_type = 1;
  64. if (!first_slice) {
  65. first_slice = slice;
  66. if (first_slice->header.
  67. sh_picture_header_in_slice_header_flag)
  68. ph = &first_slice->header.sh_picture_header;
  69. else if (!ph)
  70. break;
  71. }
  72. }
  73. }
  74. if (!ph) {
  75. av_log(bsf, AV_LOG_ERROR, "no available picture header");
  76. return AVERROR_INVALIDDATA;
  77. }
  78. aud->nal_unit_header = (H266RawNALUnitHeader) {
  79. .nal_unit_type = VVC_AUD_NUT,
  80. .nuh_layer_id = layer_id,
  81. .nuh_temporal_id_plus1 = temporal_id + 1,
  82. };
  83. aud->aud_pic_type = pic_type;
  84. aud->aud_irap_or_gdr_flag = ph->ph_gdr_or_irap_pic_flag;
  85. err = ff_cbs_insert_unit_content(pu, 0, VVC_AUD_NUT, aud, NULL);
  86. if (err < 0) {
  87. av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
  88. return err;
  89. }
  90. }
  91. /* TODO: implement more metadata parsing, like VUI, Levels etc. */
  92. //for (i = 0; i < pu->nb_units; i++) {
  93. // if (pu->units[i].type == VVC_SPS_NUT) {
  94. // }
  95. //}
  96. return 0;
  97. }
  98. static const CBSBSFType h266_metadata_type = {
  99. .codec_id = AV_CODEC_ID_VVC,
  100. .fragment_name = "access unit",
  101. .unit_name = "NAL unit",
  102. .update_fragment = &h266_metadata_update_fragment,
  103. };
  104. static int h266_metadata_init(AVBSFContext *bsf)
  105. {
  106. return ff_cbs_bsf_generic_init(bsf, &h266_metadata_type);
  107. }
  108. #define OFFSET(x) offsetof(H266MetadataContext, x)
  109. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
  110. static const AVOption h266_metadata_options[] = {
  111. BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units",
  112. aud, FLAGS),
  113. { NULL }
  114. };
  115. static const AVClass h266_metadata_class = {
  116. .class_name = "h266_metadata_bsf",
  117. .item_name = av_default_item_name,
  118. .option = h266_metadata_options,
  119. .version = LIBAVUTIL_VERSION_INT,
  120. };
  121. static const enum AVCodecID h266_metadata_codec_ids[] = {
  122. AV_CODEC_ID_VVC, AV_CODEC_ID_NONE,
  123. };
  124. const FFBitStreamFilter ff_vvc_metadata_bsf = {
  125. .p.name = "vvc_metadata",
  126. .p.codec_ids = h266_metadata_codec_ids,
  127. .p.priv_class = &h266_metadata_class,
  128. .priv_data_size = sizeof(H266MetadataContext),
  129. .init = &h266_metadata_init,
  130. .close = &ff_cbs_bsf_generic_close,
  131. .filter = &ff_cbs_bsf_generic_filter,
  132. };