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