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.

1127 lines
32KB

  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. void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
  415. {
  416. av_get_pix_fmt_string(buf, buf_size, pix_fmt);
  417. }
  418. #endif
  419. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  420. {
  421. return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
  422. }
  423. #if LIBAVCODEC_VERSION_MAJOR < 53
  424. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  425. return ff_set_systematic_pal2(pal, pix_fmt);
  426. }
  427. int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
  428. {
  429. return av_image_fill_linesizes(picture->linesize, pix_fmt, width);
  430. }
  431. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
  432. int height)
  433. {
  434. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  435. }
  436. #endif
  437. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  438. enum PixelFormat pix_fmt, int width, int height)
  439. {
  440. int ret;
  441. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  442. return ret;
  443. if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
  444. return ret;
  445. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  446. }
  447. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  448. unsigned char *dest, int dest_size)
  449. {
  450. int i, j, nb_planes = 0, linesizes[4];
  451. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  452. int size = avpicture_get_size(pix_fmt, width, height);
  453. if (size > dest_size || size < 0)
  454. return AVERROR(EINVAL);
  455. for (i = 0; i < desc->nb_components; i++)
  456. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  457. nb_planes++;
  458. av_image_fill_linesizes(linesizes, pix_fmt, width);
  459. for (i = 0; i < nb_planes; i++) {
  460. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  461. const unsigned char *s = src->data[i];
  462. h = (height + (1 << shift) - 1) >> shift;
  463. for (j = 0; j < h; j++) {
  464. memcpy(dest, s, linesizes[i]);
  465. dest += linesizes[i];
  466. s += src->linesize[i];
  467. }
  468. }
  469. if (desc->flags & PIX_FMT_PAL)
  470. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  471. return size;
  472. }
  473. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  474. {
  475. AVPicture dummy_pict;
  476. if(av_image_check_size(width, height, 0, NULL))
  477. return -1;
  478. switch (pix_fmt) {
  479. case PIX_FMT_RGB8:
  480. case PIX_FMT_BGR8:
  481. case PIX_FMT_RGB4_BYTE:
  482. case PIX_FMT_BGR4_BYTE:
  483. case PIX_FMT_GRAY8:
  484. // do not include palette for these pseudo-paletted formats
  485. return width * height;
  486. }
  487. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  488. }
  489. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  490. int has_alpha)
  491. {
  492. const PixFmtInfo *pf, *ps;
  493. const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  494. const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  495. int loss;
  496. ps = &pix_fmt_info[src_pix_fmt];
  497. /* compute loss */
  498. loss = 0;
  499. pf = &pix_fmt_info[dst_pix_fmt];
  500. if (pf->depth < ps->depth ||
  501. ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
  502. dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
  503. (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
  504. src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
  505. loss |= FF_LOSS_DEPTH;
  506. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  507. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  508. loss |= FF_LOSS_RESOLUTION;
  509. switch(pf->color_type) {
  510. case FF_COLOR_RGB:
  511. if (ps->color_type != FF_COLOR_RGB &&
  512. ps->color_type != FF_COLOR_GRAY)
  513. loss |= FF_LOSS_COLORSPACE;
  514. break;
  515. case FF_COLOR_GRAY:
  516. if (ps->color_type != FF_COLOR_GRAY)
  517. loss |= FF_LOSS_COLORSPACE;
  518. break;
  519. case FF_COLOR_YUV:
  520. if (ps->color_type != FF_COLOR_YUV)
  521. loss |= FF_LOSS_COLORSPACE;
  522. break;
  523. case FF_COLOR_YUV_JPEG:
  524. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  525. ps->color_type != FF_COLOR_YUV &&
  526. ps->color_type != FF_COLOR_GRAY)
  527. loss |= FF_LOSS_COLORSPACE;
  528. break;
  529. default:
  530. /* fail safe test */
  531. if (ps->color_type != pf->color_type)
  532. loss |= FF_LOSS_COLORSPACE;
  533. break;
  534. }
  535. if (pf->color_type == FF_COLOR_GRAY &&
  536. ps->color_type != FF_COLOR_GRAY)
  537. loss |= FF_LOSS_CHROMA;
  538. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  539. loss |= FF_LOSS_ALPHA;
  540. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  541. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  542. loss |= FF_LOSS_COLORQUANT;
  543. return loss;
  544. }
  545. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  546. {
  547. int bits;
  548. const PixFmtInfo *pf;
  549. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  550. pf = &pix_fmt_info[pix_fmt];
  551. switch(pf->pixel_type) {
  552. case FF_PIXEL_PACKED:
  553. switch(pix_fmt) {
  554. case PIX_FMT_YUYV422:
  555. case PIX_FMT_UYVY422:
  556. case PIX_FMT_RGB565BE:
  557. case PIX_FMT_RGB565LE:
  558. case PIX_FMT_RGB555BE:
  559. case PIX_FMT_RGB555LE:
  560. case PIX_FMT_RGB444BE:
  561. case PIX_FMT_RGB444LE:
  562. case PIX_FMT_BGR565BE:
  563. case PIX_FMT_BGR565LE:
  564. case PIX_FMT_BGR555BE:
  565. case PIX_FMT_BGR555LE:
  566. case PIX_FMT_BGR444BE:
  567. case PIX_FMT_BGR444LE:
  568. bits = 16;
  569. break;
  570. case PIX_FMT_UYYVYY411:
  571. bits = 12;
  572. break;
  573. default:
  574. bits = pf->depth * pf->nb_channels;
  575. break;
  576. }
  577. break;
  578. case FF_PIXEL_PLANAR:
  579. if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  580. bits = pf->depth * pf->nb_channels;
  581. } else {
  582. bits = pf->depth + ((2 * pf->depth) >>
  583. (desc->log2_chroma_w + desc->log2_chroma_h));
  584. }
  585. break;
  586. case FF_PIXEL_PALETTE:
  587. bits = 8;
  588. break;
  589. default:
  590. bits = -1;
  591. break;
  592. }
  593. return bits;
  594. }
  595. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  596. enum PixelFormat src_pix_fmt,
  597. int has_alpha,
  598. int loss_mask)
  599. {
  600. int dist, i, loss, min_dist;
  601. enum PixelFormat dst_pix_fmt;
  602. /* find exact color match with smallest size */
  603. dst_pix_fmt = PIX_FMT_NONE;
  604. min_dist = 0x7fffffff;
  605. for(i = 0;i < PIX_FMT_NB; i++) {
  606. if (pix_fmt_mask & (1ULL << i)) {
  607. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  608. if (loss == 0) {
  609. dist = avg_bits_per_pixel(i);
  610. if (dist < min_dist) {
  611. min_dist = dist;
  612. dst_pix_fmt = i;
  613. }
  614. }
  615. }
  616. }
  617. return dst_pix_fmt;
  618. }
  619. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  620. int has_alpha, int *loss_ptr)
  621. {
  622. enum PixelFormat dst_pix_fmt;
  623. int loss_mask, i;
  624. static const int loss_mask_order[] = {
  625. ~0, /* no loss first */
  626. ~FF_LOSS_ALPHA,
  627. ~FF_LOSS_RESOLUTION,
  628. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  629. ~FF_LOSS_COLORQUANT,
  630. ~FF_LOSS_DEPTH,
  631. 0,
  632. };
  633. /* try with successive loss */
  634. i = 0;
  635. for(;;) {
  636. loss_mask = loss_mask_order[i++];
  637. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  638. has_alpha, loss_mask);
  639. if (dst_pix_fmt >= 0)
  640. goto found;
  641. if (loss_mask == 0)
  642. break;
  643. }
  644. return PIX_FMT_NONE;
  645. found:
  646. if (loss_ptr)
  647. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  648. return dst_pix_fmt;
  649. }
  650. #if LIBAVCODEC_VERSION_MAJOR < 53
  651. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  652. const uint8_t *src, int src_wrap,
  653. int width, int height)
  654. {
  655. av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
  656. }
  657. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  658. {
  659. return av_image_get_linesize(pix_fmt, width, plane);
  660. }
  661. void av_picture_data_copy(uint8_t *dst_data[4], int dst_linesize[4],
  662. uint8_t *src_data[4], int src_linesize[4],
  663. enum PixelFormat pix_fmt, int width, int height)
  664. {
  665. av_image_copy(dst_data, dst_linesize, src_data, src_linesize,
  666. pix_fmt, width, height);
  667. }
  668. #endif
  669. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  670. enum PixelFormat pix_fmt, int width, int height)
  671. {
  672. av_image_copy(dst->data, dst->linesize, src->data,
  673. src->linesize, pix_fmt, width, height);
  674. }
  675. /* 2x2 -> 1x1 */
  676. void ff_shrink22(uint8_t *dst, int dst_wrap,
  677. const uint8_t *src, int src_wrap,
  678. int width, int height)
  679. {
  680. int w;
  681. const uint8_t *s1, *s2;
  682. uint8_t *d;
  683. for(;height > 0; height--) {
  684. s1 = src;
  685. s2 = s1 + src_wrap;
  686. d = dst;
  687. for(w = width;w >= 4; w-=4) {
  688. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  689. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  690. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  691. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  692. s1 += 8;
  693. s2 += 8;
  694. d += 4;
  695. }
  696. for(;w > 0; w--) {
  697. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  698. s1 += 2;
  699. s2 += 2;
  700. d++;
  701. }
  702. src += 2 * src_wrap;
  703. dst += dst_wrap;
  704. }
  705. }
  706. /* 4x4 -> 1x1 */
  707. void ff_shrink44(uint8_t *dst, int dst_wrap,
  708. const uint8_t *src, int src_wrap,
  709. int width, int height)
  710. {
  711. int w;
  712. const uint8_t *s1, *s2, *s3, *s4;
  713. uint8_t *d;
  714. for(;height > 0; height--) {
  715. s1 = src;
  716. s2 = s1 + src_wrap;
  717. s3 = s2 + src_wrap;
  718. s4 = s3 + src_wrap;
  719. d = dst;
  720. for(w = width;w > 0; w--) {
  721. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  722. s2[0] + s2[1] + s2[2] + s2[3] +
  723. s3[0] + s3[1] + s3[2] + s3[3] +
  724. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  725. s1 += 4;
  726. s2 += 4;
  727. s3 += 4;
  728. s4 += 4;
  729. d++;
  730. }
  731. src += 4 * src_wrap;
  732. dst += dst_wrap;
  733. }
  734. }
  735. /* 8x8 -> 1x1 */
  736. void ff_shrink88(uint8_t *dst, int dst_wrap,
  737. const uint8_t *src, int src_wrap,
  738. int width, int height)
  739. {
  740. int w, i;
  741. for(;height > 0; height--) {
  742. for(w = width;w > 0; w--) {
  743. int tmp=0;
  744. for(i=0; i<8; i++){
  745. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  746. src += src_wrap;
  747. }
  748. *(dst++) = (tmp + 32)>>6;
  749. src += 8 - 8*src_wrap;
  750. }
  751. src += 8*src_wrap - 8*width;
  752. dst += dst_wrap - width;
  753. }
  754. }
  755. int avpicture_alloc(AVPicture *picture,
  756. enum PixelFormat pix_fmt, int width, int height)
  757. {
  758. int ret;
  759. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  760. memset(picture, 0, sizeof(AVPicture));
  761. return ret;
  762. }
  763. return 0;
  764. }
  765. void avpicture_free(AVPicture *picture)
  766. {
  767. av_free(picture->data[0]);
  768. }
  769. /* return true if yuv planar */
  770. static inline int is_yuv_planar(const PixFmtInfo *ps)
  771. {
  772. return (ps->color_type == FF_COLOR_YUV ||
  773. ps->color_type == FF_COLOR_YUV_JPEG) &&
  774. ps->pixel_type == FF_PIXEL_PLANAR;
  775. }
  776. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  777. enum PixelFormat pix_fmt, int top_band, int left_band)
  778. {
  779. int y_shift;
  780. int x_shift;
  781. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  782. return -1;
  783. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  784. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  785. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  786. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  787. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  788. dst->linesize[0] = src->linesize[0];
  789. dst->linesize[1] = src->linesize[1];
  790. dst->linesize[2] = src->linesize[2];
  791. return 0;
  792. }
  793. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  794. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  795. int *color)
  796. {
  797. uint8_t *optr;
  798. int y_shift;
  799. int x_shift;
  800. int yheight;
  801. int i, y;
  802. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  803. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  804. for (i = 0; i < 3; i++) {
  805. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  806. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  807. if (padtop || padleft) {
  808. memset(dst->data[i], color[i],
  809. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  810. }
  811. if (padleft || padright) {
  812. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  813. (dst->linesize[i] - (padright >> x_shift));
  814. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  815. for (y = 0; y < yheight; y++) {
  816. memset(optr, color[i], (padleft + padright) >> x_shift);
  817. optr += dst->linesize[i];
  818. }
  819. }
  820. if (src) { /* first line */
  821. uint8_t *iptr = src->data[i];
  822. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  823. (padleft >> x_shift);
  824. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  825. iptr += src->linesize[i];
  826. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  827. (dst->linesize[i] - (padright >> x_shift));
  828. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  829. for (y = 0; y < yheight; y++) {
  830. memset(optr, color[i], (padleft + padright) >> x_shift);
  831. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  832. (width - padleft - padright) >> x_shift);
  833. iptr += src->linesize[i];
  834. optr += dst->linesize[i];
  835. }
  836. }
  837. if (padbottom || padright) {
  838. optr = dst->data[i] + dst->linesize[i] *
  839. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  840. memset(optr, color[i],dst->linesize[i] *
  841. (padbottom >> y_shift) + (padright >> x_shift));
  842. }
  843. }
  844. return 0;
  845. }
  846. /* NOTE: we scan all the pixels to have an exact information */
  847. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  848. {
  849. const unsigned char *p;
  850. int src_wrap, ret, x, y;
  851. unsigned int a;
  852. uint32_t *palette = (uint32_t *)src->data[1];
  853. p = src->data[0];
  854. src_wrap = src->linesize[0] - width;
  855. ret = 0;
  856. for(y=0;y<height;y++) {
  857. for(x=0;x<width;x++) {
  858. a = palette[p[0]] >> 24;
  859. if (a == 0x00) {
  860. ret |= FF_ALPHA_TRANSP;
  861. } else if (a != 0xff) {
  862. ret |= FF_ALPHA_SEMI_TRANSP;
  863. }
  864. p++;
  865. }
  866. p += src_wrap;
  867. }
  868. return ret;
  869. }
  870. int img_get_alpha_info(const AVPicture *src,
  871. enum PixelFormat pix_fmt, int width, int height)
  872. {
  873. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  874. int ret;
  875. /* no alpha can be represented in format */
  876. if (!pf->is_alpha)
  877. return 0;
  878. switch(pix_fmt) {
  879. case PIX_FMT_PAL8:
  880. ret = get_alpha_info_pal8(src, width, height);
  881. break;
  882. default:
  883. /* we do not know, so everything is indicated */
  884. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  885. break;
  886. }
  887. return ret;
  888. }
  889. #if !(HAVE_MMX && HAVE_YASM)
  890. /* filter parameters: [-1 4 2 4 -1] // 8 */
  891. static void deinterlace_line_c(uint8_t *dst,
  892. const uint8_t *lum_m4, const uint8_t *lum_m3,
  893. const uint8_t *lum_m2, const uint8_t *lum_m1,
  894. const uint8_t *lum,
  895. int size)
  896. {
  897. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  898. int sum;
  899. for(;size > 0;size--) {
  900. sum = -lum_m4[0];
  901. sum += lum_m3[0] << 2;
  902. sum += lum_m2[0] << 1;
  903. sum += lum_m1[0] << 2;
  904. sum += -lum[0];
  905. dst[0] = cm[(sum + 4) >> 3];
  906. lum_m4++;
  907. lum_m3++;
  908. lum_m2++;
  909. lum_m1++;
  910. lum++;
  911. dst++;
  912. }
  913. }
  914. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  915. uint8_t *lum_m2, uint8_t *lum_m1,
  916. uint8_t *lum, int size)
  917. {
  918. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  919. int sum;
  920. for(;size > 0;size--) {
  921. sum = -lum_m4[0];
  922. sum += lum_m3[0] << 2;
  923. sum += lum_m2[0] << 1;
  924. lum_m4[0]=lum_m2[0];
  925. sum += lum_m1[0] << 2;
  926. sum += -lum[0];
  927. lum_m2[0] = cm[(sum + 4) >> 3];
  928. lum_m4++;
  929. lum_m3++;
  930. lum_m2++;
  931. lum_m1++;
  932. lum++;
  933. }
  934. }
  935. #endif
  936. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  937. top field is copied as is, but the bottom field is deinterlaced
  938. against the top field. */
  939. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  940. const uint8_t *src1, int src_wrap,
  941. int width, int height)
  942. {
  943. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  944. int y;
  945. src_m2 = src1;
  946. src_m1 = src1;
  947. src_0=&src_m1[src_wrap];
  948. src_p1=&src_0[src_wrap];
  949. src_p2=&src_p1[src_wrap];
  950. for(y=0;y<(height-2);y+=2) {
  951. memcpy(dst,src_m1,width);
  952. dst += dst_wrap;
  953. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  954. src_m2 = src_0;
  955. src_m1 = src_p1;
  956. src_0 = src_p2;
  957. src_p1 += 2*src_wrap;
  958. src_p2 += 2*src_wrap;
  959. dst += dst_wrap;
  960. }
  961. memcpy(dst,src_m1,width);
  962. dst += dst_wrap;
  963. /* do last line */
  964. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  965. }
  966. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  967. int width, int height)
  968. {
  969. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  970. int y;
  971. uint8_t *buf;
  972. buf = (uint8_t*)av_malloc(width);
  973. src_m1 = src1;
  974. memcpy(buf,src_m1,width);
  975. src_0=&src_m1[src_wrap];
  976. src_p1=&src_0[src_wrap];
  977. src_p2=&src_p1[src_wrap];
  978. for(y=0;y<(height-2);y+=2) {
  979. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  980. src_m1 = src_p1;
  981. src_0 = src_p2;
  982. src_p1 += 2*src_wrap;
  983. src_p2 += 2*src_wrap;
  984. }
  985. /* do last line */
  986. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  987. av_free(buf);
  988. }
  989. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  990. enum PixelFormat pix_fmt, int width, int height)
  991. {
  992. int i;
  993. if (pix_fmt != PIX_FMT_YUV420P &&
  994. pix_fmt != PIX_FMT_YUVJ420P &&
  995. pix_fmt != PIX_FMT_YUV422P &&
  996. pix_fmt != PIX_FMT_YUVJ422P &&
  997. pix_fmt != PIX_FMT_YUV444P &&
  998. pix_fmt != PIX_FMT_YUV411P &&
  999. pix_fmt != PIX_FMT_GRAY8)
  1000. return -1;
  1001. if ((width & 3) != 0 || (height & 3) != 0)
  1002. return -1;
  1003. for(i=0;i<3;i++) {
  1004. if (i == 1) {
  1005. switch(pix_fmt) {
  1006. case PIX_FMT_YUVJ420P:
  1007. case PIX_FMT_YUV420P:
  1008. width >>= 1;
  1009. height >>= 1;
  1010. break;
  1011. case PIX_FMT_YUV422P:
  1012. case PIX_FMT_YUVJ422P:
  1013. width >>= 1;
  1014. break;
  1015. case PIX_FMT_YUV411P:
  1016. width >>= 2;
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. if (pix_fmt == PIX_FMT_GRAY8) {
  1022. break;
  1023. }
  1024. }
  1025. if (src == dst) {
  1026. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1027. width, height);
  1028. } else {
  1029. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1030. src->data[i], src->linesize[i],
  1031. width, height);
  1032. }
  1033. }
  1034. emms_c();
  1035. return 0;
  1036. }