Unified · Split
| @@ -27,6 +27,7 @@ · 26 unmodified lines | ||
| 27 | 27 | #include "libavutil/opt.h" |
| 28 | 28 | #include "libavutil/pixdesc.h" |
| 29 | 29 | #include "libavutil/avassert.h" |
| 30 | +#include "libavutil/imgutils.h" | |
| 30 | 31 | #include "avfilter.h" |
| 31 | 32 | #include "dnn_interface.h" |
| 32 | 33 | #include "formats.h" |
| @@ -231,6 +232,8 @@ static int config_output(AVFilterLink *outlink) · 198 unmodified lines | ||
| 231 | 232 | |
| 232 | 233 | static int copy_from_frame_to_dnn(DNNData *dnn_input, const AVFrame *frame) |
| 233 | 234 | { |
| 235 | + int bytewidth = av_image_get_linesize(frame->format, frame->width, 0); | |
| 236 | + | |
| 234 | 237 | switch (frame->format) { |
| 235 | 238 | case AV_PIX_FMT_RGB24: |
| 236 | 239 | case AV_PIX_FMT_BGR24: |
| @@ -244,42 +247,17 @@ static int copy_from_frame_to_dnn(DNNData *dnn_input, const AVFrame *frame) · 7 unmodified lines | ||
| 244 | 247 | } |
| 245 | 248 | } |
| 246 | 249 | } else { |
| 247 | − uint8_t *dnn_input_data = dnn_input->data; | |
| 248 | 250 | av_assert0(dnn_input->dt == DNN_UINT8); |
| 249 | − for (int i = 0; i < frame->height; i++) { | |
| 250 | − for(int j = 0; j < frame->width * 3; j++) { | |
| 251 | − int k = i * frame->linesize[0] + j; | |
| 252 | − int t = i * frame->width * 3 + j; | |
| 253 | − dnn_input_data[t] = frame->data[0][k]; | |
| 254 | − } | |
| 255 | − } | |
| 251 | + av_image_copy_plane(dnn_input->data, bytewidth, | |
| 252 | + frame->data[0], frame->linesize[0], | |
| 253 | + bytewidth, frame->height); | |
| 256 | 254 | } |
| 257 | 255 | return 0; |
| 258 | 256 | case AV_PIX_FMT_GRAY8: |
| 259 | − { | |
| 260 | − uint8_t *dnn_input_data = dnn_input->data; | |
| 261 | − av_assert0(dnn_input->dt == DNN_UINT8); | |
| 262 | − for (int i = 0; i < frame->height; i++) { | |
| 263 | − for(int j = 0; j < frame->width; j++) { | |
| 264 | − int k = i * frame->linesize[0] + j; | |
| 265 | − int t = i * frame->width + j; | |
| 266 | − dnn_input_data[t] = frame->data[0][k]; | |
| 267 | − } | |
| 268 | − } | |
| 269 | − } | |
| 270 | − return 0; | |
| 271 | 257 | case AV_PIX_FMT_GRAYF32: |
| 272 | − { | |
| 273 | − float *dnn_input_data = dnn_input->data; | |
| 274 | − av_assert0(dnn_input->dt == DNN_FLOAT); | |
| 275 | − for (int i = 0; i < frame->height; i++) { | |
| 276 | − for(int j = 0; j < frame->width; j++) { | |
| 277 | − int k = i * frame->linesize[0] + j * sizeof(float); | |
| 278 | − int t = i * frame->width + j; | |
| 279 | − dnn_input_data[t] = *(float*)(frame->data[0] + k); | |
| 280 | − } | |
| 281 | − } | |
| 282 | − } | |
| 258 | + av_image_copy_plane(dnn_input->data, bytewidth, | |
| 259 | + frame->data[0], frame->linesize[0], | |
| 260 | + bytewidth, frame->height); | |
| 283 | 261 | return 0; |
| 284 | 262 | default: |
| 285 | 263 | return AVERROR(EIO); |
| @@ -290,6 +268,8 @@ static int copy_from_frame_to_dnn(DNNData *dnn_input, const AVFrame *frame) · 4 unmodified lines | ||
| 290 | 268 | |
| 291 | 269 | static int copy_from_dnn_to_frame(AVFrame *frame, const DNNData *dnn_output) |
| 292 | 270 | { |
| 271 | + int bytewidth = av_image_get_linesize(frame->format, frame->width, 0); | |
| 272 | + | |
| 293 | 273 | switch (frame->format) { |
| 294 | 274 | case AV_PIX_FMT_RGB24: |
| 295 | 275 | case AV_PIX_FMT_BGR24: |
| @@ -303,42 +283,25 @@ static int copy_from_dnn_to_frame(AVFrame *frame, const DNNData *dnn_output) · 7 unmodified lines | ||
| 303 | 283 | } |
| 304 | 284 | } |
| 305 | 285 | } else { |
| 306 | − uint8_t *dnn_output_data = dnn_output->data; | |
| 307 | 286 | av_assert0(dnn_output->dt == DNN_UINT8); |
| 308 | − for (int i = 0; i < frame->height; i++) { | |
| 309 | − for(int j = 0; j < frame->width * 3; j++) { | |
| 310 | − int k = i * frame->linesize[0] + j; | |
| 311 | − int t = i * frame->width * 3 + j; | |
| 312 | − frame->data[0][k] = dnn_output_data[t]; | |
| 313 | − } | |
| 314 | − } | |
| 287 | + av_image_copy_plane(frame->data[0], frame->linesize[0], | |
| 288 | + dnn_output->data, bytewidth, | |
| 289 | + bytewidth, frame->height); | |
| 315 | 290 | } |
| 316 | 291 | return 0; |
| 317 | 292 | case AV_PIX_FMT_GRAY8: |
| 318 | − { | |
| 319 | − uint8_t *dnn_output_data = dnn_output->data; | |
| 320 | − av_assert0(dnn_output->dt == DNN_UINT8); | |
| 321 | − for (int i = 0; i < frame->height; i++) { | |
| 322 | − for(int j = 0; j < frame->width; j++) { | |
| 323 | − int k = i * frame->linesize[0] + j; | |
| 324 | − int t = i * frame->width + j; | |
| 325 | − frame->data[0][k] = dnn_output_data[t]; | |
| 326 | − } | |
| 327 | − } | |
| 328 | − } | |
| 293 | + // it is possible that data type of dnn output is float32, | |
| 294 | + // need to add support for such case when needed. | |
| 295 | + av_assert0(dnn_output->dt == DNN_UINT8); | |
| 296 | + av_image_copy_plane(frame->data[0], frame->linesize[0], | |
| 297 | + dnn_output->data, bytewidth, | |
| 298 | + bytewidth, frame->height); | |
| 329 | 299 | return 0; |
| 330 | 300 | case AV_PIX_FMT_GRAYF32: |
| 331 | − { | |
| 332 | − float *dnn_output_data = dnn_output->data; | |
| 333 | − av_assert0(dnn_output->dt == DNN_FLOAT); | |
| 334 | − for (int i = 0; i < frame->height; i++) { | |
| 335 | − for(int j = 0; j < frame->width; j++) { | |
| 336 | − int k = i * frame->linesize[0] + j * sizeof(float); | |
| 337 | − int t = i * frame->width + j; | |
| 338 | − *(float*)(frame->data[0] + k) = dnn_output_data[t]; | |
| 339 | − } | |
| 340 | − } | |
| 341 | − } | |
| 301 | + av_assert0(dnn_output->dt == DNN_FLOAT); | |
| 302 | + av_image_copy_plane(frame->data[0], frame->linesize[0], | |
| 303 | + dnn_output->data, bytewidth, | |
| 304 | + bytewidth, frame->height); | |
| 342 | 305 | return 0; |
| 343 | 306 | default: |
| 344 | 307 | return AVERROR(EIO); |