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.

922 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. if (dst_pix_fmt>=PIX_FMT_NB || dst_pix_fmt<=PIX_FMT_NONE)
  330. return ~0;
  331. const PixFmtInfo *pf, *ps;
  332. const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  333. const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  334. int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
  335. int ret, loss;
  336. ps = &pix_fmt_info[src_pix_fmt];
  337. /* compute loss */
  338. loss = 0;
  339. if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
  340. return ret;
  341. if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
  342. return ret;
  343. if (dst_min_depth < src_min_depth ||
  344. dst_max_depth < src_max_depth)
  345. loss |= FF_LOSS_DEPTH;
  346. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  347. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  348. loss |= FF_LOSS_RESOLUTION;
  349. pf = &pix_fmt_info[dst_pix_fmt];
  350. switch(pf->color_type) {
  351. case FF_COLOR_RGB:
  352. if (ps->color_type != FF_COLOR_RGB &&
  353. ps->color_type != FF_COLOR_GRAY)
  354. loss |= FF_LOSS_COLORSPACE;
  355. break;
  356. case FF_COLOR_GRAY:
  357. if (ps->color_type != FF_COLOR_GRAY)
  358. loss |= FF_LOSS_COLORSPACE;
  359. break;
  360. case FF_COLOR_YUV:
  361. if (ps->color_type != FF_COLOR_YUV)
  362. loss |= FF_LOSS_COLORSPACE;
  363. break;
  364. case FF_COLOR_YUV_JPEG:
  365. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  366. ps->color_type != FF_COLOR_YUV &&
  367. ps->color_type != FF_COLOR_GRAY)
  368. loss |= FF_LOSS_COLORSPACE;
  369. break;
  370. default:
  371. /* fail safe test */
  372. if (ps->color_type != pf->color_type)
  373. loss |= FF_LOSS_COLORSPACE;
  374. break;
  375. }
  376. if (pf->color_type == FF_COLOR_GRAY &&
  377. ps->color_type != FF_COLOR_GRAY)
  378. loss |= FF_LOSS_CHROMA;
  379. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  380. loss |= FF_LOSS_ALPHA;
  381. if (dst_pix_fmt == PIX_FMT_PAL8 &&
  382. (src_pix_fmt != PIX_FMT_PAL8 && ps->color_type != FF_COLOR_GRAY))
  383. loss |= FF_LOSS_COLORQUANT;
  384. return loss;
  385. }
  386. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  387. {
  388. const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
  389. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  390. return info->padded_size ?
  391. info->padded_size : av_get_bits_per_pixel(desc);
  392. }
  393. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  394. int has_alpha, int *loss_ptr)
  395. {
  396. enum PixelFormat dst_pix_fmt;
  397. int i;
  398. if (loss_ptr) /* all losses count (for backward compatibility) */
  399. *loss_ptr = 0;
  400. dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
  401. for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
  402. if (pix_fmt_mask & (1ULL << i))
  403. dst_pix_fmt = avcodec_find_best_pix_fmt2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
  404. }
  405. return dst_pix_fmt;
  406. }
  407. enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
  408. enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  409. {
  410. enum PixelFormat dst_pix_fmt;
  411. int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
  412. static const int loss_mask_order[] = {
  413. ~0, /* no loss first */
  414. ~FF_LOSS_ALPHA,
  415. ~FF_LOSS_RESOLUTION,
  416. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  417. ~FF_LOSS_COLORQUANT,
  418. ~FF_LOSS_DEPTH,
  419. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  420. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  421. 0x80000, //non zero entry that combines all loss variants including future additions
  422. 0,
  423. };
  424. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  425. dst_pix_fmt = PIX_FMT_NONE;
  426. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  427. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  428. /* try with successive loss */
  429. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
  430. loss_order1 = loss1 & loss_mask_order[i];
  431. loss_order2 = loss2 & loss_mask_order[i];
  432. if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
  433. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  434. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  435. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  436. }
  437. }
  438. if (loss_ptr)
  439. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  440. return dst_pix_fmt;
  441. }
  442. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  443. enum PixelFormat pix_fmt, int width, int height)
  444. {
  445. av_image_copy(dst->data, dst->linesize, src->data,
  446. src->linesize, pix_fmt, width, height);
  447. }
  448. /* 2x2 -> 1x1 */
  449. void ff_shrink22(uint8_t *dst, int dst_wrap,
  450. const uint8_t *src, int src_wrap,
  451. int width, int height)
  452. {
  453. int w;
  454. const uint8_t *s1, *s2;
  455. uint8_t *d;
  456. for(;height > 0; height--) {
  457. s1 = src;
  458. s2 = s1 + src_wrap;
  459. d = dst;
  460. for(w = width;w >= 4; w-=4) {
  461. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  462. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  463. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  464. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  465. s1 += 8;
  466. s2 += 8;
  467. d += 4;
  468. }
  469. for(;w > 0; w--) {
  470. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  471. s1 += 2;
  472. s2 += 2;
  473. d++;
  474. }
  475. src += 2 * src_wrap;
  476. dst += dst_wrap;
  477. }
  478. }
  479. /* 4x4 -> 1x1 */
  480. void ff_shrink44(uint8_t *dst, int dst_wrap,
  481. const uint8_t *src, int src_wrap,
  482. int width, int height)
  483. {
  484. int w;
  485. const uint8_t *s1, *s2, *s3, *s4;
  486. uint8_t *d;
  487. for(;height > 0; height--) {
  488. s1 = src;
  489. s2 = s1 + src_wrap;
  490. s3 = s2 + src_wrap;
  491. s4 = s3 + src_wrap;
  492. d = dst;
  493. for(w = width;w > 0; w--) {
  494. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  495. s2[0] + s2[1] + s2[2] + s2[3] +
  496. s3[0] + s3[1] + s3[2] + s3[3] +
  497. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  498. s1 += 4;
  499. s2 += 4;
  500. s3 += 4;
  501. s4 += 4;
  502. d++;
  503. }
  504. src += 4 * src_wrap;
  505. dst += dst_wrap;
  506. }
  507. }
  508. /* 8x8 -> 1x1 */
  509. void ff_shrink88(uint8_t *dst, int dst_wrap,
  510. const uint8_t *src, int src_wrap,
  511. int width, int height)
  512. {
  513. int w, i;
  514. for(;height > 0; height--) {
  515. for(w = width;w > 0; w--) {
  516. int tmp=0;
  517. for(i=0; i<8; i++){
  518. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  519. src += src_wrap;
  520. }
  521. *(dst++) = (tmp + 32)>>6;
  522. src += 8 - 8*src_wrap;
  523. }
  524. src += 8*src_wrap - 8*width;
  525. dst += dst_wrap - width;
  526. }
  527. }
  528. int avpicture_alloc(AVPicture *picture,
  529. enum PixelFormat pix_fmt, int width, int height)
  530. {
  531. int ret;
  532. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  533. memset(picture, 0, sizeof(AVPicture));
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. void avpicture_free(AVPicture *picture)
  539. {
  540. av_free(picture->data[0]);
  541. }
  542. /* return true if yuv planar */
  543. static inline int is_yuv_planar(enum PixelFormat fmt)
  544. {
  545. const PixFmtInfo *info = &pix_fmt_info[fmt];
  546. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
  547. int i;
  548. int planes[4] = { 0 };
  549. if (info->color_type != FF_COLOR_YUV &&
  550. info->color_type != FF_COLOR_YUV_JPEG)
  551. return 0;
  552. /* set the used planes */
  553. for (i = 0; i < desc->nb_components; i++)
  554. planes[desc->comp[i].plane] = 1;
  555. /* if there is an unused plane, the format is not planar */
  556. for (i = 0; i < desc->nb_components; i++)
  557. if (!planes[i])
  558. return 0;
  559. return 1;
  560. }
  561. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  562. enum PixelFormat pix_fmt, int top_band, int left_band)
  563. {
  564. int y_shift;
  565. int x_shift;
  566. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  567. return -1;
  568. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  569. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  570. if (is_yuv_planar(pix_fmt)) {
  571. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  572. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  573. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  574. } else{
  575. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  576. return -1;
  577. if(left_band) //FIXME add support for this too
  578. return -1;
  579. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  580. }
  581. dst->linesize[0] = src->linesize[0];
  582. dst->linesize[1] = src->linesize[1];
  583. dst->linesize[2] = src->linesize[2];
  584. return 0;
  585. }
  586. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  587. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  588. int *color)
  589. {
  590. uint8_t *optr;
  591. int y_shift;
  592. int x_shift;
  593. int yheight;
  594. int i, y;
  595. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  596. !is_yuv_planar(pix_fmt)) return -1;
  597. for (i = 0; i < 3; i++) {
  598. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  599. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  600. if (padtop || padleft) {
  601. memset(dst->data[i], color[i],
  602. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  603. }
  604. if (padleft || padright) {
  605. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  606. (dst->linesize[i] - (padright >> x_shift));
  607. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  608. for (y = 0; y < yheight; y++) {
  609. memset(optr, color[i], (padleft + padright) >> x_shift);
  610. optr += dst->linesize[i];
  611. }
  612. }
  613. if (src) { /* first line */
  614. uint8_t *iptr = src->data[i];
  615. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  616. (padleft >> x_shift);
  617. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  618. iptr += src->linesize[i];
  619. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  620. (dst->linesize[i] - (padright >> x_shift));
  621. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  622. for (y = 0; y < yheight; y++) {
  623. memset(optr, color[i], (padleft + padright) >> x_shift);
  624. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  625. (width - padleft - padright) >> x_shift);
  626. iptr += src->linesize[i];
  627. optr += dst->linesize[i];
  628. }
  629. }
  630. if (padbottom || padright) {
  631. optr = dst->data[i] + dst->linesize[i] *
  632. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  633. memset(optr, color[i],dst->linesize[i] *
  634. (padbottom >> y_shift) + (padright >> x_shift));
  635. }
  636. }
  637. return 0;
  638. }
  639. /* NOTE: we scan all the pixels to have an exact information */
  640. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  641. {
  642. const unsigned char *p;
  643. int src_wrap, ret, x, y;
  644. unsigned int a;
  645. uint32_t *palette = (uint32_t *)src->data[1];
  646. p = src->data[0];
  647. src_wrap = src->linesize[0] - width;
  648. ret = 0;
  649. for(y=0;y<height;y++) {
  650. for(x=0;x<width;x++) {
  651. a = palette[p[0]] >> 24;
  652. if (a == 0x00) {
  653. ret |= FF_ALPHA_TRANSP;
  654. } else if (a != 0xff) {
  655. ret |= FF_ALPHA_SEMI_TRANSP;
  656. }
  657. p++;
  658. }
  659. p += src_wrap;
  660. }
  661. return ret;
  662. }
  663. int img_get_alpha_info(const AVPicture *src,
  664. enum PixelFormat pix_fmt, int width, int height)
  665. {
  666. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  667. int ret;
  668. /* no alpha can be represented in format */
  669. if (!pf->is_alpha)
  670. return 0;
  671. switch(pix_fmt) {
  672. case PIX_FMT_PAL8:
  673. ret = get_alpha_info_pal8(src, width, height);
  674. break;
  675. default:
  676. /* we do not know, so everything is indicated */
  677. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  678. break;
  679. }
  680. return ret;
  681. }
  682. #if !(HAVE_MMX && HAVE_YASM)
  683. /* filter parameters: [-1 4 2 4 -1] // 8 */
  684. static void deinterlace_line_c(uint8_t *dst,
  685. const uint8_t *lum_m4, const uint8_t *lum_m3,
  686. const uint8_t *lum_m2, const uint8_t *lum_m1,
  687. const uint8_t *lum,
  688. int size)
  689. {
  690. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  691. int sum;
  692. for(;size > 0;size--) {
  693. sum = -lum_m4[0];
  694. sum += lum_m3[0] << 2;
  695. sum += lum_m2[0] << 1;
  696. sum += lum_m1[0] << 2;
  697. sum += -lum[0];
  698. dst[0] = cm[(sum + 4) >> 3];
  699. lum_m4++;
  700. lum_m3++;
  701. lum_m2++;
  702. lum_m1++;
  703. lum++;
  704. dst++;
  705. }
  706. }
  707. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  708. uint8_t *lum_m2, uint8_t *lum_m1,
  709. uint8_t *lum, int size)
  710. {
  711. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  712. int sum;
  713. for(;size > 0;size--) {
  714. sum = -lum_m4[0];
  715. sum += lum_m3[0] << 2;
  716. sum += lum_m2[0] << 1;
  717. lum_m4[0]=lum_m2[0];
  718. sum += lum_m1[0] << 2;
  719. sum += -lum[0];
  720. lum_m2[0] = cm[(sum + 4) >> 3];
  721. lum_m4++;
  722. lum_m3++;
  723. lum_m2++;
  724. lum_m1++;
  725. lum++;
  726. }
  727. }
  728. #endif
  729. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  730. top field is copied as is, but the bottom field is deinterlaced
  731. against the top field. */
  732. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  733. const uint8_t *src1, int src_wrap,
  734. int width, int height)
  735. {
  736. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  737. int y;
  738. src_m2 = src1;
  739. src_m1 = src1;
  740. src_0=&src_m1[src_wrap];
  741. src_p1=&src_0[src_wrap];
  742. src_p2=&src_p1[src_wrap];
  743. for(y=0;y<(height-2);y+=2) {
  744. memcpy(dst,src_m1,width);
  745. dst += dst_wrap;
  746. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  747. src_m2 = src_0;
  748. src_m1 = src_p1;
  749. src_0 = src_p2;
  750. src_p1 += 2*src_wrap;
  751. src_p2 += 2*src_wrap;
  752. dst += dst_wrap;
  753. }
  754. memcpy(dst,src_m1,width);
  755. dst += dst_wrap;
  756. /* do last line */
  757. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  758. }
  759. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  760. int width, int height)
  761. {
  762. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  763. int y;
  764. uint8_t *buf;
  765. buf = (uint8_t*)av_malloc(width);
  766. src_m1 = src1;
  767. memcpy(buf,src_m1,width);
  768. src_0=&src_m1[src_wrap];
  769. src_p1=&src_0[src_wrap];
  770. src_p2=&src_p1[src_wrap];
  771. for(y=0;y<(height-2);y+=2) {
  772. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  773. src_m1 = src_p1;
  774. src_0 = src_p2;
  775. src_p1 += 2*src_wrap;
  776. src_p2 += 2*src_wrap;
  777. }
  778. /* do last line */
  779. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  780. av_free(buf);
  781. }
  782. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  783. enum PixelFormat pix_fmt, int width, int height)
  784. {
  785. int i;
  786. if (pix_fmt != PIX_FMT_YUV420P &&
  787. pix_fmt != PIX_FMT_YUVJ420P &&
  788. pix_fmt != PIX_FMT_YUV422P &&
  789. pix_fmt != PIX_FMT_YUVJ422P &&
  790. pix_fmt != PIX_FMT_YUV444P &&
  791. pix_fmt != PIX_FMT_YUV411P &&
  792. pix_fmt != PIX_FMT_GRAY8)
  793. return -1;
  794. if ((width & 3) != 0 || (height & 3) != 0)
  795. return -1;
  796. for(i=0;i<3;i++) {
  797. if (i == 1) {
  798. switch(pix_fmt) {
  799. case PIX_FMT_YUVJ420P:
  800. case PIX_FMT_YUV420P:
  801. width >>= 1;
  802. height >>= 1;
  803. break;
  804. case PIX_FMT_YUV422P:
  805. case PIX_FMT_YUVJ422P:
  806. width >>= 1;
  807. break;
  808. case PIX_FMT_YUV411P:
  809. width >>= 2;
  810. break;
  811. default:
  812. break;
  813. }
  814. if (pix_fmt == PIX_FMT_GRAY8) {
  815. break;
  816. }
  817. }
  818. if (src == dst) {
  819. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  820. width, height);
  821. } else {
  822. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  823. src->data[i], src->linesize[i],
  824. width, height);
  825. }
  826. }
  827. emms_c();
  828. return 0;
  829. }