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.

1519 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. picture->linesize[1] = 4;
  587. break;
  588. default:
  589. return -1;
  590. }
  591. return 0;
  592. }
  593. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
  594. int height)
  595. {
  596. int size, h2, size2;
  597. const PixFmtInfo *pinfo;
  598. pinfo = &pix_fmt_info[pix_fmt];
  599. size = picture->linesize[0] * height;
  600. switch(pix_fmt) {
  601. case PIX_FMT_YUV420P:
  602. case PIX_FMT_YUV422P:
  603. case PIX_FMT_YUV444P:
  604. case PIX_FMT_YUV410P:
  605. case PIX_FMT_YUV411P:
  606. case PIX_FMT_YUV440P:
  607. case PIX_FMT_YUVJ420P:
  608. case PIX_FMT_YUVJ422P:
  609. case PIX_FMT_YUVJ444P:
  610. case PIX_FMT_YUVJ440P:
  611. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  612. size2 = picture->linesize[1] * h2;
  613. picture->data[0] = ptr;
  614. picture->data[1] = picture->data[0] + size;
  615. picture->data[2] = picture->data[1] + size2;
  616. picture->data[3] = NULL;
  617. return size + 2 * size2;
  618. case PIX_FMT_YUVA420P:
  619. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  620. size2 = picture->linesize[1] * h2;
  621. picture->data[0] = ptr;
  622. picture->data[1] = picture->data[0] + size;
  623. picture->data[2] = picture->data[1] + size2;
  624. picture->data[3] = picture->data[1] + size2 + size2;
  625. return 2 * size + 2 * size2;
  626. case PIX_FMT_NV12:
  627. case PIX_FMT_NV21:
  628. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  629. size2 = picture->linesize[1] * h2 * 2;
  630. picture->data[0] = ptr;
  631. picture->data[1] = picture->data[0] + size;
  632. picture->data[2] = NULL;
  633. picture->data[3] = NULL;
  634. return size + 2 * size2;
  635. case PIX_FMT_RGB24:
  636. case PIX_FMT_BGR24:
  637. case PIX_FMT_RGB32:
  638. case PIX_FMT_BGR32:
  639. case PIX_FMT_RGB32_1:
  640. case PIX_FMT_BGR32_1:
  641. case PIX_FMT_RGB48BE:
  642. case PIX_FMT_RGB48LE:
  643. case PIX_FMT_GRAY16BE:
  644. case PIX_FMT_GRAY16LE:
  645. case PIX_FMT_BGR555:
  646. case PIX_FMT_BGR565:
  647. case PIX_FMT_RGB555:
  648. case PIX_FMT_RGB565:
  649. case PIX_FMT_YUYV422:
  650. case PIX_FMT_UYVY422:
  651. case PIX_FMT_UYYVYY411:
  652. case PIX_FMT_RGB4:
  653. case PIX_FMT_BGR4:
  654. case PIX_FMT_MONOWHITE:
  655. case PIX_FMT_MONOBLACK:
  656. picture->data[0] = ptr;
  657. picture->data[1] = NULL;
  658. picture->data[2] = NULL;
  659. picture->data[3] = NULL;
  660. return size;
  661. case PIX_FMT_PAL8:
  662. case PIX_FMT_RGB8:
  663. case PIX_FMT_BGR8:
  664. case PIX_FMT_RGB4_BYTE:
  665. case PIX_FMT_BGR4_BYTE:
  666. case PIX_FMT_GRAY8:
  667. size2 = (size + 3) & ~3;
  668. picture->data[0] = ptr;
  669. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  670. picture->data[2] = NULL;
  671. picture->data[3] = NULL;
  672. return size2 + 256 * 4;
  673. default:
  674. picture->data[0] = NULL;
  675. picture->data[1] = NULL;
  676. picture->data[2] = NULL;
  677. picture->data[3] = NULL;
  678. return -1;
  679. }
  680. }
  681. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  682. int pix_fmt, int width, int height)
  683. {
  684. if(avcodec_check_dimensions(NULL, width, height))
  685. return -1;
  686. if (ff_fill_linesize(picture, pix_fmt, width))
  687. return -1;
  688. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  689. }
  690. int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
  691. unsigned char *dest, int dest_size)
  692. {
  693. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  694. int i, j, w, ow, h, oh, data_planes;
  695. const unsigned char* s;
  696. int size = avpicture_get_size(pix_fmt, width, height);
  697. if (size > dest_size || size < 0)
  698. return -1;
  699. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  700. if (pix_fmt == PIX_FMT_YUYV422 ||
  701. pix_fmt == PIX_FMT_UYVY422 ||
  702. pix_fmt == PIX_FMT_BGR565 ||
  703. pix_fmt == PIX_FMT_BGR555 ||
  704. pix_fmt == PIX_FMT_RGB565 ||
  705. pix_fmt == PIX_FMT_RGB555)
  706. w = width * 2;
  707. else if (pix_fmt == PIX_FMT_UYYVYY411)
  708. w = width + width/2;
  709. else if (pix_fmt == PIX_FMT_PAL8)
  710. w = width;
  711. else
  712. w = width * (pf->depth * pf->nb_channels / 8);
  713. data_planes = 1;
  714. h = height;
  715. } else {
  716. data_planes = pf->nb_channels;
  717. w = (width*pf->depth + 7)/8;
  718. h = height;
  719. }
  720. ow = w;
  721. oh = h;
  722. for (i=0; i<data_planes; i++) {
  723. if (i == 1) {
  724. w = width >> pf->x_chroma_shift;
  725. h = height >> pf->y_chroma_shift;
  726. } else if (i == 3) {
  727. w = ow;
  728. h = oh;
  729. }
  730. s = src->data[i];
  731. for(j=0; j<h; j++) {
  732. memcpy(dest, s, w);
  733. dest += w;
  734. s += src->linesize[i];
  735. }
  736. }
  737. if (pf->pixel_type == FF_PIXEL_PALETTE)
  738. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  739. return size;
  740. }
  741. int avpicture_get_size(int pix_fmt, int width, int height)
  742. {
  743. AVPicture dummy_pict;
  744. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  745. }
  746. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  747. int has_alpha)
  748. {
  749. const PixFmtInfo *pf, *ps;
  750. int loss;
  751. ps = &pix_fmt_info[src_pix_fmt];
  752. pf = &pix_fmt_info[dst_pix_fmt];
  753. /* compute loss */
  754. loss = 0;
  755. pf = &pix_fmt_info[dst_pix_fmt];
  756. if (pf->depth < ps->depth ||
  757. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  758. loss |= FF_LOSS_DEPTH;
  759. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  760. pf->y_chroma_shift > ps->y_chroma_shift)
  761. loss |= FF_LOSS_RESOLUTION;
  762. switch(pf->color_type) {
  763. case FF_COLOR_RGB:
  764. if (ps->color_type != FF_COLOR_RGB &&
  765. ps->color_type != FF_COLOR_GRAY)
  766. loss |= FF_LOSS_COLORSPACE;
  767. break;
  768. case FF_COLOR_GRAY:
  769. if (ps->color_type != FF_COLOR_GRAY)
  770. loss |= FF_LOSS_COLORSPACE;
  771. break;
  772. case FF_COLOR_YUV:
  773. if (ps->color_type != FF_COLOR_YUV)
  774. loss |= FF_LOSS_COLORSPACE;
  775. break;
  776. case FF_COLOR_YUV_JPEG:
  777. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  778. ps->color_type != FF_COLOR_YUV &&
  779. ps->color_type != FF_COLOR_GRAY)
  780. loss |= FF_LOSS_COLORSPACE;
  781. break;
  782. default:
  783. /* fail safe test */
  784. if (ps->color_type != pf->color_type)
  785. loss |= FF_LOSS_COLORSPACE;
  786. break;
  787. }
  788. if (pf->color_type == FF_COLOR_GRAY &&
  789. ps->color_type != FF_COLOR_GRAY)
  790. loss |= FF_LOSS_CHROMA;
  791. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  792. loss |= FF_LOSS_ALPHA;
  793. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  794. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  795. loss |= FF_LOSS_COLORQUANT;
  796. return loss;
  797. }
  798. static int avg_bits_per_pixel(int pix_fmt)
  799. {
  800. int bits;
  801. const PixFmtInfo *pf;
  802. pf = &pix_fmt_info[pix_fmt];
  803. switch(pf->pixel_type) {
  804. case FF_PIXEL_PACKED:
  805. switch(pix_fmt) {
  806. case PIX_FMT_YUYV422:
  807. case PIX_FMT_UYVY422:
  808. case PIX_FMT_RGB565:
  809. case PIX_FMT_RGB555:
  810. case PIX_FMT_BGR565:
  811. case PIX_FMT_BGR555:
  812. bits = 16;
  813. break;
  814. case PIX_FMT_UYYVYY411:
  815. bits = 12;
  816. break;
  817. default:
  818. bits = pf->depth * pf->nb_channels;
  819. break;
  820. }
  821. break;
  822. case FF_PIXEL_PLANAR:
  823. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  824. bits = pf->depth * pf->nb_channels;
  825. } else {
  826. bits = pf->depth + ((2 * pf->depth) >>
  827. (pf->x_chroma_shift + pf->y_chroma_shift));
  828. }
  829. break;
  830. case FF_PIXEL_PALETTE:
  831. bits = 8;
  832. break;
  833. default:
  834. bits = -1;
  835. break;
  836. }
  837. return bits;
  838. }
  839. static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  840. int src_pix_fmt,
  841. int has_alpha,
  842. int loss_mask)
  843. {
  844. int dist, i, loss, min_dist, dst_pix_fmt;
  845. /* find exact color match with smallest size */
  846. dst_pix_fmt = -1;
  847. min_dist = 0x7fffffff;
  848. for(i = 0;i < PIX_FMT_NB; i++) {
  849. if (pix_fmt_mask & (1ULL << i)) {
  850. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  851. if (loss == 0) {
  852. dist = avg_bits_per_pixel(i);
  853. if (dist < min_dist) {
  854. min_dist = dist;
  855. dst_pix_fmt = i;
  856. }
  857. }
  858. }
  859. }
  860. return dst_pix_fmt;
  861. }
  862. int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
  863. int has_alpha, int *loss_ptr)
  864. {
  865. int dst_pix_fmt, loss_mask, i;
  866. static const int loss_mask_order[] = {
  867. ~0, /* no loss first */
  868. ~FF_LOSS_ALPHA,
  869. ~FF_LOSS_RESOLUTION,
  870. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  871. ~FF_LOSS_COLORQUANT,
  872. ~FF_LOSS_DEPTH,
  873. 0,
  874. };
  875. /* try with successive loss */
  876. i = 0;
  877. for(;;) {
  878. loss_mask = loss_mask_order[i++];
  879. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  880. has_alpha, loss_mask);
  881. if (dst_pix_fmt >= 0)
  882. goto found;
  883. if (loss_mask == 0)
  884. break;
  885. }
  886. return -1;
  887. found:
  888. if (loss_ptr)
  889. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  890. return dst_pix_fmt;
  891. }
  892. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  893. const uint8_t *src, int src_wrap,
  894. int width, int height)
  895. {
  896. if((!dst) || (!src))
  897. return;
  898. for(;height > 0; height--) {
  899. memcpy(dst, src, width);
  900. dst += dst_wrap;
  901. src += src_wrap;
  902. }
  903. }
  904. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  905. {
  906. int bits;
  907. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  908. pf = &pix_fmt_info[pix_fmt];
  909. switch(pf->pixel_type) {
  910. case FF_PIXEL_PACKED:
  911. switch(pix_fmt) {
  912. case PIX_FMT_YUYV422:
  913. case PIX_FMT_UYVY422:
  914. case PIX_FMT_RGB565:
  915. case PIX_FMT_RGB555:
  916. case PIX_FMT_BGR565:
  917. case PIX_FMT_BGR555:
  918. bits = 16;
  919. break;
  920. case PIX_FMT_UYYVYY411:
  921. bits = 12;
  922. break;
  923. default:
  924. bits = pf->depth * pf->nb_channels;
  925. break;
  926. }
  927. return (width * bits + 7) >> 3;
  928. break;
  929. case FF_PIXEL_PLANAR:
  930. if (plane == 1 || plane == 2)
  931. width= -((-width)>>pf->x_chroma_shift);
  932. return (width * pf->depth + 7) >> 3;
  933. break;
  934. case FF_PIXEL_PALETTE:
  935. if (plane == 0)
  936. return width;
  937. break;
  938. }
  939. return -1;
  940. }
  941. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  942. int pix_fmt, int width, int height)
  943. {
  944. int i;
  945. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  946. pf = &pix_fmt_info[pix_fmt];
  947. switch(pf->pixel_type) {
  948. case FF_PIXEL_PACKED:
  949. case FF_PIXEL_PLANAR:
  950. for(i = 0; i < pf->nb_channels; i++) {
  951. int h;
  952. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  953. h = height;
  954. if (i == 1 || i == 2) {
  955. h= -((-height)>>pf->y_chroma_shift);
  956. }
  957. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  958. src->data[i], src->linesize[i],
  959. bwidth, h);
  960. }
  961. break;
  962. case FF_PIXEL_PALETTE:
  963. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  964. src->data[0], src->linesize[0],
  965. width, height);
  966. /* copy the palette */
  967. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  968. src->data[1], src->linesize[1],
  969. 4, 256);
  970. break;
  971. }
  972. }
  973. /* 2x2 -> 1x1 */
  974. void ff_shrink22(uint8_t *dst, int dst_wrap,
  975. const uint8_t *src, int src_wrap,
  976. int width, int height)
  977. {
  978. int w;
  979. const uint8_t *s1, *s2;
  980. uint8_t *d;
  981. for(;height > 0; height--) {
  982. s1 = src;
  983. s2 = s1 + src_wrap;
  984. d = dst;
  985. for(w = width;w >= 4; w-=4) {
  986. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  987. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  988. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  989. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  990. s1 += 8;
  991. s2 += 8;
  992. d += 4;
  993. }
  994. for(;w > 0; w--) {
  995. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  996. s1 += 2;
  997. s2 += 2;
  998. d++;
  999. }
  1000. src += 2 * src_wrap;
  1001. dst += dst_wrap;
  1002. }
  1003. }
  1004. /* 4x4 -> 1x1 */
  1005. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1006. const uint8_t *src, int src_wrap,
  1007. int width, int height)
  1008. {
  1009. int w;
  1010. const uint8_t *s1, *s2, *s3, *s4;
  1011. uint8_t *d;
  1012. for(;height > 0; height--) {
  1013. s1 = src;
  1014. s2 = s1 + src_wrap;
  1015. s3 = s2 + src_wrap;
  1016. s4 = s3 + src_wrap;
  1017. d = dst;
  1018. for(w = width;w > 0; w--) {
  1019. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1020. s2[0] + s2[1] + s2[2] + s2[3] +
  1021. s3[0] + s3[1] + s3[2] + s3[3] +
  1022. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1023. s1 += 4;
  1024. s2 += 4;
  1025. s3 += 4;
  1026. s4 += 4;
  1027. d++;
  1028. }
  1029. src += 4 * src_wrap;
  1030. dst += dst_wrap;
  1031. }
  1032. }
  1033. /* 8x8 -> 1x1 */
  1034. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1035. const uint8_t *src, int src_wrap,
  1036. int width, int height)
  1037. {
  1038. int w, i;
  1039. for(;height > 0; height--) {
  1040. for(w = width;w > 0; w--) {
  1041. int tmp=0;
  1042. for(i=0; i<8; i++){
  1043. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1044. src += src_wrap;
  1045. }
  1046. *(dst++) = (tmp + 32)>>6;
  1047. src += 8 - 8*src_wrap;
  1048. }
  1049. src += 8*src_wrap - 8*width;
  1050. dst += dst_wrap - width;
  1051. }
  1052. }
  1053. int avpicture_alloc(AVPicture *picture,
  1054. int pix_fmt, int width, int height)
  1055. {
  1056. int size;
  1057. void *ptr;
  1058. size = avpicture_get_size(pix_fmt, width, height);
  1059. if(size<0)
  1060. goto fail;
  1061. ptr = av_malloc(size);
  1062. if (!ptr)
  1063. goto fail;
  1064. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1065. if(picture->data[1] && !picture->data[2])
  1066. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1067. return 0;
  1068. fail:
  1069. memset(picture, 0, sizeof(AVPicture));
  1070. return -1;
  1071. }
  1072. void avpicture_free(AVPicture *picture)
  1073. {
  1074. av_free(picture->data[0]);
  1075. }
  1076. /* return true if yuv planar */
  1077. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1078. {
  1079. return (ps->color_type == FF_COLOR_YUV ||
  1080. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1081. ps->pixel_type == FF_PIXEL_PLANAR;
  1082. }
  1083. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1084. int pix_fmt, int top_band, int left_band)
  1085. {
  1086. int y_shift;
  1087. int x_shift;
  1088. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1089. return -1;
  1090. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1091. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1092. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1093. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1094. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1095. dst->linesize[0] = src->linesize[0];
  1096. dst->linesize[1] = src->linesize[1];
  1097. dst->linesize[2] = src->linesize[2];
  1098. return 0;
  1099. }
  1100. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1101. int pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1102. int *color)
  1103. {
  1104. uint8_t *optr;
  1105. int y_shift;
  1106. int x_shift;
  1107. int yheight;
  1108. int i, y;
  1109. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1110. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1111. for (i = 0; i < 3; i++) {
  1112. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1113. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1114. if (padtop || padleft) {
  1115. memset(dst->data[i], color[i],
  1116. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1117. }
  1118. if (padleft || padright) {
  1119. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1120. (dst->linesize[i] - (padright >> x_shift));
  1121. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1122. for (y = 0; y < yheight; y++) {
  1123. memset(optr, color[i], (padleft + padright) >> x_shift);
  1124. optr += dst->linesize[i];
  1125. }
  1126. }
  1127. if (src) { /* first line */
  1128. uint8_t *iptr = src->data[i];
  1129. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1130. (padleft >> x_shift);
  1131. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1132. iptr += src->linesize[i];
  1133. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1134. (dst->linesize[i] - (padright >> x_shift));
  1135. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1136. for (y = 0; y < yheight; y++) {
  1137. memset(optr, color[i], (padleft + padright) >> x_shift);
  1138. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1139. (width - padleft - padright) >> x_shift);
  1140. iptr += src->linesize[i];
  1141. optr += dst->linesize[i];
  1142. }
  1143. }
  1144. if (padbottom || padright) {
  1145. optr = dst->data[i] + dst->linesize[i] *
  1146. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1147. memset(optr, color[i],dst->linesize[i] *
  1148. (padbottom >> y_shift) + (padright >> x_shift));
  1149. }
  1150. }
  1151. return 0;
  1152. }
  1153. /* NOTE: we scan all the pixels to have an exact information */
  1154. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1155. {
  1156. const unsigned char *p;
  1157. int src_wrap, ret, x, y;
  1158. unsigned int a;
  1159. uint32_t *palette = (uint32_t *)src->data[1];
  1160. p = src->data[0];
  1161. src_wrap = src->linesize[0] - width;
  1162. ret = 0;
  1163. for(y=0;y<height;y++) {
  1164. for(x=0;x<width;x++) {
  1165. a = palette[p[0]] >> 24;
  1166. if (a == 0x00) {
  1167. ret |= FF_ALPHA_TRANSP;
  1168. } else if (a != 0xff) {
  1169. ret |= FF_ALPHA_SEMI_TRANSP;
  1170. }
  1171. p++;
  1172. }
  1173. p += src_wrap;
  1174. }
  1175. return ret;
  1176. }
  1177. int img_get_alpha_info(const AVPicture *src,
  1178. int pix_fmt, int width, int height)
  1179. {
  1180. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1181. int ret;
  1182. pf = &pix_fmt_info[pix_fmt];
  1183. /* no alpha can be represented in format */
  1184. if (!pf->is_alpha)
  1185. return 0;
  1186. switch(pix_fmt) {
  1187. case PIX_FMT_PAL8:
  1188. ret = get_alpha_info_pal8(src, width, height);
  1189. break;
  1190. default:
  1191. /* we do not know, so everything is indicated */
  1192. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1193. break;
  1194. }
  1195. return ret;
  1196. }
  1197. #if HAVE_MMX
  1198. #define DEINT_INPLACE_LINE_LUM \
  1199. movd_m2r(lum_m4[0],mm0);\
  1200. movd_m2r(lum_m3[0],mm1);\
  1201. movd_m2r(lum_m2[0],mm2);\
  1202. movd_m2r(lum_m1[0],mm3);\
  1203. movd_m2r(lum[0],mm4);\
  1204. punpcklbw_r2r(mm7,mm0);\
  1205. movd_r2m(mm2,lum_m4[0]);\
  1206. punpcklbw_r2r(mm7,mm1);\
  1207. punpcklbw_r2r(mm7,mm2);\
  1208. punpcklbw_r2r(mm7,mm3);\
  1209. punpcklbw_r2r(mm7,mm4);\
  1210. paddw_r2r(mm3,mm1);\
  1211. psllw_i2r(1,mm2);\
  1212. paddw_r2r(mm4,mm0);\
  1213. psllw_i2r(2,mm1);\
  1214. paddw_r2r(mm6,mm2);\
  1215. paddw_r2r(mm2,mm1);\
  1216. psubusw_r2r(mm0,mm1);\
  1217. psrlw_i2r(3,mm1);\
  1218. packuswb_r2r(mm7,mm1);\
  1219. movd_r2m(mm1,lum_m2[0]);
  1220. #define DEINT_LINE_LUM \
  1221. movd_m2r(lum_m4[0],mm0);\
  1222. movd_m2r(lum_m3[0],mm1);\
  1223. movd_m2r(lum_m2[0],mm2);\
  1224. movd_m2r(lum_m1[0],mm3);\
  1225. movd_m2r(lum[0],mm4);\
  1226. punpcklbw_r2r(mm7,mm0);\
  1227. punpcklbw_r2r(mm7,mm1);\
  1228. punpcklbw_r2r(mm7,mm2);\
  1229. punpcklbw_r2r(mm7,mm3);\
  1230. punpcklbw_r2r(mm7,mm4);\
  1231. paddw_r2r(mm3,mm1);\
  1232. psllw_i2r(1,mm2);\
  1233. paddw_r2r(mm4,mm0);\
  1234. psllw_i2r(2,mm1);\
  1235. paddw_r2r(mm6,mm2);\
  1236. paddw_r2r(mm2,mm1);\
  1237. psubusw_r2r(mm0,mm1);\
  1238. psrlw_i2r(3,mm1);\
  1239. packuswb_r2r(mm7,mm1);\
  1240. movd_r2m(mm1,dst[0]);
  1241. #endif
  1242. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1243. static void deinterlace_line(uint8_t *dst,
  1244. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1245. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1246. const uint8_t *lum,
  1247. int size)
  1248. {
  1249. #if !HAVE_MMX
  1250. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1251. int sum;
  1252. for(;size > 0;size--) {
  1253. sum = -lum_m4[0];
  1254. sum += lum_m3[0] << 2;
  1255. sum += lum_m2[0] << 1;
  1256. sum += lum_m1[0] << 2;
  1257. sum += -lum[0];
  1258. dst[0] = cm[(sum + 4) >> 3];
  1259. lum_m4++;
  1260. lum_m3++;
  1261. lum_m2++;
  1262. lum_m1++;
  1263. lum++;
  1264. dst++;
  1265. }
  1266. #else
  1267. {
  1268. pxor_r2r(mm7,mm7);
  1269. movq_m2r(ff_pw_4,mm6);
  1270. }
  1271. for (;size > 3; size-=4) {
  1272. DEINT_LINE_LUM
  1273. lum_m4+=4;
  1274. lum_m3+=4;
  1275. lum_m2+=4;
  1276. lum_m1+=4;
  1277. lum+=4;
  1278. dst+=4;
  1279. }
  1280. #endif
  1281. }
  1282. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1283. int size)
  1284. {
  1285. #if !HAVE_MMX
  1286. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1287. int sum;
  1288. for(;size > 0;size--) {
  1289. sum = -lum_m4[0];
  1290. sum += lum_m3[0] << 2;
  1291. sum += lum_m2[0] << 1;
  1292. lum_m4[0]=lum_m2[0];
  1293. sum += lum_m1[0] << 2;
  1294. sum += -lum[0];
  1295. lum_m2[0] = cm[(sum + 4) >> 3];
  1296. lum_m4++;
  1297. lum_m3++;
  1298. lum_m2++;
  1299. lum_m1++;
  1300. lum++;
  1301. }
  1302. #else
  1303. {
  1304. pxor_r2r(mm7,mm7);
  1305. movq_m2r(ff_pw_4,mm6);
  1306. }
  1307. for (;size > 3; size-=4) {
  1308. DEINT_INPLACE_LINE_LUM
  1309. lum_m4+=4;
  1310. lum_m3+=4;
  1311. lum_m2+=4;
  1312. lum_m1+=4;
  1313. lum+=4;
  1314. }
  1315. #endif
  1316. }
  1317. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1318. top field is copied as is, but the bottom field is deinterlaced
  1319. against the top field. */
  1320. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1321. const uint8_t *src1, int src_wrap,
  1322. int width, int height)
  1323. {
  1324. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1325. int y;
  1326. src_m2 = src1;
  1327. src_m1 = src1;
  1328. src_0=&src_m1[src_wrap];
  1329. src_p1=&src_0[src_wrap];
  1330. src_p2=&src_p1[src_wrap];
  1331. for(y=0;y<(height-2);y+=2) {
  1332. memcpy(dst,src_m1,width);
  1333. dst += dst_wrap;
  1334. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1335. src_m2 = src_0;
  1336. src_m1 = src_p1;
  1337. src_0 = src_p2;
  1338. src_p1 += 2*src_wrap;
  1339. src_p2 += 2*src_wrap;
  1340. dst += dst_wrap;
  1341. }
  1342. memcpy(dst,src_m1,width);
  1343. dst += dst_wrap;
  1344. /* do last line */
  1345. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1346. }
  1347. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1348. int width, int height)
  1349. {
  1350. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1351. int y;
  1352. uint8_t *buf;
  1353. buf = (uint8_t*)av_malloc(width);
  1354. src_m1 = src1;
  1355. memcpy(buf,src_m1,width);
  1356. src_0=&src_m1[src_wrap];
  1357. src_p1=&src_0[src_wrap];
  1358. src_p2=&src_p1[src_wrap];
  1359. for(y=0;y<(height-2);y+=2) {
  1360. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1361. src_m1 = src_p1;
  1362. src_0 = src_p2;
  1363. src_p1 += 2*src_wrap;
  1364. src_p2 += 2*src_wrap;
  1365. }
  1366. /* do last line */
  1367. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1368. av_free(buf);
  1369. }
  1370. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1371. int pix_fmt, int width, int height)
  1372. {
  1373. int i;
  1374. if (pix_fmt != PIX_FMT_YUV420P &&
  1375. pix_fmt != PIX_FMT_YUV422P &&
  1376. pix_fmt != PIX_FMT_YUV444P &&
  1377. pix_fmt != PIX_FMT_YUV411P &&
  1378. pix_fmt != PIX_FMT_GRAY8)
  1379. return -1;
  1380. if ((width & 3) != 0 || (height & 3) != 0)
  1381. return -1;
  1382. for(i=0;i<3;i++) {
  1383. if (i == 1) {
  1384. switch(pix_fmt) {
  1385. case PIX_FMT_YUV420P:
  1386. width >>= 1;
  1387. height >>= 1;
  1388. break;
  1389. case PIX_FMT_YUV422P:
  1390. width >>= 1;
  1391. break;
  1392. case PIX_FMT_YUV411P:
  1393. width >>= 2;
  1394. break;
  1395. default:
  1396. break;
  1397. }
  1398. if (pix_fmt == PIX_FMT_GRAY8) {
  1399. break;
  1400. }
  1401. }
  1402. if (src == dst) {
  1403. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1404. width, height);
  1405. } else {
  1406. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1407. src->data[i], src->linesize[i],
  1408. width, height);
  1409. }
  1410. }
  1411. emms_c();
  1412. return 0;
  1413. }