1
0

v4l2-decoder.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "v4l2-decoder.h"
  17. #define blog(level, msg, ...) \
  18. 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. avcodec_close(decoder->context);
  65. avcodec_free_context(&decoder->context);
  66. }
  67. }
  68. int v4l2_decode_frame(struct obs_source_frame *out, uint8_t *data,
  69. 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_YUVJ422P:
  87. case AV_PIX_FMT_YUV422P:
  88. out->format = VIDEO_FORMAT_I422;
  89. break;
  90. case AV_PIX_FMT_YUVJ420P:
  91. case AV_PIX_FMT_YUV420P:
  92. out->format = VIDEO_FORMAT_I420;
  93. break;
  94. case AV_PIX_FMT_YUVJ444P:
  95. case AV_PIX_FMT_YUV444P:
  96. out->format = VIDEO_FORMAT_I444;
  97. break;
  98. default:
  99. break;
  100. }
  101. return 0;
  102. }