]> git.sesse.net Git - ffmpeg/blob - libavcodec/h265_metadata_bsf.c
lavc: rename bsf.h to bsf_internal.h
[ffmpeg] / libavcodec / h265_metadata_bsf.c
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_internal.h"
23 #include "cbs.h"
24 #include "cbs_h265.h"
25 #include "hevc.h"
26 #include "h265_profile_level.h"
27
28 enum {
29     PASS,
30     INSERT,
31     REMOVE,
32 };
33
34 enum {
35     LEVEL_UNSET = -2,
36     LEVEL_AUTO  = -1,
37 };
38
39 typedef struct H265MetadataContext {
40     const AVClass *class;
41
42     CodedBitstreamContext *cbc;
43     CodedBitstreamFragment access_unit;
44
45     H265RawAUD aud_nal;
46
47     int aud;
48
49     AVRational sample_aspect_ratio;
50
51     int video_format;
52     int video_full_range_flag;
53     int colour_primaries;
54     int transfer_characteristics;
55     int matrix_coefficients;
56
57     int chroma_sample_loc_type;
58
59     AVRational tick_rate;
60     int poc_proportional_to_timing_flag;
61     int num_ticks_poc_diff_one;
62
63     int crop_left;
64     int crop_right;
65     int crop_top;
66     int crop_bottom;
67
68     int level;
69     int level_guess;
70     int level_warned;
71 } H265MetadataContext;
72
73
74 static void h265_metadata_guess_level(AVBSFContext *bsf,
75                                       const CodedBitstreamFragment *au)
76 {
77     H265MetadataContext *ctx = bsf->priv_data;
78     const H265LevelDescriptor *desc;
79     const H265RawProfileTierLevel *ptl = NULL;
80     const H265RawHRDParameters    *hrd = NULL;
81     int64_t bit_rate = 0;
82     int width = 0, height = 0;
83     int tile_cols = 0, tile_rows = 0;
84     int max_dec_pic_buffering = 0;
85     int i;
86
87     for (i = 0; i < au->nb_units; i++) {
88         const CodedBitstreamUnit *unit = &au->units[i];
89
90         if (unit->type == HEVC_NAL_VPS) {
91             const H265RawVPS *vps = unit->content;
92
93             ptl = &vps->profile_tier_level;
94             max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1;
95
96             if (vps->vps_num_hrd_parameters > 0)
97                 hrd = &vps->hrd_parameters[0];
98
99         } else if (unit->type == HEVC_NAL_SPS) {
100             const H265RawSPS *sps = unit->content;
101
102             ptl = &sps->profile_tier_level;
103             max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1;
104
105             width  = sps->pic_width_in_luma_samples;
106             height = sps->pic_height_in_luma_samples;
107
108             if (sps->vui.vui_hrd_parameters_present_flag)
109                 hrd = &sps->vui.hrd_parameters;
110
111         } else if (unit->type == HEVC_NAL_PPS) {
112             const H265RawPPS *pps = unit->content;
113
114             if (pps->tiles_enabled_flag) {
115                 tile_cols = pps->num_tile_columns_minus1 + 1;
116                 tile_rows = pps->num_tile_rows_minus1 + 1;
117             }
118         }
119     }
120
121     if (hrd) {
122         if (hrd->nal_hrd_parameters_present_flag) {
123             bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
124                        (INT64_C(1) << hrd->bit_rate_scale + 6);
125         } else if (hrd->vcl_hrd_parameters_present_flag) {
126             bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
127                        (INT64_C(1) << hrd->bit_rate_scale + 6);
128             // Adjust for VCL vs. NAL limits.
129             bit_rate = bit_rate * 11 / 10;
130         }
131     }
132
133     desc = ff_h265_guess_level(ptl, bit_rate, width, height,
134                                0, tile_rows, tile_cols,
135                                max_dec_pic_buffering);
136     if (desc) {
137         av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to "
138                "level %s.\n", desc->name);
139         ctx->level_guess = desc->level_idc;
140     }
141 }
142
143 static void h265_metadata_update_level(AVBSFContext *bsf,
144                                        uint8_t *level_idc)
145 {
146     H265MetadataContext *ctx = bsf->priv_data;
147
148     if (ctx->level != LEVEL_UNSET) {
149         if (ctx->level == LEVEL_AUTO) {
150             if (ctx->level_guess) {
151                 *level_idc = ctx->level_guess;
152             } else {
153                 if (!ctx->level_warned) {
154                     av_log(bsf, AV_LOG_WARNING, "Unable to determine level "
155                            "of stream: using level 8.5.\n");
156                     ctx->level_warned = 1;
157                 }
158                 *level_idc = 255;
159             }
160         } else {
161             *level_idc = ctx->level;
162         }
163     }
164 }
165
166 static int h265_metadata_update_vps(AVBSFContext *bsf,
167                                     H265RawVPS *vps)
168 {
169     H265MetadataContext *ctx = bsf->priv_data;
170
171     if (ctx->tick_rate.num && ctx->tick_rate.den) {
172         int num, den;
173
174         av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
175                   UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
176
177         vps->vps_time_scale        = num;
178         vps->vps_num_units_in_tick = den;
179
180         vps->vps_timing_info_present_flag = 1;
181
182         if (ctx->num_ticks_poc_diff_one > 0) {
183             vps->vps_num_ticks_poc_diff_one_minus1 =
184                 ctx->num_ticks_poc_diff_one - 1;
185             vps->vps_poc_proportional_to_timing_flag = 1;
186         } else if (ctx->num_ticks_poc_diff_one == 0) {
187             vps->vps_poc_proportional_to_timing_flag = 0;
188         }
189     }
190
191     h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc);
192
193     return 0;
194 }
195
196 static int h265_metadata_update_sps(AVBSFContext *bsf,
197                                     H265RawSPS *sps)
198 {
199     H265MetadataContext *ctx = bsf->priv_data;
200     int need_vui = 0;
201     int crop_unit_x, crop_unit_y;
202
203     if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
204         // Table E-1.
205         static const AVRational sar_idc[] = {
206             {   0,  0 }, // Unspecified (never written here).
207             {   1,  1 }, {  12, 11 }, {  10, 11 }, {  16, 11 },
208             {  40, 33 }, {  24, 11 }, {  20, 11 }, {  32, 11 },
209             {  80, 33 }, {  18, 11 }, {  15, 11 }, {  64, 33 },
210             { 160, 99 }, {   4,  3 }, {   3,  2 }, {   2,  1 },
211         };
212         int num, den, i;
213
214         av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
215                   ctx->sample_aspect_ratio.den, 65535);
216
217         for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
218             if (num == sar_idc[i].num &&
219                 den == sar_idc[i].den)
220                 break;
221         }
222         if (i == FF_ARRAY_ELEMS(sar_idc)) {
223             sps->vui.aspect_ratio_idc = 255;
224             sps->vui.sar_width  = num;
225             sps->vui.sar_height = den;
226         } else {
227             sps->vui.aspect_ratio_idc = i;
228         }
229         sps->vui.aspect_ratio_info_present_flag = 1;
230         need_vui = 1;
231     }
232
233 #define SET_OR_INFER(field, value, present_flag, infer) do { \
234         if (value >= 0) { \
235             field = value; \
236             need_vui = 1; \
237         } else if (!present_flag) \
238             field = infer; \
239     } while (0)
240
241     if (ctx->video_format             >= 0 ||
242         ctx->video_full_range_flag    >= 0 ||
243         ctx->colour_primaries         >= 0 ||
244         ctx->transfer_characteristics >= 0 ||
245         ctx->matrix_coefficients      >= 0) {
246
247         SET_OR_INFER(sps->vui.video_format, ctx->video_format,
248                      sps->vui.video_signal_type_present_flag, 5);
249
250         SET_OR_INFER(sps->vui.video_full_range_flag,
251                      ctx->video_full_range_flag,
252                      sps->vui.video_signal_type_present_flag, 0);
253
254         if (ctx->colour_primaries         >= 0 ||
255             ctx->transfer_characteristics >= 0 ||
256             ctx->matrix_coefficients      >= 0) {
257
258             SET_OR_INFER(sps->vui.colour_primaries,
259                          ctx->colour_primaries,
260                          sps->vui.colour_description_present_flag, 2);
261
262             SET_OR_INFER(sps->vui.transfer_characteristics,
263                          ctx->transfer_characteristics,
264                          sps->vui.colour_description_present_flag, 2);
265
266             SET_OR_INFER(sps->vui.matrix_coefficients,
267                          ctx->matrix_coefficients,
268                          sps->vui.colour_description_present_flag, 2);
269
270             sps->vui.colour_description_present_flag = 1;
271         }
272         sps->vui.video_signal_type_present_flag = 1;
273         need_vui = 1;
274     }
275
276     if (ctx->chroma_sample_loc_type >= 0) {
277         sps->vui.chroma_sample_loc_type_top_field =
278             ctx->chroma_sample_loc_type;
279         sps->vui.chroma_sample_loc_type_bottom_field =
280             ctx->chroma_sample_loc_type;
281         sps->vui.chroma_loc_info_present_flag = 1;
282         need_vui = 1;
283     }
284
285     if (ctx->tick_rate.num && ctx->tick_rate.den) {
286         int num, den;
287
288         av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
289                   UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
290
291         sps->vui.vui_time_scale        = num;
292         sps->vui.vui_num_units_in_tick = den;
293
294         sps->vui.vui_timing_info_present_flag = 1;
295         need_vui = 1;
296
297         if (ctx->num_ticks_poc_diff_one > 0) {
298             sps->vui.vui_num_ticks_poc_diff_one_minus1 =
299                 ctx->num_ticks_poc_diff_one - 1;
300             sps->vui.vui_poc_proportional_to_timing_flag = 1;
301         } else if (ctx->num_ticks_poc_diff_one == 0) {
302             sps->vui.vui_poc_proportional_to_timing_flag = 0;
303         }
304     }
305
306     if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
307         crop_unit_x = 1;
308         crop_unit_y = 1;
309     } else {
310         crop_unit_x = 1 + (sps->chroma_format_idc < 3);
311         crop_unit_y = 1 + (sps->chroma_format_idc < 2);
312     }
313 #define CROP(border, unit) do { \
314         if (ctx->crop_ ## border >= 0) { \
315             if (ctx->crop_ ## border % unit != 0) { \
316                 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
317                        "must be a multiple of %d.\n", #border, unit); \
318                 return AVERROR(EINVAL); \
319             } \
320             sps->conf_win_ ## border ## _offset = \
321                 ctx->crop_ ## border / unit; \
322             sps->conformance_window_flag = 1; \
323         } \
324     } while (0)
325     CROP(left,   crop_unit_x);
326     CROP(right,  crop_unit_x);
327     CROP(top,    crop_unit_y);
328     CROP(bottom, crop_unit_y);
329 #undef CROP
330
331     if (need_vui)
332         sps->vui_parameters_present_flag = 1;
333
334     h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc);
335
336     return 0;
337 }
338
339 static int h265_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
340 {
341     H265MetadataContext *ctx = bsf->priv_data;
342     CodedBitstreamFragment *au = &ctx->access_unit;
343     uint8_t *side_data;
344     int side_data_size;
345     int err, i;
346
347     side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
348                                         &side_data_size);
349     if (!side_data_size)
350         return 0;
351
352     err = ff_cbs_read(ctx->cbc, au, side_data, side_data_size);
353     if (err < 0) {
354         av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
355         return err;
356     }
357
358     if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
359         h265_metadata_guess_level(bsf, au);
360
361     for (i = 0; i < au->nb_units; i++) {
362         if (au->units[i].type == HEVC_NAL_VPS) {
363             err = h265_metadata_update_vps(bsf, au->units[i].content);
364             if (err < 0)
365                 return err;
366         }
367         if (au->units[i].type == HEVC_NAL_SPS) {
368             err = h265_metadata_update_sps(bsf, au->units[i].content);
369             if (err < 0)
370                 return err;
371         }
372     }
373
374     err = ff_cbs_write_fragment_data(ctx->cbc, au);
375     if (err < 0) {
376         av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
377         return err;
378     }
379
380     side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, au->data_size);
381     if (!side_data)
382         return AVERROR(ENOMEM);
383     memcpy(side_data, au->data, au->data_size);
384
385     ff_cbs_fragment_reset(ctx->cbc, au);
386
387     return 0;
388 }
389
390 static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
391 {
392     H265MetadataContext *ctx = bsf->priv_data;
393     CodedBitstreamFragment *au = &ctx->access_unit;
394     int err, i;
395
396     err = ff_bsf_get_packet_ref(bsf, pkt);
397     if (err < 0)
398         return err;
399
400     err = h265_metadata_update_side_data(bsf, pkt);
401     if (err < 0)
402         goto fail;
403
404     err = ff_cbs_read_packet(ctx->cbc, au, pkt);
405     if (err < 0) {
406         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
407         goto fail;
408     }
409
410     if (au->nb_units == 0) {
411         av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
412         err = AVERROR_INVALIDDATA;
413         goto fail;
414     }
415
416     // If an AUD is present, it must be the first NAL unit.
417     if (au->units[0].type == HEVC_NAL_AUD) {
418         if (ctx->aud == REMOVE)
419             ff_cbs_delete_unit(ctx->cbc, au, 0);
420     } else {
421         if (ctx->aud == INSERT) {
422             H265RawAUD *aud = &ctx->aud_nal;
423             int pic_type = 0, temporal_id = 8, layer_id = 0;
424
425             for (i = 0; i < au->nb_units; i++) {
426                 const H265RawNALUnitHeader *nal = au->units[i].content;
427                 if (!nal)
428                     continue;
429                 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
430                     temporal_id = nal->nuh_temporal_id_plus1 - 1;
431
432                 if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
433                     const H265RawSlice *slice = au->units[i].content;
434                     layer_id = nal->nuh_layer_id;
435                     if (slice->header.slice_type == HEVC_SLICE_B &&
436                         pic_type < 2)
437                         pic_type = 2;
438                     if (slice->header.slice_type == HEVC_SLICE_P &&
439                         pic_type < 1)
440                         pic_type = 1;
441                 }
442             }
443
444             aud->nal_unit_header = (H265RawNALUnitHeader) {
445                 .nal_unit_type         = HEVC_NAL_AUD,
446                 .nuh_layer_id          = layer_id,
447                 .nuh_temporal_id_plus1 = temporal_id + 1,
448             };
449             aud->pic_type = pic_type;
450
451             err = ff_cbs_insert_unit_content(ctx->cbc, au,
452                                              0, HEVC_NAL_AUD, aud, NULL);
453             if (err < 0) {
454                 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
455                 goto fail;
456             }
457         }
458     }
459
460     if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
461         h265_metadata_guess_level(bsf, au);
462
463     for (i = 0; i < au->nb_units; i++) {
464         if (au->units[i].type == HEVC_NAL_VPS) {
465             err = h265_metadata_update_vps(bsf, au->units[i].content);
466             if (err < 0)
467                 goto fail;
468         }
469         if (au->units[i].type == HEVC_NAL_SPS) {
470             err = h265_metadata_update_sps(bsf, au->units[i].content);
471             if (err < 0)
472                 goto fail;
473         }
474     }
475
476     err = ff_cbs_write_packet(ctx->cbc, pkt, au);
477     if (err < 0) {
478         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
479         goto fail;
480     }
481
482     err = 0;
483 fail:
484     ff_cbs_fragment_reset(ctx->cbc, au);
485
486     if (err < 0)
487         av_packet_unref(pkt);
488
489     return err;
490 }
491
492 static int h265_metadata_init(AVBSFContext *bsf)
493 {
494     H265MetadataContext *ctx = bsf->priv_data;
495     CodedBitstreamFragment *au = &ctx->access_unit;
496     int err, i;
497
498     err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_HEVC, bsf);
499     if (err < 0)
500         return err;
501
502     if (bsf->par_in->extradata) {
503         err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
504         if (err < 0) {
505             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
506             goto fail;
507         }
508
509         if (ctx->level == LEVEL_AUTO)
510             h265_metadata_guess_level(bsf, au);
511
512         for (i = 0; i < au->nb_units; i++) {
513             if (au->units[i].type == HEVC_NAL_VPS) {
514                 err = h265_metadata_update_vps(bsf, au->units[i].content);
515                 if (err < 0)
516                     goto fail;
517             }
518             if (au->units[i].type == HEVC_NAL_SPS) {
519                 err = h265_metadata_update_sps(bsf, au->units[i].content);
520                 if (err < 0)
521                     goto fail;
522             }
523         }
524
525         err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
526         if (err < 0) {
527             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
528             goto fail;
529         }
530     }
531
532     err = 0;
533 fail:
534     ff_cbs_fragment_reset(ctx->cbc, au);
535     return err;
536 }
537
538 static void h265_metadata_close(AVBSFContext *bsf)
539 {
540     H265MetadataContext *ctx = bsf->priv_data;
541
542     ff_cbs_fragment_free(ctx->cbc, &ctx->access_unit);
543     ff_cbs_close(&ctx->cbc);
544 }
545
546 #define OFFSET(x) offsetof(H265MetadataContext, x)
547 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
548 static const AVOption h265_metadata_options[] = {
549     { "aud", "Access Unit Delimiter NAL units",
550         OFFSET(aud), AV_OPT_TYPE_INT,
551         { .i64 = PASS }, PASS, REMOVE, FLAGS, "aud" },
552     { "pass",   NULL, 0, AV_OPT_TYPE_CONST,
553         { .i64 = PASS   }, .flags = FLAGS, .unit = "aud" },
554     { "insert", NULL, 0, AV_OPT_TYPE_CONST,
555         { .i64 = INSERT }, .flags = FLAGS, .unit = "aud" },
556     { "remove", NULL, 0, AV_OPT_TYPE_CONST,
557         { .i64 = REMOVE }, .flags = FLAGS, .unit = "aud" },
558
559     { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
560         OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
561         { .dbl = 0.0 }, 0, 65535, FLAGS },
562
563     { "video_format", "Set video format (table E-2)",
564         OFFSET(video_format), AV_OPT_TYPE_INT,
565         { .i64 = -1 }, -1, 7, FLAGS },
566     { "video_full_range_flag", "Set video full range flag",
567         OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
568         { .i64 = -1 }, -1, 1, FLAGS },
569     { "colour_primaries", "Set colour primaries (table E-3)",
570         OFFSET(colour_primaries), AV_OPT_TYPE_INT,
571         { .i64 = -1 }, -1, 255, FLAGS },
572     { "transfer_characteristics", "Set transfer characteristics (table E-4)",
573         OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
574         { .i64 = -1 }, -1, 255, FLAGS },
575     { "matrix_coefficients", "Set matrix coefficients (table E-5)",
576         OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
577         { .i64 = -1 }, -1, 255, FLAGS },
578
579     { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
580         OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
581         { .i64 = -1 }, -1, 6, FLAGS },
582
583     { "tick_rate",
584         "Set VPS and VUI tick rate (num_units_in_tick / time_scale)",
585         OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
586         { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
587     { "num_ticks_poc_diff_one",
588         "Set VPS and VUI number of ticks per POC increment",
589         OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
590         { .i64 = -1 }, -1, INT_MAX, FLAGS },
591
592     { "crop_left", "Set left border crop offset",
593         OFFSET(crop_left), AV_OPT_TYPE_INT,
594         { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
595     { "crop_right", "Set right border crop offset",
596         OFFSET(crop_right), AV_OPT_TYPE_INT,
597         { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
598     { "crop_top", "Set top border crop offset",
599         OFFSET(crop_top), AV_OPT_TYPE_INT,
600         { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
601     { "crop_bottom", "Set bottom border crop offset",
602         OFFSET(crop_bottom), AV_OPT_TYPE_INT,
603         { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
604
605     { "level", "Set level (tables A.6 and A.7)",
606         OFFSET(level), AV_OPT_TYPE_INT,
607         { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
608     { "auto", "Attempt to guess level from stream properties",
609         0, AV_OPT_TYPE_CONST,
610         { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
611 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
612         { .i64 = value },      .flags = FLAGS, .unit = "level"
613     { LEVEL("1",    30) },
614     { LEVEL("2",    60) },
615     { LEVEL("2.1",  63) },
616     { LEVEL("3",    90) },
617     { LEVEL("3.1",  93) },
618     { LEVEL("4",   120) },
619     { LEVEL("4.1", 123) },
620     { LEVEL("5",   150) },
621     { LEVEL("5.1", 153) },
622     { LEVEL("5.2", 156) },
623     { LEVEL("6",   180) },
624     { LEVEL("6.1", 183) },
625     { LEVEL("6.2", 186) },
626     { LEVEL("8.5", 255) },
627 #undef LEVEL
628
629     { NULL }
630 };
631
632 static const AVClass h265_metadata_class = {
633     .class_name = "h265_metadata_bsf",
634     .item_name  = av_default_item_name,
635     .option     = h265_metadata_options,
636     .version    = LIBAVUTIL_VERSION_INT,
637 };
638
639 static const enum AVCodecID h265_metadata_codec_ids[] = {
640     AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
641 };
642
643 const AVBitStreamFilter ff_hevc_metadata_bsf = {
644     .name           = "hevc_metadata",
645     .priv_data_size = sizeof(H265MetadataContext),
646     .priv_class     = &h265_metadata_class,
647     .init           = &h265_metadata_init,
648     .close          = &h265_metadata_close,
649     .filter         = &h265_metadata_filter,
650     .codec_ids      = h265_metadata_codec_ids,
651 };