Пример #1
0
int
vdvi_encode(u_char *dvi_buf, unsigned int dvi_samples, bitstream_t *bs)
{
        register u_char s1, s2;
        u_char *dvi_end, *dp, t;
        int bytes_used;

        assert(dvi_samples == VDVI_SAMPLES_PER_FRAME);

        /* Worst case is 8 bits per sample -> VDVI_SAMPLES_PER_FRAME */

        dvi_end = dvi_buf + dvi_samples / 2;
        dp      = dvi_buf;
        while (dp != dvi_end) {
                t = *dp;
                s1 = (t & 0xf0) >> 4;
                s2 = (t & 0x0f);
                bs_put(bs, (u_char)dmap[s1], dmap_bits[s1]);
                bs_put(bs, (u_char)dmap[s2], dmap_bits[s2]);
                assert(*dp == t);
                dp ++;
        }
        /* Return number of bytes used */
        bytes_used  = bs_bytes_used(bs);
        return bytes_used;
}
Пример #2
0
/* FIXME */
double baw_put(double spot, double strike, double r, double d, double vol, double expiry) {
	double M = 2.0 * r / (vol * vol);
	double N = 2.0 * (r - d) / (vol * vol);
	double K = 1.0 - exp(-r * expiry);
	double q1 = 0.5 * (-(N - 1) - sqrt((N - 1) * (N - 1) + 4 * M / K));
	double q1inf = 0.5 *(-(N - 1) - sqrt((N - 1) * (N - 1) + 4 * M));
	double Ssinf = strike / (1.0 - 1.0 / q1inf);
	double stddev = vol * sqrt(expiry);
	double h1 = ((r - d) * expiry - 2.0 * stddev) * (strike / (strike - Ssinf));
	double S0 = Ssinf + (strike - Ssinf) * exp(h1);
	double g = 1.0;
	double gprime = 1.0;
	int niters = 0;
	double Si = S0;
	double Ss;
	double pa, pe;

	while (fabs(g) > 0.000001 && fabs(gprime) > 0.000001 && ++niters < 500 && Si > 0.0) {
		double pe = bs_put(Si, strike, r, d, vol, expiry);
		double d1 = (log(Si / strike) + (r - d) * expiry + 0.5 * stddev * stddev) / stddev;

		g = strike - Si - pe +
			(1.0 / q1) * Si * (1.0 - exp(-d * expiry)) * cum_norm(-d1);
		gprime = (1.0 / q1 - 1.0) * (1.0 - exp(-d * expiry)) * cum_norm(-d1) +
			(1.0 / q1) * exp(-d * expiry) * cum_norm(-d1) * (1.0 / stddev);
		Si -= g/gprime;
	}
	Ss = fabs(g) > 0.000001 ? S0 : Si;
	pe = bs_put(spot, strike, r, d, vol, expiry);
	if (spot <= Ss)
		pa = strike - spot;
	else {
		double d1 = (log(Ss / strike) + (r - d) * expiry + 0.5 * stddev * stddev) / stddev;
		double A1 = (-Ss / q1) * (1.0 - exp(-d * expiry) * cum_norm(-d1));

		pa = pe + A1 * pow(spot / Ss, q1);
	}
	return pa > pe ? pa : pe;
}
Пример #3
0
static int beanstalk_put(struct ast_cdr *cdr) {
	struct ast_tm timeresult;
	char strAnswerTime[80] = "";
	char strStartTime[80];
	char strEndTime[80];
	char *cdr_buffer;
	int bs_id;
	int bs_socket;
	struct ast_json *t_cdr_json;

	if (!enablecdr) {
		return 0;
	}

	ast_rwlock_rdlock(&config_lock);
	bs_socket = bs_connect(bs_host, bs_port);

	if (bs_use(bs_socket, bs_tube) != BS_STATUS_OK) {
		ast_log(LOG_ERROR, "Connection to Beanstalk tube %s @ %s:%d had failed", bs_tube, bs_host, bs_port);
		ast_rwlock_unlock(&config_lock);
		return 0;
	}

	ast_localtime(&cdr->start, &timeresult, NULL);
	ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);

	if (cdr->answer.tv_sec) {
		ast_localtime(&cdr->answer, &timeresult, NULL);
		ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
	}

	ast_localtime(&cdr->end, &timeresult, NULL);
	ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);

	ast_rwlock_unlock(&config_lock);

	t_cdr_json = ast_json_pack("{s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:i, s:i, s:s, s:s, s:s, s:s}",
							   "AccountCode", S_OR(cdr->accountcode, ""),
							   "Source", S_OR(cdr->src, ""),
							   "Destination", S_OR(cdr->dst, ""),
							   "DestinationContext", S_OR(cdr->dcontext, ""),
							   "CallerID", S_OR(cdr->clid, ""),
							   "Channel", S_OR(cdr->channel, ""),
							   "DestinationChannel", S_OR(cdr->dstchannel, ""),
							   "LastApplication", S_OR(cdr->lastapp, ""),
							   "LastData", S_OR(cdr->lastdata, ""),
							   "StartTime", S_OR(strStartTime, ""),
							   "AnswerTime", S_OR(strAnswerTime, ""),
							   "EndTime", S_OR(strEndTime, ""),
							   "Duration", cdr->duration,
							   "Billsec", cdr->billsec,
							   "Disposition", S_OR(ast_cdr_disp2str(cdr->disposition), ""),
							   "AMAFlags", S_OR(ast_channel_amaflags2string(cdr->amaflags), ""),
							   "UniqueID", S_OR(cdr->uniqueid, ""),
							   "UserField", S_OR(cdr->userfield, ""));

	cdr_buffer = ast_json_dump_string(t_cdr_json);

	ast_json_unref(t_cdr_json);

	bs_id = bs_put(bs_socket, priority, BEANSTALK_JOB_DELAY, BEANSTALK_JOB_TTR, cdr_buffer, strlen(cdr_buffer));

	if (bs_id > 0) {
		ast_log(LOG_DEBUG, "Successfully created job %d with %s\n", bs_id, cdr_buffer);
	} else {
		ast_log(LOG_ERROR, "CDR job creation failed for %s\n", cdr_buffer);
	}

	bs_disconnect(bs_socket);
	ast_json_free(cdr_buffer);
	return 0;
}