Skip to content

Instantly share code, notes, and snippets.

@tmm1
Last active October 9, 2019 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmm1/5737a57ea6b398d3e8d3cad4f12ebb49 to your computer and use it in GitHub Desktop.
Save tmm1/5737a57ea6b398d3e8d3cad4f12ebb49 to your computer and use it in GitHub Desktop.
ffmpeg h264_metadata_bsf issue
$ wget https://tmm1.s3.amazonaws.com/h264-metadata.mpg
$ ffmpeg -i h264-metadata.mpg -c copy -map v -f h264 -y /dev/stdout | h264_analyze /dev/stdin > /tmp/a.out
$ ffmpeg -i h264-metadata.mpg -bsf:v h264_metadata -c copy -map v -f h264 -y /dev/stdout | h264_analyze /dev/stdin > /tmp/b.out
$ diff -uNdr /tmp/a.out /tmp/b.out | head -20
--- /tmp/a.out 2019-10-09 14:33:07.000000000 -0700
+++ /tmp/b.out 2019-10-09 14:33:01.000000000 -0700
@@ -144,8 +144,8 @@
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
-!! Found NAL at offset 150 (0x0096), size 245633 (0x3BF81)
-XX 00 00 00 01 41 88 8E 09 68 62 C2 32 89 A8 BB 23
+!! Found NAL at offset 149 (0x0095), size 245633 (0x3BF81)
+XX 80 00 00 01 41 88 8E 09 68 62 C2 32 89 A8 BB 23
==================== NAL ====================
forbidden_zero_bit : 0
@@ -184,7 +184,7 @@
no_output_of_prior_pics_flag : 0
long_term_reference_flag : 0
adaptive_ref_pic_marking_mode_flag : 1
-!! Found NAL at offset 245792 (0x3C020), size 2 (0x0002)
+!! Found NAL at offset 245786 (0x3C01A), size 2 (0x0002)
XX 00 00 00 01 09 50
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index d39173b408..1f5c4f0525 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -565,9 +565,11 @@ static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
AVBufferRef *ref;
size_t size = nal->size;
+ if (nal->type != H264_NAL_SLICE && nal->type != H264_NAL_IDR_SLICE) {
// Remove trailing zeroes.
while (size > 0 && nal->data[size - 1] == 0)
--size;
+ }
if (size == 0)
continue;
@@ -858,13 +860,13 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
pos = get_bits_count(&gbc);
len = unit->data_size;
- if (!unit->data[len - 1]) {
+ /*if (!unit->data[len - 1]) {
int z;
for (z = 0; z < len && !unit->data[len - z - 1]; z++);
av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
"from slice data.\n", z);
len -= z;
- }
+ }*/
slice->data_size = len - pos / 8;
slice->data_ref = av_buffer_ref(unit->data_ref);
@@ -1465,6 +1467,7 @@ static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
dp = 0;
for (i = 0; i < frag->nb_units; i++) {
CodedBitstreamUnit *unit = &frag->units[i];
+ size_t size;
if (unit->data_bit_padding > 0) {
if (i < frag->nb_units - 1)
@@ -1491,7 +1494,10 @@ static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
data[dp++] = 1;
zero_run = 0;
- for (sp = 0; sp < unit->data_size; sp++) {
+ size = unit->data_size;
+ while (size > 0 && unit->data[size - 1] == 0)
+ --size;
+ for (sp = 0; sp < size; sp++) {
if (zero_run < 2) {
if (unit->data[sp] == 0)
++zero_run;
@@ -1506,6 +1512,9 @@ static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
}
data[dp++] = unit->data[sp];
}
+ size = unit->data_size - size;
+ for (sp = 0; sp < size; sp++)
+ data[dp++] = 0;
}
av_assert0(dp <= max_size);

on an android device with MTK8695+MT7668, using avcodec/mediacodec with OMX.MTK.VIDEO.DECODER.AVC

observed that using either chomp_bsf or h264_metadata_bsf makes the sample stall out the hardware decoder due to trailing zeros being removed from the end of NAL_SLICE and NAL_IDR_SLICE

with the patch above, cbs keeps trailing zeros and fixes playback on the device when using h264_metadata_bsf (and possibly also when using filter_units_bsf).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment