From patchwork Thu Sep 29 18:25:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 781 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.140.66 with SMTP id o63csp494714vsd; Thu, 29 Sep 2016 11:26:03 -0700 (PDT) X-Received: by 10.28.4.72 with SMTP id 69mr33455wme.91.1475173563713; Thu, 29 Sep 2016 11:26:03 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id vm5si16024882wjc.40.2016.09.29.11.26.02; Thu, 29 Sep 2016 11:26:03 -0700 (PDT) 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=@googlemail.com; 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=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 23EAE689DED; Thu, 29 Sep 2016 21:25:36 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com [74.125.82.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C4F64689DBE for ; Thu, 29 Sep 2016 21:25:29 +0300 (EEST) Received: by mail-wm0-f65.google.com with SMTP id w72so52128wmf.1 for ; Thu, 29 Sep 2016 11:25:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0prWVf51URxr5GPzvDzN8KctoxuVjc+aw+oclhxMM3s=; b=eVwWVLwKPTB2cI1AvQaT0MLaxBMLcble3oUzM7vhkCJyDo7vwEbmsBWn2Wm2481zvf bxw0bn8jVMa0Ax0EFwCkIQFauF44AU7/DmnkohuVx/78F93a5F6ojAQMxoerYxqcvtyU FCKNoDRvQeDtCKwzdm7BpJ+RF7SETZ3OvijBMVhAxUEHPEYHb6ZxfcHyo0Mze7OeYaBB tX9h5wrwU5V7Uhv9lIF0Vu6ywbNsKCyLPHsxHl2cIiCpObXN+asnSjHFmRhw9dOOfUiz qSFQOwXETZJRFsQf1T83gcqoli3EsaUdY62Ewel1FljVL8817emA2+LtYqUWfCBsu7+R KZ/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0prWVf51URxr5GPzvDzN8KctoxuVjc+aw+oclhxMM3s=; b=Dy5ZrMLvZUSQPCublGox0jaWvNxh3x7MvGl5GMeq/a8frL4o69xuwoH5e2SnFxgUsz dZ4AkjHx9XTIwtoQNI7J2GtHy/LcpPbM+ygcaXRm0bWx0uhF6hTNto4wWzkCGaSBaRIF eNEjZniUJJ6JwngJJvqd/jVrJwLndDes9+0kqqlvHMTgrHXn41HJFnjSo/TScZxdZZe9 u3kXXM6CI18rw33Ekv6hBsk6RoSpQHtyeqj0+rh+171vsEoJL09bajC/LCpcreTKcWmU DyUddJfTeBbzvvD4JW35vv7keAgpsONL95l8EEbYmZU59UD8V7FW7ZOkqxasWvBRs1Z0 hJXQ== X-Gm-Message-State: AA6/9Rnwne2QM6QZcA4ThbIaHE7CHMPhKnJu5emNOsIqp92cu/5RYXy0Ce6LMwddgoDQ7w== X-Received: by 10.28.94.18 with SMTP id s18mr95033wmb.44.1475173541853; Thu, 29 Sep 2016 11:25:41 -0700 (PDT) Received: from localhost.localdomain ([79.240.3.206]) by smtp.googlemail.com with ESMTPSA id ya1sm15547953wjb.23.2016.09.29.11.25.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Sep 2016 11:25:41 -0700 (PDT) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Sep 2016 20:25:19 +0200 Message-Id: <20160929182519.3648-3-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20160929182519.3648-1-nfxjfg@googlemail.com> References: <20160929182519.3648-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH 3/3] ffmpeg: use new encode API 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: wm4 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- ffmpeg.c | 71 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/ffmpeg.c b/ffmpeg.c index e7ac5c9..9de8507 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -830,7 +830,7 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost, { AVCodecContext *enc = ost->enc_ctx; AVPacket pkt; - int got_packet = 0; + int ret; av_init_packet(&pkt); pkt.data = NULL; @@ -854,13 +854,19 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost, enc->time_base.num, enc->time_base.den); } - if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) { - av_log(NULL, AV_LOG_FATAL, "Audio encoding failed (avcodec_encode_audio2)\n"); - exit_program(1); - } - update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); + ret = avcodec_send_frame(enc, frame); + if (ret < 0) + goto error; + + while (1) { + ret = avcodec_receive_packet(enc, &pkt); + if (ret == AVERROR(EAGAIN)) + break; + if (ret < 0) + goto error; + + update_benchmark("encode_audio %d.%d", ost->file_index, ost->index); - if (got_packet) { av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base); if (debug_ts) { @@ -872,6 +878,11 @@ static void do_audio_out(AVFormatContext *s, OutputStream *ost, output_packet(s, &pkt, ost); } + + return; +error: + av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n"); + exit_program(1); } static void do_subtitle_out(AVFormatContext *s, @@ -1139,7 +1150,7 @@ static void do_video_out(AVFormatContext *s, } else #endif { - int got_packet, forced_keyframe = 0; + int forced_keyframe = 0; double pts_time; if (enc->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) && @@ -1206,14 +1217,18 @@ static void do_video_out(AVFormatContext *s, ost->frames_encoded++; - ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet); - update_benchmark("encode_video %d.%d", ost->file_index, ost->index); - if (ret < 0) { - av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); - exit_program(1); - } + ret = avcodec_send_frame(enc, in_picture); + if (ret < 0) + goto error; + + while (1) { + ret = avcodec_receive_packet(enc, &pkt); + update_benchmark("encode_video %d.%d", ost->file_index, ost->index); + if (ret == AVERROR(EAGAIN)) + break; + if (ret < 0) + goto error; - if (got_packet) { if (debug_ts) { av_log(NULL, AV_LOG_INFO, "encoder -> type:video " "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n", @@ -1261,6 +1276,11 @@ static void do_video_out(AVFormatContext *s, av_frame_ref(ost->last_frame, next_picture); else av_frame_free(&ost->last_frame); + + return; +error: + av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n"); + exit_program(1); } static double psnr(double d) @@ -1749,35 +1769,36 @@ static void flush_encoders(void) continue; #endif + if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO) + continue; + + avcodec_send_frame(enc, NULL); + for (;;) { - int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL; - const char *desc; + const char *desc = NULL; switch (enc->codec_type) { case AVMEDIA_TYPE_AUDIO: - encode = avcodec_encode_audio2; desc = "audio"; break; case AVMEDIA_TYPE_VIDEO: - encode = avcodec_encode_video2; desc = "video"; break; default: - stop_encoding = 1; + av_assert0(0); } - if (encode) { + if (1) { AVPacket pkt; int pkt_size; - int got_packet; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; update_benchmark(NULL); - ret = encode(enc, &pkt, NULL, &got_packet); + ret = avcodec_receive_packet(enc, &pkt); update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index); - if (ret < 0) { + if (ret < 0 && ret != AVERROR_EOF) { av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n", desc, av_err2str(ret)); @@ -1786,7 +1807,7 @@ static void flush_encoders(void) if (ost->logfile && enc->stats_out) { fprintf(ost->logfile, "%s", enc->stats_out); } - if (!got_packet) { + if (ret == AVERROR_EOF) { stop_encoding = 1; break; }