static void convert_path(String *cap, char *s, Config *c, int maxlen, int reserve) { static char delimiters[] = { '/', '#', '\0' }; char *part, **parts, *used_delim; int i, n; if ((parts = misc_str_split_delimiters(s, delimiters, &used_delim)) == NULL) { err_message_fnc("No enough memory.\n"); return; } n = 0; while (parts[n]) n++; i = 0; while ((part = parts[i])) { int over = RESERVE_LEN + string_length(cap) + strlen(part) + strlen(REPLACE_STR) + reserve / n - maxlen; if (maxlen > 0 && over > 0) { convert_cat(cap, part, c, strlen(part) - over); string_cat(cap, REPLACE_STR); } else { convert_cat(cap, part, c, 0); } if (used_delim[i] != '\0') string_cat_ch(cap, used_delim[i]); i++; } misc_free_str_array(parts); free(used_delim); }
static int get_buffer(AVCodecContext *vcodec_ctx, AVFrame *vcodec_picture) { VideoDecoder *vdec = (VideoDecoder *)vcodec_ctx->opaque; struct videodecoder_avcodec *vdm = (struct videodecoder_avcodec *)vdec->opaque; int width, height; Picture_buffer *buf; /* alignment */ width = (vcodec_ctx->width + 15) & ~15; height = (vcodec_ctx->height + 15) & ~15; if (vcodec_ctx->pix_fmt != PIX_FMT_YUV420P || width != vcodec_ctx->width || height != vcodec_ctx->height) { debug_message_fnc("avcodec: unsupported frame format, DR1 disabled.\n"); vdm->vcodec_ctx->get_buffer = avcodec_default_get_buffer; vdm->vcodec_ctx->reget_buffer = avcodec_default_reget_buffer; vdm->vcodec_ctx->release_buffer = avcodec_default_release_buffer; return avcodec_default_get_buffer(vcodec_ctx, vcodec_picture); } buf = &vdm->picture_buffer[vdm->picture_buffer_count]; if (buf->base == NULL) { int datasize = image_bpl(vdm->p) * image_height(vdm->p); int size = sizeof(struct pic_buf) + datasize; struct pic_buf *pb; if ((buf->base = memory_create()) == NULL) { err_message_fnc("No enough memory for Memory object buf->base.\n"); return -1; } if ((pb = memory_alloc(buf->base, size)) == NULL) { err_message_fnc("Cannot allocate %d bytes. No enough memory for pic_buf.\n", size); return -1; } pb->idx = vdm->picture_buffer_count; pb->mem = buf->base; memset(pb->data, 128, datasize); buf->pb = pb; buf->linesize[0] = image_width(vdm->p); buf->linesize[1] = image_width(vdm->p) >> 1; buf->linesize[2] = image_width(vdm->p) >> 1; }
void * lzma_decoder_create(I7z_stream *st, unsigned char *prop, unsigned int propsize) { ReadStream *rs; LZMA_Decoder *dec; if ((dec = calloc(1, sizeof(*dec))) == NULL) { err_message_fnc("No enough memory.\n"); return NULL; } /* Decode LZMA properties and allocate memory */ if (LzmaDecodeProperties(&dec->state.Properties, prop, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { err_message_fnc("Incorrect stream properties.\n"); return NULL; } if ((dec->state.Probs = (CProb *)malloc(LzmaGetNumProbs(&dec->state.Properties) * sizeof(CProb))) == NULL) { free(dec); err_message_fnc("No enough memory.\n"); return NULL; } if ((dec->state.Dictionary = (unsigned char *)malloc(dec->state.Properties.DictionarySize)) == NULL) { free(dec->state.Probs); free(dec); err_message_fnc("No enough memory.\n"); return NULL; } if ((rs = calloc(1, sizeof(*rs))) == NULL) { err_message_fnc("No enough memory.\n"); return NULL; } rs->InCallback.Read = read_stream; rs->st = st; LzmaDecoderInit(&dec->state); dec->offset = 0; dec->rs = rs; return dec; }
int lzma_decode(void *_dec, unsigned char *output, UINT64 *outsize) { LZMA_Decoder *dec = _dec; UINT64 decoded; UINT32 kBlockSize = 0x100000; int result; decoded = 0; while (decoded < *outsize) { SizeT blocksize = *outsize - decoded; SizeT processed; if (blocksize > kBlockSize) blocksize = kBlockSize; processed = 0; result = LzmaDecode(&dec->state, &dec->rs->InCallback, output + decoded, blocksize, &processed); switch (result) { case 0: break; case LZMA_RESULT_DATA_ERROR: err_message_fnc("Data error.\n"); return LZMA_DATA_ERROR; default: err_message_fnc("LzmaDecode returned %d.\n", result); return UNKNOWN_ERROR; } if (processed == 0) { *outsize = decoded; break; } debug_message_fnc("LzmaDecode() returned %d, processed = %d.\n", result, processed); decoded += processed; dec->offset += processed; } return 0; }
void * copy_decoder_create(I7z_stream *st, unsigned char *prop, unsigned int propsize) { COPY_Decoder *dec; if ((dec = calloc(1, sizeof(*dec))) == NULL) { err_message_fnc("No enough memory.\n"); return NULL; } dec->st = st; dec->offset = 0; return dec; }
static Dictionary * open_dictionary(char *path) { Dictionary *dic; if ((dic = calloc(1, sizeof(Dictionary))) == NULL) { err_message_fnc("No enough memory.\n"); return NULL; } if ((dic->path = strdup(path)) == NULL) return NULL; if ((dic->fd = open(path, O_RDONLY)) == -1) { perror(PROGNAME ": open"); return NULL; } cdb_init(&dic->cdb, dic->fd); pthread_mutex_init(&dic->mutex, NULL); return dic; }
static void translate(char *remote, int port, int family, char *yomi) { int sock; char *sstr; char rbuf[SKKSERV_REQUEST_SIZE]; int read_size; if ((sock = socket_connect(remote, &sstr, port, (char *)SKKSERV_SERVICE, family)) < 0) { err_message_fnc("Cannot make a connection.\n"); return; } snprintf(rbuf, sizeof(rbuf), "1%s ", yomi); write(sock, rbuf, strlen(rbuf)); if ((read_size = read(sock, rbuf, sizeof(rbuf))) > 0) { rbuf[read_size] = '\0'; printf("%s", rbuf); } free(sstr); close(sock); }
static void show_stat(char *remote, int port, int family) { int sock; char *sstr; char rbuf[SKKSERV_REQUEST_SIZE]; if ((sock = socket_connect(remote, &sstr, port, (char *)SKKSERV_SERVICE, family)) < 0) { err_message_fnc("Cannot make a connection.\n"); return; } snprintf(rbuf, sizeof(rbuf), "S\n"); write(sock, rbuf, strlen(rbuf)); if (read(sock, rbuf, sizeof(rbuf)) > 0) { int nconns, nactives; sscanf(rbuf, "S%d:%d", &nconns, &nactives); printf("%d total connections, %d connections active.\n", nconns, nactives); } free(sstr); close(sock); }
static void search_dictionaries(int out, Dlist *dic_list, char *rbuf) { Dlist_data *dd; Hash *word_hash; char word[SKKSERV_WORD_SIZE]; char result[SKKSERV_RESULT_SIZE]; char tmpresult[SKKSERV_RESULT_SIZE]; char *end; int i, p, r, len, rlen, ncandidates; if ((end = strchr(rbuf + 1, ' ')) == NULL) { rbuf[0] = SKKSERV_S_ERROR; write(out, rbuf, strlen(rbuf)); return; } len = end - (rbuf + 1); memcpy(word, rbuf + 1, len); word[len] = '\0'; word_hash = hash_create(HASH_SIZE); rlen = 1; ncandidates = 0; result[0] = SKKSERV_S_FOUND; dlist_iter(dic_list, dd) { Dictionary *dic = dlist_data(dd); pthread_mutex_lock(&dic->mutex); __cdb_findstart(dic); if ((r = __cdb_findnext(dic, word, len)) == -1) { err_message_fnc("cdb_findnext() failed.\n"); if (!ncandidates) { rbuf[0] = SKKSERV_S_ERROR; write(out, rbuf, strlen(rbuf)); } pthread_mutex_unlock(&dic->mutex); hash_destroy(word_hash); return; } debug_message_fnc("word %s, len %d, r = %d\n", word, len, r); if (r) { int dpos = cdb_datapos(&dic->cdb); int dlen = cdb_datalen(&dic->cdb); debug_message_fnc("%s found, r = %d, dpos = %d, dlen = %d.\n", word, r, dpos, dlen); if (rlen + dlen + 2 > SKKSERV_RESULT_SIZE) { err_message_fnc("Truncated: %s\n", word); r = SKKSERV_RESULT_SIZE - rlen - 2; } else { r = dlen; } debug_message_fnc("read %d bytes\n", r); if (cdb_read(&dic->cdb, tmpresult, r, dpos) == -1) { if (!ncandidates) { err_message_fnc("cdb_read() failed.\n"); rbuf[0] = SKKSERV_S_ERROR; write(out, rbuf, strlen(rbuf)); pthread_mutex_unlock(&dic->mutex); hash_destroy(word_hash); return; } else { result[rlen] = '\0'; continue; } } /* Merge */ p = 0; i = 1; while (i < r) { if (tmpresult[i] == '/') { if (i - p - 1 > 0 && hash_define_value(word_hash, tmpresult + p + 1, i - p - 1, (void *)1) == 1) { memcpy(result + rlen, tmpresult + p, i - p); rlen += i - p; ncandidates++; } p = i; } i++; } } pthread_mutex_unlock(&dic->mutex); }
static int prepare_listen(char *sname, char **sstr, int port, char *service, int nbacklogs, int family) { String *s; struct addrinfo hints, *res0, *res; int gs = 0; int opt, gaierr; int try_default_portnum; char *servername; char sbuf[NI_MAXSERV]; char ipbuf[INET6_ADDRSTRLEN]; char selfname[MAXHOSTNAMELEN + 1]; s = string_create(); servername = sname ? sname : strdup("localhost"); gethostname(selfname, MAXHOSTNAMELEN); selfname[MAXHOSTNAMELEN] = '\0'; string_cat(s, selfname); string_cat(s, ":"); if (port > -1) { snprintf(sbuf, sizeof(sbuf), "%d", port); sbuf[sizeof(sbuf) - 1] = '\0'; try_default_portnum = 0; } else { strncpy(sbuf, service, sizeof(sbuf)); try_default_portnum = 1; } for (;;) { memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; res0 = NULL; if ((gaierr = getaddrinfo(servername, sbuf, &hints, &res0))) { if (gaierr == EAI_SERVICE && try_default_portnum) { snprintf(sbuf, sizeof(sbuf), "%d", SKKSERV_PORT); sbuf[sizeof(sbuf) - 1] = '\0'; try_default_portnum = 0; continue; } #ifdef HAVE_GETADDRINFO err_message_fnc("getaddrinfo(): %s(gaierr = %d)\n", gai_strerror(gaierr), gaierr); #endif return -2; } for (res = res0; res; res = res->ai_next) { if (res->ai_family != AF_INET && res->ai_family != AF_INET6) continue; if ((gs = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) continue; ipbuf[0] = '\0'; #ifdef HAVE_GETNAMEINFO if ((gaierr = getnameinfo(res->ai_addr, res->ai_addrlen, ipbuf, sizeof(ipbuf), NULL, 0, NI_NUMERICHOST))) { err_message_fnc("getnameinfo(): %s\n", gai_strerror(gaierr)); if (res0) freeaddrinfo(res0); close(gs); return -3; } #else { struct sockaddr_in *sp_v4 = (struct sockaddr_in *)&res->ai_addr; strncpy(ipbuf, inet_ntoa(sp_v4->sin_addr), sizeof(ipbuf)); } #endif ipbuf[sizeof(ipbuf) - 1] = '\0'; opt = 1; setsockopt(gs, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); if (bind(gs, res->ai_addr, res->ai_addrlen)) { perror(PROGNAME ": bind"); close(gs); gs = -1; continue; } if (listen(gs, nbacklogs)) { perror(PROGNAME ": listen"); close(gs); gs = -1; continue; } string_cat(s, ipbuf); string_cat(s, ":"); break; } if (res0) freeaddrinfo(res0); break; } string_cat(s, " "); *sstr = strdup(string_get(s)); string_destroy(s); return gs; }
DEFINE_LOADER_PLUGIN_LOAD(p, st, vw #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , c #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , priv #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif ) { jas_image_t *ji; jas_stream_t *js; unsigned char *d; char *buf = NULL; int k, cmp[3]; unsigned int i, j; int tlx, tly; int vs, hs; //debug_message("JasPer: load() called\n"); #ifdef IDENTIFY_BEFORE_LOAD { LoaderStatus status; if ((status = identify(p, st, vw, c, priv)) != LOAD_OK) return status; stream_rewind(st); } #endif /* Read whole stream into buffer... */ { char *tmp; int size = 0, len; int bufsize = 65536; for (;;) { if ((tmp = realloc(buf, bufsize)) == NULL) { free(buf); return LOAD_ERROR; } buf = tmp; len = stream_read(st, (unsigned char *)(buf + size), bufsize - size); size += len; if (len < bufsize - size) break; bufsize += 65536; } if ((js = jas_stream_memopen(buf, size)) == NULL) { free(buf); return LOAD_ERROR; } } /* loading... */ if ((ji = jas_image_decode(js, -1, 0)) == NULL) { err_message_fnc("jas_image_decode() failed.\n"); goto error_clear; } /* colorspace conversion */ { jas_cmprof_t *jc; jas_image_t *new_ji; if ((jc = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB)) == NULL) goto error_destroy_free; if ((new_ji = jas_image_chclrspc(ji, jc, JAS_CMXFORM_INTENT_PER)) == NULL) goto error_destroy_free; jas_image_destroy(ji); ji = new_ji; } jas_stream_close(js); free(buf); debug_message("JasPer: jas_image_decode() OK: (%ld,%ld)\n", jas_image_cmptwidth(ji, 0), jas_image_cmptheight(ji, 0)); /* convert to enfle format */ p->bits_per_pixel = 24; p->type = _RGB24; p->depth = 24; cmp[0] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); cmp[1] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); cmp[2] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); /* dimension */ image_width(p) = jas_image_cmptwidth(ji, cmp[0]); image_height(p) = jas_image_cmptheight(ji, cmp[0]); image_left(p) = 0; image_top(p) = 0; image_bpl(p) = image_width(p) * 3; tlx = jas_image_cmpttlx(ji, cmp[0]); tly = jas_image_cmpttly(ji, cmp[0]); vs = jas_image_cmptvstep(ji, cmp[0]); hs = jas_image_cmpthstep(ji, cmp[0]); debug_message("JasPer: tlx %d tly %d vs %d hs %d ncomponents %d\n", tlx, tly, vs, hs, jas_image_numcmpts(ji)); /* memory allocation */ if ((d = memory_alloc(image_image(p), image_bpl(p) * image_height(p))) == NULL) { err_message("No enough memory (%d bytes)\n", image_bpl(p) * image_height(p)); goto error_destroy_free; } for (i = 0; i < image_height(p); i++) { for (j = 0; j < image_width(p); j++) { for (k = 0; k < 3; k++) *d++ = jas_image_readcmptsample(ji, cmp[k], j, i); } } jas_image_destroy(ji); return LOAD_OK; error_destroy_free: jas_image_destroy(ji); error_clear: return LOAD_ERROR; }
static int load_image(Image *p, Stream *st) { unsigned char buf[BUFSIZE], *pp, *d; unsigned int file_size, header_size, image_size, offset_to_image; unsigned short int biPlanes; unsigned int bytes_per_pal; int i, c, c2, y, compress_method; if (stream_read(st, buf, 12) != 12) return 0; file_size = utils_get_little_uint32(&buf[0]); offset_to_image = utils_get_little_uint32(&buf[8]); if (file_size < offset_to_image) return 0; if (!stream_read_little_uint32(st, &header_size)) return 0; if (header_size > 64) return 0; if (stream_read(st, buf, header_size - 4) != (int)(header_size - 4)) return 0; if (header_size >= WIN_BMP_HEADER_SIZE) { image_width(p) = utils_get_little_uint32(&buf[0]); image_height(p) = utils_get_little_uint32(&buf[4]); biPlanes = utils_get_little_uint16(&buf[8]); p->bits_per_pixel = utils_get_little_uint16(&buf[10]); } else { image_width(p) = utils_get_little_uint16(&buf[0]); image_height(p) = utils_get_little_uint16(&buf[2]); biPlanes = utils_get_little_uint16(&buf[4]); p->bits_per_pixel = utils_get_little_uint16(&buf[6]); } if (biPlanes != 1) return 0; if (image_width(p) > 10000 || image_height(p) > 10000) return 0; switch (p->bits_per_pixel) { case 1: p->type = _BITMAP_MSBFirst; /* XXX: check order */ p->depth = p->bits_per_pixel; break; case 4: p->type = _INDEX44; p->depth = 4; break; case 8: p->type = _INDEX; p->depth = 8; break; case 16: p->type = _RGB555; p->depth = 16; break; case 24: p->type = _BGR24; p->depth = 24; break; case 32: p->type = _BGRA32; p->depth = 24; break; default: show_message("bmp: read_header: unknown bpp %d detected.\n", p->bits_per_pixel); return 0; } compress_method = 0; if (header_size >= WIN_BMP_HEADER_SIZE) { compress_method = utils_get_little_uint16(&buf[12]); image_size = utils_get_little_uint32(&buf[16]); image_size = image_size; // dummy } /* other header informations are intentionally ignored */ if (p->depth <= 8) { p->ncolors = 1 << p->depth; bytes_per_pal = (header_size >= WIN_BMP_HEADER_SIZE) ? 4 : 3; if (p->ncolors * bytes_per_pal > BUFSIZE) { err_message_fnc("BUFSIZE is too small. It must be greater than %d.\n", p->ncolors * bytes_per_pal); return 0; } if (stream_read(st, buf, p->ncolors * bytes_per_pal) != (int)(p->ncolors * bytes_per_pal)) return 0; for (i = 0; i < (int)p->ncolors; i++) { p->colormap[i][0] = buf[bytes_per_pal * i + 2]; p->colormap[i][1] = buf[bytes_per_pal * i + 1]; p->colormap[i][2] = buf[bytes_per_pal * i + 0]; } } else if (p->depth == 16 && compress_method == 3) { /* Read bitmasks */ if (stream_read(st, buf, 3 * 4) != 3 * 4) return 0; unsigned int rm = utils_get_little_uint32(buf); unsigned int gm = utils_get_little_uint32(buf + 4); unsigned int bm = utils_get_little_uint32(buf + 8); if (rm == 0xf800) p->type = _RGB565; else if (rm == 0x7c00) p->type = _RGB555; else warning_fnc("Mask: R %X G %X B %X is not supported\n", rm, gm, bm); compress_method = 0; } image_bpl(p) = (image_width(p) * p->bits_per_pixel) >> 3; image_bpl(p) += (4 - (image_bpl(p) % 4)) % 4; debug_message_fnc("(%d, %d): bpp %d depth %d compress %d\n", image_width(p), image_height(p), p->bits_per_pixel, p->depth, compress_method); if ((d = memory_alloc(image_image(p), image_bpl(p) * image_height(p))) == NULL) return 0; stream_seek(st, offset_to_image, _SET); pp = d + image_bpl(p) * (image_height(p) - 1); switch (compress_method) { case BI_RGB: for (i = (int)(image_height(p) - 1); i >= 0; i--) { stream_read(st, pp, image_bpl(p)); pp -= image_bpl(p); } break; case BI_RLE4: if (p->depth != 4) show_message("Compressed RI_BLE4 bitmap with depth %d != 4.\n", p->depth); /* RLE compressed data */ /* Not complete. */ y = image_height(p); while ((c = stream_getc(st)) != -1 && y >= 0) { if (c != 0) { /* code mode */ c2 = stream_getc(st); show_message_fnc("len %d data %d\n", c, c2); for (i = 0; i < c; i += 2) { *pp++ = (unsigned char)c2; } } else { if ((c = stream_getc(st)) == 0) { /* line end */ show_message_fnc("line end %d\n", y); pp = d + image_bpl(p) * (y - 1); y--; } else if (c == 1) { /* image end */ break; } else if (c == 2) { /* offset */ c = stream_getc(st); c2 = stream_getc(st); show_message_fnc("offset %d, %d\n", c, c2); pp += (c - c2 * image_width(p)) / 2; } else { /* absolute mode */ show_message_fnc("abs len %d\n", c); for (i = 0; i < c; i += 2) *pp++ = (unsigned char)stream_getc(st); if (c % 2 != 0) /* read dummy */ c = stream_getc(st); } } } break; case BI_RLE8: if (p->depth != 8) show_message("Compressed RI_BLE8 bitmap with depth %d != 8.\n", p->depth); /* RLE compressed data */ y = image_height(p); while ((c = stream_getc(st)) != -1 && y >= 0) { if (c != 0) { /* code mode */ c2 = stream_getc(st); for (i = 0; i < c; i++) { *pp++ = (unsigned char)c2; } } else { if ((c = stream_getc(st)) == 0) { /* line end */ pp = d + image_bpl(p) * (y - 1); y--; } else if (c == 1) { /* image end */ break; } else if (c == 2) { /* offset */ c = stream_getc(st); c2 = stream_getc(st); pp += (c - c2 * image_width(p)); } else { /* absolute mode */ for (i = 0; i < c; i++) *pp++ = (unsigned char)stream_getc(st); if (c % 2 != 0) /* read dummy */ c = stream_getc(st); } } } break; default: show_message("Compressed bitmap (method = %d) not supported.\n", compress_method); return 0; } return 1; }
int main(int argc, char **argv) { extern char *optarg; extern int optind; char *optstr; char *servername = NULL; char *yomi = NULL; int ch; int port = -1; int family = AF_INET; optstr = gen_optstring(options); while ((ch = getopt(argc, argv, optstr)) != -1) { switch (ch) { case 'h': usage(); return 0; case 's': servername = strdup(optarg); break; case 'p': port = atoi(optarg); if (port < 0 || port > 65535) { err_message_fnc("Invalid port number(%d).\n", port); return INVALID_PORT_ERROR; } break; case 'f': if (strcasecmp("INET", optarg) == 0) family = AF_INET; else if (strcasecmp("INET6", optarg) == 0) family = AF_INET6; else if (strcasecmp("UNSPEC", optarg) == 0) family = AF_UNSPEC; else if (strcasecmp("4", optarg) == 0) family = AF_INET; else if (strcasecmp("6", optarg) == 0) family = AF_INET6; else if (strcasecmp("IPv4", optarg) == 0) family = AF_INET; else if (strcasecmp("IPv6", optarg) == 0) family = AF_INET6; else { err_message_fnc("Invalid family(%s).\n", optarg); return INVALID_FAMILY_ERROR; } break; case 'y': yomi = strdup(optarg); break; default: usage(); return 0; } } free(optstr); if (optind == argc) { usage(); return 0; } if (strcasecmp(argv[optind], "help") == 0) { usage(); } else if (strcasecmp(argv[optind], "stat") == 0) { if (!servername) servername = strdup("localhost"); show_stat(servername, port, family); free(servername); } else if (strcasecmp(argv[optind], "tran") == 0 && yomi) { if (!servername) servername = strdup("localhost"); translate(servername, port, family, yomi); free(servername); } else { usage(); return 1; } return 0; }
static int set_params(AudioDevice *ad, AudioFormat *format_p, int *ch_p, unsigned int *rate_p) { ALSA_data *alsa = (ALSA_data *)ad->private_data; int r, err; snd_pcm_format_t f; AudioFormat format = *format_p; snd_pcm_hw_params_t *hwparams; snd_pcm_sw_params_t *swparams; int ch = *ch_p; unsigned int rate = *rate_p; unsigned int buffer, period; snd_pcm_hw_params_alloca(&hwparams); snd_pcm_sw_params_alloca(&swparams); ad->format = _AUDIO_FORMAT_UNSET; *format_p = _AUDIO_FORMAT_UNSET; *ch_p = 0; *rate_p = 0; /* format part */ switch (format) { case _AUDIO_FORMAT_MU_LAW: f = SND_PCM_FORMAT_MU_LAW; break; case _AUDIO_FORMAT_A_LAW: f = SND_PCM_FORMAT_A_LAW; break; case _AUDIO_FORMAT_ADPCM: f = SND_PCM_FORMAT_IMA_ADPCM; break; case _AUDIO_FORMAT_U8: f = SND_PCM_FORMAT_U8; break; case _AUDIO_FORMAT_S8: f = SND_PCM_FORMAT_S8; break; case _AUDIO_FORMAT_U16_LE: f = SND_PCM_FORMAT_U16_LE; break; case _AUDIO_FORMAT_U16_BE: f = SND_PCM_FORMAT_U16_BE; break; case _AUDIO_FORMAT_S16_LE: f = SND_PCM_FORMAT_S16_LE; break; case _AUDIO_FORMAT_S16_BE: f = SND_PCM_FORMAT_S16_BE; break; #if 0 case _AUDIO_FORMAT_S32_LE: f = SND_PCM_FORMAT_U32_LE; break; case _AUDIO_FORMAT_S32_BE: f = SND_PCM_FORMAT_S32_BE; break; #endif default: show_message_fnc("format %d is invalid.\n", format); return 0; } if ((err = snd_pcm_hw_params_any(alsa->fd, hwparams)) < 0) { show_message_fnc("snd_pcm_hw_params_any() failed.\n"); return 0; } if ((err = snd_pcm_hw_params_set_access(alsa->fd, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { show_message_fnc("snd_pcm_hw_params_set_access() failed.\n"); return 0; } if ((err = snd_pcm_hw_params_set_format(alsa->fd, hwparams, f)) < 0) { show_message_fnc("snd_pcm_hw_params_set_format() failed.\n"); return 0; } ad->format = format; *format_p = format; /* channel part */ if ((err = snd_pcm_hw_params_set_channels(alsa->fd, hwparams, ch)) < 0) { show_message_fnc("snd_pcm_hw_params_set_channels() failed.\n"); return 0; } *ch_p = ch; /* rate part */ if ((err = snd_pcm_hw_params_set_rate_near(alsa->fd, hwparams, &rate, 0)) < 0) { show_message_fnc("snd_pcm_hw_params_set_rate_near() failed.\n"); return 0; } *rate_p = rate; /* buffer & period */ buffer = 500000; period = 500000 / 4; if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa->fd, hwparams, &buffer, 0)) < 0) { show_message_fnc("snd_pcm_hw_params_set_buffer_near() failed.\n"); return 0; } r = snd_pcm_hw_params_get_buffer_size(hwparams, &alsa->buffer_size); if ((err = snd_pcm_hw_params_set_period_time_near(alsa->fd, hwparams, &period, 0)) < 0) { show_message_fnc("snd_pcm_hw_params_set_period_near() failed.\n"); return 0; } r = snd_pcm_hw_params_get_period_size(hwparams, &alsa->period_size, 0); if ((err = snd_pcm_hw_params(alsa->fd, hwparams)) < 0) { perror("snd_pcm_hw_params"); err_message_fnc("snd_pcm_hw_params() failed.\n"); snd_pcm_hw_params_dump(hwparams, alsa->log); return 0; } r = snd_pcm_hw_params_get_channels(hwparams, &ad->channels); r = snd_pcm_hw_params_get_rate(hwparams, &ad->speed, 0); r = r; // dummy #ifdef DEBUG { snd_pcm_format_t form; debug_message_fnc("format "); r = snd_pcm_hw_params_get_format(hwparams, &form); switch (form) { case SND_PCM_FORMAT_MU_LAW: debug_message("MU_LAW "); break; case SND_PCM_FORMAT_A_LAW: debug_message("A_LAW "); break; case SND_PCM_FORMAT_IMA_ADPCM: debug_message("ADPCM "); break; case SND_PCM_FORMAT_U8: debug_message("U8 "); break; case SND_PCM_FORMAT_S8: debug_message("S8 "); break; case SND_PCM_FORMAT_U16_LE: debug_message("U16LE "); break; case SND_PCM_FORMAT_U16_BE: debug_message("U16BE "); break; case SND_PCM_FORMAT_S16_LE: debug_message("S16LE "); break; case SND_PCM_FORMAT_S16_BE: debug_message("S16BE "); break; #if 0 case SND_PCM_FORMAT_U32_LE: debug_message("U32LE "); break; case SND_PCM_FORMAT_S32_BE: debug_message("S32BE "); break; #endif default: debug_message("UNKNOWN "); break; } debug_message("%d ch %d Hz buffer %ld period %ld OK\n", ad->channels, ad->speed, alsa->buffer_size, alsa->period_size); } #endif /* sw_params */ if ((err = snd_pcm_sw_params_current(alsa->fd, swparams)) < 0) { show_message_fnc("snd_pcm_sw_params_any() failed.\n"); return 0; } if ((err = snd_pcm_sw_params(alsa->fd, swparams)) < 0) { show_message_fnc("snd_pcm_sw_params() failed.\n"); return 0; } debug_message_fnc("1 sample -> %ld bytes\n", snd_pcm_samples_to_bytes(alsa->fd, 1)); return 1; }