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.

1149 lines
33KB

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