You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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