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.

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