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.

964 lines
28KB

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