X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=wma_afh.c;h=63e49677b0f1b85c13c17160a7b249531c1fbcd4;hp=4c9d87e047af8d1d5873096db54ab4a9d17ab966;hb=HEAD;hpb=121015ff5cdbe6af7b84f6245ebe62fe2a52a859 diff --git a/wma_afh.c b/wma_afh.c index 4c9d87e0..8bff7cfc 100644 --- a/wma_afh.c +++ b/wma_afh.c @@ -1,8 +1,4 @@ -/* - * Copyright (C) 2009 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ +/* Copyright (C) 2009 Andre Noll , see file COPYING. */ /** \file wma_afh.c The audio format handler for WMA files. */ @@ -38,8 +34,7 @@ static int count_frames(const char *buf, int buf_size, uint32_t packet_size, sfc++; } PARA_INFO_LOG("%d frames, %d superframes\n", fc, sfc); - if (num_superframes) - *num_superframes = sfc; + *num_superframes = sfc; return fc; } @@ -68,7 +63,7 @@ static int put_utf8(uint32_t val, char *result) *out++ = in; return 1; } - bytes = (wma_log2(in) + 4) / 5; + bytes = DIV_ROUND_UP(wma_log2(in), 5); shift = (bytes - 1) * 6; *out++ = (256 - (256 >> bytes)) | (in >> shift); while (shift >= 6) { @@ -200,7 +195,7 @@ static int wma_make_chunk_table(char *buf, size_t buf_size, uint32_t packet_size size_t ct_size = 250; int ret, count = 0, num_frames, num_superframes; - afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t)); + afhi->chunk_table = arr_alloc(ct_size, sizeof(uint32_t)); afhi->chunk_table[0] = 0; afhi->chunk_table[1] = afhi->header_len; @@ -229,6 +224,7 @@ static int wma_make_chunk_table(char *buf, size_t buf_size, uint32_t packet_size } } afhi->chunks_total = j; + set_max_chunk_size(afhi); set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv); return 1; fail: @@ -316,30 +312,34 @@ static const char top_level_header_object_guid[] = { static int convert_utf8_to_utf16(char *src, char **dst) { - /* - * Without specifying LE (little endian), iconv includes a byte order - * mark (e.g. 0xFFFE) at the beginning. - */ - iconv_t cd = iconv_open("UTF-16LE", "UTF-8"); + iconv_t cd; size_t sz, inbytes, outbytes, inbytesleft, outbytesleft; char *inbuf, *outbuf; int ret; if (!src || !*src) { - *dst = para_calloc(2); - ret = 0; - goto out; + *dst = zalloc(2); + return 0; } - if (cd == (iconv_t) -1) + /* + * Without specifying LE (little endian), iconv includes a byte order + * mark (e.g. 0xFFFE) at the beginning. + */ + cd = iconv_open("UTF-16LE", "UTF-8"); + if (cd == (iconv_t)-1) { + *dst = NULL; return -ERRNO_TO_PARA_ERROR(errno); + } inbuf = src; /* even though src is in UTF-8, strlen() should DTRT */ inbytes = inbytesleft = strlen(src); outbytes = outbytesleft = 4 * inbytes + 2; /* hope that's enough */ - *dst = outbuf = para_malloc(outbytes); + *dst = outbuf = alloc(outbytes); sz = iconv(cd, ICONV_CAST &inbuf, &inbytesleft, &outbuf, &outbytesleft); if (sz == (size_t)-1) { ret = -ERRNO_TO_PARA_ERROR(errno); + free(*dst); + *dst = NULL; goto out; } assert(outbytes >= outbytesleft); @@ -351,8 +351,6 @@ static int convert_utf8_to_utf16(char *src, char **dst) *dst = outbuf; PARA_INFO_LOG("converted %s to %d UTF-16 bytes\n", src, ret); out: - if (ret < 0) - free(*dst); if (iconv_close(cd) < 0) PARA_WARNING_LOG("iconv_close: %s\n", strerror(errno)); return ret; @@ -363,8 +361,7 @@ static int make_cdo(struct taginfo *tags, const struct asf_object *cdo, struct asf_object *result) { const char *cr, *rating; /* orig data */ - uint16_t orig_title_bytes, orig_artist_bytes, orig_cr_bytes, - orig_comment_bytes, orig_rating_bytes; + uint16_t orig_cr_bytes, orig_rating_bytes; /* pointers to new UTF-16 tags */ char *artist = NULL, *title = NULL, *comment = NULL; /* number of bytes in UTF-16 for the new tags */ @@ -376,17 +373,21 @@ static int make_cdo(struct taginfo *tags, const struct asf_object *cdo, ret = convert_utf8_to_utf16(tags->artist, &artist); if (ret < 0) return ret; + assert(artist); artist_bytes = ret; ret = convert_utf8_to_utf16(tags->title, &title); if (ret < 0) goto out; + assert(title); title_bytes = ret; ret = convert_utf8_to_utf16(tags->comment, &comment); if (ret < 0) goto out; + assert(comment); comment_bytes = ret; if (cdo) { + uint16_t orig_title_bytes, orig_artist_bytes, orig_comment_bytes; /* * Sizes of the five fields (stored as 16-bit numbers) are * located after the header (16 bytes) and the cdo size (8 @@ -400,10 +401,7 @@ static int make_cdo(struct taginfo *tags, const struct asf_object *cdo, cr = cdo->ptr + 34 + orig_title_bytes + orig_artist_bytes; rating = cr + orig_cr_bytes + orig_comment_bytes; } else { - orig_title_bytes = 2; - orig_artist_bytes = 2; orig_cr_bytes = 2; - orig_comment_bytes = 2; orig_rating_bytes = 2; cr = null; rating = null; @@ -413,7 +411,7 @@ static int make_cdo(struct taginfo *tags, const struct asf_object *cdo, result->size = 16 + 8 + 5 * 2 + title_bytes + artist_bytes + orig_cr_bytes + comment_bytes + orig_rating_bytes; PARA_DEBUG_LOG("cdo is %zu bytes\n", (size_t)result->size); - p = result->ptr = para_malloc(result->size); + p = result->ptr = alloc(result->size); memcpy(p, content_description_header, 16); p += 16; write_u64(p, result->size); @@ -459,17 +457,19 @@ static int make_ecdo(struct taginfo *tags, struct asf_object *result) ret = convert_utf8_to_utf16(tags->album, &album); if (ret < 0) return ret; + assert(album); album_bytes = ret; ret = convert_utf8_to_utf16(tags->year, &year); if (ret < 0) goto out; + assert(year); year_bytes = ret; result->size = 16 + 8 + 2; /* GUID, size, count */ /* name_length + name + null + data type + val length + val */ result->size += 2 + sizeof(album_tag_header) + 2 + 2 + 2 + album_bytes; result->size += 2 + sizeof(year_tag_header) + 2 + 2 + 2 + year_bytes; - p = result->ptr = para_malloc(result->size); + p = result->ptr = alloc(result->size); memcpy(p, extended_content_header, 16); p += 16; write_u64(p, result->size); @@ -622,7 +622,7 @@ static int wma_rewrite_tags(const char *map, size_t mapsize, if (top.reserved2 != 2) return -E_NO_WMA; p++; /* objects start at p */ - top.objects = para_malloc(top.num_objects * sizeof(struct asf_object)); + top.objects = arr_alloc(top.num_objects, sizeof(struct asf_object)); ret = read_asf_objects(p, top.size - (p - map), top.num_objects, top.objects, &ton); if (ret < 0) @@ -646,13 +646,13 @@ out: static const char * const wma_suffixes[] = {"wma", NULL}; /** - * The init function of the wma audio format handler. + * The audio format handler for Windows Media Audio. * - * \param afh Pointer to the struct to initialize. + * Only WMA version 2 is supported. This audio format handler does not depend + * on any third party libraries and is therefore always compiled in. */ -void wma_afh_init(struct audio_format_handler *afh) -{ - afh->get_file_info = wma_get_file_info; - afh->suffixes = wma_suffixes; - afh->rewrite_tags = wma_rewrite_tags; -} +const struct audio_format_handler wma_afh = { + .get_file_info = wma_get_file_info, + .suffixes = wma_suffixes, + .rewrite_tags = wma_rewrite_tags, +};