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.

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