FFmpeg
Loading...
Searching...
No Matches
target_bsf_fuzzer.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "config.h"
20#include "libavutil/imgutils.h"
21#include "libavutil/mem.h"
22#include "libavutil/opt.h"
23
24#include "libavcodec/avcodec.h"
25#include "libavcodec/bsf.h"
28#include "libavcodec/internal.h"
29
30int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
31
32static void error(const char *err)
33{
34 fprintf(stderr, "%s", err);
35 exit(1);
36}
37
38static const AVBitStreamFilter *f = NULL;
39
40static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
41
42int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
43 const uint64_t fuzz_tag = FUZZ_TAG;
44 const uint8_t *last = data;
45 const uint8_t *end = data + size;
46 AVBSFContext *bsf = NULL;
48 uint64_t keyframes = 0;
49 uint64_t flushpattern = -1;
50 int res;
51
52 if (!f) {
53#ifdef FFMPEG_BSF
54#define BSF_SYMBOL0(BSF) ff_##BSF##_bsf
55#define BSF_SYMBOL(BSF) BSF_SYMBOL0(BSF)
56 extern const AVBitStreamFilter BSF_SYMBOL(FFMPEG_BSF);
57 f = &BSF_SYMBOL(FFMPEG_BSF);
58#endif
60 }
61
62 res = f ? av_bsf_alloc(f, &bsf) : av_bsf_get_null_filter(&bsf);
63 if (res == AVERROR(ENOTSUP))
64 return 0; // multi-input filters cannot be used through av_bsf_alloc()
65 if (res < 0)
66 error("Failed memory allocation");
67 f = bsf->filter;
68
69 if (size > 1024) {
71 int extradata_size;
72 int flags;
73 size -= 1024;
74 bytestream2_init(&gbc, data + size, 1024);
75 bsf->par_in->width = bytestream2_get_le32(&gbc);
76 bsf->par_in->height = bytestream2_get_le32(&gbc);
77 bsf->par_in->bit_rate = bytestream2_get_le64(&gbc);
78 bsf->par_in->bits_per_coded_sample = bytestream2_get_le32(&gbc);
79
80 if (f->codec_ids) {
81 int i, id;
82 for (i = 0; f->codec_ids[i] != AV_CODEC_ID_NONE; i++);
83 id = f->codec_ids[bytestream2_get_byte(&gbc) % i];
84 bsf->par_in->codec_id = id;
85 bsf->par_in->codec_tag = bytestream2_get_le32(&gbc);
86 }
87
88 extradata_size = bytestream2_get_le32(&gbc);
89
90 bsf->par_in->sample_rate = bytestream2_get_le32(&gbc);
91 bsf->par_in->ch_layout.nb_channels = (unsigned)bytestream2_get_le32(&gbc) % FF_SANE_NB_CHANNELS;
92 bsf->par_in->block_align = bytestream2_get_le32(&gbc);
93 keyframes = bytestream2_get_le64(&gbc);
94 flushpattern = bytestream2_get_le64(&gbc);
95 flags = bytestream2_get_byte(&gbc);
96
97 if (flags & 0x20) {
98 if (!strcmp(f->name, "av1_metadata"))
99 av_opt_set_int(bsf->priv_data, "td", bytestream2_get_byte(&gbc) % 3, 0);
100 else if (!strcmp(f->name, "h264_metadata") || !strcmp(f->name, "hevc_metadata") ||
101 !strcmp(f->name, "vvc_metadata"))
102 av_opt_set_int(bsf->priv_data, "aud", bytestream2_get_byte(&gbc) % 3, 0);
103 else if (!strcmp(f->name, "extract_extradata"))
104 av_opt_set_int(bsf->priv_data, "remove", bytestream2_get_byte(&gbc) & 1, 0);
105 }
106
107 if (extradata_size < size) {
109 if (bsf->par_in->extradata) {
110 bsf->par_in->extradata_size = extradata_size;
111 size -= bsf->par_in->extradata_size;
112 memcpy(bsf->par_in->extradata, data + size, bsf->par_in->extradata_size);
113 }
114 }
115 if (av_image_check_size(bsf->par_in->width, bsf->par_in->height, 0, bsf))
116 bsf->par_in->width = bsf->par_in->height = 0;
117 }
118
119 res = av_bsf_init(bsf);
120 if (res < 0) {
121 av_bsf_free(&bsf);
122 return 0; // Failure of av_bsf_init() does not imply that a issue was found
123 }
124
126 if (!pkt)
127 error("Failed memory allocation");
128
129 while (data < end) {
130 // Search for the TAG
131 while (data + sizeof(fuzz_tag) < end) {
132 if (data[0] == (fuzz_tag & 0xFF) && AV_RN64(data) == fuzz_tag)
133 break;
134 data++;
135 }
136 if (data + sizeof(fuzz_tag) > end)
137 data = end;
138
139 res = av_new_packet(pkt, data - last);
140 if (res < 0)
141 error("Failed memory allocation");
142 memcpy(pkt->data, last, data - last);
143 pkt->flags = (keyframes & 1) * AV_PKT_FLAG_DISCARD + (!!(keyframes & 2)) * AV_PKT_FLAG_KEY;
144 keyframes = (keyframes >> 2) + (keyframes<<62);
145 data += sizeof(fuzz_tag);
146 last = data;
147
148 if (!(flushpattern & 7))
149 av_bsf_flush(bsf);
150 flushpattern = (flushpattern >> 3) + (flushpattern << 61);
151
152 res = av_bsf_send_packet(bsf, pkt);
153 if (res < 0) {
155 continue;
156 }
157 while (av_bsf_receive_packet(bsf, pkt) >= 0)
159 }
160
162 while (av_bsf_receive_packet(bsf, pkt) >= 0)
164
166 av_bsf_free(&bsf);
167 return 0;
168}
Libavcodec external API header.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define NULL
Definition coverity.c:32
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:607
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition bsf.c:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
void av_bsf_flush(AVBSFContext *ctx)
Reset the internal bitstream filter state.
Definition bsf.c:188
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition bsf.c:99
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
int av_bsf_get_null_filter(AVBSFContext **bsf)
Get null/pass-through bitstream filter.
Definition bsf.c:551
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition packet.h:664
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_PANIC
Something went really wrong and we will crash now.
Definition log.h:197
void av_log_set_level(int level)
Set the log level.
Definition log.c:476
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition imgutils.c:318
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition opt.c:938
misc image utilities
#define AV_RN64(p)
common internal api header.
#define FF_SANE_NB_CHANNELS
Definition internal.h:37
Memory handling functions.
const char data[16]
Definition mxf.c:149
AVOptions.
The bitstream filter state.
Definition bsf.h:68
void * priv_data
Opaque filter-specific private data.
Definition bsf.h:83
AVCodecParameters * par_in
Parameters of the input stream.
Definition bsf.h:90
const struct AVBitStreamFilter * filter
The bitstream filter this context is an instance of.
Definition bsf.h:77
int nb_channels
Number of channels in this layout.
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
This structure stores compressed data.
Definition packet.h:587
#define av_mallocz(s)
static const uint64_t FUZZ_TAG
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
static void error(const char *err)
int size