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.

990 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 "imgconvert.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[PIX_FMT_NB] = {
  60. /* YUV formats */
  61. [PIX_FMT_YUV420P] = {
  62. .color_type = FF_COLOR_YUV,
  63. },
  64. [PIX_FMT_YUV422P] = {
  65. .color_type = FF_COLOR_YUV,
  66. },
  67. [PIX_FMT_YUV444P] = {
  68. .color_type = FF_COLOR_YUV,
  69. },
  70. [PIX_FMT_YUYV422] = {
  71. .color_type = FF_COLOR_YUV,
  72. },
  73. [PIX_FMT_UYVY422] = {
  74. .color_type = FF_COLOR_YUV,
  75. },
  76. [PIX_FMT_YUV410P] = {
  77. .color_type = FF_COLOR_YUV,
  78. },
  79. [PIX_FMT_YUV411P] = {
  80. .color_type = FF_COLOR_YUV,
  81. },
  82. [PIX_FMT_YUV440P] = {
  83. .color_type = FF_COLOR_YUV,
  84. },
  85. [PIX_FMT_YUV420P9LE] = {
  86. .color_type = FF_COLOR_YUV,
  87. },
  88. [PIX_FMT_YUV422P9LE] = {
  89. .color_type = FF_COLOR_YUV,
  90. },
  91. [PIX_FMT_YUV444P9LE] = {
  92. .color_type = FF_COLOR_YUV,
  93. },
  94. [PIX_FMT_YUV420P9BE] = {
  95. .color_type = FF_COLOR_YUV,
  96. },
  97. [PIX_FMT_YUV422P9BE] = {
  98. .color_type = FF_COLOR_YUV,
  99. },
  100. [PIX_FMT_YUV444P9BE] = {
  101. .color_type = FF_COLOR_YUV,
  102. },
  103. [PIX_FMT_YUV420P10LE] = {
  104. .color_type = FF_COLOR_YUV,
  105. },
  106. [PIX_FMT_YUV422P10LE] = {
  107. .color_type = FF_COLOR_YUV,
  108. },
  109. [PIX_FMT_YUV444P10LE] = {
  110. .color_type = FF_COLOR_YUV,
  111. },
  112. [PIX_FMT_YUV420P10BE] = {
  113. .color_type = FF_COLOR_YUV,
  114. },
  115. [PIX_FMT_YUV422P10BE] = {
  116. .color_type = FF_COLOR_YUV,
  117. },
  118. [PIX_FMT_YUV444P10BE] = {
  119. .color_type = FF_COLOR_YUV,
  120. },
  121. [PIX_FMT_YUV420P12LE] = {
  122. .color_type = FF_COLOR_YUV,
  123. },
  124. [PIX_FMT_YUV422P12LE] = {
  125. .color_type = FF_COLOR_YUV,
  126. },
  127. [PIX_FMT_YUV444P12LE] = {
  128. .color_type = FF_COLOR_YUV,
  129. },
  130. [PIX_FMT_YUV420P12BE] = {
  131. .color_type = FF_COLOR_YUV,
  132. },
  133. [PIX_FMT_YUV422P12BE] = {
  134. .color_type = FF_COLOR_YUV,
  135. },
  136. [PIX_FMT_YUV444P12BE] = {
  137. .color_type = FF_COLOR_YUV,
  138. },
  139. [PIX_FMT_YUV420P14LE] = {
  140. .color_type = FF_COLOR_YUV,
  141. },
  142. [PIX_FMT_YUV422P14LE] = {
  143. .color_type = FF_COLOR_YUV,
  144. },
  145. [PIX_FMT_YUV444P14LE] = {
  146. .color_type = FF_COLOR_YUV,
  147. },
  148. [PIX_FMT_YUV420P14BE] = {
  149. .color_type = FF_COLOR_YUV,
  150. },
  151. [PIX_FMT_YUV422P14BE] = {
  152. .color_type = FF_COLOR_YUV,
  153. },
  154. [PIX_FMT_YUV444P14BE] = {
  155. .color_type = FF_COLOR_YUV,
  156. },
  157. [PIX_FMT_YUV420P16LE] = {
  158. .color_type = FF_COLOR_YUV,
  159. },
  160. [PIX_FMT_YUV422P16LE] = {
  161. .color_type = FF_COLOR_YUV,
  162. },
  163. [PIX_FMT_YUV444P16LE] = {
  164. .color_type = FF_COLOR_YUV,
  165. },
  166. [PIX_FMT_YUV420P16BE] = {
  167. .color_type = FF_COLOR_YUV,
  168. },
  169. [PIX_FMT_YUV422P16BE] = {
  170. .color_type = FF_COLOR_YUV,
  171. },
  172. [PIX_FMT_YUV444P16BE] = {
  173. .color_type = FF_COLOR_YUV,
  174. },
  175. /* YUV formats with alpha plane */
  176. [PIX_FMT_YUVA420P] = {
  177. .color_type = FF_COLOR_YUV,
  178. },
  179. [PIX_FMT_YUVA422P] = {
  180. .color_type = FF_COLOR_YUV,
  181. },
  182. [PIX_FMT_YUVA444P] = {
  183. .color_type = FF_COLOR_YUV,
  184. },
  185. /* JPEG YUV */
  186. [PIX_FMT_YUVJ420P] = {
  187. .color_type = FF_COLOR_YUV_JPEG,
  188. },
  189. [PIX_FMT_YUVJ422P] = {
  190. .color_type = FF_COLOR_YUV_JPEG,
  191. },
  192. [PIX_FMT_YUVJ444P] = {
  193. .color_type = FF_COLOR_YUV_JPEG,
  194. },
  195. [PIX_FMT_YUVJ440P] = {
  196. .color_type = FF_COLOR_YUV_JPEG,
  197. },
  198. /* RGB formats */
  199. [PIX_FMT_RGB24] = {
  200. .color_type = FF_COLOR_RGB,
  201. },
  202. [PIX_FMT_BGR24] = {
  203. .color_type = FF_COLOR_RGB,
  204. },
  205. [PIX_FMT_ARGB] = {
  206. .color_type = FF_COLOR_RGB,
  207. },
  208. [PIX_FMT_RGB48BE] = {
  209. .color_type = FF_COLOR_RGB,
  210. },
  211. [PIX_FMT_RGB48LE] = {
  212. .color_type = FF_COLOR_RGB,
  213. },
  214. [PIX_FMT_RGBA64BE] = {
  215. .color_type = FF_COLOR_RGB,
  216. },
  217. [PIX_FMT_RGBA64LE] = {
  218. .color_type = FF_COLOR_RGB,
  219. },
  220. [PIX_FMT_RGB565BE] = {
  221. .color_type = FF_COLOR_RGB,
  222. },
  223. [PIX_FMT_RGB565LE] = {
  224. .color_type = FF_COLOR_RGB,
  225. },
  226. [PIX_FMT_RGB555BE] = {
  227. .color_type = FF_COLOR_RGB,
  228. .padded_size = 16,
  229. },
  230. [PIX_FMT_RGB555LE] = {
  231. .color_type = FF_COLOR_RGB,
  232. .padded_size = 16,
  233. },
  234. [PIX_FMT_RGB444BE] = {
  235. .color_type = FF_COLOR_RGB,
  236. .padded_size = 16,
  237. },
  238. [PIX_FMT_RGB444LE] = {
  239. .color_type = FF_COLOR_RGB,
  240. .padded_size = 16,
  241. },
  242. /* gray / mono formats */
  243. [PIX_FMT_GRAY16BE] = {
  244. .color_type = FF_COLOR_GRAY,
  245. },
  246. [PIX_FMT_GRAY16LE] = {
  247. .color_type = FF_COLOR_GRAY,
  248. },
  249. [PIX_FMT_GRAY8] = {
  250. .color_type = FF_COLOR_GRAY,
  251. },
  252. [PIX_FMT_GRAY8A] = {
  253. .color_type = FF_COLOR_GRAY,
  254. },
  255. [PIX_FMT_MONOWHITE] = {
  256. .color_type = FF_COLOR_GRAY,
  257. },
  258. [PIX_FMT_MONOBLACK] = {
  259. .color_type = FF_COLOR_GRAY,
  260. },
  261. /* paletted formats */
  262. [PIX_FMT_PAL8] = {
  263. .color_type = FF_COLOR_RGB,
  264. },
  265. [PIX_FMT_UYYVYY411] = {
  266. .color_type = FF_COLOR_YUV,
  267. },
  268. [PIX_FMT_ABGR] = {
  269. .color_type = FF_COLOR_RGB,
  270. },
  271. [PIX_FMT_BGR48BE] = {
  272. .color_type = FF_COLOR_RGB,
  273. },
  274. [PIX_FMT_BGR48LE] = {
  275. .color_type = FF_COLOR_RGB,
  276. },
  277. [PIX_FMT_BGRA64BE] = {
  278. .color_type = FF_COLOR_RGB,
  279. },
  280. [PIX_FMT_BGRA64LE] = {
  281. .color_type = FF_COLOR_RGB,
  282. },
  283. [PIX_FMT_BGR565BE] = {
  284. .color_type = FF_COLOR_RGB,
  285. .padded_size = 16,
  286. },
  287. [PIX_FMT_BGR565LE] = {
  288. .color_type = FF_COLOR_RGB,
  289. .padded_size = 16,
  290. },
  291. [PIX_FMT_BGR555BE] = {
  292. .color_type = FF_COLOR_RGB,
  293. .padded_size = 16,
  294. },
  295. [PIX_FMT_BGR555LE] = {
  296. .color_type = FF_COLOR_RGB,
  297. .padded_size = 16,
  298. },
  299. [PIX_FMT_BGR444BE] = {
  300. .color_type = FF_COLOR_RGB,
  301. .padded_size = 16,
  302. },
  303. [PIX_FMT_BGR444LE] = {
  304. .color_type = FF_COLOR_RGB,
  305. .padded_size = 16,
  306. },
  307. [PIX_FMT_RGB8] = {
  308. .color_type = FF_COLOR_RGB,
  309. },
  310. [PIX_FMT_RGB4] = {
  311. .color_type = FF_COLOR_RGB,
  312. },
  313. [PIX_FMT_RGB4_BYTE] = {
  314. .color_type = FF_COLOR_RGB,
  315. .padded_size = 8,
  316. },
  317. [PIX_FMT_BGR8] = {
  318. .color_type = FF_COLOR_RGB,
  319. },
  320. [PIX_FMT_BGR4] = {
  321. .color_type = FF_COLOR_RGB,
  322. },
  323. [PIX_FMT_BGR4_BYTE] = {
  324. .color_type = FF_COLOR_RGB,
  325. .padded_size = 8,
  326. },
  327. [PIX_FMT_NV12] = {
  328. .color_type = FF_COLOR_YUV,
  329. },
  330. [PIX_FMT_NV21] = {
  331. .color_type = FF_COLOR_YUV,
  332. },
  333. [PIX_FMT_BGRA] = {
  334. .color_type = FF_COLOR_RGB,
  335. },
  336. [PIX_FMT_RGBA] = {
  337. .color_type = FF_COLOR_RGB,
  338. },
  339. [PIX_FMT_GBRP] = {
  340. .color_type = FF_COLOR_RGB,
  341. },
  342. [PIX_FMT_GBRP9BE] = {
  343. .color_type = FF_COLOR_RGB,
  344. },
  345. [PIX_FMT_GBRP9LE] = {
  346. .color_type = FF_COLOR_RGB,
  347. },
  348. [PIX_FMT_GBRP10BE] = {
  349. .color_type = FF_COLOR_RGB,
  350. },
  351. [PIX_FMT_GBRP10LE] = {
  352. .color_type = FF_COLOR_RGB,
  353. },
  354. [PIX_FMT_GBRP12BE] = {
  355. .color_type = FF_COLOR_RGB,
  356. },
  357. [PIX_FMT_GBRP12LE] = {
  358. .color_type = FF_COLOR_RGB,
  359. },
  360. [PIX_FMT_GBRP14BE] = {
  361. .color_type = FF_COLOR_RGB,
  362. },
  363. [PIX_FMT_GBRP14LE] = {
  364. .color_type = FF_COLOR_RGB,
  365. },
  366. [PIX_FMT_GBRP16BE] = {
  367. .color_type = FF_COLOR_RGB,
  368. },
  369. [PIX_FMT_GBRP16LE] = {
  370. .color_type = FF_COLOR_RGB,
  371. },
  372. };
  373. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  374. {
  375. *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  376. *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  377. }
  378. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  379. {
  380. return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
  381. }
  382. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  383. enum PixelFormat pix_fmt, int width, int height)
  384. {
  385. return av_image_fill_arrays(picture->data, picture->linesize,
  386. ptr, pix_fmt, width, height, 1);
  387. }
  388. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  389. unsigned char *dest, int dest_size)
  390. {
  391. return av_image_copy_to_buffer(dest, dest_size,
  392. (const uint8_t * const*)src->data, src->linesize,
  393. pix_fmt, width, height, 1);
  394. }
  395. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  396. {
  397. return av_image_get_buffer_size(pix_fmt, width, height, 1);
  398. }
  399. static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
  400. {
  401. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  402. int i;
  403. if (!desc->nb_components) {
  404. *min = *max = 0;
  405. return AVERROR(EINVAL);
  406. }
  407. *min = INT_MAX, *max = -INT_MAX;
  408. for (i = 0; i < desc->nb_components; i++) {
  409. *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
  410. *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
  411. }
  412. return 0;
  413. }
  414. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  415. int has_alpha)
  416. {
  417. const PixFmtInfo *pf, *ps;
  418. const AVPixFmtDescriptor *src_desc;
  419. const AVPixFmtDescriptor *dst_desc;
  420. int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
  421. int ret, loss;
  422. if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
  423. return ~0;
  424. src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  425. dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  426. ps = &pix_fmt_info[src_pix_fmt];
  427. /* compute loss */
  428. loss = 0;
  429. if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
  430. return ret;
  431. if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
  432. return ret;
  433. if (dst_min_depth < src_min_depth ||
  434. dst_max_depth < src_max_depth)
  435. loss |= FF_LOSS_DEPTH;
  436. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  437. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  438. loss |= FF_LOSS_RESOLUTION;
  439. pf = &pix_fmt_info[dst_pix_fmt];
  440. switch(pf->color_type) {
  441. case FF_COLOR_RGB:
  442. if (ps->color_type != FF_COLOR_RGB &&
  443. ps->color_type != FF_COLOR_GRAY)
  444. loss |= FF_LOSS_COLORSPACE;
  445. break;
  446. case FF_COLOR_GRAY:
  447. if (ps->color_type != FF_COLOR_GRAY)
  448. loss |= FF_LOSS_COLORSPACE;
  449. break;
  450. case FF_COLOR_YUV:
  451. if (ps->color_type != FF_COLOR_YUV)
  452. loss |= FF_LOSS_COLORSPACE;
  453. break;
  454. case FF_COLOR_YUV_JPEG:
  455. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  456. ps->color_type != FF_COLOR_YUV &&
  457. ps->color_type != FF_COLOR_GRAY)
  458. loss |= FF_LOSS_COLORSPACE;
  459. break;
  460. default:
  461. /* fail safe test */
  462. if (ps->color_type != pf->color_type)
  463. loss |= FF_LOSS_COLORSPACE;
  464. break;
  465. }
  466. if (pf->color_type == FF_COLOR_GRAY &&
  467. ps->color_type != FF_COLOR_GRAY)
  468. loss |= FF_LOSS_CHROMA;
  469. if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
  470. loss |= FF_LOSS_ALPHA;
  471. if (dst_pix_fmt == PIX_FMT_PAL8 &&
  472. (src_pix_fmt != PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
  473. loss |= FF_LOSS_COLORQUANT;
  474. return loss;
  475. }
  476. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  477. {
  478. const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
  479. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  480. return info->padded_size ?
  481. info->padded_size : av_get_bits_per_pixel(desc);
  482. }
  483. #if FF_API_FIND_BEST_PIX_FMT
  484. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  485. int has_alpha, int *loss_ptr)
  486. {
  487. enum PixelFormat dst_pix_fmt;
  488. int i;
  489. if (loss_ptr) /* all losses count (for backward compatibility) */
  490. *loss_ptr = 0;
  491. dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
  492. for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
  493. if (pix_fmt_mask & (1ULL << i))
  494. dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
  495. }
  496. return dst_pix_fmt;
  497. }
  498. #endif /* FF_API_FIND_BEST_PIX_FMT */
  499. enum PixelFormat avcodec_find_best_pix_fmt_of_2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
  500. enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  501. {
  502. enum PixelFormat dst_pix_fmt;
  503. int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
  504. static const int loss_mask_order[] = {
  505. ~0, /* no loss first */
  506. ~FF_LOSS_ALPHA,
  507. ~FF_LOSS_RESOLUTION,
  508. ~FF_LOSS_COLORSPACE,
  509. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  510. ~FF_LOSS_COLORQUANT,
  511. ~FF_LOSS_DEPTH,
  512. ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
  513. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  514. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  515. 0x80000, //non zero entry that combines all loss variants including future additions
  516. 0,
  517. };
  518. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  519. dst_pix_fmt = PIX_FMT_NONE;
  520. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  521. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  522. /* try with successive loss */
  523. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
  524. loss_order1 = loss1 & loss_mask_order[i];
  525. loss_order2 = loss2 & loss_mask_order[i];
  526. if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
  527. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  528. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  529. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  530. }
  531. }
  532. if (loss_ptr)
  533. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  534. return dst_pix_fmt;
  535. }
  536. #if AV_HAVE_INCOMPATIBLE_FORK_ABI
  537. enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat *pix_fmt_list,
  538. enum PixelFormat src_pix_fmt,
  539. int has_alpha, int *loss_ptr){
  540. return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
  541. }
  542. #else
  543. enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
  544. enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  545. {
  546. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  547. }
  548. #endif
  549. enum PixelFormat avcodec_find_best_pix_fmt_of_list(enum PixelFormat *pix_fmt_list,
  550. enum PixelFormat src_pix_fmt,
  551. int has_alpha, int *loss_ptr){
  552. int i;
  553. enum PixelFormat best = PIX_FMT_NONE;
  554. for(i=0; pix_fmt_list[i] != PIX_FMT_NONE; i++)
  555. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
  556. return best;
  557. }
  558. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  559. enum PixelFormat pix_fmt, int width, int height)
  560. {
  561. av_image_copy(dst->data, dst->linesize, (const uint8_t **)src->data,
  562. src->linesize, pix_fmt, width, height);
  563. }
  564. /* 2x2 -> 1x1 */
  565. void ff_shrink22(uint8_t *dst, int dst_wrap,
  566. const uint8_t *src, int src_wrap,
  567. int width, int height)
  568. {
  569. int w;
  570. const uint8_t *s1, *s2;
  571. uint8_t *d;
  572. for(;height > 0; height--) {
  573. s1 = src;
  574. s2 = s1 + src_wrap;
  575. d = dst;
  576. for(w = width;w >= 4; w-=4) {
  577. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  578. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  579. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  580. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  581. s1 += 8;
  582. s2 += 8;
  583. d += 4;
  584. }
  585. for(;w > 0; w--) {
  586. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  587. s1 += 2;
  588. s2 += 2;
  589. d++;
  590. }
  591. src += 2 * src_wrap;
  592. dst += dst_wrap;
  593. }
  594. }
  595. /* 4x4 -> 1x1 */
  596. void ff_shrink44(uint8_t *dst, int dst_wrap,
  597. const uint8_t *src, int src_wrap,
  598. int width, int height)
  599. {
  600. int w;
  601. const uint8_t *s1, *s2, *s3, *s4;
  602. uint8_t *d;
  603. for(;height > 0; height--) {
  604. s1 = src;
  605. s2 = s1 + src_wrap;
  606. s3 = s2 + src_wrap;
  607. s4 = s3 + src_wrap;
  608. d = dst;
  609. for(w = width;w > 0; w--) {
  610. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  611. s2[0] + s2[1] + s2[2] + s2[3] +
  612. s3[0] + s3[1] + s3[2] + s3[3] +
  613. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  614. s1 += 4;
  615. s2 += 4;
  616. s3 += 4;
  617. s4 += 4;
  618. d++;
  619. }
  620. src += 4 * src_wrap;
  621. dst += dst_wrap;
  622. }
  623. }
  624. /* 8x8 -> 1x1 */
  625. void ff_shrink88(uint8_t *dst, int dst_wrap,
  626. const uint8_t *src, int src_wrap,
  627. int width, int height)
  628. {
  629. int w, i;
  630. for(;height > 0; height--) {
  631. for(w = width;w > 0; w--) {
  632. int tmp=0;
  633. for(i=0; i<8; i++){
  634. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  635. src += src_wrap;
  636. }
  637. *(dst++) = (tmp + 32)>>6;
  638. src += 8 - 8*src_wrap;
  639. }
  640. src += 8*src_wrap - 8*width;
  641. dst += dst_wrap - width;
  642. }
  643. }
  644. int avpicture_alloc(AVPicture *picture,
  645. enum PixelFormat pix_fmt, int width, int height)
  646. {
  647. int ret;
  648. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  649. memset(picture, 0, sizeof(AVPicture));
  650. return ret;
  651. }
  652. return 0;
  653. }
  654. void avpicture_free(AVPicture *picture)
  655. {
  656. av_free(picture->data[0]);
  657. }
  658. /* return true if yuv planar */
  659. static inline int is_yuv_planar(enum PixelFormat fmt)
  660. {
  661. const PixFmtInfo *info = &pix_fmt_info[fmt];
  662. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
  663. int i;
  664. int planes[4] = { 0 };
  665. if (info->color_type != FF_COLOR_YUV &&
  666. info->color_type != FF_COLOR_YUV_JPEG)
  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 PixelFormat pix_fmt, int top_band, int left_band)
  679. {
  680. int y_shift;
  681. int x_shift;
  682. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  683. return -1;
  684. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  685. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  686. if (is_yuv_planar(pix_fmt)) {
  687. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  688. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  689. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  690. } else{
  691. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  692. return -1;
  693. if(left_band) //FIXME add support for this too
  694. return -1;
  695. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  696. }
  697. dst->linesize[0] = src->linesize[0];
  698. dst->linesize[1] = src->linesize[1];
  699. dst->linesize[2] = src->linesize[2];
  700. return 0;
  701. }
  702. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  703. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  704. int *color)
  705. {
  706. uint8_t *optr;
  707. int y_shift;
  708. int x_shift;
  709. int yheight;
  710. int i, y;
  711. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  712. !is_yuv_planar(pix_fmt)) return -1;
  713. for (i = 0; i < 3; i++) {
  714. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  715. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  716. if (padtop || padleft) {
  717. memset(dst->data[i], color[i],
  718. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  719. }
  720. if (padleft || padright) {
  721. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  722. (dst->linesize[i] - (padright >> x_shift));
  723. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  724. for (y = 0; y < yheight; y++) {
  725. memset(optr, color[i], (padleft + padright) >> x_shift);
  726. optr += dst->linesize[i];
  727. }
  728. }
  729. if (src) { /* first line */
  730. uint8_t *iptr = src->data[i];
  731. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  732. (padleft >> x_shift);
  733. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  734. iptr += src->linesize[i];
  735. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  736. (dst->linesize[i] - (padright >> x_shift));
  737. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  738. for (y = 0; y < yheight; y++) {
  739. memset(optr, color[i], (padleft + padright) >> x_shift);
  740. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  741. (width - padleft - padright) >> x_shift);
  742. iptr += src->linesize[i];
  743. optr += dst->linesize[i];
  744. }
  745. }
  746. if (padbottom || padright) {
  747. optr = dst->data[i] + dst->linesize[i] *
  748. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  749. memset(optr, color[i],dst->linesize[i] *
  750. (padbottom >> y_shift) + (padright >> x_shift));
  751. }
  752. }
  753. return 0;
  754. }
  755. #if !HAVE_MMX_EXTERNAL
  756. /* filter parameters: [-1 4 2 4 -1] // 8 */
  757. static void deinterlace_line_c(uint8_t *dst,
  758. const uint8_t *lum_m4, const uint8_t *lum_m3,
  759. const uint8_t *lum_m2, const uint8_t *lum_m1,
  760. const uint8_t *lum,
  761. int size)
  762. {
  763. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  764. int sum;
  765. for(;size > 0;size--) {
  766. sum = -lum_m4[0];
  767. sum += lum_m3[0] << 2;
  768. sum += lum_m2[0] << 1;
  769. sum += lum_m1[0] << 2;
  770. sum += -lum[0];
  771. dst[0] = cm[(sum + 4) >> 3];
  772. lum_m4++;
  773. lum_m3++;
  774. lum_m2++;
  775. lum_m1++;
  776. lum++;
  777. dst++;
  778. }
  779. }
  780. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  781. uint8_t *lum_m2, uint8_t *lum_m1,
  782. uint8_t *lum, int size)
  783. {
  784. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  785. int sum;
  786. for(;size > 0;size--) {
  787. sum = -lum_m4[0];
  788. sum += lum_m3[0] << 2;
  789. sum += lum_m2[0] << 1;
  790. lum_m4[0]=lum_m2[0];
  791. sum += lum_m1[0] << 2;
  792. sum += -lum[0];
  793. lum_m2[0] = cm[(sum + 4) >> 3];
  794. lum_m4++;
  795. lum_m3++;
  796. lum_m2++;
  797. lum_m1++;
  798. lum++;
  799. }
  800. }
  801. #endif /* !HAVE_MMX_EXTERNAL */
  802. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  803. top field is copied as is, but the bottom field is deinterlaced
  804. against the top field. */
  805. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  806. const uint8_t *src1, int src_wrap,
  807. int width, int height)
  808. {
  809. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  810. int y;
  811. src_m2 = src1;
  812. src_m1 = src1;
  813. src_0=&src_m1[src_wrap];
  814. src_p1=&src_0[src_wrap];
  815. src_p2=&src_p1[src_wrap];
  816. for(y=0;y<(height-2);y+=2) {
  817. memcpy(dst,src_m1,width);
  818. dst += dst_wrap;
  819. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  820. src_m2 = src_0;
  821. src_m1 = src_p1;
  822. src_0 = src_p2;
  823. src_p1 += 2*src_wrap;
  824. src_p2 += 2*src_wrap;
  825. dst += dst_wrap;
  826. }
  827. memcpy(dst,src_m1,width);
  828. dst += dst_wrap;
  829. /* do last line */
  830. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  831. }
  832. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  833. int width, int height)
  834. {
  835. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  836. int y;
  837. uint8_t *buf;
  838. buf = av_malloc(width);
  839. src_m1 = src1;
  840. memcpy(buf,src_m1,width);
  841. src_0=&src_m1[src_wrap];
  842. src_p1=&src_0[src_wrap];
  843. src_p2=&src_p1[src_wrap];
  844. for(y=0;y<(height-2);y+=2) {
  845. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  846. src_m1 = src_p1;
  847. src_0 = src_p2;
  848. src_p1 += 2*src_wrap;
  849. src_p2 += 2*src_wrap;
  850. }
  851. /* do last line */
  852. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  853. av_free(buf);
  854. }
  855. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  856. enum PixelFormat pix_fmt, int width, int height)
  857. {
  858. int i;
  859. if (pix_fmt != PIX_FMT_YUV420P &&
  860. pix_fmt != PIX_FMT_YUVJ420P &&
  861. pix_fmt != PIX_FMT_YUV422P &&
  862. pix_fmt != PIX_FMT_YUVJ422P &&
  863. pix_fmt != PIX_FMT_YUV444P &&
  864. pix_fmt != PIX_FMT_YUV411P &&
  865. pix_fmt != PIX_FMT_GRAY8)
  866. return -1;
  867. if ((width & 3) != 0 || (height & 3) != 0)
  868. return -1;
  869. for(i=0;i<3;i++) {
  870. if (i == 1) {
  871. switch(pix_fmt) {
  872. case PIX_FMT_YUVJ420P:
  873. case PIX_FMT_YUV420P:
  874. width >>= 1;
  875. height >>= 1;
  876. break;
  877. case PIX_FMT_YUV422P:
  878. case PIX_FMT_YUVJ422P:
  879. width >>= 1;
  880. break;
  881. case PIX_FMT_YUV411P:
  882. width >>= 2;
  883. break;
  884. default:
  885. break;
  886. }
  887. if (pix_fmt == PIX_FMT_GRAY8) {
  888. break;
  889. }
  890. }
  891. if (src == dst) {
  892. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  893. width, height);
  894. } else {
  895. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  896. src->data[i], src->linesize[i],
  897. width, height);
  898. }
  899. }
  900. emms_c();
  901. return 0;
  902. }