target_enc_fuzzer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * Based on target_dec_fuzzer
  21. */
  22. #include "config.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/cpu.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mem.h"
  29. #include "libavcodec/avcodec.h"
  30. #include "libavcodec/bytestream.h"
  31. #include "libavcodec/codec_internal.h"
  32. #include "libavformat/avformat.h"
  33. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  34. extern const FFCodec * codec_list[];
  35. static void error(const char *err)
  36. {
  37. fprintf(stderr, "%s", err);
  38. exit(1);
  39. }
  40. static const FFCodec *c = NULL;
  41. // Ensure we don't loop forever
  42. const uint32_t maxiteration = 8096;
  43. static int encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt)
  44. {
  45. int ret;
  46. ret = avcodec_send_frame(enc_ctx, frame);
  47. if (ret < 0)
  48. return ret;
  49. while (ret >= 0) {
  50. ret = avcodec_receive_packet(enc_ctx, pkt);
  51. if (ret == AVERROR(EAGAIN)) {
  52. return 0;
  53. } else if (ret < 0) {
  54. return ret;
  55. }
  56. av_packet_unref(pkt);
  57. }
  58. av_assert0(0);
  59. }
  60. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  61. uint64_t maxpixels_per_frame = 512 * 512;
  62. uint64_t maxpixels;
  63. const uint8_t *end = data + size;
  64. uint32_t it = 0;
  65. uint64_t nb_samples = 0;
  66. AVDictionary *opts = NULL;
  67. if (!c) {
  68. #define ENCODER_SYMBOL0(CODEC) ff_##CODEC##_encoder
  69. #define ENCODER_SYMBOL(CODEC) ENCODER_SYMBOL0(CODEC)
  70. extern FFCodec ENCODER_SYMBOL(FFMPEG_ENCODER);
  71. codec_list[0] = &ENCODER_SYMBOL(FFMPEG_ENCODER);
  72. c = &ENCODER_SYMBOL(FFMPEG_ENCODER);
  73. av_log_set_level(AV_LOG_PANIC);
  74. }
  75. if (c->p.type != AVMEDIA_TYPE_VIDEO)
  76. return 0;
  77. maxpixels = maxpixels_per_frame * maxiteration;
  78. maxpixels_per_frame = FFMIN(maxpixels_per_frame , maxpixels);
  79. AVCodecContext* ctx = avcodec_alloc_context3(&c->p);
  80. if (!ctx)
  81. error("Failed memory allocation");
  82. if (ctx->max_pixels == 0 || ctx->max_pixels > maxpixels_per_frame)
  83. ctx->max_pixels = maxpixels_per_frame; //To reduce false positive OOM and hangs
  84. ctx->pix_fmt = AV_PIX_FMT_YUV420P;
  85. if (size > 1024) {
  86. GetByteContext gbc;
  87. int flags;
  88. int64_t flags64;
  89. size -= 1024;
  90. bytestream2_init(&gbc, data + size, 1024);
  91. ctx->width = bytestream2_get_le32(&gbc) & 0xFFFF;
  92. ctx->height = bytestream2_get_le32(&gbc) & 0xFFFF;
  93. ctx->bit_rate = bytestream2_get_le64(&gbc);
  94. ctx->gop_size = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  95. ctx->max_b_frames = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  96. ctx->time_base.num = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  97. ctx->time_base.den = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  98. ctx->framerate.num = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  99. ctx->framerate.den = bytestream2_get_le32(&gbc) & 0x7FFFFFFF;
  100. flags = bytestream2_get_byte(&gbc);
  101. if (flags & 2)
  102. ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
  103. if (flags & 0x40)
  104. av_force_cpu_flags(0);
  105. flags64 = bytestream2_get_le64(&gbc);
  106. if (c->p.pix_fmts) {
  107. int npixfmts = 0;
  108. while (c->p.pix_fmts[npixfmts++] != AV_PIX_FMT_NONE)
  109. ;
  110. ctx->pix_fmt = c->p.pix_fmts[bytestream2_get_byte(&gbc) % npixfmts];
  111. }
  112. switch (c->p.id) {
  113. case AV_CODEC_ID_FFV1:{
  114. int coder = bytestream2_get_byte(&gbc)&3;
  115. if (coder == 3) coder = -2;
  116. av_dict_set_int(&opts, "coder", coder, 0);
  117. av_dict_set_int(&opts, "context", bytestream2_get_byte(&gbc)&1, 0);
  118. av_dict_set_int(&opts, "slicecrc", bytestream2_get_byte(&gbc)&1, 0);
  119. break;}
  120. }
  121. }
  122. if (ctx->width == 0 || av_image_check_size(ctx->width, ctx->height, 0, ctx))
  123. ctx->width = ctx->height = 64;
  124. int res = avcodec_open2(ctx, &c->p, &opts);
  125. if (res < 0) {
  126. avcodec_free_context(&ctx);
  127. av_dict_free(&opts);
  128. return 0; // Failure of avcodec_open2() does not imply that a issue was found
  129. }
  130. AVFrame *frame = av_frame_alloc();
  131. AVPacket *avpkt = av_packet_alloc();
  132. if (!frame || !avpkt)
  133. error("Failed memory allocation");
  134. frame->format = ctx->pix_fmt;
  135. frame->width = ctx->width;
  136. frame->height = ctx->height;
  137. while (data < end && it < maxiteration) {
  138. res = av_frame_get_buffer(frame, 0);
  139. if (res < 0)
  140. error("Failed av_frame_get_buffer");
  141. for (int i=0; i<FF_ARRAY_ELEMS(frame->buf); i++) {
  142. if (frame->buf[i]) {
  143. int buf_size = FFMIN(end-data, frame->buf[i]->size);
  144. memcpy(frame->buf[i]->data, data, buf_size);
  145. memset(frame->buf[i]->data + buf_size, 0, frame->buf[i]->size - buf_size);
  146. data += buf_size;
  147. }
  148. }
  149. frame->pts = nb_samples;
  150. res = encode(ctx, frame, avpkt);
  151. if (res < 0)
  152. break;
  153. it++;
  154. for (int i=0; i<FF_ARRAY_ELEMS(frame->buf); i++)
  155. av_buffer_unref(&frame->buf[i]);
  156. av_packet_unref(avpkt);
  157. }
  158. encode(ctx, NULL, avpkt);
  159. av_packet_unref(avpkt);
  160. // fprintf(stderr, "frames encoded: %"PRId64", iterations: %d\n", nb_samples , it);
  161. av_frame_free(&frame);
  162. avcodec_free_context(&ctx);
  163. av_packet_free(&avpkt);
  164. av_dict_free(&opts);
  165. return 0;
  166. }