]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_metadata_bsf.c
cbs_h2645: Merge SEI message handling in common between codecs
[ffmpeg] / libavcodec / h264_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/avstring.h"
20 #include "libavutil/display.h"
21 #include "libavutil/common.h"
22 #include "libavutil/opt.h"
23
24 #include "bsf.h"
25 #include "bsf_internal.h"
26 #include "cbs.h"
27 #include "cbs_h264.h"
28 #include "h264.h"
29 #include "h264_levels.h"
30 #include "h264_sei.h"
31
32 enum {
33     PASS,
34     INSERT,
35     REMOVE,
36     EXTRACT,
37 };
38
39 enum {
40     FLIP_HORIZONTAL = 1,
41     FLIP_VERTICAL   = 2,
42 };
43
44 enum {
45     LEVEL_UNSET = -2,
46     LEVEL_AUTO  = -1,
47 };
48
49 typedef struct H264MetadataContext {
50     const AVClass *class;
51
52     CodedBitstreamContext *input;
53     CodedBitstreamContext *output;
54     CodedBitstreamFragment access_unit;
55
56     int done_first_au;
57
58     int aud;
59
60     AVRational sample_aspect_ratio;
61
62     int overscan_appropriate_flag;
63
64     int video_format;
65     int video_full_range_flag;
66     int colour_primaries;
67     int transfer_characteristics;
68     int matrix_coefficients;
69
70     int chroma_sample_loc_type;
71
72     AVRational tick_rate;
73     int fixed_frame_rate_flag;
74
75     int crop_left;
76     int crop_right;
77     int crop_top;
78     int crop_bottom;
79
80     const char *sei_user_data;
81     SEIRawUserDataUnregistered sei_user_data_payload;
82
83     int delete_filler;
84
85     int display_orientation;
86     double rotate;
87     int flip;
88     H264RawSEIDisplayOrientation display_orientation_payload;
89
90     int level;
91 } H264MetadataContext;
92
93
94 static int h264_metadata_update_sps(AVBSFContext *bsf,
95                                     H264RawSPS *sps)
96 {
97     H264MetadataContext *ctx = bsf->priv_data;
98     int need_vui = 0;
99     int crop_unit_x, crop_unit_y;
100
101     if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
102         // Table E-1.
103         static const AVRational sar_idc[] = {
104             {   0,  0 }, // Unspecified (never written here).
105             {   1,  1 }, {  12, 11 }, {  10, 11 }, {  16, 11 },
106             {  40, 33 }, {  24, 11 }, {  20, 11 }, {  32, 11 },
107             {  80, 33 }, {  18, 11 }, {  15, 11 }, {  64, 33 },
108             { 160, 99 }, {   4,  3 }, {   3,  2 }, {   2,  1 },
109         };
110         int num, den, i;
111
112         av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
113                   ctx->sample_aspect_ratio.den, 65535);
114
115         for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
116             if (num == sar_idc[i].num &&
117                 den == sar_idc[i].den)
118                 break;
119         }
120         if (i == FF_ARRAY_ELEMS(sar_idc)) {
121             sps->vui.aspect_ratio_idc = 255;
122             sps->vui.sar_width  = num;
123             sps->vui.sar_height = den;
124         } else {
125             sps->vui.aspect_ratio_idc = i;
126         }
127         sps->vui.aspect_ratio_info_present_flag = 1;
128         need_vui = 1;
129     }
130
131 #define SET_VUI_FIELD(field) do { \
132         if (ctx->field >= 0) { \
133             sps->vui.field = ctx->field; \
134             need_vui = 1; \
135         } \
136     } while (0)
137
138     if (ctx->overscan_appropriate_flag >= 0) {
139         SET_VUI_FIELD(overscan_appropriate_flag);
140         sps->vui.overscan_info_present_flag = 1;
141     }
142
143     if (ctx->video_format             >= 0 ||
144         ctx->video_full_range_flag    >= 0 ||
145         ctx->colour_primaries         >= 0 ||
146         ctx->transfer_characteristics >= 0 ||
147         ctx->matrix_coefficients      >= 0) {
148
149         SET_VUI_FIELD(video_format);
150
151         SET_VUI_FIELD(video_full_range_flag);
152
153         if (ctx->colour_primaries         >= 0 ||
154             ctx->transfer_characteristics >= 0 ||
155             ctx->matrix_coefficients      >= 0) {
156
157             SET_VUI_FIELD(colour_primaries);
158             SET_VUI_FIELD(transfer_characteristics);
159             SET_VUI_FIELD(matrix_coefficients);
160
161             sps->vui.colour_description_present_flag = 1;
162         }
163         sps->vui.video_signal_type_present_flag = 1;
164     }
165
166     if (ctx->chroma_sample_loc_type >= 0) {
167         sps->vui.chroma_sample_loc_type_top_field =
168             ctx->chroma_sample_loc_type;
169         sps->vui.chroma_sample_loc_type_bottom_field =
170             ctx->chroma_sample_loc_type;
171         sps->vui.chroma_loc_info_present_flag = 1;
172         need_vui = 1;
173     }
174
175     if (ctx->tick_rate.num && ctx->tick_rate.den) {
176         int num, den;
177
178         av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
179                   UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
180
181         sps->vui.time_scale        = num;
182         sps->vui.num_units_in_tick = den;
183
184         sps->vui.timing_info_present_flag = 1;
185         need_vui = 1;
186     }
187     SET_VUI_FIELD(fixed_frame_rate_flag);
188
189     if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
190         crop_unit_x = 1;
191         crop_unit_y = 2 - sps->frame_mbs_only_flag;
192     } else {
193         crop_unit_x = 1 + (sps->chroma_format_idc < 3);
194         crop_unit_y = (1 + (sps->chroma_format_idc < 2)) *
195                        (2 - sps->frame_mbs_only_flag);
196     }
197 #define CROP(border, unit) do { \
198         if (ctx->crop_ ## border >= 0) { \
199             if (ctx->crop_ ## border % unit != 0) { \
200                 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
201                        "must be a multiple of %d.\n", #border, unit); \
202                 return AVERROR(EINVAL); \
203             } \
204             sps->frame_crop_ ## border ## _offset = \
205                   ctx->crop_ ## border / unit; \
206             sps->frame_cropping_flag = 1; \
207         } \
208     } while (0)
209     CROP(left,   crop_unit_x);
210     CROP(right,  crop_unit_x);
211     CROP(top,    crop_unit_y);
212     CROP(bottom, crop_unit_y);
213 #undef CROP
214
215     if (ctx->level != LEVEL_UNSET) {
216         int level_idc;
217
218         if (ctx->level == LEVEL_AUTO) {
219             const H264LevelDescriptor *desc;
220             int64_t bit_rate;
221             int width, height, dpb_frames;
222             int framerate;
223
224             if (sps->vui.nal_hrd_parameters_present_flag) {
225                 bit_rate = (sps->vui.nal_hrd_parameters.bit_rate_value_minus1[0] + 1) *
226                     (INT64_C(1) << (sps->vui.nal_hrd_parameters.bit_rate_scale + 6));
227             } else if (sps->vui.vcl_hrd_parameters_present_flag) {
228                 bit_rate = (sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] + 1) *
229                     (INT64_C(1) << (sps->vui.vcl_hrd_parameters.bit_rate_scale + 6));
230                 // Adjust for VCL vs. NAL limits.
231                 bit_rate = bit_rate * 6 / 5;
232             } else {
233                 bit_rate = 0;
234             }
235
236             // Don't use max_dec_frame_buffering if it is only inferred.
237             dpb_frames = sps->vui.bitstream_restriction_flag ?
238                 sps->vui.max_dec_frame_buffering : H264_MAX_DPB_FRAMES;
239
240             width  = 16 * (sps->pic_width_in_mbs_minus1 + 1);
241             height = 16 * (sps->pic_height_in_map_units_minus1 + 1) *
242                 (2 - sps->frame_mbs_only_flag);
243
244             if (sps->vui.timing_info_present_flag)
245                 framerate = sps->vui.time_scale / sps->vui.num_units_in_tick / 2;
246             else
247                 framerate = 0;
248
249             desc = ff_h264_guess_level(sps->profile_idc, bit_rate, framerate,
250                                        width, height, dpb_frames);
251             if (desc) {
252                 level_idc = desc->level_idc;
253             } else {
254                 av_log(bsf, AV_LOG_WARNING, "Stream does not appear to "
255                        "conform to any level: using level 6.2.\n");
256                 level_idc = 62;
257             }
258         } else {
259             level_idc = ctx->level;
260         }
261
262         if (level_idc == 9) {
263             if (sps->profile_idc == 66 ||
264                 sps->profile_idc == 77 ||
265                 sps->profile_idc == 88) {
266                 sps->level_idc = 11;
267                 sps->constraint_set3_flag = 1;
268             } else {
269                 sps->level_idc = 9;
270             }
271         } else {
272             sps->level_idc = level_idc;
273         }
274     }
275
276     if (need_vui)
277         sps->vui_parameters_present_flag = 1;
278
279     return 0;
280 }
281
282 static int h264_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
283 {
284     H264MetadataContext *ctx = bsf->priv_data;
285     CodedBitstreamFragment *au = &ctx->access_unit;
286     uint8_t *side_data;
287     int side_data_size;
288     int err, i;
289
290     side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
291                                         &side_data_size);
292     if (!side_data_size)
293         return 0;
294
295     err = ff_cbs_read(ctx->input, au, side_data, side_data_size);
296     if (err < 0) {
297         av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
298         return err;
299     }
300
301     for (i = 0; i < au->nb_units; i++) {
302         if (au->units[i].type == H264_NAL_SPS) {
303             err = h264_metadata_update_sps(bsf, au->units[i].content);
304             if (err < 0)
305                 return err;
306         }
307     }
308
309     err = ff_cbs_write_fragment_data(ctx->output, au);
310     if (err < 0) {
311         av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
312         return err;
313     }
314
315     side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, au->data_size);
316     if (!side_data)
317         return AVERROR(ENOMEM);
318     memcpy(side_data, au->data, au->data_size);
319
320     ff_cbs_fragment_reset(au);
321
322     return 0;
323 }
324
325 static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
326 {
327     H264MetadataContext *ctx = bsf->priv_data;
328     CodedBitstreamFragment *au = &ctx->access_unit;
329     int err, i, j, has_sps;
330     H264RawAUD aud;
331
332     err = ff_bsf_get_packet_ref(bsf, pkt);
333     if (err < 0)
334         return err;
335
336     err = h264_metadata_update_side_data(bsf, pkt);
337     if (err < 0)
338         goto fail;
339
340     err = ff_cbs_read_packet(ctx->input, au, pkt);
341     if (err < 0) {
342         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
343         goto fail;
344     }
345
346     if (au->nb_units == 0) {
347         av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
348         err = AVERROR_INVALIDDATA;
349         goto fail;
350     }
351
352     // If an AUD is present, it must be the first NAL unit.
353     if (au->units[0].type == H264_NAL_AUD) {
354         if (ctx->aud == REMOVE)
355             ff_cbs_delete_unit(au, 0);
356     } else {
357         if (ctx->aud == INSERT) {
358             static const int primary_pic_type_table[] = {
359                 0x084, // 2, 7
360                 0x0a5, // 0, 2, 5, 7
361                 0x0e7, // 0, 1, 2, 5, 6, 7
362                 0x210, // 4, 9
363                 0x318, // 3, 4, 8, 9
364                 0x294, // 2, 4, 7, 9
365                 0x3bd, // 0, 2, 3, 4, 5, 7, 8, 9
366                 0x3ff, // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
367             };
368             int primary_pic_type_mask = 0xff;
369
370             for (i = 0; i < au->nb_units; i++) {
371                 if (au->units[i].type == H264_NAL_SLICE ||
372                     au->units[i].type == H264_NAL_IDR_SLICE) {
373                     H264RawSlice *slice = au->units[i].content;
374                     for (j = 0; j < FF_ARRAY_ELEMS(primary_pic_type_table); j++) {
375                          if (!(primary_pic_type_table[j] &
376                                (1 << slice->header.slice_type)))
377                              primary_pic_type_mask &= ~(1 << j);
378                     }
379                 }
380             }
381             for (j = 0; j < FF_ARRAY_ELEMS(primary_pic_type_table); j++)
382                 if (primary_pic_type_mask & (1 << j))
383                     break;
384             if (j >= FF_ARRAY_ELEMS(primary_pic_type_table)) {
385                 av_log(bsf, AV_LOG_ERROR, "No usable primary_pic_type: "
386                        "invalid slice types?\n");
387                 err = AVERROR_INVALIDDATA;
388                 goto fail;
389             }
390
391             aud = (H264RawAUD) {
392                 .nal_unit_header.nal_unit_type = H264_NAL_AUD,
393                 .primary_pic_type = j,
394             };
395
396             err = ff_cbs_insert_unit_content(au,
397                                              0, H264_NAL_AUD, &aud, NULL);
398             if (err < 0) {
399                 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
400                 goto fail;
401             }
402         }
403     }
404
405     has_sps = 0;
406     for (i = 0; i < au->nb_units; i++) {
407         if (au->units[i].type == H264_NAL_SPS) {
408             err = h264_metadata_update_sps(bsf, au->units[i].content);
409             if (err < 0)
410                 goto fail;
411             has_sps = 1;
412         }
413     }
414
415     // Only insert the SEI in access units containing SPSs, and also
416     // unconditionally in the first access unit we ever see.
417     if (ctx->sei_user_data && (has_sps || !ctx->done_first_au)) {
418         err = ff_cbs_sei_add_message(ctx->output, au, 1,
419                                      SEI_TYPE_USER_DATA_UNREGISTERED,
420                                      &ctx->sei_user_data_payload, NULL);
421         if (err < 0) {
422             av_log(bsf, AV_LOG_ERROR, "Failed to add user data SEI "
423                    "message to access unit.\n");
424             goto fail;
425         }
426     }
427
428     if (ctx->delete_filler) {
429         for (i = au->nb_units - 1; i >= 0; i--) {
430             if (au->units[i].type == H264_NAL_FILLER_DATA) {
431                 ff_cbs_delete_unit(au, i);
432                 continue;
433             }
434         }
435
436         ff_cbs_sei_delete_message_type(ctx->output, au,
437                                        SEI_TYPE_FILLER_PAYLOAD);
438     }
439
440     if (ctx->display_orientation != PASS) {
441         SEIRawMessage *message = NULL;
442         while (ff_cbs_sei_find_message(ctx->output, au,
443                                        SEI_TYPE_DISPLAY_ORIENTATION,
444                                        &message) == 0) {
445             H264RawSEIDisplayOrientation *disp = message->payload;
446             int32_t *matrix;
447
448             matrix = av_malloc(9 * sizeof(int32_t));
449             if (!matrix) {
450                 err = AVERROR(ENOMEM);
451                 goto fail;
452             }
453
454             av_display_rotation_set(matrix,
455                                     disp->anticlockwise_rotation *
456                                     180.0 / 65536.0);
457             av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip);
458
459             // If there are multiple display orientation messages in an
460             // access unit, then the last one added to the packet (i.e.
461             // the first one in the access unit) will prevail.
462             err = av_packet_add_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX,
463                                           (uint8_t*)matrix,
464                                           9 * sizeof(int32_t));
465             if (err < 0) {
466                 av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted "
467                        "displaymatrix side data to packet.\n");
468                 av_free(matrix);
469                 goto fail;
470             }
471         }
472
473         if (ctx->display_orientation == REMOVE ||
474             ctx->display_orientation == INSERT) {
475             ff_cbs_sei_delete_message_type(ctx->output, au,
476                                            SEI_TYPE_DISPLAY_ORIENTATION);
477         }
478     }
479     if (ctx->display_orientation == INSERT) {
480         H264RawSEIDisplayOrientation *disp =
481             &ctx->display_orientation_payload;
482         uint8_t *data;
483         int size;
484         int write = 0;
485
486         data = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size);
487         if (data && size >= 9 * sizeof(int32_t)) {
488             int32_t matrix[9];
489             int hflip, vflip;
490             double angle;
491
492             memcpy(matrix, data, sizeof(matrix));
493
494             hflip = vflip = 0;
495             if (matrix[0] < 0 && matrix[4] > 0)
496                 hflip = 1;
497             else if (matrix[0] > 0 && matrix[4] < 0)
498                 vflip = 1;
499             av_display_matrix_flip(matrix, hflip, vflip);
500
501             angle = av_display_rotation_get(matrix);
502
503             if (!(angle >= -180.0 && angle <= 180.0 /* also excludes NaN */) ||
504                 matrix[2] != 0 || matrix[5] != 0 ||
505                 matrix[6] != 0 || matrix[7] != 0) {
506                 av_log(bsf, AV_LOG_WARNING, "Input display matrix is not "
507                        "representable in H.264 parameters.\n");
508             } else {
509                 disp->hor_flip = hflip;
510                 disp->ver_flip = vflip;
511                 disp->anticlockwise_rotation =
512                     (uint16_t)rint((angle >= 0.0 ? angle
513                                                  : angle + 360.0) *
514                                    65536.0 / 360.0);
515                 write = 1;
516             }
517         }
518
519         if (has_sps || !ctx->done_first_au) {
520             if (!isnan(ctx->rotate)) {
521                 disp->anticlockwise_rotation =
522                     (uint16_t)rint((ctx->rotate >= 0.0 ? ctx->rotate
523                                                        : ctx->rotate + 360.0) *
524                                    65536.0 / 360.0);
525                 write = 1;
526             }
527             if (ctx->flip) {
528                 disp->hor_flip = !!(ctx->flip & FLIP_HORIZONTAL);
529                 disp->ver_flip = !!(ctx->flip & FLIP_VERTICAL);
530                 write = 1;
531             }
532         }
533
534         if (write) {
535             disp->display_orientation_repetition_period = 1;
536
537             err = ff_cbs_sei_add_message(ctx->output, au, 1,
538                                          SEI_TYPE_DISPLAY_ORIENTATION,
539                                          disp, NULL);
540             if (err < 0) {
541                 av_log(bsf, AV_LOG_ERROR, "Failed to add display orientation "
542                        "SEI message to access unit.\n");
543                 goto fail;
544             }
545         }
546     }
547
548     err = ff_cbs_write_packet(ctx->output, pkt, au);
549     if (err < 0) {
550         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
551         goto fail;
552     }
553
554     ctx->done_first_au = 1;
555
556     err = 0;
557 fail:
558     ff_cbs_fragment_reset(au);
559
560     if (err < 0)
561         av_packet_unref(pkt);
562
563     return err;
564 }
565
566 static int h264_metadata_init(AVBSFContext *bsf)
567 {
568     H264MetadataContext *ctx = bsf->priv_data;
569     CodedBitstreamFragment *au = &ctx->access_unit;
570     int err, i;
571
572     if (ctx->sei_user_data) {
573         SEIRawUserDataUnregistered *udu = &ctx->sei_user_data_payload;
574         int j;
575
576         // Parse UUID.  It must be a hex string of length 32, possibly
577         // containing '-'s between hex digits (which we ignore).
578         for (i = j = 0; j < 32 && i < 64 && ctx->sei_user_data[i]; i++) {
579             int c, v;
580             c = ctx->sei_user_data[i];
581             if (c == '-') {
582                 continue;
583             } else if (av_isxdigit(c)) {
584                 c = av_tolower(c);
585                 v = (c <= '9' ? c - '0' : c - 'a' + 10);
586             } else {
587                 break;
588             }
589             if (j & 1)
590                 udu->uuid_iso_iec_11578[j / 2] |= v;
591             else
592                 udu->uuid_iso_iec_11578[j / 2] = v << 4;
593             ++j;
594         }
595         if (j == 32 && ctx->sei_user_data[i] == '+') {
596             udu->data = (uint8_t*)ctx->sei_user_data + i + 1;
597             udu->data_length = strlen(udu->data) + 1;
598         } else {
599             av_log(bsf, AV_LOG_ERROR, "Invalid user data: "
600                    "must be \"UUID+string\".\n");
601             err = AVERROR(EINVAL);
602             goto fail;
603         }
604     }
605
606     err = ff_cbs_init(&ctx->input,  AV_CODEC_ID_H264, bsf);
607     if (err < 0)
608         return err;
609     err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf);
610     if (err < 0)
611         return err;
612
613     if (bsf->par_in->extradata) {
614         err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
615         if (err < 0) {
616             av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
617             goto fail;
618         }
619
620         for (i = 0; i < au->nb_units; i++) {
621             if (au->units[i].type == H264_NAL_SPS) {
622                 err = h264_metadata_update_sps(bsf, au->units[i].content);
623                 if (err < 0)
624                     goto fail;
625             }
626         }
627
628         err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
629         if (err < 0) {
630             av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
631             goto fail;
632         }
633     }
634
635     err = 0;
636 fail:
637     ff_cbs_fragment_reset(au);
638     return err;
639 }
640
641 static void h264_metadata_close(AVBSFContext *bsf)
642 {
643     H264MetadataContext *ctx = bsf->priv_data;
644
645     ff_cbs_fragment_free(&ctx->access_unit);
646     ff_cbs_close(&ctx->input);
647     ff_cbs_close(&ctx->output);
648 }
649
650 #define OFFSET(x) offsetof(H264MetadataContext, x)
651 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
652 static const AVOption h264_metadata_options[] = {
653     { "aud", "Access Unit Delimiter NAL units",
654         OFFSET(aud), AV_OPT_TYPE_INT,
655         { .i64 = PASS }, PASS, REMOVE, FLAGS, "aud" },
656     { "pass",   NULL, 0, AV_OPT_TYPE_CONST,
657         { .i64 = PASS   }, .flags = FLAGS, .unit = "aud" },
658     { "insert", NULL, 0, AV_OPT_TYPE_CONST,
659         { .i64 = INSERT }, .flags = FLAGS, .unit = "aud" },
660     { "remove", NULL, 0, AV_OPT_TYPE_CONST,
661         { .i64 = REMOVE }, .flags = FLAGS, .unit = "aud" },
662
663     { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
664         OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
665         { .dbl = 0.0 }, 0, 65535, FLAGS },
666
667     { "overscan_appropriate_flag", "Set VUI overscan appropriate flag",
668         OFFSET(overscan_appropriate_flag), AV_OPT_TYPE_INT,
669         { .i64 = -1 }, -1, 1, FLAGS },
670
671     { "video_format", "Set video format (table E-2)",
672         OFFSET(video_format), AV_OPT_TYPE_INT,
673         { .i64 = -1 }, -1, 7, FLAGS},
674     { "video_full_range_flag", "Set video full range flag",
675         OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
676         { .i64 = -1 }, -1, 1, FLAGS },
677     { "colour_primaries", "Set colour primaries (table E-3)",
678         OFFSET(colour_primaries), AV_OPT_TYPE_INT,
679         { .i64 = -1 }, -1, 255, FLAGS },
680     { "transfer_characteristics", "Set transfer characteristics (table E-4)",
681         OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
682         { .i64 = -1 }, -1, 255, FLAGS },
683     { "matrix_coefficients", "Set matrix coefficients (table E-5)",
684         OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
685         { .i64 = -1 }, -1, 255, FLAGS },
686
687     { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
688         OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
689         { .i64 = -1 }, -1, 6, FLAGS },
690
691     { "tick_rate", "Set VUI tick rate (num_units_in_tick / time_scale)",
692         OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
693         { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
694     { "fixed_frame_rate_flag", "Set VUI fixed frame rate flag",
695         OFFSET(fixed_frame_rate_flag), AV_OPT_TYPE_INT,
696         { .i64 = -1 }, -1, 1, FLAGS },
697
698     { "crop_left", "Set left border crop offset",
699         OFFSET(crop_left), AV_OPT_TYPE_INT,
700         { .i64 = -1 }, -1, H264_MAX_WIDTH, FLAGS },
701     { "crop_right", "Set right border crop offset",
702         OFFSET(crop_right), AV_OPT_TYPE_INT,
703         { .i64 = -1 }, -1, H264_MAX_WIDTH, FLAGS },
704     { "crop_top", "Set top border crop offset",
705         OFFSET(crop_top), AV_OPT_TYPE_INT,
706         { .i64 = -1 }, -1, H264_MAX_HEIGHT, FLAGS },
707     { "crop_bottom", "Set bottom border crop offset",
708         OFFSET(crop_bottom), AV_OPT_TYPE_INT,
709         { .i64 = -1 }, -1, H264_MAX_HEIGHT, FLAGS },
710
711     { "sei_user_data", "Insert SEI user data (UUID+string)",
712         OFFSET(sei_user_data), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
713
714     { "delete_filler", "Delete all filler (both NAL and SEI)",
715         OFFSET(delete_filler), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS},
716
717     { "display_orientation", "Display orientation SEI",
718         OFFSET(display_orientation), AV_OPT_TYPE_INT,
719         { .i64 = PASS }, PASS, EXTRACT, FLAGS, "disp_or" },
720     { "pass",    NULL, 0, AV_OPT_TYPE_CONST,
721         { .i64 = PASS    }, .flags = FLAGS, .unit = "disp_or" },
722     { "insert",  NULL, 0, AV_OPT_TYPE_CONST,
723         { .i64 = INSERT  }, .flags = FLAGS, .unit = "disp_or" },
724     { "remove",  NULL, 0, AV_OPT_TYPE_CONST,
725         { .i64 = REMOVE  }, .flags = FLAGS, .unit = "disp_or" },
726     { "extract", NULL, 0, AV_OPT_TYPE_CONST,
727         { .i64 = EXTRACT }, .flags = FLAGS, .unit = "disp_or" },
728
729     { "rotate", "Set rotation in display orientation SEI (anticlockwise angle in degrees)",
730         OFFSET(rotate), AV_OPT_TYPE_DOUBLE,
731         { .dbl = NAN }, -360.0, +360.0, FLAGS },
732     { "flip", "Set flip in display orientation SEI",
733         OFFSET(flip), AV_OPT_TYPE_FLAGS,
734         { .i64 = 0 }, 0, FLIP_HORIZONTAL | FLIP_VERTICAL, FLAGS, "flip" },
735     { "horizontal", "Set hor_flip",
736         0, AV_OPT_TYPE_CONST,
737         { .i64 = FLIP_HORIZONTAL }, .flags = FLAGS, .unit = "flip" },
738     { "vertical",   "Set ver_flip",
739         0, AV_OPT_TYPE_CONST,
740         { .i64 = FLIP_VERTICAL },   .flags = FLAGS, .unit = "flip" },
741
742     { "level", "Set level (table A-1)",
743         OFFSET(level), AV_OPT_TYPE_INT,
744         { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
745     { "auto", "Attempt to guess level from stream properties",
746         0, AV_OPT_TYPE_CONST,
747         { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
748 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
749         { .i64 = value },      .flags = FLAGS, .unit = "level"
750     { LEVEL("1",   10) },
751     { LEVEL("1b",   9) },
752     { LEVEL("1.1", 11) },
753     { LEVEL("1.2", 12) },
754     { LEVEL("1.3", 13) },
755     { LEVEL("2",   20) },
756     { LEVEL("2.1", 21) },
757     { LEVEL("2.2", 22) },
758     { LEVEL("3",   30) },
759     { LEVEL("3.1", 31) },
760     { LEVEL("3.2", 32) },
761     { LEVEL("4",   40) },
762     { LEVEL("4.1", 41) },
763     { LEVEL("4.2", 42) },
764     { LEVEL("5",   50) },
765     { LEVEL("5.1", 51) },
766     { LEVEL("5.2", 52) },
767     { LEVEL("6",   60) },
768     { LEVEL("6.1", 61) },
769     { LEVEL("6.2", 62) },
770 #undef LEVEL
771
772     { NULL }
773 };
774
775 static const AVClass h264_metadata_class = {
776     .class_name = "h264_metadata_bsf",
777     .item_name  = av_default_item_name,
778     .option     = h264_metadata_options,
779     .version    = LIBAVUTIL_VERSION_INT,
780 };
781
782 static const enum AVCodecID h264_metadata_codec_ids[] = {
783     AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
784 };
785
786 const AVBitStreamFilter ff_h264_metadata_bsf = {
787     .name           = "h264_metadata",
788     .priv_data_size = sizeof(H264MetadataContext),
789     .priv_class     = &h264_metadata_class,
790     .init           = &h264_metadata_init,
791     .close          = &h264_metadata_close,
792     .filter         = &h264_metadata_filter,
793     .codec_ids      = h264_metadata_codec_ids,
794 };