| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdint.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <pjlib.h> |
| 24 |
#include <pjmedia-codec/h264_packetizer.h> |
| 25 |
#include <pjmedia-codec/h263_packetizer.h> |
| 26 |
#include <pjmedia-codec/vpx_packetizer.h> |
| 27 |
|
| 28 |
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 29 |
|
| 30 |
#define kMinInputLength 10 |
| 31 |
#define kMaxInputLength 5120 |
| 32 |
|
| 33 |
pj_pool_factory *mem; |
| 34 |
static pj_caching_pool caching_pool; |
| 35 |
static int initialized = 0; |
| 36 |
|
| 37 |
|
| 38 |
typedef struct { |
| 39 |
pj_pool_t *pool; |
| 40 |
uint8_t *data; |
| 41 |
size_t size; |
| 42 |
uint8_t *output; |
| 43 |
size_t output_size; |
| 44 |
} codec_test_ctx; |
| 45 |
|
| 46 |
|
| 47 |
static pj_pool_t* create_codec_pool(const char *name) |
| 48 |
{ |
| 49 |
return pj_pool_create(mem, name, 4000, 4000, NULL); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
static pj_uint8_t* alloc_and_copy_buffer(pj_pool_t *pool, const uint8_t *data, size_t size) |
| 54 |
{ |
| 55 |
pj_uint8_t *buffer = (pj_uint8_t *)pj_pool_alloc(pool, size); |
| 56 |
if (buffer) { |
| 57 |
pj_memcpy(buffer, data, size); |
| 58 |
} |
| 59 |
return buffer; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
static int test_h264_codec(int operation, codec_test_ctx *ctx) |
| 64 |
{ |
| 65 |
pjmedia_h264_packetizer_cfg cfg; |
| 66 |
pjmedia_h264_packetizer *pktz; |
| 67 |
unsigned bits_pos = 0; |
| 68 |
|
| 69 |
if (!ctx->pool) return -1; |
| 70 |
|
| 71 |
pj_bzero(&cfg, sizeof(cfg)); |
| 72 |
cfg.mtu = 1500; |
| 73 |
cfg.unpack_nal_start = 4; |
| 74 |
cfg.mode = PJMEDIA_H264_PACKETIZER_MODE_NON_INTERLEAVED; |
| 75 |
|
| 76 |
if (pjmedia_h264_packetizer_create(ctx->pool, &cfg, &pktz) != PJ_SUCCESS) |
| 77 |
return -1; |
| 78 |
|
| 79 |
if (operation == 0) { |
| 80 |
|
| 81 |
pjmedia_h264_unpacketize(pktz, ctx->data, ctx->size, ctx->output, |
| 82 |
ctx->output_size, &bits_pos); |
| 83 |
} else { |
| 84 |
|
| 85 |
pj_uint8_t *buffer = alloc_and_copy_buffer(ctx->pool, ctx->data, ctx->size); |
| 86 |
if (buffer) { |
| 87 |
const pj_uint8_t *payloads[100]; |
| 88 |
pj_size_t payload_lens[100]; |
| 89 |
unsigned payload_cnt = 100; |
| 90 |
pjmedia_h264_packetize(pktz, buffer, ctx->size, &payload_cnt, |
| 91 |
payloads, payload_lens); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
static int test_h263_codec(int operation, codec_test_ctx *ctx) |
| 100 |
{ |
| 101 |
pjmedia_h263_packetizer_cfg cfg; |
| 102 |
pjmedia_h263_packetizer *pktz; |
| 103 |
unsigned bits_pos = 0; |
| 104 |
|
| 105 |
if (!ctx->pool) return -1; |
| 106 |
|
| 107 |
pj_bzero(&cfg, sizeof(cfg)); |
| 108 |
cfg.mtu = 1500; |
| 109 |
cfg.mode = PJMEDIA_H263_PACKETIZER_MODE_RFC4629; |
| 110 |
|
| 111 |
if (pjmedia_h263_packetizer_create(ctx->pool, &cfg, &pktz) != PJ_SUCCESS) |
| 112 |
return -1; |
| 113 |
|
| 114 |
if (operation == 0) { |
| 115 |
|
| 116 |
pjmedia_h263_unpacketize(pktz, ctx->data, ctx->size, ctx->output, |
| 117 |
ctx->output_size, &bits_pos); |
| 118 |
} else { |
| 119 |
|
| 120 |
pj_uint8_t *buffer = alloc_and_copy_buffer(ctx->pool, ctx->data, ctx->size); |
| 121 |
pj_uint8_t *payload_buf = (pj_uint8_t *)pj_pool_alloc(ctx->pool, 1500); |
| 122 |
if (buffer && payload_buf) { |
| 123 |
pj_size_t payload_len = 1500; |
| 124 |
pjmedia_h263_packetize(pktz, buffer, ctx->size, &bits_pos, |
| 125 |
&payload_buf, &payload_len); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
static int test_vpx_codec(int operation, codec_test_ctx *ctx) |
| 134 |
{ |
| 135 |
pjmedia_vpx_packetizer_cfg cfg; |
| 136 |
pjmedia_vpx_packetizer *pktz; |
| 137 |
|
| 138 |
if (!ctx->pool) return -1; |
| 139 |
|
| 140 |
pj_bzero(&cfg, sizeof(cfg)); |
| 141 |
pjmedia_vpx_packetizer_cfg_default(&cfg); |
| 142 |
cfg.mtu = 1500; |
| 143 |
|
| 144 |
if (pjmedia_vpx_packetizer_create(ctx->pool, &cfg, &pktz) != PJ_SUCCESS) |
| 145 |
return -1; |
| 146 |
|
| 147 |
if (operation == 0) { |
| 148 |
|
| 149 |
unsigned desc_len = 0; |
| 150 |
pjmedia_vpx_unpacketize(pktz, ctx->data, ctx->size, &desc_len); |
| 151 |
} else { |
| 152 |
|
| 153 |
if (ctx->size >= 20) { |
| 154 |
pj_uint8_t *payload; |
| 155 |
pj_size_t payload_len; |
| 156 |
unsigned bits_pos = 0; |
| 157 |
payload = (pj_uint8_t *)pj_pool_alloc(ctx->pool, 1500); |
| 158 |
payload_len = 1500; |
| 159 |
pjmedia_vpx_packetize(pktz, ctx->size, &bits_pos, PJ_TRUE, |
| 160 |
&payload, &payload_len); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 168 |
{ |
| 169 |
codec_test_ctx ctx; |
| 170 |
|
| 171 |
if (Size < kMinInputLength || Size > kMaxInputLength) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if (!initialized) { |
| 177 |
pj_init(); |
| 178 |
pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0); |
| 179 |
pj_log_set_level(0); |
| 180 |
mem = &caching_pool.factory; |
| 181 |
initialized = 1; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
ctx.data = (uint8_t *)calloc(Size, sizeof(uint8_t)); |
| 186 |
if (!ctx.data) return 0; |
| 187 |
memcpy((void *)ctx.data, Data, Size); |
| 188 |
ctx.size = Size; |
| 189 |
|
| 190 |
ctx.output_size = Size + 1024; |
| 191 |
ctx.output = (uint8_t *)calloc(ctx.output_size, sizeof(uint8_t)); |
| 192 |
if (!ctx.output) { |
| 193 |
free((void *)ctx.data); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
#define TEST_CODEC(name, func, op) \ |
| 199 |
ctx.pool = create_codec_pool(name); \ |
| 200 |
if (ctx.pool) { \ |
| 201 |
func(op, &ctx); \ |
| 202 |
pj_pool_release(ctx.pool); \ |
| 203 |
} |
| 204 |
|
| 205 |
TEST_CODEC("h264_u", test_h264_codec, 0); |
| 206 |
TEST_CODEC("h264_p", test_h264_codec, 1); |
| 207 |
TEST_CODEC("h263_u", test_h263_codec, 0); |
| 208 |
TEST_CODEC("h263_p", test_h263_codec, 1); |
| 209 |
TEST_CODEC("vpx_u", test_vpx_codec, 0); |
| 210 |
TEST_CODEC("vpx_p", test_vpx_codec, 1); |
| 211 |
|
| 212 |
#undef TEST_CODEC |
| 213 |
|
| 214 |
|
| 215 |
free((void *)ctx.data); |
| 216 |
free(ctx.output); |
| 217 |
|
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
#else |
| 222 |
|
| 223 |
extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
| 224 |
{ |
| 225 |
(void)Data; |
| 226 |
(void)Size; |
| 227 |
return 0; |
| 228 |
} |
| 229 |
#endif |