imgconvert.c 77 KB

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