diff mbox

[FFmpeg-devel] libavcodec/tests: Added test for libavcodec/avpacket.c

Message ID 1476662485-6371-1-git-send-email-thomastdt@gmail.com
State Superseded
Headers show

Commit Message

Thomas Turner Oct. 17, 2016, 12:01 a.m. UTC
Improved code coverage for libavcodec.
Function(s) Tested: av_packet_clone().

Signed-off-by: Thomas Turner <thomastdt@gmail.com>
---
 libavcodec/Makefile         |   3 +-
 libavcodec/tests/avpacket.c | 185 ++++++++++++++++++++++++++++++++++++++++++++
 tests/fate/libavcodec.mak   |   5 ++
 3 files changed, 192 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/tests/avpacket.c
diff mbox

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a1560ba..d64b8df 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1016,7 +1016,8 @@  SKIPHEADERS-$(CONFIG_VDA)              += vda.h vda_vt_internal.h
 SKIPHEADERS-$(CONFIG_VDPAU)            += vdpau.h vdpau_internal.h
 SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX)     += videotoolbox.h vda_vt_internal.h
 
-TESTPROGS = imgconvert                                                  \
+TESTPROGS = avpacket                                                    \
+            imgconvert                                                  \
             jpeg2000dwt                                                 \
             mathops                                                    \
             options                                                     \
diff --git a/libavcodec/tests/avpacket.c b/libavcodec/tests/avpacket.c
new file mode 100644
index 0000000..20fc188
--- /dev/null
+++ b/libavcodec/tests/avpacket.c
@@ -0,0 +1,185 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include "libavcodec/avcodec.h"
+#include "libavutil/error.h"
+
+static int IS_SPRINTF_ERR(int val, int tot_elem)
+{
+    int ret = 0;
+    if(val > tot_elem){
+        perror("overflow");
+        ret = 1;
+    }
+    else if(val < 0){
+        perror("sprintf");
+        ret = 1;
+    }
+    if(ret > 0)
+        return ret;
+
+    return ret;
+}
+
+static void LOG_AVPACKET(AVPacket avpkt, const char* message)
+{
+    int val, tot_elem;
+
+    /* initialize array's */
+    char buf_info[256]  = {0};
+    char data_info[256] = {0};
+    char side_info[256] = {0};
+
+    /* get buf information */
+    if(avpkt.buf){
+        val = sprintf(buf_info, "{buffer: %p,  data: %p, size: %d}",
+            avpkt.buf->buffer, avpkt.buf->data, avpkt.buf->size);
+        tot_elem = strlen(buf_info);
+
+        /** if sprintf error found */
+        if(IS_SPRINTF_ERR(val, tot_elem))
+            exit(-1);
+    }
+    /*get data information*/
+    if(avpkt.data){
+        val = snprintf(data_info, avpkt.size + 2 + 1, "\"%s\"", avpkt.data);
+        tot_elem = strlen(data_info);
+
+        /**if snprintf error found*/
+        if(val != tot_elem){
+            perror("snprintf");
+            exit(-1);
+        }
+    }
+    /*get side data information*/
+    if(avpkt.side_data){
+        val = sprintf(side_info, "{data: %p \"%s\", size: %d, type: %d}",
+            avpkt.side_data, avpkt.side_data->data, avpkt.side_data->size,
+            avpkt.side_data->type);
+        tot_elem = strlen(side_info);
+
+        /** if sprintf error found */
+        if(IS_SPRINTF_ERR(val, tot_elem))
+            exit(-1);
+    }
+    /* log standard packet information*/
+    av_log(NULL, AV_LOG_INFO,
+           "\n%s:\n\n"
+           "buf\t\t: %p "
+           "%s\n"
+           "pts\t\t: %" PRId64 "\n"
+           "dts\t\t: %" PRId64 "\n"
+           "data\t\t: %p "
+           "%s\n"
+           "size\t\t: %d\n"
+           "stream_index\t: %d\n"
+           "flags\t\t: %d\n"
+           "side_data\t: %p "
+           "%s\n"
+           "side_data_elems\t: %d\n"
+           "duration\t: %" PRId64 "\n"
+           "pos\t\t: %" PRId64 "\n\n",
+           message,
+           avpkt.buf,
+           buf_info,
+           avpkt.pts,
+           avpkt.dts,
+           avpkt.data,
+           data_info,
+           avpkt.size,
+           avpkt.stream_index,
+           avpkt.flags,
+           avpkt.side_data,
+           side_info,
+           avpkt.side_data_elems,
+           avpkt.duration,
+           avpkt.pos
+    );
+
+}
+
+
+static void TEST_AV_PACKET_CLONE(void)
+{
+    int ret = 0, len;
+    uint8_t *extra_data = NULL;
+    const uint8_t *data_name = NULL;
+    AVPacket avpkt, *avpkt_clone = NULL;
+    uint8_t data[] = "selftest for av_packet_clone(...)";
+
+    /* initialize avpkt */
+    av_init_packet(&avpkt);
+
+    avpkt.data = data;
+    avpkt.size = strlen(data);
+    avpkt.flags = AV_PKT_FLAG_DISCARD;
+
+    /*get side_data_name string*/
+    data_name = av_packet_side_data_name(AV_PKT_DATA_NEW_EXTRADATA);
+
+    /*Allocate a memory block*/
+    len = strlen(data_name);
+    extra_data = av_malloc(len);
+
+    if(!extra_data){
+        ret = AVERROR(ENOMEM);
+        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
+        exit(1);
+    }
+    /*copy side_data_name to extra_data array*/
+    memcpy(extra_data, data_name, len);
+
+    /*create side data for AVPacket*/
+    ret = av_packet_add_side_data(&avpkt, AV_PKT_DATA_NEW_EXTRADATA,
+                                        extra_data, len);
+    if(ret < 0){
+        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
+        exit(1);
+    }
+
+    /* clone avpkt */
+    avpkt_clone = av_packet_clone(&avpkt);
+
+    /* log packet information */
+    if(avpkt_clone){
+        LOG_AVPACKET(avpkt, "Original Packet info");
+        LOG_AVPACKET(*avpkt_clone, "Cloned Packet info");
+    }
+    else{
+        av_log(NULL, AV_LOG_ERROR, "av_packet_clone error\n");
+        exit(1);
+    }
+
+    /*cleanup*/
+    av_packet_free(&avpkt_clone);
+    av_packet_unref(&avpkt);
+
+}
+
+
+
+int main(void)
+{
+    TEST_AV_PACKET_CLONE();
+
+    return 0;
+}
diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak
index cf25285..3bc74c1 100644
--- a/tests/fate/libavcodec.mak
+++ b/tests/fate/libavcodec.mak
@@ -1,3 +1,8 @@ 
+FATE_LIBAVCODEC-yes += fate-avpacket
+fate-avpacket: libavcodec/tests/avpacket$(EXESUF)
+fate-avpacket: CMD = run libavcodec/tests/avpacket
+fate-avpacket: REF = /dev/null
+
 FATE_LIBAVCODEC-$(CONFIG_CABAC) += fate-cabac
 fate-cabac: libavcodec/tests/cabac$(EXESUF)
 fate-cabac: CMD = run libavcodec/tests/cabac