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.

975 lines
28KB

  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,
  508. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  509. ~FF_LOSS_COLORQUANT,
  510. ~FF_LOSS_DEPTH,
  511. ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
  512. ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
  513. FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
  514. 0x80000, //non zero entry that combines all loss variants including future additions
  515. 0,
  516. };
  517. loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
  518. dst_pix_fmt = PIX_FMT_NONE;
  519. loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
  520. loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
  521. /* try with successive loss */
  522. for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
  523. loss_order1 = loss1 & loss_mask_order[i];
  524. loss_order2 = loss2 & loss_mask_order[i];
  525. if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
  526. dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
  527. } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
  528. dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
  529. }
  530. }
  531. if (loss_ptr)
  532. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  533. return dst_pix_fmt;
  534. }
  535. enum PixelFormat avcodec_find_best_pix_fmt_of_list(enum PixelFormat *pix_fmt_list,
  536. enum PixelFormat src_pix_fmt,
  537. int has_alpha, int *loss_ptr){
  538. int i;
  539. enum PixelFormat best = PIX_FMT_NONE;
  540. for(i=0; pix_fmt_list[i] != PIX_FMT_NONE; i++)
  541. best = avcodec_find_best_pix_fmt2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
  542. return best;
  543. }
  544. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  545. enum PixelFormat pix_fmt, int width, int height)
  546. {
  547. av_image_copy(dst->data, dst->linesize, src->data,
  548. src->linesize, pix_fmt, width, height);
  549. }
  550. /* 2x2 -> 1x1 */
  551. void ff_shrink22(uint8_t *dst, int dst_wrap,
  552. const uint8_t *src, int src_wrap,
  553. int width, int height)
  554. {
  555. int w;
  556. const uint8_t *s1, *s2;
  557. uint8_t *d;
  558. for(;height > 0; height--) {
  559. s1 = src;
  560. s2 = s1 + src_wrap;
  561. d = dst;
  562. for(w = width;w >= 4; w-=4) {
  563. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  564. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  565. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  566. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  567. s1 += 8;
  568. s2 += 8;
  569. d += 4;
  570. }
  571. for(;w > 0; w--) {
  572. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  573. s1 += 2;
  574. s2 += 2;
  575. d++;
  576. }
  577. src += 2 * src_wrap;
  578. dst += dst_wrap;
  579. }
  580. }
  581. /* 4x4 -> 1x1 */
  582. void ff_shrink44(uint8_t *dst, int dst_wrap,
  583. const uint8_t *src, int src_wrap,
  584. int width, int height)
  585. {
  586. int w;
  587. const uint8_t *s1, *s2, *s3, *s4;
  588. uint8_t *d;
  589. for(;height > 0; height--) {
  590. s1 = src;
  591. s2 = s1 + src_wrap;
  592. s3 = s2 + src_wrap;
  593. s4 = s3 + src_wrap;
  594. d = dst;
  595. for(w = width;w > 0; w--) {
  596. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  597. s2[0] + s2[1] + s2[2] + s2[3] +
  598. s3[0] + s3[1] + s3[2] + s3[3] +
  599. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  600. s1 += 4;
  601. s2 += 4;
  602. s3 += 4;
  603. s4 += 4;
  604. d++;
  605. }
  606. src += 4 * src_wrap;
  607. dst += dst_wrap;
  608. }
  609. }
  610. /* 8x8 -> 1x1 */
  611. void ff_shrink88(uint8_t *dst, int dst_wrap,
  612. const uint8_t *src, int src_wrap,
  613. int width, int height)
  614. {
  615. int w, i;
  616. for(;height > 0; height--) {
  617. for(w = width;w > 0; w--) {
  618. int tmp=0;
  619. for(i=0; i<8; i++){
  620. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  621. src += src_wrap;
  622. }
  623. *(dst++) = (tmp + 32)>>6;
  624. src += 8 - 8*src_wrap;
  625. }
  626. src += 8*src_wrap - 8*width;
  627. dst += dst_wrap - width;
  628. }
  629. }
  630. int avpicture_alloc(AVPicture *picture,
  631. enum PixelFormat pix_fmt, int width, int height)
  632. {
  633. int ret;
  634. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  635. memset(picture, 0, sizeof(AVPicture));
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. void avpicture_free(AVPicture *picture)
  641. {
  642. av_free(picture->data[0]);
  643. }
  644. /* return true if yuv planar */
  645. static inline int is_yuv_planar(enum PixelFormat fmt)
  646. {
  647. const PixFmtInfo *info = &pix_fmt_info[fmt];
  648. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
  649. int i;
  650. int planes[4] = { 0 };
  651. if (info->color_type != FF_COLOR_YUV &&
  652. info->color_type != FF_COLOR_YUV_JPEG)
  653. return 0;
  654. /* set the used planes */
  655. for (i = 0; i < desc->nb_components; i++)
  656. planes[desc->comp[i].plane] = 1;
  657. /* if there is an unused plane, the format is not planar */
  658. for (i = 0; i < desc->nb_components; i++)
  659. if (!planes[i])
  660. return 0;
  661. return 1;
  662. }
  663. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  664. enum PixelFormat pix_fmt, int top_band, int left_band)
  665. {
  666. int y_shift;
  667. int x_shift;
  668. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  669. return -1;
  670. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  671. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  672. if (is_yuv_planar(pix_fmt)) {
  673. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  674. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  675. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  676. } else{
  677. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  678. return -1;
  679. if(left_band) //FIXME add support for this too
  680. return -1;
  681. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  682. }
  683. dst->linesize[0] = src->linesize[0];
  684. dst->linesize[1] = src->linesize[1];
  685. dst->linesize[2] = src->linesize[2];
  686. return 0;
  687. }
  688. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  689. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  690. int *color)
  691. {
  692. uint8_t *optr;
  693. int y_shift;
  694. int x_shift;
  695. int yheight;
  696. int i, y;
  697. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  698. !is_yuv_planar(pix_fmt)) return -1;
  699. for (i = 0; i < 3; i++) {
  700. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  701. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  702. if (padtop || padleft) {
  703. memset(dst->data[i], color[i],
  704. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  705. }
  706. if (padleft || padright) {
  707. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  708. (dst->linesize[i] - (padright >> x_shift));
  709. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  710. for (y = 0; y < yheight; y++) {
  711. memset(optr, color[i], (padleft + padright) >> x_shift);
  712. optr += dst->linesize[i];
  713. }
  714. }
  715. if (src) { /* first line */
  716. uint8_t *iptr = src->data[i];
  717. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  718. (padleft >> x_shift);
  719. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  720. iptr += src->linesize[i];
  721. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  722. (dst->linesize[i] - (padright >> x_shift));
  723. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  724. for (y = 0; y < yheight; y++) {
  725. memset(optr, color[i], (padleft + padright) >> x_shift);
  726. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  727. (width - padleft - padright) >> x_shift);
  728. iptr += src->linesize[i];
  729. optr += dst->linesize[i];
  730. }
  731. }
  732. if (padbottom || padright) {
  733. optr = dst->data[i] + dst->linesize[i] *
  734. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  735. memset(optr, color[i],dst->linesize[i] *
  736. (padbottom >> y_shift) + (padright >> x_shift));
  737. }
  738. }
  739. return 0;
  740. }
  741. #if !(HAVE_MMX && HAVE_YASM)
  742. /* filter parameters: [-1 4 2 4 -1] // 8 */
  743. static void deinterlace_line_c(uint8_t *dst,
  744. const uint8_t *lum_m4, const uint8_t *lum_m3,
  745. const uint8_t *lum_m2, const uint8_t *lum_m1,
  746. const uint8_t *lum,
  747. int size)
  748. {
  749. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  750. int sum;
  751. for(;size > 0;size--) {
  752. sum = -lum_m4[0];
  753. sum += lum_m3[0] << 2;
  754. sum += lum_m2[0] << 1;
  755. sum += lum_m1[0] << 2;
  756. sum += -lum[0];
  757. dst[0] = cm[(sum + 4) >> 3];
  758. lum_m4++;
  759. lum_m3++;
  760. lum_m2++;
  761. lum_m1++;
  762. lum++;
  763. dst++;
  764. }
  765. }
  766. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  767. uint8_t *lum_m2, uint8_t *lum_m1,
  768. uint8_t *lum, int size)
  769. {
  770. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  771. int sum;
  772. for(;size > 0;size--) {
  773. sum = -lum_m4[0];
  774. sum += lum_m3[0] << 2;
  775. sum += lum_m2[0] << 1;
  776. lum_m4[0]=lum_m2[0];
  777. sum += lum_m1[0] << 2;
  778. sum += -lum[0];
  779. lum_m2[0] = cm[(sum + 4) >> 3];
  780. lum_m4++;
  781. lum_m3++;
  782. lum_m2++;
  783. lum_m1++;
  784. lum++;
  785. }
  786. }
  787. #endif
  788. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  789. top field is copied as is, but the bottom field is deinterlaced
  790. against the top field. */
  791. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  792. const uint8_t *src1, int src_wrap,
  793. int width, int height)
  794. {
  795. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  796. int y;
  797. src_m2 = src1;
  798. src_m1 = src1;
  799. src_0=&src_m1[src_wrap];
  800. src_p1=&src_0[src_wrap];
  801. src_p2=&src_p1[src_wrap];
  802. for(y=0;y<(height-2);y+=2) {
  803. memcpy(dst,src_m1,width);
  804. dst += dst_wrap;
  805. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  806. src_m2 = src_0;
  807. src_m1 = src_p1;
  808. src_0 = src_p2;
  809. src_p1 += 2*src_wrap;
  810. src_p2 += 2*src_wrap;
  811. dst += dst_wrap;
  812. }
  813. memcpy(dst,src_m1,width);
  814. dst += dst_wrap;
  815. /* do last line */
  816. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  817. }
  818. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  819. int width, int height)
  820. {
  821. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  822. int y;
  823. uint8_t *buf;
  824. buf = av_malloc(width);
  825. src_m1 = src1;
  826. memcpy(buf,src_m1,width);
  827. src_0=&src_m1[src_wrap];
  828. src_p1=&src_0[src_wrap];
  829. src_p2=&src_p1[src_wrap];
  830. for(y=0;y<(height-2);y+=2) {
  831. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  832. src_m1 = src_p1;
  833. src_0 = src_p2;
  834. src_p1 += 2*src_wrap;
  835. src_p2 += 2*src_wrap;
  836. }
  837. /* do last line */
  838. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  839. av_free(buf);
  840. }
  841. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  842. enum PixelFormat pix_fmt, int width, int height)
  843. {
  844. int i;
  845. if (pix_fmt != PIX_FMT_YUV420P &&
  846. pix_fmt != PIX_FMT_YUVJ420P &&
  847. pix_fmt != PIX_FMT_YUV422P &&
  848. pix_fmt != PIX_FMT_YUVJ422P &&
  849. pix_fmt != PIX_FMT_YUV444P &&
  850. pix_fmt != PIX_FMT_YUV411P &&
  851. pix_fmt != PIX_FMT_GRAY8)
  852. return -1;
  853. if ((width & 3) != 0 || (height & 3) != 0)
  854. return -1;
  855. for(i=0;i<3;i++) {
  856. if (i == 1) {
  857. switch(pix_fmt) {
  858. case PIX_FMT_YUVJ420P:
  859. case PIX_FMT_YUV420P:
  860. width >>= 1;
  861. height >>= 1;
  862. break;
  863. case PIX_FMT_YUV422P:
  864. case PIX_FMT_YUVJ422P:
  865. width >>= 1;
  866. break;
  867. case PIX_FMT_YUV411P:
  868. width >>= 2;
  869. break;
  870. default:
  871. break;
  872. }
  873. if (pix_fmt == PIX_FMT_GRAY8) {
  874. break;
  875. }
  876. }
  877. if (src == dst) {
  878. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  879. width, height);
  880. } else {
  881. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  882. src->data[i], src->linesize[i],
  883. width, height);
  884. }
  885. }
  886. emms_c();
  887. return 0;
  888. }