imgconvert.c 28 KB

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