imgconvert.c 21 KB

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