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.

1017 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. switch (pix_fmt) {
  445. case PIX_FMT_RGB8:
  446. case PIX_FMT_BGR8:
  447. case PIX_FMT_RGB4_BYTE:
  448. case PIX_FMT_BGR4_BYTE:
  449. case PIX_FMT_GRAY8:
  450. // do not include palette for these pseudo-paletted formats
  451. return width * height;
  452. }
  453. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  454. }
  455. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  456. int has_alpha)
  457. {
  458. const PixFmtInfo *pf, *ps;
  459. const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  460. const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  461. int loss;
  462. ps = &pix_fmt_info[src_pix_fmt];
  463. /* compute loss */
  464. loss = 0;
  465. pf = &pix_fmt_info[dst_pix_fmt];
  466. if (pf->depth < ps->depth ||
  467. ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
  468. dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
  469. (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
  470. src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
  471. loss |= FF_LOSS_DEPTH;
  472. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  473. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  474. loss |= FF_LOSS_RESOLUTION;
  475. switch(pf->color_type) {
  476. case FF_COLOR_RGB:
  477. if (ps->color_type != FF_COLOR_RGB &&
  478. ps->color_type != FF_COLOR_GRAY)
  479. loss |= FF_LOSS_COLORSPACE;
  480. break;
  481. case FF_COLOR_GRAY:
  482. if (ps->color_type != FF_COLOR_GRAY)
  483. loss |= FF_LOSS_COLORSPACE;
  484. break;
  485. case FF_COLOR_YUV:
  486. if (ps->color_type != FF_COLOR_YUV)
  487. loss |= FF_LOSS_COLORSPACE;
  488. break;
  489. case FF_COLOR_YUV_JPEG:
  490. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  491. ps->color_type != FF_COLOR_YUV &&
  492. ps->color_type != FF_COLOR_GRAY)
  493. loss |= FF_LOSS_COLORSPACE;
  494. break;
  495. default:
  496. /* fail safe test */
  497. if (ps->color_type != pf->color_type)
  498. loss |= FF_LOSS_COLORSPACE;
  499. break;
  500. }
  501. if (pf->color_type == FF_COLOR_GRAY &&
  502. ps->color_type != FF_COLOR_GRAY)
  503. loss |= FF_LOSS_CHROMA;
  504. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  505. loss |= FF_LOSS_ALPHA;
  506. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  507. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  508. loss |= FF_LOSS_COLORQUANT;
  509. return loss;
  510. }
  511. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  512. {
  513. int bits;
  514. const PixFmtInfo *pf;
  515. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  516. pf = &pix_fmt_info[pix_fmt];
  517. switch(pf->pixel_type) {
  518. case FF_PIXEL_PACKED:
  519. switch(pix_fmt) {
  520. case PIX_FMT_YUYV422:
  521. case PIX_FMT_UYVY422:
  522. case PIX_FMT_RGB565BE:
  523. case PIX_FMT_RGB565LE:
  524. case PIX_FMT_RGB555BE:
  525. case PIX_FMT_RGB555LE:
  526. case PIX_FMT_RGB444BE:
  527. case PIX_FMT_RGB444LE:
  528. case PIX_FMT_BGR565BE:
  529. case PIX_FMT_BGR565LE:
  530. case PIX_FMT_BGR555BE:
  531. case PIX_FMT_BGR555LE:
  532. case PIX_FMT_BGR444BE:
  533. case PIX_FMT_BGR444LE:
  534. bits = 16;
  535. break;
  536. case PIX_FMT_UYYVYY411:
  537. bits = 12;
  538. break;
  539. default:
  540. bits = pf->depth * pf->nb_channels;
  541. break;
  542. }
  543. break;
  544. case FF_PIXEL_PLANAR:
  545. if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  546. bits = pf->depth * pf->nb_channels;
  547. } else {
  548. bits = pf->depth + ((2 * pf->depth) >>
  549. (desc->log2_chroma_w + desc->log2_chroma_h));
  550. }
  551. break;
  552. case FF_PIXEL_PALETTE:
  553. bits = 8;
  554. break;
  555. default:
  556. bits = -1;
  557. break;
  558. }
  559. return bits;
  560. }
  561. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  562. enum PixelFormat src_pix_fmt,
  563. int has_alpha,
  564. int loss_mask)
  565. {
  566. int dist, i, loss, min_dist;
  567. enum PixelFormat dst_pix_fmt;
  568. /* find exact color match with smallest size */
  569. dst_pix_fmt = PIX_FMT_NONE;
  570. min_dist = 0x7fffffff;
  571. for(i = 0;i < PIX_FMT_NB; i++) {
  572. if (pix_fmt_mask & (1ULL << i)) {
  573. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  574. if (loss == 0) {
  575. dist = avg_bits_per_pixel(i);
  576. if (dist < min_dist) {
  577. min_dist = dist;
  578. dst_pix_fmt = i;
  579. }
  580. }
  581. }
  582. }
  583. return dst_pix_fmt;
  584. }
  585. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  586. int has_alpha, int *loss_ptr)
  587. {
  588. enum PixelFormat dst_pix_fmt;
  589. int loss_mask, i;
  590. static const int loss_mask_order[] = {
  591. ~0, /* no loss first */
  592. ~FF_LOSS_ALPHA,
  593. ~FF_LOSS_RESOLUTION,
  594. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  595. ~FF_LOSS_COLORQUANT,
  596. ~FF_LOSS_DEPTH,
  597. 0,
  598. };
  599. /* try with successive loss */
  600. i = 0;
  601. for(;;) {
  602. loss_mask = loss_mask_order[i++];
  603. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  604. has_alpha, loss_mask);
  605. if (dst_pix_fmt >= 0)
  606. goto found;
  607. if (loss_mask == 0)
  608. break;
  609. }
  610. return PIX_FMT_NONE;
  611. found:
  612. if (loss_ptr)
  613. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  614. return dst_pix_fmt;
  615. }
  616. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  617. enum PixelFormat pix_fmt, int width, int height)
  618. {
  619. av_image_copy(dst->data, dst->linesize, src->data,
  620. src->linesize, pix_fmt, width, height);
  621. }
  622. /* 2x2 -> 1x1 */
  623. void ff_shrink22(uint8_t *dst, int dst_wrap,
  624. const uint8_t *src, int src_wrap,
  625. int width, int height)
  626. {
  627. int w;
  628. const uint8_t *s1, *s2;
  629. uint8_t *d;
  630. for(;height > 0; height--) {
  631. s1 = src;
  632. s2 = s1 + src_wrap;
  633. d = dst;
  634. for(w = width;w >= 4; w-=4) {
  635. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  636. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  637. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  638. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  639. s1 += 8;
  640. s2 += 8;
  641. d += 4;
  642. }
  643. for(;w > 0; w--) {
  644. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  645. s1 += 2;
  646. s2 += 2;
  647. d++;
  648. }
  649. src += 2 * src_wrap;
  650. dst += dst_wrap;
  651. }
  652. }
  653. /* 4x4 -> 1x1 */
  654. void ff_shrink44(uint8_t *dst, int dst_wrap,
  655. const uint8_t *src, int src_wrap,
  656. int width, int height)
  657. {
  658. int w;
  659. const uint8_t *s1, *s2, *s3, *s4;
  660. uint8_t *d;
  661. for(;height > 0; height--) {
  662. s1 = src;
  663. s2 = s1 + src_wrap;
  664. s3 = s2 + src_wrap;
  665. s4 = s3 + src_wrap;
  666. d = dst;
  667. for(w = width;w > 0; w--) {
  668. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  669. s2[0] + s2[1] + s2[2] + s2[3] +
  670. s3[0] + s3[1] + s3[2] + s3[3] +
  671. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  672. s1 += 4;
  673. s2 += 4;
  674. s3 += 4;
  675. s4 += 4;
  676. d++;
  677. }
  678. src += 4 * src_wrap;
  679. dst += dst_wrap;
  680. }
  681. }
  682. /* 8x8 -> 1x1 */
  683. void ff_shrink88(uint8_t *dst, int dst_wrap,
  684. const uint8_t *src, int src_wrap,
  685. int width, int height)
  686. {
  687. int w, i;
  688. for(;height > 0; height--) {
  689. for(w = width;w > 0; w--) {
  690. int tmp=0;
  691. for(i=0; i<8; i++){
  692. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  693. src += src_wrap;
  694. }
  695. *(dst++) = (tmp + 32)>>6;
  696. src += 8 - 8*src_wrap;
  697. }
  698. src += 8*src_wrap - 8*width;
  699. dst += dst_wrap - width;
  700. }
  701. }
  702. int avpicture_alloc(AVPicture *picture,
  703. enum PixelFormat pix_fmt, int width, int height)
  704. {
  705. int ret;
  706. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  707. memset(picture, 0, sizeof(AVPicture));
  708. return ret;
  709. }
  710. return 0;
  711. }
  712. void avpicture_free(AVPicture *picture)
  713. {
  714. av_free(picture->data[0]);
  715. }
  716. /* return true if yuv planar */
  717. static inline int is_yuv_planar(const PixFmtInfo *ps)
  718. {
  719. return (ps->color_type == FF_COLOR_YUV ||
  720. ps->color_type == FF_COLOR_YUV_JPEG) &&
  721. ps->pixel_type == FF_PIXEL_PLANAR;
  722. }
  723. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  724. enum PixelFormat pix_fmt, int top_band, int left_band)
  725. {
  726. int y_shift;
  727. int x_shift;
  728. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  729. return -1;
  730. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  731. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  732. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  733. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  734. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  735. dst->linesize[0] = src->linesize[0];
  736. dst->linesize[1] = src->linesize[1];
  737. dst->linesize[2] = src->linesize[2];
  738. return 0;
  739. }
  740. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  741. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  742. int *color)
  743. {
  744. uint8_t *optr;
  745. int y_shift;
  746. int x_shift;
  747. int yheight;
  748. int i, y;
  749. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  750. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  751. for (i = 0; i < 3; i++) {
  752. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  753. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  754. if (padtop || padleft) {
  755. memset(dst->data[i], color[i],
  756. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  757. }
  758. if (padleft || padright) {
  759. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  760. (dst->linesize[i] - (padright >> x_shift));
  761. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  762. for (y = 0; y < yheight; y++) {
  763. memset(optr, color[i], (padleft + padright) >> x_shift);
  764. optr += dst->linesize[i];
  765. }
  766. }
  767. if (src) { /* first line */
  768. uint8_t *iptr = src->data[i];
  769. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  770. (padleft >> x_shift);
  771. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  772. iptr += src->linesize[i];
  773. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  774. (dst->linesize[i] - (padright >> x_shift));
  775. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  776. for (y = 0; y < yheight; y++) {
  777. memset(optr, color[i], (padleft + padright) >> x_shift);
  778. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  779. (width - padleft - padright) >> x_shift);
  780. iptr += src->linesize[i];
  781. optr += dst->linesize[i];
  782. }
  783. }
  784. if (padbottom || padright) {
  785. optr = dst->data[i] + dst->linesize[i] *
  786. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  787. memset(optr, color[i],dst->linesize[i] *
  788. (padbottom >> y_shift) + (padright >> x_shift));
  789. }
  790. }
  791. return 0;
  792. }
  793. #if !(HAVE_MMX && HAVE_YASM)
  794. /* filter parameters: [-1 4 2 4 -1] // 8 */
  795. static void deinterlace_line_c(uint8_t *dst,
  796. const uint8_t *lum_m4, const uint8_t *lum_m3,
  797. const uint8_t *lum_m2, const uint8_t *lum_m1,
  798. const uint8_t *lum,
  799. int size)
  800. {
  801. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  802. int sum;
  803. for(;size > 0;size--) {
  804. sum = -lum_m4[0];
  805. sum += lum_m3[0] << 2;
  806. sum += lum_m2[0] << 1;
  807. sum += lum_m1[0] << 2;
  808. sum += -lum[0];
  809. dst[0] = cm[(sum + 4) >> 3];
  810. lum_m4++;
  811. lum_m3++;
  812. lum_m2++;
  813. lum_m1++;
  814. lum++;
  815. dst++;
  816. }
  817. }
  818. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  819. uint8_t *lum_m2, uint8_t *lum_m1,
  820. uint8_t *lum, int size)
  821. {
  822. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  823. int sum;
  824. for(;size > 0;size--) {
  825. sum = -lum_m4[0];
  826. sum += lum_m3[0] << 2;
  827. sum += lum_m2[0] << 1;
  828. lum_m4[0]=lum_m2[0];
  829. sum += lum_m1[0] << 2;
  830. sum += -lum[0];
  831. lum_m2[0] = cm[(sum + 4) >> 3];
  832. lum_m4++;
  833. lum_m3++;
  834. lum_m2++;
  835. lum_m1++;
  836. lum++;
  837. }
  838. }
  839. #endif
  840. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  841. top field is copied as is, but the bottom field is deinterlaced
  842. against the top field. */
  843. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  844. const uint8_t *src1, int src_wrap,
  845. int width, int height)
  846. {
  847. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  848. int y;
  849. src_m2 = src1;
  850. src_m1 = src1;
  851. src_0=&src_m1[src_wrap];
  852. src_p1=&src_0[src_wrap];
  853. src_p2=&src_p1[src_wrap];
  854. for(y=0;y<(height-2);y+=2) {
  855. memcpy(dst,src_m1,width);
  856. dst += dst_wrap;
  857. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  858. src_m2 = src_0;
  859. src_m1 = src_p1;
  860. src_0 = src_p2;
  861. src_p1 += 2*src_wrap;
  862. src_p2 += 2*src_wrap;
  863. dst += dst_wrap;
  864. }
  865. memcpy(dst,src_m1,width);
  866. dst += dst_wrap;
  867. /* do last line */
  868. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  869. }
  870. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  871. int width, int height)
  872. {
  873. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  874. int y;
  875. uint8_t *buf;
  876. buf = av_malloc(width);
  877. src_m1 = src1;
  878. memcpy(buf,src_m1,width);
  879. src_0=&src_m1[src_wrap];
  880. src_p1=&src_0[src_wrap];
  881. src_p2=&src_p1[src_wrap];
  882. for(y=0;y<(height-2);y+=2) {
  883. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  884. src_m1 = src_p1;
  885. src_0 = src_p2;
  886. src_p1 += 2*src_wrap;
  887. src_p2 += 2*src_wrap;
  888. }
  889. /* do last line */
  890. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  891. av_free(buf);
  892. }
  893. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  894. enum PixelFormat pix_fmt, int width, int height)
  895. {
  896. int i;
  897. if (pix_fmt != PIX_FMT_YUV420P &&
  898. pix_fmt != PIX_FMT_YUVJ420P &&
  899. pix_fmt != PIX_FMT_YUV422P &&
  900. pix_fmt != PIX_FMT_YUVJ422P &&
  901. pix_fmt != PIX_FMT_YUV444P &&
  902. pix_fmt != PIX_FMT_YUV411P &&
  903. pix_fmt != PIX_FMT_GRAY8)
  904. return -1;
  905. if ((width & 3) != 0 || (height & 3) != 0)
  906. return -1;
  907. for(i=0;i<3;i++) {
  908. if (i == 1) {
  909. switch(pix_fmt) {
  910. case PIX_FMT_YUVJ420P:
  911. case PIX_FMT_YUV420P:
  912. width >>= 1;
  913. height >>= 1;
  914. break;
  915. case PIX_FMT_YUV422P:
  916. case PIX_FMT_YUVJ422P:
  917. width >>= 1;
  918. break;
  919. case PIX_FMT_YUV411P:
  920. width >>= 2;
  921. break;
  922. default:
  923. break;
  924. }
  925. if (pix_fmt == PIX_FMT_GRAY8) {
  926. break;
  927. }
  928. }
  929. if (src == dst) {
  930. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  931. width, height);
  932. } else {
  933. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  934. src->data[i], src->linesize[i],
  935. width, height);
  936. }
  937. }
  938. emms_c();
  939. return 0;
  940. }