cbs_h264: Add support for filler NAL units
[libav.git] / libavcodec / h264_metadata_bsf.c
CommitLineData
9e93001b
MT
1/*
2 * This file is part of Libav.
3 *
4 * Libav 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 * Libav 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 Libav; 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/common.h"
21#include "libavutil/opt.h"
22
23#include "bsf.h"
24#include "cbs.h"
25#include "cbs_h264.h"
26#include "h264.h"
27#include "h264_sei.h"
28
29enum {
30 PASS,
31 INSERT,
32 REMOVE,
33};
34
35typedef struct H264MetadataContext {
36 const AVClass *class;
37
26513529 38 CodedBitstreamContext *cbc;
9e93001b
MT
39 CodedBitstreamFragment access_unit;
40
41 H264RawAUD aud_nal;
42 H264RawSEI sei_nal;
43
44 int aud;
45
46 AVRational sample_aspect_ratio;
47
48 int video_format;
49 int video_full_range_flag;
50 int colour_primaries;
51 int transfer_characteristics;
52 int matrix_coefficients;
53
54 int chroma_sample_loc_type;
55
56 AVRational tick_rate;
57 int fixed_frame_rate_flag;
58
59 int crop_left;
60 int crop_right;
61 int crop_top;
62 int crop_bottom;
63
64 const char *sei_user_data;
78fa0b90 65 int sei_first_au;
9e93001b
MT
66} H264MetadataContext;
67
68
69static int h264_metadata_update_sps(AVBSFContext *bsf,
70 H264RawSPS *sps)
71{
72 H264MetadataContext *ctx = bsf->priv_data;
73 int need_vui = 0;
74 int crop_unit_x, crop_unit_y;
75
76 if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
77 // Table E-1.
78 static const AVRational sar_idc[] = {
79 { 0, 0 }, // Unspecified (never written here).
80 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
81 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
82 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
83 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
84 };
85 int num, den, i;
86
87 av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
88 ctx->sample_aspect_ratio.den, 65535);
89
90 for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
91 if (num == sar_idc[i].num &&
92 den == sar_idc[i].den)
93 break;
94 }
95 if (i == FF_ARRAY_ELEMS(sar_idc)) {
96 sps->vui.aspect_ratio_idc = 255;
97 sps->vui.sar_width = num;
98 sps->vui.sar_height = den;
99 } else {
100 sps->vui.aspect_ratio_idc = i;
101 }
102 sps->vui.aspect_ratio_info_present_flag = 1;
103 need_vui = 1;
104 }
105
106#define SET_OR_INFER(field, value, present_flag, infer) do { \
107 if (value >= 0) { \
108 field = value; \
109 need_vui = 1; \
110 } else if (!present_flag) \
111 field = infer; \
112 } while (0)
113
114 if (ctx->video_format >= 0 ||
115 ctx->video_full_range_flag >= 0 ||
116 ctx->colour_primaries >= 0 ||
117 ctx->transfer_characteristics >= 0 ||
118 ctx->matrix_coefficients >= 0) {
119
120 SET_OR_INFER(sps->vui.video_format, ctx->video_format,
121 sps->vui.video_signal_type_present_flag, 5);
122
123 SET_OR_INFER(sps->vui.video_full_range_flag,
124 ctx->video_full_range_flag,
125 sps->vui.video_signal_type_present_flag, 0);
126
127 if (ctx->colour_primaries >= 0 ||
128 ctx->transfer_characteristics >= 0 ||
129 ctx->matrix_coefficients >= 0) {
130
131 SET_OR_INFER(sps->vui.colour_primaries,
132 ctx->colour_primaries,
133 sps->vui.colour_description_present_flag, 2);
134
135 SET_OR_INFER(sps->vui.transfer_characteristics,
136 ctx->transfer_characteristics,
137 sps->vui.colour_description_present_flag, 2);
138
139 SET_OR_INFER(sps->vui.matrix_coefficients,
140 ctx->matrix_coefficients,
141 sps->vui.colour_description_present_flag, 2);
142
143 sps->vui.colour_description_present_flag = 1;
144 }
145 sps->vui.video_signal_type_present_flag = 1;
146 need_vui = 1;
147 }
148
149 if (ctx->chroma_sample_loc_type >= 0) {
150 sps->vui.chroma_sample_loc_type_top_field =
151 ctx->chroma_sample_loc_type;
152 sps->vui.chroma_sample_loc_type_bottom_field =
153 ctx->chroma_sample_loc_type;
154 sps->vui.chroma_loc_info_present_flag = 1;
155 need_vui = 1;
156 }
157
158 if (ctx->tick_rate.num && ctx->tick_rate.den) {
159 int num, den;
160
161 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
162 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
163
164 sps->vui.time_scale = num;
165 sps->vui.num_units_in_tick = den;
166
167 sps->vui.timing_info_present_flag = 1;
168 need_vui = 1;
169 }
170 SET_OR_INFER(sps->vui.fixed_frame_rate_flag,
171 ctx->fixed_frame_rate_flag,
172 sps->vui.timing_info_present_flag, 0);
173
174 if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
175 crop_unit_x = 1;
176 crop_unit_y = 2 - sps->frame_mbs_only_flag;
177 } else {
178 crop_unit_x = 1 + (sps->chroma_format_idc < 3);
179 crop_unit_y = (1 + (sps->chroma_format_idc < 2)) *
180 (2 - sps->frame_mbs_only_flag);
181 }
182#define CROP(border, unit) do { \
183 if (ctx->crop_ ## border >= 0) { \
184 if (ctx->crop_ ## border % unit != 0) { \
185 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
186 "must be a multiple of %d.\n", #border, unit); \
187 return AVERROR(EINVAL); \
188 } \
189 sps->frame_crop_ ## border ## _offset = \
190 ctx->crop_ ## border / unit; \
191 sps->frame_cropping_flag = 1; \
192 } \
193 } while (0)
194 CROP(left, crop_unit_x);
195 CROP(right, crop_unit_x);
196 CROP(top, crop_unit_y);
197 CROP(bottom, crop_unit_y);
198#undef CROP
199
200 if (need_vui)
201 sps->vui_parameters_present_flag = 1;
202
203 return 0;
204}
205
206static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
207{
208 H264MetadataContext *ctx = bsf->priv_data;
209 AVPacket *in = NULL;
210 CodedBitstreamFragment *au = &ctx->access_unit;
211 int err, i, j, has_sps;
9e93001b
MT
212
213 err = ff_bsf_get_packet(bsf, &in);
214 if (err < 0)
215 goto fail;
216
26513529 217 err = ff_cbs_read_packet(ctx->cbc, au, in);
9e93001b
MT
218 if (err < 0) {
219 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
220 goto fail;
221 }
222
223 if (au->nb_units == 0) {
224 av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
225 err = AVERROR_INVALIDDATA;
226 goto fail;
227 }
228
229 // If an AUD is present, it must be the first NAL unit.
230 if (au->units[0].type == H264_NAL_AUD) {
231 if (ctx->aud == REMOVE)
26513529 232 ff_cbs_delete_unit(ctx->cbc, au, 0);
9e93001b
MT
233 } else {
234 if (ctx->aud == INSERT) {
235 static const int primary_pic_type_table[] = {
236 0x084, // 2, 7
237 0x0a5, // 0, 2, 5, 7
238 0x0e7, // 0, 1, 2, 5, 6, 7
239 0x210, // 4, 9
240 0x318, // 3, 4, 8, 9
241 0x294, // 2, 4, 7, 9
242 0x3bd, // 0, 2, 3, 4, 5, 7, 8, 9
243 0x3ff, // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
244 };
245 int primary_pic_type_mask = 0xff;
246 H264RawAUD *aud = &ctx->aud_nal;
247
248 for (i = 0; i < au->nb_units; i++) {
249 if (au->units[i].type == H264_NAL_SLICE ||
250 au->units[i].type == H264_NAL_IDR_SLICE) {
251 H264RawSlice *slice = au->units[i].content;
252 for (j = 0; j < FF_ARRAY_ELEMS(primary_pic_type_table); j++) {
253 if (!(primary_pic_type_table[j] &
254 (1 << slice->header.slice_type)))
255 primary_pic_type_mask &= ~(1 << j);
256 }
257 }
258 }
259 for (j = 0; j < FF_ARRAY_ELEMS(primary_pic_type_table); j++)
260 if (primary_pic_type_mask & (1 << j))
261 break;
262 if (j >= FF_ARRAY_ELEMS(primary_pic_type_table)) {
263 av_log(bsf, AV_LOG_ERROR, "No usable primary_pic_type: "
264 "invalid slice types?\n");
265 err = AVERROR_INVALIDDATA;
266 goto fail;
267 }
268
269 aud->nal_unit_header.nal_unit_type = H264_NAL_AUD;
270 aud->primary_pic_type = j;
271
26513529 272 err = ff_cbs_insert_unit_content(ctx->cbc, au,
ce5870a3 273 0, H264_NAL_AUD, aud, NULL);
9e93001b
MT
274 if (err < 0) {
275 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
276 goto fail;
277 }
278 }
279 }
280
281 has_sps = 0;
282 for (i = 0; i < au->nb_units; i++) {
283 if (au->units[i].type == H264_NAL_SPS) {
284 err = h264_metadata_update_sps(bsf, au->units[i].content);
285 if (err < 0)
286 goto fail;
287 has_sps = 1;
288 }
289 }
290
78fa0b90
MT
291 // Only insert the SEI in access units containing SPSs, and also
292 // unconditionally in the first access unit we ever see.
293 if (ctx->sei_user_data && (has_sps || !ctx->sei_first_au)) {
69062d0f
MT
294 H264RawSEIPayload payload = {
295 .payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED,
296 };
297 H264RawSEIUserDataUnregistered *udu =
298 &payload.payload.user_data_unregistered;
9e93001b 299
78fa0b90
MT
300 ctx->sei_first_au = 1;
301
9e93001b
MT
302 for (i = j = 0; j < 32 && ctx->sei_user_data[i]; i++) {
303 int c, v;
304 c = ctx->sei_user_data[i];
305 if (c == '-') {
306 continue;
307 } else if (av_isxdigit(c)) {
308 c = av_tolower(c);
309 v = (c <= '9' ? c - '0' : c - 'a' + 10);
310 } else {
311 goto invalid_user_data;
312 }
313 if (i & 1)
314 udu->uuid_iso_iec_11578[j / 2] |= v;
315 else
316 udu->uuid_iso_iec_11578[j / 2] = v << 4;
317 ++j;
318 }
319 if (j == 32 && ctx->sei_user_data[i] == '+') {
69062d0f
MT
320 size_t len = strlen(ctx->sei_user_data + i + 1);
321
322 udu->data_ref = av_buffer_alloc(len + 1);
323 if (!udu->data_ref) {
9e93001b 324 err = AVERROR(ENOMEM);
69062d0f 325 goto fail;
9e93001b
MT
326 }
327
69062d0f
MT
328 udu->data = udu->data_ref->data;
329 udu->data_length = len + 1;
330 memcpy(udu->data, ctx->sei_user_data + i + 1, len + 1);
9e93001b 331
69062d0f 332 payload.payload_size = 16 + udu->data_length;
9e93001b 333
69062d0f
MT
334 err = ff_cbs_h264_add_sei_message(ctx->cbc, au, &payload);
335 if (err < 0) {
336 av_log(bsf, AV_LOG_ERROR, "Failed to add user data SEI "
337 "message to access unit.\n");
338 goto fail;
c42b62d1
MT
339 }
340
9e93001b
MT
341 } else {
342 invalid_user_data:
343 av_log(bsf, AV_LOG_ERROR, "Invalid user data: "
344 "must be \"UUID+string\".\n");
345 err = AVERROR(EINVAL);
9e93001b 346 }
9e93001b
MT
347 }
348
26513529 349 err = ff_cbs_write_packet(ctx->cbc, out, au);
9e93001b
MT
350 if (err < 0) {
351 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
352 goto fail;
353 }
354
355 err = av_packet_copy_props(out, in);
356 if (err < 0)
357 goto fail;
358
359 err = 0;
360fail:
26513529 361 ff_cbs_fragment_uninit(ctx->cbc, au);
9e93001b
MT
362
363 av_packet_free(&in);
364
365 return err;
366}
367
368static int h264_metadata_init(AVBSFContext *bsf)
369{
370 H264MetadataContext *ctx = bsf->priv_data;
371 CodedBitstreamFragment *au = &ctx->access_unit;
372 int err, i;
373
374 err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_H264, bsf);
375 if (err < 0)
376 return err;
377
378 if (bsf->par_in->extradata) {
26513529 379 err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
9e93001b
MT
380 if (err < 0) {
381 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
382 goto fail;
383 }
384
385 for (i = 0; i < au->nb_units; i++) {
386 if (au->units[i].type == H264_NAL_SPS) {
387 err = h264_metadata_update_sps(bsf, au->units[i].content);
388 if (err < 0)
389 goto fail;
390 }
391 }
392
26513529 393 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
9e93001b
MT
394 if (err < 0) {
395 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
396 goto fail;
397 }
398 }
399
400 err = 0;
401fail:
26513529 402 ff_cbs_fragment_uninit(ctx->cbc, au);
9e93001b
MT
403 return err;
404}
405
406static void h264_metadata_close(AVBSFContext *bsf)
407{
408 H264MetadataContext *ctx = bsf->priv_data;
409 ff_cbs_close(&ctx->cbc);
410}
411
412#define OFFSET(x) offsetof(H264MetadataContext, x)
413static const AVOption h264_metadata_options[] = {
414 { "aud", "Access Unit Delimiter NAL units",
415 OFFSET(aud), AV_OPT_TYPE_INT,
416 { .i64 = PASS }, PASS, REMOVE, 0, "aud" },
417 { "pass", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PASS }, .unit = "aud" },
418 { "insert", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = INSERT }, .unit = "aud" },
419 { "remove", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE }, .unit = "aud" },
420
421 { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
422 OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
423 { .i64 = 0 }, 0, 65535 },
424
425 { "video_format", "Set video format (table E-2)",
426 OFFSET(video_format), AV_OPT_TYPE_INT,
427 { .i64 = -1 }, -1, 7 },
428 { "video_full_range_flag", "Set video full range flag",
429 OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
430 { .i64 = -1 }, -1, 1 },
431 { "colour_primaries", "Set colour primaries (table E-3)",
432 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
433 { .i64 = -1 }, -1, 255 },
434 { "transfer_characteristics", "Set transfer characteristics (table E-4)",
435 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
436 { .i64 = -1 }, -1, 255 },
437 { "matrix_coefficients", "Set matrix coefficients (table E-5)",
438 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
439 { .i64 = -1 }, -1, 255 },
440
441 { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
442 OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
443 { .i64 = -1 }, -1, 6 },
444
445 { "tick_rate", "Set VUI tick rate (num_units_in_tick / time_scale)",
446 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
447 { .i64 = 0 }, 0, UINT_MAX },
448 { "fixed_frame_rate_flag", "Set VUI fixed frame rate flag",
449 OFFSET(fixed_frame_rate_flag), AV_OPT_TYPE_INT,
450 { .i64 = -1 }, -1, 1 },
451
452 { "crop_left", "Set left border crop offset",
453 OFFSET(crop_left), AV_OPT_TYPE_INT,
454 { .i64 = -1 }, -1, H264_MAX_WIDTH },
455 { "crop_right", "Set right border crop offset",
456 OFFSET(crop_right), AV_OPT_TYPE_INT,
457 { .i64 = -1 }, -1, H264_MAX_WIDTH },
458 { "crop_top", "Set top border crop offset",
459 OFFSET(crop_top), AV_OPT_TYPE_INT,
460 { .i64 = -1 }, -1, H264_MAX_HEIGHT },
461 { "crop_bottom", "Set bottom border crop offset",
462 OFFSET(crop_bottom), AV_OPT_TYPE_INT,
463 { .i64 = -1 }, -1, H264_MAX_HEIGHT },
464
465 { "sei_user_data", "Insert SEI user data (UUID+string)",
466 OFFSET(sei_user_data), AV_OPT_TYPE_STRING, { .str = NULL } },
467
468 { NULL }
469};
470
471static const AVClass h264_metadata_class = {
472 .class_name = "h264_metadata_bsf",
473 .item_name = av_default_item_name,
474 .option = h264_metadata_options,
475 .version = LIBAVCODEC_VERSION_MAJOR,
476};
477
478static const enum AVCodecID h264_metadata_codec_ids[] = {
479 AV_CODEC_ID_H264, AV_CODEC_ID_NONE,
480};
481
482const AVBitStreamFilter ff_h264_metadata_bsf = {
483 .name = "h264_metadata",
484 .priv_data_size = sizeof(H264MetadataContext),
485 .priv_class = &h264_metadata_class,
486 .init = &h264_metadata_init,
487 .close = &h264_metadata_close,
488 .filter = &h264_metadata_filter,
489 .codec_ids = h264_metadata_codec_ids,
490};