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.

669 lines
21KB

  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 "imgconvert.h"
  33. #include "internal.h"
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/colorspace.h"
  36. #include "libavutil/common.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/imgutils.h"
  39. #if HAVE_MMX_EXTERNAL
  40. #include "x86/dsputil_x86.h"
  41. #endif
  42. #define FF_COLOR_NA -1
  43. #define FF_COLOR_RGB 0 /**< RGB color space */
  44. #define FF_COLOR_GRAY 1 /**< gray color space */
  45. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  46. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  47. #if HAVE_MMX_EXTERNAL
  48. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  49. #define deinterlace_line ff_deinterlace_line_mmx
  50. #else
  51. #define deinterlace_line_inplace deinterlace_line_inplace_c
  52. #define deinterlace_line deinterlace_line_c
  53. #endif
  54. #define pixdesc_has_alpha(pixdesc) \
  55. ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & AV_PIX_FMT_FLAG_PAL)
  56. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  57. {
  58. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  59. av_assert0(desc);
  60. *h_shift = desc->log2_chroma_w;
  61. *v_shift = desc->log2_chroma_h;
  62. }
  63. static int get_color_type(const AVPixFmtDescriptor *desc) {
  64. if (desc->flags & AV_PIX_FMT_FLAG_PAL)
  65. return FF_COLOR_RGB;
  66. if(desc->nb_components == 1 || desc->nb_components == 2)
  67. return FF_COLOR_GRAY;
  68. if(desc->name && !strncmp(desc->name, "yuvj", 4))
  69. return FF_COLOR_YUV_JPEG;
  70. if(desc->flags & AV_PIX_FMT_FLAG_RGB)
  71. return FF_COLOR_RGB;
  72. if(desc->nb_components == 0)
  73. return FF_COLOR_NA;
  74. return FF_COLOR_YUV;
  75. }
  76. static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
  77. {
  78. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  79. int i;
  80. if (!desc || !desc->nb_components) {
  81. *min = *max = 0;
  82. return AVERROR(EINVAL);
  83. }
  84. *min = INT_MAX, *max = -INT_MAX;
  85. for (i = 0; i < desc->nb_components; i++) {
  86. *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
  87. *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
  88. }
  89. return 0;
  90. }
  91. static int get_pix_fmt_score(enum AVPixelFormat dst_pix_fmt,
  92. enum AVPixelFormat src_pix_fmt,
  93. unsigned *lossp, unsigned consider)
  94. {
  95. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  96. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  97. int src_color, dst_color;
  98. int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
  99. int ret, loss, i, nb_components;
  100. int score = INT_MAX - 1;
  101. if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
  102. return ~0;
  103. /* compute loss */
  104. *lossp = loss = 0;
  105. if (dst_pix_fmt == src_pix_fmt)
  106. return INT_MAX;
  107. if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
  108. return ret;
  109. if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
  110. return ret;
  111. src_color = get_color_type(src_desc);
  112. dst_color = get_color_type(dst_desc);
  113. nb_components = FFMIN(src_desc->nb_components, dst_desc->nb_components);
  114. for (i = 0; i < nb_components; i++)
  115. if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1 && (consider & FF_LOSS_DEPTH)) {
  116. loss |= FF_LOSS_DEPTH;
  117. score -= 65536 >> dst_desc->comp[i].depth_minus1;
  118. }
  119. if (consider & FF_LOSS_RESOLUTION) {
  120. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w) {
  121. loss |= FF_LOSS_RESOLUTION;
  122. score -= 256 << dst_desc->log2_chroma_w;
  123. }
  124. if (dst_desc->log2_chroma_h > src_desc->log2_chroma_h) {
  125. loss |= FF_LOSS_RESOLUTION;
  126. score -= 256 << dst_desc->log2_chroma_h;
  127. }
  128. // don't favor 422 over 420 if downsampling is needed, because 420 has much better support on the decoder side
  129. if (dst_desc->log2_chroma_w == 1 && src_desc->log2_chroma_w == 0 &&
  130. dst_desc->log2_chroma_h == 1 && src_desc->log2_chroma_h == 0 ) {
  131. score += 512;
  132. }
  133. }
  134. if(consider & FF_LOSS_COLORSPACE)
  135. switch(dst_color) {
  136. case FF_COLOR_RGB:
  137. if (src_color != FF_COLOR_RGB &&
  138. src_color != FF_COLOR_GRAY)
  139. loss |= FF_LOSS_COLORSPACE;
  140. break;
  141. case FF_COLOR_GRAY:
  142. if (src_color != FF_COLOR_GRAY)
  143. loss |= FF_LOSS_COLORSPACE;
  144. break;
  145. case FF_COLOR_YUV:
  146. if (src_color != FF_COLOR_YUV)
  147. loss |= FF_LOSS_COLORSPACE;
  148. break;
  149. case FF_COLOR_YUV_JPEG:
  150. if (src_color != FF_COLOR_YUV_JPEG &&
  151. src_color != FF_COLOR_YUV &&
  152. src_color != FF_COLOR_GRAY)
  153. loss |= FF_LOSS_COLORSPACE;
  154. break;
  155. default:
  156. /* fail safe test */
  157. if (src_color != dst_color)
  158. loss |= FF_LOSS_COLORSPACE;
  159. break;
  160. }
  161. if(loss & FF_LOSS_COLORSPACE)
  162. score -= (nb_components * 65536) >> FFMIN(dst_desc->comp[0].depth_minus1, src_desc->comp[0].depth_minus1);
  163. if (dst_color == FF_COLOR_GRAY &&
  164. src_color != FF_COLOR_GRAY && (consider & FF_LOSS_CHROMA)) {
  165. loss |= FF_LOSS_CHROMA;
  166. score -= 2 * 65536;
  167. }
  168. if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && (consider & FF_LOSS_ALPHA))) {
  169. loss |= FF_LOSS_ALPHA;
  170. score -= 65536;
  171. }
  172. if (dst_pix_fmt == AV_PIX_FMT_PAL8 && (consider & FF_LOSS_COLORQUANT) &&
  173. (src_pix_fmt != AV_PIX_FMT_PAL8 && (src_color != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && (consider & FF_LOSS_ALPHA))))) {
  174. loss |= FF_LOSS_COLORQUANT;
  175. score -= 65536;
  176. }
  177. *lossp = loss;
  178. return score;
  179. }
  180. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  181. enum AVPixelFormat src_pix_fmt,
  182. int has_alpha)
  183. {
  184. int loss;
  185. int ret = get_pix_fmt_score(dst_pix_fmt, src_pix_fmt, &loss, has_alpha ? ~0 : ~FF_LOSS_ALPHA);
  186. if (ret < 0)
  187. return ret;
  188. return loss;
  189. }
  190. enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  191. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  192. {
  193. enum AVPixelFormat dst_pix_fmt;
  194. int loss1, loss2, loss_mask;
  195. const AVPixFmtDescriptor *desc1 = av_pix_fmt_desc_get(dst_pix_fmt1);
  196. const AVPixFmtDescriptor *desc2 = av_pix_fmt_desc_get(dst_pix_fmt2);
  197. int score1, score2;
  198. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  199. if(!has_alpha)
  200. loss_mask &= ~FF_LOSS_ALPHA;
  201. dst_pix_fmt = AV_PIX_FMT_NONE;
  202. score1 = get_pix_fmt_score(dst_pix_fmt1, src_pix_fmt, &loss1, loss_mask);
  203. score2 = get_pix_fmt_score(dst_pix_fmt2, src_pix_fmt, &loss2, loss_mask);
  204. if (score1 == score2) {
  205. if(av_get_padded_bits_per_pixel(desc2) != av_get_padded_bits_per_pixel(desc1)) {
  206. dst_pix_fmt = av_get_padded_bits_per_pixel(desc2) < av_get_padded_bits_per_pixel(desc1) ? dst_pix_fmt2 : dst_pix_fmt1;
  207. } else {
  208. dst_pix_fmt = desc2->nb_components < desc1->nb_components ? dst_pix_fmt2 : dst_pix_fmt1;
  209. }
  210. } else {
  211. dst_pix_fmt = score1 < score2 ? dst_pix_fmt2 : dst_pix_fmt1;
  212. }
  213. if (loss_ptr)
  214. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  215. return dst_pix_fmt;
  216. }
  217. #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
  218. enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
  219. enum AVPixelFormat src_pix_fmt,
  220. int has_alpha, int *loss_ptr){
  221. return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
  222. }
  223. #else
  224. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  225. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  226. {
  227. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  228. }
  229. #endif
  230. enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
  231. enum AVPixelFormat src_pix_fmt,
  232. int has_alpha, int *loss_ptr){
  233. int i;
  234. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  235. for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
  236. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
  237. return best;
  238. }
  239. /* 2x2 -> 1x1 */
  240. void ff_shrink22(uint8_t *dst, int dst_wrap,
  241. const uint8_t *src, int src_wrap,
  242. int width, int height)
  243. {
  244. int w;
  245. const uint8_t *s1, *s2;
  246. uint8_t *d;
  247. for(;height > 0; height--) {
  248. s1 = src;
  249. s2 = s1 + src_wrap;
  250. d = dst;
  251. for(w = width;w >= 4; w-=4) {
  252. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  253. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  254. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  255. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  256. s1 += 8;
  257. s2 += 8;
  258. d += 4;
  259. }
  260. for(;w > 0; w--) {
  261. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  262. s1 += 2;
  263. s2 += 2;
  264. d++;
  265. }
  266. src += 2 * src_wrap;
  267. dst += dst_wrap;
  268. }
  269. }
  270. /* 4x4 -> 1x1 */
  271. void ff_shrink44(uint8_t *dst, int dst_wrap,
  272. const uint8_t *src, int src_wrap,
  273. int width, int height)
  274. {
  275. int w;
  276. const uint8_t *s1, *s2, *s3, *s4;
  277. uint8_t *d;
  278. for(;height > 0; height--) {
  279. s1 = src;
  280. s2 = s1 + src_wrap;
  281. s3 = s2 + src_wrap;
  282. s4 = s3 + src_wrap;
  283. d = dst;
  284. for(w = width;w > 0; w--) {
  285. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  286. s2[0] + s2[1] + s2[2] + s2[3] +
  287. s3[0] + s3[1] + s3[2] + s3[3] +
  288. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  289. s1 += 4;
  290. s2 += 4;
  291. s3 += 4;
  292. s4 += 4;
  293. d++;
  294. }
  295. src += 4 * src_wrap;
  296. dst += dst_wrap;
  297. }
  298. }
  299. /* 8x8 -> 1x1 */
  300. void ff_shrink88(uint8_t *dst, int dst_wrap,
  301. const uint8_t *src, int src_wrap,
  302. int width, int height)
  303. {
  304. int w, i;
  305. for(;height > 0; height--) {
  306. for(w = width;w > 0; w--) {
  307. int tmp=0;
  308. for(i=0; i<8; i++){
  309. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  310. src += src_wrap;
  311. }
  312. *(dst++) = (tmp + 32)>>6;
  313. src += 8 - 8*src_wrap;
  314. }
  315. src += 8*src_wrap - 8*width;
  316. dst += dst_wrap - width;
  317. }
  318. }
  319. /* return true if yuv planar */
  320. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  321. {
  322. int i;
  323. int planes[4] = { 0 };
  324. if ( desc->flags & AV_PIX_FMT_FLAG_RGB
  325. || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
  326. return 0;
  327. /* set the used planes */
  328. for (i = 0; i < desc->nb_components; i++)
  329. planes[desc->comp[i].plane] = 1;
  330. /* if there is an unused plane, the format is not planar */
  331. for (i = 0; i < desc->nb_components; i++)
  332. if (!planes[i])
  333. return 0;
  334. return 1;
  335. }
  336. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  337. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  338. {
  339. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  340. int y_shift;
  341. int x_shift;
  342. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  343. return -1;
  344. y_shift = desc->log2_chroma_h;
  345. x_shift = desc->log2_chroma_w;
  346. if (is_yuv_planar(desc)) {
  347. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  348. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  349. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  350. } else{
  351. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  352. return -1;
  353. if(left_band) //FIXME add support for this too
  354. return -1;
  355. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  356. }
  357. dst->linesize[0] = src->linesize[0];
  358. dst->linesize[1] = src->linesize[1];
  359. dst->linesize[2] = src->linesize[2];
  360. return 0;
  361. }
  362. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  363. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  364. int *color)
  365. {
  366. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  367. uint8_t *optr;
  368. int y_shift;
  369. int x_shift;
  370. int yheight;
  371. int i, y;
  372. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  373. !is_yuv_planar(desc)) return -1;
  374. for (i = 0; i < 3; i++) {
  375. x_shift = i ? desc->log2_chroma_w : 0;
  376. y_shift = i ? desc->log2_chroma_h : 0;
  377. if (padtop || padleft) {
  378. memset(dst->data[i], color[i],
  379. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  380. }
  381. if (padleft || padright) {
  382. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  383. (dst->linesize[i] - (padright >> x_shift));
  384. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  385. for (y = 0; y < yheight; y++) {
  386. memset(optr, color[i], (padleft + padright) >> x_shift);
  387. optr += dst->linesize[i];
  388. }
  389. }
  390. if (src) { /* first line */
  391. uint8_t *iptr = src->data[i];
  392. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  393. (padleft >> x_shift);
  394. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  395. iptr += src->linesize[i];
  396. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  397. (dst->linesize[i] - (padright >> x_shift));
  398. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  399. for (y = 0; y < yheight; y++) {
  400. memset(optr, color[i], (padleft + padright) >> x_shift);
  401. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  402. (width - padleft - padright) >> x_shift);
  403. iptr += src->linesize[i];
  404. optr += dst->linesize[i];
  405. }
  406. }
  407. if (padbottom || padright) {
  408. optr = dst->data[i] + dst->linesize[i] *
  409. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  410. memset(optr, color[i],dst->linesize[i] *
  411. (padbottom >> y_shift) + (padright >> x_shift));
  412. }
  413. }
  414. return 0;
  415. }
  416. #if FF_API_DEINTERLACE
  417. #if !HAVE_MMX_EXTERNAL
  418. /* filter parameters: [-1 4 2 4 -1] // 8 */
  419. static void deinterlace_line_c(uint8_t *dst,
  420. const uint8_t *lum_m4, const uint8_t *lum_m3,
  421. const uint8_t *lum_m2, const uint8_t *lum_m1,
  422. const uint8_t *lum,
  423. int size)
  424. {
  425. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  426. int sum;
  427. for(;size > 0;size--) {
  428. sum = -lum_m4[0];
  429. sum += lum_m3[0] << 2;
  430. sum += lum_m2[0] << 1;
  431. sum += lum_m1[0] << 2;
  432. sum += -lum[0];
  433. dst[0] = cm[(sum + 4) >> 3];
  434. lum_m4++;
  435. lum_m3++;
  436. lum_m2++;
  437. lum_m1++;
  438. lum++;
  439. dst++;
  440. }
  441. }
  442. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  443. uint8_t *lum_m2, uint8_t *lum_m1,
  444. uint8_t *lum, int size)
  445. {
  446. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  447. int sum;
  448. for(;size > 0;size--) {
  449. sum = -lum_m4[0];
  450. sum += lum_m3[0] << 2;
  451. sum += lum_m2[0] << 1;
  452. lum_m4[0]=lum_m2[0];
  453. sum += lum_m1[0] << 2;
  454. sum += -lum[0];
  455. lum_m2[0] = cm[(sum + 4) >> 3];
  456. lum_m4++;
  457. lum_m3++;
  458. lum_m2++;
  459. lum_m1++;
  460. lum++;
  461. }
  462. }
  463. #endif /* !HAVE_MMX_EXTERNAL */
  464. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  465. top field is copied as is, but the bottom field is deinterlaced
  466. against the top field. */
  467. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  468. const uint8_t *src1, int src_wrap,
  469. int width, int height)
  470. {
  471. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  472. int y;
  473. src_m2 = src1;
  474. src_m1 = src1;
  475. src_0=&src_m1[src_wrap];
  476. src_p1=&src_0[src_wrap];
  477. src_p2=&src_p1[src_wrap];
  478. for(y=0;y<(height-2);y+=2) {
  479. memcpy(dst,src_m1,width);
  480. dst += dst_wrap;
  481. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  482. src_m2 = src_0;
  483. src_m1 = src_p1;
  484. src_0 = src_p2;
  485. src_p1 += 2*src_wrap;
  486. src_p2 += 2*src_wrap;
  487. dst += dst_wrap;
  488. }
  489. memcpy(dst,src_m1,width);
  490. dst += dst_wrap;
  491. /* do last line */
  492. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  493. }
  494. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  495. int width, int height)
  496. {
  497. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  498. int y;
  499. uint8_t *buf;
  500. buf = av_malloc(width);
  501. src_m1 = src1;
  502. memcpy(buf,src_m1,width);
  503. src_0=&src_m1[src_wrap];
  504. src_p1=&src_0[src_wrap];
  505. src_p2=&src_p1[src_wrap];
  506. for(y=0;y<(height-2);y+=2) {
  507. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  508. src_m1 = src_p1;
  509. src_0 = src_p2;
  510. src_p1 += 2*src_wrap;
  511. src_p2 += 2*src_wrap;
  512. }
  513. /* do last line */
  514. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  515. av_free(buf);
  516. }
  517. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  518. enum AVPixelFormat pix_fmt, int width, int height)
  519. {
  520. int i;
  521. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  522. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  523. pix_fmt != AV_PIX_FMT_YUV422P &&
  524. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  525. pix_fmt != AV_PIX_FMT_YUV444P &&
  526. pix_fmt != AV_PIX_FMT_YUV411P &&
  527. pix_fmt != AV_PIX_FMT_GRAY8)
  528. return -1;
  529. if ((width & 3) != 0 || (height & 3) != 0)
  530. return -1;
  531. for(i=0;i<3;i++) {
  532. if (i == 1) {
  533. switch(pix_fmt) {
  534. case AV_PIX_FMT_YUVJ420P:
  535. case AV_PIX_FMT_YUV420P:
  536. width >>= 1;
  537. height >>= 1;
  538. break;
  539. case AV_PIX_FMT_YUV422P:
  540. case AV_PIX_FMT_YUVJ422P:
  541. width >>= 1;
  542. break;
  543. case AV_PIX_FMT_YUV411P:
  544. width >>= 2;
  545. break;
  546. default:
  547. break;
  548. }
  549. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  550. break;
  551. }
  552. }
  553. if (src == dst) {
  554. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  555. width, height);
  556. } else {
  557. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  558. src->data[i], src->linesize[i],
  559. width, height);
  560. }
  561. }
  562. emms_c();
  563. return 0;
  564. }
  565. #endif /* FF_API_DEINTERLACE */
  566. #ifdef TEST
  567. int main(void){
  568. int i;
  569. int err=0;
  570. int skip = 0;
  571. for (i=0; i<AV_PIX_FMT_NB*2; i++) {
  572. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  573. if(!desc || !desc->name) {
  574. skip ++;
  575. continue;
  576. }
  577. if (skip) {
  578. av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
  579. skip = 0;
  580. }
  581. av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d colortype:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc), get_color_type(desc));
  582. if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
  583. av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
  584. err = 1;
  585. }
  586. }
  587. return err;
  588. }
  589. #endif