flac_stream_decoder.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #if defined (_WIN32)
  35. #include <io.h> /* for _setmode() */
  36. #include <fcntl.h> /* for _O_BINARY */
  37. #endif
  38. #include <stdio.h>
  39. #include <stdlib.h> /* for malloc() */
  40. #include <string.h> /* for memset/memcpy() */
  41. #include <sys/stat.h> /* for stat() */
  42. #include <sys/types.h> /* for off_t */
  43. #include "flac_FLAC_assert.h"
  44. #include "flac_share_alloc.h"
  45. #include "flac_protected_stream_decoder.h"
  46. #include "flac_private_autocpu.h"
  47. #include "flac_private_bitreader.h"
  48. #include "flac_private_bitmath.h"
  49. #include "flac_private_cpu.h"
  50. #include "flac_private_crc.h"
  51. #include "flac_private_fixed.h"
  52. #include "flac_private_format.h"
  53. #include "flac_private_lpc.h"
  54. #include "flac_private_md5.h"
  55. #include "flac_private_memory.h"
  56. #ifdef max
  57. #undef max
  58. #endif
  59. #define max(a,b) ((a)>(b)?(a):(b))
  60. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  61. #ifdef _MSC_VER
  62. #define FLAC__U64L(x) x
  63. #else
  64. #define FLAC__U64L(x) x##LLU
  65. #endif
  66. /* technically this should be in an "export.c" but this is convenient enough */
  67. FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
  68. #if FLAC__HAS_OGG
  69. 1
  70. #else
  71. 0
  72. #endif
  73. ;
  74. /***********************************************************************
  75. *
  76. * Private static data
  77. *
  78. ***********************************************************************/
  79. static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
  80. /***********************************************************************
  81. *
  82. * Private class method prototypes
  83. *
  84. ***********************************************************************/
  85. static void set_defaults_(FLAC__StreamDecoder *decoder);
  86. static FILE *get_binary_stdin_(void);
  87. static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
  88. static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
  89. static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
  90. static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
  91. static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
  92. static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
  93. static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
  94. static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
  95. static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
  96. static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
  97. static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
  98. static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
  99. static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
  100. static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
  101. static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
  102. static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
  103. static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
  104. static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
  105. static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended);
  106. static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
  107. static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
  108. #if FLAC__HAS_OGG
  109. static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
  110. static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
  111. #endif
  112. static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
  113. static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
  114. static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
  115. #if FLAC__HAS_OGG
  116. static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
  117. #endif
  118. static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
  119. static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
  120. static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
  121. static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
  122. static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
  123. /***********************************************************************
  124. *
  125. * Private class data
  126. *
  127. ***********************************************************************/
  128. typedef struct FLAC__StreamDecoderPrivate {
  129. #if FLAC__HAS_OGG
  130. FLAC__bool is_ogg;
  131. #endif
  132. FLAC__StreamDecoderReadCallback read_callback;
  133. FLAC__StreamDecoderSeekCallback seek_callback;
  134. FLAC__StreamDecoderTellCallback tell_callback;
  135. FLAC__StreamDecoderLengthCallback length_callback;
  136. FLAC__StreamDecoderEofCallback eof_callback;
  137. FLAC__StreamDecoderWriteCallback write_callback;
  138. FLAC__StreamDecoderMetadataCallback metadata_callback;
  139. FLAC__StreamDecoderErrorCallback error_callback;
  140. /* generic 32-bit datapath: */
  141. void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  142. /* generic 64-bit datapath: */
  143. void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  144. /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
  145. void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  146. /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
  147. void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
  148. FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter);
  149. void *client_data;
  150. FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
  151. FLAC__BitReader *input;
  152. FLAC__int32 *output[FLAC__MAX_CHANNELS];
  153. FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
  154. FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
  155. unsigned output_capacity, output_channels;
  156. FLAC__uint32 fixed_block_size, next_fixed_block_size;
  157. FLAC__uint64 samples_decoded;
  158. FLAC__bool has_stream_info, has_seek_table;
  159. FLAC__StreamMetadata stream_info;
  160. FLAC__StreamMetadata seek_table;
  161. FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
  162. FLAC__byte *metadata_filter_ids;
  163. size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
  164. FLAC__Frame frame;
  165. FLAC__bool cached; /* true if there is a byte in lookahead */
  166. FLAC__CPUInfo cpuinfo;
  167. FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
  168. FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
  169. /* unaligned (original) pointers to allocated data */
  170. FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
  171. FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
  172. FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
  173. FLAC__bool is_seeking;
  174. FLAC__MD5Context md5context;
  175. FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
  176. /* (the rest of these are only used for seeking) */
  177. FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
  178. FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
  179. FLAC__uint64 target_sample;
  180. unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
  181. #if FLAC__HAS_OGG
  182. FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
  183. #endif
  184. } FLAC__StreamDecoderPrivate;
  185. /***********************************************************************
  186. *
  187. * Public static class data
  188. *
  189. ***********************************************************************/
  190. FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
  191. "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
  192. "FLAC__STREAM_DECODER_READ_METADATA",
  193. "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
  194. "FLAC__STREAM_DECODER_READ_FRAME",
  195. "FLAC__STREAM_DECODER_END_OF_STREAM",
  196. "FLAC__STREAM_DECODER_OGG_ERROR",
  197. "FLAC__STREAM_DECODER_SEEK_ERROR",
  198. "FLAC__STREAM_DECODER_ABORTED",
  199. "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
  200. "FLAC__STREAM_DECODER_UNINITIALIZED"
  201. };
  202. FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
  203. "FLAC__STREAM_DECODER_INIT_STATUS_OK",
  204. "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
  205. "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
  206. "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
  207. "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
  208. "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
  209. };
  210. FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
  211. "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
  212. "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
  213. "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
  214. };
  215. FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
  216. "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
  217. "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
  218. "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
  219. };
  220. FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
  221. "FLAC__STREAM_DECODER_TELL_STATUS_OK",
  222. "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
  223. "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
  224. };
  225. FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
  226. "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
  227. "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
  228. "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
  229. };
  230. FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
  231. "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
  232. "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
  233. };
  234. FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
  235. "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
  236. "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
  237. "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
  238. "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
  239. };
  240. /***********************************************************************
  241. *
  242. * Class constructor/destructor
  243. *
  244. ***********************************************************************/
  245. FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
  246. {
  247. FLAC__StreamDecoder *decoder;
  248. unsigned i;
  249. FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
  250. decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
  251. if(decoder == 0) {
  252. return 0;
  253. }
  254. decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
  255. if(decoder->protected_ == 0) {
  256. free(decoder);
  257. return 0;
  258. }
  259. decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
  260. if(decoder->private_ == 0) {
  261. free(decoder->protected_);
  262. free(decoder);
  263. return 0;
  264. }
  265. decoder->private_->input = FLAC__bitreader_new();
  266. if(decoder->private_->input == 0) {
  267. free(decoder->private_);
  268. free(decoder->protected_);
  269. free(decoder);
  270. return 0;
  271. }
  272. decoder->private_->metadata_filter_ids_capacity = 16;
  273. if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
  274. FLAC__bitreader_delete(decoder->private_->input);
  275. free(decoder->private_);
  276. free(decoder->protected_);
  277. free(decoder);
  278. return 0;
  279. }
  280. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  281. decoder->private_->output[i] = 0;
  282. decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
  283. }
  284. decoder->private_->output_capacity = 0;
  285. decoder->private_->output_channels = 0;
  286. decoder->private_->has_seek_table = false;
  287. for(i = 0; i < FLAC__MAX_CHANNELS; i++)
  288. FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
  289. decoder->private_->file = 0;
  290. set_defaults_(decoder);
  291. decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
  292. return decoder;
  293. }
  294. FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
  295. {
  296. unsigned i;
  297. FLAC__ASSERT(0 != decoder);
  298. FLAC__ASSERT(0 != decoder->protected_);
  299. FLAC__ASSERT(0 != decoder->private_);
  300. FLAC__ASSERT(0 != decoder->private_->input);
  301. (void)FLAC__stream_decoder_finish(decoder);
  302. if(0 != decoder->private_->metadata_filter_ids)
  303. free(decoder->private_->metadata_filter_ids);
  304. FLAC__bitreader_delete(decoder->private_->input);
  305. for(i = 0; i < FLAC__MAX_CHANNELS; i++)
  306. FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
  307. free(decoder->private_);
  308. free(decoder->protected_);
  309. free(decoder);
  310. }
  311. /***********************************************************************
  312. *
  313. * Public class methods
  314. *
  315. ***********************************************************************/
  316. static FLAC__StreamDecoderInitStatus init_stream_internal_(
  317. FLAC__StreamDecoder *decoder,
  318. FLAC__StreamDecoderReadCallback read_callback,
  319. FLAC__StreamDecoderSeekCallback seek_callback,
  320. FLAC__StreamDecoderTellCallback tell_callback,
  321. FLAC__StreamDecoderLengthCallback length_callback,
  322. FLAC__StreamDecoderEofCallback eof_callback,
  323. FLAC__StreamDecoderWriteCallback write_callback,
  324. FLAC__StreamDecoderMetadataCallback metadata_callback,
  325. FLAC__StreamDecoderErrorCallback error_callback,
  326. void *client_data,
  327. FLAC__bool is_ogg
  328. )
  329. {
  330. FLAC__ASSERT(0 != decoder);
  331. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  332. return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
  333. #if !FLAC__HAS_OGG
  334. if(is_ogg)
  335. return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
  336. #endif
  337. if(
  338. 0 == read_callback ||
  339. 0 == write_callback ||
  340. 0 == error_callback ||
  341. (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
  342. )
  343. return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
  344. #if FLAC__HAS_OGG
  345. decoder->private_->is_ogg = is_ogg;
  346. if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
  347. return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
  348. #endif
  349. /*
  350. * get the CPU info and set the function pointers
  351. */
  352. FLAC__cpu_info(&decoder->private_->cpuinfo);
  353. /* first default to the non-asm routines */
  354. decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
  355. decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
  356. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
  357. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
  358. decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
  359. /* now override with asm where appropriate */
  360. #ifndef FLAC__NO_ASM
  361. if(decoder->private_->cpuinfo.use_asm) {
  362. #ifdef FLAC__CPU_IA32
  363. FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
  364. #ifdef FLAC__HAS_NASM
  365. #if 1 /*@@@@@@ OPT: not clearly faster, needs more testing */
  366. if(decoder->private_->cpuinfo.data.ia32.bswap)
  367. decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_asm_ia32_bswap;
  368. #endif
  369. if(decoder->private_->cpuinfo.data.ia32.mmx) {
  370. decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
  371. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
  372. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
  373. }
  374. else {
  375. decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
  376. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
  377. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
  378. }
  379. #endif
  380. #elif defined FLAC__CPU_PPC
  381. FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
  382. if(decoder->private_->cpuinfo.data.ppc.altivec) {
  383. decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
  384. decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
  385. }
  386. #endif
  387. }
  388. #endif
  389. /* from here on, errors are fatal */
  390. if(!FLAC__bitreader_init(decoder->private_->input, decoder->private_->cpuinfo, read_callback_, decoder)) {
  391. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  392. return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
  393. }
  394. decoder->private_->read_callback = read_callback;
  395. decoder->private_->seek_callback = seek_callback;
  396. decoder->private_->tell_callback = tell_callback;
  397. decoder->private_->length_callback = length_callback;
  398. decoder->private_->eof_callback = eof_callback;
  399. decoder->private_->write_callback = write_callback;
  400. decoder->private_->metadata_callback = metadata_callback;
  401. decoder->private_->error_callback = error_callback;
  402. decoder->private_->client_data = client_data;
  403. decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
  404. decoder->private_->samples_decoded = 0;
  405. decoder->private_->has_stream_info = false;
  406. decoder->private_->cached = false;
  407. decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
  408. decoder->private_->is_seeking = false;
  409. decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
  410. if(!FLAC__stream_decoder_reset(decoder)) {
  411. /* above call sets the state for us */
  412. return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
  413. }
  414. return FLAC__STREAM_DECODER_INIT_STATUS_OK;
  415. }
  416. FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
  417. FLAC__StreamDecoder *decoder,
  418. FLAC__StreamDecoderReadCallback read_callback,
  419. FLAC__StreamDecoderSeekCallback seek_callback,
  420. FLAC__StreamDecoderTellCallback tell_callback,
  421. FLAC__StreamDecoderLengthCallback length_callback,
  422. FLAC__StreamDecoderEofCallback eof_callback,
  423. FLAC__StreamDecoderWriteCallback write_callback,
  424. FLAC__StreamDecoderMetadataCallback metadata_callback,
  425. FLAC__StreamDecoderErrorCallback error_callback,
  426. void *client_data
  427. )
  428. {
  429. return init_stream_internal_(
  430. decoder,
  431. read_callback,
  432. seek_callback,
  433. tell_callback,
  434. length_callback,
  435. eof_callback,
  436. write_callback,
  437. metadata_callback,
  438. error_callback,
  439. client_data,
  440. /*is_ogg=*/false
  441. );
  442. }
  443. FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
  444. FLAC__StreamDecoder *decoder,
  445. FLAC__StreamDecoderReadCallback read_callback,
  446. FLAC__StreamDecoderSeekCallback seek_callback,
  447. FLAC__StreamDecoderTellCallback tell_callback,
  448. FLAC__StreamDecoderLengthCallback length_callback,
  449. FLAC__StreamDecoderEofCallback eof_callback,
  450. FLAC__StreamDecoderWriteCallback write_callback,
  451. FLAC__StreamDecoderMetadataCallback metadata_callback,
  452. FLAC__StreamDecoderErrorCallback error_callback,
  453. void *client_data
  454. )
  455. {
  456. return init_stream_internal_(
  457. decoder,
  458. read_callback,
  459. seek_callback,
  460. tell_callback,
  461. length_callback,
  462. eof_callback,
  463. write_callback,
  464. metadata_callback,
  465. error_callback,
  466. client_data,
  467. /*is_ogg=*/true
  468. );
  469. }
  470. static FLAC__StreamDecoderInitStatus init_FILE_internal_(
  471. FLAC__StreamDecoder *decoder,
  472. FILE *file,
  473. FLAC__StreamDecoderWriteCallback write_callback,
  474. FLAC__StreamDecoderMetadataCallback metadata_callback,
  475. FLAC__StreamDecoderErrorCallback error_callback,
  476. void *client_data,
  477. FLAC__bool is_ogg
  478. )
  479. {
  480. FLAC__ASSERT(0 != decoder);
  481. FLAC__ASSERT(0 != file);
  482. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  483. return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
  484. if(0 == write_callback || 0 == error_callback)
  485. return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
  486. /*
  487. * To make sure that our file does not go unclosed after an error, we
  488. * must assign the FILE pointer before any further error can occur in
  489. * this routine.
  490. */
  491. if(file == stdin)
  492. file = get_binary_stdin_(); /* just to be safe */
  493. decoder->private_->file = file;
  494. return init_stream_internal_(
  495. decoder,
  496. file_read_callback_,
  497. decoder->private_->file == stdin? 0: file_seek_callback_,
  498. decoder->private_->file == stdin? 0: file_tell_callback_,
  499. decoder->private_->file == stdin? 0: file_length_callback_,
  500. file_eof_callback_,
  501. write_callback,
  502. metadata_callback,
  503. error_callback,
  504. client_data,
  505. is_ogg
  506. );
  507. }
  508. FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
  509. FLAC__StreamDecoder *decoder,
  510. FILE *file,
  511. FLAC__StreamDecoderWriteCallback write_callback,
  512. FLAC__StreamDecoderMetadataCallback metadata_callback,
  513. FLAC__StreamDecoderErrorCallback error_callback,
  514. void *client_data
  515. )
  516. {
  517. return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
  518. }
  519. FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
  520. FLAC__StreamDecoder *decoder,
  521. FILE *file,
  522. FLAC__StreamDecoderWriteCallback write_callback,
  523. FLAC__StreamDecoderMetadataCallback metadata_callback,
  524. FLAC__StreamDecoderErrorCallback error_callback,
  525. void *client_data
  526. )
  527. {
  528. return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
  529. }
  530. static FLAC__StreamDecoderInitStatus init_file_internal_(
  531. FLAC__StreamDecoder *decoder,
  532. const char *filename,
  533. FLAC__StreamDecoderWriteCallback write_callback,
  534. FLAC__StreamDecoderMetadataCallback metadata_callback,
  535. FLAC__StreamDecoderErrorCallback error_callback,
  536. void *client_data,
  537. FLAC__bool is_ogg
  538. )
  539. {
  540. FILE *file;
  541. FLAC__ASSERT(0 != decoder);
  542. /*
  543. * To make sure that our file does not go unclosed after an error, we
  544. * have to do the same entrance checks here that are later performed
  545. * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
  546. */
  547. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  548. return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
  549. if(0 == write_callback || 0 == error_callback)
  550. return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
  551. file = filename? fopen(filename, "rb") : stdin;
  552. if(0 == file)
  553. return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
  554. return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
  555. }
  556. FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
  557. FLAC__StreamDecoder *decoder,
  558. const char *filename,
  559. FLAC__StreamDecoderWriteCallback write_callback,
  560. FLAC__StreamDecoderMetadataCallback metadata_callback,
  561. FLAC__StreamDecoderErrorCallback error_callback,
  562. void *client_data
  563. )
  564. {
  565. return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
  566. }
  567. FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
  568. FLAC__StreamDecoder *decoder,
  569. const char *filename,
  570. FLAC__StreamDecoderWriteCallback write_callback,
  571. FLAC__StreamDecoderMetadataCallback metadata_callback,
  572. FLAC__StreamDecoderErrorCallback error_callback,
  573. void *client_data
  574. )
  575. {
  576. return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
  577. }
  578. FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
  579. {
  580. FLAC__bool md5_failed = false;
  581. unsigned i;
  582. FLAC__ASSERT(0 != decoder);
  583. FLAC__ASSERT(0 != decoder->private_);
  584. FLAC__ASSERT(0 != decoder->protected_);
  585. if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
  586. return true;
  587. /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
  588. * always call FLAC__MD5Final()
  589. */
  590. FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
  591. if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
  592. free(decoder->private_->seek_table.data.seek_table.points);
  593. decoder->private_->seek_table.data.seek_table.points = 0;
  594. decoder->private_->has_seek_table = false;
  595. }
  596. FLAC__bitreader_free(decoder->private_->input);
  597. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  598. /* WATCHOUT:
  599. * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
  600. * output arrays have a buffer of up to 3 zeroes in front
  601. * (at negative indices) for alignment purposes; we use 4
  602. * to keep the data well-aligned.
  603. */
  604. if(0 != decoder->private_->output[i]) {
  605. free(decoder->private_->output[i]-4);
  606. decoder->private_->output[i] = 0;
  607. }
  608. if(0 != decoder->private_->residual_unaligned[i]) {
  609. free(decoder->private_->residual_unaligned[i]);
  610. decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
  611. }
  612. }
  613. decoder->private_->output_capacity = 0;
  614. decoder->private_->output_channels = 0;
  615. #if FLAC__HAS_OGG
  616. if(decoder->private_->is_ogg)
  617. FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
  618. #endif
  619. if(0 != decoder->private_->file) {
  620. if(decoder->private_->file != stdin)
  621. fclose(decoder->private_->file);
  622. decoder->private_->file = 0;
  623. }
  624. if(decoder->private_->do_md5_checking) {
  625. if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
  626. md5_failed = true;
  627. }
  628. decoder->private_->is_seeking = false;
  629. set_defaults_(decoder);
  630. decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
  631. return !md5_failed;
  632. }
  633. FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
  634. {
  635. FLAC__ASSERT(0 != decoder);
  636. FLAC__ASSERT(0 != decoder->private_);
  637. FLAC__ASSERT(0 != decoder->protected_);
  638. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  639. return false;
  640. #if FLAC__HAS_OGG
  641. /* can't check decoder->private_->is_ogg since that's not set until init time */
  642. FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
  643. return true;
  644. #else
  645. (void)value;
  646. return false;
  647. #endif
  648. }
  649. FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
  650. {
  651. FLAC__ASSERT(0 != decoder);
  652. FLAC__ASSERT(0 != decoder->protected_);
  653. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  654. return false;
  655. decoder->protected_->md5_checking = value;
  656. return true;
  657. }
  658. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
  659. {
  660. FLAC__ASSERT(0 != decoder);
  661. FLAC__ASSERT(0 != decoder->private_);
  662. FLAC__ASSERT(0 != decoder->protected_);
  663. FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
  664. /* double protection */
  665. if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
  666. return false;
  667. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  668. return false;
  669. decoder->private_->metadata_filter[type] = true;
  670. if(type == FLAC__METADATA_TYPE_APPLICATION)
  671. decoder->private_->metadata_filter_ids_count = 0;
  672. return true;
  673. }
  674. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
  675. {
  676. FLAC__ASSERT(0 != decoder);
  677. FLAC__ASSERT(0 != decoder->private_);
  678. FLAC__ASSERT(0 != decoder->protected_);
  679. FLAC__ASSERT(0 != id);
  680. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  681. return false;
  682. if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
  683. return true;
  684. FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
  685. if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
  686. if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
  687. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  688. return false;
  689. }
  690. decoder->private_->metadata_filter_ids_capacity *= 2;
  691. }
  692. memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
  693. decoder->private_->metadata_filter_ids_count++;
  694. return true;
  695. }
  696. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
  697. {
  698. unsigned i;
  699. FLAC__ASSERT(0 != decoder);
  700. FLAC__ASSERT(0 != decoder->private_);
  701. FLAC__ASSERT(0 != decoder->protected_);
  702. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  703. return false;
  704. for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
  705. decoder->private_->metadata_filter[i] = true;
  706. decoder->private_->metadata_filter_ids_count = 0;
  707. return true;
  708. }
  709. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
  710. {
  711. FLAC__ASSERT(0 != decoder);
  712. FLAC__ASSERT(0 != decoder->private_);
  713. FLAC__ASSERT(0 != decoder->protected_);
  714. FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
  715. /* double protection */
  716. if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
  717. return false;
  718. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  719. return false;
  720. decoder->private_->metadata_filter[type] = false;
  721. if(type == FLAC__METADATA_TYPE_APPLICATION)
  722. decoder->private_->metadata_filter_ids_count = 0;
  723. return true;
  724. }
  725. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
  726. {
  727. FLAC__ASSERT(0 != decoder);
  728. FLAC__ASSERT(0 != decoder->private_);
  729. FLAC__ASSERT(0 != decoder->protected_);
  730. FLAC__ASSERT(0 != id);
  731. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  732. return false;
  733. if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
  734. return true;
  735. FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
  736. if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
  737. if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
  738. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  739. return false;
  740. }
  741. decoder->private_->metadata_filter_ids_capacity *= 2;
  742. }
  743. memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
  744. decoder->private_->metadata_filter_ids_count++;
  745. return true;
  746. }
  747. FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
  748. {
  749. FLAC__ASSERT(0 != decoder);
  750. FLAC__ASSERT(0 != decoder->private_);
  751. FLAC__ASSERT(0 != decoder->protected_);
  752. if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
  753. return false;
  754. memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
  755. decoder->private_->metadata_filter_ids_count = 0;
  756. return true;
  757. }
  758. FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
  759. {
  760. FLAC__ASSERT(0 != decoder);
  761. FLAC__ASSERT(0 != decoder->protected_);
  762. return decoder->protected_->state;
  763. }
  764. FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
  765. {
  766. return FLAC__StreamDecoderStateString[decoder->protected_->state];
  767. }
  768. FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
  769. {
  770. FLAC__ASSERT(0 != decoder);
  771. FLAC__ASSERT(0 != decoder->protected_);
  772. return decoder->protected_->md5_checking;
  773. }
  774. FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
  775. {
  776. FLAC__ASSERT(0 != decoder);
  777. FLAC__ASSERT(0 != decoder->protected_);
  778. return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
  779. }
  780. FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
  781. {
  782. FLAC__ASSERT(0 != decoder);
  783. FLAC__ASSERT(0 != decoder->protected_);
  784. return decoder->protected_->channels;
  785. }
  786. FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
  787. {
  788. FLAC__ASSERT(0 != decoder);
  789. FLAC__ASSERT(0 != decoder->protected_);
  790. return decoder->protected_->channel_assignment;
  791. }
  792. FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
  793. {
  794. FLAC__ASSERT(0 != decoder);
  795. FLAC__ASSERT(0 != decoder->protected_);
  796. return decoder->protected_->bits_per_sample;
  797. }
  798. FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
  799. {
  800. FLAC__ASSERT(0 != decoder);
  801. FLAC__ASSERT(0 != decoder->protected_);
  802. return decoder->protected_->sample_rate;
  803. }
  804. FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
  805. {
  806. FLAC__ASSERT(0 != decoder);
  807. FLAC__ASSERT(0 != decoder->protected_);
  808. return decoder->protected_->blocksize;
  809. }
  810. FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
  811. {
  812. FLAC__ASSERT(0 != decoder);
  813. FLAC__ASSERT(0 != decoder->private_);
  814. FLAC__ASSERT(0 != position);
  815. #if FLAC__HAS_OGG
  816. if(decoder->private_->is_ogg)
  817. return false;
  818. #endif
  819. if(0 == decoder->private_->tell_callback)
  820. return false;
  821. if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
  822. return false;
  823. /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
  824. if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
  825. return false;
  826. FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
  827. *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
  828. return true;
  829. }
  830. FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
  831. {
  832. FLAC__ASSERT(0 != decoder);
  833. FLAC__ASSERT(0 != decoder->private_);
  834. FLAC__ASSERT(0 != decoder->protected_);
  835. decoder->private_->samples_decoded = 0;
  836. decoder->private_->do_md5_checking = false;
  837. #if FLAC__HAS_OGG
  838. if(decoder->private_->is_ogg)
  839. FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
  840. #endif
  841. if(!FLAC__bitreader_clear(decoder->private_->input)) {
  842. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  843. return false;
  844. }
  845. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  846. return true;
  847. }
  848. FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
  849. {
  850. FLAC__ASSERT(0 != decoder);
  851. FLAC__ASSERT(0 != decoder->private_);
  852. FLAC__ASSERT(0 != decoder->protected_);
  853. if(!FLAC__stream_decoder_flush(decoder)) {
  854. /* above call sets the state for us */
  855. return false;
  856. }
  857. #if FLAC__HAS_OGG
  858. /*@@@ could go in !internal_reset_hack block below */
  859. if(decoder->private_->is_ogg)
  860. FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
  861. #endif
  862. /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
  863. * (internal_reset_hack) don't try to rewind since we are already at
  864. * the beginning of the stream and don't want to fail if the input is
  865. * not seekable.
  866. */
  867. if(!decoder->private_->internal_reset_hack) {
  868. if(decoder->private_->file == stdin)
  869. return false; /* can't rewind stdin, reset fails */
  870. if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
  871. return false; /* seekable and seek fails, reset fails */
  872. }
  873. else
  874. decoder->private_->internal_reset_hack = false;
  875. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
  876. decoder->private_->has_stream_info = false;
  877. if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
  878. free(decoder->private_->seek_table.data.seek_table.points);
  879. decoder->private_->seek_table.data.seek_table.points = 0;
  880. decoder->private_->has_seek_table = false;
  881. }
  882. decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
  883. /*
  884. * This goes in reset() and not flush() because according to the spec, a
  885. * fixed-blocksize stream must stay that way through the whole stream.
  886. */
  887. decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
  888. /* We initialize the FLAC__MD5Context even though we may never use it. This
  889. * is because md5 checking may be turned on to start and then turned off if
  890. * a seek occurs. So we init the context here and finalize it in
  891. * FLAC__stream_decoder_finish() to make sure things are always cleaned up
  892. * properly.
  893. */
  894. FLAC__MD5Init(&decoder->private_->md5context);
  895. decoder->private_->first_frame_offset = 0;
  896. decoder->private_->unparseable_frame_count = 0;
  897. return true;
  898. }
  899. FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
  900. {
  901. FLAC__bool got_a_frame;
  902. FLAC__ASSERT(0 != decoder);
  903. FLAC__ASSERT(0 != decoder->protected_);
  904. while(1) {
  905. switch(decoder->protected_->state) {
  906. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  907. if(!find_metadata_(decoder))
  908. return false; /* above function sets the status for us */
  909. break;
  910. case FLAC__STREAM_DECODER_READ_METADATA:
  911. if(!read_metadata_(decoder))
  912. return false; /* above function sets the status for us */
  913. else
  914. return true;
  915. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  916. if(!frame_sync_(decoder))
  917. return true; /* above function sets the status for us */
  918. break;
  919. case FLAC__STREAM_DECODER_READ_FRAME:
  920. if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
  921. return false; /* above function sets the status for us */
  922. if(got_a_frame)
  923. return true; /* above function sets the status for us */
  924. break;
  925. case FLAC__STREAM_DECODER_END_OF_STREAM:
  926. case FLAC__STREAM_DECODER_ABORTED:
  927. return true;
  928. default:
  929. FLAC__ASSERT(0);
  930. return false;
  931. }
  932. }
  933. }
  934. FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
  935. {
  936. FLAC__ASSERT(0 != decoder);
  937. FLAC__ASSERT(0 != decoder->protected_);
  938. while(1) {
  939. switch(decoder->protected_->state) {
  940. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  941. if(!find_metadata_(decoder))
  942. return false; /* above function sets the status for us */
  943. break;
  944. case FLAC__STREAM_DECODER_READ_METADATA:
  945. if(!read_metadata_(decoder))
  946. return false; /* above function sets the status for us */
  947. break;
  948. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  949. case FLAC__STREAM_DECODER_READ_FRAME:
  950. case FLAC__STREAM_DECODER_END_OF_STREAM:
  951. case FLAC__STREAM_DECODER_ABORTED:
  952. return true;
  953. default:
  954. FLAC__ASSERT(0);
  955. return false;
  956. }
  957. }
  958. }
  959. FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
  960. {
  961. FLAC__bool dummy;
  962. FLAC__ASSERT(0 != decoder);
  963. FLAC__ASSERT(0 != decoder->protected_);
  964. while(1) {
  965. switch(decoder->protected_->state) {
  966. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  967. if(!find_metadata_(decoder))
  968. return false; /* above function sets the status for us */
  969. break;
  970. case FLAC__STREAM_DECODER_READ_METADATA:
  971. if(!read_metadata_(decoder))
  972. return false; /* above function sets the status for us */
  973. break;
  974. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  975. if(!frame_sync_(decoder))
  976. return true; /* above function sets the status for us */
  977. break;
  978. case FLAC__STREAM_DECODER_READ_FRAME:
  979. if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
  980. return false; /* above function sets the status for us */
  981. break;
  982. case FLAC__STREAM_DECODER_END_OF_STREAM:
  983. case FLAC__STREAM_DECODER_ABORTED:
  984. return true;
  985. default:
  986. FLAC__ASSERT(0);
  987. return false;
  988. }
  989. }
  990. }
  991. FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
  992. {
  993. FLAC__bool got_a_frame;
  994. FLAC__ASSERT(0 != decoder);
  995. FLAC__ASSERT(0 != decoder->protected_);
  996. while(1) {
  997. switch(decoder->protected_->state) {
  998. case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
  999. case FLAC__STREAM_DECODER_READ_METADATA:
  1000. return false; /* above function sets the status for us */
  1001. case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
  1002. if(!frame_sync_(decoder))
  1003. return true; /* above function sets the status for us */
  1004. break;
  1005. case FLAC__STREAM_DECODER_READ_FRAME:
  1006. if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
  1007. return false; /* above function sets the status for us */
  1008. if(got_a_frame)
  1009. return true; /* above function sets the status for us */
  1010. break;
  1011. case FLAC__STREAM_DECODER_END_OF_STREAM:
  1012. case FLAC__STREAM_DECODER_ABORTED:
  1013. return true;
  1014. default:
  1015. FLAC__ASSERT(0);
  1016. return false;
  1017. }
  1018. }
  1019. }
  1020. FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
  1021. {
  1022. FLAC__uint64 length;
  1023. FLAC__ASSERT(0 != decoder);
  1024. if(
  1025. decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
  1026. decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
  1027. decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
  1028. decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
  1029. decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
  1030. )
  1031. return false;
  1032. if(0 == decoder->private_->seek_callback)
  1033. return false;
  1034. FLAC__ASSERT(decoder->private_->seek_callback);
  1035. FLAC__ASSERT(decoder->private_->tell_callback);
  1036. FLAC__ASSERT(decoder->private_->length_callback);
  1037. FLAC__ASSERT(decoder->private_->eof_callback);
  1038. if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
  1039. return false;
  1040. decoder->private_->is_seeking = true;
  1041. /* turn off md5 checking if a seek is attempted */
  1042. decoder->private_->do_md5_checking = false;
  1043. /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */
  1044. if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
  1045. decoder->private_->is_seeking = false;
  1046. return false;
  1047. }
  1048. /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
  1049. if(
  1050. decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
  1051. decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
  1052. ) {
  1053. if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
  1054. /* above call sets the state for us */
  1055. decoder->private_->is_seeking = false;
  1056. return false;
  1057. }
  1058. /* check this again in case we didn't know total_samples the first time */
  1059. if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
  1060. decoder->private_->is_seeking = false;
  1061. return false;
  1062. }
  1063. }
  1064. {
  1065. const FLAC__bool ok =
  1066. #if FLAC__HAS_OGG
  1067. decoder->private_->is_ogg?
  1068. seek_to_absolute_sample_ogg_(decoder, length, sample) :
  1069. #endif
  1070. seek_to_absolute_sample_(decoder, length, sample)
  1071. ;
  1072. decoder->private_->is_seeking = false;
  1073. return ok;
  1074. }
  1075. }
  1076. /***********************************************************************
  1077. *
  1078. * Protected class methods
  1079. *
  1080. ***********************************************************************/
  1081. unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
  1082. {
  1083. FLAC__ASSERT(0 != decoder);
  1084. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1085. FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
  1086. return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
  1087. }
  1088. /***********************************************************************
  1089. *
  1090. * Private class methods
  1091. *
  1092. ***********************************************************************/
  1093. void set_defaults_(FLAC__StreamDecoder *decoder)
  1094. {
  1095. #if FLAC__HAS_OGG
  1096. decoder->private_->is_ogg = false;
  1097. #endif
  1098. decoder->private_->read_callback = 0;
  1099. decoder->private_->seek_callback = 0;
  1100. decoder->private_->tell_callback = 0;
  1101. decoder->private_->length_callback = 0;
  1102. decoder->private_->eof_callback = 0;
  1103. decoder->private_->write_callback = 0;
  1104. decoder->private_->metadata_callback = 0;
  1105. decoder->private_->error_callback = 0;
  1106. decoder->private_->client_data = 0;
  1107. memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
  1108. decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
  1109. decoder->private_->metadata_filter_ids_count = 0;
  1110. decoder->protected_->md5_checking = false;
  1111. #if FLAC__HAS_OGG
  1112. FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
  1113. #endif
  1114. }
  1115. /*
  1116. * This will forcibly set stdin to binary mode (for OSes that require it)
  1117. */
  1118. FILE *get_binary_stdin_(void)
  1119. {
  1120. /* if something breaks here it is probably due to the presence or
  1121. * absence of an underscore before the identifiers 'setmode',
  1122. * 'fileno', and/or 'O_BINARY'; check your system header files.
  1123. */
  1124. #if defined _MSC_VER || defined __MINGW32__
  1125. _setmode(_fileno(stdin), _O_BINARY);
  1126. #elif defined __CYGWIN__
  1127. /* almost certainly not needed for any modern Cygwin, but let's be safe... */
  1128. setmode(fileno(stdin), O_BINARY);
  1129. #elif defined __EMX__
  1130. setmode(fileno(stdin), O_BINARY);
  1131. #endif
  1132. return stdin;
  1133. }
  1134. FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
  1135. {
  1136. unsigned i;
  1137. FLAC__int32 *tmp;
  1138. if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
  1139. return true;
  1140. /* simply using realloc() is not practical because the number of channels may change mid-stream */
  1141. for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
  1142. if(0 != decoder->private_->output[i]) {
  1143. free(decoder->private_->output[i]-4);
  1144. decoder->private_->output[i] = 0;
  1145. }
  1146. if(0 != decoder->private_->residual_unaligned[i]) {
  1147. free(decoder->private_->residual_unaligned[i]);
  1148. decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
  1149. }
  1150. }
  1151. for(i = 0; i < channels; i++) {
  1152. /* WATCHOUT:
  1153. * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
  1154. * output arrays have a buffer of up to 3 zeroes in front
  1155. * (at negative indices) for alignment purposes; we use 4
  1156. * to keep the data well-aligned.
  1157. */
  1158. tmp = (FLAC__int32*)safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/);
  1159. if(tmp == 0) {
  1160. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1161. return false;
  1162. }
  1163. memset(tmp, 0, sizeof(FLAC__int32)*4);
  1164. decoder->private_->output[i] = tmp + 4;
  1165. /* WATCHOUT:
  1166. * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
  1167. */
  1168. if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
  1169. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1170. return false;
  1171. }
  1172. }
  1173. decoder->private_->output_capacity = size;
  1174. decoder->private_->output_channels = channels;
  1175. return true;
  1176. }
  1177. FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
  1178. {
  1179. size_t i;
  1180. FLAC__ASSERT(0 != decoder);
  1181. FLAC__ASSERT(0 != decoder->private_);
  1182. for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
  1183. if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
  1184. return true;
  1185. return false;
  1186. }
  1187. FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
  1188. {
  1189. FLAC__uint32 x;
  1190. unsigned i, id;
  1191. FLAC__bool first = true;
  1192. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1193. for(i = id = 0; i < 4; ) {
  1194. if(decoder->private_->cached) {
  1195. x = (FLAC__uint32)decoder->private_->lookahead;
  1196. decoder->private_->cached = false;
  1197. }
  1198. else {
  1199. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  1200. return false; /* read_callback_ sets the state for us */
  1201. }
  1202. if(x == FLAC__STREAM_SYNC_STRING[i]) {
  1203. first = true;
  1204. i++;
  1205. id = 0;
  1206. continue;
  1207. }
  1208. if(x == ID3V2_TAG_[id]) {
  1209. id++;
  1210. i = 0;
  1211. if(id == 3) {
  1212. if(!skip_id3v2_tag_(decoder))
  1213. return false; /* skip_id3v2_tag_ sets the state for us */
  1214. }
  1215. continue;
  1216. }
  1217. id = 0;
  1218. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1219. decoder->private_->header_warmup[0] = (FLAC__byte)x;
  1220. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  1221. return false; /* read_callback_ sets the state for us */
  1222. /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
  1223. /* else we have to check if the second byte is the end of a sync code */
  1224. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1225. decoder->private_->lookahead = (FLAC__byte)x;
  1226. decoder->private_->cached = true;
  1227. }
  1228. else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
  1229. decoder->private_->header_warmup[1] = (FLAC__byte)x;
  1230. decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
  1231. return true;
  1232. }
  1233. }
  1234. i = 0;
  1235. if(first) {
  1236. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  1237. first = false;
  1238. }
  1239. }
  1240. decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
  1241. return true;
  1242. }
  1243. FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
  1244. {
  1245. FLAC__bool is_last;
  1246. FLAC__uint32 i, x, type, length;
  1247. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1248. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
  1249. return false; /* read_callback_ sets the state for us */
  1250. is_last = x? true : false;
  1251. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
  1252. return false; /* read_callback_ sets the state for us */
  1253. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
  1254. return false; /* read_callback_ sets the state for us */
  1255. if(type == FLAC__METADATA_TYPE_STREAMINFO) {
  1256. if(!read_metadata_streaminfo_(decoder, is_last, length))
  1257. return false;
  1258. decoder->private_->has_stream_info = true;
  1259. if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
  1260. decoder->private_->do_md5_checking = false;
  1261. if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
  1262. decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
  1263. }
  1264. else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
  1265. if(!read_metadata_seektable_(decoder, is_last, length))
  1266. return false;
  1267. decoder->private_->has_seek_table = true;
  1268. if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
  1269. decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
  1270. }
  1271. else {
  1272. FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
  1273. unsigned real_length = length;
  1274. FLAC__StreamMetadata block;
  1275. block.is_last = is_last;
  1276. block.type = (FLAC__MetadataType)type;
  1277. block.length = length;
  1278. if(type == FLAC__METADATA_TYPE_APPLICATION) {
  1279. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
  1280. return false; /* read_callback_ sets the state for us */
  1281. if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */
  1282. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
  1283. return false;
  1284. }
  1285. real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
  1286. if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
  1287. skip_it = !skip_it;
  1288. }
  1289. if(skip_it) {
  1290. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
  1291. return false; /* read_callback_ sets the state for us */
  1292. }
  1293. else {
  1294. switch(type) {
  1295. case FLAC__METADATA_TYPE_PADDING:
  1296. /* skip the padding bytes */
  1297. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
  1298. return false; /* read_callback_ sets the state for us */
  1299. break;
  1300. case FLAC__METADATA_TYPE_APPLICATION:
  1301. /* remember, we read the ID already */
  1302. if(real_length > 0) {
  1303. if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
  1304. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1305. return false;
  1306. }
  1307. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
  1308. return false; /* read_callback_ sets the state for us */
  1309. }
  1310. else
  1311. block.data.application.data = 0;
  1312. break;
  1313. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  1314. if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
  1315. return false;
  1316. break;
  1317. case FLAC__METADATA_TYPE_CUESHEET:
  1318. if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
  1319. return false;
  1320. break;
  1321. case FLAC__METADATA_TYPE_PICTURE:
  1322. if(!read_metadata_picture_(decoder, &block.data.picture))
  1323. return false;
  1324. break;
  1325. case FLAC__METADATA_TYPE_STREAMINFO:
  1326. case FLAC__METADATA_TYPE_SEEKTABLE:
  1327. FLAC__ASSERT(0);
  1328. break;
  1329. default:
  1330. if(real_length > 0) {
  1331. if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
  1332. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1333. return false;
  1334. }
  1335. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
  1336. return false; /* read_callback_ sets the state for us */
  1337. }
  1338. else
  1339. block.data.unknown.data = 0;
  1340. break;
  1341. }
  1342. if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
  1343. decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
  1344. /* now we have to free any malloc()ed data in the block */
  1345. switch(type) {
  1346. case FLAC__METADATA_TYPE_PADDING:
  1347. break;
  1348. case FLAC__METADATA_TYPE_APPLICATION:
  1349. if(0 != block.data.application.data)
  1350. free(block.data.application.data);
  1351. break;
  1352. case FLAC__METADATA_TYPE_VORBIS_COMMENT:
  1353. if(0 != block.data.vorbis_comment.vendor_string.entry)
  1354. free(block.data.vorbis_comment.vendor_string.entry);
  1355. if(block.data.vorbis_comment.num_comments > 0)
  1356. for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
  1357. if(0 != block.data.vorbis_comment.comments[i].entry)
  1358. free(block.data.vorbis_comment.comments[i].entry);
  1359. if(0 != block.data.vorbis_comment.comments)
  1360. free(block.data.vorbis_comment.comments);
  1361. break;
  1362. case FLAC__METADATA_TYPE_CUESHEET:
  1363. if(block.data.cue_sheet.num_tracks > 0)
  1364. for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
  1365. if(0 != block.data.cue_sheet.tracks[i].indices)
  1366. free(block.data.cue_sheet.tracks[i].indices);
  1367. if(0 != block.data.cue_sheet.tracks)
  1368. free(block.data.cue_sheet.tracks);
  1369. break;
  1370. case FLAC__METADATA_TYPE_PICTURE:
  1371. if(0 != block.data.picture.mime_type)
  1372. free(block.data.picture.mime_type);
  1373. if(0 != block.data.picture.description)
  1374. free(block.data.picture.description);
  1375. if(0 != block.data.picture.data)
  1376. free(block.data.picture.data);
  1377. break;
  1378. case FLAC__METADATA_TYPE_STREAMINFO:
  1379. case FLAC__METADATA_TYPE_SEEKTABLE:
  1380. FLAC__ASSERT(0);
  1381. default:
  1382. if(0 != block.data.unknown.data)
  1383. free(block.data.unknown.data);
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. if(is_last) {
  1389. /* if this fails, it's OK, it's just a hint for the seek routine */
  1390. if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
  1391. decoder->private_->first_frame_offset = 0;
  1392. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1393. }
  1394. return true;
  1395. }
  1396. FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
  1397. {
  1398. FLAC__uint32 x;
  1399. unsigned bits, used_bits = 0;
  1400. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1401. decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
  1402. decoder->private_->stream_info.is_last = is_last;
  1403. decoder->private_->stream_info.length = length;
  1404. bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
  1405. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
  1406. return false; /* read_callback_ sets the state for us */
  1407. decoder->private_->stream_info.data.stream_info.min_blocksize = x;
  1408. used_bits += bits;
  1409. bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
  1410. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
  1411. return false; /* read_callback_ sets the state for us */
  1412. decoder->private_->stream_info.data.stream_info.max_blocksize = x;
  1413. used_bits += bits;
  1414. bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
  1415. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
  1416. return false; /* read_callback_ sets the state for us */
  1417. decoder->private_->stream_info.data.stream_info.min_framesize = x;
  1418. used_bits += bits;
  1419. bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
  1420. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
  1421. return false; /* read_callback_ sets the state for us */
  1422. decoder->private_->stream_info.data.stream_info.max_framesize = x;
  1423. used_bits += bits;
  1424. bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
  1425. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
  1426. return false; /* read_callback_ sets the state for us */
  1427. decoder->private_->stream_info.data.stream_info.sample_rate = x;
  1428. used_bits += bits;
  1429. bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
  1430. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
  1431. return false; /* read_callback_ sets the state for us */
  1432. decoder->private_->stream_info.data.stream_info.channels = x+1;
  1433. used_bits += bits;
  1434. bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
  1435. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
  1436. return false; /* read_callback_ sets the state for us */
  1437. decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
  1438. used_bits += bits;
  1439. bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
  1440. if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
  1441. return false; /* read_callback_ sets the state for us */
  1442. used_bits += bits;
  1443. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
  1444. return false; /* read_callback_ sets the state for us */
  1445. used_bits += 16*8;
  1446. /* skip the rest of the block */
  1447. FLAC__ASSERT(used_bits % 8 == 0);
  1448. length -= (used_bits / 8);
  1449. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
  1450. return false; /* read_callback_ sets the state for us */
  1451. return true;
  1452. }
  1453. FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
  1454. {
  1455. FLAC__uint32 i, x;
  1456. FLAC__uint64 xx;
  1457. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1458. decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
  1459. decoder->private_->seek_table.is_last = is_last;
  1460. decoder->private_->seek_table.length = length;
  1461. decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
  1462. /* use realloc since we may pass through here several times (e.g. after seeking) */
  1463. if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) {
  1464. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1465. return false;
  1466. }
  1467. for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
  1468. if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
  1469. return false; /* read_callback_ sets the state for us */
  1470. decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
  1471. if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
  1472. return false; /* read_callback_ sets the state for us */
  1473. decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
  1474. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
  1475. return false; /* read_callback_ sets the state for us */
  1476. decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
  1477. }
  1478. length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
  1479. /* if there is a partial point left, skip over it */
  1480. if(length > 0) {
  1481. /*@@@ do a send_error_to_client_() here? there's an argument for either way */
  1482. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
  1483. return false; /* read_callback_ sets the state for us */
  1484. }
  1485. return true;
  1486. }
  1487. FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
  1488. {
  1489. FLAC__uint32 i;
  1490. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1491. /* read vendor string */
  1492. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
  1493. if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
  1494. return false; /* read_callback_ sets the state for us */
  1495. if(obj->vendor_string.length > 0) {
  1496. if(0 == (obj->vendor_string.entry = (FLAC__byte*)safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) {
  1497. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1498. return false;
  1499. }
  1500. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
  1501. return false; /* read_callback_ sets the state for us */
  1502. obj->vendor_string.entry[obj->vendor_string.length] = '\0';
  1503. }
  1504. else
  1505. obj->vendor_string.entry = 0;
  1506. /* read num comments */
  1507. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
  1508. if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
  1509. return false; /* read_callback_ sets the state for us */
  1510. /* read comments */
  1511. if(obj->num_comments > 0) {
  1512. if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)safe_malloc_mul_2op_(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
  1513. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1514. return false;
  1515. }
  1516. for(i = 0; i < obj->num_comments; i++) {
  1517. FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
  1518. if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
  1519. return false; /* read_callback_ sets the state for us */
  1520. if(obj->comments[i].length > 0) {
  1521. if(0 == (obj->comments[i].entry = (FLAC__byte*)safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) {
  1522. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1523. return false;
  1524. }
  1525. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
  1526. return false; /* read_callback_ sets the state for us */
  1527. obj->comments[i].entry[obj->comments[i].length] = '\0';
  1528. }
  1529. else
  1530. obj->comments[i].entry = 0;
  1531. }
  1532. }
  1533. else {
  1534. obj->comments = 0;
  1535. }
  1536. return true;
  1537. }
  1538. FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
  1539. {
  1540. FLAC__uint32 i, j, x;
  1541. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1542. memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
  1543. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
  1544. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
  1545. return false; /* read_callback_ sets the state for us */
  1546. if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
  1547. return false; /* read_callback_ sets the state for us */
  1548. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
  1549. return false; /* read_callback_ sets the state for us */
  1550. obj->is_cd = x? true : false;
  1551. if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
  1552. return false; /* read_callback_ sets the state for us */
  1553. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
  1554. return false; /* read_callback_ sets the state for us */
  1555. obj->num_tracks = x;
  1556. if(obj->num_tracks > 0) {
  1557. if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
  1558. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1559. return false;
  1560. }
  1561. for(i = 0; i < obj->num_tracks; i++) {
  1562. FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
  1563. if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
  1564. return false; /* read_callback_ sets the state for us */
  1565. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
  1566. return false; /* read_callback_ sets the state for us */
  1567. track->number = (FLAC__byte)x;
  1568. FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
  1569. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
  1570. return false; /* read_callback_ sets the state for us */
  1571. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
  1572. return false; /* read_callback_ sets the state for us */
  1573. track->type = x;
  1574. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
  1575. return false; /* read_callback_ sets the state for us */
  1576. track->pre_emphasis = x;
  1577. if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
  1578. return false; /* read_callback_ sets the state for us */
  1579. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
  1580. return false; /* read_callback_ sets the state for us */
  1581. track->num_indices = (FLAC__byte)x;
  1582. if(track->num_indices > 0) {
  1583. if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
  1584. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1585. return false;
  1586. }
  1587. for(j = 0; j < track->num_indices; j++) {
  1588. FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
  1589. if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
  1590. return false; /* read_callback_ sets the state for us */
  1591. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
  1592. return false; /* read_callback_ sets the state for us */
  1593. index->number = (FLAC__byte)x;
  1594. if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
  1595. return false; /* read_callback_ sets the state for us */
  1596. }
  1597. }
  1598. }
  1599. }
  1600. return true;
  1601. }
  1602. FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
  1603. {
  1604. FLAC__uint32 x;
  1605. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1606. /* read type */
  1607. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
  1608. return false; /* read_callback_ sets the state for us */
  1609. obj->type = x;
  1610. /* read MIME type */
  1611. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
  1612. return false; /* read_callback_ sets the state for us */
  1613. if(0 == (obj->mime_type = (char*)safe_malloc_add_2op_(x, /*+*/1))) {
  1614. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1615. return false;
  1616. }
  1617. if(x > 0) {
  1618. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
  1619. return false; /* read_callback_ sets the state for us */
  1620. }
  1621. obj->mime_type[x] = '\0';
  1622. /* read description */
  1623. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
  1624. return false; /* read_callback_ sets the state for us */
  1625. if(0 == (obj->description = (FLAC__byte*)safe_malloc_add_2op_(x, /*+*/1))) {
  1626. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1627. return false;
  1628. }
  1629. if(x > 0) {
  1630. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
  1631. return false; /* read_callback_ sets the state for us */
  1632. }
  1633. obj->description[x] = '\0';
  1634. /* read width */
  1635. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
  1636. return false; /* read_callback_ sets the state for us */
  1637. /* read height */
  1638. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
  1639. return false; /* read_callback_ sets the state for us */
  1640. /* read depth */
  1641. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
  1642. return false; /* read_callback_ sets the state for us */
  1643. /* read colors */
  1644. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
  1645. return false; /* read_callback_ sets the state for us */
  1646. /* read data */
  1647. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
  1648. return false; /* read_callback_ sets the state for us */
  1649. if(0 == (obj->data = (FLAC__byte*)safe_malloc_(obj->data_length))) {
  1650. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  1651. return false;
  1652. }
  1653. if(obj->data_length > 0) {
  1654. if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
  1655. return false; /* read_callback_ sets the state for us */
  1656. }
  1657. return true;
  1658. }
  1659. FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
  1660. {
  1661. FLAC__uint32 x;
  1662. unsigned i, skip;
  1663. /* skip the version and flags bytes */
  1664. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
  1665. return false; /* read_callback_ sets the state for us */
  1666. /* get the size (in bytes) to skip */
  1667. skip = 0;
  1668. for(i = 0; i < 4; i++) {
  1669. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  1670. return false; /* read_callback_ sets the state for us */
  1671. skip <<= 7;
  1672. skip |= (x & 0x7f);
  1673. }
  1674. /* skip the rest of the tag */
  1675. if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
  1676. return false; /* read_callback_ sets the state for us */
  1677. return true;
  1678. }
  1679. FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
  1680. {
  1681. FLAC__uint32 x;
  1682. FLAC__bool first = true;
  1683. /* If we know the total number of samples in the stream, stop if we've read that many. */
  1684. /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
  1685. if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
  1686. if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
  1687. decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
  1688. return true;
  1689. }
  1690. }
  1691. /* make sure we're byte aligned */
  1692. if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
  1693. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
  1694. return false; /* read_callback_ sets the state for us */
  1695. }
  1696. while(1) {
  1697. if(decoder->private_->cached) {
  1698. x = (FLAC__uint32)decoder->private_->lookahead;
  1699. decoder->private_->cached = false;
  1700. }
  1701. else {
  1702. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  1703. return false; /* read_callback_ sets the state for us */
  1704. }
  1705. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1706. decoder->private_->header_warmup[0] = (FLAC__byte)x;
  1707. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  1708. return false; /* read_callback_ sets the state for us */
  1709. /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
  1710. /* else we have to check if the second byte is the end of a sync code */
  1711. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1712. decoder->private_->lookahead = (FLAC__byte)x;
  1713. decoder->private_->cached = true;
  1714. }
  1715. else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
  1716. decoder->private_->header_warmup[1] = (FLAC__byte)x;
  1717. decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
  1718. return true;
  1719. }
  1720. }
  1721. if(first) {
  1722. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  1723. first = false;
  1724. }
  1725. }
  1726. return true;
  1727. }
  1728. FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
  1729. {
  1730. unsigned channel;
  1731. unsigned i;
  1732. FLAC__int32 mid, side;
  1733. unsigned frame_crc; /* the one we calculate from the input stream */
  1734. FLAC__uint32 x;
  1735. *got_a_frame = false;
  1736. /* init the CRC */
  1737. frame_crc = 0;
  1738. frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
  1739. frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
  1740. FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
  1741. if(!read_frame_header_(decoder))
  1742. return false;
  1743. if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
  1744. return true;
  1745. if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
  1746. return false;
  1747. for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
  1748. /*
  1749. * first figure the correct bits-per-sample of the subframe
  1750. */
  1751. unsigned bps = decoder->private_->frame.header.bits_per_sample;
  1752. switch(decoder->private_->frame.header.channel_assignment) {
  1753. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  1754. /* no adjustment needed */
  1755. break;
  1756. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  1757. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1758. if(channel == 1)
  1759. bps++;
  1760. break;
  1761. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  1762. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1763. if(channel == 0)
  1764. bps++;
  1765. break;
  1766. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  1767. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1768. if(channel == 1)
  1769. bps++;
  1770. break;
  1771. default:
  1772. FLAC__ASSERT(0);
  1773. }
  1774. /*
  1775. * now read it
  1776. */
  1777. if(!read_subframe_(decoder, channel, bps, do_full_decode))
  1778. return false;
  1779. if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
  1780. return true;
  1781. }
  1782. if(!read_zero_padding_(decoder))
  1783. return false;
  1784. if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption (i.e. "zero bits" were not all zeroes) */
  1785. return true;
  1786. /*
  1787. * Read the frame CRC-16 from the footer and check
  1788. */
  1789. frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
  1790. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
  1791. return false; /* read_callback_ sets the state for us */
  1792. if(frame_crc == x) {
  1793. if(do_full_decode) {
  1794. /* Undo any special channel coding */
  1795. switch(decoder->private_->frame.header.channel_assignment) {
  1796. case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
  1797. /* do nothing */
  1798. break;
  1799. case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
  1800. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1801. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  1802. decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
  1803. break;
  1804. case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
  1805. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1806. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  1807. decoder->private_->output[0][i] += decoder->private_->output[1][i];
  1808. break;
  1809. case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
  1810. FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
  1811. for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
  1812. #if 1
  1813. mid = decoder->private_->output[0][i];
  1814. side = decoder->private_->output[1][i];
  1815. mid <<= 1;
  1816. mid |= (side & 1); /* i.e. if 'side' is odd... */
  1817. decoder->private_->output[0][i] = (mid + side) >> 1;
  1818. decoder->private_->output[1][i] = (mid - side) >> 1;
  1819. #else
  1820. /* OPT: without 'side' temp variable */
  1821. mid = (decoder->private_->output[0][i] << 1) | (decoder->private_->output[1][i] & 1); /* i.e. if 'side' is odd... */
  1822. decoder->private_->output[0][i] = (mid + decoder->private_->output[1][i]) >> 1;
  1823. decoder->private_->output[1][i] = (mid - decoder->private_->output[1][i]) >> 1;
  1824. #endif
  1825. }
  1826. break;
  1827. default:
  1828. FLAC__ASSERT(0);
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. else {
  1834. /* Bad frame, emit error and zero the output signal */
  1835. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
  1836. if(do_full_decode) {
  1837. for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
  1838. memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
  1839. }
  1840. }
  1841. }
  1842. *got_a_frame = true;
  1843. /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */
  1844. if(decoder->private_->next_fixed_block_size)
  1845. decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;
  1846. /* put the latest values into the public section of the decoder instance */
  1847. decoder->protected_->channels = decoder->private_->frame.header.channels;
  1848. decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
  1849. decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
  1850. decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
  1851. decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
  1852. FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  1853. decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
  1854. /* write it */
  1855. if(do_full_decode) {
  1856. if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
  1857. return false;
  1858. }
  1859. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1860. return true;
  1861. }
  1862. FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
  1863. {
  1864. FLAC__uint32 x;
  1865. FLAC__uint64 xx;
  1866. unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
  1867. FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
  1868. unsigned raw_header_len;
  1869. FLAC__bool is_unparseable = false;
  1870. FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
  1871. /* init the raw header with the saved bits from synchronization */
  1872. raw_header[0] = decoder->private_->header_warmup[0];
  1873. raw_header[1] = decoder->private_->header_warmup[1];
  1874. raw_header_len = 2;
  1875. /* check to make sure that reserved bit is 0 */
  1876. if(raw_header[1] & 0x02) /* MAGIC NUMBER */
  1877. is_unparseable = true;
  1878. /*
  1879. * Note that along the way as we read the header, we look for a sync
  1880. * code inside. If we find one it would indicate that our original
  1881. * sync was bad since there cannot be a sync code in a valid header.
  1882. *
  1883. * Three kinds of things can go wrong when reading the frame header:
  1884. * 1) We may have sync'ed incorrectly and not landed on a frame header.
  1885. * If we don't find a sync code, it can end up looking like we read
  1886. * a valid but unparseable header, until getting to the frame header
  1887. * CRC. Even then we could get a false positive on the CRC.
  1888. * 2) We may have sync'ed correctly but on an unparseable frame (from a
  1889. * future encoder).
  1890. * 3) We may be on a damaged frame which appears valid but unparseable.
  1891. *
  1892. * For all these reasons, we try and read a complete frame header as
  1893. * long as it seems valid, even if unparseable, up until the frame
  1894. * header CRC.
  1895. */
  1896. /*
  1897. * read in the raw header as bytes so we can CRC it, and parse it on the way
  1898. */
  1899. for(i = 0; i < 2; i++) {
  1900. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  1901. return false; /* read_callback_ sets the state for us */
  1902. if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
  1903. /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
  1904. decoder->private_->lookahead = (FLAC__byte)x;
  1905. decoder->private_->cached = true;
  1906. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
  1907. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1908. return true;
  1909. }
  1910. raw_header[raw_header_len++] = (FLAC__byte)x;
  1911. }
  1912. switch(x = raw_header[2] >> 4) {
  1913. case 0:
  1914. is_unparseable = true;
  1915. break;
  1916. case 1:
  1917. decoder->private_->frame.header.blocksize = 192;
  1918. break;
  1919. case 2:
  1920. case 3:
  1921. case 4:
  1922. case 5:
  1923. decoder->private_->frame.header.blocksize = 576 << (x-2);
  1924. break;
  1925. case 6:
  1926. case 7:
  1927. blocksize_hint = x;
  1928. break;
  1929. case 8:
  1930. case 9:
  1931. case 10:
  1932. case 11:
  1933. case 12:
  1934. case 13:
  1935. case 14:
  1936. case 15:
  1937. decoder->private_->frame.header.blocksize = 256 << (x-8);
  1938. break;
  1939. default:
  1940. FLAC__ASSERT(0);
  1941. break;
  1942. }
  1943. switch(x = raw_header[2] & 0x0f) {
  1944. case 0:
  1945. if(decoder->private_->has_stream_info)
  1946. decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
  1947. else
  1948. is_unparseable = true;
  1949. break;
  1950. case 1:
  1951. decoder->private_->frame.header.sample_rate = 88200;
  1952. break;
  1953. case 2:
  1954. decoder->private_->frame.header.sample_rate = 176400;
  1955. break;
  1956. case 3:
  1957. decoder->private_->frame.header.sample_rate = 192000;
  1958. break;
  1959. case 4:
  1960. decoder->private_->frame.header.sample_rate = 8000;
  1961. break;
  1962. case 5:
  1963. decoder->private_->frame.header.sample_rate = 16000;
  1964. break;
  1965. case 6:
  1966. decoder->private_->frame.header.sample_rate = 22050;
  1967. break;
  1968. case 7:
  1969. decoder->private_->frame.header.sample_rate = 24000;
  1970. break;
  1971. case 8:
  1972. decoder->private_->frame.header.sample_rate = 32000;
  1973. break;
  1974. case 9:
  1975. decoder->private_->frame.header.sample_rate = 44100;
  1976. break;
  1977. case 10:
  1978. decoder->private_->frame.header.sample_rate = 48000;
  1979. break;
  1980. case 11:
  1981. decoder->private_->frame.header.sample_rate = 96000;
  1982. break;
  1983. case 12:
  1984. case 13:
  1985. case 14:
  1986. sample_rate_hint = x;
  1987. break;
  1988. case 15:
  1989. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
  1990. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  1991. return true;
  1992. default:
  1993. FLAC__ASSERT(0);
  1994. }
  1995. x = (unsigned)(raw_header[3] >> 4);
  1996. if(x & 8) {
  1997. decoder->private_->frame.header.channels = 2;
  1998. switch(x & 7) {
  1999. case 0:
  2000. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
  2001. break;
  2002. case 1:
  2003. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
  2004. break;
  2005. case 2:
  2006. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
  2007. break;
  2008. default:
  2009. is_unparseable = true;
  2010. break;
  2011. }
  2012. }
  2013. else {
  2014. decoder->private_->frame.header.channels = (unsigned)x + 1;
  2015. decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
  2016. }
  2017. switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
  2018. case 0:
  2019. if(decoder->private_->has_stream_info)
  2020. decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
  2021. else
  2022. is_unparseable = true;
  2023. break;
  2024. case 1:
  2025. decoder->private_->frame.header.bits_per_sample = 8;
  2026. break;
  2027. case 2:
  2028. decoder->private_->frame.header.bits_per_sample = 12;
  2029. break;
  2030. case 4:
  2031. decoder->private_->frame.header.bits_per_sample = 16;
  2032. break;
  2033. case 5:
  2034. decoder->private_->frame.header.bits_per_sample = 20;
  2035. break;
  2036. case 6:
  2037. decoder->private_->frame.header.bits_per_sample = 24;
  2038. break;
  2039. case 3:
  2040. case 7:
  2041. is_unparseable = true;
  2042. break;
  2043. default:
  2044. FLAC__ASSERT(0);
  2045. break;
  2046. }
  2047. /* check to make sure that reserved bit is 0 */
  2048. if(raw_header[3] & 0x01) /* MAGIC NUMBER */
  2049. is_unparseable = true;
  2050. /* read the frame's starting sample number (or frame number as the case may be) */
  2051. if(
  2052. raw_header[1] & 0x01 ||
  2053. /*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */
  2054. (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
  2055. ) { /* variable blocksize */
  2056. if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
  2057. return false; /* read_callback_ sets the state for us */
  2058. if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
  2059. decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
  2060. decoder->private_->cached = true;
  2061. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
  2062. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2063. return true;
  2064. }
  2065. decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
  2066. decoder->private_->frame.header.number.sample_number = xx;
  2067. }
  2068. else { /* fixed blocksize */
  2069. if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
  2070. return false; /* read_callback_ sets the state for us */
  2071. if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
  2072. decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
  2073. decoder->private_->cached = true;
  2074. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
  2075. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2076. return true;
  2077. }
  2078. decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
  2079. decoder->private_->frame.header.number.frame_number = x;
  2080. }
  2081. if(blocksize_hint) {
  2082. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  2083. return false; /* read_callback_ sets the state for us */
  2084. raw_header[raw_header_len++] = (FLAC__byte)x;
  2085. if(blocksize_hint == 7) {
  2086. FLAC__uint32 _x;
  2087. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
  2088. return false; /* read_callback_ sets the state for us */
  2089. raw_header[raw_header_len++] = (FLAC__byte)_x;
  2090. x = (x << 8) | _x;
  2091. }
  2092. decoder->private_->frame.header.blocksize = x+1;
  2093. }
  2094. if(sample_rate_hint) {
  2095. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  2096. return false; /* read_callback_ sets the state for us */
  2097. raw_header[raw_header_len++] = (FLAC__byte)x;
  2098. if(sample_rate_hint != 12) {
  2099. FLAC__uint32 _x;
  2100. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
  2101. return false; /* read_callback_ sets the state for us */
  2102. raw_header[raw_header_len++] = (FLAC__byte)_x;
  2103. x = (x << 8) | _x;
  2104. }
  2105. if(sample_rate_hint == 12)
  2106. decoder->private_->frame.header.sample_rate = x*1000;
  2107. else if(sample_rate_hint == 13)
  2108. decoder->private_->frame.header.sample_rate = x;
  2109. else
  2110. decoder->private_->frame.header.sample_rate = x*10;
  2111. }
  2112. /* read the CRC-8 byte */
  2113. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
  2114. return false; /* read_callback_ sets the state for us */
  2115. crc8 = (FLAC__byte)x;
  2116. if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
  2117. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
  2118. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2119. return true;
  2120. }
  2121. /* calculate the sample number from the frame number if needed */
  2122. decoder->private_->next_fixed_block_size = 0;
  2123. if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
  2124. x = decoder->private_->frame.header.number.frame_number;
  2125. decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
  2126. if(decoder->private_->fixed_block_size)
  2127. decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
  2128. else if(decoder->private_->has_stream_info) {
  2129. if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
  2130. decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
  2131. decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
  2132. }
  2133. else
  2134. is_unparseable = true;
  2135. }
  2136. else if(x == 0) {
  2137. decoder->private_->frame.header.number.sample_number = 0;
  2138. decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
  2139. }
  2140. else {
  2141. /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */
  2142. decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
  2143. }
  2144. }
  2145. if(is_unparseable) {
  2146. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
  2147. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2148. return true;
  2149. }
  2150. return true;
  2151. }
  2152. FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
  2153. {
  2154. FLAC__uint32 x;
  2155. FLAC__bool wasted_bits;
  2156. unsigned i;
  2157. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
  2158. return false; /* read_callback_ sets the state for us */
  2159. wasted_bits = (x & 1);
  2160. x &= 0xfe;
  2161. if(wasted_bits) {
  2162. unsigned u;
  2163. if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
  2164. return false; /* read_callback_ sets the state for us */
  2165. decoder->private_->frame.subframes[channel].wasted_bits = u+1;
  2166. bps -= decoder->private_->frame.subframes[channel].wasted_bits;
  2167. }
  2168. else
  2169. decoder->private_->frame.subframes[channel].wasted_bits = 0;
  2170. /*
  2171. * Lots of magic numbers here
  2172. */
  2173. if(x & 0x80) {
  2174. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  2175. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2176. return true;
  2177. }
  2178. else if(x == 0) {
  2179. if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
  2180. return false;
  2181. }
  2182. else if(x == 2) {
  2183. if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
  2184. return false;
  2185. }
  2186. else if(x < 16) {
  2187. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
  2188. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2189. return true;
  2190. }
  2191. else if(x <= 24) {
  2192. if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
  2193. return false;
  2194. if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
  2195. return true;
  2196. }
  2197. else if(x < 64) {
  2198. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
  2199. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2200. return true;
  2201. }
  2202. else {
  2203. if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
  2204. return false;
  2205. if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
  2206. return true;
  2207. }
  2208. if(wasted_bits && do_full_decode) {
  2209. x = decoder->private_->frame.subframes[channel].wasted_bits;
  2210. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  2211. decoder->private_->output[channel][i] <<= x;
  2212. }
  2213. return true;
  2214. }
  2215. FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
  2216. {
  2217. FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
  2218. FLAC__int32 x;
  2219. unsigned i;
  2220. FLAC__int32 *output = decoder->private_->output[channel];
  2221. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
  2222. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
  2223. return false; /* read_callback_ sets the state for us */
  2224. subframe->value = x;
  2225. /* decode the subframe */
  2226. if(do_full_decode) {
  2227. for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
  2228. output[i] = x;
  2229. }
  2230. return true;
  2231. }
  2232. FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
  2233. {
  2234. FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
  2235. FLAC__int32 i32;
  2236. FLAC__uint32 u32;
  2237. unsigned u;
  2238. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
  2239. subframe->residual = decoder->private_->residual[channel];
  2240. subframe->order = order;
  2241. /* read warm-up samples */
  2242. for(u = 0; u < order; u++) {
  2243. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
  2244. return false; /* read_callback_ sets the state for us */
  2245. subframe->warmup[u] = i32;
  2246. }
  2247. /* read entropy coding method info */
  2248. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
  2249. return false; /* read_callback_ sets the state for us */
  2250. subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
  2251. switch(subframe->entropy_coding_method.type) {
  2252. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  2253. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  2254. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
  2255. return false; /* read_callback_ sets the state for us */
  2256. subframe->entropy_coding_method.data.partitioned_rice.order = u32;
  2257. subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
  2258. break;
  2259. default:
  2260. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
  2261. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2262. return true;
  2263. }
  2264. /* read residual */
  2265. switch(subframe->entropy_coding_method.type) {
  2266. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  2267. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  2268. if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
  2269. return false;
  2270. break;
  2271. default:
  2272. FLAC__ASSERT(0);
  2273. }
  2274. /* decode the subframe */
  2275. if(do_full_decode) {
  2276. memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
  2277. FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
  2278. }
  2279. return true;
  2280. }
  2281. FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
  2282. {
  2283. FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
  2284. FLAC__int32 i32;
  2285. FLAC__uint32 u32;
  2286. unsigned u;
  2287. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
  2288. subframe->residual = decoder->private_->residual[channel];
  2289. subframe->order = order;
  2290. /* read warm-up samples */
  2291. for(u = 0; u < order; u++) {
  2292. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
  2293. return false; /* read_callback_ sets the state for us */
  2294. subframe->warmup[u] = i32;
  2295. }
  2296. /* read qlp coeff precision */
  2297. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
  2298. return false; /* read_callback_ sets the state for us */
  2299. if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
  2300. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  2301. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2302. return true;
  2303. }
  2304. subframe->qlp_coeff_precision = u32+1;
  2305. /* read qlp shift */
  2306. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
  2307. return false; /* read_callback_ sets the state for us */
  2308. subframe->quantization_level = i32;
  2309. /* read quantized lp coefficiencts */
  2310. for(u = 0; u < order; u++) {
  2311. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
  2312. return false; /* read_callback_ sets the state for us */
  2313. subframe->qlp_coeff[u] = i32;
  2314. }
  2315. /* read entropy coding method info */
  2316. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
  2317. return false; /* read_callback_ sets the state for us */
  2318. subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
  2319. switch(subframe->entropy_coding_method.type) {
  2320. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  2321. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  2322. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
  2323. return false; /* read_callback_ sets the state for us */
  2324. subframe->entropy_coding_method.data.partitioned_rice.order = u32;
  2325. subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
  2326. break;
  2327. default:
  2328. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
  2329. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2330. return true;
  2331. }
  2332. /* read residual */
  2333. switch(subframe->entropy_coding_method.type) {
  2334. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
  2335. case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
  2336. if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
  2337. return false;
  2338. break;
  2339. default:
  2340. FLAC__ASSERT(0);
  2341. }
  2342. /* decode the subframe */
  2343. if(do_full_decode) {
  2344. memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
  2345. /*@@@@@@ technically not pessimistic enough, should be more like
  2346. if( (FLAC__uint64)order * ((((FLAC__uint64)1)<<bps)-1) * ((1<<subframe->qlp_coeff_precision)-1) < (((FLAC__uint64)-1) << 32) )
  2347. */
  2348. if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
  2349. if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
  2350. if(order <= 8)
  2351. decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  2352. else
  2353. decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  2354. }
  2355. else
  2356. decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  2357. else
  2358. decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
  2359. }
  2360. return true;
  2361. }
  2362. FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
  2363. {
  2364. FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
  2365. FLAC__int32 x, *residual = decoder->private_->residual[channel];
  2366. unsigned i;
  2367. decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
  2368. subframe->data = residual;
  2369. for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
  2370. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
  2371. return false; /* read_callback_ sets the state for us */
  2372. residual[i] = x;
  2373. }
  2374. /* decode the subframe */
  2375. if(do_full_decode)
  2376. memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
  2377. return true;
  2378. }
  2379. FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended)
  2380. {
  2381. FLAC__uint32 rice_parameter;
  2382. FLAC__int32 i;
  2383. unsigned partition, sample, u;
  2384. const unsigned partitions = 1u << partition_order;
  2385. const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
  2386. const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
  2387. const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
  2388. /* sanity checks */
  2389. if(partition_order == 0) {
  2390. if(decoder->private_->frame.header.blocksize < predictor_order) {
  2391. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  2392. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2393. return true;
  2394. }
  2395. }
  2396. else {
  2397. if(partition_samples < predictor_order) {
  2398. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  2399. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2400. return true;
  2401. }
  2402. }
  2403. if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
  2404. decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
  2405. return false;
  2406. }
  2407. sample = 0;
  2408. for(partition = 0; partition < partitions; partition++) {
  2409. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
  2410. return false; /* read_callback_ sets the state for us */
  2411. partitioned_rice_contents->parameters[partition] = rice_parameter;
  2412. if(rice_parameter < pesc) {
  2413. partitioned_rice_contents->raw_bits[partition] = 0;
  2414. u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
  2415. FLAC__ASSERT (sizeof (int) == 4); // ppgb 20071120: needed for the following cast.
  2416. if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, (int *) residual + sample, u, rice_parameter))
  2417. return false; /* read_callback_ sets the state for us */
  2418. sample += u;
  2419. }
  2420. else {
  2421. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
  2422. return false; /* read_callback_ sets the state for us */
  2423. partitioned_rice_contents->raw_bits[partition] = rice_parameter;
  2424. for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
  2425. if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
  2426. return false; /* read_callback_ sets the state for us */
  2427. residual[sample] = i;
  2428. }
  2429. }
  2430. }
  2431. return true;
  2432. }
  2433. FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
  2434. {
  2435. if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
  2436. FLAC__uint32 zero = 0;
  2437. if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
  2438. return false; /* read_callback_ sets the state for us */
  2439. if(zero != 0) {
  2440. send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
  2441. decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
  2442. }
  2443. }
  2444. return true;
  2445. }
  2446. FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
  2447. {
  2448. FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
  2449. if(
  2450. #if FLAC__HAS_OGG
  2451. /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
  2452. !decoder->private_->is_ogg &&
  2453. #endif
  2454. decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
  2455. ) {
  2456. *bytes = 0;
  2457. decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
  2458. return false;
  2459. }
  2460. else if(*bytes > 0) {
  2461. /* While seeking, it is possible for our seek to land in the
  2462. * middle of audio data that looks exactly like a frame header
  2463. * from a future version of an encoder. When that happens, our
  2464. * error callback will get an
  2465. * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
  2466. * unparseable_frame_count. But there is a remote possibility
  2467. * that it is properly synced at such a "future-codec frame",
  2468. * so to make sure, we wait to see many "unparseable" errors in
  2469. * a row before bailing out.
  2470. */
  2471. if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
  2472. decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
  2473. return false;
  2474. }
  2475. else {
  2476. const FLAC__StreamDecoderReadStatus status =
  2477. #if FLAC__HAS_OGG
  2478. decoder->private_->is_ogg?
  2479. read_callback_ogg_aspect_(decoder, buffer, bytes) :
  2480. #endif
  2481. decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
  2482. ;
  2483. if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
  2484. decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
  2485. return false;
  2486. }
  2487. else if(*bytes == 0) {
  2488. if(
  2489. status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
  2490. (
  2491. #if FLAC__HAS_OGG
  2492. /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
  2493. !decoder->private_->is_ogg &&
  2494. #endif
  2495. decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
  2496. )
  2497. ) {
  2498. decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
  2499. return false;
  2500. }
  2501. else
  2502. return true;
  2503. }
  2504. else
  2505. return true;
  2506. }
  2507. }
  2508. else {
  2509. /* abort to avoid a deadlock */
  2510. decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
  2511. return false;
  2512. }
  2513. /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
  2514. * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
  2515. * and at the same time hit the end of the stream (for example, seeking
  2516. * to a point that is after the beginning of the last Ogg page). There
  2517. * is no way to report an Ogg sync loss through the callbacks (see note
  2518. * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
  2519. * So to keep the decoder from stopping at this point we gate the call
  2520. * to the eof_callback and let the Ogg decoder aspect set the
  2521. * end-of-stream state when it is needed.
  2522. */
  2523. }
  2524. #if FLAC__HAS_OGG
  2525. FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
  2526. {
  2527. switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
  2528. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
  2529. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  2530. /* we don't really have a way to handle lost sync via read
  2531. * callback so we'll let it pass and let the underlying
  2532. * FLAC decoder catch the error
  2533. */
  2534. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
  2535. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  2536. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
  2537. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  2538. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
  2539. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
  2540. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
  2541. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
  2542. case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
  2543. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  2544. default:
  2545. FLAC__ASSERT(0);
  2546. /* double protection */
  2547. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  2548. }
  2549. }
  2550. FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
  2551. {
  2552. FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
  2553. switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
  2554. case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
  2555. return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
  2556. case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
  2557. return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
  2558. case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
  2559. return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
  2560. default:
  2561. /* double protection: */
  2562. FLAC__ASSERT(0);
  2563. return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
  2564. }
  2565. }
  2566. #endif
  2567. FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
  2568. {
  2569. if(decoder->private_->is_seeking) {
  2570. FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
  2571. FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
  2572. FLAC__uint64 target_sample = decoder->private_->target_sample;
  2573. FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  2574. #if FLAC__HAS_OGG
  2575. decoder->private_->got_a_frame = true;
  2576. #endif
  2577. decoder->private_->last_frame = *frame; /* save the frame */
  2578. if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
  2579. unsigned delta = (unsigned)(target_sample - this_frame_sample);
  2580. /* kick out of seek mode */
  2581. decoder->private_->is_seeking = false;
  2582. /* shift out the samples before target_sample */
  2583. if(delta > 0) {
  2584. unsigned channel;
  2585. const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
  2586. for(channel = 0; channel < frame->header.channels; channel++)
  2587. newbuffer[channel] = buffer[channel] + delta;
  2588. decoder->private_->last_frame.header.blocksize -= delta;
  2589. decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
  2590. /* write the relevant samples */
  2591. return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
  2592. }
  2593. else {
  2594. /* write the relevant samples */
  2595. return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
  2596. }
  2597. }
  2598. else {
  2599. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  2600. }
  2601. }
  2602. else {
  2603. /*
  2604. * If we never got STREAMINFO, turn off MD5 checking to save
  2605. * cycles since we don't have a sum to compare to anyway
  2606. */
  2607. if(!decoder->private_->has_stream_info)
  2608. decoder->private_->do_md5_checking = false;
  2609. if(decoder->private_->do_md5_checking) {
  2610. if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
  2611. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  2612. }
  2613. return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
  2614. }
  2615. }
  2616. void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
  2617. {
  2618. if(!decoder->private_->is_seeking)
  2619. decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
  2620. else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
  2621. decoder->private_->unparseable_frame_count++;
  2622. }
  2623. FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
  2624. {
  2625. FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
  2626. FLAC__int64 pos = -1;
  2627. int i;
  2628. unsigned approx_bytes_per_frame;
  2629. FLAC__bool first_seek = true;
  2630. const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
  2631. const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
  2632. const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
  2633. const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
  2634. const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
  2635. /* take these from the current frame in case they've changed mid-stream */
  2636. unsigned channels = FLAC__stream_decoder_get_channels(decoder);
  2637. unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
  2638. const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
  2639. /* use values from stream info if we didn't decode a frame */
  2640. if(channels == 0)
  2641. channels = decoder->private_->stream_info.data.stream_info.channels;
  2642. if(bps == 0)
  2643. bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
  2644. /* we are just guessing here */
  2645. if(max_framesize > 0)
  2646. approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
  2647. /*
  2648. * Check if it's a known fixed-blocksize stream. Note that though
  2649. * the spec doesn't allow zeroes in the STREAMINFO block, we may
  2650. * never get a STREAMINFO block when decoding so the value of
  2651. * min_blocksize might be zero.
  2652. */
  2653. else if(min_blocksize == max_blocksize && min_blocksize > 0) {
  2654. /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
  2655. approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
  2656. }
  2657. else
  2658. approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
  2659. /*
  2660. * First, we set an upper and lower bound on where in the
  2661. * stream we will search. For now we assume the worst case
  2662. * scenario, which is our best guess at the beginning of
  2663. * the first frame and end of the stream.
  2664. */
  2665. lower_bound = first_frame_offset;
  2666. lower_bound_sample = 0;
  2667. upper_bound = stream_length;
  2668. upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
  2669. /*
  2670. * Now we refine the bounds if we have a seektable with
  2671. * suitable points. Note that according to the spec they
  2672. * must be ordered by ascending sample number.
  2673. *
  2674. * Note: to protect against invalid seek tables we will ignore points
  2675. * that have frame_samples==0 or sample_number>=total_samples
  2676. */
  2677. if(seek_table) {
  2678. FLAC__uint64 new_lower_bound = lower_bound;
  2679. FLAC__uint64 new_upper_bound = upper_bound;
  2680. FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
  2681. FLAC__uint64 new_upper_bound_sample = upper_bound_sample;
  2682. /* find the closest seek point <= target_sample, if it exists */
  2683. for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
  2684. if(
  2685. seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
  2686. seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
  2687. (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
  2688. seek_table->points[i].sample_number <= target_sample
  2689. )
  2690. break;
  2691. }
  2692. if(i >= 0) { /* i.e. we found a suitable seek point... */
  2693. new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
  2694. new_lower_bound_sample = seek_table->points[i].sample_number;
  2695. }
  2696. /* find the closest seek point > target_sample, if it exists */
  2697. for(i = 0; i < (int)seek_table->num_points; i++) {
  2698. if(
  2699. seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
  2700. seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
  2701. (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
  2702. seek_table->points[i].sample_number > target_sample
  2703. )
  2704. break;
  2705. }
  2706. if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
  2707. new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
  2708. new_upper_bound_sample = seek_table->points[i].sample_number;
  2709. }
  2710. /* final protection against unsorted seek tables; keep original values if bogus */
  2711. if(new_upper_bound >= new_lower_bound) {
  2712. lower_bound = new_lower_bound;
  2713. upper_bound = new_upper_bound;
  2714. lower_bound_sample = new_lower_bound_sample;
  2715. upper_bound_sample = new_upper_bound_sample;
  2716. }
  2717. }
  2718. FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
  2719. /* there are 2 insidious ways that the following equality occurs, which
  2720. * we need to fix:
  2721. * 1) total_samples is 0 (unknown) and target_sample is 0
  2722. * 2) total_samples is 0 (unknown) and target_sample happens to be
  2723. * exactly equal to the last seek point in the seek table; this
  2724. * means there is no seek point above it, and upper_bound_samples
  2725. * remains equal to the estimate (of target_samples) we made above
  2726. * in either case it does not hurt to move upper_bound_sample up by 1
  2727. */
  2728. if(upper_bound_sample == lower_bound_sample)
  2729. upper_bound_sample++;
  2730. decoder->private_->target_sample = target_sample;
  2731. while(1) {
  2732. /* check if the bounds are still ok */
  2733. if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
  2734. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2735. return false;
  2736. }
  2737. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  2738. #if defined _MSC_VER || defined __MINGW32__
  2739. /* with VC++ you have to spoon feed it the casting */
  2740. pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame;
  2741. #else
  2742. pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
  2743. #endif
  2744. #else
  2745. /* a little less accurate: */
  2746. if(upper_bound - lower_bound < 0xffffffff)
  2747. pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
  2748. else /* @@@ WATCHOUT, ~2TB limit */
  2749. pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame;
  2750. #endif
  2751. if(pos >= (FLAC__int64)upper_bound)
  2752. pos = (FLAC__int64)upper_bound - 1;
  2753. if(pos < (FLAC__int64)lower_bound)
  2754. pos = (FLAC__int64)lower_bound;
  2755. if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
  2756. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2757. return false;
  2758. }
  2759. if(!FLAC__stream_decoder_flush(decoder)) {
  2760. /* above call sets the state for us */
  2761. return false;
  2762. }
  2763. /* Now we need to get a frame. First we need to reset our
  2764. * unparseable_frame_count; if we get too many unparseable
  2765. * frames in a row, the read callback will return
  2766. * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
  2767. * FLAC__stream_decoder_process_single() to return false.
  2768. */
  2769. decoder->private_->unparseable_frame_count = 0;
  2770. if(!FLAC__stream_decoder_process_single(decoder)) {
  2771. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2772. return false;
  2773. }
  2774. /* our write callback will change the state when it gets to the target frame */
  2775. /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */
  2776. #if 0
  2777. /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
  2778. if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
  2779. break;
  2780. #endif
  2781. if(!decoder->private_->is_seeking)
  2782. break;
  2783. FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  2784. this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
  2785. if (0 == decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
  2786. if (pos == (FLAC__int64)lower_bound) {
  2787. /* can't move back any more than the first frame, something is fatally wrong */
  2788. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2789. return false;
  2790. }
  2791. /* our last move backwards wasn't big enough, try again */
  2792. approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
  2793. continue;
  2794. }
  2795. /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */
  2796. first_seek = false;
  2797. /* make sure we are not seeking in corrupted stream */
  2798. if (this_frame_sample < lower_bound_sample) {
  2799. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2800. return false;
  2801. }
  2802. /* we need to narrow the search */
  2803. if(target_sample < this_frame_sample) {
  2804. upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
  2805. /*@@@@@@ what will decode position be if at end of stream? */
  2806. if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
  2807. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2808. return false;
  2809. }
  2810. approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
  2811. }
  2812. else { /* target_sample >= this_frame_sample + this frame's blocksize */
  2813. lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
  2814. if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
  2815. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2816. return false;
  2817. }
  2818. approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
  2819. }
  2820. }
  2821. return true;
  2822. }
  2823. #if FLAC__HAS_OGG
  2824. FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
  2825. {
  2826. FLAC__uint64 left_pos = 0, right_pos = stream_length;
  2827. FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
  2828. FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1;
  2829. FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
  2830. FLAC__bool did_a_seek;
  2831. unsigned iteration = 0;
  2832. /* In the first iterations, we will calculate the target byte position
  2833. * by the distance from the target sample to left_sample and
  2834. * right_sample (let's call it "proportional search"). After that, we
  2835. * will switch to binary search.
  2836. */
  2837. unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
  2838. /* We will switch to a linear search once our current sample is less
  2839. * than this number of samples ahead of the target sample
  2840. */
  2841. static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
  2842. /* If the total number of samples is unknown, use a large value, and
  2843. * force binary search immediately.
  2844. */
  2845. if(right_sample == 0) {
  2846. right_sample = (FLAC__uint64)(-1);
  2847. BINARY_SEARCH_AFTER_ITERATION = 0;
  2848. }
  2849. decoder->private_->target_sample = target_sample;
  2850. for( ; ; iteration++) {
  2851. if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
  2852. if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
  2853. pos = (right_pos + left_pos) / 2;
  2854. }
  2855. else {
  2856. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  2857. #if defined _MSC_VER || defined __MINGW32__
  2858. /* with MSVC you have to spoon feed it the casting */
  2859. pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos));
  2860. #else
  2861. pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
  2862. #endif
  2863. #else
  2864. /* a little less accurate: */
  2865. if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
  2866. pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
  2867. else /* @@@ WATCHOUT, ~2TB limit */
  2868. pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
  2869. #endif
  2870. /* @@@ TODO: might want to limit pos to some distance
  2871. * before EOF, to make sure we land before the last frame,
  2872. * thereby getting a this_frame_sample and so having a better
  2873. * estimate.
  2874. */
  2875. }
  2876. /* physical seek */
  2877. if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
  2878. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2879. return false;
  2880. }
  2881. if(!FLAC__stream_decoder_flush(decoder)) {
  2882. /* above call sets the state for us */
  2883. return false;
  2884. }
  2885. did_a_seek = true;
  2886. }
  2887. else
  2888. did_a_seek = false;
  2889. decoder->private_->got_a_frame = false;
  2890. if(!FLAC__stream_decoder_process_single(decoder)) {
  2891. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2892. return false;
  2893. }
  2894. if(!decoder->private_->got_a_frame) {
  2895. if(did_a_seek) {
  2896. /* this can happen if we seek to a point after the last frame; we drop
  2897. * to binary search right away in this case to avoid any wasted
  2898. * iterations of proportional search.
  2899. */
  2900. right_pos = pos;
  2901. BINARY_SEARCH_AFTER_ITERATION = 0;
  2902. }
  2903. else {
  2904. /* this can probably only happen if total_samples is unknown and the
  2905. * target_sample is past the end of the stream
  2906. */
  2907. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2908. return false;
  2909. }
  2910. }
  2911. /* our write callback will change the state when it gets to the target frame */
  2912. else if(!decoder->private_->is_seeking) {
  2913. break;
  2914. }
  2915. else {
  2916. this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
  2917. FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
  2918. if (did_a_seek) {
  2919. if (this_frame_sample <= target_sample) {
  2920. /* The 'equal' case should not happen, since
  2921. * FLAC__stream_decoder_process_single()
  2922. * should recognize that it has hit the
  2923. * target sample and we would exit through
  2924. * the 'break' above.
  2925. */
  2926. FLAC__ASSERT(this_frame_sample != target_sample);
  2927. left_sample = this_frame_sample;
  2928. /* sanity check to avoid infinite loop */
  2929. if (left_pos == pos) {
  2930. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2931. return false;
  2932. }
  2933. left_pos = pos;
  2934. }
  2935. else if(this_frame_sample > target_sample) {
  2936. right_sample = this_frame_sample;
  2937. /* sanity check to avoid infinite loop */
  2938. if (right_pos == pos) {
  2939. decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
  2940. return false;
  2941. }
  2942. right_pos = pos;
  2943. }
  2944. }
  2945. }
  2946. }
  2947. return true;
  2948. }
  2949. #endif
  2950. FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
  2951. {
  2952. (void)client_data;
  2953. if(*bytes > 0) {
  2954. *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
  2955. if(ferror(decoder->private_->file))
  2956. return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
  2957. else if(*bytes == 0)
  2958. return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  2959. else
  2960. return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
  2961. }
  2962. else
  2963. return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
  2964. }
  2965. FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
  2966. {
  2967. (void)client_data;
  2968. if(decoder->private_->file == stdin)
  2969. return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
  2970. else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
  2971. return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
  2972. else
  2973. return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
  2974. }
  2975. FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
  2976. {
  2977. off_t pos;
  2978. (void)client_data;
  2979. if(decoder->private_->file == stdin)
  2980. return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
  2981. else if((pos = ftello(decoder->private_->file)) < 0)
  2982. return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
  2983. else {
  2984. *absolute_byte_offset = (FLAC__uint64)pos;
  2985. return FLAC__STREAM_DECODER_TELL_STATUS_OK;
  2986. }
  2987. }
  2988. FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
  2989. {
  2990. struct stat filestats;
  2991. (void)client_data;
  2992. if(decoder->private_->file == stdin)
  2993. return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
  2994. else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
  2995. return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
  2996. else {
  2997. *stream_length = (FLAC__uint64)filestats.st_size;
  2998. return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
  2999. }
  3000. }
  3001. FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
  3002. {
  3003. (void)client_data;
  3004. return feof(decoder->private_->file)? true : false;
  3005. }