utils.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #define _DEFAULT_SOURCE
  22. #define _SVID_SOURCE // needed for MAP_ANONYMOUS
  23. #define _DARWIN_C_SOURCE // needed for MAP_ANON
  24. #include <inttypes.h>
  25. #include <math.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #if HAVE_MMAP
  29. #include <sys/mman.h>
  30. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  31. #define MAP_ANONYMOUS MAP_ANON
  32. #endif
  33. #endif
  34. #if HAVE_VIRTUALALLOC
  35. #define WIN32_LEAN_AND_MEAN
  36. #include <windows.h>
  37. #endif
  38. #include "libavutil/attributes.h"
  39. #include "libavutil/avassert.h"
  40. #include "libavutil/cpu.h"
  41. #include "libavutil/imgutils.h"
  42. #include "libavutil/intreadwrite.h"
  43. #include "libavutil/libm.h"
  44. #include "libavutil/mathematics.h"
  45. #include "libavutil/opt.h"
  46. #include "libavutil/pixdesc.h"
  47. #include "libavutil/slicethread.h"
  48. #include "libavutil/thread.h"
  49. #include "libavutil/aarch64/cpu.h"
  50. #include "libavutil/ppc/cpu.h"
  51. #include "libavutil/x86/asm.h"
  52. #include "libavutil/x86/cpu.h"
  53. #include "rgb2rgb.h"
  54. #include "swscale.h"
  55. #include "swscale_internal.h"
  56. static SwsVector *sws_getIdentityVec(void);
  57. static void sws_addVec(SwsVector *a, SwsVector *b);
  58. static void sws_shiftVec(SwsVector *a, int shift);
  59. static void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level);
  60. static void handle_formats(SwsContext *c);
  61. typedef struct FormatEntry {
  62. uint8_t is_supported_in :1;
  63. uint8_t is_supported_out :1;
  64. uint8_t is_supported_endianness :1;
  65. } FormatEntry;
  66. static const FormatEntry format_entries[] = {
  67. [AV_PIX_FMT_YUV420P] = { 1, 1 },
  68. [AV_PIX_FMT_YUYV422] = { 1, 1 },
  69. [AV_PIX_FMT_RGB24] = { 1, 1 },
  70. [AV_PIX_FMT_BGR24] = { 1, 1 },
  71. [AV_PIX_FMT_YUV422P] = { 1, 1 },
  72. [AV_PIX_FMT_YUV444P] = { 1, 1 },
  73. [AV_PIX_FMT_YUV410P] = { 1, 1 },
  74. [AV_PIX_FMT_YUV411P] = { 1, 1 },
  75. [AV_PIX_FMT_GRAY8] = { 1, 1 },
  76. [AV_PIX_FMT_MONOWHITE] = { 1, 1 },
  77. [AV_PIX_FMT_MONOBLACK] = { 1, 1 },
  78. [AV_PIX_FMT_PAL8] = { 1, 0 },
  79. [AV_PIX_FMT_YUVJ420P] = { 1, 1 },
  80. [AV_PIX_FMT_YUVJ411P] = { 1, 1 },
  81. [AV_PIX_FMT_YUVJ422P] = { 1, 1 },
  82. [AV_PIX_FMT_YUVJ444P] = { 1, 1 },
  83. [AV_PIX_FMT_YVYU422] = { 1, 1 },
  84. [AV_PIX_FMT_UYVY422] = { 1, 1 },
  85. [AV_PIX_FMT_UYYVYY411] = { 0, 0 },
  86. [AV_PIX_FMT_BGR8] = { 1, 1 },
  87. [AV_PIX_FMT_BGR4] = { 0, 1 },
  88. [AV_PIX_FMT_BGR4_BYTE] = { 1, 1 },
  89. [AV_PIX_FMT_RGB8] = { 1, 1 },
  90. [AV_PIX_FMT_RGB4] = { 0, 1 },
  91. [AV_PIX_FMT_RGB4_BYTE] = { 1, 1 },
  92. [AV_PIX_FMT_NV12] = { 1, 1 },
  93. [AV_PIX_FMT_NV21] = { 1, 1 },
  94. [AV_PIX_FMT_ARGB] = { 1, 1 },
  95. [AV_PIX_FMT_RGBA] = { 1, 1 },
  96. [AV_PIX_FMT_ABGR] = { 1, 1 },
  97. [AV_PIX_FMT_BGRA] = { 1, 1 },
  98. [AV_PIX_FMT_0RGB] = { 1, 1 },
  99. [AV_PIX_FMT_RGB0] = { 1, 1 },
  100. [AV_PIX_FMT_0BGR] = { 1, 1 },
  101. [AV_PIX_FMT_BGR0] = { 1, 1 },
  102. [AV_PIX_FMT_GRAY9BE] = { 1, 1 },
  103. [AV_PIX_FMT_GRAY9LE] = { 1, 1 },
  104. [AV_PIX_FMT_GRAY10BE] = { 1, 1 },
  105. [AV_PIX_FMT_GRAY10LE] = { 1, 1 },
  106. [AV_PIX_FMT_GRAY12BE] = { 1, 1 },
  107. [AV_PIX_FMT_GRAY12LE] = { 1, 1 },
  108. [AV_PIX_FMT_GRAY14BE] = { 1, 1 },
  109. [AV_PIX_FMT_GRAY14LE] = { 1, 1 },
  110. [AV_PIX_FMT_GRAY16BE] = { 1, 1 },
  111. [AV_PIX_FMT_GRAY16LE] = { 1, 1 },
  112. [AV_PIX_FMT_YUV440P] = { 1, 1 },
  113. [AV_PIX_FMT_YUVJ440P] = { 1, 1 },
  114. [AV_PIX_FMT_YUV440P10LE] = { 1, 1 },
  115. [AV_PIX_FMT_YUV440P10BE] = { 1, 1 },
  116. [AV_PIX_FMT_YUV440P12LE] = { 1, 1 },
  117. [AV_PIX_FMT_YUV440P12BE] = { 1, 1 },
  118. [AV_PIX_FMT_YUVA420P] = { 1, 1 },
  119. [AV_PIX_FMT_YUVA422P] = { 1, 1 },
  120. [AV_PIX_FMT_YUVA444P] = { 1, 1 },
  121. [AV_PIX_FMT_YUVA420P9BE] = { 1, 1 },
  122. [AV_PIX_FMT_YUVA420P9LE] = { 1, 1 },
  123. [AV_PIX_FMT_YUVA422P9BE] = { 1, 1 },
  124. [AV_PIX_FMT_YUVA422P9LE] = { 1, 1 },
  125. [AV_PIX_FMT_YUVA444P9BE] = { 1, 1 },
  126. [AV_PIX_FMT_YUVA444P9LE] = { 1, 1 },
  127. [AV_PIX_FMT_YUVA420P10BE]= { 1, 1 },
  128. [AV_PIX_FMT_YUVA420P10LE]= { 1, 1 },
  129. [AV_PIX_FMT_YUVA422P10BE]= { 1, 1 },
  130. [AV_PIX_FMT_YUVA422P10LE]= { 1, 1 },
  131. [AV_PIX_FMT_YUVA444P10BE]= { 1, 1 },
  132. [AV_PIX_FMT_YUVA444P10LE]= { 1, 1 },
  133. [AV_PIX_FMT_YUVA420P16BE]= { 1, 1 },
  134. [AV_PIX_FMT_YUVA420P16LE]= { 1, 1 },
  135. [AV_PIX_FMT_YUVA422P16BE]= { 1, 1 },
  136. [AV_PIX_FMT_YUVA422P16LE]= { 1, 1 },
  137. [AV_PIX_FMT_YUVA444P16BE]= { 1, 1 },
  138. [AV_PIX_FMT_YUVA444P16LE]= { 1, 1 },
  139. [AV_PIX_FMT_RGB48BE] = { 1, 1 },
  140. [AV_PIX_FMT_RGB48LE] = { 1, 1 },
  141. [AV_PIX_FMT_RGBA64BE] = { 1, 1, 1 },
  142. [AV_PIX_FMT_RGBA64LE] = { 1, 1, 1 },
  143. [AV_PIX_FMT_RGB565BE] = { 1, 1 },
  144. [AV_PIX_FMT_RGB565LE] = { 1, 1 },
  145. [AV_PIX_FMT_RGB555BE] = { 1, 1 },
  146. [AV_PIX_FMT_RGB555LE] = { 1, 1 },
  147. [AV_PIX_FMT_BGR565BE] = { 1, 1 },
  148. [AV_PIX_FMT_BGR565LE] = { 1, 1 },
  149. [AV_PIX_FMT_BGR555BE] = { 1, 1 },
  150. [AV_PIX_FMT_BGR555LE] = { 1, 1 },
  151. [AV_PIX_FMT_YUV420P16LE] = { 1, 1 },
  152. [AV_PIX_FMT_YUV420P16BE] = { 1, 1 },
  153. [AV_PIX_FMT_YUV422P16LE] = { 1, 1 },
  154. [AV_PIX_FMT_YUV422P16BE] = { 1, 1 },
  155. [AV_PIX_FMT_YUV444P16LE] = { 1, 1 },
  156. [AV_PIX_FMT_YUV444P16BE] = { 1, 1 },
  157. [AV_PIX_FMT_RGB444LE] = { 1, 1 },
  158. [AV_PIX_FMT_RGB444BE] = { 1, 1 },
  159. [AV_PIX_FMT_BGR444LE] = { 1, 1 },
  160. [AV_PIX_FMT_BGR444BE] = { 1, 1 },
  161. [AV_PIX_FMT_YA8] = { 1, 1 },
  162. [AV_PIX_FMT_YA16BE] = { 1, 1 },
  163. [AV_PIX_FMT_YA16LE] = { 1, 1 },
  164. [AV_PIX_FMT_BGR48BE] = { 1, 1 },
  165. [AV_PIX_FMT_BGR48LE] = { 1, 1 },
  166. [AV_PIX_FMT_BGRA64BE] = { 1, 1, 1 },
  167. [AV_PIX_FMT_BGRA64LE] = { 1, 1, 1 },
  168. [AV_PIX_FMT_YUV420P9BE] = { 1, 1 },
  169. [AV_PIX_FMT_YUV420P9LE] = { 1, 1 },
  170. [AV_PIX_FMT_YUV420P10BE] = { 1, 1 },
  171. [AV_PIX_FMT_YUV420P10LE] = { 1, 1 },
  172. [AV_PIX_FMT_YUV420P12BE] = { 1, 1 },
  173. [AV_PIX_FMT_YUV420P12LE] = { 1, 1 },
  174. [AV_PIX_FMT_YUV420P14BE] = { 1, 1 },
  175. [AV_PIX_FMT_YUV420P14LE] = { 1, 1 },
  176. [AV_PIX_FMT_YUV422P9BE] = { 1, 1 },
  177. [AV_PIX_FMT_YUV422P9LE] = { 1, 1 },
  178. [AV_PIX_FMT_YUV422P10BE] = { 1, 1 },
  179. [AV_PIX_FMT_YUV422P10LE] = { 1, 1 },
  180. [AV_PIX_FMT_YUV422P12BE] = { 1, 1 },
  181. [AV_PIX_FMT_YUV422P12LE] = { 1, 1 },
  182. [AV_PIX_FMT_YUV422P14BE] = { 1, 1 },
  183. [AV_PIX_FMT_YUV422P14LE] = { 1, 1 },
  184. [AV_PIX_FMT_YUV444P9BE] = { 1, 1 },
  185. [AV_PIX_FMT_YUV444P9LE] = { 1, 1 },
  186. [AV_PIX_FMT_YUV444P10BE] = { 1, 1 },
  187. [AV_PIX_FMT_YUV444P10LE] = { 1, 1 },
  188. [AV_PIX_FMT_YUV444P12BE] = { 1, 1 },
  189. [AV_PIX_FMT_YUV444P12LE] = { 1, 1 },
  190. [AV_PIX_FMT_YUV444P14BE] = { 1, 1 },
  191. [AV_PIX_FMT_YUV444P14LE] = { 1, 1 },
  192. [AV_PIX_FMT_GBRP] = { 1, 1 },
  193. [AV_PIX_FMT_GBRP9LE] = { 1, 1 },
  194. [AV_PIX_FMT_GBRP9BE] = { 1, 1 },
  195. [AV_PIX_FMT_GBRP10LE] = { 1, 1 },
  196. [AV_PIX_FMT_GBRP10BE] = { 1, 1 },
  197. [AV_PIX_FMT_GBRAP10LE] = { 1, 1 },
  198. [AV_PIX_FMT_GBRAP10BE] = { 1, 1 },
  199. [AV_PIX_FMT_GBRP12LE] = { 1, 1 },
  200. [AV_PIX_FMT_GBRP12BE] = { 1, 1 },
  201. [AV_PIX_FMT_GBRAP12LE] = { 1, 1 },
  202. [AV_PIX_FMT_GBRAP12BE] = { 1, 1 },
  203. [AV_PIX_FMT_GBRP14LE] = { 1, 1 },
  204. [AV_PIX_FMT_GBRP14BE] = { 1, 1 },
  205. [AV_PIX_FMT_GBRP16LE] = { 1, 1 },
  206. [AV_PIX_FMT_GBRP16BE] = { 1, 1 },
  207. [AV_PIX_FMT_GBRPF32LE] = { 1, 1 },
  208. [AV_PIX_FMT_GBRPF32BE] = { 1, 1 },
  209. [AV_PIX_FMT_GBRAPF32LE] = { 1, 1 },
  210. [AV_PIX_FMT_GBRAPF32BE] = { 1, 1 },
  211. [AV_PIX_FMT_GBRAP] = { 1, 1 },
  212. [AV_PIX_FMT_GBRAP16LE] = { 1, 1 },
  213. [AV_PIX_FMT_GBRAP16BE] = { 1, 1 },
  214. [AV_PIX_FMT_BAYER_BGGR8] = { 1, 0 },
  215. [AV_PIX_FMT_BAYER_RGGB8] = { 1, 0 },
  216. [AV_PIX_FMT_BAYER_GBRG8] = { 1, 0 },
  217. [AV_PIX_FMT_BAYER_GRBG8] = { 1, 0 },
  218. [AV_PIX_FMT_BAYER_BGGR16LE] = { 1, 0 },
  219. [AV_PIX_FMT_BAYER_BGGR16BE] = { 1, 0 },
  220. [AV_PIX_FMT_BAYER_RGGB16LE] = { 1, 0 },
  221. [AV_PIX_FMT_BAYER_RGGB16BE] = { 1, 0 },
  222. [AV_PIX_FMT_BAYER_GBRG16LE] = { 1, 0 },
  223. [AV_PIX_FMT_BAYER_GBRG16BE] = { 1, 0 },
  224. [AV_PIX_FMT_BAYER_GRBG16LE] = { 1, 0 },
  225. [AV_PIX_FMT_BAYER_GRBG16BE] = { 1, 0 },
  226. [AV_PIX_FMT_XYZ12BE] = { 1, 1, 1 },
  227. [AV_PIX_FMT_XYZ12LE] = { 1, 1, 1 },
  228. [AV_PIX_FMT_AYUV64LE] = { 1, 1},
  229. [AV_PIX_FMT_P010LE] = { 1, 1 },
  230. [AV_PIX_FMT_P010BE] = { 1, 1 },
  231. [AV_PIX_FMT_P016LE] = { 1, 1 },
  232. [AV_PIX_FMT_P016BE] = { 1, 1 },
  233. [AV_PIX_FMT_GRAYF32LE] = { 1, 1 },
  234. [AV_PIX_FMT_GRAYF32BE] = { 1, 1 },
  235. [AV_PIX_FMT_YUVA422P12BE] = { 1, 1 },
  236. [AV_PIX_FMT_YUVA422P12LE] = { 1, 1 },
  237. [AV_PIX_FMT_YUVA444P12BE] = { 1, 1 },
  238. [AV_PIX_FMT_YUVA444P12LE] = { 1, 1 },
  239. [AV_PIX_FMT_NV24] = { 1, 1 },
  240. [AV_PIX_FMT_NV42] = { 1, 1 },
  241. [AV_PIX_FMT_Y210LE] = { 1, 0 },
  242. [AV_PIX_FMT_X2RGB10LE] = { 1, 1 },
  243. [AV_PIX_FMT_X2BGR10LE] = { 1, 1 },
  244. [AV_PIX_FMT_P210BE] = { 1, 1 },
  245. [AV_PIX_FMT_P210LE] = { 1, 1 },
  246. [AV_PIX_FMT_P410BE] = { 1, 1 },
  247. [AV_PIX_FMT_P410LE] = { 1, 1 },
  248. [AV_PIX_FMT_P216BE] = { 1, 1 },
  249. [AV_PIX_FMT_P216LE] = { 1, 1 },
  250. [AV_PIX_FMT_P416BE] = { 1, 1 },
  251. [AV_PIX_FMT_P416LE] = { 1, 1 },
  252. };
  253. int ff_shuffle_filter_coefficients(SwsContext *c, int *filterPos,
  254. int filterSize, int16_t *filter,
  255. int dstW)
  256. {
  257. #if ARCH_X86_64
  258. int i, j, k;
  259. int cpu_flags = av_get_cpu_flags();
  260. // avx2 hscale filter processes 16 pixel blocks.
  261. if (!filter || dstW % 16 != 0)
  262. return 0;
  263. if (EXTERNAL_AVX2_FAST(cpu_flags) && !(cpu_flags & AV_CPU_FLAG_SLOW_GATHER)) {
  264. if ((c->srcBpc == 8) && (c->dstBpc <= 14)) {
  265. int16_t *filterCopy = NULL;
  266. if (filterSize > 4) {
  267. if (!FF_ALLOC_TYPED_ARRAY(filterCopy, dstW * filterSize))
  268. return AVERROR(ENOMEM);
  269. memcpy(filterCopy, filter, dstW * filterSize * sizeof(int16_t));
  270. }
  271. // Do not swap filterPos for pixels which won't be processed by
  272. // the main loop.
  273. for (i = 0; i + 8 <= dstW; i += 8) {
  274. FFSWAP(int, filterPos[i + 2], filterPos[i + 4]);
  275. FFSWAP(int, filterPos[i + 3], filterPos[i + 5]);
  276. }
  277. if (filterSize > 4) {
  278. // 16 pixels are processed at a time.
  279. for (i = 0; i + 16 <= dstW; i += 16) {
  280. // 4 filter coeffs are processed at a time.
  281. for (k = 0; k + 4 <= filterSize; k += 4) {
  282. for (j = 0; j < 16; ++j) {
  283. int from = (i + j) * filterSize + k;
  284. int to = i * filterSize + j * 4 + k * 16;
  285. memcpy(&filter[to], &filterCopy[from], 4 * sizeof(int16_t));
  286. }
  287. }
  288. }
  289. }
  290. av_free(filterCopy);
  291. }
  292. }
  293. #endif
  294. return 0;
  295. }
  296. int sws_isSupportedInput(enum AVPixelFormat pix_fmt)
  297. {
  298. return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
  299. format_entries[pix_fmt].is_supported_in : 0;
  300. }
  301. int sws_isSupportedOutput(enum AVPixelFormat pix_fmt)
  302. {
  303. return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
  304. format_entries[pix_fmt].is_supported_out : 0;
  305. }
  306. int sws_isSupportedEndiannessConversion(enum AVPixelFormat pix_fmt)
  307. {
  308. return (unsigned)pix_fmt < FF_ARRAY_ELEMS(format_entries) ?
  309. format_entries[pix_fmt].is_supported_endianness : 0;
  310. }
  311. static double getSplineCoeff(double a, double b, double c, double d,
  312. double dist)
  313. {
  314. if (dist <= 1.0)
  315. return ((d * dist + c) * dist + b) * dist + a;
  316. else
  317. return getSplineCoeff(0.0,
  318. b + 2.0 * c + 3.0 * d,
  319. c + 3.0 * d,
  320. -b - 3.0 * c - 6.0 * d,
  321. dist - 1.0);
  322. }
  323. static av_cold int get_local_pos(SwsContext *s, int chr_subsample, int pos, int dir)
  324. {
  325. if (pos == -1 || pos <= -513) {
  326. pos = (128 << chr_subsample) - 128;
  327. }
  328. pos += 128; // relative to ideal left edge
  329. return pos >> chr_subsample;
  330. }
  331. typedef struct {
  332. int flag; ///< flag associated to the algorithm
  333. const char *description; ///< human-readable description
  334. int size_factor; ///< size factor used when initing the filters
  335. } ScaleAlgorithm;
  336. static const ScaleAlgorithm scale_algorithms[] = {
  337. { SWS_AREA, "area averaging", 1 /* downscale only, for upscale it is bilinear */ },
  338. { SWS_BICUBIC, "bicubic", 4 },
  339. { SWS_BICUBLIN, "luma bicubic / chroma bilinear", -1 },
  340. { SWS_BILINEAR, "bilinear", 2 },
  341. { SWS_FAST_BILINEAR, "fast bilinear", -1 },
  342. { SWS_GAUSS, "Gaussian", 8 /* infinite ;) */ },
  343. { SWS_LANCZOS, "Lanczos", -1 /* custom */ },
  344. { SWS_POINT, "nearest neighbor / point", -1 },
  345. { SWS_SINC, "sinc", 20 /* infinite ;) */ },
  346. { SWS_SPLINE, "bicubic spline", 20 /* infinite :)*/ },
  347. { SWS_X, "experimental", 8 },
  348. };
  349. static av_cold int initFilter(int16_t **outFilter, int32_t **filterPos,
  350. int *outFilterSize, int xInc, int srcW,
  351. int dstW, int filterAlign, int one,
  352. int flags, int cpu_flags,
  353. SwsVector *srcFilter, SwsVector *dstFilter,
  354. double param[2], int srcPos, int dstPos)
  355. {
  356. int i;
  357. int filterSize;
  358. int filter2Size;
  359. int minFilterSize;
  360. int64_t *filter = NULL;
  361. int64_t *filter2 = NULL;
  362. const int64_t fone = 1LL << (54 - FFMIN(av_log2(srcW/dstW), 8));
  363. int ret = -1;
  364. emms_c(); // FIXME should not be required but IS (even for non-MMX versions)
  365. // NOTE: the +3 is for the MMX(+1) / SSE(+3) scaler which reads over the end
  366. if (!FF_ALLOC_TYPED_ARRAY(*filterPos, dstW + 3))
  367. goto nomem;
  368. if (FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) { // unscaled
  369. int i;
  370. filterSize = 1;
  371. if (!FF_ALLOCZ_TYPED_ARRAY(filter, dstW * filterSize))
  372. goto nomem;
  373. for (i = 0; i < dstW; i++) {
  374. filter[i * filterSize] = fone;
  375. (*filterPos)[i] = i;
  376. }
  377. } else if (flags & SWS_POINT) { // lame looking point sampling mode
  378. int i;
  379. int64_t xDstInSrc;
  380. filterSize = 1;
  381. if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
  382. goto nomem;
  383. xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
  384. for (i = 0; i < dstW; i++) {
  385. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  386. (*filterPos)[i] = xx;
  387. filter[i] = fone;
  388. xDstInSrc += xInc;
  389. }
  390. } else if ((xInc <= (1 << 16) && (flags & SWS_AREA)) ||
  391. (flags & SWS_FAST_BILINEAR)) { // bilinear upscale
  392. int i;
  393. int64_t xDstInSrc;
  394. filterSize = 2;
  395. if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
  396. goto nomem;
  397. xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
  398. for (i = 0; i < dstW; i++) {
  399. int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
  400. int j;
  401. (*filterPos)[i] = xx;
  402. // bilinear upscale / linear interpolate / area averaging
  403. for (j = 0; j < filterSize; j++) {
  404. int64_t coeff = fone - FFABS((int64_t)xx * (1 << 16) - xDstInSrc) * (fone >> 16);
  405. if (coeff < 0)
  406. coeff = 0;
  407. filter[i * filterSize + j] = coeff;
  408. xx++;
  409. }
  410. xDstInSrc += xInc;
  411. }
  412. } else {
  413. int64_t xDstInSrc;
  414. int sizeFactor = -1;
  415. for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
  416. if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
  417. sizeFactor = scale_algorithms[i].size_factor;
  418. break;
  419. }
  420. }
  421. if (flags & SWS_LANCZOS)
  422. sizeFactor = param[0] != SWS_PARAM_DEFAULT ? ceil(2 * param[0]) : 6;
  423. av_assert0(sizeFactor > 0);
  424. if (xInc <= 1 << 16)
  425. filterSize = 1 + sizeFactor; // upscale
  426. else
  427. filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
  428. filterSize = FFMIN(filterSize, srcW - 2);
  429. filterSize = FFMAX(filterSize, 1);
  430. if (!FF_ALLOC_TYPED_ARRAY(filter, dstW * filterSize))
  431. goto nomem;
  432. xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
  433. for (i = 0; i < dstW; i++) {
  434. int xx = (xDstInSrc - (filterSize - 2) * (1LL<<16)) / (1 << 17);
  435. int j;
  436. (*filterPos)[i] = xx;
  437. for (j = 0; j < filterSize; j++) {
  438. int64_t d = (FFABS(((int64_t)xx * (1 << 17)) - xDstInSrc)) << 13;
  439. double floatd;
  440. int64_t coeff;
  441. if (xInc > 1 << 16)
  442. d = d * dstW / srcW;
  443. floatd = d * (1.0 / (1 << 30));
  444. if (flags & SWS_BICUBIC) {
  445. int64_t B = (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1 << 24);
  446. int64_t C = (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1 << 24);
  447. if (d >= 1LL << 31) {
  448. coeff = 0.0;
  449. } else {
  450. int64_t dd = (d * d) >> 30;
  451. int64_t ddd = (dd * d) >> 30;
  452. if (d < 1LL << 30)
  453. coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
  454. (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
  455. (6 * (1 << 24) - 2 * B) * (1 << 30);
  456. else
  457. coeff = (-B - 6 * C) * ddd +
  458. (6 * B + 30 * C) * dd +
  459. (-12 * B - 48 * C) * d +
  460. (8 * B + 24 * C) * (1 << 30);
  461. }
  462. coeff /= (1LL<<54)/fone;
  463. } else if (flags & SWS_X) {
  464. double A = param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  465. double c;
  466. if (floatd < 1.0)
  467. c = cos(floatd * M_PI);
  468. else
  469. c = -1.0;
  470. if (c < 0.0)
  471. c = -pow(-c, A);
  472. else
  473. c = pow(c, A);
  474. coeff = (c * 0.5 + 0.5) * fone;
  475. } else if (flags & SWS_AREA) {
  476. int64_t d2 = d - (1 << 29);
  477. if (d2 * xInc < -(1LL << (29 + 16)))
  478. coeff = 1.0 * (1LL << (30 + 16));
  479. else if (d2 * xInc < (1LL << (29 + 16)))
  480. coeff = -d2 * xInc + (1LL << (29 + 16));
  481. else
  482. coeff = 0.0;
  483. coeff *= fone >> (30 + 16);
  484. } else if (flags & SWS_GAUSS) {
  485. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  486. coeff = exp2(-p * floatd * floatd) * fone;
  487. } else if (flags & SWS_SINC) {
  488. coeff = (d ? sin(floatd * M_PI) / (floatd * M_PI) : 1.0) * fone;
  489. } else if (flags & SWS_LANCZOS) {
  490. double p = param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  491. coeff = (d ? sin(floatd * M_PI) * sin(floatd * M_PI / p) /
  492. (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
  493. if (floatd > p)
  494. coeff = 0;
  495. } else if (flags & SWS_BILINEAR) {
  496. coeff = (1 << 30) - d;
  497. if (coeff < 0)
  498. coeff = 0;
  499. coeff *= fone >> 30;
  500. } else if (flags & SWS_SPLINE) {
  501. double p = -2.196152422706632;
  502. coeff = getSplineCoeff(1.0, 0.0, p, -p - 1.0, floatd) * fone;
  503. } else {
  504. av_assert0(0);
  505. }
  506. filter[i * filterSize + j] = coeff;
  507. xx++;
  508. }
  509. xDstInSrc += 2LL * xInc;
  510. }
  511. }
  512. /* apply src & dst Filter to filter -> filter2
  513. * av_free(filter);
  514. */
  515. av_assert0(filterSize > 0);
  516. filter2Size = filterSize;
  517. if (srcFilter)
  518. filter2Size += srcFilter->length - 1;
  519. if (dstFilter)
  520. filter2Size += dstFilter->length - 1;
  521. av_assert0(filter2Size > 0);
  522. if (!FF_ALLOCZ_TYPED_ARRAY(filter2, dstW * filter2Size))
  523. goto nomem;
  524. for (i = 0; i < dstW; i++) {
  525. int j, k;
  526. if (srcFilter) {
  527. for (k = 0; k < srcFilter->length; k++) {
  528. for (j = 0; j < filterSize; j++)
  529. filter2[i * filter2Size + k + j] +=
  530. srcFilter->coeff[k] * filter[i * filterSize + j];
  531. }
  532. } else {
  533. for (j = 0; j < filterSize; j++)
  534. filter2[i * filter2Size + j] = filter[i * filterSize + j];
  535. }
  536. // FIXME dstFilter
  537. (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
  538. }
  539. av_freep(&filter);
  540. /* try to reduce the filter-size (step1 find size and shift left) */
  541. // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
  542. minFilterSize = 0;
  543. for (i = dstW - 1; i >= 0; i--) {
  544. int min = filter2Size;
  545. int j;
  546. int64_t cutOff = 0.0;
  547. /* get rid of near zero elements on the left by shifting left */
  548. for (j = 0; j < filter2Size; j++) {
  549. int k;
  550. cutOff += FFABS(filter2[i * filter2Size]);
  551. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  552. break;
  553. /* preserve monotonicity because the core can't handle the
  554. * filter otherwise */
  555. if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
  556. break;
  557. // move filter coefficients left
  558. for (k = 1; k < filter2Size; k++)
  559. filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
  560. filter2[i * filter2Size + k - 1] = 0;
  561. (*filterPos)[i]++;
  562. }
  563. cutOff = 0;
  564. /* count near zeros on the right */
  565. for (j = filter2Size - 1; j > 0; j--) {
  566. cutOff += FFABS(filter2[i * filter2Size + j]);
  567. if (cutOff > SWS_MAX_REDUCE_CUTOFF * fone)
  568. break;
  569. min--;
  570. }
  571. if (min > minFilterSize)
  572. minFilterSize = min;
  573. }
  574. if (PPC_ALTIVEC(cpu_flags)) {
  575. // we can handle the special case 4, so we don't want to go the full 8
  576. if (minFilterSize < 5)
  577. filterAlign = 4;
  578. /* We really don't want to waste our time doing useless computation, so
  579. * fall back on the scalar C code for very small filters.
  580. * Vectorizing is worth it only if you have a decent-sized vector. */
  581. if (minFilterSize < 3)
  582. filterAlign = 1;
  583. }
  584. if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX) {
  585. // special case for unscaled vertical filtering
  586. if (minFilterSize == 1 && filterAlign == 2)
  587. filterAlign = 1;
  588. }
  589. av_assert0(minFilterSize > 0);
  590. filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
  591. av_assert0(filterSize > 0);
  592. filter = av_malloc_array(dstW, filterSize * sizeof(*filter));
  593. if (!filter)
  594. goto nomem;
  595. if (filterSize >= MAX_FILTER_SIZE * 16 /
  596. ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16)) {
  597. ret = RETCODE_USE_CASCADE;
  598. goto fail;
  599. }
  600. *outFilterSize = filterSize;
  601. if (flags & SWS_PRINT_INFO)
  602. av_log(NULL, AV_LOG_VERBOSE,
  603. "SwScaler: reducing / aligning filtersize %d -> %d\n",
  604. filter2Size, filterSize);
  605. /* try to reduce the filter-size (step2 reduce it) */
  606. for (i = 0; i < dstW; i++) {
  607. int j;
  608. for (j = 0; j < filterSize; j++) {
  609. if (j >= filter2Size)
  610. filter[i * filterSize + j] = 0;
  611. else
  612. filter[i * filterSize + j] = filter2[i * filter2Size + j];
  613. if ((flags & SWS_BITEXACT) && j >= minFilterSize)
  614. filter[i * filterSize + j] = 0;
  615. }
  616. }
  617. // FIXME try to align filterPos if possible
  618. // fix borders
  619. for (i = 0; i < dstW; i++) {
  620. int j;
  621. if ((*filterPos)[i] < 0) {
  622. // move filter coefficients left to compensate for filterPos
  623. for (j = 1; j < filterSize; j++) {
  624. int left = FFMAX(j + (*filterPos)[i], 0);
  625. filter[i * filterSize + left] += filter[i * filterSize + j];
  626. filter[i * filterSize + j] = 0;
  627. }
  628. (*filterPos)[i]= 0;
  629. }
  630. if ((*filterPos)[i] + filterSize > srcW) {
  631. int shift = (*filterPos)[i] + FFMIN(filterSize - srcW, 0);
  632. int64_t acc = 0;
  633. for (j = filterSize - 1; j >= 0; j--) {
  634. if ((*filterPos)[i] + j >= srcW) {
  635. acc += filter[i * filterSize + j];
  636. filter[i * filterSize + j] = 0;
  637. }
  638. }
  639. for (j = filterSize - 1; j >= 0; j--) {
  640. if (j < shift) {
  641. filter[i * filterSize + j] = 0;
  642. } else {
  643. filter[i * filterSize + j] = filter[i * filterSize + j - shift];
  644. }
  645. }
  646. (*filterPos)[i]-= shift;
  647. filter[i * filterSize + srcW - 1 - (*filterPos)[i]] += acc;
  648. }
  649. av_assert0((*filterPos)[i] >= 0);
  650. av_assert0((*filterPos)[i] < srcW);
  651. if ((*filterPos)[i] + filterSize > srcW) {
  652. for (j = 0; j < filterSize; j++) {
  653. av_assert0((*filterPos)[i] + j < srcW || !filter[i * filterSize + j]);
  654. }
  655. }
  656. }
  657. // Note the +1 is for the MMX scaler which reads over the end
  658. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  659. if (!FF_ALLOCZ_TYPED_ARRAY(*outFilter, *outFilterSize * (dstW + 3)))
  660. goto nomem;
  661. /* normalize & store in outFilter */
  662. for (i = 0; i < dstW; i++) {
  663. int j;
  664. int64_t error = 0;
  665. int64_t sum = 0;
  666. for (j = 0; j < filterSize; j++) {
  667. sum += filter[i * filterSize + j];
  668. }
  669. sum = (sum + one / 2) / one;
  670. if (!sum) {
  671. av_log(NULL, AV_LOG_WARNING, "SwScaler: zero vector in scaling\n");
  672. sum = 1;
  673. }
  674. for (j = 0; j < *outFilterSize; j++) {
  675. int64_t v = filter[i * filterSize + j] + error;
  676. int intV = ROUNDED_DIV(v, sum);
  677. (*outFilter)[i * (*outFilterSize) + j] = intV;
  678. error = v - intV * sum;
  679. }
  680. }
  681. (*filterPos)[dstW + 0] =
  682. (*filterPos)[dstW + 1] =
  683. (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1]; /* the MMX/SSE scaler will
  684. * read over the end */
  685. for (i = 0; i < *outFilterSize; i++) {
  686. int k = (dstW - 1) * (*outFilterSize) + i;
  687. (*outFilter)[k + 1 * (*outFilterSize)] =
  688. (*outFilter)[k + 2 * (*outFilterSize)] =
  689. (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
  690. }
  691. ret = 0;
  692. goto done;
  693. nomem:
  694. ret = AVERROR(ENOMEM);
  695. fail:
  696. if(ret < 0)
  697. av_log(NULL, ret == RETCODE_USE_CASCADE ? AV_LOG_DEBUG : AV_LOG_ERROR, "sws: initFilter failed\n");
  698. done:
  699. av_free(filter);
  700. av_free(filter2);
  701. return ret;
  702. }
  703. static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
  704. {
  705. int64_t W, V, Z, Cy, Cu, Cv;
  706. int64_t vr = table[0];
  707. int64_t ub = table[1];
  708. int64_t ug = -table[2];
  709. int64_t vg = -table[3];
  710. int64_t ONE = 65536;
  711. int64_t cy = ONE;
  712. uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
  713. int i;
  714. static const int8_t map[] = {
  715. BY_IDX, GY_IDX, -1 , BY_IDX, BY_IDX, GY_IDX, -1 , BY_IDX,
  716. RY_IDX, -1 , GY_IDX, RY_IDX, RY_IDX, -1 , GY_IDX, RY_IDX,
  717. RY_IDX, GY_IDX, -1 , RY_IDX, RY_IDX, GY_IDX, -1 , RY_IDX,
  718. BY_IDX, -1 , GY_IDX, BY_IDX, BY_IDX, -1 , GY_IDX, BY_IDX,
  719. BU_IDX, GU_IDX, -1 , BU_IDX, BU_IDX, GU_IDX, -1 , BU_IDX,
  720. RU_IDX, -1 , GU_IDX, RU_IDX, RU_IDX, -1 , GU_IDX, RU_IDX,
  721. RU_IDX, GU_IDX, -1 , RU_IDX, RU_IDX, GU_IDX, -1 , RU_IDX,
  722. BU_IDX, -1 , GU_IDX, BU_IDX, BU_IDX, -1 , GU_IDX, BU_IDX,
  723. BV_IDX, GV_IDX, -1 , BV_IDX, BV_IDX, GV_IDX, -1 , BV_IDX,
  724. RV_IDX, -1 , GV_IDX, RV_IDX, RV_IDX, -1 , GV_IDX, RV_IDX,
  725. RV_IDX, GV_IDX, -1 , RV_IDX, RV_IDX, GV_IDX, -1 , RV_IDX,
  726. BV_IDX, -1 , GV_IDX, BV_IDX, BV_IDX, -1 , GV_IDX, BV_IDX,
  727. RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
  728. BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
  729. GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 ,
  730. -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX, -1 , GY_IDX,
  731. RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
  732. BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
  733. GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 ,
  734. -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX, -1 , GU_IDX,
  735. RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
  736. BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
  737. GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 ,
  738. -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, -1 , GV_IDX, //23
  739. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //24
  740. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //25
  741. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //26
  742. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //27
  743. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //28
  744. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //29
  745. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //30
  746. -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , //31
  747. BY_IDX, GY_IDX, RY_IDX, -1 , -1 , -1 , -1 , -1 , //32
  748. BU_IDX, GU_IDX, RU_IDX, -1 , -1 , -1 , -1 , -1 , //33
  749. BV_IDX, GV_IDX, RV_IDX, -1 , -1 , -1 , -1 , -1 , //34
  750. };
  751. dstRange = 0; //FIXME range = 1 is handled elsewhere
  752. if (!dstRange) {
  753. cy = cy * 255 / 219;
  754. } else {
  755. vr = vr * 224 / 255;
  756. ub = ub * 224 / 255;
  757. ug = ug * 224 / 255;
  758. vg = vg * 224 / 255;
  759. }
  760. W = ROUNDED_DIV(ONE*ONE*ug, ub);
  761. V = ROUNDED_DIV(ONE*ONE*vg, vr);
  762. Z = ONE*ONE-W-V;
  763. Cy = ROUNDED_DIV(cy*Z, ONE);
  764. Cu = ROUNDED_DIV(ub*Z, ONE);
  765. Cv = ROUNDED_DIV(vr*Z, ONE);
  766. c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cy);
  767. c->input_rgb2yuv_table[GY_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cy);
  768. c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cy);
  769. c->input_rgb2yuv_table[RU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V , Cu);
  770. c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cu);
  771. c->input_rgb2yuv_table[BU_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W) , Cu);
  772. c->input_rgb2yuv_table[RV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z) , Cv);
  773. c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE , Cv);
  774. c->input_rgb2yuv_table[BV_IDX] = ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W , Cv);
  775. if(/*!dstRange && */!memcmp(table, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], sizeof(ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]))) {
  776. c->input_rgb2yuv_table[BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  777. c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  778. c->input_rgb2yuv_table[BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  779. c->input_rgb2yuv_table[GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  780. c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  781. c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  782. c->input_rgb2yuv_table[RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  783. c->input_rgb2yuv_table[RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  784. c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
  785. }
  786. for(i=0; i<FF_ARRAY_ELEMS(map); i++)
  787. AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
  788. }
  789. static void fill_xyztables(struct SwsContext *c)
  790. {
  791. int i;
  792. double xyzgamma = XYZ_GAMMA;
  793. double rgbgamma = 1.0 / RGB_GAMMA;
  794. double xyzgammainv = 1.0 / XYZ_GAMMA;
  795. double rgbgammainv = RGB_GAMMA;
  796. static const int16_t xyz2rgb_matrix[3][4] = {
  797. {13270, -6295, -2041},
  798. {-3969, 7682, 170},
  799. { 228, -835, 4329} };
  800. static const int16_t rgb2xyz_matrix[3][4] = {
  801. {1689, 1464, 739},
  802. { 871, 2929, 296},
  803. { 79, 488, 3891} };
  804. static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
  805. memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
  806. memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
  807. c->xyzgamma = xyzgamma_tab;
  808. c->rgbgamma = rgbgamma_tab;
  809. c->xyzgammainv = xyzgammainv_tab;
  810. c->rgbgammainv = rgbgammainv_tab;
  811. if (rgbgamma_tab[4095])
  812. return;
  813. /* set gamma vectors */
  814. for (i = 0; i < 4096; i++) {
  815. xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
  816. rgbgamma_tab[i] = lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
  817. xyzgammainv_tab[i] = lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
  818. rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
  819. }
  820. }
  821. static int range_override_needed(enum AVPixelFormat format)
  822. {
  823. return !isYUV(format) && !isGray(format);
  824. }
  825. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  826. int srcRange, const int table[4], int dstRange,
  827. int brightness, int contrast, int saturation)
  828. {
  829. const AVPixFmtDescriptor *desc_dst;
  830. const AVPixFmtDescriptor *desc_src;
  831. int need_reinit = 0;
  832. if (c->nb_slice_ctx) {
  833. int parent_ret = 0;
  834. for (int i = 0; i < c->nb_slice_ctx; i++) {
  835. int ret = sws_setColorspaceDetails(c->slice_ctx[i], inv_table,
  836. srcRange, table, dstRange,
  837. brightness, contrast, saturation);
  838. if (ret < 0)
  839. parent_ret = ret;
  840. }
  841. return parent_ret;
  842. }
  843. handle_formats(c);
  844. desc_dst = av_pix_fmt_desc_get(c->dstFormat);
  845. desc_src = av_pix_fmt_desc_get(c->srcFormat);
  846. if(range_override_needed(c->dstFormat))
  847. dstRange = 0;
  848. if(range_override_needed(c->srcFormat))
  849. srcRange = 0;
  850. if (c->srcRange != srcRange ||
  851. c->dstRange != dstRange ||
  852. c->brightness != brightness ||
  853. c->contrast != contrast ||
  854. c->saturation != saturation ||
  855. memcmp(c->srcColorspaceTable, inv_table, sizeof(int) * 4) ||
  856. memcmp(c->dstColorspaceTable, table, sizeof(int) * 4)
  857. )
  858. need_reinit = 1;
  859. memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
  860. memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
  861. c->brightness = brightness;
  862. c->contrast = contrast;
  863. c->saturation = saturation;
  864. c->srcRange = srcRange;
  865. c->dstRange = dstRange;
  866. //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
  867. //and what we have in ticket 2939 looks better with this check
  868. if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
  869. ff_sws_init_range_convert(c);
  870. c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
  871. c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
  872. if (c->cascaded_context[c->cascaded_mainindex])
  873. return sws_setColorspaceDetails(c->cascaded_context[c->cascaded_mainindex],inv_table, srcRange,table, dstRange, brightness, contrast, saturation);
  874. if (!need_reinit)
  875. return 0;
  876. if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat))) {
  877. if (!c->cascaded_context[0] &&
  878. memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4) &&
  879. c->srcW && c->srcH && c->dstW && c->dstH) {
  880. enum AVPixelFormat tmp_format;
  881. int tmp_width, tmp_height;
  882. int srcW = c->srcW;
  883. int srcH = c->srcH;
  884. int dstW = c->dstW;
  885. int dstH = c->dstH;
  886. int ret;
  887. av_log(c, AV_LOG_VERBOSE, "YUV color matrix differs for YUV->YUV, using intermediate RGB to convert\n");
  888. if (isNBPS(c->dstFormat) || is16BPS(c->dstFormat)) {
  889. if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
  890. tmp_format = AV_PIX_FMT_BGRA64;
  891. } else {
  892. tmp_format = AV_PIX_FMT_BGR48;
  893. }
  894. } else {
  895. if (isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) {
  896. tmp_format = AV_PIX_FMT_BGRA;
  897. } else {
  898. tmp_format = AV_PIX_FMT_BGR24;
  899. }
  900. }
  901. if (srcW*srcH > dstW*dstH) {
  902. tmp_width = dstW;
  903. tmp_height = dstH;
  904. } else {
  905. tmp_width = srcW;
  906. tmp_height = srcH;
  907. }
  908. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  909. tmp_width, tmp_height, tmp_format, 64);
  910. if (ret < 0)
  911. return ret;
  912. c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, c->srcFormat,
  913. tmp_width, tmp_height, tmp_format,
  914. c->flags, c->param);
  915. if (!c->cascaded_context[0])
  916. return -1;
  917. c->cascaded_context[0]->alphablend = c->alphablend;
  918. ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
  919. if (ret < 0)
  920. return ret;
  921. //we set both src and dst depending on that the RGB side will be ignored
  922. sws_setColorspaceDetails(c->cascaded_context[0], inv_table,
  923. srcRange, table, dstRange,
  924. brightness, contrast, saturation);
  925. c->cascaded_context[1] = sws_alloc_set_opts(tmp_width, tmp_height, tmp_format,
  926. dstW, dstH, c->dstFormat,
  927. c->flags, c->param);
  928. if (!c->cascaded_context[1])
  929. return -1;
  930. c->cascaded_context[1]->srcRange = srcRange;
  931. c->cascaded_context[1]->dstRange = dstRange;
  932. ret = sws_init_context(c->cascaded_context[1], NULL , NULL);
  933. if (ret < 0)
  934. return ret;
  935. sws_setColorspaceDetails(c->cascaded_context[1], inv_table,
  936. srcRange, table, dstRange,
  937. 0, 1 << 16, 1 << 16);
  938. return 0;
  939. }
  940. //We do not support this combination currently, we need to cascade more contexts to compensate
  941. if (c->cascaded_context[0] && memcmp(c->dstColorspaceTable, c->srcColorspaceTable, sizeof(int) * 4))
  942. return -1; //AVERROR_PATCHWELCOME;
  943. return 0;
  944. }
  945. if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
  946. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
  947. contrast, saturation);
  948. // FIXME factorize
  949. #if ARCH_PPC
  950. ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
  951. contrast, saturation);
  952. #endif
  953. }
  954. fill_rgb2yuv_table(c, table, dstRange);
  955. return 0;
  956. }
  957. int sws_getColorspaceDetails(struct SwsContext *c, int **inv_table,
  958. int *srcRange, int **table, int *dstRange,
  959. int *brightness, int *contrast, int *saturation)
  960. {
  961. if (!c )
  962. return -1;
  963. if (c->nb_slice_ctx) {
  964. return sws_getColorspaceDetails(c->slice_ctx[0], inv_table, srcRange,
  965. table, dstRange, brightness, contrast,
  966. saturation);
  967. }
  968. *inv_table = c->srcColorspaceTable;
  969. *table = c->dstColorspaceTable;
  970. *srcRange = range_override_needed(c->srcFormat) ? 1 : c->srcRange;
  971. *dstRange = range_override_needed(c->dstFormat) ? 1 : c->dstRange;
  972. *brightness = c->brightness;
  973. *contrast = c->contrast;
  974. *saturation = c->saturation;
  975. return 0;
  976. }
  977. static int handle_jpeg(enum AVPixelFormat *format)
  978. {
  979. switch (*format) {
  980. case AV_PIX_FMT_YUVJ420P:
  981. *format = AV_PIX_FMT_YUV420P;
  982. return 1;
  983. case AV_PIX_FMT_YUVJ411P:
  984. *format = AV_PIX_FMT_YUV411P;
  985. return 1;
  986. case AV_PIX_FMT_YUVJ422P:
  987. *format = AV_PIX_FMT_YUV422P;
  988. return 1;
  989. case AV_PIX_FMT_YUVJ444P:
  990. *format = AV_PIX_FMT_YUV444P;
  991. return 1;
  992. case AV_PIX_FMT_YUVJ440P:
  993. *format = AV_PIX_FMT_YUV440P;
  994. return 1;
  995. case AV_PIX_FMT_GRAY8:
  996. case AV_PIX_FMT_YA8:
  997. case AV_PIX_FMT_GRAY9LE:
  998. case AV_PIX_FMT_GRAY9BE:
  999. case AV_PIX_FMT_GRAY10LE:
  1000. case AV_PIX_FMT_GRAY10BE:
  1001. case AV_PIX_FMT_GRAY12LE:
  1002. case AV_PIX_FMT_GRAY12BE:
  1003. case AV_PIX_FMT_GRAY14LE:
  1004. case AV_PIX_FMT_GRAY14BE:
  1005. case AV_PIX_FMT_GRAY16LE:
  1006. case AV_PIX_FMT_GRAY16BE:
  1007. case AV_PIX_FMT_YA16BE:
  1008. case AV_PIX_FMT_YA16LE:
  1009. return 1;
  1010. default:
  1011. return 0;
  1012. }
  1013. }
  1014. static int handle_0alpha(enum AVPixelFormat *format)
  1015. {
  1016. switch (*format) {
  1017. case AV_PIX_FMT_0BGR : *format = AV_PIX_FMT_ABGR ; return 1;
  1018. case AV_PIX_FMT_BGR0 : *format = AV_PIX_FMT_BGRA ; return 4;
  1019. case AV_PIX_FMT_0RGB : *format = AV_PIX_FMT_ARGB ; return 1;
  1020. case AV_PIX_FMT_RGB0 : *format = AV_PIX_FMT_RGBA ; return 4;
  1021. default: return 0;
  1022. }
  1023. }
  1024. static int handle_xyz(enum AVPixelFormat *format)
  1025. {
  1026. switch (*format) {
  1027. case AV_PIX_FMT_XYZ12BE : *format = AV_PIX_FMT_RGB48BE; return 1;
  1028. case AV_PIX_FMT_XYZ12LE : *format = AV_PIX_FMT_RGB48LE; return 1;
  1029. default: return 0;
  1030. }
  1031. }
  1032. static void handle_formats(SwsContext *c)
  1033. {
  1034. c->src0Alpha |= handle_0alpha(&c->srcFormat);
  1035. c->dst0Alpha |= handle_0alpha(&c->dstFormat);
  1036. c->srcXYZ |= handle_xyz(&c->srcFormat);
  1037. c->dstXYZ |= handle_xyz(&c->dstFormat);
  1038. if (c->srcXYZ || c->dstXYZ)
  1039. fill_xyztables(c);
  1040. }
  1041. SwsContext *sws_alloc_context(void)
  1042. {
  1043. SwsContext *c = av_mallocz(sizeof(SwsContext));
  1044. av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
  1045. if (c) {
  1046. c->av_class = &ff_sws_context_class;
  1047. av_opt_set_defaults(c);
  1048. atomic_init(&c->stride_unaligned_warned, 0);
  1049. atomic_init(&c->data_unaligned_warned, 0);
  1050. }
  1051. return c;
  1052. }
  1053. static uint16_t * alloc_gamma_tbl(double e)
  1054. {
  1055. int i = 0;
  1056. uint16_t * tbl;
  1057. tbl = (uint16_t*)av_malloc(sizeof(uint16_t) * 1 << 16);
  1058. if (!tbl)
  1059. return NULL;
  1060. for (i = 0; i < 65536; ++i) {
  1061. tbl[i] = pow(i / 65535.0, e) * 65535.0;
  1062. }
  1063. return tbl;
  1064. }
  1065. static enum AVPixelFormat alphaless_fmt(enum AVPixelFormat fmt)
  1066. {
  1067. switch(fmt) {
  1068. case AV_PIX_FMT_ARGB: return AV_PIX_FMT_RGB24;
  1069. case AV_PIX_FMT_RGBA: return AV_PIX_FMT_RGB24;
  1070. case AV_PIX_FMT_ABGR: return AV_PIX_FMT_BGR24;
  1071. case AV_PIX_FMT_BGRA: return AV_PIX_FMT_BGR24;
  1072. case AV_PIX_FMT_YA8: return AV_PIX_FMT_GRAY8;
  1073. case AV_PIX_FMT_YUVA420P: return AV_PIX_FMT_YUV420P;
  1074. case AV_PIX_FMT_YUVA422P: return AV_PIX_FMT_YUV422P;
  1075. case AV_PIX_FMT_YUVA444P: return AV_PIX_FMT_YUV444P;
  1076. case AV_PIX_FMT_GBRAP: return AV_PIX_FMT_GBRP;
  1077. case AV_PIX_FMT_GBRAP10LE: return AV_PIX_FMT_GBRP10;
  1078. case AV_PIX_FMT_GBRAP10BE: return AV_PIX_FMT_GBRP10;
  1079. case AV_PIX_FMT_GBRAP12LE: return AV_PIX_FMT_GBRP12;
  1080. case AV_PIX_FMT_GBRAP12BE: return AV_PIX_FMT_GBRP12;
  1081. case AV_PIX_FMT_GBRAP16LE: return AV_PIX_FMT_GBRP16;
  1082. case AV_PIX_FMT_GBRAP16BE: return AV_PIX_FMT_GBRP16;
  1083. case AV_PIX_FMT_RGBA64LE: return AV_PIX_FMT_RGB48;
  1084. case AV_PIX_FMT_RGBA64BE: return AV_PIX_FMT_RGB48;
  1085. case AV_PIX_FMT_BGRA64LE: return AV_PIX_FMT_BGR48;
  1086. case AV_PIX_FMT_BGRA64BE: return AV_PIX_FMT_BGR48;
  1087. case AV_PIX_FMT_YA16BE: return AV_PIX_FMT_GRAY16;
  1088. case AV_PIX_FMT_YA16LE: return AV_PIX_FMT_GRAY16;
  1089. case AV_PIX_FMT_YUVA420P9BE: return AV_PIX_FMT_YUV420P9;
  1090. case AV_PIX_FMT_YUVA422P9BE: return AV_PIX_FMT_YUV422P9;
  1091. case AV_PIX_FMT_YUVA444P9BE: return AV_PIX_FMT_YUV444P9;
  1092. case AV_PIX_FMT_YUVA420P9LE: return AV_PIX_FMT_YUV420P9;
  1093. case AV_PIX_FMT_YUVA422P9LE: return AV_PIX_FMT_YUV422P9;
  1094. case AV_PIX_FMT_YUVA444P9LE: return AV_PIX_FMT_YUV444P9;
  1095. case AV_PIX_FMT_YUVA420P10BE: return AV_PIX_FMT_YUV420P10;
  1096. case AV_PIX_FMT_YUVA422P10BE: return AV_PIX_FMT_YUV422P10;
  1097. case AV_PIX_FMT_YUVA444P10BE: return AV_PIX_FMT_YUV444P10;
  1098. case AV_PIX_FMT_YUVA420P10LE: return AV_PIX_FMT_YUV420P10;
  1099. case AV_PIX_FMT_YUVA422P10LE: return AV_PIX_FMT_YUV422P10;
  1100. case AV_PIX_FMT_YUVA444P10LE: return AV_PIX_FMT_YUV444P10;
  1101. case AV_PIX_FMT_YUVA420P16BE: return AV_PIX_FMT_YUV420P16;
  1102. case AV_PIX_FMT_YUVA422P16BE: return AV_PIX_FMT_YUV422P16;
  1103. case AV_PIX_FMT_YUVA444P16BE: return AV_PIX_FMT_YUV444P16;
  1104. case AV_PIX_FMT_YUVA420P16LE: return AV_PIX_FMT_YUV420P16;
  1105. case AV_PIX_FMT_YUVA422P16LE: return AV_PIX_FMT_YUV422P16;
  1106. case AV_PIX_FMT_YUVA444P16LE: return AV_PIX_FMT_YUV444P16;
  1107. // case AV_PIX_FMT_AYUV64LE:
  1108. // case AV_PIX_FMT_AYUV64BE:
  1109. // case AV_PIX_FMT_PAL8:
  1110. default: return AV_PIX_FMT_NONE;
  1111. }
  1112. }
  1113. static int context_init_threaded(SwsContext *c,
  1114. SwsFilter *src_filter, SwsFilter *dst_filter)
  1115. {
  1116. int ret;
  1117. ret = avpriv_slicethread_create(&c->slicethread, (void*)c,
  1118. ff_sws_slice_worker, NULL, c->nb_threads);
  1119. if (ret == AVERROR(ENOSYS)) {
  1120. c->nb_threads = 1;
  1121. return 0;
  1122. } else if (ret < 0)
  1123. return ret;
  1124. c->nb_threads = ret;
  1125. c->slice_ctx = av_calloc(c->nb_threads, sizeof(*c->slice_ctx));
  1126. c->slice_err = av_calloc(c->nb_threads, sizeof(*c->slice_err));
  1127. if (!c->slice_ctx || !c->slice_err)
  1128. return AVERROR(ENOMEM);
  1129. for (int i = 0; i < c->nb_threads; i++) {
  1130. c->slice_ctx[i] = sws_alloc_context();
  1131. if (!c->slice_ctx[i])
  1132. return AVERROR(ENOMEM);
  1133. c->slice_ctx[i]->parent = c;
  1134. ret = av_opt_copy((void*)c->slice_ctx[i], (void*)c);
  1135. if (ret < 0)
  1136. return ret;
  1137. c->slice_ctx[i]->nb_threads = 1;
  1138. ret = sws_init_context(c->slice_ctx[i], src_filter, dst_filter);
  1139. if (ret < 0)
  1140. return ret;
  1141. c->nb_slice_ctx++;
  1142. if (c->slice_ctx[i]->dither == SWS_DITHER_ED) {
  1143. av_log(c, AV_LOG_VERBOSE,
  1144. "Error-diffusion dither is in use, scaling will be single-threaded.");
  1145. break;
  1146. }
  1147. }
  1148. c->frame_src = av_frame_alloc();
  1149. c->frame_dst = av_frame_alloc();
  1150. if (!c->frame_src || !c->frame_dst)
  1151. return AVERROR(ENOMEM);
  1152. return 0;
  1153. }
  1154. av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
  1155. SwsFilter *dstFilter)
  1156. {
  1157. int i;
  1158. int usesVFilter, usesHFilter;
  1159. int unscaled;
  1160. SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
  1161. int srcW = c->srcW;
  1162. int srcH = c->srcH;
  1163. int dstW = c->dstW;
  1164. int dstH = c->dstH;
  1165. int dst_stride = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
  1166. int flags, cpu_flags;
  1167. enum AVPixelFormat srcFormat = c->srcFormat;
  1168. enum AVPixelFormat dstFormat = c->dstFormat;
  1169. const AVPixFmtDescriptor *desc_src;
  1170. const AVPixFmtDescriptor *desc_dst;
  1171. int ret = 0;
  1172. enum AVPixelFormat tmpFmt;
  1173. static const float float_mult = 1.0f / 255.0f;
  1174. static AVOnce rgb2rgb_once = AV_ONCE_INIT;
  1175. if (c->nb_threads != 1) {
  1176. ret = context_init_threaded(c, srcFilter, dstFilter);
  1177. if (ret < 0 || c->nb_threads > 1)
  1178. return ret;
  1179. // threading disabled in this build, init as single-threaded
  1180. }
  1181. cpu_flags = av_get_cpu_flags();
  1182. flags = c->flags;
  1183. emms_c();
  1184. if (ff_thread_once(&rgb2rgb_once, ff_sws_rgb2rgb_init) != 0)
  1185. return AVERROR_UNKNOWN;
  1186. unscaled = (srcW == dstW && srcH == dstH);
  1187. c->srcRange |= handle_jpeg(&c->srcFormat);
  1188. c->dstRange |= handle_jpeg(&c->dstFormat);
  1189. if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
  1190. av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
  1191. if (!c->contrast && !c->saturation && !c->dstFormatBpp)
  1192. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
  1193. ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
  1194. c->dstRange, 0, 1 << 16, 1 << 16);
  1195. handle_formats(c);
  1196. srcFormat = c->srcFormat;
  1197. dstFormat = c->dstFormat;
  1198. desc_src = av_pix_fmt_desc_get(srcFormat);
  1199. desc_dst = av_pix_fmt_desc_get(dstFormat);
  1200. // If the source has no alpha then disable alpha blendaway
  1201. if (c->src0Alpha)
  1202. c->alphablend = SWS_ALPHA_BLEND_NONE;
  1203. if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
  1204. av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
  1205. if (!sws_isSupportedInput(srcFormat)) {
  1206. av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
  1207. av_get_pix_fmt_name(srcFormat));
  1208. return AVERROR(EINVAL);
  1209. }
  1210. if (!sws_isSupportedOutput(dstFormat)) {
  1211. av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
  1212. av_get_pix_fmt_name(dstFormat));
  1213. return AVERROR(EINVAL);
  1214. }
  1215. }
  1216. av_assert2(desc_src && desc_dst);
  1217. i = flags & (SWS_POINT |
  1218. SWS_AREA |
  1219. SWS_BILINEAR |
  1220. SWS_FAST_BILINEAR |
  1221. SWS_BICUBIC |
  1222. SWS_X |
  1223. SWS_GAUSS |
  1224. SWS_LANCZOS |
  1225. SWS_SINC |
  1226. SWS_SPLINE |
  1227. SWS_BICUBLIN);
  1228. /* provide a default scaler if not set by caller */
  1229. if (!i) {
  1230. if (dstW < srcW && dstH < srcH)
  1231. flags |= SWS_BICUBIC;
  1232. else if (dstW > srcW && dstH > srcH)
  1233. flags |= SWS_BICUBIC;
  1234. else
  1235. flags |= SWS_BICUBIC;
  1236. c->flags = flags;
  1237. } else if (i & (i - 1)) {
  1238. av_log(c, AV_LOG_ERROR,
  1239. "Exactly one scaler algorithm must be chosen, got %X\n", i);
  1240. return AVERROR(EINVAL);
  1241. }
  1242. /* sanity check */
  1243. if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
  1244. /* FIXME check if these are enough and try to lower them after
  1245. * fixing the relevant parts of the code */
  1246. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  1247. srcW, srcH, dstW, dstH);
  1248. return AVERROR(EINVAL);
  1249. }
  1250. if (flags & SWS_FAST_BILINEAR) {
  1251. if (srcW < 8 || dstW < 8) {
  1252. flags ^= SWS_FAST_BILINEAR | SWS_BILINEAR;
  1253. c->flags = flags;
  1254. }
  1255. }
  1256. if (!dstFilter)
  1257. dstFilter = &dummyFilter;
  1258. if (!srcFilter)
  1259. srcFilter = &dummyFilter;
  1260. c->lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
  1261. c->lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
  1262. c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
  1263. c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
  1264. c->vRounder = 4 * 0x0001000100010001ULL;
  1265. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
  1266. (srcFilter->chrV && srcFilter->chrV->length > 1) ||
  1267. (dstFilter->lumV && dstFilter->lumV->length > 1) ||
  1268. (dstFilter->chrV && dstFilter->chrV->length > 1);
  1269. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
  1270. (srcFilter->chrH && srcFilter->chrH->length > 1) ||
  1271. (dstFilter->lumH && dstFilter->lumH->length > 1) ||
  1272. (dstFilter->chrH && dstFilter->chrH->length > 1);
  1273. av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
  1274. av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
  1275. c->dst_slice_align = 1 << c->chrDstVSubSample;
  1276. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
  1277. if (dstW&1) {
  1278. av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
  1279. flags |= SWS_FULL_CHR_H_INT;
  1280. c->flags = flags;
  1281. }
  1282. if ( c->chrSrcHSubSample == 0
  1283. && c->chrSrcVSubSample == 0
  1284. && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
  1285. && !(c->flags & SWS_FAST_BILINEAR)
  1286. ) {
  1287. av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
  1288. flags |= SWS_FULL_CHR_H_INT;
  1289. c->flags = flags;
  1290. }
  1291. }
  1292. if (c->dither == SWS_DITHER_AUTO) {
  1293. if (flags & SWS_ERROR_DIFFUSION)
  1294. c->dither = SWS_DITHER_ED;
  1295. }
  1296. if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
  1297. dstFormat == AV_PIX_FMT_RGB4_BYTE ||
  1298. dstFormat == AV_PIX_FMT_BGR8 ||
  1299. dstFormat == AV_PIX_FMT_RGB8) {
  1300. if (c->dither == SWS_DITHER_AUTO)
  1301. c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
  1302. if (!(flags & SWS_FULL_CHR_H_INT)) {
  1303. if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER || c->dither == SWS_DITHER_NONE) {
  1304. av_log(c, AV_LOG_DEBUG,
  1305. "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
  1306. av_get_pix_fmt_name(dstFormat));
  1307. flags |= SWS_FULL_CHR_H_INT;
  1308. c->flags = flags;
  1309. }
  1310. }
  1311. if (flags & SWS_FULL_CHR_H_INT) {
  1312. if (c->dither == SWS_DITHER_BAYER) {
  1313. av_log(c, AV_LOG_DEBUG,
  1314. "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
  1315. av_get_pix_fmt_name(dstFormat));
  1316. c->dither = SWS_DITHER_ED;
  1317. }
  1318. }
  1319. }
  1320. if (isPlanarRGB(dstFormat)) {
  1321. if (!(flags & SWS_FULL_CHR_H_INT)) {
  1322. av_log(c, AV_LOG_DEBUG,
  1323. "%s output is not supported with half chroma resolution, switching to full\n",
  1324. av_get_pix_fmt_name(dstFormat));
  1325. flags |= SWS_FULL_CHR_H_INT;
  1326. c->flags = flags;
  1327. }
  1328. }
  1329. /* reuse chroma for 2 pixels RGB/BGR unless user wants full
  1330. * chroma interpolation */
  1331. if (flags & SWS_FULL_CHR_H_INT &&
  1332. isAnyRGB(dstFormat) &&
  1333. !isPlanarRGB(dstFormat) &&
  1334. dstFormat != AV_PIX_FMT_RGBA64LE &&
  1335. dstFormat != AV_PIX_FMT_RGBA64BE &&
  1336. dstFormat != AV_PIX_FMT_BGRA64LE &&
  1337. dstFormat != AV_PIX_FMT_BGRA64BE &&
  1338. dstFormat != AV_PIX_FMT_RGB48LE &&
  1339. dstFormat != AV_PIX_FMT_RGB48BE &&
  1340. dstFormat != AV_PIX_FMT_BGR48LE &&
  1341. dstFormat != AV_PIX_FMT_BGR48BE &&
  1342. dstFormat != AV_PIX_FMT_RGBA &&
  1343. dstFormat != AV_PIX_FMT_ARGB &&
  1344. dstFormat != AV_PIX_FMT_BGRA &&
  1345. dstFormat != AV_PIX_FMT_ABGR &&
  1346. dstFormat != AV_PIX_FMT_RGB24 &&
  1347. dstFormat != AV_PIX_FMT_BGR24 &&
  1348. dstFormat != AV_PIX_FMT_BGR4_BYTE &&
  1349. dstFormat != AV_PIX_FMT_RGB4_BYTE &&
  1350. dstFormat != AV_PIX_FMT_BGR8 &&
  1351. dstFormat != AV_PIX_FMT_RGB8
  1352. ) {
  1353. av_log(c, AV_LOG_WARNING,
  1354. "full chroma interpolation for destination format '%s' not yet implemented\n",
  1355. av_get_pix_fmt_name(dstFormat));
  1356. flags &= ~SWS_FULL_CHR_H_INT;
  1357. c->flags = flags;
  1358. }
  1359. if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
  1360. c->chrDstHSubSample = 1;
  1361. // drop some chroma lines if the user wants it
  1362. c->vChrDrop = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
  1363. SWS_SRC_V_CHR_DROP_SHIFT;
  1364. c->chrSrcVSubSample += c->vChrDrop;
  1365. /* drop every other pixel for chroma calculation unless user
  1366. * wants full chroma */
  1367. if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP) &&
  1368. srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
  1369. srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
  1370. srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
  1371. srcFormat != AV_PIX_FMT_GBRP9BE && srcFormat != AV_PIX_FMT_GBRP9LE &&
  1372. srcFormat != AV_PIX_FMT_GBRP10BE && srcFormat != AV_PIX_FMT_GBRP10LE &&
  1373. srcFormat != AV_PIX_FMT_GBRAP10BE && srcFormat != AV_PIX_FMT_GBRAP10LE &&
  1374. srcFormat != AV_PIX_FMT_GBRP12BE && srcFormat != AV_PIX_FMT_GBRP12LE &&
  1375. srcFormat != AV_PIX_FMT_GBRAP12BE && srcFormat != AV_PIX_FMT_GBRAP12LE &&
  1376. srcFormat != AV_PIX_FMT_GBRP14BE && srcFormat != AV_PIX_FMT_GBRP14LE &&
  1377. srcFormat != AV_PIX_FMT_GBRP16BE && srcFormat != AV_PIX_FMT_GBRP16LE &&
  1378. srcFormat != AV_PIX_FMT_GBRAP16BE && srcFormat != AV_PIX_FMT_GBRAP16LE &&
  1379. srcFormat != AV_PIX_FMT_GBRPF32BE && srcFormat != AV_PIX_FMT_GBRPF32LE &&
  1380. srcFormat != AV_PIX_FMT_GBRAPF32BE && srcFormat != AV_PIX_FMT_GBRAPF32LE &&
  1381. ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
  1382. (flags & SWS_FAST_BILINEAR)))
  1383. c->chrSrcHSubSample = 1;
  1384. // Note the AV_CEIL_RSHIFT is so that we always round toward +inf.
  1385. c->chrSrcW = AV_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
  1386. c->chrSrcH = AV_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
  1387. c->chrDstW = AV_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
  1388. c->chrDstH = AV_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
  1389. if (!FF_ALLOCZ_TYPED_ARRAY(c->formatConvBuffer, FFALIGN(srcW * 2 + 78, 16) * 2))
  1390. goto nomem;
  1391. c->frame_src = av_frame_alloc();
  1392. c->frame_dst = av_frame_alloc();
  1393. if (!c->frame_src || !c->frame_dst)
  1394. goto nomem;
  1395. c->srcBpc = desc_src->comp[0].depth;
  1396. if (c->srcBpc < 8)
  1397. c->srcBpc = 8;
  1398. c->dstBpc = desc_dst->comp[0].depth;
  1399. if (c->dstBpc < 8)
  1400. c->dstBpc = 8;
  1401. if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
  1402. c->srcBpc = 16;
  1403. if (c->dstBpc == 16)
  1404. dst_stride <<= 1;
  1405. if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
  1406. c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
  1407. c->chrDstW >= c->chrSrcW &&
  1408. (srcW & 15) == 0;
  1409. if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
  1410. && (flags & SWS_FAST_BILINEAR)) {
  1411. if (flags & SWS_PRINT_INFO)
  1412. av_log(c, AV_LOG_INFO,
  1413. "output width is not a multiple of 32 -> no MMXEXT scaler\n");
  1414. }
  1415. if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
  1416. c->canMMXEXTBeUsed = 0;
  1417. } else
  1418. c->canMMXEXTBeUsed = 0;
  1419. c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
  1420. c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
  1421. /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
  1422. * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
  1423. * correct scaling.
  1424. * n-2 is the last chrominance sample available.
  1425. * This is not perfect, but no one should notice the difference, the more
  1426. * correct variant would be like the vertical one, but that would require
  1427. * some special code for the first and last pixel */
  1428. if (flags & SWS_FAST_BILINEAR) {
  1429. if (c->canMMXEXTBeUsed) {
  1430. c->lumXInc += 20;
  1431. c->chrXInc += 20;
  1432. }
  1433. // we don't use the x86 asm scaler if MMX is available
  1434. else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
  1435. c->lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
  1436. c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
  1437. }
  1438. }
  1439. // hardcoded for now
  1440. c->gamma_value = 2.2;
  1441. tmpFmt = AV_PIX_FMT_RGBA64LE;
  1442. if (!unscaled && c->gamma_flag && (srcFormat != tmpFmt || dstFormat != tmpFmt)) {
  1443. SwsContext *c2;
  1444. c->cascaded_context[0] = NULL;
  1445. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1446. srcW, srcH, tmpFmt, 64);
  1447. if (ret < 0)
  1448. return ret;
  1449. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  1450. srcW, srcH, tmpFmt,
  1451. flags, NULL, NULL, c->param);
  1452. if (!c->cascaded_context[0]) {
  1453. return AVERROR(ENOMEM);
  1454. }
  1455. c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFmt,
  1456. dstW, dstH, tmpFmt,
  1457. flags, srcFilter, dstFilter, c->param);
  1458. if (!c->cascaded_context[1])
  1459. return AVERROR(ENOMEM);
  1460. c2 = c->cascaded_context[1];
  1461. c2->is_internal_gamma = 1;
  1462. c2->gamma = alloc_gamma_tbl( c->gamma_value);
  1463. c2->inv_gamma = alloc_gamma_tbl(1.f/c->gamma_value);
  1464. if (!c2->gamma || !c2->inv_gamma)
  1465. return AVERROR(ENOMEM);
  1466. // is_internal_flag is set after creating the context
  1467. // to properly create the gamma convert FilterDescriptor
  1468. // we have to re-initialize it
  1469. ff_free_filters(c2);
  1470. if ((ret = ff_init_filters(c2)) < 0) {
  1471. sws_freeContext(c2);
  1472. c->cascaded_context[1] = NULL;
  1473. return ret;
  1474. }
  1475. c->cascaded_context[2] = NULL;
  1476. if (dstFormat != tmpFmt) {
  1477. ret = av_image_alloc(c->cascaded1_tmp, c->cascaded1_tmpStride,
  1478. dstW, dstH, tmpFmt, 64);
  1479. if (ret < 0)
  1480. return ret;
  1481. c->cascaded_context[2] = sws_getContext(dstW, dstH, tmpFmt,
  1482. dstW, dstH, dstFormat,
  1483. flags, NULL, NULL, c->param);
  1484. if (!c->cascaded_context[2])
  1485. return AVERROR(ENOMEM);
  1486. }
  1487. return 0;
  1488. }
  1489. if (isBayer(srcFormat)) {
  1490. if (!unscaled ||
  1491. (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P &&
  1492. dstFormat != AV_PIX_FMT_RGB48)) {
  1493. enum AVPixelFormat tmpFormat = isBayer16BPS(srcFormat) ? AV_PIX_FMT_RGB48 : AV_PIX_FMT_RGB24;
  1494. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1495. srcW, srcH, tmpFormat, 64);
  1496. if (ret < 0)
  1497. return ret;
  1498. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  1499. srcW, srcH, tmpFormat,
  1500. flags, srcFilter, NULL, c->param);
  1501. if (!c->cascaded_context[0])
  1502. return AVERROR(ENOMEM);
  1503. c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
  1504. dstW, dstH, dstFormat,
  1505. flags, NULL, dstFilter, c->param);
  1506. if (!c->cascaded_context[1])
  1507. return AVERROR(ENOMEM);
  1508. return 0;
  1509. }
  1510. }
  1511. if (unscaled && c->srcBpc == 8 && dstFormat == AV_PIX_FMT_GRAYF32){
  1512. for (i = 0; i < 256; ++i){
  1513. c->uint2float_lut[i] = (float)i * float_mult;
  1514. }
  1515. }
  1516. // float will be converted to uint16_t
  1517. if ((srcFormat == AV_PIX_FMT_GRAYF32BE || srcFormat == AV_PIX_FMT_GRAYF32LE) &&
  1518. (!unscaled || unscaled && dstFormat != srcFormat && (srcFormat != AV_PIX_FMT_GRAYF32 ||
  1519. dstFormat != AV_PIX_FMT_GRAY8))){
  1520. c->srcBpc = 16;
  1521. }
  1522. if (CONFIG_SWSCALE_ALPHA && isALPHA(srcFormat) && !isALPHA(dstFormat)) {
  1523. enum AVPixelFormat tmpFormat = alphaless_fmt(srcFormat);
  1524. if (tmpFormat != AV_PIX_FMT_NONE && c->alphablend != SWS_ALPHA_BLEND_NONE) {
  1525. if (!unscaled ||
  1526. dstFormat != tmpFormat ||
  1527. usesHFilter || usesVFilter ||
  1528. c->srcRange != c->dstRange
  1529. ) {
  1530. c->cascaded_mainindex = 1;
  1531. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1532. srcW, srcH, tmpFormat, 64);
  1533. if (ret < 0)
  1534. return ret;
  1535. c->cascaded_context[0] = sws_alloc_set_opts(srcW, srcH, srcFormat,
  1536. srcW, srcH, tmpFormat,
  1537. flags, c->param);
  1538. if (!c->cascaded_context[0])
  1539. return AVERROR(EINVAL);
  1540. c->cascaded_context[0]->alphablend = c->alphablend;
  1541. ret = sws_init_context(c->cascaded_context[0], NULL , NULL);
  1542. if (ret < 0)
  1543. return ret;
  1544. c->cascaded_context[1] = sws_alloc_set_opts(srcW, srcH, tmpFormat,
  1545. dstW, dstH, dstFormat,
  1546. flags, c->param);
  1547. if (!c->cascaded_context[1])
  1548. return AVERROR(EINVAL);
  1549. c->cascaded_context[1]->srcRange = c->srcRange;
  1550. c->cascaded_context[1]->dstRange = c->dstRange;
  1551. ret = sws_init_context(c->cascaded_context[1], srcFilter , dstFilter);
  1552. if (ret < 0)
  1553. return ret;
  1554. return 0;
  1555. }
  1556. }
  1557. }
  1558. #if HAVE_MMAP && HAVE_MPROTECT && defined(MAP_ANONYMOUS)
  1559. #define USE_MMAP 1
  1560. #else
  1561. #define USE_MMAP 0
  1562. #endif
  1563. /* precalculate horizontal scaler filter coefficients */
  1564. {
  1565. #if HAVE_MMXEXT_INLINE
  1566. // can't downscale !!!
  1567. if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
  1568. c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
  1569. NULL, NULL, 8);
  1570. c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
  1571. NULL, NULL, NULL, 4);
  1572. #if USE_MMAP
  1573. c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
  1574. PROT_READ | PROT_WRITE,
  1575. MAP_PRIVATE | MAP_ANONYMOUS,
  1576. -1, 0);
  1577. c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
  1578. PROT_READ | PROT_WRITE,
  1579. MAP_PRIVATE | MAP_ANONYMOUS,
  1580. -1, 0);
  1581. #elif HAVE_VIRTUALALLOC
  1582. c->lumMmxextFilterCode = VirtualAlloc(NULL,
  1583. c->lumMmxextFilterCodeSize,
  1584. MEM_COMMIT,
  1585. PAGE_EXECUTE_READWRITE);
  1586. c->chrMmxextFilterCode = VirtualAlloc(NULL,
  1587. c->chrMmxextFilterCodeSize,
  1588. MEM_COMMIT,
  1589. PAGE_EXECUTE_READWRITE);
  1590. #else
  1591. c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
  1592. c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
  1593. #endif
  1594. #ifdef MAP_ANONYMOUS
  1595. if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
  1596. #else
  1597. if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
  1598. #endif
  1599. {
  1600. av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
  1601. return AVERROR(ENOMEM);
  1602. }
  1603. if (!FF_ALLOCZ_TYPED_ARRAY(c->hLumFilter, dstW / 8 + 8) ||
  1604. !FF_ALLOCZ_TYPED_ARRAY(c->hChrFilter, c->chrDstW / 4 + 8) ||
  1605. !FF_ALLOCZ_TYPED_ARRAY(c->hLumFilterPos, dstW / 2 / 8 + 8) ||
  1606. !FF_ALLOCZ_TYPED_ARRAY(c->hChrFilterPos, c->chrDstW / 2 / 4 + 8))
  1607. goto nomem;
  1608. ff_init_hscaler_mmxext( dstW, c->lumXInc, c->lumMmxextFilterCode,
  1609. c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
  1610. ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
  1611. c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
  1612. #if USE_MMAP
  1613. if ( mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
  1614. || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
  1615. av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
  1616. ret = AVERROR(EINVAL);
  1617. goto fail;
  1618. }
  1619. #endif
  1620. } else
  1621. #endif /* HAVE_MMXEXT_INLINE */
  1622. {
  1623. const int filterAlign = X86_MMX(cpu_flags) ? 4 :
  1624. PPC_ALTIVEC(cpu_flags) ? 8 :
  1625. have_neon(cpu_flags) ? 4 : 1;
  1626. if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
  1627. &c->hLumFilterSize, c->lumXInc,
  1628. srcW, dstW, filterAlign, 1 << 14,
  1629. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  1630. cpu_flags, srcFilter->lumH, dstFilter->lumH,
  1631. c->param,
  1632. get_local_pos(c, 0, 0, 0),
  1633. get_local_pos(c, 0, 0, 0))) < 0)
  1634. goto fail;
  1635. if (ff_shuffle_filter_coefficients(c, c->hLumFilterPos, c->hLumFilterSize, c->hLumFilter, dstW) < 0)
  1636. goto nomem;
  1637. if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
  1638. &c->hChrFilterSize, c->chrXInc,
  1639. c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
  1640. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  1641. cpu_flags, srcFilter->chrH, dstFilter->chrH,
  1642. c->param,
  1643. get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
  1644. get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
  1645. goto fail;
  1646. if (ff_shuffle_filter_coefficients(c, c->hChrFilterPos, c->hChrFilterSize, c->hChrFilter, c->chrDstW) < 0)
  1647. goto nomem;
  1648. }
  1649. } // initialize horizontal stuff
  1650. /* precalculate vertical scaler filter coefficients */
  1651. {
  1652. const int filterAlign = X86_MMX(cpu_flags) ? 2 :
  1653. PPC_ALTIVEC(cpu_flags) ? 8 :
  1654. have_neon(cpu_flags) ? 2 : 1;
  1655. if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
  1656. c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
  1657. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  1658. cpu_flags, srcFilter->lumV, dstFilter->lumV,
  1659. c->param,
  1660. get_local_pos(c, 0, 0, 1),
  1661. get_local_pos(c, 0, 0, 1))) < 0)
  1662. goto fail;
  1663. if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
  1664. c->chrYInc, c->chrSrcH, c->chrDstH,
  1665. filterAlign, (1 << 12),
  1666. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  1667. cpu_flags, srcFilter->chrV, dstFilter->chrV,
  1668. c->param,
  1669. get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
  1670. get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
  1671. goto fail;
  1672. #if HAVE_ALTIVEC
  1673. if (!FF_ALLOC_TYPED_ARRAY(c->vYCoeffsBank, c->vLumFilterSize * c->dstH) ||
  1674. !FF_ALLOC_TYPED_ARRAY(c->vCCoeffsBank, c->vChrFilterSize * c->chrDstH))
  1675. goto nomem;
  1676. for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
  1677. int j;
  1678. short *p = (short *)&c->vYCoeffsBank[i];
  1679. for (j = 0; j < 8; j++)
  1680. p[j] = c->vLumFilter[i];
  1681. }
  1682. for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
  1683. int j;
  1684. short *p = (short *)&c->vCCoeffsBank[i];
  1685. for (j = 0; j < 8; j++)
  1686. p[j] = c->vChrFilter[i];
  1687. }
  1688. #endif
  1689. }
  1690. for (i = 0; i < 4; i++)
  1691. if (!FF_ALLOCZ_TYPED_ARRAY(c->dither_error[i], c->dstW + 3))
  1692. goto nomem;
  1693. c->needAlpha = (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat)) ? 1 : 0;
  1694. // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
  1695. c->uv_off = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
  1696. c->uv_offx2 = dst_stride + 16;
  1697. av_assert0(c->chrDstH <= dstH);
  1698. if (flags & SWS_PRINT_INFO) {
  1699. const char *scaler = NULL, *cpucaps;
  1700. for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
  1701. if (flags & scale_algorithms[i].flag) {
  1702. scaler = scale_algorithms[i].description;
  1703. break;
  1704. }
  1705. }
  1706. if (!scaler)
  1707. scaler = "ehh flags invalid?!";
  1708. av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
  1709. scaler,
  1710. av_get_pix_fmt_name(srcFormat),
  1711. #ifdef DITHER1XBPP
  1712. dstFormat == AV_PIX_FMT_BGR555 || dstFormat == AV_PIX_FMT_BGR565 ||
  1713. dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
  1714. dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
  1715. "dithered " : "",
  1716. #else
  1717. "",
  1718. #endif
  1719. av_get_pix_fmt_name(dstFormat));
  1720. if (INLINE_MMXEXT(cpu_flags))
  1721. cpucaps = "MMXEXT";
  1722. else if (INLINE_AMD3DNOW(cpu_flags))
  1723. cpucaps = "3DNOW";
  1724. else if (INLINE_MMX(cpu_flags))
  1725. cpucaps = "MMX";
  1726. else if (PPC_ALTIVEC(cpu_flags))
  1727. cpucaps = "AltiVec";
  1728. else
  1729. cpucaps = "C";
  1730. av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
  1731. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  1732. av_log(c, AV_LOG_DEBUG,
  1733. "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1734. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  1735. av_log(c, AV_LOG_DEBUG,
  1736. "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  1737. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
  1738. c->chrXInc, c->chrYInc);
  1739. }
  1740. /* alpha blend special case, note this has been split via cascaded contexts if its scaled */
  1741. if (unscaled && !usesHFilter && !usesVFilter &&
  1742. c->alphablend != SWS_ALPHA_BLEND_NONE &&
  1743. isALPHA(srcFormat) &&
  1744. (c->srcRange == c->dstRange || isAnyRGB(dstFormat)) &&
  1745. alphaless_fmt(srcFormat) == dstFormat
  1746. ) {
  1747. c->convert_unscaled = ff_sws_alphablendaway;
  1748. if (flags & SWS_PRINT_INFO)
  1749. av_log(c, AV_LOG_INFO,
  1750. "using alpha blendaway %s -> %s special converter\n",
  1751. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  1752. return 0;
  1753. }
  1754. /* unscaled special cases */
  1755. if (unscaled && !usesHFilter && !usesVFilter &&
  1756. (c->srcRange == c->dstRange || isAnyRGB(dstFormat) ||
  1757. isFloat(srcFormat) || isFloat(dstFormat) || isBayer(srcFormat))){
  1758. ff_get_unscaled_swscale(c);
  1759. if (c->convert_unscaled) {
  1760. if (flags & SWS_PRINT_INFO)
  1761. av_log(c, AV_LOG_INFO,
  1762. "using unscaled %s -> %s special converter\n",
  1763. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  1764. return 0;
  1765. }
  1766. }
  1767. ff_sws_init_scale(c);
  1768. return ff_init_filters(c);
  1769. nomem:
  1770. ret = AVERROR(ENOMEM);
  1771. fail: // FIXME replace things by appropriate error codes
  1772. if (ret == RETCODE_USE_CASCADE) {
  1773. int tmpW = sqrt(srcW * (int64_t)dstW);
  1774. int tmpH = sqrt(srcH * (int64_t)dstH);
  1775. enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
  1776. if (isALPHA(srcFormat))
  1777. tmpFormat = AV_PIX_FMT_YUVA420P;
  1778. if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
  1779. return AVERROR(EINVAL);
  1780. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  1781. tmpW, tmpH, tmpFormat, 64);
  1782. if (ret < 0)
  1783. return ret;
  1784. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  1785. tmpW, tmpH, tmpFormat,
  1786. flags, srcFilter, NULL, c->param);
  1787. if (!c->cascaded_context[0])
  1788. return AVERROR(ENOMEM);
  1789. c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
  1790. dstW, dstH, dstFormat,
  1791. flags, NULL, dstFilter, c->param);
  1792. if (!c->cascaded_context[1])
  1793. return AVERROR(ENOMEM);
  1794. return 0;
  1795. }
  1796. return ret;
  1797. }
  1798. SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
  1799. int dstW, int dstH, enum AVPixelFormat dstFormat,
  1800. int flags, const double *param)
  1801. {
  1802. SwsContext *c;
  1803. if (!(c = sws_alloc_context()))
  1804. return NULL;
  1805. c->flags = flags;
  1806. c->srcW = srcW;
  1807. c->srcH = srcH;
  1808. c->dstW = dstW;
  1809. c->dstH = dstH;
  1810. c->srcFormat = srcFormat;
  1811. c->dstFormat = dstFormat;
  1812. if (param) {
  1813. c->param[0] = param[0];
  1814. c->param[1] = param[1];
  1815. }
  1816. return c;
  1817. }
  1818. SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
  1819. int dstW, int dstH, enum AVPixelFormat dstFormat,
  1820. int flags, SwsFilter *srcFilter,
  1821. SwsFilter *dstFilter, const double *param)
  1822. {
  1823. SwsContext *c;
  1824. c = sws_alloc_set_opts(srcW, srcH, srcFormat,
  1825. dstW, dstH, dstFormat,
  1826. flags, param);
  1827. if (!c)
  1828. return NULL;
  1829. if (sws_init_context(c, srcFilter, dstFilter) < 0) {
  1830. sws_freeContext(c);
  1831. return NULL;
  1832. }
  1833. return c;
  1834. }
  1835. static int isnan_vec(SwsVector *a)
  1836. {
  1837. int i;
  1838. for (i=0; i<a->length; i++)
  1839. if (isnan(a->coeff[i]))
  1840. return 1;
  1841. return 0;
  1842. }
  1843. static void makenan_vec(SwsVector *a)
  1844. {
  1845. int i;
  1846. for (i=0; i<a->length; i++)
  1847. a->coeff[i] = NAN;
  1848. }
  1849. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  1850. float lumaSharpen, float chromaSharpen,
  1851. float chromaHShift, float chromaVShift,
  1852. int verbose)
  1853. {
  1854. SwsFilter *filter = av_malloc(sizeof(SwsFilter));
  1855. if (!filter)
  1856. return NULL;
  1857. if (lumaGBlur != 0.0) {
  1858. filter->lumH = sws_getGaussianVec(lumaGBlur, 3.0);
  1859. filter->lumV = sws_getGaussianVec(lumaGBlur, 3.0);
  1860. } else {
  1861. filter->lumH = sws_getIdentityVec();
  1862. filter->lumV = sws_getIdentityVec();
  1863. }
  1864. if (chromaGBlur != 0.0) {
  1865. filter->chrH = sws_getGaussianVec(chromaGBlur, 3.0);
  1866. filter->chrV = sws_getGaussianVec(chromaGBlur, 3.0);
  1867. } else {
  1868. filter->chrH = sws_getIdentityVec();
  1869. filter->chrV = sws_getIdentityVec();
  1870. }
  1871. if (!filter->lumH || !filter->lumV || !filter->chrH || !filter->chrV)
  1872. goto fail;
  1873. if (chromaSharpen != 0.0) {
  1874. SwsVector *id = sws_getIdentityVec();
  1875. if (!id)
  1876. goto fail;
  1877. sws_scaleVec(filter->chrH, -chromaSharpen);
  1878. sws_scaleVec(filter->chrV, -chromaSharpen);
  1879. sws_addVec(filter->chrH, id);
  1880. sws_addVec(filter->chrV, id);
  1881. sws_freeVec(id);
  1882. }
  1883. if (lumaSharpen != 0.0) {
  1884. SwsVector *id = sws_getIdentityVec();
  1885. if (!id)
  1886. goto fail;
  1887. sws_scaleVec(filter->lumH, -lumaSharpen);
  1888. sws_scaleVec(filter->lumV, -lumaSharpen);
  1889. sws_addVec(filter->lumH, id);
  1890. sws_addVec(filter->lumV, id);
  1891. sws_freeVec(id);
  1892. }
  1893. if (chromaHShift != 0.0)
  1894. sws_shiftVec(filter->chrH, (int)(chromaHShift + 0.5));
  1895. if (chromaVShift != 0.0)
  1896. sws_shiftVec(filter->chrV, (int)(chromaVShift + 0.5));
  1897. sws_normalizeVec(filter->chrH, 1.0);
  1898. sws_normalizeVec(filter->chrV, 1.0);
  1899. sws_normalizeVec(filter->lumH, 1.0);
  1900. sws_normalizeVec(filter->lumV, 1.0);
  1901. if (isnan_vec(filter->chrH) ||
  1902. isnan_vec(filter->chrV) ||
  1903. isnan_vec(filter->lumH) ||
  1904. isnan_vec(filter->lumV))
  1905. goto fail;
  1906. if (verbose)
  1907. sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
  1908. if (verbose)
  1909. sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
  1910. return filter;
  1911. fail:
  1912. sws_freeVec(filter->lumH);
  1913. sws_freeVec(filter->lumV);
  1914. sws_freeVec(filter->chrH);
  1915. sws_freeVec(filter->chrV);
  1916. av_freep(&filter);
  1917. return NULL;
  1918. }
  1919. SwsVector *sws_allocVec(int length)
  1920. {
  1921. SwsVector *vec;
  1922. if(length <= 0 || length > INT_MAX/ sizeof(double))
  1923. return NULL;
  1924. vec = av_malloc(sizeof(SwsVector));
  1925. if (!vec)
  1926. return NULL;
  1927. vec->length = length;
  1928. vec->coeff = av_malloc(sizeof(double) * length);
  1929. if (!vec->coeff)
  1930. av_freep(&vec);
  1931. return vec;
  1932. }
  1933. SwsVector *sws_getGaussianVec(double variance, double quality)
  1934. {
  1935. const int length = (int)(variance * quality + 0.5) | 1;
  1936. int i;
  1937. double middle = (length - 1) * 0.5;
  1938. SwsVector *vec;
  1939. if(variance < 0 || quality < 0)
  1940. return NULL;
  1941. vec = sws_allocVec(length);
  1942. if (!vec)
  1943. return NULL;
  1944. for (i = 0; i < length; i++) {
  1945. double dist = i - middle;
  1946. vec->coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
  1947. sqrt(2 * variance * M_PI);
  1948. }
  1949. sws_normalizeVec(vec, 1.0);
  1950. return vec;
  1951. }
  1952. /**
  1953. * Allocate and return a vector with length coefficients, all
  1954. * with the same value c.
  1955. */
  1956. static
  1957. SwsVector *sws_getConstVec(double c, int length)
  1958. {
  1959. int i;
  1960. SwsVector *vec = sws_allocVec(length);
  1961. if (!vec)
  1962. return NULL;
  1963. for (i = 0; i < length; i++)
  1964. vec->coeff[i] = c;
  1965. return vec;
  1966. }
  1967. /**
  1968. * Allocate and return a vector with just one coefficient, with
  1969. * value 1.0.
  1970. */
  1971. static
  1972. SwsVector *sws_getIdentityVec(void)
  1973. {
  1974. return sws_getConstVec(1.0, 1);
  1975. }
  1976. static double sws_dcVec(SwsVector *a)
  1977. {
  1978. int i;
  1979. double sum = 0;
  1980. for (i = 0; i < a->length; i++)
  1981. sum += a->coeff[i];
  1982. return sum;
  1983. }
  1984. void sws_scaleVec(SwsVector *a, double scalar)
  1985. {
  1986. int i;
  1987. for (i = 0; i < a->length; i++)
  1988. a->coeff[i] *= scalar;
  1989. }
  1990. void sws_normalizeVec(SwsVector *a, double height)
  1991. {
  1992. sws_scaleVec(a, height / sws_dcVec(a));
  1993. }
  1994. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
  1995. {
  1996. int length = FFMAX(a->length, b->length);
  1997. int i;
  1998. SwsVector *vec = sws_getConstVec(0.0, length);
  1999. if (!vec)
  2000. return NULL;
  2001. for (i = 0; i < a->length; i++)
  2002. vec->coeff[i + (length - 1) / 2 - (a->length - 1) / 2] += a->coeff[i];
  2003. for (i = 0; i < b->length; i++)
  2004. vec->coeff[i + (length - 1) / 2 - (b->length - 1) / 2] += b->coeff[i];
  2005. return vec;
  2006. }
  2007. /* shift left / or right if "shift" is negative */
  2008. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
  2009. {
  2010. int length = a->length + FFABS(shift) * 2;
  2011. int i;
  2012. SwsVector *vec = sws_getConstVec(0.0, length);
  2013. if (!vec)
  2014. return NULL;
  2015. for (i = 0; i < a->length; i++) {
  2016. vec->coeff[i + (length - 1) / 2 -
  2017. (a->length - 1) / 2 - shift] = a->coeff[i];
  2018. }
  2019. return vec;
  2020. }
  2021. static
  2022. void sws_shiftVec(SwsVector *a, int shift)
  2023. {
  2024. SwsVector *shifted = sws_getShiftedVec(a, shift);
  2025. if (!shifted) {
  2026. makenan_vec(a);
  2027. return;
  2028. }
  2029. av_free(a->coeff);
  2030. a->coeff = shifted->coeff;
  2031. a->length = shifted->length;
  2032. av_free(shifted);
  2033. }
  2034. static
  2035. void sws_addVec(SwsVector *a, SwsVector *b)
  2036. {
  2037. SwsVector *sum = sws_sumVec(a, b);
  2038. if (!sum) {
  2039. makenan_vec(a);
  2040. return;
  2041. }
  2042. av_free(a->coeff);
  2043. a->coeff = sum->coeff;
  2044. a->length = sum->length;
  2045. av_free(sum);
  2046. }
  2047. /**
  2048. * Print with av_log() a textual representation of the vector a
  2049. * if log_level <= av_log_level.
  2050. */
  2051. static
  2052. void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
  2053. {
  2054. int i;
  2055. double max = 0;
  2056. double min = 0;
  2057. double range;
  2058. for (i = 0; i < a->length; i++)
  2059. if (a->coeff[i] > max)
  2060. max = a->coeff[i];
  2061. for (i = 0; i < a->length; i++)
  2062. if (a->coeff[i] < min)
  2063. min = a->coeff[i];
  2064. range = max - min;
  2065. for (i = 0; i < a->length; i++) {
  2066. int x = (int)((a->coeff[i] - min) * 60.0 / range + 0.5);
  2067. av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
  2068. for (; x > 0; x--)
  2069. av_log(log_ctx, log_level, " ");
  2070. av_log(log_ctx, log_level, "|\n");
  2071. }
  2072. }
  2073. void sws_freeVec(SwsVector *a)
  2074. {
  2075. if (!a)
  2076. return;
  2077. av_freep(&a->coeff);
  2078. a->length = 0;
  2079. av_free(a);
  2080. }
  2081. void sws_freeFilter(SwsFilter *filter)
  2082. {
  2083. if (!filter)
  2084. return;
  2085. sws_freeVec(filter->lumH);
  2086. sws_freeVec(filter->lumV);
  2087. sws_freeVec(filter->chrH);
  2088. sws_freeVec(filter->chrV);
  2089. av_free(filter);
  2090. }
  2091. void sws_freeContext(SwsContext *c)
  2092. {
  2093. int i;
  2094. if (!c)
  2095. return;
  2096. for (i = 0; i < c->nb_slice_ctx; i++)
  2097. sws_freeContext(c->slice_ctx[i]);
  2098. av_freep(&c->slice_ctx);
  2099. av_freep(&c->slice_err);
  2100. avpriv_slicethread_free(&c->slicethread);
  2101. for (i = 0; i < 4; i++)
  2102. av_freep(&c->dither_error[i]);
  2103. av_frame_free(&c->frame_src);
  2104. av_frame_free(&c->frame_dst);
  2105. av_freep(&c->src_ranges.ranges);
  2106. av_freep(&c->vLumFilter);
  2107. av_freep(&c->vChrFilter);
  2108. av_freep(&c->hLumFilter);
  2109. av_freep(&c->hChrFilter);
  2110. #if HAVE_ALTIVEC
  2111. av_freep(&c->vYCoeffsBank);
  2112. av_freep(&c->vCCoeffsBank);
  2113. #endif
  2114. av_freep(&c->vLumFilterPos);
  2115. av_freep(&c->vChrFilterPos);
  2116. av_freep(&c->hLumFilterPos);
  2117. av_freep(&c->hChrFilterPos);
  2118. #if HAVE_MMX_INLINE
  2119. #if USE_MMAP
  2120. if (c->lumMmxextFilterCode)
  2121. munmap(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize);
  2122. if (c->chrMmxextFilterCode)
  2123. munmap(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize);
  2124. #elif HAVE_VIRTUALALLOC
  2125. if (c->lumMmxextFilterCode)
  2126. VirtualFree(c->lumMmxextFilterCode, 0, MEM_RELEASE);
  2127. if (c->chrMmxextFilterCode)
  2128. VirtualFree(c->chrMmxextFilterCode, 0, MEM_RELEASE);
  2129. #else
  2130. av_free(c->lumMmxextFilterCode);
  2131. av_free(c->chrMmxextFilterCode);
  2132. #endif
  2133. c->lumMmxextFilterCode = NULL;
  2134. c->chrMmxextFilterCode = NULL;
  2135. #endif /* HAVE_MMX_INLINE */
  2136. av_freep(&c->yuvTable);
  2137. av_freep(&c->formatConvBuffer);
  2138. sws_freeContext(c->cascaded_context[0]);
  2139. sws_freeContext(c->cascaded_context[1]);
  2140. sws_freeContext(c->cascaded_context[2]);
  2141. memset(c->cascaded_context, 0, sizeof(c->cascaded_context));
  2142. av_freep(&c->cascaded_tmp[0]);
  2143. av_freep(&c->cascaded1_tmp[0]);
  2144. av_freep(&c->gamma);
  2145. av_freep(&c->inv_gamma);
  2146. av_freep(&c->rgb0_scratch);
  2147. av_freep(&c->xyz_scratch);
  2148. ff_free_filters(c);
  2149. av_free(c);
  2150. }
  2151. struct SwsContext *sws_getCachedContext(struct SwsContext *context, int srcW,
  2152. int srcH, enum AVPixelFormat srcFormat,
  2153. int dstW, int dstH,
  2154. enum AVPixelFormat dstFormat, int flags,
  2155. SwsFilter *srcFilter,
  2156. SwsFilter *dstFilter,
  2157. const double *param)
  2158. {
  2159. static const double default_param[2] = { SWS_PARAM_DEFAULT,
  2160. SWS_PARAM_DEFAULT };
  2161. int64_t src_h_chr_pos = -513, dst_h_chr_pos = -513,
  2162. src_v_chr_pos = -513, dst_v_chr_pos = -513;
  2163. if (!param)
  2164. param = default_param;
  2165. if (context &&
  2166. (context->srcW != srcW ||
  2167. context->srcH != srcH ||
  2168. context->srcFormat != srcFormat ||
  2169. context->dstW != dstW ||
  2170. context->dstH != dstH ||
  2171. context->dstFormat != dstFormat ||
  2172. context->flags != flags ||
  2173. context->param[0] != param[0] ||
  2174. context->param[1] != param[1])) {
  2175. av_opt_get_int(context, "src_h_chr_pos", 0, &src_h_chr_pos);
  2176. av_opt_get_int(context, "src_v_chr_pos", 0, &src_v_chr_pos);
  2177. av_opt_get_int(context, "dst_h_chr_pos", 0, &dst_h_chr_pos);
  2178. av_opt_get_int(context, "dst_v_chr_pos", 0, &dst_v_chr_pos);
  2179. sws_freeContext(context);
  2180. context = NULL;
  2181. }
  2182. if (!context) {
  2183. if (!(context = sws_alloc_context()))
  2184. return NULL;
  2185. context->srcW = srcW;
  2186. context->srcH = srcH;
  2187. context->srcFormat = srcFormat;
  2188. context->dstW = dstW;
  2189. context->dstH = dstH;
  2190. context->dstFormat = dstFormat;
  2191. context->flags = flags;
  2192. context->param[0] = param[0];
  2193. context->param[1] = param[1];
  2194. av_opt_set_int(context, "src_h_chr_pos", src_h_chr_pos, 0);
  2195. av_opt_set_int(context, "src_v_chr_pos", src_v_chr_pos, 0);
  2196. av_opt_set_int(context, "dst_h_chr_pos", dst_h_chr_pos, 0);
  2197. av_opt_set_int(context, "dst_v_chr_pos", dst_v_chr_pos, 0);
  2198. if (sws_init_context(context, srcFilter, dstFilter) < 0) {
  2199. sws_freeContext(context);
  2200. return NULL;
  2201. }
  2202. }
  2203. return context;
  2204. }
  2205. int ff_range_add(RangeList *rl, unsigned int start, unsigned int len)
  2206. {
  2207. Range *tmp;
  2208. unsigned int idx;
  2209. /* find the first existing range after the new one */
  2210. for (idx = 0; idx < rl->nb_ranges; idx++)
  2211. if (rl->ranges[idx].start > start)
  2212. break;
  2213. /* check for overlap */
  2214. if (idx > 0) {
  2215. Range *prev = &rl->ranges[idx - 1];
  2216. if (prev->start + prev->len > start)
  2217. return AVERROR(EINVAL);
  2218. }
  2219. if (idx < rl->nb_ranges) {
  2220. Range *next = &rl->ranges[idx];
  2221. if (start + len > next->start)
  2222. return AVERROR(EINVAL);
  2223. }
  2224. tmp = av_fast_realloc(rl->ranges, &rl->ranges_allocated,
  2225. (rl->nb_ranges + 1) * sizeof(*rl->ranges));
  2226. if (!tmp)
  2227. return AVERROR(ENOMEM);
  2228. rl->ranges = tmp;
  2229. memmove(rl->ranges + idx + 1, rl->ranges + idx,
  2230. sizeof(*rl->ranges) * (rl->nb_ranges - idx));
  2231. rl->ranges[idx].start = start;
  2232. rl->ranges[idx].len = len;
  2233. rl->nb_ranges++;
  2234. /* merge ranges */
  2235. if (idx > 0) {
  2236. Range *prev = &rl->ranges[idx - 1];
  2237. Range *cur = &rl->ranges[idx];
  2238. if (prev->start + prev->len == cur->start) {
  2239. prev->len += cur->len;
  2240. memmove(rl->ranges + idx - 1, rl->ranges + idx,
  2241. sizeof(*rl->ranges) * (rl->nb_ranges - idx));
  2242. rl->nb_ranges--;
  2243. idx--;
  2244. }
  2245. }
  2246. if (idx < rl->nb_ranges - 1) {
  2247. Range *cur = &rl->ranges[idx];
  2248. Range *next = &rl->ranges[idx + 1];
  2249. if (cur->start + cur->len == next->start) {
  2250. cur->len += next->len;
  2251. memmove(rl->ranges + idx, rl->ranges + idx + 1,
  2252. sizeof(*rl->ranges) * (rl->nb_ranges - idx - 1));
  2253. rl->nb_ranges--;
  2254. }
  2255. }
  2256. return 0;
  2257. }