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.

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