12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461 |
- /**
- ******************************************************************************
- * @file stm32h7xx_hal_jpeg.c
- * @author MCD Application Team
- * @version V1.0.0
- * @date 21-April-2017
- * @brief JPEG HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the JPEG encoder/decoder peripheral:
- * + Initialization and de-initialization functions
- * + JPEG processing functions encoding and decoding
- * + JPEG decoding Getting Info and encoding configuration setting
- * + JPEG enable/disable header parsing functions (for decoding)
- * + JPEG Input/Output Buffer configuration.
- * + JPEG callback functions
- * + JPEG Abort/Pause/Resume functions
- * + JPEG custom quantization tables setting functions
- * + IRQ handler management
- * + Peripheral State and Error functions
- *
- @verbatim
- ==============================================================================
- ##### How to use this driver #####
- ==============================================================================
- [..]
- (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
- Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
- (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
- the encoding parameters (mandatory before calling the encoding function).
- the application can change the encoding parameter "ImageQuality" from
- 1 to 100 to obtain a more or less quality (visual quality vs the original row image),
- and inversely more or less jpg file size.
- (#) Note that for decoding operation the JPEG peripheral output data are organized in
- YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
- ISO/IEC 10918-1 standard.
- It is up to the application to transform these YCbCr blocks to RGB data that can be display.
-
- Respectively, for Encoding operation the JPEG peripheral input should be organized
- in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
- MCU blocks transformation before feeding the JPEG peripheral with data.
- (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
- a JPEG encoding/decoding operation in polling method (blocking).
- (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
- a JPEG encoding/decoding operation with Interrupt method (not blocking).
- (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
- a JPEG encoding/decoding operation with DMA method (not blocking).
- (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
- is a JPEG decoding to provide the application with JPEG image parameters.
- This callback is asserted when the JPEG peripheral successfully parse the
- JPEG header.
- (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
- operations to inform the application that the input buffer has been
- consumed by the peripheral and to ask for a new data chunk if the operation
- (encoding/decoding) has not been complete yet.
- (++) This CallBack should be implemented in the application side. It should
- call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
- or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
- to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
- application to provide a new input data chunk.
- Once the application succeed getting new data and if the input has been paused,
- the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
- input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
- If the application has ended feeding the HAL JPEG with input data (no more input data), the application
- Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
- with the parameter InDataLength set to zero.
-
- (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
- to the application to provide the input data (for encoding or decoding) by chunks.
- If the new input data chunk is not available (because data should be read from an input file
- for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
- Once the new input data chunk is available ( read from a file for example), the application
- can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
- then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
-
- (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
- any time (outside the HAL_JPEG_GetDataCallback) Once the new input chunk data available.
- However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
- (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
- Transferring the previous chunk buffer to the JPEG peripheral.
-
- (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
- has filled the given output buffer with the given size.
-
- (++) This CallBack should be implemented in the application side. It should
- call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
- with the new output buffer location and size to be used to store next data chunk.
- if the application is not ready to provide the output chunk location then it can
- call the function HAL_JPEG_Pause with parameter XferSelection set to "JPEG_PAUSE_RESUME_OUTPUT"
- to inform the JPEG HAL driver that it shall pause output data. Once the application
- is ready to receive the new data chunk (output buffer location free or available) it should call
- the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
- with the new output chunk buffer location and size, then call "HAL_JPEG_Resume"
- to inform the HAL that it shall resume outputting data in the given output buffer.
- (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
- the application to receive data from the JPEG peripheral by chunks. when a chunk
- is received, the application can pause the HAL JPEG output data to be able to process
- these received data (YCbCr to RGB conversion in case of decoding or data storage in case
- of encoding).
- (++) The application can call functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
- any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
- However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
- (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
- Transferring the previous chunk buffer from the JPEG peripheral to the application.
- (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
- ended the current JPEG encoding operation, and all output data has been transmitted
- to the application.
- (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
- ended the current JPEG decoding operation. and all output data has been transmitted
- to the application.
- (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
- the current operation. the application can call the function "HAL_JPEG_GetError"
- to retrieve the error codes.
- (#) By default the HAL JPEG driver uses the default quantization tables
- as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
- User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
- Note that for decoding the quantization tables are automatically extracted from
- the JPEG header.
- (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
- *** JPEG HAL driver macros list ***
- =============================================
- [..]
- Below the list of most used macros in JPEG HAL driver.
-
- (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
- (+) __HAL_JPEG_ENABLE : Enable the JPEG peripheral.
- (+) __HAL_JPEG_DISABLE : Disable the JPEG peripheral.
- (+) __HAL_JPEG_GET_FLAG : Check the specified JPEG status flag.
- (+) __HAL_JPEG_CLEAR_FLAG : Clear the specified JPEG status flag.
- (+) __HAL_JPEG_ENABLE_IT : Enable the specified JPEG Interrupt.
- (+) __HAL_JPEG_DISABLE_IT : Disable the specified JPEG Interrupt.
- (+) __HAL_JPEG_GET_IT_SOURCE : returns the state of the specified JPEG Interrupt (Enabled or disabled).
- @endverbatim
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32h7xx_hal.h"
- /** @addtogroup STM32H7xx_HAL_Driver
- * @{
- */
- /** @defgroup JPEG JPEG
- * @brief JPEG HAL module driver.
- * @{
- */
- #ifdef HAL_JPEG_MODULE_ENABLED
- /* Private define ------------------------------------------------------------*/
- /** @addtogroup JPEG_Private_Constants
- * @{
- */
- #define JPEG_TIMEOUT_VALUE ((uint32_t)1000U) /* 1s */
- #define JPEG_AC_HUFF_TABLE_SIZE ((uint32_t)162U) /* Huffman AC table size : 162 codes*/
- #define JPEG_DC_HUFF_TABLE_SIZE ((uint32_t)12U) /* Huffman AC table size : 12 codes*/
- #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
- #define JPEG_FIFO_TH_SIZE ((uint32_t)8U) /* JPEG Input/Output HW FIFO Threshold in words*/
- #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
- #define JPEG_CONTEXT_ENCODE ((uint32_t)0x00000001U) /* JPEG context : operation is encoding*/
- #define JPEG_CONTEXT_DECODE ((uint32_t)0x00000002U) /* JPEG context : operation is decoding*/
- #define JPEG_CONTEXT_OPERATION_MASK ((uint32_t)0x00000003U) /* JPEG context : operation Mask */
- #define JPEG_CONTEXT_POLLING ((uint32_t)0x00000004U) /* JPEG context : Transfer use Polling */
- #define JPEG_CONTEXT_IT ((uint32_t)0x00000008U) /* JPEG context : Transfer use Interrupt */
- #define JPEG_CONTEXT_DMA ((uint32_t)0x0000000CU) /* JPEG context : Transfer use DMA */
- #define JPEG_CONTEXT_METHOD_MASK ((uint32_t)0x0000000CU) /* JPEG context : Transfer Mask */
- #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100U) /* JPEG context : encoding config done */
- #define JPEG_CONTEXT_PAUSE_INPUT ((uint32_t)0x00001000U) /* JPEG context : Pause Input */
- #define JPEG_CONTEXT_PAUSE_OUTPUT ((uint32_t)0x00002000U) /* JPEG context : Pause Output */
-
- #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000U) /* JPEG context : Use custom quantization tables */
- #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000U) /* JPEG context : ending with DMA in progress */
-
- #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000U) /* Process is on going */
- #define JPEG_PROCESS_DONE ((uint32_t)0x00000001U) /* Process is done (ends) */
- /**
- * @}
- */
- /* Private typedef -----------------------------------------------------------*/
- /** @addtogroup JPEG_Private_Types
- * @{
- */
- /*
- JPEG Huffman Table Structure definition :
- This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
- */
- typedef struct
- {
- /* These two fields directly represent the contents of a JPEG DHT marker */
- uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
-
- uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
-
-
- }JPEG_ACHuffTableTypeDef;
- typedef struct
- {
- /* These two fields directly represent the contents of a JPEG DHT marker */
- uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
-
- uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
-
-
- }JPEG_DCHuffTableTypeDef;
-
- typedef struct
- {
- uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
-
- uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
-
- }JPEG_AC_HuffCodeTableTypeDef;
- typedef struct
- {
- uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
-
- uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
-
- }JPEG_DC_HuffCodeTableTypeDef;
- /**
- * @}
- */
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /** @addtogroup JPEG_Private_Variables
- * @{
- */
- static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
- {
- { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
-
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
- };
- static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
- {
- { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
-
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
- };
- static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
- {
- { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
-
- { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, /*HUFFVAL */
- 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
- 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
- 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
- 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
- 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
- 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
- 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
- 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
- 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
- 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
- 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
- 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
- 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
- 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
- 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
- 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
- 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
- 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
- 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
- 0xf9, 0xfa }
- };
- static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
- {
- { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
-
- { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, /*HUFFVAL */
- 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
- 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
- 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
- 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
- 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
- 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
- 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
- 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
- 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
- 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
- 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
- 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
- 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
- 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
- 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
- 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
- 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
- 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
- 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
- 0xf9, 0xfa }
- };
- /*
- These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1.
- */
- static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
- {
- 16, 11, 10, 16, 24, 40, 51, 61,
- 12, 12, 14, 19, 26, 58, 60, 55,
- 14, 13, 16, 24, 40, 57, 69, 56,
- 14, 17, 22, 29, 51, 87, 80, 62,
- 18, 22, 37, 56, 68, 109, 103, 77,
- 24, 35, 55, 64, 81, 104, 113, 92,
- 49, 64, 78, 87, 103, 121, 120, 101,
- 72, 92, 95, 98, 112, 100, 103, 99
- };
- static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
- {
- 17, 18, 24, 47, 99, 99, 99, 99,
- 18, 21, 26, 66, 99, 99, 99, 99,
- 24, 26, 56, 99, 99, 99, 99, 99,
- 47, 66, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99,
- 99, 99, 99, 99, 99, 99, 99, 99
- };
- static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
- {
- 0, 1, 8, 16, 9, 2, 3, 10,
- 17, 24, 32, 25, 18, 11, 4, 5,
- 12, 19, 26, 33, 40, 48, 41, 34,
- 27, 20, 13, 6, 7, 14, 21, 28,
- 35, 42, 49, 56, 57, 50, 43, 36,
- 29, 22, 15, 23, 30, 37, 44, 51,
- 58, 59, 52, 45, 38, 31, 39, 46,
- 53, 60, 61, 54, 47, 55, 62, 63
- };
- /**
- * @}
- */
- /* Private function prototypes -----------------------------------------------*/
- /** @addtogroup JPEG_Private_Functions_Prototypes
- * @{
- */
- static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
- static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
- static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
- static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, __IO uint32_t *DCTableAddress);
- static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, __IO uint32_t *ACTableAddress);
- static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
- static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
- static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, __IO uint32_t *QTableAddress);
- static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
- static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
- static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
- static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
- static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
- static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
- static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
- static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
- static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
- static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
- static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
- static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
- static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma);
- static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma);
- static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma);
- static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma);
- /**
- * @}
- */
- /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
- * @{
- */
- /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
- * @brief Initialization and de-initialization functions.
- *
- @verbatim
- ==============================================================================
- ##### Initialization and de-initialization functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) Initialize the JPEG peripheral and creates the associated handle
- (+) DeInitialize the JPEG peripheral
-
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the JPEG according to the specified
- * parameters in the JPEG_InitTypeDef and creates the associated handle.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
- {
- /*Note : these intermediate variables are used to avoid MISRA warning
- regarding rule 11.5 */
- uint32_t acLum_huffmanTableAddr = (uint32_t)(&JPEG_ACLUM_HuffTable);
- uint32_t dcLum_huffmanTableAddr = (uint32_t)(&JPEG_DCLUM_HuffTable);
- uint32_t acChrom_huffmanTableAddr = (uint32_t)(&JPEG_ACCHROM_HuffTable);
- uint32_t dcChrom_huffmanTableAddr = (uint32_t)(&JPEG_DCCHROM_HuffTable);
-
- /* Check the JPEG handle allocation */
- if(hjpeg == NULL)
- {
- return HAL_ERROR;
- }
- if(hjpeg->State == HAL_JPEG_STATE_RESET)
- {
- /* Allocate lock resource and initialize it */
- hjpeg->Lock = HAL_UNLOCKED;
- /* Init the low level hardware : GPIO, CLOCK */
- HAL_JPEG_MspInit(hjpeg);
- }
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_BUSY;
-
- /* Start the JPEG Core*/
- __HAL_JPEG_ENABLE(hjpeg);
-
- /* Stop the JPEG encoding/decoding process*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
-
- /* Disable All Interrupts */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- /* Flush input and output FIFOs*/
- hjpeg->Instance->CR |= JPEG_CR_IFF;
- hjpeg->Instance->CR |= JPEG_CR_OFF;
-
- /* Clear all flags */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
-
- /* init default quantization tables*/
- hjpeg->QuantTable0 = (uint8_t *)((uint32_t)JPEG_LUM_QuantTable);
- hjpeg->QuantTable1 = (uint8_t *)((uint32_t)JPEG_CHROM_QuantTable);
- hjpeg->QuantTable2 = NULL;
- hjpeg->QuantTable3 = NULL;
-
- /* init the default Huffman tables*/
- if(JPEG_Set_HuffEnc_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)acLum_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcLum_huffmanTableAddr, (JPEG_ACHuffTableTypeDef *)acChrom_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcChrom_huffmanTableAddr) != HAL_OK)
- {
- hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
-
- return HAL_ERROR;
- }
-
- /* Enable header processing*/
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
-
- /* Reset JpegInCount and JpegOutCount */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /* Reset the JPEG ErrorCode */
- hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
-
- /*Clear the context filelds*/
- hjpeg->Context = 0;
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief DeInitializes the JPEG peripheral.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
- {
- /* Check the JPEG handle allocation */
- if(hjpeg == NULL)
- {
- return HAL_ERROR;
- }
-
- /* DeInit the low level hardware: CLOCK, NVIC.*/
- HAL_JPEG_MspDeInit(hjpeg);
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_BUSY;
-
- /* Reset the JPEG ErrorCode */
- hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
-
- /* Reset JpegInCount and JpegOutCount */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_RESET;
-
- /*Clear the context fields*/
- hjpeg->Context = 0;
- /* Release Lock */
- __HAL_UNLOCK(hjpeg);
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Initializes the JPEG MSP.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_MspInit could be implemented in the user file
- */
- }
- /**
- * @brief DeInitializes JPEG MSP.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_MspDeInit could be implemented in the user file
- */
- }
- /**
- * @}
- */
- /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
- * @brief JPEG Configuration functions.
- *
- @verbatim
- ==============================================================================
- ##### Configuration functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
- (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
- (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
- (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
- (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
-
- @endverbatim
- * @{
- */
- /**
- * @brief Set the JPEG encoding configuration.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pConf: pointer to a JPEG_ConfTypeDef structure that contains
- * the encoding configuration
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
- {
- uint32_t error = HAL_OK;
- uint32_t numberMCU, hfactor, vfactor,hMCU, vMCU;
-
- /* Check the JPEG handle allocation */
- if( (hjpeg == NULL) || (pConf == NULL) )
- {
- return HAL_ERROR;
- }
- else
- {
- /* Check the parameters */
- assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
- assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
- assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
-
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- hjpeg->State = HAL_JPEG_STATE_BUSY;
-
- hjpeg->Conf.ColorSpace = pConf->ColorSpace;
- hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
- hjpeg->Conf.ImageHeight = pConf->ImageHeight;
- hjpeg->Conf.ImageWidth = pConf->ImageWidth;
- hjpeg->Conf.ImageQuality = pConf->ImageQuality;
-
- /* Reset the Color Space : by default only one quantization table is used*/
- hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
-
- /* Set Number of color components*/
- if(hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
- {
- /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
- hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
-
- JPEG_SetColorGrayScale(hjpeg);
- /* Set quantization table 0*/
- error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
- }
- else if(hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
- {
- /*
- Set the Color Space for YCbCr : 2 quantization tables are used
- one for Luminance(Y) and one for both Chrominances (Cb & Cr)
- */
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
-
- JPEG_SetColorYCBCR(hjpeg);
-
- /* Set quantization table 0*/
- error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
- /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
- error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
- if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
- {
- /* use 3 quantization tables , one for each component*/
- hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
-
- error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
-
- /*Use Quantization 1 table for component 1*/
- hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
- hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
-
- /*Use Quantization 2 table for component 2*/
- hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
- hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
- }
- }
- else /* ColorSpace == JPEG_CMYK_COLORSPACE */
- {
- JPEG_SetColorCMYK(hjpeg);
-
- /* Set quantization table 0*/
- error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
- /*By default quantization table 0 for All components*/
-
- if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
- {
- /* use 4 quantization tables , one for each component*/
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
-
- error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
- error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
- error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
-
- /*Use Quantization 1 table for component 1*/
- hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
-
- /*Use Quantization 2 table for component 2*/
- hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
-
- /*Use Quantization 3 table for component 3*/
- hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
- }
- }
- if(error != HAL_OK)
- {
- hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Set the JPEG State to ready */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- return HAL_ERROR;
- }
- /* Set the image size*/
- MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFF) << 16)); /* set the number of lines*/
- MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFF) << 16)); /* set the number of pixels per line*/
-
- if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
- {
- hfactor = 16;
- vfactor = 16;
- }
- else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
- {
- hfactor = 16;
- vfactor = 8;
- }
- else /* Default is 8x8 MCU, 4:4:4*/
- {
- hfactor = 8;
- vfactor = 8;
- }
-
- hMCU = (hjpeg->Conf.ImageWidth / hfactor);
- if((hjpeg->Conf.ImageWidth % hfactor) != 0)
- {
- hMCU++; /*+1 for horizontal incomplete MCU */
- }
- vMCU = (hjpeg->Conf.ImageHeight / vfactor);
- if((hjpeg->Conf.ImageHeight % vfactor) != 0)
- {
- vMCU++; /*+1 for vertical incomplete MCU */
- }
-
- numberMCU = (hMCU * vMCU) - 1; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
- /* Set the number of MCU*/
- hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
-
- hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Set the JPEG State to ready */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Return function status */
- return HAL_BUSY;
- }
- }
- }
- /**
- * @brief Extract the image configuration from the JPEG header during the decoding
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
- * The JPEG decoded header informations
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
- {
- uint32_t yblockNb, cBblockNb, cRblockNb;
-
- /* Check the JPEG handle allocation */
- if((hjpeg == NULL) || (pInfo == NULL))
- {
- return HAL_ERROR;
- }
-
- /*Read the conf parameters */
- if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
- {
- pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
- }
- else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0)
- {
- pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
- }
- else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
- {
- pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
- }
-
- pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000U) >> 16;
- pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000U) >> 16;
-
- if((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
- {
- yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
- cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
- cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
-
- if((yblockNb == 1) && (cBblockNb == 0) && (cRblockNb == 0))
- {
- pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
- }
- else if((yblockNb == 0) && (cBblockNb == 0) && (cRblockNb == 0))
- {
- pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
- }
- else if((yblockNb == 3) && (cBblockNb == 0) && (cRblockNb == 0))
- {
- pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
- }
- else /*Default is 4:4:4*/
- {
- pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
- }
- }
- else
- {
- pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
- }
-
- pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Enable JPEG Header parsing for decoding
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for the JPEG.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
- {
- /* Process locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_BUSY;
-
- /* Enable header processing*/
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
-
- /* Process unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- return HAL_OK;
- }
- else
- {
- /* Process unlocked */
- __HAL_UNLOCK(hjpeg);
- return HAL_BUSY;
- }
- }
- /**
- * @brief Disable JPEG Header parsing for decoding
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for the JPEG.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
- {
- /* Process locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_BUSY;
-
- /* Disable header processing*/
- hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
-
- /* Process unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- return HAL_OK;
- }
- else
- {
- /* Process unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- }
- /**
- * @brief Modify the default Quantization tables used for JPEG encoding.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param QTable0 : pointer to uint8_t , define the user quantification table for color component 1.
- * If NULL assume no need to update the table and no error return
- * @param QTable1 : pointer to uint8_t , define the user quantification table for color component 2.
- * If NULL assume no need to update the table and no error return.
- * @param QTable2 : pointer to uint8_t , define the user quantification table for color component 3,
- * If NULL assume no need to update the table and no error return.
- * @param QTable3 : pointer to uint8_t , define the user quantification table for color component 4.
- * If NULL assume no need to update the table and no error return.
- *
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1, uint8_t *QTable2, uint8_t *QTable3)
- {
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- /* Change the DMA state */
- hjpeg->State = HAL_JPEG_STATE_BUSY;
-
- hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
-
- hjpeg->QuantTable0 = QTable0;
- hjpeg->QuantTable1 = QTable1;
- hjpeg->QuantTable2 = QTable2;
- hjpeg->QuantTable3 = QTable3;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Change the DMA state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /* Return function status */
- return HAL_OK;
- }
- else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- }
- /**
- * @}
- */
-
- /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
- * @brief processing functions.
- *
- @verbatim
- ==============================================================================
- ##### JPEG processing functions #####
- ==============================================================================
- [..] This section provides functions allowing to:
- (+) HAL_JPEG_Encode() : JPEG encoding with polling process
- (+) HAL_JPEG_Decode() : JPEG decoding with polling process
- (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
- (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
- (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
- (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
- (+) HAL_JPEG_Pause() : Pause the Input/Output processing
- (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
- (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
- (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
- (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
- @endverbatim
- * @{
- */
- /**
- * @brief Starts JPEG encoding with polling processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataInMCU: Pointer to the Input buffer
- * @param InDataLength: size in bytes Input buffer
- * @param pDataOut: Pointer to the jpeg output data buffer
- * @param OutDataLength: size in bytes of the Output buffer
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
- {
- uint32_t tickstart = 0;
-
- /* Check the parameters */
- assert_param((InDataLength >= 4));
- assert_param((OutDataLength >= 4));
-
- /* Check In/out buffer allocation and size */
- if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
- {
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State != HAL_JPEG_STATE_READY)
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
- {
- /*Change JPEG state*/
- hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
-
- /*Set the Context to Encode with Polling*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
- hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
-
- /* Get tick */
- tickstart = HAL_GetTick();
- /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
- InDataLength = InDataLength - (InDataLength % 4);
- OutDataLength = OutDataLength - (OutDataLength % 4);
-
- /*Store In/out buffers pointers and size*/
- hjpeg->pJpegInBuffPtr = pDataInMCU;
- hjpeg->pJpegOutBuffPtr = pDataOut;
- hjpeg->InDataLength = InDataLength;
- hjpeg->OutDataLength = OutDataLength;
-
- /*Reset In/out data counter */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Init decoding process*/
- JPEG_Init_Process(hjpeg);
-
- /*JPEG data processing : In/Out FIFO transfer*/
- while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
- {
- if(Timeout != HAL_MAX_DELAY)
- {
- if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
- {
-
- /* Update error code */
- hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /*Change JPEG state*/
- hjpeg->State= HAL_JPEG_STATE_READY;
-
- return HAL_TIMEOUT;
- }
- }
- }
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /*Change JPEG state*/
- hjpeg->State= HAL_JPEG_STATE_READY;
-
- }else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_ERROR;
- }
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Starts JPEG decoding with polling processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataIn: Pointer to the input data buffer
- * @param InDataLength: size in bytes Input buffer
- * @param pDataOutMCU: Pointer to the Output data buffer
- * @param OutDataLength: size in bytes of the Output buffer
- * @param Timeout: Specify Timeout value
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength, uint32_t Timeout)
- {
- uint32_t tickstart = 0;
- /* Check the parameters */
- assert_param((InDataLength >= 4));
- assert_param((OutDataLength >= 4));
-
- /* Check In/out buffer allocation and size */
- if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
- {
- return HAL_ERROR;
- }
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- /* Get tick */
- tickstart = HAL_GetTick();
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- /*Change JPEG state*/
- hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
-
- /*Set the Context to Decode with Polling*/
- /*Set the Context to Encode with Polling*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
- hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
-
- /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
- InDataLength = InDataLength - (InDataLength % 4);
- OutDataLength = OutDataLength - (OutDataLength % 4);
-
- /*Store In/out buffers pointers and size*/
- hjpeg->pJpegInBuffPtr = pDataIn;
- hjpeg->pJpegOutBuffPtr = pDataOutMCU;
- hjpeg->InDataLength = InDataLength;
- hjpeg->OutDataLength = OutDataLength;
-
- /*Reset In/out data counter */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Init decoding process*/
- JPEG_Init_Process(hjpeg);
- /*JPEG data processing : In/Out FIFO transfer*/
- while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
- {
- if(Timeout != HAL_MAX_DELAY)
- {
- if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
- {
-
- /* Update error code */
- hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
- /*Change JPEG state*/
- hjpeg->State= HAL_JPEG_STATE_READY;
- return HAL_TIMEOUT;
- }
- }
- }
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /*Change JPEG state*/
- hjpeg->State= HAL_JPEG_STATE_READY;
-
- }else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Starts JPEG encoding with interrupt processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataInMCU: Pointer to the Input buffer
- * @param InDataLength: size in bytes Input buffer
- * @param pDataOut: Pointer to the jpeg output data buffer
- * @param OutDataLength: size in bytes of the Output buffer
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
- {
- /* Check the parameters */
- assert_param((InDataLength >= 4));
- assert_param((OutDataLength >= 4));
-
- /* Check In/out buffer allocation and size */
- if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
- {
- return HAL_ERROR;
- }
-
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State != HAL_JPEG_STATE_READY)
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- else
- {
- if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
- {
- /*Change JPEG state*/
- hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
-
- /*Set the Context to Encode with IT*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
- hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
-
- /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
- InDataLength = InDataLength - (InDataLength % 4);
- OutDataLength = OutDataLength - (OutDataLength % 4);
-
- /*Store In/out buffers pointers and size*/
- hjpeg->pJpegInBuffPtr = pDataInMCU;
- hjpeg->pJpegOutBuffPtr = pDataOut;
- hjpeg->InDataLength = InDataLength;
- hjpeg->OutDataLength = OutDataLength;
-
- /*Reset In/out data counter */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Init decoding process*/
- JPEG_Init_Process(hjpeg);
-
- }
- else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_ERROR;
- }
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Starts JPEG decoding with interrupt processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataIn: Pointer to the input data buffer
- * @param InDataLength: size in bytes Input buffer
- * @param pDataOutMCU: Pointer to the Output data buffer
- * @param OutDataLength: size in bytes of the Output buffer
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
- {
- /* Check the parameters */
- assert_param((InDataLength >= 4));
- assert_param((OutDataLength >= 4));
-
- /* Check In/out buffer allocation and size */
- if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
- {
- return HAL_ERROR;
- }
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- /*Change JPEG state*/
- hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
-
- /*Set the Context to Decode with IT*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
- hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
-
- /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
- InDataLength = InDataLength - (InDataLength % 4);
- OutDataLength = OutDataLength - (OutDataLength % 4);
-
- /*Store In/out buffers pointers and size*/
- hjpeg->pJpegInBuffPtr = pDataIn;
- hjpeg->pJpegOutBuffPtr = pDataOutMCU;
- hjpeg->InDataLength = InDataLength;
- hjpeg->OutDataLength = OutDataLength;
-
- /*Reset In/out data counter */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Init decoding process*/
- JPEG_Init_Process(hjpeg);
-
- }
- else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Starts JPEG encoding with DMA processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataInMCU: Pointer to the Input buffer
- * @param InDataLength: size in bytes Input buffer
- * @param pDataOut: Pointer to the jpeg output data buffer
- * @param OutDataLength: size in bytes of the Output buffer
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
- {
- /* Check the parameters */
- assert_param((InDataLength >= 4));
- assert_param((OutDataLength >= 4));
-
- /* Check In/out buffer allocation and size */
- if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
- {
- return HAL_ERROR;
- }
-
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State != HAL_JPEG_STATE_READY)
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- else
- {
- if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
- {
- /*Change JPEG state*/
- hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
-
- /*Set the Context to Encode with DMA*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
- hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
-
- /*Store In/out buffers pointers and size*/
- hjpeg->pJpegInBuffPtr = pDataInMCU;
- hjpeg->pJpegOutBuffPtr = pDataOut;
- hjpeg->InDataLength = InDataLength;
- hjpeg->OutDataLength = OutDataLength;
-
- /*Reset In/out data counter */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Init decoding process*/
- JPEG_Init_Process(hjpeg);
-
- /* JPEG encoding process using DMA */
- JPEG_DMA_StartProcess(hjpeg);
-
- }
- else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_ERROR;
- }
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Starts JPEG decoding with DMA processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataIn: Pointer to the input data buffer
- * @param InDataLength: size in bytes Input buffer
- * @param pDataOutMCU: Pointer to the Output data buffer
- * @param OutDataLength: size in bytes of the Output buffer
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
- {
- /* Check the parameters */
- assert_param((InDataLength >= 4));
- assert_param((OutDataLength >= 4));
-
- /* Check In/out buffer allocation and size */
- if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
- {
- return HAL_ERROR;
- }
-
- /* Process Locked */
- __HAL_LOCK(hjpeg);
-
- if(hjpeg->State == HAL_JPEG_STATE_READY)
- {
- /*Change JPEG state*/
- hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
-
- /*Set the Context to Decode with DMA*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
- hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
-
- /*Store In/out buffers pointers and size*/
- hjpeg->pJpegInBuffPtr = pDataIn;
- hjpeg->pJpegOutBuffPtr = pDataOutMCU;
- hjpeg->InDataLength = InDataLength;
- hjpeg->OutDataLength = OutDataLength;
-
- /*Reset In/out data counter */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Init decoding process*/
- JPEG_Init_Process(hjpeg);
-
- /* JPEG decoding process using DMA */
- JPEG_DMA_StartProcess(hjpeg);
-
- }
- else
- {
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_BUSY;
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Pause the JPEG Input/Output processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param XferSelection: This parameter can be one of the following values :
- * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
- * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
- * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
- {
- uint32_t mask = 0;
-
- assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
-
- if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
- {
- if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
- {
- hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
- }
- if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
- {
- hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
- }
- }
- else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
- {
-
- if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
- {
- hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
- mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
- }
- if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
- {
- hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
- mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
- }
- __HAL_JPEG_DISABLE_IT(hjpeg,mask);
- }
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Resume the JPEG Input/Output processing
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param XferSelection: This parameter can be one of the following values :
- * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
- * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
- * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
- {
- uint32_t mask = 0;
- uint32_t xfrSize = 0;
- assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
-
- if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
- {
-
- if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
- {
- hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
-
- /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
- then MDMA In buffer size is 32 bytes
-
- else (MDMA In is triggred with JPEG In FIFO not full flag)
- then MDMA In buffer size is 4 bytes
- */
- xfrSize = hjpeg->hdmain->Init.BufferTransferLength;
-
- /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
- hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % xfrSize);
-
- if(hjpeg->InDataLength > 0)
- {
- /* Start DMA FIFO In transfer */
- HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength, 1);
- }
- }
- if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
- {
- hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
-
- if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
- {
- JPEG_DMA_PollResidualData(hjpeg);
- }
- else
- {
- /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
- then MDMA out buffer size is 32 bytes
- else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
- then MDMA buffer size is 4 bytes
- */
- xfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
-
- /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
- hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % xfrSize);
-
- /* Start DMA FIFO Out transfer */
- HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength, 1);
- }
- }
- }
- else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
- {
- if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
- {
- hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
- mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
- }
- if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
- {
- hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
- mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
- }
- __HAL_JPEG_ENABLE_IT(hjpeg,mask);
- }
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Config Encoding/Decoding Input Buffer.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module.
- * @param pNewInputBuffer: Pointer to the new input data buffer
- * @param InDataLength: Size in bytes of the new Input data buffer
- * @retval HAL status
- */
- void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
- {
- hjpeg->pJpegInBuffPtr = pNewInputBuffer;
- hjpeg->InDataLength = InDataLength;
- }
- /**
- * @brief Config Encoding/Decoding Output Buffer.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module.
- * @param pNewOutputBuffer: Pointer to the new output data buffer
- * @param OutDataLength: Size in bytes of the new Output data buffer
- * @retval HAL status
- */
- void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
- {
- hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
- hjpeg->OutDataLength = OutDataLength;
- }
- /**
- * @brief Aborts the JPEG Encoding/Decoding.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t tickstart, tmpContext;
-
- tmpContext = hjpeg->Context;
-
- /*Reset the Context operation and method*/
- hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
-
- if((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
- {
- /* Stop the DMA In/out Xfer*/
- HAL_MDMA_Abort(hjpeg->hdmaout);
- HAL_MDMA_Abort(hjpeg->hdmain);
- }
-
- /* Stop the JPEG encoding/decoding process*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
- /* Get tick */
- tickstart = HAL_GetTick();
-
- /* Check if the JPEG Codec is effectively disabled */
- while(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != RESET)
- {
- /* Check for the Timeout */
- if((HAL_GetTick() - tickstart ) > JPEG_TIMEOUT_VALUE)
- {
- /* Update error code */
- hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
-
- /* Change the DMA state */
- hjpeg->State = HAL_JPEG_STATE_TIMEOUT;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- return HAL_TIMEOUT;
- }
- }
-
- /* Disable All Interrupts */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- /* Flush input and output FIFOs*/
- hjpeg->Instance->CR |= JPEG_CR_IFF;
- hjpeg->Instance->CR |= JPEG_CR_OFF;
-
- /* Clear all flags */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
- /* Reset JpegInCount and JpegOutCount */
- hjpeg->JpegInCount = 0;
- hjpeg->JpegOutCount = 0;
-
- /*Reset the Context Pause*/
- hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
-
- /* Change the DMA state*/
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Return function status */
- return HAL_OK;
- }
-
-
- /**
- * @}
- */
-
- /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
- * @brief JPEG process callback functions.
- *
- @verbatim
- ==============================================================================
- ##### JPEG Decode and Encode callback functions #####
- ==============================================================================
- [..] This section provides callback functions:
- (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
- (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
- (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
- (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
- (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
- (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
- @endverbatim
- * @{
- */
- /**
- * @brief Decoding JPEG Info ready callback.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
- * The JPEG decoded header informations
- * @retval None
- */
- __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg,JPEG_ConfTypeDef *pInfo)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
- UNUSED(pInfo);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
- */
- }
- /**
- * @brief Encoding complete callback.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
- */
- }
- /**
- * @brief Decoding complete callback.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
- */
- }
- /**
- * @brief JPEG error callback.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_ErrorCallback could be implemented in the user file
- */
- }
- /**
- * @brief Get New Data chunk callback.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param NbDecodedData: Number of consummed data in the previous chunk in bytes
- * @retval None
- */
- __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
- UNUSED(NbDecodedData);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_GetDataCallback could be implemented in the user file
- */
- }
- /**
- * @brief Decoded/Encoded Data ready callback.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param pDataOut: pointer to the output data buffer
- * @param OutDataLength: number in bytes of data available in the specified output buffer
- * @retval None
- */
- __weak void HAL_JPEG_DataReadyCallback (JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hjpeg);
- UNUSED(pDataOut);
- UNUSED(OutDataLength);
-
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_JPEG_DataReadyCallback could be implemented in the user file
- */
- }
- /**
- * @}
- */
-
- /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
- * @brief JPEG IRQ handler.
- *
- @verbatim
- ==============================================================================
- ##### JPEG IRQ handler management #####
- ==============================================================================
- [..] This section provides JPEG IRQ handler function.
- (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
- @endverbatim
- * @{
- */
- /**
- * @brief This function handles JPEG interrupt request.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
- {
- switch(hjpeg->State)
- {
- case HAL_JPEG_STATE_BUSY_ENCODING:
- case HAL_JPEG_STATE_BUSY_DECODING:
- /* continue JPEG data encoding/Decoding*/
- /* JPEG data processing : In/Out FIFO transfer*/
- if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
- {
- JPEG_Process(hjpeg);
- }
- else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
- {
- JPEG_DMA_ContinueProcess(hjpeg);
- }
-
- break;
-
- default:
- break;
- }
- }
- /**
- * @}
- */
- /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
- * @brief Peripheral State functions.
- *
- @verbatim
- ==============================================================================
- ##### Peripheral State and Error functions #####
- ==============================================================================
- [..] This section provides JPEG State and Errors function.
- (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
- (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
- @endverbatim
- * @{
- */
- /**
- * @brief Returns the JPEG state.
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval JPEG state
- */
- HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
- {
- return hjpeg->State;
- }
- /**
- * @brief Return the JPEG error code
- * @param hjpeg : pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for the specified JPEG.
- * @retval JPEG Error Code
- */
- uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
- {
- return hjpeg->ErrorCode;
- }
- /**
- * @}
- */
- /**
- * @}
- */
-
- /** @addtogroup JPEG_Private_Functions
- * @{
- */
- /**
- * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
- * @param Bits: pointer to bits table
- * @param Huffsize: pointer to sizes table
- * @param Huffcode: pointer to codes table
- * @param LastK: pointer to last Coeff (table dimmension)
- * @retval HAL status
- */
- static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
- {
- uint32_t i, p, l, code, si;
-
- /* Figure C.1: Generation of table of Huffman code sizes */
- p = 0;
- for (l = 0; l < 16; l++)
- {
- i = (uint32_t)Bits[l];
- if ( (p + i) > 256)
- { /* check for table overflow */
- return HAL_ERROR;
- }
- while (i != 0)
- {
- Huffsize[p++] = (uint8_t) l+1;
- i--;
- }
- }
- Huffsize[p] = 0;
- *LastK = p;
-
- /* Figure C.2: Generation of table of Huffman codes */
- code = 0;
- si = Huffsize[0];
- p = 0;
- while (Huffsize[p] != 0)
- {
- while (((uint32_t) Huffsize[p]) == si)
- {
- Huffcode[p++] = code;
- code++;
- }
- /* code must fit in "size" bits (si), no code is allowed to be all ones*/
- if (((uint32_t) code) >= (((uint32_t) 1) << si))
- {
- return HAL_ERROR;
- }
- code <<= 1;
- si++;
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
- * that can programmed to the JPEG encoder registers
- * @param AC_BitsValsTable: pointer to AC huffman bits/vals table
- * @param AC_SizeCodesTable: pointer to AC huffman Sizes/Codes table
- * @retval HAL status
- */
- static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
- {
- HAL_StatusTypeDef error;
- uint8_t huffsize[257];
- uint32_t huffcode[257];
- uint32_t k;
- uint32_t l,lsb, msb;
- uint32_t lastK;
-
- error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
- if(error != HAL_OK)
- {
- return error;
- }
-
- /* Figure C.3: Ordering procedure for encoding procedure code tables */
- k=0;
-
- while(k < lastK)
- {
- l = AC_BitsValsTable->HuffVal[k];
- if(l == 0)
- {
- l = 160; /*l = 0x00 EOB code*/
- }
- else if(l == 0xF0)/* l = 0xF0 ZRL code*/
- {
- l = 161;
- }
- else
- {
- msb = (l & 0xF0) >> 4;
- lsb = (l & 0x0F);
- l = (msb * 10) + lsb - 1;
- }
- if(l >= JPEG_AC_HUFF_TABLE_SIZE)
- {
- return HAL_ERROR; /* Huffman Table overflow error*/
- }
- else
- {
- AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
- AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
- k++;
- }
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
- * that can programmed to the JPEG encoder registers
- * @param DC_BitsValsTable: pointer to DC huffman bits/vals table
- * @param DC_SizeCodesTable: pointer to DC huffman Sizes/Codes table
- * @retval HAL status
- */
- static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
- {
- HAL_StatusTypeDef error;
- uint32_t k;
- uint32_t l;
- uint32_t lastK;
- uint8_t huffsize[257];
- uint32_t huffcode[257];
- error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
- if(error != HAL_OK)
- {
- return error;
- }
- /* Figure C.3: ordering procedure for encoding procedure code tables */
- k=0;
-
- while(k < lastK)
- {
- l = DC_BitsValsTable->HuffVal[k];
- if(l >= JPEG_DC_HUFF_TABLE_SIZE)
- {
- return HAL_ERROR; /* Huffman Table overflow error*/
- }
- else
- {
- DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
- DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
- k++;
- }
- }
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Set the JPEG register with an DC huffman table at the given DC table address
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param HuffTableDC: pointer to DC huffman table
- * @param DCTableAddress: Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
- * @retval HAL status
- */
- static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, __IO uint32_t *DCTableAddress)
- {
- HAL_StatusTypeDef error = HAL_OK;
- JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
- uint32_t i, lsb, msb;
- __IO uint32_t *address, *addressDef;
-
- if(DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
- {
- address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE/2));
- }
- else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
- {
- address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE/2));
- }
- else
- {
- return HAL_ERROR;
- }
- if(HuffTableDC != NULL)
- {
- error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
- if(error != HAL_OK)
- {
- return error;
- }
- addressDef = address;
- *addressDef = 0x0FFF0FFF;
- addressDef++;
- *addressDef = 0x0FFF0FFF;
-
- i = JPEG_DC_HUFF_TABLE_SIZE;
- while(i>0)
- {
- i--;
- address --;
- msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
- i--;
- lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
- *address = lsb | (msb << 16);
- }
- }
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Set the JPEG register with an AC huffman table at the given AC table address
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param HuffTableAC: pointer to AC huffman table
- * @param ACTableAddress: Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
- * @retval HAL status
- */
- static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, __IO uint32_t *ACTableAddress)
- {
- HAL_StatusTypeDef error = HAL_OK;
- JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
- uint32_t i, lsb, msb;
- __IO uint32_t *address, *addressDef;
-
- if(ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
- {
- address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE/2));
- }
- else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
- {
- address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE/2));
- }
- else
- {
- return HAL_ERROR;
- }
-
- if(HuffTableAC != NULL)
- {
- error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
- if(error != HAL_OK)
- {
- return error;
- }
- /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
- /* Locations 162:175 of each AC table contain information used internally by the core */
- addressDef = address;
- for(i=0; i<3; i++)
- {
- *addressDef = 0x0FFF0FFF;
- addressDef++;
- }
- *addressDef = 0x0FD10FD0;
- addressDef++;
- *addressDef = 0x0FD30FD2;
- addressDef++;
- *addressDef = 0x0FD50FD4;
- addressDef++;
- *addressDef = 0x0FD70FD6;
- /* end of Locations 162:175 */
-
- i = JPEG_AC_HUFF_TABLE_SIZE;
- while (i > 0)
- {
- i--;
- address--;
- msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
- i--;
- lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
-
- *address = lsb | (msb << 16);
- }
- }
-
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Configure the JPEG encoder register huffman tables to used during
- * the encdoing operation
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param HuffTableAC0: AC0 huffman table
- * @param HuffTableDC0: DC0 huffman table
- * @param HuffTableAC1: AC1 huffman table
- * @param HuffTableDC1: DC1 huffman table
- * @retval None
- */
- static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
- {
- HAL_StatusTypeDef error = HAL_OK;
-
- JPEG_Set_Huff_DHTMem(hjpeg, HuffTableAC0, HuffTableDC0, HuffTableAC1, HuffTableDC1);
-
- if(HuffTableAC0 != NULL)
- {
- error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC0, (hjpeg->Instance->HUFFENC_AC0));
- if(error != HAL_OK)
- {
- return error;
- }
- }
-
- if(HuffTableAC1 != NULL)
- {
- error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC1, (hjpeg->Instance->HUFFENC_AC1));
- if(error != HAL_OK)
- {
- return error;
- }
- }
-
- if(HuffTableDC0 != NULL)
- {
- error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC0, hjpeg->Instance->HUFFENC_DC0);
- if(error != HAL_OK)
- {
- return error;
- }
- }
-
- if(HuffTableDC1 != NULL)
- {
- error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC1, hjpeg->Instance->HUFFENC_DC1);
- if(error != HAL_OK)
- {
- return error;
- }
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Configure the JPEG register huffman tables to be included in the JPEG
- * file header (used for encoding only)
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param HuffTableAC0: AC0 huffman table
- * @param HuffTableDC0: DC0 huffman table
- * @param HuffTableAC1: AC1 huffman table
- * @param HuffTableDC1: DC1 huffman table
- * @retval None
- */
- static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
- {
- uint32_t value, index;
- __IO uint32_t *address;
- if(HuffTableDC0 != NULL)
- {
- /* DC0 Huffman Table : BITS*/
- /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
- address = (hjpeg->Instance->DHTMEM + 3);
- index = 16;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableDC0->Bits[index-1] & 0xFF) << 24)|
- (((uint32_t)HuffTableDC0->Bits[index-2] & 0xFF) << 16)|
- (((uint32_t)HuffTableDC0->Bits[index-3] & 0xFF) << 8) |
- ((uint32_t)HuffTableDC0->Bits[index-4] & 0xFF);
- address--;
- index -=4;
-
- }
- /* DC0 Huffman Table : Val*/
- /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
- address = (hjpeg->Instance->DHTMEM + 6);
- index = 12;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableDC0->HuffVal[index-1] & 0xFF) << 24)|
- (((uint32_t)HuffTableDC0->HuffVal[index-2] & 0xFF) << 16)|
- (((uint32_t)HuffTableDC0->HuffVal[index-3] & 0xFF) << 8) |
- ((uint32_t)HuffTableDC0->HuffVal[index-4] & 0xFF);
- address--;
- index -=4;
- }
- }
- if(HuffTableAC0 != NULL)
- {
- /* AC0 Huffman Table : BITS*/
- /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
- address = (hjpeg->Instance->DHTMEM + 10);
- index = 16;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableAC0->Bits[index-1] & 0xFF) << 24)|
- (((uint32_t)HuffTableAC0->Bits[index-2] & 0xFF) << 16)|
- (((uint32_t)HuffTableAC0->Bits[index-3] & 0xFF) << 8) |
- ((uint32_t)HuffTableAC0->Bits[index-4] & 0xFF);
- address--;
- index -=4;
-
- }
- /* AC0 Huffman Table : Val*/
- /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
- /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
- address = (hjpeg->Instance->DHTMEM + 51);
- value = *address & 0xFFFF0000U;
- value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFF) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFF);
- *address = value;
-
- /*continue setting 160 AC0 huffman values */
- address--; /* address = hjpeg->Instance->DHTMEM + 50*/
- index = 160;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableAC0->HuffVal[index-1] & 0xFF) << 24)|
- (((uint32_t)HuffTableAC0->HuffVal[index-2] & 0xFF) << 16)|
- (((uint32_t)HuffTableAC0->HuffVal[index-3] & 0xFF) << 8) |
- ((uint32_t)HuffTableAC0->HuffVal[index-4] & 0xFF);
- address--;
- index -=4;
- }
- }
- if(HuffTableDC1 != NULL)
- {
- /* DC1 Huffman Table : BITS*/
- /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
- /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
- address = (hjpeg->Instance->DHTMEM + 51);
- value = *address & 0x0000FFFFU;
- value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFF) << 16);
- *address = value;
-
- /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
- address = (hjpeg->Instance->DHTMEM + 55);
- value = *address & 0xFFFF0000U;
- value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFF);
- *address = value;
-
- /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
- address--;
- index = 12;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableDC1->Bits[index+1] & 0xFF) << 24)|
- (((uint32_t)HuffTableDC1->Bits[index] & 0xFF) << 16)|
- (((uint32_t)HuffTableDC1->Bits[index-1] & 0xFF) << 8) |
- ((uint32_t)HuffTableDC1->Bits[index-2] & 0xFF);
- address--;
- index -=4;
-
- }
- /* DC1 Huffman Table : Val*/
- /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
- /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
- address = (hjpeg->Instance->DHTMEM + 55);
- value = *address & 0x0000FFFF;
- value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFF) << 16);
- *address = value;
-
- /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
- address = (hjpeg->Instance->DHTMEM + 58);
- value = *address & 0xFFFF0000U;
- value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFF);
- *address = value;
-
- /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
- address--;
- index = 8;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableDC1->HuffVal[index+1] & 0xFF) << 24)|
- (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFF) << 16)|
- (((uint32_t)HuffTableDC1->HuffVal[index-1] & 0xFF) << 8) |
- ((uint32_t)HuffTableDC1->HuffVal[index-2] & 0xFF);
- address--;
- index -=4;
- }
- }
-
- if(HuffTableAC1 != NULL)
- {
- /* AC1 Huffman Table : BITS*/
- /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
- /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
- address = (hjpeg->Instance->DHTMEM + 58);
- value = *address & 0x0000FFFFU;
- value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFF) << 16);
- *address = value;
-
- /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
- address = (hjpeg->Instance->DHTMEM + 62);
- value = *address & 0xFFFF0000U;
- value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFF);
- *address = value;
-
- /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
- address--;
- index = 12;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableAC1->Bits[index+1] & 0xFF) << 24)|
- (((uint32_t)HuffTableAC1->Bits[index] & 0xFF) << 16)|
- (((uint32_t)HuffTableAC1->Bits[index-1] & 0xFF) << 8) |
- ((uint32_t)HuffTableAC1->Bits[index-2] & 0xFF);
- address--;
- index -=4;
-
- }
- /* AC1 Huffman Table : Val*/
- /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
- /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
- address = (hjpeg->Instance->DHTMEM + 62);
- value = *address & 0x0000FFFF;
- value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFF) << 16);
- *address = value;
-
- /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
- address = (hjpeg->Instance->DHTMEM + 102);
- index = 160;
- while(index > 0)
- {
- *address = (((uint32_t)HuffTableAC1->HuffVal[index+1] & 0xFF) << 24)|
- (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFF) << 16)|
- (((uint32_t)HuffTableAC1->HuffVal[index-1] & 0xFF) << 8) |
- ((uint32_t)HuffTableAC1->HuffVal[index-2] & 0xFF);
- address--;
- index -=4;
- }
- }
- }
- /**
- * @brief Configure the JPEG registers with a given quantization table
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param QTable: pointer to an array of 64 bytes giving the quantization table
- * @param QTableAddress: destination quantization address in the JPEG peripheral
- * it could be QMEM0, QMEM1, QMEM2 or QMEM3
- * @retval None
- */
- static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, __IO uint32_t *QTableAddress)
- {
- uint32_t i, j, quantRow, quantVal, ScaleFactor;
- __IO uint32_t *tableAddress;
-
- if((QTableAddress == ((hjpeg->Instance->QMEM0))) ||
- (QTableAddress == ((hjpeg->Instance->QMEM1))) ||
- (QTableAddress == ((hjpeg->Instance->QMEM2))) ||
- (QTableAddress == ((hjpeg->Instance->QMEM3))))
- {
- tableAddress = QTableAddress;
- }
- else
- {
- return HAL_ERROR;
- }
-
- if ((hjpeg->Conf.ImageQuality >= 50) && (hjpeg->Conf.ImageQuality <= 100))
- {
- ScaleFactor = 200 - (hjpeg->Conf.ImageQuality * 2);
- }
- else if (hjpeg->Conf.ImageQuality > 0)
- {
- ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
- }
- else
- {
- return HAL_ERROR;
- }
- /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
- i = 0;
- while( i < JPEG_QUANT_TABLE_SIZE)
- {
- quantRow = 0;
- for(j=0; j<4; j++)
- {
- /* Note that the quantization coefficients must be specified in the table in zigzag order */
- quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i+j]]) * ScaleFactor) + 50) / 100;
-
- if(quantVal == 0)
- {
- quantVal = 1;
- }
- else if (quantVal > 255)
- {
- quantVal = 255;
- }
-
- quantRow |= ((quantVal & 0xFF) << (8 * j));
- }
- i += 4;
- *tableAddress = quantRow;
- tableAddress ++;
- }
- /* Return function status */
- return HAL_OK;
- }
- /**
- * @brief Configure the JPEG registers for YCbCr color space
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t ySamplingH;
- uint32_t ySamplingV;
- uint32_t yblockNb;
-
- /*Set Number of color components to 3*/
- hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
-
- /* compute MCU block size and Y, Cb ,Cr sampling factors*/
- if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
- {
- ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
- ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
-
- yblockNb = 0x30; /* 4 blocks of 8x8*/
- }
- else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
- {
- ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
- ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
-
- yblockNb = 0x10; /* 2 blocks of 8x8*/
- }
- else /*JPEG_444_SUBSAMPLING and default*/
- {
- ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
- ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
-
- yblockNb = 0; /* 1 block of 8x8*/
- }
-
- hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
- hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
-
- /*Reset CONFR4 register*/
- hjpeg->Instance->CONFR4 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
- hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
-
- /*Reset CONFR5 register*/
- hjpeg->Instance->CONFR5 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
- hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
-
- /*Reset CONFR6 register*/
- hjpeg->Instance->CONFR6 = 0;
- /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
- /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
- /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
- hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
-
- }
- /**
- * @brief Configure the JPEG registers for GrayScale color space
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
- {
- /*Set Number of color components to 1*/
- hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
-
- /*in GrayScale use 1 single Quantization table (Table 0)*/
- /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
-
- /*Reset CONFR4 register*/
- hjpeg->Instance->CONFR4 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
- hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
- }
- /**
- * @brief Configure the JPEG registers for CMYK color space
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t ySamplingH;
- uint32_t ySamplingV;
- uint32_t yblockNb;
-
- /*Set Number of color components to 4*/
- hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
-
- /* compute MCU block size and Y, Cb ,Cr sampling factors*/
- if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
- {
- ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
- ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
-
- yblockNb = 0x30; /* 4 blocks of 8x8*/
- }
- else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
- {
- ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
- ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
-
- yblockNb = 0x10; /* 2 blocks of 8x8*/
- }
- else /*JPEG_444_SUBSAMPLING and default*/
- {
- ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
- ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
-
- yblockNb = 0; /* 1 block of 8x8*/
- }
-
- /*Reset CONFR4 register*/
- hjpeg->Instance->CONFR4 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
- hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
-
- /*Reset CONFR5 register*/
- hjpeg->Instance->CONFR5 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
- hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
-
- /*Reset CONFR6 register*/
- hjpeg->Instance->CONFR6 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
- hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
-
- /*Reset CONFR7 register*/
- hjpeg->Instance->CONFR7 = 0;
- /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
- hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
- }
- /**
- * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None
- */
- static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
- {
- /*Reset pause*/
- hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
-
- if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
- {
- /*Set JPEG Codec to Decoding mode */
- hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
- }
- else if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
- {
- /*Set JPEG Codec to Encoding mode */
- hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
- }
-
- /*Stop JPEG processing */
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
-
- /* Disable All Interrupts */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- /* Flush input and output FIFOs*/
- hjpeg->Instance->CR |= JPEG_CR_IFF;
- hjpeg->Instance->CR |= JPEG_CR_OFF;
-
- /* Clear all flags */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
-
- /*Start Encoding/Decoding*/
- hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
-
- if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
- {
- /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
- __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC |JPEG_IT_HPD);
- }
- else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
- {
- /*Enable End Of Conversation, and End Of Header parsing interruptions*/
- __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC |JPEG_IT_HPD);
-
- }
- }
- /**
- * @brief JPEG encoding/decoding process in case of Polling or Interrupt
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
- */
- static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t tmpContext;
-
- /*End of header processing flag rised*/
- if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
- {
- /*Call Header parsing complet callback */
- HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
- /* Reset the ImageQuality */
- hjpeg->Conf.ImageQuality = 0;
- /* Note : the image quality is only available at the end of the decoding operation */
- /* at the current stage the calculated image quality is not correct so reset it */
-
- /*Call Info Ready callback */
- HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
-
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
-
- /* Clear header processing done flag */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
- }
- /*Input FIFO status handling*/
- if((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0)
- {
- if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != RESET)
- {
- /*Input FIFO threshold flag rised*/
- /*4 words (16 bytes) can be written in */
- JPEG_ReadInputData(hjpeg,JPEG_FIFO_TH_SIZE);
- }
- else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != RESET)
- {
- /*Input FIFO Not Full flag rised*/
- /*32-bit value can be written in */
- JPEG_ReadInputData(hjpeg,1);
- }
- }
-
-
- /*Output FIFO flag handling*/
- if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
- {
- if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != RESET)
- {
- /*Output FIFO threshold flag rised*/
- /*4 words (16 bytes) can be read out */
- JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
- }
- else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != RESET)
- {
- /*Output FIFO Not Empty flag rised*/
- /*32-bit value can be read out */
- JPEG_StoreOutputData(hjpeg, 1);
- }
- }
-
- /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
- if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
- {
- /*Stop Encoding/Decoding*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
- if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
- {
- /* Disable All Interrupts */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
- }
-
- /* Clear all flags */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
-
- /*Call End of conversion callback */
- if(hjpeg->JpegOutCount > 0)
- {
- /*Output Buffer is not empty, call DecodedDataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
-
- /*Reset Context Operation*/
- tmpContext = hjpeg->Context;
- /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
- hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /*Call End of Encoding/Decoding callback */
- if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
- {
- HAL_JPEG_DecodeCpltCallback(hjpeg);
- }
- else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
- {
- HAL_JPEG_EncodeCpltCallback(hjpeg);
- }
-
- return JPEG_PROCESS_DONE;
- }
-
- return JPEG_PROCESS_ONGOING;
- }
- /**
- * @brief Store some output data from the JPEG peripheral to the output buffer.
- * This function is used when the JPEG peripheral has new data to output
- * in case of Polling or Interrupt process
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param nbOutputWords: Number of output words (of 32 bits) ready from the JPEG peripheral
- * @retval None
- */
- static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
- {
- uint32_t index, nBwords, nbBytes , dataword;
- if(hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords*4)))
- {
- for(index = 0; index < nbOutputWords; index++)
- {
- /*Transfer 32 bits from the JPEG output FIFO*/
- dataword = hjpeg->Instance->DOR;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = dataword & 0x000000FF;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1] = (dataword & 0x0000FF00) >> 8;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2] = (dataword & 0x00FF0000) >> 16;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3] = (dataword & 0xFF000000) >> 24;
- hjpeg->JpegOutCount += 4;
- }
- if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
- {
- /*Output Buffer is full, call DecodedDataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
- }
- else if(hjpeg->OutDataLength > hjpeg->JpegOutCount)
- {
- nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount)/4;
- for(index = 0; index < nBwords; index++)
- {
- /*Transfer 32 bits from the JPEG output FIFO*/
- dataword = hjpeg->Instance->DOR;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = dataword & 0x000000FF;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1] = (dataword & 0x0000FF00) >> 8;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2] = (dataword & 0x00FF0000) >> 16;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3] = (dataword & 0xFF000000) >> 24;
- hjpeg->JpegOutCount += 4;
- }
- if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
- {
- /*Output Buffer is full, call DecodedDataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
- else
- {
- nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
- dataword = hjpeg->Instance->DOR;
- for(index = 0; index < nbBytes; index++)
- {
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
- hjpeg->JpegOutCount++;
- }
- /*Output Buffer is full, call DecodedDataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
-
- nbBytes = 4 - nbBytes;
- for(index = nbBytes; index < 4; index++)
- {
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
- hjpeg->JpegOutCount++;
- }
- }
- }
- }
- /**
- * @brief Read some input Data from the input buffer.
- * This function is used when the JPEG peripheral needs new data
- * in case of Polling or Interrupt process
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @param nbRequestWords: Number of input words (of 32 bits) that the JPE peripheral request
- * @retval None
- */
- static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
- {
- uint32_t nbBytes = 0, nBwords = 0, index = 0, Dataword = 0, inputCount = 0;
-
- if((hjpeg->InDataLength == 0) || (nbRequestWords == 0))
- {
- /* No more Input data : nothing to do*/
- HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
- }
- else if(hjpeg->InDataLength > hjpeg->JpegInCount)
- {
- nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
- }
- else if(hjpeg->InDataLength == hjpeg->JpegInCount)
- {
- /*Call HAL_JPEG_GetDataCallback to get new data */
- HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
- if(hjpeg->InDataLength > 4)
- {
- hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
- }
- hjpeg->JpegInCount = 0;
- nbBytes = hjpeg->InDataLength;
- }
- if((nbBytes > 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0))
- {
- nBwords = nbBytes / 4;
- if(nBwords >= nbRequestWords)
- {
- for(index = 0; index < nbRequestWords; index++)
- {
- inputCount = hjpeg->JpegInCount;
- hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount])) | (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 1])) << 8) |\
- (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 2])) << 16) | (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 3])) << 24));
-
- hjpeg->JpegInCount += 4;
- }
- }
- else /*nBwords < nbRequestWords*/
- {
- if(nBwords > 0)
- {
- for(index = 0; index < nBwords; index++)
- {
- inputCount = hjpeg->JpegInCount;
- hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount])) | (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 1])) << 8) |\
- (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 2])) << 16) | (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 3])) << 24));
-
- hjpeg->JpegInCount += 4;
- }
- }
- else
- {
- /* end of file*/
- Dataword = 0;
- for(index=0; index< nbBytes; index++)
- {
- Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8 * index);
- hjpeg->JpegInCount++;
- }
- hjpeg->Instance->DIR = Dataword;
- }
- }
- }
- }
- /**
- * @brief Start the JPEG DMA process (encoding/decoding)
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
- */
- static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t inXfrSize, outXfrSize;
-
- /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
- then MDMA In buffer size is 32 bytes
- else (MDMA In is triggred with JPEG In FIFO not full flag)
- then MDMA In buffer size is 4 bytes
- */
- inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
-
- /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
- then MDMA out buffer size is 32 bytes
- else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
- then MDMA buffer size is 4 bytes
- */
- outXfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
- if((hjpeg->InDataLength < inXfrSize) || (hjpeg->OutDataLength < outXfrSize))
- {
- return HAL_ERROR;
- }
- /* Set the JPEG MDMA In transfer complete callback */
- hjpeg->hdmain->XferCpltCallback = JPEG_MDMAInCpltCallback;
- /* Set the MDMA In error callback */
- hjpeg->hdmain->XferErrorCallback = JPEG_MDMAErrorCallback;
-
- /* Set the JPEG MDMA Out transfer complete callback */
- hjpeg->hdmaout->XferCpltCallback = JPEG_MDMAOutCpltCallback;
- /* Set the MDMA In error callback */
- hjpeg->hdmaout->XferErrorCallback = JPEG_MDMAErrorCallback;
- /* Set the MDMA Out Abort callback */
- hjpeg->hdmaout->XferAbortCallback = JPEG_MDMAOutAbortCallback;
-
- /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
- hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
-
- /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
- hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % outXfrSize);
-
- /* Start MDMA FIFO Out transfer */
- HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength, 1);
- /* Start DMA FIFO In transfer */
- HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength, 1);
-
- return HAL_OK;
- }
- /**
- * @brief Continue the current JPEG DMA process (encoding/decoding)
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
- */
- static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
- {
- /*End of header processing flag rises*/
- if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
- {
- /*Call Header parsing complete callback */
- HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
-
- /* Reset the ImageQuality */
- hjpeg->Conf.ImageQuality = 0;
- /* Note : the image quality is only available at the end of the decoding operation */
- /* at the current stage the calculated image quality is not correct so reset it */
-
- /*Call Info Ready callback */
- HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
-
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
-
- /* Clear header processing done flag */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
- }
-
- /*End of Conversion handling*/
- if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != RESET)
- {
- hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
-
- /*Stop Encoding/Decoding*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
-
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- /* Clear all flags */
- __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
-
-
- if(hjpeg->hdmain->State == HAL_MDMA_STATE_BUSY)
- {
- /* Stop the MDMA In Xfer*/
- HAL_MDMA_Abort_IT(hjpeg->hdmain);
- }
- if(hjpeg->hdmaout->State == HAL_MDMA_STATE_BUSY)
- {
- /* Stop the MDMA out Xfer*/
- HAL_MDMA_Abort_IT(hjpeg->hdmaout);
- }
- else
- {
- return JPEG_DMA_EndProcess(hjpeg);
- }
- }
-
- return JPEG_PROCESS_ONGOING;
- }
- /**
- * @brief Finalize the current JPEG DMA process (encoding/decoding)
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval JPEG_PROCESS_DONE
- */
- static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t tmpContext;
- hjpeg->JpegOutCount = hjpeg->OutDataLength - (hjpeg->hdmaout->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
-
- /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
- if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
- {
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
-
- /*Check if remaining data in the output FIFO*/
- if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0)
- {
- if(hjpeg->JpegOutCount > 0)
- {
- /*Output Buffer is not empty, call DecodedDataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
-
- /*Stop Encoding/Decoding*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
-
- tmpContext = hjpeg->Context;
- /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
- hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /*Call End of Encoding/Decoding callback */
- if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
- {
- HAL_JPEG_DecodeCpltCallback(hjpeg);
- }
- else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
- {
- HAL_JPEG_EncodeCpltCallback(hjpeg);
- }
- }
- else if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
- {
- JPEG_DMA_PollResidualData(hjpeg);
- return JPEG_PROCESS_DONE;
- }
-
- return JPEG_PROCESS_ONGOING;
- }
- /**
- * @brief Poll residual output data when DMA process (encoding/decoding)
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval None.
- */
- static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t tmpContext, count = JPEG_FIFO_SIZE, dataOut;
-
- while((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0) && (count > 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0))
- {
- count--;
-
- dataOut = hjpeg->Instance->DOR;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = dataOut & 0x000000FF;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1] = (dataOut & 0x0000FF00) >> 8;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2] = (dataOut & 0x00FF0000) >> 16;
- hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3] = (dataOut & 0xFF000000) >> 24;
- hjpeg->JpegOutCount += 4;
-
- if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
- {
- /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
- }
-
- if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
- {
- /*Stop Encoding/Decoding*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
-
- if(hjpeg->JpegOutCount > 0)
- {
- /*Output Buffer is not empty, call DecodedDataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
- hjpeg->JpegOutCount = 0;
- }
-
- tmpContext = hjpeg->Context;
- /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
- hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
-
- /* Process Unlocked */
- __HAL_UNLOCK(hjpeg);
-
- /* Change the JPEG state */
- hjpeg->State = HAL_JPEG_STATE_READY;
-
- /*Call End of Encoding/Decoding callback */
- if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
- {
- HAL_JPEG_DecodeCpltCallback(hjpeg);
- }
- else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
- {
- HAL_JPEG_EncodeCpltCallback(hjpeg);
- }
- }
- }
- /**
- * @brief DMA input transfer complete callback
- * @param hmdma: pointer to a DMA_HandleTypeDef structure.
- * @retval None
- */
- static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma)
- {
- uint32_t inXfrSize;
-
- JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((MDMA_HandleTypeDef*)hmdma)->Parent;
-
- /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
- {
-
- /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
- then MDMA In buffer size is 32 bytes
- else (MDMA In is triggred with JPEG In FIFO not full flag)
- then MDMA In buffer size is 4 bytes
- */
- inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
-
- hjpeg->JpegInCount = hjpeg->InDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
- /*Call HAL_JPEG_GetDataCallback to get new data */
- HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
-
-
- if(hjpeg->InDataLength >= inXfrSize)
- {
- /*JPEG Input DMA transfer data number must be multiple of MDMA buffer size
- as the destination is a 32 bits register */
- hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
-
- }
- else if(hjpeg->InDataLength > 0)
- {
- /* Transfer the remaining Data, must be multiple of source data size (byte) and destination data size (word) */
- if((hjpeg->InDataLength % 4) != 0)
- {
- hjpeg->InDataLength = ((hjpeg->InDataLength / 4) + 1) * 4;
- }
- }
-
- if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && (hjpeg->InDataLength > 0))
- {
- /* Start MDMA FIFO In transfer */
- HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength, 1);
- }
-
- /* JPEG Conversion still on going : Enable the JPEG IT */
- __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
- }
- }
- /**
- * @brief DMA output transfer complete callback
- * @param hmdma: pointer to a DMA_HandleTypeDef structure.
- * @retval None
- */
- static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma)
- {
- JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((MDMA_HandleTypeDef*)hmdma)->Parent;
-
-
- /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
- {
- if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0)
- {
- hjpeg->JpegOutCount = hjpeg->OutDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
-
- /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
- HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
-
- if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
- {
- /* Start MDMA FIFO Out transfer */
- HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength, 1);
- }
- }
-
- /* JPEG Conversion still on going : Enable the JPEG IT */
- __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
- }
- }
- /**
- * @brief DMA Transfer error callback
- * @param hmdma: pointer to a DMA_HandleTypeDef structure.
- * @retval None
- */
- static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma)
- {
- JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((MDMA_HandleTypeDef*)hmdma)->Parent;
-
- /*Stop Encoding/Decoding*/
- hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
-
- /* Disable All Interrupts */
- __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
-
- hjpeg->State= HAL_JPEG_STATE_READY;
- hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
- HAL_JPEG_ErrorCallback(hjpeg);
- }
- /**
- * @brief DMA output Abort callback
- * @param hmdma: pointer to a DMA_HandleTypeDef structure.
- * @retval None
- */
- static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma)
- {
- JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((MDMA_HandleTypeDef*)hmdma)->Parent;
-
- if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
- {
- JPEG_DMA_EndProcess(hjpeg);
- }
- }
- /**
- * @brief Calculate the decoded image quality (from 1 to 100)
- * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
- * the configuration information for JPEG module
- * @retval JPEG image quality from 1 to 100.
- */
- static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
- {
- uint32_t quality = 0;
- uint32_t quantRow, quantVal,scale, i, j;
- __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
-
- i = 0;
- while( i < JPEG_QUANT_TABLE_SIZE)
- {
- quantRow = *tableAddress;
- for(j=0; j<4; j++)
- {
- quantVal = (quantRow >> (8 * j)) & 0xFF;
- if(quantVal == 1)
- {
- /* if Quantization value = 1 then quality is 100%*/
- quality += 100;
- }
- else
- {
- /* Note that the quantization coefficients must be specified in the table in zigzag order */
- scale = (quantVal*100)/((uint32_t) JPEG_LUM_QuantTable[JPEG_ZIGZAG_ORDER[i+j]]);
-
- if(scale <= 100)
- {
- quality += (200 - scale)/2;
- }
- else
- {
- quality += 5000/scale;
- }
- }
- }
- i += 4;
- tableAddress ++;
- }
- return (quality/((uint32_t)64));
- }
- /**
- * @}
- */
- #endif /* HAL_JPEG_MODULE_ENABLED */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|