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.

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