FFmpeg  4.4.8
vf_colormatrix.c
Go to the documentation of this file.
1 /*
2  * ColorMatrix v2.2 for Avisynth 2.5.x
3  *
4  * Copyright (C) 2006-2007 Kevin Stone
5  *
6  * ColorMatrix 1.x is Copyright (C) Wilbert Dijkhof
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * ColorMatrix 2.0 is based on the original ColorMatrix filter by Wilbert
26  * Dijkhof. It adds the ability to convert between any of: Rec.709, FCC,
27  * Rec.601, and SMPTE 240M. It also makes pre and post clipping optional,
28  * adds an option to use scaled or non-scaled coefficients, and more...
29  */
30 
31 #include <float.h>
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/avstring.h"
40 
41 #define NS(n) ((n) < 0 ? (int)((n)*65536.0-0.5+DBL_EPSILON) : (int)((n)*65536.0+0.5))
42 #define CB(n) av_clip_uint8(n)
43 
44 static const double yuv_coeff_luma[5][3] = {
45  { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
46  { +0.5900, +0.1100, +0.3000 }, // FCC (1)
47  { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
48  { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
49  { +0.6780, +0.0593, +0.2627 }, // Rec.2020 (4)
50 };
51 
52 enum ColorMode {
60 };
61 
62 typedef struct ColorMatrixContext {
63  const AVClass *class;
64  int yuv_convert[25][3][3];
66  int source, dest; ///< ColorMode
67  int mode;
68  int hsub, vsub;
70 
71 typedef struct ThreadData {
72  AVFrame *dst;
73  const AVFrame *src;
74  int c2;
75  int c3;
76  int c4;
77  int c5;
78  int c6;
79  int c7;
80 } ThreadData;
81 
82 #define OFFSET(x) offsetof(ColorMatrixContext, x)
83 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
84 
85 static const AVOption colormatrix_options[] = {
86  { "src", "set source color matrix", OFFSET(source), AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
87  { "dst", "set destination color matrix", OFFSET(dest), AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
88  { "bt709", "set BT.709 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT709}, .flags=FLAGS, .unit="color_mode" },
89  { "fcc", "set FCC colorspace ", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_FCC}, .flags=FLAGS, .unit="color_mode" },
90  { "bt601", "set BT.601 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
91  { "bt470", "set BT.470 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
92  { "bt470bg", "set BT.470 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
93  { "smpte170m", "set SMTPE-170M colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
94  { "smpte240m", "set SMPTE-240M colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_SMPTE240M}, .flags=FLAGS, .unit="color_mode" },
95  { "bt2020", "set BT.2020 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT2020}, .flags=FLAGS, .unit="color_mode" },
96  { NULL }
97 };
98 
99 AVFILTER_DEFINE_CLASS(colormatrix);
100 
101 #define ma m[0][0]
102 #define mb m[0][1]
103 #define mc m[0][2]
104 #define md m[1][0]
105 #define me m[1][1]
106 #define mf m[1][2]
107 #define mg m[2][0]
108 #define mh m[2][1]
109 #define mi m[2][2]
110 
111 #define ima im[0][0]
112 #define imb im[0][1]
113 #define imc im[0][2]
114 #define imd im[1][0]
115 #define ime im[1][1]
116 #define imf im[1][2]
117 #define img im[2][0]
118 #define imh im[2][1]
119 #define imi im[2][2]
120 
121 static void inverse3x3(double im[3][3], double m[3][3])
122 {
123  double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
124  det = 1.0 / det;
125  ima = det * (me * mi - mf * mh);
126  imb = det * (mc * mh - mb * mi);
127  imc = det * (mb * mf - mc * me);
128  imd = det * (mf * mg - md * mi);
129  ime = det * (ma * mi - mc * mg);
130  imf = det * (mc * md - ma * mf);
131  img = det * (md * mh - me * mg);
132  imh = det * (mb * mg - ma * mh);
133  imi = det * (ma * me - mb * md);
134 }
135 
136 static void solve_coefficients(double cm[3][3], double rgb[3][3], double yuv[3][3])
137 {
138  int i, j;
139  for (i = 0; i < 3; i++)
140  for (j = 0; j < 3; j++)
141  cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
142 }
143 
145 {
146  ColorMatrixContext *color = ctx->priv;
147  double yuv_coeff[5][3][3];
148  double rgb_coeffd[5][3][3];
149  double yuv_convertd[25][3][3];
150  double bscale, rscale;
151  int v = 0;
152  int i, j, k;
153  for (i = 0; i < 5; i++) {
154  yuv_coeff[i][0][0] = yuv_coeff_luma[i][0];
155  yuv_coeff[i][0][1] = yuv_coeff_luma[i][1];
156  yuv_coeff[i][0][2] = yuv_coeff_luma[i][2];
157  bscale = 0.5 / (yuv_coeff[i][0][1] - 1.0);
158  rscale = 0.5 / (yuv_coeff[i][0][2] - 1.0);
159  yuv_coeff[i][1][0] = bscale * yuv_coeff[i][0][0];
160  yuv_coeff[i][1][1] = 0.5;
161  yuv_coeff[i][1][2] = bscale * yuv_coeff[i][0][2];
162  yuv_coeff[i][2][0] = rscale * yuv_coeff[i][0][0];
163  yuv_coeff[i][2][1] = rscale * yuv_coeff[i][0][1];
164  yuv_coeff[i][2][2] = 0.5;
165  }
166  for (i = 0; i < 5; i++)
167  inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
168  for (i = 0; i < 5; i++) {
169  for (j = 0; j < 5; j++) {
170  solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
171  for (k = 0; k < 3; k++) {
172  color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
173  color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
174  color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
175  }
176  if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
177  color->yuv_convert[v][2][0] != 0) {
178  av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
179  }
180  v++;
181  }
182  }
183 }
184 
185 static const char * const color_modes[] = {"bt709", "fcc", "bt601", "smpte240m", "bt2020"};
186 
188 {
189  ColorMatrixContext *color = ctx->priv;
190 
191  if (color->dest == COLOR_MODE_NONE) {
192  av_log(ctx, AV_LOG_ERROR, "Unspecified destination color space\n");
193  return AVERROR(EINVAL);
194  }
195 
196  if (color->source == color->dest) {
197  av_log(ctx, AV_LOG_ERROR, "Source and destination color space must not be identical\n");
198  return AVERROR(EINVAL);
199  }
200 
202 
203  return 0;
204 }
205 
206 static int process_slice_uyvy422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
207 {
208  const ThreadData *td = arg;
209  const AVFrame *src = td->src;
210  AVFrame *dst = td->dst;
211  const int height = src->height;
212  const int width = src->width*2;
213  const int src_pitch = src->linesize[0];
214  const int dst_pitch = dst->linesize[0];
215  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
216  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
217  const unsigned char *srcp = src->data[0] + slice_start * src_pitch;
218  unsigned char *dstp = dst->data[0] + slice_start * dst_pitch;
219  const int c2 = td->c2;
220  const int c3 = td->c3;
221  const int c4 = td->c4;
222  const int c5 = td->c5;
223  const int c6 = td->c6;
224  const int c7 = td->c7;
225  int x, y;
226 
227  for (y = slice_start; y < slice_end; y++) {
228  for (x = 0; x < width; x += 4) {
229  const int u = srcp[x + 0] - 128;
230  const int v = srcp[x + 2] - 128;
231  const int uvval = c2 * u + c3 * v + 1081344;
232  dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
233  dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
234  dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
235  dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
236  }
237  srcp += src_pitch;
238  dstp += dst_pitch;
239  }
240 
241  return 0;
242 }
243 
244 static int process_slice_yuv444p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
245 {
246  const ThreadData *td = arg;
247  const AVFrame *src = td->src;
248  AVFrame *dst = td->dst;
249  const int height = src->height;
250  const int width = src->width;
251  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
252  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
253  const int src_pitchY = src->linesize[0];
254  const int src_pitchUV = src->linesize[1];
255  const unsigned char *srcpU = src->data[1] + slice_start * src_pitchUV;
256  const unsigned char *srcpV = src->data[2] + slice_start * src_pitchUV;
257  const unsigned char *srcpY = src->data[0] + slice_start * src_pitchY;
258  const int dst_pitchY = dst->linesize[0];
259  const int dst_pitchUV = dst->linesize[1];
260  unsigned char *dstpU = dst->data[1] + slice_start * dst_pitchUV;
261  unsigned char *dstpV = dst->data[2] + slice_start * dst_pitchUV;
262  unsigned char *dstpY = dst->data[0] + slice_start * dst_pitchY;
263  const int c2 = td->c2;
264  const int c3 = td->c3;
265  const int c4 = td->c4;
266  const int c5 = td->c5;
267  const int c6 = td->c6;
268  const int c7 = td->c7;
269  int x, y;
270 
271  for (y = slice_start; y < slice_end; y++) {
272  for (x = 0; x < width; x++) {
273  const int u = srcpU[x] - 128;
274  const int v = srcpV[x] - 128;
275  const int uvval = c2 * u + c3 * v + 1081344;
276  dstpY[x] = CB((65536 * (srcpY[x] - 16) + uvval) >> 16);
277  dstpU[x] = CB((c4 * u + c5 * v + 8421376) >> 16);
278  dstpV[x] = CB((c6 * u + c7 * v + 8421376) >> 16);
279  }
280  srcpY += src_pitchY;
281  dstpY += dst_pitchY;
282  srcpU += src_pitchUV;
283  srcpV += src_pitchUV;
284  dstpU += dst_pitchUV;
285  dstpV += dst_pitchUV;
286  }
287 
288  return 0;
289 }
290 
291 static int process_slice_yuv422p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
292 {
293  const ThreadData *td = arg;
294  const AVFrame *src = td->src;
295  AVFrame *dst = td->dst;
296  const int height = src->height;
297  const int width = src->width;
298  const int slice_start = ff_slice_pos(height, jobnr, nb_jobs);
299  const int slice_end = ff_slice_pos(height, jobnr + 1, nb_jobs);
300  const int src_pitchY = src->linesize[0];
301  const int src_pitchUV = src->linesize[1];
302  const unsigned char *srcpU = src->data[1] + slice_start * src_pitchUV;
303  const unsigned char *srcpV = src->data[2] + slice_start * src_pitchUV;
304  const unsigned char *srcpY = src->data[0] + slice_start * src_pitchY;
305  const int dst_pitchY = dst->linesize[0];
306  const int dst_pitchUV = dst->linesize[1];
307  unsigned char *dstpU = dst->data[1] + slice_start * dst_pitchUV;
308  unsigned char *dstpV = dst->data[2] + slice_start * dst_pitchUV;
309  unsigned char *dstpY = dst->data[0] + slice_start * dst_pitchY;
310  const int c2 = td->c2;
311  const int c3 = td->c3;
312  const int c4 = td->c4;
313  const int c5 = td->c5;
314  const int c6 = td->c6;
315  const int c7 = td->c7;
316  int x, y;
317 
318  for (y = slice_start; y < slice_end; y++) {
319  for (x = 0; x < width; x += 2) {
320  const int u = srcpU[x >> 1] - 128;
321  const int v = srcpV[x >> 1] - 128;
322  const int uvval = c2 * u + c3 * v + 1081344;
323  dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
324  dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
325  dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
326  dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
327  }
328  srcpY += src_pitchY;
329  dstpY += dst_pitchY;
330  srcpU += src_pitchUV;
331  srcpV += src_pitchUV;
332  dstpU += dst_pitchUV;
333  dstpV += dst_pitchUV;
334  }
335 
336  return 0;
337 }
338 
339 static int process_slice_yuv420p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
340 {
341  const ThreadData *td = arg;
342  const AVFrame *src = td->src;
343  AVFrame *dst = td->dst;
344  const int height = FFALIGN(src->height, 2) >> 1;
345  const int width = src->width;
346  const int slice_start = (ff_slice_pos(height, jobnr, nb_jobs)) << 1;
347  const int slice_end = (ff_slice_pos(height, jobnr + 1, nb_jobs)) << 1;
348  const int src_pitchY = src->linesize[0];
349  const int src_pitchUV = src->linesize[1];
350  const int dst_pitchY = dst->linesize[0];
351  const int dst_pitchUV = dst->linesize[1];
352  const unsigned char *srcpY = src->data[0] + src_pitchY * slice_start;
353  const unsigned char *srcpU = src->data[1] + src_pitchUV * (slice_start >> 1);
354  const unsigned char *srcpV = src->data[2] + src_pitchUV * (slice_start >> 1);
355  const unsigned char *srcpN = src->data[0] + src_pitchY * (slice_start + 1);
356  unsigned char *dstpU = dst->data[1] + dst_pitchUV * (slice_start >> 1);
357  unsigned char *dstpV = dst->data[2] + dst_pitchUV * (slice_start >> 1);
358  unsigned char *dstpY = dst->data[0] + dst_pitchY * slice_start;
359  unsigned char *dstpN = dst->data[0] + dst_pitchY * (slice_start + 1);
360  const int c2 = td->c2;
361  const int c3 = td->c3;
362  const int c4 = td->c4;
363  const int c5 = td->c5;
364  const int c6 = td->c6;
365  const int c7 = td->c7;
366  int x, y;
367 
368  for (y = slice_start; y < slice_end; y += 2) {
369  for (x = 0; x < width; x += 2) {
370  const int u = srcpU[x >> 1] - 128;
371  const int v = srcpV[x >> 1] - 128;
372  const int uvval = c2 * u + c3 * v + 1081344;
373  dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
374  dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
375  dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
376  dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
377  dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
378  dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
379  }
380  srcpY += src_pitchY << 1;
381  dstpY += dst_pitchY << 1;
382  srcpN += src_pitchY << 1;
383  dstpN += dst_pitchY << 1;
384  srcpU += src_pitchUV;
385  srcpV += src_pitchUV;
386  dstpU += dst_pitchUV;
387  dstpV += dst_pitchUV;
388  }
389 
390  return 0;
391 }
392 
393 static int config_input(AVFilterLink *inlink)
394 {
395  AVFilterContext *ctx = inlink->dst;
396  ColorMatrixContext *color = ctx->priv;
397  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
398 
399  color->hsub = pix_desc->log2_chroma_w;
400  color->vsub = pix_desc->log2_chroma_h;
401 
402  av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n",
403  color_modes[color->source], color_modes[color->dest]);
404 
405  return 0;
406 }
407 
409 {
410  static const enum AVPixelFormat pix_fmts[] = {
416  };
418  if (!fmts_list)
419  return AVERROR(ENOMEM);
420  return ff_set_common_formats(ctx, fmts_list);
421 }
422 
423 static int filter_frame(AVFilterLink *link, AVFrame *in)
424 {
425  AVFilterContext *ctx = link->dst;
426  ColorMatrixContext *color = ctx->priv;
427  AVFilterLink *outlink = ctx->outputs[0];
428  AVFrame *out;
429  ThreadData td = {0};
430 
431  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
432  if (!out) {
433  av_frame_free(&in);
434  return AVERROR(ENOMEM);
435  }
437 
438  if (color->source == COLOR_MODE_NONE) {
439  enum AVColorSpace cs = in->colorspace;
440  enum ColorMode source;
441 
442  switch(cs) {
443  case AVCOL_SPC_BT709 : source = COLOR_MODE_BT709 ; break;
444  case AVCOL_SPC_FCC : source = COLOR_MODE_FCC ; break;
445  case AVCOL_SPC_SMPTE240M : source = COLOR_MODE_SMPTE240M ; break;
446  case AVCOL_SPC_BT470BG : source = COLOR_MODE_BT601 ; break;
447  case AVCOL_SPC_SMPTE170M : source = COLOR_MODE_BT601 ; break;
448  case AVCOL_SPC_BT2020_NCL: source = COLOR_MODE_BT2020 ; break;
449  case AVCOL_SPC_BT2020_CL : source = COLOR_MODE_BT2020 ; break;
450  default :
451  av_log(ctx, AV_LOG_ERROR, "Input frame does not specify a supported colorspace, and none has been specified as source either\n");
452  av_frame_free(&out);
453  return AVERROR(EINVAL);
454  }
455  color->mode = source * 5 + color->dest;
456  } else
457  color->mode = color->source * 5 + color->dest;
458 
459  switch(color->dest) {
460  case COLOR_MODE_BT709 : out->colorspace = AVCOL_SPC_BT709 ; break;
461  case COLOR_MODE_FCC : out->colorspace = AVCOL_SPC_FCC ; break;
462  case COLOR_MODE_SMPTE240M: out->colorspace = AVCOL_SPC_SMPTE240M ; break;
463  case COLOR_MODE_BT601 : out->colorspace = AVCOL_SPC_BT470BG ; break;
464  case COLOR_MODE_BT2020 : out->colorspace = AVCOL_SPC_BT2020_NCL; break;
465  }
466 
467  td.src = in;
468  td.dst = out;
469  td.c2 = color->yuv_convert[color->mode][0][1];
470  td.c3 = color->yuv_convert[color->mode][0][2];
471  td.c4 = color->yuv_convert[color->mode][1][1];
472  td.c5 = color->yuv_convert[color->mode][1][2];
473  td.c6 = color->yuv_convert[color->mode][2][1];
474  td.c7 = color->yuv_convert[color->mode][2][2];
475 
476  if (in->format == AV_PIX_FMT_YUV444P)
478  FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
479  else if (in->format == AV_PIX_FMT_YUV422P)
481  FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
482  else if (in->format == AV_PIX_FMT_YUV420P)
484  FFMIN(in->height / 2, ff_filter_get_nb_threads(ctx)));
485  else
487  FFMIN(in->height, ff_filter_get_nb_threads(ctx)));
488 
489  av_frame_free(&in);
490  return ff_filter_frame(outlink, out);
491 }
492 
493 static const AVFilterPad colormatrix_inputs[] = {
494  {
495  .name = "default",
496  .type = AVMEDIA_TYPE_VIDEO,
497  .config_props = config_input,
498  .filter_frame = filter_frame,
499  },
500  { NULL }
501 };
502 
504  {
505  .name = "default",
506  .type = AVMEDIA_TYPE_VIDEO,
507  },
508  { NULL }
509 };
510 
512  .name = "colormatrix",
513  .description = NULL_IF_CONFIG_SMALL("Convert color matrix."),
514  .priv_size = sizeof(ColorMatrixContext),
515  .init = init,
519  .priv_class = &colormatrix_class,
521 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
ColorMode
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
Main libavfilter public API header.
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:264
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
#define cm
Definition: dvbsubdec.c:37
float im
Definition: fft.c:82
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition: filters.h:271
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:126
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int i
Definition: input.c:407
const char * arg
Definition: jacosubdec.c:66
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define FFALIGN(x, a)
Definition: macros.h:48
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
static const uint64_t c2
Definition: murmur3.c:52
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:512
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:514
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:518
@ AVCOL_SPC_BT2020_CL
ITU-R BT2020 constant luminance system.
Definition: pixfmt.h:524
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:523
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:519
@ AVCOL_SPC_FCC
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:517
@ AVCOL_SPC_SMPTE240M
functionally identical to above
Definition: pixfmt.h:520
#define td
Definition: regdef.h:70
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A list of supported formats for one end of a filter link.
Definition: formats.h:65
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
AVOption.
Definition: opt.h:248
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
int dest
ColorMode.
int yuv_convert[25][3][3]
Used for passing data between threads.
Definition: dsddec.c:67
const AVFrame * src
AVFrame * dst
Definition: vf_blend.c:57
Definition: rpzaenc.c:58
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
FILE * out
Definition: movenc.c:54
AVFormatContext * ctx
Definition: movenc.c:48
#define height
#define width
static const double yuv_coeff_luma[5][3]
@ COLOR_MODE_BT601
@ COLOR_MODE_FCC
@ COLOR_MODE_BT709
@ COLOR_MODE_SMPTE240M
@ COLOR_MODE_COUNT
@ COLOR_MODE_NONE
@ COLOR_MODE_BT2020
#define imd
#define mc
#define ma
static int process_slice_yuv420p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define mf
#define mb
static int process_slice_yuv444p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define imb
static int process_slice_yuv422p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static int filter_frame(AVFilterLink *link, AVFrame *in)
AVFILTER_DEFINE_CLASS(colormatrix)
static int query_formats(AVFilterContext *ctx)
static void calc_coefficients(AVFilterContext *ctx)
#define imc
static int config_input(AVFilterLink *inlink)
#define mg
#define FLAGS
#define NS(n)
static const char *const color_modes[]
static void inverse3x3(double im[3][3], double m[3][3])
static const AVOption colormatrix_options[]
#define mi
#define md
static void solve_coefficients(double cm[3][3], double rgb[3][3], double yuv[3][3])
#define CB(n)
static av_cold int init(AVFilterContext *ctx)
#define mh
#define imf
#define me
static const AVFilterPad colormatrix_inputs[]
#define img
#define OFFSET(x)
#define imh
static int process_slice_uyvy422(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
#define ima
#define ime
static const AVFilterPad colormatrix_outputs[]
AVFilter ff_vf_colormatrix
#define imi
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:104