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