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>
20inline 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;
234 format_map[
"YUY2"] = AV_PIX_FMT_YUYV422;
237 format_map[
"YVYU"] = AV_PIX_FMT_YVYU422;
247inline bool initFrameFromRawData(AVFrame* frame, uint8_t* p, std::size_t sz)
249 switch(frame->format)
251 case AV_PIX_FMT_YUV420P:
252 case AV_PIX_FMT_YUVJ420P:
257 frame->linesize[0] = frame->width;
260 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
261 frame->linesize[1] = frame->width / 2;
264 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height / 2;
265 frame->linesize[2] = frame->width / 2;
269 case AV_PIX_FMT_NV12:
270 case AV_PIX_FMT_NV21:
275 frame->linesize[0] = frame->width;
278 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
279 frame->linesize[1] = frame->width;
284 case AV_PIX_FMT_P016LE:
285 case AV_PIX_FMT_P016BE:
287 constexpr int byte_per_component = 2;
291 frame->linesize[0] = byte_per_component * frame->width;
294 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
295 frame->linesize[1] = byte_per_component * frame->width;
300 case AV_PIX_FMT_YUV420P16LE:
301 case AV_PIX_FMT_YUV420P16BE:
303 constexpr int byte_per_component = 2;
307 frame->linesize[0] = byte_per_component * frame->width;
310 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
311 frame->linesize[1] = byte_per_component * frame->width / 2;
314 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height / 2;
315 frame->linesize[2] = byte_per_component * frame->width / 2;
319 case AV_PIX_FMT_YUV444P:
320 case AV_PIX_FMT_YUVJ444P:
321 case AV_PIX_FMT_GBRP:
326 frame->linesize[0] = frame->width;
329 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
330 frame->linesize[1] = frame->width;
333 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
334 frame->linesize[2] = frame->width;
338 case AV_PIX_FMT_YUV444P16LE:
339 case AV_PIX_FMT_YUV444P16BE:
341 constexpr int byte_per_component = 2;
345 frame->linesize[0] = byte_per_component * frame->width;
348 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
349 frame->linesize[1] = byte_per_component * frame->width;
352 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
353 frame->linesize[2] = byte_per_component * frame->width;
357 case AV_PIX_FMT_YUVA444P:
358 case AV_PIX_FMT_GBRAP:
359 case AV_PIX_FMT_GBRAP16BE:
360 case AV_PIX_FMT_GBRAP16LE:
363 frame->linesize[0] = frame->width;
365 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
366 frame->linesize[1] = frame->width;
368 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
369 frame->linesize[2] = frame->width;
371 frame->data[3] = frame->data[2] + frame->linesize[2] * frame->height;
372 frame->linesize[3] = frame->width;
376 case AV_PIX_FMT_YUV422P:
377 case AV_PIX_FMT_YUVJ422P:
382 frame->linesize[0] = frame->width;
385 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
386 frame->linesize[1] = frame->width / 2;
389 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
390 frame->linesize[2] = frame->width / 2;
394 case AV_PIX_FMT_YUV422P16LE:
395 case AV_PIX_FMT_YUV422P16BE:
397 constexpr int byte_per_component = 2;
401 frame->linesize[0] = byte_per_component * frame->width;
404 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
405 frame->linesize[1] = byte_per_component * frame->width / 2;
408 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
409 frame->linesize[2] = byte_per_component * frame->width / 2;
413 case AV_PIX_FMT_NV24:
414 case AV_PIX_FMT_NV42:
419 frame->linesize[0] = frame->width;
422 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
423 frame->linesize[1] = frame->width * 2;
427 case AV_PIX_FMT_YUV410P:
432 frame->linesize[0] = frame->width;
435 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
436 frame->linesize[1] = frame->width / 4;
439 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height / 4;
440 frame->linesize[2] = frame->width / 4;
444 case AV_PIX_FMT_YUV411P:
449 frame->linesize[0] = frame->width;
452 frame->data[1] = frame->data[0] + frame->linesize[0] * frame->height;
453 frame->linesize[1] = frame->width / 4;
456 frame->data[2] = frame->data[1] + frame->linesize[1] * frame->height;
457 frame->linesize[2] = frame->width / 4;
461 case AV_PIX_FMT_YUV440P:
462 case AV_PIX_FMT_YUVJ440P:
463 case AV_PIX_FMT_YUVA420P:
465 case AV_PIX_FMT_YUV420P9BE:
466 case AV_PIX_FMT_YUV420P9LE:
467 case AV_PIX_FMT_YUV420P10BE:
468 case AV_PIX_FMT_YUV420P10LE:
469 case AV_PIX_FMT_YUV422P10BE:
470 case AV_PIX_FMT_YUV422P10LE:
471 case AV_PIX_FMT_YUV444P9BE:
472 case AV_PIX_FMT_YUV444P9LE:
473 case AV_PIX_FMT_YUV444P10BE:
474 case AV_PIX_FMT_YUV444P10LE:
475 case AV_PIX_FMT_YUV422P9BE:
476 case AV_PIX_FMT_YUV422P9LE:
478 case AV_PIX_FMT_GBRP9BE:
479 case AV_PIX_FMT_GBRP9LE:
480 case AV_PIX_FMT_GBRP10BE:
481 case AV_PIX_FMT_GBRP10LE:
482 case AV_PIX_FMT_GBRP16BE:
483 case AV_PIX_FMT_GBRP16LE:
484 case AV_PIX_FMT_YUVA422P:
485 case AV_PIX_FMT_YUVA420P9BE:
486 case AV_PIX_FMT_YUVA420P9LE:
487 case AV_PIX_FMT_YUVA422P9BE:
488 case AV_PIX_FMT_YUVA422P9LE:
489 case AV_PIX_FMT_YUVA444P9BE:
490 case AV_PIX_FMT_YUVA444P9LE:
491 case AV_PIX_FMT_YUVA420P10BE:
492 case AV_PIX_FMT_YUVA420P10LE:
493 case AV_PIX_FMT_YUVA422P10BE:
494 case AV_PIX_FMT_YUVA422P10LE:
495 case AV_PIX_FMT_YUVA444P10BE:
496 case AV_PIX_FMT_YUVA444P10LE:
497 case AV_PIX_FMT_YUVA420P16BE:
498 case AV_PIX_FMT_YUVA420P16LE:
499 case AV_PIX_FMT_YUVA422P16BE:
500 case AV_PIX_FMT_YUVA422P16LE:
501 case AV_PIX_FMT_YUVA444P16BE:
502 case AV_PIX_FMT_YUVA444P16LE:
504 case AV_PIX_FMT_YUV420P12BE:
505 case AV_PIX_FMT_YUV420P12LE:
506 case AV_PIX_FMT_YUV420P14BE:
507 case AV_PIX_FMT_YUV420P14LE:
508 case AV_PIX_FMT_YUV422P12BE:
509 case AV_PIX_FMT_YUV422P12LE:
510 case AV_PIX_FMT_YUV422P14BE:
511 case AV_PIX_FMT_YUV422P14LE:
512 case AV_PIX_FMT_YUV444P12BE:
513 case AV_PIX_FMT_YUV444P12LE:
514 case AV_PIX_FMT_YUV444P14BE:
515 case AV_PIX_FMT_YUV444P14LE:
516 case AV_PIX_FMT_GBRP12BE:
517 case AV_PIX_FMT_GBRP12LE:
518 case AV_PIX_FMT_GBRP14BE:
519 case AV_PIX_FMT_GBRP14LE:
520 case AV_PIX_FMT_YUVJ411P:
522 case AV_PIX_FMT_YUV440P10LE:
523 case AV_PIX_FMT_YUV440P10BE:
524 case AV_PIX_FMT_YUV440P12LE:
525 case AV_PIX_FMT_YUV440P12BE:
527 case AV_PIX_FMT_GBRAP12BE:
528 case AV_PIX_FMT_GBRAP12LE:
530 case AV_PIX_FMT_GBRAP10BE:
531 case AV_PIX_FMT_GBRAP10LE:
533 case AV_PIX_FMT_GBRPF32BE:
534 case AV_PIX_FMT_GBRPF32LE:
535 case AV_PIX_FMT_GBRAPF32BE:
536 case AV_PIX_FMT_GBRAPF32LE:
538 case AV_PIX_FMT_YUVA422P12BE:
539 case AV_PIX_FMT_YUVA422P12LE:
540 case AV_PIX_FMT_YUVA444P12BE:
541 case AV_PIX_FMT_YUVA444P12LE:
543 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";