imgconvert.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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
  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 "internal.h"
  33. #include "imgconvert.h"
  34. #include "libavutil/colorspace.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavcore/imgutils.h"
  37. #if HAVE_MMX && HAVE_YASM
  38. #include "x86/dsputil_mmx.h"
  39. #endif
  40. #define xglue(x, y) x ## y
  41. #define glue(x, y) xglue(x, y)
  42. #define FF_COLOR_RGB 0 /**< RGB color space */
  43. #define FF_COLOR_GRAY 1 /**< gray color space */
  44. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  45. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  46. #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
  47. #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
  48. #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
  49. #if HAVE_MMX && HAVE_YASM
  50. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  51. #define deinterlace_line ff_deinterlace_line_mmx
  52. #else
  53. #define deinterlace_line_inplace deinterlace_line_inplace_c
  54. #define deinterlace_line deinterlace_line_c
  55. #endif
  56. typedef struct PixFmtInfo {
  57. uint8_t nb_channels; /**< number of channels (including alpha) */
  58. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  59. uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
  60. uint8_t is_alpha : 1; /**< true if alpha can be specified */
  61. uint8_t depth; /**< bit depth of the color components */
  62. } PixFmtInfo;
  63. /* this table gives more information about formats */
  64. static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  65. /* YUV formats */
  66. [PIX_FMT_YUV420P] = {
  67. .nb_channels = 3,
  68. .color_type = FF_COLOR_YUV,
  69. .pixel_type = FF_PIXEL_PLANAR,
  70. .depth = 8,
  71. },
  72. [PIX_FMT_YUV422P] = {
  73. .nb_channels = 3,
  74. .color_type = FF_COLOR_YUV,
  75. .pixel_type = FF_PIXEL_PLANAR,
  76. .depth = 8,
  77. },
  78. [PIX_FMT_YUV444P] = {
  79. .nb_channels = 3,
  80. .color_type = FF_COLOR_YUV,
  81. .pixel_type = FF_PIXEL_PLANAR,
  82. .depth = 8,
  83. },
  84. [PIX_FMT_YUYV422] = {
  85. .nb_channels = 1,
  86. .color_type = FF_COLOR_YUV,
  87. .pixel_type = FF_PIXEL_PACKED,
  88. .depth = 8,
  89. },
  90. [PIX_FMT_UYVY422] = {
  91. .nb_channels = 1,
  92. .color_type = FF_COLOR_YUV,
  93. .pixel_type = FF_PIXEL_PACKED,
  94. .depth = 8,
  95. },
  96. [PIX_FMT_YUV410P] = {
  97. .nb_channels = 3,
  98. .color_type = FF_COLOR_YUV,
  99. .pixel_type = FF_PIXEL_PLANAR,
  100. .depth = 8,
  101. },
  102. [PIX_FMT_YUV411P] = {
  103. .nb_channels = 3,
  104. .color_type = FF_COLOR_YUV,
  105. .pixel_type = FF_PIXEL_PLANAR,
  106. .depth = 8,
  107. },
  108. [PIX_FMT_YUV440P] = {
  109. .nb_channels = 3,
  110. .color_type = FF_COLOR_YUV,
  111. .pixel_type = FF_PIXEL_PLANAR,
  112. .depth = 8,
  113. },
  114. [PIX_FMT_YUV420P16LE] = {
  115. .nb_channels = 3,
  116. .color_type = FF_COLOR_YUV,
  117. .pixel_type = FF_PIXEL_PLANAR,
  118. .depth = 16,
  119. },
  120. [PIX_FMT_YUV422P16LE] = {
  121. .nb_channels = 3,
  122. .color_type = FF_COLOR_YUV,
  123. .pixel_type = FF_PIXEL_PLANAR,
  124. .depth = 16,
  125. },
  126. [PIX_FMT_YUV444P16LE] = {
  127. .nb_channels = 3,
  128. .color_type = FF_COLOR_YUV,
  129. .pixel_type = FF_PIXEL_PLANAR,
  130. .depth = 16,
  131. },
  132. [PIX_FMT_YUV420P16BE] = {
  133. .nb_channels = 3,
  134. .color_type = FF_COLOR_YUV,
  135. .pixel_type = FF_PIXEL_PLANAR,
  136. .depth = 16,
  137. },
  138. [PIX_FMT_YUV422P16BE] = {
  139. .nb_channels = 3,
  140. .color_type = FF_COLOR_YUV,
  141. .pixel_type = FF_PIXEL_PLANAR,
  142. .depth = 16,
  143. },
  144. [PIX_FMT_YUV444P16BE] = {
  145. .nb_channels = 3,
  146. .color_type = FF_COLOR_YUV,
  147. .pixel_type = FF_PIXEL_PLANAR,
  148. .depth = 16,
  149. },
  150. /* YUV formats with alpha plane */
  151. [PIX_FMT_YUVA420P] = {
  152. .nb_channels = 4,
  153. .color_type = FF_COLOR_YUV,
  154. .pixel_type = FF_PIXEL_PLANAR,
  155. .depth = 8,
  156. },
  157. /* JPEG YUV */
  158. [PIX_FMT_YUVJ420P] = {
  159. .nb_channels = 3,
  160. .color_type = FF_COLOR_YUV_JPEG,
  161. .pixel_type = FF_PIXEL_PLANAR,
  162. .depth = 8,
  163. },
  164. [PIX_FMT_YUVJ422P] = {
  165. .nb_channels = 3,
  166. .color_type = FF_COLOR_YUV_JPEG,
  167. .pixel_type = FF_PIXEL_PLANAR,
  168. .depth = 8,
  169. },
  170. [PIX_FMT_YUVJ444P] = {
  171. .nb_channels = 3,
  172. .color_type = FF_COLOR_YUV_JPEG,
  173. .pixel_type = FF_PIXEL_PLANAR,
  174. .depth = 8,
  175. },
  176. [PIX_FMT_YUVJ440P] = {
  177. .nb_channels = 3,
  178. .color_type = FF_COLOR_YUV_JPEG,
  179. .pixel_type = FF_PIXEL_PLANAR,
  180. .depth = 8,
  181. },
  182. /* RGB formats */
  183. [PIX_FMT_RGB24] = {
  184. .nb_channels = 3,
  185. .color_type = FF_COLOR_RGB,
  186. .pixel_type = FF_PIXEL_PACKED,
  187. .depth = 8,
  188. },
  189. [PIX_FMT_BGR24] = {
  190. .nb_channels = 3,
  191. .color_type = FF_COLOR_RGB,
  192. .pixel_type = FF_PIXEL_PACKED,
  193. .depth = 8,
  194. },
  195. [PIX_FMT_ARGB] = {
  196. .nb_channels = 4, .is_alpha = 1,
  197. .color_type = FF_COLOR_RGB,
  198. .pixel_type = FF_PIXEL_PACKED,
  199. .depth = 8,
  200. },
  201. [PIX_FMT_RGB48BE] = {
  202. .nb_channels = 3,
  203. .color_type = FF_COLOR_RGB,
  204. .pixel_type = FF_PIXEL_PACKED,
  205. .depth = 16,
  206. },
  207. [PIX_FMT_RGB48LE] = {
  208. .nb_channels = 3,
  209. .color_type = FF_COLOR_RGB,
  210. .pixel_type = FF_PIXEL_PACKED,
  211. .depth = 16,
  212. },
  213. [PIX_FMT_RGB565BE] = {
  214. .nb_channels = 3,
  215. .color_type = FF_COLOR_RGB,
  216. .pixel_type = FF_PIXEL_PACKED,
  217. .depth = 5,
  218. },
  219. [PIX_FMT_RGB565LE] = {
  220. .nb_channels = 3,
  221. .color_type = FF_COLOR_RGB,
  222. .pixel_type = FF_PIXEL_PACKED,
  223. .depth = 5,
  224. },
  225. [PIX_FMT_RGB555BE] = {
  226. .nb_channels = 3,
  227. .color_type = FF_COLOR_RGB,
  228. .pixel_type = FF_PIXEL_PACKED,
  229. .depth = 5,
  230. },
  231. [PIX_FMT_RGB555LE] = {
  232. .nb_channels = 3,
  233. .color_type = FF_COLOR_RGB,
  234. .pixel_type = FF_PIXEL_PACKED,
  235. .depth = 5,
  236. },
  237. [PIX_FMT_RGB444BE] = {
  238. .nb_channels = 3,
  239. .color_type = FF_COLOR_RGB,
  240. .pixel_type = FF_PIXEL_PACKED,
  241. .depth = 4,
  242. },
  243. [PIX_FMT_RGB444LE] = {
  244. .nb_channels = 3,
  245. .color_type = FF_COLOR_RGB,
  246. .pixel_type = FF_PIXEL_PACKED,
  247. .depth = 4,
  248. },
  249. /* gray / mono formats */
  250. [PIX_FMT_GRAY16BE] = {
  251. .nb_channels = 1,
  252. .color_type = FF_COLOR_GRAY,
  253. .pixel_type = FF_PIXEL_PLANAR,
  254. .depth = 16,
  255. },
  256. [PIX_FMT_GRAY16LE] = {
  257. .nb_channels = 1,
  258. .color_type = FF_COLOR_GRAY,
  259. .pixel_type = FF_PIXEL_PLANAR,
  260. .depth = 16,
  261. },
  262. [PIX_FMT_GRAY8] = {
  263. .nb_channels = 1,
  264. .color_type = FF_COLOR_GRAY,
  265. .pixel_type = FF_PIXEL_PLANAR,
  266. .depth = 8,
  267. },
  268. [PIX_FMT_MONOWHITE] = {
  269. .nb_channels = 1,
  270. .color_type = FF_COLOR_GRAY,
  271. .pixel_type = FF_PIXEL_PLANAR,
  272. .depth = 1,
  273. },
  274. [PIX_FMT_MONOBLACK] = {
  275. .nb_channels = 1,
  276. .color_type = FF_COLOR_GRAY,
  277. .pixel_type = FF_PIXEL_PLANAR,
  278. .depth = 1,
  279. },
  280. /* paletted formats */
  281. [PIX_FMT_PAL8] = {
  282. .nb_channels = 4, .is_alpha = 1,
  283. .color_type = FF_COLOR_RGB,
  284. .pixel_type = FF_PIXEL_PALETTE,
  285. .depth = 8,
  286. },
  287. [PIX_FMT_UYYVYY411] = {
  288. .nb_channels = 1,
  289. .color_type = FF_COLOR_YUV,
  290. .pixel_type = FF_PIXEL_PACKED,
  291. .depth = 8,
  292. },
  293. [PIX_FMT_ABGR] = {
  294. .nb_channels = 4, .is_alpha = 1,
  295. .color_type = FF_COLOR_RGB,
  296. .pixel_type = FF_PIXEL_PACKED,
  297. .depth = 8,
  298. },
  299. [PIX_FMT_BGR565BE] = {
  300. .nb_channels = 3,
  301. .color_type = FF_COLOR_RGB,
  302. .pixel_type = FF_PIXEL_PACKED,
  303. .depth = 5,
  304. },
  305. [PIX_FMT_BGR565LE] = {
  306. .nb_channels = 3,
  307. .color_type = FF_COLOR_RGB,
  308. .pixel_type = FF_PIXEL_PACKED,
  309. .depth = 5,
  310. },
  311. [PIX_FMT_BGR555BE] = {
  312. .nb_channels = 3,
  313. .color_type = FF_COLOR_RGB,
  314. .pixel_type = FF_PIXEL_PACKED,
  315. .depth = 5,
  316. },
  317. [PIX_FMT_BGR555LE] = {
  318. .nb_channels = 3,
  319. .color_type = FF_COLOR_RGB,
  320. .pixel_type = FF_PIXEL_PACKED,
  321. .depth = 5,
  322. },
  323. [PIX_FMT_BGR444BE] = {
  324. .nb_channels = 3,
  325. .color_type = FF_COLOR_RGB,
  326. .pixel_type = FF_PIXEL_PACKED,
  327. .depth = 4,
  328. },
  329. [PIX_FMT_BGR444LE] = {
  330. .nb_channels = 3,
  331. .color_type = FF_COLOR_RGB,
  332. .pixel_type = FF_PIXEL_PACKED,
  333. .depth = 4,
  334. },
  335. [PIX_FMT_RGB8] = {
  336. .nb_channels = 1,
  337. .color_type = FF_COLOR_RGB,
  338. .pixel_type = FF_PIXEL_PACKED,
  339. .depth = 8,
  340. },
  341. [PIX_FMT_RGB4] = {
  342. .nb_channels = 1,
  343. .color_type = FF_COLOR_RGB,
  344. .pixel_type = FF_PIXEL_PACKED,
  345. .depth = 4,
  346. },
  347. [PIX_FMT_RGB4_BYTE] = {
  348. .nb_channels = 1,
  349. .color_type = FF_COLOR_RGB,
  350. .pixel_type = FF_PIXEL_PACKED,
  351. .depth = 8,
  352. },
  353. [PIX_FMT_BGR8] = {
  354. .nb_channels = 1,
  355. .color_type = FF_COLOR_RGB,
  356. .pixel_type = FF_PIXEL_PACKED,
  357. .depth = 8,
  358. },
  359. [PIX_FMT_BGR4] = {
  360. .nb_channels = 1,
  361. .color_type = FF_COLOR_RGB,
  362. .pixel_type = FF_PIXEL_PACKED,
  363. .depth = 4,
  364. },
  365. [PIX_FMT_BGR4_BYTE] = {
  366. .nb_channels = 1,
  367. .color_type = FF_COLOR_RGB,
  368. .pixel_type = FF_PIXEL_PACKED,
  369. .depth = 8,
  370. },
  371. [PIX_FMT_NV12] = {
  372. .nb_channels = 2,
  373. .color_type = FF_COLOR_YUV,
  374. .pixel_type = FF_PIXEL_PLANAR,
  375. .depth = 8,
  376. },
  377. [PIX_FMT_NV21] = {
  378. .nb_channels = 2,
  379. .color_type = FF_COLOR_YUV,
  380. .pixel_type = FF_PIXEL_PLANAR,
  381. .depth = 8,
  382. },
  383. [PIX_FMT_BGRA] = {
  384. .nb_channels = 4, .is_alpha = 1,
  385. .color_type = FF_COLOR_RGB,
  386. .pixel_type = FF_PIXEL_PACKED,
  387. .depth = 8,
  388. },
  389. [PIX_FMT_RGBA] = {
  390. .nb_channels = 4, .is_alpha = 1,
  391. .color_type = FF_COLOR_RGB,
  392. .pixel_type = FF_PIXEL_PACKED,
  393. .depth = 8,
  394. },
  395. };
  396. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  397. {
  398. *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  399. *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  400. }
  401. const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
  402. {
  403. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  404. return NULL;
  405. else
  406. return av_pix_fmt_descriptors[pix_fmt].name;
  407. }
  408. #if LIBAVCODEC_VERSION_MAJOR < 53
  409. enum PixelFormat avcodec_get_pix_fmt(const char *name)
  410. {
  411. return av_get_pix_fmt(name);
  412. }
  413. #endif
  414. void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
  415. {
  416. /* print header */
  417. if (pix_fmt < 0)
  418. snprintf (buf, buf_size,
  419. "name " " nb_channels" " depth" " is_alpha"
  420. );
  421. else{
  422. PixFmtInfo info= pix_fmt_info[pix_fmt];
  423. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  424. snprintf (buf, buf_size,
  425. "%-11s %5d %9d %6c",
  426. av_pix_fmt_descriptors[pix_fmt].name,
  427. info.nb_channels,
  428. info.depth,
  429. is_alpha_char
  430. );
  431. }
  432. }
  433. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  434. {
  435. return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
  436. }
  437. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  438. int i;
  439. for(i=0; i<256; i++){
  440. int r,g,b;
  441. switch(pix_fmt) {
  442. case PIX_FMT_RGB8:
  443. r= (i>>5 )*36;
  444. g= ((i>>2)&7)*36;
  445. b= (i&3 )*85;
  446. break;
  447. case PIX_FMT_BGR8:
  448. b= (i>>6 )*85;
  449. g= ((i>>3)&7)*36;
  450. r= (i&7 )*36;
  451. break;
  452. case PIX_FMT_RGB4_BYTE:
  453. r= (i>>3 )*255;
  454. g= ((i>>1)&3)*85;
  455. b= (i&1 )*255;
  456. break;
  457. case PIX_FMT_BGR4_BYTE:
  458. b= (i>>3 )*255;
  459. g= ((i>>1)&3)*85;
  460. r= (i&1 )*255;
  461. break;
  462. case PIX_FMT_GRAY8:
  463. r=b=g= i;
  464. break;
  465. default:
  466. return -1;
  467. }
  468. pal[i] = b + (g<<8) + (r<<16);
  469. }
  470. return 0;
  471. }
  472. #if LIBAVCODEC_VERSION_MAJOR < 53
  473. int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
  474. {
  475. return av_image_fill_linesizes(picture->linesize, pix_fmt, width);
  476. }
  477. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
  478. int height)
  479. {
  480. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  481. }
  482. #endif
  483. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  484. enum PixelFormat pix_fmt, int width, int height)
  485. {
  486. if(av_image_check_size(width, height, 0, NULL))
  487. return -1;
  488. if (av_image_fill_linesizes(picture->linesize, pix_fmt, width))
  489. return -1;
  490. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  491. }
  492. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  493. unsigned char *dest, int dest_size)
  494. {
  495. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  496. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  497. int i, j, w, ow, h, oh, data_planes;
  498. const unsigned char* s;
  499. int size = avpicture_get_size(pix_fmt, width, height);
  500. if (size > dest_size || size < 0)
  501. return -1;
  502. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  503. if (pix_fmt == PIX_FMT_YUYV422 ||
  504. pix_fmt == PIX_FMT_UYVY422 ||
  505. pix_fmt == PIX_FMT_BGR565BE ||
  506. pix_fmt == PIX_FMT_BGR565LE ||
  507. pix_fmt == PIX_FMT_BGR555BE ||
  508. pix_fmt == PIX_FMT_BGR555LE ||
  509. pix_fmt == PIX_FMT_BGR444BE ||
  510. pix_fmt == PIX_FMT_BGR444LE ||
  511. pix_fmt == PIX_FMT_RGB565BE ||
  512. pix_fmt == PIX_FMT_RGB565LE ||
  513. pix_fmt == PIX_FMT_RGB555BE ||
  514. pix_fmt == PIX_FMT_RGB555LE ||
  515. pix_fmt == PIX_FMT_RGB444BE ||
  516. pix_fmt == PIX_FMT_RGB444LE)
  517. w = width * 2;
  518. else if (pix_fmt == PIX_FMT_UYYVYY411)
  519. w = width + width/2;
  520. else if (pix_fmt == PIX_FMT_PAL8)
  521. w = width;
  522. else
  523. w = width * (pf->depth * pf->nb_channels / 8);
  524. data_planes = 1;
  525. h = height;
  526. } else {
  527. data_planes = pf->nb_channels;
  528. w = (width*pf->depth + 7)/8;
  529. h = height;
  530. }
  531. ow = w;
  532. oh = h;
  533. for (i=0; i<data_planes; i++) {
  534. if (i == 1) {
  535. w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
  536. h = -((-height) >> desc->log2_chroma_h);
  537. if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
  538. w <<= 1;
  539. } else if (i == 3) {
  540. w = ow;
  541. h = oh;
  542. }
  543. s = src->data[i];
  544. for(j=0; j<h; j++) {
  545. memcpy(dest, s, w);
  546. dest += w;
  547. s += src->linesize[i];
  548. }
  549. }
  550. if (pf->pixel_type == FF_PIXEL_PALETTE)
  551. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  552. return size;
  553. }
  554. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  555. {
  556. AVPicture dummy_pict;
  557. if(av_image_check_size(width, height, 0, NULL))
  558. return -1;
  559. switch (pix_fmt) {
  560. case PIX_FMT_RGB8:
  561. case PIX_FMT_BGR8:
  562. case PIX_FMT_RGB4_BYTE:
  563. case PIX_FMT_BGR4_BYTE:
  564. case PIX_FMT_GRAY8:
  565. // do not include palette for these pseudo-paletted formats
  566. return width * height;
  567. }
  568. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  569. }
  570. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  571. int has_alpha)
  572. {
  573. const PixFmtInfo *pf, *ps;
  574. const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  575. const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  576. int loss;
  577. ps = &pix_fmt_info[src_pix_fmt];
  578. /* compute loss */
  579. loss = 0;
  580. pf = &pix_fmt_info[dst_pix_fmt];
  581. if (pf->depth < ps->depth ||
  582. ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
  583. dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
  584. (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
  585. src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
  586. loss |= FF_LOSS_DEPTH;
  587. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  588. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  589. loss |= FF_LOSS_RESOLUTION;
  590. switch(pf->color_type) {
  591. case FF_COLOR_RGB:
  592. if (ps->color_type != FF_COLOR_RGB &&
  593. ps->color_type != FF_COLOR_GRAY)
  594. loss |= FF_LOSS_COLORSPACE;
  595. break;
  596. case FF_COLOR_GRAY:
  597. if (ps->color_type != FF_COLOR_GRAY)
  598. loss |= FF_LOSS_COLORSPACE;
  599. break;
  600. case FF_COLOR_YUV:
  601. if (ps->color_type != FF_COLOR_YUV)
  602. loss |= FF_LOSS_COLORSPACE;
  603. break;
  604. case FF_COLOR_YUV_JPEG:
  605. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  606. ps->color_type != FF_COLOR_YUV &&
  607. ps->color_type != FF_COLOR_GRAY)
  608. loss |= FF_LOSS_COLORSPACE;
  609. break;
  610. default:
  611. /* fail safe test */
  612. if (ps->color_type != pf->color_type)
  613. loss |= FF_LOSS_COLORSPACE;
  614. break;
  615. }
  616. if (pf->color_type == FF_COLOR_GRAY &&
  617. ps->color_type != FF_COLOR_GRAY)
  618. loss |= FF_LOSS_CHROMA;
  619. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  620. loss |= FF_LOSS_ALPHA;
  621. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  622. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  623. loss |= FF_LOSS_COLORQUANT;
  624. return loss;
  625. }
  626. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  627. {
  628. int bits;
  629. const PixFmtInfo *pf;
  630. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  631. pf = &pix_fmt_info[pix_fmt];
  632. switch(pf->pixel_type) {
  633. case FF_PIXEL_PACKED:
  634. switch(pix_fmt) {
  635. case PIX_FMT_YUYV422:
  636. case PIX_FMT_UYVY422:
  637. case PIX_FMT_RGB565BE:
  638. case PIX_FMT_RGB565LE:
  639. case PIX_FMT_RGB555BE:
  640. case PIX_FMT_RGB555LE:
  641. case PIX_FMT_RGB444BE:
  642. case PIX_FMT_RGB444LE:
  643. case PIX_FMT_BGR565BE:
  644. case PIX_FMT_BGR565LE:
  645. case PIX_FMT_BGR555BE:
  646. case PIX_FMT_BGR555LE:
  647. case PIX_FMT_BGR444BE:
  648. case PIX_FMT_BGR444LE:
  649. bits = 16;
  650. break;
  651. case PIX_FMT_UYYVYY411:
  652. bits = 12;
  653. break;
  654. default:
  655. bits = pf->depth * pf->nb_channels;
  656. break;
  657. }
  658. break;
  659. case FF_PIXEL_PLANAR:
  660. if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  661. bits = pf->depth * pf->nb_channels;
  662. } else {
  663. bits = pf->depth + ((2 * pf->depth) >>
  664. (desc->log2_chroma_w + desc->log2_chroma_h));
  665. }
  666. break;
  667. case FF_PIXEL_PALETTE:
  668. bits = 8;
  669. break;
  670. default:
  671. bits = -1;
  672. break;
  673. }
  674. return bits;
  675. }
  676. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  677. enum PixelFormat src_pix_fmt,
  678. int has_alpha,
  679. int loss_mask)
  680. {
  681. int dist, i, loss, min_dist;
  682. enum PixelFormat dst_pix_fmt;
  683. /* find exact color match with smallest size */
  684. dst_pix_fmt = PIX_FMT_NONE;
  685. min_dist = 0x7fffffff;
  686. for(i = 0;i < PIX_FMT_NB; i++) {
  687. if (pix_fmt_mask & (1ULL << i)) {
  688. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  689. if (loss == 0) {
  690. dist = avg_bits_per_pixel(i);
  691. if (dist < min_dist) {
  692. min_dist = dist;
  693. dst_pix_fmt = i;
  694. }
  695. }
  696. }
  697. }
  698. return dst_pix_fmt;
  699. }
  700. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  701. int has_alpha, int *loss_ptr)
  702. {
  703. enum PixelFormat dst_pix_fmt;
  704. int loss_mask, i;
  705. static const int loss_mask_order[] = {
  706. ~0, /* no loss first */
  707. ~FF_LOSS_ALPHA,
  708. ~FF_LOSS_RESOLUTION,
  709. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  710. ~FF_LOSS_COLORQUANT,
  711. ~FF_LOSS_DEPTH,
  712. 0,
  713. };
  714. /* try with successive loss */
  715. i = 0;
  716. for(;;) {
  717. loss_mask = loss_mask_order[i++];
  718. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  719. has_alpha, loss_mask);
  720. if (dst_pix_fmt >= 0)
  721. goto found;
  722. if (loss_mask == 0)
  723. break;
  724. }
  725. return PIX_FMT_NONE;
  726. found:
  727. if (loss_ptr)
  728. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  729. return dst_pix_fmt;
  730. }
  731. #if LIBAVCODEC_VERSION_MAJOR < 53
  732. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  733. const uint8_t *src, int src_wrap,
  734. int width, int height)
  735. {
  736. av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
  737. }
  738. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  739. {
  740. return av_image_get_linesize(pix_fmt, width, plane);
  741. }
  742. #endif
  743. void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
  744. uint8_t *src_data[4], int src_linesize[4],
  745. enum PixelFormat pix_fmt, int width, int height)
  746. {
  747. int i;
  748. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  749. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  750. switch(pf->pixel_type) {
  751. case FF_PIXEL_PACKED:
  752. case FF_PIXEL_PLANAR:
  753. for(i = 0; i < pf->nb_channels; i++) {
  754. int h;
  755. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  756. h = height;
  757. if (i == 1 || i == 2) {
  758. h= -((-height)>>desc->log2_chroma_h);
  759. }
  760. av_image_copy_plane(dst_data[i], dst_linesize[i],
  761. src_data[i], src_linesize[i],
  762. bwidth, h);
  763. }
  764. break;
  765. case FF_PIXEL_PALETTE:
  766. av_image_copy_plane(dst_data[0], dst_linesize[0],
  767. src_data[0], src_linesize[0],
  768. width, height);
  769. /* copy the palette */
  770. memcpy(dst_data[1], src_data[1], 4*256);
  771. break;
  772. }
  773. }
  774. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  775. enum PixelFormat pix_fmt, int width, int height)
  776. {
  777. av_picture_data_copy(dst->data, dst->linesize, src->data,
  778. src->linesize, pix_fmt, width, height);
  779. }
  780. /* 2x2 -> 1x1 */
  781. void ff_shrink22(uint8_t *dst, int dst_wrap,
  782. const uint8_t *src, int src_wrap,
  783. int width, int height)
  784. {
  785. int w;
  786. const uint8_t *s1, *s2;
  787. uint8_t *d;
  788. for(;height > 0; height--) {
  789. s1 = src;
  790. s2 = s1 + src_wrap;
  791. d = dst;
  792. for(w = width;w >= 4; w-=4) {
  793. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  794. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  795. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  796. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  797. s1 += 8;
  798. s2 += 8;
  799. d += 4;
  800. }
  801. for(;w > 0; w--) {
  802. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  803. s1 += 2;
  804. s2 += 2;
  805. d++;
  806. }
  807. src += 2 * src_wrap;
  808. dst += dst_wrap;
  809. }
  810. }
  811. /* 4x4 -> 1x1 */
  812. void ff_shrink44(uint8_t *dst, int dst_wrap,
  813. const uint8_t *src, int src_wrap,
  814. int width, int height)
  815. {
  816. int w;
  817. const uint8_t *s1, *s2, *s3, *s4;
  818. uint8_t *d;
  819. for(;height > 0; height--) {
  820. s1 = src;
  821. s2 = s1 + src_wrap;
  822. s3 = s2 + src_wrap;
  823. s4 = s3 + src_wrap;
  824. d = dst;
  825. for(w = width;w > 0; w--) {
  826. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  827. s2[0] + s2[1] + s2[2] + s2[3] +
  828. s3[0] + s3[1] + s3[2] + s3[3] +
  829. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  830. s1 += 4;
  831. s2 += 4;
  832. s3 += 4;
  833. s4 += 4;
  834. d++;
  835. }
  836. src += 4 * src_wrap;
  837. dst += dst_wrap;
  838. }
  839. }
  840. /* 8x8 -> 1x1 */
  841. void ff_shrink88(uint8_t *dst, int dst_wrap,
  842. const uint8_t *src, int src_wrap,
  843. int width, int height)
  844. {
  845. int w, i;
  846. for(;height > 0; height--) {
  847. for(w = width;w > 0; w--) {
  848. int tmp=0;
  849. for(i=0; i<8; i++){
  850. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  851. src += src_wrap;
  852. }
  853. *(dst++) = (tmp + 32)>>6;
  854. src += 8 - 8*src_wrap;
  855. }
  856. src += 8*src_wrap - 8*width;
  857. dst += dst_wrap - width;
  858. }
  859. }
  860. int avpicture_alloc(AVPicture *picture,
  861. enum PixelFormat pix_fmt, int width, int height)
  862. {
  863. int size;
  864. void *ptr;
  865. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  866. if(size<0)
  867. goto fail;
  868. ptr = av_malloc(size);
  869. if (!ptr)
  870. goto fail;
  871. avpicture_fill(picture, ptr, pix_fmt, width, height);
  872. if(picture->data[1] && !picture->data[2])
  873. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  874. return 0;
  875. fail:
  876. memset(picture, 0, sizeof(AVPicture));
  877. return -1;
  878. }
  879. void avpicture_free(AVPicture *picture)
  880. {
  881. av_free(picture->data[0]);
  882. }
  883. /* return true if yuv planar */
  884. static inline int is_yuv_planar(const PixFmtInfo *ps)
  885. {
  886. return (ps->color_type == FF_COLOR_YUV ||
  887. ps->color_type == FF_COLOR_YUV_JPEG) &&
  888. ps->pixel_type == FF_PIXEL_PLANAR;
  889. }
  890. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  891. enum PixelFormat pix_fmt, int top_band, int left_band)
  892. {
  893. int y_shift;
  894. int x_shift;
  895. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  896. return -1;
  897. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  898. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  899. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  900. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  901. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  902. dst->linesize[0] = src->linesize[0];
  903. dst->linesize[1] = src->linesize[1];
  904. dst->linesize[2] = src->linesize[2];
  905. return 0;
  906. }
  907. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  908. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  909. int *color)
  910. {
  911. uint8_t *optr;
  912. int y_shift;
  913. int x_shift;
  914. int yheight;
  915. int i, y;
  916. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  917. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  918. for (i = 0; i < 3; i++) {
  919. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  920. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  921. if (padtop || padleft) {
  922. memset(dst->data[i], color[i],
  923. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  924. }
  925. if (padleft || padright) {
  926. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  927. (dst->linesize[i] - (padright >> x_shift));
  928. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  929. for (y = 0; y < yheight; y++) {
  930. memset(optr, color[i], (padleft + padright) >> x_shift);
  931. optr += dst->linesize[i];
  932. }
  933. }
  934. if (src) { /* first line */
  935. uint8_t *iptr = src->data[i];
  936. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  937. (padleft >> x_shift);
  938. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  939. iptr += src->linesize[i];
  940. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  941. (dst->linesize[i] - (padright >> x_shift));
  942. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  943. for (y = 0; y < yheight; y++) {
  944. memset(optr, color[i], (padleft + padright) >> x_shift);
  945. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  946. (width - padleft - padright) >> x_shift);
  947. iptr += src->linesize[i];
  948. optr += dst->linesize[i];
  949. }
  950. }
  951. if (padbottom || padright) {
  952. optr = dst->data[i] + dst->linesize[i] *
  953. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  954. memset(optr, color[i],dst->linesize[i] *
  955. (padbottom >> y_shift) + (padright >> x_shift));
  956. }
  957. }
  958. return 0;
  959. }
  960. /* NOTE: we scan all the pixels to have an exact information */
  961. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  962. {
  963. const unsigned char *p;
  964. int src_wrap, ret, x, y;
  965. unsigned int a;
  966. uint32_t *palette = (uint32_t *)src->data[1];
  967. p = src->data[0];
  968. src_wrap = src->linesize[0] - width;
  969. ret = 0;
  970. for(y=0;y<height;y++) {
  971. for(x=0;x<width;x++) {
  972. a = palette[p[0]] >> 24;
  973. if (a == 0x00) {
  974. ret |= FF_ALPHA_TRANSP;
  975. } else if (a != 0xff) {
  976. ret |= FF_ALPHA_SEMI_TRANSP;
  977. }
  978. p++;
  979. }
  980. p += src_wrap;
  981. }
  982. return ret;
  983. }
  984. int img_get_alpha_info(const AVPicture *src,
  985. enum PixelFormat pix_fmt, int width, int height)
  986. {
  987. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  988. int ret;
  989. /* no alpha can be represented in format */
  990. if (!pf->is_alpha)
  991. return 0;
  992. switch(pix_fmt) {
  993. case PIX_FMT_PAL8:
  994. ret = get_alpha_info_pal8(src, width, height);
  995. break;
  996. default:
  997. /* we do not know, so everything is indicated */
  998. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  999. break;
  1000. }
  1001. return ret;
  1002. }
  1003. #if !(HAVE_MMX && HAVE_YASM)
  1004. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1005. static void deinterlace_line_c(uint8_t *dst,
  1006. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1007. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1008. const uint8_t *lum,
  1009. int size)
  1010. {
  1011. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1012. int sum;
  1013. for(;size > 0;size--) {
  1014. sum = -lum_m4[0];
  1015. sum += lum_m3[0] << 2;
  1016. sum += lum_m2[0] << 1;
  1017. sum += lum_m1[0] << 2;
  1018. sum += -lum[0];
  1019. dst[0] = cm[(sum + 4) >> 3];
  1020. lum_m4++;
  1021. lum_m3++;
  1022. lum_m2++;
  1023. lum_m1++;
  1024. lum++;
  1025. dst++;
  1026. }
  1027. }
  1028. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  1029. uint8_t *lum_m2, uint8_t *lum_m1,
  1030. uint8_t *lum, int size)
  1031. {
  1032. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1033. int sum;
  1034. for(;size > 0;size--) {
  1035. sum = -lum_m4[0];
  1036. sum += lum_m3[0] << 2;
  1037. sum += lum_m2[0] << 1;
  1038. lum_m4[0]=lum_m2[0];
  1039. sum += lum_m1[0] << 2;
  1040. sum += -lum[0];
  1041. lum_m2[0] = cm[(sum + 4) >> 3];
  1042. lum_m4++;
  1043. lum_m3++;
  1044. lum_m2++;
  1045. lum_m1++;
  1046. lum++;
  1047. }
  1048. }
  1049. #endif
  1050. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1051. top field is copied as is, but the bottom field is deinterlaced
  1052. against the top field. */
  1053. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1054. const uint8_t *src1, int src_wrap,
  1055. int width, int height)
  1056. {
  1057. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1058. int y;
  1059. src_m2 = src1;
  1060. src_m1 = src1;
  1061. src_0=&src_m1[src_wrap];
  1062. src_p1=&src_0[src_wrap];
  1063. src_p2=&src_p1[src_wrap];
  1064. for(y=0;y<(height-2);y+=2) {
  1065. memcpy(dst,src_m1,width);
  1066. dst += dst_wrap;
  1067. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1068. src_m2 = src_0;
  1069. src_m1 = src_p1;
  1070. src_0 = src_p2;
  1071. src_p1 += 2*src_wrap;
  1072. src_p2 += 2*src_wrap;
  1073. dst += dst_wrap;
  1074. }
  1075. memcpy(dst,src_m1,width);
  1076. dst += dst_wrap;
  1077. /* do last line */
  1078. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1079. }
  1080. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1081. int width, int height)
  1082. {
  1083. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1084. int y;
  1085. uint8_t *buf;
  1086. buf = (uint8_t*)av_malloc(width);
  1087. src_m1 = src1;
  1088. memcpy(buf,src_m1,width);
  1089. src_0=&src_m1[src_wrap];
  1090. src_p1=&src_0[src_wrap];
  1091. src_p2=&src_p1[src_wrap];
  1092. for(y=0;y<(height-2);y+=2) {
  1093. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1094. src_m1 = src_p1;
  1095. src_0 = src_p2;
  1096. src_p1 += 2*src_wrap;
  1097. src_p2 += 2*src_wrap;
  1098. }
  1099. /* do last line */
  1100. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1101. av_free(buf);
  1102. }
  1103. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1104. enum PixelFormat pix_fmt, int width, int height)
  1105. {
  1106. int i;
  1107. if (pix_fmt != PIX_FMT_YUV420P &&
  1108. pix_fmt != PIX_FMT_YUV422P &&
  1109. pix_fmt != PIX_FMT_YUV444P &&
  1110. pix_fmt != PIX_FMT_YUV411P &&
  1111. pix_fmt != PIX_FMT_GRAY8)
  1112. return -1;
  1113. if ((width & 3) != 0 || (height & 3) != 0)
  1114. return -1;
  1115. for(i=0;i<3;i++) {
  1116. if (i == 1) {
  1117. switch(pix_fmt) {
  1118. case PIX_FMT_YUV420P:
  1119. width >>= 1;
  1120. height >>= 1;
  1121. break;
  1122. case PIX_FMT_YUV422P:
  1123. width >>= 1;
  1124. break;
  1125. case PIX_FMT_YUV411P:
  1126. width >>= 2;
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. if (pix_fmt == PIX_FMT_GRAY8) {
  1132. break;
  1133. }
  1134. }
  1135. if (src == dst) {
  1136. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1137. width, height);
  1138. } else {
  1139. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1140. src->data[i], src->linesize[i],
  1141. width, height);
  1142. }
  1143. }
  1144. emms_c();
  1145. return 0;
  1146. }