1
0

v4l2-mjpeg.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "v4l2-mjpeg.h"
  16. #define blog(level, msg, ...) \
  17. blog(level, "v4l2-input: mjpeg: " msg, ##__VA_ARGS__)
  18. int v4l2_init_mjpeg(struct v4l2_mjpeg_decoder *decoder)
  19. {
  20. decoder->codec = avcodec_find_decoder(AV_CODEC_ID_MJPEG);
  21. if (!decoder->codec) {
  22. return -1;
  23. }
  24. decoder->context = avcodec_alloc_context3(decoder->codec);
  25. if (!decoder->context) {
  26. return -1;
  27. }
  28. decoder->packet = av_packet_alloc();
  29. if (!decoder->packet) {
  30. return -1;
  31. }
  32. decoder->frame = av_frame_alloc();
  33. if (!decoder->frame) {
  34. return -1;
  35. }
  36. decoder->context->flags2 |= AV_CODEC_FLAG2_FAST;
  37. if (avcodec_open2(decoder->context, decoder->codec, NULL) < 0) {
  38. blog(LOG_ERROR, "failed to open codec");
  39. return -1;
  40. }
  41. blog(LOG_DEBUG, "initialized avcodec");
  42. return 0;
  43. }
  44. void v4l2_destroy_mjpeg(struct v4l2_mjpeg_decoder *decoder)
  45. {
  46. blog(LOG_DEBUG, "destroying avcodec");
  47. if (decoder->frame) {
  48. av_frame_free(&decoder->frame);
  49. }
  50. if (decoder->packet) {
  51. av_packet_free(&decoder->packet);
  52. }
  53. if (decoder->context) {
  54. avcodec_free_context(&decoder->context);
  55. }
  56. }
  57. int v4l2_decode_mjpeg(struct obs_source_frame *out, uint8_t *data,
  58. size_t length, struct v4l2_mjpeg_decoder *decoder)
  59. {
  60. decoder->packet->data = data;
  61. decoder->packet->size = length;
  62. if (avcodec_send_packet(decoder->context, decoder->packet) < 0) {
  63. blog(LOG_ERROR, "failed to send jpeg to codec");
  64. return -1;
  65. }
  66. if (avcodec_receive_frame(decoder->context, decoder->frame) < 0) {
  67. blog(LOG_ERROR, "failed to recieve frame from codec");
  68. return -1;
  69. }
  70. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i) {
  71. out->data[i] = decoder->frame->data[i];
  72. out->linesize[i] = decoder->frame->linesize[i];
  73. }
  74. switch (decoder->context->pix_fmt) {
  75. case AV_PIX_FMT_YUVJ422P:
  76. case AV_PIX_FMT_YUV422P:
  77. out->format = VIDEO_FORMAT_I422;
  78. break;
  79. case AV_PIX_FMT_YUVJ420P:
  80. case AV_PIX_FMT_YUV420P:
  81. out->format = VIDEO_FORMAT_I420;
  82. break;
  83. case AV_PIX_FMT_YUVJ444P:
  84. case AV_PIX_FMT_YUV444P:
  85. out->format = VIDEO_FORMAT_I444;
  86. break;
  87. default:
  88. break;
  89. }
  90. return 0;
  91. }