- AVPacket *in = NULL;
- CodedBitstreamFragment *au = &ctx->access_unit;
- int err, i, j, has_sps;
- H264RawAUD aud;
- uint8_t *displaymatrix_side_data = NULL;
- size_t displaymatrix_side_data_size = 0;
-
- err = ff_bsf_get_packet(bsf, &in);
- if (err < 0)
- return err;
-
- err = ff_cbs_read_packet(ctx->cbc, au, in);
- if (err < 0) {
- av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
- goto fail;
- }
-
- if (au->nb_units == 0) {
- av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
-
- // If an AUD is present, it must be the first NAL unit.
- if (au->units[0].type == H264_NAL_AUD) {
- if (ctx->aud == REMOVE)
- ff_cbs_delete_unit(ctx->cbc, au, 0);
- } else {
- if (ctx->aud == INSERT) {
- static const int primary_pic_type_table[] = {
- 0x084, // 2, 7
- 0x0a5, // 0, 2, 5, 7
- 0x0e7, // 0, 1, 2, 5, 6, 7
- 0x210, // 4, 9
- 0x318, // 3, 4, 8, 9
- 0x294, // 2, 4, 7, 9
- 0x3bd, // 0, 2, 3, 4, 5, 7, 8, 9
- 0x3ff, // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- };
- int primary_pic_type_mask = 0xff;
-
- for (i = 0; i < au->nb_units; i++) {
- if (au->units[i].type == H264_NAL_SLICE ||
- au->units[i].type == H264_NAL_IDR_SLICE) {
- H264RawSlice *slice = au->units[i].content;
- for (j = 0; j < FF_ARRAY_ELEMS(primary_pic_type_table); j++) {
- if (!(primary_pic_type_table[j] &
- (1 << slice->header.slice_type)))
- primary_pic_type_mask &= ~(1 << j);
- }
- }
- }
- for (j = 0; j < FF_ARRAY_ELEMS(primary_pic_type_table); j++)
- if (primary_pic_type_mask & (1 << j))
- break;
- if (j >= FF_ARRAY_ELEMS(primary_pic_type_table)) {
- av_log(bsf, AV_LOG_ERROR, "No usable primary_pic_type: "
- "invalid slice types?\n");
- err = AVERROR_INVALIDDATA;
- goto fail;
- }
-
- aud = (H264RawAUD) {
- .nal_unit_header.nal_unit_type = H264_NAL_AUD,
- .primary_pic_type = j,
- };
-
- err = ff_cbs_insert_unit_content(ctx->cbc, au,
- 0, H264_NAL_AUD, &aud, NULL);
- if (err < 0) {
- av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
- goto fail;
- }
- }
- }
-
- has_sps = 0;
- for (i = 0; i < au->nb_units; i++) {
- if (au->units[i].type == H264_NAL_SPS) {
- err = h264_metadata_update_sps(bsf, au->units[i].content);
- if (err < 0)
- goto fail;
- has_sps = 1;
- }
- }
-
- // Only insert the SEI in access units containing SPSs, and also
- // unconditionally in the first access unit we ever see.
- if (ctx->sei_user_data && (has_sps || !ctx->done_first_au)) {
- H264RawSEIPayload payload = {
- .payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED,
- };
- H264RawSEIUserDataUnregistered *udu =
- &payload.payload.user_data_unregistered;
-
- for (i = j = 0; j < 32 && ctx->sei_user_data[i]; i++) {
- int c, v;
- c = ctx->sei_user_data[i];
- if (c == '-') {
- continue;
- } else if (av_isxdigit(c)) {
- c = av_tolower(c);
- v = (c <= '9' ? c - '0' : c - 'a' + 10);
- } else {
- goto invalid_user_data;
- }
- if (i & 1)
- udu->uuid_iso_iec_11578[j / 2] |= v;
- else
- udu->uuid_iso_iec_11578[j / 2] = v << 4;
- ++j;
- }
- if (j == 32 && ctx->sei_user_data[i] == '+') {
- size_t len = strlen(ctx->sei_user_data + i + 1);
-
- udu->data_ref = av_buffer_alloc(len + 1);
- if (!udu->data_ref) {
- err = AVERROR(ENOMEM);
- goto fail;
- }
-
- udu->data = udu->data_ref->data;
- udu->data_length = len + 1;
- memcpy(udu->data, ctx->sei_user_data + i + 1, len + 1);
-
- err = ff_cbs_h264_add_sei_message(ctx->cbc, au, &payload);
- if (err < 0) {
- av_log(bsf, AV_LOG_ERROR, "Failed to add user data SEI "
- "message to access unit.\n");
- goto fail;
- }
-
- } else {
- invalid_user_data:
- av_log(bsf, AV_LOG_ERROR, "Invalid user data: "
- "must be \"UUID+string\".\n");
- err = AVERROR(EINVAL);
- goto fail;
+ SEIRawMessage *message;
+ int err;
+
+ message = NULL;
+ while (ff_cbs_sei_find_message(ctx->common.output, au,
+ SEI_TYPE_DISPLAY_ORIENTATION,
+ &message) == 0) {
+ H264RawSEIDisplayOrientation *disp = message->payload;
+ int32_t *matrix;
+
+ matrix = av_malloc(9 * sizeof(int32_t));
+ if (!matrix)
+ return AVERROR(ENOMEM);
+
+ av_display_rotation_set(matrix,
+ disp->anticlockwise_rotation *
+ 180.0 / 65536.0);
+ av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip);
+
+ // If there are multiple display orientation messages in an
+ // access unit, then the last one added to the packet (i.e.
+ // the first one in the access unit) will prevail.
+ err = av_packet_add_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX,
+ (uint8_t*)matrix,
+ 9 * sizeof(int32_t));
+ if (err < 0) {
+ av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted "
+ "displaymatrix side data to packet.\n");
+ av_free(matrix);
+ return AVERROR(ENOMEM);