~ubuntu-branches/ubuntu/oneiric/libav/oneiric

« back to all changes in this revision

Viewing changes to libavcodec/avpacket.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-19 15:04:55 UTC
  • mfrom: (1.2.1 upstream)
  • mto: (1.3.4 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20110419150455-c1nac6gjm3t2aa4n
Tags: 4:0.7~b1-1
* New upstream version
* bump SONAME and SHLIBS
* configure flags --disable-stripping was removed upstream
* the MAINTAINERS file was removed upstream
* remove patch disable-configuration-warning.patch
* drop avfilter confflags, it is enable by default in 0.7
* libfaad wrapper has been removed upstream
* also update the *contents* of the lintian overrides

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * AVPacket functions for libavcodec
3
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
4
 *
5
 
 * This file is part of FFmpeg.
 
5
 * This file is part of Libav.
6
6
 *
7
 
 * FFmpeg is free software; you can redistribute it and/or
 
7
 * Libav is free software; you can redistribute it and/or
8
8
 * modify it under the terms of the GNU Lesser General Public
9
9
 * License as published by the Free Software Foundation; either
10
10
 * version 2.1 of the License, or (at your option) any later version.
11
11
 *
12
 
 * FFmpeg is distributed in the hope that it will be useful,
 
12
 * Libav is distributed in the hope that it will be useful,
13
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
15
 * Lesser General Public License for more details.
16
16
 *
17
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
 
18
 * License along with Libav; if not, write to the Free Software
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
21
21
 
22
22
#include "avcodec.h"
 
23
#include "libavutil/avassert.h"
23
24
 
24
25
 
25
26
void av_destruct_packet_nofree(AVPacket *pkt)
26
27
{
27
28
    pkt->data = NULL; pkt->size = 0;
 
29
    pkt->side_data       = NULL;
 
30
    pkt->side_data_elems = 0;
28
31
}
29
32
 
30
33
void av_destruct_packet(AVPacket *pkt)
31
34
{
 
35
    int i;
 
36
 
32
37
    av_free(pkt->data);
33
38
    pkt->data = NULL; pkt->size = 0;
 
39
 
 
40
    for (i = 0; i < pkt->side_data_elems; i++)
 
41
        av_free(pkt->side_data[i].data);
 
42
    av_freep(&pkt->side_data);
 
43
    pkt->side_data_elems = 0;
34
44
}
35
45
 
36
46
void av_init_packet(AVPacket *pkt)
43
53
    pkt->flags = 0;
44
54
    pkt->stream_index = 0;
45
55
    pkt->destruct= NULL;
 
56
    pkt->side_data       = NULL;
 
57
    pkt->side_data_elems = 0;
46
58
}
47
59
 
48
60
int av_new_packet(AVPacket *pkt, int size)
71
83
    memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
72
84
}
73
85
 
 
86
int av_grow_packet(AVPacket *pkt, int grow_by)
 
87
{
 
88
    void *new_ptr;
 
89
    av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
 
90
    if (!pkt->size)
 
91
        return av_new_packet(pkt, grow_by);
 
92
    if ((unsigned)grow_by > INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
 
93
        return -1;
 
94
    new_ptr = av_realloc(pkt->data, pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
 
95
    if (!new_ptr)
 
96
        return AVERROR(ENOMEM);
 
97
    pkt->data = new_ptr;
 
98
    pkt->size += grow_by;
 
99
    memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 
100
    return 0;
 
101
}
 
102
 
 
103
#define DUP_DATA(dst, src, size, padding) \
 
104
    do { \
 
105
        void *data; \
 
106
        if (padding) { \
 
107
            if ((unsigned)(size) > (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
 
108
                goto failed_alloc; \
 
109
            data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); \
 
110
        } else { \
 
111
            data = av_malloc(size); \
 
112
        } \
 
113
        if (!data) \
 
114
            goto failed_alloc; \
 
115
        memcpy(data, src, size); \
 
116
        if (padding) \
 
117
            memset((uint8_t*)data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); \
 
118
        dst = data; \
 
119
    } while(0)
 
120
 
74
121
int av_dup_packet(AVPacket *pkt)
75
122
{
 
123
    AVPacket tmp_pkt;
 
124
 
76
125
    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;
 
126
        tmp_pkt = *pkt;
 
127
 
 
128
        pkt->data      = NULL;
 
129
        pkt->side_data = NULL;
 
130
        DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1);
88
131
        pkt->destruct = av_destruct_packet;
 
132
 
 
133
        if (pkt->side_data_elems) {
 
134
            int i;
 
135
 
 
136
            DUP_DATA(pkt->side_data, tmp_pkt.side_data,
 
137
                     pkt->side_data_elems * sizeof(*pkt->side_data), 0);
 
138
            memset(pkt->side_data, 0, pkt->side_data_elems * sizeof(*pkt->side_data));
 
139
            for (i = 0; i < pkt->side_data_elems; i++) {
 
140
                DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
 
141
                         pkt->side_data[i].size, 1);
 
142
            }
 
143
        }
89
144
    }
90
145
    return 0;
 
146
failed_alloc:
 
147
    av_destruct_packet(pkt);
 
148
    return AVERROR(ENOMEM);
91
149
}
92
150
 
93
151
void av_free_packet(AVPacket *pkt)
95
153
    if (pkt) {
96
154
        if (pkt->destruct) pkt->destruct(pkt);
97
155
        pkt->data = NULL; pkt->size = 0;
98
 
    }
 
156
        pkt->side_data       = NULL;
 
157
        pkt->side_data_elems = 0;
 
158
    }
 
159
}
 
160
 
 
161
uint8_t* av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
 
162
                                 int size)
 
163
{
 
164
    int elems = pkt->side_data_elems;
 
165
 
 
166
    if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
 
167
        return NULL;
 
168
    if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
 
169
        return NULL;
 
170
 
 
171
    pkt->side_data = av_realloc(pkt->side_data, (elems + 1) * sizeof(*pkt->side_data));
 
172
    if (!pkt->side_data)
 
173
        return NULL;
 
174
 
 
175
    pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
 
176
    if (!pkt->side_data[elems].data)
 
177
        return NULL;
 
178
    pkt->side_data[elems].size = size;
 
179
    pkt->side_data[elems].type = type;
 
180
    pkt->side_data_elems++;
 
181
 
 
182
    return pkt->side_data[elems].data;
 
183
}
 
184
 
 
185
uint8_t* av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
 
186
                                 int *size)
 
187
{
 
188
    int i;
 
189
 
 
190
    for (i = 0; i < pkt->side_data_elems; i++) {
 
191
        if (pkt->side_data[i].type == type) {
 
192
            if (size)
 
193
                *size = pkt->side_data[i].size;
 
194
            return pkt->side_data[i].data;
 
195
        }
 
196
    }
 
197
    return NULL;
99
198
}