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.

962 lines
27KB

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