From patchwork Fri Mar 24 22:31:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 3087 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.50.79 with SMTP id y76csp293435vsy; Fri, 24 Mar 2017 15:33:07 -0700 (PDT) X-Received: by 10.223.162.212 with SMTP id t20mr9481701wra.122.1490394787883; Fri, 24 Mar 2017 15:33:07 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k127si4905722wmb.49.2017.03.24.15.33.07; Fri, 24 Mar 2017 15:33:07 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 109C568922F; Sat, 25 Mar 2017 00:32:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f194.google.com (mail-qk0-f194.google.com [209.85.220.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4676F68832B for ; Sat, 25 Mar 2017 00:32:13 +0200 (EET) Received: by mail-qk0-f194.google.com with SMTP id p22so544026qka.0 for ; Fri, 24 Mar 2017 15:32:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=9wGheJS+esx0uQwjlkUldopAHBQx+n6RPRdvj4TxaQs=; b=YIGtJPqXr9oV1kUXGHA2XurF0A1YhGzbkMz6YKFIVcQqRe64sQmOdVB3DhGQX16fSu IlipB0HW0HVPiO88cAz+o0kH5sBV1hftbw+ExO3U8T5qJM4GsfY0bDE+hUtOt+pIs7Kp SFvXx0DWtuocpBPx0WAqfNDlcSuE7C/RUpuEeBo2GELZrezTYpwVUsv/Gxz13krYGqQb l0tdusslel8pOLxSxQ0myY58sSahuROdo0JtGQ+hLblSaYaM3ULU8s96uh34oBmQgr15 Mx7Mughi3I+UafYVeo22UDd9TcrqPgMJGGuz6clg+pXsFEVeRZHaZcbgwlKU6INH65Sq TXeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=9wGheJS+esx0uQwjlkUldopAHBQx+n6RPRdvj4TxaQs=; b=kszEkmmvFtP6PWIi0yed578IGBlP483E7kdljRotT4Cg0krwLhKiCXlboYmR9erNqo Y7482Qd4AJ09lxaZ0FmPIehCfxbaCmMP80tEZ8RaU+a1q41JKQuHnoKUVMy1lDvgKO+4 tZ3lnSHm+8ZiveV7irVsOjjcbOZoE1PwzQJ2JTqj2dTVXBiHf8ZbjIccmLBIWZn1JhlN /Hcv6SUmWMah/ece+bBrkA/UskBx+7hkXWE8j8XpqgeD+3/Z7ZHKozSimVVcbUX0vGk9 ZPMaN2SmAKey8UX3t3tASa9JvyMkoz5rT+2BAn0FQtEJa/9adXGhD+3ToXxa63Eh41Y1 osbw== X-Gm-Message-State: AFeK/H3mD2jJEAbFzdHiP8nS9WrMipJWpRtG2CG4hE3A9Db5+taZuptoSExCfF5I73qktQ== X-Received: by 10.55.109.198 with SMTP id i189mr10574328qkc.242.1490394752960; Fri, 24 Mar 2017 15:32:32 -0700 (PDT) Received: from localhost.localdomain ([181.231.62.139]) by smtp.gmail.com with ESMTPSA id e19sm2414824qta.68.2017.03.24.15.32.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Mar 2017 15:32:32 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 Mar 2017 19:31:44 -0300 Message-Id: <20170324223147.2692-4-jamrial@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170324223147.2692-1-jamrial@gmail.com> References: <20170324223147.2692-1-jamrial@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/6] avcodec/extract_extradata_bsf: use the parsing code from h264_split() 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The current code discards every non SPS/PPS/VPS NAL from extradata. No samples where such filtering breaks decoding has been found, but it's nonetheless safer to keep the current behaviour from AVCodecParser for the time being, or until the failures with h264 are fixed. Signed-off-by: James Almer --- libavcodec/extract_extradata_bsf.c | 107 ++++++++++++------------------------- 1 file changed, 35 insertions(+), 72 deletions(-) diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c index bb2e9bf68c..89981996f2 100644 --- a/libavcodec/extract_extradata_bsf.c +++ b/libavcodec/extract_extradata_bsf.c @@ -40,15 +40,6 @@ typedef struct ExtractExtradataContext { int remove; } ExtractExtradataContext; -static int val_in_array(const int *arr, int len, int val) -{ - int i; - for (i = 0; i < len; i++) - if (arr[i] == val) - return 1; - return 0; -} - static int extract_extradata_h264(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size) { @@ -98,77 +89,49 @@ static int extract_extradata_h264(AVBSFContext *ctx, AVPacket *pkt, static int extract_extradata_h265(AVBSFContext *ctx, AVPacket *pkt, uint8_t **data, int *size) { - static const int extradata_nal_types_hevc[] = { - HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS, - }; - ExtractExtradataContext *s = ctx->priv_data; + uint32_t state = UINT32_MAX; + const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size; + int has_vps = 0; + int has_sps = 0; + int has_pps = 0; + int nalu_type; - H2645Packet h2645_pkt = { 0 }; - int extradata_size = 0; - const int *extradata_nal_types; - int nb_extradata_nal_types; - int i, ret = 0; - - extradata_nal_types = extradata_nal_types_hevc; - nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc); - - ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size, - ctx, 0, 0, ctx->par_in->codec_id, 1); - if (ret < 0) - return ret; - - for (i = 0; i < h2645_pkt.nb_nals; i++) { - H2645NAL *nal = &h2645_pkt.nals[i]; - if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) - extradata_size += nal->raw_size + 3; - } - - if (extradata_size) { - AVBufferRef *filtered_buf; - uint8_t *extradata, *filtered_data; - - if (s->remove) { - filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE); - if (!filtered_buf) - goto fail; - filtered_data = filtered_buf->data; - } - - extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); - if (!extradata) { - av_buffer_unref(&filtered_buf); - goto fail; - } + while (ptr < end) { + ptr = avpriv_find_start_code(ptr, end, &state); + if ((state & 0xFFFFFF00) != 0x100) + break; + nalu_type = (state >> 1) & 0x3F; + if (nalu_type == HEVC_NAL_VPS) + has_vps = 1; + else if (nalu_type == HEVC_NAL_SPS) + has_sps = 1; + else if (nalu_type == HEVC_NAL_PPS) + has_pps = 1; + else if ((nalu_type != HEVC_NAL_SEI_PREFIX || has_pps) && + nalu_type != HEVC_NAL_AUD) { + if (has_vps && has_sps) { + while (ptr - 4 > pkt->data && ptr[-5] == 0) + ptr--; + if (ptr - pkt->data > 4) { + *size = ptr - 4 - pkt->data; + *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!*data) + return AVERROR(ENOMEM); - *data = extradata; - *size = extradata_size; + memcpy(*data, pkt->data, *size); - for (i = 0; i < h2645_pkt.nb_nals; i++) { - H2645NAL *nal = &h2645_pkt.nals[i]; - if (val_in_array(extradata_nal_types, nb_extradata_nal_types, - nal->type)) { - AV_WB24(extradata, 1); // startcode - memcpy(extradata + 3, nal->raw_data, nal->raw_size); - extradata += 3 + nal->raw_size; - } else if (s->remove) { - AV_WB24(filtered_data, 1); // startcode - memcpy(filtered_data + 3, nal->raw_data, nal->raw_size); - filtered_data += 3 + nal->raw_size; + if (s->remove) { + pkt->data += *size; + pkt->size -= *size; + } + } + return 0; } } - - if (s->remove) { - av_buffer_unref(&pkt->buf); - pkt->buf = filtered_buf; - pkt->data = filtered_buf->data; - pkt->size = filtered_data - filtered_buf->data; - } } -fail: - ff_h2645_packet_uninit(&h2645_pkt); - return ret; + return 0; } static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,