Commit 36fcdc3f authored by Andreas Rheinhardt's avatar Andreas Rheinhardt Committed by Mark Thompson

av1/h264_metadata, filter_units: Count down when deleting units

When testing whether a particular unit should be kept or discarded, it
is best to start at the very last unit of a fragment and count down,
because that way a unit that will eventually be deleted won't be
memmoved during earlier deletions; and frag/au->nb_units need only be
evaluated once in this case and the counter is automatically correct
when a unit got deleted.

It also works for double loops, i.e. when looping over all SEI messages
in all SEI units of an access unit.
Signed-off-by: 's avatarAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
parent b0810454
...@@ -160,14 +160,13 @@ static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) ...@@ -160,14 +160,13 @@ static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
} }
if (ctx->delete_padding) { if (ctx->delete_padding) {
for (i = 0; i < frag->nb_units; i++) { for (i = frag->nb_units - 1; i >= 0; i--) {
if (frag->units[i].type == AV1_OBU_PADDING) { if (frag->units[i].type == AV1_OBU_PADDING) {
err = ff_cbs_delete_unit(ctx->cbc, frag, i); err = ff_cbs_delete_unit(ctx->cbc, frag, i);
if (err < 0) { if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to delete Padding OBU.\n"); av_log(bsf, AV_LOG_ERROR, "Failed to delete Padding OBU.\n");
goto fail; goto fail;
} }
--i;
} }
} }
} }
......
...@@ -117,16 +117,14 @@ static int filter_units_filter(AVBSFContext *bsf, AVPacket *pkt) ...@@ -117,16 +117,14 @@ static int filter_units_filter(AVBSFContext *bsf, AVPacket *pkt)
goto fail; goto fail;
} }
for (i = 0; i < frag->nb_units; i++) { for (i = frag->nb_units - 1; i >= 0; i--) {
for (j = 0; j < ctx->nb_types; j++) { for (j = 0; j < ctx->nb_types; j++) {
if (frag->units[i].type == ctx->type_list[j]) if (frag->units[i].type == ctx->type_list[j])
break; break;
} }
if (ctx->mode == REMOVE ? j < ctx->nb_types if (ctx->mode == REMOVE ? j < ctx->nb_types
: j >= ctx->nb_types) { : j >= ctx->nb_types)
ff_cbs_delete_unit(ctx->cbc, frag, i); ff_cbs_delete_unit(ctx->cbc, frag, i);
--i;
}
} }
if (frag->nb_units == 0) { if (frag->nb_units == 0) {
......
...@@ -428,7 +428,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -428,7 +428,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
} }
if (ctx->delete_filler) { if (ctx->delete_filler) {
for (i = 0; i < au->nb_units; i++) { for (i = au->nb_units - 1; i >= 0; i--) {
if (au->units[i].type == H264_NAL_FILLER_DATA) { if (au->units[i].type == H264_NAL_FILLER_DATA) {
// Filler NAL units. // Filler NAL units.
err = ff_cbs_delete_unit(ctx->cbc, au, i); err = ff_cbs_delete_unit(ctx->cbc, au, i);
...@@ -437,7 +437,6 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -437,7 +437,6 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
"filler NAL.\n"); "filler NAL.\n");
goto fail; goto fail;
} }
--i;
continue; continue;
} }
...@@ -445,7 +444,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -445,7 +444,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
// Filler SEI messages. // Filler SEI messages.
H264RawSEI *sei = au->units[i].content; H264RawSEI *sei = au->units[i].content;
for (j = 0; j < sei->payload_count; j++) { for (j = sei->payload_count - 1; j >= 0; j--) {
if (sei->payload[j].payload_type == if (sei->payload[j].payload_type ==
H264_SEI_TYPE_FILLER_PAYLOAD) { H264_SEI_TYPE_FILLER_PAYLOAD) {
err = ff_cbs_h264_delete_sei_message(ctx->cbc, au, err = ff_cbs_h264_delete_sei_message(ctx->cbc, au,
...@@ -455,10 +454,6 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -455,10 +454,6 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
"filler SEI message.\n"); "filler SEI message.\n");
goto fail; goto fail;
} }
// Renumbering might have happened, start again at
// the same NAL unit position.
--i;
break;
} }
} }
} }
...@@ -466,13 +461,13 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -466,13 +461,13 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
} }
if (ctx->display_orientation != PASS) { if (ctx->display_orientation != PASS) {
for (i = 0; i < au->nb_units; i++) { for (i = au->nb_units - 1; i >= 0; i--) {
H264RawSEI *sei; H264RawSEI *sei;
if (au->units[i].type != H264_NAL_SEI) if (au->units[i].type != H264_NAL_SEI)
continue; continue;
sei = au->units[i].content; sei = au->units[i].content;
for (j = 0; j < sei->payload_count; j++) { for (j = sei->payload_count - 1; j >= 0; j--) {
H264RawSEIDisplayOrientation *disp; H264RawSEIDisplayOrientation *disp;
int32_t *matrix; int32_t *matrix;
...@@ -490,8 +485,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -490,8 +485,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
"display orientation SEI message.\n"); "display orientation SEI message.\n");
goto fail; goto fail;
} }
--i; continue;
break;
} }
matrix = av_mallocz(9 * sizeof(int32_t)); matrix = av_mallocz(9 * sizeof(int32_t));
...@@ -506,7 +500,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out) ...@@ -506,7 +500,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip); av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip);
// If there are multiple display orientation messages in an // If there are multiple display orientation messages in an
// access unit then ignore all but the last one. // access unit then ignore all but the first one.
av_freep(&displaymatrix_side_data); av_freep(&displaymatrix_side_data);
displaymatrix_side_data = (uint8_t*)matrix; displaymatrix_side_data = (uint8_t*)matrix;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment