History

avcodec/h264_mp4toannexb_bsf: fix missing PS before IDR frames

If there is a single group of SPS/PPS before an IDR frame, but no SPS/PPS after that, we will miss the chance to reset idr_sps_seen/idr_pps_seen. No SPS/PPS are inserted afterwards. This patch saves in-band SPS/PPS and insert them before IDR frames when necessary.

Zhao Zhili committed 3 years ago

Commit d3aa0cd1 parent 4c4b8332 files · +84 −7Browse files

Unified · Split

2 files
  1. libavcodec/h264_mp4toannexb_bsf.c +77 −6
  2. tests/fate/h264.mak +7 −1
libavcodec/h264_mp4toannexb_bsf.c+77−6
@@ -36,6 +36,8 @@ typedef struct H264BSFContext { · 35 unmodified lines
3636 uint8_t *pps;
3737 int sps_size;
3838 int pps_size;
39+ unsigned sps_buf_size;
40+ unsigned pps_buf_size;
3941 uint8_t length_size;
4042 uint8_t new_idr;
4143 uint8_t idr_sps_seen;
@@ -130,16 +132,33 @@ pps: · 88 unmodified lines
130132 memset(out + total_size, 0, padding);
131133
132134 if (pps_offset) {
133− s->sps = out;
135+ uint8_t *sps;
136+
134137 s->sps_size = pps_offset;
138+ sps = av_fast_realloc(s->sps, &s->sps_buf_size, s->sps_size);
139+ if (!sps) {
140+ av_free(out);
141+ return AVERROR(ENOMEM);
142+ }
143+ s->sps = sps;
144+ memcpy(s->sps, out, s->sps_size);
135145 } else {
136146 av_log(ctx, AV_LOG_WARNING,
137147 "Warning: SPS NALU missing or invalid. "
138148 "The resulting stream may not play.\n");
139149 }
140150 if (pps_offset < total_size) {
141− s->pps = out + pps_offset;
151+ uint8_t *pps;
152+
142153 s->pps_size = total_size - pps_offset;
154+ pps = av_fast_realloc(s->pps, &s->pps_buf_size, s->pps_size);
155+ if (!pps) {
156+ av_freep(&s->sps);
157+ av_free(out);
158+ return AVERROR(ENOMEM);
159+ }
160+ s->pps = pps;
161+ memcpy(s->pps, out + pps_offset, s->pps_size);
143162 } else {
144163 av_log(ctx, AV_LOG_WARNING,
145164 "Warning: PPS NALU missing or invalid. "
@@ -153,6 +172,35 @@ pps: · 7 unmodified lines
153172 return length_size;
154173 }
155174
175+static int h264_mp4toannexb_save_ps(uint8_t **dst, int *dst_size,
176+ unsigned *dst_buf_size,
177+ const uint8_t *nal, uint32_t nal_size,
178+ int first)
179+{
180+ static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
181+ const int start_code_size = sizeof(nalu_header);
182+ uint8_t *ptr;
183+ uint32_t size;
184+
185+ if (first)
186+ size = 0;
187+ else
188+ size = *dst_size;
189+
190+ ptr = av_fast_realloc(*dst, dst_buf_size, size + nal_size + start_code_size);
191+ if (!ptr)
192+ return AVERROR(ENOMEM);
193+
194+ memcpy(ptr + size, nalu_header, start_code_size);
195+ size += start_code_size;
196+ memcpy(ptr + size, nal, nal_size);
197+ size += nal_size;
198+
199+ *dst = ptr;
200+ *dst_size = size;
201+ return 0;
202+}
203+
156204 static int h264_mp4toannexb_init(AVBSFContext *ctx)
157205 {
158206 H264BSFContext *s = ctx->priv_data;
@@ -211,6 +259,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 52 unmodified lines
211259 if (j) \
212260 av_log(__VA_ARGS__)
213261 for (int j = 0; j < 2; j++) {
262+ int sps_count = 0;
263+ int pps_count = 0;
264+
214265 buf = in->data;
215266 new_idr = s->new_idr;
216267 sps_seen = s->idr_sps_seen;
@@ -241,8 +292,18 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 24 unmodified lines
241292
242293 if (unit_type == H264_NAL_SPS) {
243294 sps_seen = new_idr = 1;
295+ if (!j) {
296+ h264_mp4toannexb_save_ps(&s->sps, &s->sps_size, &s->sps_buf_size,
297+ buf, nal_size, !sps_count);
298+ sps_count++;
299+ }
244300 } else if (unit_type == H264_NAL_PPS) {
245301 pps_seen = new_idr = 1;
302+ if (!j) {
303+ h264_mp4toannexb_save_ps(&s->pps, &s->pps_size, &s->pps_buf_size,
304+ buf, nal_size, !pps_count);
305+ pps_count++;
306+ }
246307 /* if SPS has not been seen yet, prepend the AVCC one to PPS */
247308 if (!sps_seen) {
248309 if (!s->sps_size) {
@@ -262,9 +323,10 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 13 unmodified lines
262323
263324 /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
264325 if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) {
265− if (ctx->par_out->extradata)
266− count_or_copy(&out, &out_size, ctx->par_out->extradata,
267− ctx->par_out->extradata_size, PS_OUT_OF_BAND, j);
326+ if (s->sps_size)
327+ count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j);
328+ if (s->pps_size)
329+ count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j);
268330 new_idr = 0;
269331 /* if only SPS has been seen, also insert PPS */
270332 } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) {
@@ -280,7 +342,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 9 unmodified lines
280342 else
281343 ps = PS_NONE;
282344 count_or_copy(&out, &out_size, buf, nal_size, ps, j);
283− if (!new_idr && unit_type == H264_NAL_SLICE) {
345+ if (unit_type == H264_NAL_SLICE) {
284346 new_idr = 1;
285347 sps_seen = 0;
286348 pps_seen = 0;
@@ -320,6 +382,14 @@ fail: · 33 unmodified lines
320382 return ret;
321383 }
322384
385+static void h264_mp4toannexb_close(AVBSFContext *ctx)
386+{
387+ H264BSFContext *s = ctx->priv_data;
388+
389+ av_freep(&s->sps);
390+ av_freep(&s->pps);
391+}
392+
323393 static void h264_mp4toannexb_flush(AVBSFContext *ctx)
324394 {
325395 H264BSFContext *s = ctx->priv_data;
@@ -339,5 +409,6 @@ const FFBitStreamFilter ff_h264_mp4toannexb_bsf = { · 13 unmodified lines
339409 .priv_data_size = sizeof(H264BSFContext),
340410 .init = h264_mp4toannexb_init,
341411 .filter = h264_mp4toannexb_filter,
412+ .close = h264_mp4toannexb_close,
342413 .flush = h264_mp4toannexb_flush,
343414 };
tests/fate/h264.mak+7−1
@@ -224,7 +224,9 @@ FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-unescaped-extradata · 223 unmodified lines
224224 # this sample contains field-coded frames, with both fields in a single packet
225225 FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-twofields-packet
226226
227−FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb
227+FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb \
228+ fate-h264-bsf-mp4toannexb-2
229+
228230 FATE_H264-$(call FRAMECRC, MATROSKA, H264) += fate-h264-direct-bff
229231 FATE_H264-$(call FRAMECRC, FLV, H264, SCALE_FILTER) += fate-h264-brokensps-2580
230232 FATE_H264-$(call FRAMECRC, MXF, H264, PCM_S24LE_DECODER SCALE_FILTER ARESAMPLE_FILTER) += fate-h264-xavc-4389
@@ -426,6 +428,10 @@ fate-h264-conformance-sva_nl1_b: CMD = framecrc -i $(TARGET_SAM · 195 unmodified lines
426428 fate-h264-conformance-sva_nl2_e: CMD = framecrc -i $(TARGET_SAMPLES)/h264-conformance/SVA_NL2_E.264
427429
428430 fate-h264-bsf-mp4toannexb: CMD = md5 -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -c:v copy -f h264
431+# First IDR is prefixed by SPS/PPS
432+fate-h264-bsf-mp4toannexb-2: CMD = md5 -i $(TARGET_SAMPLES)/h264/ps_prefix_first_idr.mp4 -c:v copy -f h264
433+fate-h264-bsf-mp4toannexb-2: CMP = oneline
434+fate-h264-bsf-mp4toannexb-2: REF = cffcfa6a2d0b58c9de1f5785f099f41d
429435
430436 fate-h264-crop-to-container: CMD = framemd5 -i $(TARGET_SAMPLES)/h264/crop-to-container-dims-canon.mov
431437 fate-h264-direct-bff: CMD = framecrc -i $(TARGET_SAMPLES)/h264/direct-bff.mkv

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