From patchwork Mon Nov 22 20:53:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ridley Combs X-Patchwork-Id: 31540 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp7583065iob; Mon, 22 Nov 2021 12:54:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJzHM2Y3EE5gsUdL80heAaJy5xdZC8hl+FwV5XEg8ov+LR6AK4HQRh54xbS/cSkTbngueCtu X-Received: by 2002:a05:6402:2930:: with SMTP id ee48mr71715319edb.295.1637614472061; Mon, 22 Nov 2021 12:54:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637614472; cv=none; d=google.com; s=arc-20160816; b=LmFCK08XkloCzdeJeqGWPJefEDlwkhrePpxqhxNGNPQvup7EBKC9KGfOBT5+VFx4GF Xsyvd429iGtG4QPgfrPSmnxmzjSlW890ZCwUugiGUp0k7kiXAKqi192JxePxF/vtOwco rI8PbfmIr065Pp3h1vQclRmu9AzSD/jmHhOTVuyzaJn20yQ9pfmS78/Ed8s83f3CWbcV HW3ONjgazlw3CaYTtTH677jkZO1MMUasWIPQ6SZDeFLoU1ADgvGHme4G7OBEQkOtfxuY hOPlT1qf0J6UbMPPdYn/b5NggefSf2yyZXAXIWMRPFQjgiyNHaORxGhSfdg02eU4yg/y rCXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=9USmkLa3WgykokEfk6l0toq9dAyEZaqAvQ0ZmulxTHo=; b=CgiMXYbleb0dEE2Ml1Cn8hN9BWqW/uN00zdYEGZzO28ZRdpXkrciEUl3WEvuqi1+Vk kKNTmQwJH8UxreAxN87m+uXqeHY8vb1KdYbXY69gg6rIZJRyvDLuSaWi/k3/FAREEC10 DE1nTAcO/BI0DKjFhWgXgj0NifkHICSwZWzVSchtREk+NTu/RMdS7e2b3z4WtgYAZ/Qr hPhe0IRzkoHI1ZOjhNGfmkrDIenAnRXFqg+wyusziIdI9OK9+5rg8ELmuH07eU9Gir9d myclhi78rgWUV11p9MAc8GLyWwJqasLgIzSUMvSDmvcpIM2DjXTrcUY4grzd6g/O37fJ ZgzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@rcombs.me header.s=google header.b=nDwapgFr; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=rcombs.me Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 5si20957596eji.566.2021.11.22.12.54.31; Mon, 22 Nov 2021 12:54:32 -0800 (PST) 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=@rcombs.me header.s=google header.b=nDwapgFr; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=rcombs.me Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C6BE668A93D; Mon, 22 Nov 2021 22:53:47 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8FC0068A701 for ; Mon, 22 Nov 2021 22:53:38 +0200 (EET) Received: by mail-io1-f52.google.com with SMTP id e144so25093867iof.3 for ; Mon, 22 Nov 2021 12:53:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rcombs.me; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=UEW4GZ9JW0/r6cyOGPmq4BBHjwoMynaY9s2/VJGvBxA=; b=nDwapgFr6Y2NvjEL9L9m2rKRH9rCvK+P8XGqRj2vVMhKpzUVPZm2duaq6j7hm7m2GW a0BBTx8dudmmByeerqL1OGLA+doNNxnVZap6fFHTHr1R6X1mmPWBfyLHjQnwUFo+KmkZ S0rOWtx/ItjjvkjEn5FJJrCmITuK4Bwh9BJruhG5sBs+QleMCtbct18r4wWpUkHRc1YR ddppCqTHw8TLUgcb/BUIgXCM21kVS3jYhyK4WCS84Wv+etTyFkNqtAzxOmn5UkbxQJ3z Ag1cgh6xgDzxMemf/OW50BlEoutmYt5QlXXsIKTZ1+RfuBQ6SlUUaBIytwuoYidkOsX9 /ddw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UEW4GZ9JW0/r6cyOGPmq4BBHjwoMynaY9s2/VJGvBxA=; b=InyXYyK3+IqFm2MsS0RpefxxRMvqqHU97BIQhP8W2+TiGm7nIvft73Uv8mKJyHaGWR X9VdOZESvreowV7KWkGdwwXLPLh8X1bF5mnoDolm3DPG5n2zDx3exGTOhGVWIYf5aukz X9BvZpty7XlXs1GN52a1gLR2RGNzXCLeCIG2FxS+ZGaXkyGRPL0z3ZadABeWlPnLd1o6 vaa0SUNSHOweUBl7rVlbrfgs2ggu/j50l2J7Hjp7OCwR9IwfoZbKD12/clKY5XvPufjo LZKVP6TUNfeWJV81DNorXAX4hnstsQ5vHVEe1Oj1IWyG/FpCYOxYwYMTIN8lAJb/qmNw ZCog== X-Gm-Message-State: AOAM531KwI5bwlQyf4cmtKVbwrm0N2/Kf46aey8ovRvEQ/GpnJdqVySS ibYLHGlNiHM1QPt/bED5nu+yN142pY1vJ2k= X-Received: by 2002:a5e:da09:: with SMTP id x9mr26399434ioj.171.1637614417007; Mon, 22 Nov 2021 12:53:37 -0800 (PST) Received: from rcombs-mbp.localdomain ([2601:243:2000:5ac:edf5:499:2479:42a8]) by smtp.gmail.com with ESMTPSA id b3sm3571580ile.26.2021.11.22.12.53.36 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Nov 2021 12:53:36 -0800 (PST) From: rcombs To: ffmpeg-devel@ffmpeg.org Date: Mon, 22 Nov 2021 14:53:19 -0600 Message-Id: <20211122205330.43546-5-rcombs@rcombs.me> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211122205330.43546-1-rcombs@rcombs.me> References: <20211122205330.43546-1-rcombs@rcombs.me> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/16] lavc/videotoolbox: escape 0x00000[0-3]s in avcC PSs X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: 1J/WYeN1HHJw --- libavcodec/videotoolbox.c | 53 +++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/libavcodec/videotoolbox.c b/libavcodec/videotoolbox.c index 49e726a75f..bd6e769207 100644 --- a/libavcodec/videotoolbox.c +++ b/libavcodec/videotoolbox.c @@ -140,14 +140,51 @@ int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame) #define AV_W8(p, v) *(p) = (v) +static int escape_ps(uint8_t* dst, const uint8_t* src, int src_size) +{ + int i; + int size = src_size; + uint8_t* p = dst; + + for (i = 0; i < src_size; i++) { + if (i + 2 < src_size && + src[i] == 0x00 && + src[i + 1] == 0x00 && + src[i + 2] <= 0x03) { + if (dst) { + *p++ = src[i++]; + *p++ = src[i++]; + *p++ = 0x03; + } else { + i += 2; + } + size++; + } + if (dst) + *p++ = src[i]; + } + + if (dst) + av_assert0((p - dst) == size); + + return size; +} + CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx) { VTContext *vtctx = avctx->internal->hwaccel_priv_data; H264Context *h = avctx->priv_data; CFDataRef data = NULL; uint8_t *p; - int vt_extradata_size = 6 + 2 + h->ps.sps->data_size + 3 + h->ps.pps->data_size; - uint8_t *vt_extradata = av_malloc(vt_extradata_size); + int sps_size = escape_ps(NULL, h->ps.sps->data, h->ps.sps->data_size); + int pps_size = escape_ps(NULL, h->ps.pps->data, h->ps.pps->data_size); + int vt_extradata_size; + uint8_t *vt_extradata; + int i; + + vt_extradata_size = 6 + 2 + sps_size + 3 + pps_size; + vt_extradata = av_malloc(vt_extradata_size); + if (!vt_extradata) return NULL; @@ -159,14 +196,14 @@ CFDataRef ff_videotoolbox_avcc_extradata_create(AVCodecContext *avctx) AV_W8(p + 3, h->ps.sps->data[3]); /* level */ AV_W8(p + 4, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 3 (11) */ AV_W8(p + 5, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */ - AV_WB16(p + 6, h->ps.sps->data_size); - memcpy(p + 8, h->ps.sps->data, h->ps.sps->data_size); - p += 8 + h->ps.sps->data_size; + AV_WB16(p + 6, sps_size); + p += 8; + p += escape_ps(p, h->ps.sps->data, h->ps.sps->data_size); AV_W8(p + 0, 1); /* number of pps */ - AV_WB16(p + 1, h->ps.pps->data_size); - memcpy(p + 3, h->ps.pps->data, h->ps.pps->data_size); + AV_WB16(p + 1, pps_size); + p += 3; + p += escape_ps(p, h->ps.pps->data, h->ps.pps->data_size); - p += 3 + h->ps.pps->data_size; av_assert0(p - vt_extradata == vt_extradata_size); // save sps header (profile/level) used to create decoder session,