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.

853 lines
25KB

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