]> git.sesse.net Git - ffmpeg/blob - libavcodec/avpacket.c
g726: decode directly to the user-provided AVFrame
[ffmpeg] / libavcodec / avpacket.c
1 /*
2  * AVPacket functions for libavcodec
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <string.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/mem.h"
26 #include "avcodec.h"
27
28 void av_destruct_packet(AVPacket *pkt)
29 {
30     av_free(pkt->data);
31     pkt->data = NULL;
32     pkt->size = 0;
33 }
34
35 void av_init_packet(AVPacket *pkt)
36 {
37     pkt->pts                  = AV_NOPTS_VALUE;
38     pkt->dts                  = AV_NOPTS_VALUE;
39     pkt->pos                  = -1;
40     pkt->duration             = 0;
41     pkt->convergence_duration = 0;
42     pkt->flags                = 0;
43     pkt->stream_index         = 0;
44     pkt->destruct             = NULL;
45     pkt->side_data            = NULL;
46     pkt->side_data_elems      = 0;
47 }
48
49 int av_new_packet(AVPacket *pkt, int size)
50 {
51     uint8_t *data = NULL;
52     if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
53         data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
54     if (data) {
55         memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
56     } else
57         size = 0;
58
59     av_init_packet(pkt);
60     pkt->data     = data;
61     pkt->size     = size;
62     pkt->destruct = av_destruct_packet;
63     if (!data)
64         return AVERROR(ENOMEM);
65     return 0;
66 }
67
68 void av_shrink_packet(AVPacket *pkt, int size)
69 {
70     if (pkt->size <= size)
71         return;
72     pkt->size = size;
73     memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
74 }
75
76 int av_grow_packet(AVPacket *pkt, int grow_by)
77 {
78     void *new_ptr;
79     av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
80     if (!pkt->size)
81         return av_new_packet(pkt, grow_by);
82     if ((unsigned)grow_by >
83         INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
84         return -1;
85     new_ptr = av_realloc(pkt->data,
86                          pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
87     if (!new_ptr)
88         return AVERROR(ENOMEM);
89     pkt->data  = new_ptr;
90     pkt->size += grow_by;
91     memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
92     return 0;
93 }
94
95 #define DUP_DATA(dst, src, size, padding)                               \
96     do {                                                                \
97         void *data;                                                     \
98         if (padding) {                                                  \
99             if ((unsigned)(size) >                                      \
100                 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE)        \
101                 goto failed_alloc;                                      \
102             data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);      \
103         } else {                                                        \
104             data = av_malloc(size);                                     \
105         }                                                               \
106         if (!data)                                                      \
107             goto failed_alloc;                                          \
108         memcpy(data, src, size);                                        \
109         if (padding)                                                    \
110             memset((uint8_t *)data + size, 0,                           \
111                    FF_INPUT_BUFFER_PADDING_SIZE);                       \
112         dst = data;                                                     \
113     } while (0)
114
115 int av_dup_packet(AVPacket *pkt)
116 {
117     AVPacket tmp_pkt;
118
119     if (pkt->destruct == NULL && pkt->data) {
120         tmp_pkt = *pkt;
121
122         pkt->data      = NULL;
123         pkt->side_data = NULL;
124         DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1);
125         pkt->destruct = av_destruct_packet;
126
127         if (pkt->side_data_elems) {
128             int i;
129
130             DUP_DATA(pkt->side_data, tmp_pkt.side_data,
131                      pkt->side_data_elems * sizeof(*pkt->side_data), 0);
132             memset(pkt->side_data, 0,
133                    pkt->side_data_elems * sizeof(*pkt->side_data));
134             for (i = 0; i < pkt->side_data_elems; i++)
135                 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
136                          tmp_pkt.side_data[i].size, 1);
137         }
138     }
139     return 0;
140
141 failed_alloc:
142     av_destruct_packet(pkt);
143     return AVERROR(ENOMEM);
144 }
145
146 void av_free_packet(AVPacket *pkt)
147 {
148     if (pkt) {
149         int i;
150
151         if (pkt->destruct)
152             pkt->destruct(pkt);
153         pkt->data            = NULL;
154         pkt->size            = 0;
155
156         for (i = 0; i < pkt->side_data_elems; i++)
157             av_free(pkt->side_data[i].data);
158         av_freep(&pkt->side_data);
159         pkt->side_data_elems = 0;
160     }
161 }
162
163 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
164                                  int size)
165 {
166     int elems = pkt->side_data_elems;
167
168     if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
169         return NULL;
170     if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
171         return NULL;
172
173     pkt->side_data = av_realloc(pkt->side_data,
174                                 (elems + 1) * sizeof(*pkt->side_data));
175     if (!pkt->side_data)
176         return NULL;
177
178     pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
179     if (!pkt->side_data[elems].data)
180         return NULL;
181     pkt->side_data[elems].size = size;
182     pkt->side_data[elems].type = type;
183     pkt->side_data_elems++;
184
185     return pkt->side_data[elems].data;
186 }
187
188 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
189                                  int *size)
190 {
191     int i;
192
193     for (i = 0; i < pkt->side_data_elems; i++) {
194         if (pkt->side_data[i].type == type) {
195             if (size)
196                 *size = pkt->side_data[i].size;
197             return pkt->side_data[i].data;
198         }
199     }
200     return NULL;
201 }
202
203 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
204                                int size)
205 {
206     int i;
207
208     for (i = 0; i < pkt->side_data_elems; i++) {
209         if (pkt->side_data[i].type == type) {
210             if (size > pkt->side_data[i].size)
211                 return AVERROR(ENOMEM);
212             pkt->side_data[i].size = size;
213             return 0;
214         }
215     }
216     return AVERROR(ENOENT);
217 }