FFmpeg
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 
26 {
27  struct SwsContext *sws_ctx;
28  int bytewidth = av_image_get_linesize(frame->format, frame->width, 0);
29  if (output->dt != DNN_FLOAT) {
30  av_log(log_ctx, AV_LOG_ERROR, "do not support data type rather than DNN_FLOAT\n");
31  return DNN_ERROR;
32  }
33 
34  switch (frame->format) {
35  case AV_PIX_FMT_RGB24:
36  case AV_PIX_FMT_BGR24:
37  sws_ctx = sws_getContext(frame->width * 3,
38  frame->height,
40  frame->width * 3,
41  frame->height,
43  0, NULL, NULL, NULL);
44  sws_scale(sws_ctx, (const uint8_t *[4]){(const uint8_t *)output->data, 0, 0, 0},
45  (const int[4]){frame->width * 3 * sizeof(float), 0, 0, 0}, 0, frame->height,
46  (uint8_t * const*)frame->data, frame->linesize);
47  sws_freeContext(sws_ctx);
48  return DNN_SUCCESS;
49  case AV_PIX_FMT_GRAYF32:
50  av_image_copy_plane(frame->data[0], frame->linesize[0],
51  output->data, bytewidth,
52  bytewidth, frame->height);
53  return DNN_SUCCESS;
54  case AV_PIX_FMT_YUV420P:
55  case AV_PIX_FMT_YUV422P:
56  case AV_PIX_FMT_YUV444P:
57  case AV_PIX_FMT_YUV410P:
58  case AV_PIX_FMT_YUV411P:
59  case AV_PIX_FMT_GRAY8:
60  sws_ctx = sws_getContext(frame->width,
61  frame->height,
63  frame->width,
64  frame->height,
66  0, NULL, NULL, NULL);
67  sws_scale(sws_ctx, (const uint8_t *[4]){(const uint8_t *)output->data, 0, 0, 0},
68  (const int[4]){frame->width * sizeof(float), 0, 0, 0}, 0, frame->height,
69  (uint8_t * const*)frame->data, frame->linesize);
70  sws_freeContext(sws_ctx);
71  return DNN_SUCCESS;
72  default:
73  av_log(log_ctx, AV_LOG_ERROR, "do not support frame format %d\n", frame->format);
74  return DNN_ERROR;
75  }
76 
77  return DNN_SUCCESS;
78 }
79 
81 {
82  struct SwsContext *sws_ctx;
83  int bytewidth = av_image_get_linesize(frame->format, frame->width, 0);
84  if (input->dt != DNN_FLOAT) {
85  av_log(log_ctx, AV_LOG_ERROR, "do not support data type rather than DNN_FLOAT\n");
86  return DNN_ERROR;
87  }
88 
89  switch (frame->format) {
90  case AV_PIX_FMT_RGB24:
91  case AV_PIX_FMT_BGR24:
92  sws_ctx = sws_getContext(frame->width * 3,
93  frame->height,
95  frame->width * 3,
96  frame->height,
98  0, NULL, NULL, NULL);
99  sws_scale(sws_ctx, (const uint8_t **)frame->data,
100  frame->linesize, 0, frame->height,
101  (uint8_t * const*)(&input->data),
102  (const int [4]){frame->width * 3 * sizeof(float), 0, 0, 0});
103  sws_freeContext(sws_ctx);
104  break;
105  case AV_PIX_FMT_GRAYF32:
106  av_image_copy_plane(input->data, bytewidth,
107  frame->data[0], frame->linesize[0],
108  bytewidth, frame->height);
109  break;
110  case AV_PIX_FMT_YUV420P:
111  case AV_PIX_FMT_YUV422P:
112  case AV_PIX_FMT_YUV444P:
113  case AV_PIX_FMT_YUV410P:
114  case AV_PIX_FMT_YUV411P:
115  case AV_PIX_FMT_GRAY8:
116  sws_ctx = sws_getContext(frame->width,
117  frame->height,
119  frame->width,
120  frame->height,
122  0, NULL, NULL, NULL);
123  sws_scale(sws_ctx, (const uint8_t **)frame->data,
124  frame->linesize, 0, frame->height,
125  (uint8_t * const*)(&input->data),
126  (const int [4]){frame->width * sizeof(float), 0, 0, 0});
127  sws_freeContext(sws_ctx);
128  break;
129  default:
130  av_log(log_ctx, AV_LOG_ERROR, "do not support frame format %d\n", frame->format);
131  return DNN_ERROR;
132  }
133 
134  return DNN_SUCCESS;
135 }
#define NULL
Definition: coverity.c:32
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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
DNNReturnType proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
Definition: dnn_io_proc.c:25
uint8_t
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
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:1917
external API header
#define av_log(a,...)
int width
Definition: frame.h:366
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * data
Definition: dnn_interface.h:39
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2337
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
DNNReturnType
Definition: dnn_interface.h:32
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:381
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:431
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:339
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:744
DNN input&output process between AVFrame and DNNData.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Y , 8bpp.
Definition: pixfmt.h:74
DNNReturnType proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, void *log_ctx)
Definition: dnn_io_proc.c:80
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
int height
Definition: frame.h:366
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:373
DNNDataType dt
Definition: dnn_interface.h:40