From patchwork Wed Jan 3 04:54:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhangjiejun1992@gmail.com X-Patchwork-Id: 7097 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp15472751jad; Tue, 2 Jan 2018 20:55:21 -0800 (PST) X-Google-Smtp-Source: ACJfBosJhTimBYfNpCaP/ZY5UZLmogHKP3I8AMqWHxCfpIwn33AqHCUPzmX+ormuNxwQl83nX14T X-Received: by 10.28.239.13 with SMTP id n13mr329327wmh.151.1514955321616; Tue, 02 Jan 2018 20:55:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514955321; cv=none; d=google.com; s=arc-20160816; b=EgVQPNKz2cl04zw8Nc/cAvPxKtp/0z5m2QI42mhQGzBZoLTWXluF6itx8boq/oARfC 5za26wJQFj4J1119Z8BXGmtV9lJ07EqShYxuI8CiTfMYi8DzT5UI5d+zVmGPo2y+mpqt I+06jK4g7gx4NP9f+lAm6JNqxvpXi4eyicZaRrTDOgpe+eeWB6ytpC4uvHePO9aoF28h 3cx6G3lbgQrlGbLCxWkM4M93TPl0QHDFBsv0FgzCKF8VhEVSwlFe1kpBD7Q3wpZAO+6A WEqwoSBW6Aym++pv4kL0J8J2UV7VTOlxWTrbWFvJehc+gD7V6r2c6un7JiaQYEUUEeMv JQJg== 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:cc: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:arc-authentication-results; bh=FfexOMG+caiP+umKOszgPgcjKampa5ULEiA3+HV+h3Y=; b=He0KpXXpBBasKoWvvzWinRwgi6nihNS+wwFhdf1j9h4BJiJrZDUvqG0uchNxOfDpd9 cdNSEGn/lNbpwTvOYXhEEWah0A/tajbpVIY84FuZ9mmCAKrxGZnmc5ZZ+cuc69wJFitm revfLlMVWx4MFABTpzZhhxqcLazqf4yiEMmghrgYZ85rsbGsJp8BiAqx4yCSHKs44kuz OMWqUTCJ1hwYxfqQSpgyqVyZRXpQheUfKo4toCGBFo62WgFDqKKu4qGnJIr7Dw1J4Kd9 kcFd0NRlhU4xDMAZyupgYLQ87gww++B9JejDDDkLzHJDsg8c5sR11ovBDwgrQhlGVcDf IQHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=f/R/euQ8; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q17si319586wmf.242.2018.01.02.20.55.20; Tue, 02 Jan 2018 20:55:21 -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=@gmail.com header.s=20161025 header.b=f/R/euQ8; 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 2E7E1688310; Wed, 3 Jan 2018 06:55:03 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl0-f48.google.com (mail-pl0-f48.google.com [209.85.160.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B80B3680389 for ; Wed, 3 Jan 2018 06:54:55 +0200 (EET) Received: by mail-pl0-f48.google.com with SMTP id n13so554871plp.11 for ; Tue, 02 Jan 2018 20:55:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=+u9Vd4zqtai7qH5v3Lswysx336l0ZmwtuDgAWeWx110=; b=f/R/euQ8lG5QmSwO+9uY7kFBYBzQFUX6uhKOk4tr9R71agIA863fAzhpbpHTAred0w 3YjldzAejAnSOvsN1pfshjQ69qN+6rc43/s0xdBYM0jwcjso2LyYVpyzBDL38cdzwhTx 3HXa9anXwN4lv3Rmop+MSiXHMc4psE8v1Kt7mdTevVksT0AP/gZ6kdnlhYx0qAYBWJai ShCCN6Uf3b3jZMIRrbTBSWeBprEzC+cBKCcBM5W6HshvPWX6LXMZKH99rBlC9faqqOOt AnsEAbnANjf0tfRGHn8xARhdXdfLdfh8F6QyZsQsLh6oQWYqnLswZ0sEY4quglX/BvOU WnPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+u9Vd4zqtai7qH5v3Lswysx336l0ZmwtuDgAWeWx110=; b=eTnmK6+dFNn7AqfxKJpaTg+oUxqqUYn+oBakhYQHkjB8/HGt+BhRbHjPfBtKq1kwHi zkeCNdJuuZebMefo9+A0MNlHI82Ejhhb0s1acl86sNw6xX4iaueuSD02L1A5hsidVTWm tlVylzP7qYIieW734+krEGDvJ43X4CEeTuKd24zfag+OCF116dHjHEPQ85hrPiMezZkk wm9rmh7PXXuWxWOmstzggfuoilDP8D1DU/U+VfK8nnm8m7r6Q26csiYTDLsJcHEJCcpX 914fd4QLtV1ny411zgcelVLnuuTf43JEIpFwN1Ulh23Gs4Yw6OilaGmttmoRnShJ+93z tG1A== X-Gm-Message-State: AKGB3mL7Ol5xvd5rbz7fIyKsbTXsZ4jVq4/XnRCG2d/W063kToXz2yli a+Tc4XYlvu+EvG5n3VXv3QoJ9g== X-Received: by 10.159.235.132 with SMTP id f4mr332486plr.122.1514955310232; Tue, 02 Jan 2018 20:55:10 -0800 (PST) Received: from localhost.localdomain (ec2-52-220-245-83.ap-southeast-1.compute.amazonaws.com. [52.220.245.83]) by smtp.gmail.com with ESMTPSA id e190sm490066pfe.15.2018.01.02.20.55.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 02 Jan 2018 20:55:09 -0800 (PST) From: zhangjiejun1992@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Jan 2018 12:54:20 +0800 Message-Id: <20180103045420.11869-1-zhangjiejun1992@gmail.com> X-Mailer: git-send-email 2.14.3 (Apple Git-98) Subject: [FFmpeg-devel] [PATCH v4] lavc/audiotoolboxenc: fix noise in encoded audio 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 Cc: Jiejun Zhang MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Jiejun Zhang This fixes #6940 Although undocumented, AudioToolbox seems to require the data supplied by the callback (i.e. ffat_encode_callback) being unchanged until the next time the callback is called. In the old implementation, the AVBuffer backing the frame is recycled after the frame is freed, and somebody else (maybe the decoder) will write into the AVBuffer and change the data. AudioToolbox then encodes some wrong data and noise is produced. Retaining a frame reference solves this problem. --- libavcodec/audiotoolboxenc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c index 71885d1530..4d8130af96 100644 --- a/libavcodec/audiotoolboxenc.c +++ b/libavcodec/audiotoolboxenc.c @@ -48,6 +48,8 @@ typedef struct ATDecodeContext { AudioFrameQueue afq; int eof; int frame_size; + + AVFrame* encoding_frame; } ATDecodeContext; static UInt32 ffat_get_format_id(enum AVCodecID codec, int profile) @@ -442,6 +444,10 @@ static av_cold int ffat_init_encoder(AVCodecContext *avctx) ff_af_queue_init(avctx, &at->afq); + at->encoding_frame = av_frame_alloc(); + if (!at->encoding_frame) + return AVERROR(ENOMEM); + return 0; } @@ -453,6 +459,7 @@ static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_pac AVCodecContext *avctx = inctx; ATDecodeContext *at = avctx->priv_data; AVFrame *frame; + int ret; if (!at->frame_queue.available) { if (at->eof) { @@ -475,6 +482,12 @@ static OSStatus ffat_encode_callback(AudioConverterRef converter, UInt32 *nb_pac if (*nb_packets > frame->nb_samples) *nb_packets = frame->nb_samples; + av_frame_unref(at->encoding_frame); + if ((ret = av_frame_ref(at->encoding_frame, frame)) < 0) { + *nb_packets = 0; + return ret; + } + ff_bufqueue_add(avctx, &at->used_frame_queue, frame); return 0; @@ -565,6 +578,7 @@ static av_cold int ffat_close_encoder(AVCodecContext *avctx) ff_bufqueue_discard_all(&at->frame_queue); ff_bufqueue_discard_all(&at->used_frame_queue); ff_af_queue_close(&at->afq); + av_frame_free(&at->encoding_frame); return 0; }