MPD: lib/ffmpeg/Buffer.hxx Source File

MPD 0.20.18

Buffer.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FFMPEG_BUFFER_HXX
21 #define MPD_FFMPEG_BUFFER_HXX
22
23 extern "C" {
24 #include <libavutil/mem.h>
25
26 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(52, 18, 0)
27 #define HAVE_AV_FAST_MALLOC
28 #else
29 #include <libavcodec/avcodec.h>
30 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 25, 0)
31 #define HAVE_AV_FAST_MALLOC
32 #endif
33 #endif
34 }
35
36 #include <stddef.h>
37
38 /* suppress the ffmpeg compatibility macro */
39 #ifdef SampleFormat
40 #undef SampleFormat
41 #endif
42
43 class FfmpegBuffer {
44 void *data;
45 unsigned size;
46
47 public:
48 FfmpegBuffer():data(nullptr), size(0) {}
49
50 ~FfmpegBuffer() {
51 av_free(data);
52 }
53
54 gcc_malloc
55 void *Get(size_t min_size) {
56 #ifdef HAVE_AV_FAST_MALLOC
57 av_fast_malloc(&data, &size, min_size);
58 #else
59 void *new_data = av_fast_realloc(data, &size, min_size);
60 if (new_data == nullptr)
61 return AVERROR(ENOMEM);
62 data = new_data;
63 #endif
64 return data;
65 }
66
67 template<typename T>
68 T *GetT(size_t n) {
69 return (T *)Get(n * sizeof(T));
70 }
71 };
72
73 #endif
FfmpegBuffer::~FfmpegBuffer
~FfmpegBuffer()
Definition: Buffer.hxx:50
FfmpegBuffer
Definition: Buffer.hxx:43
gcc_malloc
#define gcc_malloc
Definition: Compiler.h:112
FfmpegBuffer::FfmpegBuffer
FfmpegBuffer()
Definition: Buffer.hxx:48
FfmpegBuffer::GetT
T * GetT(size_t n)
Definition: Buffer.hxx:68
FfmpegBuffer::Get
gcc_malloc void * Get(size_t min_size)
Definition: Buffer.hxx:55