FFmpeg  4.4.9
dnn_io_proc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "dnn_io_proc.h"
22 #include "libavutil/imgutils.h"
23 #include "libswscale/swscale.h"
24 #include "libavutil/avassert.h"
25 
27 {
28  struct SwsContext *sws_ctx;
29  int bytewidth = av_image_get_linesize(frame->format, frame->width, 0);
30  if (bytewidth < 0) {
31  return DNN_ERROR;
32  }
33  if (output->dt != DNN_FLOAT) {
34  avpriv_report_missing_feature(log_ctx, "data type rather than DNN_FLOAT");
35  return DNN_ERROR;
36  }
37 
38  switch (frame->format) {
39  case AV_PIX_FMT_RGB24:
40  case AV_PIX_FMT_BGR24:
41  if (output->channels != 3) {
42  av_log(log_ctx, AV_LOG_ERROR,
43  "dnn_processing to a %s frame requires a 3-channel model "
44  "output, got %d channels; channel-reducing/expanding "
45  "models (e.g. single-channel matte output) are not "
46  "supported by this filter\n",
48  return DNN_ERROR;
49  }
50  sws_ctx = sws_getContext(frame->width * 3,
51  frame->height,
53  frame->width * 3,
54  frame->height,
56  0, NULL, NULL, NULL);
57  if (!sws_ctx) {
58  av_log(log_ctx, AV_LOG_ERROR, "Impossible to create scale context for the conversion "
59  "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
62  return DNN_ERROR;
63  }
64  sws_scale(sws_ctx, (const uint8_t *[4]){(const uint8_t *)output->data, 0, 0, 0},
65  (const int[4]){frame->width * 3 * sizeof(float), 0, 0, 0}, 0, frame->height,
66  (uint8_t * const*)frame->data, frame->linesize);
67  sws_freeContext(sws_ctx);
68  return DNN_SUCCESS;
69  case AV_PIX_FMT_GRAYF32:
71  output->data, bytewidth,
72  bytewidth, frame->height);
73  return DNN_SUCCESS;
74  case AV_PIX_FMT_YUV420P:
75  case AV_PIX_FMT_YUV422P:
76  case AV_PIX_FMT_YUV444P:
77  case AV_PIX_FMT_YUV410P:
78  case AV_PIX_FMT_YUV411P:
79  case AV_PIX_FMT_GRAY8:
80  case AV_PIX_FMT_NV12:
81  sws_ctx = sws_getContext(frame->width,
82  frame->height,
84  frame->width,
85  frame->height,
87  0, NULL, NULL, NULL);
88  if (!sws_ctx) {
89  av_log(log_ctx, AV_LOG_ERROR, "Impossible to create scale context for the conversion "
90  "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
93  return DNN_ERROR;
94  }
95  sws_scale(sws_ctx, (const uint8_t *[4]){(const uint8_t *)output->data, 0, 0, 0},
96  (const int[4]){frame->width * sizeof(float), 0, 0, 0}, 0, frame->height,
97  (uint8_t * const*)frame->data, frame->linesize);
98  sws_freeContext(sws_ctx);
99  return DNN_SUCCESS;
100  default:
102  return DNN_ERROR;
103  }
104 
105  return DNN_SUCCESS;
106 }
107 
109 {
110  struct SwsContext *sws_ctx;
111  int bytewidth = av_image_get_linesize(frame->format, frame->width, 0);
112  if (bytewidth < 0) {
113  return DNN_ERROR;
114  }
115  if (input->dt != DNN_FLOAT) {
116  avpriv_report_missing_feature(log_ctx, "data type rather than DNN_FLOAT");
117  return DNN_ERROR;
118  }
119 
120  switch (frame->format) {
121  case AV_PIX_FMT_RGB24:
122  case AV_PIX_FMT_BGR24:
123  sws_ctx = sws_getContext(frame->width * 3,
124  frame->height,
126  frame->width * 3,
127  frame->height,
129  0, NULL, NULL, NULL);
130  if (!sws_ctx) {
131  av_log(log_ctx, AV_LOG_ERROR, "Impossible to create scale context for the conversion "
132  "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
135  return DNN_ERROR;
136  }
137  sws_scale(sws_ctx, (const uint8_t **)frame->data,
138  frame->linesize, 0, frame->height,
139  (uint8_t * const [4]){input->data, 0, 0, 0},
140  (const int [4]){frame->width * 3 * sizeof(float), 0, 0, 0});
141  sws_freeContext(sws_ctx);
142  break;
143  case AV_PIX_FMT_GRAYF32:
144  av_image_copy_plane(input->data, bytewidth,
145  frame->data[0], frame->linesize[0],
146  bytewidth, frame->height);
147  break;
148  case AV_PIX_FMT_YUV420P:
149  case AV_PIX_FMT_YUV422P:
150  case AV_PIX_FMT_YUV444P:
151  case AV_PIX_FMT_YUV410P:
152  case AV_PIX_FMT_YUV411P:
153  case AV_PIX_FMT_GRAY8:
154  case AV_PIX_FMT_NV12:
155  sws_ctx = sws_getContext(frame->width,
156  frame->height,
158  frame->width,
159  frame->height,
161  0, NULL, NULL, NULL);
162  if (!sws_ctx) {
163  av_log(log_ctx, AV_LOG_ERROR, "Impossible to create scale context for the conversion "
164  "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
167  return DNN_ERROR;
168  }
169  sws_scale(sws_ctx, (const uint8_t **)frame->data,
170  frame->linesize, 0, frame->height,
171  (uint8_t * const*)(&input->data),
172  (const int [4]){frame->width * sizeof(float), 0, 0, 0});
173  sws_freeContext(sws_ctx);
174  break;
175  default:
177  return DNN_ERROR;
178  }
179 
180  return DNN_SUCCESS;
181 }
182 
184 {
185  if (data->dt == DNN_UINT8 && data->order == DCO_BGR) {
186  return AV_PIX_FMT_BGR24;
187  }
188 
189  av_assert0(!"not supported yet.\n");
190  return AV_PIX_FMT_BGR24;
191 }
192 
194 {
195  struct SwsContext *sws_ctx;
196  int linesizes[4];
197  enum AVPixelFormat fmt = get_pixel_format(input);
199  input->width, input->height, fmt,
201  if (!sws_ctx) {
202  av_log(log_ctx, AV_LOG_ERROR, "Impossible to create scale context for the conversion "
203  "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
205  av_get_pix_fmt_name(fmt), input->width, input->height);
206  return DNN_ERROR;
207  }
208 
209  if (av_image_fill_linesizes(linesizes, fmt, input->width) < 0) {
210  av_log(log_ctx, AV_LOG_ERROR, "unable to get linesizes with av_image_fill_linesizes");
211  sws_freeContext(sws_ctx);
212  return DNN_ERROR;
213  }
214 
215  sws_scale(sws_ctx, (const uint8_t *const *)frame->data, frame->linesize, 0, frame->height,
216  (uint8_t *const *)(&input->data), linesizes);
217 
218  sws_freeContext(sws_ctx);
219  return DNN_SUCCESS;
220 }
221 
223 {
224  switch (func_type)
225  {
226  case DFT_PROCESS_FRAME:
227  return proc_from_frame_to_dnn_frameprocessing(frame, input, log_ctx);
229  return proc_from_frame_to_dnn_analytics(frame, input, log_ctx);
230  default:
231  avpriv_report_missing_feature(log_ctx, "model function type %d", func_type);
232  return DNN_ERROR;
233  }
234 }
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define NULL
Definition: coverity.c:32
static AVFrame * frame
DNNReturnType
Definition: dnn_interface.h:33
@ DNN_ERROR
Definition: dnn_interface.h:33
@ DNN_SUCCESS
Definition: dnn_interface.h:33
DNNFunctionType
Definition: dnn_interface.h:51
@ DFT_PROCESS_FRAME
Definition: dnn_interface.h:53
@ DFT_ANALYTICS_DETECT
Definition: dnn_interface.h:54
@ DNN_UINT8
Definition: dnn_interface.h:37
@ DNN_FLOAT
Definition: dnn_interface.h:37
@ DCO_BGR
Definition: dnn_interface.h:41
DNNReturnType ff_proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
Definition: dnn_io_proc.c:26
static DNNReturnType proc_from_frame_to_dnn_frameprocessing(AVFrame *frame, DNNData *input, void *log_ctx)
Definition: dnn_io_proc.c:108
DNNReturnType ff_proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, DNNFunctionType func_type, void *log_ctx)
Definition: dnn_io_proc.c:222
static enum AVPixelFormat get_pixel_format(DNNData *data)
Definition: dnn_io_proc.c:183
static DNNReturnType proc_from_frame_to_dnn_analytics(AVFrame *frame, DNNData *input, void *log_ctx)
Definition: dnn_io_proc.c:193
DNN input&output process between AVFrame and DNNData.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:376
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition: imgutils.c:76
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
#define SWS_FAST_BILINEAR
Definition: swscale.h:58
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2373
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
Definition: swscale.c:745
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
Definition: utils.c:1953
misc image utilities
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
const char data[16]
Definition: mxf.c:142
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:431
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ 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_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
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 width
Definition: frame.h:376
int height
Definition: frame.h:376
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
int channels
Definition: dnn_interface.h:60
DNNDataType dt
Definition: dnn_interface.h:62
int height
Definition: dnn_interface.h:60
void * data
Definition: dnn_interface.h:59
external API header
#define av_log(a,...)