Index: apps/metadata/rm.c =================================================================== --- apps/metadata/rm.c (revision 0) +++ apps/metadata/rm.c (revision 0) @@ -0,0 +1,425 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id:$ + * + * Copyright (C) 2009 Mohamed Tarek + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include +#include +#include +#include + +#include +#include "system.h" +#include "metadata.h" +#include "metadata_common.h" +#include "metadata_parsers.h" +#include "logf.h" + +#define MAX_STRING 92 /* Size of id3v1 buffers in metadata.h */ + +int read_cook_extradata(int fd, RMContext *rmctx) { + read_uint32be(fd, &rmctx->cook_version); + read_uint16be(fd, &rmctx->samples_pf_pc); + read_uint16be(fd, &rmctx->nb_subbands); + if(rmctx->extradata_size == 16) { + read_uint32be(fd, &rmctx->unused); + read_uint16be(fd, &rmctx->js_subband_start); + read_uint16be(fd, &rmctx->js_vlc_bits); + } + return rmctx->extradata_size; /* for 'skipped' */ +} + +void print_cook_extradata(RMContext *rmctx) { + + DEBUGF(" cook_version = 0x%08lx\n", rmctx->cook_version); + DEBUGF(" samples_per_frame_per_channel = %d\n", rmctx->samples_pf_pc); + DEBUGF(" number_of_subbands_in_freq_domain = %d\n", rmctx->nb_subbands); + if(rmctx->extradata_size == 16) { + DEBUGF(" joint_stereo_subband_start = %d\n",rmctx->js_subband_start); + DEBUGF(" joint_stereo_vlc_bits = %d\n", rmctx->js_vlc_bits); + } +} + + +struct real_object_t +{ + uint32_t fourcc; + uint32_t size; + uint16_t version; +}; + +#define FOURCC(a,b,c,d) (((a)<<24) | ((b) << 16) | ((c) << 8) | (d)) + +static int real_read_object_header(int fd, struct real_object_t* obj) +{ + int n; + + if ((n = read_uint32be(fd, &obj->fourcc)) <= 0) return n; + if ((n = read_uint32be(fd, &obj->size)) <= 0) return n; + if ((n = read_uint16be(fd, &obj->version)) <= 0) return n; + + return 1; +} + +static char* fourcc2str(uint32_t f) +{ + static char res[5]; + + res[0] = (f & 0xff000000) >> 24; + res[1] = (f & 0xff0000) >> 16; + res[2] = (f & 0xff00) >> 8; + res[3] = (f & 0xff); + res[4] = 0; + + return res; +} + +static int real_read_audio_stream_info(int fd, RMContext *rmctx) +{ + int skipped = 0; + uint32_t version; + struct real_object_t obj; + memset(&obj,0,sizeof(obj)); + uint32_t header_size; + uint16_t flavor; + uint32_t coded_framesize; + uint32_t unknown1; + uint32_t unknown2; + uint32_t unknown3; + uint16_t unknown4; + uint16_t unknown5; + uint16_t unknown6; + uint16_t unknown7; + uint32_t unknown8; + uint8_t interleaver_id_length; + uint32_t interleaver_id; + uint8_t fourcc_length; + uint32_t fourcc = 0; + uint8_t unknown9; + uint16_t unknown10; + uint8_t unknown11; + + read_uint32be(fd, &version); + skipped += 4; + + DEBUGF(" version=0x%04lx\n",((version >> 16) & 0xff)); + if (((version >> 16) & 0xff) == 3) { + /* Very old version */ + } else { + real_read_object_header(fd, &obj); + skipped += 10; + read_uint32be(fd, &header_size); + skipped += 4; + /* obj.size will be filled with an unknown value, replaced with header_size */ + DEBUGF(" Object: %s, size: %ld bytes, version: 0x%04x\n",fourcc2str(obj.fourcc),header_size,obj.version); + + read_uint16be(fd, &flavor); + read_uint32be(fd, &coded_framesize); + read_uint32be(fd, &unknown1); + read_uint32be(fd, &unknown2); + read_uint32be(fd, &unknown3); + read_uint16be(fd, &rmctx->sub_packet_h); + read_uint16be(fd, &rmctx->block_align); + read_uint16be(fd, &rmctx->sub_packet_size); + read_uint16be(fd, &unknown4); + skipped += 26; + if (((version >> 16) & 0xff) == 5) + { + read_uint16be(fd, &unknown5); + read_uint16be(fd, &unknown6); + read_uint16be(fd, &unknown7); + skipped += 6; + } + read_uint16be(fd, &rmctx->sample_rate); + read_uint32be(fd, &unknown8); + read_uint16be(fd, &rmctx->nb_channels); + skipped += 8; + if (((version >> 16) & 0xff) == 4) + { + read_uint8(fd, &interleaver_id_length); + read_uint32be(fd, &interleaver_id); + read_uint8(fd, &fourcc_length); + read_uint32be(fd, &fourcc); + skipped += 10; + } + if (((version >> 16) & 0xff) == 5) + { + read_uint32be(fd, &interleaver_id); + read_uint32be(fd, &fourcc); + skipped += 8; + } + read_uint8(fd,&unknown9); + read_uint16be(fd,&unknown10); + skipped += 3; + if (((version >> 16) & 0xff) == 5) + { + read_uint8(fd, &unknown11); + skipped += 1; + } + + read_uint32be(fd, &rmctx->extradata_size); + skipped += 4; + if(!strncmp(fourcc2str(fourcc),"cook",4)){ + skipped += read_cook_extradata(fd, rmctx); + rmctx->codec_type = cook; + } + + + DEBUGF(" flavor = %d\n",flavor); + DEBUGF(" coded_frame_size = %ld\n",coded_framesize); + DEBUGF(" sub_packet_h = %d\n",rmctx->sub_packet_h); + DEBUGF(" frame_size = %d\n",rmctx->block_align); + DEBUGF(" sub_packet_size = %d\n",rmctx->sub_packet_size); + DEBUGF(" sample_rate= %d\n",rmctx->sample_rate); + DEBUGF(" channels= %d\n",rmctx->nb_channels); + DEBUGF(" fourcc = %s\n",fourcc2str(fourcc)); + DEBUGF(" codec_extra_data_length = %ld\n",rmctx->extradata_size); + DEBUGF(" codec_extradata :\n"); + print_cook_extradata(rmctx); + + } + + return skipped; +} + +int real_parse_header(int fd, RMContext *rmctx) +{ + struct real_object_t obj; + memset(&obj,0,sizeof(obj)); + int res; + int skipped; + off_t curpos; + uint8_t len; /* Holds a string_length, which is then passed to read_string() */ + + uint32_t unknown1; + uint32_t unknown2; + + uint32_t max_bitrate; + uint32_t avg_bitrate = 0; + uint32_t max_packet_size; + uint32_t avg_packet_size; + uint32_t packet_count; + uint32_t preroll; + uint32_t duration; + uint32_t index_offset; + uint16_t num_streams; + uint16_t flags = 0; + + uint16_t stream_id; + uint32_t start_time; + char desc[256]; + char mimetype[256]; + uint32_t codec_data_size; + uint32_t v; + + uint32_t next_data_off; + uint8_t header_end; + + curpos = lseek(fd, 0, SEEK_SET); + res = real_read_object_header(fd, &obj); + + if (obj.fourcc == FOURCC('.','r','a',0xfd)) + { + /* Very old .ra format - not yet supported */ + return -1; + } + else if (obj.fourcc != FOURCC('.','R','M','F')) + { + return -1; + } + + read_uint32be(fd, &unknown1); + read_uint32be(fd, &unknown2); + + DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos); + DEBUGF(" unknown1=%ld (0x%08lx)\n",unknown1,unknown1); + DEBUGF(" unknown2=%ld (0x%08lx)\n",unknown2,unknown2); + + res = real_read_object_header(fd, &obj); + header_end = 0; + while(res) + { + DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos); + skipped = 10; + if(obj.fourcc == FOURCC('I','N','D','X')) + break; + switch (obj.fourcc) + { + case FOURCC('P','R','O','P'): /* File properties */ + read_uint32be(fd, &max_bitrate); + read_uint32be(fd, &rmctx->bit_rate); /*avg bitrate*/ + read_uint32be(fd, &max_packet_size); + read_uint32be(fd, &avg_packet_size); + read_uint32be(fd, &packet_count); + read_uint32be(fd, &rmctx->duration); + read_uint32be(fd, &preroll); + read_uint32be(fd, &index_offset); + read_uint32be(fd, &rmctx->data_offset); + read_uint16be(fd, &num_streams); + read_uint16be(fd, &rmctx->flags); + skipped += 40; + + DEBUGF(" max_bitrate = %ld\n",max_bitrate); + DEBUGF(" avg_bitrate = %ld\n",avg_bitrate); + DEBUGF(" max_packet_size = %ld\n",max_packet_size); + DEBUGF(" avg_packet_size = %ld\n",avg_packet_size); + DEBUGF(" packet_count = %ld\n",packet_count); + DEBUGF(" duration = %ld\n",rmctx->duration); + DEBUGF(" preroll = %ld\n",preroll); + DEBUGF(" index_offset = %ld\n",index_offset); + DEBUGF(" data_offset = %ld\n",rmctx->data_offset); + DEBUGF(" num_streams = %d\n",num_streams); + DEBUGF(" flags=0x%04x\n",flags); + break; + + case FOURCC('C','O','N','T'): + /* Four strings - Title, Author, Copyright, Comment */ + read_uint8(fd,&len); + skipped += (int)read_string(fd, rmctx->title, MAX_STRING, '\0', len); + read_uint8(fd,&len); + skipped += (int)read_string(fd, rmctx->author, MAX_STRING, '\0', len); + read_uint8(fd,&len); + skipped += (int)read_string(fd, rmctx->copyright, MAX_STRING, '\0', len); + read_uint8(fd,&len); + skipped += (int)read_string(fd, rmctx->comment, MAX_STRING, '\0', len); + skipped += 4; + + DEBUGF(" title=\"%s\"\n",rmctx->title); + DEBUGF(" author=\"%s\"\n",rmctx->author); + DEBUGF(" copyright=\"%s\"\n",rmctx->copyright); + DEBUGF(" comment=\"%s\"\n",rmctx->comment); + break; + + case FOURCC('M','D','P','R'): /* Media properties */ + read_uint16be(fd,&stream_id); + skipped += 2; + read_uint32be(fd,&max_bitrate); + skipped += 4; + read_uint32be(fd,&avg_bitrate); + skipped += 4; + read_uint32be(fd,&max_packet_size); + skipped += 4; + read_uint32be(fd,&avg_packet_size); + skipped += 4; + read_uint32be(fd,&start_time); + skipped += 4; + read_uint32be(fd,&preroll); + skipped += 4; + read_uint32be(fd,&duration); + skipped += 4; + read_uint8(fd,&len); + skipped += 1; + skipped += (int)read_string(fd, desc, 256, '\0', len); + read_uint8(fd,&len); + skipped += 1; + skipped += (int)read_string(fd, mimetype, 256, '\0', len); + read_uint32be(fd,&codec_data_size); + skipped += 4; + //From ffmpeg: codec_pos = url_ftell(pb); + read_uint32be(fd,&v); + skipped += 4; + + DEBUGF(" stream_id = 0x%04x\n",stream_id); + DEBUGF(" max_bitrate = %ld\n",max_bitrate); + DEBUGF(" avg_bitrate = %ld\n",avg_bitrate); + DEBUGF(" max_packet_size = %ld\n",max_packet_size); + DEBUGF(" avg_packet_size = %ld\n",avg_packet_size); + DEBUGF(" start_time = %ld\n",start_time); + DEBUGF(" preroll = %ld\n",preroll); + DEBUGF(" duration = %ld\n",duration); + DEBUGF(" desc=\"%s\"\n",desc); + DEBUGF(" mimetype=\"%s\"\n",mimetype); + DEBUGF(" codec_data_size = %ld\n",codec_data_size); + DEBUGF(" v=\"%s\"\n", fourcc2str(v)); + + if (v == FOURCC('.','r','a',0xfd)) + { + skipped += real_read_audio_stream_info(fd, rmctx); + } + + break; + + case FOURCC('D','A','T','A'): + + read_uint32be(fd,&rmctx->nb_packets); + skipped += 4; + read_uint32be(fd,&next_data_off); + skipped += 4; + if (!rmctx->nb_packets && (rmctx->flags & 4)) + rmctx->nb_packets = 3600 * 25; + + /*** + * nb_packets correction : + * in some samples, number of packets may not exactly form + * an integer number of scrambling units. This is corrected + * by constructing a partially filled unit out of the few + * remaining samples at the end of decoding. + ***/ + if(rmctx->nb_packets % rmctx->sub_packet_h) + rmctx->nb_packets += rmctx->sub_packet_h - (rmctx->nb_packets % rmctx->sub_packet_h); + + DEBUGF(" data_nb_packets = %ld\n",rmctx->nb_packets); + DEBUGF(" next DATA offset = %ld\n",next_data_off); + header_end = 1; + break; + } + if(header_end) break; + curpos = lseek(fd, obj.size - skipped, SEEK_CUR); + res = real_read_object_header(fd, &obj); + } + + + return 0; +} + +void copy_metadata(struct mp3entry *id3, RMContext *rmctx) +{ + /* Copy tags */ + memcpy(id3->id3v1buf[0], rmctx->title, MAX_STRING); + memcpy(id3->id3v1buf[1], rmctx->author, MAX_STRING); + memcpy(id3->id3v1buf[2], rmctx->comment, MAX_STRING); + + id3->title = id3->id3v1buf[0]; + id3->artist = id3->id3v1buf[1]; + id3->comment = id3->id3v1buf[2]; + + //logf("METADATA : 1-%s 2-%s 3-%s\n", rmctx->title, rmctx->author,rmctx->comment); + /* Copy stream parameters */ + id3->bitrate = rmctx->bit_rate / 1000; + id3->frequency = rmctx->sample_rate; + id3->length = rmctx->duration; + switch(rmctx->codec_type) + { + case cook: + id3->codectype = AFMT_COOK; + break; + } + memcpy(id3->toc, rmctx, sizeof(RMContext)); +} + +bool get_rm_metadata(int fd, struct mp3entry* id3) +{ + RMContext rmctx; + memset(&rmctx,0,sizeof(RMContext)); + real_parse_header(fd, &rmctx); + copy_metadata(id3, &rmctx); + id3->filesize = filesize(fd); + return true; +} + Index: apps/metadata/metadata_parsers.h =================================================================== --- apps/metadata/metadata_parsers.h (revision 20966) +++ apps/metadata/metadata_parsers.h (working copy) @@ -39,3 +39,4 @@ bool get_a52_metadata(int fd, struct mp3entry* id3); bool get_asf_metadata(int fd, struct mp3entry* id3); bool get_asap_metadata(int fd, struct mp3entry* id3); +bool get_rm_metadata(int fd, struct mp3entry* id3); Index: apps/metadata.c =================================================================== --- apps/metadata.c (revision 20966) +++ apps/metadata.c (working copy) @@ -115,6 +115,9 @@ /* Amiga SAP File */ [AFMT_SAP] = AFMT_ENTRY("SAP", "asap", NULL, "sap\0" ), + /* Cook in RM/RA */ + [AFMT_COOK] = + AFMT_ENTRY("Cook", "cook", NULL, "rm\0ra\0" ), #endif }; @@ -372,6 +375,14 @@ id3->filesize = filesize(fd); id3->genre_string = id3_get_num_genre(36); break; + + case AFMT_COOK: + if (!get_rm_metadata(fd, id3)) + { + DEBUGF("get_rm_metadata error\n"); + return false; + } + break; #endif /* CONFIG_CODEC == SWCODEC */ Index: apps/metadata.h =================================================================== --- apps/metadata.h (revision 20966) +++ apps/metadata.h (working copy) @@ -61,6 +61,7 @@ AFMT_WMA, /* WMAV1/V2 in ASF */ AFMT_MOD, /* Amiga MOD File Format */ AFMT_SAP, /* Amiga 8Bit SAP Format */ + AFMT_COOK, /* Cook in RM/RA */ #endif /* add new formats at any index above this line to have a sensible order - Index: apps/SOURCES =================================================================== --- apps/SOURCES (revision 20966) +++ apps/SOURCES (working copy) @@ -162,6 +162,7 @@ metadata/wavpack.c metadata/a52.c metadata/asap.c +metadata/rm.c #endif #ifdef HAVE_TAGCACHE tagcache.c Index: apps/filetypes.c =================================================================== --- apps/filetypes.c (revision 20966) +++ apps/filetypes.c (working copy) @@ -83,6 +83,8 @@ { "ape", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, { "mac", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, { "sap" ,FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, + { "rm", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, + { "ra", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, #endif { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, Index: apps/codecs/libcook/bitstream.h =================================================================== --- apps/codecs/libcook/bitstream.h (revision 21039) +++ apps/codecs/libcook/bitstream.h (working copy) @@ -26,7 +26,7 @@ #ifndef AVCODEC_BITSTREAM_H #define AVCODEC_BITSTREAM_H -#include +#include #include #include #include @@ -736,6 +736,7 @@ } } +#if 0 static inline int check_marker(GetBitContext *s, const char *msg) { int bit= get_bits1(s); @@ -744,6 +745,7 @@ return bit; } +#endif /** * init GetBitContext. Index: apps/codecs/libcook/cook.h =================================================================== --- apps/codecs/libcook/cook.h (revision 21066) +++ apps/codecs/libcook/cook.h (working copy) @@ -22,7 +22,7 @@ #ifndef _COOK_H #define _COOK_H -#include +#include #include "bitstream.h" #include "../librm/rm.h" #include "cookdata_fixpoint.h" Index: apps/codecs/libcook/cook_fixpoint.h =================================================================== --- apps/codecs/libcook/cook_fixpoint.h (revision 21039) +++ apps/codecs/libcook/cook_fixpoint.h (working copy) @@ -54,29 +54,6 @@ }; /** - * Initialise fixed point implementation. - * Nothing to do for fixed point. - * - * @param q pointer to the COOKContext - */ -static inline int init_cook_math(COOKContext *q) -{ - return 0; -} - -/** - * Free resources used by floating point implementation. - * Nothing to do for fixed point. - * - * @param q pointer to the COOKContext - */ -static inline void free_cook_math(COOKContext *q) -{ - return; -} - - -/** * Fixed point multiply by power of two. * * @param x fix point value @@ -167,7 +144,7 @@ } } - +#ifdef TEST /** * The modulated lapped transform, this takes transform coefficients * and transforms them into timedomain samples. @@ -205,8 +182,36 @@ q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j]); } while (++i < n); } +#else +#include +static inline void imlt_math(COOKContext *q, FIXP *in) +{ + const int n = q->samples_per_channel; + const int step = 4 << (10 - av_log2(n)); + int i = 0, j = step>>1; + mdct_backward(2 * n, in, q->mono_mdct_output); + + do { + FIXP tmp = q->mono_mdct_output[i]; + + q->mono_mdct_output[i] = + fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j]); + q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j+1]); + j += step; + } while (++i < n/2); + do { + FIXP tmp = q->mono_mdct_output[i]; + + j -= step; + q->mono_mdct_output[i] = + fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j+1]); + q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j]); + } while (++i < n); +} +#endif + /** * Perform buffer overlapping. * Index: apps/codecs/libcook/bswap.h =================================================================== --- apps/codecs/libcook/bswap.h (revision 21039) +++ apps/codecs/libcook/bswap.h (working copy) @@ -26,7 +26,7 @@ #ifndef AVUTIL_BSWAP_H #define AVUTIL_BSWAP_H -#include +#include //#include "ffmpeg_config.h" //#include "common.h" Index: apps/codecs/libcook/cookdata_fixpoint.h =================================================================== --- apps/codecs/libcook/cookdata_fixpoint.h (revision 21039) +++ apps/codecs/libcook/cookdata_fixpoint.h (working copy) @@ -26,7 +26,7 @@ * fixed point data types and constants */ -#include +#include typedef int32_t FIXP; /* Fixed point variable type */ typedef uint16_t FIXPU; /* Fixed point fraction 0<=x<1 */ @@ -39,11 +39,11 @@ typedef struct { } realvars_t; - +#ifdef TEST #define cPI1_8 0xec83 /* 1pi/8 2^16 */ #define cPI2_8 0xb505 /* 2pi/8 2^16 */ #define cPI3_8 0x61f8 /* 3pi/8 2^16 */ - +#endif static const FIXPU sincos_lookup[2050] = { /* x_i = 2^16 sin(i 2pi/8192), 2^16 cos(i 2pi/8192); i=0..1024 */ 0x0000, 0xffff, 0x0032, 0xffff, 0x0065, 0xffff, 0x0097, 0xffff, Index: apps/codecs/libcook/Makefile.test =================================================================== --- apps/codecs/libcook/Makefile.test (revision 21066) +++ apps/codecs/libcook/Makefile.test (working copy) @@ -1,4 +1,4 @@ -CFLAGS = -Wall -O3 +CFLAGS = -Wall -O3 -DTEST OBJS = main.o bitstream.o cook.o ../librm/rm.o cooktest: $(OBJS) gcc -o cooktest $(OBJS) Index: apps/codecs/libcook/bitstream.c =================================================================== --- apps/codecs/libcook/bitstream.c (revision 21039) +++ apps/codecs/libcook/bitstream.c (working copy) @@ -29,6 +29,12 @@ #include "bitstream.h" +#ifdef TEST +#define DEBUGF printf +#else +#define DEBUGF(...) +#endif + const uint8_t ff_log2_run[32]={ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, @@ -127,8 +133,7 @@ vlc->table_size += size; if (vlc->table_size > vlc->table_allocated) { if(use_static>1){ - printf("init_vlc() used with too little memory : table_size > allocated_memory\n"); - abort(); //cant do anything, init_vlc() is used with too little memory + DEBUGF("init_vlc() used with too little memory : table_size > allocated_memory\n"); } if (!vlc->table) @@ -151,7 +156,7 @@ table_size = 1 << table_nb_bits; table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC)); #ifdef DEBUG_VLC - printf("new table index=%d size=%d code_prefix=%x n=%d\n", + DEBUGF("new table index=%d size=%d code_prefix=%x n=%d\n", table_index, table_size, code_prefix, n_prefix); #endif if (table_index < 0) @@ -175,7 +180,7 @@ else GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size); #if defined(DEBUG_VLC) && 0 - printf("i=%d n=%d code=0x%x\n", i, n, code); + DEBUGF("i=%d n=%d code=0x%x\n", i, n, code); #endif /* if code matches the prefix, it is in the table */ n -= n_prefix; @@ -192,11 +197,11 @@ if(flags & INIT_VLC_LE) j = (code >> n_prefix) + (k<> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); #ifdef DEBUG_VLC - printf("%4x: n=%d (subtable)\n", + DEBUGF("%4x: n=%d (subtable)\n", j, n); #endif /* compute table size */ @@ -282,7 +287,7 @@ if(vlc->table_size && vlc->table_size == vlc->table_allocated){ return 0; }else if(vlc->table_size){ - abort(); // fatal error, we are called on a partially initialized table + return -1; // fatal error, we are called on a partially initialized table } }else if(!(flags & INIT_VLC_USE_STATIC)) { vlc->table = NULL; @@ -296,7 +301,7 @@ } #ifdef DEBUG_VLC - printf("build table nb_codes=%d\n", nb_codes); + DEBUGF("build table nb_codes=%d\n", nb_codes); #endif if (build_table(vlc, nb_bits, nb_codes, @@ -304,20 +309,14 @@ codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, 0, 0, flags) < 0) { - free(&vlc->table); + //free(&vlc->table); return -1; } /* Changed the following condition to be true if table_size > table_allocated. * * This would be more sensible for static tables since we want warnings for * * memory shortages only. */ if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size > vlc->table_allocated) - printf("needed %d had %d\n", vlc->table_size, vlc->table_allocated); + DEBUGF("needed %d had %d\n", vlc->table_size, vlc->table_allocated); return 0; } - -void free_vlc(VLC *vlc) -{ - free(&vlc->table); -} - Index: apps/codecs/libcook/cook.c =================================================================== --- apps/codecs/libcook/cook.c (revision 21039) +++ apps/codecs/libcook/cook.c (working copy) @@ -61,7 +61,7 @@ #define MAX_SUBPACKETS 5 //#define COOKDEBUG #if 0 -#define DEBUGF(message,args ...) printf +#define DEBUGF printf #else #define DEBUGF(...) #endif @@ -69,7 +69,7 @@ /** * Random bit stream generator. */ -static int inline cook_random(COOKContext *q) +static inline int cook_random(COOKContext *q) { q->random_state = q->random_state * 214013 + 2531011; /* typical RNG numbers */ @@ -200,7 +200,7 @@ i = 0; while (n--) { int index = get_bits(gb, 3); - int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1; + int gain = get_bits1(gb) ? (int)get_bits(gb, 4) - 7 : -1; while (i <= index) gaininfo[i++] = gain; } @@ -789,7 +789,7 @@ return -1; - if(q->block_align >= UINT_MAX/2) + if(rmctx->block_align >= UINT16_MAX/2) return -1; q->gains1.now = q->gain_1; Index: apps/codecs/codecs.make =================================================================== --- apps/codecs/codecs.make (revision 21039) +++ apps/codecs/codecs.make (working copy) @@ -33,6 +33,8 @@ include $(APPSDIR)/codecs/libtremor/libtremor.make include $(APPSDIR)/codecs/libwavpack/libwavpack.make include $(APPSDIR)/codecs/libwma/libwma.make +include $(APPSDIR)/codecs/libcook/libcook.make +include $(APPSDIR)/codecs/librm/librm.make # compile flags for codecs CODECFLAGS = $(CFLAGS) -I$(APPSDIR)/codecs -I$(APPSDIR)/codecs/lib \ @@ -47,7 +49,7 @@ CODECLIBS := $(DEMACLIB) $(A52LIB) $(ALACLIB) $(ASAPLIB) \ $(FAADLIB) $(FFMPEGFLACLIB) $(M4ALIB) $(MADLIB) $(MUSEPACKLIB) \ - $(SPCLIB) $(SPEEXLIB) $(TREMORLIB) $(WAVPACKLIB) $(WMALIB) \ + $(SPCLIB) $(SPEEXLIB) $(TREMORLIB) $(WAVPACKLIB) $(WMALIB) $(COOKLIB) $(RMLIB)\ $(CODECLIB) $(CODECS): $(CODEC_CRT0) $(CODECLINK_LDS) @@ -73,6 +75,7 @@ $(CODECDIR)/wma.codec : $(CODECDIR)/libwma.a $(CODECDIR)/wavpack_enc.codec: $(CODECDIR)/libwavpack.a $(CODECDIR)/asap.codec : $(CODECDIR)/libasap.a +$(CODECDIR)/cook.codec : $(CODECDIR)/libcook.a $(CODECS): $(CODECLIB) # this must be last in codec dependency list Index: apps/codecs/SOURCES =================================================================== --- apps/codecs/SOURCES (revision 21039) +++ apps/codecs/SOURCES (working copy) @@ -22,6 +22,7 @@ aiff.c speex.c adx.c +cook.c #if defined(HAVE_RECORDING) && !defined(SIMULATOR) /* encoders */ aiff_enc.c Index: apps/codecs/librm/rm.c =================================================================== --- apps/codecs/librm/rm.c (revision 21066) +++ apps/codecs/librm/rm.c (working copy) @@ -21,11 +21,6 @@ ****************************************************************************/ #include #include -#include -#include -#include -#include -#include #include "rm.h" @@ -52,7 +47,26 @@ { return (uint32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]); } +void advance_buffer(uint8_t **buf, int val) +{ + *buf += val; +} +#ifdef TEST +#include +#include +#include +int filesize(int fd) +{ + struct stat buf; + + if (fstat(fd,&buf) == -1) { + return -1; + } else { + return (int)buf.st_size; + } +} + static int read_uint8(int fd, uint8_t* buf) { unsigned char tmp[1]; @@ -83,22 +97,8 @@ return res; } -off_t filesize(int fd) -{ - struct stat buf; - if (fstat(fd,&buf) == -1) { - return -1; - } else { - return buf.st_size; - } -} -void advance_buffer(uint8_t **buf, int val) -{ - *buf += val; -} - int read_cook_extradata(int fd, RMContext *rmctx) { read_uint32be(fd, &rmctx->cook_version); read_uint16be(fd, &rmctx->samples_pf_pc); @@ -113,12 +113,12 @@ void print_cook_extradata(RMContext *rmctx) { - printf(" cook_version = 0x%08x\n", rmctx->cook_version); - printf(" samples_per_frame_per_channel = %d\n", rmctx->samples_pf_pc); - printf(" number_of_subbands_in_freq_domain = %d\n", rmctx->nb_subbands); + DEBUGF(" cook_version = 0x%08x\n", rmctx->cook_version); + DEBUGF(" samples_per_frame_per_channel = %d\n", rmctx->samples_pf_pc); + DEBUGF(" number_of_subbands_in_freq_domain = %d\n", rmctx->nb_subbands); if(rmctx->extradata_size == 16) { - printf(" joint_stereo_subband_start = %d\n",rmctx->js_subband_start); - printf(" joint_stereo_vlc_bits = %d\n", rmctx->js_vlc_bits); + DEBUGF(" joint_stereo_subband_start = %d\n",rmctx->js_subband_start); + DEBUGF(" joint_stereo_vlc_bits = %d\n", rmctx->js_vlc_bits); } } @@ -196,7 +196,7 @@ read_uint32be(fd, &version); skipped += 4; - printf(" version=0x%04x\n",((version >> 16) & 0xff)); + DEBUGF(" version=0x%04x\n",((version >> 16) & 0xff)); if (((version >> 16) & 0xff) == 3) { /* Very old version */ } else { @@ -205,7 +205,7 @@ read_uint32be(fd, &header_size); skipped += 4; /* obj.size will be filled with an unknown value, replaced with header_size */ - printf(" Object: %s, size: %d bytes, version: 0x%04x\n",fourcc2str(obj.fourcc),header_size,obj.version); + DEBUGF(" Object: %s, size: %d bytes, version: 0x%04x\n",fourcc2str(obj.fourcc),header_size,obj.version); read_uint16be(fd, &flavor); read_uint32be(fd, &coded_framesize); @@ -253,20 +253,22 @@ read_uint32be(fd, &rmctx->extradata_size); skipped += 4; - if(!strncmp(fourcc2str(fourcc),"cook",4)) + if(!strncmp(fourcc2str(fourcc),"cook",4)){ skipped += read_cook_extradata(fd, rmctx); + rmctx->codec_type = cook; + } - printf(" flavor = %d\n",flavor); - printf(" coded_frame_size = %d\n",coded_framesize); - printf(" sub_packet_h = %d\n",rmctx->sub_packet_h); - printf(" frame_size = %d\n",rmctx->block_align); - printf(" sub_packet_size = %d\n",rmctx->sub_packet_size); - printf(" sample_rate= %d\n",rmctx->sample_rate); - printf(" channels= %d\n",rmctx->nb_channels); - printf(" fourcc = %s\n",fourcc2str(fourcc)); - printf(" codec_extra_data_length = %d\n",rmctx->extradata_size); - printf(" codec_extradata :\n"); + DEBUGF(" flavor = %d\n",flavor); + DEBUGF(" coded_frame_size = %d\n",coded_framesize); + DEBUGF(" sub_packet_h = %d\n",rmctx->sub_packet_h); + DEBUGF(" frame_size = %d\n",rmctx->block_align); + DEBUGF(" sub_packet_size = %d\n",rmctx->sub_packet_size); + DEBUGF(" sample_rate= %d\n",rmctx->sample_rate); + DEBUGF(" channels= %d\n",rmctx->nb_channels); + DEBUGF(" fourcc = %s\n",fourcc2str(fourcc)); + DEBUGF(" codec_extra_data_length = %d\n",rmctx->extradata_size); + DEBUGF(" codec_extradata :\n"); print_cook_extradata(rmctx); } @@ -290,8 +292,8 @@ uint32_t max_packet_size; uint32_t avg_packet_size; uint32_t packet_count; + uint32_t preroll; uint32_t duration; - uint32_t preroll; uint32_t index_offset; uint16_t num_streams; uint16_t flags = 0; @@ -303,11 +305,6 @@ uint32_t codec_data_size; uint32_t v; - char title[256]; - char author[256]; - char copyright[256]; - char comment[256]; - uint32_t next_data_off; uint8_t header_end; @@ -327,15 +324,15 @@ read_uint32be(fd, &unknown1); read_uint32be(fd, &unknown2); - printf("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos); - printf(" unknown1=%d (0x%08x)\n",unknown1,unknown1); - printf(" unknown2=%d (0x%08x)\n",unknown2,unknown2); + DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos); + DEBUGF(" unknown1=%d (0x%08x)\n",unknown1,unknown1); + DEBUGF(" unknown2=%d (0x%08x)\n",unknown2,unknown2); res = real_read_object_header(fd, &obj); header_end = 0; while(res) { - printf("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos); + DEBUGF("Object: %s, size: %d bytes, version: 0x%04x, pos: %d\n",fourcc2str(obj.fourcc),(int)obj.size,obj.version,(int)curpos); skipped = 10; if(obj.fourcc == FOURCC('I','N','D','X')) break; @@ -347,7 +344,7 @@ read_uint32be(fd, &max_packet_size); read_uint32be(fd, &avg_packet_size); read_uint32be(fd, &packet_count); - read_uint32be(fd, &duration); + read_uint32be(fd, &rmctx->duration); read_uint32be(fd, &preroll); read_uint32be(fd, &index_offset); read_uint32be(fd, &rmctx->data_offset); @@ -355,30 +352,30 @@ read_uint16be(fd, &rmctx->flags); skipped += 40; - printf(" max_bitrate = %d\n",max_bitrate); - printf(" avg_bitrate = %d\n",avg_bitrate); - printf(" max_packet_size = %d\n",max_packet_size); - printf(" avg_packet_size = %d\n",avg_packet_size); - printf(" packet_count = %d\n",packet_count); - printf(" duration = %d\n",duration); - printf(" preroll = %d\n",preroll); - printf(" index_offset = %d\n",index_offset); - printf(" data_offset = %d\n",rmctx->data_offset); - printf(" num_streams = %d\n",num_streams); - printf(" flags=0x%04x\n",flags); + DEBUGF(" max_bitrate = %d\n",max_bitrate); + DEBUGF(" avg_bitrate = %d\n",avg_bitrate); + DEBUGF(" max_packet_size = %d\n",max_packet_size); + DEBUGF(" avg_packet_size = %d\n",avg_packet_size); + DEBUGF(" packet_count = %d\n",packet_count); + DEBUGF(" duration = %d\n",rmctx->duration); + DEBUGF(" preroll = %d\n",preroll); + DEBUGF(" index_offset = %d\n",index_offset); + DEBUGF(" data_offset = %d\n",rmctx->data_offset); + DEBUGF(" num_streams = %d\n",num_streams); + DEBUGF(" flags=0x%04x\n",flags); break; case FOURCC('C','O','N','T'): /* Four strings - Title, Author, Copyright, Comment */ - skipped += read_str(fd,title); - skipped += read_str(fd,author); - skipped += read_str(fd,copyright); - skipped += read_str(fd,comment); + skipped += read_str(fd,rmctx->title); + skipped += read_str(fd,rmctx->author); + skipped += read_str(fd,rmctx->copyright); + skipped += read_str(fd,rmctx->comment); - printf(" title=\"%s\"\n",title); - printf(" author=\"%s\"\n",author); - printf(" copyright=\"%s\"\n",copyright); - printf(" comment=\"%s\"\n",comment); + DEBUGF(" title=\"%s\"\n",rmctx->title); + DEBUGF(" author=\"%s\"\n",rmctx->author); + DEBUGF(" copyright=\"%s\"\n",rmctx->copyright); + DEBUGF(" comment=\"%s\"\n",rmctx->comment); break; case FOURCC('M','D','P','R'): /* Media properties */ @@ -406,18 +403,18 @@ read_uint32be(fd,&v); skipped += 4; - printf(" stream_id = 0x%04x\n",stream_id); - printf(" max_bitrate = %d\n",max_bitrate); - printf(" avg_bitrate = %d\n",avg_bitrate); - printf(" max_packet_size = %d\n",max_packet_size); - printf(" avg_packet_size = %d\n",avg_packet_size); - printf(" start_time = %d\n",start_time); - printf(" preroll = %d\n",preroll); - printf(" duration = %d\n",duration); - printf(" desc=\"%s\"\n",desc); - printf(" mimetype=\"%s\"\n",mimetype); - printf(" codec_data_size = %d\n",codec_data_size); - printf(" v=\"%s\"\n", fourcc2str(v)); + DEBUGF(" stream_id = 0x%04x\n",stream_id); + DEBUGF(" max_bitrate = %d\n",max_bitrate); + DEBUGF(" avg_bitrate = %d\n",avg_bitrate); + DEBUGF(" max_packet_size = %d\n",max_packet_size); + DEBUGF(" avg_packet_size = %d\n",avg_packet_size); + DEBUGF(" start_time = %d\n",start_time); + DEBUGF(" preroll = %d\n",preroll); + DEBUGF(" duration = %d\n",duration); + DEBUGF(" desc=\"%s\"\n",desc); + DEBUGF(" mimetype=\"%s\"\n",mimetype); + DEBUGF(" codec_data_size = %d\n",codec_data_size); + DEBUGF(" v=\"%s\"\n", fourcc2str(v)); if (v == FOURCC('.','r','a',0xfd)) { @@ -445,8 +442,8 @@ if(rmctx->nb_packets % rmctx->sub_packet_h) rmctx->nb_packets += rmctx->sub_packet_h - (rmctx->nb_packets % rmctx->sub_packet_h); - printf(" data_nb_packets = %d\n",rmctx->nb_packets); - printf(" next DATA offset = %d\n",next_data_off); + DEBUGF(" data_nb_packets = %d\n",rmctx->nb_packets); + DEBUGF(" next DATA offset = %d\n",next_data_off); header_end = 1; break; } @@ -504,13 +501,15 @@ //return pkt->data; } +#endif /*TEST*/ /** * Another version of rm_get_packet which reads from a memory buffer - * instead of readind from a file descriptor. + * instead of reading from a file descriptor. **/ -void rm_get_packet_membuf(uint8_t **filebuf,RMContext *rmctx, RMPacket *pkt) +int rm_get_packet_membuf(uint8_t **filebuf,RMContext *rmctx, RMPacket *pkt) { + int consumed = 0; uint8_t unknown; uint16_t x, place; uint16_t sps = rmctx->sub_packet_size; @@ -524,11 +523,10 @@ pkt->length = get_uint16be(*filebuf+2); pkt->stream_number = get_uint16be(*filebuf+4); pkt->timestamp = get_uint32be(*filebuf+6); - DEBUGF(" version = %d\n" + /*DEBUGF(" version = %d\n" " length = %d\n" " stream = %d\n" - " timestamp= %d\n",pkt->version,pkt->length,pkt->stream_number,pkt->timestamp); - + " timestamp= %d\n\n",pkt->version,pkt->length,pkt->stream_number,pkt->timestamp);*/ unknown = get_uint8(*filebuf+10); pkt->flags = get_uint8(*filebuf+11); @@ -541,15 +539,19 @@ rmctx->audiotimestamp = pkt->timestamp; advance_buffer(filebuf,12); - + consumed += 12; for(x = 0 ; x < w/sps; x++) { place = sps*(h*x+((h+1)/2)*(y&1)+(y>>1)); + //DEBUGF(" Place = %p\n",place); pkt->frames[place/sps] = *filebuf; advance_buffer(filebuf,sps); + consumed += sps; } rmctx->audio_pkt_cnt++; }while(++(rmctx->sub_packet_cnt) < h); + +return consumed; } #ifdef DEBUG Index: apps/codecs/librm/rm.h =================================================================== --- apps/codecs/librm/rm.h (revision 21066) +++ apps/codecs/librm/rm.h (working copy) @@ -22,8 +22,9 @@ #define _RM_H #include -#include +#include +enum codecs{cook}; typedef struct rm_packet { uint8_t data[30000]; /* Reordered data. No malloc, hence the size */ @@ -46,6 +47,7 @@ /* Stream Variables */ uint32_t data_offset; + uint32_t duration; uint32_t audiotimestamp; /* Audio packet timestamp*/ uint16_t sub_packet_cnt; /* Subpacket counter, used while reading */ uint16_t sub_packet_size, sub_packet_h, coded_framesize; /* Descrambling parameters from container */ @@ -53,6 +55,7 @@ uint16_t sub_packet_lengths[16]; /* Length of each subpacket */ /* Codec Context */ + enum codecs codec_type; uint16_t block_align; uint32_t nb_packets; int frame_number; @@ -71,13 +74,15 @@ uint16_t js_subband_start; /* joint stereo subband start */ uint16_t js_vlc_bits; + /* Metadata */ + char title[256]; + char author[256]; + char copyright[256]; + char comment[256]; + } RMContext; -int open_wav(char* filename); -void close_wav(int fd, RMContext *rmctx); int real_parse_header(int fd, RMContext *rmctx); void rm_get_packet(int fd,RMContext *rmctx, RMPacket *pkt); -void rm_get_packet_membuf(uint8_t **filebuf,RMContext *rmctx, RMPacket *pkt); -off_t filesize(int fd); -void advance_buffer(uint8_t **buf, int val); +int rm_get_packet_membuf(uint8_t **filebuf,RMContext *rmctx, RMPacket *pkt); #endif Index: apps/codecs/cook.c =================================================================== --- apps/codecs/cook.c (revision 0) +++ apps/codecs/cook.c (revision 0) @@ -0,0 +1,126 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * Copyright (C) 2009 Mohamed Tarek + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include + +#include "codeclib.h" +#include "inttypes.h" +#include "libcook/cook.h" + +#define DATA_HEADER_SIZE 18 /* size of DATA chunk header in a rm file */ + +CODEC_HEADER + +void init_rm(RMContext *rmctx) +{ + memcpy(rmctx, ci->id3->toc, sizeof(RMContext)); +} + +/* this is the codec entry point */ +enum codec_status codec_main(void) +{ +next_track: + if (codec_init()) { + DEBUGF("codec init failed\n"); + return CODEC_ERROR; + } + while (!*ci->taginfo_ready && !ci->stop_codec) + ci->sleep(1); + + static size_t buff_size; + int datasize, nb_frames, consumed,i; + uint8_t *bit_buffer; + int16_t outbuf[2048]; + uint16_t fs,sps,h; + uint32_t packet_count; + int scrambling_unit_size; + RMContext rmctx; + RMPacket pkt; + COOKContext q; + + codec_set_replaygain(ci->id3); + ci->memset(&rmctx,0,sizeof(RMContext)); + ci->memset(&pkt,0,sizeof(RMPacket)); + ci->memset(&q,0,sizeof(COOKContext)); + + init_rm(&rmctx); + DEBUGF("1-%s 2-%s 3-%s\n", ci->id3->id3v1buf[0], ci->id3->id3v1buf[1],ci->id3->id3v1buf[2]); + ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency); + ci->configure(DSP_SET_SAMPLE_DEPTH, 16); + ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ? + STEREO_MONO : STEREO_INTERLEAVED); + + packet_count = rmctx.nb_packets; + rmctx.audio_framesize = rmctx.block_align; + rmctx.block_align = rmctx.sub_packet_size; + fs = rmctx.audio_framesize; + sps= rmctx.block_align; + h = rmctx.sub_packet_h; + scrambling_unit_size = h*fs; + cook_decode_init(&rmctx, &q); + + ci->set_elapsed(0); + ci->advance_buffer(rmctx.data_offset + DATA_HEADER_SIZE); + + /* The main decoder loop */ + while (1) + { + /*if (ci->seek_time) { + + ci->set_elapsed(ci->seek_time); + n = ci->seek_time/10; + memset(buf,0,BUF_SIZE); + ci->seek_complete(); + }*/ + + while(packet_count) + { + ci->yield(); + if (ci->stop_codec || ci->new_track) + goto done; + + bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, scrambling_unit_size); + consumed = rm_get_packet_membuf(&bit_buffer, &rmctx, &pkt); + /*DEBUGF(" version = %d\n" + " length = %d\n" + " stream = %d\n" + " timestamp= %d\n",pkt.version,pkt.length,pkt.stream_number,pkt.timestamp);*/ + + for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++) + { + //bit_buffer += pkt.frames[i]; + nb_frames = cook_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i], rmctx.block_align); + rmctx.frame_number++; + ci->pcmbuf_insert(outbuf, NULL, rmctx.samples_pf_pc/rmctx.nb_channels); + ci->set_elapsed(rmctx.audiotimestamp+(1000*8*sps/rmctx.bit_rate)*i); + } + packet_count -= rmctx.audio_pkt_cnt; + rmctx.audio_pkt_cnt = 0; + ci->advance_buffer(consumed); + } + goto done; + + } + done : + if (ci->request_next_track()) + goto next_track; + + return CODEC_OK; +} +