From patchwork Sat May 2 10:10:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhao, Gang" X-Patchwork-Id: 19429 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 7EDED44B5C3 for ; Sat, 2 May 2020 13:17:24 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5623D68C6DC; Sat, 2 May 2020 13:17:24 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9546B68C55F for ; Sat, 2 May 2020 13:17:17 +0300 (EEST) Received: by mail-pg1-f193.google.com with SMTP id s8so5839372pgq.1 for ; Sat, 02 May 2020 03:17:17 -0700 (PDT) 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:in-reply-to :references; bh=kbr7t4kiYwcVXNyfujz6K6JxvyKiJE+9RQqNUAaKjj8=; b=Bcc0b6pxrODXmKSZd4jJQSWbLfWslFgfzrMhofXIjoEswpyeICL9+kt665cpNPgEH1 59wyeY2S1lUkd+og9dFZ1PYZ3lkb1KAZwR+VCOOWLXLZRDOfeQhBa3DqttJIJlBdcjOo 01/aqKlUs9MdqBK4M1n8HcSlA5d96E86RagVORxD0cbWn7gYOLIVdJ4YgZ0sibSGwZIG YInTBbCCNr28/1f1+/UMYEI2RDkIy260WYrqBiL+iyqKsSim9BG0mLlGtrJzwQlWQwTu OkbFksjBcKTPwvPoFvhxejD/ENyZnRFB6sMsahgPfxmxeuDk8+HaLiA7ZHsSffy49fx+ gh3Q== 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:in-reply-to:references; bh=kbr7t4kiYwcVXNyfujz6K6JxvyKiJE+9RQqNUAaKjj8=; b=hE+nl+6JM0RA19NKznxBIb87GUaNCXpFNgIIyKoWeyMXXlfx4LxdzKKGDFQXgy4l5L y44OyCRFSTdw3kQU980ET5JZmLATti41tyTSnBcumekNJqAO4nlAgqX3e3gliSZPu2ZN hTxh6NRTq0pnmmc5F0D9aCO7faCoVgseiq9Sb+A7b/qwb9Q7OXH90mLnTA5ooxEdgXmF I/6stzMZzKZLidsbwX8wgRGGJuffqpFuVVJEjrgv3dPtO8egSz/38Vq7/g5nRmrRgcZv eYB7S3+RAmXXoOqDUlhWYq9sV9afOaTkRmU5G7bcOB6lCm/1gps4McRCxj66FSeAG2m/ c8Kw== X-Gm-Message-State: AGi0Pua7e7+aaLcVH1BYBJYqTEeDYo6QtceZZHiWotSwmtx3OmZlLbJv 7W5dvdfZ0syYzwzZs4Dcfk83IWCOcCE= X-Google-Smtp-Source: APiQypL22iZVQSza4i8I/GWOBCM/wCz/P7cYHmcD/4lTNgYcndKY4eofWil/IuOa0lXSbEE4yMJD7A== X-Received: by 2002:a65:5805:: with SMTP id g5mr8052190pgr.110.1588414234455; Sat, 02 May 2020 03:10:34 -0700 (PDT) Received: from localhost.localdomain ([2600:3c01::f03c:91ff:feee:c20d]) by smtp.gmail.com with ESMTPSA id m1sm1775387pjg.26.2020.05.02.03.10.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 03:10:33 -0700 (PDT) From: "Zhao, Gang" To: Date: Sat, 2 May 2020 18:10:12 +0800 Message-Id: <46108efb9fa7a1ee4161e185f42f3baae536ad11.1588413324.git.gang.zhao.42@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> References: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> In-Reply-To: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> References: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/4] doc/examples/transcoding.c: Fixed a compile warning 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixed deprecated declarations compile warning by replacing deprecated api avcodec_encode_{audio,video}2() and avcodec_decode_{audio4,video2}() with avcodec_send_frame()/avcodec_receive_packet() and avcodec_send_packet()/avcodec_receive_frame(). Also made some clean up according to this change. Signed-off-by: Zhao, Gang --- Tested that the new code generated the same file as before by running ./doc/examples/transcoding_g my_samples/transcoding.mp4 my_samples/transcoding.avi doc/examples/transcoding.c | 106 +++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 51 deletions(-) diff --git doc/examples/transcoding.c doc/examples/transcoding.c index e48837cbd2..8073e5a2e1 100644 --- doc/examples/transcoding.c +++ doc/examples/transcoding.c @@ -402,39 +402,50 @@ static int init_filters(void) return 0; } -static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) { +static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index) { int ret; - int got_frame_local; AVPacket enc_pkt; - int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) = - (ifmt_ctx->streams[stream_index]->codecpar->codec_type == - AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2; - - if (!got_frame) - got_frame = &got_frame_local; 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); - ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt, - filt_frame, got_frame); - av_frame_free(&filt_frame); - if (ret < 0) - return ret; - if (!(*got_frame)) - return 0; - /* prepare packet for muxing */ - enc_pkt.stream_index = stream_index; - av_packet_rescale_ts(&enc_pkt, - stream_ctx[stream_index].enc_ctx->time_base, - ofmt_ctx->streams[stream_index]->time_base); + /* send the frame for encoding */ + ret = avcodec_send_frame(stream_ctx[stream_index].enc_ctx, filt_frame); + if (ret < 0) { + fprintf(stderr, "(%s:%d) Error sending the frame to the encoder: %s\n", + __FUNCTION__, __LINE__, av_err2str(ret)); + exit(1); + } + + /* read all the available output packets (in general there may be any + * number of them */ + while (ret >= 0) { + ret = avcodec_receive_packet(stream_ctx[stream_index].enc_ctx, &enc_pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + /* return OK. Should call avcodec_send_frame again to get more packets */ + ret = 0; + break; + } + else if (ret < 0) { + fprintf(stderr, "Error encoding audio frame\n"); + exit(1); + } + + /* prepare packet for muxing */ + enc_pkt.stream_index = stream_index; + av_packet_rescale_ts(&enc_pkt, + stream_ctx[stream_index].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); + } - av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n"); - /* mux encoded frame */ - ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt); + av_frame_free(&filt_frame); return ret; } @@ -474,7 +485,7 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index) } filt_frame->pict_type = AV_PICTURE_TYPE_NONE; - ret = encode_write_frame(filt_frame, stream_index, NULL); + ret = encode_write_frame(filt_frame, stream_index); if (ret < 0) break; } @@ -484,22 +495,12 @@ static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index) static int flush_encoder(unsigned int stream_index) { - int ret; - int got_frame; - if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities & AV_CODEC_CAP_DELAY)) return 0; - while (1) { - av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index); - ret = encode_write_frame(NULL, stream_index, &got_frame); - if (ret < 0) - break; - if (!got_frame) - return 0; - } - return ret; + av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index); + return encode_write_frame(NULL, stream_index); } int main(int argc, char **argv) @@ -507,17 +508,15 @@ int main(int argc, char **argv) int ret; AVPacket packet = { .data = NULL, .size = 0 }; AVFrame *frame = NULL; - enum AVMediaType type; unsigned int stream_index; unsigned int i; - int got_frame; - int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *); if (argc != 3) { av_log(NULL, AV_LOG_ERROR, "Usage: %s \n", argv[0]); return 1; } + av_log_set_level(AV_LOG_DEBUG); if ((ret = open_input_file(argv[1])) < 0) goto end; if ((ret = open_output_file(argv[2])) < 0) @@ -530,7 +529,6 @@ int main(int argc, char **argv) if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0) break; stream_index = packet.stream_index; - type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type; av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n", stream_index); @@ -544,24 +542,30 @@ int main(int argc, char **argv) av_packet_rescale_ts(&packet, ifmt_ctx->streams[stream_index]->time_base, stream_ctx[stream_index].dec_ctx->time_base); - dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 : - avcodec_decode_audio4; - ret = dec_func(stream_ctx[stream_index].dec_ctx, frame, - &got_frame, &packet); + + /* send the packet for decoding */ + ret = avcodec_send_packet(stream_ctx[stream_index].dec_ctx, &packet); if (ret < 0) { - av_frame_free(&frame); - av_log(NULL, AV_LOG_ERROR, "Decoding failed\n"); - break; + fprintf(stderr, "(%s:%d) Error sending the packet to the encoder: %s\n", + __FUNCTION__, __LINE__, av_err2str(ret)); + exit(1); } - if (got_frame) { + /* read all the available output frames (in general there may be any + * number of them */ + while (ret >= 0) { + ret = avcodec_receive_frame(stream_ctx[stream_index].dec_ctx, frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; + else if (ret < 0) { + fprintf(stderr, "Error encoding audio frame\n"); + exit(1); + } + frame->pts = frame->best_effort_timestamp; ret = filter_encode_write_frame(frame, stream_index); - av_frame_free(&frame); if (ret < 0) goto end; - } else { - av_frame_free(&frame); } } else { /* remux this frame without reencoding */