You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1529 lines
43KB

  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/imgconvert.c
  23. * misc image conversion routines
  24. */
  25. /* TODO:
  26. * - write 'ffimg' program to test all the image related stuff
  27. * - move all api to slice based system
  28. * - integrate deinterlacing, postprocessing and scaling in the conversion process
  29. */
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "colorspace.h"
  33. #if HAVE_MMX
  34. #include "x86/mmx.h"
  35. #include "x86/dsputil_mmx.h"
  36. #endif
  37. #define xglue(x, y) x ## y
  38. #define glue(x, y) xglue(x, y)
  39. #define FF_COLOR_RGB 0 /**< RGB color space */
  40. #define FF_COLOR_GRAY 1 /**< gray color space */
  41. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  42. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  43. #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
  44. #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
  45. #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
  46. typedef struct PixFmtInfo {
  47. const char *name;
  48. uint8_t nb_channels; /**< number of channels (including alpha) */
  49. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  50. uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
  51. uint8_t is_alpha : 1; /**< true if alpha can be specified */
  52. uint8_t is_hwaccel : 1; /**< true if this is an HW accelerated format */
  53. uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
  54. uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
  55. uint8_t depth; /**< bit depth of the color components */
  56. } PixFmtInfo;
  57. /* this table gives more information about formats */
  58. static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  59. /* YUV formats */
  60. [PIX_FMT_YUV420P] = {
  61. .name = "yuv420p",
  62. .nb_channels = 3,
  63. .color_type = FF_COLOR_YUV,
  64. .pixel_type = FF_PIXEL_PLANAR,
  65. .depth = 8,
  66. .x_chroma_shift = 1, .y_chroma_shift = 1,
  67. },
  68. [PIX_FMT_YUV422P] = {
  69. .name = "yuv422p",
  70. .nb_channels = 3,
  71. .color_type = FF_COLOR_YUV,
  72. .pixel_type = FF_PIXEL_PLANAR,
  73. .depth = 8,
  74. .x_chroma_shift = 1, .y_chroma_shift = 0,
  75. },
  76. [PIX_FMT_YUV444P] = {
  77. .name = "yuv444p",
  78. .nb_channels = 3,
  79. .color_type = FF_COLOR_YUV,
  80. .pixel_type = FF_PIXEL_PLANAR,
  81. .depth = 8,
  82. .x_chroma_shift = 0, .y_chroma_shift = 0,
  83. },
  84. [PIX_FMT_YUYV422] = {
  85. .name = "yuyv422",
  86. .nb_channels = 1,
  87. .color_type = FF_COLOR_YUV,
  88. .pixel_type = FF_PIXEL_PACKED,
  89. .depth = 8,
  90. .x_chroma_shift = 1, .y_chroma_shift = 0,
  91. },
  92. [PIX_FMT_UYVY422] = {
  93. .name = "uyvy422",
  94. .nb_channels = 1,
  95. .color_type = FF_COLOR_YUV,
  96. .pixel_type = FF_PIXEL_PACKED,
  97. .depth = 8,
  98. .x_chroma_shift = 1, .y_chroma_shift = 0,
  99. },
  100. [PIX_FMT_YUV410P] = {
  101. .name = "yuv410p",
  102. .nb_channels = 3,
  103. .color_type = FF_COLOR_YUV,
  104. .pixel_type = FF_PIXEL_PLANAR,
  105. .depth = 8,
  106. .x_chroma_shift = 2, .y_chroma_shift = 2,
  107. },
  108. [PIX_FMT_YUV411P] = {
  109. .name = "yuv411p",
  110. .nb_channels = 3,
  111. .color_type = FF_COLOR_YUV,
  112. .pixel_type = FF_PIXEL_PLANAR,
  113. .depth = 8,
  114. .x_chroma_shift = 2, .y_chroma_shift = 0,
  115. },
  116. [PIX_FMT_YUV440P] = {
  117. .name = "yuv440p",
  118. .nb_channels = 3,
  119. .color_type = FF_COLOR_YUV,
  120. .pixel_type = FF_PIXEL_PLANAR,
  121. .depth = 8,
  122. .x_chroma_shift = 0, .y_chroma_shift = 1,
  123. },
  124. /* YUV formats with alpha plane */
  125. [PIX_FMT_YUVA420P] = {
  126. .name = "yuva420p",
  127. .nb_channels = 4,
  128. .color_type = FF_COLOR_YUV,
  129. .pixel_type = FF_PIXEL_PLANAR,
  130. .depth = 8,
  131. .x_chroma_shift = 1, .y_chroma_shift = 1,
  132. },
  133. /* JPEG YUV */
  134. [PIX_FMT_YUVJ420P] = {
  135. .name = "yuvj420p",
  136. .nb_channels = 3,
  137. .color_type = FF_COLOR_YUV_JPEG,
  138. .pixel_type = FF_PIXEL_PLANAR,
  139. .depth = 8,
  140. .x_chroma_shift = 1, .y_chroma_shift = 1,
  141. },
  142. [PIX_FMT_YUVJ422P] = {
  143. .name = "yuvj422p",
  144. .nb_channels = 3,
  145. .color_type = FF_COLOR_YUV_JPEG,
  146. .pixel_type = FF_PIXEL_PLANAR,
  147. .depth = 8,
  148. .x_chroma_shift = 1, .y_chroma_shift = 0,
  149. },
  150. [PIX_FMT_YUVJ444P] = {
  151. .name = "yuvj444p",
  152. .nb_channels = 3,
  153. .color_type = FF_COLOR_YUV_JPEG,
  154. .pixel_type = FF_PIXEL_PLANAR,
  155. .depth = 8,
  156. .x_chroma_shift = 0, .y_chroma_shift = 0,
  157. },
  158. [PIX_FMT_YUVJ440P] = {
  159. .name = "yuvj440p",
  160. .nb_channels = 3,
  161. .color_type = FF_COLOR_YUV_JPEG,
  162. .pixel_type = FF_PIXEL_PLANAR,
  163. .depth = 8,
  164. .x_chroma_shift = 0, .y_chroma_shift = 1,
  165. },
  166. /* RGB formats */
  167. [PIX_FMT_RGB24] = {
  168. .name = "rgb24",
  169. .nb_channels = 3,
  170. .color_type = FF_COLOR_RGB,
  171. .pixel_type = FF_PIXEL_PACKED,
  172. .depth = 8,
  173. .x_chroma_shift = 0, .y_chroma_shift = 0,
  174. },
  175. [PIX_FMT_BGR24] = {
  176. .name = "bgr24",
  177. .nb_channels = 3,
  178. .color_type = FF_COLOR_RGB,
  179. .pixel_type = FF_PIXEL_PACKED,
  180. .depth = 8,
  181. .x_chroma_shift = 0, .y_chroma_shift = 0,
  182. },
  183. [PIX_FMT_RGB32] = {
  184. .name = "rgb32",
  185. .nb_channels = 4, .is_alpha = 1,
  186. .color_type = FF_COLOR_RGB,
  187. .pixel_type = FF_PIXEL_PACKED,
  188. .depth = 8,
  189. .x_chroma_shift = 0, .y_chroma_shift = 0,
  190. },
  191. [PIX_FMT_RGB48BE] = {
  192. .name = "rgb48be",
  193. .nb_channels = 3,
  194. .color_type = FF_COLOR_RGB,
  195. .pixel_type = FF_PIXEL_PACKED,
  196. .depth = 16,
  197. .x_chroma_shift = 0, .y_chroma_shift = 0,
  198. },
  199. [PIX_FMT_RGB48LE] = {
  200. .name = "rgb48le",
  201. .nb_channels = 3,
  202. .color_type = FF_COLOR_RGB,
  203. .pixel_type = FF_PIXEL_PACKED,
  204. .depth = 16,
  205. .x_chroma_shift = 0, .y_chroma_shift = 0,
  206. },
  207. [PIX_FMT_RGB565] = {
  208. .name = "rgb565",
  209. .nb_channels = 3,
  210. .color_type = FF_COLOR_RGB,
  211. .pixel_type = FF_PIXEL_PACKED,
  212. .depth = 5,
  213. .x_chroma_shift = 0, .y_chroma_shift = 0,
  214. },
  215. [PIX_FMT_RGB555] = {
  216. .name = "rgb555",
  217. .nb_channels = 3,
  218. .color_type = FF_COLOR_RGB,
  219. .pixel_type = FF_PIXEL_PACKED,
  220. .depth = 5,
  221. .x_chroma_shift = 0, .y_chroma_shift = 0,
  222. },
  223. /* gray / mono formats */
  224. [PIX_FMT_GRAY16BE] = {
  225. .name = "gray16be",
  226. .nb_channels = 1,
  227. .color_type = FF_COLOR_GRAY,
  228. .pixel_type = FF_PIXEL_PLANAR,
  229. .depth = 16,
  230. },
  231. [PIX_FMT_GRAY16LE] = {
  232. .name = "gray16le",
  233. .nb_channels = 1,
  234. .color_type = FF_COLOR_GRAY,
  235. .pixel_type = FF_PIXEL_PLANAR,
  236. .depth = 16,
  237. },
  238. [PIX_FMT_GRAY8] = {
  239. .name = "gray",
  240. .nb_channels = 1,
  241. .color_type = FF_COLOR_GRAY,
  242. .pixel_type = FF_PIXEL_PLANAR,
  243. .depth = 8,
  244. },
  245. [PIX_FMT_MONOWHITE] = {
  246. .name = "monow",
  247. .nb_channels = 1,
  248. .color_type = FF_COLOR_GRAY,
  249. .pixel_type = FF_PIXEL_PLANAR,
  250. .depth = 1,
  251. },
  252. [PIX_FMT_MONOBLACK] = {
  253. .name = "monob",
  254. .nb_channels = 1,
  255. .color_type = FF_COLOR_GRAY,
  256. .pixel_type = FF_PIXEL_PLANAR,
  257. .depth = 1,
  258. },
  259. /* paletted formats */
  260. [PIX_FMT_PAL8] = {
  261. .name = "pal8",
  262. .nb_channels = 4, .is_alpha = 1,
  263. .color_type = FF_COLOR_RGB,
  264. .pixel_type = FF_PIXEL_PALETTE,
  265. .depth = 8,
  266. },
  267. [PIX_FMT_XVMC_MPEG2_MC] = {
  268. .name = "xvmcmc",
  269. .is_hwaccel = 1,
  270. },
  271. [PIX_FMT_XVMC_MPEG2_IDCT] = {
  272. .name = "xvmcidct",
  273. .is_hwaccel = 1,
  274. },
  275. [PIX_FMT_VDPAU_MPEG1] = {
  276. .name = "vdpau_mpeg1",
  277. .is_hwaccel = 1,
  278. .x_chroma_shift = 1, .y_chroma_shift = 1,
  279. },
  280. [PIX_FMT_VDPAU_MPEG2] = {
  281. .name = "vdpau_mpeg2",
  282. .is_hwaccel = 1,
  283. .x_chroma_shift = 1, .y_chroma_shift = 1,
  284. },
  285. [PIX_FMT_VDPAU_H264] = {
  286. .name = "vdpau_h264",
  287. .is_hwaccel = 1,
  288. .x_chroma_shift = 1, .y_chroma_shift = 1,
  289. },
  290. [PIX_FMT_VDPAU_WMV3] = {
  291. .name = "vdpau_wmv3",
  292. .is_hwaccel = 1,
  293. .x_chroma_shift = 1, .y_chroma_shift = 1,
  294. },
  295. [PIX_FMT_VDPAU_VC1] = {
  296. .name = "vdpau_vc1",
  297. .is_hwaccel = 1,
  298. .x_chroma_shift = 1, .y_chroma_shift = 1,
  299. },
  300. [PIX_FMT_UYYVYY411] = {
  301. .name = "uyyvyy411",
  302. .nb_channels = 1,
  303. .color_type = FF_COLOR_YUV,
  304. .pixel_type = FF_PIXEL_PACKED,
  305. .depth = 8,
  306. .x_chroma_shift = 2, .y_chroma_shift = 0,
  307. },
  308. [PIX_FMT_BGR32] = {
  309. .name = "bgr32",
  310. .nb_channels = 4, .is_alpha = 1,
  311. .color_type = FF_COLOR_RGB,
  312. .pixel_type = FF_PIXEL_PACKED,
  313. .depth = 8,
  314. .x_chroma_shift = 0, .y_chroma_shift = 0,
  315. },
  316. [PIX_FMT_BGR565] = {
  317. .name = "bgr565",
  318. .nb_channels = 3,
  319. .color_type = FF_COLOR_RGB,
  320. .pixel_type = FF_PIXEL_PACKED,
  321. .depth = 5,
  322. .x_chroma_shift = 0, .y_chroma_shift = 0,
  323. },
  324. [PIX_FMT_BGR555] = {
  325. .name = "bgr555",
  326. .nb_channels = 3,
  327. .color_type = FF_COLOR_RGB,
  328. .pixel_type = FF_PIXEL_PACKED,
  329. .depth = 5,
  330. .x_chroma_shift = 0, .y_chroma_shift = 0,
  331. },
  332. [PIX_FMT_RGB8] = {
  333. .name = "rgb8",
  334. .nb_channels = 1,
  335. .color_type = FF_COLOR_RGB,
  336. .pixel_type = FF_PIXEL_PACKED,
  337. .depth = 8,
  338. .x_chroma_shift = 0, .y_chroma_shift = 0,
  339. },
  340. [PIX_FMT_RGB4] = {
  341. .name = "rgb4",
  342. .nb_channels = 1,
  343. .color_type = FF_COLOR_RGB,
  344. .pixel_type = FF_PIXEL_PACKED,
  345. .depth = 4,
  346. .x_chroma_shift = 0, .y_chroma_shift = 0,
  347. },
  348. [PIX_FMT_RGB4_BYTE] = {
  349. .name = "rgb4_byte",
  350. .nb_channels = 1,
  351. .color_type = FF_COLOR_RGB,
  352. .pixel_type = FF_PIXEL_PACKED,
  353. .depth = 8,
  354. .x_chroma_shift = 0, .y_chroma_shift = 0,
  355. },
  356. [PIX_FMT_BGR8] = {
  357. .name = "bgr8",
  358. .nb_channels = 1,
  359. .color_type = FF_COLOR_RGB,
  360. .pixel_type = FF_PIXEL_PACKED,
  361. .depth = 8,
  362. .x_chroma_shift = 0, .y_chroma_shift = 0,
  363. },
  364. [PIX_FMT_BGR4] = {
  365. .name = "bgr4",
  366. .nb_channels = 1,
  367. .color_type = FF_COLOR_RGB,
  368. .pixel_type = FF_PIXEL_PACKED,
  369. .depth = 4,
  370. .x_chroma_shift = 0, .y_chroma_shift = 0,
  371. },
  372. [PIX_FMT_BGR4_BYTE] = {
  373. .name = "bgr4_byte",
  374. .nb_channels = 1,
  375. .color_type = FF_COLOR_RGB,
  376. .pixel_type = FF_PIXEL_PACKED,
  377. .depth = 8,
  378. .x_chroma_shift = 0, .y_chroma_shift = 0,
  379. },
  380. [PIX_FMT_NV12] = {
  381. .name = "nv12",
  382. .nb_channels = 2,
  383. .color_type = FF_COLOR_YUV,
  384. .pixel_type = FF_PIXEL_PLANAR,
  385. .depth = 8,
  386. .x_chroma_shift = 1, .y_chroma_shift = 1,
  387. },
  388. [PIX_FMT_NV21] = {
  389. .name = "nv12",
  390. .nb_channels = 2,
  391. .color_type = FF_COLOR_YUV,
  392. .pixel_type = FF_PIXEL_PLANAR,
  393. .depth = 8,
  394. .x_chroma_shift = 1, .y_chroma_shift = 1,
  395. },
  396. [PIX_FMT_BGR32_1] = {
  397. .name = "bgr32_1",
  398. .nb_channels = 4, .is_alpha = 1,
  399. .color_type = FF_COLOR_RGB,
  400. .pixel_type = FF_PIXEL_PACKED,
  401. .depth = 8,
  402. .x_chroma_shift = 0, .y_chroma_shift = 0,
  403. },
  404. [PIX_FMT_RGB32_1] = {
  405. .name = "rgb32_1",
  406. .nb_channels = 4, .is_alpha = 1,
  407. .color_type = FF_COLOR_RGB,
  408. .pixel_type = FF_PIXEL_PACKED,
  409. .depth = 8,
  410. .x_chroma_shift = 0, .y_chroma_shift = 0,
  411. },
  412. /* VA API formats */
  413. [PIX_FMT_VAAPI_MOCO] = {
  414. .name = "vaapi_moco",
  415. .is_hwaccel = 1,
  416. .x_chroma_shift = 1, .y_chroma_shift = 1,
  417. },
  418. [PIX_FMT_VAAPI_IDCT] = {
  419. .name = "vaapi_idct",
  420. .is_hwaccel = 1,
  421. .x_chroma_shift = 1, .y_chroma_shift = 1,
  422. },
  423. [PIX_FMT_VAAPI_VLD] = {
  424. .name = "vaapi_vld",
  425. .is_hwaccel = 1,
  426. .x_chroma_shift = 1, .y_chroma_shift = 1,
  427. },
  428. };
  429. void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
  430. {
  431. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  432. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  433. }
  434. const char *avcodec_get_pix_fmt_name(int pix_fmt)
  435. {
  436. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  437. return NULL;
  438. else
  439. return pix_fmt_info[pix_fmt].name;
  440. }
  441. enum PixelFormat avcodec_get_pix_fmt(const char* name)
  442. {
  443. int i;
  444. for (i=0; i < PIX_FMT_NB; i++)
  445. if (!strcmp(pix_fmt_info[i].name, name))
  446. return i;
  447. return PIX_FMT_NONE;
  448. }
  449. void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
  450. {
  451. /* print header */
  452. if (pix_fmt < 0)
  453. snprintf (buf, buf_size,
  454. "name " " nb_channels" " depth" " is_alpha"
  455. );
  456. else{
  457. PixFmtInfo info= pix_fmt_info[pix_fmt];
  458. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  459. snprintf (buf, buf_size,
  460. "%-10s" " %1d " " %2d " " %c ",
  461. info.name,
  462. info.nb_channels,
  463. info.depth,
  464. is_alpha_char
  465. );
  466. }
  467. }
  468. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  469. {
  470. return pix_fmt_info[pix_fmt].is_hwaccel;
  471. }
  472. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  473. int i;
  474. for(i=0; i<256; i++){
  475. int r,g,b;
  476. switch(pix_fmt) {
  477. case PIX_FMT_RGB8:
  478. r= (i>>5 )*36;
  479. g= ((i>>2)&7)*36;
  480. b= (i&3 )*85;
  481. break;
  482. case PIX_FMT_BGR8:
  483. b= (i>>6 )*85;
  484. g= ((i>>3)&7)*36;
  485. r= (i&7 )*36;
  486. break;
  487. case PIX_FMT_RGB4_BYTE:
  488. r= (i>>3 )*255;
  489. g= ((i>>1)&3)*85;
  490. b= (i&1 )*255;
  491. break;
  492. case PIX_FMT_BGR4_BYTE:
  493. b= (i>>3 )*255;
  494. g= ((i>>1)&3)*85;
  495. r= (i&1 )*255;
  496. break;
  497. case PIX_FMT_GRAY8:
  498. r=b=g= i;
  499. break;
  500. default:
  501. return -1;
  502. }
  503. pal[i] = b + (g<<8) + (r<<16);
  504. }
  505. return 0;
  506. }
  507. int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
  508. {
  509. int w2;
  510. const PixFmtInfo *pinfo;
  511. memset(picture->linesize, 0, sizeof(picture->linesize));
  512. pinfo = &pix_fmt_info[pix_fmt];
  513. switch(pix_fmt) {
  514. case PIX_FMT_YUV420P:
  515. case PIX_FMT_YUV422P:
  516. case PIX_FMT_YUV444P:
  517. case PIX_FMT_YUV410P:
  518. case PIX_FMT_YUV411P:
  519. case PIX_FMT_YUV440P:
  520. case PIX_FMT_YUVJ420P:
  521. case PIX_FMT_YUVJ422P:
  522. case PIX_FMT_YUVJ444P:
  523. case PIX_FMT_YUVJ440P:
  524. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  525. picture->linesize[0] = width;
  526. picture->linesize[1] = w2;
  527. picture->linesize[2] = w2;
  528. break;
  529. case PIX_FMT_YUVA420P:
  530. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  531. picture->linesize[0] = width;
  532. picture->linesize[1] = w2;
  533. picture->linesize[2] = w2;
  534. picture->linesize[3] = width;
  535. break;
  536. case PIX_FMT_NV12:
  537. case PIX_FMT_NV21:
  538. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  539. picture->linesize[0] = width;
  540. picture->linesize[1] = w2;
  541. break;
  542. case PIX_FMT_RGB24:
  543. case PIX_FMT_BGR24:
  544. picture->linesize[0] = width * 3;
  545. break;
  546. case PIX_FMT_RGB32:
  547. case PIX_FMT_BGR32:
  548. case PIX_FMT_RGB32_1:
  549. case PIX_FMT_BGR32_1:
  550. picture->linesize[0] = width * 4;
  551. break;
  552. case PIX_FMT_RGB48BE:
  553. case PIX_FMT_RGB48LE:
  554. picture->linesize[0] = width * 6;
  555. break;
  556. case PIX_FMT_GRAY16BE:
  557. case PIX_FMT_GRAY16LE:
  558. case PIX_FMT_BGR555:
  559. case PIX_FMT_BGR565:
  560. case PIX_FMT_RGB555:
  561. case PIX_FMT_RGB565:
  562. case PIX_FMT_YUYV422:
  563. picture->linesize[0] = width * 2;
  564. break;
  565. case PIX_FMT_UYVY422:
  566. picture->linesize[0] = width * 2;
  567. break;
  568. case PIX_FMT_UYYVYY411:
  569. picture->linesize[0] = width + width/2;
  570. break;
  571. case PIX_FMT_RGB4:
  572. case PIX_FMT_BGR4:
  573. picture->linesize[0] = width / 2;
  574. break;
  575. case PIX_FMT_MONOWHITE:
  576. case PIX_FMT_MONOBLACK:
  577. picture->linesize[0] = (width + 7) >> 3;
  578. break;
  579. case PIX_FMT_PAL8:
  580. case PIX_FMT_RGB8:
  581. case PIX_FMT_BGR8:
  582. case PIX_FMT_RGB4_BYTE:
  583. case PIX_FMT_BGR4_BYTE:
  584. case PIX_FMT_GRAY8:
  585. picture->linesize[0] = width;
  586. break;
  587. default:
  588. return -1;
  589. }
  590. return 0;
  591. }
  592. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
  593. int height)
  594. {
  595. int size, h2, size2;
  596. const PixFmtInfo *pinfo;
  597. pinfo = &pix_fmt_info[pix_fmt];
  598. size = picture->linesize[0] * height;
  599. switch(pix_fmt) {
  600. case PIX_FMT_YUV420P:
  601. case PIX_FMT_YUV422P:
  602. case PIX_FMT_YUV444P:
  603. case PIX_FMT_YUV410P:
  604. case PIX_FMT_YUV411P:
  605. case PIX_FMT_YUV440P:
  606. case PIX_FMT_YUVJ420P:
  607. case PIX_FMT_YUVJ422P:
  608. case PIX_FMT_YUVJ444P:
  609. case PIX_FMT_YUVJ440P:
  610. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  611. size2 = picture->linesize[1] * h2;
  612. picture->data[0] = ptr;
  613. picture->data[1] = picture->data[0] + size;
  614. picture->data[2] = picture->data[1] + size2;
  615. picture->data[3] = NULL;
  616. return size + 2 * size2;
  617. case PIX_FMT_YUVA420P:
  618. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  619. size2 = picture->linesize[1] * h2;
  620. picture->data[0] = ptr;
  621. picture->data[1] = picture->data[0] + size;
  622. picture->data[2] = picture->data[1] + size2;
  623. picture->data[3] = picture->data[1] + size2 + size2;
  624. return 2 * size + 2 * size2;
  625. case PIX_FMT_NV12:
  626. case PIX_FMT_NV21:
  627. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  628. size2 = picture->linesize[1] * h2 * 2;
  629. picture->data[0] = ptr;
  630. picture->data[1] = picture->data[0] + size;
  631. picture->data[2] = NULL;
  632. picture->data[3] = NULL;
  633. return size + 2 * size2;
  634. case PIX_FMT_RGB24:
  635. case PIX_FMT_BGR24:
  636. case PIX_FMT_RGB32:
  637. case PIX_FMT_BGR32:
  638. case PIX_FMT_RGB32_1:
  639. case PIX_FMT_BGR32_1:
  640. case PIX_FMT_RGB48BE:
  641. case PIX_FMT_RGB48LE:
  642. case PIX_FMT_GRAY16BE:
  643. case PIX_FMT_GRAY16LE:
  644. case PIX_FMT_BGR555:
  645. case PIX_FMT_BGR565:
  646. case PIX_FMT_RGB555:
  647. case PIX_FMT_RGB565:
  648. case PIX_FMT_YUYV422:
  649. case PIX_FMT_UYVY422:
  650. case PIX_FMT_UYYVYY411:
  651. case PIX_FMT_RGB4:
  652. case PIX_FMT_BGR4:
  653. case PIX_FMT_MONOWHITE:
  654. case PIX_FMT_MONOBLACK:
  655. picture->data[0] = ptr;
  656. picture->data[1] = NULL;
  657. picture->data[2] = NULL;
  658. picture->data[3] = NULL;
  659. return size;
  660. case PIX_FMT_PAL8:
  661. case PIX_FMT_RGB8:
  662. case PIX_FMT_BGR8:
  663. case PIX_FMT_RGB4_BYTE:
  664. case PIX_FMT_BGR4_BYTE:
  665. case PIX_FMT_GRAY8:
  666. size2 = (size + 3) & ~3;
  667. picture->data[0] = ptr;
  668. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  669. picture->data[2] = NULL;
  670. picture->data[3] = NULL;
  671. return size2 + 256 * 4;
  672. default:
  673. picture->data[0] = NULL;
  674. picture->data[1] = NULL;
  675. picture->data[2] = NULL;
  676. picture->data[3] = NULL;
  677. return -1;
  678. }
  679. }
  680. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  681. int pix_fmt, int width, int height)
  682. {
  683. if(avcodec_check_dimensions(NULL, width, height))
  684. return -1;
  685. if (ff_fill_linesize(picture, pix_fmt, width))
  686. return -1;
  687. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  688. }
  689. int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
  690. unsigned char *dest, int dest_size)
  691. {
  692. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  693. int i, j, w, ow, h, oh, data_planes;
  694. const unsigned char* s;
  695. int size = avpicture_get_size(pix_fmt, width, height);
  696. if (size > dest_size || size < 0)
  697. return -1;
  698. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  699. if (pix_fmt == PIX_FMT_YUYV422 ||
  700. pix_fmt == PIX_FMT_UYVY422 ||
  701. pix_fmt == PIX_FMT_BGR565 ||
  702. pix_fmt == PIX_FMT_BGR555 ||
  703. pix_fmt == PIX_FMT_RGB565 ||
  704. pix_fmt == PIX_FMT_RGB555)
  705. w = width * 2;
  706. else if (pix_fmt == PIX_FMT_UYYVYY411)
  707. w = width + width/2;
  708. else if (pix_fmt == PIX_FMT_PAL8)
  709. w = width;
  710. else
  711. w = width * (pf->depth * pf->nb_channels / 8);
  712. data_planes = 1;
  713. h = height;
  714. } else {
  715. data_planes = pf->nb_channels;
  716. w = (width*pf->depth + 7)/8;
  717. h = height;
  718. }
  719. ow = w;
  720. oh = h;
  721. for (i=0; i<data_planes; i++) {
  722. if (i == 1) {
  723. w = width >> pf->x_chroma_shift;
  724. h = height >> pf->y_chroma_shift;
  725. } else if (i == 3) {
  726. w = ow;
  727. h = oh;
  728. }
  729. s = src->data[i];
  730. for(j=0; j<h; j++) {
  731. memcpy(dest, s, w);
  732. dest += w;
  733. s += src->linesize[i];
  734. }
  735. }
  736. if (pf->pixel_type == FF_PIXEL_PALETTE)
  737. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  738. return size;
  739. }
  740. int avpicture_get_size(int pix_fmt, int width, int height)
  741. {
  742. AVPicture dummy_pict;
  743. if(avcodec_check_dimensions(NULL, width, height))
  744. return -1;
  745. switch (pix_fmt) {
  746. case PIX_FMT_RGB8:
  747. case PIX_FMT_BGR8:
  748. case PIX_FMT_RGB4_BYTE:
  749. case PIX_FMT_BGR4_BYTE:
  750. case PIX_FMT_GRAY8:
  751. // do not include palette for these pseudo-paletted formats
  752. return width * height;
  753. }
  754. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  755. }
  756. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  757. int has_alpha)
  758. {
  759. const PixFmtInfo *pf, *ps;
  760. int loss;
  761. ps = &pix_fmt_info[src_pix_fmt];
  762. pf = &pix_fmt_info[dst_pix_fmt];
  763. /* compute loss */
  764. loss = 0;
  765. pf = &pix_fmt_info[dst_pix_fmt];
  766. if (pf->depth < ps->depth ||
  767. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  768. loss |= FF_LOSS_DEPTH;
  769. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  770. pf->y_chroma_shift > ps->y_chroma_shift)
  771. loss |= FF_LOSS_RESOLUTION;
  772. switch(pf->color_type) {
  773. case FF_COLOR_RGB:
  774. if (ps->color_type != FF_COLOR_RGB &&
  775. ps->color_type != FF_COLOR_GRAY)
  776. loss |= FF_LOSS_COLORSPACE;
  777. break;
  778. case FF_COLOR_GRAY:
  779. if (ps->color_type != FF_COLOR_GRAY)
  780. loss |= FF_LOSS_COLORSPACE;
  781. break;
  782. case FF_COLOR_YUV:
  783. if (ps->color_type != FF_COLOR_YUV)
  784. loss |= FF_LOSS_COLORSPACE;
  785. break;
  786. case FF_COLOR_YUV_JPEG:
  787. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  788. ps->color_type != FF_COLOR_YUV &&
  789. ps->color_type != FF_COLOR_GRAY)
  790. loss |= FF_LOSS_COLORSPACE;
  791. break;
  792. default:
  793. /* fail safe test */
  794. if (ps->color_type != pf->color_type)
  795. loss |= FF_LOSS_COLORSPACE;
  796. break;
  797. }
  798. if (pf->color_type == FF_COLOR_GRAY &&
  799. ps->color_type != FF_COLOR_GRAY)
  800. loss |= FF_LOSS_CHROMA;
  801. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  802. loss |= FF_LOSS_ALPHA;
  803. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  804. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  805. loss |= FF_LOSS_COLORQUANT;
  806. return loss;
  807. }
  808. static int avg_bits_per_pixel(int pix_fmt)
  809. {
  810. int bits;
  811. const PixFmtInfo *pf;
  812. pf = &pix_fmt_info[pix_fmt];
  813. switch(pf->pixel_type) {
  814. case FF_PIXEL_PACKED:
  815. switch(pix_fmt) {
  816. case PIX_FMT_YUYV422:
  817. case PIX_FMT_UYVY422:
  818. case PIX_FMT_RGB565:
  819. case PIX_FMT_RGB555:
  820. case PIX_FMT_BGR565:
  821. case PIX_FMT_BGR555:
  822. bits = 16;
  823. break;
  824. case PIX_FMT_UYYVYY411:
  825. bits = 12;
  826. break;
  827. default:
  828. bits = pf->depth * pf->nb_channels;
  829. break;
  830. }
  831. break;
  832. case FF_PIXEL_PLANAR:
  833. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  834. bits = pf->depth * pf->nb_channels;
  835. } else {
  836. bits = pf->depth + ((2 * pf->depth) >>
  837. (pf->x_chroma_shift + pf->y_chroma_shift));
  838. }
  839. break;
  840. case FF_PIXEL_PALETTE:
  841. bits = 8;
  842. break;
  843. default:
  844. bits = -1;
  845. break;
  846. }
  847. return bits;
  848. }
  849. static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  850. int src_pix_fmt,
  851. int has_alpha,
  852. int loss_mask)
  853. {
  854. int dist, i, loss, min_dist, dst_pix_fmt;
  855. /* find exact color match with smallest size */
  856. dst_pix_fmt = -1;
  857. min_dist = 0x7fffffff;
  858. for(i = 0;i < PIX_FMT_NB; i++) {
  859. if (pix_fmt_mask & (1ULL << i)) {
  860. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  861. if (loss == 0) {
  862. dist = avg_bits_per_pixel(i);
  863. if (dist < min_dist) {
  864. min_dist = dist;
  865. dst_pix_fmt = i;
  866. }
  867. }
  868. }
  869. }
  870. return dst_pix_fmt;
  871. }
  872. int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
  873. int has_alpha, int *loss_ptr)
  874. {
  875. int dst_pix_fmt, loss_mask, i;
  876. static const int loss_mask_order[] = {
  877. ~0, /* no loss first */
  878. ~FF_LOSS_ALPHA,
  879. ~FF_LOSS_RESOLUTION,
  880. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  881. ~FF_LOSS_COLORQUANT,
  882. ~FF_LOSS_DEPTH,
  883. 0,
  884. };
  885. /* try with successive loss */
  886. i = 0;
  887. for(;;) {
  888. loss_mask = loss_mask_order[i++];
  889. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  890. has_alpha, loss_mask);
  891. if (dst_pix_fmt >= 0)
  892. goto found;
  893. if (loss_mask == 0)
  894. break;
  895. }
  896. return -1;
  897. found:
  898. if (loss_ptr)
  899. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  900. return dst_pix_fmt;
  901. }
  902. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  903. const uint8_t *src, int src_wrap,
  904. int width, int height)
  905. {
  906. if((!dst) || (!src))
  907. return;
  908. for(;height > 0; height--) {
  909. memcpy(dst, src, width);
  910. dst += dst_wrap;
  911. src += src_wrap;
  912. }
  913. }
  914. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  915. {
  916. int bits;
  917. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  918. pf = &pix_fmt_info[pix_fmt];
  919. switch(pf->pixel_type) {
  920. case FF_PIXEL_PACKED:
  921. switch(pix_fmt) {
  922. case PIX_FMT_YUYV422:
  923. case PIX_FMT_UYVY422:
  924. case PIX_FMT_RGB565:
  925. case PIX_FMT_RGB555:
  926. case PIX_FMT_BGR565:
  927. case PIX_FMT_BGR555:
  928. bits = 16;
  929. break;
  930. case PIX_FMT_UYYVYY411:
  931. bits = 12;
  932. break;
  933. default:
  934. bits = pf->depth * pf->nb_channels;
  935. break;
  936. }
  937. return (width * bits + 7) >> 3;
  938. break;
  939. case FF_PIXEL_PLANAR:
  940. if (plane == 1 || plane == 2)
  941. width= -((-width)>>pf->x_chroma_shift);
  942. return (width * pf->depth + 7) >> 3;
  943. break;
  944. case FF_PIXEL_PALETTE:
  945. if (plane == 0)
  946. return width;
  947. break;
  948. }
  949. return -1;
  950. }
  951. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  952. int pix_fmt, int width, int height)
  953. {
  954. int i;
  955. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  956. pf = &pix_fmt_info[pix_fmt];
  957. switch(pf->pixel_type) {
  958. case FF_PIXEL_PACKED:
  959. case FF_PIXEL_PLANAR:
  960. for(i = 0; i < pf->nb_channels; i++) {
  961. int h;
  962. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  963. h = height;
  964. if (i == 1 || i == 2) {
  965. h= -((-height)>>pf->y_chroma_shift);
  966. }
  967. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  968. src->data[i], src->linesize[i],
  969. bwidth, h);
  970. }
  971. break;
  972. case FF_PIXEL_PALETTE:
  973. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  974. src->data[0], src->linesize[0],
  975. width, height);
  976. /* copy the palette */
  977. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  978. src->data[1], src->linesize[1],
  979. 4, 256);
  980. break;
  981. }
  982. }
  983. /* 2x2 -> 1x1 */
  984. void ff_shrink22(uint8_t *dst, int dst_wrap,
  985. const uint8_t *src, int src_wrap,
  986. int width, int height)
  987. {
  988. int w;
  989. const uint8_t *s1, *s2;
  990. uint8_t *d;
  991. for(;height > 0; height--) {
  992. s1 = src;
  993. s2 = s1 + src_wrap;
  994. d = dst;
  995. for(w = width;w >= 4; w-=4) {
  996. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  997. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  998. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  999. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1000. s1 += 8;
  1001. s2 += 8;
  1002. d += 4;
  1003. }
  1004. for(;w > 0; w--) {
  1005. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1006. s1 += 2;
  1007. s2 += 2;
  1008. d++;
  1009. }
  1010. src += 2 * src_wrap;
  1011. dst += dst_wrap;
  1012. }
  1013. }
  1014. /* 4x4 -> 1x1 */
  1015. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1016. const uint8_t *src, int src_wrap,
  1017. int width, int height)
  1018. {
  1019. int w;
  1020. const uint8_t *s1, *s2, *s3, *s4;
  1021. uint8_t *d;
  1022. for(;height > 0; height--) {
  1023. s1 = src;
  1024. s2 = s1 + src_wrap;
  1025. s3 = s2 + src_wrap;
  1026. s4 = s3 + src_wrap;
  1027. d = dst;
  1028. for(w = width;w > 0; w--) {
  1029. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1030. s2[0] + s2[1] + s2[2] + s2[3] +
  1031. s3[0] + s3[1] + s3[2] + s3[3] +
  1032. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1033. s1 += 4;
  1034. s2 += 4;
  1035. s3 += 4;
  1036. s4 += 4;
  1037. d++;
  1038. }
  1039. src += 4 * src_wrap;
  1040. dst += dst_wrap;
  1041. }
  1042. }
  1043. /* 8x8 -> 1x1 */
  1044. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1045. const uint8_t *src, int src_wrap,
  1046. int width, int height)
  1047. {
  1048. int w, i;
  1049. for(;height > 0; height--) {
  1050. for(w = width;w > 0; w--) {
  1051. int tmp=0;
  1052. for(i=0; i<8; i++){
  1053. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1054. src += src_wrap;
  1055. }
  1056. *(dst++) = (tmp + 32)>>6;
  1057. src += 8 - 8*src_wrap;
  1058. }
  1059. src += 8*src_wrap - 8*width;
  1060. dst += dst_wrap - width;
  1061. }
  1062. }
  1063. int avpicture_alloc(AVPicture *picture,
  1064. int pix_fmt, int width, int height)
  1065. {
  1066. int size;
  1067. void *ptr;
  1068. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  1069. if(size<0)
  1070. goto fail;
  1071. ptr = av_malloc(size);
  1072. if (!ptr)
  1073. goto fail;
  1074. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1075. if(picture->data[1] && !picture->data[2])
  1076. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1077. return 0;
  1078. fail:
  1079. memset(picture, 0, sizeof(AVPicture));
  1080. return -1;
  1081. }
  1082. void avpicture_free(AVPicture *picture)
  1083. {
  1084. av_free(picture->data[0]);
  1085. }
  1086. /* return true if yuv planar */
  1087. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1088. {
  1089. return (ps->color_type == FF_COLOR_YUV ||
  1090. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1091. ps->pixel_type == FF_PIXEL_PLANAR;
  1092. }
  1093. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1094. int pix_fmt, int top_band, int left_band)
  1095. {
  1096. int y_shift;
  1097. int x_shift;
  1098. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1099. return -1;
  1100. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1101. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1102. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1103. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1104. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1105. dst->linesize[0] = src->linesize[0];
  1106. dst->linesize[1] = src->linesize[1];
  1107. dst->linesize[2] = src->linesize[2];
  1108. return 0;
  1109. }
  1110. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1111. int pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1112. int *color)
  1113. {
  1114. uint8_t *optr;
  1115. int y_shift;
  1116. int x_shift;
  1117. int yheight;
  1118. int i, y;
  1119. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1120. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1121. for (i = 0; i < 3; i++) {
  1122. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1123. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1124. if (padtop || padleft) {
  1125. memset(dst->data[i], color[i],
  1126. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1127. }
  1128. if (padleft || padright) {
  1129. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1130. (dst->linesize[i] - (padright >> x_shift));
  1131. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1132. for (y = 0; y < yheight; y++) {
  1133. memset(optr, color[i], (padleft + padright) >> x_shift);
  1134. optr += dst->linesize[i];
  1135. }
  1136. }
  1137. if (src) { /* first line */
  1138. uint8_t *iptr = src->data[i];
  1139. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1140. (padleft >> x_shift);
  1141. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1142. iptr += src->linesize[i];
  1143. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1144. (dst->linesize[i] - (padright >> x_shift));
  1145. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1146. for (y = 0; y < yheight; y++) {
  1147. memset(optr, color[i], (padleft + padright) >> x_shift);
  1148. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1149. (width - padleft - padright) >> x_shift);
  1150. iptr += src->linesize[i];
  1151. optr += dst->linesize[i];
  1152. }
  1153. }
  1154. if (padbottom || padright) {
  1155. optr = dst->data[i] + dst->linesize[i] *
  1156. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1157. memset(optr, color[i],dst->linesize[i] *
  1158. (padbottom >> y_shift) + (padright >> x_shift));
  1159. }
  1160. }
  1161. return 0;
  1162. }
  1163. /* NOTE: we scan all the pixels to have an exact information */
  1164. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1165. {
  1166. const unsigned char *p;
  1167. int src_wrap, ret, x, y;
  1168. unsigned int a;
  1169. uint32_t *palette = (uint32_t *)src->data[1];
  1170. p = src->data[0];
  1171. src_wrap = src->linesize[0] - width;
  1172. ret = 0;
  1173. for(y=0;y<height;y++) {
  1174. for(x=0;x<width;x++) {
  1175. a = palette[p[0]] >> 24;
  1176. if (a == 0x00) {
  1177. ret |= FF_ALPHA_TRANSP;
  1178. } else if (a != 0xff) {
  1179. ret |= FF_ALPHA_SEMI_TRANSP;
  1180. }
  1181. p++;
  1182. }
  1183. p += src_wrap;
  1184. }
  1185. return ret;
  1186. }
  1187. int img_get_alpha_info(const AVPicture *src,
  1188. int pix_fmt, int width, int height)
  1189. {
  1190. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1191. int ret;
  1192. pf = &pix_fmt_info[pix_fmt];
  1193. /* no alpha can be represented in format */
  1194. if (!pf->is_alpha)
  1195. return 0;
  1196. switch(pix_fmt) {
  1197. case PIX_FMT_PAL8:
  1198. ret = get_alpha_info_pal8(src, width, height);
  1199. break;
  1200. default:
  1201. /* we do not know, so everything is indicated */
  1202. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1203. break;
  1204. }
  1205. return ret;
  1206. }
  1207. #if HAVE_MMX
  1208. #define DEINT_INPLACE_LINE_LUM \
  1209. movd_m2r(lum_m4[0],mm0);\
  1210. movd_m2r(lum_m3[0],mm1);\
  1211. movd_m2r(lum_m2[0],mm2);\
  1212. movd_m2r(lum_m1[0],mm3);\
  1213. movd_m2r(lum[0],mm4);\
  1214. punpcklbw_r2r(mm7,mm0);\
  1215. movd_r2m(mm2,lum_m4[0]);\
  1216. punpcklbw_r2r(mm7,mm1);\
  1217. punpcklbw_r2r(mm7,mm2);\
  1218. punpcklbw_r2r(mm7,mm3);\
  1219. punpcklbw_r2r(mm7,mm4);\
  1220. paddw_r2r(mm3,mm1);\
  1221. psllw_i2r(1,mm2);\
  1222. paddw_r2r(mm4,mm0);\
  1223. psllw_i2r(2,mm1);\
  1224. paddw_r2r(mm6,mm2);\
  1225. paddw_r2r(mm2,mm1);\
  1226. psubusw_r2r(mm0,mm1);\
  1227. psrlw_i2r(3,mm1);\
  1228. packuswb_r2r(mm7,mm1);\
  1229. movd_r2m(mm1,lum_m2[0]);
  1230. #define DEINT_LINE_LUM \
  1231. movd_m2r(lum_m4[0],mm0);\
  1232. movd_m2r(lum_m3[0],mm1);\
  1233. movd_m2r(lum_m2[0],mm2);\
  1234. movd_m2r(lum_m1[0],mm3);\
  1235. movd_m2r(lum[0],mm4);\
  1236. punpcklbw_r2r(mm7,mm0);\
  1237. punpcklbw_r2r(mm7,mm1);\
  1238. punpcklbw_r2r(mm7,mm2);\
  1239. punpcklbw_r2r(mm7,mm3);\
  1240. punpcklbw_r2r(mm7,mm4);\
  1241. paddw_r2r(mm3,mm1);\
  1242. psllw_i2r(1,mm2);\
  1243. paddw_r2r(mm4,mm0);\
  1244. psllw_i2r(2,mm1);\
  1245. paddw_r2r(mm6,mm2);\
  1246. paddw_r2r(mm2,mm1);\
  1247. psubusw_r2r(mm0,mm1);\
  1248. psrlw_i2r(3,mm1);\
  1249. packuswb_r2r(mm7,mm1);\
  1250. movd_r2m(mm1,dst[0]);
  1251. #endif
  1252. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1253. static void deinterlace_line(uint8_t *dst,
  1254. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1255. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1256. const uint8_t *lum,
  1257. int size)
  1258. {
  1259. #if !HAVE_MMX
  1260. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1261. int sum;
  1262. for(;size > 0;size--) {
  1263. sum = -lum_m4[0];
  1264. sum += lum_m3[0] << 2;
  1265. sum += lum_m2[0] << 1;
  1266. sum += lum_m1[0] << 2;
  1267. sum += -lum[0];
  1268. dst[0] = cm[(sum + 4) >> 3];
  1269. lum_m4++;
  1270. lum_m3++;
  1271. lum_m2++;
  1272. lum_m1++;
  1273. lum++;
  1274. dst++;
  1275. }
  1276. #else
  1277. {
  1278. pxor_r2r(mm7,mm7);
  1279. movq_m2r(ff_pw_4,mm6);
  1280. }
  1281. for (;size > 3; size-=4) {
  1282. DEINT_LINE_LUM
  1283. lum_m4+=4;
  1284. lum_m3+=4;
  1285. lum_m2+=4;
  1286. lum_m1+=4;
  1287. lum+=4;
  1288. dst+=4;
  1289. }
  1290. #endif
  1291. }
  1292. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1293. int size)
  1294. {
  1295. #if !HAVE_MMX
  1296. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1297. int sum;
  1298. for(;size > 0;size--) {
  1299. sum = -lum_m4[0];
  1300. sum += lum_m3[0] << 2;
  1301. sum += lum_m2[0] << 1;
  1302. lum_m4[0]=lum_m2[0];
  1303. sum += lum_m1[0] << 2;
  1304. sum += -lum[0];
  1305. lum_m2[0] = cm[(sum + 4) >> 3];
  1306. lum_m4++;
  1307. lum_m3++;
  1308. lum_m2++;
  1309. lum_m1++;
  1310. lum++;
  1311. }
  1312. #else
  1313. {
  1314. pxor_r2r(mm7,mm7);
  1315. movq_m2r(ff_pw_4,mm6);
  1316. }
  1317. for (;size > 3; size-=4) {
  1318. DEINT_INPLACE_LINE_LUM
  1319. lum_m4+=4;
  1320. lum_m3+=4;
  1321. lum_m2+=4;
  1322. lum_m1+=4;
  1323. lum+=4;
  1324. }
  1325. #endif
  1326. }
  1327. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1328. top field is copied as is, but the bottom field is deinterlaced
  1329. against the top field. */
  1330. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1331. const uint8_t *src1, int src_wrap,
  1332. int width, int height)
  1333. {
  1334. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1335. int y;
  1336. src_m2 = src1;
  1337. src_m1 = src1;
  1338. src_0=&src_m1[src_wrap];
  1339. src_p1=&src_0[src_wrap];
  1340. src_p2=&src_p1[src_wrap];
  1341. for(y=0;y<(height-2);y+=2) {
  1342. memcpy(dst,src_m1,width);
  1343. dst += dst_wrap;
  1344. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1345. src_m2 = src_0;
  1346. src_m1 = src_p1;
  1347. src_0 = src_p2;
  1348. src_p1 += 2*src_wrap;
  1349. src_p2 += 2*src_wrap;
  1350. dst += dst_wrap;
  1351. }
  1352. memcpy(dst,src_m1,width);
  1353. dst += dst_wrap;
  1354. /* do last line */
  1355. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1356. }
  1357. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1358. int width, int height)
  1359. {
  1360. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1361. int y;
  1362. uint8_t *buf;
  1363. buf = (uint8_t*)av_malloc(width);
  1364. src_m1 = src1;
  1365. memcpy(buf,src_m1,width);
  1366. src_0=&src_m1[src_wrap];
  1367. src_p1=&src_0[src_wrap];
  1368. src_p2=&src_p1[src_wrap];
  1369. for(y=0;y<(height-2);y+=2) {
  1370. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1371. src_m1 = src_p1;
  1372. src_0 = src_p2;
  1373. src_p1 += 2*src_wrap;
  1374. src_p2 += 2*src_wrap;
  1375. }
  1376. /* do last line */
  1377. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1378. av_free(buf);
  1379. }
  1380. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1381. int pix_fmt, int width, int height)
  1382. {
  1383. int i;
  1384. if (pix_fmt != PIX_FMT_YUV420P &&
  1385. pix_fmt != PIX_FMT_YUV422P &&
  1386. pix_fmt != PIX_FMT_YUV444P &&
  1387. pix_fmt != PIX_FMT_YUV411P &&
  1388. pix_fmt != PIX_FMT_GRAY8)
  1389. return -1;
  1390. if ((width & 3) != 0 || (height & 3) != 0)
  1391. return -1;
  1392. for(i=0;i<3;i++) {
  1393. if (i == 1) {
  1394. switch(pix_fmt) {
  1395. case PIX_FMT_YUV420P:
  1396. width >>= 1;
  1397. height >>= 1;
  1398. break;
  1399. case PIX_FMT_YUV422P:
  1400. width >>= 1;
  1401. break;
  1402. case PIX_FMT_YUV411P:
  1403. width >>= 2;
  1404. break;
  1405. default:
  1406. break;
  1407. }
  1408. if (pix_fmt == PIX_FMT_GRAY8) {
  1409. break;
  1410. }
  1411. }
  1412. if (src == dst) {
  1413. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1414. width, height);
  1415. } else {
  1416. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1417. src->data[i], src->linesize[i],
  1418. width, height);
  1419. }
  1420. }
  1421. emms_c();
  1422. return 0;
  1423. }