FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/h266_metadata.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 8 46 17.4%
Functions: 2 2 100.0%
Branches: 7 42 16.7%

Line Branch Exec 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 #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 2132 static int h266_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
40 CodedBitstreamFragment *pu)
41 {
42 2132 H266MetadataContext *ctx = bsf->priv_data;
43 int err, i;
44
45 // If an AUD is present, it must be the first NAL unit.
46
3/4
✓ Branch 0 taken 2132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 2120 times.
2132 if (pu->nb_units && pu->units[0].type == VVC_AUD_NUT) {
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ctx->aud == BSF_ELEMENT_REMOVE)
48 ff_cbs_delete_unit(pu, 0);
49
3/4
✓ Branch 0 taken 2094 times.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2094 times.
2120 } else if ( pkt && ctx->aud == BSF_ELEMENT_INSERT) {
50 const H266RawSlice *first_slice = NULL;
51 const H266RawPictureHeader *ph = NULL;
52 H266RawAUD *aud = &ctx->aud_nal;
53 int pic_type = 0, temporal_id = 8, layer_id = 0;
54 for (i = 0; i < pu->nb_units; i++) {
55 const H266RawNALUnitHeader *nal = pu->units[i].content;
56 if (!nal)
57 continue;
58 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
59 temporal_id = nal->nuh_temporal_id_plus1 - 1;
60 if ( nal->nal_unit_type == VVC_PH_NUT ) {
61 const H266RawPH *header = pu->units[i].content;
62 ph = &header->ph_picture_header;
63 } else if (IS_H266_SLICE(nal->nal_unit_type)) {
64 const H266RawSlice *slice = pu->units[i].content;
65 layer_id = nal->nuh_layer_id;
66 if (slice->header.sh_slice_type == VVC_SLICE_TYPE_B &&
67 pic_type < 2)
68 pic_type = 2;
69 if (slice->header.sh_slice_type == VVC_SLICE_TYPE_P &&
70 pic_type < 1)
71 pic_type = 1;
72 if (!first_slice) {
73 first_slice = slice;
74 if (first_slice->header.
75 sh_picture_header_in_slice_header_flag)
76 ph = &first_slice->header.sh_picture_header;
77 else if (!ph)
78 break;
79 }
80 }
81 }
82 if (!ph) {
83 av_log(bsf, AV_LOG_ERROR, "no avaliable picture header");
84 return AVERROR_INVALIDDATA;
85 }
86
87 aud->nal_unit_header = (H266RawNALUnitHeader) {
88 .nal_unit_type = VVC_AUD_NUT,
89 .nuh_layer_id = layer_id,
90 .nuh_temporal_id_plus1 = temporal_id + 1,
91 };
92 aud->aud_pic_type = pic_type;
93 aud->aud_irap_or_gdr_flag = ph->ph_gdr_or_irap_pic_flag;
94
95 err = ff_cbs_insert_unit_content(pu, 0, VVC_AUD_NUT, aud, NULL);
96 if (err < 0) {
97 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
98 return err;
99 }
100 }
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 2132 return 0;
108 }
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 26 static int h266_metadata_init(AVBSFContext *bsf)
118 {
119 26 return ff_cbs_bsf_generic_init(bsf, &h266_metadata_type);
120 }
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 };
151