v4l2-decoder.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright (C) 2020 by Morten Bøgeskov <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <obs-module.h>
  15. #include <linux/videodev2.h>
  16. #include <libavutil/error.h>
  17. #include "v4l2-decoder.h"
  18. #define blog(level, msg, ...) blog(level, "v4l2-input: decoder: " msg, ##__VA_ARGS__)
  19. int v4l2_init_decoder(struct v4l2_decoder *decoder, int pixfmt)
  20. {
  21. if (pixfmt == V4L2_PIX_FMT_MJPEG) {
  22. decoder->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  23. } else if (pixfmt == V4L2_PIX_FMT_H264) {
  24. decoder->codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  25. }
  26. if (!decoder->codec) {
  27. if (pixfmt == V4L2_PIX_FMT_MJPEG) {
  28. blog(LOG_ERROR, "failed to find MJPEG decoder");
  29. } else if (pixfmt == V4L2_PIX_FMT_H264) {
  30. blog(LOG_ERROR, "failed to find H264 decoder");
  31. }
  32. return -1;
  33. }
  34. decoder->context = avcodec_alloc_context3(decoder->codec);
  35. if (!decoder->context) {
  36. return -1;
  37. }
  38. decoder->packet = av_packet_alloc();
  39. if (!decoder->packet) {
  40. return -1;
  41. }
  42. decoder->frame = av_frame_alloc();
  43. if (!decoder->frame) {
  44. return -1;
  45. }
  46. decoder->context->flags2 |= AV_CODEC_FLAG2_FAST;
  47. if (avcodec_open2(decoder->context, decoder->codec, NULL) < 0) {
  48. blog(LOG_ERROR, "failed to open codec");
  49. return -1;
  50. }
  51. blog(LOG_DEBUG, "initialized avcodec");
  52. return 0;
  53. }
  54. void v4l2_destroy_decoder(struct v4l2_decoder *decoder)
  55. {
  56. blog(LOG_DEBUG, "destroying avcodec");
  57. if (decoder->frame) {
  58. av_frame_free(&decoder->frame);
  59. }
  60. if (decoder->packet) {
  61. av_packet_free(&decoder->packet);
  62. }
  63. if (decoder->context) {
  64. #if LIBAVCODEC_VERSION_MAJOR < 61
  65. avcodec_close(decoder->context);
  66. #endif
  67. avcodec_free_context(&decoder->context);
  68. }
  69. }
  70. int v4l2_decode_frame(struct obs_source_frame *out, uint8_t *data, size_t length, struct v4l2_decoder *decoder)
  71. {
  72. int r;
  73. decoder->packet->data = data;
  74. decoder->packet->size = length;
  75. if (avcodec_send_packet(decoder->context, decoder->packet) < 0) {
  76. blog(LOG_ERROR, "failed to send frame to codec");
  77. return -1;
  78. }
  79. r = avcodec_receive_frame(decoder->context, decoder->frame);
  80. if (r == AVERROR(EAGAIN)) {
  81. blog(LOG_DEBUG, "failed to receive frame in this state, try to send new frame to codec");
  82. return 0;
  83. } else if (r < 0) {
  84. blog(LOG_ERROR, "failed to receive frame from codec");
  85. return -1;
  86. }
  87. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i) {
  88. out->data[i] = decoder->frame->data[i];
  89. out->linesize[i] = decoder->frame->linesize[i];
  90. }
  91. switch (decoder->context->pix_fmt) {
  92. case AV_PIX_FMT_GRAY8:
  93. out->format = VIDEO_FORMAT_Y800;
  94. break;
  95. case AV_PIX_FMT_YUVJ422P:
  96. case AV_PIX_FMT_YUV422P:
  97. out->format = VIDEO_FORMAT_I422;
  98. break;
  99. case AV_PIX_FMT_YUVJ420P:
  100. case AV_PIX_FMT_YUV420P:
  101. out->format = VIDEO_FORMAT_I420;
  102. break;
  103. case AV_PIX_FMT_YUVJ444P:
  104. case AV_PIX_FMT_YUV444P:
  105. out->format = VIDEO_FORMAT_I444;
  106. break;
  107. default:
  108. break;
  109. }
  110. return 0;
  111. }