target_sws_fuzzer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "config.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/cpu.h"
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/opt.h"
  28. #include "libavcodec/bytestream.h"
  29. #include "libswscale/swscale.h"
  30. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
  31. static void error(const char *err)
  32. {
  33. fprintf(stderr, "%s", err);
  34. exit(1);
  35. }
  36. static int alloc_plane(uint8_t *data[AV_VIDEO_MAX_PLANES], int stride[AV_VIDEO_MAX_PLANES], int w, int h, int format, int *hshift, int *vshift)
  37. {
  38. size_t size[AV_VIDEO_MAX_PLANES];
  39. ptrdiff_t ptrdiff_stride[AV_VIDEO_MAX_PLANES];
  40. int ret = av_image_fill_linesizes(stride, format, w);
  41. if (ret < 0)
  42. return -1;
  43. av_assert0(AV_VIDEO_MAX_PLANES == 4); // Some of the libavutil API has 4 hardcoded so this has undefined behaviour if its not 4
  44. av_pix_fmt_get_chroma_sub_sample(format, hshift, vshift);
  45. for(int p=0; p<AV_VIDEO_MAX_PLANES; p++) {
  46. stride[p] =
  47. ptrdiff_stride[p] = FFALIGN(stride[p], 32);
  48. }
  49. ret = av_image_fill_plane_sizes(size, format, h, ptrdiff_stride);
  50. if (ret < 0)
  51. return ret;
  52. for(int p=0; p<AV_VIDEO_MAX_PLANES; p++) {
  53. if (size[p]) {
  54. data[p] = av_mallocz(size[p] + 32);
  55. if (!data[p])
  56. return -1;
  57. } else
  58. data[p] = NULL;
  59. }
  60. return 0;
  61. }
  62. static void free_plane(uint8_t *data[AV_VIDEO_MAX_PLANES])
  63. {
  64. for(int p=0; p<AV_VIDEO_MAX_PLANES; p++)
  65. av_freep(&data[p]);
  66. }
  67. static void mapres(unsigned *r0, unsigned *r1) {
  68. double d = (double)(*r0*10ll - 9ll*UINT32_MAX) / UINT32_MAX;
  69. double a = exp(d) * 16384 / exp(1) ;
  70. int ai = (int)round(a);
  71. uint64_t maxb = 16384 / ai;
  72. *r0 = ai;
  73. *r1 = 1 + (*r1 * maxb) / UINT32_MAX;
  74. }
  75. int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  76. int srcW= 48, srcH = 48;
  77. int dstW= 48, dstH = 48;
  78. int srcHShift, srcVShift;
  79. int dstHShift, dstVShift;
  80. unsigned flags = 1;
  81. int srcStride[AV_VIDEO_MAX_PLANES] = {0};
  82. int dstStride[AV_VIDEO_MAX_PLANES] = {0};
  83. int ret;
  84. const uint8_t *end = data + size;
  85. enum AVPixelFormat srcFormat = AV_PIX_FMT_YUV420P;
  86. enum AVPixelFormat dstFormat = AV_PIX_FMT_YUV420P;
  87. uint8_t *src[AV_VIDEO_MAX_PLANES] = { 0 };
  88. uint8_t *dst[AV_VIDEO_MAX_PLANES] = { 0 };
  89. struct SwsContext *sws = NULL;
  90. const AVPixFmtDescriptor *desc_src, *desc_dst;
  91. if (size > 128) {
  92. GetByteContext gbc;
  93. int64_t flags64;
  94. size -= 128;
  95. bytestream2_init(&gbc, data + size, 128);
  96. srcW = bytestream2_get_le32(&gbc);
  97. srcH = bytestream2_get_le32(&gbc);
  98. dstW = bytestream2_get_le32(&gbc);
  99. dstH = bytestream2_get_le32(&gbc);
  100. mapres(&srcW, &srcH);
  101. mapres(&dstW, &dstH);
  102. flags = bytestream2_get_le32(&gbc);
  103. unsigned mask = flags & (SWS_POINT |
  104. SWS_AREA |
  105. SWS_BILINEAR |
  106. SWS_FAST_BILINEAR |
  107. SWS_BICUBIC |
  108. SWS_X |
  109. SWS_GAUSS |
  110. SWS_LANCZOS |
  111. SWS_SINC |
  112. SWS_SPLINE |
  113. SWS_BICUBLIN);
  114. mask &= flags;
  115. if (mask && (mask & (mask -1)))
  116. return 0; // multiple scalers are set, not possible
  117. srcFormat = bytestream2_get_le32(&gbc) % AV_PIX_FMT_NB;
  118. dstFormat = bytestream2_get_le32(&gbc) % AV_PIX_FMT_NB;
  119. flags64 = bytestream2_get_le64(&gbc);
  120. if (flags64 & 0x10)
  121. av_force_cpu_flags(0);
  122. if (av_image_check_size(srcW, srcH, srcFormat, NULL) < 0)
  123. srcW = srcH = 23;
  124. if (av_image_check_size(dstW, dstH, dstFormat, NULL) < 0)
  125. dstW = dstH = 23;
  126. //TODO alphablend
  127. }
  128. desc_src = av_pix_fmt_desc_get(srcFormat);
  129. desc_dst = av_pix_fmt_desc_get(dstFormat);
  130. fprintf(stderr, "%d x %d %s -> %d x %d %s\n", srcW, srcH, desc_src->name, dstW, dstH, desc_dst->name);
  131. ret = alloc_plane(src, srcStride, srcW, srcH, srcFormat, &srcHShift, &srcVShift);
  132. if (ret < 0)
  133. goto end;
  134. ret = alloc_plane(dst, dstStride, dstW, dstH, dstFormat, &dstHShift, &dstVShift);
  135. if (ret < 0)
  136. goto end;
  137. for(int p=0; p<AV_VIDEO_MAX_PLANES; p++) {
  138. int psize = srcStride[p] * AV_CEIL_RSHIFT(srcH, (p == 1 || p == 2) ? srcVShift : 0);
  139. if (psize > size)
  140. psize = size;
  141. if (psize) {
  142. memcpy(src[p], data, psize);
  143. data += psize;
  144. size -= psize;
  145. }
  146. }
  147. sws = sws_alloc_context();
  148. if (!sws)
  149. error("Failed sws allocation");
  150. av_opt_set_int(sws, "sws_flags", flags, 0);
  151. av_opt_set_int(sws, "srcw", srcW, 0);
  152. av_opt_set_int(sws, "srch", srcH, 0);
  153. av_opt_set_int(sws, "dstw", dstW, 0);
  154. av_opt_set_int(sws, "dsth", dstH, 0);
  155. av_opt_set_int(sws, "src_format", srcFormat, 0);
  156. av_opt_set_int(sws, "dst_format", dstFormat, 0);
  157. av_opt_set(sws, "alphablend", "none", 0);
  158. ret = sws_init_context(sws, NULL, NULL);
  159. if (ret < 0)
  160. goto end;
  161. //TODO Slices
  162. sws_scale(sws, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);
  163. end:
  164. sws_freeContext(sws);
  165. free_plane(src);
  166. free_plane(dst);
  167. return 0;
  168. }