History

avcodec/bsf/h264_mp4toannexb: Fix mixed bitstream format

This bsf converts AV_PKT_DATA_NEW_EXTRADATA side data in avcc format to in-band annexb format. However, the side data wasn't been removed and copied from input packet to output packet. So the output packet has mixed bitstream format. We don't support mixed bitstream format. For example, h264_metadata report error in the following case: ffmpeg -i foo.flv \ -bsf:v "h264_mp4toannexb,h264_metadata" \ -c copy -f null This patch removed NEW_EXTRADATA side data after process. This patch also add a check so only NEW_EXTRADATA in avcc format is processed. NEW_EXTRADATA in annexb format is copied to output as is.

Zhao Zhili committed 1 year ago

Commit 25812d31 parent 26752362 files · +14 −12Browse files

Unified · Split

2 files
  1. libavcodec/bsf/h264_mp4toannexb.c +12 −10
  2. tests/fate/h264.mak +2 −2
libavcodec/bsf/h264_mp4toannexb.c+12−10
@@ -93,6 +93,11 @@ static int h264_extradata_to_annexb(AVBSFContext *ctx, · 92 unmodified lines
9393 const int padding = AV_INPUT_BUFFER_PADDING_SIZE;
9494 int length_size, pps_offset = 0;
9595
96+ if (extradata_size < 7) {
97+ av_log(ctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extradata_size);
98+ return AVERROR_INVALIDDATA;
99+ }
100+
96101 bytestream2_init(gb, extradata, extradata_size);
97102
98103 bytestream2_skipu(gb, 4);
@@ -262,16 +267,11 @@ static int h264_mp4toannexb_init(AVBSFContext *ctx) · 163 unmodified lines
262267 (extra_size >= 4 && AV_RB32(ctx->par_in->extradata) == 1)) {
263268 av_log(ctx, AV_LOG_VERBOSE,
264269 "The input looks like it is Annex B already\n");
265− } else if (extra_size >= 7) {
266− return h264_extradata_to_annexb(ctx,
267− ctx->par_in->extradata,
268− ctx->par_in->extradata_size);
269− } else {
270− av_log(ctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extra_size);
271− return AVERROR_INVALIDDATA;
270+ return 0;
272271 }
273−
274− return 0;
272+ return h264_extradata_to_annexb(ctx,
273+ ctx->par_in->extradata,
274+ ctx->par_in->extradata_size);
275275 }
276276
277277 static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt)
@@ -293,10 +293,12 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 15 unmodified lines
293293
294294 extradata = av_packet_get_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
295295 &extradata_size);
296− if (extradata) {
296+ if (extradata && extradata[0] == 1) {
297297 ret = h264_extradata_to_annexb(ctx, extradata, extradata_size);
298298 if (ret < 0)
299299 goto fail;
300+ av_packet_side_data_remove(in->side_data, &in->side_data_elems,
301+ AV_PKT_DATA_NEW_EXTRADATA);
300302 }
301303
302304 /* nothing to filter */
tests/fate/h264.mak+2−2
@@ -225,7 +225,7 @@ FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-unescaped-extradata · 224 unmodified lines
225225 # this sample contains field-coded frames, with both fields in a single packet
226226 FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-twofields-packet
227227
228−FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF SCALE_FILTER) += fate-h264-bsf-mp4toannexb-new-extradata
228+FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF H264_METADATA_BSF SCALE_FILTER) += fate-h264-bsf-mp4toannexb-new-extradata
229229
230230 FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb \
231231 fate-h264-bsf-mp4toannexb-2 \
@@ -440,7 +440,7 @@ fate-h264-bsf-mp4toannexb: CMD = md5 -i $(TARGET_SAMPLES) · 208 unmodified lines
440440 fate-h264-bsf-mp4toannexb-2: CMD = md5 -i $(TARGET_SAMPLES)/h264/ps_prefix_first_idr.mp4 -c:v copy -f h264
441441 fate-h264-bsf-mp4toannexb-2: CMP = oneline
442442 fate-h264-bsf-mp4toannexb-2: REF = cffcfa6a2d0b58c9de1f5785f099f41d
443−fate-h264-bsf-mp4toannexb-new-extradata: CMD = stream_remux mov $(TARGET_SAMPLES)/h264/extradata-reload-multi-stsd.mov "" h264 "-map 0:v"
443+fate-h264-bsf-mp4toannexb-new-extradata: CMD = stream_remux mov $(TARGET_SAMPLES)/h264/extradata-reload-multi-stsd.mov "" h264 "-bsf h264_mp4toannexb,h264_metadata -map 0:v"
444444 fate-h264-bsf-dts2pts: CMD = transcode "h264" $(TARGET_SAMPLES)/h264-conformance/CAPAMA3_Sand_F.264 \
445445 mov "-c:v copy -bsf:v dts2pts -frames:v 50" "-c:v copy"
446446 fate-h264_mp4toannexb_ticket5927: CMD = transcode "mp4" $(TARGET_SAMPLES)/h264/thezerotheorem-cut.mp4 \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t