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.

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