<HTML><BODY>I tryed to use ffmpeg with qsv, but had some troubles with it. I used an example made by Anton Khirnov. <br>An error occured when it called av_hwframe_transfer_data() and I dont know what to do. There is an output:<br><p>[avi @ 04AE1E20] non-interleaved AVI<br>[AVHWFramesContext @ 0CDA4960] Error synchronizing the operation: -3<br>Error transferring the data to system memory<br>[AVHWFramesContext @ 0CDA4960] Error synchronizing the operation: -2<br>Error transferring the data to system memory<br>Unknown error occurred</p>Could you help me to resolve this issue?<br><br>There is the example src:<br><p>/*<br> * Copyright (c) 2015 Anton Khirnov<br> *<br> * Permission is hereby granted, free of charge, to any person obtaining a copy<br> * of this software and associated documentation files (the "Software"), to deal<br> * in the Software without restriction, including without limitation the rights<br> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell<br> * copies of the Software, and to permit persons to whom the Software is<br> * furnished to do so, subject to the following conditions:<br> *<br> * The above copyright notice and this permission notice shall be included in<br> * all copies or substantial portions of the Software.<br> *<br> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,<br> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN<br> * THE SOFTWARE.<br> */</p><p>/**<br> * @file<br> * Intel QSV-accelerated H.264 decoding example.<br> *<br> * @example qsvdec.c<br> * This example shows how to do QSV-accelerated H.264 decoding with output<br> * frames in the GPU video surfaces.<br> */</p><p>#include "config.h"</p><p>#include <stdio.h></p><p>#include "libavformat/avformat.h"<br>#include "libavformat/avio.h"</p><p>#include "libavcodec/avcodec.h"</p><p>#include "libavutil/buffer.h"<br>#include "libavutil/error.h"<br>#include "libavutil/hwcontext.h"<br>#include "libavutil/hwcontext_qsv.h"<br>#include "libavutil/mem.h"</p><p>typedef struct DecodeContext {<br> AVBufferRef *hw_device_ref;<br>} DecodeContext;</p><p>static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)<br>{<br> while (*pix_fmts != AV_PIX_FMT_NONE) {<br> if (*pix_fmts == AV_PIX_FMT_QSV) {<br> DecodeContext *decode = avctx->opaque;<br> AVHWFramesContext *frames_ctx;<br> AVQSVFramesContext *frames_hwctx;<br> int ret;</p><p>/* create a pool of surfaces to be used by the decoder */<br> avctx->hw_frames_ctx = av_hwframe_ctx_alloc(decode->hw_device_ref);<br> if (!avctx->hw_frames_ctx)<br> return AV_PIX_FMT_NONE;<br> frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;<br> frames_hwctx = frames_ctx->hwctx;</p><p>frames_ctx->format = AV_PIX_FMT_QSV;<br> frames_ctx->sw_format = avctx->sw_pix_fmt;<br> frames_ctx->width = FFALIGN(avctx->coded_width, 32);<br> frames_ctx->height = FFALIGN(avctx->coded_height, 32);<br> frames_ctx->initial_pool_size = 32;</p><p>frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;</p><p>ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);<br> if (ret < 0)<br> return AV_PIX_FMT_NONE;</p><p>return AV_PIX_FMT_QSV;<br> }</p><p>pix_fmts++;<br> }</p><p>fprintf(stderr, "The QSV pixel format not offered in get_format()\n");</p><p>return AV_PIX_FMT_NONE;<br>}</p><p>static int decode_packet(DecodeContext *decode, AVCodecContext *decoder_ctx,<br> AVFrame *frame, AVFrame *sw_frame,<br> AVPacket *pkt, AVIOContext *output_ctx)<br>{<br> int ret = 0;</p><p>ret = avcodec_send_packet(decoder_ctx, pkt);<br> if (ret < 0) {<br> fprintf(stderr, "Error during decoding\n");<br> return ret;<br> }</p><p>while (ret >= 0) {<br> int i, j;</p><p>ret = avcodec_receive_frame(decoder_ctx, frame);<br> if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)<br> break;<br> else if (ret < 0) {<br> fprintf(stderr, "Error during decoding\n");<br> return ret;<br> }</p><p>/* A real program would do something useful with the decoded frame here.<br> * We just retrieve the raw data and write it to a file, which is rather<br> * useless but pedagogic. */<br> ret = av_hwframe_transfer_data(sw_frame, frame, 0);<br> if (ret < 0) {<br> fprintf(stderr, "Error transferring the data to system memory\n");<br> goto fail;<br> }</p><p>for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++)<br> for (j = 0; j < (sw_frame->height >> (i > 0)); j++)<br> avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], sw_frame->width);</p><p>fail:<br> av_frame_unref(sw_frame);<br> av_frame_unref(frame);</p><p>if (ret < 0)<br> return ret;<br> }</p><p>return 0;<br>}</p><p>int main(int argc, char **argv)<br>{<br> AVFormatContext *input_ctx = NULL;<br> AVStream *video_st = NULL;<br> AVCodecContext *decoder_ctx = NULL;<br> const AVCodec *decoder;</p><p>AVPacket pkt = { 0 };<br> AVFrame *frame = NULL, *sw_frame = NULL;</p><p>DecodeContext decode = { NULL };</p><p>AVIOContext *output_ctx = NULL;</p><p>int ret, i;</p><p>av_register_all();</p><p>if (argc < 3) {<br> fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);<br> return 1;<br> }</p><p>/* open the input file */<br> ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);<br> if (ret < 0) {<br> fprintf(stderr, "Cannot open input file '%s': ", argv[1]);<br> goto finish;<br> }</p><p>/* find the first H.264 video stream */<br> for (i = 0; i < input_ctx->nb_streams; i++) {<br> AVStream *st = input_ctx->streams[i];</p><p>if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)<br> video_st = st;<br> else<br> st->discard = AVDISCARD_ALL;<br> }<br> if (!video_st) {<br> fprintf(stderr, "No H.264 video stream in the input file\n");<br> goto finish;<br> }</p><p>/* open the hardware device */<br> ret = av_hwdevice_ctx_create(&decode.hw_device_ref, AV_HWDEVICE_TYPE_QSV,<br> "auto", NULL, 0);<br> if (ret < 0) {<br> fprintf(stderr, "Cannot open the hardware device\n");<br> goto finish;<br> }</p><p>/* initialize the decoder */<br> decoder = avcodec_find_decoder_by_name("h264_qsv");<br> if (!decoder) {<br> fprintf(stderr, "The QSV decoder is not present in libavcodec\n");<br> goto finish;<br> }</p><p>decoder_ctx = avcodec_alloc_context3(decoder);<br> if (!decoder_ctx) {<br> ret = AVERROR(ENOMEM);<br> goto finish;<br> }<br> decoder_ctx->codec_id = AV_CODEC_ID_H264;<br> if (video_st->codecpar->extradata_size) {<br> decoder_ctx->extradata = av_mallocz(video_st->codecpar->extradata_size +<br> AV_INPUT_BUFFER_PADDING_SIZE);<br> if (!decoder_ctx->extradata) {<br> ret = AVERROR(ENOMEM);<br> goto finish;<br> }<br> memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,<br> video_st->codecpar->extradata_size);<br> decoder_ctx->extradata_size = video_st->codecpar->extradata_size;<br> }<br> decoder_ctx->refcounted_frames = 1;</p><p>decoder_ctx->opaque = &decode;<br> decoder_ctx->get_format = get_format;</p><p>ret = avcodec_open2(decoder_ctx, NULL, NULL);<br> if (ret < 0) {<br> fprintf(stderr, "Error opening the decoder: ");<br> goto finish;<br> }</p><p>/* open the output stream */<br> ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);<br> if (ret < 0) {<br> fprintf(stderr, "Error opening the output context: ");<br> goto finish;<br> }</p><p>frame = av_frame_alloc();<br> sw_frame = av_frame_alloc();<br> if (!frame || !sw_frame) {<br> ret = AVERROR(ENOMEM);<br> goto finish;<br> }</p><p>/* actual decoding */<br> while (ret >= 0) {<br> ret = av_read_frame(input_ctx, &pkt);<br> if (ret < 0)<br> break;</p><p>if (pkt.stream_index == video_st->index)<br> ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx);</p><p>av_packet_unref(&pkt);<br> }</p><p>/* flush the decoder */<br> pkt.data = NULL;<br> pkt.size = 0;<br> ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx);</p><p>finish:<br> if (ret < 0) {<br> char buf[1024];<br> av_strerror(ret, buf, sizeof(buf));<br> fprintf(stderr, "%s\n", buf);<br> }</p><p>avformat_close_input(&input_ctx);</p><p>av_frame_free(&frame);<br> av_frame_free(&sw_frame);</p><p>avcodec_free_context(&decoder_ctx);</p><p>av_buffer_unref(&decode.hw_device_ref);</p><p>avio_close(output_ctx);</p><p>return ret;<br>}</p><br><br><br>С уважением,<br>Maxim Malofeev<br>maximmalofeev@bk.ru</BODY></HTML>