mpegvideo_enc: enable rtp_mode when multiple slices are used
[libav.git] / libavcodec / qsvenc.c
CommitLineData
72b7441a
AK
1/*
2 * Intel MediaSDK QSV encoder utility functions
3 *
4 * copyright (c) 2013 Yukinori Yamazoe
5 * copyright (c) 2015 Anton Khirnov
6 *
7 * This file is part of Libav.
8 *
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <string.h>
25#include <sys/types.h>
26#include <mfx/mfxvideo.h>
27
28#include "libavutil/common.h"
29#include "libavutil/mem.h"
30#include "libavutil/log.h"
31#include "libavutil/time.h"
32#include "libavutil/imgutils.h"
33
34#include "avcodec.h"
35#include "internal.h"
36#include "qsv.h"
37#include "qsv_internal.h"
38#include "qsvenc.h"
39
40static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
41{
42 const char *ratecontrol_desc;
43
44 float quant;
45 int ret;
46
47 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
48 if (ret < 0)
49 return AVERROR_BUG;
50 q->param.mfx.CodecId = ret;
51
66acb76b
AK
52 q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
53
72b7441a
AK
54 if (avctx->level > 0)
55 q->param.mfx.CodecLevel = avctx->level;
56
57 q->param.mfx.CodecProfile = q->profile;
58 q->param.mfx.TargetUsage = q->preset;
59 q->param.mfx.GopPicSize = FFMAX(0, avctx->gop_size);
60 q->param.mfx.GopRefDist = FFMAX(-1, avctx->max_b_frames) + 1;
7c6eb0a1 61 q->param.mfx.GopOptFlag = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ?
72b7441a
AK
62 MFX_GOP_CLOSED : 0;
63 q->param.mfx.IdrInterval = q->idr_interval;
64 q->param.mfx.NumSlice = avctx->slices;
65 q->param.mfx.NumRefFrame = FFMAX(0, avctx->refs);
66 q->param.mfx.EncodedOrder = 0;
67 q->param.mfx.BufferSizeInKB = 0;
68
69 q->param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
66acb76b 70 q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align);
72b7441a
AK
71 q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 32);
72 q->param.mfx.FrameInfo.CropX = 0;
73 q->param.mfx.FrameInfo.CropY = 0;
74 q->param.mfx.FrameInfo.CropW = avctx->width;
75 q->param.mfx.FrameInfo.CropH = avctx->height;
76 q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num;
77 q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den;
78 q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
79 q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
80 q->param.mfx.FrameInfo.BitDepthLuma = 8;
81 q->param.mfx.FrameInfo.BitDepthChroma = 8;
82
83 if (avctx->framerate.den > 0 && avctx->framerate.num > 0) {
84 q->param.mfx.FrameInfo.FrameRateExtN = avctx->framerate.num;
85 q->param.mfx.FrameInfo.FrameRateExtD = avctx->framerate.den;
86 } else {
87 q->param.mfx.FrameInfo.FrameRateExtN = avctx->time_base.den;
88 q->param.mfx.FrameInfo.FrameRateExtD = avctx->time_base.num;
89 }
90
7c6eb0a1 91 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
72b7441a
AK
92 q->param.mfx.RateControlMethod = MFX_RATECONTROL_CQP;
93 ratecontrol_desc = "constant quantization parameter (CQP)";
94 } else if (avctx->rc_max_rate == avctx->bit_rate) {
95 q->param.mfx.RateControlMethod = MFX_RATECONTROL_CBR;
96 ratecontrol_desc = "constant bitrate (CBR)";
97 } else if (!avctx->rc_max_rate) {
98 q->param.mfx.RateControlMethod = MFX_RATECONTROL_AVBR;
99 ratecontrol_desc = "average variable bitrate (AVBR)";
100 } else {
101 q->param.mfx.RateControlMethod = MFX_RATECONTROL_VBR;
102 ratecontrol_desc = "variable bitrate (VBR)";
103 }
104
105 av_log(avctx, AV_LOG_VERBOSE, "Using the %s ratecontrol method\n", ratecontrol_desc);
106
107 switch (q->param.mfx.RateControlMethod) {
108 case MFX_RATECONTROL_CBR:
109 case MFX_RATECONTROL_VBR:
110 q->param.mfx.InitialDelayInKB = avctx->rc_initial_buffer_occupancy / 1000;
111 q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
112 q->param.mfx.MaxKbps = avctx->bit_rate / 1000;
113 break;
114 case MFX_RATECONTROL_CQP:
115 quant = avctx->global_quality / FF_QP2LAMBDA;
116
117 q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51);
118 q->param.mfx.QPP = av_clip(quant, 0, 51);
119 q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51);
120
121 break;
122 case MFX_RATECONTROL_AVBR:
123 q->param.mfx.TargetKbps = avctx->bit_rate / 1000;
124 q->param.mfx.Convergence = q->avbr_convergence;
125 q->param.mfx.Accuracy = q->avbr_accuracy;
126 break;
127 }
128
66acb76b
AK
129 // the HEVC encoder plugin currently fails if coding options
130 // are provided
131 if (avctx->codec_id != AV_CODEC_ID_HEVC) {
132 q->extco.Header.BufferId = MFX_EXTBUFF_CODING_OPTION;
133 q->extco.Header.BufferSz = sizeof(q->extco);
134 q->extco.CAVLC = avctx->coder_type == FF_CODER_TYPE_VLC ?
135 MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
72b7441a 136
dc923bc2 137 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->extco;
66acb76b 138 }
72b7441a
AK
139
140 return 0;
141}
142
143static int qsv_retrieve_enc_params(AVCodecContext *avctx, QSVEncContext *q)
144{
145 uint8_t sps_buf[128];
146 uint8_t pps_buf[128];
147
148 mfxExtCodingOptionSPSPPS extradata = {
149 .Header.BufferId = MFX_EXTBUFF_CODING_OPTION_SPSPPS,
150 .Header.BufferSz = sizeof(extradata),
151 .SPSBuffer = sps_buf, .SPSBufSize = sizeof(sps_buf),
152 .PPSBuffer = pps_buf, .PPSBufSize = sizeof(pps_buf)
153 };
154
155 mfxExtBuffer *ext_buffers[] = {
156 (mfxExtBuffer*)&extradata,
157 };
158
3a85397e 159 int need_pps = avctx->codec_id != AV_CODEC_ID_MPEG2VIDEO;
72b7441a
AK
160 int ret;
161
162 q->param.ExtParam = ext_buffers;
163 q->param.NumExtParam = FF_ARRAY_ELEMS(ext_buffers);
164
165 ret = MFXVideoENCODE_GetVideoParam(q->session, &q->param);
166 if (ret < 0)
167 return ff_qsv_error(ret);
168
169 q->packet_size = q->param.mfx.BufferSizeInKB * 1000;
170
3a85397e 171 if (!extradata.SPSBufSize || (need_pps && !extradata.PPSBufSize)) {
72b7441a
AK
172 av_log(avctx, AV_LOG_ERROR, "No extradata returned from libmfx.\n");
173 return AVERROR_UNKNOWN;
174 }
175
3a85397e 176 avctx->extradata = av_malloc(extradata.SPSBufSize + need_pps * extradata.PPSBufSize +
059a9348 177 AV_INPUT_BUFFER_PADDING_SIZE);
72b7441a
AK
178 if (!avctx->extradata)
179 return AVERROR(ENOMEM);
180
181 memcpy(avctx->extradata, sps_buf, extradata.SPSBufSize);
3a85397e
AK
182 if (need_pps)
183 memcpy(avctx->extradata + extradata.SPSBufSize, pps_buf, extradata.PPSBufSize);
184 avctx->extradata_size = extradata.SPSBufSize + need_pps * extradata.PPSBufSize;
059a9348 185 memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
72b7441a
AK
186
187 return 0;
188}
189
dc923bc2
AK
190static int qsv_init_opaque_alloc(AVCodecContext *avctx, QSVEncContext *q)
191{
192 AVQSVContext *qsv = avctx->hwaccel_context;
193 mfxFrameSurface1 *surfaces;
194 int nb_surfaces, i;
195
196 nb_surfaces = qsv->nb_opaque_surfaces + q->req.NumFrameSuggested + q->async_depth;
197
198 q->opaque_alloc_buf = av_buffer_allocz(sizeof(*surfaces) * nb_surfaces);
199 if (!q->opaque_alloc_buf)
200 return AVERROR(ENOMEM);
201
202 q->opaque_surfaces = av_malloc_array(nb_surfaces, sizeof(*q->opaque_surfaces));
203 if (!q->opaque_surfaces)
204 return AVERROR(ENOMEM);
205
206 surfaces = (mfxFrameSurface1*)q->opaque_alloc_buf->data;
207 for (i = 0; i < nb_surfaces; i++) {
208 surfaces[i].Info = q->req.Info;
209 q->opaque_surfaces[i] = surfaces + i;
210 }
211
212 q->opaque_alloc.Header.BufferId = MFX_EXTBUFF_OPAQUE_SURFACE_ALLOCATION;
213 q->opaque_alloc.Header.BufferSz = sizeof(q->opaque_alloc);
214 q->opaque_alloc.In.Surfaces = q->opaque_surfaces;
215 q->opaque_alloc.In.NumSurface = nb_surfaces;
216 q->opaque_alloc.In.Type = q->req.Type;
217
218 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer *)&q->opaque_alloc;
219
220 qsv->nb_opaque_surfaces = nb_surfaces;
221 qsv->opaque_surfaces = q->opaque_alloc_buf;
222 qsv->opaque_alloc_type = q->req.Type;
223
224 return 0;
225}
226
72b7441a
AK
227int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
228{
dc923bc2 229 int opaque_alloc = 0;
72b7441a
AK
230 int ret;
231
232 q->param.IOPattern = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
233 q->param.AsyncDepth = q->async_depth;
234
69b92f1b
AK
235 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
236 (sizeof(AVPacket) + sizeof(mfxSyncPoint) + sizeof(mfxBitstream*)));
237 if (!q->async_fifo)
238 return AVERROR(ENOMEM);
239
72b7441a
AK
240 if (avctx->hwaccel_context) {
241 AVQSVContext *qsv = avctx->hwaccel_context;
242
243 q->session = qsv->session;
244 q->param.IOPattern = qsv->iopattern;
dc923bc2
AK
245
246 opaque_alloc = qsv->opaque_alloc;
72b7441a
AK
247 }
248
249 if (!q->session) {
66acb76b
AK
250 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
251 q->load_plugins);
72b7441a
AK
252 if (ret < 0)
253 return ret;
254
255 q->session = q->internal_session;
256 }
257
258 ret = init_video_param(avctx, q);
259 if (ret < 0)
260 return ret;
261
262 ret = MFXVideoENCODE_QueryIOSurf(q->session, &q->param, &q->req);
263 if (ret < 0) {
264 av_log(avctx, AV_LOG_ERROR, "Error querying the encoding parameters\n");
265 return ff_qsv_error(ret);
266 }
267
dc923bc2
AK
268 if (opaque_alloc) {
269 ret = qsv_init_opaque_alloc(avctx, q);
270 if (ret < 0)
271 return ret;
272 }
273
772c87c5
AK
274 if (avctx->hwaccel_context) {
275 AVQSVContext *qsv = avctx->hwaccel_context;
276 int i, j;
277
dc923bc2 278 q->extparam = av_mallocz_array(qsv->nb_ext_buffers + q->nb_extparam_internal,
772c87c5
AK
279 sizeof(*q->extparam));
280 if (!q->extparam)
281 return AVERROR(ENOMEM);
282
283 q->param.ExtParam = q->extparam;
284 for (i = 0; i < qsv->nb_ext_buffers; i++)
285 q->param.ExtParam[i] = qsv->ext_buffers[i];
286 q->param.NumExtParam = qsv->nb_ext_buffers;
287
dc923bc2 288 for (i = 0; i < q->nb_extparam_internal; i++) {
772c87c5
AK
289 for (j = 0; j < qsv->nb_ext_buffers; j++) {
290 if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId)
291 break;
292 }
293 if (j < qsv->nb_ext_buffers)
294 continue;
295
296 q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i];
297 }
298 } else {
299 q->param.ExtParam = q->extparam_internal;
dc923bc2 300 q->param.NumExtParam = q->nb_extparam_internal;
772c87c5
AK
301 }
302
72b7441a
AK
303 ret = MFXVideoENCODE_Init(q->session, &q->param);
304 if (ret < 0) {
305 av_log(avctx, AV_LOG_ERROR, "Error initializing the encoder\n");
306 return ff_qsv_error(ret);
307 }
308
309 ret = qsv_retrieve_enc_params(avctx, q);
310 if (ret < 0) {
311 av_log(avctx, AV_LOG_ERROR, "Error retrieving encoding parameters.\n");
312 return ret;
313 }
314
72b7441a
AK
315 q->avctx = avctx;
316
317 return 0;
318}
319
320static void clear_unused_frames(QSVEncContext *q)
321{
322 QSVFrame *cur = q->work_frames;
323 while (cur) {
324 if (cur->surface && !cur->surface->Data.Locked) {
325 cur->surface = NULL;
326 av_frame_unref(cur->frame);
327 }
328 cur = cur->next;
329 }
330}
331
332static int get_free_frame(QSVEncContext *q, QSVFrame **f)
333{
334 QSVFrame *frame, **last;
335
336 clear_unused_frames(q);
337
338 frame = q->work_frames;
339 last = &q->work_frames;
340 while (frame) {
341 if (!frame->surface) {
342 *f = frame;
343 return 0;
344 }
345
346 last = &frame->next;
347 frame = frame->next;
348 }
349
350 frame = av_mallocz(sizeof(*frame));
351 if (!frame)
352 return AVERROR(ENOMEM);
353 frame->frame = av_frame_alloc();
354 if (!frame->frame) {
355 av_freep(&frame);
356 return AVERROR(ENOMEM);
357 }
358 *last = frame;
359
360 *f = frame;
361
362 return 0;
363}
364
365static int submit_frame(QSVEncContext *q, const AVFrame *frame,
366 mfxFrameSurface1 **surface)
367{
368 QSVFrame *qf;
369 int ret;
370
371 ret = get_free_frame(q, &qf);
372 if (ret < 0)
373 return ret;
374
375 if (frame->format == AV_PIX_FMT_QSV) {
376 ret = av_frame_ref(qf->frame, frame);
377 if (ret < 0)
378 return ret;
379
380 qf->surface = (mfxFrameSurface1*)qf->frame->data[3];
f6f32fc9 381 } else {
2ec96b6b
AK
382 /* make a copy if the input is not padded as libmfx requires */
383 if (frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) {
384 qf->frame->height = FFALIGN(frame->height, 32);
385 qf->frame->width = FFALIGN(frame->width, q->width_align);
386
387 ret = ff_get_buffer(q->avctx, qf->frame, AV_GET_BUFFER_FLAG_REF);
388 if (ret < 0)
389 return ret;
390
391 qf->frame->height = frame->height;
392 qf->frame->width = frame->width;
393 ret = av_frame_copy(qf->frame, frame);
394 if (ret < 0) {
395 av_frame_unref(qf->frame);
396 return ret;
397 }
398 } else {
399 ret = av_frame_ref(qf->frame, frame);
400 if (ret < 0)
401 return ret;
72b7441a 402 }
72b7441a 403
2ec96b6b 404 qf->surface_internal.Info = q->param.mfx.FrameInfo;
72b7441a 405
2ec96b6b
AK
406 qf->surface_internal.Info.PicStruct =
407 !frame->interlaced_frame ? MFX_PICSTRUCT_PROGRESSIVE :
408 frame->top_field_first ? MFX_PICSTRUCT_FIELD_TFF :
409 MFX_PICSTRUCT_FIELD_BFF;
410 if (frame->repeat_pict == 1)
411 qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FIELD_REPEATED;
412 else if (frame->repeat_pict == 2)
413 qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_DOUBLING;
414 else if (frame->repeat_pict == 4)
415 qf->surface_internal.Info.PicStruct |= MFX_PICSTRUCT_FRAME_TRIPLING;
72b7441a 416
2ec96b6b
AK
417 qf->surface_internal.Data.PitchLow = qf->frame->linesize[0];
418 qf->surface_internal.Data.Y = qf->frame->data[0];
419 qf->surface_internal.Data.UV = qf->frame->data[1];
72b7441a 420
2ec96b6b 421 qf->surface = &qf->surface_internal;
f6f32fc9
AK
422 }
423
424 qf->surface->Data.TimeStamp = av_rescale_q(frame->pts, q->avctx->time_base, (AVRational){1, 90000});
72b7441a
AK
425
426 *surface = qf->surface;
427
428 return 0;
429}
430
431static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
432{
433 if (q->param.mfx.CodecId == MFX_CODEC_AVC) {
434 if (q->param.mfx.CodecProfile == MFX_PROFILE_AVC_BASELINE ||
435 q->param.mfx.CodecLevel < MFX_LEVEL_AVC_21 ||
436 q->param.mfx.CodecLevel > MFX_LEVEL_AVC_41)
437 av_log(avctx, AV_LOG_WARNING,
438 "Interlaced coding is supported"
439 " at Main/High Profile Level 2.1-4.1\n");
440 }
441}
442
443int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
444 AVPacket *pkt, const AVFrame *frame, int *got_packet)
445{
69b92f1b
AK
446 AVPacket new_pkt = { 0 };
447 mfxBitstream *bs;
72b7441a
AK
448
449 mfxFrameSurface1 *surf = NULL;
450 mfxSyncPoint sync = NULL;
451 int ret;
452
453 if (frame) {
454 ret = submit_frame(q, frame, &surf);
455 if (ret < 0) {
456 av_log(avctx, AV_LOG_ERROR, "Error submitting the frame for encoding.\n");
457 return ret;
458 }
459 }
460
69b92f1b 461 ret = av_new_packet(&new_pkt, q->packet_size);
72b7441a
AK
462 if (ret < 0) {
463 av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
464 return ret;
465 }
69b92f1b
AK
466
467 bs = av_mallocz(sizeof(*bs));
468 if (!bs) {
469 av_packet_unref(&new_pkt);
470 return AVERROR(ENOMEM);
471 }
472 bs->Data = new_pkt.data;
473 bs->MaxLength = new_pkt.size;
72b7441a
AK
474
475 do {
69b92f1b 476 ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, bs, &sync);
72b7441a
AK
477 if (ret == MFX_WRN_DEVICE_BUSY)
478 av_usleep(1);
479 } while (ret > 0);
480
69b92f1b
AK
481 if (ret < 0) {
482 av_packet_unref(&new_pkt);
483 av_freep(&bs);
72b7441a 484 return (ret == MFX_ERR_MORE_DATA) ? 0 : ff_qsv_error(ret);
69b92f1b 485 }
72b7441a
AK
486
487 if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
488 print_interlace_msg(avctx, q);
489
490 if (sync) {
69b92f1b
AK
491 av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
492 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
493 av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL);
494 } else {
495 av_packet_unref(&new_pkt);
496 av_freep(&bs);
497 }
498
499 if (!av_fifo_space(q->async_fifo) ||
500 (!frame && av_fifo_size(q->async_fifo))) {
501 av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
502 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
503 av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
504
72b7441a
AK
505 MFXVideoCORE_SyncOperation(q->session, sync, 60000);
506
69b92f1b
AK
507 new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
508 new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
509 new_pkt.size = bs->DataLength;
510
511 if (bs->FrameType & MFX_FRAMETYPE_IDR ||
512 bs->FrameType & MFX_FRAMETYPE_xIDR)
513 new_pkt.flags |= AV_PKT_FLAG_KEY;
514
40cf1bba
VG
515#if FF_API_CODED_FRAME
516FF_DISABLE_DEPRECATION_WARNINGS
69b92f1b 517 if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
72b7441a 518 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
69b92f1b 519 else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
72b7441a 520 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
69b92f1b 521 else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
72b7441a 522 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
40cf1bba
VG
523FF_ENABLE_DEPRECATION_WARNINGS
524#endif
72b7441a 525
69b92f1b
AK
526 av_freep(&bs);
527
528 if (pkt->data) {
529 if (pkt->size < new_pkt.size) {
530 av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n",
531 pkt->size, new_pkt.size);
532 av_packet_unref(&new_pkt);
533 return AVERROR(EINVAL);
534 }
72b7441a 535
69b92f1b
AK
536 memcpy(pkt->data, new_pkt.data, new_pkt.size);
537 pkt->size = new_pkt.size;
538
539 ret = av_packet_copy_props(pkt, &new_pkt);
540 av_packet_unref(&new_pkt);
541 if (ret < 0)
542 return ret;
543 } else
544 *pkt = new_pkt;
72b7441a
AK
545
546 *got_packet = 1;
547 }
548
549 return 0;
550}
551
552int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
553{
554 QSVFrame *cur;
555
83847cc8
AK
556 if (q->session)
557 MFXVideoENCODE_Close(q->session);
72b7441a
AK
558 if (q->internal_session)
559 MFXClose(q->internal_session);
560 q->session = NULL;
561 q->internal_session = NULL;
562
563 cur = q->work_frames;
564 while (cur) {
565 q->work_frames = cur->next;
566 av_frame_free(&cur->frame);
567 av_freep(&cur);
568 cur = q->work_frames;
569 }
570
69b92f1b
AK
571 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
572 AVPacket pkt;
573 mfxSyncPoint sync;
574 mfxBitstream *bs;
575
576 av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL);
577 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
578 av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
579
580 av_freep(&bs);
581 av_packet_unref(&pkt);
582 }
583 av_fifo_free(q->async_fifo);
584 q->async_fifo = NULL;
585
dc923bc2
AK
586 av_freep(&q->opaque_surfaces);
587 av_buffer_unref(&q->opaque_alloc_buf);
588
772c87c5
AK
589 av_freep(&q->extparam);
590
72b7441a
AK
591 return 0;
592}