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.

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