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.

530 lines
16KB

  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. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  40. {
  41. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  42. av_assert0(desc);
  43. *h_shift = desc->log2_chroma_w;
  44. *v_shift = desc->log2_chroma_h;
  45. }
  46. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  47. enum AVPixelFormat src_pix_fmt,
  48. int has_alpha)
  49. {
  50. return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  51. }
  52. enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  53. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  54. {
  55. return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  56. }
  57. #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
  58. enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
  59. enum AVPixelFormat src_pix_fmt,
  60. int has_alpha, int *loss_ptr){
  61. return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
  62. }
  63. #else
  64. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  65. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  66. {
  67. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  68. }
  69. #endif
  70. enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
  71. enum AVPixelFormat src_pix_fmt,
  72. int has_alpha, int *loss_ptr){
  73. int i;
  74. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  75. int loss;
  76. for (i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++) {
  77. loss = loss_ptr ? *loss_ptr : 0;
  78. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, &loss);
  79. }
  80. if (loss_ptr)
  81. *loss_ptr = loss;
  82. return best;
  83. }
  84. /* 2x2 -> 1x1 */
  85. void ff_shrink22(uint8_t *dst, int dst_wrap,
  86. const uint8_t *src, int src_wrap,
  87. int width, int height)
  88. {
  89. int w;
  90. const uint8_t *s1, *s2;
  91. uint8_t *d;
  92. for(;height > 0; height--) {
  93. s1 = src;
  94. s2 = s1 + src_wrap;
  95. d = dst;
  96. for(w = width;w >= 4; w-=4) {
  97. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  98. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  99. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  100. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  101. s1 += 8;
  102. s2 += 8;
  103. d += 4;
  104. }
  105. for(;w > 0; w--) {
  106. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  107. s1 += 2;
  108. s2 += 2;
  109. d++;
  110. }
  111. src += 2 * src_wrap;
  112. dst += dst_wrap;
  113. }
  114. }
  115. /* 4x4 -> 1x1 */
  116. void ff_shrink44(uint8_t *dst, int dst_wrap,
  117. const uint8_t *src, int src_wrap,
  118. int width, int height)
  119. {
  120. int w;
  121. const uint8_t *s1, *s2, *s3, *s4;
  122. uint8_t *d;
  123. for(;height > 0; height--) {
  124. s1 = src;
  125. s2 = s1 + src_wrap;
  126. s3 = s2 + src_wrap;
  127. s4 = s3 + src_wrap;
  128. d = dst;
  129. for(w = width;w > 0; w--) {
  130. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  131. s2[0] + s2[1] + s2[2] + s2[3] +
  132. s3[0] + s3[1] + s3[2] + s3[3] +
  133. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  134. s1 += 4;
  135. s2 += 4;
  136. s3 += 4;
  137. s4 += 4;
  138. d++;
  139. }
  140. src += 4 * src_wrap;
  141. dst += dst_wrap;
  142. }
  143. }
  144. /* 8x8 -> 1x1 */
  145. void ff_shrink88(uint8_t *dst, int dst_wrap,
  146. const uint8_t *src, int src_wrap,
  147. int width, int height)
  148. {
  149. int w, i;
  150. for(;height > 0; height--) {
  151. for(w = width;w > 0; w--) {
  152. int tmp=0;
  153. for(i=0; i<8; i++){
  154. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  155. src += src_wrap;
  156. }
  157. *(dst++) = (tmp + 32)>>6;
  158. src += 8 - 8*src_wrap;
  159. }
  160. src += 8*src_wrap - 8*width;
  161. dst += dst_wrap - width;
  162. }
  163. }
  164. /* return true if yuv planar */
  165. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  166. {
  167. int i;
  168. int planes[4] = { 0 };
  169. if ( desc->flags & AV_PIX_FMT_FLAG_RGB
  170. || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
  171. return 0;
  172. /* set the used planes */
  173. for (i = 0; i < desc->nb_components; i++)
  174. planes[desc->comp[i].plane] = 1;
  175. /* if there is an unused plane, the format is not planar */
  176. for (i = 0; i < desc->nb_components; i++)
  177. if (!planes[i])
  178. return 0;
  179. return 1;
  180. }
  181. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  182. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  183. {
  184. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  185. int y_shift;
  186. int x_shift;
  187. int max_step[4];
  188. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  189. return -1;
  190. y_shift = desc->log2_chroma_h;
  191. x_shift = desc->log2_chroma_w;
  192. av_image_fill_max_pixsteps(max_step, NULL, desc);
  193. if (is_yuv_planar(desc)) {
  194. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  195. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  196. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  197. } else{
  198. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  199. return -1;
  200. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + (left_band * max_step[0]);
  201. }
  202. dst->linesize[0] = src->linesize[0];
  203. dst->linesize[1] = src->linesize[1];
  204. dst->linesize[2] = src->linesize[2];
  205. return 0;
  206. }
  207. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  208. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  209. int *color)
  210. {
  211. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  212. uint8_t *optr;
  213. int y_shift;
  214. int x_shift;
  215. int yheight;
  216. int i, y;
  217. int max_step[4];
  218. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  219. return -1;
  220. if (!is_yuv_planar(desc)) {
  221. if (src)
  222. return -1; //TODO: Not yet implemented
  223. av_image_fill_max_pixsteps(max_step, NULL, desc);
  224. if (padtop || padleft) {
  225. memset(dst->data[0], color[0],
  226. dst->linesize[0] * padtop + (padleft * max_step[0]));
  227. }
  228. if (padleft || padright) {
  229. optr = dst->data[0] + dst->linesize[0] * padtop +
  230. (dst->linesize[0] - (padright * max_step[0]));
  231. yheight = height - 1 - (padtop + padbottom);
  232. for (y = 0; y < yheight; y++) {
  233. memset(optr, color[0], (padleft + padright) * max_step[0]);
  234. optr += dst->linesize[0];
  235. }
  236. }
  237. if (padbottom || padright) {
  238. optr = dst->data[0] + dst->linesize[0] * (height - padbottom) -
  239. (padright * max_step[0]);
  240. memset(optr, color[0], dst->linesize[0] * padbottom +
  241. (padright * max_step[0]));
  242. }
  243. return 0;
  244. }
  245. for (i = 0; i < 3; i++) {
  246. x_shift = i ? desc->log2_chroma_w : 0;
  247. y_shift = i ? desc->log2_chroma_h : 0;
  248. if (padtop || padleft) {
  249. memset(dst->data[i], color[i],
  250. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  251. }
  252. if (padleft || padright) {
  253. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  254. (dst->linesize[i] - (padright >> x_shift));
  255. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  256. for (y = 0; y < yheight; y++) {
  257. memset(optr, color[i], (padleft + padright) >> x_shift);
  258. optr += dst->linesize[i];
  259. }
  260. }
  261. if (src) { /* first line */
  262. uint8_t *iptr = src->data[i];
  263. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  264. (padleft >> x_shift);
  265. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  266. iptr += src->linesize[i];
  267. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  268. (dst->linesize[i] - (padright >> x_shift));
  269. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  270. for (y = 0; y < yheight; y++) {
  271. memset(optr, color[i], (padleft + padright) >> x_shift);
  272. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  273. (width - padleft - padright) >> x_shift);
  274. iptr += src->linesize[i];
  275. optr += dst->linesize[i];
  276. }
  277. }
  278. if (padbottom || padright) {
  279. optr = dst->data[i] + dst->linesize[i] *
  280. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  281. memset(optr, color[i],dst->linesize[i] *
  282. (padbottom >> y_shift) + (padright >> x_shift));
  283. }
  284. }
  285. return 0;
  286. }
  287. #if FF_API_DEINTERLACE
  288. #if HAVE_MMX_EXTERNAL
  289. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  290. #define deinterlace_line ff_deinterlace_line_mmx
  291. #else
  292. #define deinterlace_line_inplace deinterlace_line_inplace_c
  293. #define deinterlace_line deinterlace_line_c
  294. /* filter parameters: [-1 4 2 4 -1] // 8 */
  295. static void deinterlace_line_c(uint8_t *dst,
  296. const uint8_t *lum_m4, const uint8_t *lum_m3,
  297. const uint8_t *lum_m2, const uint8_t *lum_m1,
  298. const uint8_t *lum,
  299. int size)
  300. {
  301. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  302. int sum;
  303. for(;size > 0;size--) {
  304. sum = -lum_m4[0];
  305. sum += lum_m3[0] << 2;
  306. sum += lum_m2[0] << 1;
  307. sum += lum_m1[0] << 2;
  308. sum += -lum[0];
  309. dst[0] = cm[(sum + 4) >> 3];
  310. lum_m4++;
  311. lum_m3++;
  312. lum_m2++;
  313. lum_m1++;
  314. lum++;
  315. dst++;
  316. }
  317. }
  318. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  319. uint8_t *lum_m2, uint8_t *lum_m1,
  320. uint8_t *lum, int size)
  321. {
  322. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  323. int sum;
  324. for(;size > 0;size--) {
  325. sum = -lum_m4[0];
  326. sum += lum_m3[0] << 2;
  327. sum += lum_m2[0] << 1;
  328. lum_m4[0]=lum_m2[0];
  329. sum += lum_m1[0] << 2;
  330. sum += -lum[0];
  331. lum_m2[0] = cm[(sum + 4) >> 3];
  332. lum_m4++;
  333. lum_m3++;
  334. lum_m2++;
  335. lum_m1++;
  336. lum++;
  337. }
  338. }
  339. #endif /* !HAVE_MMX_EXTERNAL */
  340. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  341. top field is copied as is, but the bottom field is deinterlaced
  342. against the top field. */
  343. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  344. const uint8_t *src1, int src_wrap,
  345. int width, int height)
  346. {
  347. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  348. int y;
  349. src_m2 = src1;
  350. src_m1 = src1;
  351. src_0=&src_m1[src_wrap];
  352. src_p1=&src_0[src_wrap];
  353. src_p2=&src_p1[src_wrap];
  354. for(y=0;y<(height-2);y+=2) {
  355. memcpy(dst,src_m1,width);
  356. dst += dst_wrap;
  357. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  358. src_m2 = src_0;
  359. src_m1 = src_p1;
  360. src_0 = src_p2;
  361. src_p1 += 2*src_wrap;
  362. src_p2 += 2*src_wrap;
  363. dst += dst_wrap;
  364. }
  365. memcpy(dst,src_m1,width);
  366. dst += dst_wrap;
  367. /* do last line */
  368. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  369. }
  370. static int deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  371. int width, int height)
  372. {
  373. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  374. int y;
  375. uint8_t *buf;
  376. buf = av_malloc(width);
  377. if (!buf)
  378. return AVERROR(ENOMEM);
  379. src_m1 = src1;
  380. memcpy(buf,src_m1,width);
  381. src_0=&src_m1[src_wrap];
  382. src_p1=&src_0[src_wrap];
  383. src_p2=&src_p1[src_wrap];
  384. for(y=0;y<(height-2);y+=2) {
  385. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  386. src_m1 = src_p1;
  387. src_0 = src_p2;
  388. src_p1 += 2*src_wrap;
  389. src_p2 += 2*src_wrap;
  390. }
  391. /* do last line */
  392. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  393. av_free(buf);
  394. return 0;
  395. }
  396. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  397. enum AVPixelFormat pix_fmt, int width, int height)
  398. {
  399. int i, ret;
  400. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  401. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  402. pix_fmt != AV_PIX_FMT_YUV422P &&
  403. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  404. pix_fmt != AV_PIX_FMT_YUV444P &&
  405. pix_fmt != AV_PIX_FMT_YUV411P &&
  406. pix_fmt != AV_PIX_FMT_GRAY8)
  407. return -1;
  408. if ((width & 3) != 0 || (height & 3) != 0)
  409. return -1;
  410. for(i=0;i<3;i++) {
  411. if (i == 1) {
  412. switch(pix_fmt) {
  413. case AV_PIX_FMT_YUVJ420P:
  414. case AV_PIX_FMT_YUV420P:
  415. width >>= 1;
  416. height >>= 1;
  417. break;
  418. case AV_PIX_FMT_YUV422P:
  419. case AV_PIX_FMT_YUVJ422P:
  420. width >>= 1;
  421. break;
  422. case AV_PIX_FMT_YUV411P:
  423. width >>= 2;
  424. break;
  425. default:
  426. break;
  427. }
  428. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  429. break;
  430. }
  431. }
  432. if (src == dst) {
  433. ret = deinterlace_bottom_field_inplace(dst->data[i],
  434. dst->linesize[i],
  435. width, height);
  436. if (ret < 0)
  437. return ret;
  438. } else {
  439. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  440. src->data[i], src->linesize[i],
  441. width, height);
  442. }
  443. }
  444. emms_c();
  445. return 0;
  446. }
  447. #endif /* FF_API_DEINTERLACE */
  448. #ifdef TEST
  449. int main(void){
  450. int i;
  451. int err=0;
  452. int skip = 0;
  453. for (i=0; i<AV_PIX_FMT_NB*2; i++) {
  454. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  455. if(!desc || !desc->name) {
  456. skip ++;
  457. continue;
  458. }
  459. if (skip) {
  460. av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
  461. skip = 0;
  462. }
  463. av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc));
  464. if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
  465. av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
  466. err = 1;
  467. }
  468. }
  469. return err;
  470. }
  471. #endif