]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/qsvenc.c
Deprecate avctx.coded_frame
[ffmpeg] / libavcodec / qsvenc.c
index 690d5aa36d26d00f6d7bbc060e1fb2ee159b7cfe..ca91e3a5cb880613b178e2e9b046052c829981ed 100644 (file)
@@ -49,6 +49,8 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
         return AVERROR_BUG;
     q->param.mfx.CodecId = ret;
 
+    q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16;
+
     if (avctx->level > 0)
         q->param.mfx.CodecLevel = avctx->level;
 
@@ -65,7 +67,7 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
     q->param.mfx.BufferSizeInKB     = 0;
 
     q->param.mfx.FrameInfo.FourCC         = MFX_FOURCC_NV12;
-    q->param.mfx.FrameInfo.Width          = FFALIGN(avctx->width, 16);
+    q->param.mfx.FrameInfo.Width          = FFALIGN(avctx->width, q->width_align);
     q->param.mfx.FrameInfo.Height         = FFALIGN(avctx->height, 32);
     q->param.mfx.FrameInfo.CropX          = 0;
     q->param.mfx.FrameInfo.CropY          = 0;
@@ -124,15 +126,19 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q)
         break;
     }
 
-    q->extco.Header.BufferId      = MFX_EXTBUFF_CODING_OPTION;
-    q->extco.Header.BufferSz      = sizeof(q->extco);
-    q->extco.CAVLC                = avctx->coder_type == FF_CODER_TYPE_VLC ?
-                                    MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
+    // the HEVC encoder plugin currently fails if coding options
+    // are provided
+    if (avctx->codec_id != AV_CODEC_ID_HEVC) {
+        q->extco.Header.BufferId      = MFX_EXTBUFF_CODING_OPTION;
+        q->extco.Header.BufferSz      = sizeof(q->extco);
+        q->extco.CAVLC                = avctx->coder_type == FF_CODER_TYPE_VLC ?
+                                        MFX_CODINGOPTION_ON : MFX_CODINGOPTION_UNKNOWN;
 
-    q->extparam[0] = (mfxExtBuffer *)&q->extco;
+        q->extparam[0] = (mfxExtBuffer *)&q->extco;
 
-    q->param.ExtParam    = q->extparam;
-    q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam);
+        q->param.ExtParam    = q->extparam;
+        q->param.NumExtParam = FF_ARRAY_ELEMS(q->extparam);
+    }
 
     return 0;
 }
@@ -191,6 +197,11 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
     q->param.IOPattern  = MFX_IOPATTERN_IN_SYSTEM_MEMORY;
     q->param.AsyncDepth = q->async_depth;
 
+    q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
+                                  (sizeof(AVPacket) + sizeof(mfxSyncPoint) + sizeof(mfxBitstream*)));
+    if (!q->async_fifo)
+        return AVERROR(ENOMEM);
+
     if (avctx->hwaccel_context) {
         AVQSVContext *qsv = avctx->hwaccel_context;
 
@@ -199,7 +210,8 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
     }
 
     if (!q->session) {
-        ret = ff_qsv_init_internal_session(avctx, &q->internal_session);
+        ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
+                                           q->load_plugins);
         if (ret < 0)
             return ret;
 
@@ -228,10 +240,6 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
         return ret;
     }
 
-    avctx->coded_frame = av_frame_alloc();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
     q->avctx = avctx;
 
     return 0;
@@ -303,9 +311,9 @@ static int submit_frame(QSVEncContext *q, const AVFrame *frame,
     }
 
     /* make a copy if the input is not padded as libmfx requires */
-    if (frame->height & 31 || frame->linesize[0] & 15) {
+    if (frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) {
         qf->frame->height = FFALIGN(frame->height, 32);
-        qf->frame->width  = FFALIGN(frame->width, 16);
+        qf->frame->width  = FFALIGN(frame->width, q->width_align);
 
         ret = ff_get_buffer(q->avctx, qf->frame, AV_GET_BUFFER_FLAG_REF);
         if (ret < 0)
@@ -364,7 +372,8 @@ static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
 int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
                   AVPacket *pkt, const AVFrame *frame, int *got_packet)
 {
-    mfxBitstream bs = { { { 0 } } };
+    AVPacket new_pkt = { 0 };
+    mfxBitstream *bs;
 
     mfxFrameSurface1 *surf = NULL;
     mfxSyncPoint sync      = NULL;
@@ -378,43 +387,90 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
         }
     }
 
-    ret = ff_alloc_packet(pkt, q->packet_size);
+    ret = av_new_packet(&new_pkt, q->packet_size);
     if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
         return ret;
     }
-    bs.Data      = pkt->data;
-    bs.MaxLength = pkt->size;
+
+    bs = av_mallocz(sizeof(*bs));
+    if (!bs) {
+        av_packet_unref(&new_pkt);
+        return AVERROR(ENOMEM);
+    }
+    bs->Data      = new_pkt.data;
+    bs->MaxLength = new_pkt.size;
 
     do {
-        ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, &bs, &sync);
+        ret = MFXVideoENCODE_EncodeFrameAsync(q->session, NULL, surf, bs, &sync);
         if (ret == MFX_WRN_DEVICE_BUSY)
             av_usleep(1);
     } while (ret > 0);
 
-    if (ret < 0)
+    if (ret < 0) {
+        av_packet_unref(&new_pkt);
+        av_freep(&bs);
         return (ret == MFX_ERR_MORE_DATA) ? 0 : ff_qsv_error(ret);
+    }
 
     if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame)
         print_interlace_msg(avctx, q);
 
     if (sync) {
+        av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
+        av_fifo_generic_write(q->async_fifo, &sync,    sizeof(sync),    NULL);
+        av_fifo_generic_write(q->async_fifo, &bs,      sizeof(bs),    NULL);
+    } else {
+        av_packet_unref(&new_pkt);
+        av_freep(&bs);
+    }
+
+    if (!av_fifo_space(q->async_fifo) ||
+        (!frame && av_fifo_size(q->async_fifo))) {
+        av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
+        av_fifo_generic_read(q->async_fifo, &sync,    sizeof(sync),    NULL);
+        av_fifo_generic_read(q->async_fifo, &bs,      sizeof(bs),      NULL);
+
         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
 
-        if (bs.FrameType & MFX_FRAMETYPE_I || bs.FrameType & MFX_FRAMETYPE_xI)
+        new_pkt.dts  = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
+        new_pkt.pts  = av_rescale_q(bs->TimeStamp,       (AVRational){1, 90000}, avctx->time_base);
+        new_pkt.size = bs->DataLength;
+
+        if (bs->FrameType & MFX_FRAMETYPE_IDR ||
+            bs->FrameType & MFX_FRAMETYPE_xIDR)
+            new_pkt.flags |= AV_PKT_FLAG_KEY;
+
+#if FF_API_CODED_FRAME
+FF_DISABLE_DEPRECATION_WARNINGS
+        if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
             avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
-        else if (bs.FrameType & MFX_FRAMETYPE_P || bs.FrameType & MFX_FRAMETYPE_xP)
+        else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
             avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
-        else if (bs.FrameType & MFX_FRAMETYPE_B || bs.FrameType & MFX_FRAMETYPE_xB)
+        else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
             avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
 
-        pkt->dts  = av_rescale_q(bs.DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
-        pkt->pts  = av_rescale_q(bs.TimeStamp,       (AVRational){1, 90000}, avctx->time_base);
-        pkt->size = bs.DataLength;
+        av_freep(&bs);
 
-        if (bs.FrameType & MFX_FRAMETYPE_IDR ||
-            bs.FrameType & MFX_FRAMETYPE_xIDR)
-            pkt->flags |= AV_PKT_FLAG_KEY;
+        if (pkt->data) {
+            if (pkt->size < new_pkt.size) {
+                av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n",
+                       pkt->size, new_pkt.size);
+                av_packet_unref(&new_pkt);
+                return AVERROR(EINVAL);
+            }
+
+            memcpy(pkt->data, new_pkt.data, new_pkt.size);
+            pkt->size = new_pkt.size;
+
+            ret = av_packet_copy_props(pkt, &new_pkt);
+            av_packet_unref(&new_pkt);
+            if (ret < 0)
+                return ret;
+        } else
+            *pkt = new_pkt;
 
         *got_packet = 1;
     }
@@ -440,7 +496,20 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
         cur = q->work_frames;
     }
 
-    av_frame_free(&avctx->coded_frame);
+    while (q->async_fifo && av_fifo_size(q->async_fifo)) {
+        AVPacket pkt;
+        mfxSyncPoint sync;
+        mfxBitstream *bs;
+
+        av_fifo_generic_read(q->async_fifo, &pkt,  sizeof(pkt),  NULL);
+        av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
+        av_fifo_generic_read(q->async_fifo, &bs,   sizeof(bs),   NULL);
+
+        av_freep(&bs);
+        av_packet_unref(&pkt);
+    }
+    av_fifo_free(q->async_fifo);
+    q->async_fifo = NULL;
 
     return 0;
 }