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.

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