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.

959 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. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  483. int has_alpha, int *loss_ptr)
  484. {
  485. enum PixelFormat dst_pix_fmt;
  486. int i;
  487. if (loss_ptr) /* all losses count (for backward compatibility) */
  488. *loss_ptr = 0;
  489. dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
  490. for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
  491. if (pix_fmt_mask & (1ULL << i))
  492. dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
  493. }
  494. return dst_pix_fmt;
  495. }
  496. enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
  497. enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  498. {
  499. enum PixelFormat dst_pix_fmt;
  500. int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
  501. static const int loss_mask_order[] = {
  502. ~0, /* no loss first */
  503. ~FF_LOSS_ALPHA,
  504. ~FF_LOSS_RESOLUTION,
  505. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  506. ~FF_LOSS_COLORQUANT,
  507. ~FF_LOSS_DEPTH,
  508. ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
  509. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  510. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  511. 0x80000, //non zero entry that combines all loss variants including future additions
  512. 0,
  513. };
  514. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  515. dst_pix_fmt = PIX_FMT_NONE;
  516. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  517. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  518. /* try with successive loss */
  519. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
  520. loss_order1 = loss1 & loss_mask_order[i];
  521. loss_order2 = loss2 & loss_mask_order[i];
  522. if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
  523. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  524. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  525. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  526. }
  527. }
  528. if (loss_ptr)
  529. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  530. return dst_pix_fmt;
  531. }
  532. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  533. enum PixelFormat pix_fmt, int width, int height)
  534. {
  535. av_image_copy(dst->data, dst->linesize, src->data,
  536. src->linesize, pix_fmt, width, height);
  537. }
  538. /* 2x2 -> 1x1 */
  539. void ff_shrink22(uint8_t *dst, int dst_wrap,
  540. const uint8_t *src, int src_wrap,
  541. int width, int height)
  542. {
  543. int w;
  544. const uint8_t *s1, *s2;
  545. uint8_t *d;
  546. for(;height > 0; height--) {
  547. s1 = src;
  548. s2 = s1 + src_wrap;
  549. d = dst;
  550. for(w = width;w >= 4; w-=4) {
  551. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  552. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  553. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  554. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  555. s1 += 8;
  556. s2 += 8;
  557. d += 4;
  558. }
  559. for(;w > 0; w--) {
  560. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  561. s1 += 2;
  562. s2 += 2;
  563. d++;
  564. }
  565. src += 2 * src_wrap;
  566. dst += dst_wrap;
  567. }
  568. }
  569. /* 4x4 -> 1x1 */
  570. void ff_shrink44(uint8_t *dst, int dst_wrap,
  571. const uint8_t *src, int src_wrap,
  572. int width, int height)
  573. {
  574. int w;
  575. const uint8_t *s1, *s2, *s3, *s4;
  576. uint8_t *d;
  577. for(;height > 0; height--) {
  578. s1 = src;
  579. s2 = s1 + src_wrap;
  580. s3 = s2 + src_wrap;
  581. s4 = s3 + src_wrap;
  582. d = dst;
  583. for(w = width;w > 0; w--) {
  584. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  585. s2[0] + s2[1] + s2[2] + s2[3] +
  586. s3[0] + s3[1] + s3[2] + s3[3] +
  587. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  588. s1 += 4;
  589. s2 += 4;
  590. s3 += 4;
  591. s4 += 4;
  592. d++;
  593. }
  594. src += 4 * src_wrap;
  595. dst += dst_wrap;
  596. }
  597. }
  598. /* 8x8 -> 1x1 */
  599. void ff_shrink88(uint8_t *dst, int dst_wrap,
  600. const uint8_t *src, int src_wrap,
  601. int width, int height)
  602. {
  603. int w, i;
  604. for(;height > 0; height--) {
  605. for(w = width;w > 0; w--) {
  606. int tmp=0;
  607. for(i=0; i<8; i++){
  608. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  609. src += src_wrap;
  610. }
  611. *(dst++) = (tmp + 32)>>6;
  612. src += 8 - 8*src_wrap;
  613. }
  614. src += 8*src_wrap - 8*width;
  615. dst += dst_wrap - width;
  616. }
  617. }
  618. int avpicture_alloc(AVPicture *picture,
  619. enum PixelFormat pix_fmt, int width, int height)
  620. {
  621. int ret;
  622. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  623. memset(picture, 0, sizeof(AVPicture));
  624. return ret;
  625. }
  626. return 0;
  627. }
  628. void avpicture_free(AVPicture *picture)
  629. {
  630. av_free(picture->data[0]);
  631. }
  632. /* return true if yuv planar */
  633. static inline int is_yuv_planar(enum PixelFormat fmt)
  634. {
  635. const PixFmtInfo *info = &pix_fmt_info[fmt];
  636. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
  637. int i;
  638. int planes[4] = { 0 };
  639. if (info->color_type != FF_COLOR_YUV &&
  640. info->color_type != FF_COLOR_YUV_JPEG)
  641. return 0;
  642. /* set the used planes */
  643. for (i = 0; i < desc->nb_components; i++)
  644. planes[desc->comp[i].plane] = 1;
  645. /* if there is an unused plane, the format is not planar */
  646. for (i = 0; i < desc->nb_components; i++)
  647. if (!planes[i])
  648. return 0;
  649. return 1;
  650. }
  651. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  652. enum PixelFormat pix_fmt, int top_band, int left_band)
  653. {
  654. int y_shift;
  655. int x_shift;
  656. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  657. return -1;
  658. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  659. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  660. if (is_yuv_planar(pix_fmt)) {
  661. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  662. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  663. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  664. } else{
  665. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  666. return -1;
  667. if(left_band) //FIXME add support for this too
  668. return -1;
  669. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  670. }
  671. dst->linesize[0] = src->linesize[0];
  672. dst->linesize[1] = src->linesize[1];
  673. dst->linesize[2] = src->linesize[2];
  674. return 0;
  675. }
  676. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  677. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  678. int *color)
  679. {
  680. uint8_t *optr;
  681. int y_shift;
  682. int x_shift;
  683. int yheight;
  684. int i, y;
  685. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  686. !is_yuv_planar(pix_fmt)) return -1;
  687. for (i = 0; i < 3; i++) {
  688. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  689. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  690. if (padtop || padleft) {
  691. memset(dst->data[i], color[i],
  692. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  693. }
  694. if (padleft || padright) {
  695. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  696. (dst->linesize[i] - (padright >> x_shift));
  697. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  698. for (y = 0; y < yheight; y++) {
  699. memset(optr, color[i], (padleft + padright) >> x_shift);
  700. optr += dst->linesize[i];
  701. }
  702. }
  703. if (src) { /* first line */
  704. uint8_t *iptr = src->data[i];
  705. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  706. (padleft >> x_shift);
  707. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  708. iptr += src->linesize[i];
  709. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  710. (dst->linesize[i] - (padright >> x_shift));
  711. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  712. for (y = 0; y < yheight; y++) {
  713. memset(optr, color[i], (padleft + padright) >> x_shift);
  714. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  715. (width - padleft - padright) >> x_shift);
  716. iptr += src->linesize[i];
  717. optr += dst->linesize[i];
  718. }
  719. }
  720. if (padbottom || padright) {
  721. optr = dst->data[i] + dst->linesize[i] *
  722. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  723. memset(optr, color[i],dst->linesize[i] *
  724. (padbottom >> y_shift) + (padright >> x_shift));
  725. }
  726. }
  727. return 0;
  728. }
  729. #if !(HAVE_MMX && HAVE_YASM)
  730. /* filter parameters: [-1 4 2 4 -1] // 8 */
  731. static void deinterlace_line_c(uint8_t *dst,
  732. const uint8_t *lum_m4, const uint8_t *lum_m3,
  733. const uint8_t *lum_m2, const uint8_t *lum_m1,
  734. const uint8_t *lum,
  735. int size)
  736. {
  737. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  738. int sum;
  739. for(;size > 0;size--) {
  740. sum = -lum_m4[0];
  741. sum += lum_m3[0] << 2;
  742. sum += lum_m2[0] << 1;
  743. sum += lum_m1[0] << 2;
  744. sum += -lum[0];
  745. dst[0] = cm[(sum + 4) >> 3];
  746. lum_m4++;
  747. lum_m3++;
  748. lum_m2++;
  749. lum_m1++;
  750. lum++;
  751. dst++;
  752. }
  753. }
  754. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  755. uint8_t *lum_m2, uint8_t *lum_m1,
  756. uint8_t *lum, int size)
  757. {
  758. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  759. int sum;
  760. for(;size > 0;size--) {
  761. sum = -lum_m4[0];
  762. sum += lum_m3[0] << 2;
  763. sum += lum_m2[0] << 1;
  764. lum_m4[0]=lum_m2[0];
  765. sum += lum_m1[0] << 2;
  766. sum += -lum[0];
  767. lum_m2[0] = cm[(sum + 4) >> 3];
  768. lum_m4++;
  769. lum_m3++;
  770. lum_m2++;
  771. lum_m1++;
  772. lum++;
  773. }
  774. }
  775. #endif
  776. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  777. top field is copied as is, but the bottom field is deinterlaced
  778. against the top field. */
  779. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  780. const uint8_t *src1, int src_wrap,
  781. int width, int height)
  782. {
  783. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  784. int y;
  785. src_m2 = src1;
  786. src_m1 = src1;
  787. src_0=&src_m1[src_wrap];
  788. src_p1=&src_0[src_wrap];
  789. src_p2=&src_p1[src_wrap];
  790. for(y=0;y<(height-2);y+=2) {
  791. memcpy(dst,src_m1,width);
  792. dst += dst_wrap;
  793. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  794. src_m2 = src_0;
  795. src_m1 = src_p1;
  796. src_0 = src_p2;
  797. src_p1 += 2*src_wrap;
  798. src_p2 += 2*src_wrap;
  799. dst += dst_wrap;
  800. }
  801. memcpy(dst,src_m1,width);
  802. dst += dst_wrap;
  803. /* do last line */
  804. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  805. }
  806. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  807. int width, int height)
  808. {
  809. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  810. int y;
  811. uint8_t *buf;
  812. buf = av_malloc(width);
  813. src_m1 = src1;
  814. memcpy(buf,src_m1,width);
  815. src_0=&src_m1[src_wrap];
  816. src_p1=&src_0[src_wrap];
  817. src_p2=&src_p1[src_wrap];
  818. for(y=0;y<(height-2);y+=2) {
  819. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  820. src_m1 = src_p1;
  821. src_0 = src_p2;
  822. src_p1 += 2*src_wrap;
  823. src_p2 += 2*src_wrap;
  824. }
  825. /* do last line */
  826. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  827. av_free(buf);
  828. }
  829. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  830. enum PixelFormat pix_fmt, int width, int height)
  831. {
  832. int i;
  833. if (pix_fmt != PIX_FMT_YUV420P &&
  834. pix_fmt != PIX_FMT_YUVJ420P &&
  835. pix_fmt != PIX_FMT_YUV422P &&
  836. pix_fmt != PIX_FMT_YUVJ422P &&
  837. pix_fmt != PIX_FMT_YUV444P &&
  838. pix_fmt != PIX_FMT_YUV411P &&
  839. pix_fmt != PIX_FMT_GRAY8)
  840. return -1;
  841. if ((width & 3) != 0 || (height & 3) != 0)
  842. return -1;
  843. for(i=0;i<3;i++) {
  844. if (i == 1) {
  845. switch(pix_fmt) {
  846. case PIX_FMT_YUVJ420P:
  847. case PIX_FMT_YUV420P:
  848. width >>= 1;
  849. height >>= 1;
  850. break;
  851. case PIX_FMT_YUV422P:
  852. case PIX_FMT_YUVJ422P:
  853. width >>= 1;
  854. break;
  855. case PIX_FMT_YUV411P:
  856. width >>= 2;
  857. break;
  858. default:
  859. break;
  860. }
  861. if (pix_fmt == PIX_FMT_GRAY8) {
  862. break;
  863. }
  864. }
  865. if (src == dst) {
  866. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  867. width, height);
  868. } else {
  869. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  870. src->data[i], src->linesize[i],
  871. width, height);
  872. }
  873. }
  874. emms_c();
  875. return 0;
  876. }