v4l2-mjpeg.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. decoder->context->pix_fmt = AV_PIX_FMT_YUVJ422P;
  38. if (avcodec_open2(decoder->context, decoder->codec, NULL) < 0) {
  39. blog(LOG_ERROR, "failed to open codec");
  40. return -1;
  41. }
  42. blog(LOG_DEBUG, "initialized avcodec");
  43. return 0;
  44. }
  45. void v4l2_destroy_mjpeg(struct v4l2_mjpeg_decoder *decoder)
  46. {
  47. blog(LOG_DEBUG, "destroying avcodec");
  48. if (decoder->frame) {
  49. av_frame_free(&decoder->frame);
  50. }
  51. if (decoder->packet) {
  52. av_packet_free(&decoder->packet);
  53. }
  54. if (decoder->context) {
  55. avcodec_free_context(&decoder->context);
  56. }
  57. }
  58. int v4l2_decode_mjpeg(struct obs_source_frame *out, uint8_t *data,
  59. size_t length, struct v4l2_mjpeg_decoder *decoder)
  60. {
  61. decoder->packet->data = data;
  62. decoder->packet->size = length;
  63. if (avcodec_send_packet(decoder->context, decoder->packet) < 0) {
  64. blog(LOG_ERROR, "failed to send jpeg to codec");
  65. return -1;
  66. }
  67. if (avcodec_receive_frame(decoder->context, decoder->frame) < 0) {
  68. blog(LOG_ERROR, "failed to recieve frame from codec");
  69. return -1;
  70. }
  71. for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i) {
  72. out->data[i] = decoder->frame->data[i];
  73. out->linesize[i] = decoder->frame->linesize[i];
  74. }
  75. return 0;
  76. }