Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Anton Khirnov <anton@khirnov.net>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 21/35] lavc/qsvenc: switch to new FIFO API
Date: Tue, 11 Jan 2022 21:45:56 +0100
Message-ID: <20220111204610.14262-21-anton@khirnov.net> (raw)
In-Reply-To: <20220111204610.14262-1-anton@khirnov.net>

---
 libavcodec/qsvenc.c | 120 +++++++++++++++++++-------------------------
 1 file changed, 52 insertions(+), 68 deletions(-)

diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 4e7a15f060..ee489325f5 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -85,6 +85,12 @@ static const struct profile_names vp9_profiles[] = {
 #endif
 };
 
+typedef struct QSVPacket {
+    AVPacket        pkt;
+    mfxSyncPoint   *sync;
+    mfxBitstream   *bs;
+} QSVPacket;
+
 static const char *print_profile(enum AVCodecID codec_id, mfxU16 profile)
 {
     const struct profile_names *profiles;
@@ -1258,16 +1264,6 @@ static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q)
     return 0;
 }
 
-static inline unsigned int qsv_fifo_item_size(void)
-{
-    return sizeof(AVPacket) + sizeof(mfxSyncPoint*) + sizeof(mfxBitstream*);
-}
-
-static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
-{
-    return av_fifo_size(fifo)/qsv_fifo_item_size();
-}
-
 int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
 {
     int iopattern = 0;
@@ -1276,7 +1272,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
 
     q->param.AsyncDepth = q->async_depth;
 
-    q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
+    q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVPacket), 0);
     if (!q->async_fifo)
         return AVERROR(ENOMEM);
 
@@ -1578,15 +1574,13 @@ static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
 static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
                         const AVFrame *frame)
 {
-    AVPacket new_pkt = { 0 };
-    mfxBitstream *bs = NULL;
+    QSVPacket pkt = { { 0 } };
 #if QSV_VERSION_ATLEAST(1, 26)
     mfxExtAVCEncodedFrameInfo *enc_info = NULL;
     mfxExtBuffer **enc_buf = NULL;
 #endif
 
     mfxFrameSurface1 *surf = NULL;
-    mfxSyncPoint *sync     = NULL;
     QSVFrame *qsv_frame = NULL;
     mfxEncodeCtrl* enc_ctrl = NULL;
     int ret;
@@ -1609,17 +1603,17 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
         }
     }
 
-    ret = av_new_packet(&new_pkt, q->packet_size);
+    ret = av_new_packet(&pkt.pkt, q->packet_size);
     if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
         return ret;
     }
 
-    bs = av_mallocz(sizeof(*bs));
-    if (!bs)
+    pkt.bs = av_mallocz(sizeof(*pkt.bs));
+    if (!pkt.bs)
         goto nomem;
-    bs->Data      = new_pkt.data;
-    bs->MaxLength = new_pkt.size;
+    pkt.bs->Data      = pkt.pkt.data;
+    pkt.bs->MaxLength = pkt.pkt.size;
 
 #if QSV_VERSION_ATLEAST(1, 26)
     if (avctx->codec_id == AV_CODEC_ID_H264) {
@@ -1629,13 +1623,13 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
 
         enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
         enc_info->Header.BufferSz = sizeof (*enc_info);
-        bs->NumExtParam = 1;
+        pkt.bs->NumExtParam = 1;
         enc_buf = av_mallocz(sizeof(mfxExtBuffer *));
         if (!enc_buf)
             goto nomem;
         enc_buf[0] = (mfxExtBuffer *)enc_info;
 
-        bs->ExtParam = enc_buf;
+        pkt.bs->ExtParam = enc_buf;
     }
 #endif
 
@@ -1643,12 +1637,12 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
         q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl);
     }
 
-    sync = av_mallocz(sizeof(*sync));
-    if (!sync)
+    pkt.sync = av_mallocz(sizeof(*pkt.sync));
+    if (!pkt.sync)
         goto nomem;
 
     do {
-        ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync);
+        ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, pkt.bs, pkt.sync);
         if (ret == MFX_WRN_DEVICE_BUSY)
             av_usleep(500);
     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);
@@ -1667,15 +1661,13 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
 
     ret = 0;
 
-    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);
+    if (*pkt.sync) {
+        av_fifo_write(q->async_fifo, &pkt, 1);
     } else {
 free:
-        av_freep(&sync);
-        av_packet_unref(&new_pkt);
-        av_freep(&bs);
+        av_freep(&pkt.sync);
+        av_packet_unref(&pkt.pkt);
+        av_freep(&pkt.bs);
 #if QSV_VERSION_ATLEAST(1, 26)
         if (avctx->codec_id == AV_CODEC_ID_H264) {
             av_freep(&enc_info);
@@ -1699,60 +1691,56 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
     if (ret < 0)
         return ret;
 
-    if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
-        (!frame && av_fifo_size(q->async_fifo))) {
-        AVPacket new_pkt;
-        mfxBitstream *bs;
-        mfxSyncPoint *sync;
+    if ((av_fifo_can_read(q->async_fifo) >= q->async_depth) ||
+        (!frame && av_fifo_can_read(q->async_fifo))) {
+        QSVPacket qpkt;
 #if QSV_VERSION_ATLEAST(1, 26)
         mfxExtAVCEncodedFrameInfo *enc_info;
         mfxExtBuffer **enc_buf;
 #endif
         enum AVPictureType pict_type;
 
-        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);
+        av_fifo_read(q->async_fifo, &qpkt, 1);
 
         do {
-            ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
+            ret = MFXVideoCORE_SyncOperation(q->session, *qpkt.sync, 1000);
         } while (ret == MFX_WRN_IN_EXECUTION);
 
-        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;
+        qpkt.pkt.dts  = av_rescale_q(qpkt.bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
+        qpkt.pkt.pts  = av_rescale_q(qpkt.bs->TimeStamp,       (AVRational){1, 90000}, avctx->time_base);
+        qpkt.pkt.size = qpkt.bs->DataLength;
 
-        if (bs->FrameType & MFX_FRAMETYPE_IDR || bs->FrameType & MFX_FRAMETYPE_xIDR) {
-            new_pkt.flags |= AV_PKT_FLAG_KEY;
+        if (qpkt.bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.bs->FrameType & MFX_FRAMETYPE_xIDR) {
+            qpkt.pkt.flags |= AV_PKT_FLAG_KEY;
             pict_type = AV_PICTURE_TYPE_I;
-        } else if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
+        } else if (qpkt.bs->FrameType & MFX_FRAMETYPE_I || qpkt.bs->FrameType & MFX_FRAMETYPE_xI)
             pict_type = AV_PICTURE_TYPE_I;
-        else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
+        else if (qpkt.bs->FrameType & MFX_FRAMETYPE_P || qpkt.bs->FrameType & MFX_FRAMETYPE_xP)
             pict_type = AV_PICTURE_TYPE_P;
-        else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
+        else if (qpkt.bs->FrameType & MFX_FRAMETYPE_B || qpkt.bs->FrameType & MFX_FRAMETYPE_xB)
             pict_type = AV_PICTURE_TYPE_B;
-        else if (bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
+        else if (qpkt.bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
             pict_type = AV_PICTURE_TYPE_NONE;
             av_log(avctx, AV_LOG_WARNING, "Unknown FrameType, set pict_type to AV_PICTURE_TYPE_NONE.\n");
         } else {
-            av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", bs->FrameType);
+            av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", qpkt.bs->FrameType);
             return AVERROR_INVALIDDATA;
         }
 
 #if QSV_VERSION_ATLEAST(1, 26)
         if (avctx->codec_id == AV_CODEC_ID_H264) {
-            enc_buf = bs->ExtParam;
-            enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam);
-            ff_side_data_set_encoder_stats(&new_pkt,
+            enc_buf = qpkt.bs->ExtParam;
+            enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
+            ff_side_data_set_encoder_stats(&qpkt.pkt,
                 enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type);
             av_freep(&enc_info);
             av_freep(&enc_buf);
         }
 #endif
-        av_freep(&bs);
-        av_freep(&sync);
+        av_freep(&qpkt.bs);
+        av_freep(&qpkt.sync);
 
-        av_packet_move_ref(pkt, &new_pkt);
+        av_packet_move_ref(pkt, &qpkt.pkt);
 
         *got_packet = 1;
     }
@@ -1782,26 +1770,22 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
         cur = q->work_frames;
     }
 
-    while (q->async_fifo && av_fifo_size(q->async_fifo)) {
-        AVPacket pkt;
-        mfxSyncPoint *sync;
-        mfxBitstream *bs;
+    while (q->async_fifo && av_fifo_can_read(q->async_fifo)) {
+        QSVPacket pkt;
 
-        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_fifo_read(q->async_fifo, &pkt, 1);
 
 #if QSV_VERSION_ATLEAST(1, 26)
         if (avctx->codec_id == AV_CODEC_ID_H264) {
-            mfxExtBuffer **enc_buf = bs->ExtParam;
-            mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam);
+            mfxExtBuffer **enc_buf = pkt.bs->ExtParam;
+            mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
             av_freep(&enc_info);
             av_freep(&enc_buf);
         }
 #endif
-        av_freep(&sync);
-        av_freep(&bs);
-        av_packet_unref(&pkt);
+        av_freep(&pkt.sync);
+        av_freep(&pkt.bs);
+        av_packet_unref(&pkt.pkt);
     }
     av_fifo_free(q->async_fifo);
     q->async_fifo = NULL;
-- 
2.33.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2022-01-11 20:49 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump Anton Khirnov
2022-01-13 14:22   ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size Anton Khirnov
2022-01-13 16:50   ` Andreas Rheinhardt
2022-01-13 16:59   ` James Almer
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 04/35] lavu/fifo: add new functions for determinining reading/writing size Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function Anton Khirnov
2022-01-13 17:04   ` James Almer
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 06/35] lavu/fifo: add a new function for draining the FIFO Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions Anton Khirnov
2022-01-13 17:31   ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions Anton Khirnov
2022-01-13 17:41   ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed Anton Khirnov
2022-01-13 17:53   ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 10/35] lavu/fifo: deprecate old API Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 11/35] lavu/tests/fifo: switch to the new API Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API Anton Khirnov
2022-01-13 18:21   ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: " Anton Khirnov
2022-01-12 14:46   ` Michael Niedermayer
2022-01-12 19:29     ` Anton Khirnov
2022-01-13 14:14       ` Michael Niedermayer
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 14/35] lavc/cuviddec: do not reallocate the fifo unnecessarily Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 15/35] lavc/cuviddec: convert to the new FIFO API Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 16/35] lavc/libvorbisenc: switch to " Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 17/35] lavc/libvpxenc: switch to the " Anton Khirnov
2022-01-12 18:15   ` James Zern
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 18/35] lavc/libvpxenc: remove unneeded context variable Anton Khirnov
2022-01-12 18:15   ` James Zern
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 19/35] lavc/nvenc: switch to the new FIFO API Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 20/35] lavc/qsvdec: " Anton Khirnov
2022-01-11 20:45 ` Anton Khirnov [this message]
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 22/35] lavf/dvenc: return an error on audio/video desync Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 23/35] lavf/dvenc: switch to new FIFO API Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 24/35] lavf/mpegenc: " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 25/35] lavf/swfenc: " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 26/35] lavf/udp: " Anton Khirnov
2022-01-13 18:45   ` Andreas Rheinhardt
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 27/35] lavf/async: " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 28/35] lavd/jack: switch to the " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 29/35] lavu/audio_fifo: drop an unnecessary include Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 30/35] lavu/audio_fifo: switch to new FIFO API Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: " Anton Khirnov
2022-01-13 19:03   ` Andreas Rheinhardt
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 32/35] lavfi/qsvvpp: " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 33/35] lavfi/vf_deshake_opencl: " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 34/35] ffplay: " Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 35/35] ffmpeg: " Anton Khirnov
2022-01-13 13:59 ` [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Andreas Rheinhardt
2022-01-13 14:27 ` Anton Khirnov
2022-01-13 14:38   ` Andreas Rheinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220111204610.14262-21-anton@khirnov.net \
    --to=anton@khirnov.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git