vf_waveform.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * Copyright (c) 2012-2015 Paul B Mahol
  3. * Copyright (c) 2013 Marton Balint
  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. #include "libavutil/avassert.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. enum FilterType {
  30. LOWPASS,
  31. FLAT,
  32. AFLAT,
  33. CHROMA,
  34. ACHROMA,
  35. COLOR,
  36. NB_FILTERS
  37. };
  38. typedef struct WaveformContext {
  39. const AVClass *class;
  40. int mode;
  41. int ncomp;
  42. int pcomp;
  43. const uint8_t *bg_color;
  44. float fintensity;
  45. int intensity;
  46. int mirror;
  47. int display;
  48. int envelope;
  49. int estart[4];
  50. int eend[4];
  51. int *emax[4][4];
  52. int *emin[4][4];
  53. int *peak;
  54. int filter;
  55. int bits;
  56. int max;
  57. int size;
  58. void (*waveform)(struct WaveformContext *s, AVFrame *in, AVFrame *out,
  59. int component, int intensity, int offset, int column);
  60. const AVPixFmtDescriptor *desc;
  61. } WaveformContext;
  62. #define OFFSET(x) offsetof(WaveformContext, x)
  63. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  64. static const AVOption waveform_options[] = {
  65. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
  66. { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
  67. { "row", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  68. { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  69. { "intensity", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 1, FLAGS },
  70. { "i", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 1, FLAGS },
  71. { "mirror", "set mirroring", OFFSET(mirror), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  72. { "r", "set mirroring", OFFSET(mirror), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  73. { "display", "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display" },
  74. { "d", "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display" },
  75. { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "display" },
  76. { "parade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "display" },
  77. { "components", "set components to display", OFFSET(pcomp), AV_OPT_TYPE_INT, {.i64=1}, 1, 15, FLAGS },
  78. { "c", "set components to display", OFFSET(pcomp), AV_OPT_TYPE_INT, {.i64=1}, 1, 15, FLAGS },
  79. { "envelope", "set envelope to display", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope" },
  80. { "e", "set envelope to display", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope" },
  81. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
  82. { "instant", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
  83. { "peak", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
  84. { "peak+instant", NULL, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
  85. { "filter", "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS-1, FLAGS, "filter" },
  86. { "f", "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS-1, FLAGS, "filter" },
  87. { "lowpass", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LOWPASS}, 0, 0, FLAGS, "filter" },
  88. { "flat" , NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "filter" },
  89. { "aflat" , NULL, 0, AV_OPT_TYPE_CONST, {.i64=AFLAT}, 0, 0, FLAGS, "filter" },
  90. { "chroma", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CHROMA}, 0, 0, FLAGS, "filter" },
  91. { "achroma", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ACHROMA}, 0, 0, FLAGS, "filter" },
  92. { "color", NULL, 0, AV_OPT_TYPE_CONST, {.i64=COLOR}, 0, 0, FLAGS, "filter" },
  93. { NULL }
  94. };
  95. AVFILTER_DEFINE_CLASS(waveform);
  96. static const enum AVPixelFormat lowpass_pix_fmts[] = {
  97. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  98. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  99. AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  100. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  101. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  102. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
  103. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
  104. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  105. AV_PIX_FMT_GRAY8,
  106. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
  107. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
  108. AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
  109. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
  110. AV_PIX_FMT_NONE
  111. };
  112. static const enum AVPixelFormat flat_pix_fmts[] = {
  113. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
  114. };
  115. static const enum AVPixelFormat color_pix_fmts[] = {
  116. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  117. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  118. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  119. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
  120. AV_PIX_FMT_NONE
  121. };
  122. static int query_formats(AVFilterContext *ctx)
  123. {
  124. WaveformContext *s = ctx->priv;
  125. AVFilterFormats *fmts_list;
  126. const enum AVPixelFormat *pix_fmts;
  127. switch (s->filter) {
  128. case LOWPASS: pix_fmts = lowpass_pix_fmts; break;
  129. case FLAT:
  130. case AFLAT:
  131. case CHROMA:
  132. case ACHROMA: pix_fmts = flat_pix_fmts; break;
  133. case COLOR: pix_fmts = color_pix_fmts; break;
  134. }
  135. fmts_list = ff_make_format_list(pix_fmts);
  136. if (!fmts_list)
  137. return AVERROR(ENOMEM);
  138. return ff_set_common_formats(ctx, fmts_list);
  139. }
  140. static void envelope_instant16(WaveformContext *s, AVFrame *out, int plane, int component)
  141. {
  142. const int dst_linesize = out->linesize[component] / 2;
  143. const int bg = s->bg_color[component] * (s->max / 256);
  144. const int limit = s->max - 1;
  145. const int is_chroma = (component == 1 || component == 2);
  146. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  147. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  148. const int dst_h = AV_CEIL_RSHIFT(out->height, shift_h);
  149. const int dst_w = AV_CEIL_RSHIFT(out->width, shift_w);
  150. const int start = s->estart[plane];
  151. const int end = s->eend[plane];
  152. uint16_t *dst;
  153. int x, y;
  154. if (s->mode) {
  155. for (x = 0; x < dst_w; x++) {
  156. for (y = start; y < end; y++) {
  157. dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
  158. if (dst[0] != bg) {
  159. dst[0] = limit;
  160. break;
  161. }
  162. }
  163. for (y = end - 1; y >= start; y--) {
  164. dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
  165. if (dst[0] != bg) {
  166. dst[0] = limit;
  167. break;
  168. }
  169. }
  170. }
  171. } else {
  172. for (y = 0; y < dst_h; y++) {
  173. dst = (uint16_t *)out->data[component] + y * dst_linesize;
  174. for (x = start; x < end; x++) {
  175. if (dst[x] != bg) {
  176. dst[x] = limit;
  177. break;
  178. }
  179. }
  180. for (x = end - 1; x >= start; x--) {
  181. if (dst[x] != bg) {
  182. dst[x] = limit;
  183. break;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. static void envelope_instant(WaveformContext *s, AVFrame *out, int plane, int component)
  190. {
  191. const int dst_linesize = out->linesize[component];
  192. const uint8_t bg = s->bg_color[component];
  193. const int is_chroma = (component == 1 || component == 2);
  194. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  195. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  196. const int dst_h = AV_CEIL_RSHIFT(out->height, shift_h);
  197. const int dst_w = AV_CEIL_RSHIFT(out->width, shift_w);
  198. const int start = s->estart[plane];
  199. const int end = s->eend[plane];
  200. uint8_t *dst;
  201. int x, y;
  202. if (s->mode) {
  203. for (x = 0; x < dst_w; x++) {
  204. for (y = start; y < end; y++) {
  205. dst = out->data[component] + y * dst_linesize + x;
  206. if (dst[0] != bg) {
  207. dst[0] = 255;
  208. break;
  209. }
  210. }
  211. for (y = end - 1; y >= start; y--) {
  212. dst = out->data[component] + y * dst_linesize + x;
  213. if (dst[0] != bg) {
  214. dst[0] = 255;
  215. break;
  216. }
  217. }
  218. }
  219. } else {
  220. for (y = 0; y < dst_h; y++) {
  221. dst = out->data[component] + y * dst_linesize;
  222. for (x = start; x < end; x++) {
  223. if (dst[x] != bg) {
  224. dst[x] = 255;
  225. break;
  226. }
  227. }
  228. for (x = end - 1; x >= start; x--) {
  229. if (dst[x] != bg) {
  230. dst[x] = 255;
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. static void envelope_peak16(WaveformContext *s, AVFrame *out, int plane, int component)
  238. {
  239. const int dst_linesize = out->linesize[component] / 2;
  240. const int bg = s->bg_color[component] * (s->max / 256);
  241. const int limit = s->max - 1;
  242. const int is_chroma = (component == 1 || component == 2);
  243. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  244. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  245. const int dst_h = AV_CEIL_RSHIFT(out->height, shift_h);
  246. const int dst_w = AV_CEIL_RSHIFT(out->width, shift_w);
  247. const int start = s->estart[plane];
  248. const int end = s->eend[plane];
  249. int *emax = s->emax[plane][component];
  250. int *emin = s->emin[plane][component];
  251. uint16_t *dst;
  252. int x, y;
  253. if (s->mode) {
  254. for (x = 0; x < dst_w; x++) {
  255. for (y = start; y < end && y < emin[x]; y++) {
  256. dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
  257. if (dst[0] != bg) {
  258. emin[x] = y;
  259. break;
  260. }
  261. }
  262. for (y = end - 1; y >= start && y >= emax[x]; y--) {
  263. dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
  264. if (dst[0] != bg) {
  265. emax[x] = y;
  266. break;
  267. }
  268. }
  269. }
  270. if (s->envelope == 3)
  271. envelope_instant16(s, out, plane, component);
  272. for (x = 0; x < dst_w; x++) {
  273. dst = (uint16_t *)out->data[component] + emin[x] * dst_linesize + x;
  274. dst[0] = limit;
  275. dst = (uint16_t *)out->data[component] + emax[x] * dst_linesize + x;
  276. dst[0] = limit;
  277. }
  278. } else {
  279. for (y = 0; y < dst_h; y++) {
  280. dst = (uint16_t *)out->data[component] + y * dst_linesize;
  281. for (x = start; x < end && x < emin[y]; x++) {
  282. if (dst[x] != bg) {
  283. emin[y] = x;
  284. break;
  285. }
  286. }
  287. for (x = end - 1; x >= start && x >= emax[y]; x--) {
  288. if (dst[x] != bg) {
  289. emax[y] = x;
  290. break;
  291. }
  292. }
  293. }
  294. if (s->envelope == 3)
  295. envelope_instant16(s, out, plane, component);
  296. for (y = 0; y < dst_h; y++) {
  297. dst = (uint16_t *)out->data[component] + y * dst_linesize + emin[y];
  298. dst[0] = limit;
  299. dst = (uint16_t *)out->data[component] + y * dst_linesize + emax[y];
  300. dst[0] = limit;
  301. }
  302. }
  303. }
  304. static void envelope_peak(WaveformContext *s, AVFrame *out, int plane, int component)
  305. {
  306. const int dst_linesize = out->linesize[component];
  307. const int bg = s->bg_color[component];
  308. const int is_chroma = (component == 1 || component == 2);
  309. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  310. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  311. const int dst_h = AV_CEIL_RSHIFT(out->height, shift_h);
  312. const int dst_w = AV_CEIL_RSHIFT(out->width, shift_w);
  313. const int start = s->estart[plane];
  314. const int end = s->eend[plane];
  315. int *emax = s->emax[plane][component];
  316. int *emin = s->emin[plane][component];
  317. uint8_t *dst;
  318. int x, y;
  319. if (s->mode) {
  320. for (x = 0; x < dst_w; x++) {
  321. for (y = start; y < end && y < emin[x]; y++) {
  322. dst = out->data[component] + y * dst_linesize + x;
  323. if (dst[0] != bg) {
  324. emin[x] = y;
  325. break;
  326. }
  327. }
  328. for (y = end - 1; y >= start && y >= emax[x]; y--) {
  329. dst = out->data[component] + y * dst_linesize + x;
  330. if (dst[0] != bg) {
  331. emax[x] = y;
  332. break;
  333. }
  334. }
  335. }
  336. if (s->envelope == 3)
  337. envelope_instant(s, out, plane, component);
  338. for (x = 0; x < dst_w; x++) {
  339. dst = out->data[component] + emin[x] * dst_linesize + x;
  340. dst[0] = 255;
  341. dst = out->data[component] + emax[x] * dst_linesize + x;
  342. dst[0] = 255;
  343. }
  344. } else {
  345. for (y = 0; y < dst_h; y++) {
  346. dst = out->data[component] + y * dst_linesize;
  347. for (x = start; x < end && x < emin[y]; x++) {
  348. if (dst[x] != bg) {
  349. emin[y] = x;
  350. break;
  351. }
  352. }
  353. for (x = end - 1; x >= start && x >= emax[y]; x--) {
  354. if (dst[x] != bg) {
  355. emax[y] = x;
  356. break;
  357. }
  358. }
  359. }
  360. if (s->envelope == 3)
  361. envelope_instant(s, out, plane, component);
  362. for (y = 0; y < dst_h; y++) {
  363. dst = out->data[component] + y * dst_linesize + emin[y];
  364. dst[0] = 255;
  365. dst = out->data[component] + y * dst_linesize + emax[y];
  366. dst[0] = 255;
  367. }
  368. }
  369. }
  370. static void envelope16(WaveformContext *s, AVFrame *out, int plane, int component)
  371. {
  372. if (s->envelope == 0) {
  373. return;
  374. } else if (s->envelope == 1) {
  375. envelope_instant16(s, out, plane, component);
  376. } else {
  377. envelope_peak16(s, out, plane, component);
  378. }
  379. }
  380. static void envelope(WaveformContext *s, AVFrame *out, int plane, int component)
  381. {
  382. if (s->envelope == 0) {
  383. return;
  384. } else if (s->envelope == 1) {
  385. envelope_instant(s, out, plane, component);
  386. } else {
  387. envelope_peak(s, out, plane, component);
  388. }
  389. }
  390. static void update16(uint16_t *target, int max, int intensity, int limit)
  391. {
  392. if (*target <= max)
  393. *target += intensity;
  394. else
  395. *target = limit;
  396. }
  397. static void update(uint8_t *target, int max, int intensity)
  398. {
  399. if (*target <= max)
  400. *target += intensity;
  401. else
  402. *target = 255;
  403. }
  404. static void lowpass16(WaveformContext *s, AVFrame *in, AVFrame *out,
  405. int component, int intensity, int offset, int column)
  406. {
  407. const int plane = s->desc->comp[component].plane;
  408. const int mirror = s->mirror;
  409. const int is_chroma = (component == 1 || component == 2);
  410. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  411. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  412. const int src_linesize = in->linesize[plane] / 2;
  413. const int dst_linesize = out->linesize[plane] / 2;
  414. const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
  415. const int limit = s->max - 1;
  416. const int max = limit - intensity;
  417. const int src_h = AV_CEIL_RSHIFT(in->height, shift_h);
  418. const int src_w = AV_CEIL_RSHIFT(in->width, shift_w);
  419. const uint16_t *src_data = (const uint16_t *)in->data[plane];
  420. uint16_t *dst_data = (uint16_t *)out->data[plane] + (column ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
  421. uint16_t * const dst_bottom_line = dst_data + dst_linesize * ((s->size >> shift_h) - 1);
  422. uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
  423. const uint16_t *p;
  424. int y;
  425. if (!column && mirror)
  426. dst_data += s->size >> shift_w;
  427. for (y = 0; y < src_h; y++) {
  428. const uint16_t *src_data_end = src_data + src_w;
  429. uint16_t *dst = dst_line;
  430. for (p = src_data; p < src_data_end; p++) {
  431. uint16_t *target;
  432. int v = FFMIN(*p, limit);
  433. if (column) {
  434. target = dst++ + dst_signed_linesize * (v >> shift_h);
  435. } else {
  436. if (mirror)
  437. target = dst_data - (v >> shift_w) - 1;
  438. else
  439. target = dst_data + (v >> shift_w);
  440. }
  441. update16(target, max, intensity, limit);
  442. }
  443. src_data += src_linesize;
  444. dst_data += dst_linesize;
  445. }
  446. envelope16(s, out, plane, plane);
  447. }
  448. static void lowpass(WaveformContext *s, AVFrame *in, AVFrame *out,
  449. int component, int intensity, int offset, int column)
  450. {
  451. const int plane = s->desc->comp[component].plane;
  452. const int mirror = s->mirror;
  453. const int is_chroma = (component == 1 || component == 2);
  454. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  455. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  456. const int src_linesize = in->linesize[plane];
  457. const int dst_linesize = out->linesize[plane];
  458. const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
  459. const int max = 255 - intensity;
  460. const int src_h = AV_CEIL_RSHIFT(in->height, shift_h);
  461. const int src_w = AV_CEIL_RSHIFT(in->width, shift_w);
  462. const uint8_t *src_data = in->data[plane];
  463. uint8_t *dst_data = out->data[plane] + (column ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
  464. uint8_t * const dst_bottom_line = dst_data + dst_linesize * ((s->size >> shift_h) - 1);
  465. uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
  466. const uint8_t *p;
  467. int y;
  468. if (!column && mirror)
  469. dst_data += s->size >> shift_w;
  470. for (y = 0; y < src_h; y++) {
  471. const uint8_t *src_data_end = src_data + src_w;
  472. uint8_t *dst = dst_line;
  473. for (p = src_data; p < src_data_end; p++) {
  474. uint8_t *target;
  475. if (column) {
  476. target = dst++ + dst_signed_linesize * (*p >> shift_h);
  477. } else {
  478. if (mirror)
  479. target = dst_data - (*p >> shift_w) - 1;
  480. else
  481. target = dst_data + (*p >> shift_w);
  482. }
  483. update(target, max, intensity);
  484. }
  485. src_data += src_linesize;
  486. dst_data += dst_linesize;
  487. }
  488. envelope(s, out, plane, plane);
  489. }
  490. static void flat(WaveformContext *s, AVFrame *in, AVFrame *out,
  491. int component, int intensity, int offset, int column)
  492. {
  493. const int plane = s->desc->comp[component].plane;
  494. const int mirror = s->mirror;
  495. const int c0_linesize = in->linesize[ plane + 0 ];
  496. const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
  497. const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
  498. const int d0_linesize = out->linesize[ plane + 0 ];
  499. const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
  500. const int max = 255 - intensity;
  501. const int src_h = in->height;
  502. const int src_w = in->width;
  503. int x, y;
  504. if (column) {
  505. const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
  506. const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
  507. for (x = 0; x < src_w; x++) {
  508. const uint8_t *c0_data = in->data[plane + 0];
  509. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  510. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  511. uint8_t *d0_data = out->data[plane] + offset * d0_linesize;
  512. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
  513. uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
  514. uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
  515. uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
  516. uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
  517. for (y = 0; y < src_h; y++) {
  518. const int c0 = c0_data[x] + 256;
  519. const int c1 = FFABS(c1_data[x] - 128) + FFABS(c2_data[x] - 128);
  520. uint8_t *target;
  521. target = d0 + x + d0_signed_linesize * c0;
  522. update(target, max, intensity);
  523. target = d1 + x + d1_signed_linesize * (c0 - c1);
  524. update(target, max, 1);
  525. target = d1 + x + d1_signed_linesize * (c0 + c1);
  526. update(target, max, 1);
  527. c0_data += c0_linesize;
  528. c1_data += c1_linesize;
  529. c2_data += c2_linesize;
  530. d0_data += d0_linesize;
  531. d1_data += d1_linesize;
  532. }
  533. }
  534. } else {
  535. const uint8_t *c0_data = in->data[plane];
  536. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  537. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  538. uint8_t *d0_data = out->data[plane] + offset;
  539. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
  540. if (mirror) {
  541. d0_data += s->size - 1;
  542. d1_data += s->size - 1;
  543. }
  544. for (y = 0; y < src_h; y++) {
  545. for (x = 0; x < src_w; x++) {
  546. int c0 = c0_data[x] + 256;
  547. const int c1 = FFABS(c1_data[x] - 128) + FFABS(c2_data[x] - 128);
  548. uint8_t *target;
  549. if (mirror) {
  550. target = d0_data - c0;
  551. update(target, max, intensity);
  552. target = d1_data - (c0 - c1);
  553. update(target, max, 1);
  554. target = d1_data - (c0 + c1);
  555. update(target, max, 1);
  556. } else {
  557. target = d0_data + c0;
  558. update(target, max, intensity);
  559. target = d1_data + (c0 - c1);
  560. update(target, max, 1);
  561. target = d1_data + (c0 + c1);
  562. update(target, max, 1);
  563. }
  564. }
  565. c0_data += c0_linesize;
  566. c1_data += c1_linesize;
  567. c2_data += c2_linesize;
  568. d0_data += d0_linesize;
  569. d1_data += d1_linesize;
  570. }
  571. }
  572. envelope(s, out, plane, plane);
  573. envelope(s, out, plane, (plane + 1) % s->ncomp);
  574. }
  575. static void aflat(WaveformContext *s, AVFrame *in, AVFrame *out,
  576. int component, int intensity, int offset, int column)
  577. {
  578. const int plane = s->desc->comp[component].plane;
  579. const int mirror = s->mirror;
  580. const int c0_linesize = in->linesize[ plane + 0 ];
  581. const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
  582. const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
  583. const int d0_linesize = out->linesize[ plane + 0 ];
  584. const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
  585. const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
  586. const int max = 255 - intensity;
  587. const int src_h = in->height;
  588. const int src_w = in->width;
  589. int x, y;
  590. if (column) {
  591. const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
  592. const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
  593. const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
  594. for (x = 0; x < src_w; x++) {
  595. const uint8_t *c0_data = in->data[plane + 0];
  596. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  597. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  598. uint8_t *d0_data = out->data[plane] + offset * d0_linesize;
  599. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
  600. uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset * d2_linesize;
  601. uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
  602. uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
  603. uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
  604. uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
  605. uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
  606. uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
  607. for (y = 0; y < src_h; y++) {
  608. const int c0 = c0_data[x] + 128;
  609. const int c1 = c1_data[x] - 128;
  610. const int c2 = c2_data[x] - 128;
  611. uint8_t *target;
  612. target = d0 + x + d0_signed_linesize * c0;
  613. update(target, max, intensity);
  614. target = d1 + x + d1_signed_linesize * (c0 + c1);
  615. update(target, max, 1);
  616. target = d2 + x + d2_signed_linesize * (c0 + c2);
  617. update(target, max, 1);
  618. c0_data += c0_linesize;
  619. c1_data += c1_linesize;
  620. c2_data += c2_linesize;
  621. d0_data += d0_linesize;
  622. d1_data += d1_linesize;
  623. d2_data += d2_linesize;
  624. }
  625. }
  626. } else {
  627. const uint8_t *c0_data = in->data[plane];
  628. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  629. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  630. uint8_t *d0_data = out->data[plane] + offset;
  631. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
  632. uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset;
  633. if (mirror) {
  634. d0_data += s->size - 1;
  635. d1_data += s->size - 1;
  636. d2_data += s->size - 1;
  637. }
  638. for (y = 0; y < src_h; y++) {
  639. for (x = 0; x < src_w; x++) {
  640. const int c0 = c0_data[x] + 128;
  641. const int c1 = c1_data[x] - 128;
  642. const int c2 = c2_data[x] - 128;
  643. uint8_t *target;
  644. if (mirror) {
  645. target = d0_data - c0;
  646. update(target, max, intensity);
  647. target = d1_data - (c0 + c1);
  648. update(target, max, 1);
  649. target = d2_data - (c0 + c2);
  650. update(target, max, 1);
  651. } else {
  652. target = d0_data + c0;
  653. update(target, max, intensity);
  654. target = d1_data + (c0 + c1);
  655. update(target, max, 1);
  656. target = d2_data + (c0 + c2);
  657. update(target, max, 1);
  658. }
  659. }
  660. c0_data += c0_linesize;
  661. c1_data += c1_linesize;
  662. c2_data += c2_linesize;
  663. d0_data += d0_linesize;
  664. d1_data += d1_linesize;
  665. d2_data += d2_linesize;
  666. }
  667. }
  668. envelope(s, out, plane, (plane + 0) % s->ncomp);
  669. envelope(s, out, plane, (plane + 1) % s->ncomp);
  670. envelope(s, out, plane, (plane + 2) % s->ncomp);
  671. }
  672. static void chroma(WaveformContext *s, AVFrame *in, AVFrame *out,
  673. int component, int intensity, int offset, int column)
  674. {
  675. const int plane = s->desc->comp[component].plane;
  676. const int mirror = s->mirror;
  677. const int c0_linesize = in->linesize[(plane + 1) % s->ncomp];
  678. const int c1_linesize = in->linesize[(plane + 2) % s->ncomp];
  679. const int dst_linesize = out->linesize[plane];
  680. const int max = 255 - intensity;
  681. const int src_h = in->height;
  682. const int src_w = in->width;
  683. int x, y;
  684. if (column) {
  685. const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
  686. for (x = 0; x < src_w; x++) {
  687. const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
  688. const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
  689. uint8_t *dst_data = out->data[plane] + offset * dst_linesize;
  690. uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
  691. uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
  692. uint8_t *dst = dst_line;
  693. for (y = 0; y < src_h; y++) {
  694. const int sum = FFABS(c0_data[x] - 128) + FFABS(c1_data[x] - 128);
  695. uint8_t *target;
  696. target = dst + x + dst_signed_linesize * (256 - sum);
  697. update(target, max, intensity);
  698. target = dst + x + dst_signed_linesize * (255 + sum);
  699. update(target, max, intensity);
  700. c0_data += c0_linesize;
  701. c1_data += c1_linesize;
  702. dst_data += dst_linesize;
  703. }
  704. }
  705. } else {
  706. const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
  707. const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
  708. uint8_t *dst_data = out->data[plane] + offset;
  709. if (mirror)
  710. dst_data += s->size - 1;
  711. for (y = 0; y < src_h; y++) {
  712. for (x = 0; x < src_w; x++) {
  713. const int sum = FFABS(c0_data[x] - 128) + FFABS(c1_data[x] - 128);
  714. uint8_t *target;
  715. if (mirror) {
  716. target = dst_data - (256 - sum);
  717. update(target, max, intensity);
  718. target = dst_data - (255 + sum);
  719. update(target, max, intensity);
  720. } else {
  721. target = dst_data + (256 - sum);
  722. update(target, max, intensity);
  723. target = dst_data + (255 + sum);
  724. update(target, max, intensity);
  725. }
  726. }
  727. c0_data += c0_linesize;
  728. c1_data += c1_linesize;
  729. dst_data += dst_linesize;
  730. }
  731. }
  732. envelope(s, out, plane, (plane + 0) % s->ncomp);
  733. }
  734. static void achroma(WaveformContext *s, AVFrame *in, AVFrame *out,
  735. int component, int intensity, int offset, int column)
  736. {
  737. const int plane = s->desc->comp[component].plane;
  738. const int mirror = s->mirror;
  739. const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
  740. const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
  741. const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
  742. const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
  743. const int max = 255 - intensity;
  744. const int src_h = in->height;
  745. const int src_w = in->width;
  746. int x, y;
  747. if (column) {
  748. const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
  749. const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
  750. for (x = 0; x < src_w; x++) {
  751. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  752. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  753. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
  754. uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset * d2_linesize;
  755. uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
  756. uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
  757. uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
  758. uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
  759. for (y = 0; y < src_h; y++) {
  760. const int c1 = c1_data[x] - 128;
  761. const int c2 = c2_data[x] - 128;
  762. uint8_t *target;
  763. target = d1 + x + d1_signed_linesize * (128 + c1);
  764. update(target, max, intensity);
  765. target = d2 + x + d2_signed_linesize * (128 + c2);
  766. update(target, max, intensity);
  767. c1_data += c1_linesize;
  768. c2_data += c2_linesize;
  769. d1_data += d1_linesize;
  770. d2_data += d2_linesize;
  771. }
  772. }
  773. } else {
  774. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  775. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  776. uint8_t *d0_data = out->data[plane] + offset;
  777. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
  778. uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset;
  779. if (mirror) {
  780. d0_data += s->size - 1;
  781. d1_data += s->size - 1;
  782. d2_data += s->size - 1;
  783. }
  784. for (y = 0; y < src_h; y++) {
  785. for (x = 0; x < src_w; x++) {
  786. const int c1 = c1_data[x] - 128;
  787. const int c2 = c2_data[x] - 128;
  788. uint8_t *target;
  789. if (mirror) {
  790. target = d1_data - (128 + c1);
  791. update(target, max, intensity);
  792. target = d2_data - (128 + c2);
  793. update(target, max, intensity);
  794. } else {
  795. target = d1_data + (128 + c1);
  796. update(target, max, intensity);
  797. target = d2_data + (128 + c2);
  798. update(target, max, intensity);
  799. }
  800. }
  801. c1_data += c1_linesize;
  802. c2_data += c2_linesize;
  803. d1_data += d1_linesize;
  804. d2_data += d2_linesize;
  805. }
  806. }
  807. envelope(s, out, plane, (plane + 1) % s->ncomp);
  808. envelope(s, out, plane, (plane + 2) % s->ncomp);
  809. }
  810. static void color16(WaveformContext *s, AVFrame *in, AVFrame *out,
  811. int component, int intensity, int offset, int column)
  812. {
  813. const int plane = s->desc->comp[component].plane;
  814. const int mirror = s->mirror;
  815. const int limit = s->max - 1;
  816. const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0];
  817. const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp];
  818. const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp];
  819. const int c0_linesize = in->linesize[ plane + 0 ] / 2;
  820. const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
  821. const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
  822. const int d0_linesize = out->linesize[ plane + 0 ] / 2;
  823. const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
  824. const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;
  825. const int src_h = in->height;
  826. const int src_w = in->width;
  827. int x, y;
  828. if (s->mode) {
  829. const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
  830. const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
  831. const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
  832. uint16_t *d0_data = (uint16_t *)out->data[plane] + offset * d0_linesize;
  833. uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
  834. uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset * d2_linesize;
  835. uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
  836. uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
  837. uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
  838. uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
  839. uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
  840. uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);
  841. for (y = 0; y < src_h; y++) {
  842. for (x = 0; x < src_w; x++) {
  843. const int c0 = FFMIN(c0_data[x], limit);
  844. const int c1 = c1_data[x];
  845. const int c2 = c2_data[x];
  846. *(d0 + d0_signed_linesize * c0 + x) = c0;
  847. *(d1 + d1_signed_linesize * c0 + x) = c1;
  848. *(d2 + d2_signed_linesize * c0 + x) = c2;
  849. }
  850. c0_data += c0_linesize;
  851. c1_data += c1_linesize;
  852. c2_data += c2_linesize;
  853. d0_data += d0_linesize;
  854. d1_data += d1_linesize;
  855. d2_data += d2_linesize;
  856. }
  857. } else {
  858. uint16_t *d0_data = (uint16_t *)out->data[plane] + offset;
  859. uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset;
  860. uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset;
  861. if (mirror) {
  862. d0_data += s->size - 1;
  863. d1_data += s->size - 1;
  864. d2_data += s->size - 1;
  865. }
  866. for (y = 0; y < src_h; y++) {
  867. for (x = 0; x < src_w; x++) {
  868. const int c0 = FFMIN(c0_data[x], limit);
  869. const int c1 = c1_data[x];
  870. const int c2 = c2_data[x];
  871. if (mirror) {
  872. *(d0_data - c0) = c0;
  873. *(d1_data - c0) = c1;
  874. *(d2_data - c0) = c2;
  875. } else {
  876. *(d0_data + c0) = c0;
  877. *(d1_data + c0) = c1;
  878. *(d2_data + c0) = c2;
  879. }
  880. }
  881. c0_data += c0_linesize;
  882. c1_data += c1_linesize;
  883. c2_data += c2_linesize;
  884. d0_data += d0_linesize;
  885. d1_data += d1_linesize;
  886. d2_data += d2_linesize;
  887. }
  888. }
  889. envelope16(s, out, plane, plane);
  890. }
  891. static void color(WaveformContext *s, AVFrame *in, AVFrame *out,
  892. int component, int intensity, int offset, int column)
  893. {
  894. const int plane = s->desc->comp[component].plane;
  895. const int mirror = s->mirror;
  896. const uint8_t *c0_data = in->data[plane + 0];
  897. const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
  898. const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
  899. const int c0_linesize = in->linesize[ plane + 0 ];
  900. const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
  901. const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
  902. const int d0_linesize = out->linesize[ plane + 0 ];
  903. const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
  904. const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
  905. const int src_h = in->height;
  906. const int src_w = in->width;
  907. int x, y;
  908. if (s->mode) {
  909. const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
  910. const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
  911. const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
  912. uint8_t *d0_data = out->data[plane] + offset * d0_linesize;
  913. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
  914. uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset * d2_linesize;
  915. uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
  916. uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
  917. uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
  918. uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
  919. uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
  920. uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
  921. for (y = 0; y < src_h; y++) {
  922. for (x = 0; x < src_w; x++) {
  923. const int c0 = c0_data[x];
  924. const int c1 = c1_data[x];
  925. const int c2 = c2_data[x];
  926. *(d0 + d0_signed_linesize * c0 + x) = c0;
  927. *(d1 + d1_signed_linesize * c0 + x) = c1;
  928. *(d2 + d2_signed_linesize * c0 + x) = c2;
  929. }
  930. c0_data += c0_linesize;
  931. c1_data += c1_linesize;
  932. c2_data += c2_linesize;
  933. d0_data += d0_linesize;
  934. d1_data += d1_linesize;
  935. d2_data += d2_linesize;
  936. }
  937. } else {
  938. uint8_t *d0_data = out->data[plane] + offset;
  939. uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
  940. uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset;
  941. if (mirror) {
  942. d0_data += s->size - 1;
  943. d1_data += s->size - 1;
  944. d2_data += s->size - 1;
  945. }
  946. for (y = 0; y < src_h; y++) {
  947. for (x = 0; x < src_w; x++) {
  948. const int c0 = c0_data[x];
  949. const int c1 = c1_data[x];
  950. const int c2 = c2_data[x];
  951. if (mirror) {
  952. *(d0_data - c0) = c0;
  953. *(d1_data - c0) = c1;
  954. *(d2_data - c0) = c2;
  955. } else {
  956. *(d0_data + c0) = c0;
  957. *(d1_data + c0) = c1;
  958. *(d2_data + c0) = c2;
  959. }
  960. }
  961. c0_data += c0_linesize;
  962. c1_data += c1_linesize;
  963. c2_data += c2_linesize;
  964. d0_data += d0_linesize;
  965. d1_data += d1_linesize;
  966. d2_data += d2_linesize;
  967. }
  968. }
  969. envelope(s, out, plane, plane);
  970. }
  971. static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
  972. static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
  973. static int config_input(AVFilterLink *inlink)
  974. {
  975. AVFilterContext *ctx = inlink->dst;
  976. WaveformContext *s = ctx->priv;
  977. s->desc = av_pix_fmt_desc_get(inlink->format);
  978. s->ncomp = s->desc->nb_components;
  979. s->bits = s->desc->comp[0].depth;
  980. s->max = 1 << s->bits;
  981. s->intensity = s->fintensity * (s->max - 1);
  982. switch (s->filter) {
  983. case LOWPASS:
  984. s->size = 256;
  985. s->waveform = s->bits > 8 ? lowpass16 : lowpass; break;
  986. case FLAT:
  987. s->size = 256 * 3;
  988. s->waveform = flat; break;
  989. case AFLAT:
  990. s->size = 256 * 2;
  991. s->waveform = aflat; break;
  992. case CHROMA:
  993. s->size = 256 * 2;
  994. s->waveform = chroma; break;
  995. case ACHROMA:
  996. s->size = 256;
  997. s->waveform = achroma; break;
  998. case COLOR:
  999. s->size = 256;
  1000. s->waveform = s->bits > 8 ? color16 : color; break;
  1001. }
  1002. s->size = s->size << (s->bits - 8);
  1003. switch (inlink->format) {
  1004. case AV_PIX_FMT_GBRAP:
  1005. case AV_PIX_FMT_GBRP:
  1006. case AV_PIX_FMT_GBRP9:
  1007. case AV_PIX_FMT_GBRP10:
  1008. s->bg_color = black_gbrp_color;
  1009. break;
  1010. default:
  1011. s->bg_color = black_yuva_color;
  1012. }
  1013. return 0;
  1014. }
  1015. static int config_output(AVFilterLink *outlink)
  1016. {
  1017. AVFilterContext *ctx = outlink->src;
  1018. AVFilterLink *inlink = ctx->inputs[0];
  1019. WaveformContext *s = ctx->priv;
  1020. int comp = 0, i, j = 0, k, p, size, shift;
  1021. for (i = 0; i < s->ncomp; i++) {
  1022. if ((1 << i) & s->pcomp)
  1023. comp++;
  1024. }
  1025. av_freep(&s->peak);
  1026. if (s->mode) {
  1027. outlink->h = s->size * FFMAX(comp * s->display, 1);
  1028. size = inlink->w;
  1029. } else {
  1030. outlink->w = s->size * FFMAX(comp * s->display, 1);
  1031. size = inlink->h;
  1032. }
  1033. s->peak = av_malloc_array(size, 32 * sizeof(*s->peak));
  1034. if (!s->peak)
  1035. return AVERROR(ENOMEM);
  1036. for (p = 0; p < 4; p++) {
  1037. const int is_chroma = (p == 1 || p == 2);
  1038. const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
  1039. const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
  1040. const int plane = s->desc->comp[p].plane;
  1041. int offset;
  1042. if (!((1 << p) & s->pcomp))
  1043. continue;
  1044. shift = s->mode ? shift_h : shift_w;
  1045. for (k = 0; k < 4; k++) {
  1046. s->emax[plane][k] = s->peak + size * (plane * 4 + k + 0);
  1047. s->emin[plane][k] = s->peak + size * (plane * 4 + k + 16);
  1048. }
  1049. offset = j++ * s->size * s->display;
  1050. s->estart[plane] = offset >> shift;
  1051. s->eend[plane] = (offset + s->size - 1) >> shift;
  1052. for (i = 0; i < size; i++) {
  1053. for (k = 0; k < 4; k++) {
  1054. s->emax[plane][k][i] = s->estart[plane];
  1055. s->emin[plane][k][i] = s->eend[plane];
  1056. }
  1057. }
  1058. }
  1059. outlink->sample_aspect_ratio = (AVRational){1,1};
  1060. return 0;
  1061. }
  1062. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  1063. {
  1064. AVFilterContext *ctx = inlink->dst;
  1065. WaveformContext *s = ctx->priv;
  1066. AVFilterLink *outlink = ctx->outputs[0];
  1067. AVFrame *out;
  1068. int i, j, k;
  1069. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1070. if (!out) {
  1071. av_frame_free(&in);
  1072. return AVERROR(ENOMEM);
  1073. }
  1074. out->pts = in->pts;
  1075. for (k = 0; k < s->ncomp; k++) {
  1076. const int is_chroma = (k == 1 || k == 2);
  1077. const int dst_h = AV_CEIL_RSHIFT(outlink->h, (is_chroma ? s->desc->log2_chroma_h : 0));
  1078. const int dst_w = AV_CEIL_RSHIFT(outlink->w, (is_chroma ? s->desc->log2_chroma_w : 0));
  1079. if (s->bits <= 8) {
  1080. for (i = 0; i < dst_h ; i++)
  1081. memset(out->data[s->desc->comp[k].plane] +
  1082. i * out->linesize[s->desc->comp[k].plane],
  1083. s->bg_color[k], dst_w);
  1084. } else {
  1085. const int mult = s->size / 256;
  1086. uint16_t *dst = (uint16_t *)out->data[s->desc->comp[k].plane];
  1087. for (i = 0; i < dst_h ; i++) {
  1088. for (j = 0; j < dst_w; j++)
  1089. dst[j] = s->bg_color[k] * mult;
  1090. dst += out->linesize[s->desc->comp[k].plane] / 2;
  1091. }
  1092. }
  1093. }
  1094. for (k = 0, i = 0; k < s->ncomp; k++) {
  1095. if ((1 << k) & s->pcomp) {
  1096. const int offset = i++ * s->size * s->display;
  1097. s->waveform(s, in, out, k, s->intensity, offset, s->mode);
  1098. }
  1099. }
  1100. av_frame_free(&in);
  1101. return ff_filter_frame(outlink, out);
  1102. }
  1103. static av_cold void uninit(AVFilterContext *ctx)
  1104. {
  1105. WaveformContext *s = ctx->priv;
  1106. av_freep(&s->peak);
  1107. }
  1108. static const AVFilterPad inputs[] = {
  1109. {
  1110. .name = "default",
  1111. .type = AVMEDIA_TYPE_VIDEO,
  1112. .filter_frame = filter_frame,
  1113. .config_props = config_input,
  1114. },
  1115. { NULL }
  1116. };
  1117. static const AVFilterPad outputs[] = {
  1118. {
  1119. .name = "default",
  1120. .type = AVMEDIA_TYPE_VIDEO,
  1121. .config_props = config_output,
  1122. },
  1123. { NULL }
  1124. };
  1125. AVFilter ff_vf_waveform = {
  1126. .name = "waveform",
  1127. .description = NULL_IF_CONFIG_SMALL("Video waveform monitor."),
  1128. .priv_size = sizeof(WaveformContext),
  1129. .priv_class = &waveform_class,
  1130. .query_formats = query_formats,
  1131. .uninit = uninit,
  1132. .inputs = inputs,
  1133. .outputs = outputs,
  1134. };