imgconvert.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003
  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/imgconvert.c
  23. * misc image conversion routines
  24. */
  25. /* TODO:
  26. * - write 'ffimg' program to test all the image related stuff
  27. * - move all api to slice based system
  28. * - integrate deinterlacing, postprocessing and scaling in the conversion process
  29. */
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "colorspace.h"
  33. #if HAVE_MMX
  34. #include "x86/mmx.h"
  35. #include "x86/dsputil_mmx.h"
  36. #endif
  37. #define xglue(x, y) x ## y
  38. #define glue(x, y) xglue(x, y)
  39. #define FF_COLOR_RGB 0 /**< RGB color space */
  40. #define FF_COLOR_GRAY 1 /**< gray color space */
  41. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  42. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  43. #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
  44. #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
  45. #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
  46. typedef struct PixFmtInfo {
  47. const char *name;
  48. uint8_t nb_channels; /**< number of channels (including alpha) */
  49. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  50. uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
  51. uint8_t is_alpha : 1; /**< true if alpha can be specified */
  52. uint8_t is_hwaccel : 1; /**< true if this is an HW accelerated format */
  53. uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
  54. uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
  55. uint8_t depth; /**< bit depth of the color components */
  56. } PixFmtInfo;
  57. /* this table gives more information about formats */
  58. static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  59. /* YUV formats */
  60. [PIX_FMT_YUV420P] = {
  61. .name = "yuv420p",
  62. .nb_channels = 3,
  63. .color_type = FF_COLOR_YUV,
  64. .pixel_type = FF_PIXEL_PLANAR,
  65. .depth = 8,
  66. .x_chroma_shift = 1, .y_chroma_shift = 1,
  67. },
  68. [PIX_FMT_YUV422P] = {
  69. .name = "yuv422p",
  70. .nb_channels = 3,
  71. .color_type = FF_COLOR_YUV,
  72. .pixel_type = FF_PIXEL_PLANAR,
  73. .depth = 8,
  74. .x_chroma_shift = 1, .y_chroma_shift = 0,
  75. },
  76. [PIX_FMT_YUV444P] = {
  77. .name = "yuv444p",
  78. .nb_channels = 3,
  79. .color_type = FF_COLOR_YUV,
  80. .pixel_type = FF_PIXEL_PLANAR,
  81. .depth = 8,
  82. .x_chroma_shift = 0, .y_chroma_shift = 0,
  83. },
  84. [PIX_FMT_YUYV422] = {
  85. .name = "yuyv422",
  86. .nb_channels = 1,
  87. .color_type = FF_COLOR_YUV,
  88. .pixel_type = FF_PIXEL_PACKED,
  89. .depth = 8,
  90. .x_chroma_shift = 1, .y_chroma_shift = 0,
  91. },
  92. [PIX_FMT_UYVY422] = {
  93. .name = "uyvy422",
  94. .nb_channels = 1,
  95. .color_type = FF_COLOR_YUV,
  96. .pixel_type = FF_PIXEL_PACKED,
  97. .depth = 8,
  98. .x_chroma_shift = 1, .y_chroma_shift = 0,
  99. },
  100. [PIX_FMT_YUV410P] = {
  101. .name = "yuv410p",
  102. .nb_channels = 3,
  103. .color_type = FF_COLOR_YUV,
  104. .pixel_type = FF_PIXEL_PLANAR,
  105. .depth = 8,
  106. .x_chroma_shift = 2, .y_chroma_shift = 2,
  107. },
  108. [PIX_FMT_YUV411P] = {
  109. .name = "yuv411p",
  110. .nb_channels = 3,
  111. .color_type = FF_COLOR_YUV,
  112. .pixel_type = FF_PIXEL_PLANAR,
  113. .depth = 8,
  114. .x_chroma_shift = 2, .y_chroma_shift = 0,
  115. },
  116. [PIX_FMT_YUV440P] = {
  117. .name = "yuv440p",
  118. .nb_channels = 3,
  119. .color_type = FF_COLOR_YUV,
  120. .pixel_type = FF_PIXEL_PLANAR,
  121. .depth = 8,
  122. .x_chroma_shift = 0, .y_chroma_shift = 1,
  123. },
  124. /* YUV formats with alpha plane */
  125. [PIX_FMT_YUVA420P] = {
  126. .name = "yuva420p",
  127. .nb_channels = 4,
  128. .color_type = FF_COLOR_YUV,
  129. .pixel_type = FF_PIXEL_PLANAR,
  130. .depth = 8,
  131. .x_chroma_shift = 1, .y_chroma_shift = 1,
  132. },
  133. /* JPEG YUV */
  134. [PIX_FMT_YUVJ420P] = {
  135. .name = "yuvj420p",
  136. .nb_channels = 3,
  137. .color_type = FF_COLOR_YUV_JPEG,
  138. .pixel_type = FF_PIXEL_PLANAR,
  139. .depth = 8,
  140. .x_chroma_shift = 1, .y_chroma_shift = 1,
  141. },
  142. [PIX_FMT_YUVJ422P] = {
  143. .name = "yuvj422p",
  144. .nb_channels = 3,
  145. .color_type = FF_COLOR_YUV_JPEG,
  146. .pixel_type = FF_PIXEL_PLANAR,
  147. .depth = 8,
  148. .x_chroma_shift = 1, .y_chroma_shift = 0,
  149. },
  150. [PIX_FMT_YUVJ444P] = {
  151. .name = "yuvj444p",
  152. .nb_channels = 3,
  153. .color_type = FF_COLOR_YUV_JPEG,
  154. .pixel_type = FF_PIXEL_PLANAR,
  155. .depth = 8,
  156. .x_chroma_shift = 0, .y_chroma_shift = 0,
  157. },
  158. [PIX_FMT_YUVJ440P] = {
  159. .name = "yuvj440p",
  160. .nb_channels = 3,
  161. .color_type = FF_COLOR_YUV_JPEG,
  162. .pixel_type = FF_PIXEL_PLANAR,
  163. .depth = 8,
  164. .x_chroma_shift = 0, .y_chroma_shift = 1,
  165. },
  166. /* RGB formats */
  167. [PIX_FMT_RGB24] = {
  168. .name = "rgb24",
  169. .nb_channels = 3,
  170. .color_type = FF_COLOR_RGB,
  171. .pixel_type = FF_PIXEL_PACKED,
  172. .depth = 8,
  173. .x_chroma_shift = 0, .y_chroma_shift = 0,
  174. },
  175. [PIX_FMT_BGR24] = {
  176. .name = "bgr24",
  177. .nb_channels = 3,
  178. .color_type = FF_COLOR_RGB,
  179. .pixel_type = FF_PIXEL_PACKED,
  180. .depth = 8,
  181. .x_chroma_shift = 0, .y_chroma_shift = 0,
  182. },
  183. [PIX_FMT_RGB32] = {
  184. .name = "rgb32",
  185. .nb_channels = 4, .is_alpha = 1,
  186. .color_type = FF_COLOR_RGB,
  187. .pixel_type = FF_PIXEL_PACKED,
  188. .depth = 8,
  189. .x_chroma_shift = 0, .y_chroma_shift = 0,
  190. },
  191. [PIX_FMT_RGB48BE] = {
  192. .name = "rgb48be",
  193. .nb_channels = 3,
  194. .color_type = FF_COLOR_RGB,
  195. .pixel_type = FF_PIXEL_PACKED,
  196. .depth = 16,
  197. .x_chroma_shift = 0, .y_chroma_shift = 0,
  198. },
  199. [PIX_FMT_RGB48LE] = {
  200. .name = "rgb48le",
  201. .nb_channels = 3,
  202. .color_type = FF_COLOR_RGB,
  203. .pixel_type = FF_PIXEL_PACKED,
  204. .depth = 16,
  205. .x_chroma_shift = 0, .y_chroma_shift = 0,
  206. },
  207. [PIX_FMT_RGB565] = {
  208. .name = "rgb565",
  209. .nb_channels = 3,
  210. .color_type = FF_COLOR_RGB,
  211. .pixel_type = FF_PIXEL_PACKED,
  212. .depth = 5,
  213. .x_chroma_shift = 0, .y_chroma_shift = 0,
  214. },
  215. [PIX_FMT_RGB555] = {
  216. .name = "rgb555",
  217. .nb_channels = 3,
  218. .color_type = FF_COLOR_RGB,
  219. .pixel_type = FF_PIXEL_PACKED,
  220. .depth = 5,
  221. .x_chroma_shift = 0, .y_chroma_shift = 0,
  222. },
  223. /* gray / mono formats */
  224. [PIX_FMT_GRAY16BE] = {
  225. .name = "gray16be",
  226. .nb_channels = 1,
  227. .color_type = FF_COLOR_GRAY,
  228. .pixel_type = FF_PIXEL_PLANAR,
  229. .depth = 16,
  230. },
  231. [PIX_FMT_GRAY16LE] = {
  232. .name = "gray16le",
  233. .nb_channels = 1,
  234. .color_type = FF_COLOR_GRAY,
  235. .pixel_type = FF_PIXEL_PLANAR,
  236. .depth = 16,
  237. },
  238. [PIX_FMT_GRAY8] = {
  239. .name = "gray",
  240. .nb_channels = 1,
  241. .color_type = FF_COLOR_GRAY,
  242. .pixel_type = FF_PIXEL_PLANAR,
  243. .depth = 8,
  244. },
  245. [PIX_FMT_MONOWHITE] = {
  246. .name = "monow",
  247. .nb_channels = 1,
  248. .color_type = FF_COLOR_GRAY,
  249. .pixel_type = FF_PIXEL_PLANAR,
  250. .depth = 1,
  251. },
  252. [PIX_FMT_MONOBLACK] = {
  253. .name = "monob",
  254. .nb_channels = 1,
  255. .color_type = FF_COLOR_GRAY,
  256. .pixel_type = FF_PIXEL_PLANAR,
  257. .depth = 1,
  258. },
  259. /* paletted formats */
  260. [PIX_FMT_PAL8] = {
  261. .name = "pal8",
  262. .nb_channels = 4, .is_alpha = 1,
  263. .color_type = FF_COLOR_RGB,
  264. .pixel_type = FF_PIXEL_PALETTE,
  265. .depth = 8,
  266. },
  267. [PIX_FMT_XVMC_MPEG2_MC] = {
  268. .name = "xvmcmc",
  269. .is_hwaccel = 1,
  270. },
  271. [PIX_FMT_XVMC_MPEG2_IDCT] = {
  272. .name = "xvmcidct",
  273. .is_hwaccel = 1,
  274. },
  275. [PIX_FMT_VDPAU_MPEG1] = {
  276. .name = "vdpau_mpeg1",
  277. .is_hwaccel = 1,
  278. },
  279. [PIX_FMT_VDPAU_MPEG2] = {
  280. .name = "vdpau_mpeg2",
  281. .is_hwaccel = 1,
  282. },
  283. [PIX_FMT_VDPAU_H264] = {
  284. .name = "vdpau_h264",
  285. .is_hwaccel = 1,
  286. },
  287. [PIX_FMT_VDPAU_WMV3] = {
  288. .name = "vdpau_wmv3",
  289. .is_hwaccel = 1,
  290. },
  291. [PIX_FMT_VDPAU_VC1] = {
  292. .name = "vdpau_vc1",
  293. .is_hwaccel = 1,
  294. },
  295. [PIX_FMT_UYYVYY411] = {
  296. .name = "uyyvyy411",
  297. .nb_channels = 1,
  298. .color_type = FF_COLOR_YUV,
  299. .pixel_type = FF_PIXEL_PACKED,
  300. .depth = 8,
  301. .x_chroma_shift = 2, .y_chroma_shift = 0,
  302. },
  303. [PIX_FMT_BGR32] = {
  304. .name = "bgr32",
  305. .nb_channels = 4, .is_alpha = 1,
  306. .color_type = FF_COLOR_RGB,
  307. .pixel_type = FF_PIXEL_PACKED,
  308. .depth = 8,
  309. .x_chroma_shift = 0, .y_chroma_shift = 0,
  310. },
  311. [PIX_FMT_BGR565] = {
  312. .name = "bgr565",
  313. .nb_channels = 3,
  314. .color_type = FF_COLOR_RGB,
  315. .pixel_type = FF_PIXEL_PACKED,
  316. .depth = 5,
  317. .x_chroma_shift = 0, .y_chroma_shift = 0,
  318. },
  319. [PIX_FMT_BGR555] = {
  320. .name = "bgr555",
  321. .nb_channels = 3,
  322. .color_type = FF_COLOR_RGB,
  323. .pixel_type = FF_PIXEL_PACKED,
  324. .depth = 5,
  325. .x_chroma_shift = 0, .y_chroma_shift = 0,
  326. },
  327. [PIX_FMT_RGB8] = {
  328. .name = "rgb8",
  329. .nb_channels = 1,
  330. .color_type = FF_COLOR_RGB,
  331. .pixel_type = FF_PIXEL_PACKED,
  332. .depth = 8,
  333. .x_chroma_shift = 0, .y_chroma_shift = 0,
  334. },
  335. [PIX_FMT_RGB4] = {
  336. .name = "rgb4",
  337. .nb_channels = 1,
  338. .color_type = FF_COLOR_RGB,
  339. .pixel_type = FF_PIXEL_PACKED,
  340. .depth = 4,
  341. .x_chroma_shift = 0, .y_chroma_shift = 0,
  342. },
  343. [PIX_FMT_RGB4_BYTE] = {
  344. .name = "rgb4_byte",
  345. .nb_channels = 1,
  346. .color_type = FF_COLOR_RGB,
  347. .pixel_type = FF_PIXEL_PACKED,
  348. .depth = 8,
  349. .x_chroma_shift = 0, .y_chroma_shift = 0,
  350. },
  351. [PIX_FMT_BGR8] = {
  352. .name = "bgr8",
  353. .nb_channels = 1,
  354. .color_type = FF_COLOR_RGB,
  355. .pixel_type = FF_PIXEL_PACKED,
  356. .depth = 8,
  357. .x_chroma_shift = 0, .y_chroma_shift = 0,
  358. },
  359. [PIX_FMT_BGR4] = {
  360. .name = "bgr4",
  361. .nb_channels = 1,
  362. .color_type = FF_COLOR_RGB,
  363. .pixel_type = FF_PIXEL_PACKED,
  364. .depth = 4,
  365. .x_chroma_shift = 0, .y_chroma_shift = 0,
  366. },
  367. [PIX_FMT_BGR4_BYTE] = {
  368. .name = "bgr4_byte",
  369. .nb_channels = 1,
  370. .color_type = FF_COLOR_RGB,
  371. .pixel_type = FF_PIXEL_PACKED,
  372. .depth = 8,
  373. .x_chroma_shift = 0, .y_chroma_shift = 0,
  374. },
  375. [PIX_FMT_NV12] = {
  376. .name = "nv12",
  377. .nb_channels = 2,
  378. .color_type = FF_COLOR_YUV,
  379. .pixel_type = FF_PIXEL_PLANAR,
  380. .depth = 8,
  381. .x_chroma_shift = 1, .y_chroma_shift = 1,
  382. },
  383. [PIX_FMT_NV21] = {
  384. .name = "nv12",
  385. .nb_channels = 2,
  386. .color_type = FF_COLOR_YUV,
  387. .pixel_type = FF_PIXEL_PLANAR,
  388. .depth = 8,
  389. .x_chroma_shift = 1, .y_chroma_shift = 1,
  390. },
  391. [PIX_FMT_BGR32_1] = {
  392. .name = "bgr32_1",
  393. .nb_channels = 4, .is_alpha = 1,
  394. .color_type = FF_COLOR_RGB,
  395. .pixel_type = FF_PIXEL_PACKED,
  396. .depth = 8,
  397. .x_chroma_shift = 0, .y_chroma_shift = 0,
  398. },
  399. [PIX_FMT_RGB32_1] = {
  400. .name = "rgb32_1",
  401. .nb_channels = 4, .is_alpha = 1,
  402. .color_type = FF_COLOR_RGB,
  403. .pixel_type = FF_PIXEL_PACKED,
  404. .depth = 8,
  405. .x_chroma_shift = 0, .y_chroma_shift = 0,
  406. },
  407. /* VA API formats */
  408. [PIX_FMT_VAAPI_MOCO] = {
  409. .name = "vaapi_moco",
  410. .is_hwaccel = 1,
  411. },
  412. [PIX_FMT_VAAPI_IDCT] = {
  413. .name = "vaapi_idct",
  414. .is_hwaccel = 1,
  415. },
  416. [PIX_FMT_VAAPI_VLD] = {
  417. .name = "vaapi_vld",
  418. .is_hwaccel = 1,
  419. },
  420. };
  421. void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
  422. {
  423. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  424. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  425. }
  426. const char *avcodec_get_pix_fmt_name(int pix_fmt)
  427. {
  428. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  429. return NULL;
  430. else
  431. return pix_fmt_info[pix_fmt].name;
  432. }
  433. enum PixelFormat avcodec_get_pix_fmt(const char* name)
  434. {
  435. int i;
  436. for (i=0; i < PIX_FMT_NB; i++)
  437. if (!strcmp(pix_fmt_info[i].name, name))
  438. return i;
  439. return PIX_FMT_NONE;
  440. }
  441. void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
  442. {
  443. /* print header */
  444. if (pix_fmt < 0)
  445. snprintf (buf, buf_size,
  446. "name " " nb_channels" " depth" " is_alpha"
  447. );
  448. else{
  449. PixFmtInfo info= pix_fmt_info[pix_fmt];
  450. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  451. snprintf (buf, buf_size,
  452. "%-10s" " %1d " " %2d " " %c ",
  453. info.name,
  454. info.nb_channels,
  455. info.depth,
  456. is_alpha_char
  457. );
  458. }
  459. }
  460. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  461. {
  462. return pix_fmt_info[pix_fmt].is_hwaccel;
  463. }
  464. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  465. int i;
  466. for(i=0; i<256; i++){
  467. int r,g,b;
  468. switch(pix_fmt) {
  469. case PIX_FMT_RGB8:
  470. r= (i>>5 )*36;
  471. g= ((i>>2)&7)*36;
  472. b= (i&3 )*85;
  473. break;
  474. case PIX_FMT_BGR8:
  475. b= (i>>6 )*85;
  476. g= ((i>>3)&7)*36;
  477. r= (i&7 )*36;
  478. break;
  479. case PIX_FMT_RGB4_BYTE:
  480. r= (i>>3 )*255;
  481. g= ((i>>1)&3)*85;
  482. b= (i&1 )*255;
  483. break;
  484. case PIX_FMT_BGR4_BYTE:
  485. b= (i>>3 )*255;
  486. g= ((i>>1)&3)*85;
  487. r= (i&1 )*255;
  488. break;
  489. case PIX_FMT_GRAY8:
  490. r=b=g= i;
  491. break;
  492. default:
  493. return -1;
  494. }
  495. pal[i] = b + (g<<8) + (r<<16);
  496. }
  497. return 0;
  498. }
  499. int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
  500. {
  501. int w2;
  502. const PixFmtInfo *pinfo;
  503. memset(picture->linesize, 0, sizeof(picture->linesize));
  504. pinfo = &pix_fmt_info[pix_fmt];
  505. switch(pix_fmt) {
  506. case PIX_FMT_YUV420P:
  507. case PIX_FMT_YUV422P:
  508. case PIX_FMT_YUV444P:
  509. case PIX_FMT_YUV410P:
  510. case PIX_FMT_YUV411P:
  511. case PIX_FMT_YUV440P:
  512. case PIX_FMT_YUVJ420P:
  513. case PIX_FMT_YUVJ422P:
  514. case PIX_FMT_YUVJ444P:
  515. case PIX_FMT_YUVJ440P:
  516. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  517. picture->linesize[0] = width;
  518. picture->linesize[1] = w2;
  519. picture->linesize[2] = w2;
  520. break;
  521. case PIX_FMT_YUVA420P:
  522. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  523. picture->linesize[0] = width;
  524. picture->linesize[1] = w2;
  525. picture->linesize[2] = w2;
  526. picture->linesize[3] = width;
  527. break;
  528. case PIX_FMT_NV12:
  529. case PIX_FMT_NV21:
  530. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  531. picture->linesize[0] = width;
  532. picture->linesize[1] = w2;
  533. break;
  534. case PIX_FMT_RGB24:
  535. case PIX_FMT_BGR24:
  536. picture->linesize[0] = width * 3;
  537. break;
  538. case PIX_FMT_RGB32:
  539. case PIX_FMT_BGR32:
  540. case PIX_FMT_RGB32_1:
  541. case PIX_FMT_BGR32_1:
  542. picture->linesize[0] = width * 4;
  543. break;
  544. case PIX_FMT_RGB48BE:
  545. case PIX_FMT_RGB48LE:
  546. picture->linesize[0] = width * 6;
  547. break;
  548. case PIX_FMT_GRAY16BE:
  549. case PIX_FMT_GRAY16LE:
  550. case PIX_FMT_BGR555:
  551. case PIX_FMT_BGR565:
  552. case PIX_FMT_RGB555:
  553. case PIX_FMT_RGB565:
  554. case PIX_FMT_YUYV422:
  555. picture->linesize[0] = width * 2;
  556. break;
  557. case PIX_FMT_UYVY422:
  558. picture->linesize[0] = width * 2;
  559. break;
  560. case PIX_FMT_UYYVYY411:
  561. picture->linesize[0] = width + width/2;
  562. break;
  563. case PIX_FMT_RGB4:
  564. case PIX_FMT_BGR4:
  565. picture->linesize[0] = width / 2;
  566. break;
  567. case PIX_FMT_MONOWHITE:
  568. case PIX_FMT_MONOBLACK:
  569. picture->linesize[0] = (width + 7) >> 3;
  570. break;
  571. case PIX_FMT_PAL8:
  572. case PIX_FMT_RGB8:
  573. case PIX_FMT_BGR8:
  574. case PIX_FMT_RGB4_BYTE:
  575. case PIX_FMT_BGR4_BYTE:
  576. case PIX_FMT_GRAY8:
  577. picture->linesize[0] = width;
  578. picture->linesize[1] = 4;
  579. break;
  580. default:
  581. return -1;
  582. }
  583. return 0;
  584. }
  585. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
  586. int height)
  587. {
  588. int size, h2, size2;
  589. const PixFmtInfo *pinfo;
  590. pinfo = &pix_fmt_info[pix_fmt];
  591. size = picture->linesize[0] * height;
  592. switch(pix_fmt) {
  593. case PIX_FMT_YUV420P:
  594. case PIX_FMT_YUV422P:
  595. case PIX_FMT_YUV444P:
  596. case PIX_FMT_YUV410P:
  597. case PIX_FMT_YUV411P:
  598. case PIX_FMT_YUV440P:
  599. case PIX_FMT_YUVJ420P:
  600. case PIX_FMT_YUVJ422P:
  601. case PIX_FMT_YUVJ444P:
  602. case PIX_FMT_YUVJ440P:
  603. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  604. size2 = picture->linesize[1] * h2;
  605. picture->data[0] = ptr;
  606. picture->data[1] = picture->data[0] + size;
  607. picture->data[2] = picture->data[1] + size2;
  608. picture->data[3] = NULL;
  609. return size + 2 * size2;
  610. case PIX_FMT_YUVA420P:
  611. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  612. size2 = picture->linesize[1] * h2;
  613. picture->data[0] = ptr;
  614. picture->data[1] = picture->data[0] + size;
  615. picture->data[2] = picture->data[1] + size2;
  616. picture->data[3] = picture->data[1] + size2 + size2;
  617. return 2 * size + 2 * size2;
  618. case PIX_FMT_NV12:
  619. case PIX_FMT_NV21:
  620. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  621. size2 = picture->linesize[1] * h2 * 2;
  622. picture->data[0] = ptr;
  623. picture->data[1] = picture->data[0] + size;
  624. picture->data[2] = NULL;
  625. picture->data[3] = NULL;
  626. return size + 2 * size2;
  627. case PIX_FMT_RGB24:
  628. case PIX_FMT_BGR24:
  629. case PIX_FMT_RGB32:
  630. case PIX_FMT_BGR32:
  631. case PIX_FMT_RGB32_1:
  632. case PIX_FMT_BGR32_1:
  633. case PIX_FMT_RGB48BE:
  634. case PIX_FMT_RGB48LE:
  635. case PIX_FMT_GRAY16BE:
  636. case PIX_FMT_GRAY16LE:
  637. case PIX_FMT_BGR555:
  638. case PIX_FMT_BGR565:
  639. case PIX_FMT_RGB555:
  640. case PIX_FMT_RGB565:
  641. case PIX_FMT_YUYV422:
  642. case PIX_FMT_UYVY422:
  643. case PIX_FMT_UYYVYY411:
  644. case PIX_FMT_RGB4:
  645. case PIX_FMT_BGR4:
  646. case PIX_FMT_MONOWHITE:
  647. case PIX_FMT_MONOBLACK:
  648. picture->data[0] = ptr;
  649. picture->data[1] = NULL;
  650. picture->data[2] = NULL;
  651. picture->data[3] = NULL;
  652. return size;
  653. case PIX_FMT_PAL8:
  654. case PIX_FMT_RGB8:
  655. case PIX_FMT_BGR8:
  656. case PIX_FMT_RGB4_BYTE:
  657. case PIX_FMT_BGR4_BYTE:
  658. case PIX_FMT_GRAY8:
  659. size2 = (size + 3) & ~3;
  660. picture->data[0] = ptr;
  661. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  662. picture->data[2] = NULL;
  663. picture->data[3] = NULL;
  664. return size2 + 256 * 4;
  665. default:
  666. picture->data[0] = NULL;
  667. picture->data[1] = NULL;
  668. picture->data[2] = NULL;
  669. picture->data[3] = NULL;
  670. return -1;
  671. }
  672. }
  673. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  674. int pix_fmt, int width, int height)
  675. {
  676. if(avcodec_check_dimensions(NULL, width, height))
  677. return -1;
  678. if (ff_fill_linesize(picture, pix_fmt, width))
  679. return -1;
  680. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  681. }
  682. int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
  683. unsigned char *dest, int dest_size)
  684. {
  685. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  686. int i, j, w, ow, h, oh, data_planes;
  687. const unsigned char* s;
  688. int size = avpicture_get_size(pix_fmt, width, height);
  689. if (size > dest_size || size < 0)
  690. return -1;
  691. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  692. if (pix_fmt == PIX_FMT_YUYV422 ||
  693. pix_fmt == PIX_FMT_UYVY422 ||
  694. pix_fmt == PIX_FMT_BGR565 ||
  695. pix_fmt == PIX_FMT_BGR555 ||
  696. pix_fmt == PIX_FMT_RGB565 ||
  697. pix_fmt == PIX_FMT_RGB555)
  698. w = width * 2;
  699. else if (pix_fmt == PIX_FMT_UYYVYY411)
  700. w = width + width/2;
  701. else if (pix_fmt == PIX_FMT_PAL8)
  702. w = width;
  703. else
  704. w = width * (pf->depth * pf->nb_channels / 8);
  705. data_planes = 1;
  706. h = height;
  707. } else {
  708. data_planes = pf->nb_channels;
  709. w = (width*pf->depth + 7)/8;
  710. h = height;
  711. }
  712. ow = w;
  713. oh = h;
  714. for (i=0; i<data_planes; i++) {
  715. if (i == 1) {
  716. w = width >> pf->x_chroma_shift;
  717. h = height >> pf->y_chroma_shift;
  718. } else if (i == 3) {
  719. w = ow;
  720. h = oh;
  721. }
  722. s = src->data[i];
  723. for(j=0; j<h; j++) {
  724. memcpy(dest, s, w);
  725. dest += w;
  726. s += src->linesize[i];
  727. }
  728. }
  729. if (pf->pixel_type == FF_PIXEL_PALETTE)
  730. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  731. return size;
  732. }
  733. int avpicture_get_size(int pix_fmt, int width, int height)
  734. {
  735. AVPicture dummy_pict;
  736. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  737. }
  738. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  739. int has_alpha)
  740. {
  741. const PixFmtInfo *pf, *ps;
  742. int loss;
  743. ps = &pix_fmt_info[src_pix_fmt];
  744. pf = &pix_fmt_info[dst_pix_fmt];
  745. /* compute loss */
  746. loss = 0;
  747. pf = &pix_fmt_info[dst_pix_fmt];
  748. if (pf->depth < ps->depth ||
  749. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  750. loss |= FF_LOSS_DEPTH;
  751. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  752. pf->y_chroma_shift > ps->y_chroma_shift)
  753. loss |= FF_LOSS_RESOLUTION;
  754. switch(pf->color_type) {
  755. case FF_COLOR_RGB:
  756. if (ps->color_type != FF_COLOR_RGB &&
  757. ps->color_type != FF_COLOR_GRAY)
  758. loss |= FF_LOSS_COLORSPACE;
  759. break;
  760. case FF_COLOR_GRAY:
  761. if (ps->color_type != FF_COLOR_GRAY)
  762. loss |= FF_LOSS_COLORSPACE;
  763. break;
  764. case FF_COLOR_YUV:
  765. if (ps->color_type != FF_COLOR_YUV)
  766. loss |= FF_LOSS_COLORSPACE;
  767. break;
  768. case FF_COLOR_YUV_JPEG:
  769. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  770. ps->color_type != FF_COLOR_YUV &&
  771. ps->color_type != FF_COLOR_GRAY)
  772. loss |= FF_LOSS_COLORSPACE;
  773. break;
  774. default:
  775. /* fail safe test */
  776. if (ps->color_type != pf->color_type)
  777. loss |= FF_LOSS_COLORSPACE;
  778. break;
  779. }
  780. if (pf->color_type == FF_COLOR_GRAY &&
  781. ps->color_type != FF_COLOR_GRAY)
  782. loss |= FF_LOSS_CHROMA;
  783. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  784. loss |= FF_LOSS_ALPHA;
  785. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  786. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  787. loss |= FF_LOSS_COLORQUANT;
  788. return loss;
  789. }
  790. static int avg_bits_per_pixel(int pix_fmt)
  791. {
  792. int bits;
  793. const PixFmtInfo *pf;
  794. pf = &pix_fmt_info[pix_fmt];
  795. switch(pf->pixel_type) {
  796. case FF_PIXEL_PACKED:
  797. switch(pix_fmt) {
  798. case PIX_FMT_YUYV422:
  799. case PIX_FMT_UYVY422:
  800. case PIX_FMT_RGB565:
  801. case PIX_FMT_RGB555:
  802. case PIX_FMT_BGR565:
  803. case PIX_FMT_BGR555:
  804. bits = 16;
  805. break;
  806. case PIX_FMT_UYYVYY411:
  807. bits = 12;
  808. break;
  809. default:
  810. bits = pf->depth * pf->nb_channels;
  811. break;
  812. }
  813. break;
  814. case FF_PIXEL_PLANAR:
  815. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  816. bits = pf->depth * pf->nb_channels;
  817. } else {
  818. bits = pf->depth + ((2 * pf->depth) >>
  819. (pf->x_chroma_shift + pf->y_chroma_shift));
  820. }
  821. break;
  822. case FF_PIXEL_PALETTE:
  823. bits = 8;
  824. break;
  825. default:
  826. bits = -1;
  827. break;
  828. }
  829. return bits;
  830. }
  831. static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  832. int src_pix_fmt,
  833. int has_alpha,
  834. int loss_mask)
  835. {
  836. int dist, i, loss, min_dist, dst_pix_fmt;
  837. /* find exact color match with smallest size */
  838. dst_pix_fmt = -1;
  839. min_dist = 0x7fffffff;
  840. /* test only the first 64 pixel formats to avoid undefined behaviour */
  841. for (i = 0; i < 64; i++) {
  842. if (pix_fmt_mask & (1ULL << i)) {
  843. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  844. if (loss == 0) {
  845. dist = avg_bits_per_pixel(i);
  846. if (dist < min_dist) {
  847. min_dist = dist;
  848. dst_pix_fmt = i;
  849. }
  850. }
  851. }
  852. }
  853. return dst_pix_fmt;
  854. }
  855. int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
  856. int has_alpha, int *loss_ptr)
  857. {
  858. int dst_pix_fmt, loss_mask, i;
  859. static const int loss_mask_order[] = {
  860. ~0, /* no loss first */
  861. ~FF_LOSS_ALPHA,
  862. ~FF_LOSS_RESOLUTION,
  863. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  864. ~FF_LOSS_COLORQUANT,
  865. ~FF_LOSS_DEPTH,
  866. 0,
  867. };
  868. /* try with successive loss */
  869. i = 0;
  870. for(;;) {
  871. loss_mask = loss_mask_order[i++];
  872. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  873. has_alpha, loss_mask);
  874. if (dst_pix_fmt >= 0)
  875. goto found;
  876. if (loss_mask == 0)
  877. break;
  878. }
  879. return -1;
  880. found:
  881. if (loss_ptr)
  882. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  883. return dst_pix_fmt;
  884. }
  885. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  886. const uint8_t *src, int src_wrap,
  887. int width, int height)
  888. {
  889. if((!dst) || (!src))
  890. return;
  891. for(;height > 0; height--) {
  892. memcpy(dst, src, width);
  893. dst += dst_wrap;
  894. src += src_wrap;
  895. }
  896. }
  897. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  898. {
  899. int bits;
  900. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  901. pf = &pix_fmt_info[pix_fmt];
  902. switch(pf->pixel_type) {
  903. case FF_PIXEL_PACKED:
  904. switch(pix_fmt) {
  905. case PIX_FMT_YUYV422:
  906. case PIX_FMT_UYVY422:
  907. case PIX_FMT_RGB565:
  908. case PIX_FMT_RGB555:
  909. case PIX_FMT_BGR565:
  910. case PIX_FMT_BGR555:
  911. bits = 16;
  912. break;
  913. case PIX_FMT_UYYVYY411:
  914. bits = 12;
  915. break;
  916. default:
  917. bits = pf->depth * pf->nb_channels;
  918. break;
  919. }
  920. return (width * bits + 7) >> 3;
  921. break;
  922. case FF_PIXEL_PLANAR:
  923. if (plane == 1 || plane == 2)
  924. width= -((-width)>>pf->x_chroma_shift);
  925. return (width * pf->depth + 7) >> 3;
  926. break;
  927. case FF_PIXEL_PALETTE:
  928. if (plane == 0)
  929. return width;
  930. break;
  931. }
  932. return -1;
  933. }
  934. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  935. int pix_fmt, int width, int height)
  936. {
  937. int i;
  938. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  939. pf = &pix_fmt_info[pix_fmt];
  940. switch(pf->pixel_type) {
  941. case FF_PIXEL_PACKED:
  942. case FF_PIXEL_PLANAR:
  943. for(i = 0; i < pf->nb_channels; i++) {
  944. int h;
  945. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  946. h = height;
  947. if (i == 1 || i == 2) {
  948. h= -((-height)>>pf->y_chroma_shift);
  949. }
  950. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  951. src->data[i], src->linesize[i],
  952. bwidth, h);
  953. }
  954. break;
  955. case FF_PIXEL_PALETTE:
  956. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  957. src->data[0], src->linesize[0],
  958. width, height);
  959. /* copy the palette */
  960. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  961. src->data[1], src->linesize[1],
  962. 4, 256);
  963. break;
  964. }
  965. }
  966. /* XXX: totally non optimized */
  967. static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
  968. int width, int height)
  969. {
  970. const uint8_t *p, *p1;
  971. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  972. int w;
  973. p1 = src->data[0];
  974. lum1 = dst->data[0];
  975. cb1 = dst->data[1];
  976. cr1 = dst->data[2];
  977. for(;height >= 1; height -= 2) {
  978. p = p1;
  979. lum = lum1;
  980. cb = cb1;
  981. cr = cr1;
  982. for(w = width; w >= 2; w -= 2) {
  983. lum[0] = p[0];
  984. cb[0] = p[1];
  985. lum[1] = p[2];
  986. cr[0] = p[3];
  987. p += 4;
  988. lum += 2;
  989. cb++;
  990. cr++;
  991. }
  992. if (w) {
  993. lum[0] = p[0];
  994. cb[0] = p[1];
  995. cr[0] = p[3];
  996. cb++;
  997. cr++;
  998. }
  999. p1 += src->linesize[0];
  1000. lum1 += dst->linesize[0];
  1001. if (height>1) {
  1002. p = p1;
  1003. lum = lum1;
  1004. for(w = width; w >= 2; w -= 2) {
  1005. lum[0] = p[0];
  1006. lum[1] = p[2];
  1007. p += 4;
  1008. lum += 2;
  1009. }
  1010. if (w) {
  1011. lum[0] = p[0];
  1012. }
  1013. p1 += src->linesize[0];
  1014. lum1 += dst->linesize[0];
  1015. }
  1016. cb1 += dst->linesize[1];
  1017. cr1 += dst->linesize[2];
  1018. }
  1019. }
  1020. static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
  1021. int width, int height)
  1022. {
  1023. const uint8_t *p, *p1;
  1024. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1025. int w;
  1026. p1 = src->data[0];
  1027. lum1 = dst->data[0];
  1028. cb1 = dst->data[1];
  1029. cr1 = dst->data[2];
  1030. for(;height >= 1; height -= 2) {
  1031. p = p1;
  1032. lum = lum1;
  1033. cb = cb1;
  1034. cr = cr1;
  1035. for(w = width; w >= 2; w -= 2) {
  1036. lum[0] = p[1];
  1037. cb[0] = p[0];
  1038. lum[1] = p[3];
  1039. cr[0] = p[2];
  1040. p += 4;
  1041. lum += 2;
  1042. cb++;
  1043. cr++;
  1044. }
  1045. if (w) {
  1046. lum[0] = p[1];
  1047. cb[0] = p[0];
  1048. cr[0] = p[2];
  1049. cb++;
  1050. cr++;
  1051. }
  1052. p1 += src->linesize[0];
  1053. lum1 += dst->linesize[0];
  1054. if (height>1) {
  1055. p = p1;
  1056. lum = lum1;
  1057. for(w = width; w >= 2; w -= 2) {
  1058. lum[0] = p[1];
  1059. lum[1] = p[3];
  1060. p += 4;
  1061. lum += 2;
  1062. }
  1063. if (w) {
  1064. lum[0] = p[1];
  1065. }
  1066. p1 += src->linesize[0];
  1067. lum1 += dst->linesize[0];
  1068. }
  1069. cb1 += dst->linesize[1];
  1070. cr1 += dst->linesize[2];
  1071. }
  1072. }
  1073. static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
  1074. int width, int height)
  1075. {
  1076. const uint8_t *p, *p1;
  1077. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1078. int w;
  1079. p1 = src->data[0];
  1080. lum1 = dst->data[0];
  1081. cb1 = dst->data[1];
  1082. cr1 = dst->data[2];
  1083. for(;height > 0; height--) {
  1084. p = p1;
  1085. lum = lum1;
  1086. cb = cb1;
  1087. cr = cr1;
  1088. for(w = width; w >= 2; w -= 2) {
  1089. lum[0] = p[1];
  1090. cb[0] = p[0];
  1091. lum[1] = p[3];
  1092. cr[0] = p[2];
  1093. p += 4;
  1094. lum += 2;
  1095. cb++;
  1096. cr++;
  1097. }
  1098. p1 += src->linesize[0];
  1099. lum1 += dst->linesize[0];
  1100. cb1 += dst->linesize[1];
  1101. cr1 += dst->linesize[2];
  1102. }
  1103. }
  1104. static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
  1105. int width, int height)
  1106. {
  1107. const uint8_t *p, *p1;
  1108. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1109. int w;
  1110. p1 = src->data[0];
  1111. lum1 = dst->data[0];
  1112. cb1 = dst->data[1];
  1113. cr1 = dst->data[2];
  1114. for(;height > 0; height--) {
  1115. p = p1;
  1116. lum = lum1;
  1117. cb = cb1;
  1118. cr = cr1;
  1119. for(w = width; w >= 2; w -= 2) {
  1120. lum[0] = p[0];
  1121. cb[0] = p[1];
  1122. lum[1] = p[2];
  1123. cr[0] = p[3];
  1124. p += 4;
  1125. lum += 2;
  1126. cb++;
  1127. cr++;
  1128. }
  1129. p1 += src->linesize[0];
  1130. lum1 += dst->linesize[0];
  1131. cb1 += dst->linesize[1];
  1132. cr1 += dst->linesize[2];
  1133. }
  1134. }
  1135. static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
  1136. int width, int height)
  1137. {
  1138. uint8_t *p, *p1;
  1139. const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1140. int w;
  1141. p1 = dst->data[0];
  1142. lum1 = src->data[0];
  1143. cb1 = src->data[1];
  1144. cr1 = src->data[2];
  1145. for(;height > 0; height--) {
  1146. p = p1;
  1147. lum = lum1;
  1148. cb = cb1;
  1149. cr = cr1;
  1150. for(w = width; w >= 2; w -= 2) {
  1151. p[0] = lum[0];
  1152. p[1] = cb[0];
  1153. p[2] = lum[1];
  1154. p[3] = cr[0];
  1155. p += 4;
  1156. lum += 2;
  1157. cb++;
  1158. cr++;
  1159. }
  1160. p1 += dst->linesize[0];
  1161. lum1 += src->linesize[0];
  1162. cb1 += src->linesize[1];
  1163. cr1 += src->linesize[2];
  1164. }
  1165. }
  1166. static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
  1167. int width, int height)
  1168. {
  1169. uint8_t *p, *p1;
  1170. const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1171. int w;
  1172. p1 = dst->data[0];
  1173. lum1 = src->data[0];
  1174. cb1 = src->data[1];
  1175. cr1 = src->data[2];
  1176. for(;height > 0; height--) {
  1177. p = p1;
  1178. lum = lum1;
  1179. cb = cb1;
  1180. cr = cr1;
  1181. for(w = width; w >= 2; w -= 2) {
  1182. p[1] = lum[0];
  1183. p[0] = cb[0];
  1184. p[3] = lum[1];
  1185. p[2] = cr[0];
  1186. p += 4;
  1187. lum += 2;
  1188. cb++;
  1189. cr++;
  1190. }
  1191. p1 += dst->linesize[0];
  1192. lum1 += src->linesize[0];
  1193. cb1 += src->linesize[1];
  1194. cr1 += src->linesize[2];
  1195. }
  1196. }
  1197. static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
  1198. int width, int height)
  1199. {
  1200. const uint8_t *p, *p1;
  1201. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1202. int w;
  1203. p1 = src->data[0];
  1204. lum1 = dst->data[0];
  1205. cb1 = dst->data[1];
  1206. cr1 = dst->data[2];
  1207. for(;height > 0; height--) {
  1208. p = p1;
  1209. lum = lum1;
  1210. cb = cb1;
  1211. cr = cr1;
  1212. for(w = width; w >= 4; w -= 4) {
  1213. cb[0] = p[0];
  1214. lum[0] = p[1];
  1215. lum[1] = p[2];
  1216. cr[0] = p[3];
  1217. lum[2] = p[4];
  1218. lum[3] = p[5];
  1219. p += 6;
  1220. lum += 4;
  1221. cb++;
  1222. cr++;
  1223. }
  1224. p1 += src->linesize[0];
  1225. lum1 += dst->linesize[0];
  1226. cb1 += dst->linesize[1];
  1227. cr1 += dst->linesize[2];
  1228. }
  1229. }
  1230. static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
  1231. int width, int height)
  1232. {
  1233. int w, h;
  1234. uint8_t *line1, *line2, *linesrc = dst->data[0];
  1235. uint8_t *lum1, *lum2, *lumsrc = src->data[0];
  1236. uint8_t *cb1, *cb2 = src->data[1];
  1237. uint8_t *cr1, *cr2 = src->data[2];
  1238. for(h = height / 2; h--;) {
  1239. line1 = linesrc;
  1240. line2 = linesrc + dst->linesize[0];
  1241. lum1 = lumsrc;
  1242. lum2 = lumsrc + src->linesize[0];
  1243. cb1 = cb2;
  1244. cr1 = cr2;
  1245. for(w = width / 2; w--;) {
  1246. *line1++ = *lum1++; *line2++ = *lum2++;
  1247. *line1++ = *line2++ = *cb1++;
  1248. *line1++ = *lum1++; *line2++ = *lum2++;
  1249. *line1++ = *line2++ = *cr1++;
  1250. }
  1251. linesrc += dst->linesize[0] * 2;
  1252. lumsrc += src->linesize[0] * 2;
  1253. cb2 += src->linesize[1];
  1254. cr2 += src->linesize[2];
  1255. }
  1256. }
  1257. static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
  1258. int width, int height)
  1259. {
  1260. int w, h;
  1261. uint8_t *line1, *line2, *linesrc = dst->data[0];
  1262. uint8_t *lum1, *lum2, *lumsrc = src->data[0];
  1263. uint8_t *cb1, *cb2 = src->data[1];
  1264. uint8_t *cr1, *cr2 = src->data[2];
  1265. for(h = height / 2; h--;) {
  1266. line1 = linesrc;
  1267. line2 = linesrc + dst->linesize[0];
  1268. lum1 = lumsrc;
  1269. lum2 = lumsrc + src->linesize[0];
  1270. cb1 = cb2;
  1271. cr1 = cr2;
  1272. for(w = width / 2; w--;) {
  1273. *line1++ = *line2++ = *cb1++;
  1274. *line1++ = *lum1++; *line2++ = *lum2++;
  1275. *line1++ = *line2++ = *cr1++;
  1276. *line1++ = *lum1++; *line2++ = *lum2++;
  1277. }
  1278. linesrc += dst->linesize[0] * 2;
  1279. lumsrc += src->linesize[0] * 2;
  1280. cb2 += src->linesize[1];
  1281. cr2 += src->linesize[2];
  1282. }
  1283. }
  1284. /* 2x2 -> 1x1 */
  1285. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1286. const uint8_t *src, int src_wrap,
  1287. int width, int height)
  1288. {
  1289. int w;
  1290. const uint8_t *s1, *s2;
  1291. uint8_t *d;
  1292. for(;height > 0; height--) {
  1293. s1 = src;
  1294. s2 = s1 + src_wrap;
  1295. d = dst;
  1296. for(w = width;w >= 4; w-=4) {
  1297. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1298. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1299. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1300. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1301. s1 += 8;
  1302. s2 += 8;
  1303. d += 4;
  1304. }
  1305. for(;w > 0; w--) {
  1306. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1307. s1 += 2;
  1308. s2 += 2;
  1309. d++;
  1310. }
  1311. src += 2 * src_wrap;
  1312. dst += dst_wrap;
  1313. }
  1314. }
  1315. /* 4x4 -> 1x1 */
  1316. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1317. const uint8_t *src, int src_wrap,
  1318. int width, int height)
  1319. {
  1320. int w;
  1321. const uint8_t *s1, *s2, *s3, *s4;
  1322. uint8_t *d;
  1323. for(;height > 0; height--) {
  1324. s1 = src;
  1325. s2 = s1 + src_wrap;
  1326. s3 = s2 + src_wrap;
  1327. s4 = s3 + src_wrap;
  1328. d = dst;
  1329. for(w = width;w > 0; w--) {
  1330. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1331. s2[0] + s2[1] + s2[2] + s2[3] +
  1332. s3[0] + s3[1] + s3[2] + s3[3] +
  1333. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1334. s1 += 4;
  1335. s2 += 4;
  1336. s3 += 4;
  1337. s4 += 4;
  1338. d++;
  1339. }
  1340. src += 4 * src_wrap;
  1341. dst += dst_wrap;
  1342. }
  1343. }
  1344. /* 8x8 -> 1x1 */
  1345. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1346. const uint8_t *src, int src_wrap,
  1347. int width, int height)
  1348. {
  1349. int w, i;
  1350. for(;height > 0; height--) {
  1351. for(w = width;w > 0; w--) {
  1352. int tmp=0;
  1353. for(i=0; i<8; i++){
  1354. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1355. src += src_wrap;
  1356. }
  1357. *(dst++) = (tmp + 32)>>6;
  1358. src += 8 - 8*src_wrap;
  1359. }
  1360. src += 8*src_wrap - 8*width;
  1361. dst += dst_wrap - width;
  1362. }
  1363. }
  1364. /* XXX: add jpeg quantize code */
  1365. #define TRANSP_INDEX (6*6*6)
  1366. /* this is maybe slow, but allows for extensions */
  1367. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  1368. {
  1369. return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
  1370. }
  1371. static void build_rgb_palette(uint8_t *palette, int has_alpha)
  1372. {
  1373. uint32_t *pal;
  1374. static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
  1375. int i, r, g, b;
  1376. pal = (uint32_t *)palette;
  1377. i = 0;
  1378. for(r = 0; r < 6; r++) {
  1379. for(g = 0; g < 6; g++) {
  1380. for(b = 0; b < 6; b++) {
  1381. pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
  1382. (pal_value[g] << 8) | pal_value[b];
  1383. }
  1384. }
  1385. }
  1386. if (has_alpha)
  1387. pal[i++] = 0;
  1388. while (i < 256)
  1389. pal[i++] = 0xff000000;
  1390. }
  1391. /* copy bit n to bits 0 ... n - 1 */
  1392. static inline unsigned int bitcopy_n(unsigned int a, int n)
  1393. {
  1394. int mask;
  1395. mask = (1 << n) - 1;
  1396. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  1397. }
  1398. /* rgb555 handling */
  1399. #define RGB_NAME rgb555
  1400. #define RGB_IN(r, g, b, s)\
  1401. {\
  1402. unsigned int v = ((const uint16_t *)(s))[0];\
  1403. r = bitcopy_n(v >> (10 - 3), 3);\
  1404. g = bitcopy_n(v >> (5 - 3), 3);\
  1405. b = bitcopy_n(v << 3, 3);\
  1406. }
  1407. #define RGB_OUT(d, r, g, b)\
  1408. {\
  1409. ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
  1410. }
  1411. #define BPP 2
  1412. #include "imgconvert_template.c"
  1413. /* rgb565 handling */
  1414. #define RGB_NAME rgb565
  1415. #define RGB_IN(r, g, b, s)\
  1416. {\
  1417. unsigned int v = ((const uint16_t *)(s))[0];\
  1418. r = bitcopy_n(v >> (11 - 3), 3);\
  1419. g = bitcopy_n(v >> (5 - 2), 2);\
  1420. b = bitcopy_n(v << 3, 3);\
  1421. }
  1422. #define RGB_OUT(d, r, g, b)\
  1423. {\
  1424. ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  1425. }
  1426. #define BPP 2
  1427. #include "imgconvert_template.c"
  1428. /* bgr24 handling */
  1429. #define RGB_NAME bgr24
  1430. #define RGB_IN(r, g, b, s)\
  1431. {\
  1432. b = (s)[0];\
  1433. g = (s)[1];\
  1434. r = (s)[2];\
  1435. }
  1436. #define RGB_OUT(d, r, g, b)\
  1437. {\
  1438. (d)[0] = b;\
  1439. (d)[1] = g;\
  1440. (d)[2] = r;\
  1441. }
  1442. #define BPP 3
  1443. #include "imgconvert_template.c"
  1444. #undef RGB_IN
  1445. #undef RGB_OUT
  1446. #undef BPP
  1447. /* rgb24 handling */
  1448. #define RGB_NAME rgb24
  1449. #define FMT_RGB24
  1450. #define RGB_IN(r, g, b, s)\
  1451. {\
  1452. r = (s)[0];\
  1453. g = (s)[1];\
  1454. b = (s)[2];\
  1455. }
  1456. #define RGB_OUT(d, r, g, b)\
  1457. {\
  1458. (d)[0] = r;\
  1459. (d)[1] = g;\
  1460. (d)[2] = b;\
  1461. }
  1462. #define BPP 3
  1463. #include "imgconvert_template.c"
  1464. /* rgb32 handling */
  1465. #define RGB_NAME rgb32
  1466. #define FMT_RGB32
  1467. #define RGB_IN(r, g, b, s)\
  1468. {\
  1469. unsigned int v = ((const uint32_t *)(s))[0];\
  1470. r = (v >> 16) & 0xff;\
  1471. g = (v >> 8) & 0xff;\
  1472. b = v & 0xff;\
  1473. }
  1474. #define RGBA_IN(r, g, b, a, s)\
  1475. {\
  1476. unsigned int v = ((const uint32_t *)(s))[0];\
  1477. a = (v >> 24) & 0xff;\
  1478. r = (v >> 16) & 0xff;\
  1479. g = (v >> 8) & 0xff;\
  1480. b = v & 0xff;\
  1481. }
  1482. #define RGBA_OUT(d, r, g, b, a)\
  1483. {\
  1484. ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
  1485. }
  1486. #define BPP 4
  1487. #include "imgconvert_template.c"
  1488. static void mono_to_gray(AVPicture *dst, const AVPicture *src,
  1489. int width, int height, int xor_mask)
  1490. {
  1491. const unsigned char *p;
  1492. unsigned char *q;
  1493. int v, dst_wrap, src_wrap;
  1494. int y, w;
  1495. p = src->data[0];
  1496. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  1497. q = dst->data[0];
  1498. dst_wrap = dst->linesize[0] - width;
  1499. for(y=0;y<height;y++) {
  1500. w = width;
  1501. while (w >= 8) {
  1502. v = *p++ ^ xor_mask;
  1503. q[0] = -(v >> 7);
  1504. q[1] = -((v >> 6) & 1);
  1505. q[2] = -((v >> 5) & 1);
  1506. q[3] = -((v >> 4) & 1);
  1507. q[4] = -((v >> 3) & 1);
  1508. q[5] = -((v >> 2) & 1);
  1509. q[6] = -((v >> 1) & 1);
  1510. q[7] = -((v >> 0) & 1);
  1511. w -= 8;
  1512. q += 8;
  1513. }
  1514. if (w > 0) {
  1515. v = *p++ ^ xor_mask;
  1516. do {
  1517. q[0] = -((v >> 7) & 1);
  1518. q++;
  1519. v <<= 1;
  1520. } while (--w);
  1521. }
  1522. p += src_wrap;
  1523. q += dst_wrap;
  1524. }
  1525. }
  1526. static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
  1527. int width, int height)
  1528. {
  1529. mono_to_gray(dst, src, width, height, 0xff);
  1530. }
  1531. static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
  1532. int width, int height)
  1533. {
  1534. mono_to_gray(dst, src, width, height, 0x00);
  1535. }
  1536. static void gray_to_mono(AVPicture *dst, const AVPicture *src,
  1537. int width, int height, int xor_mask)
  1538. {
  1539. int n;
  1540. const uint8_t *s;
  1541. uint8_t *d;
  1542. int j, b, v, n1, src_wrap, dst_wrap, y;
  1543. s = src->data[0];
  1544. src_wrap = src->linesize[0] - width;
  1545. d = dst->data[0];
  1546. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  1547. for(y=0;y<height;y++) {
  1548. n = width;
  1549. while (n >= 8) {
  1550. v = 0;
  1551. for(j=0;j<8;j++) {
  1552. b = s[0];
  1553. s++;
  1554. v = (v << 1) | (b >> 7);
  1555. }
  1556. d[0] = v ^ xor_mask;
  1557. d++;
  1558. n -= 8;
  1559. }
  1560. if (n > 0) {
  1561. n1 = n;
  1562. v = 0;
  1563. while (n > 0) {
  1564. b = s[0];
  1565. s++;
  1566. v = (v << 1) | (b >> 7);
  1567. n--;
  1568. }
  1569. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  1570. d++;
  1571. }
  1572. s += src_wrap;
  1573. d += dst_wrap;
  1574. }
  1575. }
  1576. static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
  1577. int width, int height)
  1578. {
  1579. gray_to_mono(dst, src, width, height, 0xff);
  1580. }
  1581. static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
  1582. int width, int height)
  1583. {
  1584. gray_to_mono(dst, src, width, height, 0x00);
  1585. }
  1586. static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
  1587. int width, int height)
  1588. {
  1589. int x, y, src_wrap, dst_wrap;
  1590. uint8_t *s, *d;
  1591. s = src->data[0];
  1592. src_wrap = src->linesize[0] - width;
  1593. d = dst->data[0];
  1594. dst_wrap = dst->linesize[0] - width * 2;
  1595. for(y=0; y<height; y++){
  1596. for(x=0; x<width; x++){
  1597. *d++ = *s;
  1598. *d++ = *s++;
  1599. }
  1600. s += src_wrap;
  1601. d += dst_wrap;
  1602. }
  1603. }
  1604. static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
  1605. int width, int height)
  1606. {
  1607. int x, y, src_wrap, dst_wrap;
  1608. uint8_t *s, *d;
  1609. s = src->data[0];
  1610. src_wrap = src->linesize[0] - width * 2;
  1611. d = dst->data[0];
  1612. dst_wrap = dst->linesize[0] - width;
  1613. for(y=0; y<height; y++){
  1614. for(x=0; x<width; x++){
  1615. *d++ = *s;
  1616. s += 2;
  1617. }
  1618. s += src_wrap;
  1619. d += dst_wrap;
  1620. }
  1621. }
  1622. static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
  1623. int width, int height)
  1624. {
  1625. gray16_to_gray(dst, src, width, height);
  1626. }
  1627. static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
  1628. int width, int height)
  1629. {
  1630. AVPicture tmpsrc = *src;
  1631. tmpsrc.data[0]++;
  1632. gray16_to_gray(dst, &tmpsrc, width, height);
  1633. }
  1634. static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
  1635. int width, int height)
  1636. {
  1637. int x, y, src_wrap, dst_wrap;
  1638. uint16_t *s, *d;
  1639. s = (uint16_t*)src->data[0];
  1640. src_wrap = (src->linesize[0] - width * 2)/2;
  1641. d = (uint16_t*)dst->data[0];
  1642. dst_wrap = (dst->linesize[0] - width * 2)/2;
  1643. for(y=0; y<height; y++){
  1644. for(x=0; x<width; x++){
  1645. *d++ = bswap_16(*s++);
  1646. }
  1647. s += src_wrap;
  1648. d += dst_wrap;
  1649. }
  1650. }
  1651. typedef struct ConvertEntry {
  1652. void (*convert)(AVPicture *dst,
  1653. const AVPicture *src, int width, int height);
  1654. } ConvertEntry;
  1655. /* Add each new conversion function in this table. In order to be able
  1656. to convert from any format to any format, the following constraints
  1657. must be satisfied:
  1658. - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
  1659. - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
  1660. - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
  1661. - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
  1662. PIX_FMT_RGB24.
  1663. - PIX_FMT_422 must convert to and from PIX_FMT_422P.
  1664. The other conversion functions are just optimizations for common cases.
  1665. */
  1666. static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  1667. [PIX_FMT_YUV420P] = {
  1668. [PIX_FMT_YUYV422] = {
  1669. .convert = yuv420p_to_yuyv422,
  1670. },
  1671. [PIX_FMT_RGB555] = {
  1672. .convert = yuv420p_to_rgb555
  1673. },
  1674. [PIX_FMT_RGB565] = {
  1675. .convert = yuv420p_to_rgb565
  1676. },
  1677. [PIX_FMT_BGR24] = {
  1678. .convert = yuv420p_to_bgr24
  1679. },
  1680. [PIX_FMT_RGB24] = {
  1681. .convert = yuv420p_to_rgb24
  1682. },
  1683. [PIX_FMT_RGB32] = {
  1684. .convert = yuv420p_to_rgb32
  1685. },
  1686. [PIX_FMT_UYVY422] = {
  1687. .convert = yuv420p_to_uyvy422,
  1688. },
  1689. },
  1690. [PIX_FMT_YUV422P] = {
  1691. [PIX_FMT_YUYV422] = {
  1692. .convert = yuv422p_to_yuyv422,
  1693. },
  1694. [PIX_FMT_UYVY422] = {
  1695. .convert = yuv422p_to_uyvy422,
  1696. },
  1697. },
  1698. [PIX_FMT_YUV444P] = {
  1699. [PIX_FMT_RGB24] = {
  1700. .convert = yuv444p_to_rgb24
  1701. },
  1702. },
  1703. [PIX_FMT_YUVJ420P] = {
  1704. [PIX_FMT_RGB555] = {
  1705. .convert = yuvj420p_to_rgb555
  1706. },
  1707. [PIX_FMT_RGB565] = {
  1708. .convert = yuvj420p_to_rgb565
  1709. },
  1710. [PIX_FMT_BGR24] = {
  1711. .convert = yuvj420p_to_bgr24
  1712. },
  1713. [PIX_FMT_RGB24] = {
  1714. .convert = yuvj420p_to_rgb24
  1715. },
  1716. [PIX_FMT_RGB32] = {
  1717. .convert = yuvj420p_to_rgb32
  1718. },
  1719. },
  1720. [PIX_FMT_YUVJ444P] = {
  1721. [PIX_FMT_RGB24] = {
  1722. .convert = yuvj444p_to_rgb24
  1723. },
  1724. },
  1725. [PIX_FMT_YUYV422] = {
  1726. [PIX_FMT_YUV420P] = {
  1727. .convert = yuyv422_to_yuv420p,
  1728. },
  1729. [PIX_FMT_YUV422P] = {
  1730. .convert = yuyv422_to_yuv422p,
  1731. },
  1732. },
  1733. [PIX_FMT_UYVY422] = {
  1734. [PIX_FMT_YUV420P] = {
  1735. .convert = uyvy422_to_yuv420p,
  1736. },
  1737. [PIX_FMT_YUV422P] = {
  1738. .convert = uyvy422_to_yuv422p,
  1739. },
  1740. },
  1741. [PIX_FMT_RGB24] = {
  1742. [PIX_FMT_YUV420P] = {
  1743. .convert = rgb24_to_yuv420p
  1744. },
  1745. [PIX_FMT_RGB565] = {
  1746. .convert = rgb24_to_rgb565
  1747. },
  1748. [PIX_FMT_RGB555] = {
  1749. .convert = rgb24_to_rgb555
  1750. },
  1751. [PIX_FMT_RGB32] = {
  1752. .convert = rgb24_to_rgb32
  1753. },
  1754. [PIX_FMT_BGR24] = {
  1755. .convert = rgb24_to_bgr24
  1756. },
  1757. [PIX_FMT_GRAY8] = {
  1758. .convert = rgb24_to_gray
  1759. },
  1760. [PIX_FMT_PAL8] = {
  1761. .convert = rgb24_to_pal8
  1762. },
  1763. [PIX_FMT_YUV444P] = {
  1764. .convert = rgb24_to_yuv444p
  1765. },
  1766. [PIX_FMT_YUVJ420P] = {
  1767. .convert = rgb24_to_yuvj420p
  1768. },
  1769. [PIX_FMT_YUVJ444P] = {
  1770. .convert = rgb24_to_yuvj444p
  1771. },
  1772. },
  1773. [PIX_FMT_RGB32] = {
  1774. [PIX_FMT_RGB24] = {
  1775. .convert = rgb32_to_rgb24
  1776. },
  1777. [PIX_FMT_BGR24] = {
  1778. .convert = rgb32_to_bgr24
  1779. },
  1780. [PIX_FMT_RGB565] = {
  1781. .convert = rgb32_to_rgb565
  1782. },
  1783. [PIX_FMT_RGB555] = {
  1784. .convert = rgb32_to_rgb555
  1785. },
  1786. [PIX_FMT_PAL8] = {
  1787. .convert = rgb32_to_pal8
  1788. },
  1789. [PIX_FMT_YUV420P] = {
  1790. .convert = rgb32_to_yuv420p
  1791. },
  1792. [PIX_FMT_GRAY8] = {
  1793. .convert = rgb32_to_gray
  1794. },
  1795. },
  1796. [PIX_FMT_BGR24] = {
  1797. [PIX_FMT_RGB32] = {
  1798. .convert = bgr24_to_rgb32
  1799. },
  1800. [PIX_FMT_RGB24] = {
  1801. .convert = bgr24_to_rgb24
  1802. },
  1803. [PIX_FMT_YUV420P] = {
  1804. .convert = bgr24_to_yuv420p
  1805. },
  1806. [PIX_FMT_GRAY8] = {
  1807. .convert = bgr24_to_gray
  1808. },
  1809. },
  1810. [PIX_FMT_RGB555] = {
  1811. [PIX_FMT_RGB24] = {
  1812. .convert = rgb555_to_rgb24
  1813. },
  1814. [PIX_FMT_RGB32] = {
  1815. .convert = rgb555_to_rgb32
  1816. },
  1817. [PIX_FMT_YUV420P] = {
  1818. .convert = rgb555_to_yuv420p
  1819. },
  1820. [PIX_FMT_GRAY8] = {
  1821. .convert = rgb555_to_gray
  1822. },
  1823. },
  1824. [PIX_FMT_RGB565] = {
  1825. [PIX_FMT_RGB32] = {
  1826. .convert = rgb565_to_rgb32
  1827. },
  1828. [PIX_FMT_RGB24] = {
  1829. .convert = rgb565_to_rgb24
  1830. },
  1831. [PIX_FMT_YUV420P] = {
  1832. .convert = rgb565_to_yuv420p
  1833. },
  1834. [PIX_FMT_GRAY8] = {
  1835. .convert = rgb565_to_gray
  1836. },
  1837. },
  1838. [PIX_FMT_GRAY16BE] = {
  1839. [PIX_FMT_GRAY8] = {
  1840. .convert = gray16be_to_gray
  1841. },
  1842. [PIX_FMT_GRAY16LE] = {
  1843. .convert = gray16_to_gray16
  1844. },
  1845. },
  1846. [PIX_FMT_GRAY16LE] = {
  1847. [PIX_FMT_GRAY8] = {
  1848. .convert = gray16le_to_gray
  1849. },
  1850. [PIX_FMT_GRAY16BE] = {
  1851. .convert = gray16_to_gray16
  1852. },
  1853. },
  1854. [PIX_FMT_GRAY8] = {
  1855. [PIX_FMT_RGB555] = {
  1856. .convert = gray_to_rgb555
  1857. },
  1858. [PIX_FMT_RGB565] = {
  1859. .convert = gray_to_rgb565
  1860. },
  1861. [PIX_FMT_RGB24] = {
  1862. .convert = gray_to_rgb24
  1863. },
  1864. [PIX_FMT_BGR24] = {
  1865. .convert = gray_to_bgr24
  1866. },
  1867. [PIX_FMT_RGB32] = {
  1868. .convert = gray_to_rgb32
  1869. },
  1870. [PIX_FMT_MONOWHITE] = {
  1871. .convert = gray_to_monowhite
  1872. },
  1873. [PIX_FMT_MONOBLACK] = {
  1874. .convert = gray_to_monoblack
  1875. },
  1876. [PIX_FMT_GRAY16LE] = {
  1877. .convert = gray_to_gray16
  1878. },
  1879. [PIX_FMT_GRAY16BE] = {
  1880. .convert = gray_to_gray16
  1881. },
  1882. },
  1883. [PIX_FMT_MONOWHITE] = {
  1884. [PIX_FMT_GRAY8] = {
  1885. .convert = monowhite_to_gray
  1886. },
  1887. },
  1888. [PIX_FMT_MONOBLACK] = {
  1889. [PIX_FMT_GRAY8] = {
  1890. .convert = monoblack_to_gray
  1891. },
  1892. },
  1893. [PIX_FMT_PAL8] = {
  1894. [PIX_FMT_RGB555] = {
  1895. .convert = pal8_to_rgb555
  1896. },
  1897. [PIX_FMT_RGB565] = {
  1898. .convert = pal8_to_rgb565
  1899. },
  1900. [PIX_FMT_BGR24] = {
  1901. .convert = pal8_to_bgr24
  1902. },
  1903. [PIX_FMT_RGB24] = {
  1904. .convert = pal8_to_rgb24
  1905. },
  1906. [PIX_FMT_RGB32] = {
  1907. .convert = pal8_to_rgb32
  1908. },
  1909. },
  1910. [PIX_FMT_UYYVYY411] = {
  1911. [PIX_FMT_YUV411P] = {
  1912. .convert = uyyvyy411_to_yuv411p,
  1913. },
  1914. },
  1915. };
  1916. int avpicture_alloc(AVPicture *picture,
  1917. int pix_fmt, int width, int height)
  1918. {
  1919. int size;
  1920. void *ptr;
  1921. size = avpicture_get_size(pix_fmt, width, height);
  1922. if(size<0)
  1923. goto fail;
  1924. ptr = av_malloc(size);
  1925. if (!ptr)
  1926. goto fail;
  1927. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1928. if(picture->data[1] && !picture->data[2])
  1929. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1930. return 0;
  1931. fail:
  1932. memset(picture, 0, sizeof(AVPicture));
  1933. return -1;
  1934. }
  1935. void avpicture_free(AVPicture *picture)
  1936. {
  1937. av_free(picture->data[0]);
  1938. }
  1939. /* return true if yuv planar */
  1940. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1941. {
  1942. return (ps->color_type == FF_COLOR_YUV ||
  1943. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1944. ps->pixel_type == FF_PIXEL_PLANAR;
  1945. }
  1946. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1947. int pix_fmt, int top_band, int left_band)
  1948. {
  1949. int y_shift;
  1950. int x_shift;
  1951. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1952. return -1;
  1953. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1954. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1955. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1956. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1957. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1958. dst->linesize[0] = src->linesize[0];
  1959. dst->linesize[1] = src->linesize[1];
  1960. dst->linesize[2] = src->linesize[2];
  1961. return 0;
  1962. }
  1963. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1964. int pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1965. int *color)
  1966. {
  1967. uint8_t *optr;
  1968. int y_shift;
  1969. int x_shift;
  1970. int yheight;
  1971. int i, y;
  1972. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1973. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1974. for (i = 0; i < 3; i++) {
  1975. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1976. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1977. if (padtop || padleft) {
  1978. memset(dst->data[i], color[i],
  1979. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1980. }
  1981. if (padleft || padright) {
  1982. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1983. (dst->linesize[i] - (padright >> x_shift));
  1984. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1985. for (y = 0; y < yheight; y++) {
  1986. memset(optr, color[i], (padleft + padright) >> x_shift);
  1987. optr += dst->linesize[i];
  1988. }
  1989. }
  1990. if (src) { /* first line */
  1991. uint8_t *iptr = src->data[i];
  1992. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1993. (padleft >> x_shift);
  1994. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1995. iptr += src->linesize[i];
  1996. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1997. (dst->linesize[i] - (padright >> x_shift));
  1998. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1999. for (y = 0; y < yheight; y++) {
  2000. memset(optr, color[i], (padleft + padright) >> x_shift);
  2001. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  2002. (width - padleft - padright) >> x_shift);
  2003. iptr += src->linesize[i];
  2004. optr += dst->linesize[i];
  2005. }
  2006. }
  2007. if (padbottom || padright) {
  2008. optr = dst->data[i] + dst->linesize[i] *
  2009. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  2010. memset(optr, color[i],dst->linesize[i] *
  2011. (padbottom >> y_shift) + (padright >> x_shift));
  2012. }
  2013. }
  2014. return 0;
  2015. }
  2016. #if !CONFIG_SWSCALE
  2017. static uint8_t y_ccir_to_jpeg[256];
  2018. static uint8_t y_jpeg_to_ccir[256];
  2019. static uint8_t c_ccir_to_jpeg[256];
  2020. static uint8_t c_jpeg_to_ccir[256];
  2021. /* init various conversion tables */
  2022. static av_cold void img_convert_init(void)
  2023. {
  2024. int i;
  2025. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2026. for(i = 0;i < 256; i++) {
  2027. y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
  2028. y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
  2029. c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
  2030. c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
  2031. }
  2032. }
  2033. /* apply to each pixel the given table */
  2034. static void img_apply_table(uint8_t *dst, int dst_wrap,
  2035. const uint8_t *src, int src_wrap,
  2036. int width, int height, const uint8_t *table1)
  2037. {
  2038. int n;
  2039. const uint8_t *s;
  2040. uint8_t *d;
  2041. const uint8_t *table;
  2042. table = table1;
  2043. for(;height > 0; height--) {
  2044. s = src;
  2045. d = dst;
  2046. n = width;
  2047. while (n >= 4) {
  2048. d[0] = table[s[0]];
  2049. d[1] = table[s[1]];
  2050. d[2] = table[s[2]];
  2051. d[3] = table[s[3]];
  2052. d += 4;
  2053. s += 4;
  2054. n -= 4;
  2055. }
  2056. while (n > 0) {
  2057. d[0] = table[s[0]];
  2058. d++;
  2059. s++;
  2060. n--;
  2061. }
  2062. dst += dst_wrap;
  2063. src += src_wrap;
  2064. }
  2065. }
  2066. /* XXX: use generic filter ? */
  2067. /* XXX: in most cases, the sampling position is incorrect */
  2068. /* 4x1 -> 1x1 */
  2069. static void shrink41(uint8_t *dst, int dst_wrap,
  2070. const uint8_t *src, int src_wrap,
  2071. int width, int height)
  2072. {
  2073. int w;
  2074. const uint8_t *s;
  2075. uint8_t *d;
  2076. for(;height > 0; height--) {
  2077. s = src;
  2078. d = dst;
  2079. for(w = width;w > 0; w--) {
  2080. d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
  2081. s += 4;
  2082. d++;
  2083. }
  2084. src += src_wrap;
  2085. dst += dst_wrap;
  2086. }
  2087. }
  2088. /* 2x1 -> 1x1 */
  2089. static void shrink21(uint8_t *dst, int dst_wrap,
  2090. const uint8_t *src, int src_wrap,
  2091. int width, int height)
  2092. {
  2093. int w;
  2094. const uint8_t *s;
  2095. uint8_t *d;
  2096. for(;height > 0; height--) {
  2097. s = src;
  2098. d = dst;
  2099. for(w = width;w > 0; w--) {
  2100. d[0] = (s[0] + s[1]) >> 1;
  2101. s += 2;
  2102. d++;
  2103. }
  2104. src += src_wrap;
  2105. dst += dst_wrap;
  2106. }
  2107. }
  2108. /* 1x2 -> 1x1 */
  2109. static void shrink12(uint8_t *dst, int dst_wrap,
  2110. const uint8_t *src, int src_wrap,
  2111. int width, int height)
  2112. {
  2113. int w;
  2114. uint8_t *d;
  2115. const uint8_t *s1, *s2;
  2116. for(;height > 0; height--) {
  2117. s1 = src;
  2118. s2 = s1 + src_wrap;
  2119. d = dst;
  2120. for(w = width;w >= 4; w-=4) {
  2121. d[0] = (s1[0] + s2[0]) >> 1;
  2122. d[1] = (s1[1] + s2[1]) >> 1;
  2123. d[2] = (s1[2] + s2[2]) >> 1;
  2124. d[3] = (s1[3] + s2[3]) >> 1;
  2125. s1 += 4;
  2126. s2 += 4;
  2127. d += 4;
  2128. }
  2129. for(;w > 0; w--) {
  2130. d[0] = (s1[0] + s2[0]) >> 1;
  2131. s1++;
  2132. s2++;
  2133. d++;
  2134. }
  2135. src += 2 * src_wrap;
  2136. dst += dst_wrap;
  2137. }
  2138. }
  2139. static void grow21_line(uint8_t *dst, const uint8_t *src,
  2140. int width)
  2141. {
  2142. int w;
  2143. const uint8_t *s1;
  2144. uint8_t *d;
  2145. s1 = src;
  2146. d = dst;
  2147. for(w = width;w >= 4; w-=4) {
  2148. d[1] = d[0] = s1[0];
  2149. d[3] = d[2] = s1[1];
  2150. s1 += 2;
  2151. d += 4;
  2152. }
  2153. for(;w >= 2; w -= 2) {
  2154. d[1] = d[0] = s1[0];
  2155. s1 ++;
  2156. d += 2;
  2157. }
  2158. /* only needed if width is not a multiple of two */
  2159. /* XXX: veryfy that */
  2160. if (w) {
  2161. d[0] = s1[0];
  2162. }
  2163. }
  2164. static void grow41_line(uint8_t *dst, const uint8_t *src,
  2165. int width)
  2166. {
  2167. int w, v;
  2168. const uint8_t *s1;
  2169. uint8_t *d;
  2170. s1 = src;
  2171. d = dst;
  2172. for(w = width;w >= 4; w-=4) {
  2173. v = s1[0];
  2174. d[0] = v;
  2175. d[1] = v;
  2176. d[2] = v;
  2177. d[3] = v;
  2178. s1 ++;
  2179. d += 4;
  2180. }
  2181. }
  2182. /* 1x1 -> 2x1 */
  2183. static void grow21(uint8_t *dst, int dst_wrap,
  2184. const uint8_t *src, int src_wrap,
  2185. int width, int height)
  2186. {
  2187. for(;height > 0; height--) {
  2188. grow21_line(dst, src, width);
  2189. src += src_wrap;
  2190. dst += dst_wrap;
  2191. }
  2192. }
  2193. /* 1x1 -> 1x2 */
  2194. static void grow12(uint8_t *dst, int dst_wrap,
  2195. const uint8_t *src, int src_wrap,
  2196. int width, int height)
  2197. {
  2198. for(;height > 0; height-=2) {
  2199. memcpy(dst, src, width);
  2200. dst += dst_wrap;
  2201. memcpy(dst, src, width);
  2202. dst += dst_wrap;
  2203. src += src_wrap;
  2204. }
  2205. }
  2206. /* 1x1 -> 2x2 */
  2207. static void grow22(uint8_t *dst, int dst_wrap,
  2208. const uint8_t *src, int src_wrap,
  2209. int width, int height)
  2210. {
  2211. for(;height > 0; height--) {
  2212. grow21_line(dst, src, width);
  2213. if (height%2)
  2214. src += src_wrap;
  2215. dst += dst_wrap;
  2216. }
  2217. }
  2218. /* 1x1 -> 4x1 */
  2219. static void grow41(uint8_t *dst, int dst_wrap,
  2220. const uint8_t *src, int src_wrap,
  2221. int width, int height)
  2222. {
  2223. for(;height > 0; height--) {
  2224. grow41_line(dst, src, width);
  2225. src += src_wrap;
  2226. dst += dst_wrap;
  2227. }
  2228. }
  2229. /* 1x1 -> 4x4 */
  2230. static void grow44(uint8_t *dst, int dst_wrap,
  2231. const uint8_t *src, int src_wrap,
  2232. int width, int height)
  2233. {
  2234. for(;height > 0; height--) {
  2235. grow41_line(dst, src, width);
  2236. if ((height & 3) == 1)
  2237. src += src_wrap;
  2238. dst += dst_wrap;
  2239. }
  2240. }
  2241. /* 1x2 -> 2x1 */
  2242. static void conv411(uint8_t *dst, int dst_wrap,
  2243. const uint8_t *src, int src_wrap,
  2244. int width, int height)
  2245. {
  2246. int w, c;
  2247. const uint8_t *s1, *s2;
  2248. uint8_t *d;
  2249. width>>=1;
  2250. for(;height > 0; height--) {
  2251. s1 = src;
  2252. s2 = src + src_wrap;
  2253. d = dst;
  2254. for(w = width;w > 0; w--) {
  2255. c = (s1[0] + s2[0]) >> 1;
  2256. d[0] = c;
  2257. d[1] = c;
  2258. s1++;
  2259. s2++;
  2260. d += 2;
  2261. }
  2262. src += src_wrap * 2;
  2263. dst += dst_wrap;
  2264. }
  2265. }
  2266. /* XXX: always use linesize. Return -1 if not supported */
  2267. int img_convert(AVPicture *dst, int dst_pix_fmt,
  2268. const AVPicture *src, int src_pix_fmt,
  2269. int src_width, int src_height)
  2270. {
  2271. static int initialized;
  2272. int i, ret, dst_width, dst_height, int_pix_fmt;
  2273. const PixFmtInfo *src_pix, *dst_pix;
  2274. const ConvertEntry *ce;
  2275. AVPicture tmp1, *tmp = &tmp1;
  2276. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  2277. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  2278. return -1;
  2279. if (src_width <= 0 || src_height <= 0)
  2280. return 0;
  2281. if (!initialized) {
  2282. initialized = 1;
  2283. img_convert_init();
  2284. }
  2285. dst_width = src_width;
  2286. dst_height = src_height;
  2287. dst_pix = &pix_fmt_info[dst_pix_fmt];
  2288. src_pix = &pix_fmt_info[src_pix_fmt];
  2289. if (src_pix_fmt == dst_pix_fmt) {
  2290. /* no conversion needed: just copy */
  2291. av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
  2292. return 0;
  2293. }
  2294. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  2295. if (ce->convert) {
  2296. /* specific conversion routine */
  2297. ce->convert(dst, src, dst_width, dst_height);
  2298. return 0;
  2299. }
  2300. /* gray to YUV */
  2301. if (is_yuv_planar(dst_pix) &&
  2302. src_pix_fmt == PIX_FMT_GRAY8) {
  2303. int w, h, y;
  2304. uint8_t *d;
  2305. if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
  2306. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2307. src->data[0], src->linesize[0],
  2308. dst_width, dst_height);
  2309. } else {
  2310. img_apply_table(dst->data[0], dst->linesize[0],
  2311. src->data[0], src->linesize[0],
  2312. dst_width, dst_height,
  2313. y_jpeg_to_ccir);
  2314. }
  2315. /* fill U and V with 128 */
  2316. w = dst_width;
  2317. h = dst_height;
  2318. w >>= dst_pix->x_chroma_shift;
  2319. h >>= dst_pix->y_chroma_shift;
  2320. for(i = 1; i <= 2; i++) {
  2321. d = dst->data[i];
  2322. for(y = 0; y< h; y++) {
  2323. memset(d, 128, w);
  2324. d += dst->linesize[i];
  2325. }
  2326. }
  2327. return 0;
  2328. }
  2329. /* YUV to gray */
  2330. if (is_yuv_planar(src_pix) &&
  2331. dst_pix_fmt == PIX_FMT_GRAY8) {
  2332. if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
  2333. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2334. src->data[0], src->linesize[0],
  2335. dst_width, dst_height);
  2336. } else {
  2337. img_apply_table(dst->data[0], dst->linesize[0],
  2338. src->data[0], src->linesize[0],
  2339. dst_width, dst_height,
  2340. y_ccir_to_jpeg);
  2341. }
  2342. return 0;
  2343. }
  2344. /* YUV to YUV planar */
  2345. if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
  2346. int x_shift, y_shift, w, h, xy_shift;
  2347. void (*resize_func)(uint8_t *dst, int dst_wrap,
  2348. const uint8_t *src, int src_wrap,
  2349. int width, int height);
  2350. /* compute chroma size of the smallest dimensions */
  2351. w = dst_width;
  2352. h = dst_height;
  2353. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  2354. w >>= dst_pix->x_chroma_shift;
  2355. else
  2356. w >>= src_pix->x_chroma_shift;
  2357. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  2358. h >>= dst_pix->y_chroma_shift;
  2359. else
  2360. h >>= src_pix->y_chroma_shift;
  2361. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  2362. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  2363. xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
  2364. /* there must be filters for conversion at least from and to
  2365. YUV444 format */
  2366. switch(xy_shift) {
  2367. case 0x00:
  2368. resize_func = ff_img_copy_plane;
  2369. break;
  2370. case 0x10:
  2371. resize_func = shrink21;
  2372. break;
  2373. case 0x20:
  2374. resize_func = shrink41;
  2375. break;
  2376. case 0x01:
  2377. resize_func = shrink12;
  2378. break;
  2379. case 0x11:
  2380. resize_func = ff_shrink22;
  2381. break;
  2382. case 0x22:
  2383. resize_func = ff_shrink44;
  2384. break;
  2385. case 0xf0:
  2386. resize_func = grow21;
  2387. break;
  2388. case 0x0f:
  2389. resize_func = grow12;
  2390. break;
  2391. case 0xe0:
  2392. resize_func = grow41;
  2393. break;
  2394. case 0xff:
  2395. resize_func = grow22;
  2396. break;
  2397. case 0xee:
  2398. resize_func = grow44;
  2399. break;
  2400. case 0xf1:
  2401. resize_func = conv411;
  2402. break;
  2403. default:
  2404. /* currently not handled */
  2405. goto no_chroma_filter;
  2406. }
  2407. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2408. src->data[0], src->linesize[0],
  2409. dst_width, dst_height);
  2410. for(i = 1;i <= 2; i++)
  2411. resize_func(dst->data[i], dst->linesize[i],
  2412. src->data[i], src->linesize[i],
  2413. dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
  2414. /* if yuv color space conversion is needed, we do it here on
  2415. the destination image */
  2416. if (dst_pix->color_type != src_pix->color_type) {
  2417. const uint8_t *y_table, *c_table;
  2418. if (dst_pix->color_type == FF_COLOR_YUV) {
  2419. y_table = y_jpeg_to_ccir;
  2420. c_table = c_jpeg_to_ccir;
  2421. } else {
  2422. y_table = y_ccir_to_jpeg;
  2423. c_table = c_ccir_to_jpeg;
  2424. }
  2425. img_apply_table(dst->data[0], dst->linesize[0],
  2426. dst->data[0], dst->linesize[0],
  2427. dst_width, dst_height,
  2428. y_table);
  2429. for(i = 1;i <= 2; i++)
  2430. img_apply_table(dst->data[i], dst->linesize[i],
  2431. dst->data[i], dst->linesize[i],
  2432. dst_width>>dst_pix->x_chroma_shift,
  2433. dst_height>>dst_pix->y_chroma_shift,
  2434. c_table);
  2435. }
  2436. return 0;
  2437. }
  2438. no_chroma_filter:
  2439. /* try to use an intermediate format */
  2440. if (src_pix_fmt == PIX_FMT_YUYV422 ||
  2441. dst_pix_fmt == PIX_FMT_YUYV422) {
  2442. /* specific case: convert to YUV422P first */
  2443. int_pix_fmt = PIX_FMT_YUV422P;
  2444. } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
  2445. dst_pix_fmt == PIX_FMT_UYVY422) {
  2446. /* specific case: convert to YUV422P first */
  2447. int_pix_fmt = PIX_FMT_YUV422P;
  2448. } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
  2449. dst_pix_fmt == PIX_FMT_UYYVYY411) {
  2450. /* specific case: convert to YUV411P first */
  2451. int_pix_fmt = PIX_FMT_YUV411P;
  2452. } else if ((src_pix->color_type == FF_COLOR_GRAY &&
  2453. src_pix_fmt != PIX_FMT_GRAY8) ||
  2454. (dst_pix->color_type == FF_COLOR_GRAY &&
  2455. dst_pix_fmt != PIX_FMT_GRAY8)) {
  2456. /* gray8 is the normalized format */
  2457. int_pix_fmt = PIX_FMT_GRAY8;
  2458. } else if ((is_yuv_planar(src_pix) &&
  2459. src_pix_fmt != PIX_FMT_YUV444P &&
  2460. src_pix_fmt != PIX_FMT_YUVJ444P)) {
  2461. /* yuv444 is the normalized format */
  2462. if (src_pix->color_type == FF_COLOR_YUV_JPEG)
  2463. int_pix_fmt = PIX_FMT_YUVJ444P;
  2464. else
  2465. int_pix_fmt = PIX_FMT_YUV444P;
  2466. } else if ((is_yuv_planar(dst_pix) &&
  2467. dst_pix_fmt != PIX_FMT_YUV444P &&
  2468. dst_pix_fmt != PIX_FMT_YUVJ444P)) {
  2469. /* yuv444 is the normalized format */
  2470. if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
  2471. int_pix_fmt = PIX_FMT_YUVJ444P;
  2472. else
  2473. int_pix_fmt = PIX_FMT_YUV444P;
  2474. } else {
  2475. /* the two formats are rgb or gray8 or yuv[j]444p */
  2476. if (src_pix->is_alpha && dst_pix->is_alpha)
  2477. int_pix_fmt = PIX_FMT_RGB32;
  2478. else
  2479. int_pix_fmt = PIX_FMT_RGB24;
  2480. }
  2481. if (src_pix_fmt == int_pix_fmt)
  2482. return -1;
  2483. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  2484. return -1;
  2485. ret = -1;
  2486. if (img_convert(tmp, int_pix_fmt,
  2487. src, src_pix_fmt, src_width, src_height) < 0)
  2488. goto fail1;
  2489. if (img_convert(dst, dst_pix_fmt,
  2490. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  2491. goto fail1;
  2492. ret = 0;
  2493. fail1:
  2494. avpicture_free(tmp);
  2495. return ret;
  2496. }
  2497. #endif
  2498. /* NOTE: we scan all the pixels to have an exact information */
  2499. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  2500. {
  2501. const unsigned char *p;
  2502. int src_wrap, ret, x, y;
  2503. unsigned int a;
  2504. uint32_t *palette = (uint32_t *)src->data[1];
  2505. p = src->data[0];
  2506. src_wrap = src->linesize[0] - width;
  2507. ret = 0;
  2508. for(y=0;y<height;y++) {
  2509. for(x=0;x<width;x++) {
  2510. a = palette[p[0]] >> 24;
  2511. if (a == 0x00) {
  2512. ret |= FF_ALPHA_TRANSP;
  2513. } else if (a != 0xff) {
  2514. ret |= FF_ALPHA_SEMI_TRANSP;
  2515. }
  2516. p++;
  2517. }
  2518. p += src_wrap;
  2519. }
  2520. return ret;
  2521. }
  2522. int img_get_alpha_info(const AVPicture *src,
  2523. int pix_fmt, int width, int height)
  2524. {
  2525. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  2526. int ret;
  2527. pf = &pix_fmt_info[pix_fmt];
  2528. /* no alpha can be represented in format */
  2529. if (!pf->is_alpha)
  2530. return 0;
  2531. switch(pix_fmt) {
  2532. case PIX_FMT_RGB32:
  2533. ret = get_alpha_info_rgb32(src, width, height);
  2534. break;
  2535. case PIX_FMT_PAL8:
  2536. ret = get_alpha_info_pal8(src, width, height);
  2537. break;
  2538. default:
  2539. /* we do not know, so everything is indicated */
  2540. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  2541. break;
  2542. }
  2543. return ret;
  2544. }
  2545. #if HAVE_MMX
  2546. #define DEINT_INPLACE_LINE_LUM \
  2547. movd_m2r(lum_m4[0],mm0);\
  2548. movd_m2r(lum_m3[0],mm1);\
  2549. movd_m2r(lum_m2[0],mm2);\
  2550. movd_m2r(lum_m1[0],mm3);\
  2551. movd_m2r(lum[0],mm4);\
  2552. punpcklbw_r2r(mm7,mm0);\
  2553. movd_r2m(mm2,lum_m4[0]);\
  2554. punpcklbw_r2r(mm7,mm1);\
  2555. punpcklbw_r2r(mm7,mm2);\
  2556. punpcklbw_r2r(mm7,mm3);\
  2557. punpcklbw_r2r(mm7,mm4);\
  2558. paddw_r2r(mm3,mm1);\
  2559. psllw_i2r(1,mm2);\
  2560. paddw_r2r(mm4,mm0);\
  2561. psllw_i2r(2,mm1);\
  2562. paddw_r2r(mm6,mm2);\
  2563. paddw_r2r(mm2,mm1);\
  2564. psubusw_r2r(mm0,mm1);\
  2565. psrlw_i2r(3,mm1);\
  2566. packuswb_r2r(mm7,mm1);\
  2567. movd_r2m(mm1,lum_m2[0]);
  2568. #define DEINT_LINE_LUM \
  2569. movd_m2r(lum_m4[0],mm0);\
  2570. movd_m2r(lum_m3[0],mm1);\
  2571. movd_m2r(lum_m2[0],mm2);\
  2572. movd_m2r(lum_m1[0],mm3);\
  2573. movd_m2r(lum[0],mm4);\
  2574. punpcklbw_r2r(mm7,mm0);\
  2575. punpcklbw_r2r(mm7,mm1);\
  2576. punpcklbw_r2r(mm7,mm2);\
  2577. punpcklbw_r2r(mm7,mm3);\
  2578. punpcklbw_r2r(mm7,mm4);\
  2579. paddw_r2r(mm3,mm1);\
  2580. psllw_i2r(1,mm2);\
  2581. paddw_r2r(mm4,mm0);\
  2582. psllw_i2r(2,mm1);\
  2583. paddw_r2r(mm6,mm2);\
  2584. paddw_r2r(mm2,mm1);\
  2585. psubusw_r2r(mm0,mm1);\
  2586. psrlw_i2r(3,mm1);\
  2587. packuswb_r2r(mm7,mm1);\
  2588. movd_r2m(mm1,dst[0]);
  2589. #endif
  2590. /* filter parameters: [-1 4 2 4 -1] // 8 */
  2591. static void deinterlace_line(uint8_t *dst,
  2592. const uint8_t *lum_m4, const uint8_t *lum_m3,
  2593. const uint8_t *lum_m2, const uint8_t *lum_m1,
  2594. const uint8_t *lum,
  2595. int size)
  2596. {
  2597. #if !HAVE_MMX
  2598. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2599. int sum;
  2600. for(;size > 0;size--) {
  2601. sum = -lum_m4[0];
  2602. sum += lum_m3[0] << 2;
  2603. sum += lum_m2[0] << 1;
  2604. sum += lum_m1[0] << 2;
  2605. sum += -lum[0];
  2606. dst[0] = cm[(sum + 4) >> 3];
  2607. lum_m4++;
  2608. lum_m3++;
  2609. lum_m2++;
  2610. lum_m1++;
  2611. lum++;
  2612. dst++;
  2613. }
  2614. #else
  2615. {
  2616. pxor_r2r(mm7,mm7);
  2617. movq_m2r(ff_pw_4,mm6);
  2618. }
  2619. for (;size > 3; size-=4) {
  2620. DEINT_LINE_LUM
  2621. lum_m4+=4;
  2622. lum_m3+=4;
  2623. lum_m2+=4;
  2624. lum_m1+=4;
  2625. lum+=4;
  2626. dst+=4;
  2627. }
  2628. #endif
  2629. }
  2630. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  2631. int size)
  2632. {
  2633. #if !HAVE_MMX
  2634. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2635. int sum;
  2636. for(;size > 0;size--) {
  2637. sum = -lum_m4[0];
  2638. sum += lum_m3[0] << 2;
  2639. sum += lum_m2[0] << 1;
  2640. lum_m4[0]=lum_m2[0];
  2641. sum += lum_m1[0] << 2;
  2642. sum += -lum[0];
  2643. lum_m2[0] = cm[(sum + 4) >> 3];
  2644. lum_m4++;
  2645. lum_m3++;
  2646. lum_m2++;
  2647. lum_m1++;
  2648. lum++;
  2649. }
  2650. #else
  2651. {
  2652. pxor_r2r(mm7,mm7);
  2653. movq_m2r(ff_pw_4,mm6);
  2654. }
  2655. for (;size > 3; size-=4) {
  2656. DEINT_INPLACE_LINE_LUM
  2657. lum_m4+=4;
  2658. lum_m3+=4;
  2659. lum_m2+=4;
  2660. lum_m1+=4;
  2661. lum+=4;
  2662. }
  2663. #endif
  2664. }
  2665. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  2666. top field is copied as is, but the bottom field is deinterlaced
  2667. against the top field. */
  2668. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  2669. const uint8_t *src1, int src_wrap,
  2670. int width, int height)
  2671. {
  2672. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  2673. int y;
  2674. src_m2 = src1;
  2675. src_m1 = src1;
  2676. src_0=&src_m1[src_wrap];
  2677. src_p1=&src_0[src_wrap];
  2678. src_p2=&src_p1[src_wrap];
  2679. for(y=0;y<(height-2);y+=2) {
  2680. memcpy(dst,src_m1,width);
  2681. dst += dst_wrap;
  2682. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  2683. src_m2 = src_0;
  2684. src_m1 = src_p1;
  2685. src_0 = src_p2;
  2686. src_p1 += 2*src_wrap;
  2687. src_p2 += 2*src_wrap;
  2688. dst += dst_wrap;
  2689. }
  2690. memcpy(dst,src_m1,width);
  2691. dst += dst_wrap;
  2692. /* do last line */
  2693. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  2694. }
  2695. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  2696. int width, int height)
  2697. {
  2698. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  2699. int y;
  2700. uint8_t *buf;
  2701. buf = (uint8_t*)av_malloc(width);
  2702. src_m1 = src1;
  2703. memcpy(buf,src_m1,width);
  2704. src_0=&src_m1[src_wrap];
  2705. src_p1=&src_0[src_wrap];
  2706. src_p2=&src_p1[src_wrap];
  2707. for(y=0;y<(height-2);y+=2) {
  2708. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  2709. src_m1 = src_p1;
  2710. src_0 = src_p2;
  2711. src_p1 += 2*src_wrap;
  2712. src_p2 += 2*src_wrap;
  2713. }
  2714. /* do last line */
  2715. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  2716. av_free(buf);
  2717. }
  2718. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  2719. int pix_fmt, int width, int height)
  2720. {
  2721. int i;
  2722. if (pix_fmt != PIX_FMT_YUV420P &&
  2723. pix_fmt != PIX_FMT_YUV422P &&
  2724. pix_fmt != PIX_FMT_YUV444P &&
  2725. pix_fmt != PIX_FMT_YUV411P &&
  2726. pix_fmt != PIX_FMT_GRAY8)
  2727. return -1;
  2728. if ((width & 3) != 0 || (height & 3) != 0)
  2729. return -1;
  2730. for(i=0;i<3;i++) {
  2731. if (i == 1) {
  2732. switch(pix_fmt) {
  2733. case PIX_FMT_YUV420P:
  2734. width >>= 1;
  2735. height >>= 1;
  2736. break;
  2737. case PIX_FMT_YUV422P:
  2738. width >>= 1;
  2739. break;
  2740. case PIX_FMT_YUV411P:
  2741. width >>= 2;
  2742. break;
  2743. default:
  2744. break;
  2745. }
  2746. if (pix_fmt == PIX_FMT_GRAY8) {
  2747. break;
  2748. }
  2749. }
  2750. if (src == dst) {
  2751. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  2752. width, height);
  2753. } else {
  2754. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  2755. src->data[i], src->linesize[i],
  2756. width, height);
  2757. }
  2758. }
  2759. emms_c();
  2760. return 0;
  2761. }