From patchwork Mon Feb 1 22:44:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 25338 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 B0552449C7A for ; Tue, 2 Feb 2021 00:53:06 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 86F7868A107; Tue, 2 Feb 2021 00:53:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 47A1A6881F5 for ; Tue, 2 Feb 2021 00:52:59 +0200 (EET) Received: by mail-qv1-f44.google.com with SMTP id r13so4627984qvm.11 for ; Mon, 01 Feb 2021 14:52:59 -0800 (PST) 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:mime-version :content-transfer-encoding; bh=HKqjN7udDOvYrvw/gaJz7t3eFIoKZMPvBICYq36AWvM=; b=rh2foSQUG8mG/fQ3F53ZGZUSKs3zW54DJf6MyiLUQ6D8PbbdSH/SfYxd2ZThrB1QuL lTF0/RDYXqkBZOkXizWkh5F/v8ojInKSqQuTs8+kYL48xY8mW27e0C0rkm44gziwfrhv GWdpgk/oDxQH2j3GrPGy+r/BDY4Q1fP4SKVSU6kVAPfK9W4iUqDqRBj5rW8qsK2Cf9Kg uHcPTqfK94U8bY5KIXOyA4xtjgstyIeZlHxJzF93wcQ3WYrZp5Wv2RWmroaZcLrh2m5D xLcvbEYxTlDtlTuTBWPr3/mXWdms0ZskmRvnJSk9MAOqY6YnXo0NIVtcPuNJKdYACum0 ip3g== 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:mime-version:content-transfer-encoding; bh=HKqjN7udDOvYrvw/gaJz7t3eFIoKZMPvBICYq36AWvM=; b=Oyq5fNLYuTYV1KBDBr+8wzlFvlah9B3SkmQ03GfMQXKX1T4+Eff1YgAbBseBKb+lQi cfNjgq6SUBRHJX6JE3Eoj131iDOLyCV7ByXebSGSrmYHNZCK8fzh/gEm1mSlcNSBx/p+ iBaDMibzLGF7uQvdn6VBvNPkF5si/Mtuha3vYKpyPwf4WSJgW9L3XvaJoVyhS4EagSUp pxJBHKwlHfE+pb9lrXi9YVlA+jYwxEPGtChqeqYPFHdXm2yPkfiVoTGqZwSSjkOzExQR qj8CVy60OCYmmmkv6tPXqv9WEiRfpcE8HonQLqlaLJXXNxlthk5RjFx3pjILV0RSCa+b WdQA== X-Gm-Message-State: AOAM530KY8eiUxSpvzEAQMcxutpZhIy4PE5LEBuvCrF8FEynPtn2tWpw AxK4pW1uiwJQs3uKJHm3fKPQpSYaZkIbFw== X-Google-Smtp-Source: ABdhPJxTsWMpWlLbfY7oQFlwTtRbSy2pTh/c7wYz24TGXh68fWiqm9OjULMcacJFVmaxFDV7MT2J8A== X-Received: by 2002:a05:6214:21a5:: with SMTP id t5mr17360414qvc.20.1612219491451; Mon, 01 Feb 2021 14:44:51 -0800 (PST) Received: from localhost.localdomain ([181.23.89.132]) by smtp.gmail.com with ESMTPSA id e1sm15812720qkd.135.2021.02.01.14.44.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 14:44:50 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 1 Feb 2021 19:44:16 -0300 Message-Id: <20210201224421.1395-6-jamrial@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210201224421.1395-1-jamrial@gmail.com> References: <20210201224421.1395-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/10] doc/examples/transcode_aac: use av_packet_alloc() to allocate packets 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" Signed-off-by: James Almer --- doc/examples/transcode_aac.c | 46 +++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/doc/examples/transcode_aac.c b/doc/examples/transcode_aac.c index e0c76f5b35..73786ab59b 100644 --- a/doc/examples/transcode_aac.c +++ b/doc/examples/transcode_aac.c @@ -245,14 +245,16 @@ cleanup: /** * Initialize one data packet for reading or writing. - * @param packet Packet to be initialized + * @param[out] packet Packet to be initialized + * @return Error code (0 if successful) */ -static void init_packet(AVPacket *packet) +static int init_packet(AVPacket **packet) { - av_init_packet(packet); - /* Set the packet data and size so that it is recognized as being empty. */ - packet->data = NULL; - packet->size = 0; + if (!(*packet = av_packet_alloc())) { + fprintf(stderr, "Could not allocate packet\n"); + return AVERROR(ENOMEM); + } + return 0; } /** @@ -371,28 +373,31 @@ static int decode_audio_frame(AVFrame *frame, int *data_present, int *finished) { /* Packet used for temporary storage. */ - AVPacket input_packet; + AVPacket *input_packet; int error; - init_packet(&input_packet); + + error = init_packet(&input_packet); + if (error < 0) + return error; /* Read one audio frame from the input file into a temporary packet. */ - if ((error = av_read_frame(input_format_context, &input_packet)) < 0) { + if ((error = av_read_frame(input_format_context, input_packet)) < 0) { /* If we are at the end of the file, flush the decoder below. */ if (error == AVERROR_EOF) *finished = 1; else { fprintf(stderr, "Could not read frame (error '%s')\n", av_err2str(error)); - return error; + goto cleanup; } } /* Send the audio frame stored in the temporary packet to the decoder. * The input audio stream decoder is used to do this. */ - if ((error = avcodec_send_packet(input_codec_context, &input_packet)) < 0) { + if ((error = avcodec_send_packet(input_codec_context, input_packet)) < 0) { fprintf(stderr, "Could not send packet for decoding (error '%s')\n", av_err2str(error)); - return error; + goto cleanup; } /* Receive one frame from the decoder. */ @@ -418,7 +423,7 @@ static int decode_audio_frame(AVFrame *frame, } cleanup: - av_packet_unref(&input_packet); + av_packet_free(&input_packet); return error; } @@ -661,9 +666,12 @@ static int encode_audio_frame(AVFrame *frame, int *data_present) { /* Packet used for temporary storage. */ - AVPacket output_packet; + AVPacket *output_packet; int error; - init_packet(&output_packet); + + error = init_packet(&output_packet); + if (error < 0) + return error; /* Set a timestamp based on the sample rate for the container. */ if (frame) { @@ -681,11 +689,11 @@ static int encode_audio_frame(AVFrame *frame, } else if (error < 0) { fprintf(stderr, "Could not send packet for encoding (error '%s')\n", av_err2str(error)); - return error; + goto cleanup; } /* Receive one encoded frame from the encoder. */ - error = avcodec_receive_packet(output_codec_context, &output_packet); + error = avcodec_receive_packet(output_codec_context, output_packet); /* If the encoder asks for more data to be able to provide an * encoded frame, return indicating that no data is present. */ if (error == AVERROR(EAGAIN)) { @@ -706,14 +714,14 @@ static int encode_audio_frame(AVFrame *frame, /* Write one audio frame from the temporary packet to the output file. */ if (*data_present && - (error = av_write_frame(output_format_context, &output_packet)) < 0) { + (error = av_write_frame(output_format_context, output_packet)) < 0) { fprintf(stderr, "Could not write frame (error '%s')\n", av_err2str(error)); goto cleanup; } cleanup: - av_packet_unref(&output_packet); + av_packet_free(&output_packet); return error; }