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.

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