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.

997 lines
29KB

  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
  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/avassert.h"
  34. #include "libavutil/colorspace.h"
  35. #include "libavutil/common.h"
  36. #include "libavutil/pixdesc.h"
  37. #include "libavutil/imgutils.h"
  38. #if HAVE_MMX_EXTERNAL
  39. #include "x86/dsputil_mmx.h"
  40. #endif
  41. #define FF_COLOR_RGB 0 /**< RGB color space */
  42. #define FF_COLOR_GRAY 1 /**< gray color space */
  43. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  44. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  45. #if HAVE_MMX_EXTERNAL
  46. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  47. #define deinterlace_line ff_deinterlace_line_mmx
  48. #else
  49. #define deinterlace_line_inplace deinterlace_line_inplace_c
  50. #define deinterlace_line deinterlace_line_c
  51. #endif
  52. #define pixdesc_has_alpha(pixdesc) \
  53. ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
  54. typedef struct PixFmtInfo {
  55. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  56. uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
  57. } PixFmtInfo;
  58. /* this table gives more information about formats */
  59. static const PixFmtInfo pix_fmt_info[AV_PIX_FMT_NB] = {
  60. /* YUV formats */
  61. [AV_PIX_FMT_YUV420P] = {
  62. .color_type = FF_COLOR_YUV,
  63. },
  64. [AV_PIX_FMT_YUV422P] = {
  65. .color_type = FF_COLOR_YUV,
  66. },
  67. [AV_PIX_FMT_YUV444P] = {
  68. .color_type = FF_COLOR_YUV,
  69. },
  70. [AV_PIX_FMT_YUYV422] = {
  71. .color_type = FF_COLOR_YUV,
  72. },
  73. [AV_PIX_FMT_UYVY422] = {
  74. .color_type = FF_COLOR_YUV,
  75. },
  76. [AV_PIX_FMT_YUV410P] = {
  77. .color_type = FF_COLOR_YUV,
  78. },
  79. [AV_PIX_FMT_YUV411P] = {
  80. .color_type = FF_COLOR_YUV,
  81. },
  82. [AV_PIX_FMT_YUV440P] = {
  83. .color_type = FF_COLOR_YUV,
  84. },
  85. [AV_PIX_FMT_YUV420P9LE] = {
  86. .color_type = FF_COLOR_YUV,
  87. },
  88. [AV_PIX_FMT_YUV422P9LE] = {
  89. .color_type = FF_COLOR_YUV,
  90. },
  91. [AV_PIX_FMT_YUV444P9LE] = {
  92. .color_type = FF_COLOR_YUV,
  93. },
  94. [AV_PIX_FMT_YUV420P9BE] = {
  95. .color_type = FF_COLOR_YUV,
  96. },
  97. [AV_PIX_FMT_YUV422P9BE] = {
  98. .color_type = FF_COLOR_YUV,
  99. },
  100. [AV_PIX_FMT_YUV444P9BE] = {
  101. .color_type = FF_COLOR_YUV,
  102. },
  103. [AV_PIX_FMT_YUV420P10LE] = {
  104. .color_type = FF_COLOR_YUV,
  105. },
  106. [AV_PIX_FMT_YUV422P10LE] = {
  107. .color_type = FF_COLOR_YUV,
  108. },
  109. [AV_PIX_FMT_YUV444P10LE] = {
  110. .color_type = FF_COLOR_YUV,
  111. },
  112. [AV_PIX_FMT_YUV420P10BE] = {
  113. .color_type = FF_COLOR_YUV,
  114. },
  115. [AV_PIX_FMT_YUV422P10BE] = {
  116. .color_type = FF_COLOR_YUV,
  117. },
  118. [AV_PIX_FMT_YUV444P10BE] = {
  119. .color_type = FF_COLOR_YUV,
  120. },
  121. [AV_PIX_FMT_YUV420P12LE] = {
  122. .color_type = FF_COLOR_YUV,
  123. },
  124. [AV_PIX_FMT_YUV422P12LE] = {
  125. .color_type = FF_COLOR_YUV,
  126. },
  127. [AV_PIX_FMT_YUV444P12LE] = {
  128. .color_type = FF_COLOR_YUV,
  129. },
  130. [AV_PIX_FMT_YUV420P12BE] = {
  131. .color_type = FF_COLOR_YUV,
  132. },
  133. [AV_PIX_FMT_YUV422P12BE] = {
  134. .color_type = FF_COLOR_YUV,
  135. },
  136. [AV_PIX_FMT_YUV444P12BE] = {
  137. .color_type = FF_COLOR_YUV,
  138. },
  139. [AV_PIX_FMT_YUV420P14LE] = {
  140. .color_type = FF_COLOR_YUV,
  141. },
  142. [AV_PIX_FMT_YUV422P14LE] = {
  143. .color_type = FF_COLOR_YUV,
  144. },
  145. [AV_PIX_FMT_YUV444P14LE] = {
  146. .color_type = FF_COLOR_YUV,
  147. },
  148. [AV_PIX_FMT_YUV420P14BE] = {
  149. .color_type = FF_COLOR_YUV,
  150. },
  151. [AV_PIX_FMT_YUV422P14BE] = {
  152. .color_type = FF_COLOR_YUV,
  153. },
  154. [AV_PIX_FMT_YUV444P14BE] = {
  155. .color_type = FF_COLOR_YUV,
  156. },
  157. [AV_PIX_FMT_YUV420P16LE] = {
  158. .color_type = FF_COLOR_YUV,
  159. },
  160. [AV_PIX_FMT_YUV422P16LE] = {
  161. .color_type = FF_COLOR_YUV,
  162. },
  163. [AV_PIX_FMT_YUV444P16LE] = {
  164. .color_type = FF_COLOR_YUV,
  165. },
  166. [AV_PIX_FMT_YUV420P16BE] = {
  167. .color_type = FF_COLOR_YUV,
  168. },
  169. [AV_PIX_FMT_YUV422P16BE] = {
  170. .color_type = FF_COLOR_YUV,
  171. },
  172. [AV_PIX_FMT_YUV444P16BE] = {
  173. .color_type = FF_COLOR_YUV,
  174. },
  175. /* YUV formats with alpha plane */
  176. [AV_PIX_FMT_YUVA420P] = {
  177. .color_type = FF_COLOR_YUV,
  178. },
  179. [AV_PIX_FMT_YUVA422P] = {
  180. .color_type = FF_COLOR_YUV,
  181. },
  182. [AV_PIX_FMT_YUVA444P] = {
  183. .color_type = FF_COLOR_YUV,
  184. },
  185. [AV_PIX_FMT_YUVA420P9LE] = {
  186. .color_type = FF_COLOR_YUV,
  187. },
  188. [AV_PIX_FMT_YUVA422P9LE] = {
  189. .color_type = FF_COLOR_YUV,
  190. },
  191. [AV_PIX_FMT_YUVA444P9LE] = {
  192. .color_type = FF_COLOR_YUV,
  193. },
  194. [AV_PIX_FMT_YUVA420P9BE] = {
  195. .color_type = FF_COLOR_YUV,
  196. },
  197. [AV_PIX_FMT_YUVA422P9BE] = {
  198. .color_type = FF_COLOR_YUV,
  199. },
  200. [AV_PIX_FMT_YUVA444P9BE] = {
  201. .color_type = FF_COLOR_YUV,
  202. },
  203. [AV_PIX_FMT_YUVA420P10LE] = {
  204. .color_type = FF_COLOR_YUV,
  205. },
  206. [AV_PIX_FMT_YUVA422P10LE] = {
  207. .color_type = FF_COLOR_YUV,
  208. },
  209. [AV_PIX_FMT_YUVA444P10LE] = {
  210. .color_type = FF_COLOR_YUV,
  211. },
  212. [AV_PIX_FMT_YUVA420P10BE] = {
  213. .color_type = FF_COLOR_YUV,
  214. },
  215. [AV_PIX_FMT_YUVA422P10BE] = {
  216. .color_type = FF_COLOR_YUV,
  217. },
  218. [AV_PIX_FMT_YUVA444P10BE] = {
  219. .color_type = FF_COLOR_YUV,
  220. },
  221. /* JPEG YUV */
  222. [AV_PIX_FMT_YUVJ420P] = {
  223. .color_type = FF_COLOR_YUV_JPEG,
  224. },
  225. [AV_PIX_FMT_YUVJ422P] = {
  226. .color_type = FF_COLOR_YUV_JPEG,
  227. },
  228. [AV_PIX_FMT_YUVJ444P] = {
  229. .color_type = FF_COLOR_YUV_JPEG,
  230. },
  231. [AV_PIX_FMT_YUVJ440P] = {
  232. .color_type = FF_COLOR_YUV_JPEG,
  233. },
  234. /* RGB formats */
  235. [AV_PIX_FMT_RGB24] = {
  236. .color_type = FF_COLOR_RGB,
  237. },
  238. [AV_PIX_FMT_BGR24] = {
  239. .color_type = FF_COLOR_RGB,
  240. },
  241. [AV_PIX_FMT_ARGB] = {
  242. .color_type = FF_COLOR_RGB,
  243. },
  244. [AV_PIX_FMT_RGB48BE] = {
  245. .color_type = FF_COLOR_RGB,
  246. },
  247. [AV_PIX_FMT_RGB48LE] = {
  248. .color_type = FF_COLOR_RGB,
  249. },
  250. [AV_PIX_FMT_RGBA64BE] = {
  251. .color_type = FF_COLOR_RGB,
  252. },
  253. [AV_PIX_FMT_RGBA64LE] = {
  254. .color_type = FF_COLOR_RGB,
  255. },
  256. [AV_PIX_FMT_RGB565BE] = {
  257. .color_type = FF_COLOR_RGB,
  258. },
  259. [AV_PIX_FMT_RGB565LE] = {
  260. .color_type = FF_COLOR_RGB,
  261. },
  262. [AV_PIX_FMT_RGB555BE] = {
  263. .color_type = FF_COLOR_RGB,
  264. .padded_size = 16,
  265. },
  266. [AV_PIX_FMT_RGB555LE] = {
  267. .color_type = FF_COLOR_RGB,
  268. .padded_size = 16,
  269. },
  270. [AV_PIX_FMT_RGB444BE] = {
  271. .color_type = FF_COLOR_RGB,
  272. .padded_size = 16,
  273. },
  274. [AV_PIX_FMT_RGB444LE] = {
  275. .color_type = FF_COLOR_RGB,
  276. .padded_size = 16,
  277. },
  278. /* gray / mono formats */
  279. [AV_PIX_FMT_GRAY16BE] = {
  280. .color_type = FF_COLOR_GRAY,
  281. },
  282. [AV_PIX_FMT_GRAY16LE] = {
  283. .color_type = FF_COLOR_GRAY,
  284. },
  285. [AV_PIX_FMT_GRAY8] = {
  286. .color_type = FF_COLOR_GRAY,
  287. },
  288. [AV_PIX_FMT_GRAY8A] = {
  289. .color_type = FF_COLOR_GRAY,
  290. },
  291. [AV_PIX_FMT_MONOWHITE] = {
  292. .color_type = FF_COLOR_GRAY,
  293. },
  294. [AV_PIX_FMT_MONOBLACK] = {
  295. .color_type = FF_COLOR_GRAY,
  296. },
  297. /* paletted formats */
  298. [AV_PIX_FMT_PAL8] = {
  299. .color_type = FF_COLOR_RGB,
  300. },
  301. [AV_PIX_FMT_UYYVYY411] = {
  302. .color_type = FF_COLOR_YUV,
  303. },
  304. [AV_PIX_FMT_ABGR] = {
  305. .color_type = FF_COLOR_RGB,
  306. },
  307. [AV_PIX_FMT_BGR48BE] = {
  308. .color_type = FF_COLOR_RGB,
  309. },
  310. [AV_PIX_FMT_BGR48LE] = {
  311. .color_type = FF_COLOR_RGB,
  312. },
  313. [AV_PIX_FMT_BGRA64BE] = {
  314. .color_type = FF_COLOR_RGB,
  315. },
  316. [AV_PIX_FMT_BGRA64LE] = {
  317. .color_type = FF_COLOR_RGB,
  318. },
  319. [AV_PIX_FMT_BGR565BE] = {
  320. .color_type = FF_COLOR_RGB,
  321. .padded_size = 16,
  322. },
  323. [AV_PIX_FMT_BGR565LE] = {
  324. .color_type = FF_COLOR_RGB,
  325. .padded_size = 16,
  326. },
  327. [AV_PIX_FMT_BGR555BE] = {
  328. .color_type = FF_COLOR_RGB,
  329. .padded_size = 16,
  330. },
  331. [AV_PIX_FMT_BGR555LE] = {
  332. .color_type = FF_COLOR_RGB,
  333. .padded_size = 16,
  334. },
  335. [AV_PIX_FMT_BGR444BE] = {
  336. .color_type = FF_COLOR_RGB,
  337. .padded_size = 16,
  338. },
  339. [AV_PIX_FMT_BGR444LE] = {
  340. .color_type = FF_COLOR_RGB,
  341. .padded_size = 16,
  342. },
  343. [AV_PIX_FMT_RGB8] = {
  344. .color_type = FF_COLOR_RGB,
  345. },
  346. [AV_PIX_FMT_RGB4] = {
  347. .color_type = FF_COLOR_RGB,
  348. },
  349. [AV_PIX_FMT_RGB4_BYTE] = {
  350. .color_type = FF_COLOR_RGB,
  351. .padded_size = 8,
  352. },
  353. [AV_PIX_FMT_BGR8] = {
  354. .color_type = FF_COLOR_RGB,
  355. },
  356. [AV_PIX_FMT_BGR4] = {
  357. .color_type = FF_COLOR_RGB,
  358. },
  359. [AV_PIX_FMT_BGR4_BYTE] = {
  360. .color_type = FF_COLOR_RGB,
  361. .padded_size = 8,
  362. },
  363. [AV_PIX_FMT_NV12] = {
  364. .color_type = FF_COLOR_YUV,
  365. },
  366. [AV_PIX_FMT_NV21] = {
  367. .color_type = FF_COLOR_YUV,
  368. },
  369. [AV_PIX_FMT_BGRA] = {
  370. .color_type = FF_COLOR_RGB,
  371. },
  372. [AV_PIX_FMT_RGBA] = {
  373. .color_type = FF_COLOR_RGB,
  374. },
  375. [AV_PIX_FMT_GBRP] = {
  376. .color_type = FF_COLOR_RGB,
  377. },
  378. [AV_PIX_FMT_GBRP9BE] = {
  379. .color_type = FF_COLOR_RGB,
  380. },
  381. [AV_PIX_FMT_GBRP9LE] = {
  382. .color_type = FF_COLOR_RGB,
  383. },
  384. [AV_PIX_FMT_GBRP10BE] = {
  385. .color_type = FF_COLOR_RGB,
  386. },
  387. [AV_PIX_FMT_GBRP10LE] = {
  388. .color_type = FF_COLOR_RGB,
  389. },
  390. [AV_PIX_FMT_GBRP12BE] = {
  391. .color_type = FF_COLOR_RGB,
  392. },
  393. [AV_PIX_FMT_GBRP12LE] = {
  394. .color_type = FF_COLOR_RGB,
  395. },
  396. [AV_PIX_FMT_GBRP14BE] = {
  397. .color_type = FF_COLOR_RGB,
  398. },
  399. [AV_PIX_FMT_GBRP14LE] = {
  400. .color_type = FF_COLOR_RGB,
  401. },
  402. [AV_PIX_FMT_GBRP16BE] = {
  403. .color_type = FF_COLOR_RGB,
  404. },
  405. [AV_PIX_FMT_GBRP16LE] = {
  406. .color_type = FF_COLOR_RGB,
  407. },
  408. };
  409. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  410. {
  411. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  412. av_assert0(desc);
  413. *h_shift = desc->log2_chroma_w;
  414. *v_shift = desc->log2_chroma_h;
  415. }
  416. static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
  417. {
  418. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  419. int i;
  420. if (!desc || !desc->nb_components) {
  421. *min = *max = 0;
  422. return AVERROR(EINVAL);
  423. }
  424. *min = INT_MAX, *max = -INT_MAX;
  425. for (i = 0; i < desc->nb_components; i++) {
  426. *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
  427. *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
  428. }
  429. return 0;
  430. }
  431. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
  432. int has_alpha)
  433. {
  434. const PixFmtInfo *pf, *ps;
  435. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  436. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  437. int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
  438. int ret, loss;
  439. if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
  440. return ~0;
  441. ps = &pix_fmt_info[src_pix_fmt];
  442. /* compute loss */
  443. loss = 0;
  444. if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
  445. return ret;
  446. if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
  447. return ret;
  448. if (dst_min_depth < src_min_depth ||
  449. dst_max_depth < src_max_depth)
  450. loss |= FF_LOSS_DEPTH;
  451. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  452. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  453. loss |= FF_LOSS_RESOLUTION;
  454. pf = &pix_fmt_info[dst_pix_fmt];
  455. switch(pf->color_type) {
  456. case FF_COLOR_RGB:
  457. if (ps->color_type != FF_COLOR_RGB &&
  458. ps->color_type != FF_COLOR_GRAY)
  459. loss |= FF_LOSS_COLORSPACE;
  460. break;
  461. case FF_COLOR_GRAY:
  462. if (ps->color_type != FF_COLOR_GRAY)
  463. loss |= FF_LOSS_COLORSPACE;
  464. break;
  465. case FF_COLOR_YUV:
  466. if (ps->color_type != FF_COLOR_YUV)
  467. loss |= FF_LOSS_COLORSPACE;
  468. break;
  469. case FF_COLOR_YUV_JPEG:
  470. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  471. ps->color_type != FF_COLOR_YUV &&
  472. ps->color_type != FF_COLOR_GRAY)
  473. loss |= FF_LOSS_COLORSPACE;
  474. break;
  475. default:
  476. /* fail safe test */
  477. if (ps->color_type != pf->color_type)
  478. loss |= FF_LOSS_COLORSPACE;
  479. break;
  480. }
  481. if (pf->color_type == FF_COLOR_GRAY &&
  482. ps->color_type != FF_COLOR_GRAY)
  483. loss |= FF_LOSS_CHROMA;
  484. if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
  485. loss |= FF_LOSS_ALPHA;
  486. if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
  487. (src_pix_fmt != AV_PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
  488. loss |= FF_LOSS_COLORQUANT;
  489. return loss;
  490. }
  491. static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
  492. {
  493. const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
  494. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  495. return info->padded_size ?
  496. info->padded_size : av_get_bits_per_pixel(desc);
  497. }
  498. #if FF_API_FIND_BEST_PIX_FMT
  499. enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
  500. int has_alpha, int *loss_ptr)
  501. {
  502. enum AVPixelFormat dst_pix_fmt;
  503. int i;
  504. if (loss_ptr) /* all losses count (for backward compatibility) */
  505. *loss_ptr = 0;
  506. dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
  507. for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
  508. if (pix_fmt_mask & (1ULL << i))
  509. dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
  510. }
  511. return dst_pix_fmt;
  512. }
  513. #endif /* FF_API_FIND_BEST_PIX_FMT */
  514. enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  515. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  516. {
  517. enum AVPixelFormat dst_pix_fmt;
  518. int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
  519. const AVPixFmtDescriptor *desc1 = av_pix_fmt_desc_get(dst_pix_fmt1);
  520. const AVPixFmtDescriptor *desc2 = av_pix_fmt_desc_get(dst_pix_fmt2);
  521. static const int loss_mask_order[] = {
  522. ~0, /* no loss first */
  523. ~FF_LOSS_ALPHA,
  524. ~FF_LOSS_RESOLUTION,
  525. ~FF_LOSS_COLORSPACE,
  526. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  527. ~FF_LOSS_COLORQUANT,
  528. ~FF_LOSS_DEPTH,
  529. ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
  530. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  531. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  532. 0x80000, //non zero entry that combines all loss variants including future additions
  533. 0,
  534. };
  535. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  536. dst_pix_fmt = AV_PIX_FMT_NONE;
  537. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  538. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  539. /* try with successive loss */
  540. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
  541. loss_order1 = loss1 & loss_mask_order[i];
  542. loss_order2 = loss2 & loss_mask_order[i];
  543. if (loss_order1 == 0 && loss_order2 == 0 && dst_pix_fmt2 != AV_PIX_FMT_NONE && dst_pix_fmt1 != AV_PIX_FMT_NONE){ /* use format with smallest depth */
  544. if(avg_bits_per_pixel(dst_pix_fmt2) != avg_bits_per_pixel(dst_pix_fmt1)) {
  545. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  546. } else {
  547. dst_pix_fmt = desc2->nb_components < desc1->nb_components ? dst_pix_fmt2 : dst_pix_fmt1;
  548. }
  549. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  550. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  551. }
  552. }
  553. if (loss_ptr)
  554. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  555. return dst_pix_fmt;
  556. }
  557. #if AV_HAVE_INCOMPATIBLE_FORK_ABI
  558. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  559. enum AVPixelFormat src_pix_fmt,
  560. int has_alpha, int *loss_ptr){
  561. return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
  562. }
  563. #else
  564. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  565. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  566. {
  567. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  568. }
  569. #endif
  570. enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
  571. enum AVPixelFormat src_pix_fmt,
  572. int has_alpha, int *loss_ptr){
  573. int i;
  574. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  575. for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
  576. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
  577. return best;
  578. }
  579. /* 2x2 -> 1x1 */
  580. void ff_shrink22(uint8_t *dst, int dst_wrap,
  581. const uint8_t *src, int src_wrap,
  582. int width, int height)
  583. {
  584. int w;
  585. const uint8_t *s1, *s2;
  586. uint8_t *d;
  587. for(;height > 0; height--) {
  588. s1 = src;
  589. s2 = s1 + src_wrap;
  590. d = dst;
  591. for(w = width;w >= 4; w-=4) {
  592. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  593. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  594. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  595. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  596. s1 += 8;
  597. s2 += 8;
  598. d += 4;
  599. }
  600. for(;w > 0; w--) {
  601. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  602. s1 += 2;
  603. s2 += 2;
  604. d++;
  605. }
  606. src += 2 * src_wrap;
  607. dst += dst_wrap;
  608. }
  609. }
  610. /* 4x4 -> 1x1 */
  611. void ff_shrink44(uint8_t *dst, int dst_wrap,
  612. const uint8_t *src, int src_wrap,
  613. int width, int height)
  614. {
  615. int w;
  616. const uint8_t *s1, *s2, *s3, *s4;
  617. uint8_t *d;
  618. for(;height > 0; height--) {
  619. s1 = src;
  620. s2 = s1 + src_wrap;
  621. s3 = s2 + src_wrap;
  622. s4 = s3 + src_wrap;
  623. d = dst;
  624. for(w = width;w > 0; w--) {
  625. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  626. s2[0] + s2[1] + s2[2] + s2[3] +
  627. s3[0] + s3[1] + s3[2] + s3[3] +
  628. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  629. s1 += 4;
  630. s2 += 4;
  631. s3 += 4;
  632. s4 += 4;
  633. d++;
  634. }
  635. src += 4 * src_wrap;
  636. dst += dst_wrap;
  637. }
  638. }
  639. /* 8x8 -> 1x1 */
  640. void ff_shrink88(uint8_t *dst, int dst_wrap,
  641. const uint8_t *src, int src_wrap,
  642. int width, int height)
  643. {
  644. int w, i;
  645. for(;height > 0; height--) {
  646. for(w = width;w > 0; w--) {
  647. int tmp=0;
  648. for(i=0; i<8; i++){
  649. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  650. src += src_wrap;
  651. }
  652. *(dst++) = (tmp + 32)>>6;
  653. src += 8 - 8*src_wrap;
  654. }
  655. src += 8*src_wrap - 8*width;
  656. dst += dst_wrap - width;
  657. }
  658. }
  659. /* return true if yuv planar */
  660. static inline int is_yuv_planar(enum AVPixelFormat fmt)
  661. {
  662. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  663. int i;
  664. int planes[4] = { 0 };
  665. if ( desc->flags & PIX_FMT_RGB
  666. || !(desc->flags & PIX_FMT_PLANAR))
  667. return 0;
  668. /* set the used planes */
  669. for (i = 0; i < desc->nb_components; i++)
  670. planes[desc->comp[i].plane] = 1;
  671. /* if there is an unused plane, the format is not planar */
  672. for (i = 0; i < desc->nb_components; i++)
  673. if (!planes[i])
  674. return 0;
  675. return 1;
  676. }
  677. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  678. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  679. {
  680. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  681. int y_shift;
  682. int x_shift;
  683. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  684. return -1;
  685. y_shift = desc->log2_chroma_h;
  686. x_shift = desc->log2_chroma_w;
  687. if (is_yuv_planar(pix_fmt)) {
  688. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  689. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  690. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  691. } else{
  692. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  693. return -1;
  694. if(left_band) //FIXME add support for this too
  695. return -1;
  696. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  697. }
  698. dst->linesize[0] = src->linesize[0];
  699. dst->linesize[1] = src->linesize[1];
  700. dst->linesize[2] = src->linesize[2];
  701. return 0;
  702. }
  703. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  704. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  705. int *color)
  706. {
  707. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  708. uint8_t *optr;
  709. int y_shift;
  710. int x_shift;
  711. int yheight;
  712. int i, y;
  713. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  714. !is_yuv_planar(pix_fmt)) return -1;
  715. for (i = 0; i < 3; i++) {
  716. x_shift = i ? desc->log2_chroma_w : 0;
  717. y_shift = i ? desc->log2_chroma_h : 0;
  718. if (padtop || padleft) {
  719. memset(dst->data[i], color[i],
  720. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  721. }
  722. if (padleft || padright) {
  723. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  724. (dst->linesize[i] - (padright >> x_shift));
  725. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  726. for (y = 0; y < yheight; y++) {
  727. memset(optr, color[i], (padleft + padright) >> x_shift);
  728. optr += dst->linesize[i];
  729. }
  730. }
  731. if (src) { /* first line */
  732. uint8_t *iptr = src->data[i];
  733. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  734. (padleft >> x_shift);
  735. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  736. iptr += src->linesize[i];
  737. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  738. (dst->linesize[i] - (padright >> x_shift));
  739. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  740. for (y = 0; y < yheight; y++) {
  741. memset(optr, color[i], (padleft + padright) >> x_shift);
  742. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  743. (width - padleft - padright) >> x_shift);
  744. iptr += src->linesize[i];
  745. optr += dst->linesize[i];
  746. }
  747. }
  748. if (padbottom || padright) {
  749. optr = dst->data[i] + dst->linesize[i] *
  750. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  751. memset(optr, color[i],dst->linesize[i] *
  752. (padbottom >> y_shift) + (padright >> x_shift));
  753. }
  754. }
  755. return 0;
  756. }
  757. #if !HAVE_MMX_EXTERNAL
  758. /* filter parameters: [-1 4 2 4 -1] // 8 */
  759. static void deinterlace_line_c(uint8_t *dst,
  760. const uint8_t *lum_m4, const uint8_t *lum_m3,
  761. const uint8_t *lum_m2, const uint8_t *lum_m1,
  762. const uint8_t *lum,
  763. int size)
  764. {
  765. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  766. int sum;
  767. for(;size > 0;size--) {
  768. sum = -lum_m4[0];
  769. sum += lum_m3[0] << 2;
  770. sum += lum_m2[0] << 1;
  771. sum += lum_m1[0] << 2;
  772. sum += -lum[0];
  773. dst[0] = cm[(sum + 4) >> 3];
  774. lum_m4++;
  775. lum_m3++;
  776. lum_m2++;
  777. lum_m1++;
  778. lum++;
  779. dst++;
  780. }
  781. }
  782. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  783. uint8_t *lum_m2, uint8_t *lum_m1,
  784. uint8_t *lum, int size)
  785. {
  786. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  787. int sum;
  788. for(;size > 0;size--) {
  789. sum = -lum_m4[0];
  790. sum += lum_m3[0] << 2;
  791. sum += lum_m2[0] << 1;
  792. lum_m4[0]=lum_m2[0];
  793. sum += lum_m1[0] << 2;
  794. sum += -lum[0];
  795. lum_m2[0] = cm[(sum + 4) >> 3];
  796. lum_m4++;
  797. lum_m3++;
  798. lum_m2++;
  799. lum_m1++;
  800. lum++;
  801. }
  802. }
  803. #endif /* !HAVE_MMX_EXTERNAL */
  804. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  805. top field is copied as is, but the bottom field is deinterlaced
  806. against the top field. */
  807. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  808. const uint8_t *src1, int src_wrap,
  809. int width, int height)
  810. {
  811. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  812. int y;
  813. src_m2 = src1;
  814. src_m1 = src1;
  815. src_0=&src_m1[src_wrap];
  816. src_p1=&src_0[src_wrap];
  817. src_p2=&src_p1[src_wrap];
  818. for(y=0;y<(height-2);y+=2) {
  819. memcpy(dst,src_m1,width);
  820. dst += dst_wrap;
  821. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  822. src_m2 = src_0;
  823. src_m1 = src_p1;
  824. src_0 = src_p2;
  825. src_p1 += 2*src_wrap;
  826. src_p2 += 2*src_wrap;
  827. dst += dst_wrap;
  828. }
  829. memcpy(dst,src_m1,width);
  830. dst += dst_wrap;
  831. /* do last line */
  832. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  833. }
  834. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  835. int width, int height)
  836. {
  837. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  838. int y;
  839. uint8_t *buf;
  840. buf = av_malloc(width);
  841. src_m1 = src1;
  842. memcpy(buf,src_m1,width);
  843. src_0=&src_m1[src_wrap];
  844. src_p1=&src_0[src_wrap];
  845. src_p2=&src_p1[src_wrap];
  846. for(y=0;y<(height-2);y+=2) {
  847. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  848. src_m1 = src_p1;
  849. src_0 = src_p2;
  850. src_p1 += 2*src_wrap;
  851. src_p2 += 2*src_wrap;
  852. }
  853. /* do last line */
  854. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  855. av_free(buf);
  856. }
  857. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  858. enum AVPixelFormat pix_fmt, int width, int height)
  859. {
  860. int i;
  861. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  862. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  863. pix_fmt != AV_PIX_FMT_YUV422P &&
  864. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  865. pix_fmt != AV_PIX_FMT_YUV444P &&
  866. pix_fmt != AV_PIX_FMT_YUV411P &&
  867. pix_fmt != AV_PIX_FMT_GRAY8)
  868. return -1;
  869. if ((width & 3) != 0 || (height & 3) != 0)
  870. return -1;
  871. for(i=0;i<3;i++) {
  872. if (i == 1) {
  873. switch(pix_fmt) {
  874. case AV_PIX_FMT_YUVJ420P:
  875. case AV_PIX_FMT_YUV420P:
  876. width >>= 1;
  877. height >>= 1;
  878. break;
  879. case AV_PIX_FMT_YUV422P:
  880. case AV_PIX_FMT_YUVJ422P:
  881. width >>= 1;
  882. break;
  883. case AV_PIX_FMT_YUV411P:
  884. width >>= 2;
  885. break;
  886. default:
  887. break;
  888. }
  889. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  890. break;
  891. }
  892. }
  893. if (src == dst) {
  894. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  895. width, height);
  896. } else {
  897. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  898. src->data[i], src->linesize[i],
  899. width, height);
  900. }
  901. }
  902. emms_c();
  903. return 0;
  904. }
  905. #ifdef TEST
  906. int main(void){
  907. int i;
  908. for (i=0; i<AV_PIX_FMT_NB*2; i++) {
  909. AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  910. if(!desc)
  911. continue;
  912. av_log(0, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d\n", desc->name, is_yuv_planar(i), avg_bits_per_pixel(i));
  913. }
  914. return 0;
  915. }
  916. #endif