utils.c 94 KB

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