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.

1581 lines
45KB

  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_RGB565BE] = {
  208. .name = "rgb565be",
  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_RGB565LE] = {
  216. .name = "rgb565le",
  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. [PIX_FMT_RGB555BE] = {
  224. .name = "rgb555be",
  225. .nb_channels = 3,
  226. .color_type = FF_COLOR_RGB,
  227. .pixel_type = FF_PIXEL_PACKED,
  228. .depth = 5,
  229. .x_chroma_shift = 0, .y_chroma_shift = 0,
  230. },
  231. [PIX_FMT_RGB555LE] = {
  232. .name = "rgb555le",
  233. .nb_channels = 3,
  234. .color_type = FF_COLOR_RGB,
  235. .pixel_type = FF_PIXEL_PACKED,
  236. .depth = 5,
  237. .x_chroma_shift = 0, .y_chroma_shift = 0,
  238. },
  239. /* gray / mono formats */
  240. [PIX_FMT_GRAY16BE] = {
  241. .name = "gray16be",
  242. .nb_channels = 1,
  243. .color_type = FF_COLOR_GRAY,
  244. .pixel_type = FF_PIXEL_PLANAR,
  245. .depth = 16,
  246. },
  247. [PIX_FMT_GRAY16LE] = {
  248. .name = "gray16le",
  249. .nb_channels = 1,
  250. .color_type = FF_COLOR_GRAY,
  251. .pixel_type = FF_PIXEL_PLANAR,
  252. .depth = 16,
  253. },
  254. [PIX_FMT_GRAY8] = {
  255. .name = "gray",
  256. .nb_channels = 1,
  257. .color_type = FF_COLOR_GRAY,
  258. .pixel_type = FF_PIXEL_PLANAR,
  259. .depth = 8,
  260. },
  261. [PIX_FMT_MONOWHITE] = {
  262. .name = "monow",
  263. .nb_channels = 1,
  264. .color_type = FF_COLOR_GRAY,
  265. .pixel_type = FF_PIXEL_PLANAR,
  266. .depth = 1,
  267. },
  268. [PIX_FMT_MONOBLACK] = {
  269. .name = "monob",
  270. .nb_channels = 1,
  271. .color_type = FF_COLOR_GRAY,
  272. .pixel_type = FF_PIXEL_PLANAR,
  273. .depth = 1,
  274. },
  275. /* paletted formats */
  276. [PIX_FMT_PAL8] = {
  277. .name = "pal8",
  278. .nb_channels = 4, .is_alpha = 1,
  279. .color_type = FF_COLOR_RGB,
  280. .pixel_type = FF_PIXEL_PALETTE,
  281. .depth = 8,
  282. },
  283. [PIX_FMT_XVMC_MPEG2_MC] = {
  284. .name = "xvmcmc",
  285. .is_hwaccel = 1,
  286. },
  287. [PIX_FMT_XVMC_MPEG2_IDCT] = {
  288. .name = "xvmcidct",
  289. .is_hwaccel = 1,
  290. },
  291. [PIX_FMT_VDPAU_MPEG1] = {
  292. .name = "vdpau_mpeg1",
  293. .is_hwaccel = 1,
  294. .x_chroma_shift = 1, .y_chroma_shift = 1,
  295. },
  296. [PIX_FMT_VDPAU_MPEG2] = {
  297. .name = "vdpau_mpeg2",
  298. .is_hwaccel = 1,
  299. .x_chroma_shift = 1, .y_chroma_shift = 1,
  300. },
  301. [PIX_FMT_VDPAU_H264] = {
  302. .name = "vdpau_h264",
  303. .is_hwaccel = 1,
  304. .x_chroma_shift = 1, .y_chroma_shift = 1,
  305. },
  306. [PIX_FMT_VDPAU_WMV3] = {
  307. .name = "vdpau_wmv3",
  308. .is_hwaccel = 1,
  309. .x_chroma_shift = 1, .y_chroma_shift = 1,
  310. },
  311. [PIX_FMT_VDPAU_VC1] = {
  312. .name = "vdpau_vc1",
  313. .is_hwaccel = 1,
  314. .x_chroma_shift = 1, .y_chroma_shift = 1,
  315. },
  316. [PIX_FMT_UYYVYY411] = {
  317. .name = "uyyvyy411",
  318. .nb_channels = 1,
  319. .color_type = FF_COLOR_YUV,
  320. .pixel_type = FF_PIXEL_PACKED,
  321. .depth = 8,
  322. .x_chroma_shift = 2, .y_chroma_shift = 0,
  323. },
  324. [PIX_FMT_BGR32] = {
  325. .name = "bgr32",
  326. .nb_channels = 4, .is_alpha = 1,
  327. .color_type = FF_COLOR_RGB,
  328. .pixel_type = FF_PIXEL_PACKED,
  329. .depth = 8,
  330. .x_chroma_shift = 0, .y_chroma_shift = 0,
  331. },
  332. [PIX_FMT_BGR565BE] = {
  333. .name = "bgr565be",
  334. .nb_channels = 3,
  335. .color_type = FF_COLOR_RGB,
  336. .pixel_type = FF_PIXEL_PACKED,
  337. .depth = 5,
  338. .x_chroma_shift = 0, .y_chroma_shift = 0,
  339. },
  340. [PIX_FMT_BGR565LE] = {
  341. .name = "bgr565le",
  342. .nb_channels = 3,
  343. .color_type = FF_COLOR_RGB,
  344. .pixel_type = FF_PIXEL_PACKED,
  345. .depth = 5,
  346. .x_chroma_shift = 0, .y_chroma_shift = 0,
  347. },
  348. [PIX_FMT_BGR555BE] = {
  349. .name = "bgr555be",
  350. .nb_channels = 3,
  351. .color_type = FF_COLOR_RGB,
  352. .pixel_type = FF_PIXEL_PACKED,
  353. .depth = 5,
  354. .x_chroma_shift = 0, .y_chroma_shift = 0,
  355. },
  356. [PIX_FMT_BGR555LE] = {
  357. .name = "bgr555le",
  358. .nb_channels = 3,
  359. .color_type = FF_COLOR_RGB,
  360. .pixel_type = FF_PIXEL_PACKED,
  361. .depth = 5,
  362. .x_chroma_shift = 0, .y_chroma_shift = 0,
  363. },
  364. [PIX_FMT_RGB8] = {
  365. .name = "rgb8",
  366. .nb_channels = 1,
  367. .color_type = FF_COLOR_RGB,
  368. .pixel_type = FF_PIXEL_PACKED,
  369. .depth = 8,
  370. .x_chroma_shift = 0, .y_chroma_shift = 0,
  371. },
  372. [PIX_FMT_RGB4] = {
  373. .name = "rgb4",
  374. .nb_channels = 1,
  375. .color_type = FF_COLOR_RGB,
  376. .pixel_type = FF_PIXEL_PACKED,
  377. .depth = 4,
  378. .x_chroma_shift = 0, .y_chroma_shift = 0,
  379. },
  380. [PIX_FMT_RGB4_BYTE] = {
  381. .name = "rgb4_byte",
  382. .nb_channels = 1,
  383. .color_type = FF_COLOR_RGB,
  384. .pixel_type = FF_PIXEL_PACKED,
  385. .depth = 8,
  386. .x_chroma_shift = 0, .y_chroma_shift = 0,
  387. },
  388. [PIX_FMT_BGR8] = {
  389. .name = "bgr8",
  390. .nb_channels = 1,
  391. .color_type = FF_COLOR_RGB,
  392. .pixel_type = FF_PIXEL_PACKED,
  393. .depth = 8,
  394. .x_chroma_shift = 0, .y_chroma_shift = 0,
  395. },
  396. [PIX_FMT_BGR4] = {
  397. .name = "bgr4",
  398. .nb_channels = 1,
  399. .color_type = FF_COLOR_RGB,
  400. .pixel_type = FF_PIXEL_PACKED,
  401. .depth = 4,
  402. .x_chroma_shift = 0, .y_chroma_shift = 0,
  403. },
  404. [PIX_FMT_BGR4_BYTE] = {
  405. .name = "bgr4_byte",
  406. .nb_channels = 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. [PIX_FMT_NV12] = {
  413. .name = "nv12",
  414. .nb_channels = 2,
  415. .color_type = FF_COLOR_YUV,
  416. .pixel_type = FF_PIXEL_PLANAR,
  417. .depth = 8,
  418. .x_chroma_shift = 1, .y_chroma_shift = 1,
  419. },
  420. [PIX_FMT_NV21] = {
  421. .name = "nv12",
  422. .nb_channels = 2,
  423. .color_type = FF_COLOR_YUV,
  424. .pixel_type = FF_PIXEL_PLANAR,
  425. .depth = 8,
  426. .x_chroma_shift = 1, .y_chroma_shift = 1,
  427. },
  428. [PIX_FMT_BGR32_1] = {
  429. .name = "bgr32_1",
  430. .nb_channels = 4, .is_alpha = 1,
  431. .color_type = FF_COLOR_RGB,
  432. .pixel_type = FF_PIXEL_PACKED,
  433. .depth = 8,
  434. .x_chroma_shift = 0, .y_chroma_shift = 0,
  435. },
  436. [PIX_FMT_RGB32_1] = {
  437. .name = "rgb32_1",
  438. .nb_channels = 4, .is_alpha = 1,
  439. .color_type = FF_COLOR_RGB,
  440. .pixel_type = FF_PIXEL_PACKED,
  441. .depth = 8,
  442. .x_chroma_shift = 0, .y_chroma_shift = 0,
  443. },
  444. /* VA API formats */
  445. [PIX_FMT_VAAPI_MOCO] = {
  446. .name = "vaapi_moco",
  447. .is_hwaccel = 1,
  448. .x_chroma_shift = 1, .y_chroma_shift = 1,
  449. },
  450. [PIX_FMT_VAAPI_IDCT] = {
  451. .name = "vaapi_idct",
  452. .is_hwaccel = 1,
  453. .x_chroma_shift = 1, .y_chroma_shift = 1,
  454. },
  455. [PIX_FMT_VAAPI_VLD] = {
  456. .name = "vaapi_vld",
  457. .is_hwaccel = 1,
  458. .x_chroma_shift = 1, .y_chroma_shift = 1,
  459. },
  460. };
  461. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  462. {
  463. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  464. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  465. }
  466. const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
  467. {
  468. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  469. return NULL;
  470. else
  471. return pix_fmt_info[pix_fmt].name;
  472. }
  473. static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
  474. {
  475. int i;
  476. for (i=0; i < PIX_FMT_NB; i++)
  477. if (!strcmp(pix_fmt_info[i].name, name))
  478. return i;
  479. return PIX_FMT_NONE;
  480. }
  481. #ifdef WORDS_BIGENDIAN
  482. # define X_NE(be, le) be
  483. #else
  484. # define X_NE(be, le) le
  485. #endif
  486. enum PixelFormat avcodec_get_pix_fmt(const char *name)
  487. {
  488. enum PixelFormat pix_fmt = avcodec_get_pix_fmt_internal(name);
  489. if (pix_fmt == PIX_FMT_NONE) {
  490. char name2[32];
  491. snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
  492. pix_fmt = avcodec_get_pix_fmt_internal(name2);
  493. }
  494. return pix_fmt;
  495. }
  496. void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
  497. {
  498. /* print header */
  499. if (pix_fmt < 0)
  500. snprintf (buf, buf_size,
  501. "name " " nb_channels" " depth" " is_alpha"
  502. );
  503. else{
  504. PixFmtInfo info= pix_fmt_info[pix_fmt];
  505. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  506. snprintf (buf, buf_size,
  507. "%-10s" " %1d " " %2d " " %c ",
  508. info.name,
  509. info.nb_channels,
  510. info.depth,
  511. is_alpha_char
  512. );
  513. }
  514. }
  515. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  516. {
  517. return pix_fmt_info[pix_fmt].is_hwaccel;
  518. }
  519. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  520. int i;
  521. for(i=0; i<256; i++){
  522. int r,g,b;
  523. switch(pix_fmt) {
  524. case PIX_FMT_RGB8:
  525. r= (i>>5 )*36;
  526. g= ((i>>2)&7)*36;
  527. b= (i&3 )*85;
  528. break;
  529. case PIX_FMT_BGR8:
  530. b= (i>>6 )*85;
  531. g= ((i>>3)&7)*36;
  532. r= (i&7 )*36;
  533. break;
  534. case PIX_FMT_RGB4_BYTE:
  535. r= (i>>3 )*255;
  536. g= ((i>>1)&3)*85;
  537. b= (i&1 )*255;
  538. break;
  539. case PIX_FMT_BGR4_BYTE:
  540. b= (i>>3 )*255;
  541. g= ((i>>1)&3)*85;
  542. r= (i&1 )*255;
  543. break;
  544. case PIX_FMT_GRAY8:
  545. r=b=g= i;
  546. break;
  547. default:
  548. return -1;
  549. }
  550. pal[i] = b + (g<<8) + (r<<16);
  551. }
  552. return 0;
  553. }
  554. int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
  555. {
  556. int w2;
  557. const PixFmtInfo *pinfo;
  558. memset(picture->linesize, 0, sizeof(picture->linesize));
  559. pinfo = &pix_fmt_info[pix_fmt];
  560. switch(pix_fmt) {
  561. case PIX_FMT_YUV420P:
  562. case PIX_FMT_YUV422P:
  563. case PIX_FMT_YUV444P:
  564. case PIX_FMT_YUV410P:
  565. case PIX_FMT_YUV411P:
  566. case PIX_FMT_YUV440P:
  567. case PIX_FMT_YUVJ420P:
  568. case PIX_FMT_YUVJ422P:
  569. case PIX_FMT_YUVJ444P:
  570. case PIX_FMT_YUVJ440P:
  571. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  572. picture->linesize[0] = width;
  573. picture->linesize[1] = w2;
  574. picture->linesize[2] = w2;
  575. break;
  576. case PIX_FMT_YUVA420P:
  577. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  578. picture->linesize[0] = width;
  579. picture->linesize[1] = w2;
  580. picture->linesize[2] = w2;
  581. picture->linesize[3] = width;
  582. break;
  583. case PIX_FMT_NV12:
  584. case PIX_FMT_NV21:
  585. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  586. picture->linesize[0] = width;
  587. picture->linesize[1] = w2;
  588. break;
  589. case PIX_FMT_RGB24:
  590. case PIX_FMT_BGR24:
  591. picture->linesize[0] = width * 3;
  592. break;
  593. case PIX_FMT_RGB32:
  594. case PIX_FMT_BGR32:
  595. case PIX_FMT_RGB32_1:
  596. case PIX_FMT_BGR32_1:
  597. picture->linesize[0] = width * 4;
  598. break;
  599. case PIX_FMT_RGB48BE:
  600. case PIX_FMT_RGB48LE:
  601. picture->linesize[0] = width * 6;
  602. break;
  603. case PIX_FMT_GRAY16BE:
  604. case PIX_FMT_GRAY16LE:
  605. case PIX_FMT_BGR555:
  606. case PIX_FMT_BGR565:
  607. case PIX_FMT_RGB555:
  608. case PIX_FMT_RGB565:
  609. case PIX_FMT_YUYV422:
  610. picture->linesize[0] = width * 2;
  611. break;
  612. case PIX_FMT_UYVY422:
  613. picture->linesize[0] = width * 2;
  614. break;
  615. case PIX_FMT_UYYVYY411:
  616. picture->linesize[0] = width + width/2;
  617. break;
  618. case PIX_FMT_RGB4:
  619. case PIX_FMT_BGR4:
  620. picture->linesize[0] = width / 2;
  621. break;
  622. case PIX_FMT_MONOWHITE:
  623. case PIX_FMT_MONOBLACK:
  624. picture->linesize[0] = (width + 7) >> 3;
  625. break;
  626. case PIX_FMT_PAL8:
  627. case PIX_FMT_RGB8:
  628. case PIX_FMT_BGR8:
  629. case PIX_FMT_RGB4_BYTE:
  630. case PIX_FMT_BGR4_BYTE:
  631. case PIX_FMT_GRAY8:
  632. picture->linesize[0] = width;
  633. break;
  634. default:
  635. return -1;
  636. }
  637. return 0;
  638. }
  639. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
  640. int height)
  641. {
  642. int size, h2, size2;
  643. const PixFmtInfo *pinfo;
  644. pinfo = &pix_fmt_info[pix_fmt];
  645. size = picture->linesize[0] * height;
  646. switch(pix_fmt) {
  647. case PIX_FMT_YUV420P:
  648. case PIX_FMT_YUV422P:
  649. case PIX_FMT_YUV444P:
  650. case PIX_FMT_YUV410P:
  651. case PIX_FMT_YUV411P:
  652. case PIX_FMT_YUV440P:
  653. case PIX_FMT_YUVJ420P:
  654. case PIX_FMT_YUVJ422P:
  655. case PIX_FMT_YUVJ444P:
  656. case PIX_FMT_YUVJ440P:
  657. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  658. size2 = picture->linesize[1] * h2;
  659. picture->data[0] = ptr;
  660. picture->data[1] = picture->data[0] + size;
  661. picture->data[2] = picture->data[1] + size2;
  662. picture->data[3] = NULL;
  663. return size + 2 * size2;
  664. case PIX_FMT_YUVA420P:
  665. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  666. size2 = picture->linesize[1] * h2;
  667. picture->data[0] = ptr;
  668. picture->data[1] = picture->data[0] + size;
  669. picture->data[2] = picture->data[1] + size2;
  670. picture->data[3] = picture->data[1] + size2 + size2;
  671. return 2 * size + 2 * size2;
  672. case PIX_FMT_NV12:
  673. case PIX_FMT_NV21:
  674. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  675. size2 = picture->linesize[1] * h2 * 2;
  676. picture->data[0] = ptr;
  677. picture->data[1] = picture->data[0] + size;
  678. picture->data[2] = NULL;
  679. picture->data[3] = NULL;
  680. return size + 2 * size2;
  681. case PIX_FMT_RGB24:
  682. case PIX_FMT_BGR24:
  683. case PIX_FMT_RGB32:
  684. case PIX_FMT_BGR32:
  685. case PIX_FMT_RGB32_1:
  686. case PIX_FMT_BGR32_1:
  687. case PIX_FMT_RGB48BE:
  688. case PIX_FMT_RGB48LE:
  689. case PIX_FMT_GRAY16BE:
  690. case PIX_FMT_GRAY16LE:
  691. case PIX_FMT_BGR555:
  692. case PIX_FMT_BGR565:
  693. case PIX_FMT_RGB555:
  694. case PIX_FMT_RGB565:
  695. case PIX_FMT_YUYV422:
  696. case PIX_FMT_UYVY422:
  697. case PIX_FMT_UYYVYY411:
  698. case PIX_FMT_RGB4:
  699. case PIX_FMT_BGR4:
  700. case PIX_FMT_MONOWHITE:
  701. case PIX_FMT_MONOBLACK:
  702. picture->data[0] = ptr;
  703. picture->data[1] = NULL;
  704. picture->data[2] = NULL;
  705. picture->data[3] = NULL;
  706. return size;
  707. case PIX_FMT_PAL8:
  708. case PIX_FMT_RGB8:
  709. case PIX_FMT_BGR8:
  710. case PIX_FMT_RGB4_BYTE:
  711. case PIX_FMT_BGR4_BYTE:
  712. case PIX_FMT_GRAY8:
  713. size2 = (size + 3) & ~3;
  714. picture->data[0] = ptr;
  715. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  716. picture->data[2] = NULL;
  717. picture->data[3] = NULL;
  718. return size2 + 256 * 4;
  719. default:
  720. picture->data[0] = NULL;
  721. picture->data[1] = NULL;
  722. picture->data[2] = NULL;
  723. picture->data[3] = NULL;
  724. return -1;
  725. }
  726. }
  727. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  728. enum PixelFormat pix_fmt, int width, int height)
  729. {
  730. if(avcodec_check_dimensions(NULL, width, height))
  731. return -1;
  732. if (ff_fill_linesize(picture, pix_fmt, width))
  733. return -1;
  734. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  735. }
  736. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  737. unsigned char *dest, int dest_size)
  738. {
  739. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  740. int i, j, w, ow, h, oh, data_planes;
  741. const unsigned char* s;
  742. int size = avpicture_get_size(pix_fmt, width, height);
  743. if (size > dest_size || size < 0)
  744. return -1;
  745. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  746. if (pix_fmt == PIX_FMT_YUYV422 ||
  747. pix_fmt == PIX_FMT_UYVY422 ||
  748. pix_fmt == PIX_FMT_BGR565 ||
  749. pix_fmt == PIX_FMT_BGR555 ||
  750. pix_fmt == PIX_FMT_RGB565 ||
  751. pix_fmt == PIX_FMT_RGB555)
  752. w = width * 2;
  753. else if (pix_fmt == PIX_FMT_UYYVYY411)
  754. w = width + width/2;
  755. else if (pix_fmt == PIX_FMT_PAL8)
  756. w = width;
  757. else
  758. w = width * (pf->depth * pf->nb_channels / 8);
  759. data_planes = 1;
  760. h = height;
  761. } else {
  762. data_planes = pf->nb_channels;
  763. w = (width*pf->depth + 7)/8;
  764. h = height;
  765. }
  766. ow = w;
  767. oh = h;
  768. for (i=0; i<data_planes; i++) {
  769. if (i == 1) {
  770. w = width >> pf->x_chroma_shift;
  771. h = height >> pf->y_chroma_shift;
  772. } else if (i == 3) {
  773. w = ow;
  774. h = oh;
  775. }
  776. s = src->data[i];
  777. for(j=0; j<h; j++) {
  778. memcpy(dest, s, w);
  779. dest += w;
  780. s += src->linesize[i];
  781. }
  782. }
  783. if (pf->pixel_type == FF_PIXEL_PALETTE)
  784. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  785. return size;
  786. }
  787. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  788. {
  789. AVPicture dummy_pict;
  790. if(avcodec_check_dimensions(NULL, width, height))
  791. return -1;
  792. switch (pix_fmt) {
  793. case PIX_FMT_RGB8:
  794. case PIX_FMT_BGR8:
  795. case PIX_FMT_RGB4_BYTE:
  796. case PIX_FMT_BGR4_BYTE:
  797. case PIX_FMT_GRAY8:
  798. // do not include palette for these pseudo-paletted formats
  799. return width * height;
  800. }
  801. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  802. }
  803. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  804. int has_alpha)
  805. {
  806. const PixFmtInfo *pf, *ps;
  807. int loss;
  808. ps = &pix_fmt_info[src_pix_fmt];
  809. pf = &pix_fmt_info[dst_pix_fmt];
  810. /* compute loss */
  811. loss = 0;
  812. pf = &pix_fmt_info[dst_pix_fmt];
  813. if (pf->depth < ps->depth ||
  814. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  815. loss |= FF_LOSS_DEPTH;
  816. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  817. pf->y_chroma_shift > ps->y_chroma_shift)
  818. loss |= FF_LOSS_RESOLUTION;
  819. switch(pf->color_type) {
  820. case FF_COLOR_RGB:
  821. if (ps->color_type != FF_COLOR_RGB &&
  822. ps->color_type != FF_COLOR_GRAY)
  823. loss |= FF_LOSS_COLORSPACE;
  824. break;
  825. case FF_COLOR_GRAY:
  826. if (ps->color_type != FF_COLOR_GRAY)
  827. loss |= FF_LOSS_COLORSPACE;
  828. break;
  829. case FF_COLOR_YUV:
  830. if (ps->color_type != FF_COLOR_YUV)
  831. loss |= FF_LOSS_COLORSPACE;
  832. break;
  833. case FF_COLOR_YUV_JPEG:
  834. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  835. ps->color_type != FF_COLOR_YUV &&
  836. ps->color_type != FF_COLOR_GRAY)
  837. loss |= FF_LOSS_COLORSPACE;
  838. break;
  839. default:
  840. /* fail safe test */
  841. if (ps->color_type != pf->color_type)
  842. loss |= FF_LOSS_COLORSPACE;
  843. break;
  844. }
  845. if (pf->color_type == FF_COLOR_GRAY &&
  846. ps->color_type != FF_COLOR_GRAY)
  847. loss |= FF_LOSS_CHROMA;
  848. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  849. loss |= FF_LOSS_ALPHA;
  850. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  851. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  852. loss |= FF_LOSS_COLORQUANT;
  853. return loss;
  854. }
  855. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  856. {
  857. int bits;
  858. const PixFmtInfo *pf;
  859. pf = &pix_fmt_info[pix_fmt];
  860. switch(pf->pixel_type) {
  861. case FF_PIXEL_PACKED:
  862. switch(pix_fmt) {
  863. case PIX_FMT_YUYV422:
  864. case PIX_FMT_UYVY422:
  865. case PIX_FMT_RGB565:
  866. case PIX_FMT_RGB555:
  867. case PIX_FMT_BGR565:
  868. case PIX_FMT_BGR555:
  869. bits = 16;
  870. break;
  871. case PIX_FMT_UYYVYY411:
  872. bits = 12;
  873. break;
  874. default:
  875. bits = pf->depth * pf->nb_channels;
  876. break;
  877. }
  878. break;
  879. case FF_PIXEL_PLANAR:
  880. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  881. bits = pf->depth * pf->nb_channels;
  882. } else {
  883. bits = pf->depth + ((2 * pf->depth) >>
  884. (pf->x_chroma_shift + pf->y_chroma_shift));
  885. }
  886. break;
  887. case FF_PIXEL_PALETTE:
  888. bits = 8;
  889. break;
  890. default:
  891. bits = -1;
  892. break;
  893. }
  894. return bits;
  895. }
  896. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  897. enum PixelFormat src_pix_fmt,
  898. int has_alpha,
  899. int loss_mask)
  900. {
  901. int dist, i, loss, min_dist;
  902. enum PixelFormat dst_pix_fmt;
  903. /* find exact color match with smallest size */
  904. dst_pix_fmt = -1;
  905. min_dist = 0x7fffffff;
  906. for(i = 0;i < PIX_FMT_NB; i++) {
  907. if (pix_fmt_mask & (1ULL << i)) {
  908. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  909. if (loss == 0) {
  910. dist = avg_bits_per_pixel(i);
  911. if (dist < min_dist) {
  912. min_dist = dist;
  913. dst_pix_fmt = i;
  914. }
  915. }
  916. }
  917. }
  918. return dst_pix_fmt;
  919. }
  920. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  921. int has_alpha, int *loss_ptr)
  922. {
  923. enum PixelFormat dst_pix_fmt;
  924. int loss_mask, i;
  925. static const int loss_mask_order[] = {
  926. ~0, /* no loss first */
  927. ~FF_LOSS_ALPHA,
  928. ~FF_LOSS_RESOLUTION,
  929. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  930. ~FF_LOSS_COLORQUANT,
  931. ~FF_LOSS_DEPTH,
  932. 0,
  933. };
  934. /* try with successive loss */
  935. i = 0;
  936. for(;;) {
  937. loss_mask = loss_mask_order[i++];
  938. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  939. has_alpha, loss_mask);
  940. if (dst_pix_fmt >= 0)
  941. goto found;
  942. if (loss_mask == 0)
  943. break;
  944. }
  945. return -1;
  946. found:
  947. if (loss_ptr)
  948. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  949. return dst_pix_fmt;
  950. }
  951. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  952. const uint8_t *src, int src_wrap,
  953. int width, int height)
  954. {
  955. if((!dst) || (!src))
  956. return;
  957. for(;height > 0; height--) {
  958. memcpy(dst, src, width);
  959. dst += dst_wrap;
  960. src += src_wrap;
  961. }
  962. }
  963. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  964. {
  965. int bits;
  966. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  967. pf = &pix_fmt_info[pix_fmt];
  968. switch(pf->pixel_type) {
  969. case FF_PIXEL_PACKED:
  970. switch(pix_fmt) {
  971. case PIX_FMT_YUYV422:
  972. case PIX_FMT_UYVY422:
  973. case PIX_FMT_RGB565:
  974. case PIX_FMT_RGB555:
  975. case PIX_FMT_BGR565:
  976. case PIX_FMT_BGR555:
  977. bits = 16;
  978. break;
  979. case PIX_FMT_UYYVYY411:
  980. bits = 12;
  981. break;
  982. default:
  983. bits = pf->depth * pf->nb_channels;
  984. break;
  985. }
  986. return (width * bits + 7) >> 3;
  987. break;
  988. case FF_PIXEL_PLANAR:
  989. if (plane == 1 || plane == 2)
  990. width= -((-width)>>pf->x_chroma_shift);
  991. return (width * pf->depth + 7) >> 3;
  992. break;
  993. case FF_PIXEL_PALETTE:
  994. if (plane == 0)
  995. return width;
  996. break;
  997. }
  998. return -1;
  999. }
  1000. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  1001. enum PixelFormat pix_fmt, int width, int height)
  1002. {
  1003. int i;
  1004. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1005. pf = &pix_fmt_info[pix_fmt];
  1006. switch(pf->pixel_type) {
  1007. case FF_PIXEL_PACKED:
  1008. case FF_PIXEL_PLANAR:
  1009. for(i = 0; i < pf->nb_channels; i++) {
  1010. int h;
  1011. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  1012. h = height;
  1013. if (i == 1 || i == 2) {
  1014. h= -((-height)>>pf->y_chroma_shift);
  1015. }
  1016. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  1017. src->data[i], src->linesize[i],
  1018. bwidth, h);
  1019. }
  1020. break;
  1021. case FF_PIXEL_PALETTE:
  1022. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  1023. src->data[0], src->linesize[0],
  1024. width, height);
  1025. /* copy the palette */
  1026. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  1027. src->data[1], src->linesize[1],
  1028. 4, 256);
  1029. break;
  1030. }
  1031. }
  1032. /* 2x2 -> 1x1 */
  1033. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1034. const uint8_t *src, int src_wrap,
  1035. int width, int height)
  1036. {
  1037. int w;
  1038. const uint8_t *s1, *s2;
  1039. uint8_t *d;
  1040. for(;height > 0; height--) {
  1041. s1 = src;
  1042. s2 = s1 + src_wrap;
  1043. d = dst;
  1044. for(w = width;w >= 4; w-=4) {
  1045. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1046. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1047. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1048. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1049. s1 += 8;
  1050. s2 += 8;
  1051. d += 4;
  1052. }
  1053. for(;w > 0; w--) {
  1054. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1055. s1 += 2;
  1056. s2 += 2;
  1057. d++;
  1058. }
  1059. src += 2 * src_wrap;
  1060. dst += dst_wrap;
  1061. }
  1062. }
  1063. /* 4x4 -> 1x1 */
  1064. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1065. const uint8_t *src, int src_wrap,
  1066. int width, int height)
  1067. {
  1068. int w;
  1069. const uint8_t *s1, *s2, *s3, *s4;
  1070. uint8_t *d;
  1071. for(;height > 0; height--) {
  1072. s1 = src;
  1073. s2 = s1 + src_wrap;
  1074. s3 = s2 + src_wrap;
  1075. s4 = s3 + src_wrap;
  1076. d = dst;
  1077. for(w = width;w > 0; w--) {
  1078. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1079. s2[0] + s2[1] + s2[2] + s2[3] +
  1080. s3[0] + s3[1] + s3[2] + s3[3] +
  1081. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1082. s1 += 4;
  1083. s2 += 4;
  1084. s3 += 4;
  1085. s4 += 4;
  1086. d++;
  1087. }
  1088. src += 4 * src_wrap;
  1089. dst += dst_wrap;
  1090. }
  1091. }
  1092. /* 8x8 -> 1x1 */
  1093. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1094. const uint8_t *src, int src_wrap,
  1095. int width, int height)
  1096. {
  1097. int w, i;
  1098. for(;height > 0; height--) {
  1099. for(w = width;w > 0; w--) {
  1100. int tmp=0;
  1101. for(i=0; i<8; i++){
  1102. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1103. src += src_wrap;
  1104. }
  1105. *(dst++) = (tmp + 32)>>6;
  1106. src += 8 - 8*src_wrap;
  1107. }
  1108. src += 8*src_wrap - 8*width;
  1109. dst += dst_wrap - width;
  1110. }
  1111. }
  1112. int avpicture_alloc(AVPicture *picture,
  1113. enum PixelFormat pix_fmt, int width, int height)
  1114. {
  1115. int size;
  1116. void *ptr;
  1117. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  1118. if(size<0)
  1119. goto fail;
  1120. ptr = av_malloc(size);
  1121. if (!ptr)
  1122. goto fail;
  1123. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1124. if(picture->data[1] && !picture->data[2])
  1125. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1126. return 0;
  1127. fail:
  1128. memset(picture, 0, sizeof(AVPicture));
  1129. return -1;
  1130. }
  1131. void avpicture_free(AVPicture *picture)
  1132. {
  1133. av_free(picture->data[0]);
  1134. }
  1135. /* return true if yuv planar */
  1136. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1137. {
  1138. return (ps->color_type == FF_COLOR_YUV ||
  1139. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1140. ps->pixel_type == FF_PIXEL_PLANAR;
  1141. }
  1142. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1143. int pix_fmt, int top_band, int left_band)
  1144. {
  1145. int y_shift;
  1146. int x_shift;
  1147. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1148. return -1;
  1149. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1150. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1151. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1152. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1153. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1154. dst->linesize[0] = src->linesize[0];
  1155. dst->linesize[1] = src->linesize[1];
  1156. dst->linesize[2] = src->linesize[2];
  1157. return 0;
  1158. }
  1159. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1160. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1161. int *color)
  1162. {
  1163. uint8_t *optr;
  1164. int y_shift;
  1165. int x_shift;
  1166. int yheight;
  1167. int i, y;
  1168. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1169. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1170. for (i = 0; i < 3; i++) {
  1171. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1172. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1173. if (padtop || padleft) {
  1174. memset(dst->data[i], color[i],
  1175. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1176. }
  1177. if (padleft || padright) {
  1178. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1179. (dst->linesize[i] - (padright >> x_shift));
  1180. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1181. for (y = 0; y < yheight; y++) {
  1182. memset(optr, color[i], (padleft + padright) >> x_shift);
  1183. optr += dst->linesize[i];
  1184. }
  1185. }
  1186. if (src) { /* first line */
  1187. uint8_t *iptr = src->data[i];
  1188. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1189. (padleft >> x_shift);
  1190. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1191. iptr += src->linesize[i];
  1192. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1193. (dst->linesize[i] - (padright >> x_shift));
  1194. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1195. for (y = 0; y < yheight; y++) {
  1196. memset(optr, color[i], (padleft + padright) >> x_shift);
  1197. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1198. (width - padleft - padright) >> x_shift);
  1199. iptr += src->linesize[i];
  1200. optr += dst->linesize[i];
  1201. }
  1202. }
  1203. if (padbottom || padright) {
  1204. optr = dst->data[i] + dst->linesize[i] *
  1205. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1206. memset(optr, color[i],dst->linesize[i] *
  1207. (padbottom >> y_shift) + (padright >> x_shift));
  1208. }
  1209. }
  1210. return 0;
  1211. }
  1212. /* NOTE: we scan all the pixels to have an exact information */
  1213. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1214. {
  1215. const unsigned char *p;
  1216. int src_wrap, ret, x, y;
  1217. unsigned int a;
  1218. uint32_t *palette = (uint32_t *)src->data[1];
  1219. p = src->data[0];
  1220. src_wrap = src->linesize[0] - width;
  1221. ret = 0;
  1222. for(y=0;y<height;y++) {
  1223. for(x=0;x<width;x++) {
  1224. a = palette[p[0]] >> 24;
  1225. if (a == 0x00) {
  1226. ret |= FF_ALPHA_TRANSP;
  1227. } else if (a != 0xff) {
  1228. ret |= FF_ALPHA_SEMI_TRANSP;
  1229. }
  1230. p++;
  1231. }
  1232. p += src_wrap;
  1233. }
  1234. return ret;
  1235. }
  1236. int img_get_alpha_info(const AVPicture *src,
  1237. enum PixelFormat pix_fmt, int width, int height)
  1238. {
  1239. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1240. int ret;
  1241. pf = &pix_fmt_info[pix_fmt];
  1242. /* no alpha can be represented in format */
  1243. if (!pf->is_alpha)
  1244. return 0;
  1245. switch(pix_fmt) {
  1246. case PIX_FMT_PAL8:
  1247. ret = get_alpha_info_pal8(src, width, height);
  1248. break;
  1249. default:
  1250. /* we do not know, so everything is indicated */
  1251. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1252. break;
  1253. }
  1254. return ret;
  1255. }
  1256. #if HAVE_MMX
  1257. #define DEINT_INPLACE_LINE_LUM \
  1258. movd_m2r(lum_m4[0],mm0);\
  1259. movd_m2r(lum_m3[0],mm1);\
  1260. movd_m2r(lum_m2[0],mm2);\
  1261. movd_m2r(lum_m1[0],mm3);\
  1262. movd_m2r(lum[0],mm4);\
  1263. punpcklbw_r2r(mm7,mm0);\
  1264. movd_r2m(mm2,lum_m4[0]);\
  1265. punpcklbw_r2r(mm7,mm1);\
  1266. punpcklbw_r2r(mm7,mm2);\
  1267. punpcklbw_r2r(mm7,mm3);\
  1268. punpcklbw_r2r(mm7,mm4);\
  1269. paddw_r2r(mm3,mm1);\
  1270. psllw_i2r(1,mm2);\
  1271. paddw_r2r(mm4,mm0);\
  1272. psllw_i2r(2,mm1);\
  1273. paddw_r2r(mm6,mm2);\
  1274. paddw_r2r(mm2,mm1);\
  1275. psubusw_r2r(mm0,mm1);\
  1276. psrlw_i2r(3,mm1);\
  1277. packuswb_r2r(mm7,mm1);\
  1278. movd_r2m(mm1,lum_m2[0]);
  1279. #define DEINT_LINE_LUM \
  1280. movd_m2r(lum_m4[0],mm0);\
  1281. movd_m2r(lum_m3[0],mm1);\
  1282. movd_m2r(lum_m2[0],mm2);\
  1283. movd_m2r(lum_m1[0],mm3);\
  1284. movd_m2r(lum[0],mm4);\
  1285. punpcklbw_r2r(mm7,mm0);\
  1286. punpcklbw_r2r(mm7,mm1);\
  1287. punpcklbw_r2r(mm7,mm2);\
  1288. punpcklbw_r2r(mm7,mm3);\
  1289. punpcklbw_r2r(mm7,mm4);\
  1290. paddw_r2r(mm3,mm1);\
  1291. psllw_i2r(1,mm2);\
  1292. paddw_r2r(mm4,mm0);\
  1293. psllw_i2r(2,mm1);\
  1294. paddw_r2r(mm6,mm2);\
  1295. paddw_r2r(mm2,mm1);\
  1296. psubusw_r2r(mm0,mm1);\
  1297. psrlw_i2r(3,mm1);\
  1298. packuswb_r2r(mm7,mm1);\
  1299. movd_r2m(mm1,dst[0]);
  1300. #endif
  1301. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1302. static void deinterlace_line(uint8_t *dst,
  1303. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1304. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1305. const uint8_t *lum,
  1306. int size)
  1307. {
  1308. #if !HAVE_MMX
  1309. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1310. int sum;
  1311. for(;size > 0;size--) {
  1312. sum = -lum_m4[0];
  1313. sum += lum_m3[0] << 2;
  1314. sum += lum_m2[0] << 1;
  1315. sum += lum_m1[0] << 2;
  1316. sum += -lum[0];
  1317. dst[0] = cm[(sum + 4) >> 3];
  1318. lum_m4++;
  1319. lum_m3++;
  1320. lum_m2++;
  1321. lum_m1++;
  1322. lum++;
  1323. dst++;
  1324. }
  1325. #else
  1326. {
  1327. pxor_r2r(mm7,mm7);
  1328. movq_m2r(ff_pw_4,mm6);
  1329. }
  1330. for (;size > 3; size-=4) {
  1331. DEINT_LINE_LUM
  1332. lum_m4+=4;
  1333. lum_m3+=4;
  1334. lum_m2+=4;
  1335. lum_m1+=4;
  1336. lum+=4;
  1337. dst+=4;
  1338. }
  1339. #endif
  1340. }
  1341. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1342. int size)
  1343. {
  1344. #if !HAVE_MMX
  1345. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1346. int sum;
  1347. for(;size > 0;size--) {
  1348. sum = -lum_m4[0];
  1349. sum += lum_m3[0] << 2;
  1350. sum += lum_m2[0] << 1;
  1351. lum_m4[0]=lum_m2[0];
  1352. sum += lum_m1[0] << 2;
  1353. sum += -lum[0];
  1354. lum_m2[0] = cm[(sum + 4) >> 3];
  1355. lum_m4++;
  1356. lum_m3++;
  1357. lum_m2++;
  1358. lum_m1++;
  1359. lum++;
  1360. }
  1361. #else
  1362. {
  1363. pxor_r2r(mm7,mm7);
  1364. movq_m2r(ff_pw_4,mm6);
  1365. }
  1366. for (;size > 3; size-=4) {
  1367. DEINT_INPLACE_LINE_LUM
  1368. lum_m4+=4;
  1369. lum_m3+=4;
  1370. lum_m2+=4;
  1371. lum_m1+=4;
  1372. lum+=4;
  1373. }
  1374. #endif
  1375. }
  1376. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1377. top field is copied as is, but the bottom field is deinterlaced
  1378. against the top field. */
  1379. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1380. const uint8_t *src1, int src_wrap,
  1381. int width, int height)
  1382. {
  1383. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1384. int y;
  1385. src_m2 = src1;
  1386. src_m1 = src1;
  1387. src_0=&src_m1[src_wrap];
  1388. src_p1=&src_0[src_wrap];
  1389. src_p2=&src_p1[src_wrap];
  1390. for(y=0;y<(height-2);y+=2) {
  1391. memcpy(dst,src_m1,width);
  1392. dst += dst_wrap;
  1393. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1394. src_m2 = src_0;
  1395. src_m1 = src_p1;
  1396. src_0 = src_p2;
  1397. src_p1 += 2*src_wrap;
  1398. src_p2 += 2*src_wrap;
  1399. dst += dst_wrap;
  1400. }
  1401. memcpy(dst,src_m1,width);
  1402. dst += dst_wrap;
  1403. /* do last line */
  1404. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1405. }
  1406. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1407. int width, int height)
  1408. {
  1409. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1410. int y;
  1411. uint8_t *buf;
  1412. buf = (uint8_t*)av_malloc(width);
  1413. src_m1 = src1;
  1414. memcpy(buf,src_m1,width);
  1415. src_0=&src_m1[src_wrap];
  1416. src_p1=&src_0[src_wrap];
  1417. src_p2=&src_p1[src_wrap];
  1418. for(y=0;y<(height-2);y+=2) {
  1419. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1420. src_m1 = src_p1;
  1421. src_0 = src_p2;
  1422. src_p1 += 2*src_wrap;
  1423. src_p2 += 2*src_wrap;
  1424. }
  1425. /* do last line */
  1426. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1427. av_free(buf);
  1428. }
  1429. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1430. enum PixelFormat pix_fmt, int width, int height)
  1431. {
  1432. int i;
  1433. if (pix_fmt != PIX_FMT_YUV420P &&
  1434. pix_fmt != PIX_FMT_YUV422P &&
  1435. pix_fmt != PIX_FMT_YUV444P &&
  1436. pix_fmt != PIX_FMT_YUV411P &&
  1437. pix_fmt != PIX_FMT_GRAY8)
  1438. return -1;
  1439. if ((width & 3) != 0 || (height & 3) != 0)
  1440. return -1;
  1441. for(i=0;i<3;i++) {
  1442. if (i == 1) {
  1443. switch(pix_fmt) {
  1444. case PIX_FMT_YUV420P:
  1445. width >>= 1;
  1446. height >>= 1;
  1447. break;
  1448. case PIX_FMT_YUV422P:
  1449. width >>= 1;
  1450. break;
  1451. case PIX_FMT_YUV411P:
  1452. width >>= 2;
  1453. break;
  1454. default:
  1455. break;
  1456. }
  1457. if (pix_fmt == PIX_FMT_GRAY8) {
  1458. break;
  1459. }
  1460. }
  1461. if (src == dst) {
  1462. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1463. width, height);
  1464. } else {
  1465. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1466. src->data[i], src->linesize[i],
  1467. width, height);
  1468. }
  1469. }
  1470. emms_c();
  1471. return 0;
  1472. }