2 #include <Media/Libav.hpp>
4 #include <ossia/detail/hash_map.hpp>
8 #include <score_plugin_media_export.h>
13 #include <libavformat/avformat.h>
14 #include <libavutil/pixdesc.h>
20 inline const ossia::hash_map<std::string, AVPixelFormat>& gstreamerToLibav()
22 static const auto map = [] {
23 ossia::hash_map<std::string, AVPixelFormat> format_map;
128 format_map[
"A420"] = AV_PIX_FMT_YUVA420P;
129 format_map[
"A420_10BE"] = AV_PIX_FMT_YUVA420P10BE;
130 format_map[
"A420_10LE"] = AV_PIX_FMT_YUVA420P10LE;
131 format_map[
"A422_10BE"] = AV_PIX_FMT_YUVA422P10BE;
132 format_map[
"A422_10LE"] = AV_PIX_FMT_YUVA422P10LE;
133 format_map[
"A444_10BE"] = AV_PIX_FMT_YUVA444P10BE;
134 format_map[
"A444_10LE"] = AV_PIX_FMT_YUVA444P10BE;
135 format_map[
"ABGR"] = AV_PIX_FMT_ABGR;
138 format_map[
"ARGB"] = AV_PIX_FMT_ARGB;
144 format_map[
"AYUV64"] = AV_PIX_FMT_AYUV64;
149 format_map[
"BGRA"] = AV_PIX_FMT_BGRA;
153 format_map[
"BGRX"] = AV_PIX_FMT_BGR0;
155 format_map[
"GBR"] = AV_PIX_FMT_GBRP;
156 format_map[
"GBRA"] = AV_PIX_FMT_GBRAP;
166 format_map[
"GRAY16_BE"] = AV_PIX_FMT_GRAY16BE;
167 format_map[
"GRAY16_LE"] = AV_PIX_FMT_GRAY16LE;
168 format_map[
"GRAY8"] = AV_PIX_FMT_GRAY8;
169 format_map[
"I420"] = AV_PIX_FMT_YUV420P;
170 format_map[
"I420_10BE"] = AV_PIX_FMT_YUV420P10BE;
171 format_map[
"I420_10LE"] = AV_PIX_FMT_YUV420P10LE;
172 format_map[
"I420_12BE"] = AV_PIX_FMT_YUV420P12BE;
173 format_map[
"I420_12LE"] = AV_PIX_FMT_YUV420P12LE;
174 format_map[
"I422_10BE"] = AV_PIX_FMT_YUV422P10BE;
175 format_map[
"I422_10LE"] = AV_PIX_FMT_YUV422P10LE;
176 format_map[
"I422_12BE"] = AV_PIX_FMT_YUV422P12BE;
177 format_map[
"I422_12LE"] = AV_PIX_FMT_YUV422P12LE;
180 format_map[
"NV12"] = AV_PIX_FMT_NV12;
186 format_map[
"NV16"] = AV_PIX_FMT_NV16;
188 format_map[
"NV21"] = AV_PIX_FMT_NV21;
189 format_map[
"NV24"] = AV_PIX_FMT_NV24;
191 format_map[
"P010_10BE"] = AV_PIX_FMT_P010BE;
192 format_map[
"P010_10LE"] = AV_PIX_FMT_P010LE;
195 format_map[
"P016_BE"] = AV_PIX_FMT_P016BE;
196 format_map[
"P016_LE"] = AV_PIX_FMT_P016LE;
198 format_map[
"RGB"] = AV_PIX_FMT_RGB24;
203 format_map[
"RGBA"] = AV_PIX_FMT_RGBA;
204 format_map[
"RGBA64_BE"] = AV_PIX_FMT_RGBA64BE;
205 format_map[
"RGBA64_LE"] = AV_PIX_FMT_RGBA64LE;
206 format_map[
"RGBP"] = AV_PIX_FMT_RGB24;
207 format_map[
"RGBX"] = AV_PIX_FMT_RGB0;
210 format_map[
"UYVY"] = AV_PIX_FMT_UYVY422;
216 format_map[
"XBGR"] = AV_PIX_FMT_0BGR;
217 format_map[
"XRGB"] = AV_PIX_FMT_0RGB;
218 format_map[
"Y210"] = AV_PIX_FMT_Y210;
221 format_map[
"Y410"] = AV_PIX_FMT_YUV410P;
226 format_map[
"Y444"] = AV_PIX_FMT_YUV444P;
227 format_map[
"Y444_10BE"] = AV_PIX_FMT_YUV444P10BE;
228 format_map[
"Y444_10LE"] = AV_PIX_FMT_YUV444P10LE;
229 format_map[
"Y444_12BE"] = AV_PIX_FMT_YUV444P12BE;
230 format_map[
"Y444_12LE"] = AV_PIX_FMT_YUV444P12LE;
231 format_map[
"Y444_16BE"] = AV_PIX_FMT_YUV444P16BE;
232 format_map[
"Y444_16LE"] = AV_PIX_FMT_YUV444P16LE;
237 format_map[
"YVYU"] = AV_PIX_FMT_YVYU422;
246 inline void initFrameFromRawData(AVFrame* frame, uint8_t* p, std::size_t sz)
248 switch(frame->format)
250 case AV_PIX_FMT_YUV420P:
251 case AV_PIX_FMT_YUVJ420P:
256 frame->linesize[0] = frame->width;
259 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
260 frame->linesize[1] = frame->width / 2;
263 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height / 2;
264 frame->linesize[2] = frame->width / 2;
268 case AV_PIX_FMT_NV12:
269 case AV_PIX_FMT_NV21:
274 frame->linesize[0] = frame->width;
277 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
278 frame->linesize[1] = frame->width;
283 case AV_PIX_FMT_P016LE:
284 case AV_PIX_FMT_P016BE:
286 constexpr
int byte_per_component = 2;
290 frame->linesize[0] = byte_per_component * frame->width;
293 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
294 frame->linesize[1] = byte_per_component * frame->width;
299 case AV_PIX_FMT_YUV420P16LE:
300 case AV_PIX_FMT_YUV420P16BE:
302 constexpr
int byte_per_component = 2;
306 frame->linesize[0] = byte_per_component * frame->width;
309 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
310 frame->linesize[1] = byte_per_component * frame->width / 2;
313 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height / 2;
314 frame->linesize[2] = byte_per_component * frame->width / 2;
318 case AV_PIX_FMT_YUV444P:
319 case AV_PIX_FMT_YUVJ444P:
320 case AV_PIX_FMT_GBRP:
325 frame->linesize[0] = frame->width;
328 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
329 frame->linesize[1] = frame->width;
332 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
333 frame->linesize[2] = frame->width;
337 case AV_PIX_FMT_YUV444P16LE:
338 case AV_PIX_FMT_YUV444P16BE:
340 constexpr
int byte_per_component = 2;
344 frame->linesize[0] = byte_per_component * frame->width;
347 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
348 frame->linesize[1] = byte_per_component * frame->width;
351 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
352 frame->linesize[2] = byte_per_component * frame->width;
356 case AV_PIX_FMT_YUVA444P:
357 case AV_PIX_FMT_GBRAP:
358 case AV_PIX_FMT_GBRAP16BE:
359 case AV_PIX_FMT_GBRAP16LE:
362 frame->linesize[0] = frame->width;
364 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
365 frame->linesize[1] = frame->width;
367 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
368 frame->linesize[2] = frame->width;
370 frame->data[3] = frame->data[2] + frame->linesize[2] * frame->height;
371 frame->linesize[3] = frame->width;
375 case AV_PIX_FMT_YUV422P:
376 case AV_PIX_FMT_YUVJ422P:
381 frame->linesize[0] = frame->width;
384 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
385 frame->linesize[1] = frame->width / 2;
388 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
389 frame->linesize[2] = frame->width / 2;
393 case AV_PIX_FMT_YUV422P16LE:
394 case AV_PIX_FMT_YUV422P16BE:
396 constexpr
int byte_per_component = 2;
400 frame->linesize[0] = byte_per_component * frame->width;
403 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
404 frame->linesize[1] = byte_per_component * frame->width / 2;
407 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
408 frame->linesize[2] = byte_per_component * frame->width / 2;
412 case AV_PIX_FMT_NV24:
413 case AV_PIX_FMT_NV42:
418 frame->linesize[0] = frame->width;
421 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
422 frame->linesize[1] = frame->width * 2;
426 case AV_PIX_FMT_YUV410P:
431 frame->linesize[0] = frame->width;
434 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
435 frame->linesize[1] = frame->width / 4;
438 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height / 4;
439 frame->linesize[2] = frame->width / 4;
443 case AV_PIX_FMT_YUV411P:
448 frame->linesize[0] = frame->width;
451 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
452 frame->linesize[1] = frame->width / 4;
455 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
456 frame->linesize[2] = frame->width / 4;
460 case AV_PIX_FMT_YUV440P:
461 case AV_PIX_FMT_YUVJ440P:
462 case AV_PIX_FMT_YUVA420P:
464 case AV_PIX_FMT_YUV420P9BE:
465 case AV_PIX_FMT_YUV420P9LE:
466 case AV_PIX_FMT_YUV420P10BE:
467 case AV_PIX_FMT_YUV420P10LE:
468 case AV_PIX_FMT_YUV422P10BE:
469 case AV_PIX_FMT_YUV422P10LE:
470 case AV_PIX_FMT_YUV444P9BE:
471 case AV_PIX_FMT_YUV444P9LE:
472 case AV_PIX_FMT_YUV444P10BE:
473 case AV_PIX_FMT_YUV444P10LE:
474 case AV_PIX_FMT_YUV422P9BE:
475 case AV_PIX_FMT_YUV422P9LE:
477 case AV_PIX_FMT_GBRP9BE:
478 case AV_PIX_FMT_GBRP9LE:
479 case AV_PIX_FMT_GBRP10BE:
480 case AV_PIX_FMT_GBRP10LE:
481 case AV_PIX_FMT_GBRP16BE:
482 case AV_PIX_FMT_GBRP16LE:
483 case AV_PIX_FMT_YUVA422P:
484 case AV_PIX_FMT_YUVA420P9BE:
485 case AV_PIX_FMT_YUVA420P9LE:
486 case AV_PIX_FMT_YUVA422P9BE:
487 case AV_PIX_FMT_YUVA422P9LE:
488 case AV_PIX_FMT_YUVA444P9BE:
489 case AV_PIX_FMT_YUVA444P9LE:
490 case AV_PIX_FMT_YUVA420P10BE:
491 case AV_PIX_FMT_YUVA420P10LE:
492 case AV_PIX_FMT_YUVA422P10BE:
493 case AV_PIX_FMT_YUVA422P10LE:
494 case AV_PIX_FMT_YUVA444P10BE:
495 case AV_PIX_FMT_YUVA444P10LE:
496 case AV_PIX_FMT_YUVA420P16BE:
497 case AV_PIX_FMT_YUVA420P16LE:
498 case AV_PIX_FMT_YUVA422P16BE:
499 case AV_PIX_FMT_YUVA422P16LE:
500 case AV_PIX_FMT_YUVA444P16BE:
501 case AV_PIX_FMT_YUVA444P16LE:
503 case AV_PIX_FMT_YUV420P12BE:
504 case AV_PIX_FMT_YUV420P12LE:
505 case AV_PIX_FMT_YUV420P14BE:
506 case AV_PIX_FMT_YUV420P14LE:
507 case AV_PIX_FMT_YUV422P12BE:
508 case AV_PIX_FMT_YUV422P12LE:
509 case AV_PIX_FMT_YUV422P14BE:
510 case AV_PIX_FMT_YUV422P14LE:
511 case AV_PIX_FMT_YUV444P12BE:
512 case AV_PIX_FMT_YUV444P12LE:
513 case AV_PIX_FMT_YUV444P14BE:
514 case AV_PIX_FMT_YUV444P14LE:
515 case AV_PIX_FMT_GBRP12BE:
516 case AV_PIX_FMT_GBRP12LE:
517 case AV_PIX_FMT_GBRP14BE:
518 case AV_PIX_FMT_GBRP14LE:
519 case AV_PIX_FMT_YUVJ411P:
521 case AV_PIX_FMT_YUV440P10LE:
522 case AV_PIX_FMT_YUV440P10BE:
523 case AV_PIX_FMT_YUV440P12LE:
524 case AV_PIX_FMT_YUV440P12BE:
526 case AV_PIX_FMT_GBRAP12BE:
527 case AV_PIX_FMT_GBRAP12LE:
529 case AV_PIX_FMT_GBRAP10BE:
530 case AV_PIX_FMT_GBRAP10LE:
532 case AV_PIX_FMT_GBRPF32BE:
533 case AV_PIX_FMT_GBRPF32LE:
534 case AV_PIX_FMT_GBRAPF32BE:
535 case AV_PIX_FMT_GBRAPF32LE:
537 case AV_PIX_FMT_YUVA422P12BE:
538 case AV_PIX_FMT_YUVA422P12LE:
539 case AV_PIX_FMT_YUVA444P12BE:
540 case AV_PIX_FMT_YUVA444P12LE:
542 qDebug() <<
"TODO unhandled video format";
547 case AV_PIX_FMT_MONOWHITE:
548 case AV_PIX_FMT_MONOBLACK:
550 frame->linesize[0] = frame->width / CHAR_BIT;
553 case AV_PIX_FMT_BGR4:
554 case AV_PIX_FMT_RGB4:
556 frame->linesize[0] = frame->width * 4. / 8.;
559 case AV_PIX_FMT_GRAY8:
560 case AV_PIX_FMT_BGR8:
561 case AV_PIX_FMT_RGB8:
562 case AV_PIX_FMT_BGR4_BYTE:
563 case AV_PIX_FMT_RGB4_BYTE:
565 frame->linesize[0] = frame->width * 8. / 8.;
568 case AV_PIX_FMT_GRAY9BE:
569 case AV_PIX_FMT_GRAY9LE:
571 frame->linesize[0] = frame->width * 9. / 8.;
574 case AV_PIX_FMT_P010LE:
575 case AV_PIX_FMT_P010BE:
576 case AV_PIX_FMT_GRAY10BE:
577 case AV_PIX_FMT_GRAY10LE:
579 frame->linesize[0] = frame->width * 10. / 8.;
582 case AV_PIX_FMT_GRAY12BE:
583 case AV_PIX_FMT_GRAY12LE:
584 case AV_PIX_FMT_UYYVYY411:
586 frame->linesize[0] = frame->width * 12. / 8.;
589 case AV_PIX_FMT_GRAY14BE:
590 case AV_PIX_FMT_GRAY14LE:
592 frame->linesize[0] = frame->width * 14. / 8.;
595 case AV_PIX_FMT_NV16:
596 case AV_PIX_FMT_YUYV422:
597 case AV_PIX_FMT_UYVY422:
598 case AV_PIX_FMT_YVYU422:
599 case AV_PIX_FMT_GRAY16BE:
600 case AV_PIX_FMT_GRAY16LE:
603 case AV_PIX_FMT_RGB565BE:
604 case AV_PIX_FMT_RGB565LE:
605 case AV_PIX_FMT_RGB555BE:
606 case AV_PIX_FMT_RGB555LE:
608 case AV_PIX_FMT_BGR565BE:
609 case AV_PIX_FMT_BGR565LE:
610 case AV_PIX_FMT_BGR555BE:
611 case AV_PIX_FMT_BGR555LE:
613 case AV_PIX_FMT_RGB444LE:
614 case AV_PIX_FMT_RGB444BE:
615 case AV_PIX_FMT_BGR444LE:
616 case AV_PIX_FMT_BGR444BE:
618 frame->linesize[0] = frame->width * 16. / 8.;
621 case AV_PIX_FMT_NV20LE:
622 case AV_PIX_FMT_NV20BE:
623 case AV_PIX_FMT_Y210BE:
624 case AV_PIX_FMT_Y210LE:
626 frame->linesize[0] = frame->width * 20. / 8.;
629 case AV_PIX_FMT_RGB24:
630 case AV_PIX_FMT_BGR24:
632 frame->linesize[0] = frame->width * 24. / 8.;
642 case AV_PIX_FMT_ARGB:
643 case AV_PIX_FMT_RGBA:
644 case AV_PIX_FMT_ABGR:
645 case AV_PIX_FMT_BGRA:
646 case AV_PIX_FMT_0RGB:
647 case AV_PIX_FMT_RGB0:
648 case AV_PIX_FMT_0BGR:
649 case AV_PIX_FMT_BGR0:
650 case AV_PIX_FMT_GRAYF32BE:
651 case AV_PIX_FMT_GRAYF32LE:
652 case AV_PIX_FMT_YA16BE:
653 case AV_PIX_FMT_YA16LE:
655 frame->linesize[0] = frame->width * 32. / 8.;
658 case AV_PIX_FMT_XYZ12LE:
659 case AV_PIX_FMT_XYZ12BE:
661 frame->linesize[0] = frame->width * 36. / 8.;
664 case AV_PIX_FMT_RGB48BE:
665 case AV_PIX_FMT_RGB48LE:
667 case AV_PIX_FMT_BGR48BE:
668 case AV_PIX_FMT_BGR48LE:
670 frame->linesize[0] = frame->width * 48. / 8.;
673 case AV_PIX_FMT_RGBA64BE:
674 case AV_PIX_FMT_RGBA64LE:
675 case AV_PIX_FMT_BGRA64BE:
676 case AV_PIX_FMT_BGRA64LE:
677 case AV_PIX_FMT_AYUV64LE:
678 case AV_PIX_FMT_AYUV64BE:
680 frame->linesize[0] = frame->width * 64. / 8.;
683 case AV_PIX_FMT_BAYER_BGGR8:
684 case AV_PIX_FMT_BAYER_RGGB8:
685 case AV_PIX_FMT_BAYER_GBRG8:
686 case AV_PIX_FMT_BAYER_GRBG8:
687 case AV_PIX_FMT_BAYER_BGGR16LE:
688 case AV_PIX_FMT_BAYER_BGGR16BE:
689 case AV_PIX_FMT_BAYER_RGGB16LE:
690 case AV_PIX_FMT_BAYER_RGGB16BE:
691 case AV_PIX_FMT_BAYER_GBRG16LE:
692 case AV_PIX_FMT_BAYER_GBRG16BE:
693 case AV_PIX_FMT_BAYER_GRBG16LE:
694 case AV_PIX_FMT_BAYER_GRBG16BE:
695 case AV_PIX_FMT_PAL8:
697 qDebug() <<
"TODO unhandled video format";