~ubuntu-branches/ubuntu/utopic/libav/utopic

« back to all changes in this revision

Viewing changes to libavutil/mem.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-12-21 15:32:13 UTC
  • mto: (1.2.18)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: package-import@ubuntu.com-20121221153213-fudzrugjzivtv0wp
Tags: upstream-9~beta3
ImportĀ upstreamĀ versionĀ 9~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "config.h"
28
28
 
29
29
#include <limits.h>
 
30
#include <stdint.h>
30
31
#include <stdlib.h>
31
32
#include <string.h>
32
33
#if HAVE_MALLOC_H
34
35
#endif
35
36
 
36
37
#include "avutil.h"
 
38
#include "intreadwrite.h"
37
39
#include "mem.h"
38
40
 
39
 
/* here we can use OS-dependent allocation functions */
40
 
#undef free
41
 
#undef malloc
42
 
#undef realloc
43
 
 
44
41
#ifdef MALLOC_PREFIX
45
42
 
46
43
#define malloc         AV_JOIN(MALLOC_PREFIX, malloc)
177
174
    }
178
175
    return ptr;
179
176
}
 
177
 
 
178
static void fill16(uint8_t *dst, int len)
 
179
{
 
180
    uint32_t v = AV_RN16(dst - 2);
 
181
 
 
182
    v |= v << 16;
 
183
 
 
184
    while (len >= 4) {
 
185
        AV_WN32(dst, v);
 
186
        dst += 4;
 
187
        len -= 4;
 
188
    }
 
189
 
 
190
    while (len--) {
 
191
        *dst = dst[-2];
 
192
        dst++;
 
193
    }
 
194
}
 
195
 
 
196
static void fill24(uint8_t *dst, int len)
 
197
{
 
198
#if HAVE_BIGENDIAN
 
199
    uint32_t v = AV_RB24(dst - 3);
 
200
    uint32_t a = v << 8  | v >> 16;
 
201
    uint32_t b = v << 16 | v >> 8;
 
202
    uint32_t c = v << 24 | v;
 
203
#else
 
204
    uint32_t v = AV_RL24(dst - 3);
 
205
    uint32_t a = v       | v << 24;
 
206
    uint32_t b = v >> 8  | v << 16;
 
207
    uint32_t c = v >> 16 | v << 8;
 
208
#endif
 
209
 
 
210
    while (len >= 12) {
 
211
        AV_WN32(dst,     a);
 
212
        AV_WN32(dst + 4, b);
 
213
        AV_WN32(dst + 8, c);
 
214
        dst += 12;
 
215
        len -= 12;
 
216
    }
 
217
 
 
218
    if (len >= 4) {
 
219
        AV_WN32(dst, a);
 
220
        dst += 4;
 
221
        len -= 4;
 
222
    }
 
223
 
 
224
    if (len >= 4) {
 
225
        AV_WN32(dst, b);
 
226
        dst += 4;
 
227
        len -= 4;
 
228
    }
 
229
 
 
230
    while (len--) {
 
231
        *dst = dst[-3];
 
232
        dst++;
 
233
    }
 
234
}
 
235
 
 
236
static void fill32(uint8_t *dst, int len)
 
237
{
 
238
    uint32_t v = AV_RN32(dst - 4);
 
239
 
 
240
    while (len >= 4) {
 
241
        AV_WN32(dst, v);
 
242
        dst += 4;
 
243
        len -= 4;
 
244
    }
 
245
 
 
246
    while (len--) {
 
247
        *dst = dst[-4];
 
248
        dst++;
 
249
    }
 
250
}
 
251
 
 
252
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
 
253
{
 
254
    const uint8_t *src = &dst[-back];
 
255
    if (back == 1) {
 
256
        memset(dst, *src, cnt);
 
257
    } else if (back == 2) {
 
258
        fill16(dst, cnt);
 
259
    } else if (back == 3) {
 
260
        fill24(dst, cnt);
 
261
    } else if (back == 4) {
 
262
        fill32(dst, cnt);
 
263
    } else {
 
264
        if (cnt >= 16) {
 
265
            int blocklen = back;
 
266
            while (cnt > blocklen) {
 
267
                memcpy(dst, src, blocklen);
 
268
                dst       += blocklen;
 
269
                cnt       -= blocklen;
 
270
                blocklen <<= 1;
 
271
            }
 
272
            memcpy(dst, src, cnt);
 
273
            return;
 
274
        }
 
275
        if (cnt >= 8) {
 
276
            AV_COPY32U(dst,     src);
 
277
            AV_COPY32U(dst + 4, src + 4);
 
278
            src += 8;
 
279
            dst += 8;
 
280
            cnt -= 8;
 
281
        }
 
282
        if (cnt >= 4) {
 
283
            AV_COPY32U(dst, src);
 
284
            src += 4;
 
285
            dst += 4;
 
286
            cnt -= 4;
 
287
        }
 
288
        if (cnt >= 2) {
 
289
            AV_COPY16U(dst, src);
 
290
            src += 2;
 
291
            dst += 2;
 
292
            cnt -= 2;
 
293
        }
 
294
        if (cnt)
 
295
            *dst = *src;
 
296
    }
 
297
}