From patchwork Fri Dec 29 01:33:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 7006 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp9926187jad; Thu, 28 Dec 2017 17:33:28 -0800 (PST) X-Google-Smtp-Source: ACJfBovijBca8eZE6VuZ2W/pAendSh6+MOjU8wmY+p1JAnuCf+eogM8YNvTPfvAVMLSCZ2IrOp7q X-Received: by 10.223.202.17 with SMTP id o17mr11560677wrh.250.1514511208587; Thu, 28 Dec 2017 17:33:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514511208; cv=none; d=google.com; s=arc-20160816; b=mQGHGrDvlOUjq7SoP901GpelZj3fvsKvjCAT+jbtNCnrvTWDckCd9MgrUrFdlLGy0H 0850Z/Nh6cm2a8jDOvTDiHPu7yCVxPxvj+72XCE+1Ln/8SNmkH6skl62jSGwZvSryuGf EmyBDHvABEIcre5e2mfuIu1jlZu1dfhGBRlvcGyGb4UrZaCIXpmNqkMEuxsOQlaYqb6I DSPpxaS5pl6o+iIMoczalaW5uVYjGxjlyR0xvTKkFMR2FOZ/m9Ai3I9FZ7we7yuU6xep +OcC/OXqbsbqzfnFrHw0p36RBf6jX4cDTeCZiZN5qXVdhDebxJbtTHyQFEXe7a4wwoMw tETQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=KXfvSoAj2a01jPzFMapGj4723QCmf3QjcC6HjPrmPFY=; b=BBKfq5R+WTGOnBVnb0eaHUmMWnetZxWjvCwYwz148EM1EDwre0lYq3L66dzfAMWdh1 eDB3MzTh3WD8+syDfg84j0lfKeQz1tw+kCtUtr5GndwDcRk6XkdIlrVPnUw4VEiQHz1H iQJNvH+IFCL+H5QeLlGYhS0enA4Aa+DN33Ty2GReOqMIeQCPDCMhdpw3+oUpyoeC4a3u IonZp2dnDsidKKUmRs0M1TPdRBMso75SiX7uovWcP2R3XF9twUsWh+c38BMkdhc7X1FI HFcZLyHb5T+Eq/AtW39huHM8mRdFMjqFeIl8IKONoCLHqGHOYKgluMhoRljW7xP//wPZ SGEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=gzBRXsBr; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p36si19920658wrb.67.2017.12.28.17.33.27; Thu, 28 Dec 2017 17:33:28 -0800 (PST) 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=@tmm1-net.20150623.gappssmtp.com header.s=20150623 header.b=gzBRXsBr; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62245689723; Fri, 29 Dec 2017 03:33:12 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl0-f67.google.com (mail-pl0-f67.google.com [209.85.160.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 42C6C680BE6 for ; Fri, 29 Dec 2017 03:33:06 +0200 (EET) Received: by mail-pl0-f67.google.com with SMTP id bd8so22127344plb.9 for ; Thu, 28 Dec 2017 17:33:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tmm1-net.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=1PKnLuo1cy9Xq3iEm9IA/gxmLVfVQIyPchcz7mFH4as=; b=gzBRXsBrWR9vujCctl3kdlel7ok+JpvNAEhMJowJsAx8XAiqJXyHP4d8sCPYiF8LKE H1VQYokFPTHB8GhyOk3Sd102urlaurBBpEbcqmzxZE/v9XH3B+ZMzqtBI/DXM+PensQm nNiL+Y51qwJwc2o/HczXhoyi8tI7nzJq8UrCTsMG4dqPln811nPetf7pUrjzkpy7rA85 MmgycHFJyhadlh8T4stxBxBaRq5RLq+uEkNTa1y9JjG8bNeVtlL+agWsMeAq/bBEGNLf 6bINXTu4TZYrRvDUiv5l7JY7Q711Cg3zEsj3vJD+cgkhyKFKbutDxXX2kxx1CxldUhkC 9Ybw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=1PKnLuo1cy9Xq3iEm9IA/gxmLVfVQIyPchcz7mFH4as=; b=cinpfwE9ItSa3PJQdfcyhyA4jGjWjkRafus3PNijQ1idEef07rW1Vku7YDCanRPWvP LBBGPa2EN5vN0gIHWPmBbwXVxD0CQWBIedUxSNhQCdDzMb4Y/qeSutAEoNKTf34qBZk0 Z6G5dROfZGNjI+tPXp2A+nzW4sNvPfmsunIbmzoj9J8o6ezEPcowLU0KAuIf5N+0ajkt yn+DL14Q4aJqMPuDrJUuQwA3RCjVsdR4lRN7tvt5F14jO5ZmbxxJPo5ZwUUKTioGrR2c nOAi/RgEmiA/VqiI5dIuLRSg5bA2aBRGqdfJAmX7lIzNQYfHUjEUW6Pc4KnU0aUpl2cs 3qQQ== X-Gm-Message-State: AKGB3mKN04R7F+rl7VwvoXYD0n6DFUzOMHjqawsgSWNyb9L29erheBRS JwvS0u97DYDLdqMQ/VNZ7WgYgO6t X-Received: by 10.84.235.2 with SMTP id o2mr33027976plk.64.1514511198634; Thu, 28 Dec 2017 17:33:18 -0800 (PST) Received: from localhost.localdomain (c-69-181-54-242.hsd1.ca.comcast.net. [69.181.54.242]) by smtp.gmail.com with ESMTPSA id e2sm58780182pgv.34.2017.12.28.17.33.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 28 Dec 2017 17:33:18 -0800 (PST) From: Aman Gupta To: ffmpeg-devel@ffmpeg.org Date: Thu, 28 Dec 2017 17:33:14 -0800 Message-Id: <20171229013314.77974-1-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171229003653.66604-1-ffmpeg@tmm1.net> References: <20171229003653.66604-1-ffmpeg@tmm1.net> Subject: [FFmpeg-devel] [PATCH v2] libavcodec/mediacodecdec: switch to new decoder 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: matthieu.bouron@gmail.com, Aman Gupta MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Aman Gupta Using the new API gives the decoder the ability to produce N frames per input packet. This is particularly useful with mpeg2 decoders on some android devices, which automatically deinterlace video and produce one frame per field. Signed-off-by: Aman Gupta --- libavcodec/mediacodecdec.c | 77 +++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/libavcodec/mediacodecdec.c b/libavcodec/mediacodecdec.c index b698ceaef9..90497c64da 100644 --- a/libavcodec/mediacodecdec.c +++ b/libavcodec/mediacodecdec.c @@ -31,6 +31,7 @@ #include "libavutil/pixfmt.h" #include "avcodec.h" +#include "decode.h" #include "h264_parse.h" #include "hevc_parse.h" #include "hwaccel.h" @@ -424,29 +425,13 @@ static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame, return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt); } -static int mediacodec_decode_frame(AVCodecContext *avctx, void *data, - int *got_frame, AVPacket *avpkt) +static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame) { MediaCodecH264DecContext *s = avctx->priv_data; - AVFrame *frame = data; int ret; - - /* buffer the input packet */ - if (avpkt->size) { - AVPacket input_pkt = { 0 }; - - if (av_fifo_space(s->fifo) < sizeof(input_pkt)) { - ret = av_fifo_realloc2(s->fifo, - av_fifo_size(s->fifo) + sizeof(input_pkt)); - if (ret < 0) - return ret; - } - - ret = av_packet_ref(&input_pkt, avpkt); - if (ret < 0) - return ret; - av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL); - } + int got_frame = 0; + int is_eof = 0; + AVPacket pkt = {0}; /* * MediaCodec.flush() discards both input and output buffers, thus we @@ -470,26 +455,51 @@ static int mediacodec_decode_frame(AVCodecContext *avctx, void *data, */ if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) { if (!ff_mediacodec_dec_flush(avctx, s->ctx)) { - return avpkt->size; + return 0; } + return AVERROR(EAGAIN); + } + + ret = ff_decode_get_packet(avctx, &pkt); + if (ret == AVERROR_EOF) + is_eof = 1; + else if (ret == AVERROR(EAGAIN)) + ; // no input packet, but fallthrough to check for pending frames + else if (ret < 0) + return ret; + + /* buffer the input packet */ + if (pkt.size) { + if (av_fifo_space(s->fifo) < sizeof(pkt)) { + ret = av_fifo_realloc2(s->fifo, + av_fifo_size(s->fifo) + sizeof(pkt)); + if (ret < 0) { + av_packet_unref(&pkt); + return ret; + } + } + av_fifo_generic_write(s->fifo, &pkt, sizeof(pkt), NULL); } /* process buffered data */ - while (!*got_frame) { + while (!got_frame) { /* prepare the input data */ if (s->buffered_pkt.size <= 0) { av_packet_unref(&s->buffered_pkt); /* no more data */ if (av_fifo_size(s->fifo) < sizeof(AVPacket)) { - return avpkt->size ? avpkt->size : - ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt); + AVPacket null_pkt = {0}; + if (is_eof) + return ff_mediacodec_dec_decode(avctx, s->ctx, frame, + &got_frame, &null_pkt); + return AVERROR(EAGAIN); } av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL); } - ret = mediacodec_process_data(avctx, frame, got_frame, &s->buffered_pkt); + ret = mediacodec_process_data(avctx, frame, &got_frame, &s->buffered_pkt); if (ret < 0) return ret; @@ -497,7 +507,10 @@ static int mediacodec_decode_frame(AVCodecContext *avctx, void *data, s->buffered_pkt.data += ret; } - return avpkt->size; + if (got_frame) + return 0; + + return AVERROR(EAGAIN); } static void mediacodec_decode_flush(AVCodecContext *avctx) @@ -537,7 +550,7 @@ AVCodec ff_h264_mediacodec_decoder = { .id = AV_CODEC_ID_H264, .priv_data_size = sizeof(MediaCodecH264DecContext), .init = mediacodec_decode_init, - .decode = mediacodec_decode_frame, + .receive_frame = mediacodec_receive_frame, .flush = mediacodec_decode_flush, .close = mediacodec_decode_close, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, @@ -556,7 +569,7 @@ AVCodec ff_hevc_mediacodec_decoder = { .id = AV_CODEC_ID_HEVC, .priv_data_size = sizeof(MediaCodecH264DecContext), .init = mediacodec_decode_init, - .decode = mediacodec_decode_frame, + .receive_frame = mediacodec_receive_frame, .flush = mediacodec_decode_flush, .close = mediacodec_decode_close, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, @@ -575,7 +588,7 @@ AVCodec ff_mpeg2_mediacodec_decoder = { .id = AV_CODEC_ID_MPEG2VIDEO, .priv_data_size = sizeof(MediaCodecH264DecContext), .init = mediacodec_decode_init, - .decode = mediacodec_decode_frame, + .receive_frame = mediacodec_receive_frame, .flush = mediacodec_decode_flush, .close = mediacodec_decode_close, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, @@ -593,7 +606,7 @@ AVCodec ff_mpeg4_mediacodec_decoder = { .id = AV_CODEC_ID_MPEG4, .priv_data_size = sizeof(MediaCodecH264DecContext), .init = mediacodec_decode_init, - .decode = mediacodec_decode_frame, + .receive_frame = mediacodec_receive_frame, .flush = mediacodec_decode_flush, .close = mediacodec_decode_close, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, @@ -611,7 +624,7 @@ AVCodec ff_vp8_mediacodec_decoder = { .id = AV_CODEC_ID_VP8, .priv_data_size = sizeof(MediaCodecH264DecContext), .init = mediacodec_decode_init, - .decode = mediacodec_decode_frame, + .receive_frame = mediacodec_receive_frame, .flush = mediacodec_decode_flush, .close = mediacodec_decode_close, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, @@ -629,7 +642,7 @@ AVCodec ff_vp9_mediacodec_decoder = { .id = AV_CODEC_ID_VP9, .priv_data_size = sizeof(MediaCodecH264DecContext), .init = mediacodec_decode_init, - .decode = mediacodec_decode_frame, + .receive_frame = mediacodec_receive_frame, .flush = mediacodec_decode_flush, .close = mediacodec_decode_close, .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,