FFmpeg
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"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 
30 #include "libavcodec/bytestream.h"
31 
33 
34 #define SWR_CH_MAX 32
35 
36 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
37 
38 static const enum AVSampleFormat formats[] = {
49 };
50 
51 static const AVChannelLayout layouts[]={
68 };
69 
70 int 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;
83  int in_sample_nb;
84  int out_sample_nb = size;
85  int count;
86  int ret;
87 
88  if (size > 128) {
89  GetByteContext gbc;
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);
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",
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 
148  av_freep(&out_data);
149 
150 end:
151  swr_free(&swr);
152 
153  return 0;
154 }
formats
formats
Definition: signature.h:47
av_force_cpu_flags
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
Definition: cpu.c:79
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
opt.h
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:387
SwrContext::in_sample_rate
int in_sample_rate
input sample rate
Definition: swresample_internal.h:105
GetByteContext
Definition: bytestream.h:33
int64_t
long long int64_t
Definition: coverity.c:34
av_samples_fill_arrays
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
AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_2_2
Definition: channel_layout.h:394
SwrContext::out_sample_rate
int out_sample_rate
output sample rate
Definition: swresample_internal.h:106
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
SwrContext::out_ch_layout
AVChannelLayout out_ch_layout
output channel layout
Definition: swresample_internal.h:104
data
const char data[16]
Definition: mxf.c:148
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AV_CHANNEL_LAYOUT_7POINT1_WIDE
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE
Definition: channel_layout.h:410
SwrContext::out_sample_fmt
enum AVSampleFormat out_sample_fmt
output sample format
Definition: swresample_internal.h:101
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:390
swr_convert
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:719
avassert.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:140
av_channel_layout_describe
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.
Definition: channel_layout.c:648
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:392
AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_7POINT1
Definition: channel_layout.h:409
intreadwrite.h
AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:398
av_get_sample_fmt_name
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:51
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
SwrContext::in_ch_layout
AVChannelLayout in_ch_layout
input channel layout
Definition: swresample_internal.h:103
NULL
#define NULL
Definition: coverity.c:32
swresample.h
AV_CHANNEL_LAYOUT_22POINT2
#define AV_CHANNEL_LAYOUT_22POINT2
Definition: channel_layout.h:422
AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK
#define AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK
Definition: channel_layout.h:412
swr_alloc_set_opts2
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:40
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
cpu.h
size
int size
Definition: twinvq_data.h:10344
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:121
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: target_swr_fuzzer.c:70
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_QUAD
Definition: channel_layout.h:395
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
SWR_CH_MAX
#define SWR_CH_MAX
Definition: target_swr_fuzzer.c:34
ret
ret
Definition: filter_design.txt:187
SwrContext::in_sample_fmt
enum AVSampleFormat in_sample_fmt
input sample format
Definition: swresample_internal.h:99
AV_CHANNEL_LAYOUT_7POINT0
#define AV_CHANNEL_LAYOUT_7POINT0
Definition: channel_layout.h:407
AV_CHANNEL_LAYOUT_2_1
#define AV_CHANNEL_LAYOUT_2_1
Definition: channel_layout.h:389
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:67
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:444
mem.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:386
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
bytestream.h
AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:399
imgutils.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_5POINT0
Definition: channel_layout.h:396
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
avstring.h
AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_5POINT1
Definition: channel_layout.h:397
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
layouts
static const AVChannelLayout layouts[]
Definition: target_swr_fuzzer.c:51
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60