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.

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