Perform QSV-accelerated transcoding and show to dynamically change encoder's options.Usage: qsv_transcode input_stream codec output_stream initial option { frame_number new_option } e.g: - qsv_transcode input.mp4 h264_qsv output_h264.mp4 "g 60"
- qsv_transcode input.mp4 hevc_qsv output_hevc.mp4 "g 60 async_depth 1" 100 "g 120" (initialize codec with gop_size 60 and change it to 120 after 100 frames)
#include <stdio.h>
#include <errno.h>
{
if (strlen(optstr) == 0)
return 0;
key = strtok(optstr,
" ");
do {
return 0;
} while(1);
}
{
static int frame_number = 0;
frame_number++;
fprintf(stderr, "The dynamic parameter is wrong\n");
}
if (e) {
}
}
}
{
}
}
fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
}
{
fprintf(stderr, "Cannot open input file '%s', Error code: %s\n",
}
fprintf(stderr, "Cannot find input stream information. Error code: %s\n",
}
fprintf(stderr, "Cannot find a video stream in the input file. "
}
switch(
video->codecpar->codec_id) {
break;
break;
break;
break;
break;
break;
break;
default:
fprintf(stderr, "Codec is not supportted by qsv\n");
}
fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n",
}
fprintf(stderr, "A hardware device reference create failed.\n");
}
fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
}
{
fprintf(stderr, "Failed to set dynamic parameter. Error code: %s\n",
goto end;
}
fprintf(stderr,
"Error during encoding. Error code: %s\n",
av_err2str(
ret));
goto end;
}
while (1) {
break;
fprintf(stderr, "Error during writing data to output file. "
}
}
end:
return 0;
}
{
fprintf(stderr,
"Error during decoding. Error code: %s\n",
av_err2str(
ret));
}
return 0;
fprintf(stderr,
"Error while decoding. Error code: %s\n",
av_err2str(
ret));
}
}
fprintf(stderr, "Failed to set encoding parameter.\n");
}
if (e) {
}
fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
}
fprintf(stderr, "Failed to allocate stream for output format.\n");
}
fprintf(stderr, "Failed to copy the stream parameters. "
}
fprintf(stderr, "Error while writing stream header. "
}
}
fprintf(stderr, "Error during encoding and writing.\n");
}
}
int main(
int argc,
char **argv)
{
if (argc < 5 || (argc - 5) % 2) {
" <\"encoding option set 0\"> [<frame_number> <\"encoding options set 1\">]...\n", argv[0]);
return 1;
}
}
fprintf(stderr,
"Failed to create a QSV device. Error code: %s\n",
av_err2str(
ret));
goto end;
}
if (!dec_pkt) {
fprintf(stderr, "Failed to allocate decode packet\n");
goto end;
}
goto end;
fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
goto end;
}
fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
goto end;
}
goto end;
}
fprintf(stderr, "Cannot open output file. "
goto end;
}
break;
}
if ((
ret =
dec_enc(dec_pkt, enc_codec, argv[4])) < 0) {
fprintf(stderr,
"Failed to flush decoder %s\n",
av_err2str(
ret));
goto end;
}
fprintf(stderr,
"Failed to flush encoder %s\n",
av_err2str(
ret));
goto end;
}
fprintf(stderr,
"Failed to write trailer %s\n",
av_err2str(
ret));
end:
}
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
AVPixelFormat
Pixel format.
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Callback to negotiate the pixel format.
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
AVStream ** streams
A list of all streams in the file.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
static int setting_number
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
static int dec_enc(AVPacket *pkt, const AVCodec *enc_codec, char *optstr)
static const chunk_decoder decoder[8]
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
static AVFormatContext * ofmt_ctx
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
static enum AVPixelFormat pix_fmts[]
#define AVIO_FLAG_WRITE
write-only
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
static int dynamic_set_parameter(AVCodecContext *avctx)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
const AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
AVIOContext * pb
I/O context.
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
static int open_input_file(char *filename)
@ AV_PIX_FMT_QSV
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
static DynamicSetting * dynamic_setting
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
static int current_setting_number
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
int main(int argc, char **argv)
static AVFormatContext * ifmt_ctx
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
static int encode_write(AVPacket *enc_pkt, AVFrame *frame)
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another.
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
#define i(width, name, range_min, range_max)
static AVCodecContext * encoder_ctx
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
Guess the frame rate, based on both the container and codec information.
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
main external API structure.
A Quick Description Of Rate Distortion Theory We want to encode a video
static int str_to_dict(char *optstr, AVDictionary **opt)
A reference to a data buffer.
int avcodec_parameters_from_context(struct AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
This structure stores compressed data.
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file ensuring correct interleaving.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int width
picture width / height.
static AVCodecContext * decoder_ctx
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
static AVBufferRef * hw_device_ctx
const AVCodec * avcodec_find_encoder_by_name(const char *name)
Find a registered encoder with the specified name.