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.

1037 lines
30KB

  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 "libavutil/colorspace.h"
  34. #include "libavutil/common.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #if HAVE_MMX_EXTERNAL
  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_EXTERNAL
  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[AV_PIX_FMT_NB] = {
  63. /* YUV formats */
  64. [AV_PIX_FMT_YUV420P] = {
  65. .nb_channels = 3,
  66. .color_type = FF_COLOR_YUV,
  67. .pixel_type = FF_PIXEL_PLANAR,
  68. .depth = 8,
  69. },
  70. [AV_PIX_FMT_YUV422P] = {
  71. .nb_channels = 3,
  72. .color_type = FF_COLOR_YUV,
  73. .pixel_type = FF_PIXEL_PLANAR,
  74. .depth = 8,
  75. },
  76. [AV_PIX_FMT_YUV444P] = {
  77. .nb_channels = 3,
  78. .color_type = FF_COLOR_YUV,
  79. .pixel_type = FF_PIXEL_PLANAR,
  80. .depth = 8,
  81. },
  82. [AV_PIX_FMT_YUYV422] = {
  83. .nb_channels = 1,
  84. .color_type = FF_COLOR_YUV,
  85. .pixel_type = FF_PIXEL_PACKED,
  86. .depth = 8,
  87. },
  88. [AV_PIX_FMT_UYVY422] = {
  89. .nb_channels = 1,
  90. .color_type = FF_COLOR_YUV,
  91. .pixel_type = FF_PIXEL_PACKED,
  92. .depth = 8,
  93. },
  94. [AV_PIX_FMT_YUV410P] = {
  95. .nb_channels = 3,
  96. .color_type = FF_COLOR_YUV,
  97. .pixel_type = FF_PIXEL_PLANAR,
  98. .depth = 8,
  99. },
  100. [AV_PIX_FMT_YUV411P] = {
  101. .nb_channels = 3,
  102. .color_type = FF_COLOR_YUV,
  103. .pixel_type = FF_PIXEL_PLANAR,
  104. .depth = 8,
  105. },
  106. [AV_PIX_FMT_YUV440P] = {
  107. .nb_channels = 3,
  108. .color_type = FF_COLOR_YUV,
  109. .pixel_type = FF_PIXEL_PLANAR,
  110. .depth = 8,
  111. },
  112. [AV_PIX_FMT_YUV420P16LE] = {
  113. .nb_channels = 3,
  114. .color_type = FF_COLOR_YUV,
  115. .pixel_type = FF_PIXEL_PLANAR,
  116. .depth = 16,
  117. },
  118. [AV_PIX_FMT_YUV422P16LE] = {
  119. .nb_channels = 3,
  120. .color_type = FF_COLOR_YUV,
  121. .pixel_type = FF_PIXEL_PLANAR,
  122. .depth = 16,
  123. },
  124. [AV_PIX_FMT_YUV444P16LE] = {
  125. .nb_channels = 3,
  126. .color_type = FF_COLOR_YUV,
  127. .pixel_type = FF_PIXEL_PLANAR,
  128. .depth = 16,
  129. },
  130. [AV_PIX_FMT_YUV420P16BE] = {
  131. .nb_channels = 3,
  132. .color_type = FF_COLOR_YUV,
  133. .pixel_type = FF_PIXEL_PLANAR,
  134. .depth = 16,
  135. },
  136. [AV_PIX_FMT_YUV422P16BE] = {
  137. .nb_channels = 3,
  138. .color_type = FF_COLOR_YUV,
  139. .pixel_type = FF_PIXEL_PLANAR,
  140. .depth = 16,
  141. },
  142. [AV_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. [AV_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. [AV_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. [AV_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. [AV_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. [AV_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. [AV_PIX_FMT_RGB24] = {
  182. .nb_channels = 3,
  183. .color_type = FF_COLOR_RGB,
  184. .pixel_type = FF_PIXEL_PACKED,
  185. .depth = 8,
  186. },
  187. [AV_PIX_FMT_BGR24] = {
  188. .nb_channels = 3,
  189. .color_type = FF_COLOR_RGB,
  190. .pixel_type = FF_PIXEL_PACKED,
  191. .depth = 8,
  192. },
  193. [AV_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. [AV_PIX_FMT_RGB48BE] = {
  200. .nb_channels = 3,
  201. .color_type = FF_COLOR_RGB,
  202. .pixel_type = FF_PIXEL_PACKED,
  203. .depth = 16,
  204. },
  205. [AV_PIX_FMT_RGB48LE] = {
  206. .nb_channels = 3,
  207. .color_type = FF_COLOR_RGB,
  208. .pixel_type = FF_PIXEL_PACKED,
  209. .depth = 16,
  210. },
  211. [AV_PIX_FMT_RGB565BE] = {
  212. .nb_channels = 3,
  213. .color_type = FF_COLOR_RGB,
  214. .pixel_type = FF_PIXEL_PACKED,
  215. .depth = 5,
  216. },
  217. [AV_PIX_FMT_RGB565LE] = {
  218. .nb_channels = 3,
  219. .color_type = FF_COLOR_RGB,
  220. .pixel_type = FF_PIXEL_PACKED,
  221. .depth = 5,
  222. },
  223. [AV_PIX_FMT_RGB555BE] = {
  224. .nb_channels = 3,
  225. .color_type = FF_COLOR_RGB,
  226. .pixel_type = FF_PIXEL_PACKED,
  227. .depth = 5,
  228. },
  229. [AV_PIX_FMT_RGB555LE] = {
  230. .nb_channels = 3,
  231. .color_type = FF_COLOR_RGB,
  232. .pixel_type = FF_PIXEL_PACKED,
  233. .depth = 5,
  234. },
  235. [AV_PIX_FMT_RGB444BE] = {
  236. .nb_channels = 3,
  237. .color_type = FF_COLOR_RGB,
  238. .pixel_type = FF_PIXEL_PACKED,
  239. .depth = 4,
  240. },
  241. [AV_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. [AV_PIX_FMT_GRAY16BE] = {
  249. .nb_channels = 1,
  250. .color_type = FF_COLOR_GRAY,
  251. .pixel_type = FF_PIXEL_PLANAR,
  252. .depth = 16,
  253. },
  254. [AV_PIX_FMT_GRAY16LE] = {
  255. .nb_channels = 1,
  256. .color_type = FF_COLOR_GRAY,
  257. .pixel_type = FF_PIXEL_PLANAR,
  258. .depth = 16,
  259. },
  260. [AV_PIX_FMT_GRAY8] = {
  261. .nb_channels = 1,
  262. .color_type = FF_COLOR_GRAY,
  263. .pixel_type = FF_PIXEL_PLANAR,
  264. .depth = 8,
  265. },
  266. [AV_PIX_FMT_MONOWHITE] = {
  267. .nb_channels = 1,
  268. .color_type = FF_COLOR_GRAY,
  269. .pixel_type = FF_PIXEL_PLANAR,
  270. .depth = 1,
  271. },
  272. [AV_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. [AV_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. [AV_PIX_FMT_UYYVYY411] = {
  286. .nb_channels = 1,
  287. .color_type = FF_COLOR_YUV,
  288. .pixel_type = FF_PIXEL_PACKED,
  289. .depth = 8,
  290. },
  291. [AV_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. [AV_PIX_FMT_BGR565BE] = {
  298. .nb_channels = 3,
  299. .color_type = FF_COLOR_RGB,
  300. .pixel_type = FF_PIXEL_PACKED,
  301. .depth = 5,
  302. },
  303. [AV_PIX_FMT_BGR565LE] = {
  304. .nb_channels = 3,
  305. .color_type = FF_COLOR_RGB,
  306. .pixel_type = FF_PIXEL_PACKED,
  307. .depth = 5,
  308. },
  309. [AV_PIX_FMT_BGR555BE] = {
  310. .nb_channels = 3,
  311. .color_type = FF_COLOR_RGB,
  312. .pixel_type = FF_PIXEL_PACKED,
  313. .depth = 5,
  314. },
  315. [AV_PIX_FMT_BGR555LE] = {
  316. .nb_channels = 3,
  317. .color_type = FF_COLOR_RGB,
  318. .pixel_type = FF_PIXEL_PACKED,
  319. .depth = 5,
  320. },
  321. [AV_PIX_FMT_BGR444BE] = {
  322. .nb_channels = 3,
  323. .color_type = FF_COLOR_RGB,
  324. .pixel_type = FF_PIXEL_PACKED,
  325. .depth = 4,
  326. },
  327. [AV_PIX_FMT_BGR444LE] = {
  328. .nb_channels = 3,
  329. .color_type = FF_COLOR_RGB,
  330. .pixel_type = FF_PIXEL_PACKED,
  331. .depth = 4,
  332. },
  333. [AV_PIX_FMT_RGB8] = {
  334. .nb_channels = 1,
  335. .color_type = FF_COLOR_RGB,
  336. .pixel_type = FF_PIXEL_PACKED,
  337. .depth = 8,
  338. },
  339. [AV_PIX_FMT_RGB4] = {
  340. .nb_channels = 1,
  341. .color_type = FF_COLOR_RGB,
  342. .pixel_type = FF_PIXEL_PACKED,
  343. .depth = 4,
  344. },
  345. [AV_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. [AV_PIX_FMT_BGR8] = {
  352. .nb_channels = 1,
  353. .color_type = FF_COLOR_RGB,
  354. .pixel_type = FF_PIXEL_PACKED,
  355. .depth = 8,
  356. },
  357. [AV_PIX_FMT_BGR4] = {
  358. .nb_channels = 1,
  359. .color_type = FF_COLOR_RGB,
  360. .pixel_type = FF_PIXEL_PACKED,
  361. .depth = 4,
  362. },
  363. [AV_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. [AV_PIX_FMT_NV12] = {
  370. .nb_channels = 2,
  371. .color_type = FF_COLOR_YUV,
  372. .pixel_type = FF_PIXEL_PLANAR,
  373. .depth = 8,
  374. },
  375. [AV_PIX_FMT_NV21] = {
  376. .nb_channels = 2,
  377. .color_type = FF_COLOR_YUV,
  378. .pixel_type = FF_PIXEL_PLANAR,
  379. .depth = 8,
  380. },
  381. [AV_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. [AV_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 AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  395. {
  396. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  397. *h_shift = desc->log2_chroma_w;
  398. *v_shift = desc->log2_chroma_h;
  399. }
  400. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  401. enum AVPixelFormat pix_fmt, int width, int height)
  402. {
  403. int ret;
  404. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  405. return ret;
  406. if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
  407. return ret;
  408. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  409. }
  410. int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
  411. unsigned char *dest, int dest_size)
  412. {
  413. int i, j, nb_planes = 0, linesizes[4];
  414. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  415. int size = avpicture_get_size(pix_fmt, width, height);
  416. if (size > dest_size || size < 0)
  417. return AVERROR(EINVAL);
  418. for (i = 0; i < desc->nb_components; i++)
  419. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  420. nb_planes++;
  421. av_image_fill_linesizes(linesizes, pix_fmt, width);
  422. for (i = 0; i < nb_planes; i++) {
  423. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  424. const unsigned char *s = src->data[i];
  425. h = (height + (1 << shift) - 1) >> shift;
  426. for (j = 0; j < h; j++) {
  427. memcpy(dest, s, linesizes[i]);
  428. dest += linesizes[i];
  429. s += src->linesize[i];
  430. }
  431. }
  432. if (desc->flags & PIX_FMT_PAL)
  433. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  434. return size;
  435. }
  436. int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
  437. {
  438. AVPicture dummy_pict;
  439. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  440. if(av_image_check_size(width, height, 0, NULL))
  441. return -1;
  442. if (desc->flags & PIX_FMT_PSEUDOPAL)
  443. // do not include palette for these pseudo-paletted formats
  444. return width * height;
  445. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  446. }
  447. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
  448. int has_alpha)
  449. {
  450. const PixFmtInfo *pf, *ps;
  451. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  452. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  453. int loss;
  454. ps = &pix_fmt_info[src_pix_fmt];
  455. /* compute loss */
  456. loss = 0;
  457. pf = &pix_fmt_info[dst_pix_fmt];
  458. if (pf->depth < ps->depth ||
  459. ((dst_pix_fmt == AV_PIX_FMT_RGB555BE || dst_pix_fmt == AV_PIX_FMT_RGB555LE ||
  460. dst_pix_fmt == AV_PIX_FMT_BGR555BE || dst_pix_fmt == AV_PIX_FMT_BGR555LE) &&
  461. (src_pix_fmt == AV_PIX_FMT_RGB565BE || src_pix_fmt == AV_PIX_FMT_RGB565LE ||
  462. src_pix_fmt == AV_PIX_FMT_BGR565BE || src_pix_fmt == AV_PIX_FMT_BGR565LE)))
  463. loss |= FF_LOSS_DEPTH;
  464. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  465. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  466. loss |= FF_LOSS_RESOLUTION;
  467. switch(pf->color_type) {
  468. case FF_COLOR_RGB:
  469. if (ps->color_type != FF_COLOR_RGB &&
  470. ps->color_type != FF_COLOR_GRAY)
  471. loss |= FF_LOSS_COLORSPACE;
  472. break;
  473. case FF_COLOR_GRAY:
  474. if (ps->color_type != FF_COLOR_GRAY)
  475. loss |= FF_LOSS_COLORSPACE;
  476. break;
  477. case FF_COLOR_YUV:
  478. if (ps->color_type != FF_COLOR_YUV)
  479. loss |= FF_LOSS_COLORSPACE;
  480. break;
  481. case FF_COLOR_YUV_JPEG:
  482. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  483. ps->color_type != FF_COLOR_YUV &&
  484. ps->color_type != FF_COLOR_GRAY)
  485. loss |= FF_LOSS_COLORSPACE;
  486. break;
  487. default:
  488. /* fail safe test */
  489. if (ps->color_type != pf->color_type)
  490. loss |= FF_LOSS_COLORSPACE;
  491. break;
  492. }
  493. if (pf->color_type == FF_COLOR_GRAY &&
  494. ps->color_type != FF_COLOR_GRAY)
  495. loss |= FF_LOSS_CHROMA;
  496. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  497. loss |= FF_LOSS_ALPHA;
  498. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  499. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  500. loss |= FF_LOSS_COLORQUANT;
  501. return loss;
  502. }
  503. static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
  504. {
  505. int bits;
  506. const PixFmtInfo *pf;
  507. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  508. pf = &pix_fmt_info[pix_fmt];
  509. switch(pf->pixel_type) {
  510. case FF_PIXEL_PACKED:
  511. switch(pix_fmt) {
  512. case AV_PIX_FMT_YUYV422:
  513. case AV_PIX_FMT_UYVY422:
  514. case AV_PIX_FMT_RGB565BE:
  515. case AV_PIX_FMT_RGB565LE:
  516. case AV_PIX_FMT_RGB555BE:
  517. case AV_PIX_FMT_RGB555LE:
  518. case AV_PIX_FMT_RGB444BE:
  519. case AV_PIX_FMT_RGB444LE:
  520. case AV_PIX_FMT_BGR565BE:
  521. case AV_PIX_FMT_BGR565LE:
  522. case AV_PIX_FMT_BGR555BE:
  523. case AV_PIX_FMT_BGR555LE:
  524. case AV_PIX_FMT_BGR444BE:
  525. case AV_PIX_FMT_BGR444LE:
  526. bits = 16;
  527. break;
  528. case AV_PIX_FMT_UYYVYY411:
  529. bits = 12;
  530. break;
  531. default:
  532. bits = pf->depth * pf->nb_channels;
  533. break;
  534. }
  535. break;
  536. case FF_PIXEL_PLANAR:
  537. if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  538. bits = pf->depth * pf->nb_channels;
  539. } else {
  540. bits = pf->depth + ((2 * pf->depth) >>
  541. (desc->log2_chroma_w + desc->log2_chroma_h));
  542. }
  543. break;
  544. case FF_PIXEL_PALETTE:
  545. bits = 8;
  546. break;
  547. default:
  548. bits = -1;
  549. break;
  550. }
  551. return bits;
  552. }
  553. static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
  554. enum AVPixelFormat src_pix_fmt,
  555. int has_alpha,
  556. int loss_mask)
  557. {
  558. int dist, i, loss, min_dist;
  559. enum AVPixelFormat dst_pix_fmt;
  560. /* find exact color match with smallest size */
  561. dst_pix_fmt = AV_PIX_FMT_NONE;
  562. min_dist = 0x7fffffff;
  563. i = 0;
  564. while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
  565. enum AVPixelFormat pix_fmt = pix_fmt_list[i];
  566. if (i > AV_PIX_FMT_NB) {
  567. av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
  568. "it is either not properly terminated or contains duplicates\n");
  569. return AV_PIX_FMT_NONE;
  570. }
  571. loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
  572. if (loss == 0) {
  573. dist = avg_bits_per_pixel(pix_fmt);
  574. if (dist < min_dist) {
  575. min_dist = dist;
  576. dst_pix_fmt = pix_fmt;
  577. }
  578. }
  579. i++;
  580. }
  581. return dst_pix_fmt;
  582. }
  583. #if FF_API_FIND_BEST_PIX_FMT
  584. enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
  585. int has_alpha, int *loss_ptr)
  586. {
  587. enum AVPixelFormat list[64];
  588. int i, j = 0;
  589. // test only the first 64 pixel formats to avoid undefined behaviour
  590. for (i = 0; i < 64; i++) {
  591. if (pix_fmt_mask & (1ULL << i))
  592. list[j++] = i;
  593. }
  594. list[j] = AV_PIX_FMT_NONE;
  595. return avcodec_find_best_pix_fmt2(list, src_pix_fmt, has_alpha, loss_ptr);
  596. }
  597. #endif /* FF_API_FIND_BEST_PIX_FMT */
  598. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  599. enum AVPixelFormat src_pix_fmt,
  600. int has_alpha, int *loss_ptr)
  601. {
  602. enum AVPixelFormat dst_pix_fmt;
  603. int loss_mask, i;
  604. static const int loss_mask_order[] = {
  605. ~0, /* no loss first */
  606. ~FF_LOSS_ALPHA,
  607. ~FF_LOSS_RESOLUTION,
  608. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  609. ~FF_LOSS_COLORQUANT,
  610. ~FF_LOSS_DEPTH,
  611. 0,
  612. };
  613. /* try with successive loss */
  614. i = 0;
  615. for(;;) {
  616. loss_mask = loss_mask_order[i++];
  617. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
  618. has_alpha, loss_mask);
  619. if (dst_pix_fmt >= 0)
  620. goto found;
  621. if (loss_mask == 0)
  622. break;
  623. }
  624. return AV_PIX_FMT_NONE;
  625. found:
  626. if (loss_ptr)
  627. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  628. return dst_pix_fmt;
  629. }
  630. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  631. enum AVPixelFormat pix_fmt, int width, int height)
  632. {
  633. av_image_copy(dst->data, dst->linesize, src->data,
  634. src->linesize, pix_fmt, width, height);
  635. }
  636. /* 2x2 -> 1x1 */
  637. void ff_shrink22(uint8_t *dst, int dst_wrap,
  638. const uint8_t *src, int src_wrap,
  639. int width, int height)
  640. {
  641. int w;
  642. const uint8_t *s1, *s2;
  643. uint8_t *d;
  644. for(;height > 0; height--) {
  645. s1 = src;
  646. s2 = s1 + src_wrap;
  647. d = dst;
  648. for(w = width;w >= 4; w-=4) {
  649. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  650. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  651. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  652. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  653. s1 += 8;
  654. s2 += 8;
  655. d += 4;
  656. }
  657. for(;w > 0; w--) {
  658. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  659. s1 += 2;
  660. s2 += 2;
  661. d++;
  662. }
  663. src += 2 * src_wrap;
  664. dst += dst_wrap;
  665. }
  666. }
  667. /* 4x4 -> 1x1 */
  668. void ff_shrink44(uint8_t *dst, int dst_wrap,
  669. const uint8_t *src, int src_wrap,
  670. int width, int height)
  671. {
  672. int w;
  673. const uint8_t *s1, *s2, *s3, *s4;
  674. uint8_t *d;
  675. for(;height > 0; height--) {
  676. s1 = src;
  677. s2 = s1 + src_wrap;
  678. s3 = s2 + src_wrap;
  679. s4 = s3 + src_wrap;
  680. d = dst;
  681. for(w = width;w > 0; w--) {
  682. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  683. s2[0] + s2[1] + s2[2] + s2[3] +
  684. s3[0] + s3[1] + s3[2] + s3[3] +
  685. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  686. s1 += 4;
  687. s2 += 4;
  688. s3 += 4;
  689. s4 += 4;
  690. d++;
  691. }
  692. src += 4 * src_wrap;
  693. dst += dst_wrap;
  694. }
  695. }
  696. /* 8x8 -> 1x1 */
  697. void ff_shrink88(uint8_t *dst, int dst_wrap,
  698. const uint8_t *src, int src_wrap,
  699. int width, int height)
  700. {
  701. int w, i;
  702. for(;height > 0; height--) {
  703. for(w = width;w > 0; w--) {
  704. int tmp=0;
  705. for(i=0; i<8; i++){
  706. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  707. src += src_wrap;
  708. }
  709. *(dst++) = (tmp + 32)>>6;
  710. src += 8 - 8*src_wrap;
  711. }
  712. src += 8*src_wrap - 8*width;
  713. dst += dst_wrap - width;
  714. }
  715. }
  716. int avpicture_alloc(AVPicture *picture,
  717. enum AVPixelFormat pix_fmt, int width, int height)
  718. {
  719. int ret;
  720. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  721. memset(picture, 0, sizeof(AVPicture));
  722. return ret;
  723. }
  724. return 0;
  725. }
  726. void avpicture_free(AVPicture *picture)
  727. {
  728. av_free(picture->data[0]);
  729. }
  730. /* return true if yuv planar */
  731. static inline int is_yuv_planar(const PixFmtInfo *ps)
  732. {
  733. return (ps->color_type == FF_COLOR_YUV ||
  734. ps->color_type == FF_COLOR_YUV_JPEG) &&
  735. ps->pixel_type == FF_PIXEL_PLANAR;
  736. }
  737. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  738. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  739. {
  740. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  741. int y_shift;
  742. int x_shift;
  743. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  744. return -1;
  745. y_shift = desc->log2_chroma_h;
  746. x_shift = desc->log2_chroma_w;
  747. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  748. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  749. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  750. dst->linesize[0] = src->linesize[0];
  751. dst->linesize[1] = src->linesize[1];
  752. dst->linesize[2] = src->linesize[2];
  753. return 0;
  754. }
  755. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  756. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  757. int *color)
  758. {
  759. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  760. uint8_t *optr;
  761. int y_shift;
  762. int x_shift;
  763. int yheight;
  764. int i, y;
  765. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  766. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  767. for (i = 0; i < 3; i++) {
  768. x_shift = i ? desc->log2_chroma_w : 0;
  769. y_shift = i ? desc->log2_chroma_h : 0;
  770. if (padtop || padleft) {
  771. memset(dst->data[i], color[i],
  772. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  773. }
  774. if (padleft || padright) {
  775. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  776. (dst->linesize[i] - (padright >> x_shift));
  777. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  778. for (y = 0; y < yheight; y++) {
  779. memset(optr, color[i], (padleft + padright) >> x_shift);
  780. optr += dst->linesize[i];
  781. }
  782. }
  783. if (src) { /* first line */
  784. uint8_t *iptr = src->data[i];
  785. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  786. (padleft >> x_shift);
  787. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  788. iptr += src->linesize[i];
  789. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  790. (dst->linesize[i] - (padright >> x_shift));
  791. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  792. for (y = 0; y < yheight; y++) {
  793. memset(optr, color[i], (padleft + padright) >> x_shift);
  794. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  795. (width - padleft - padright) >> x_shift);
  796. iptr += src->linesize[i];
  797. optr += dst->linesize[i];
  798. }
  799. }
  800. if (padbottom || padright) {
  801. optr = dst->data[i] + dst->linesize[i] *
  802. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  803. memset(optr, color[i],dst->linesize[i] *
  804. (padbottom >> y_shift) + (padright >> x_shift));
  805. }
  806. }
  807. return 0;
  808. }
  809. #if !HAVE_MMX_EXTERNAL
  810. /* filter parameters: [-1 4 2 4 -1] // 8 */
  811. static void deinterlace_line_c(uint8_t *dst,
  812. const uint8_t *lum_m4, const uint8_t *lum_m3,
  813. const uint8_t *lum_m2, const uint8_t *lum_m1,
  814. const uint8_t *lum,
  815. int size)
  816. {
  817. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  818. int sum;
  819. for(;size > 0;size--) {
  820. sum = -lum_m4[0];
  821. sum += lum_m3[0] << 2;
  822. sum += lum_m2[0] << 1;
  823. sum += lum_m1[0] << 2;
  824. sum += -lum[0];
  825. dst[0] = cm[(sum + 4) >> 3];
  826. lum_m4++;
  827. lum_m3++;
  828. lum_m2++;
  829. lum_m1++;
  830. lum++;
  831. dst++;
  832. }
  833. }
  834. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  835. uint8_t *lum_m2, uint8_t *lum_m1,
  836. uint8_t *lum, int size)
  837. {
  838. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  839. int sum;
  840. for(;size > 0;size--) {
  841. sum = -lum_m4[0];
  842. sum += lum_m3[0] << 2;
  843. sum += lum_m2[0] << 1;
  844. lum_m4[0]=lum_m2[0];
  845. sum += lum_m1[0] << 2;
  846. sum += -lum[0];
  847. lum_m2[0] = cm[(sum + 4) >> 3];
  848. lum_m4++;
  849. lum_m3++;
  850. lum_m2++;
  851. lum_m1++;
  852. lum++;
  853. }
  854. }
  855. #endif /* !HAVE_MMX_EXTERNAL */
  856. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  857. top field is copied as is, but the bottom field is deinterlaced
  858. against the top field. */
  859. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  860. const uint8_t *src1, int src_wrap,
  861. int width, int height)
  862. {
  863. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  864. int y;
  865. src_m2 = src1;
  866. src_m1 = src1;
  867. src_0=&src_m1[src_wrap];
  868. src_p1=&src_0[src_wrap];
  869. src_p2=&src_p1[src_wrap];
  870. for(y=0;y<(height-2);y+=2) {
  871. memcpy(dst,src_m1,width);
  872. dst += dst_wrap;
  873. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  874. src_m2 = src_0;
  875. src_m1 = src_p1;
  876. src_0 = src_p2;
  877. src_p1 += 2*src_wrap;
  878. src_p2 += 2*src_wrap;
  879. dst += dst_wrap;
  880. }
  881. memcpy(dst,src_m1,width);
  882. dst += dst_wrap;
  883. /* do last line */
  884. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  885. }
  886. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  887. int width, int height)
  888. {
  889. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  890. int y;
  891. uint8_t *buf;
  892. buf = av_malloc(width);
  893. src_m1 = src1;
  894. memcpy(buf,src_m1,width);
  895. src_0=&src_m1[src_wrap];
  896. src_p1=&src_0[src_wrap];
  897. src_p2=&src_p1[src_wrap];
  898. for(y=0;y<(height-2);y+=2) {
  899. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  900. src_m1 = src_p1;
  901. src_0 = src_p2;
  902. src_p1 += 2*src_wrap;
  903. src_p2 += 2*src_wrap;
  904. }
  905. /* do last line */
  906. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  907. av_free(buf);
  908. }
  909. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  910. enum AVPixelFormat pix_fmt, int width, int height)
  911. {
  912. int i;
  913. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  914. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  915. pix_fmt != AV_PIX_FMT_YUV422P &&
  916. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  917. pix_fmt != AV_PIX_FMT_YUV444P &&
  918. pix_fmt != AV_PIX_FMT_YUV411P &&
  919. pix_fmt != AV_PIX_FMT_GRAY8)
  920. return -1;
  921. if ((width & 3) != 0 || (height & 3) != 0)
  922. return -1;
  923. for(i=0;i<3;i++) {
  924. if (i == 1) {
  925. switch(pix_fmt) {
  926. case AV_PIX_FMT_YUVJ420P:
  927. case AV_PIX_FMT_YUV420P:
  928. width >>= 1;
  929. height >>= 1;
  930. break;
  931. case AV_PIX_FMT_YUV422P:
  932. case AV_PIX_FMT_YUVJ422P:
  933. width >>= 1;
  934. break;
  935. case AV_PIX_FMT_YUV411P:
  936. width >>= 2;
  937. break;
  938. default:
  939. break;
  940. }
  941. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  942. break;
  943. }
  944. }
  945. if (src == dst) {
  946. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  947. width, height);
  948. } else {
  949. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  950. src->data[i], src->linesize[i],
  951. width, height);
  952. }
  953. }
  954. emms_c();
  955. return 0;
  956. }