Improved code coverage for libavcodec Function(s) Tested: av_packet_clone()
Signed-off-by: Thomas Turner <thomas...@gmail.com> --- libavcodec/Makefile | 3 +- libavcodec/tests/avpacket.c | 254 ++++++++++++++++++++++++++++++++++++++++++++ tests/fate/libavcodec.mak | 5 + 3 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 libavcodec/tests/avpacket.c 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..67ec71b --- /dev/null +++ b/libavcodec/tests/avpacket.c @@ -0,0 +1,254 @@ +/* + * 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 char* getbuffer(AVPacket avpkt, int index) +{ + uint8_t *buffer; + int val, buffer_size = 256; + + /* Allocate 256 bytes */ + if((buffer = malloc(buffer_size)) == NULL){ + perror("malloc"); + goto EXIT; + } + + if(index == 0){ + val = snprintf(buffer, buffer_size, + "{buffer: %p, data: %p, size: %d}", + avpkt.buf->buffer, avpkt.buf->data, avpkt.buf->size); + } + else if(index == 1){ + val = snprintf(buffer, buffer_size, "\"%s\"", avpkt.data); + } + else if(index == 2){ + val = snprintf(buffer, buffer_size, + "{data: %p \"%s\", size: %d, type: %d}", + avpkt.side_data, avpkt.side_data->data, avpkt.side_data->size, + avpkt.side_data->type); + } + + /* snprintf fail check */ + if(!(val > -1 && val < buffer_size)){ + perror("snprintf"); + free(buffer); + goto EXIT; + } + + return buffer; + +EXIT: + exit(-1); +} + +static void log_avpacket(AVPacket avpkt, const char* message) +{ + uint8_t *buf_info = 0, *data_info = 0, *side_info = 0; + + /* get buf information */ + if(avpkt.buf){ + buf_info = getbuffer(avpkt, 0); + } + + /* get data information */ + if(avpkt.data){ + data_info = getbuffer(avpkt, 1); + } + + /* get side data information */ + if(avpkt.side_data){ + side_info = getbuffer(avpkt, 2); + } + + /* 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 ? (char*)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 int compare_av_packet(AVPacket* p1, AVPacket* p2) +{ + /* compare data */ + + if(p1->size != p2->size){ + fprintf(stderr, "size\n"); + goto fail; + } + if(strncmp(p1->data, p2->data, p1->size) != 0){ + fprintf(stderr, "data\n"); + goto fail; + } + if(p1->pts != p2->pts){ + fprintf(stderr, "pts\n"); + goto fail; + } + if(p1->dts != p2->dts){ + fprintf(stderr, "dts\n"); + goto fail; + } + if(p1->stream_index != p2->stream_index){ + fprintf(stderr, "stream_index\n"); + goto fail; + } + if(p1->flags != p2->flags){ + fprintf(stderr, "flags\n"); + goto fail; + } + if(p1->side_data_elems != p2->side_data_elems){ + fprintf(stderr, "side_data_elems\n"); + goto fail; + } + if(p1->duration != p2->duration){ + fprintf(stderr, "duration\n"); + goto fail; + } + if(p1->pos != p2->pos){ + fprintf(stderr, "pos\n"); + goto fail; + } + + /*compare reference to the data buffer contents*/ + + if(p2->buf->size != (p2->size + AV_INPUT_BUFFER_PADDING_SIZE)){ + fprintf(stderr, "buf->size\n"); + goto fail; + } + if(strncmp(p1->data, p2->buf->data, p1->size) != 0){ + fprintf(stderr, "buf->data\n"); + goto fail; + } + + return 0; + +fail: + return 1; +} + +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 bloc */ + len = strlen(data_name); + extra_data = av_malloc(len); + + if(!extra_data){ + ret = AVERROR(ENOMEM); + goto print_error; + } + /* 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){ + goto print_error; + } + + /* clone avpkt */ + avpkt_clone = av_packet_clone(&avpkt); + + /* log packet information */ + if(avpkt_clone){ + if(compare_av_packet(&avpkt, avpkt_clone)){ + log_avpacket(avpkt, "Original Packet info"); + log_avpacket(*avpkt_clone, "Cloned Packet info"); + av_log(NULL, AV_LOG_ERROR, "\n\nfailed av_packet_clone test.\n"); + } + } + else{ + av_log(NULL, AV_LOG_ERROR, "av_packet_clone error\n"); + goto fail; + } + + /* cleanup */ + av_packet_free(&avpkt_clone); + av_packet_unref(&avpkt); + + return; + +print_error: + fprintf(stderr, "Error occurred: %s\n", av_err2str(ret)); + +fail: + exit(1); + +} + + +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 -- 1.9.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel