From patchwork Sat May 2 10:10:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhao, Gang" X-Patchwork-Id: 19430 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 4A43844A5B8 for ; Sat, 2 May 2020 13:43:00 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 256EF68C469; Sat, 2 May 2020 13:43:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f68.google.com (mail-io1-f68.google.com [209.85.166.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A84E668C3B1 for ; Sat, 2 May 2020 13:42:52 +0300 (EEST) Received: by mail-io1-f68.google.com with SMTP id y26so7302921ioj.2 for ; Sat, 02 May 2020 03:42:52 -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=+8fUAay5TMgqXeZxKN1dkK4swzilJ/nl+cvEOtSnapg=; b=R9wi21vWBQOjSROhr8GoQcGgvac42T2/UNOwCccXrtET+BAUcDQ25g8n3wkKV+DA7J SSii7/s+4+7vFKA30QUgJdj23ohhJhzvbbU20pODLNBIbMNUorKO4yqFT2iun72CZPal JJkGqtjf4R8cHCWdgvqijW96aHUNU1IqID/e0hivsuxIfKJWZim0BDyxCDfOClYJtsQ+ ogUPbPyYPe27zMl9KzBP2AtZDOq91ZhKWzS0mBl/TUkO8Q4qGVhs6FXxGjb82zT4Fd0a mLVebPEcS2sMJARzu81E1swCHRdl5y8ZB4+hVJzh3KBXBr1V3mgPQMyCUyIl4sM1WcH6 I+0w== 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=+8fUAay5TMgqXeZxKN1dkK4swzilJ/nl+cvEOtSnapg=; b=OZhsTucW1f+Tdy6nklqKiRi2sM+3jQnMhjErn3OoXx5Y7cmZx3XaY13YIeD4JV/H2u C2JyjrF7iUqoT6S0xXVQrTjxbiNws0LBFo4WVyPoOQR0gdNOWSGq3xnP9lmPL+CN5lUP AOIjTNscNjLA8CHvwVjv+lJCRGFpH9houyT7SVZcVJOmQYxXGxBlq2jGhZDM+0mKxHpR ijTqPoiwP52QsSXNoZeQ/hupp2U56pimgG7LzcOsLP4RuciSEfnF95GNGVQXPVSpnZQ9 diT+lONZB7tRtdeA8xo9I3b/FaYCX0E619lLfd+QCvDkkTPmv4WZId22qJQe9EwQR5Sw qFTA== X-Gm-Message-State: AGi0PubXFXeLqEslbp4rgEeaOd5mAILP8YwZSvIGVrtSZkKMWc4LRILB z5bPFetYFE5rAh+iskEpUcG/ZXCdDU4= X-Google-Smtp-Source: APiQypJ2IIwFPtbBEhB8N5HsltPKXAQlGLVDEdzj6Gelrcaq0OOgygfpCYK6i787b/DJweiqeF5meg== X-Received: by 2002:a63:5020:: with SMTP id e32mr5116757pgb.277.1588414229561; Sat, 02 May 2020 03:10:29 -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.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 03:10:28 -0700 (PDT) From: "Zhao, Gang" To: Date: Sat, 2 May 2020 18:10:10 +0800 Message-Id: <4a8148cffdcf122e3b7e0b9ef5308a6db3cc1334.1588413324.git.gang.zhao.42@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/4] doc/examples/demuxing_decoding: 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" Replaced deprecated api avcodec_decode_{audio4,video2}() with 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 files as before by running ./doc/examples/demuxing_decoding_g my_samples/demuxing_decoding.mp4 my_samples/demuxing_decoding.video my_samples/demuxing_decoding.audio doc/examples/demuxing_decoding.c | 102 ++++++++++++------------------- 1 file changed, 38 insertions(+), 64 deletions(-) diff --git doc/examples/demuxing_decoding.c doc/examples/demuxing_decoding.c index 69a31a8935..b2db3f1afa 100644 --- doc/examples/demuxing_decoding.c +++ doc/examples/demuxing_decoding.c @@ -55,29 +55,34 @@ static AVPacket pkt; static int video_frame_count = 0; static int audio_frame_count = 0; -/* Enable or disable frame reference counting. You are not supposed to support - * both paths in your application but pick the one most appropriate to your - * needs. Look for the use of refcount in this example to see what are the - * differences of API usage between them. */ -static int refcount = 0; - -static int decode_packet(int *got_frame, int cached) +static int decode_packet(int cached) { - int ret = 0; - int decoded = pkt.size; + int ret; + AVCodecContext *ctx; - *got_frame = 0; + if (pkt.stream_index == video_stream_idx) + ctx = video_dec_ctx; + else if (pkt.stream_index == audio_stream_idx) + ctx = audio_dec_ctx; - if (pkt.stream_index == video_stream_idx) { - /* decode video frame */ - ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt); - if (ret < 0) { - fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret)); - return ret; - } + /* send the packet with the compressed data to the decoder */ + ret = avcodec_send_packet(ctx, &pkt); + if (ret < 0) { + fprintf(stderr, "Error submitting the packet to the decoder\n"); + exit(1); + } - if (*got_frame) { + /* read all the output frames (in general there may be any number of them */ + while (ret >= 0) { + ret = avcodec_receive_frame(ctx, frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + return 0; + else if (ret < 0) { + fprintf(stderr, "Error during decoding\n"); + exit(1); + } + if (pkt.stream_index == video_stream_idx) { if (frame->width != width || frame->height != height || frame->format != pix_fmt) { /* To handle this change, one could call av_image_alloc again and @@ -105,21 +110,7 @@ static int decode_packet(int *got_frame, int cached) /* write to rawvideo file */ fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file); - } - } else if (pkt.stream_index == audio_stream_idx) { - /* decode audio frame */ - ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt); - if (ret < 0) { - fprintf(stderr, "Error decoding audio frame (%s)\n", av_err2str(ret)); - return ret; - } - /* Some audio decoders decode only part of the packet, and have to be - * called again with the remainder of the packet data. - * Sample: fate-suite/lossless-audio/luckynight-partial.shn - * Also, some decoders might over-read the packet. */ - decoded = FFMIN(ret, pkt.size); - - if (*got_frame) { + } else if (pkt.stream_index == audio_stream_idx) { size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(frame->format); printf("audio_frame%s n:%d nb_samples:%d pts:%s\n", cached ? "(cached)" : "", @@ -138,12 +129,7 @@ static int decode_packet(int *got_frame, int cached) } } - /* If we use frame reference counting, we own the data and need - * to de-reference it when we don't use it anymore */ - if (*got_frame && refcount) - av_frame_unref(frame); - - return decoded; + return ret; } static int open_codec_context(int *stream_idx, @@ -186,8 +172,6 @@ static int open_codec_context(int *stream_idx, return ret; } - /* Init the decoders, with or without reference counting */ - av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0); if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) { fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(type)); @@ -230,24 +214,18 @@ static int get_format_from_sample_fmt(const char **fmt, int main (int argc, char **argv) { - int ret = 0, got_frame; + int ret = 0; if (argc != 4 && argc != 5) { - fprintf(stderr, "usage: %s [-refcount] input_file video_output_file audio_output_file\n" + fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n" "API example program to show how to read frames from an input file.\n" "This program reads frames from a file, decodes them, and writes decoded\n" "video frames to a rawvideo file named video_output_file, and decoded\n" - "audio frames to a rawaudio file named audio_output_file.\n\n" - "If the -refcount option is specified, the program use the\n" - "reference counting frame system which allows keeping a copy of\n" - "the data for longer than one decode call.\n" - "\n", argv[0]); + "audio frames to a rawaudio file named audio_output_file.\n", + argv[0]); exit(1); } - if (argc == 5 && !strcmp(argv[1], "-refcount")) { - refcount = 1; - argv++; - } + src_filename = argv[1]; video_dst_filename = argv[2]; audio_dst_filename = argv[3]; @@ -325,23 +303,19 @@ int main (int argc, char **argv) /* read frames from the file */ while (av_read_frame(fmt_ctx, &pkt) >= 0) { - AVPacket orig_pkt = pkt; - do { - ret = decode_packet(&got_frame, 0); - if (ret < 0) - break; - pkt.data += ret; - pkt.size -= ret; - } while (pkt.size > 0); - av_packet_unref(&orig_pkt); + ret = decode_packet(0); + if (ret < 0) + break; } /* flush cached frames */ pkt.data = NULL; pkt.size = 0; - do { - decode_packet(&got_frame, 1); - } while (got_frame); + ret = decode_packet(1); + if (ret < 0) { + printf("Flushing cached frames failed\n"); + goto end; + } printf("Demuxing succeeded.\n"); From patchwork Sat May 2 10:10:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhao, Gang" X-Patchwork-Id: 19427 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 8C7DA449CF4 for ; Sat, 2 May 2020 13:10:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 630C268C506; Sat, 2 May 2020 13:10:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f66.google.com (mail-pj1-f66.google.com [209.85.216.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 81B0868C506 for ; Sat, 2 May 2020 13:10:34 +0300 (EEST) Received: by mail-pj1-f66.google.com with SMTP id e6so1126640pjt.4 for ; Sat, 02 May 2020 03:10:34 -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=3TxpNw35Z4gkmeiAykN8Gh5gnNSaHhMcs6iDgY3O3LI=; b=bWXrdskumc9f9F5x6FIwqX6E63oLg1WbOTGNChqeM0Uhg+nyfuP7wbaKjvXgqRrUnt zmcW8dh1//ONYg8JeUvb2Oy3eyTp+u8A5KN/olfQkaDz1lSTt3do5kNoo1iVfmJIT4nJ wiUV30eD/9OSLJXmP4qw1/hArNXBVD7Pbvvc4WoE9hyyXqfgvYyzvdnDEGxbnH0Co/tx LMZlnOlwTZjvf2p/AFCUBco9Zban+vcmYoxqmUcMuHP4PCzjdafmn4U4W/wbBz/404uU HUoyh5i9M0ODBpE1f3J6SNotvLCRdPNmShY3MMXH0WdHCPHT/+aYpmGwhVp/nkC7QdeP 1F+g== 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=3TxpNw35Z4gkmeiAykN8Gh5gnNSaHhMcs6iDgY3O3LI=; b=bmVwP2Jfu8AEDR/P0Wp8yZWgNqE75NSAMHMkFh6IpkDUDCD6zqj6DNO/Hy0TxI5nKc raze9Eq8TGSKXT7W5Zgl5my1o95HzWmXjE0K5afTe9ReppMuClPCebr3uDdmQV9OMPpX ozvLAmQhWRoHqrlkz5LsDcCMSbw1Tjo6h5ApUZhKnx+02j5+qLMiMWaXMZw6+Kn6bQx7 DnaX0IPuV8DgPsgIpoOODkHdxYJ9P5pFJc8yqqtuA2tzt16thyeeTbUuIP4xc5THHDJk B+z4e7Kbb8is6rGZeqMx5e5mbP+z38Mfwxq9SF+xdX54DBmiArV40gJEj8QfLgjMB0ma Woww== X-Gm-Message-State: AGi0PuYu3omRAWYTHUNJR4U8W7mGAYZBii2EoPk36EI90jWSRLARX8NB +AtHkG3+TlvfU0o0bIUl71ruZvybQVc= X-Google-Smtp-Source: APiQypIQ3WrPcK6zS35OHVMfG3kczFy4YQT/zsyVRyNa31TdMrg+wUTjn1VXhThWlM2taLRhP2l3Ag== X-Received: by 2002:a17:902:b682:: with SMTP id c2mr8838484pls.144.1588414231990; Sat, 02 May 2020 03:10:31 -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.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 03:10:31 -0700 (PDT) From: "Zhao, Gang" To: Date: Sat, 2 May 2020 18:10:11 +0800 Message-Id: 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 2/4] doc/examples/muxing.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 deprecation declarations compile warning by replacing deprecated api avcodec_encode_{audio,video}2() with avcodec_send_frame()/avcodec_receive_packet(). 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/muxing_g my_samples/muxing.mp4 doc/examples/muxing.c | 104 +++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 67 deletions(-) diff --git doc/examples/muxing.c doc/examples/muxing.c index 9af9aae483..7e781c0783 100644 --- doc/examples/muxing.c +++ doc/examples/muxing.c @@ -280,8 +280,10 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A static AVFrame *get_audio_frame(OutputStream *ost) { AVFrame *frame = ost->tmp_frame; - int j, i, v; + int j, i, v, ret; int16_t *q = (int16_t*)frame->data[0]; + int dst_nb_samples; + AVCodecContext *c = ost->enc; /* check if we want to generate more frames */ if (av_compare_ts(ost->next_pts, ost->enc->time_base, @@ -299,27 +301,6 @@ static AVFrame *get_audio_frame(OutputStream *ost) frame->pts = ost->next_pts; ost->next_pts += frame->nb_samples; - return frame; -} - -/* - * encode one audio frame and send it to the muxer - * return 1 when encoding is finished, 0 otherwise - */ -static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) -{ - AVCodecContext *c; - AVPacket pkt = { 0 }; // data and size must be 0; - AVFrame *frame; - int ret; - int got_packet; - int dst_nb_samples; - - av_init_packet(&pkt); - c = ost->enc; - - frame = get_audio_frame(ost); - if (frame) { /* convert samples from native format to destination codec format, using the resampler */ /* compute destination number of samples */ @@ -349,13 +330,40 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) ost->samples_count += dst_nb_samples; } - ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet); + return frame; +} + +/* + * encode one frame and send it to the muxer + * return 1 when encoding is finished, 0 otherwise + */ +static int muxing_write_frame(AVFormatContext *oc, OutputStream *ost, AVFrame *frame) +{ + AVCodecContext *c; + AVPacket pkt = { 0 }; // data and size must be 0; + int ret; + + av_init_packet(&pkt); + c = ost->enc; + + /* send the frame for encoding */ + ret = avcodec_send_frame(c, frame); if (ret < 0) { - fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret)); + fprintf(stderr, "Error sending the frame to the encoder: %s\n", av_err2str(ret)); exit(1); } - if (got_packet) { + /* read all the available output packets (in general there may be any + * number of them */ + while (ret >= 0) { + ret = avcodec_receive_packet(c, &pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + goto exit; + else if (ret < 0) { + fprintf(stderr, "Error encoding audio frame\n"); + exit(1); + } + ret = write_frame(oc, &c->time_base, ost->st, &pkt); if (ret < 0) { fprintf(stderr, "Error while writing audio frame: %s\n", @@ -364,7 +372,8 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) } } - return (frame || got_packet) ? 0 : 1; +exit: + return frame ? 0 : 1; } /**************************************************************/ @@ -500,45 +509,6 @@ static AVFrame *get_video_frame(OutputStream *ost) return ost->frame; } -/* - * encode one video frame and send it to the muxer - * return 1 when encoding is finished, 0 otherwise - */ -static int write_video_frame(AVFormatContext *oc, OutputStream *ost) -{ - int ret; - AVCodecContext *c; - AVFrame *frame; - int got_packet = 0; - AVPacket pkt = { 0 }; - - c = ost->enc; - - frame = get_video_frame(ost); - - av_init_packet(&pkt); - - /* encode the image */ - ret = avcodec_encode_video2(c, &pkt, frame, &got_packet); - if (ret < 0) { - fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret)); - exit(1); - } - - if (got_packet) { - ret = write_frame(oc, &c->time_base, ost->st, &pkt); - } else { - ret = 0; - } - - if (ret < 0) { - fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret)); - exit(1); - } - - return (frame || got_packet) ? 0 : 1; -} - static void close_stream(AVFormatContext *oc, OutputStream *ost) { avcodec_free_context(&ost->enc); @@ -638,9 +608,9 @@ int main(int argc, char **argv) if (encode_video && (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base, audio_st.next_pts, audio_st.enc->time_base) <= 0)) { - encode_video = !write_video_frame(oc, &video_st); + encode_video = !muxing_write_frame(oc, &video_st, get_video_frame(&video_st)); } else { - encode_audio = !write_audio_frame(oc, &audio_st); + encode_audio = !muxing_write_frame(oc, &audio_st, get_audio_frame(&audio_st)); } } 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 */ From patchwork Sat May 2 10:10:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhao, Gang" X-Patchwork-Id: 19428 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 A2D29449CF4 for ; Sat, 2 May 2020 13:10:45 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8772568C7BE; Sat, 2 May 2020 13:10:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0931468C7AA for ; Sat, 2 May 2020 13:10:39 +0300 (EEST) Received: by mail-pj1-f52.google.com with SMTP id fu13so1127475pjb.5 for ; Sat, 02 May 2020 03:10:38 -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=hmsjHOM9fOd4H6hV3x9qR9iEikLlDGXt+UtPHCgpvVg=; b=WLdM8sTv6p9VyMTZCrK1tPays5fLOMTE8kZTLugLgSXULv9HzJ4+6PFPv/ExARuf7L HZvgTz4tZlS1DEMG4ZeqU2yOVpdxLn2VGfMcKqM6+bn4Kf5y0ExmBetoCeJi/TKRXBFU FwHNvt7GHUcz29YSeNC3B3u+4WUbiGC1wMhU/wmw64RITotR5pK6hnZuHUGv1t5pK2HQ XXjdMkKA21DwiE/n9D88tNz6sGV0LkSA5PEkdN3gzi/uplgbXGT3T6lmP4ZnGa1Ugaws 1AvcBDY06UCsgAdWiCq4a0q8KbkQY+Mf+tx+DCburgVXbpuMYxKzPQPnha2HHI0ACiLN 1diw== 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=hmsjHOM9fOd4H6hV3x9qR9iEikLlDGXt+UtPHCgpvVg=; b=foCgY7eBH8EwVsVQZg3LEq2NhH5xYfvAjI//GqsXij6M9XCuzAlWbmyi/cB3EUAwa8 QRcLkCTB4yH4p9fFQadFkTSpi6j5JrSlZ4R1ARb9dC65DoDEUiqnUIXsfzSTVCLL8A5q M75jkxFvrzzgbU9coouizOXdnzmFJAJKqSMKURVAZzZIJkulrkH9h+HPsGU+yRxDDKkC iTqt/T/fp4L0NMBwt9lKe/uuBaXagcmEx8g+m6YLn0g/spWfI53R6lDMBONCuXAAJyo2 McCPd6ovhMvMz8s4KUJznkqQV22j4qmrrGbmyhZAl3myVY64JKAD0g6k2d02owG6rJuI TjOw== X-Gm-Message-State: AGi0PuYmMQi5tFIgtJ/x+evVota7XpatL5hr+MWEfKK7jc+YIHOfaHiA la423InUq5sc+Iy0QyFe/Q4APagY0es= X-Google-Smtp-Source: APiQypJ7D6gUGWQYYF5ygzkQJE9h0dbCQrdMEFMnIODSAfNkiIF43oLKPPjnEXwRylTJ68SRGhowyg== X-Received: by 2002:a17:902:aa94:: with SMTP id d20mr8271341plr.15.1588414236889; Sat, 02 May 2020 03:10:36 -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.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 03:10:36 -0700 (PDT) From: "Zhao, Gang" To: Date: Sat, 2 May 2020 18:10:13 +0800 Message-Id: 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 4/4] avcodec/encode: avcodec/frame_thread_encoder: 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" Disable deprecation declarations compile warning when we really need to call these deprecated functions. Signed-off-by: Zhao, Gang --- libavcodec/encode.c | 4 ++++ libavcodec/frame_thread_encoder.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git libavcodec/encode.c libavcodec/encode.c index 9ed2cf0f59..f787aceaf8 100644 --- libavcodec/encode.c +++ libavcodec/encode.c @@ -368,11 +368,15 @@ static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packe avctx->internal->buffer_pkt_valid = 0; if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { +#if FF_API_PKT_PTS +FF_DISABLE_DEPRECATION_WARNINGS ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt, frame, got_packet); } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) { ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt, frame, got_packet); +FF_ENABLE_DEPRECATION_WARNINGS +#endif } else { ret = AVERROR(EINVAL); } diff --git libavcodec/frame_thread_encoder.c libavcodec/frame_thread_encoder.c index 949bc69f81..cb04e1fe61 100644 --- libavcodec/frame_thread_encoder.c +++ libavcodec/frame_thread_encoder.c @@ -86,7 +86,11 @@ static void * attribute_align_arg worker(void *v){ pthread_mutex_unlock(&c->task_fifo_mutex); frame = task.indata; +#if FF_API_PKT_PTS +FF_DISABLE_DEPRECATION_WARNINGS ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet); +FF_ENABLE_DEPRECATION_WARNINGS +#endif pthread_mutex_lock(&c->buffer_mutex); av_frame_unref(frame); pthread_mutex_unlock(&c->buffer_mutex);