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