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.

1010 lines
29KB

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