From patchwork Fri Aug 16 23:05:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Li X-Patchwork-Id: 14559 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 94421448223 for ; Sat, 17 Aug 2019 02:12:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6DD1D68AC5F; Sat, 17 Aug 2019 02:12:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8D2AE68ABB0 for ; Sat, 17 Aug 2019 02:12:34 +0300 (EEST) Received: by mail-wr1-f41.google.com with SMTP id s18so2989756wrn.1 for ; Fri, 16 Aug 2019 16:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=x7wsHFEhB7L9XPcBq7BNKf23zfdbwfCykv8wGsLBr8o=; b=B2EH3rtJ/v8KiXHYgWCDaeXEo2rdIdyRZj3oaipc2Kmk7Nw6J8kKjWi44chlaqDAKg 93y5ORQTSx54DUnEn0W8trxHK0Jgn91hY6ou9dYM5cqLfaPeqflN4bEr5Skh55qKYFgx 948KWACaIjgtPNXVS34L83kCdf0nHIEiytDYKQu/BU+A3+JBau5pEyjenQuRw0Ojoe/G la8glYYigY4hb6d1LfXgZm02m/omzc4Ihw0wnZCdNVLxpSvkJHVd0arhq9qVTkC6S8pp UK6sdFifXYqEwKyhiJS1jbuuVGo/FtyTPqt6yYBmoRPg0LfKglKQHGbtzwgP1g3DmYH1 j17Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=x7wsHFEhB7L9XPcBq7BNKf23zfdbwfCykv8wGsLBr8o=; b=IDPTUuSS5Xbe1FRjZSZBvI3bVlVUhCKBQyIQzvt+n12nL71mtyU++BDijE6viOEBJ2 dOk+lCfzgZEptVQ5udAQrAtFu1K4A7G0Fb/hwENpkURfXs7ok+9YUGopS+0W8P8pWZYR hMLWi+DmPwBRb+/iBcdm88Ja9LULtKSYhsf5E9ohXW9sRC5U0FyleUMg0IKFSBtoQxzX /y3JxP/oFgzdnRIy74vAudixqAbex4FqfNS1ojfq966Nmqrj42ey2+0++DW/SyA7ST7b gGDl8lvYJadFpHWM1pg8QlkRWNVo3gHLKiTW7nAYakXfLx84ppt4t+km+ccxnALa7+0R UAzQ== X-Gm-Message-State: APjAAAWS+H7OvzWl+TVEVbaA2eLiypeslET+/qlnje0G/8ZqoFISnA/N qPzhlFkDqGINLF9OQBcgPVibdqA1yrLU2JPF2AOBaEXB X-Google-Smtp-Source: APXvYqw2QNUkew3HGe7VyeOd6QKp4t4xIJu9uNkC+5aCNcS7dRTIV7GJhtArnF9FwWU1G3CgqYqie39qdFucNAYuPRM= X-Received: by 2002:adf:fd82:: with SMTP id d2mr12693662wrr.194.1565996744921; Fri, 16 Aug 2019 16:05:44 -0700 (PDT) MIME-Version: 1.0 From: Jun Li Date: Fri, 16 Aug 2019 16:05:33 -0700 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH v1] avcodec/h264_mp4toannexb_bsf: force sps/pps writing before the first pict X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Hi, This patch is trying to fix #6869. The ticket has a h264 stream NOT starting with IDR, and all frames are I frames. And the mp4toannexb filter insert SPS/PPS before IDR, so leads to the result that the output has no SPS/PPS. The fix is just simply insert SPS/PPS before first picture, no matter what type it is. Thanks. Best Regards -Jun From 052674e9d4e2e6cf29141858b6cdc707fb75ecb1 Mon Sep 17 00:00:00 2001 From: Jun Li Date: Fri, 16 Aug 2019 15:08:23 -0700 Subject: [PATCH v1] avcodec/h264_mp4toannexb_bsf: force sps/pps writing before the first pict Fix #6869, write sps/pps before the first picture nal, no matter what type of picture it is. --- libavcodec/h264_mp4toannexb_bsf.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c index fb3f24ea40..e616c4c210 100644 --- a/libavcodec/h264_mp4toannexb_bsf.c +++ b/libavcodec/h264_mp4toannexb_bsf.c @@ -36,6 +36,7 @@ typedef struct H264BSFContext { uint8_t idr_sps_seen; uint8_t idr_pps_seen; int extradata_parsed; + int first_pict; } H264BSFContext; static int alloc_and_copy(AVPacket *out, @@ -160,6 +161,7 @@ static int h264_mp4toannexb_init(AVBSFContext *ctx) s->idr_sps_seen = 0; s->idr_pps_seen = 0; s->extradata_parsed = 1; + s->first_pict = 0; } else { av_log(ctx, AV_LOG_ERROR, "Invalid extradata size: %d\n", extra_size); return AVERROR_INVALIDDATA; @@ -207,6 +209,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) buf += s->length_size; unit_type = *buf & 0x1f; + if (!s->first_pict && (unit_type == H264_NAL_IDR_SLICE || unit_type == H264_NAL_SLICE)) + s->first_pict = 1; + if (nal_size > buf_end - buf || nal_size < 0) goto fail; @@ -236,15 +241,15 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) if (!s->new_idr && unit_type == H264_NAL_IDR_SLICE && (buf[1] & 0x80)) s->new_idr = 1; - /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ - if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && !s->idr_sps_seen && !s->idr_pps_seen) { + /* prepend only to the first type 5 NAL unit of an IDR picture or the first pict NAL of the entire stream, if no sps/pps are already present */ + if ((s->new_idr && unit_type == H264_NAL_IDR_SLICE || s->first_pict == 1) && !s->idr_sps_seen && !s->idr_pps_seen) { if ((ret=alloc_and_copy(out, ctx->par_out->extradata, ctx->par_out->extradata_size, buf, nal_size, 1)) < 0) goto fail; s->new_idr = 0; /* if only SPS has been seen, also insert PPS */ - } else if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && s->idr_sps_seen && !s->idr_pps_seen) { + } else if ((s->new_idr && unit_type == H264_NAL_IDR_SLICE || s->first_pict == 1) && s->idr_sps_seen && !s->idr_pps_seen) { if (s->pps_offset == -1) { av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n"); if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size, 0)) < 0) @@ -263,6 +268,10 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) } } + /* first picture has been found, set the flag to -1 */ + if (s->first_pict == 1) + s->first_pict = -1; + next_nal: buf += nal_size; cumul_size += nal_size + s->length_size; -- 2.17.1