v4l2-decoder.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright (C) 2020 by Morten Bøgeskov <source@kosmisk.dk>
  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 "v4l2-decoder.h"
  17. #define blog(level, msg, ...) blog(level, "v4l2-input: decoder: " msg, ##__VA_ARGS__)
  18. int v4l2_init_decoder(struct v4l2_decoder *decoder, int pixfmt)
  19. {
  20. if (pixfmt == V4L2_PIX_FMT_MJPEG) {
  21. decoder->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  22. } else if (pixfmt == V4L2_PIX_FMT_H264) {
  23. decoder->codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  24. }
  25. if (!decoder->codec) {
  26. if (pixfmt == V4L2_PIX_FMT_MJPEG) {
  27. blog(LOG_ERROR, "failed to find MJPEG decoder");
  28. } else if (pixfmt == V4L2_PIX_FMT_H264) {
  29. blog(LOG_ERROR, "failed to find H264 decoder");
  30. }
  31. return -1;
  32. }
  33. decoder->context = avcodec_alloc_context3(decoder->codec);
  34. if (!decoder->context) {
  35. return -1;
  36. }
  37. decoder->packet = av_packet_alloc();
  38. if (!decoder->packet) {
  39. return -1;
  40. }
  41. decoder->frame = av_frame_alloc();
  42. if (!decoder->frame) {
  43. return -1;
  44. }
  45. decoder->context->flags2 |= AV_CODEC_FLAG2_FAST;
  46. if (avcodec_open2(decoder->context, decoder->codec, NULL) < 0) {
  47. blog(LOG_ERROR, "failed to open codec");
  48. return -1;
  49. }
  50. blog(LOG_DEBUG, "initialized avcodec");
  51. return 0;
  52. }
  53. void v4l2_destroy_decoder(struct v4l2_decoder *decoder)
  54. {
  55. blog(LOG_DEBUG, "destroying avcodec");
  56. if (decoder->frame) {
  57. av_frame_free(&decoder->frame);
  58. }
  59. if (decoder->packet) {
  60. av_packet_free(&decoder->packet);
  61. }
  62. if (decoder->context) {
  63. #if LIBAVCODEC_VERSION_MAJOR < 61
  64. avcodec_close(decoder->context);
  65. #endif
  66. avcodec_free_context(&decoder->context);
  67. }
  68. }
  69. int v4l2_decode_frame(struct obs_source_frame *out, uint8_t *data, size_t length, struct v4l2_decoder *decoder)
  70. {
  71. decoder->packet->data = data;
  72. decoder->packet->size = length;
  73. if (avcodec_send_packet(decoder->context, decoder->packet) < 0) {
  74. blog(LOG_ERROR, "failed to send frame to codec");
  75. return -1;
  76. }
  77. if (avcodec_receive_frame(decoder->context, decoder->frame) < 0) {
  78. blog(LOG_ERROR, "failed to receive frame from codec");
  79. return -1;
  80. }
  81. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i) {
  82. out->data[i] = decoder->frame->data[i];
  83. out->linesize[i] = decoder->frame->linesize[i];
  84. }
  85. switch (decoder->context->pix_fmt) {
  86. case AV_PIX_FMT_GRAY8:
  87. out->format = VIDEO_FORMAT_Y800;
  88. break;
  89. case AV_PIX_FMT_YUVJ422P:
  90. case AV_PIX_FMT_YUV422P:
  91. out->format = VIDEO_FORMAT_I422;
  92. break;
  93. case AV_PIX_FMT_YUVJ420P:
  94. case AV_PIX_FMT_YUV420P:
  95. out->format = VIDEO_FORMAT_I420;
  96. break;
  97. case AV_PIX_FMT_YUVJ444P:
  98. case AV_PIX_FMT_YUV444P:
  99. out->format = VIDEO_FORMAT_I444;
  100. break;
  101. default:
  102. break;
  103. }
  104. return 0;
  105. }