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.

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