Move av_packet_*() functions from libavformat/ to libavcodec/, where the
[ffmpeg-lucabe.git] / libavcodec / avpacket.c
blob107afb3817b24eb1632a14bd43a9bcc19c63e7c5
1 /*
2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "avcodec.h"
25 void av_destruct_packet_nofree(AVPacket *pkt)
27 pkt->data = NULL; pkt->size = 0;
30 void av_destruct_packet(AVPacket *pkt)
32 av_free(pkt->data);
33 pkt->data = NULL; pkt->size = 0;
36 void av_init_packet(AVPacket *pkt)
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= av_destruct_packet_nofree;
48 int av_new_packet(AVPacket *pkt, int size)
50 uint8_t *data;
51 if((unsigned)size > (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
52 return AVERROR(ENOMEM);
53 data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
54 if (!data)
55 return AVERROR(ENOMEM);
56 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
58 av_init_packet(pkt);
59 pkt->data = data;
60 pkt->size = size;
61 pkt->destruct = av_destruct_packet;
62 return 0;
65 int av_dup_packet(AVPacket *pkt)
67 if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
68 uint8_t *data;
69 /* We duplicate the packet and don't forget to add the padding again. */
70 if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
71 return AVERROR(ENOMEM);
72 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
73 if (!data) {
74 return AVERROR(ENOMEM);
76 memcpy(data, pkt->data, pkt->size);
77 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
78 pkt->data = data;
79 pkt->destruct = av_destruct_packet;
81 return 0;