From patchwork Mon Jul 12 11:07:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 28895 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:bbc9:0:0:0:0:0 with SMTP id c9csp2812094ybk; Mon, 12 Jul 2021 04:08:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPZyNicp0AwwEUBo03YpkxMD6uVC4NULunkOVTHZw730EDs+lAEok1eacbbx2/D6qPq17W X-Received: by 2002:a05:6402:49a:: with SMTP id k26mr64304259edv.279.1626088126653; Mon, 12 Jul 2021 04:08:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626088126; cv=none; d=google.com; s=arc-20160816; b=HuRo3e3KkSLgyGwkZenpF8VCkjwhyRZjDd3LJzht5JUaJEXV24L13MHepePtWtx+Zu RSy8JXltn5tA0fd/vW2OHWQpxkhSX5/cJa5cGrgsH02Y6LfzPO8yGpOU6djaB1faeldJ xFoYEiZVYhr/IJkclPdTUou6ZYXvw5Xz51T6aDOXFeRV2HDtCZGfNsrZfXKrvDUfCDMU Ood+NVuJnTciiihwJhayxIkzuRu27L7jh/xk9vEKktpQjdZMFW1+OlEJwQwMtzQ218Kf HtxOLdJG8vTFg+1FsQ8bWcCUJqOvsC6pN6u+K1PNmLcCb2ZJJsTCgSYXo6h3VKcjPOS6 AVcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=b++2g2thxZiV/z5ED8x0EG6YIuGJmpfwy8MHe1S0mHc=; b=Cpc3zVXR1HlVCda5Y8YjxWmpPLdl3ncCaLKFPk63PrUhz1caFmAosgSDSfcGD+eWnA DQIqIorWN1h8v0tY9tSABnzhCUT1wF0Wokp3Frq5tIsMMIpOhcS0iccCXmA40GzZVpDR tjW22y/39e49dFvgXOCLUKiCnbvIVb8sTG8BKK0/HKOIVdRc7bSP6kc98EKMM7P3yxYD AWpGw/PYOEi3n8sL0B9T6164Z2aJLfFKCuTgn8SDOwrauwjGqKavOmDlFB1Wq+yglBWM c60iM+mWYLGV8+ArXp16tlirzU16MPweA411XplJQYcgWqZeRtq++eq/VT5tXO3G9xVo PZdA== ARC-Authentication-Results: i=1; mx.google.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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d9si16245049ede.533.2021.07.12.04.08.46; Mon, 12 Jul 2021 04:08:46 -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; 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 4E88768A962; Mon, 12 Jul 2021 14:08:05 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B307C6898E4 for ; Mon, 12 Jul 2021 14:07:55 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 67F9C240692 for ; Mon, 12 Jul 2021 13:07:55 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id sLtf-76LplLG for ; Mon, 12 Jul 2021 13:07:54 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 6C31F240694 for ; Mon, 12 Jul 2021 13:07:52 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 98D9E3A08EC; Mon, 12 Jul 2021 13:07:50 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 12 Jul 2021 13:07:02 +0200 Message-Id: <20210712110709.15532-2-anton@khirnov.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210712110709.15532-1-anton@khirnov.net> References: <20210712110709.15532-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/8] tools/venc_data_dump: factor out demux/decode code X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: QFzQs80LonOn Content-Length: 14247 It can be shared with other simple demux/decode tools. --- tests/ref/fate/source | 1 + tools/Makefile | 2 + tools/decode_simple.c | 157 +++++++++++++++++++++++++++++++++++++++++ tools/decode_simple.h | 53 ++++++++++++++ tools/venc_data_dump.c | 156 +++++++++------------------------------- 5 files changed, 248 insertions(+), 121 deletions(-) create mode 100644 tools/decode_simple.c create mode 100644 tools/decode_simple.h diff --git a/tests/ref/fate/source b/tests/ref/fate/source index c64bc05241..69dcdc4f27 100644 --- a/tests/ref/fate/source +++ b/tests/ref/fate/source @@ -20,5 +20,6 @@ Headers without standard inclusion guards: compat/djgpp/math.h compat/float/float.h compat/float/limits.h +tools/decode_simple.h Use of av_clip() where av_clip_uintp2() could be used: Use of av_clip() where av_clip_intp2() could be used: diff --git a/tools/Makefile b/tools/Makefile index 82baa8eadb..ec260f254e 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -17,6 +17,8 @@ tools/target_dem_fuzzer.o: tools/target_dem_fuzzer.c tools/target_io_dem_fuzzer.o: tools/target_dem_fuzzer.c $(COMPILE_C) -DIO_FLAT=0 +tools/venc_data_dump$(EXESUF): tools/decode_simple.o + OUTDIRS += tools clean:: diff --git a/tools/decode_simple.c b/tools/decode_simple.c new file mode 100644 index 0000000000..b679fd7ce6 --- /dev/null +++ b/tools/decode_simple.c @@ -0,0 +1,157 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* shared code for simple demux/decode tools */ + +#include +#include + +#include "decode_simple.h" + +#include "libavformat/avformat.h" + +#include "libavcodec/avcodec.h" +#include "libavcodec/packet.h" + +#include "libavutil/dict.h" +#include "libavutil/error.h" +#include "libavutil/frame.h" + +static int decode_read(DecodeContext *dc, int flush) +{ + const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN); + int ret = 0; + + while (ret >= 0 && + (dc->max_frames == 0 || dc->decoder->frame_number < dc->max_frames)) { + ret = avcodec_receive_frame(dc->decoder, dc->frame); + if (ret < 0) { + if (ret == AVERROR_EOF) { + int err = dc->process_frame(dc, NULL); + if (err < 0) + return err; + } + + return (ret == ret_done) ? 0 : ret; + } + + ret = dc->process_frame(dc, dc->frame); + av_frame_unref(dc->frame); + if (ret < 0) + return ret; + + if (dc->max_frames && dc->decoder->frame_number == dc->max_frames) + return 1; + } + + return (dc->max_frames == 0 || dc->decoder->frame_number < dc->max_frames) ? 0 : 1; +} + +int ds_run(DecodeContext *dc) +{ + int ret; + + ret = avcodec_open2(dc->decoder, NULL, &dc->decoder_opts); + if (ret < 0) + return ret; + + while (ret >= 0) { + ret = av_read_frame(dc->demuxer, dc->pkt); + if (ret < 0) + goto flush; + if (dc->pkt->stream_index != dc->stream->index) { + av_packet_unref(dc->pkt); + continue; + } + + ret = avcodec_send_packet(dc->decoder, dc->pkt); + if (ret < 0) { + fprintf(stderr, "Error decoding: %d\n", ret); + return ret; + } + av_packet_unref(dc->pkt); + + ret = decode_read(dc, 0); + if (ret < 0) { + fprintf(stderr, "Error decoding: %d\n", ret); + return ret; + } else if (ret > 0) + return 0; + } + +flush: + avcodec_send_packet(dc->decoder, NULL); + ret = decode_read(dc, 1); + if (ret < 0) { + fprintf(stderr, "Error flushing: %d\n", ret); + return ret; + } + + return 0; +} + +void ds_free(DecodeContext *dc) +{ + av_dict_free(&dc->decoder_opts); + + av_frame_free(&dc->frame); + av_packet_free(&dc->pkt); + + avcodec_free_context(&dc->decoder); + avformat_close_input(&dc->demuxer); +} + +int ds_open(DecodeContext *dc, const char *url, int stream_idx) +{ + const AVCodec *codec; + int ret; + + memset(dc, 0, sizeof(*dc)); + + dc->pkt = av_packet_alloc(); + dc->frame = av_frame_alloc(); + if (!dc->pkt || !dc->frame) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = avformat_open_input(&dc->demuxer, url, NULL, NULL); + if (ret < 0) { + fprintf(stderr, "Error opening input file: %d\n", ret); + return ret; + } + + if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams) + return AVERROR(EINVAL); + + dc->stream = dc->demuxer->streams[stream_idx]; + + codec = avcodec_find_decoder(dc->stream->codecpar->codec_id); + if (!codec) + return AVERROR_DECODER_NOT_FOUND; + + dc->decoder = avcodec_alloc_context3(codec); + if (!dc->decoder) + return AVERROR(ENOMEM); + + return 0; + +fail: + ds_free(dc); + return ret; +} diff --git a/tools/decode_simple.h b/tools/decode_simple.h new file mode 100644 index 0000000000..d480489a6c --- /dev/null +++ b/tools/decode_simple.h @@ -0,0 +1,53 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* shared code for simple demux/decode tools */ + +#ifndef DECODE_SIMPLE_H +#define DECODE_SIMPLE_H + +#include "libavformat/avformat.h" + +#include "libavcodec/avcodec.h" +#include "libavcodec/packet.h" + +#include "libavutil/dict.h" +#include "libavutil/frame.h" + + +typedef struct DecodeContext { + AVFormatContext *demuxer; + AVStream *stream; + AVCodecContext *decoder; + + AVPacket *pkt; + AVFrame *frame; + + int (*process_frame)(struct DecodeContext *dc, AVFrame *frame); + void *opaque; + + AVDictionary *decoder_opts; + int max_frames; +} DecodeContext; + +int ds_open(DecodeContext *dc, const char *url, int stream_idx); +void ds_free(DecodeContext *dc); + +int ds_run(DecodeContext *dc); + +#endif /* DECODE_SIMPLE_H */ diff --git a/tools/venc_data_dump.c b/tools/venc_data_dump.c index 3a6ce94268..3a3543f80f 100644 --- a/tools/venc_data_dump.c +++ b/tools/venc_data_dump.c @@ -20,6 +20,8 @@ #include #include +#include "decode_simple.h" + #include "libavutil/common.h" #include "libavutil/dict.h" #include "libavutil/error.h" @@ -29,85 +31,44 @@ #include "libavcodec/avcodec.h" -static int decode_read(AVCodecContext *decoder, AVFrame *frame, int flush, int max_frames) +static int process_frame(DecodeContext *dc, AVFrame *frame) { - const int ret_done = flush ? AVERROR_EOF : AVERROR(EAGAIN); - int ret = 0; - - while (ret >= 0 && - (max_frames == 0 || decoder->frame_number < max_frames)) { - AVFrameSideData *sd; + AVFrameSideData *sd; - ret = avcodec_receive_frame(decoder, frame); - if (ret < 0) - return (ret == ret_done) ? 0 : ret; + if (!frame) + return 0; - fprintf(stdout, "frame %d\n", decoder->frame_number - 1); + fprintf(stdout, "frame %d\n", dc->decoder->frame_number - 1); - sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS); - if (sd) { - AVVideoEncParams *par = (AVVideoEncParams*)sd->data; + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_VIDEO_ENC_PARAMS); + if (sd) { + AVVideoEncParams *par = (AVVideoEncParams*)sd->data; - fprintf(stdout, "AVVideoEncParams %d\n", par->type); - fprintf(stdout, "qp %d\n", par->qp); - for (int i = 0; i < FF_ARRAY_ELEMS(par->delta_qp); i++) - for (int j = 0; j < FF_ARRAY_ELEMS(par->delta_qp[i]); j++) { - if (par->delta_qp[i][j]) - fprintf(stdout, "delta_qp[%d][%d] %"PRId32"\n", i, j, par->delta_qp[i][j]); - } + fprintf(stdout, "AVVideoEncParams %d\n", par->type); + fprintf(stdout, "qp %d\n", par->qp); + for (int i = 0; i < FF_ARRAY_ELEMS(par->delta_qp); i++) + for (int j = 0; j < FF_ARRAY_ELEMS(par->delta_qp[i]); j++) { + if (par->delta_qp[i][j]) + fprintf(stdout, "delta_qp[%d][%d] %"PRId32"\n", i, j, par->delta_qp[i][j]); + } - if (par->nb_blocks) { - fprintf(stdout, "nb_blocks %d\n", par->nb_blocks); - for (int i = 0; i < par->nb_blocks; i++) { - AVVideoBlockParams *b = av_video_enc_params_block(par, i); + if (par->nb_blocks) { + fprintf(stdout, "nb_blocks %d\n", par->nb_blocks); + for (int i = 0; i < par->nb_blocks; i++) { + AVVideoBlockParams *b = av_video_enc_params_block(par, i); - fprintf(stdout, "block %d %d:%d %dx%d %"PRId32"\n", - i, b->src_x, b->src_y, b->w, b->h, b->delta_qp); - } + fprintf(stdout, "block %d %d:%d %dx%d %"PRId32"\n", + i, b->src_x, b->src_y, b->w, b->h, b->delta_qp); } } - - av_frame_unref(frame); - - if (max_frames && decoder->frame_number == max_frames) - return 1; } - return (max_frames == 0 || decoder->frame_number < max_frames) ? 0 : 1; -} - -static int decoder_init(AVFormatContext *demuxer, int stream_idx, - AVCodecContext **dec, AVDictionary **opts) -{ - const AVCodec *codec; - int ret; - - if (stream_idx < 0 || stream_idx >= demuxer->nb_streams) - return AVERROR(EINVAL); - - codec = avcodec_find_decoder(demuxer->streams[stream_idx]->codecpar->codec_id); - if (!codec) - return AVERROR_DECODER_NOT_FOUND; - - *dec = avcodec_alloc_context3(codec); - if (!*dec) - return AVERROR(ENOMEM); - - ret = avcodec_open2(*dec, NULL, opts); - if (ret < 0) - return ret; - return 0; } int main(int argc, char **argv) { - AVFormatContext *demuxer = NULL; - AVCodecContext *decoder = NULL; - AVDictionary *opts = NULL; - - AVPacket *pkt = NULL; - AVFrame *frame = NULL; + DecodeContext dc; unsigned int stream_idx, max_frames; const char *filename, *thread_type = NULL, *nb_threads = NULL; @@ -126,70 +87,23 @@ int main(int argc, char **argv) thread_type = argv[5]; } - ret = av_dict_set(&opts, "threads", nb_threads, 0); - ret |= av_dict_set(&opts, "thread_type", thread_type, 0); - ret |= av_dict_set(&opts, "export_side_data", "venc_params", 0); - - ret = avformat_open_input(&demuxer, filename, NULL, NULL); - if (ret < 0) { - fprintf(stderr, "Error opening input file: %d\n", ret); - return ret; - } - - ret = decoder_init(demuxer, stream_idx, &decoder, &opts); - if (ret < 0) { - fprintf(stderr, "Error initializing decoder\n"); - goto finish; - } - - pkt = av_packet_alloc(); - frame = av_frame_alloc(); - if (!pkt || !frame) { - ret = AVERROR(ENOMEM); + ret = ds_open(&dc, filename, stream_idx); + if (ret < 0) goto finish; - } - while (ret >= 0) { - ret = av_read_frame(demuxer, pkt); - if (ret < 0) - goto flush; - if (pkt->stream_index != stream_idx) { - av_packet_unref(pkt); - continue; - } + dc.process_frame = process_frame; + dc.max_frames = max_frames; - ret = avcodec_send_packet(decoder, pkt); - if (ret < 0) { - fprintf(stderr, "Error decoding: %d\n", ret); - goto finish; - } - av_packet_unref(pkt); - - ret = decode_read(decoder, frame, 0, max_frames); - if (ret < 0) { - fprintf(stderr, "Error decoding: %d\n", ret); - goto finish; - } else if (ret > 0) { - ret = 0; - goto finish; - } - } + ret = av_dict_set(&dc.decoder_opts, "threads", nb_threads, 0); + ret |= av_dict_set(&dc.decoder_opts, "thread_type", thread_type, 0); + ret |= av_dict_set(&dc.decoder_opts, "export_side_data", "venc_params", 0); -flush: - avcodec_send_packet(decoder, NULL); - ret = decode_read(decoder, frame, 1, max_frames); - if (ret < 0) { - fprintf(stderr, "Error flushing: %d\n", ret); + if (ret < 0) goto finish; - } - ret = 0; -finish: - av_dict_free(&opts); - av_packet_free(&pkt); - av_frame_free(&frame); - avcodec_free_context(&decoder); - avformat_close_input(&demuxer); + ret = ds_run(&dc); +finish: + ds_free(&dc); return ret; }