[FFmpeg-devel] [PATCH 10/18] avdevice/decklink_dec: port to the new packet list API

James Almer jamrial at gmail.com
Wed Nov 18 18:52:39 EET 2020


Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavdevice/decklink_common.h |  2 +-
 libavdevice/decklink_dec.cpp  | 53 +++++++++++++----------------------
 2 files changed, 20 insertions(+), 35 deletions(-)

diff --git a/libavdevice/decklink_common.h b/libavdevice/decklink_common.h
index f35bd9ae6f..9ee877f349 100644
--- a/libavdevice/decklink_common.h
+++ b/libavdevice/decklink_common.h
@@ -75,7 +75,7 @@ class decklink_output_callback;
 class decklink_input_callback;
 
 typedef struct AVPacketQueue {
-    AVPacketList *first_pkt, *last_pkt;
+    AVPacketList *pktl;
     int nb_packets;
     unsigned long long size;
     int abort_request;
diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp
index 6517b9df13..1115c5af79 100644
--- a/libavdevice/decklink_dec.cpp
+++ b/libavdevice/decklink_dec.cpp
@@ -469,10 +469,13 @@ skip_packet:
     return tgt;
 }
 
-static void avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
+static int avpacket_queue_init(AVFormatContext *avctx, AVPacketQueue *q)
 {
     struct decklink_cctx *ctx = (struct decklink_cctx *)avctx->priv_data;
     memset(q, 0, sizeof(AVPacketQueue));
+    q->pktl = av_packet_list_alloc();
+    if (!q->pktl)
+        return AVERROR(ENOMEM);
     pthread_mutex_init(&q->mutex, NULL);
     pthread_cond_init(&q->cond, NULL);
     q->avctx = avctx;
@@ -484,13 +487,7 @@ static void avpacket_queue_flush(AVPacketQueue *q)
     AVPacketList *pkt, *pkt1;
 
     pthread_mutex_lock(&q->mutex);
-    for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
-        pkt1 = pkt->next;
-        av_packet_unref(&pkt->pkt);
-        av_freep(&pkt);
-    }
-    q->last_pkt   = NULL;
-    q->first_pkt  = NULL;
+    av_packet_list_flush(q->pktl);
     q->nb_packets = 0;
     q->size       = 0;
     pthread_mutex_unlock(&q->mutex);
@@ -499,6 +496,7 @@ static void avpacket_queue_flush(AVPacketQueue *q)
 static void avpacket_queue_end(AVPacketQueue *q)
 {
     avpacket_queue_flush(q);
+    av_packet_list_free(&q->pktl);
     pthread_mutex_destroy(&q->mutex);
     pthread_cond_destroy(&q->cond);
 }
@@ -514,7 +512,7 @@ static unsigned long long avpacket_queue_size(AVPacketQueue *q)
 
 static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
 {
-    AVPacketList *pkt1;
+    int size;
 
     // Drop Packet if queue size is > maximum queue size
     if (avpacket_queue_size(q) > (uint64_t)q->max_q_size) {
@@ -528,25 +526,16 @@ static int avpacket_queue_put(AVPacketQueue *q, AVPacket *pkt)
         return -1;
     }
 
-    pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
-    if (!pkt1) {
-        av_packet_unref(pkt);
-        return -1;
-    }
-    av_packet_move_ref(&pkt1->pkt, pkt);
-    pkt1->next = NULL;
-
     pthread_mutex_lock(&q->mutex);
 
-    if (!q->last_pkt) {
-        q->first_pkt = pkt1;
-    } else {
-        q->last_pkt->next = pkt1;
-    }
+    size = pkt->size;
+
+    ret = av_packet_list_put(q->pktl, pkt, 0);
+    if (ret < 0)
+        return 0;
 
-    q->last_pkt = pkt1;
     q->nb_packets++;
-    q->size += pkt1->pkt.size + sizeof(*pkt1);
+    q->size += size;
 
     pthread_cond_signal(&q->cond);
 
@@ -562,16 +551,10 @@ static int avpacket_queue_get(AVPacketQueue *q, AVPacket *pkt, int block)
     pthread_mutex_lock(&q->mutex);
 
     for (;; ) {
-        pkt1 = q->first_pkt;
-        if (pkt1) {
-            q->first_pkt = pkt1->next;
-            if (!q->first_pkt) {
-                q->last_pkt = NULL;
-            }
+        ret = av_packet_list_get(q->pktl, pkt, 0);
+        if (!ret) {
             q->nb_packets--;
-            q->size -= pkt1->pkt.size + sizeof(*pkt1);
-            *pkt     = pkt1->pkt;
-            av_free(pkt1);
+            q->size -= pkt.size;
             ret = 1;
             break;
         } else if (!block) {
@@ -1382,7 +1365,9 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx)
         goto error;
     }
 
-    avpacket_queue_init (avctx, &ctx->queue);
+    ret = avpacket_queue_init(avctx, &ctx->queue);
+    if (ret < 0)
+        goto error;
 
     if (ctx->dli->StartStreams() != S_OK) {
         av_log(avctx, AV_LOG_ERROR, "Cannot start input stream\n");
-- 
2.29.2



More information about the ffmpeg-devel mailing list