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.

924 lines
27KB

  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 "imgconvert.h"
  34. #include "libavutil/colorspace.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #if HAVE_MMX && HAVE_YASM
  38. #include "x86/dsputil_mmx.h"
  39. #endif
  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 && HAVE_YASM
  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. typedef struct PixFmtInfo {
  52. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  53. uint8_t is_alpha : 1; /**< true if alpha can be specified */
  54. uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
  55. } PixFmtInfo;
  56. /* this table gives more information about formats */
  57. static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  58. /* YUV formats */
  59. [PIX_FMT_YUV420P] = {
  60. .color_type = FF_COLOR_YUV,
  61. },
  62. [PIX_FMT_YUV422P] = {
  63. .color_type = FF_COLOR_YUV,
  64. },
  65. [PIX_FMT_YUV444P] = {
  66. .color_type = FF_COLOR_YUV,
  67. },
  68. [PIX_FMT_YUYV422] = {
  69. .color_type = FF_COLOR_YUV,
  70. },
  71. [PIX_FMT_UYVY422] = {
  72. .color_type = FF_COLOR_YUV,
  73. },
  74. [PIX_FMT_YUV410P] = {
  75. .color_type = FF_COLOR_YUV,
  76. },
  77. [PIX_FMT_YUV411P] = {
  78. .color_type = FF_COLOR_YUV,
  79. },
  80. [PIX_FMT_YUV440P] = {
  81. .color_type = FF_COLOR_YUV,
  82. },
  83. [PIX_FMT_YUV420P16LE] = {
  84. .color_type = FF_COLOR_YUV,
  85. },
  86. [PIX_FMT_YUV422P16LE] = {
  87. .color_type = FF_COLOR_YUV,
  88. },
  89. [PIX_FMT_YUV444P16LE] = {
  90. .color_type = FF_COLOR_YUV,
  91. },
  92. [PIX_FMT_YUV420P16BE] = {
  93. .color_type = FF_COLOR_YUV,
  94. },
  95. [PIX_FMT_YUV422P16BE] = {
  96. .color_type = FF_COLOR_YUV,
  97. },
  98. [PIX_FMT_YUV444P16BE] = {
  99. .color_type = FF_COLOR_YUV,
  100. },
  101. /* YUV formats with alpha plane */
  102. [PIX_FMT_YUVA420P] = {
  103. .color_type = FF_COLOR_YUV,
  104. },
  105. /* JPEG YUV */
  106. [PIX_FMT_YUVJ420P] = {
  107. .color_type = FF_COLOR_YUV_JPEG,
  108. },
  109. [PIX_FMT_YUVJ422P] = {
  110. .color_type = FF_COLOR_YUV_JPEG,
  111. },
  112. [PIX_FMT_YUVJ444P] = {
  113. .color_type = FF_COLOR_YUV_JPEG,
  114. },
  115. [PIX_FMT_YUVJ440P] = {
  116. .color_type = FF_COLOR_YUV_JPEG,
  117. },
  118. /* RGB formats */
  119. [PIX_FMT_RGB24] = {
  120. .color_type = FF_COLOR_RGB,
  121. },
  122. [PIX_FMT_BGR24] = {
  123. .color_type = FF_COLOR_RGB,
  124. },
  125. [PIX_FMT_ARGB] = {
  126. .is_alpha = 1,
  127. .color_type = FF_COLOR_RGB,
  128. },
  129. [PIX_FMT_RGB48BE] = {
  130. .color_type = FF_COLOR_RGB,
  131. },
  132. [PIX_FMT_RGB48LE] = {
  133. .color_type = FF_COLOR_RGB,
  134. },
  135. [PIX_FMT_RGB565BE] = {
  136. .color_type = FF_COLOR_RGB,
  137. },
  138. [PIX_FMT_RGB565LE] = {
  139. .color_type = FF_COLOR_RGB,
  140. },
  141. [PIX_FMT_RGB555BE] = {
  142. .color_type = FF_COLOR_RGB,
  143. .padded_size = 16,
  144. },
  145. [PIX_FMT_RGB555LE] = {
  146. .color_type = FF_COLOR_RGB,
  147. .padded_size = 16,
  148. },
  149. [PIX_FMT_RGB444BE] = {
  150. .color_type = FF_COLOR_RGB,
  151. .padded_size = 16,
  152. },
  153. [PIX_FMT_RGB444LE] = {
  154. .color_type = FF_COLOR_RGB,
  155. .padded_size = 16,
  156. },
  157. /* gray / mono formats */
  158. [PIX_FMT_GRAY16BE] = {
  159. .color_type = FF_COLOR_GRAY,
  160. },
  161. [PIX_FMT_GRAY16LE] = {
  162. .color_type = FF_COLOR_GRAY,
  163. },
  164. [PIX_FMT_GRAY8] = {
  165. .color_type = FF_COLOR_GRAY,
  166. },
  167. [PIX_FMT_MONOWHITE] = {
  168. .color_type = FF_COLOR_GRAY,
  169. },
  170. [PIX_FMT_MONOBLACK] = {
  171. .color_type = FF_COLOR_GRAY,
  172. },
  173. /* paletted formats */
  174. [PIX_FMT_PAL8] = {
  175. .is_alpha = 1,
  176. .color_type = FF_COLOR_RGB,
  177. },
  178. [PIX_FMT_UYYVYY411] = {
  179. .color_type = FF_COLOR_YUV,
  180. },
  181. [PIX_FMT_ABGR] = {
  182. .is_alpha = 1,
  183. .color_type = FF_COLOR_RGB,
  184. },
  185. [PIX_FMT_BGR565BE] = {
  186. .color_type = FF_COLOR_RGB,
  187. .padded_size = 16,
  188. },
  189. [PIX_FMT_BGR565LE] = {
  190. .color_type = FF_COLOR_RGB,
  191. .padded_size = 16,
  192. },
  193. [PIX_FMT_BGR555BE] = {
  194. .color_type = FF_COLOR_RGB,
  195. .padded_size = 16,
  196. },
  197. [PIX_FMT_BGR555LE] = {
  198. .color_type = FF_COLOR_RGB,
  199. .padded_size = 16,
  200. },
  201. [PIX_FMT_BGR444BE] = {
  202. .color_type = FF_COLOR_RGB,
  203. .padded_size = 16,
  204. },
  205. [PIX_FMT_BGR444LE] = {
  206. .color_type = FF_COLOR_RGB,
  207. .padded_size = 16,
  208. },
  209. [PIX_FMT_RGB8] = {
  210. .color_type = FF_COLOR_RGB,
  211. },
  212. [PIX_FMT_RGB4] = {
  213. .color_type = FF_COLOR_RGB,
  214. },
  215. [PIX_FMT_RGB4_BYTE] = {
  216. .color_type = FF_COLOR_RGB,
  217. .padded_size = 8,
  218. },
  219. [PIX_FMT_BGR8] = {
  220. .color_type = FF_COLOR_RGB,
  221. },
  222. [PIX_FMT_BGR4] = {
  223. .color_type = FF_COLOR_RGB,
  224. },
  225. [PIX_FMT_BGR4_BYTE] = {
  226. .color_type = FF_COLOR_RGB,
  227. .padded_size = 8,
  228. },
  229. [PIX_FMT_NV12] = {
  230. .color_type = FF_COLOR_YUV,
  231. },
  232. [PIX_FMT_NV21] = {
  233. .color_type = FF_COLOR_YUV,
  234. },
  235. [PIX_FMT_BGRA] = {
  236. .is_alpha = 1,
  237. .color_type = FF_COLOR_RGB,
  238. },
  239. [PIX_FMT_RGBA] = {
  240. .is_alpha = 1,
  241. .color_type = FF_COLOR_RGB,
  242. },
  243. };
  244. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  245. {
  246. *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  247. *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  248. }
  249. #if FF_API_GET_PIX_FMT_NAME
  250. const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
  251. {
  252. return av_get_pix_fmt_name(pix_fmt);
  253. }
  254. #endif
  255. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  256. {
  257. return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
  258. }
  259. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  260. enum PixelFormat pix_fmt, int width, int height)
  261. {
  262. int ret;
  263. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  264. return ret;
  265. if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
  266. return ret;
  267. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  268. }
  269. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  270. unsigned char *dest, int dest_size)
  271. {
  272. int i, j, nb_planes = 0, linesizes[4];
  273. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  274. int size = avpicture_get_size(pix_fmt, width, height);
  275. if (size > dest_size || size < 0)
  276. return AVERROR(EINVAL);
  277. for (i = 0; i < desc->nb_components; i++)
  278. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  279. nb_planes++;
  280. av_image_fill_linesizes(linesizes, pix_fmt, width);
  281. for (i = 0; i < nb_planes; i++) {
  282. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  283. const unsigned char *s = src->data[i];
  284. h = (height + (1 << shift) - 1) >> shift;
  285. for (j = 0; j < h; j++) {
  286. memcpy(dest, s, linesizes[i]);
  287. dest += linesizes[i];
  288. s += src->linesize[i];
  289. }
  290. }
  291. if (desc->flags & PIX_FMT_PAL)
  292. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  293. return size;
  294. }
  295. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  296. {
  297. AVPicture dummy_pict;
  298. if(av_image_check_size(width, height, 0, NULL))
  299. return -1;
  300. switch (pix_fmt) {
  301. case PIX_FMT_RGB8:
  302. case PIX_FMT_BGR8:
  303. case PIX_FMT_RGB4_BYTE:
  304. case PIX_FMT_BGR4_BYTE:
  305. case PIX_FMT_GRAY8:
  306. // do not include palette for these pseudo-paletted formats
  307. return width * height;
  308. }
  309. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  310. }
  311. static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
  312. {
  313. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  314. int i;
  315. if (!desc->nb_components) {
  316. *min = *max = 0;
  317. return AVERROR(EINVAL);
  318. }
  319. *min = INT_MAX, *max = -INT_MAX;
  320. for (i = 0; i < desc->nb_components; i++) {
  321. *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
  322. *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
  323. }
  324. return 0;
  325. }
  326. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  327. int has_alpha)
  328. {
  329. const PixFmtInfo *pf, *ps;
  330. const AVPixFmtDescriptor *src_desc;
  331. const AVPixFmtDescriptor *dst_desc;
  332. int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
  333. int ret, loss;
  334. if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
  335. return ~0;
  336. src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  337. dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  338. ps = &pix_fmt_info[src_pix_fmt];
  339. /* compute loss */
  340. loss = 0;
  341. if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
  342. return ret;
  343. if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
  344. return ret;
  345. if (dst_min_depth < src_min_depth ||
  346. dst_max_depth < src_max_depth)
  347. loss |= FF_LOSS_DEPTH;
  348. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  349. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  350. loss |= FF_LOSS_RESOLUTION;
  351. pf = &pix_fmt_info[dst_pix_fmt];
  352. switch(pf->color_type) {
  353. case FF_COLOR_RGB:
  354. if (ps->color_type != FF_COLOR_RGB &&
  355. ps->color_type != FF_COLOR_GRAY)
  356. loss |= FF_LOSS_COLORSPACE;
  357. break;
  358. case FF_COLOR_GRAY:
  359. if (ps->color_type != FF_COLOR_GRAY)
  360. loss |= FF_LOSS_COLORSPACE;
  361. break;
  362. case FF_COLOR_YUV:
  363. if (ps->color_type != FF_COLOR_YUV)
  364. loss |= FF_LOSS_COLORSPACE;
  365. break;
  366. case FF_COLOR_YUV_JPEG:
  367. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  368. ps->color_type != FF_COLOR_YUV &&
  369. ps->color_type != FF_COLOR_GRAY)
  370. loss |= FF_LOSS_COLORSPACE;
  371. break;
  372. default:
  373. /* fail safe test */
  374. if (ps->color_type != pf->color_type)
  375. loss |= FF_LOSS_COLORSPACE;
  376. break;
  377. }
  378. if (pf->color_type == FF_COLOR_GRAY &&
  379. ps->color_type != FF_COLOR_GRAY)
  380. loss |= FF_LOSS_CHROMA;
  381. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  382. loss |= FF_LOSS_ALPHA;
  383. if (dst_pix_fmt == PIX_FMT_PAL8 &&
  384. (src_pix_fmt != PIX_FMT_PAL8 && ps->color_type != FF_COLOR_GRAY))
  385. loss |= FF_LOSS_COLORQUANT;
  386. return loss;
  387. }
  388. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  389. {
  390. const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
  391. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  392. return info->padded_size ?
  393. info->padded_size : av_get_bits_per_pixel(desc);
  394. }
  395. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  396. int has_alpha, int *loss_ptr)
  397. {
  398. enum PixelFormat dst_pix_fmt;
  399. int i;
  400. if (loss_ptr) /* all losses count (for backward compatibility) */
  401. *loss_ptr = 0;
  402. dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
  403. for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
  404. if (pix_fmt_mask & (1ULL << i))
  405. dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
  406. }
  407. return dst_pix_fmt;
  408. }
  409. enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
  410. enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  411. {
  412. enum PixelFormat dst_pix_fmt;
  413. int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
  414. static const int loss_mask_order[] = {
  415. ~0, /* no loss first */
  416. ~FF_LOSS_ALPHA,
  417. ~FF_LOSS_RESOLUTION,
  418. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  419. ~FF_LOSS_COLORQUANT,
  420. ~FF_LOSS_DEPTH,
  421. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  422. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  423. 0x80000, //non zero entry that combines all loss variants including future additions
  424. 0,
  425. };
  426. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  427. dst_pix_fmt = PIX_FMT_NONE;
  428. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  429. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  430. /* try with successive loss */
  431. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
  432. loss_order1 = loss1 & loss_mask_order[i];
  433. loss_order2 = loss2 & loss_mask_order[i];
  434. if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
  435. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  436. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  437. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  438. }
  439. }
  440. if (loss_ptr)
  441. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  442. return dst_pix_fmt;
  443. }
  444. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  445. enum PixelFormat pix_fmt, int width, int height)
  446. {
  447. av_image_copy(dst->data, dst->linesize, src->data,
  448. src->linesize, pix_fmt, width, height);
  449. }
  450. /* 2x2 -> 1x1 */
  451. void ff_shrink22(uint8_t *dst, int dst_wrap,
  452. const uint8_t *src, int src_wrap,
  453. int width, int height)
  454. {
  455. int w;
  456. const uint8_t *s1, *s2;
  457. uint8_t *d;
  458. for(;height > 0; height--) {
  459. s1 = src;
  460. s2 = s1 + src_wrap;
  461. d = dst;
  462. for(w = width;w >= 4; w-=4) {
  463. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  464. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  465. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  466. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  467. s1 += 8;
  468. s2 += 8;
  469. d += 4;
  470. }
  471. for(;w > 0; w--) {
  472. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  473. s1 += 2;
  474. s2 += 2;
  475. d++;
  476. }
  477. src += 2 * src_wrap;
  478. dst += dst_wrap;
  479. }
  480. }
  481. /* 4x4 -> 1x1 */
  482. void ff_shrink44(uint8_t *dst, int dst_wrap,
  483. const uint8_t *src, int src_wrap,
  484. int width, int height)
  485. {
  486. int w;
  487. const uint8_t *s1, *s2, *s3, *s4;
  488. uint8_t *d;
  489. for(;height > 0; height--) {
  490. s1 = src;
  491. s2 = s1 + src_wrap;
  492. s3 = s2 + src_wrap;
  493. s4 = s3 + src_wrap;
  494. d = dst;
  495. for(w = width;w > 0; w--) {
  496. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  497. s2[0] + s2[1] + s2[2] + s2[3] +
  498. s3[0] + s3[1] + s3[2] + s3[3] +
  499. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  500. s1 += 4;
  501. s2 += 4;
  502. s3 += 4;
  503. s4 += 4;
  504. d++;
  505. }
  506. src += 4 * src_wrap;
  507. dst += dst_wrap;
  508. }
  509. }
  510. /* 8x8 -> 1x1 */
  511. void ff_shrink88(uint8_t *dst, int dst_wrap,
  512. const uint8_t *src, int src_wrap,
  513. int width, int height)
  514. {
  515. int w, i;
  516. for(;height > 0; height--) {
  517. for(w = width;w > 0; w--) {
  518. int tmp=0;
  519. for(i=0; i<8; i++){
  520. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  521. src += src_wrap;
  522. }
  523. *(dst++) = (tmp + 32)>>6;
  524. src += 8 - 8*src_wrap;
  525. }
  526. src += 8*src_wrap - 8*width;
  527. dst += dst_wrap - width;
  528. }
  529. }
  530. int avpicture_alloc(AVPicture *picture,
  531. enum PixelFormat pix_fmt, int width, int height)
  532. {
  533. int ret;
  534. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  535. memset(picture, 0, sizeof(AVPicture));
  536. return ret;
  537. }
  538. return 0;
  539. }
  540. void avpicture_free(AVPicture *picture)
  541. {
  542. av_free(picture->data[0]);
  543. }
  544. /* return true if yuv planar */
  545. static inline int is_yuv_planar(enum PixelFormat fmt)
  546. {
  547. const PixFmtInfo *info = &pix_fmt_info[fmt];
  548. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
  549. int i;
  550. int planes[4] = { 0 };
  551. if (info->color_type != FF_COLOR_YUV &&
  552. info->color_type != FF_COLOR_YUV_JPEG)
  553. return 0;
  554. /* set the used planes */
  555. for (i = 0; i < desc->nb_components; i++)
  556. planes[desc->comp[i].plane] = 1;
  557. /* if there is an unused plane, the format is not planar */
  558. for (i = 0; i < desc->nb_components; i++)
  559. if (!planes[i])
  560. return 0;
  561. return 1;
  562. }
  563. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  564. enum PixelFormat pix_fmt, int top_band, int left_band)
  565. {
  566. int y_shift;
  567. int x_shift;
  568. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  569. return -1;
  570. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  571. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  572. if (is_yuv_planar(pix_fmt)) {
  573. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  574. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  575. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  576. } else{
  577. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  578. return -1;
  579. if(left_band) //FIXME add support for this too
  580. return -1;
  581. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  582. }
  583. dst->linesize[0] = src->linesize[0];
  584. dst->linesize[1] = src->linesize[1];
  585. dst->linesize[2] = src->linesize[2];
  586. return 0;
  587. }
  588. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  589. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  590. int *color)
  591. {
  592. uint8_t *optr;
  593. int y_shift;
  594. int x_shift;
  595. int yheight;
  596. int i, y;
  597. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  598. !is_yuv_planar(pix_fmt)) return -1;
  599. for (i = 0; i < 3; i++) {
  600. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  601. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  602. if (padtop || padleft) {
  603. memset(dst->data[i], color[i],
  604. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  605. }
  606. if (padleft || padright) {
  607. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  608. (dst->linesize[i] - (padright >> x_shift));
  609. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  610. for (y = 0; y < yheight; y++) {
  611. memset(optr, color[i], (padleft + padright) >> x_shift);
  612. optr += dst->linesize[i];
  613. }
  614. }
  615. if (src) { /* first line */
  616. uint8_t *iptr = src->data[i];
  617. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  618. (padleft >> x_shift);
  619. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  620. iptr += src->linesize[i];
  621. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  622. (dst->linesize[i] - (padright >> x_shift));
  623. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  624. for (y = 0; y < yheight; y++) {
  625. memset(optr, color[i], (padleft + padright) >> x_shift);
  626. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  627. (width - padleft - padright) >> x_shift);
  628. iptr += src->linesize[i];
  629. optr += dst->linesize[i];
  630. }
  631. }
  632. if (padbottom || padright) {
  633. optr = dst->data[i] + dst->linesize[i] *
  634. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  635. memset(optr, color[i],dst->linesize[i] *
  636. (padbottom >> y_shift) + (padright >> x_shift));
  637. }
  638. }
  639. return 0;
  640. }
  641. /* NOTE: we scan all the pixels to have an exact information */
  642. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  643. {
  644. const unsigned char *p;
  645. int src_wrap, ret, x, y;
  646. unsigned int a;
  647. uint32_t *palette = (uint32_t *)src->data[1];
  648. p = src->data[0];
  649. src_wrap = src->linesize[0] - width;
  650. ret = 0;
  651. for(y=0;y<height;y++) {
  652. for(x=0;x<width;x++) {
  653. a = palette[p[0]] >> 24;
  654. if (a == 0x00) {
  655. ret |= FF_ALPHA_TRANSP;
  656. } else if (a != 0xff) {
  657. ret |= FF_ALPHA_SEMI_TRANSP;
  658. }
  659. p++;
  660. }
  661. p += src_wrap;
  662. }
  663. return ret;
  664. }
  665. int img_get_alpha_info(const AVPicture *src,
  666. enum PixelFormat pix_fmt, int width, int height)
  667. {
  668. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  669. int ret;
  670. /* no alpha can be represented in format */
  671. if (!pf->is_alpha)
  672. return 0;
  673. switch(pix_fmt) {
  674. case PIX_FMT_PAL8:
  675. ret = get_alpha_info_pal8(src, width, height);
  676. break;
  677. default:
  678. /* we do not know, so everything is indicated */
  679. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  680. break;
  681. }
  682. return ret;
  683. }
  684. #if !(HAVE_MMX && HAVE_YASM)
  685. /* filter parameters: [-1 4 2 4 -1] // 8 */
  686. static void deinterlace_line_c(uint8_t *dst,
  687. const uint8_t *lum_m4, const uint8_t *lum_m3,
  688. const uint8_t *lum_m2, const uint8_t *lum_m1,
  689. const uint8_t *lum,
  690. int size)
  691. {
  692. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  693. int sum;
  694. for(;size > 0;size--) {
  695. sum = -lum_m4[0];
  696. sum += lum_m3[0] << 2;
  697. sum += lum_m2[0] << 1;
  698. sum += lum_m1[0] << 2;
  699. sum += -lum[0];
  700. dst[0] = cm[(sum + 4) >> 3];
  701. lum_m4++;
  702. lum_m3++;
  703. lum_m2++;
  704. lum_m1++;
  705. lum++;
  706. dst++;
  707. }
  708. }
  709. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  710. uint8_t *lum_m2, uint8_t *lum_m1,
  711. uint8_t *lum, int size)
  712. {
  713. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  714. int sum;
  715. for(;size > 0;size--) {
  716. sum = -lum_m4[0];
  717. sum += lum_m3[0] << 2;
  718. sum += lum_m2[0] << 1;
  719. lum_m4[0]=lum_m2[0];
  720. sum += lum_m1[0] << 2;
  721. sum += -lum[0];
  722. lum_m2[0] = cm[(sum + 4) >> 3];
  723. lum_m4++;
  724. lum_m3++;
  725. lum_m2++;
  726. lum_m1++;
  727. lum++;
  728. }
  729. }
  730. #endif
  731. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  732. top field is copied as is, but the bottom field is deinterlaced
  733. against the top field. */
  734. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  735. const uint8_t *src1, int src_wrap,
  736. int width, int height)
  737. {
  738. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  739. int y;
  740. src_m2 = src1;
  741. src_m1 = src1;
  742. src_0=&src_m1[src_wrap];
  743. src_p1=&src_0[src_wrap];
  744. src_p2=&src_p1[src_wrap];
  745. for(y=0;y<(height-2);y+=2) {
  746. memcpy(dst,src_m1,width);
  747. dst += dst_wrap;
  748. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  749. src_m2 = src_0;
  750. src_m1 = src_p1;
  751. src_0 = src_p2;
  752. src_p1 += 2*src_wrap;
  753. src_p2 += 2*src_wrap;
  754. dst += dst_wrap;
  755. }
  756. memcpy(dst,src_m1,width);
  757. dst += dst_wrap;
  758. /* do last line */
  759. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  760. }
  761. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  762. int width, int height)
  763. {
  764. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  765. int y;
  766. uint8_t *buf;
  767. buf = (uint8_t*)av_malloc(width);
  768. src_m1 = src1;
  769. memcpy(buf,src_m1,width);
  770. src_0=&src_m1[src_wrap];
  771. src_p1=&src_0[src_wrap];
  772. src_p2=&src_p1[src_wrap];
  773. for(y=0;y<(height-2);y+=2) {
  774. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  775. src_m1 = src_p1;
  776. src_0 = src_p2;
  777. src_p1 += 2*src_wrap;
  778. src_p2 += 2*src_wrap;
  779. }
  780. /* do last line */
  781. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  782. av_free(buf);
  783. }
  784. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  785. enum PixelFormat pix_fmt, int width, int height)
  786. {
  787. int i;
  788. if (pix_fmt != PIX_FMT_YUV420P &&
  789. pix_fmt != PIX_FMT_YUVJ420P &&
  790. pix_fmt != PIX_FMT_YUV422P &&
  791. pix_fmt != PIX_FMT_YUVJ422P &&
  792. pix_fmt != PIX_FMT_YUV444P &&
  793. pix_fmt != PIX_FMT_YUV411P &&
  794. pix_fmt != PIX_FMT_GRAY8)
  795. return -1;
  796. if ((width & 3) != 0 || (height & 3) != 0)
  797. return -1;
  798. for(i=0;i<3;i++) {
  799. if (i == 1) {
  800. switch(pix_fmt) {
  801. case PIX_FMT_YUVJ420P:
  802. case PIX_FMT_YUV420P:
  803. width >>= 1;
  804. height >>= 1;
  805. break;
  806. case PIX_FMT_YUV422P:
  807. case PIX_FMT_YUVJ422P:
  808. width >>= 1;
  809. break;
  810. case PIX_FMT_YUV411P:
  811. width >>= 2;
  812. break;
  813. default:
  814. break;
  815. }
  816. if (pix_fmt == PIX_FMT_GRAY8) {
  817. break;
  818. }
  819. }
  820. if (src == dst) {
  821. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  822. width, height);
  823. } else {
  824. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  825. src->data[i], src->linesize[i],
  826. width, height);
  827. }
  828. }
  829. emms_c();
  830. return 0;
  831. }