imgconvert_template.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * templates for 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. #ifndef RGB_OUT
  22. #define RGB_OUT(d, r, g, b) RGBA_OUT(d, r, g, b, 0xff)
  23. #endif
  24. static void glue(yuv420p_to_, RGB_NAME)(AVPicture *dst, const AVPicture *src,
  25. int width, int height)
  26. {
  27. const uint8_t *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr;
  28. uint8_t *d, *d1, *d2;
  29. int w, y, cb, cr, r_add, g_add, b_add, width2;
  30. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  31. unsigned int r, g, b;
  32. d = dst->data[0];
  33. y1_ptr = src->data[0];
  34. cb_ptr = src->data[1];
  35. cr_ptr = src->data[2];
  36. width2 = (width + 1) >> 1;
  37. for(;height >= 2; height -= 2) {
  38. d1 = d;
  39. d2 = d + dst->linesize[0];
  40. y2_ptr = y1_ptr + src->linesize[0];
  41. for(w = width; w >= 2; w -= 2) {
  42. YUV_TO_RGB1_CCIR(cb_ptr[0], cr_ptr[0]);
  43. /* output 4 pixels */
  44. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[0]);
  45. RGB_OUT(d1, r, g, b);
  46. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[1]);
  47. RGB_OUT(d1 + BPP, r, g, b);
  48. YUV_TO_RGB2_CCIR(r, g, b, y2_ptr[0]);
  49. RGB_OUT(d2, r, g, b);
  50. YUV_TO_RGB2_CCIR(r, g, b, y2_ptr[1]);
  51. RGB_OUT(d2 + BPP, r, g, b);
  52. d1 += 2 * BPP;
  53. d2 += 2 * BPP;
  54. y1_ptr += 2;
  55. y2_ptr += 2;
  56. cb_ptr++;
  57. cr_ptr++;
  58. }
  59. /* handle odd width */
  60. if (w) {
  61. YUV_TO_RGB1_CCIR(cb_ptr[0], cr_ptr[0]);
  62. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[0]);
  63. RGB_OUT(d1, r, g, b);
  64. YUV_TO_RGB2_CCIR(r, g, b, y2_ptr[0]);
  65. RGB_OUT(d2, r, g, b);
  66. d1 += BPP;
  67. d2 += BPP;
  68. y1_ptr++;
  69. y2_ptr++;
  70. cb_ptr++;
  71. cr_ptr++;
  72. }
  73. d += 2 * dst->linesize[0];
  74. y1_ptr += 2 * src->linesize[0] - width;
  75. cb_ptr += src->linesize[1] - width2;
  76. cr_ptr += src->linesize[2] - width2;
  77. }
  78. /* handle odd height */
  79. if (height) {
  80. d1 = d;
  81. for(w = width; w >= 2; w -= 2) {
  82. YUV_TO_RGB1_CCIR(cb_ptr[0], cr_ptr[0]);
  83. /* output 2 pixels */
  84. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[0]);
  85. RGB_OUT(d1, r, g, b);
  86. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[1]);
  87. RGB_OUT(d1 + BPP, r, g, b);
  88. d1 += 2 * BPP;
  89. y1_ptr += 2;
  90. cb_ptr++;
  91. cr_ptr++;
  92. }
  93. /* handle width */
  94. if (w) {
  95. YUV_TO_RGB1_CCIR(cb_ptr[0], cr_ptr[0]);
  96. /* output 2 pixels */
  97. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[0]);
  98. RGB_OUT(d1, r, g, b);
  99. d1 += BPP;
  100. y1_ptr++;
  101. cb_ptr++;
  102. cr_ptr++;
  103. }
  104. }
  105. }
  106. static void glue(yuvj420p_to_, RGB_NAME)(AVPicture *dst, const AVPicture *src,
  107. int width, int height)
  108. {
  109. const uint8_t *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr;
  110. uint8_t *d, *d1, *d2;
  111. int w, y, cb, cr, r_add, g_add, b_add, width2;
  112. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  113. unsigned int r, g, b;
  114. d = dst->data[0];
  115. y1_ptr = src->data[0];
  116. cb_ptr = src->data[1];
  117. cr_ptr = src->data[2];
  118. width2 = (width + 1) >> 1;
  119. for(;height >= 2; height -= 2) {
  120. d1 = d;
  121. d2 = d + dst->linesize[0];
  122. y2_ptr = y1_ptr + src->linesize[0];
  123. for(w = width; w >= 2; w -= 2) {
  124. YUV_TO_RGB1(cb_ptr[0], cr_ptr[0]);
  125. /* output 4 pixels */
  126. YUV_TO_RGB2(r, g, b, y1_ptr[0]);
  127. RGB_OUT(d1, r, g, b);
  128. YUV_TO_RGB2(r, g, b, y1_ptr[1]);
  129. RGB_OUT(d1 + BPP, r, g, b);
  130. YUV_TO_RGB2(r, g, b, y2_ptr[0]);
  131. RGB_OUT(d2, r, g, b);
  132. YUV_TO_RGB2(r, g, b, y2_ptr[1]);
  133. RGB_OUT(d2 + BPP, r, g, b);
  134. d1 += 2 * BPP;
  135. d2 += 2 * BPP;
  136. y1_ptr += 2;
  137. y2_ptr += 2;
  138. cb_ptr++;
  139. cr_ptr++;
  140. }
  141. /* handle odd width */
  142. if (w) {
  143. YUV_TO_RGB1(cb_ptr[0], cr_ptr[0]);
  144. YUV_TO_RGB2(r, g, b, y1_ptr[0]);
  145. RGB_OUT(d1, r, g, b);
  146. YUV_TO_RGB2(r, g, b, y2_ptr[0]);
  147. RGB_OUT(d2, r, g, b);
  148. d1 += BPP;
  149. d2 += BPP;
  150. y1_ptr++;
  151. y2_ptr++;
  152. cb_ptr++;
  153. cr_ptr++;
  154. }
  155. d += 2 * dst->linesize[0];
  156. y1_ptr += 2 * src->linesize[0] - width;
  157. cb_ptr += src->linesize[1] - width2;
  158. cr_ptr += src->linesize[2] - width2;
  159. }
  160. /* handle odd height */
  161. if (height) {
  162. d1 = d;
  163. for(w = width; w >= 2; w -= 2) {
  164. YUV_TO_RGB1(cb_ptr[0], cr_ptr[0]);
  165. /* output 2 pixels */
  166. YUV_TO_RGB2(r, g, b, y1_ptr[0]);
  167. RGB_OUT(d1, r, g, b);
  168. YUV_TO_RGB2(r, g, b, y1_ptr[1]);
  169. RGB_OUT(d1 + BPP, r, g, b);
  170. d1 += 2 * BPP;
  171. y1_ptr += 2;
  172. cb_ptr++;
  173. cr_ptr++;
  174. }
  175. /* handle width */
  176. if (w) {
  177. YUV_TO_RGB1(cb_ptr[0], cr_ptr[0]);
  178. /* output 2 pixels */
  179. YUV_TO_RGB2(r, g, b, y1_ptr[0]);
  180. RGB_OUT(d1, r, g, b);
  181. d1 += BPP;
  182. y1_ptr++;
  183. cb_ptr++;
  184. cr_ptr++;
  185. }
  186. }
  187. }
  188. static void glue(RGB_NAME, _to_yuv420p)(AVPicture *dst, const AVPicture *src,
  189. int width, int height)
  190. {
  191. int wrap, wrap3, width2;
  192. int r, g, b, r1, g1, b1, w;
  193. uint8_t *lum, *cb, *cr;
  194. const uint8_t *p;
  195. lum = dst->data[0];
  196. cb = dst->data[1];
  197. cr = dst->data[2];
  198. width2 = (width + 1) >> 1;
  199. wrap = dst->linesize[0];
  200. wrap3 = src->linesize[0];
  201. p = src->data[0];
  202. for(;height>=2;height -= 2) {
  203. for(w = width; w >= 2; w -= 2) {
  204. RGB_IN(r, g, b, p);
  205. r1 = r;
  206. g1 = g;
  207. b1 = b;
  208. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  209. RGB_IN(r, g, b, p + BPP);
  210. r1 += r;
  211. g1 += g;
  212. b1 += b;
  213. lum[1] = RGB_TO_Y_CCIR(r, g, b);
  214. p += wrap3;
  215. lum += wrap;
  216. RGB_IN(r, g, b, p);
  217. r1 += r;
  218. g1 += g;
  219. b1 += b;
  220. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  221. RGB_IN(r, g, b, p + BPP);
  222. r1 += r;
  223. g1 += g;
  224. b1 += b;
  225. lum[1] = RGB_TO_Y_CCIR(r, g, b);
  226. cb[0] = RGB_TO_U_CCIR(r1, g1, b1, 2);
  227. cr[0] = RGB_TO_V_CCIR(r1, g1, b1, 2);
  228. cb++;
  229. cr++;
  230. p += -wrap3 + 2 * BPP;
  231. lum += -wrap + 2;
  232. }
  233. if (w) {
  234. RGB_IN(r, g, b, p);
  235. r1 = r;
  236. g1 = g;
  237. b1 = b;
  238. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  239. p += wrap3;
  240. lum += wrap;
  241. RGB_IN(r, g, b, p);
  242. r1 += r;
  243. g1 += g;
  244. b1 += b;
  245. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  246. cb[0] = RGB_TO_U_CCIR(r1, g1, b1, 1);
  247. cr[0] = RGB_TO_V_CCIR(r1, g1, b1, 1);
  248. cb++;
  249. cr++;
  250. p += -wrap3 + BPP;
  251. lum += -wrap + 1;
  252. }
  253. p += wrap3 + (wrap3 - width * BPP);
  254. lum += wrap + (wrap - width);
  255. cb += dst->linesize[1] - width2;
  256. cr += dst->linesize[2] - width2;
  257. }
  258. /* handle odd height */
  259. if (height) {
  260. for(w = width; w >= 2; w -= 2) {
  261. RGB_IN(r, g, b, p);
  262. r1 = r;
  263. g1 = g;
  264. b1 = b;
  265. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  266. RGB_IN(r, g, b, p + BPP);
  267. r1 += r;
  268. g1 += g;
  269. b1 += b;
  270. lum[1] = RGB_TO_Y_CCIR(r, g, b);
  271. cb[0] = RGB_TO_U_CCIR(r1, g1, b1, 1);
  272. cr[0] = RGB_TO_V_CCIR(r1, g1, b1, 1);
  273. cb++;
  274. cr++;
  275. p += 2 * BPP;
  276. lum += 2;
  277. }
  278. if (w) {
  279. RGB_IN(r, g, b, p);
  280. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  281. cb[0] = RGB_TO_U_CCIR(r, g, b, 0);
  282. cr[0] = RGB_TO_V_CCIR(r, g, b, 0);
  283. }
  284. }
  285. }
  286. static void glue(RGB_NAME, _to_gray)(AVPicture *dst, const AVPicture *src,
  287. int width, int height)
  288. {
  289. const unsigned char *p;
  290. unsigned char *q;
  291. int r, g, b, dst_wrap, src_wrap;
  292. int x, y;
  293. p = src->data[0];
  294. src_wrap = src->linesize[0] - BPP * width;
  295. q = dst->data[0];
  296. dst_wrap = dst->linesize[0] - width;
  297. for(y=0;y<height;y++) {
  298. for(x=0;x<width;x++) {
  299. RGB_IN(r, g, b, p);
  300. q[0] = RGB_TO_Y(r, g, b);
  301. q++;
  302. p += BPP;
  303. }
  304. p += src_wrap;
  305. q += dst_wrap;
  306. }
  307. }
  308. static void glue(gray_to_, RGB_NAME)(AVPicture *dst, const AVPicture *src,
  309. int width, int height)
  310. {
  311. const unsigned char *p;
  312. unsigned char *q;
  313. int r, dst_wrap, src_wrap;
  314. int x, y;
  315. p = src->data[0];
  316. src_wrap = src->linesize[0] - width;
  317. q = dst->data[0];
  318. dst_wrap = dst->linesize[0] - BPP * width;
  319. for(y=0;y<height;y++) {
  320. for(x=0;x<width;x++) {
  321. r = p[0];
  322. RGB_OUT(q, r, r, r);
  323. q += BPP;
  324. p ++;
  325. }
  326. p += src_wrap;
  327. q += dst_wrap;
  328. }
  329. }
  330. static void glue(pal8_to_, RGB_NAME)(AVPicture *dst, const AVPicture *src,
  331. int width, int height)
  332. {
  333. const unsigned char *p;
  334. unsigned char *q;
  335. int r, g, b, dst_wrap, src_wrap;
  336. int x, y;
  337. uint32_t v;
  338. const uint32_t *palette;
  339. p = src->data[0];
  340. src_wrap = src->linesize[0] - width;
  341. palette = (uint32_t *)src->data[1];
  342. q = dst->data[0];
  343. dst_wrap = dst->linesize[0] - BPP * width;
  344. for(y=0;y<height;y++) {
  345. for(x=0;x<width;x++) {
  346. v = palette[p[0]];
  347. r = (v >> 16) & 0xff;
  348. g = (v >> 8) & 0xff;
  349. b = (v) & 0xff;
  350. #ifdef RGBA_OUT
  351. {
  352. int a;
  353. a = (v >> 24) & 0xff;
  354. RGBA_OUT(q, r, g, b, a);
  355. }
  356. #else
  357. RGB_OUT(q, r, g, b);
  358. #endif
  359. q += BPP;
  360. p ++;
  361. }
  362. p += src_wrap;
  363. q += dst_wrap;
  364. }
  365. }
  366. // RGB24 has optimized routines
  367. #if !defined(FMT_RGB32) && !defined(FMT_RGB24)
  368. /* alpha support */
  369. static void glue(rgb32_to_, RGB_NAME)(AVPicture *dst, const AVPicture *src,
  370. int width, int height)
  371. {
  372. const uint8_t *s;
  373. uint8_t *d;
  374. int src_wrap, dst_wrap, j, y;
  375. unsigned int v, r, g, b;
  376. #ifdef RGBA_OUT
  377. unsigned int a;
  378. #endif
  379. s = src->data[0];
  380. src_wrap = src->linesize[0] - width * 4;
  381. d = dst->data[0];
  382. dst_wrap = dst->linesize[0] - width * BPP;
  383. for(y=0;y<height;y++) {
  384. for(j = 0;j < width; j++) {
  385. v = ((const uint32_t *)(s))[0];
  386. r = (v >> 16) & 0xff;
  387. g = (v >> 8) & 0xff;
  388. b = v & 0xff;
  389. #ifdef RGBA_OUT
  390. a = (v >> 24) & 0xff;
  391. RGBA_OUT(d, r, g, b, a);
  392. #else
  393. RGB_OUT(d, r, g, b);
  394. #endif
  395. s += 4;
  396. d += BPP;
  397. }
  398. s += src_wrap;
  399. d += dst_wrap;
  400. }
  401. }
  402. static void glue(RGB_NAME, _to_rgb32)(AVPicture *dst, const AVPicture *src,
  403. int width, int height)
  404. {
  405. const uint8_t *s;
  406. uint8_t *d;
  407. int src_wrap, dst_wrap, j, y;
  408. unsigned int r, g, b;
  409. #ifdef RGBA_IN
  410. unsigned int a;
  411. #endif
  412. s = src->data[0];
  413. src_wrap = src->linesize[0] - width * BPP;
  414. d = dst->data[0];
  415. dst_wrap = dst->linesize[0] - width * 4;
  416. for(y=0;y<height;y++) {
  417. for(j = 0;j < width; j++) {
  418. #ifdef RGBA_IN
  419. RGBA_IN(r, g, b, a, s);
  420. ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;
  421. #else
  422. RGB_IN(r, g, b, s);
  423. ((uint32_t *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;
  424. #endif
  425. d += 4;
  426. s += BPP;
  427. }
  428. s += src_wrap;
  429. d += dst_wrap;
  430. }
  431. }
  432. #endif /* !defined(FMT_RGB32) */
  433. #ifndef FMT_RGB24
  434. static void glue(rgb24_to_, RGB_NAME)(AVPicture *dst, const AVPicture *src,
  435. int width, int height)
  436. {
  437. const uint8_t *s;
  438. uint8_t *d;
  439. int src_wrap, dst_wrap, j, y;
  440. unsigned int r, g, b;
  441. s = src->data[0];
  442. src_wrap = src->linesize[0] - width * 3;
  443. d = dst->data[0];
  444. dst_wrap = dst->linesize[0] - width * BPP;
  445. for(y=0;y<height;y++) {
  446. for(j = 0;j < width; j++) {
  447. r = s[0];
  448. g = s[1];
  449. b = s[2];
  450. RGB_OUT(d, r, g, b);
  451. s += 3;
  452. d += BPP;
  453. }
  454. s += src_wrap;
  455. d += dst_wrap;
  456. }
  457. }
  458. static void glue(RGB_NAME, _to_rgb24)(AVPicture *dst, const AVPicture *src,
  459. int width, int height)
  460. {
  461. const uint8_t *s;
  462. uint8_t *d;
  463. int src_wrap, dst_wrap, j, y;
  464. unsigned int r, g , b;
  465. s = src->data[0];
  466. src_wrap = src->linesize[0] - width * BPP;
  467. d = dst->data[0];
  468. dst_wrap = dst->linesize[0] - width * 3;
  469. for(y=0;y<height;y++) {
  470. for(j = 0;j < width; j++) {
  471. RGB_IN(r, g, b, s)
  472. d[0] = r;
  473. d[1] = g;
  474. d[2] = b;
  475. d += 3;
  476. s += BPP;
  477. }
  478. s += src_wrap;
  479. d += dst_wrap;
  480. }
  481. }
  482. #endif /* !FMT_RGB24 */
  483. #ifdef FMT_RGB24
  484. static void yuv444p_to_rgb24(AVPicture *dst, const AVPicture *src,
  485. int width, int height)
  486. {
  487. const uint8_t *y1_ptr, *cb_ptr, *cr_ptr;
  488. uint8_t *d, *d1;
  489. int w, y, cb, cr, r_add, g_add, b_add;
  490. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  491. unsigned int r, g, b;
  492. d = dst->data[0];
  493. y1_ptr = src->data[0];
  494. cb_ptr = src->data[1];
  495. cr_ptr = src->data[2];
  496. for(;height > 0; height --) {
  497. d1 = d;
  498. for(w = width; w > 0; w--) {
  499. YUV_TO_RGB1_CCIR(cb_ptr[0], cr_ptr[0]);
  500. YUV_TO_RGB2_CCIR(r, g, b, y1_ptr[0]);
  501. RGB_OUT(d1, r, g, b);
  502. d1 += BPP;
  503. y1_ptr++;
  504. cb_ptr++;
  505. cr_ptr++;
  506. }
  507. d += dst->linesize[0];
  508. y1_ptr += src->linesize[0] - width;
  509. cb_ptr += src->linesize[1] - width;
  510. cr_ptr += src->linesize[2] - width;
  511. }
  512. }
  513. static void yuvj444p_to_rgb24(AVPicture *dst, const AVPicture *src,
  514. int width, int height)
  515. {
  516. const uint8_t *y1_ptr, *cb_ptr, *cr_ptr;
  517. uint8_t *d, *d1;
  518. int w, y, cb, cr, r_add, g_add, b_add;
  519. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  520. unsigned int r, g, b;
  521. d = dst->data[0];
  522. y1_ptr = src->data[0];
  523. cb_ptr = src->data[1];
  524. cr_ptr = src->data[2];
  525. for(;height > 0; height --) {
  526. d1 = d;
  527. for(w = width; w > 0; w--) {
  528. YUV_TO_RGB1(cb_ptr[0], cr_ptr[0]);
  529. YUV_TO_RGB2(r, g, b, y1_ptr[0]);
  530. RGB_OUT(d1, r, g, b);
  531. d1 += BPP;
  532. y1_ptr++;
  533. cb_ptr++;
  534. cr_ptr++;
  535. }
  536. d += dst->linesize[0];
  537. y1_ptr += src->linesize[0] - width;
  538. cb_ptr += src->linesize[1] - width;
  539. cr_ptr += src->linesize[2] - width;
  540. }
  541. }
  542. static void rgb24_to_yuv444p(AVPicture *dst, const AVPicture *src,
  543. int width, int height)
  544. {
  545. int src_wrap, x, y;
  546. int r, g, b;
  547. uint8_t *lum, *cb, *cr;
  548. const uint8_t *p;
  549. lum = dst->data[0];
  550. cb = dst->data[1];
  551. cr = dst->data[2];
  552. src_wrap = src->linesize[0] - width * BPP;
  553. p = src->data[0];
  554. for(y=0;y<height;y++) {
  555. for(x=0;x<width;x++) {
  556. RGB_IN(r, g, b, p);
  557. lum[0] = RGB_TO_Y_CCIR(r, g, b);
  558. cb[0] = RGB_TO_U_CCIR(r, g, b, 0);
  559. cr[0] = RGB_TO_V_CCIR(r, g, b, 0);
  560. p += BPP;
  561. cb++;
  562. cr++;
  563. lum++;
  564. }
  565. p += src_wrap;
  566. lum += dst->linesize[0] - width;
  567. cb += dst->linesize[1] - width;
  568. cr += dst->linesize[2] - width;
  569. }
  570. }
  571. static void rgb24_to_yuvj420p(AVPicture *dst, const AVPicture *src,
  572. int width, int height)
  573. {
  574. int wrap, wrap3, width2;
  575. int r, g, b, r1, g1, b1, w;
  576. uint8_t *lum, *cb, *cr;
  577. const uint8_t *p;
  578. lum = dst->data[0];
  579. cb = dst->data[1];
  580. cr = dst->data[2];
  581. width2 = (width + 1) >> 1;
  582. wrap = dst->linesize[0];
  583. wrap3 = src->linesize[0];
  584. p = src->data[0];
  585. for(;height>=2;height -= 2) {
  586. for(w = width; w >= 2; w -= 2) {
  587. RGB_IN(r, g, b, p);
  588. r1 = r;
  589. g1 = g;
  590. b1 = b;
  591. lum[0] = RGB_TO_Y(r, g, b);
  592. RGB_IN(r, g, b, p + BPP);
  593. r1 += r;
  594. g1 += g;
  595. b1 += b;
  596. lum[1] = RGB_TO_Y(r, g, b);
  597. p += wrap3;
  598. lum += wrap;
  599. RGB_IN(r, g, b, p);
  600. r1 += r;
  601. g1 += g;
  602. b1 += b;
  603. lum[0] = RGB_TO_Y(r, g, b);
  604. RGB_IN(r, g, b, p + BPP);
  605. r1 += r;
  606. g1 += g;
  607. b1 += b;
  608. lum[1] = RGB_TO_Y(r, g, b);
  609. cb[0] = RGB_TO_U(r1, g1, b1, 2);
  610. cr[0] = RGB_TO_V(r1, g1, b1, 2);
  611. cb++;
  612. cr++;
  613. p += -wrap3 + 2 * BPP;
  614. lum += -wrap + 2;
  615. }
  616. if (w) {
  617. RGB_IN(r, g, b, p);
  618. r1 = r;
  619. g1 = g;
  620. b1 = b;
  621. lum[0] = RGB_TO_Y(r, g, b);
  622. p += wrap3;
  623. lum += wrap;
  624. RGB_IN(r, g, b, p);
  625. r1 += r;
  626. g1 += g;
  627. b1 += b;
  628. lum[0] = RGB_TO_Y(r, g, b);
  629. cb[0] = RGB_TO_U(r1, g1, b1, 1);
  630. cr[0] = RGB_TO_V(r1, g1, b1, 1);
  631. cb++;
  632. cr++;
  633. p += -wrap3 + BPP;
  634. lum += -wrap + 1;
  635. }
  636. p += wrap3 + (wrap3 - width * BPP);
  637. lum += wrap + (wrap - width);
  638. cb += dst->linesize[1] - width2;
  639. cr += dst->linesize[2] - width2;
  640. }
  641. /* handle odd height */
  642. if (height) {
  643. for(w = width; w >= 2; w -= 2) {
  644. RGB_IN(r, g, b, p);
  645. r1 = r;
  646. g1 = g;
  647. b1 = b;
  648. lum[0] = RGB_TO_Y(r, g, b);
  649. RGB_IN(r, g, b, p + BPP);
  650. r1 += r;
  651. g1 += g;
  652. b1 += b;
  653. lum[1] = RGB_TO_Y(r, g, b);
  654. cb[0] = RGB_TO_U(r1, g1, b1, 1);
  655. cr[0] = RGB_TO_V(r1, g1, b1, 1);
  656. cb++;
  657. cr++;
  658. p += 2 * BPP;
  659. lum += 2;
  660. }
  661. if (w) {
  662. RGB_IN(r, g, b, p);
  663. lum[0] = RGB_TO_Y(r, g, b);
  664. cb[0] = RGB_TO_U(r, g, b, 0);
  665. cr[0] = RGB_TO_V(r, g, b, 0);
  666. }
  667. }
  668. }
  669. static void rgb24_to_yuvj444p(AVPicture *dst, const AVPicture *src,
  670. int width, int height)
  671. {
  672. int src_wrap, x, y;
  673. int r, g, b;
  674. uint8_t *lum, *cb, *cr;
  675. const uint8_t *p;
  676. lum = dst->data[0];
  677. cb = dst->data[1];
  678. cr = dst->data[2];
  679. src_wrap = src->linesize[0] - width * BPP;
  680. p = src->data[0];
  681. for(y=0;y<height;y++) {
  682. for(x=0;x<width;x++) {
  683. RGB_IN(r, g, b, p);
  684. lum[0] = RGB_TO_Y(r, g, b);
  685. cb[0] = RGB_TO_U(r, g, b, 0);
  686. cr[0] = RGB_TO_V(r, g, b, 0);
  687. p += BPP;
  688. cb++;
  689. cr++;
  690. lum++;
  691. }
  692. p += src_wrap;
  693. lum += dst->linesize[0] - width;
  694. cb += dst->linesize[1] - width;
  695. cr += dst->linesize[2] - width;
  696. }
  697. }
  698. #endif /* FMT_RGB24 */
  699. #if defined(FMT_RGB24) || defined(FMT_RGB32)
  700. static void glue(RGB_NAME, _to_pal8)(AVPicture *dst, const AVPicture *src,
  701. int width, int height)
  702. {
  703. const unsigned char *p;
  704. unsigned char *q;
  705. int dst_wrap, src_wrap;
  706. int x, y, has_alpha;
  707. unsigned int r, g, b;
  708. p = src->data[0];
  709. src_wrap = src->linesize[0] - BPP * width;
  710. q = dst->data[0];
  711. dst_wrap = dst->linesize[0] - width;
  712. has_alpha = 0;
  713. for(y=0;y<height;y++) {
  714. for(x=0;x<width;x++) {
  715. #ifdef RGBA_IN
  716. {
  717. unsigned int a;
  718. RGBA_IN(r, g, b, a, p);
  719. /* crude approximation for alpha ! */
  720. if (a < 0x80) {
  721. has_alpha = 1;
  722. q[0] = TRANSP_INDEX;
  723. } else {
  724. q[0] = gif_clut_index(r, g, b);
  725. }
  726. }
  727. #else
  728. RGB_IN(r, g, b, p);
  729. q[0] = gif_clut_index(r, g, b);
  730. #endif
  731. q++;
  732. p += BPP;
  733. }
  734. p += src_wrap;
  735. q += dst_wrap;
  736. }
  737. build_rgb_palette(dst->data[1], has_alpha);
  738. }
  739. #endif /* defined(FMT_RGB24) || defined(FMT_RGB32) */
  740. #ifdef RGBA_IN
  741. static int glue(get_alpha_info_, RGB_NAME)(const AVPicture *src,
  742. int width, int height)
  743. {
  744. const unsigned char *p;
  745. int src_wrap, ret, x, y;
  746. unsigned int r, g, b, a;
  747. p = src->data[0];
  748. src_wrap = src->linesize[0] - BPP * width;
  749. ret = 0;
  750. for(y=0;y<height;y++) {
  751. for(x=0;x<width;x++) {
  752. RGBA_IN(r, g, b, a, p);
  753. if (a == 0x00) {
  754. ret |= FF_ALPHA_TRANSP;
  755. } else if (a != 0xff) {
  756. ret |= FF_ALPHA_SEMI_TRANSP;
  757. }
  758. p += BPP;
  759. }
  760. p += src_wrap;
  761. }
  762. return ret;
  763. }
  764. #endif /* RGBA_IN */
  765. #undef RGB_IN
  766. #undef RGBA_IN
  767. #undef RGB_OUT
  768. #undef RGBA_OUT
  769. #undef BPP
  770. #undef RGB_NAME
  771. #undef FMT_RGB24
  772. #undef FMT_RGB32