diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-06-30 11:44:24 +0200 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-08-08 19:21:39 +0200 |
commit | 94170e041107a3831fdd39ebb53f4f50d06edea4 (patch) | |
tree | fc07ce9308bf2c9f3b91f778b63e42768d29abf4 /tools/venc_data_dump.c | |
parent | 7f660035feabe932f9adb8adeb8991b5a47e5009 (diff) | |
download | ffmpeg-94170e041107a3831fdd39ebb53f4f50d06edea4.tar.gz |
tools/venc_data_dump: factor out demux/decode code
It can be shared with other simple demux/decode tools.
Diffstat (limited to 'tools/venc_data_dump.c')
-rw-r--r-- | tools/venc_data_dump.c | 156 |
1 files changed, 35 insertions, 121 deletions
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 <stdint.h> #include <stdlib.h> +#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; } |