fuzz-h264.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2023 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <pjlib.h>
  22. #include <pjmedia-codec/h264_packetizer.h>
  23. #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
  24. #define kMinInputLength 10
  25. #define kMaxInputLength 5120
  26. pj_pool_factory *mem;
  27. int h264_unpacketizer(const uint8_t *data, size_t size,
  28. uint8_t *output, size_t output_size)
  29. {
  30. int ret = 0;
  31. pj_pool_t *pool;
  32. pj_status_t status;
  33. pjmedia_h264_packetizer_cfg cfg;
  34. pjmedia_h264_packetizer *pktz;
  35. unsigned bits_pos = 0;
  36. pool = pj_pool_create(mem, "h264_test", 1000, 1000, NULL);
  37. pj_bzero(&cfg, sizeof(cfg));
  38. cfg.mtu = 1500;
  39. cfg.unpack_nal_start = 4;
  40. cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED;
  41. status = pjmedia_h264_packetizer_create(pool, &cfg, &pktz);
  42. if (status == PJ_SUCCESS) {
  43. status = pjmedia_h264_unpacketize(pktz, data, size, output,
  44. output_size, &bits_pos);
  45. }
  46. pj_pool_release(pool);
  47. return ret;
  48. }
  49. extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  50. {
  51. int ret = 0;
  52. uint8_t *data;
  53. uint8_t *output;
  54. size_t output_size;
  55. pj_caching_pool caching_pool;
  56. if (Size < kMinInputLength || Size > kMaxInputLength) {
  57. return 1;
  58. }
  59. /* Add null termination for the data */
  60. data = (uint8_t *)calloc((Size+1), sizeof(uint8_t));
  61. memcpy((void *)data, (void *)Data, Size);
  62. output_size = Size + 32;
  63. output = (uint8_t *)calloc(output_size, sizeof(uint8_t));
  64. /* Init */
  65. pj_init();
  66. pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
  67. pj_log_set_level(0);
  68. mem = &caching_pool.factory;
  69. /* Fuzz */
  70. ret = h264_unpacketizer(data, Size, output, output_size);
  71. free(data);
  72. free(output);
  73. pj_caching_pool_destroy(&caching_pool);
  74. return ret;
  75. }
  76. #else
  77. extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  78. {
  79. PJ_UNUSED_ARG(Data);
  80. PJ_UNUSED_ARG(Size);
  81. return 0;
  82. }
  83. #endif