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