Esempio n. 1
0
int multi_dgemm_type::operator()(size_t index, size_t size)
{
  LIBXSTREAM_CHECK_CONDITION(ready() && (index + size) <= m_host_data->size());

  if (0 < size) {
    if (0 == demux()) {
      // This manual synchronization prevents multiple threads from queuing work into the *same* stream (at the same time).
      // This is only needed if the stream was created without demux support in order to rely on manual synchronization.
      LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_stream_lock(m_stream));
    }
    const size_t i0 = m_host_data->idata()[index], i1 = m_host_data->idata()[index+size];
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_memcpy_h2d(m_host_data->adata() + i0, m_adata, sizeof(double) * (i1 - i0), m_stream));
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_memcpy_h2d(m_host_data->bdata() + i0, m_bdata, sizeof(double) * (i1 - i0), m_stream));
    // transferring cdata is part of the benchmark; since it is all zeros we could do better with libxstream_memset_zero
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_memcpy_h2d(m_host_data->cdata() + i0, m_cdata, sizeof(double) * (i1 - i0), m_stream));
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_memcpy_h2d(m_host_data->idata() + index, m_idata, sizeof(size_t) * size, m_stream));
#if defined(LIBXSTREAM_DEBUG)
    size_t n = 0;
    LIBXSTREAM_ASSERT(LIBXSTREAM_ERROR_NONE == libxstream_fn_nargs(m_signature, &n) && 6 == n);
#endif
    const size_t nn = i1 - m_host_data->idata()[index+size-1];
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_fn_input(m_signature, 0, &size, libxstream_map_to_type(size), 0, 0));
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_fn_input(m_signature, 1,   &nn, libxstream_map_to_type(nn  ), 0, 0));
    LIBXSTREAM_ASSERT(LIBXSTREAM_ERROR_NONE == libxstream_get_arity(m_signature, &n) && 6 == n);
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_fn_call(m_host_data->process(), m_signature, m_stream, LIBXSTREAM_CALL_DEFAULT));
    LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_memcpy_d2h(m_cdata, m_host_data->cdata() + i0, sizeof(double) * (i1 - i0), m_stream));
    if (0 == demux()) {
      LIBXSTREAM_CHECK_CALL_ASSERT(libxstream_stream_unlock(m_stream));
    }
  }

  return LIBXSTREAM_ERROR_NONE;
}
Esempio n. 2
0
static int
p_seprint(Msg *m)
{
	Hdr *h;
	int len;

	if(m->pe - m->ps < IP6HDR)
		return -1;
	h = (Hdr*)m->ps;

	demux(p_mux, h->proto, h->proto, m, &dump);

	/* truncate the message if there's extra */
	len = NetS(h->length) + IP6HDR;
	if(len < m->pe - m->ps)
		m->pe = m->ps + len;

	m->p = seprint(m->p, m->e, "s=%I d=%I ttl=%3d pr=%d ln=%d",
			h->src, h->dst,
			h->ttl,
			h->proto,
			NetS(h->length)
			);

	v6hdr_seprint(m);

	return 0;
}
Esempio n. 3
0
static int
p_seprint(Msg *m)
{
	Hdr h;
	int len;

	len = parsehdr(&h, m->ps, m->pe);
	if(len < 0)
		return -1;
	m->ps += len;

	demux(p_mux, h.proto, h.proto, m, &dump);

	m->p = seprint(m->p, m->e, "version=%d proto=%#x flags=%#.4ux", h.version, h.proto, h.flags);
	if(h.flags&GRE_chksum)
		m->p = seprint(m->p, m->e, " checksum=%#.4ux", h.chksum);
	if(h.flags&GRE_key)
		m->p = seprint(m->p, m->e, " key=%#.8ulx", h.key);
	if(h.flags&GRE_seq)
		m->p = seprint(m->p, m->e, " seq=%#.8ulx", h.seq);
	if(h.flags&GRE_ack)
		m->p = seprint(m->p, m->e, " ack=%#.8ulx", h.ack);
	if(h.flags&GRE_routing)
		m->p = seprint(m->p, m->e, " offset=%#x haverouting", h.offset);
	if(h.version == 0)
		m->p = seprint(m->p, m->e, " recursion=%u", (h.flags&GRE_recur)>>8);

	return 0;
}
Esempio n. 4
0
static int
p_seprint(Msg *m)
{
	int f, len;
	Hdr *h;

	if(m->pe - m->ps < IPHDR)
		return -1;
	h = (Hdr*)m->ps;

	/* next protocol, just dump unless this is the first fragment */
	m->pr = &dump;
	f = NetS(h->frag);
	if((f & ~(IP_DF|IP_MF)) == 0)
		demux(p_mux, h->proto, h->proto, m, &dump);

	/* truncate the message if there's extra */
	len = NetS(h->length);
	if(len < m->pe - m->ps)
		m->pe = m->ps + len;

	/* next header */
	m->ps += (h->vihl  &0xf) << 2;

	m->p = seprint(m->p, m->e, "s=%V d=%V id=%4.4ux frag=%4.4ux ttl=%3d pr=%d ln=%d",
		h->src, h->dst, NetS(h->id), NetS(h->frag), h->ttl, h->proto,
		NetS(h->length));
	return 0;
}
Esempio n. 5
0
static int
p_seprint(Msg *m)
{
	Hdr *h;
	int len;

	if(m->pe - m->ps < EAPOLHDR)
		return -1;

	h = (Hdr*)m->ps;

	/* len does not include header */
	m->ps += EAPOLHDR;

	/* truncate the message if there's extra */
	len = NetS(h->len);
	if(m->ps + len < m->pe)
		m->pe = m->ps + len;
	else if(m->ps+len > m->pe)
		return -1;

	/* next protocol  depending on type*/
	demux(p_mux, h->type, h->type, m, &dump);

	m->p = seprint(m->p, m->e, "type=%s version=%1d datalen=%1d",
			op(h->type), h->vi, len);
	return 0;
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
	char *param;

	if (argc < 2)
	{
#ifdef _WIN32
		param = strrchr(argv[0], '\\');
#else
		param = strrchr(argv[0], '/');
#endif
		printf("%s <file>\n", param == NULL? argv[0] : param + 1);
		return 0;
	}

	param = argv[argc - 1];
	if (param[0] == '"')
	{
		//remove double quote
		++param;
		param[strlen(param) - 1] = '\0';
	}

	if (demux(param))
		printf("success\n");
	else
		printf("failed\n");
	return 0;
}
Esempio n. 7
0
File: bootp.c Progetto: aahud/harvey
static int
p_seprint(Msg *m)
{
	Hdr *h;
	uint32_t x;

	h = (Hdr*)m->ps;

	if(m->pe < (uint8_t*)h->sname)
		return -1;

	/* point past data */
	m->ps = h->optdata;

	/* next protocol */
	m->pr = nil;
	if(m->pe >= (uint8_t*)h->optdata){
		x = NetL(h->optmagic);
		demux(p_mux, x, x, m, &dump);
	}

	m->p = seprint(m->p, m->e, "t=%s ht=%d hl=%d hp=%d xid=%x sec=%d fl=%4.4x ca=%V ya=%V sa=%V ga=%V cha=%E magic=%lx",
		op(h->op), h->htype, h->hlen, h->hops,
		NetL(h->xid), NetS(h->secs), NetS(h->flags),
		h->ciaddr, h->yiaddr, h->siaddr, h->giaddr, h->chaddr,
		(uint32_t)NetL(h->optmagic));
	if(m->pe > (uint8_t*)h->sname && *h->sname)
		m->p = seprint(m->p, m->e, " snam=%s", h->sname);
	if(m->pe > (uint8_t*)h->file && *h->file)
		m->p = seprint(m->p, m->e, " file=%s", h->file);
	return 0;
}
Esempio n. 8
0
static void ts_loop (void)
{
#define PACKETS (BUFFER_SIZE / 188)
    uint8_t * buf;
    uint8_t * data;
    uint8_t * end;
    int packets;
    int i;
    int pid;

    do {
	packets = fread (buffer, 188, PACKETS, in_file);
	for (i = 0; i < packets; i++) {
	    buf = buffer + i * 188;
	    end = buf + 188;
	    if (buf[0] != 0x47) {
		fprintf (stderr, "bad sync byte\n");
		exit (1);
	    }
	    pid = ((buf[1] << 8) + buf[2]) & 0x1fff;
	    if (pid != demux_pid)
		continue;
	    data = buf + 4;
	    if (buf[3] & 0x20) {	/* buf contains an adaptation field */
		data = buf + 5 + buf[4];
		if (data > end)
		    continue;
	    }
	    if (buf[3] & 0x10)
		demux (data, end, (buf[1] & 0x40) ? DEMUX_PAYLOAD_START : 0);
	}
    } while (packets == PACKETS);
}
Esempio n. 9
0
/**@ingroup gen_demux
 *
 */
int work(void **inp, void **out) {
	int i;
	int rcv_len, out_len;

	rcv_len=get_input_samples(0);
	moddebug("%d samples recevied.\n",rcv_len);
	if (!rcv_len)
		return 0;

	modinfo_msg("received %d bytes\n",rcv_len);
	if ((rcv_len - sum_special_output_lengths) % (nof_outputs-nof_special_outputs)) {
/*		moderror_msg("Received length %d is not multiple of the number of output interfaces %d\n",
				rcv_len,nof_outputs);
*/		return 0;
	}

	out_len = (rcv_len - sum_special_output_lengths) / (nof_outputs-nof_special_outputs);
	for (i=0;i<nof_outputs;i++) {
		if (special_output_length[i]) {
			output_lengths[i] = special_output_length[i];
		} else {
			output_lengths[i] = out_len;
		}
		set_output_samples(i,output_lengths[i]);
		if (!out[i]) {
			moderror_msg("output itf %d not ready\n",out[i]);
			return -1;
		}
	}

	demux(inp[0],out,output_lengths,output_padding_pre,
			output_padding_post,nof_outputs,input_sample_sz);

	return 0;
}
Esempio n. 10
0
static void ps_loop (void)
{
    uint8_t * end;

    do {
	end = buffer + fread (buffer, 1, BUFFER_SIZE, in_file);
	if (demux (buffer, end, 0))
	    break;	/* hit program_end_code */
    } while (end == buffer + BUFFER_SIZE);
}
Esempio n. 11
0
int main(int argc, char* argv[]) {
	if (argc < 4) {
		printf("missing some argv\n");
		return -1;
	}
	const char *in_filename = argv[1];
	const char *out_filename_v = argv[2];
	const char *out_filename_a = argv[3];
	int ret = demux(in_filename, out_filename_v, out_filename_a);
	return 0;
}
Esempio n. 12
0
static int
p_seprint(Msg *m)
{
	Hdr *h;

	if(m->pe - m->ps < HDR)
		return -1;

	h = (Hdr*)m->ps;
	m->ps += HDR;

	/* next protocol  depending on type*/
	demux(p_mux, h->desc, h->desc, m, &dump);

	m->p = seprint(m->p, m->e, "desc=%s", op(h->desc));
	return 0;
}
Esempio n. 13
0
File: aoe.c Progetto: bhanug/harvey
static int
p_seprint(Msg *m)
{
	Hdr *h;

	if(m->pe - m->ps < Hsize)
		return 0;

	h = (Hdr*)m->ps;
	m->ps += Hsize;

	demux(p_mux, h->cmd, h->cmd, m, &dump);

	m->p = seprint(m->p, m->e, "ver=%d flag=%4b err=%d %d.%d cmd=%ux tag=%ux",
		h->verflags >> 4, h->verflags & 0xf, h->error, NetS(h->major),
		h->minor, h->cmd, NetL(h->tag));
	return 0;
}
Esempio n. 14
0
FFMPEGVideoWidget::FFMPEGVideoWidget(QWidget *parent) :
    QGLWidget(QGLFormat(QGL::SampleBuffers), parent),
    first_frame(true)
{
    setFixedSize(320, 240);
    setAutoFillBackground(false);
    //Set up the demuxer
    m_demuxer = new Demuxer(QString(PIPENAME));
    m_ffmpeg = new FFMPEGWrapper(39082, m_demuxer);
    m_ffmpeg_thread = new QThread(this);
    m_ffmpeg->moveToThread(m_ffmpeg_thread);
    connect(m_ffmpeg_thread, SIGNAL(started()), m_ffmpeg, SLOT(demux()));
    connect(m_ffmpeg, SIGNAL(finished()), m_ffmpeg_thread, SLOT(quit()));

    connect(m_demuxer, SIGNAL(newImage(QImage *)), this, SLOT(addImage(QImage *)));

    //Set up the timer
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(timerTick()));

}
Esempio n. 15
0
int main(int argc, char **argv) {
	int ret = 0;

	if (argc != 4 && argc != 5) {
		fprintf(stderr,
				"usage: %s [-refcount=<old|new_norefcount|new_refcount>] "
						"input_file video_output_file audio_output_file\n"
						"API example program to show how to read frames from an input file.\n"
						"This program reads frames from a file, decodes them, and writes decoded\n"
						"video frames to a rawvideo file named video_output_file, and decoded\n"
						"audio frames to a rawaudio file named audio_output_file.\n\n"
						"If the -refcount option is specified, the program use the\n"
						"reference counting frame system which allows keeping a copy of\n"
						"the data for longer than one decode call. If unset, it's using\n"
						"the classic old method.\n"
						"\n", argv[0]);
		exit(1);
	}
//	if (argc == 5) {
//		const char *mode = argv[1] + strlen("-refcount=");
//		if (!strcmp(mode, "old"))
//			api_mode = API_MODE_OLD;
//		else if (!strcmp(mode, "new_norefcount"))
//			api_mode = API_MODE_NEW_API_NO_REF_COUNT;
//		else if (!strcmp(mode, "new_refcount"))
//			api_mode = API_MODE_NEW_API_REF_COUNT;
//		else {
//			fprintf(stderr, "unknow mode '%s'\n", mode);
//			exit(1);
//		}
//		argv++;
//	}
	const char *src_file = argv[1];
	const char *video_dst_file = argv[2];
	const char *audio_dst_file = argv[3];

	ret = demux(src_file, video_dst_file, audio_dst_file);

	return ret < 0;
}
Esempio n. 16
0
File: ppp.c Progetto: 99years/plan9
static int
p_seprint(Msg *m)
{
	int proto;
	int len;

	len = m->pe - m->ps;
	if(len < 3)
		return -1;

	if(m->ps[0] == PPP_addr && m->ps[1] == PPP_ctl)
		m->ps += 2;

	proto = *m->ps++;
	if((proto&1) == 0)
		proto = (proto<<8) | *m->ps++;
	
	m->p = seprint(m->p, m->e, "pr=%ud len=%d", proto, len);
	demux(p_mux, proto, proto, m, &dump);

	return 0;
}
Esempio n. 17
0
File: il.c Progetto: 99years/plan9
static int
p_seprint(Msg *m)
{
	Hdr *h;
	int dport, sport;

	if(m->pe - m->ps < ILLEN)
		return -1;
	h = (Hdr*)m->ps;
	m->ps += ILLEN;

	dport = NetS(h->dport);
	sport = NetS(h->sport);
	demux(p_mux, sport, dport, m, &dump);

	m->p = seprint(m->p, m->e, "s=%d d=%d t=%s id=%lud ack=%lud spec=%d ck=%4.4ux ln=%d",
			sport, dport, pkttype(h->type),
			(ulong)NetL(h->id), (ulong)NetL(h->ack),
			h->spec,
			NetS(h->sum), NetS(h->len));
	return 0;
}
Esempio n. 18
0
File: udp.c Progetto: bhanug/harvey
static int
p_seprint(Msg *m)
{
	Hdr *h;
	int dport, sport;


	if(m->pe - m->ps < UDPLEN)
		return -1;
	h = (Hdr*)m->ps;
	m->ps += UDPLEN;

	/* next protocol */
	sport = NetS(h->sport);
	dport = NetS(h->dport);
	demux(p_mux, sport, dport, m, defproto);
	defproto = &dump;

	m->p = seprint(m->p, m->e, "s=%d d=%d ck=%4.4ux ln=%4d",
			NetS(h->sport), dport,
			NetS(h->cksum), NetS(h->len));
	return 0;
}
Esempio n. 19
0
static void ts_loop (void)
{
    uint8_t * buf;
    uint8_t * nextbuf;
    uint8_t * data;
    uint8_t * end;
    int pid;

    buf = buffer;
    while (1) {
	end = buf + fread (buf, 1, buffer + BUFFER_SIZE - buf, in_file);
	buf = buffer;
	for (; (nextbuf = buf + 188) <= end; buf = nextbuf) {
	    if (*buf != 0x47) {
		fprintf (stderr, "bad sync byte\n");
		nextbuf = buf + 1;
		continue;
	    }
	    pid = ((buf[1] << 8) + buf[2]) & 0x1fff;
	    if (pid != demux_pid)
		continue;
	    data = buf + 4;
	    if (buf[3] & 0x20) {	/* buf contains an adaptation field */
		data = buf + 5 + buf[4];
		if (data > nextbuf)
		    continue;
	    }
	    if (buf[3] & 0x10)
		demux (data, nextbuf,
		       (buf[1] & 0x40) ? DEMUX_PAYLOAD_START : 0);
	}
	if (end != buffer + BUFFER_SIZE)
	    break;
	memcpy (buffer, buf, end - buf);
	buf = buffer + (end - buf);
    }
}
Esempio n. 20
0
static int
p_seprint(Msg *m)
{
	char *s, *t;
	Hdr *h;

	if(m->pe - m->ps < Hsize)
		return 0;

	h = (Hdr*)m->ps;
	m->ps += Hsize;

	demux(p_mux, h->cmd, h->cmd, m, &dump);

	s = "unk";
	if(h->cmd < nelem(ctab))
		s = ctab[h->cmd];
	t = "unk";
	if(h->err < nelem(etab))
		s = etab[h->err];
	m->p = seprint(m->p, m->e, "cmd=%d %s err=%d %s cnt=%d\n",
		h->cmd, s, h->err, t, h->cnt);
	return 0;
}
Esempio n. 21
0
 static constexpr auto apply(F f, G g) {
     return domain(f) == domain(g) && all_of(domain(f), demux(equal)(f, g));
 }
Esempio n. 22
0
/// <pre>
/// Example:
///              small(0)    medium(1)     large(2)
///          +-------------+-------------+-------------+ 
/// vol= 0   |  1,3,5      |  8          | 11, 9       |
/// vol= 1   |  6,10       |  15,16      | 2, 4, 17    |
///          +-------------+-------------+-------------+ 
/// </pre>
int CTestIdMux::Run(void)
{
    {{
    CTestObjDeMux obj_demux;
    unsigned coord[2]; 
    obj_demux.GetCoordinates(0, coord);
    assert(coord[0] == 1 && coord[1] == 2);
    }}

    TIdDeMux demux(2);

    demux.InitDim(0, 2);
    demux.InitDim(1, 3);

    TIdDeMux::TDimVector& dv0 = demux.PutDimVector(0);
    TIdDeMux::TDimVector& dv1 = demux.PutDimVector(1);

    {{
    TIdDeMux::TDimensionalPoint pt(2);
    pt[0] = 1; pt[1] = 2;
    demux.SetCoordinates(17, pt);
    }}

    dv0[0]->set(1);
    dv0[0]->set(3);
    dv0[0]->set(5);
    dv0[0]->set(8);
    dv0[0]->set(11);
    dv0[0]->set(9);

    dv0[1]->set(6);
    dv0[1]->set(10);
    dv0[1]->set(15);
    dv0[1]->set(16);
    dv0[1]->set(2);
    dv0[1]->set(4);

    dv1[0]->set(1);
    dv1[0]->set(3);
    dv1[0]->set(5);
    dv1[0]->set(6);
    dv1[0]->set(10);

    dv1[1]->set(8);
    dv1[1]->set(15);
    dv1[1]->set(16);

    dv1[2]->set(11);
    dv1[2]->set(9);
    dv1[2]->set(2);
    dv1[2]->set(4);


    bool found;

    TIdDeMux::TDimensionalPoint pt;
    found = demux.GetCoordinates(5, &pt);
    assert(found);
    assert(pt.size() == 2);
    assert(pt[0] == 0);
    assert(pt[1] == 0);

    
    found = demux.GetCoordinates(15, &pt);
    assert(found);
    assert(pt.size() == 2);
    assert(pt[0] == 1 && pt[1] == 1);

    found = demux.GetCoordinates(11, &pt);
    assert(found);
    assert(pt.size() == 2);
    assert(pt[0] == 0 && pt[1] == 2);

    found = demux.GetCoordinates(2, &pt);
    assert(found);
    assert(pt.size() == 2);
    assert(pt[0] == 1 && pt[1] == 2);

    found = demux.GetCoordinates(17, &pt);
    assert(found);
    assert(pt.size() == 2);
    assert(pt[0] == 1 && pt[1] == 2);


    found = demux.GetCoordinates(200, &pt);
    assert(!found);



    return 0;
}
Esempio n. 23
0
/* Play a list of audio files. */
int
main(int argc, char **argv) {
	int		errorStatus = 0;
	int		i;
	int		c;
	int		cnt;
	int		file_type;
	int		rem;
	int		outsiz;
	int		tsize;
	int		len;
	int		err;
	int		ifd;
	int		stdinseen;
	int		regular;
	int		swapBytes;
	int		frame;
	char		*outbuf;
	caddr_t		mapaddr;
	struct stat	st;
	char		*cp;
	char		ctldev[MAXPATHLEN];

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/* Get the program name */
	prog = strrchr(argv[0], '/');
	if (prog == NULL)
		prog = argv[0];
	else
		prog++;
	Stdin = MGET("(stdin)");

	/* Check AUDIODEV environment for audio device name */
	if (cp = getenv("AUDIODEV")) {
		Audio_dev = cp;
	}

	/* Parse the command line arguments */
	err = 0;
	while ((i = getopt(argc, argv, prog_opts)) != EOF) {
		switch (i) {
			case 'v':
				if (parse_unsigned(optarg, &Volume, "-v")) {
					err++;
				} else if (Volume > MAX_GAIN) {
					Error(stderr, MGET("%s: invalid value "
					    "for -v\n"), prog);
					err++;
				}
				break;
			case 'd':
				Audio_dev = optarg;
				break;
			case 'V':
				Verbose = TRUE;
				break;
			case 'E':
				Errdetect = TRUE;
				break;
			case 'i':
				Immediate = TRUE;
				break;
			case '?':
				usage();
		/*NOTREACHED*/
		}
	}
	if (err > 0)
		exit(1);

	argc -= optind;		/* update arg pointers */
	argv += optind;

	/* Validate and open the audio device */
	err = stat(Audio_dev, &st);
	if (err < 0) {
		Error(stderr, MGET("%s: cannot stat "), prog);
		perror(Audio_dev);
		exit(1);
	}
	if (!S_ISCHR(st.st_mode)) {
		Error(stderr, MGET("%s: %s is not an audio device\n"), prog,
		    Audio_dev);
		exit(1);
	}

	/* This should probably use audio_cntl instead of open_audio */
	if ((argc <= 0) && isatty(fileno(stdin))) {
		Error(stderr, MGET("%s: No files and stdin is a tty.\n"), prog);
		exit(1);
	}

	/* Check on the -i status now. */
	Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK);
	if ((Audio_fd < 0) && (errno == EBUSY)) {
		if (Immediate) {
			Error(stderr, MGET("%s: %s is busy\n"), prog,
			    Audio_dev);
			exit(1);
		}
	}
	(void) close(Audio_fd);
	Audio_fd = -1;

	/* Try to open the control device and save the current format */
	(void) snprintf(ctldev, sizeof (ctldev), "%sctl", Audio_dev);
	Audio_ctlfd = open(ctldev, O_RDWR);
	if (Audio_ctlfd >= 0) {
		/*
		 * wait for the device to become available then get the
		 * controls. We want to save the format that is left when the
		 * device is in a quiescent state. So wait until then.
		 */
		Audio_fd = open(Audio_dev, O_WRONLY);
		(void) close(Audio_fd);
		Audio_fd = -1;
		if (audio_get_play_config(Audio_ctlfd, &Save_hdr)
		    != AUDIO_SUCCESS) {
			(void) close(Audio_ctlfd);
			Audio_ctlfd = -1;
		}
	}

	/* store AUDIOPATH so we don't keep doing getenv() */
	Audio_path = getenv("AUDIOPATH");

	/* Set up SIGINT handler to flush output */
	(void) signal(SIGINT, sigint);

	/* Set the endian nature of the machine. */
	if ((ulong_t)1 != htonl((ulong_t)1)) {
		NetEndian = FALSE;
	}

	/* If no filenames, read stdin */
	stdinseen = FALSE;
	if (argc <= 0) {
		Ifile = Stdin;
	} else {
		Ifile = *argv++;
		argc--;
	}

	/* Loop through all filenames */
	do {
		/* Interpret "-" filename to mean stdin */
		if (strcmp(Ifile, "-") == 0)
			Ifile = Stdin;

		if (Ifile == Stdin) {
			if (stdinseen) {
				Error(stderr,
				    MGET("%s: stdin already processed\n"),
				    prog);
				goto nextfile;
			}
			stdinseen = TRUE;
			ifd = fileno(stdin);
		} else {
			if ((ifd = path_open(Ifile, O_RDONLY, 0, Audio_path))
			    < 0) {
				Error(stderr, MGET("%s: cannot open "), prog);
				perror(Ifile);
				errorStatus++;
				goto nextfile;
			}
		}

		/* Check to make sure this is an audio file */
		err = audio_read_filehdr(ifd, &File_hdr, &file_type,
		    (char *)NULL, 0);
		if (err != AUDIO_SUCCESS) {
			Error(stderr,
			    MGET("%s: %s is not a valid audio file\n"),
			    prog, Ifile);
			errorStatus++;
			goto closeinput;
		}

		/* If G.72X adpcm, set flags for conversion */
		if ((File_hdr.encoding == AUDIO_ENCODING_G721) &&
		    (File_hdr.samples_per_unit == 2) &&
		    (File_hdr.bytes_per_unit == 1)) {
			Decode = AUDIO_ENCODING_G721;
			File_hdr.encoding = AUDIO_ENCODING_ULAW;
			File_hdr.samples_per_unit = 1;
			File_hdr.bytes_per_unit = 1;
			adpcm_state = (struct audio_g72x_state *)malloc
			    (sizeof (*adpcm_state) * File_hdr.channels);
			for (i = 0; i < File_hdr.channels; i++) {
				g721_init_state(&adpcm_state[i]);
			}
		} else if ((File_hdr.encoding == AUDIO_ENCODING_G723) &&
		    (File_hdr.samples_per_unit == 8) &&
		    (File_hdr.bytes_per_unit == 3)) {
			Decode = AUDIO_ENCODING_G723;
			File_hdr.encoding = AUDIO_ENCODING_ULAW;
			File_hdr.samples_per_unit = 1;
			File_hdr.bytes_per_unit = 1;
			adpcm_state = (struct audio_g72x_state *)malloc
			    (sizeof (*adpcm_state) * File_hdr.channels);
			for (i = 0; i < File_hdr.channels; i++) {
				g723_init_state(&adpcm_state[i]);
			}
		} else {
			Decode = AUDIO_ENCODING_NONE;
		}

		/* Check the device configuration */
		open_audio();
		if (audio_cmp_hdr(&Dev_hdr, &File_hdr) != 0) {
			/*
			 * The device does not match the input file.
			 * Wait for any old output to drain, then attempt
			 * to reconfigure the audio device to match the
			 * input data.
			 */
			if (audio_drain(Audio_fd, FALSE) != AUDIO_SUCCESS) {
				/* Flush any remaining audio */
				(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);

				Error(stderr, MGET("%s: "), prog);
				perror(MGET("AUDIO_DRAIN error"));
				exit(1);
			}

			/* Flush any remaining audio */
			(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);

			if (!reconfig()) {
				errorStatus++;
				goto closeinput;
			}
		}


		/* try to do the mmaping - for regular files only ... */
		err = fstat(ifd, &st);
		if (err < 0) {
			Error(stderr, MGET("%s: cannot stat "), prog);
			perror(Ifile);
			exit(1);
		}
		regular = (S_ISREG(st.st_mode));


		/* If regular file, map it.  Else, allocate a buffer */
		mapaddr = 0;

		/*
		 * This should compare to MAP_FAILED not -1, can't
		 * find MAP_FAILED
		 */
		if (regular && ((mapaddr = mmap(0, st.st_size, PROT_READ,
		    MAP_SHARED, ifd, 0)) != MAP_FAILED)) {

			(void) madvise(mapaddr, st.st_size, MADV_SEQUENTIAL);

			/* Skip the file header and set the proper size */
			cnt = lseek(ifd, 0, SEEK_CUR);
			if (cnt < 0) {
			    perror("lseek");
			    exit(1);
			}
			inbuf = (unsigned char *) mapaddr + cnt;
			len = cnt = st.st_size - cnt;
		} else {		/* Not a regular file, or map failed */

			/* mark is so. */
			mapaddr = 0;

			/* Allocate buffer to hold 10 seconds of data */
			cnt = BUFFER_LEN * File_hdr.sample_rate *
			    File_hdr.bytes_per_unit * File_hdr.channels;
			if (bufsiz != cnt) {
				if (buf != NULL) {
					(void) free(buf);
				}
				buf = (unsigned char *) malloc(cnt);
				if (buf == NULL) {
					Error(stderr,
					    MGET("%s: couldn't allocate %dK "
					    "buf\n"), prog, bufsiz / 1000);
					exit(1);
				}
				inbuf = buf;
				bufsiz = cnt;
			}
		}

		/* Set buffer sizes and pointers for conversion, if any */
		switch (Decode) {
		default:
		case AUDIO_ENCODING_NONE:
			insiz = bufsiz;
			outbuf = (char *)buf;
			break;
		case AUDIO_ENCODING_G721:
			insiz = ADPCM_SIZE / 2;
			outbuf = (char *)adpcm_buf;
			initmux(1, 2);
			break;
		case AUDIO_ENCODING_G723:
			insiz = (ADPCM_SIZE * 3) / 8;
			outbuf = (char *)adpcm_buf;
			initmux(3, 8);
			break;
		}

		/*
		 * 8-bit audio isn't a problem, however 16-bit audio is.
		 * If the file is an endian that is different from the machine
		 * then the bytes will need to be swapped.
		 *
		 * Note: Because the G.72X conversions produce 8bit output,
		 * they don't require a byte swap before display and so
		 * this scheme works just fine. If a conversion is added
		 * that produces a 16 bit result and therefore requires
		 * byte swapping before output, then a mechanism
		 * for chaining the two conversions will have to be built.
		 *
		 * Note: The following if() could be simplified, but then
		 * it gets to be very hard to read. So it's left as is.
		 */

		if (File_hdr.bytes_per_unit == 2 &&
		    ((!NetEndian && file_type == FILE_AIFF) ||
		    (!NetEndian && file_type == FILE_AU) ||
		    (NetEndian && file_type == FILE_WAV))) {
			swapBytes = TRUE;
		} else {
			swapBytes = FALSE;
		}

		if (swapBytes) {
			/* Read in interal number of sample frames. */
			frame = File_hdr.bytes_per_unit * File_hdr.channels;
			insiz = (SWAP_SIZE / frame) * frame;
			/* make the output buffer  the swap buffer. */
			outbuf = (char *)swap_buf;
		}

		/*
		 * At this point, we're all ready to copy the data.
		 */
		if (mapaddr == 0) { /* Not mmapped, do it a buffer at a time. */
			inbuf = buf;
			frame = File_hdr.bytes_per_unit * File_hdr.channels;
			rem = 0;
			while ((cnt = read(ifd, inbuf+rem, insiz-rem)) >= 0) {
				/*
				 * We need to ensure only an integral number of
				 * samples is ever written to the audio device.
				 */
				cnt = cnt + rem;
				rem = cnt % frame;
				cnt = cnt - rem;

				/*
				 * If decoding adpcm, or swapping bytes do it
				 * now.
				 *
				 * We treat the swapping like a separate
				 * encoding here because the G.72X encodings
				 * decode to single byte output samples. If
				 * another encoding is added and it produces
				 * multi-byte output samples this will have to
				 * be changed.
				 */
				if (Decode == AUDIO_ENCODING_G721) {
				    outsiz = 0;
				    demux(1, cnt / File_hdr.channels);
				    for (c = 0; c < File_hdr.channels; c++) {
					err = g721_decode(in_ch_data[c],
					    cnt / File_hdr.channels,
					    &File_hdr,
					    (void*)out_ch_data[c],
					    &tsize,
					    &adpcm_state[c]);
					outsiz = outsiz + tsize;
					if (err != AUDIO_SUCCESS) {
					    Error(stderr, MGET(
						"%s: error decoding g721\n"),
						prog);
						errorStatus++;
						break;
					}
				    }
				    mux(outbuf);
				    cnt = outsiz;
				} else if (Decode == AUDIO_ENCODING_G723) {
				    outsiz = 0;
				    demux(3, cnt / File_hdr.channels);
				    for (c = 0; c < File_hdr.channels; c++) {
					err = g723_decode(in_ch_data[c],
					    cnt / File_hdr.channels,
					    &File_hdr,
					    (void*)out_ch_data[c],
					    &tsize,
					    &adpcm_state[c]);
					outsiz = outsiz + tsize;
					if (err != AUDIO_SUCCESS) {
					    Error(stderr, MGET(
						"%s: error decoding g723\n"),
						prog);
					    errorStatus++;
					    break;
					}
				    }
				    mux(outbuf);
				    cnt = outsiz;
				} else if (swapBytes) {
					swab((char *)inbuf, outbuf, cnt);
				}

				/* If input EOF, write an eof marker */
				err = write(Audio_fd, outbuf, cnt);

				if (err < 0) {
					perror("write");
					errorStatus++;
					break;
				} else if (err != cnt) {
					Error(stderr,
					    MGET("%s: output error: "), prog);
					perror("");
					errorStatus++;
					break;
				}
				if (cnt == 0) {
					break;
				}
				/* Move remainder to the front of the buffer */
				if (rem != 0) {
					(void *)memcpy(inbuf, inbuf + cnt, rem);
				}

			}
			if (cnt < 0) {
				Error(stderr, MGET("%s: error reading "), prog);
				perror(Ifile);
				errorStatus++;
			}
		} else {	/* We're mmaped */
			if ((Decode != AUDIO_ENCODING_NONE) || swapBytes) {

				/* Transform data if we have to. */
				for (i = 0; i <= len; i += cnt) {
					cnt = insiz;
					if ((i + cnt) > len) {
						cnt = len - i;
					}
					if (Decode == AUDIO_ENCODING_G721) {
					    outsiz = 0;
					    demux(1, cnt / File_hdr.channels);
					    for (c = 0; c < File_hdr.channels;
						c++) {
						err = g721_decode(
						    in_ch_data[c],
						    cnt / File_hdr.channels,
						    &File_hdr,
						    (void*)out_ch_data[c],
						    &tsize,
						    &adpcm_state[c]);
						outsiz = outsiz + tsize;
						if (err != AUDIO_SUCCESS) {
						    Error(stderr, MGET(
							"%s: error decoding "
							"g721\n"), prog);
						    errorStatus++;
						    break;
						}
					    }
					    mux(outbuf);
					} else if
					    (Decode == AUDIO_ENCODING_G723) {
						outsiz = 0;
						demux(3,
						    cnt / File_hdr.channels);
						for (c = 0;
							c < File_hdr.channels;
							c++) {
						    err = g723_decode(
							in_ch_data[c],
							cnt /
							    File_hdr.channels,
							&File_hdr,
							(void*)out_ch_data[c],
							&tsize,
							&adpcm_state[c]);
						    outsiz = outsiz + tsize;
						    if (err != AUDIO_SUCCESS) {
							Error(stderr, MGET(
							    "%s: error "
							    "decoding g723\n"),
							    prog);
							errorStatus++;
							break;
						    }
						}
						mux(outbuf);
					} else if (swapBytes) {
						swab((char *)inbuf, outbuf,
						    cnt);
						outsiz = cnt;
					}
					inbuf += cnt;

					/* If input EOF, write an eof marker */
					err = write(Audio_fd, (char *)outbuf,
					    outsiz);
					if (err < 0) {
						perror("write");
						errorStatus++;
					} else if (outsiz == 0) {
						break;
					}

				}
			} else {
				/* write the whole thing at once!  */
				err = write(Audio_fd, inbuf, len);
				if (err < 0) {
					perror("write");
					errorStatus++;
				}
				if (err != len) {
					Error(stderr,
					    MGET("%s: output error: "), prog);
					perror("");
					errorStatus++;
				}
				err = write(Audio_fd, inbuf, 0);
				if (err < 0) {
					perror("write");
					errorStatus++;
				}
			}
		}

		/* Free memory if decoding ADPCM */
		switch (Decode) {
		case AUDIO_ENCODING_G721:
		case AUDIO_ENCODING_G723:
			freemux();
			break;
		default:
			break;
		}

closeinput:;
		if (mapaddr != 0)
			(void) munmap(mapaddr, st.st_size);
		(void) close(ifd);		/* close input file */
		if (Errdetect) {
			cnt = 0;
			audio_set_play_error(Audio_fd, (unsigned int *)&cnt);
			if (cnt) {
				Error(stderr,
				    MGET("%s: output underflow in %s\n"),
				    Ifile, prog);
				errorStatus++;
			}
		}
nextfile:;
	} while ((argc > 0) && (argc--, (Ifile = *argv++) != NULL));

	/*
	 * Though drain is implicit on close(), it's performed here
	 * to ensure that the volume is reset after all output is complete.
	 */
	(void) audio_drain(Audio_fd, FALSE);

	/* Flush any remaining audio */
	(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);

	if (Volume != INT_MAX)
		(void) audio_set_play_gain(Audio_fd, &Savevol);
	if ((Audio_ctlfd >= 0) && (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0)) {
		(void) audio_set_play_config(Audio_fd, &Save_hdr);
	}
	(void) close(Audio_fd);			/* close output */
	return (errorStatus);
}