From patchwork Sun Aug 15 18:25:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nachiket Tarate X-Patchwork-Id: 29542 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp1402380iov; Sun, 15 Aug 2021 11:26:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNGescnnniqPBL9FkErCXn1e+obd7S2jNrxnUGqDd5BLWSmq1CCR86WZmS2THEwNHqgoDx X-Received: by 2002:a17:906:76da:: with SMTP id q26mr12450570ejn.402.1629051981002; Sun, 15 Aug 2021 11:26:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629051980; cv=none; d=google.com; s=arc-20160816; b=wIedV1lAFth6fKz3mYntfTZ/l620JIzc5iNScKKCRxNfqZppYXNy+0u/OYLHvdYSdG XIO1Sj55BGTFFEogaeFp7d09KdbtpagP7eV0wOfgFS/iuqYEQcCQXGmjNGQKnwzdjS7T 4bSBWmQ/Wwu+6rILTbbqvRJIrCra2ST6pX4veFQodp8Nznm6oYgMAh0X40ujtB2lUHSF yHsX9Hd1Q9BvQdGPNa01m/lZ+vMafTbL1c/HwF6+mpcIpetZnIZjf9H7kOGkyZqVqNEf +BB4Qw/zCyUSu81IBSnZHiYK+YwZ+f7rsQ651gyG8SatJTRDvzWKpIZ4E9cS1Vym1O6q weAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to; bh=NhUNBPGsVsXWCzGW+5ViZzTXRV95GHMUgCOOhWzDkFM=; b=cbAALBRDjJ9Bn75nt8/ugiDH7aNvrJxPdpskM/XfvQr0UsEv5g7OQwytQJfxArEXKi o6jETyFI+PWru2Vcnr63BLCb18u/NpNaPf5dbNpryi4T0poZlFrAlyFGhEUXMPZ6yOVw 4ZM6+ThIkDTMfW7soGcVrrPSeoEtTb0B6GcIIQTf+KPfKjDL30SdxbQduu8keBcjRmxW KWm3Pm0gGvP4KvOyAp70EbEmfoJWqXryQhUDPjGPgIh6OI5ej1EVQgW2LCa9znpk8ZPm deBuzy/f7LHGQuqNQcqawwd/v+TkuycnNScxmLqF4Cz0OJ3NVtdDoWrV7JJOJtcMU072 Z+7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=MgsJrlLH; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j7si9638810ejm.492.2021.08.15.11.26.20; Sun, 15 Aug 2021 11:26:20 -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 header.s=20161025 header.b=MgsJrlLH; 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=QUARANTINE 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 F30FC68A41C; Sun, 15 Aug 2021 21:26:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6AE80687EBB for ; Sun, 15 Aug 2021 21:26:10 +0300 (EEST) Received: by mail-pj1-f53.google.com with SMTP id 28-20020a17090a031cb0290178dcd8a4d1so16092117pje.0 for ; Sun, 15 Aug 2021 11:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=rH//sR6h/d6ERccj10gXUgD260cVKVDxasaeeHrEGFE=; b=MgsJrlLHMgAfd+lN9oXrx76XFGFbR9XhZBWaZL3Hvd9UEXNHIx+9WGXDNpxdzIE9sQ c1GhtY3agIxItqfzrQRRF5SRWMIjHXNqQZ4JPzUiJK612HME2sBwDq6QTopyFWxumG8O sHR0PCcpJciutWUdSZjoq7MNj8o8MYuBHfBh3EQRpl5kxqPtlkvqmoe73aKwan6V4svm 1z0EEmqwde9AKAxgvP5xhIQtSUY6PTQgN7JyGafxdqbdv3jQcO8D/U6yH9e7qlr6/5GG gHd2o7zd0zBuNUG6FR1HXEBpgLygwYs3gy4uyMpfZtQyX+kyFzc6nJPNYJiagKZW3jQv wLHA== 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; bh=rH//sR6h/d6ERccj10gXUgD260cVKVDxasaeeHrEGFE=; b=dFTxxWtCFSzEyvXkConZis8bXpGxuEJSKX0WFVrKu47G4PmiuKlfZHyIDGOIS5aQrE vyGcDvZElgStguI1BrKLoOYwPyiIpJgS+6ib5Sml7gujp4DYbUTSb8AaA2BySSKSnSIL 1rd2sjvjDj7YL71II4VJ85bh/7O3zWVnbvoMjD7fNflUBpzEW+OQwSiFwB43cSJMZiY6 lmLaabR78vCVJD+51ql5nWQKELbmtnq6JyIIbOC33A4LEcxaNnnwwVh5E+iVRkK5J8l0 UId/spQH3TwVqqQKCLcAwomeaSd/F1G1MU9ibEZRIoTmGgrcRfdkh3Gfi+xeQq5LsqQI 7JEQ== X-Gm-Message-State: AOAM533cVj56N2zenvQsdmaKKVXOJ6RboX8gNvSzPWI5Fa//KEpJt76+ 1fFQ592p/LMitXx+4gy2tUeejhKs/LhVaw== X-Received: by 2002:a17:903:234e:b0:12d:ad8d:56c6 with SMTP id c14-20020a170903234e00b0012dad8d56c6mr5180532plh.23.1629051968008; Sun, 15 Aug 2021 11:26:08 -0700 (PDT) Received: from localhost.localdomain ([2401:4900:4458:83cd:6c5d:85b2:b4ee:c5e3]) by smtp.gmail.com with ESMTPSA id t10sm7054681pji.30.2021.08.15.11.26.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Aug 2021 11:26:07 -0700 (PDT) From: Nachiket Tarate To: ffmpeg-devel@ffmpeg.org Date: Sun, 15 Aug 2021 23:55:51 +0530 Message-Id: <20210815182551.12684-1-nachiket.programmer@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/4] libavcodec/adts_header: add frame_length field and avpriv function to parse AAC ADTS header 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: a0oGC8JxGlOD These will be used by HLS demuxer in case of sample decryption. Signed-off-by: Nachiket Tarate --- libavcodec/adts_header.c | 1 + libavcodec/adts_header.h | 14 ++++++++++++++ libavcodec/adts_parser.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/libavcodec/adts_header.c b/libavcodec/adts_header.c index 0889820f8a..e4454529c4 100644 --- a/libavcodec/adts_header.c +++ b/libavcodec/adts_header.c @@ -66,6 +66,7 @@ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr) hdr->sample_rate = avpriv_mpeg4audio_sample_rates[sr]; hdr->samples = (rdb + 1) * 1024; hdr->bit_rate = size * 8 * hdr->sample_rate / hdr->samples; + hdr->frame_length = size; return size; } diff --git a/libavcodec/adts_header.h b/libavcodec/adts_header.h index f615f6a9f9..9ecd67fb5b 100644 --- a/libavcodec/adts_header.h +++ b/libavcodec/adts_header.h @@ -34,6 +34,7 @@ typedef struct AACADTSHeaderInfo { uint8_t sampling_index; uint8_t chan_config; uint8_t num_aac_frames; + uint32_t frame_length; } AACADTSHeaderInfo; /** @@ -47,4 +48,17 @@ typedef struct AACADTSHeaderInfo { */ int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr); +/** + * Parse the ADTS frame header contained in the buffer, which is + * the first 54 bits. + * @param[in] buf Pointer to buffer containing the first 54 bits of the frame. + * @param[in] size Size of buffer containing the first 54 bits of the frame. + * @param[out] phdr Pointer to pointer to struct AACADTSHeaderInfo for which + * memory is allocated and header info is written into it. + * @return Returns 0 on success, -1 if there is a sync word mismatch, + * -2 if the version element is invalid, -3 if the sample rate + * element is invalid, or -4 if the bit rate element is invalid. + */ +int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_t size); + #endif /* AVCODEC_ADTS_HEADER_H */ diff --git a/libavcodec/adts_parser.c b/libavcodec/adts_parser.c index 5c9f8ff6f2..32489dadf4 100644 --- a/libavcodec/adts_parser.c +++ b/libavcodec/adts_parser.c @@ -42,3 +42,35 @@ int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames) return AVERROR(ENOSYS); #endif } + +int avpriv_adts_header_parse(AACADTSHeaderInfo **phdr, const uint8_t *buf, size_t size) +{ +#if CONFIG_ADTS_HEADER + int ret = 0; + GetBitContext gb; + + if (size < AV_AAC_ADTS_HEADER_SIZE) + return AVERROR_INVALIDDATA; + + if (!*phdr) + *phdr = av_mallocz(sizeof(AACADTSHeaderInfo)); + if (!*phdr) + return AVERROR(ENOMEM); + + ret = init_get_bits8(&gb, buf, AV_AAC_ADTS_HEADER_SIZE); + if (ret < 0) { + av_free(*phdr); + return ret; + } + + ret = ff_adts_header_parse(&gb, *phdr); + if (ret < 0) { + av_free(*phdr); + return ret; + } + + return 0; +#else + return AVERROR(ENOSYS); +#endif +} From patchwork Sun Aug 15 18:26:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nachiket Tarate X-Patchwork-Id: 29544 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp1402828iov; Sun, 15 Aug 2021 11:27:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqPVioiob0QyLfzYeW27X1ZdPS0tUHQ0ut/KBT5qxrfBC8RwQRBx7fU3hTBJxE+JDp6g32 X-Received: by 2002:a17:906:31cf:: with SMTP id f15mr12753708ejf.272.1629052028939; Sun, 15 Aug 2021 11:27:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629052028; cv=none; d=google.com; s=arc-20160816; b=ql/n3+840hbV0lMSH1IcP1OvJk0ZFHv8k9UX5utCB5nZ4CNLpDaGCB5lVBckadobsg 6X3jqBeldOTHcYLdvFq22GHxFAMU6RjTzJ0mam0FwNPdMu/VdShiG640W+CNdqqAbCM2 gAvaK/Ofy0OAkc6HqapSLLoZSl0pa8SNAqyAlkWN0sMTvoaPtrhPrL+1QQo8S4bQMQyH sJcjF/9SFmtf+UntigW9Zo8JA+6PyBQsglIKLdhWZhvyYfsvg40xp+qCfrRY3z6t5PDa o7QLOOJpX2V6EKRPsNjLFen4aa9QxXNP7rbwAFaHwg8/ruUlWHfhnciEVNFrP0aV4uX7 RYLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to; bh=VF0oYrdES9B9C/+dFniWlB+BNX1FOYlkS0h9r1VhZag=; b=tzE6Mxfomvm4OcuBCNKvOsdo2PqI9i97nMkFl2TkO3ODxQ4pVYbtk2f6CMisc0THy2 5ZnpZQpwxSxtvZ21DHQWl3kI4dG/w9jjTvJSxGTg4oJNygADUXIsSfXqhB09AltMShnS 5vbfgi3COXq4AOhkdb+nYQ+yEzktHFWcoCVz5YC9T4HpPSg5+zCmci05Kan/RuZbCzwv wyA/EddE6/sn3H/FC9TIaEpAv2kKuuv0cGRE3bonBCpKUUtKy5y/4KMrFkeYMcEYjtmG HPKpN+W2HWFbcskS9ntJSM6J3UpnM3ecsC/F9jY2cPGH/I3p/VnV7+OlZhI0FK6HcFcj xogw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Ln60sPiU; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s18si9996186ejh.472.2021.08.15.11.27.08; Sun, 15 Aug 2021 11:27:08 -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 header.s=20161025 header.b=Ln60sPiU; 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=QUARANTINE 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 734B368A4A8; Sun, 15 Aug 2021 21:26:50 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EF31D68A473 for ; Sun, 15 Aug 2021 21:26:43 +0300 (EEST) Received: by mail-pl1-f178.google.com with SMTP id c17so13028237plz.2 for ; Sun, 15 Aug 2021 11:26:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=FNXDfl1S+JQ0sAz1GYQcfxk3M/WY8ezVaKCkQa5FctU=; b=Ln60sPiU6M6wffVLnypitOVBjF767JYrBu8bBgRr03EJB3lXwQt7CyB4lLUob0MUho cB6y4US9o5Z5936np77TOIFo9tDrnEvgBqFJRVEbxPZLxU1Ywjz06OGNWZLVHJHcPXes kwbO3e/N3KIz8NL+aGQQ5Itt00rkd8KAR6L6EoXY0iIxPpg/vzHdZFwMe7zstkQYu30F F+eTfXsaXEpqFvoNdKmdTJgMZW38XPhFoPfMj+zovqs1ooV0JdQb/jfM4Q4LkOCeJ4ux EAZVsAUYZIEmlWZ7hiTClsxZRDAfl2QlKjShL03VjiyY5tMzlMhkxbYaq3vED+5veCj9 lscg== 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; bh=FNXDfl1S+JQ0sAz1GYQcfxk3M/WY8ezVaKCkQa5FctU=; b=FjtRs4BieCv2Fu8LQpZtXRatSSIKjFqXSydkZbx1urZj72eB10AUNBmGFpzxlPKR0X +y779judiMLHouynNK3vac+WkrSktmgGaelxxt36NJyb/P6pZakLazDenBACwA5QZvoD dSYPyq6rhPJSD9FyMSAdVI5+Xb87EPl3NJrs6G+z5l3ByfiQs7YOzQXFgAfZl/tFI22U 6ouwpYdT30boh5kuq2acHwOBi/raJREFOR0M38oO272GrDQSqOmt0dty9M0JP2JRDwCH cRUqKXO0Cx0j1z2Q/WvuCmaEGzch34TigltlKNd2VKgn8+4lWYyuagscTlDVm9Z5DFZw aPiQ== X-Gm-Message-State: AOAM531fzUzDwcEuoQNuCqI57bke/r1Lnb+GQetRYFLBoREt8LcnEGto ojEiOjyMuBNa5nWsmEJRL3icjFZF6ivKEQ== X-Received: by 2002:aa7:9821:0:b0:3e0:f219:6721 with SMTP id q1-20020aa79821000000b003e0f2196721mr12476795pfl.77.1629052001708; Sun, 15 Aug 2021 11:26:41 -0700 (PDT) Received: from localhost.localdomain ([2401:4900:4458:83cd:6c5d:85b2:b4ee:c5e3]) by smtp.gmail.com with ESMTPSA id a2sm10454443pgb.19.2021.08.15.11.26.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Aug 2021 11:26:41 -0700 (PDT) From: Nachiket Tarate To: ffmpeg-devel@ffmpeg.org Date: Sun, 15 Aug 2021 23:56:26 +0530 Message-Id: <20210815182626.12744-1-nachiket.programmer@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 2/4] libavformat/mov: add support for 'cens', 'cbc1' and 'cbcs' encryption schemes specified in Common Encryption (CENC) standard 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: GX3T11kCaiu0 correct implementation of 'cenc' encryption scheme to support decryption of partial cipher blocks at the end of subsamples https://www.iso.org/standard/68042.html Signed-off-by: Nachiket Tarate --- libavformat/isom.h | 2 + libavformat/mov.c | 246 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 242 insertions(+), 6 deletions(-) diff --git a/libavformat/isom.h b/libavformat/isom.h index ac1b3f3d56..705813844f 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -237,6 +237,8 @@ typedef struct MOVStreamContext { int has_sidx; // If there is an sidx entry for this stream. struct { struct AVAESCTR* aes_ctr; + struct AVAES *aes_ctx; + unsigned int frag_index_entry_base; unsigned int per_sample_iv_size; // Either 0, 8, or 16. AVEncryptionInfo *default_encrypted_sample; MOVEncryptionIndex *encryption_index; diff --git a/libavformat/mov.c b/libavformat/mov.c index 46bc7b5aa3..a0f52bd203 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -6578,15 +6578,150 @@ static int mov_read_dfla(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } -static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size) +static int cenc_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size) { int i, ret; + int bytes_of_protected_data; + int partially_encrypted_block_size; + uint8_t *partially_encrypted_block; + uint8_t block[16]; - if (sample->scheme != MKBETAG('c','e','n','c') || sample->crypt_byte_block != 0 || sample->skip_byte_block != 0) { - av_log(c->fc, AV_LOG_ERROR, "Only the 'cenc' encryption scheme is supported\n"); - return AVERROR_PATCHWELCOME; + if (!sc->cenc.aes_ctr) { + /* initialize the cipher */ + sc->cenc.aes_ctr = av_aes_ctr_alloc(); + if (!sc->cenc.aes_ctr) { + return AVERROR(ENOMEM); + } + + ret = av_aes_ctr_init(sc->cenc.aes_ctr, c->decryption_key); + if (ret < 0) { + return ret; + } + } + + av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv); + + if (!sample->subsample_count) { + /* decrypt the whole packet */ + av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size); + return 0; + } + + partially_encrypted_block_size = 0; + + for (i = 0; i < sample->subsample_count; i++) { + if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); + return AVERROR_INVALIDDATA; + } + + /* skip the clear bytes */ + input += sample->subsamples[i].bytes_of_clear_data; + size -= sample->subsamples[i].bytes_of_clear_data; + + /* decrypt the encrypted bytes */ + + if (partially_encrypted_block_size != 0) + { + memcpy(block, partially_encrypted_block, partially_encrypted_block_size); + memcpy(block+partially_encrypted_block_size, input, 16-partially_encrypted_block_size); + av_aes_ctr_crypt(sc->cenc.aes_ctr, block, block, 16); + memcpy(partially_encrypted_block, block, partially_encrypted_block_size); + memcpy(input, block+partially_encrypted_block_size, 16-partially_encrypted_block_size); + input += 16-partially_encrypted_block_size; + size -= 16-partially_encrypted_block_size; + bytes_of_protected_data = sample->subsamples[i].bytes_of_protected_data - (16-partially_encrypted_block_size); + } else { + bytes_of_protected_data = sample->subsamples[i].bytes_of_protected_data; + } + + if (i < sample->subsample_count-1) { + int num_of_encrypted_blocks = bytes_of_protected_data/16; + partially_encrypted_block_size = bytes_of_protected_data%16; + if (partially_encrypted_block_size != 0) + partially_encrypted_block = input + 16*num_of_encrypted_blocks; + av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, 16*num_of_encrypted_blocks); + } else { + av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, bytes_of_protected_data); + } + + input += bytes_of_protected_data; + size -= bytes_of_protected_data; + } + + if (size > 0) { + av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n"); + return AVERROR_INVALIDDATA; + } + + return 0; +} + +static int cbc1_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size) +{ + int i, ret; + int num_of_encrypted_blocks; + uint8_t iv[16]; + + if (!sc->cenc.aes_ctx) { + /* initialize the cipher */ + sc->cenc.aes_ctx = av_aes_alloc(); + if (!sc->cenc.aes_ctx) { + return AVERROR(ENOMEM); + } + + ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1); + if (ret < 0) { + return ret; + } } + memcpy(iv, sample->iv, 16); + + /* whole-block full sample encryption */ + if (!sample->subsample_count) { + /* decrypt the whole packet */ + av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1); + return 0; + } + + for (i = 0; i < sample->subsample_count; i++) { + if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); + return AVERROR_INVALIDDATA; + } + + if (sample->subsamples[i].bytes_of_protected_data % 16 != 0) { + av_log(c->fc, AV_LOG_ERROR, "subsample BytesOfProtectedData is not a multiple of 16\n"); + return AVERROR_INVALIDDATA; + } + + /* skip the clear bytes */ + input += sample->subsamples[i].bytes_of_clear_data; + size -= sample->subsamples[i].bytes_of_clear_data; + + /* decrypt the encrypted bytes */ + num_of_encrypted_blocks = sample->subsamples[i].bytes_of_protected_data/16; + if (num_of_encrypted_blocks > 0) { + av_aes_crypt(sc->cenc.aes_ctx, input, input, num_of_encrypted_blocks, iv, 1); + } + input += sample->subsamples[i].bytes_of_protected_data; + size -= sample->subsamples[i].bytes_of_protected_data; + } + + if (size > 0) { + av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n"); + return AVERROR_INVALIDDATA; + } + + return 0; +} + +static int cens_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size) +{ + int i, ret, rem_bytes; + uint8_t *data; + if (!sc->cenc.aes_ctr) { /* initialize the cipher */ sc->cenc.aes_ctr = av_aes_ctr_alloc(); @@ -6602,10 +6737,14 @@ static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *s av_aes_ctr_set_full_iv(sc->cenc.aes_ctr, sample->iv); + /* whole-block full sample encryption */ if (!sample->subsample_count) { /* decrypt the whole packet */ av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, size); return 0; + } else if (sample->crypt_byte_block == 0 && sample->skip_byte_block == 0) { + av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cens' scheme\n"); + return AVERROR_INVALIDDATA; } for (i = 0; i < sample->subsample_count; i++) { @@ -6619,7 +6758,18 @@ static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *s size -= sample->subsamples[i].bytes_of_clear_data; /* decrypt the encrypted bytes */ - av_aes_ctr_crypt(sc->cenc.aes_ctr, input, input, sample->subsamples[i].bytes_of_protected_data); + data = input; + rem_bytes = sample->subsamples[i].bytes_of_protected_data; + while (rem_bytes > 0) { + if (rem_bytes < 16*sample->crypt_byte_block) { + break; + } + av_aes_ctr_crypt(sc->cenc.aes_ctr, data, data, 16*sample->crypt_byte_block); + data += 16*sample->crypt_byte_block; + rem_bytes -= 16*sample->crypt_byte_block; + data += FFMIN(16*sample->skip_byte_block, rem_bytes); + rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes); + } input += sample->subsamples[i].bytes_of_protected_data; size -= sample->subsamples[i].bytes_of_protected_data; } @@ -6632,6 +6782,88 @@ static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *s return 0; } +static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size) +{ + int i, ret, rem_bytes; + uint8_t iv[16]; + uint8_t *data; + + if (!sc->cenc.aes_ctx) { + /* initialize the cipher */ + sc->cenc.aes_ctx = av_aes_alloc(); + if (!sc->cenc.aes_ctx) { + return AVERROR(ENOMEM); + } + + ret = av_aes_init(sc->cenc.aes_ctx, c->decryption_key, 16 * 8, 1); + if (ret < 0) { + return ret; + } + } + + /* whole-block full sample encryption */ + if (!sample->subsample_count) { + /* decrypt the whole packet */ + memcpy(iv, sample->iv, 16); + av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1); + return 0; + } else if (sample->crypt_byte_block == 0 && sample->skip_byte_block == 0) { + av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 'cbcs' scheme\n"); + return AVERROR_INVALIDDATA; + } + + for (i = 0; i < sample->subsample_count; i++) { + if (sample->subsamples[i].bytes_of_clear_data + sample->subsamples[i].bytes_of_protected_data > size) { + av_log(c->fc, AV_LOG_ERROR, "subsample size exceeds the packet size left\n"); + return AVERROR_INVALIDDATA; + } + + /* skip the clear bytes */ + input += sample->subsamples[i].bytes_of_clear_data; + size -= sample->subsamples[i].bytes_of_clear_data; + + /* decrypt the encrypted bytes */ + memcpy(iv, sample->iv, 16); + data = input; + rem_bytes = sample->subsamples[i].bytes_of_protected_data; + while (rem_bytes > 0) { + if (rem_bytes < 16*sample->crypt_byte_block) { + break; + } + av_aes_crypt(sc->cenc.aes_ctx, data, data, sample->crypt_byte_block, iv, 1); + data += 16*sample->crypt_byte_block; + rem_bytes -= 16*sample->crypt_byte_block; + data += FFMIN(16*sample->skip_byte_block, rem_bytes); + rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes); + } + input += sample->subsamples[i].bytes_of_protected_data; + size -= sample->subsamples[i].bytes_of_protected_data; + } + + if (size > 0) { + av_log(c->fc, AV_LOG_ERROR, "leftover packet bytes after subsample processing\n"); + return AVERROR_INVALIDDATA; + } + + return 0; +} + +static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *sample, uint8_t *input, int size) +{ + if (sample->scheme == MKBETAG('c','e','n','c') && sample->crypt_byte_block == 0 && sample->skip_byte_block == 0) { + return cenc_scheme_decrypt(c, sc, sample, input, size); + } else if (sample->scheme == MKBETAG('c','b','c','1') && sample->crypt_byte_block == 0 && sample->skip_byte_block == 0) { + return cbc1_scheme_decrypt(c, sc, sample, input, size); + } else if (sample->scheme == MKBETAG('c','e','n','s')) { + return cens_scheme_decrypt(c, sc, sample, input, size); + } else if (sample->scheme == MKBETAG('c','b','c','s')) { + return cbcs_scheme_decrypt(c, sc, sample, input, size); + } else { + av_log(c->fc, AV_LOG_ERROR, "invalid encryption scheme\n"); + return AVERROR_INVALIDDATA; + } +} + static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index) { MOVFragmentStreamInfo *frag_stream_info; @@ -6646,7 +6878,9 @@ static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPa // Note this only supports encryption info in the first sample descriptor. if (mov->fragment.stsd_id == 1) { if (frag_stream_info->encryption_index) { - encrypted_index = current_index - frag_stream_info->index_entry; + if (current_index == 0 && frag_stream_info->index_entry > 0) + sc->cenc.frag_index_entry_base = frag_stream_info->index_entry; + encrypted_index = current_index - (frag_stream_info->index_entry - sc->cenc.frag_index_entry_base); encryption_index = frag_stream_info->encryption_index; } else { encryption_index = sc->cenc.encryption_index; From patchwork Sun Aug 15 18:26:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nachiket Tarate X-Patchwork-Id: 29543 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp1402939iov; Sun, 15 Aug 2021 11:27:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzG5qbdBl5WvrynmLOsl4Uip+vOxNd4XmG9jVtpAX1aU3c/34e5+NsNMUHt2qD53qnJOyD8 X-Received: by 2002:a17:906:ced1:: with SMTP id si17mr12085002ejb.506.1629052045054; Sun, 15 Aug 2021 11:27:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629052045; cv=none; d=google.com; s=arc-20160816; b=qT/G3URfO/cU0+dxWaYzDVjrhkDC7wfNfJweAELgIZfX6hFpqNx4ZQL/33l4bw+8AH ZwA72eiMnyD/OPUZLAJ6yoTpWjDY2Ug/EZTDiyB08GppUKBSWcAGKpwlNJHIIn7YN58Q jg/PV4ydbpS2xSy1DwkclTCdngpBm6icE7gVU3rKwtwWGf3FBLBGXcFfINGW1mRCU+sJ ptI2/OHDPzMELAHx7uRInXndMZC3YQ8bq5Eq5MiSPQLP0rlydp9ZypOPBvefzIthNxkK ZpWY6gfXw8A/qhtrQx0onwvUmJEr/3fcAFMeOlv0r4diROYkr8wQSmEqvQeT76vAAuF7 yQjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to; bh=7+kPs7lt5kz/rguUE4HwaVtfn1ZG0n3zhoSU3toaxMY=; b=aGtGeQqxE+K+1MzLhBc+SD/Hyk0GvSr0XIhtqnln1F7KCQrRkvh/QmQ7wvroxBu5Qe sTaWi3F6qJ8V/AoD1TiXE6pD20Tqb9da8xIjADPXQxsgTWzv1o+Q1xnUmNITQzwY+a08 e7rRwp7hAtxZHtP7TJ98ShxqW/QcLDyc7kDIb4f2Y6/barbHBeFkp7LCJO5Pxf2nL12n WSxjGpZ1Ojg6msAnQbMD6Ebf/pFhBOGdIbEs+GVwP8+NpWxw0N81Y+p5UW3KF64HfAYf Gswy7WtY/XHPLpsyJnPmNYhKOPCamhrde01dbPCEDsYkZorzRgLgPXXhoiiFbg/O9XVA vM1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=m6yMJLgR; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a24si7851855edv.185.2021.08.15.11.27.24; Sun, 15 Aug 2021 11:27:25 -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 header.s=20161025 header.b=m6yMJLgR; 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=QUARANTINE 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 92C5768A4D4; Sun, 15 Aug 2021 21:27:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DB2F868A42A for ; Sun, 15 Aug 2021 21:27:14 +0300 (EEST) Received: by mail-pj1-f49.google.com with SMTP id j1so23064375pjv.3 for ; Sun, 15 Aug 2021 11:27:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=YamjbpDFjm9TSa0/GwCyTp/fYqjW5TNSEU27cEYjB/Q=; b=m6yMJLgR8oeRhqt+3phpSFi0wWuRHUFuoh4dzANU23hoUNGkO1rv3m6IUGnmTMHGNg zjiOjTRH1IKX4OxGuhhreVDgX0OJrLHRdfHTnuCoLDlMqc5aFGctcanIzAunnIJJo9QQ 3vrl4Lynmgic3QFKjVh0Kbs2Q8DSiijJracPveS+oHlCsWKk6lVei+bNZHet5ZizUPTe sOoiSvUzRjNo0mb0piV+9/XNY7SAi6muwjKbSQRF8lSH5lNXWS5fGIfAVY4CR8g5FawC 5Y9NxXEnW4m6ux67p8BIzldNnSDafZGBbYPEpCmRRN2US8Fg5HPPzznD2zFDbWEUlpWc Vpug== 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; bh=YamjbpDFjm9TSa0/GwCyTp/fYqjW5TNSEU27cEYjB/Q=; b=cmlc7pqdyp+fGNDYGhaHUr3h4qkqScsWX67g57aEFphKtR/XcPxJIATKItmwu2CnFv jT48r6GDpkQ9VIifZlgqS4WIuQChPhr0dB8SaC+gPPqv+/msWZLuAxP4MO5TpMqKM91z YnGYrU1q/0sFIquuLRcFdzQu2Ti0NKrlt9nGWIP0DSBK7zwDtlou7FdoI2SIROAEInBH ma4J9dOvnLBk4Fcykhw5B11PEybBq10WbJoivyd/U7y1shUZFYJr9HYzAWENV9FcOtiK bvSM1gfo10MqDCH9lWy1G65dr33rqPc4Aygy0KTQVK6hr/eJjtf7gEDrvZd8xk2cZFsu O+wA== X-Gm-Message-State: AOAM533XnvFFX+IPqEXkRYsaDL71JjLNGxktS3vH/kEg2sa/y6UwTkoO mmg7L1MlGCQkLsdTnEXmEQVZsHAV16xCVw== X-Received: by 2002:a63:d106:: with SMTP id k6mr12261426pgg.234.1629052032893; Sun, 15 Aug 2021 11:27:12 -0700 (PDT) Received: from localhost.localdomain ([2401:4900:4458:83cd:6c5d:85b2:b4ee:c5e3]) by smtp.gmail.com with ESMTPSA id m2sm10224776pgu.15.2021.08.15.11.27.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Aug 2021 11:27:12 -0700 (PDT) From: Nachiket Tarate To: ffmpeg-devel@ffmpeg.org Date: Sun, 15 Aug 2021 23:56:59 +0530 Message-Id: <20210815182659.12806-1-nachiket.programmer@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 3/4] libavformat/hls: add support for decryption of HLS media segments encrypted using SAMPLE-AES encryption method 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jlbKAlUnUmS4 Apple HTTP Live Streaming Sample Encryption: https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption Signed-off-by: Nachiket Tarate --- libavformat/Makefile | 2 +- libavformat/hls.c | 128 +++++++-- libavformat/hls_sample_encryption.c | 389 ++++++++++++++++++++++++++++ libavformat/hls_sample_encryption.h | 65 +++++ libavformat/mpegts.c | 11 + 5 files changed, 576 insertions(+), 19 deletions(-) create mode 100644 libavformat/hls_sample_encryption.c create mode 100644 libavformat/hls_sample_encryption.h diff --git a/libavformat/Makefile b/libavformat/Makefile index 813ddd3c20..7e3c9c23c6 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -238,7 +238,7 @@ OBJS-$(CONFIG_HCOM_DEMUXER) += hcom.o pcm.o OBJS-$(CONFIG_HDS_MUXER) += hdsenc.o OBJS-$(CONFIG_HEVC_DEMUXER) += hevcdec.o rawdec.o OBJS-$(CONFIG_HEVC_MUXER) += rawenc.o -OBJS-$(CONFIG_HLS_DEMUXER) += hls.o +OBJS-$(CONFIG_HLS_DEMUXER) += hls.o hls_sample_encryption.o OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o hlsplaylist.o avc.o OBJS-$(CONFIG_HNM_DEMUXER) += hnm.o OBJS-$(CONFIG_ICO_DEMUXER) += icodec.o diff --git a/libavformat/hls.c b/libavformat/hls.c index 3c1b80f60c..022dae0391 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2,6 +2,7 @@ * Apple HTTP Live Streaming demuxer * Copyright (c) 2010 Martin Storsjo * Copyright (c) 2013 Anssi Hannula + * Copyright (c) 2021 Nachiket Tarate * * This file is part of FFmpeg. * @@ -39,6 +40,8 @@ #include "avio_internal.h" #include "id3v2.h" +#include "hls_sample_encryption.h" + #define INITIAL_BUFFER_SIZE 32768 #define MAX_FIELD_LEN 64 @@ -145,6 +148,8 @@ struct playlist { int id3_changed; /* ID3 tag data has changed at some point */ ID3v2ExtraMeta *id3_deferred_extra; /* stored here until subdemuxer is opened */ + HLSAudioSetupInfo audio_setup_info; + int64_t seek_timestamp; int seek_flags; int seek_stream_index; /* into subdemuxer stream array */ @@ -214,6 +219,7 @@ typedef struct HLSContext { int http_multiple; int http_seekable; AVIOContext *playlist_pb; + HLSCryptoContext crypto_ctx; } HLSContext; static void free_segment_dynarray(struct segment **segments, int n_segments) @@ -1006,7 +1012,10 @@ fail: static struct segment *current_segment(struct playlist *pls) { - return pls->segments[pls->cur_seq_no - pls->start_seq_no]; + int64_t n = pls->cur_seq_no - pls->start_seq_no; + if (n >= pls->n_segments) + return NULL; + return pls->segments[n]; } static struct segment *next_segment(struct playlist *pls) @@ -1035,17 +1044,18 @@ static int read_from_url(struct playlist *pls, struct segment *seg, /* Parse the raw ID3 data and pass contents to caller */ static void parse_id3(AVFormatContext *s, AVIOContext *pb, - AVDictionary **metadata, int64_t *dts, + AVDictionary **metadata, int64_t *dts, HLSAudioSetupInfo *audio_setup_info, ID3v2ExtraMetaAPIC **apic, ID3v2ExtraMeta **extra_meta) { static const char id3_priv_owner_ts[] = "com.apple.streaming.transportStreamTimestamp"; + static const char id3_priv_owner_audio_setup[] = "com.apple.streaming.audioDescription"; ID3v2ExtraMeta *meta; ff_id3v2_read_dict(pb, metadata, ID3v2_DEFAULT_MAGIC, extra_meta); for (meta = *extra_meta; meta; meta = meta->next) { if (!strcmp(meta->tag, "PRIV")) { ID3v2ExtraMetaPRIV *priv = &meta->data.priv; - if (priv->datasize == 8 && !strcmp(priv->owner, id3_priv_owner_ts)) { + if (priv->datasize == 8 && !av_strncasecmp(priv->owner, id3_priv_owner_ts, 44)) { /* 33-bit MPEG timestamp */ int64_t ts = AV_RB64(priv->data); av_log(s, AV_LOG_DEBUG, "HLS ID3 audio timestamp %"PRId64"\n", ts); @@ -1053,6 +1063,8 @@ static void parse_id3(AVFormatContext *s, AVIOContext *pb, *dts = ts; else av_log(s, AV_LOG_ERROR, "Invalid HLS ID3 audio timestamp %"PRId64"\n", ts); + } else if (priv->datasize >= 8 && !av_strncasecmp(priv->owner, id3_priv_owner_audio_setup, 36)) { + ff_hls_senc_read_audio_setup_info(audio_setup_info, priv->data, priv->datasize); } } else if (!strcmp(meta->tag, "APIC") && apic) *apic = &meta->data.apic; @@ -1096,7 +1108,7 @@ static void handle_id3(AVIOContext *pb, struct playlist *pls) ID3v2ExtraMeta *extra_meta = NULL; int64_t timestamp = AV_NOPTS_VALUE; - parse_id3(pls->ctx, pb, &metadata, ×tamp, &apic, &extra_meta); + parse_id3(pls->ctx, pb, &metadata, ×tamp, &pls->audio_setup_info, &apic, &extra_meta); if (timestamp != AV_NOPTS_VALUE) { pls->id3_mpegts_timestamp = timestamp; @@ -1250,10 +1262,7 @@ static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, av_log(pls->parent, AV_LOG_VERBOSE, "HLS request for url '%s', offset %"PRId64", playlist %d\n", seg->url, seg->url_offset, pls->index); - if (seg->key_type == KEY_NONE) { - ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http); - } else if (seg->key_type == KEY_AES_128) { - char iv[33], key[33], url[MAX_URL_SIZE]; + if (seg->key_type == KEY_AES_128 || seg->key_type == KEY_SAMPLE_AES) { if (strcmp(seg->key, pls->key_url)) { AVIOContext *pb = NULL; if (open_url(pls->parent, &pb, seg->key, &c->avio_opts, opts, NULL) == 0) { @@ -1269,6 +1278,10 @@ static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, } av_strlcpy(pls->key_url, seg->key, sizeof(pls->key_url)); } + } + + if (seg->key_type == KEY_AES_128) { + char iv[33], key[33], url[MAX_URL_SIZE]; ff_data_to_hex(iv, seg->iv, sizeof(seg->iv), 0); ff_data_to_hex(key, pls->key, sizeof(pls->key), 0); iv[32] = key[32] = '\0'; @@ -1285,13 +1298,9 @@ static int open_input(HLSContext *c, struct playlist *pls, struct segment *seg, goto cleanup; } ret = 0; - } else if (seg->key_type == KEY_SAMPLE_AES) { - av_log(pls->parent, AV_LOG_ERROR, - "SAMPLE-AES encryption is not supported yet\n"); - ret = AVERROR_PATCHWELCOME; + } else { + ret = open_url(pls->parent, in, seg->url, &c->avio_opts, opts, &is_http); } - else - ret = AVERROR(ENOSYS); /* Seek to the requested position. If this was a HTTP request, the offset * should already be where want it to, but this allows e.g. local testing @@ -1854,6 +1863,9 @@ static int hls_close(AVFormatContext *s) free_variant_list(c); free_rendition_list(c); + if (c->crypto_ctx.aes_ctx) + av_free(c->crypto_ctx.aes_ctx); + av_dict_free(&c->avio_opts); ff_format_io_close(c->ctx, &c->playlist_pb); @@ -1959,7 +1971,8 @@ static int hls_read_header(AVFormatContext *s) struct playlist *pls = c->playlists[i]; const AVInputFormat *in_fmt = NULL; char *url; - AVDictionary *seg_format_opts = NULL; + AVDictionary *options = NULL; + struct segment *seg = NULL; if (!(pls->ctx = avformat_alloc_context())) return AVERROR(ENOMEM); @@ -1989,8 +2002,55 @@ static int hls_read_header(AVFormatContext *s) pls->ctx = NULL; return AVERROR(ENOMEM); } + ffio_init_context(&pls->pb, pls->read_buffer, INITIAL_BUFFER_SIZE, 0, pls, read_data, NULL, NULL); + + /* + * If encryption scheme is SAMPLE-AES, try to read ID3 tags of + * external audio track that contains audio setup information + */ + seg = current_segment(pls); + if (seg && seg->key_type == KEY_SAMPLE_AES && pls->n_renditions > 0 && + pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO) { + uint8_t buf[HLS_MAX_ID3_TAGS_DATA_LEN]; + if ((ret = avio_read(&pls->pb, buf, HLS_MAX_ID3_TAGS_DATA_LEN)) < 0) { + /* Fail if error was not end of file */ + if (ret != AVERROR_EOF) { + avformat_free_context(pls->ctx); + pls->ctx = NULL; + return ret; + } + } + ret = 0; + /* Reset reading */ + ff_format_io_close(pls->parent, &pls->input); + pls->input = NULL; + pls->input_read_done = 0; + ff_format_io_close(pls->parent, &pls->input_next); + pls->input_next = NULL; + pls->input_next_requested = 0; + pls->cur_seg_offset = 0; + pls->cur_init_section = NULL; + /* Reset EOF flag */ + pls->pb.eof_reached = 0; + /* Clear any buffered data */ + pls->pb.buf_end = pls->pb.buf_ptr = pls->pb.buffer; + /* Reset the position */ + pls->pb.pos = 0; + } + + /* + * If encryption scheme is SAMPLE-AES and audio setup information is present in external audio track, + * use that information to find the media format, otherwise probe input data + */ + if (seg && seg->key_type == KEY_SAMPLE_AES && pls->is_id3_timestamped && + pls->audio_setup_info.codec_id != AV_CODEC_ID_NONE) { + void *iter = NULL; + while ((in_fmt = (const AVInputFormat *)av_demuxer_iterate(&iter))) + if (in_fmt->raw_codec_id == pls->audio_setup_info.codec_id) + break; + } else { pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4; pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE; pls->ctx->interrupt_callback = s->interrupt_callback; @@ -2008,6 +2068,24 @@ static int hls_read_header(AVFormatContext *s) return ret; } av_free(url); + } + + if (seg && seg->key_type == KEY_SAMPLE_AES) { + if (strstr(in_fmt->name, "mov")) { + char key[33]; + ff_data_to_hex(key, pls->key, sizeof(pls->key), 0); + key[32] = '\0'; + av_dict_set(&options, "decryption_key", key, AV_OPT_FLAG_DECODING_PARAM); + } else if (!c->crypto_ctx.aes_ctx) { + c->crypto_ctx.aes_ctx = av_aes_alloc(); + if (!c->crypto_ctx.aes_ctx) { + avformat_free_context(pls->ctx); + pls->ctx = NULL; + return AVERROR(ENOMEM); + } + } + } + pls->ctx->pb = &pls->pb; pls->ctx->io_open = nested_io_open; pls->ctx->flags |= s->flags & ~AVFMT_FLAG_CUSTOM_IO; @@ -2015,10 +2093,10 @@ static int hls_read_header(AVFormatContext *s) if ((ret = ff_copy_whiteblacklists(pls->ctx, s)) < 0) return ret; - av_dict_copy(&seg_format_opts, c->seg_format_opts, 0); + av_dict_copy(&options, c->seg_format_opts, 0); - ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, &seg_format_opts); - av_dict_free(&seg_format_opts); + ret = avformat_open_input(&pls->ctx, pls->segments[0]->url, in_fmt, &options); + av_dict_free(&options); if (ret < 0) return ret; @@ -2039,7 +2117,12 @@ static int hls_read_header(AVFormatContext *s) * on us if they want to. */ if (pls->is_id3_timestamped || (pls->n_renditions > 0 && pls->renditions[0]->type == AVMEDIA_TYPE_AUDIO)) { + if (seg && seg->key_type == KEY_SAMPLE_AES && pls->audio_setup_info.setup_data_length > 0 && + pls->ctx->nb_streams == 1) + ret = ff_hls_senc_parse_audio_setup_info(pls->ctx->streams[0], &pls->audio_setup_info); + else ret = avformat_find_stream_info(pls->ctx, NULL); + if (ret < 0) return ret; } @@ -2166,6 +2249,7 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) while (1) { int64_t ts_diff; AVRational tb; + struct segment *seg = NULL; ret = av_read_frame(pls->ctx, pls->pkt); if (ret < 0) { if (!avio_feof(&pls->pb) && ret != AVERROR_EOF) @@ -2184,6 +2268,14 @@ static int hls_read_packet(AVFormatContext *s, AVPacket *pkt) get_timebase(pls), AV_TIME_BASE_Q); } + seg = current_segment(pls); + if (seg && seg->key_type == KEY_SAMPLE_AES && !strstr(pls->ctx->iformat->name, "mov")) { + enum AVCodecID codec_id = pls->ctx->streams[pls->pkt->stream_index]->codecpar->codec_id; + memcpy(c->crypto_ctx.iv, seg->iv, sizeof(seg->iv)); + memcpy(c->crypto_ctx.key, pls->key, sizeof(pls->key)); + ff_hls_senc_decrypt_frame(codec_id, &c->crypto_ctx, pls->pkt); + } + if (pls->seek_timestamp == AV_NOPTS_VALUE) break; diff --git a/libavformat/hls_sample_encryption.c b/libavformat/hls_sample_encryption.c new file mode 100644 index 0000000000..6538c2dceb --- /dev/null +++ b/libavformat/hls_sample_encryption.c @@ -0,0 +1,389 @@ +/* + * Apple HTTP Live Streaming Sample Encryption/Decryption + * + * Copyright (c) 2021 Nachiket Tarate + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Apple HTTP Live Streaming Sample Encryption + * https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption + */ + +#include "hls_sample_encryption.h" + +#include "libavcodec/adts_header.h" +#include "libavcodec/adts_parser.h" +#include "libavcodec/ac3_parser_internal.h" + + +typedef struct NALUnit { + uint8_t *data; + int type; + int length; + int start_code_length; +} NALUnit; + +typedef struct AudioFrame { + uint8_t *data; + int length; + int header_length; +} AudioFrame; + +typedef struct CodecParserContext { + const uint8_t *buf_ptr; + const uint8_t *buf_end; +} CodecParserContext; + +static const int eac3_sample_rate_tab[] = { 48000, 44100, 32000, 0 }; + +void ff_hls_senc_read_audio_setup_info(HLSAudioSetupInfo *info, const uint8_t *buf, size_t size) +{ + if (size < 8) + return; + + info->codec_tag = AV_RL32(buf); + + if (info->codec_tag == MKTAG('z','a','a','c')) + info->codec_id = AV_CODEC_ID_AAC; + else if (info->codec_tag == MKTAG('z','a','c','3')) + info->codec_id = AV_CODEC_ID_AC3; + else if (info->codec_tag == MKTAG('z','e','c','3')) + info->codec_id = AV_CODEC_ID_EAC3; + else + info->codec_id = AV_CODEC_ID_NONE; + + buf += 4; + info->priming = AV_RL16(buf); + buf += 2; + info->version = *buf++; + info->setup_data_length = *buf++; + + if (info->setup_data_length > size - 8) + info->setup_data_length = size - 8; + + if (info->setup_data_length > HLS_MAX_AUDIO_SETUP_DATA_LEN) + return; + + memcpy(info->setup_data, buf, info->setup_data_length); +} + +int ff_hls_senc_parse_audio_setup_info(AVStream *st, HLSAudioSetupInfo *info) +{ + int ret = 0; + + st->codecpar->codec_tag = info->codec_tag; + + if (st->codecpar->codec_id == AV_CODEC_ID_AAC) + return 0; + + if (st->codecpar->codec_id != AV_CODEC_ID_AC3 && st->codecpar->codec_id != AV_CODEC_ID_EAC3) + return AVERROR_INVALIDDATA; + + if (st->codecpar->codec_id == AV_CODEC_ID_AC3) { + AC3HeaderInfo *ac3hdr = NULL; + + ret = avpriv_ac3_parse_header(&ac3hdr, info->setup_data, info->setup_data_length); + if (ret < 0) { + if (ret != AVERROR(ENOMEM)) + av_free(ac3hdr); + return ret; + } + + st->codecpar->sample_rate = ac3hdr->sample_rate; + st->codecpar->channels = ac3hdr->channels; + st->codecpar->channel_layout = ac3hdr->channel_layout; + st->codecpar->bit_rate = ac3hdr->bit_rate; + + av_free(ac3hdr); + } else { /* Parse 'dec3' EC3SpecificBox */ + GetBitContext gb; + int data_rate, fscod, acmod, lfeon; + + ret = init_get_bits8(&gb, info->setup_data, info->setup_data_length); + if (ret < 0) + return AVERROR_INVALIDDATA; + + data_rate = get_bits(&gb, 13); + skip_bits(&gb, 3); + fscod = get_bits(&gb, 2); + skip_bits(&gb, 10); + acmod = get_bits(&gb, 3); + lfeon = get_bits(&gb, 1); + + st->codecpar->sample_rate = eac3_sample_rate_tab[fscod]; + + st->codecpar->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; + if (lfeon) + st->codecpar->channel_layout |= AV_CH_LOW_FREQUENCY; + + st->codecpar->channels = av_get_channel_layout_nb_channels(st->codecpar->channel_layout); + + st->codecpar->bit_rate = data_rate*1000; + } + + return 0; +} + +/* + * Remove start code emulation prevention 0x03 bytes + */ +static void remove_scep_3_bytes(NALUnit *nalu) +{ + int i = 0; + int j = 0; + + uint8_t *data = nalu->data; + + while (i < nalu->length) { + if (nalu->length - i > 3 && AV_RB24(&data[i]) == 0x000003) { + data[j++] = data[i++]; + data[j++] = data[i++]; + i++; + } else { + data[j++] = data[i++]; + } + } + + nalu->length = j; +} + +static int get_next_nal_unit(CodecParserContext *ctx, NALUnit *nalu) +{ + const uint8_t *nalu_start = ctx->buf_ptr; + + if (ctx->buf_end - ctx->buf_ptr >= 4 && AV_RB32(ctx->buf_ptr) == 0x00000001) + nalu->start_code_length = 4; + else if (ctx->buf_end - ctx->buf_ptr >= 3 && AV_RB24(ctx->buf_ptr) == 0x000001) + nalu->start_code_length = 3; + else /* No start code at the beginning of the NAL unit */ + return -1; + + ctx->buf_ptr += nalu->start_code_length; + + while (ctx->buf_ptr < ctx->buf_end) { + if (ctx->buf_end - ctx->buf_ptr >= 4 && AV_RB32(ctx->buf_ptr) == 0x00000001) + break; + else if (ctx->buf_end - ctx->buf_ptr >= 3 && AV_RB24(ctx->buf_ptr) == 0x000001) + break; + ctx->buf_ptr++; + } + + nalu->data = (uint8_t *)nalu_start + nalu->start_code_length; + nalu->length = ctx->buf_ptr - nalu->data; + nalu->type = *nalu->data & 0x1F; + + return 0; +} + +static int decrypt_nal_unit(HLSCryptoContext *crypto_ctx, NALUnit *nalu) +{ + int ret = 0; + int rem_bytes; + uint8_t *data; + uint8_t iv[16]; + + ret = av_aes_init(crypto_ctx->aes_ctx, crypto_ctx->key, 16 * 8, 1); + if (ret < 0) + return ret; + + /* Remove start code emulation prevention 0x03 bytes */ + remove_scep_3_bytes(nalu); + + data = nalu->data + 32; + rem_bytes = nalu->length - 32; + + memcpy(iv, crypto_ctx->iv, 16); + + while (rem_bytes > 0) { + if (rem_bytes > 16) { + av_aes_crypt(crypto_ctx->aes_ctx, data, data, 1, iv, 1); + data += 16; + rem_bytes -= 16; + } + data += FFMIN(144, rem_bytes); + rem_bytes -= FFMIN(144, rem_bytes); + } + + return 0; +} + +static int decrypt_video_frame(HLSCryptoContext *crypto_ctx, AVPacket *pkt) +{ + int ret = 0; + CodecParserContext ctx; + NALUnit nalu; + uint8_t *data_ptr; + int move_nalu = 0; + + memset(&ctx, 0, sizeof(ctx)); + ctx.buf_ptr = pkt->data; + ctx.buf_end = pkt->data + pkt->size; + + data_ptr = pkt->data; + + while (ctx.buf_ptr < ctx.buf_end) { + memset(&nalu, 0, sizeof(nalu)); + ret = get_next_nal_unit(&ctx, &nalu); + if (ret < 0) + return ret; + if ((nalu.type == 0x01 || nalu.type == 0x05) && nalu.length > 48) { + int encrypted_nalu_length = nalu.length; + ret = decrypt_nal_unit(crypto_ctx, &nalu); + if (ret < 0) + return ret; + move_nalu = nalu.length != encrypted_nalu_length; + } + if (move_nalu) + memmove(data_ptr, nalu.data - nalu.start_code_length, nalu.start_code_length + nalu.length); + data_ptr += nalu.start_code_length + nalu.length; + } + + av_shrink_packet(pkt, data_ptr - pkt->data); + + return 0; +} + +static int get_next_adts_frame(CodecParserContext *ctx, AudioFrame *frame) +{ + int ret = 0; + + AACADTSHeaderInfo *adts_hdr = NULL; + + /* Find next sync word 0xFFF */ + while (ctx->buf_ptr < ctx->buf_end - 1) { + if (*ctx->buf_ptr == 0xFF && *(ctx->buf_ptr + 1) & 0xF0 == 0xF0) + break; + ctx->buf_ptr++; + } + + if (ctx->buf_ptr >= ctx->buf_end - 1) + return -1; + + frame->data = (uint8_t*)ctx->buf_ptr; + + ret = avpriv_adts_header_parse (&adts_hdr, frame->data, ctx->buf_end - frame->data); + if (ret < 0) + return ret; + + frame->header_length = adts_hdr->crc_absent ? AV_AAC_ADTS_HEADER_SIZE : AV_AAC_ADTS_HEADER_SIZE + 2; + frame->length = adts_hdr->frame_length; + + av_free(adts_hdr); + + return 0; +} + +static int get_next_ac3_eac3_sync_frame(CodecParserContext *ctx, AudioFrame *frame) +{ + int ret = 0; + + AC3HeaderInfo *hdr = NULL; + + /* Find next sync word 0x0B77 */ + while (ctx->buf_ptr < ctx->buf_end - 1) { + if (*ctx->buf_ptr == 0x0B && *(ctx->buf_ptr + 1) == 0x77) + break; + ctx->buf_ptr++; + } + + if (ctx->buf_ptr >= ctx->buf_end - 1) + return -1; + + frame->data = (uint8_t*)ctx->buf_ptr; + frame->header_length = 0; + + ret = avpriv_ac3_parse_header(&hdr, frame->data, ctx->buf_end - frame->data); + if (ret < 0) { + if (ret != AVERROR(ENOMEM)) + av_free(hdr); + return ret; + } + + frame->length = hdr->frame_size; + + av_free(hdr); + + return 0; +} + +static int get_next_sync_frame(enum AVCodecID codec_id, CodecParserContext *ctx, AudioFrame *frame) +{ + if (codec_id == AV_CODEC_ID_AAC) + return get_next_adts_frame(ctx, frame); + else if (codec_id == AV_CODEC_ID_AC3 || codec_id == AV_CODEC_ID_EAC3) + return get_next_ac3_eac3_sync_frame(ctx, frame); + else + return AVERROR_INVALIDDATA; +} + +static int decrypt_sync_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AudioFrame *frame) +{ + int ret = 0; + uint8_t *data; + int num_of_encrypted_blocks; + + ret = av_aes_init(crypto_ctx->aes_ctx, crypto_ctx->key, 16 * 8, 1); + if (ret < 0) + return ret; + + data = frame->data + frame->header_length + 16; + + num_of_encrypted_blocks = (frame->length - frame->header_length - 16)/16; + + av_aes_crypt(crypto_ctx->aes_ctx, data, data, num_of_encrypted_blocks, crypto_ctx->iv, 1); + + return 0; +} + +static int decrypt_audio_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt) +{ + int ret = 0; + CodecParserContext ctx; + AudioFrame frame; + + memset(&ctx, 0, sizeof(ctx)); + ctx.buf_ptr = pkt->data; + ctx.buf_end = pkt->data + pkt->size; + + while (ctx.buf_ptr < ctx.buf_end) { + memset(&frame, 0, sizeof(frame)); + ret = get_next_sync_frame(codec_id, &ctx, &frame); + if (ret < 0) + return ret; + if (frame.length - frame.header_length > 31) { + ret = decrypt_sync_frame(codec_id, crypto_ctx, &frame); + if (ret < 0) + return ret; + } + ctx.buf_ptr += frame.length; + } + + return 0; +} + +int ff_hls_senc_decrypt_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt) +{ + if (codec_id == AV_CODEC_ID_H264) + return decrypt_video_frame(crypto_ctx, pkt); + else if (codec_id == AV_CODEC_ID_AAC || codec_id == AV_CODEC_ID_AC3 || codec_id == AV_CODEC_ID_EAC3) + return decrypt_audio_frame(codec_id, crypto_ctx, pkt); + + return AVERROR_INVALIDDATA; +} diff --git a/libavformat/hls_sample_encryption.h b/libavformat/hls_sample_encryption.h new file mode 100644 index 0000000000..ff3f9c22f7 --- /dev/null +++ b/libavformat/hls_sample_encryption.h @@ -0,0 +1,65 @@ +/* + * Apple HTTP Live Streaming Sample Encryption/Decryption + * + * Copyright (c) 2021 Nachiket Tarate + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Apple HTTP Live Streaming Sample Encryption + * https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption + */ + +#ifndef AVFORMAT_HLS_SAMPLE_ENCRYPTION_H +#define AVFORMAT_HLS_SAMPLE_ENCRYPTION_H + +#include + +#include "avformat.h" +#include "libavcodec/avcodec.h" + +#include "libavutil/aes.h" + +#define HLS_MAX_ID3_TAGS_DATA_LEN 138 +#define HLS_MAX_AUDIO_SETUP_DATA_LEN 10 + +typedef struct HLSCryptoContext { + struct AVAES *aes_ctx; + uint8_t key[16]; + uint8_t iv[16]; +} HLSCryptoContext; + +typedef struct HLSAudioSetupInfo { + enum AVCodecID codec_id; + uint32_t codec_tag; + uint16_t priming; + uint8_t version; + uint8_t setup_data_length; + uint8_t setup_data[HLS_MAX_AUDIO_SETUP_DATA_LEN]; +} HLSAudioSetupInfo; + + +void ff_hls_senc_read_audio_setup_info(HLSAudioSetupInfo *info, const uint8_t *buf, size_t size); + +int ff_hls_senc_parse_audio_setup_info(AVStream *st, HLSAudioSetupInfo *info); + +int ff_hls_senc_decrypt_frame(enum AVCodecID codec_id, HLSCryptoContext *crypto_ctx, AVPacket *pkt); + +#endif /* AVFORMAT_HLS_SAMPLE_ENCRYPTION_H */ + diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index a02965bacf..25518e3172 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -839,6 +839,15 @@ static const StreamType MISC_types[] = { { 0 }, }; +/* HLS Sample Encryption Types */ +static const StreamType HLS_SAMPLE_ENC_types[] = { + { 0xdb, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264}, + { 0xcf, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC }, + { 0xc1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, + { 0xc2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3}, + { 0 }, +}; + static const StreamType REGD_types[] = { { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC }, { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, @@ -948,6 +957,8 @@ static int mpegts_set_stream_info(AVStream *st, PESContext *pes, } if (st->codecpar->codec_id == AV_CODEC_ID_NONE) mpegts_find_stream_type(st, pes->stream_type, MISC_types); + if (st->codecpar->codec_id == AV_CODEC_ID_NONE) + mpegts_find_stream_type(st, pes->stream_type, HLS_SAMPLE_ENC_types); if (st->codecpar->codec_id == AV_CODEC_ID_NONE) { st->codecpar->codec_id = old_codec_id; st->codecpar->codec_type = old_codec_type; From patchwork Sun Aug 15 18:27:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nachiket Tarate X-Patchwork-Id: 29545 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp1403141iov; Sun, 15 Aug 2021 11:27:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwtXAhRxMffSKtxGpwVTd2yIRNogK3EJeNcft22097RPA7+0lOcWWwXpFGZ0fBRL08x6Eg X-Received: by 2002:a05:6402:2554:: with SMTP id l20mr2173973edb.252.1629052069627; Sun, 15 Aug 2021 11:27:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629052069; cv=none; d=google.com; s=arc-20160816; b=XtvFMaaDElI8+GFo3jCNZXbUpp0HRbvpXLrchIeAtjW/i0/qqW7wV50FAroycKa5GL mB58W26hJohbdN5wZZveflU7l+MYR1R94QqN93MIirgJf3qmg9P0SeGa1c1MQV/HYnB8 fKGMrtie02WGI1t40diCp2hk7Dt4Nqzk8hhpCVWPMR0sSodePlJnQofjtPpkbyZSaIaa GNob9JG7DM3eNCDa15IRUuL+VdJaeJwlsKIGDnPLsFlg8DrLu9funuo1uqt3CPp6XmbO lSNpVSP8Y40V5CZCWTqsZgToDHwux/RpNxCig5CM3fw5jpPUQuQJSJJJqT6xSNvYWY9L V/Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to; bh=wOanLjywALz3WUuYB+6l2zgBKJDlCCX5wqaicXbVVyE=; b=ou9EQ8bv/Je6+CFOF2bmg1uCM+y8Ypnys151/RNbs+NZtFG94DYUzjBUGhsBI/v71g DTBqpZZ3E4rHQZVVwd27Fszxchh/e4wKaH8oLE3PNSxvJiUh3zgB3xGc4PNuhLsxvVBp UsD7yDAaTCmiSQrZWgyIgaeTpeZWA1npmnQFfL6qqO1i0ryDF0HXqFHByoYiaiaxxqQ0 ToFmaBmSs1hS7AuJP85Qx1r5WAQ/0x3s77B1FJpgK4oPrEg8sXUVZVmyAc8n+khLMs7p RtOc8THO4E2BDV2uTRbgY+a+3/2h/ISUj5Y65YJAl9xXGafZTaKtBTNg3BuMzQP/b8x4 PhsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Tg6JYO2H; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g23si7568019ejk.552.2021.08.15.11.27.46; Sun, 15 Aug 2021 11:27:49 -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 header.s=20161025 header.b=Tg6JYO2H; 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=QUARANTINE 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 C1C3668A4A3; Sun, 15 Aug 2021 21:27:44 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0F315689D1F for ; Sun, 15 Aug 2021 21:27:42 +0300 (EEST) Received: by mail-pl1-f181.google.com with SMTP id w6so11335124plg.9 for ; Sun, 15 Aug 2021 11:27:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=erHUTjZLlzj5ftXMrMYW8XK3/GV8xXASVU3OEtacL9s=; b=Tg6JYO2HrJk5DQycjWCpwRd7hZekVKS3ZOjoQqXoQIhES6BNLyb0J1bJSgQiCcm1c8 PJy/hXSYIxWBNdmoEogh+AOsCbAEfTUiXu2k1cu0uegYJYlvtvYJdEIRHshidl+TZGWd NIS3G06RVftS4d1KXvW8qBjoqmavwAXyedh7hKwsmY7ZM/M1GByW07fV1ueupuy5gfXk fOj46mLJ1+thnGhMf8DfAB92cpmNVWlw4oUtVx5UCSVRA4pFuuTO953GzcYn+c/nII9L jCg7Rt4G4PeXr+RyFF/OA/aDz2m11D8Qq2ElPbWj1+gLzR3/Mr5/unUWNI1A6MMNsKd9 D1qQ== 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; bh=erHUTjZLlzj5ftXMrMYW8XK3/GV8xXASVU3OEtacL9s=; b=LbNoCMFTn4SmK3fdtK0NhimU/kcMoZRL/FUsusKave/kcvTovtkk8uv9Pu9Avs86mP mAcohQOIxUFl5q0CaAwZdx+SIkjJIgQJvAOBY2Pdtrt1RAD8LYH7QYOZN3dscZpMdd4C alUwOjebIarVPq9ZW2DA2DuMlVPl96r+mjkCEu2qiqQsBpHn4mu81XP05dwKHqphSknV HwwFVIkDyW5HUSll03zj1TyvvpEhm205ri7DHmIm8TCac7gI19hrZ8YrfH60gEQmJ+Yj nFWQfSRjZZdO3+z9u/qrKI0aA7+uZsrqjsHv+UEX9Gw0auB3MVc44v0uJwp363v2sMyV URrg== X-Gm-Message-State: AOAM530DO2cSDmyaKeY5+cWRDt/gy7ZtLttpw6UU/3tgptoS5GE7qFjn gaVGbiMwIUTKAGR0Uh+1YeiHFtCVw5r5Dg== X-Received: by 2002:a65:4045:: with SMTP id h5mr12221323pgp.136.1629052060939; Sun, 15 Aug 2021 11:27:40 -0700 (PDT) Received: from localhost.localdomain ([2401:4900:4458:83cd:6c5d:85b2:b4ee:c5e3]) by smtp.gmail.com with ESMTPSA id 73sm1485138pfz.73.2021.08.15.11.27.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Aug 2021 11:27:40 -0700 (PDT) From: Nachiket Tarate To: ffmpeg-devel@ffmpeg.org Date: Sun, 15 Aug 2021 23:57:26 +0530 Message-Id: <20210815182726.12862-1-nachiket.programmer@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 4/4] libavformat/hls: correct indentation 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: /GON1MTtjrvh Signed-off-by: Nachiket Tarate --- libavformat/hls.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 022dae0391..7e457281f4 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -2051,23 +2051,23 @@ static int hls_read_header(AVFormatContext *s) if (in_fmt->raw_codec_id == pls->audio_setup_info.codec_id) break; } else { - pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4; - pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE; - pls->ctx->interrupt_callback = s->interrupt_callback; - url = av_strdup(pls->segments[0]->url); - ret = av_probe_input_buffer(&pls->pb, &in_fmt, url, NULL, 0, 0); - if (ret < 0) { - /* Free the ctx - it isn't initialized properly at this point, - * so avformat_close_input shouldn't be called. If - * avformat_open_input fails below, it frees and zeros the - * context, so it doesn't need any special treatment like this. */ - av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url); - avformat_free_context(pls->ctx); - pls->ctx = NULL; + pls->ctx->probesize = s->probesize > 0 ? s->probesize : 1024 * 4; + pls->ctx->max_analyze_duration = s->max_analyze_duration > 0 ? s->max_analyze_duration : 4 * AV_TIME_BASE; + pls->ctx->interrupt_callback = s->interrupt_callback; + url = av_strdup(pls->segments[0]->url); + ret = av_probe_input_buffer(&pls->pb, &in_fmt, url, NULL, 0, 0); + if (ret < 0) { + /* Free the ctx - it isn't initialized properly at this point, + * so avformat_close_input shouldn't be called. If + * avformat_open_input fails below, it frees and zeros the + * context, so it doesn't need any special treatment like this. */ + av_log(s, AV_LOG_ERROR, "Error when loading first segment '%s'\n", url); + avformat_free_context(pls->ctx); + pls->ctx = NULL; + av_free(url); + return ret; + } av_free(url); - return ret; - } - av_free(url); } if (seg && seg->key_type == KEY_SAMPLE_AES) { @@ -2121,7 +2121,7 @@ static int hls_read_header(AVFormatContext *s) pls->ctx->nb_streams == 1) ret = ff_hls_senc_parse_audio_setup_info(pls->ctx->streams[0], &pls->audio_setup_info); else - ret = avformat_find_stream_info(pls->ctx, NULL); + ret = avformat_find_stream_info(pls->ctx, NULL); if (ret < 0) return ret;