1/* 2 * AVPacket functions for libavcodec 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard 4 * 5 * This file is part of FFmpeg. 6 * 7 * FFmpeg is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * FFmpeg is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with FFmpeg; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 */ 21 22#include "avcodec.h" 23 24 25void av_destruct_packet_nofree(AVPacket *pkt) 26{ 27 pkt->data = NULL; pkt->size = 0; 28} 29 30void av_destruct_packet(AVPacket *pkt) 31{ 32 av_free(pkt->data); 33 pkt->data = NULL; pkt->size = 0; 34} 35 36void av_init_packet(AVPacket *pkt) 37{ 38 pkt->pts = AV_NOPTS_VALUE; 39 pkt->dts = AV_NOPTS_VALUE; 40 pkt->pos = -1; 41 pkt->duration = 0; 42 pkt->convergence_duration = 0; 43 pkt->flags = 0; 44 pkt->stream_index = 0; 45 pkt->destruct= NULL; 46} 47 48int av_new_packet(AVPacket *pkt, int size) 49{ 50 uint8_t *data= NULL; 51 if((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE) 52 data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); 53 if (data){ 54 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 55 }else 56 size=0; 57 58 av_init_packet(pkt); 59 pkt->data = data; 60 pkt->size = size; 61 pkt->destruct = av_destruct_packet; 62 if(!data) 63 return AVERROR(ENOMEM); 64 return 0; 65} 66 67void av_shrink_packet(AVPacket *pkt, int size) 68{ 69 if (pkt->size <= size) return; 70 pkt->size = size; 71 memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 72} 73 74int av_dup_packet(AVPacket *pkt) 75{ 76 if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) { 77 uint8_t *data; 78 /* We duplicate the packet and don't forget to add the padding again. */ 79 if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE) 80 return AVERROR(ENOMEM); 81 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE); 82 if (!data) { 83 return AVERROR(ENOMEM); 84 } 85 memcpy(data, pkt->data, pkt->size); 86 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); 87 pkt->data = data; 88 pkt->destruct = av_destruct_packet; 89 } 90 return 0; 91} 92 93void av_free_packet(AVPacket *pkt) 94{ 95 if (pkt) { 96 if (pkt->destruct) pkt->destruct(pkt); 97 pkt->data = NULL; pkt->size = 0; 98 } 99} 100