flac.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:10k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*****************************************************************************
  2.  *
  3.  * This program is free software ; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  *
  8.  * This program is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  16.  *
  17.  * $Id: mpc.c 543 2006-01-07 22:06:24Z picard $
  18.  *
  19.  * The Core Pocket Media Player
  20.  * Copyright (c) 2006 Jory 'jcsston' Stone
  21.  *
  22.  ****************************************************************************/
  23.  
  24. #include "../common/common.h"
  25. #include "flac.h"
  26. #include "FLAC/seekable_stream_decoder.h"
  27. typedef struct flac 
  28. {
  29. format_base Format;
  30. FLAC__SeekableStreamDecoder *Decoder;
  31. FLAC__StreamMetadata_StreamInfo Info;
  32. int16_t* Buffer;
  33. int BufferLen;
  34. int BufferFilled;
  35. int SampleRate;
  36. int SampleSize;
  37. int64_t Samples;
  38. } flac;
  39. #undef malloc
  40. void* _calloc(int n,int m)
  41. {
  42. void* p = malloc(n*m);
  43. if (p) memset(p,0,n*m);
  44. return p;
  45. }
  46. // libFLAC callbacks
  47. static FLAC__SeekableStreamDecoderReadStatus ReadCallback(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  48. {
  49. format_reader* Reader = ((flac *)client_data)->Format.Reader;
  50. *bytes = Reader->Read(Reader,buffer,*bytes);
  51. return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
  52. }
  53. static FLAC__SeekableStreamDecoderSeekStatus SeekCallback(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  54. {
  55. format_reader* Reader = ((flac *)client_data)->Format.Reader;
  56. return Reader->Seek(Reader,(filepos_t)absolute_byte_offset,SEEK_SET) >= 0 ? FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK : FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
  57. }
  58. static FLAC__SeekableStreamDecoderTellStatus TellCallback(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  59. {
  60. format_reader* Reader = ((flac *)client_data)->Format.Reader;
  61. *absolute_byte_offset = Reader->FilePos;
  62. return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
  63. }
  64. static FLAC__SeekableStreamDecoderLengthStatus LengthCallback(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  65. {
  66. format_reader* Reader = ((flac *)client_data)->Format.Reader;
  67. if (Reader->Format->FileSize < 0)
  68. return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
  69. *stream_length = Reader->Format->FileSize;
  70. return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
  71. }
  72. static FLAC__bool EofCallback(const FLAC__SeekableStreamDecoder *decoder, void *client_data)
  73. {
  74. format_reader* Reader = ((flac *)client_data)->Format.Reader;
  75. return (Reader->Format->FileSize == Reader->FilePos) ? true : false;
  76. }
  77. static FLAC__StreamDecoderWriteStatus WriteCallback(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
  78. {
  79. flac* p = (flac *)client_data;  
  80. int16_t *pBuffer = p->Buffer;
  81. int size = frame->header.blocksize * frame->header.channels;
  82. int sizeChannel = frame->header.blocksize;
  83. int s;
  84. if (p->BufferLen < size)
  85. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  86. if (frame->header.channels == 1) 
  87. {
  88. for(s = 0; s < size; s++) 
  89. pBuffer[s] = (int16_t)buffer[0][s];
  90. else 
  91. {    
  92. // Interleave the channels    
  93. unsigned c;
  94. for(s = 0; s < sizeChannel; s++) 
  95. for (c = 0; c < frame->header.channels; c++) 
  96. *(pBuffer++) = (int16_t)buffer[c][s];
  97. }
  98. p->BufferFilled = sizeChannel;
  99. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  100. }
  101. static void MetadataCallback(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  102. {
  103. flac* p = (flac *)client_data;
  104. if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) 
  105. {
  106. p->Info = metadata->data.stream_info;
  107. else 
  108. if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) 
  109. {
  110. const FLAC__StreamMetadata_VorbisComment *comments = &metadata->data.vorbis_comment;
  111. tchar_t s[256];
  112.     
  113. if (p->Format.Comment.Node) 
  114. {
  115. uint32_t No;
  116. for (No = 0; No < comments->num_comments; ++No) 
  117. {
  118. UTF8ToTcs(s, TSIZEOF(s), (const char *)comments->comments[No].entry);
  119. p->Format.Comment.Node->Set(p->Format.Comment.Node, p->Format.Comment.No, s, sizeof(s));
  120. }
  121. }    
  122. }
  123. }
  124. static void ErrorCallback(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  125. {
  126. }
  127. static void Done(flac* p)
  128. {
  129. free(p->Buffer);
  130. p->Buffer = NULL;
  131. if (p->Decoder != NULL) 
  132. {
  133. FLAC__seekable_stream_decoder_delete(p->Decoder);
  134. p->Decoder = NULL;
  135. }
  136. }
  137. static int Init(flac* p)
  138. {
  139. format_stream* s;
  140. FLAC__StreamDecoderState state;
  141. p->Format.HeaderLoaded = 1;
  142. p->Format.TimeStamps = 0;
  143. p->Decoder = FLAC__seekable_stream_decoder_new();
  144. FLAC__seekable_stream_decoder_set_client_data(p->Decoder, p);
  145. FLAC__seekable_stream_decoder_set_read_callback(p->Decoder, ReadCallback);
  146. FLAC__seekable_stream_decoder_set_seek_callback(p->Decoder, SeekCallback);
  147. FLAC__seekable_stream_decoder_set_length_callback(p->Decoder, LengthCallback);
  148. FLAC__seekable_stream_decoder_set_tell_callback(p->Decoder, TellCallback);
  149. FLAC__seekable_stream_decoder_set_eof_callback(p->Decoder, EofCallback);
  150. FLAC__seekable_stream_decoder_set_write_callback(p->Decoder, WriteCallback);
  151. FLAC__seekable_stream_decoder_set_metadata_callback(p->Decoder, MetadataCallback);
  152. FLAC__seekable_stream_decoder_set_error_callback(p->Decoder, ErrorCallback);
  153. FLAC__seekable_stream_decoder_set_metadata_respond_all(p->Decoder);
  154. state = FLAC__seekable_stream_decoder_init(p->Decoder);
  155. p->Samples = 0;
  156. if (FLAC__seekable_stream_decoder_process_until_end_of_metadata(p->Decoder) != true)
  157. return ERR_INVALID_DATA;
  158. p->BufferLen = p->Info.max_blocksize * p->Info.channels;
  159. p->Buffer = (int16_t*)malloc(sizeof(int16_t)*p->BufferLen);
  160. if (!p->Buffer)
  161. return ERR_OUT_OF_MEMORY;
  162. s = Format_AddStream(&p->Format,sizeof(format_stream));
  163. if (s)
  164. {
  165. PacketFormatClear(&s->Format);
  166. s->Format.Type = PACKET_AUDIO;
  167. s->Format.Format.Audio.Format = AUDIOFMT_PCM;
  168. s->Format.Format.Audio.Bits = 16;
  169. s->Format.Format.Audio.SampleRate = p->Info.sample_rate;
  170. s->Format.Format.Audio.Channels = p->Info.channels;
  171. PacketFormatDefault(&s->Format);
  172. if (p->Format.FileSize>0)
  173. s->Format.ByteRate = (int)
  174. (((float) p->Format.FileSize / (int64_t)p->Info.total_samples * s->Format.Format.Audio.Channels * s->Format.Format.Audio.Bits / 8) 
  175. * s->Format.Format.Audio.SampleRate 
  176. * s->Format.Format.Audio.Channels 
  177. * s->Format.Format.Audio.Bits / 128);
  178. s->Fragmented = 1;
  179. s->DisableDrop = 1;
  180. p->Format.Duration = (tick_t)((p->Info.total_samples * TICKSPERSEC) / p->Info.sample_rate);
  181. Format_PrepairStream(&p->Format,s);
  182. }
  183. p->SampleRate = p->Info.sample_rate;
  184. p->SampleSize = p->Info.channels * sizeof(int16_t);
  185. return ERR_NONE;
  186. }
  187. static int Seek(flac* p, tick_t Time, int FilePos, bool_t PrevKey)
  188. {
  189. int64_t Samples;
  190. if (Time < 0)
  191. {
  192. if (FilePos<0 || p->Format.FileSize<0)
  193. return ERR_NOT_SUPPORTED;
  194. Time = Scale(FilePos,p->Format.Duration,p->Format.FileSize);
  195. }
  196. Samples = ((int64_t)Time * p->SampleRate+(TICKSPERSEC/2)) / TICKSPERSEC;
  197. if (!FLAC__seekable_stream_decoder_seek_absolute(p->Decoder,Samples))
  198. return ERR_NOT_SUPPORTED;
  199. Format_AfterSeek(&p->Format);
  200. p->Samples = Samples;
  201. return ERR_NONE;
  202. }
  203. static int Process(flac* p,format_stream* Stream)
  204. {
  205. int Result = ERR_NONE;
  206. if (Stream->Pending)
  207. {
  208. Result = Format_Send(&p->Format,Stream);
  209. if (Result == ERR_BUFFER_FULL || Result == ERR_SYNCED)
  210. return Result;
  211. }
  212. if (p->Format.Reader[0].BufferAvailable < (MINBUFFER/2) && !p->Format.Reader[0].NoMoreInput)
  213. return ERR_NEED_MORE_DATA;
  214.     p->BufferFilled = 0;
  215.     if (FLAC__seekable_stream_decoder_process_single(p->Decoder) != true) 
  216. {
  217. FLAC__StreamDecoderState state;
  218. // Check state
  219. state = FLAC__seekable_stream_decoder_get_stream_decoder_state(p->Decoder);
  220. switch (state) 
  221. {
  222. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  223. case FLAC__STREAM_DECODER_READ_METADATA:
  224. // We should have already read in the metadata
  225. return ERR_INVALID_DATA;
  226. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:          
  227. case FLAC__STREAM_DECODER_READ_FRAME:
  228. // Need more data
  229. return ERR_NEED_MORE_DATA;
  230. case FLAC__STREAM_DECODER_END_OF_STREAM:          
  231. case FLAC__STREAM_DECODER_ABORTED:
  232. case FLAC__STREAM_DECODER_UNPARSEABLE_STREAM:
  233. return ERR_INVALID_DATA;
  234. case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
  235. return ERR_OUT_OF_MEMORY;
  236. case FLAC__STREAM_DECODER_ALREADY_INITIALIZED:
  237. case FLAC__STREAM_DECODER_INVALID_CALLBACK:
  238. case FLAC__STREAM_DECODER_UNINITIALIZED:
  239. // I don't see how this could happen
  240. return ERR_INVALID_DATA;
  241. }
  242. return ERR_INVALID_DATA;
  243.     }
  244. if (p->BufferFilled == 0)
  245. return Format_CheckEof(&p->Format,Stream);
  246. Stream->Packet.RefTime = (tick_t)((p->Samples * TICKSPERSEC) / p->SampleRate);
  247. Stream->Packet.Data[0] = p->Buffer;
  248. Stream->Packet.Length = p->BufferFilled * p->SampleSize;
  249. Stream->Pending = 1;
  250. p->Samples += p->BufferFilled;
  251. Result = Format_Send(&p->Format,Stream);
  252. if (Result == ERR_BUFFER_FULL || Result == ERR_NEED_MORE_DATA)
  253. Result = ERR_NONE;
  254. return Result;
  255. }
  256. static int Create(flac* p)
  257. {
  258. p->Format.Init = (fmtfunc) Init;
  259. p->Format.Done = (fmtvoid) Done;
  260. p->Format.Seek = (fmtseek) Seek;
  261. p->Format.Process = (fmtstreamprocess) Process;
  262. p->Format.FillQueue = NULL;
  263. p->Format.ReadPacket = NULL;
  264. p->Format.Sended = NULL;
  265. return ERR_NONE;
  266. }
  267. static const nodedef FLAC =
  268. {
  269. sizeof(flac),
  270. FLAC_ID,
  271. FORMATBASE_CLASS,
  272. PRI_DEFAULT,
  273. (nodecreate)Create,
  274. };
  275. void FLAC_Init()
  276. {
  277. NodeRegisterClass(&FLAC);
  278. }
  279. void FLAC_Done()
  280. {
  281. NodeUnRegisterClass(FLAC_ID);
  282. }