2019-11-30 20:57:44 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
2020-11-15 03:14:24 +02:00
|
|
|
#include "libavutil/opt.h"
|
2019-11-30 20:57:44 +02:00
|
|
|
|
|
|
|
#include "libavcodec/avcodec.h"
|
2021-06-11 23:35:33 +02:00
|
|
|
#include "libavcodec/bsf.h"
|
2020-04-07 16:49:28 +02:00
|
|
|
#include "libavcodec/bsf_internal.h"
|
2019-11-30 20:57:44 +02:00
|
|
|
#include "libavcodec/bytestream.h"
|
|
|
|
#include "libavcodec/internal.h"
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
|
|
|
|
|
|
|
static void error(const char *err)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-09-06 19:38:09 +02:00
|
|
|
static const AVBitStreamFilter *f = NULL;
|
2019-11-30 20:57:44 +02:00
|
|
|
|
|
|
|
static const uint64_t FUZZ_TAG = 0x4741542D5A5A5546ULL;
|
|
|
|
|
|
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
|
|
const uint64_t fuzz_tag = FUZZ_TAG;
|
|
|
|
const uint8_t *last = data;
|
|
|
|
const uint8_t *end = data + size;
|
|
|
|
AVBSFContext *bsf = NULL;
|
2022-02-24 02:38:30 +02:00
|
|
|
AVPacket *pkt;
|
2019-11-30 20:57:44 +02:00
|
|
|
uint64_t keyframes = 0;
|
2020-11-15 03:11:17 +02:00
|
|
|
uint64_t flushpattern = -1;
|
2019-11-30 20:57:44 +02:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
#ifdef FFMPEG_BSF
|
|
|
|
#define BSF_SYMBOL0(BSF) ff_##BSF##_bsf
|
|
|
|
#define BSF_SYMBOL(BSF) BSF_SYMBOL0(BSF)
|
2021-09-06 19:38:09 +02:00
|
|
|
extern const AVBitStreamFilter BSF_SYMBOL(FFMPEG_BSF);
|
2019-11-30 20:57:44 +02:00
|
|
|
f = &BSF_SYMBOL(FFMPEG_BSF);
|
|
|
|
#endif
|
|
|
|
av_log_set_level(AV_LOG_PANIC);
|
|
|
|
}
|
|
|
|
|
2021-09-06 17:04:31 +02:00
|
|
|
res = f ? av_bsf_alloc(f, &bsf) : av_bsf_get_null_filter(&bsf);
|
2019-11-30 20:57:44 +02:00
|
|
|
if (res < 0)
|
|
|
|
error("Failed memory allocation");
|
2021-09-06 17:04:31 +02:00
|
|
|
f = bsf->filter;
|
2019-11-30 20:57:44 +02:00
|
|
|
|
|
|
|
if (size > 1024) {
|
|
|
|
GetByteContext gbc;
|
|
|
|
int extradata_size;
|
2020-11-15 03:14:24 +02:00
|
|
|
int flags;
|
2019-11-30 20:57:44 +02:00
|
|
|
size -= 1024;
|
|
|
|
bytestream2_init(&gbc, data + size, 1024);
|
|
|
|
bsf->par_in->width = bytestream2_get_le32(&gbc);
|
|
|
|
bsf->par_in->height = bytestream2_get_le32(&gbc);
|
|
|
|
bsf->par_in->bit_rate = bytestream2_get_le64(&gbc);
|
|
|
|
bsf->par_in->bits_per_coded_sample = bytestream2_get_le32(&gbc);
|
|
|
|
|
|
|
|
if (f->codec_ids) {
|
|
|
|
int i, id;
|
|
|
|
for (i = 0; f->codec_ids[i] != AV_CODEC_ID_NONE; i++);
|
|
|
|
id = f->codec_ids[bytestream2_get_byte(&gbc) % i];
|
|
|
|
bsf->par_in->codec_id = id;
|
|
|
|
bsf->par_in->codec_tag = bytestream2_get_le32(&gbc);
|
|
|
|
}
|
|
|
|
|
|
|
|
extradata_size = bytestream2_get_le32(&gbc);
|
|
|
|
|
|
|
|
bsf->par_in->sample_rate = bytestream2_get_le32(&gbc);
|
|
|
|
bsf->par_in->channels = (unsigned)bytestream2_get_le32(&gbc) % FF_SANE_NB_CHANNELS;
|
|
|
|
bsf->par_in->block_align = bytestream2_get_le32(&gbc);
|
|
|
|
keyframes = bytestream2_get_le64(&gbc);
|
2020-11-15 03:11:17 +02:00
|
|
|
flushpattern = bytestream2_get_le64(&gbc);
|
2020-11-15 03:14:24 +02:00
|
|
|
flags = bytestream2_get_byte(&gbc);
|
|
|
|
|
|
|
|
if (flags & 0x20) {
|
|
|
|
if (!strcmp(f->name, "av1_metadata"))
|
|
|
|
av_opt_set_int(bsf->priv_data, "td", bytestream2_get_byte(&gbc) % 3, 0);
|
|
|
|
else if (!strcmp(f->name, "h264_metadata") || !strcmp(f->name, "h265_metadata"))
|
|
|
|
av_opt_set_int(bsf->priv_data, "aud", bytestream2_get_byte(&gbc) % 3, 0);
|
|
|
|
else if (!strcmp(f->name, "extract_extradata"))
|
|
|
|
av_opt_set_int(bsf->priv_data, "remove", bytestream2_get_byte(&gbc) & 1, 0);
|
|
|
|
}
|
2019-11-30 20:57:44 +02:00
|
|
|
|
|
|
|
if (extradata_size < size) {
|
|
|
|
bsf->par_in->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (bsf->par_in->extradata) {
|
|
|
|
bsf->par_in->extradata_size = extradata_size;
|
|
|
|
size -= bsf->par_in->extradata_size;
|
|
|
|
memcpy(bsf->par_in->extradata, data + size, bsf->par_in->extradata_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (av_image_check_size(bsf->par_in->width, bsf->par_in->height, 0, bsf))
|
|
|
|
bsf->par_in->width = bsf->par_in->height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = av_bsf_init(bsf);
|
|
|
|
if (res < 0) {
|
|
|
|
av_bsf_free(&bsf);
|
|
|
|
return 0; // Failure of av_bsf_init() does not imply that a issue was found
|
|
|
|
}
|
|
|
|
|
2022-02-24 02:38:30 +02:00
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt)
|
2021-01-31 18:22:43 +02:00
|
|
|
error("Failed memory allocation");
|
|
|
|
|
2019-11-30 20:57:44 +02:00
|
|
|
while (data < end) {
|
|
|
|
// Search for the TAG
|
|
|
|
while (data + sizeof(fuzz_tag) < end) {
|
|
|
|
if (data[0] == (fuzz_tag & 0xFF) && AV_RN64(data) == fuzz_tag)
|
|
|
|
break;
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
if (data + sizeof(fuzz_tag) > end)
|
|
|
|
data = end;
|
|
|
|
|
2022-02-24 02:38:30 +02:00
|
|
|
res = av_new_packet(pkt, data - last);
|
2019-11-30 20:57:44 +02:00
|
|
|
if (res < 0)
|
|
|
|
error("Failed memory allocation");
|
2022-02-24 02:38:30 +02:00
|
|
|
memcpy(pkt->data, last, data - last);
|
|
|
|
pkt->flags = (keyframes & 1) * AV_PKT_FLAG_DISCARD + (!!(keyframes & 2)) * AV_PKT_FLAG_KEY;
|
2019-11-30 20:57:44 +02:00
|
|
|
keyframes = (keyframes >> 2) + (keyframes<<62);
|
|
|
|
data += sizeof(fuzz_tag);
|
|
|
|
last = data;
|
|
|
|
|
2020-11-15 03:11:17 +02:00
|
|
|
if (!(flushpattern & 7))
|
|
|
|
av_bsf_flush(bsf);
|
|
|
|
flushpattern = (flushpattern >> 3) + (flushpattern << 61);
|
|
|
|
|
2022-02-24 02:38:30 +02:00
|
|
|
res = av_bsf_send_packet(bsf, pkt);
|
|
|
|
if (res < 0) {
|
|
|
|
av_packet_unref(pkt);
|
|
|
|
continue;
|
2019-11-30 20:57:44 +02:00
|
|
|
}
|
2022-02-24 02:38:30 +02:00
|
|
|
while (av_bsf_receive_packet(bsf, pkt) >= 0)
|
|
|
|
av_packet_unref(pkt);
|
2019-11-30 20:57:44 +02:00
|
|
|
}
|
|
|
|
|
2022-02-24 02:38:30 +02:00
|
|
|
av_bsf_send_packet(bsf, NULL);
|
|
|
|
while (av_bsf_receive_packet(bsf, pkt) >= 0)
|
|
|
|
av_packet_unref(pkt);
|
2019-11-30 20:57:44 +02:00
|
|
|
|
2022-02-24 02:38:30 +02:00
|
|
|
av_packet_free(&pkt);
|
2019-11-30 20:57:44 +02:00
|
|
|
av_bsf_free(&bsf);
|
|
|
|
return 0;
|
|
|
|
}
|