当前位置:   article > 正文

FFmpeg:进行qsv加速转码,以及如何动态更改编码器的选项(附完整源代码)_ffmpeg qsv硬件编码c++

ffmpeg qsv硬件编码c++

FFmpeg:进行qsv加速转码,以及如何动态更改编码器的选项

#include <stdio.h>
#include <errno.h>
#include <libavutil/hwcontext.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>

static AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
static AVBufferRef *hw_device_ctx = NULL;
static AVCodecContext *decoder_ctx = NULL, *encoder_ctx = NULL;
static int video_stream = -1;

typedef struct DynamicSetting {
   
    int frame_number;
    char* optstr;
} DynamicSetting;
static DynamicSetting *dynamic_setting;
static int setting_number;
static int current_setting_number;

static int str_to_dict(char* optstr, AVDictionary **opt)
{
   
    char *key, *value;
    if (strlen(optstr) == 0)
        return 0;
    key = strtok(optstr, " ");
    if (key == NULL)
        return AVERROR(ENAVAIL);
    value = strtok(NULL, " ");
    if (value == NULL)
        return AVERROR(ENAVAIL);
    av_dict_set(opt, key, value, 0);
    do {
   
        key = strtok(NULL, " ");
        if (key == NULL)
            return 0;
        value = strtok(NULL, " ");
        if (value == NULL)
            return AVERROR(ENAVAIL);
        av_dict_set(opt, key, value, 0);
    } while(key != NULL);
    return 0;
}

static int dynamic_set_parameter(AVCodecContext *avctx)
{
   
    AVDictionary *opts = NULL;
    int ret = 0;
    static int frame_number = 0;
    frame_number++;
    if (current_setting_number < setting_number &&
        frame_number == dynamic_setting[current_setting_number].frame_number) {
   
        AVDictionaryEntry *e = NULL;
        ret = str_to_dict(dynamic_setting[current_setting_number].optstr, &opts);
        if (ret < 0) {
   
            fprintf(stderr, "The dynamic parameter is wrong\n");
            goto fail;
        }
        /* Set common option. The dictionary will be freed and replaced
         * by a new one containing all options not found in common option list.
         * Then this new dictionary is used to set private option. */
        if ((ret = av_opt_set_dict(avctx, &opts)) < 0)
            goto fail;
        /* Set codec specific option */
        if ((ret = av_opt_set_dict(avctx->priv_data, &opts)) < 0)
            goto fail;
        /* There is no "framerate" option in commom option list. Use "-r" to set
         * framerate, which is compatible with ffmpeg commandline. The video is
         * assumed to be average frame rate, so set time_base to 1/framerate. */
        e = av_dict_get(opts, "r", NULL, 0);
        if (e) {
   
            avctx->framerate = av_d2q(atof(e->value), INT_MAX);
            encoder_ctx->time_base = av_inv_q(encoder_ctx->framerate);
        }
    }
fail:
    av_dict_free(&opts);
    return ret;
}

static int get_format(AVCodecContext *avctx, const
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/446283
推荐阅读
相关标签
  

闽ICP备14008679号