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.

1027 lines
30KB

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