diff mbox series

[FFmpeg-devel,11/18] avdevice/vfwcap: port to the new packet list API

Message ID 20201118165247.4130-12-jamrial@gmail.com
State New
Headers show
Series AVPacketList public API
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

James Almer Nov. 18, 2020, 4:52 p.m. UTC
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavdevice/vfwcap.c | 49 +++++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 28 deletions(-)
diff mbox series

Patch

diff --git a/libavdevice/vfwcap.c b/libavdevice/vfwcap.c
index e2ab276c2e..eb5c29923f 100644
--- a/libavdevice/vfwcap.c
+++ b/libavdevice/vfwcap.c
@@ -178,7 +178,8 @@  static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
 {
     AVFormatContext *s;
     struct vfw_ctx *ctx;
-    AVPacketList **ppktl, *pktl_next;
+    AVPacket pkt = { 0 };
+    int ret;
 
     s = (AVFormatContext *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
     ctx = s->priv_data;
@@ -190,21 +191,18 @@  static LRESULT CALLBACK videostream_cb(HWND hwnd, LPVIDEOHDR vdhdr)
 
     WaitForSingleObject(ctx->mutex, INFINITE);
 
-    pktl_next = av_mallocz(sizeof(AVPacketList));
-    if(!pktl_next)
+    if (av_new_packet(&pkt, vdhdr->dwBytesUsed) < 0)
         goto fail;
 
-    if(av_new_packet(&pktl_next->pkt, vdhdr->dwBytesUsed) < 0) {
-        av_free(pktl_next);
+    pkt.pts = vdhdr->dwTimeCaptured;
+    memcpy(pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
+
+    ret = av_packet_list_put(ctx->pktl, &pkt, NULL, 0);
+    if (ret < 0) {
+        av_packet_unref(&pkt);
         goto fail;
     }
 
-    pktl_next->pkt.pts = vdhdr->dwTimeCaptured;
-    memcpy(pktl_next->pkt.data, vdhdr->lpData, vdhdr->dwBytesUsed);
-
-    for(ppktl = &ctx->pktl ; *ppktl ; ppktl = &(*ppktl)->next);
-    *ppktl = pktl_next;
-
     ctx->curbufsize += vdhdr->dwBytesUsed;
 
     SetEvent(ctx->event);
@@ -219,7 +217,6 @@  fail:
 static int vfw_read_close(AVFormatContext *s)
 {
     struct vfw_ctx *ctx = s->priv_data;
-    AVPacketList *pktl;
 
     if(ctx->hwnd) {
         SendMessage(ctx->hwnd, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
@@ -231,13 +228,7 @@  static int vfw_read_close(AVFormatContext *s)
     if(ctx->event)
         CloseHandle(ctx->event);
 
-    pktl = ctx->pktl;
-    while (pktl) {
-        AVPacketList *next = pktl->next;
-        av_packet_unref(&pktl->pkt);
-        av_free(pktl);
-        pktl = next;
-    }
+    av_packet_list_free(&ctx->pktl);
 
     return 0;
 }
@@ -300,6 +291,13 @@  static int vfw_read_header(AVFormatContext *s)
 
     SetWindowLongPtr(ctx->hwnd, GWLP_USERDATA, (LONG_PTR) s);
 
+
+    ctx->pktl = av_packet_list_alloc();
+    if (!ctx->pktl) {
+        vfw_read_close(s);
+        return AVERROR(ENOMEM);
+    }
+
     st = avformat_new_stream(s, NULL);
     if(!st) {
         vfw_read_close(s);
@@ -439,19 +437,14 @@  fail:
 static int vfw_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     struct vfw_ctx *ctx = s->priv_data;
-    AVPacketList *pktl = NULL;
+    int ret = AVERROR(EAGAIN);
 
-    while(!pktl) {
+    while (ret) {
         WaitForSingleObject(ctx->mutex, INFINITE);
-        pktl = ctx->pktl;
-        if(ctx->pktl) {
-            *pkt = ctx->pktl->pkt;
-            ctx->pktl = ctx->pktl->next;
-            av_free(pktl);
-        }
+        ret = av_packet_list_get(ctx->pktl, pkt, 0);
         ResetEvent(ctx->event);
         ReleaseMutex(ctx->mutex);
-        if(!pktl) {
+        if (ret) {
             if(s->flags & AVFMT_FLAG_NONBLOCK) {
                 return AVERROR(EAGAIN);
             } else {