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.

921 lines
26KB

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