From patchwork Mon Feb 1 22:44:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 25332 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 0FBD044BD9A for ; Tue, 2 Feb 2021 00:45:01 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E58A868A588; Tue, 2 Feb 2021 00:45:00 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5B2C368A328 for ; Tue, 2 Feb 2021 00:44:54 +0200 (EET) Received: by mail-qk1-f179.google.com with SMTP id r77so18034657qka.12 for ; Mon, 01 Feb 2021 14:44:54 -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=9aljdYkb+Y3yTM3ooLhRosreB0W3nf0TBwCQ0LbT4kE=; b=uhvFvRDZjygPY7Rcz+Gyqc4r2VPMAR3i06gmRwh7Xf1dRbkfirXn+iJhCsKYdbeQ1x ANhyYjmHBCgHx05T4Of9IX5ACRwCS9SkvdgsOy85Jx/hIoR2JoQOnUUarQokVfwb3mUu 7TxnrxGf5Vjl6ngZ9ruTIrSomhZ8BaCxesJMmnCshcc3e3evcCl0+NFOmxovn3z+N4j2 CPAwUJ7DaLJLtQNCkyp87H0aX2neVKO6/ij20vDdR5ilV7qYNr0u59/yCG73b4iYBlt3 d04quJDHBaeQtWgvNlplJxeyscQ474S1XoCpKmdcbm7gXxxp2kxb7t66V9T172XzR0CR 0PjA== 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=9aljdYkb+Y3yTM3ooLhRosreB0W3nf0TBwCQ0LbT4kE=; b=o1iKvHe2QW1RorGSP580SZyltV5XZgJGxKjCwfQyUQIYymqo5cZPfJjrYeijV8KgQO QOTkqRNJenP3M1YDC17LB4tKGaS00UCsCuZMS5EtJ1frtgwD49xt+R6ZuWqIYk4fep2g gDmDjsfXyoA6/YRr5kIlDiRJGb2jcyBeCEd+QntMxWZ8Jdr4D6zeyzYRozdwZ2clbqGo 3C7G5wgxJAlUJswXZxQg/Tk8MOVr7PehufZUCPiNO3mCczZZ22ERMIUaLdhUceN74S8O QFTJSxPfKBTAntiXNvKxYHrEjW6KrFrSD2DaRpxHazq+Micgp7U7DrV7HKkvOnv83Jq7 kLwg== X-Gm-Message-State: AOAM531lsmgR7Mw3aGvRBihCHYb/rNowYmH3RJ/HrF1o8wqz0knqPSmB OfJc30qPIVgcYkEO+aFUgj2uuLqrWS6+IA== X-Google-Smtp-Source: ABdhPJx0eORXav7zCJr5WiOdb/kPH1zZ+6STq0nhvXyoDAzPK6bT054y4p9y4yvvIhW6m0cYrHg42g== X-Received: by 2002:a37:63d4:: with SMTP id x203mr18089106qkb.105.1612219492868; Mon, 01 Feb 2021 14:44:52 -0800 (PST) Received: from localhost.localdomain ([181.23.89.132]) by smtp.gmail.com with ESMTPSA id e1sm15812720qkd.135.2021.02.01.14.44.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 14:44:52 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 1 Feb 2021 19:44:17 -0300 Message-Id: <20210201224421.1395-7-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 06/10] doc/examples/transcoding: 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/transcoding.c | 48 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/doc/examples/transcoding.c b/doc/examples/transcoding.c index 5aff08c135..6ca3089330 100644 --- a/doc/examples/transcoding.c +++ b/doc/examples/transcoding.c @@ -42,6 +42,7 @@ typedef struct FilteringContext { AVFilterContext *buffersrc_ctx; AVFilterGraph *filter_graph; + AVPacket *enc_pkt; AVFrame *filtered_frame; } FilteringContext; static FilteringContext *filter_ctx; @@ -407,6 +408,10 @@ static int init_filters(void) if (ret) return ret; + filter_ctx[i].enc_pkt = av_packet_alloc(); + if (!filter_ctx[i].enc_pkt) + return AVERROR(ENOMEM); + filter_ctx[i].filtered_frame = av_frame_alloc(); if (!filter_ctx[i].filtered_frame) return AVERROR(ENOMEM); @@ -414,17 +419,17 @@ static int init_filters(void) return 0; } -static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index) +static int encode_write_frame(unsigned int stream_index, int flush) { StreamContext *stream = &stream_ctx[stream_index]; + FilteringContext *filter = &filter_ctx[stream_index]; + AVFrame *filt_frame = flush ? NULL : filter->filtered_frame; + AVPacket *enc_pkt = filter->enc_pkt; int ret; - AVPacket enc_pkt; av_log(NULL, AV_LOG_INFO, "Encoding frame\n"); /* encode filtered frame */ - enc_pkt.data = NULL; - enc_pkt.size = 0; - av_init_packet(&enc_pkt); + av_packet_unref(enc_pkt); ret = avcodec_send_frame(stream->enc_ctx, filt_frame); @@ -432,20 +437,20 @@ static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index) return ret; while (ret >= 0) { - ret = avcodec_receive_packet(stream->enc_ctx, &enc_pkt); + ret = avcodec_receive_packet(stream->enc_ctx, enc_pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) return 0; /* prepare packet for muxing */ - enc_pkt.stream_index = stream_index; - av_packet_rescale_ts(&enc_pkt, + enc_pkt->stream_index = stream_index; + av_packet_rescale_ts(enc_pkt, stream->enc_ctx->time_base, ofmt_ctx->streams[stream_index]->time_base); av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n"); /* mux encoded frame */ - ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt); + ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt); } return ret; @@ -481,7 +486,7 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index) } filter->filtered_frame->pict_type = AV_PICTURE_TYPE_NONE; - ret = encode_write_frame(filter->filtered_frame, stream_index); + ret = encode_write_frame(stream_index, 0); av_frame_unref(filter->filtered_frame); if (ret < 0) break; @@ -497,13 +502,13 @@ static int flush_encoder(unsigned int stream_index) return 0; av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index); - return encode_write_frame(NULL, stream_index); + return encode_write_frame(stream_index, 1); } int main(int argc, char **argv) { int ret; - AVPacket packet = { .data = NULL, .size = 0 }; + AVPacket *packet = NULL; unsigned int stream_index; unsigned int i; @@ -518,12 +523,14 @@ int main(int argc, char **argv) goto end; if ((ret = init_filters()) < 0) goto end; + if (!(packet = av_packet_alloc())) + goto end; /* read all packets */ while (1) { - if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0) + if ((ret = av_read_frame(ifmt_ctx, packet)) < 0) break; - stream_index = packet.stream_index; + stream_index = packet->stream_index; av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n", stream_index); @@ -532,10 +539,10 @@ int main(int argc, char **argv) av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n"); - av_packet_rescale_ts(&packet, + av_packet_rescale_ts(packet, ifmt_ctx->streams[stream_index]->time_base, stream->dec_ctx->time_base); - ret = avcodec_send_packet(stream->dec_ctx, &packet); + ret = avcodec_send_packet(stream->dec_ctx, packet); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Decoding failed\n"); break; @@ -555,15 +562,15 @@ int main(int argc, char **argv) } } else { /* remux this frame without reencoding */ - av_packet_rescale_ts(&packet, + av_packet_rescale_ts(packet, ifmt_ctx->streams[stream_index]->time_base, ofmt_ctx->streams[stream_index]->time_base); - ret = av_interleaved_write_frame(ofmt_ctx, &packet); + ret = av_interleaved_write_frame(ofmt_ctx, packet); if (ret < 0) goto end; } - av_packet_unref(&packet); + av_packet_unref(packet); } /* flush filters and encoders */ @@ -587,13 +594,14 @@ int main(int argc, char **argv) av_write_trailer(ofmt_ctx); end: - av_packet_unref(&packet); + av_packet_free(&packet); for (i = 0; i < ifmt_ctx->nb_streams; i++) { avcodec_free_context(&stream_ctx[i].dec_ctx); if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx) avcodec_free_context(&stream_ctx[i].enc_ctx); if (filter_ctx && filter_ctx[i].filter_graph) { avfilter_graph_free(&filter_ctx[i].filter_graph); + av_packet_free(&filter_ctx[i].enc_pkt); av_frame_free(&filter_ctx[i].filtered_frame); }