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.

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