• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/avpacket.c

Go to the documentation of this file.
00001 /*
00002  * AVPacket functions for libavcodec
00003  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avcodec.h"
00023 #include "libavutil/avassert.h"
00024 #include "bytestream.h"
00025 
00026 void av_destruct_packet_nofree(AVPacket *pkt)
00027 {
00028     pkt->data = NULL; pkt->size = 0;
00029 }
00030 
00031 void av_destruct_packet(AVPacket *pkt)
00032 {
00033     av_free(pkt->data);
00034     pkt->data = NULL; pkt->size = 0;
00035 }
00036 
00037 void av_init_packet(AVPacket *pkt)
00038 {
00039     pkt->pts   = AV_NOPTS_VALUE;
00040     pkt->dts   = AV_NOPTS_VALUE;
00041     pkt->pos   = -1;
00042     pkt->duration = 0;
00043     pkt->convergence_duration = 0;
00044     pkt->flags = 0;
00045     pkt->stream_index = 0;
00046     pkt->destruct= NULL;
00047 }
00048 
00049 int av_new_packet(AVPacket *pkt, int size)
00050 {
00051     uint8_t *data= NULL;
00052     if((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
00053         data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00054     if (data){
00055         memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00056     }else
00057         size=0;
00058 
00059     av_init_packet(pkt);
00060     pkt->data = data;
00061     pkt->size = size;
00062     pkt->destruct = av_destruct_packet;
00063     if(!data)
00064         return AVERROR(ENOMEM);
00065     return 0;
00066 }
00067 
00068 void av_shrink_packet(AVPacket *pkt, int size)
00069 {
00070     if (pkt->size <= size) return;
00071     pkt->size = size;
00072     memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00073 }
00074 
00075 int av_grow_packet(AVPacket *pkt, int grow_by)
00076 {
00077     void *new_ptr;
00078     av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
00079     if (!pkt->size)
00080         return av_new_packet(pkt, grow_by);
00081     if ((unsigned)grow_by > INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
00082         return -1;
00083     new_ptr = av_realloc(pkt->data, pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
00084     if (!new_ptr)
00085         return AVERROR(ENOMEM);
00086     pkt->data = new_ptr;
00087     pkt->size += grow_by;
00088     memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00089     return 0;
00090 }
00091 
00092 int av_dup_packet(AVPacket *pkt)
00093 {
00094     if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
00095         uint8_t *data;
00096         /* We duplicate the packet and don't forget to add the padding again. */
00097         if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
00098             return AVERROR(ENOMEM);
00099         data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00100         if (!data) {
00101             return AVERROR(ENOMEM);
00102         }
00103         memcpy(data, pkt->data, pkt->size);
00104         memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00105         pkt->data = data;
00106         pkt->destruct = av_destruct_packet;
00107     }
00108     return 0;
00109 }
00110 
00111 void av_free_packet(AVPacket *pkt)
00112 {
00113     if (pkt) {
00114         if (pkt->destruct) pkt->destruct(pkt);
00115         pkt->data = NULL; pkt->size = 0;
00116     }
00117 }
00118 

Generated on Fri Feb 22 2013 07:24:25 for FFmpeg by  doxygen 1.7.1