FFmpeg
Loading...
Searching...
No Matches
target_swr_fuzzer.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Michael Niedermayer <michael-ffmpeg@niedermayer.cc>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "config.h"
22#include "libavutil/avassert.h"
23#include "libavutil/avstring.h"
24#include "libavutil/cpu.h"
25#include "libavutil/imgutils.h"
27#include "libavutil/mem.h"
28#include "libavutil/opt.h"
29
31
33
34#define SWR_CH_MAX 32
35
36int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
37
50
69
70int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
71 struct SwrContext * swr= NULL;
75 int in_sample_rate = 44100;
76 int out_sample_rate = 44100;
77 int in_ch_count, out_ch_count;
78 char in_layout_string[256];
79 char out_layout_string[256];
80 uint8_t * ain[SWR_CH_MAX];
81 uint8_t *aout[SWR_CH_MAX];
82 uint8_t *out_data = NULL;
83 int in_sample_nb;
84 int out_sample_nb = size;
85 int count;
86 int ret;
87
88 if (size > 128) {
90 int64_t flags64;
91
92 size -= 128;
93 bytestream2_init(&gbc, data + size, 128);
94 in_sample_rate = bytestream2_get_le16(&gbc) + 1;
95 out_sample_rate = bytestream2_get_le16(&gbc) + 1;
96 in_sample_fmt = formats[bytestream2_get_byte(&gbc) % FF_ARRAY_ELEMS(formats)];
97 out_sample_fmt = formats[bytestream2_get_byte(&gbc) % FF_ARRAY_ELEMS(formats)];
98 av_channel_layout_copy(& in_ch_layout, &layouts[bytestream2_get_byte(&gbc) % FF_ARRAY_ELEMS(layouts)]);
99 av_channel_layout_copy(&out_ch_layout, &layouts[bytestream2_get_byte(&gbc) % FF_ARRAY_ELEMS(layouts)]);
100
101 out_sample_nb = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
102
103 flags64 = bytestream2_get_le64(&gbc);
104 if (flags64 & 0x10)
106 }
107
108 in_ch_count= in_ch_layout.nb_channels;
109 out_ch_count= out_ch_layout.nb_channels;
110 av_channel_layout_describe(& in_ch_layout, in_layout_string, sizeof( in_layout_string));
111 av_channel_layout_describe(&out_ch_layout, out_layout_string, sizeof(out_layout_string));
112
113 // fprintf(stderr, "%s %d %s -> %s %d %s\n",
114 // av_get_sample_fmt_name( in_sample_fmt), in_sample_rate, in_layout_string,
115 // av_get_sample_fmt_name(out_sample_fmt), out_sample_rate, out_layout_string);
116
119 0, 0) < 0) {
120 fprintf(stderr, "Failed swr_alloc_set_opts2()\n");
121 goto end;
122 }
123
124 if (swr_init(swr) < 0) {
125 fprintf(stderr, "Failed swr_init()\n");
126 goto end;
127 }
128
129 in_sample_nb = size / (in_ch_count * av_get_bytes_per_sample(in_sample_fmt));
130 out_sample_nb = out_sample_nb % (av_rescale(in_sample_nb, 2*out_sample_rate, in_sample_rate) + 1);
131
132 if (in_sample_nb > 1000*1000 || out_sample_nb > 1000*1000)
133 goto end;
134
135 out_data = av_malloc(out_sample_nb * out_ch_count * av_get_bytes_per_sample(out_sample_fmt));
136 if (!out_data)
137 goto end;
138
139 ret = av_samples_fill_arrays(ain , NULL, data, in_ch_count, in_sample_nb, in_sample_fmt, 1);
140 if (ret < 0)
141 goto end;
142 ret = av_samples_fill_arrays(aout, NULL, out_data, out_ch_count, out_sample_nb, out_sample_fmt, 1);
143 if (ret < 0)
144 goto end;
145
146 count = swr_convert(swr, aout, out_sample_nb, (const uint8_t **)ain, in_sample_nb);
147
148end:
149 av_freep(&out_data);
150 swr_free(&swr);
151
152 return 0;
153}
#define SWR_CH_MAX
Definition af_amerge.c:37
simple assert() macros that are a bit more flexible than ISO C assert().
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_7POINT0
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK
#define AV_CHANNEL_LAYOUT_2_1
#define AV_CHANNEL_LAYOUT_22POINT2
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
#define AV_CHANNEL_LAYOUT_7POINT1
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AV_CHANNEL_LAYOUT_QUAD
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition samplefmt.h:63
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition samplefmt.h:59
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition samplefmt.h:57
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
@ AV_SAMPLE_FMT_DBL
double
Definition samplefmt.h:61
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill plane data pointers and linesize for samples with sample format sample_fmt.
Definition samplefmt.c:153
int swr_alloc_set_opts2(struct SwrContext **ps, const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
Allocate SwrContext if needed and set/reset common parameters.
Definition swresample.c:54
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition swresample.c:137
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
Definition swresample.c:725
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition swresample.c:156
misc image utilities
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
Definition cpu.c:81
Memory handling functions.
enum MovChannelLayoutTag * layouts
Definition mov_chan.c:335
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
formats
Definition signature.h:47
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
The libswresample context.
int out_sample_rate
output sample rate
int in_sample_rate
input sample rate
AVChannelLayout out_ch_layout
output channel layout
enum AVSampleFormat in_sample_fmt
input sample format
AVChannelLayout in_ch_layout
input channel layout
enum AVSampleFormat out_sample_fmt
output sample format
libswresample public header
#define av_freep(p)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
int size