Fix re-encoding after flattening

The decoder is using the SVC NAL unit prefix data on some Samsung
devices.

PiperOrigin-RevId: 414457181
This commit is contained in:
kimvde 2021-12-06 17:01:11 +00:00 committed by Ian Baker
parent 6bceec3246
commit 94caa8ad6c
2 changed files with 59 additions and 79 deletions

View File

@ -18,110 +18,110 @@ format 1:
data = length 10, hash 7A0D0F2B data = length 10, hash 7A0D0F2B
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -549003117 dataHashCode = 1949079733
size = 5438 size = 5446
isKeyFrame = true isKeyFrame = true
presentationTimeUs = 0 presentationTimeUs = 0
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 593600631 dataHashCode = -1397194508
size = 117 size = 125
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 14000 presentationTimeUs = 14000
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -961321612 dataHashCode = 1147159698
size = 139 size = 147
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 47333 presentationTimeUs = 47333
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -386347143 dataHashCode = 524634358
size = 141 size = 149
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 80667 presentationTimeUs = 80667
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -1289764147 dataHashCode = 2031178347
size = 141 size = 149
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 114000 presentationTimeUs = 114000
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 1337088875 dataHashCode = -625462168
size = 161 size = 169
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 147333 presentationTimeUs = 147333
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -322406979 dataHashCode = -973299745
size = 118 size = 126
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 180667 presentationTimeUs = 180667
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -1688033783 dataHashCode = -788426325
size = 112 size = 120
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 228042 presentationTimeUs = 228042
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -700344608 dataHashCode = 2009515523
size = 118 size = 126
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 244708 presentationTimeUs = 244708
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -1441653629 dataHashCode = -874600600
size = 1172 size = 1180
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 334083 presentationTimeUs = 334083
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 1201357091 dataHashCode = 984869991
size = 208 size = 216
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 267416 presentationTimeUs = 267416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -668484307 dataHashCode = 2106811176
size = 111 size = 119
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 234083 presentationTimeUs = 234083
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 653508165 dataHashCode = -1981166365
size = 137 size = 145
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 300750 presentationTimeUs = 300750
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -816848987 dataHashCode = 1234592714
size = 1266 size = 1274
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 467416 presentationTimeUs = 467416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 1842436292 dataHashCode = -13135608
size = 182 size = 190
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 400750 presentationTimeUs = 400750
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -559603233 dataHashCode = 1840621658
size = 99 size = 107
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 367416 presentationTimeUs = 367416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -666437886 dataHashCode = 1637734271
size = 117 size = 125
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 434083 presentationTimeUs = 434083
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 182521759 dataHashCode = 2112365658
size = 1101 size = 1109
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 600750 presentationTimeUs = 600750
sample: sample:
@ -276,68 +276,68 @@ sample:
presentationTimeUs = 199105 presentationTimeUs = 199105
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 2139021989 dataHashCode = -968901399
size = 242 size = 250
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 534083 presentationTimeUs = 534083
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 2013165108 dataHashCode = -1184738023
size = 116 size = 124
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 500750 presentationTimeUs = 500750
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 405675195 dataHashCode = 1756300509
size = 126 size = 134
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 567416 presentationTimeUs = 567416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -1893277090 dataHashCode = 823429273
size = 1193 size = 1201
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 734083 presentationTimeUs = 734083
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -1554795381 dataHashCode = 651718599
size = 205 size = 213
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 667416 presentationTimeUs = 667416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 1197099206 dataHashCode = 846349953
size = 117 size = 125
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 634083 presentationTimeUs = 634083
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -674808173 dataHashCode = 1331153462
size = 106 size = 114
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 700750 presentationTimeUs = 700750
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -775517313 dataHashCode = -73358172
size = 1002 size = 1010
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 867416 presentationTimeUs = 867416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = -2045106113 dataHashCode = -1395269253
size = 201 size = 209
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 800750 presentationTimeUs = 800750
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 305167697 dataHashCode = -1001367604
size = 131 size = 139
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 767416 presentationTimeUs = 767416
sample: sample:
trackIndex = 1 trackIndex = 1
dataHashCode = 554021920 dataHashCode = -122569918
size = 130 size = 138
isKeyFrame = false isKeyFrame = false
presentationTimeUs = 834083 presentationTimeUs = 834083
released = true released = true

View File

@ -33,7 +33,6 @@ import androidx.media3.extractor.metadata.mp4.SlowMotionData;
import androidx.media3.extractor.metadata.mp4.SmtaMetadataEntry; import androidx.media3.extractor.metadata.mp4.SmtaMetadataEntry;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import org.checkerframework.checker.nullness.qual.RequiresNonNull; import org.checkerframework.checker.nullness.qual.RequiresNonNull;
@ -157,7 +156,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
// reused for the empty end-of-stream buffer. // reused for the empty end-of-stream buffer.
buffer.timeUs = getCurrentFrameOutputTimeUs(/* inputTimeUs= */ buffer.timeUs); buffer.timeUs = getCurrentFrameOutputTimeUs(/* inputTimeUs= */ buffer.timeUs);
if (shouldKeepFrame) { if (shouldKeepFrame) {
skipToNextNalUnit(data); // Skip over prefix_nal_unit_svc. data.position(originalPosition);
return false; return false;
} }
return true; return true;
@ -261,25 +260,6 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
return Math.round(outputTimeUs * INPUT_FRAME_RATE / captureFrameRate); return Math.round(outputTimeUs * INPUT_FRAME_RATE / captureFrameRate);
} }
/**
* Advances the position of {@code data} to the start of the next NAL unit.
*
* @throws IllegalStateException If no NAL unit is found.
*/
private void skipToNextNalUnit(ByteBuffer data) {
int newPosition = data.position();
while (data.remaining() >= NAL_START_CODE_LENGTH) {
data.get(scratch, 0, NAL_START_CODE_LENGTH);
if (Arrays.equals(scratch, NAL_START_CODE)) {
data.position(newPosition);
return;
}
newPosition++;
data.position(newPosition);
}
throw new IllegalStateException("Could not find NAL unit start code.");
}
/** Returns the {@link MetadataInfo} derived from the {@link Metadata} provided. */ /** Returns the {@link MetadataInfo} derived from the {@link Metadata} provided. */
private static MetadataInfo getMetadataInfo(@Nullable Metadata metadata) { private static MetadataInfo getMetadataInfo(@Nullable Metadata metadata) {
MetadataInfo metadataInfo = new MetadataInfo(); MetadataInfo metadataInfo = new MetadataInfo();