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.

1023 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/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. int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
  417. enum AVPixelFormat pix_fmt, int width, int height)
  418. {
  419. return av_image_fill_arrays(picture->data, picture->linesize,
  420. ptr, pix_fmt, width, height, 1);
  421. }
  422. int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
  423. unsigned char *dest, int dest_size)
  424. {
  425. return av_image_copy_to_buffer(dest, dest_size,
  426. (const uint8_t * const*)src->data, src->linesize,
  427. pix_fmt, width, height, 1);
  428. }
  429. int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
  430. {
  431. return av_image_get_buffer_size(pix_fmt, width, height, 1);
  432. }
  433. static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
  434. {
  435. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  436. int i;
  437. if (!desc || !desc->nb_components) {
  438. *min = *max = 0;
  439. return AVERROR(EINVAL);
  440. }
  441. *min = INT_MAX, *max = -INT_MAX;
  442. for (i = 0; i < desc->nb_components; i++) {
  443. *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
  444. *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
  445. }
  446. return 0;
  447. }
  448. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
  449. int has_alpha)
  450. {
  451. const PixFmtInfo *pf, *ps;
  452. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  453. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  454. int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
  455. int ret, loss;
  456. if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
  457. return ~0;
  458. ps = &pix_fmt_info[src_pix_fmt];
  459. /* compute loss */
  460. loss = 0;
  461. if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
  462. return ret;
  463. if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
  464. return ret;
  465. if (dst_min_depth < src_min_depth ||
  466. dst_max_depth < src_max_depth)
  467. loss |= FF_LOSS_DEPTH;
  468. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  469. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  470. loss |= FF_LOSS_RESOLUTION;
  471. pf = &pix_fmt_info[dst_pix_fmt];
  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 (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
  502. loss |= FF_LOSS_ALPHA;
  503. if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
  504. (src_pix_fmt != AV_PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
  505. loss |= FF_LOSS_COLORQUANT;
  506. return loss;
  507. }
  508. static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
  509. {
  510. const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
  511. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  512. return info->padded_size ?
  513. info->padded_size : av_get_bits_per_pixel(desc);
  514. }
  515. #if FF_API_FIND_BEST_PIX_FMT
  516. enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
  517. int has_alpha, int *loss_ptr)
  518. {
  519. enum AVPixelFormat dst_pix_fmt;
  520. int i;
  521. if (loss_ptr) /* all losses count (for backward compatibility) */
  522. *loss_ptr = 0;
  523. dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
  524. for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
  525. if (pix_fmt_mask & (1ULL << i))
  526. dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
  527. }
  528. return dst_pix_fmt;
  529. }
  530. #endif /* FF_API_FIND_BEST_PIX_FMT */
  531. enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  532. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  533. {
  534. enum AVPixelFormat dst_pix_fmt;
  535. int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
  536. static const int loss_mask_order[] = {
  537. ~0, /* no loss first */
  538. ~FF_LOSS_ALPHA,
  539. ~FF_LOSS_RESOLUTION,
  540. ~FF_LOSS_COLORSPACE,
  541. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  542. ~FF_LOSS_COLORQUANT,
  543. ~FF_LOSS_DEPTH,
  544. ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
  545. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  546. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  547. 0x80000, //non zero entry that combines all loss variants including future additions
  548. 0,
  549. };
  550. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  551. dst_pix_fmt = AV_PIX_FMT_NONE;
  552. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  553. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  554. /* try with successive loss */
  555. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
  556. loss_order1 = loss1 & loss_mask_order[i];
  557. loss_order2 = loss2 & loss_mask_order[i];
  558. 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 */
  559. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  560. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  561. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  562. }
  563. }
  564. if (loss_ptr)
  565. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  566. return dst_pix_fmt;
  567. }
  568. #if AV_HAVE_INCOMPATIBLE_FORK_ABI
  569. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  570. enum AVPixelFormat src_pix_fmt,
  571. int has_alpha, int *loss_ptr){
  572. return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
  573. }
  574. #else
  575. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  576. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  577. {
  578. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  579. }
  580. #endif
  581. enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
  582. enum AVPixelFormat src_pix_fmt,
  583. int has_alpha, int *loss_ptr){
  584. int i;
  585. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  586. for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
  587. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
  588. return best;
  589. }
  590. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  591. enum AVPixelFormat pix_fmt, int width, int height)
  592. {
  593. av_image_copy(dst->data, dst->linesize, (const uint8_t **)src->data,
  594. src->linesize, pix_fmt, width, height);
  595. }
  596. /* 2x2 -> 1x1 */
  597. void ff_shrink22(uint8_t *dst, int dst_wrap,
  598. const uint8_t *src, int src_wrap,
  599. int width, int height)
  600. {
  601. int w;
  602. const uint8_t *s1, *s2;
  603. uint8_t *d;
  604. for(;height > 0; height--) {
  605. s1 = src;
  606. s2 = s1 + src_wrap;
  607. d = dst;
  608. for(w = width;w >= 4; w-=4) {
  609. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  610. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  611. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  612. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  613. s1 += 8;
  614. s2 += 8;
  615. d += 4;
  616. }
  617. for(;w > 0; w--) {
  618. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  619. s1 += 2;
  620. s2 += 2;
  621. d++;
  622. }
  623. src += 2 * src_wrap;
  624. dst += dst_wrap;
  625. }
  626. }
  627. /* 4x4 -> 1x1 */
  628. void ff_shrink44(uint8_t *dst, int dst_wrap,
  629. const uint8_t *src, int src_wrap,
  630. int width, int height)
  631. {
  632. int w;
  633. const uint8_t *s1, *s2, *s3, *s4;
  634. uint8_t *d;
  635. for(;height > 0; height--) {
  636. s1 = src;
  637. s2 = s1 + src_wrap;
  638. s3 = s2 + src_wrap;
  639. s4 = s3 + src_wrap;
  640. d = dst;
  641. for(w = width;w > 0; w--) {
  642. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  643. s2[0] + s2[1] + s2[2] + s2[3] +
  644. s3[0] + s3[1] + s3[2] + s3[3] +
  645. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  646. s1 += 4;
  647. s2 += 4;
  648. s3 += 4;
  649. s4 += 4;
  650. d++;
  651. }
  652. src += 4 * src_wrap;
  653. dst += dst_wrap;
  654. }
  655. }
  656. /* 8x8 -> 1x1 */
  657. void ff_shrink88(uint8_t *dst, int dst_wrap,
  658. const uint8_t *src, int src_wrap,
  659. int width, int height)
  660. {
  661. int w, i;
  662. for(;height > 0; height--) {
  663. for(w = width;w > 0; w--) {
  664. int tmp=0;
  665. for(i=0; i<8; i++){
  666. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  667. src += src_wrap;
  668. }
  669. *(dst++) = (tmp + 32)>>6;
  670. src += 8 - 8*src_wrap;
  671. }
  672. src += 8*src_wrap - 8*width;
  673. dst += dst_wrap - width;
  674. }
  675. }
  676. int avpicture_alloc(AVPicture *picture,
  677. enum AVPixelFormat pix_fmt, int width, int height)
  678. {
  679. int ret;
  680. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  681. memset(picture, 0, sizeof(AVPicture));
  682. return ret;
  683. }
  684. return 0;
  685. }
  686. void avpicture_free(AVPicture *picture)
  687. {
  688. av_free(picture->data[0]);
  689. }
  690. /* return true if yuv planar */
  691. static inline int is_yuv_planar(enum AVPixelFormat fmt)
  692. {
  693. const PixFmtInfo *info = &pix_fmt_info[fmt];
  694. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
  695. int i;
  696. int planes[4] = { 0 };
  697. if (info->color_type != FF_COLOR_YUV &&
  698. info->color_type != FF_COLOR_YUV_JPEG)
  699. return 0;
  700. /* set the used planes */
  701. for (i = 0; i < desc->nb_components; i++)
  702. planes[desc->comp[i].plane] = 1;
  703. /* if there is an unused plane, the format is not planar */
  704. for (i = 0; i < desc->nb_components; i++)
  705. if (!planes[i])
  706. return 0;
  707. return 1;
  708. }
  709. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  710. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  711. {
  712. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  713. int y_shift;
  714. int x_shift;
  715. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  716. return -1;
  717. y_shift = desc->log2_chroma_h;
  718. x_shift = desc->log2_chroma_w;
  719. if (is_yuv_planar(pix_fmt)) {
  720. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  721. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  722. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  723. } else{
  724. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  725. return -1;
  726. if(left_band) //FIXME add support for this too
  727. return -1;
  728. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  729. }
  730. dst->linesize[0] = src->linesize[0];
  731. dst->linesize[1] = src->linesize[1];
  732. dst->linesize[2] = src->linesize[2];
  733. return 0;
  734. }
  735. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  736. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  737. int *color)
  738. {
  739. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  740. uint8_t *optr;
  741. int y_shift;
  742. int x_shift;
  743. int yheight;
  744. int i, y;
  745. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  746. !is_yuv_planar(pix_fmt)) return -1;
  747. for (i = 0; i < 3; i++) {
  748. x_shift = i ? desc->log2_chroma_w : 0;
  749. y_shift = i ? desc->log2_chroma_h : 0;
  750. if (padtop || padleft) {
  751. memset(dst->data[i], color[i],
  752. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  753. }
  754. if (padleft || padright) {
  755. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  756. (dst->linesize[i] - (padright >> x_shift));
  757. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  758. for (y = 0; y < yheight; y++) {
  759. memset(optr, color[i], (padleft + padright) >> x_shift);
  760. optr += dst->linesize[i];
  761. }
  762. }
  763. if (src) { /* first line */
  764. uint8_t *iptr = src->data[i];
  765. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  766. (padleft >> x_shift);
  767. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  768. iptr += src->linesize[i];
  769. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  770. (dst->linesize[i] - (padright >> x_shift));
  771. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  772. for (y = 0; y < yheight; y++) {
  773. memset(optr, color[i], (padleft + padright) >> x_shift);
  774. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  775. (width - padleft - padright) >> x_shift);
  776. iptr += src->linesize[i];
  777. optr += dst->linesize[i];
  778. }
  779. }
  780. if (padbottom || padright) {
  781. optr = dst->data[i] + dst->linesize[i] *
  782. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  783. memset(optr, color[i],dst->linesize[i] *
  784. (padbottom >> y_shift) + (padright >> x_shift));
  785. }
  786. }
  787. return 0;
  788. }
  789. #if !HAVE_MMX_EXTERNAL
  790. /* filter parameters: [-1 4 2 4 -1] // 8 */
  791. static void deinterlace_line_c(uint8_t *dst,
  792. const uint8_t *lum_m4, const uint8_t *lum_m3,
  793. const uint8_t *lum_m2, const uint8_t *lum_m1,
  794. const uint8_t *lum,
  795. int size)
  796. {
  797. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  798. int sum;
  799. for(;size > 0;size--) {
  800. sum = -lum_m4[0];
  801. sum += lum_m3[0] << 2;
  802. sum += lum_m2[0] << 1;
  803. sum += lum_m1[0] << 2;
  804. sum += -lum[0];
  805. dst[0] = cm[(sum + 4) >> 3];
  806. lum_m4++;
  807. lum_m3++;
  808. lum_m2++;
  809. lum_m1++;
  810. lum++;
  811. dst++;
  812. }
  813. }
  814. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  815. uint8_t *lum_m2, uint8_t *lum_m1,
  816. uint8_t *lum, int size)
  817. {
  818. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  819. int sum;
  820. for(;size > 0;size--) {
  821. sum = -lum_m4[0];
  822. sum += lum_m3[0] << 2;
  823. sum += lum_m2[0] << 1;
  824. lum_m4[0]=lum_m2[0];
  825. sum += lum_m1[0] << 2;
  826. sum += -lum[0];
  827. lum_m2[0] = cm[(sum + 4) >> 3];
  828. lum_m4++;
  829. lum_m3++;
  830. lum_m2++;
  831. lum_m1++;
  832. lum++;
  833. }
  834. }
  835. #endif /* !HAVE_MMX_EXTERNAL */
  836. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  837. top field is copied as is, but the bottom field is deinterlaced
  838. against the top field. */
  839. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  840. const uint8_t *src1, int src_wrap,
  841. int width, int height)
  842. {
  843. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  844. int y;
  845. src_m2 = src1;
  846. src_m1 = src1;
  847. src_0=&src_m1[src_wrap];
  848. src_p1=&src_0[src_wrap];
  849. src_p2=&src_p1[src_wrap];
  850. for(y=0;y<(height-2);y+=2) {
  851. memcpy(dst,src_m1,width);
  852. dst += dst_wrap;
  853. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  854. src_m2 = src_0;
  855. src_m1 = src_p1;
  856. src_0 = src_p2;
  857. src_p1 += 2*src_wrap;
  858. src_p2 += 2*src_wrap;
  859. dst += dst_wrap;
  860. }
  861. memcpy(dst,src_m1,width);
  862. dst += dst_wrap;
  863. /* do last line */
  864. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  865. }
  866. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  867. int width, int height)
  868. {
  869. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  870. int y;
  871. uint8_t *buf;
  872. buf = av_malloc(width);
  873. src_m1 = src1;
  874. memcpy(buf,src_m1,width);
  875. src_0=&src_m1[src_wrap];
  876. src_p1=&src_0[src_wrap];
  877. src_p2=&src_p1[src_wrap];
  878. for(y=0;y<(height-2);y+=2) {
  879. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  880. src_m1 = src_p1;
  881. src_0 = src_p2;
  882. src_p1 += 2*src_wrap;
  883. src_p2 += 2*src_wrap;
  884. }
  885. /* do last line */
  886. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  887. av_free(buf);
  888. }
  889. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  890. enum AVPixelFormat pix_fmt, int width, int height)
  891. {
  892. int i;
  893. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  894. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  895. pix_fmt != AV_PIX_FMT_YUV422P &&
  896. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  897. pix_fmt != AV_PIX_FMT_YUV444P &&
  898. pix_fmt != AV_PIX_FMT_YUV411P &&
  899. pix_fmt != AV_PIX_FMT_GRAY8)
  900. return -1;
  901. if ((width & 3) != 0 || (height & 3) != 0)
  902. return -1;
  903. for(i=0;i<3;i++) {
  904. if (i == 1) {
  905. switch(pix_fmt) {
  906. case AV_PIX_FMT_YUVJ420P:
  907. case AV_PIX_FMT_YUV420P:
  908. width >>= 1;
  909. height >>= 1;
  910. break;
  911. case AV_PIX_FMT_YUV422P:
  912. case AV_PIX_FMT_YUVJ422P:
  913. width >>= 1;
  914. break;
  915. case AV_PIX_FMT_YUV411P:
  916. width >>= 2;
  917. break;
  918. default:
  919. break;
  920. }
  921. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  922. break;
  923. }
  924. }
  925. if (src == dst) {
  926. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  927. width, height);
  928. } else {
  929. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  930. src->data[i], src->linesize[i],
  931. width, height);
  932. }
  933. }
  934. emms_c();
  935. return 0;
  936. }