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.

1626 lines
46KB

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