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.

1043 lines
31KB

  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  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 "internal.h"
  33. #include "libavutil/colorspace.h"
  34. #include "libavutil/common.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #if HAVE_MMX_EXTERNAL
  38. #include "x86/dsputil_mmx.h"
  39. #endif
  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. #if HAVE_MMX_EXTERNAL
  48. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  49. #define deinterlace_line ff_deinterlace_line_mmx
  50. #else
  51. #define deinterlace_line_inplace deinterlace_line_inplace_c
  52. #define deinterlace_line deinterlace_line_c
  53. #endif
  54. typedef struct PixFmtInfo {
  55. uint8_t nb_channels; /**< number of channels (including alpha) */
  56. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  57. uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
  58. uint8_t is_alpha : 1; /**< true if alpha can be specified */
  59. uint8_t depth; /**< bit depth of the color components */
  60. } PixFmtInfo;
  61. /* this table gives more information about formats */
  62. static const PixFmtInfo pix_fmt_info[AV_PIX_FMT_NB] = {
  63. /* YUV formats */
  64. [AV_PIX_FMT_YUV420P] = {
  65. .nb_channels = 3,
  66. .color_type = FF_COLOR_YUV,
  67. .pixel_type = FF_PIXEL_PLANAR,
  68. .depth = 8,
  69. },
  70. [AV_PIX_FMT_YUV422P] = {
  71. .nb_channels = 3,
  72. .color_type = FF_COLOR_YUV,
  73. .pixel_type = FF_PIXEL_PLANAR,
  74. .depth = 8,
  75. },
  76. [AV_PIX_FMT_YUV444P] = {
  77. .nb_channels = 3,
  78. .color_type = FF_COLOR_YUV,
  79. .pixel_type = FF_PIXEL_PLANAR,
  80. .depth = 8,
  81. },
  82. [AV_PIX_FMT_YUYV422] = {
  83. .nb_channels = 1,
  84. .color_type = FF_COLOR_YUV,
  85. .pixel_type = FF_PIXEL_PACKED,
  86. .depth = 8,
  87. },
  88. [AV_PIX_FMT_UYVY422] = {
  89. .nb_channels = 1,
  90. .color_type = FF_COLOR_YUV,
  91. .pixel_type = FF_PIXEL_PACKED,
  92. .depth = 8,
  93. },
  94. [AV_PIX_FMT_YUV410P] = {
  95. .nb_channels = 3,
  96. .color_type = FF_COLOR_YUV,
  97. .pixel_type = FF_PIXEL_PLANAR,
  98. .depth = 8,
  99. },
  100. [AV_PIX_FMT_YUV411P] = {
  101. .nb_channels = 3,
  102. .color_type = FF_COLOR_YUV,
  103. .pixel_type = FF_PIXEL_PLANAR,
  104. .depth = 8,
  105. },
  106. [AV_PIX_FMT_YUV440P] = {
  107. .nb_channels = 3,
  108. .color_type = FF_COLOR_YUV,
  109. .pixel_type = FF_PIXEL_PLANAR,
  110. .depth = 8,
  111. },
  112. [AV_PIX_FMT_YUV420P16LE] = {
  113. .nb_channels = 3,
  114. .color_type = FF_COLOR_YUV,
  115. .pixel_type = FF_PIXEL_PLANAR,
  116. .depth = 16,
  117. },
  118. [AV_PIX_FMT_YUV422P16LE] = {
  119. .nb_channels = 3,
  120. .color_type = FF_COLOR_YUV,
  121. .pixel_type = FF_PIXEL_PLANAR,
  122. .depth = 16,
  123. },
  124. [AV_PIX_FMT_YUV444P16LE] = {
  125. .nb_channels = 3,
  126. .color_type = FF_COLOR_YUV,
  127. .pixel_type = FF_PIXEL_PLANAR,
  128. .depth = 16,
  129. },
  130. [AV_PIX_FMT_YUV420P16BE] = {
  131. .nb_channels = 3,
  132. .color_type = FF_COLOR_YUV,
  133. .pixel_type = FF_PIXEL_PLANAR,
  134. .depth = 16,
  135. },
  136. [AV_PIX_FMT_YUV422P16BE] = {
  137. .nb_channels = 3,
  138. .color_type = FF_COLOR_YUV,
  139. .pixel_type = FF_PIXEL_PLANAR,
  140. .depth = 16,
  141. },
  142. [AV_PIX_FMT_YUV444P16BE] = {
  143. .nb_channels = 3,
  144. .color_type = FF_COLOR_YUV,
  145. .pixel_type = FF_PIXEL_PLANAR,
  146. .depth = 16,
  147. },
  148. /* YUV formats with alpha plane */
  149. [AV_PIX_FMT_YUVA420P] = {
  150. .nb_channels = 4,
  151. .color_type = FF_COLOR_YUV,
  152. .pixel_type = FF_PIXEL_PLANAR,
  153. .depth = 8,
  154. },
  155. /* JPEG YUV */
  156. [AV_PIX_FMT_YUVJ420P] = {
  157. .nb_channels = 3,
  158. .color_type = FF_COLOR_YUV_JPEG,
  159. .pixel_type = FF_PIXEL_PLANAR,
  160. .depth = 8,
  161. },
  162. [AV_PIX_FMT_YUVJ422P] = {
  163. .nb_channels = 3,
  164. .color_type = FF_COLOR_YUV_JPEG,
  165. .pixel_type = FF_PIXEL_PLANAR,
  166. .depth = 8,
  167. },
  168. [AV_PIX_FMT_YUVJ444P] = {
  169. .nb_channels = 3,
  170. .color_type = FF_COLOR_YUV_JPEG,
  171. .pixel_type = FF_PIXEL_PLANAR,
  172. .depth = 8,
  173. },
  174. [AV_PIX_FMT_YUVJ440P] = {
  175. .nb_channels = 3,
  176. .color_type = FF_COLOR_YUV_JPEG,
  177. .pixel_type = FF_PIXEL_PLANAR,
  178. .depth = 8,
  179. },
  180. /* RGB formats */
  181. [AV_PIX_FMT_RGB24] = {
  182. .nb_channels = 3,
  183. .color_type = FF_COLOR_RGB,
  184. .pixel_type = FF_PIXEL_PACKED,
  185. .depth = 8,
  186. },
  187. [AV_PIX_FMT_BGR24] = {
  188. .nb_channels = 3,
  189. .color_type = FF_COLOR_RGB,
  190. .pixel_type = FF_PIXEL_PACKED,
  191. .depth = 8,
  192. },
  193. [AV_PIX_FMT_ARGB] = {
  194. .nb_channels = 4, .is_alpha = 1,
  195. .color_type = FF_COLOR_RGB,
  196. .pixel_type = FF_PIXEL_PACKED,
  197. .depth = 8,
  198. },
  199. [AV_PIX_FMT_RGB48BE] = {
  200. .nb_channels = 3,
  201. .color_type = FF_COLOR_RGB,
  202. .pixel_type = FF_PIXEL_PACKED,
  203. .depth = 16,
  204. },
  205. [AV_PIX_FMT_RGB48LE] = {
  206. .nb_channels = 3,
  207. .color_type = FF_COLOR_RGB,
  208. .pixel_type = FF_PIXEL_PACKED,
  209. .depth = 16,
  210. },
  211. [AV_PIX_FMT_RGB565BE] = {
  212. .nb_channels = 3,
  213. .color_type = FF_COLOR_RGB,
  214. .pixel_type = FF_PIXEL_PACKED,
  215. .depth = 5,
  216. },
  217. [AV_PIX_FMT_RGB565LE] = {
  218. .nb_channels = 3,
  219. .color_type = FF_COLOR_RGB,
  220. .pixel_type = FF_PIXEL_PACKED,
  221. .depth = 5,
  222. },
  223. [AV_PIX_FMT_RGB555BE] = {
  224. .nb_channels = 3,
  225. .color_type = FF_COLOR_RGB,
  226. .pixel_type = FF_PIXEL_PACKED,
  227. .depth = 5,
  228. },
  229. [AV_PIX_FMT_RGB555LE] = {
  230. .nb_channels = 3,
  231. .color_type = FF_COLOR_RGB,
  232. .pixel_type = FF_PIXEL_PACKED,
  233. .depth = 5,
  234. },
  235. [AV_PIX_FMT_RGB444BE] = {
  236. .nb_channels = 3,
  237. .color_type = FF_COLOR_RGB,
  238. .pixel_type = FF_PIXEL_PACKED,
  239. .depth = 4,
  240. },
  241. [AV_PIX_FMT_RGB444LE] = {
  242. .nb_channels = 3,
  243. .color_type = FF_COLOR_RGB,
  244. .pixel_type = FF_PIXEL_PACKED,
  245. .depth = 4,
  246. },
  247. /* gray / mono formats */
  248. [AV_PIX_FMT_GRAY16BE] = {
  249. .nb_channels = 1,
  250. .color_type = FF_COLOR_GRAY,
  251. .pixel_type = FF_PIXEL_PLANAR,
  252. .depth = 16,
  253. },
  254. [AV_PIX_FMT_GRAY16LE] = {
  255. .nb_channels = 1,
  256. .color_type = FF_COLOR_GRAY,
  257. .pixel_type = FF_PIXEL_PLANAR,
  258. .depth = 16,
  259. },
  260. [AV_PIX_FMT_GRAY8] = {
  261. .nb_channels = 1,
  262. .color_type = FF_COLOR_GRAY,
  263. .pixel_type = FF_PIXEL_PLANAR,
  264. .depth = 8,
  265. },
  266. [AV_PIX_FMT_MONOWHITE] = {
  267. .nb_channels = 1,
  268. .color_type = FF_COLOR_GRAY,
  269. .pixel_type = FF_PIXEL_PLANAR,
  270. .depth = 1,
  271. },
  272. [AV_PIX_FMT_MONOBLACK] = {
  273. .nb_channels = 1,
  274. .color_type = FF_COLOR_GRAY,
  275. .pixel_type = FF_PIXEL_PLANAR,
  276. .depth = 1,
  277. },
  278. /* paletted formats */
  279. [AV_PIX_FMT_PAL8] = {
  280. .nb_channels = 4, .is_alpha = 1,
  281. .color_type = FF_COLOR_RGB,
  282. .pixel_type = FF_PIXEL_PALETTE,
  283. .depth = 8,
  284. },
  285. [AV_PIX_FMT_UYYVYY411] = {
  286. .nb_channels = 1,
  287. .color_type = FF_COLOR_YUV,
  288. .pixel_type = FF_PIXEL_PACKED,
  289. .depth = 8,
  290. },
  291. [AV_PIX_FMT_ABGR] = {
  292. .nb_channels = 4, .is_alpha = 1,
  293. .color_type = FF_COLOR_RGB,
  294. .pixel_type = FF_PIXEL_PACKED,
  295. .depth = 8,
  296. },
  297. [AV_PIX_FMT_BGR565BE] = {
  298. .nb_channels = 3,
  299. .color_type = FF_COLOR_RGB,
  300. .pixel_type = FF_PIXEL_PACKED,
  301. .depth = 5,
  302. },
  303. [AV_PIX_FMT_BGR565LE] = {
  304. .nb_channels = 3,
  305. .color_type = FF_COLOR_RGB,
  306. .pixel_type = FF_PIXEL_PACKED,
  307. .depth = 5,
  308. },
  309. [AV_PIX_FMT_BGR555BE] = {
  310. .nb_channels = 3,
  311. .color_type = FF_COLOR_RGB,
  312. .pixel_type = FF_PIXEL_PACKED,
  313. .depth = 5,
  314. },
  315. [AV_PIX_FMT_BGR555LE] = {
  316. .nb_channels = 3,
  317. .color_type = FF_COLOR_RGB,
  318. .pixel_type = FF_PIXEL_PACKED,
  319. .depth = 5,
  320. },
  321. [AV_PIX_FMT_BGR444BE] = {
  322. .nb_channels = 3,
  323. .color_type = FF_COLOR_RGB,
  324. .pixel_type = FF_PIXEL_PACKED,
  325. .depth = 4,
  326. },
  327. [AV_PIX_FMT_BGR444LE] = {
  328. .nb_channels = 3,
  329. .color_type = FF_COLOR_RGB,
  330. .pixel_type = FF_PIXEL_PACKED,
  331. .depth = 4,
  332. },
  333. [AV_PIX_FMT_RGB8] = {
  334. .nb_channels = 1,
  335. .color_type = FF_COLOR_RGB,
  336. .pixel_type = FF_PIXEL_PACKED,
  337. .depth = 8,
  338. },
  339. [AV_PIX_FMT_RGB4] = {
  340. .nb_channels = 1,
  341. .color_type = FF_COLOR_RGB,
  342. .pixel_type = FF_PIXEL_PACKED,
  343. .depth = 4,
  344. },
  345. [AV_PIX_FMT_RGB4_BYTE] = {
  346. .nb_channels = 1,
  347. .color_type = FF_COLOR_RGB,
  348. .pixel_type = FF_PIXEL_PACKED,
  349. .depth = 8,
  350. },
  351. [AV_PIX_FMT_BGR8] = {
  352. .nb_channels = 1,
  353. .color_type = FF_COLOR_RGB,
  354. .pixel_type = FF_PIXEL_PACKED,
  355. .depth = 8,
  356. },
  357. [AV_PIX_FMT_BGR4] = {
  358. .nb_channels = 1,
  359. .color_type = FF_COLOR_RGB,
  360. .pixel_type = FF_PIXEL_PACKED,
  361. .depth = 4,
  362. },
  363. [AV_PIX_FMT_BGR4_BYTE] = {
  364. .nb_channels = 1,
  365. .color_type = FF_COLOR_RGB,
  366. .pixel_type = FF_PIXEL_PACKED,
  367. .depth = 8,
  368. },
  369. [AV_PIX_FMT_NV12] = {
  370. .nb_channels = 2,
  371. .color_type = FF_COLOR_YUV,
  372. .pixel_type = FF_PIXEL_PLANAR,
  373. .depth = 8,
  374. },
  375. [AV_PIX_FMT_NV21] = {
  376. .nb_channels = 2,
  377. .color_type = FF_COLOR_YUV,
  378. .pixel_type = FF_PIXEL_PLANAR,
  379. .depth = 8,
  380. },
  381. [AV_PIX_FMT_BGRA] = {
  382. .nb_channels = 4, .is_alpha = 1,
  383. .color_type = FF_COLOR_RGB,
  384. .pixel_type = FF_PIXEL_PACKED,
  385. .depth = 8,
  386. },
  387. [AV_PIX_FMT_RGBA] = {
  388. .nb_channels = 4, .is_alpha = 1,
  389. .color_type = FF_COLOR_RGB,
  390. .pixel_type = FF_PIXEL_PACKED,
  391. .depth = 8,
  392. },
  393. };
  394. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  395. {
  396. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  397. *h_shift = desc->log2_chroma_w;
  398. *v_shift = desc->log2_chroma_h;
  399. }
  400. int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  401. {
  402. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  403. return desc->flags & PIX_FMT_HWACCEL;
  404. }
  405. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  406. enum AVPixelFormat pix_fmt, int width, int height)
  407. {
  408. int ret;
  409. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  410. return ret;
  411. if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
  412. return ret;
  413. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  414. }
  415. int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
  416. unsigned char *dest, int dest_size)
  417. {
  418. int i, j, nb_planes = 0, linesizes[4];
  419. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  420. int size = avpicture_get_size(pix_fmt, width, height);
  421. if (size > dest_size || size < 0)
  422. return AVERROR(EINVAL);
  423. for (i = 0; i < desc->nb_components; i++)
  424. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  425. nb_planes++;
  426. av_image_fill_linesizes(linesizes, pix_fmt, width);
  427. for (i = 0; i < nb_planes; i++) {
  428. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  429. const unsigned char *s = src->data[i];
  430. h = (height + (1 << shift) - 1) >> shift;
  431. for (j = 0; j < h; j++) {
  432. memcpy(dest, s, linesizes[i]);
  433. dest += linesizes[i];
  434. s += src->linesize[i];
  435. }
  436. }
  437. if (desc->flags & PIX_FMT_PAL)
  438. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  439. return size;
  440. }
  441. int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
  442. {
  443. AVPicture dummy_pict;
  444. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  445. if(av_image_check_size(width, height, 0, NULL))
  446. return -1;
  447. if (desc->flags & PIX_FMT_PSEUDOPAL)
  448. // do not include palette for these pseudo-paletted formats
  449. return width * height;
  450. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  451. }
  452. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
  453. int has_alpha)
  454. {
  455. const PixFmtInfo *pf, *ps;
  456. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  457. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  458. int loss;
  459. ps = &pix_fmt_info[src_pix_fmt];
  460. /* compute loss */
  461. loss = 0;
  462. pf = &pix_fmt_info[dst_pix_fmt];
  463. if (pf->depth < ps->depth ||
  464. ((dst_pix_fmt == AV_PIX_FMT_RGB555BE || dst_pix_fmt == AV_PIX_FMT_RGB555LE ||
  465. dst_pix_fmt == AV_PIX_FMT_BGR555BE || dst_pix_fmt == AV_PIX_FMT_BGR555LE) &&
  466. (src_pix_fmt == AV_PIX_FMT_RGB565BE || src_pix_fmt == AV_PIX_FMT_RGB565LE ||
  467. src_pix_fmt == AV_PIX_FMT_BGR565BE || src_pix_fmt == AV_PIX_FMT_BGR565LE)))
  468. loss |= FF_LOSS_DEPTH;
  469. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  470. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  471. loss |= FF_LOSS_RESOLUTION;
  472. switch(pf->color_type) {
  473. case FF_COLOR_RGB:
  474. if (ps->color_type != FF_COLOR_RGB &&
  475. ps->color_type != FF_COLOR_GRAY)
  476. loss |= FF_LOSS_COLORSPACE;
  477. break;
  478. case FF_COLOR_GRAY:
  479. if (ps->color_type != FF_COLOR_GRAY)
  480. loss |= FF_LOSS_COLORSPACE;
  481. break;
  482. case FF_COLOR_YUV:
  483. if (ps->color_type != FF_COLOR_YUV)
  484. loss |= FF_LOSS_COLORSPACE;
  485. break;
  486. case FF_COLOR_YUV_JPEG:
  487. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  488. ps->color_type != FF_COLOR_YUV &&
  489. ps->color_type != FF_COLOR_GRAY)
  490. loss |= FF_LOSS_COLORSPACE;
  491. break;
  492. default:
  493. /* fail safe test */
  494. if (ps->color_type != pf->color_type)
  495. loss |= FF_LOSS_COLORSPACE;
  496. break;
  497. }
  498. if (pf->color_type == FF_COLOR_GRAY &&
  499. ps->color_type != FF_COLOR_GRAY)
  500. loss |= FF_LOSS_CHROMA;
  501. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  502. loss |= FF_LOSS_ALPHA;
  503. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  504. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  505. loss |= FF_LOSS_COLORQUANT;
  506. return loss;
  507. }
  508. static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
  509. {
  510. int bits;
  511. const PixFmtInfo *pf;
  512. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  513. pf = &pix_fmt_info[pix_fmt];
  514. switch(pf->pixel_type) {
  515. case FF_PIXEL_PACKED:
  516. switch(pix_fmt) {
  517. case AV_PIX_FMT_YUYV422:
  518. case AV_PIX_FMT_UYVY422:
  519. case AV_PIX_FMT_RGB565BE:
  520. case AV_PIX_FMT_RGB565LE:
  521. case AV_PIX_FMT_RGB555BE:
  522. case AV_PIX_FMT_RGB555LE:
  523. case AV_PIX_FMT_RGB444BE:
  524. case AV_PIX_FMT_RGB444LE:
  525. case AV_PIX_FMT_BGR565BE:
  526. case AV_PIX_FMT_BGR565LE:
  527. case AV_PIX_FMT_BGR555BE:
  528. case AV_PIX_FMT_BGR555LE:
  529. case AV_PIX_FMT_BGR444BE:
  530. case AV_PIX_FMT_BGR444LE:
  531. bits = 16;
  532. break;
  533. case AV_PIX_FMT_UYYVYY411:
  534. bits = 12;
  535. break;
  536. default:
  537. bits = pf->depth * pf->nb_channels;
  538. break;
  539. }
  540. break;
  541. case FF_PIXEL_PLANAR:
  542. if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  543. bits = pf->depth * pf->nb_channels;
  544. } else {
  545. bits = pf->depth + ((2 * pf->depth) >>
  546. (desc->log2_chroma_w + desc->log2_chroma_h));
  547. }
  548. break;
  549. case FF_PIXEL_PALETTE:
  550. bits = 8;
  551. break;
  552. default:
  553. bits = -1;
  554. break;
  555. }
  556. return bits;
  557. }
  558. static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
  559. enum AVPixelFormat src_pix_fmt,
  560. int has_alpha,
  561. int loss_mask)
  562. {
  563. int dist, i, loss, min_dist;
  564. enum AVPixelFormat dst_pix_fmt;
  565. /* find exact color match with smallest size */
  566. dst_pix_fmt = AV_PIX_FMT_NONE;
  567. min_dist = 0x7fffffff;
  568. i = 0;
  569. while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
  570. enum AVPixelFormat pix_fmt = pix_fmt_list[i];
  571. if (i > AV_PIX_FMT_NB) {
  572. av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
  573. "it is either not properly terminated or contains duplicates\n");
  574. return AV_PIX_FMT_NONE;
  575. }
  576. loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
  577. if (loss == 0) {
  578. dist = avg_bits_per_pixel(pix_fmt);
  579. if (dist < min_dist) {
  580. min_dist = dist;
  581. dst_pix_fmt = pix_fmt;
  582. }
  583. }
  584. i++;
  585. }
  586. return dst_pix_fmt;
  587. }
  588. #if FF_API_FIND_BEST_PIX_FMT
  589. enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
  590. int has_alpha, int *loss_ptr)
  591. {
  592. enum AVPixelFormat list[64];
  593. int i, j = 0;
  594. // test only the first 64 pixel formats to avoid undefined behaviour
  595. for (i = 0; i < 64; i++) {
  596. if (pix_fmt_mask & (1ULL << i))
  597. list[j++] = i;
  598. }
  599. list[j] = AV_PIX_FMT_NONE;
  600. return avcodec_find_best_pix_fmt2(list, src_pix_fmt, has_alpha, loss_ptr);
  601. }
  602. #endif /* FF_API_FIND_BEST_PIX_FMT */
  603. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  604. enum AVPixelFormat src_pix_fmt,
  605. int has_alpha, int *loss_ptr)
  606. {
  607. enum AVPixelFormat dst_pix_fmt;
  608. int loss_mask, i;
  609. static const int loss_mask_order[] = {
  610. ~0, /* no loss first */
  611. ~FF_LOSS_ALPHA,
  612. ~FF_LOSS_RESOLUTION,
  613. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  614. ~FF_LOSS_COLORQUANT,
  615. ~FF_LOSS_DEPTH,
  616. 0,
  617. };
  618. /* try with successive loss */
  619. i = 0;
  620. for(;;) {
  621. loss_mask = loss_mask_order[i++];
  622. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
  623. has_alpha, loss_mask);
  624. if (dst_pix_fmt >= 0)
  625. goto found;
  626. if (loss_mask == 0)
  627. break;
  628. }
  629. return AV_PIX_FMT_NONE;
  630. found:
  631. if (loss_ptr)
  632. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  633. return dst_pix_fmt;
  634. }
  635. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  636. enum AVPixelFormat pix_fmt, int width, int height)
  637. {
  638. av_image_copy(dst->data, dst->linesize, src->data,
  639. src->linesize, pix_fmt, width, height);
  640. }
  641. /* 2x2 -> 1x1 */
  642. void ff_shrink22(uint8_t *dst, int dst_wrap,
  643. const uint8_t *src, int src_wrap,
  644. int width, int height)
  645. {
  646. int w;
  647. const uint8_t *s1, *s2;
  648. uint8_t *d;
  649. for(;height > 0; height--) {
  650. s1 = src;
  651. s2 = s1 + src_wrap;
  652. d = dst;
  653. for(w = width;w >= 4; w-=4) {
  654. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  655. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  656. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  657. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  658. s1 += 8;
  659. s2 += 8;
  660. d += 4;
  661. }
  662. for(;w > 0; w--) {
  663. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  664. s1 += 2;
  665. s2 += 2;
  666. d++;
  667. }
  668. src += 2 * src_wrap;
  669. dst += dst_wrap;
  670. }
  671. }
  672. /* 4x4 -> 1x1 */
  673. void ff_shrink44(uint8_t *dst, int dst_wrap,
  674. const uint8_t *src, int src_wrap,
  675. int width, int height)
  676. {
  677. int w;
  678. const uint8_t *s1, *s2, *s3, *s4;
  679. uint8_t *d;
  680. for(;height > 0; height--) {
  681. s1 = src;
  682. s2 = s1 + src_wrap;
  683. s3 = s2 + src_wrap;
  684. s4 = s3 + src_wrap;
  685. d = dst;
  686. for(w = width;w > 0; w--) {
  687. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  688. s2[0] + s2[1] + s2[2] + s2[3] +
  689. s3[0] + s3[1] + s3[2] + s3[3] +
  690. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  691. s1 += 4;
  692. s2 += 4;
  693. s3 += 4;
  694. s4 += 4;
  695. d++;
  696. }
  697. src += 4 * src_wrap;
  698. dst += dst_wrap;
  699. }
  700. }
  701. /* 8x8 -> 1x1 */
  702. void ff_shrink88(uint8_t *dst, int dst_wrap,
  703. const uint8_t *src, int src_wrap,
  704. int width, int height)
  705. {
  706. int w, i;
  707. for(;height > 0; height--) {
  708. for(w = width;w > 0; w--) {
  709. int tmp=0;
  710. for(i=0; i<8; i++){
  711. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  712. src += src_wrap;
  713. }
  714. *(dst++) = (tmp + 32)>>6;
  715. src += 8 - 8*src_wrap;
  716. }
  717. src += 8*src_wrap - 8*width;
  718. dst += dst_wrap - width;
  719. }
  720. }
  721. int avpicture_alloc(AVPicture *picture,
  722. enum AVPixelFormat pix_fmt, int width, int height)
  723. {
  724. int ret;
  725. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  726. memset(picture, 0, sizeof(AVPicture));
  727. return ret;
  728. }
  729. return 0;
  730. }
  731. void avpicture_free(AVPicture *picture)
  732. {
  733. av_free(picture->data[0]);
  734. }
  735. /* return true if yuv planar */
  736. static inline int is_yuv_planar(const PixFmtInfo *ps)
  737. {
  738. return (ps->color_type == FF_COLOR_YUV ||
  739. ps->color_type == FF_COLOR_YUV_JPEG) &&
  740. ps->pixel_type == FF_PIXEL_PLANAR;
  741. }
  742. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  743. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  744. {
  745. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  746. int y_shift;
  747. int x_shift;
  748. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  749. return -1;
  750. y_shift = desc->log2_chroma_h;
  751. x_shift = desc->log2_chroma_w;
  752. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  753. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  754. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  755. dst->linesize[0] = src->linesize[0];
  756. dst->linesize[1] = src->linesize[1];
  757. dst->linesize[2] = src->linesize[2];
  758. return 0;
  759. }
  760. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  761. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  762. int *color)
  763. {
  764. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  765. uint8_t *optr;
  766. int y_shift;
  767. int x_shift;
  768. int yheight;
  769. int i, y;
  770. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  771. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  772. for (i = 0; i < 3; i++) {
  773. x_shift = i ? desc->log2_chroma_w : 0;
  774. y_shift = i ? desc->log2_chroma_h : 0;
  775. if (padtop || padleft) {
  776. memset(dst->data[i], color[i],
  777. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  778. }
  779. if (padleft || padright) {
  780. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  781. (dst->linesize[i] - (padright >> x_shift));
  782. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  783. for (y = 0; y < yheight; y++) {
  784. memset(optr, color[i], (padleft + padright) >> x_shift);
  785. optr += dst->linesize[i];
  786. }
  787. }
  788. if (src) { /* first line */
  789. uint8_t *iptr = src->data[i];
  790. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  791. (padleft >> x_shift);
  792. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  793. iptr += src->linesize[i];
  794. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  795. (dst->linesize[i] - (padright >> x_shift));
  796. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  797. for (y = 0; y < yheight; y++) {
  798. memset(optr, color[i], (padleft + padright) >> x_shift);
  799. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  800. (width - padleft - padright) >> x_shift);
  801. iptr += src->linesize[i];
  802. optr += dst->linesize[i];
  803. }
  804. }
  805. if (padbottom || padright) {
  806. optr = dst->data[i] + dst->linesize[i] *
  807. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  808. memset(optr, color[i],dst->linesize[i] *
  809. (padbottom >> y_shift) + (padright >> x_shift));
  810. }
  811. }
  812. return 0;
  813. }
  814. #if !HAVE_MMX_EXTERNAL
  815. /* filter parameters: [-1 4 2 4 -1] // 8 */
  816. static void deinterlace_line_c(uint8_t *dst,
  817. const uint8_t *lum_m4, const uint8_t *lum_m3,
  818. const uint8_t *lum_m2, const uint8_t *lum_m1,
  819. const uint8_t *lum,
  820. int size)
  821. {
  822. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  823. int sum;
  824. for(;size > 0;size--) {
  825. sum = -lum_m4[0];
  826. sum += lum_m3[0] << 2;
  827. sum += lum_m2[0] << 1;
  828. sum += lum_m1[0] << 2;
  829. sum += -lum[0];
  830. dst[0] = cm[(sum + 4) >> 3];
  831. lum_m4++;
  832. lum_m3++;
  833. lum_m2++;
  834. lum_m1++;
  835. lum++;
  836. dst++;
  837. }
  838. }
  839. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  840. uint8_t *lum_m2, uint8_t *lum_m1,
  841. uint8_t *lum, int size)
  842. {
  843. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  844. int sum;
  845. for(;size > 0;size--) {
  846. sum = -lum_m4[0];
  847. sum += lum_m3[0] << 2;
  848. sum += lum_m2[0] << 1;
  849. lum_m4[0]=lum_m2[0];
  850. sum += lum_m1[0] << 2;
  851. sum += -lum[0];
  852. lum_m2[0] = cm[(sum + 4) >> 3];
  853. lum_m4++;
  854. lum_m3++;
  855. lum_m2++;
  856. lum_m1++;
  857. lum++;
  858. }
  859. }
  860. #endif /* !HAVE_MMX_EXTERNAL */
  861. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  862. top field is copied as is, but the bottom field is deinterlaced
  863. against the top field. */
  864. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  865. const uint8_t *src1, int src_wrap,
  866. int width, int height)
  867. {
  868. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  869. int y;
  870. src_m2 = src1;
  871. src_m1 = src1;
  872. src_0=&src_m1[src_wrap];
  873. src_p1=&src_0[src_wrap];
  874. src_p2=&src_p1[src_wrap];
  875. for(y=0;y<(height-2);y+=2) {
  876. memcpy(dst,src_m1,width);
  877. dst += dst_wrap;
  878. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  879. src_m2 = src_0;
  880. src_m1 = src_p1;
  881. src_0 = src_p2;
  882. src_p1 += 2*src_wrap;
  883. src_p2 += 2*src_wrap;
  884. dst += dst_wrap;
  885. }
  886. memcpy(dst,src_m1,width);
  887. dst += dst_wrap;
  888. /* do last line */
  889. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  890. }
  891. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  892. int width, int height)
  893. {
  894. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  895. int y;
  896. uint8_t *buf;
  897. buf = av_malloc(width);
  898. src_m1 = src1;
  899. memcpy(buf,src_m1,width);
  900. src_0=&src_m1[src_wrap];
  901. src_p1=&src_0[src_wrap];
  902. src_p2=&src_p1[src_wrap];
  903. for(y=0;y<(height-2);y+=2) {
  904. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  905. src_m1 = src_p1;
  906. src_0 = src_p2;
  907. src_p1 += 2*src_wrap;
  908. src_p2 += 2*src_wrap;
  909. }
  910. /* do last line */
  911. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  912. av_free(buf);
  913. }
  914. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  915. enum AVPixelFormat pix_fmt, int width, int height)
  916. {
  917. int i;
  918. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  919. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  920. pix_fmt != AV_PIX_FMT_YUV422P &&
  921. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  922. pix_fmt != AV_PIX_FMT_YUV444P &&
  923. pix_fmt != AV_PIX_FMT_YUV411P &&
  924. pix_fmt != AV_PIX_FMT_GRAY8)
  925. return -1;
  926. if ((width & 3) != 0 || (height & 3) != 0)
  927. return -1;
  928. for(i=0;i<3;i++) {
  929. if (i == 1) {
  930. switch(pix_fmt) {
  931. case AV_PIX_FMT_YUVJ420P:
  932. case AV_PIX_FMT_YUV420P:
  933. width >>= 1;
  934. height >>= 1;
  935. break;
  936. case AV_PIX_FMT_YUV422P:
  937. case AV_PIX_FMT_YUVJ422P:
  938. width >>= 1;
  939. break;
  940. case AV_PIX_FMT_YUV411P:
  941. width >>= 2;
  942. break;
  943. default:
  944. break;
  945. }
  946. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  947. break;
  948. }
  949. }
  950. if (src == dst) {
  951. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  952. width, height);
  953. } else {
  954. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  955. src->data[i], src->linesize[i],
  956. width, height);
  957. }
  958. }
  959. emms_c();
  960. return 0;
  961. }