4444#include "libavutil/hwcontext_qsv.h"
4545#include "libavutil/mem.h"
4646
47- typedef struct DecodeContext {
48- AVBufferRef * hw_device_ref ;
49- } DecodeContext ;
50-
5147static int get_format (AVCodecContext * avctx , const enum AVPixelFormat * pix_fmts )
5248{
5349 while (* pix_fmts != AV_PIX_FMT_NONE ) {
5450 if (* pix_fmts == AV_PIX_FMT_QSV ) {
55- DecodeContext * decode = avctx -> opaque ;
56- AVHWFramesContext * frames_ctx ;
57- AVQSVFramesContext * frames_hwctx ;
58- int ret ;
59-
60- /* create a pool of surfaces to be used by the decoder */
61- avctx -> hw_frames_ctx = av_hwframe_ctx_alloc (decode -> hw_device_ref );
62- if (!avctx -> hw_frames_ctx )
63- return AV_PIX_FMT_NONE ;
64- frames_ctx = (AVHWFramesContext * )avctx -> hw_frames_ctx -> data ;
65- frames_hwctx = frames_ctx -> hwctx ;
66-
67- frames_ctx -> format = AV_PIX_FMT_QSV ;
68- frames_ctx -> sw_format = avctx -> sw_pix_fmt ;
69- frames_ctx -> width = FFALIGN (avctx -> coded_width , 32 );
70- frames_ctx -> height = FFALIGN (avctx -> coded_height , 32 );
71- frames_ctx -> initial_pool_size = 32 ;
72-
73- frames_hwctx -> frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET ;
74-
75- ret = av_hwframe_ctx_init (avctx -> hw_frames_ctx );
76- if (ret < 0 )
77- return AV_PIX_FMT_NONE ;
78-
7951 return AV_PIX_FMT_QSV ;
8052 }
8153
@@ -87,7 +59,7 @@ static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
8759 return AV_PIX_FMT_NONE ;
8860}
8961
90- static int decode_packet (DecodeContext * decode , AVCodecContext * decoder_ctx ,
62+ static int decode_packet (AVCodecContext * decoder_ctx ,
9163 AVFrame * frame , AVFrame * sw_frame ,
9264 AVPacket * pkt , AVIOContext * output_ctx )
9365{
@@ -144,12 +116,12 @@ int main(int argc, char **argv)
144116 AVPacket pkt = { 0 };
145117 AVFrame * frame = NULL , * sw_frame = NULL ;
146118
147- DecodeContext decode = { NULL };
148-
149119 AVIOContext * output_ctx = NULL ;
150120
151121 int ret , i ;
152122
123+ AVBufferRef * device_ref = NULL ;
124+
153125 if (argc < 3 ) {
154126 fprintf (stderr , "Usage: %s <input file> <output file>\n" , argv [0 ]);
155127 return 1 ;
@@ -177,7 +149,7 @@ int main(int argc, char **argv)
177149 }
178150
179151 /* open the hardware device */
180- ret = av_hwdevice_ctx_create (& decode . hw_device_ref , AV_HWDEVICE_TYPE_QSV ,
152+ ret = av_hwdevice_ctx_create (& device_ref , AV_HWDEVICE_TYPE_QSV ,
181153 "auto" , NULL , 0 );
182154 if (ret < 0 ) {
183155 fprintf (stderr , "Cannot open the hardware device\n" );
@@ -209,7 +181,8 @@ int main(int argc, char **argv)
209181 decoder_ctx -> extradata_size = video_st -> codecpar -> extradata_size ;
210182 }
211183
212- decoder_ctx -> opaque = & decode ;
184+
185+ decoder_ctx -> hw_device_ctx = av_buffer_ref (device_ref );
213186 decoder_ctx -> get_format = get_format ;
214187
215188 ret = avcodec_open2 (decoder_ctx , NULL , NULL );
@@ -239,15 +212,15 @@ int main(int argc, char **argv)
239212 break ;
240213
241214 if (pkt .stream_index == video_st -> index )
242- ret = decode_packet (& decode , decoder_ctx , frame , sw_frame , & pkt , output_ctx );
215+ ret = decode_packet (decoder_ctx , frame , sw_frame , & pkt , output_ctx );
243216
244217 av_packet_unref (& pkt );
245218 }
246219
247220 /* flush the decoder */
248221 pkt .data = NULL ;
249222 pkt .size = 0 ;
250- ret = decode_packet (& decode , decoder_ctx , frame , sw_frame , & pkt , output_ctx );
223+ ret = decode_packet (decoder_ctx , frame , sw_frame , & pkt , output_ctx );
251224
252225finish :
253226 if (ret < 0 ) {
@@ -263,7 +236,7 @@ int main(int argc, char **argv)
263236
264237 avcodec_free_context (& decoder_ctx );
265238
266- av_buffer_unref (& decode . hw_device_ref );
239+ av_buffer_unref (& device_ref );
267240
268241 avio_close (output_ctx );
269242
0 commit comments