recording.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #if DEMOMODE
  2. #include "recording.h"
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <ctime>
  7. #include <iostream>
  8. #include <thread>
  9. #include <vector>
  10. extern "C" {
  11. #include <libavcodec/avcodec.h>
  12. #include <libavformat/avformat.h>
  13. #include <libavutil/imgutils.h>
  14. #include <libswscale/swscale.h>
  15. }
  16. const int FPS = 30;
  17. const int FRAME_COUNT = FPS * 5;
  18. int WIDTH = 0;
  19. int HEIGHT = 0;
  20. int SOURCE_WIDTH = 0;
  21. int SOURCE_HEIGHT = 0;
  22. int64_t frame_count = 0;
  23. const AVCodec* video_codec = nullptr;
  24. AVCodecContext* video_codec_context = nullptr;
  25. AVFormatContext* format_context = nullptr;
  26. AVStream* video_stream = nullptr;
  27. SwsContext* sws_context = nullptr;
  28. AVFrame* video_frame = nullptr;
  29. AVFrame* rgb_frame = nullptr;
  30. int init_encoder (const char* output_file, int sourceWidth, int sourceHeight) {
  31. float factor = 512.0f / (float)sourceWidth;
  32. SOURCE_WIDTH = sourceWidth;
  33. SOURCE_HEIGHT = sourceHeight;
  34. WIDTH = (float)sourceWidth * factor;
  35. HEIGHT = (float)sourceHeight * factor;
  36. avformat_network_init ();
  37. if (avformat_alloc_output_context2 (&format_context, nullptr, "webm", output_file) < 0) {
  38. std::cerr << "Error initializing format context" << std::endl;
  39. return -1;
  40. }
  41. video_codec = avcodec_find_encoder (AV_CODEC_ID_VP9);
  42. if (!video_codec) {
  43. std::cerr << "VP8 codec not found!" << std::endl;
  44. return -1;
  45. }
  46. video_codec_context = avcodec_alloc_context3 (video_codec);
  47. if (!video_codec_context) {
  48. std::cerr << "Error allocating video codec context" << std::endl;
  49. return -1;
  50. }
  51. video_codec_context->bit_rate = 4000000;
  52. video_codec_context->width = WIDTH;
  53. video_codec_context->height = HEIGHT;
  54. video_codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
  55. video_codec_context->time_base = (AVRational) { 1, FPS };
  56. video_codec_context->framerate = (AVRational) { FPS, 1 };
  57. video_codec_context->gop_size = 12;
  58. video_codec_context->max_b_frames = 1;
  59. video_codec_context->qmin = 10;
  60. video_codec_context->qmax = 40;
  61. if (avcodec_open2 (video_codec_context, video_codec, nullptr) < 0) {
  62. std::cerr << "Error opening VP8 codec" << std::endl;
  63. return -1;
  64. }
  65. video_stream = avformat_new_stream (format_context, video_codec);
  66. if (!video_stream) {
  67. std::cerr << "Error creating video stream" << std::endl;
  68. return -1;
  69. }
  70. if (avcodec_parameters_from_context (video_stream->codecpar, video_codec_context) < 0) {
  71. std::cerr << "Error copying codec parameters to stream" << std::endl;
  72. return -1;
  73. }
  74. video_stream->time_base = video_codec_context->time_base;
  75. if (avio_open (&format_context->pb, output_file, AVIO_FLAG_WRITE) < 0) {
  76. std::cerr << "Error opening output file" << std::endl;
  77. return -1;
  78. }
  79. if (avformat_write_header (format_context, nullptr) < 0) {
  80. std::cerr << "Error writing file header" << std::endl;
  81. return -1;
  82. }
  83. video_frame = av_frame_alloc ();
  84. video_frame->format = AV_PIX_FMT_YUV420P;
  85. video_frame->width = WIDTH;
  86. video_frame->height = HEIGHT;
  87. av_frame_get_buffer (video_frame, 0);
  88. rgb_frame = av_frame_alloc ();
  89. rgb_frame->format = AV_PIX_FMT_RGB24;
  90. rgb_frame->width = SOURCE_WIDTH;
  91. rgb_frame->height = SOURCE_HEIGHT;
  92. sws_context = sws_getContext (
  93. SOURCE_WIDTH, SOURCE_HEIGHT, AV_PIX_FMT_RGB24, WIDTH, HEIGHT, AV_PIX_FMT_YUV420P, SWS_BICUBIC, nullptr, nullptr,
  94. nullptr
  95. );
  96. return 0;
  97. }
  98. int write_video_frame (const uint8_t* rgb_data) {
  99. av_image_fill_arrays (
  100. rgb_frame->data, rgb_frame->linesize, rgb_data, AV_PIX_FMT_RGB24, SOURCE_WIDTH, SOURCE_HEIGHT, 1
  101. );
  102. sws_scale (
  103. sws_context, rgb_frame->data, rgb_frame->linesize, 0, SOURCE_HEIGHT, video_frame->data, video_frame->linesize
  104. );
  105. int ret = avcodec_send_frame (video_codec_context, video_frame);
  106. if (ret < 0) {
  107. std::cerr << "Error sending video frame: " << ret << std::endl;
  108. return -1;
  109. }
  110. AVPacket packet;
  111. av_init_packet (&packet);
  112. packet.data = nullptr;
  113. packet.size = 0;
  114. ret = avcodec_receive_packet (video_codec_context, &packet);
  115. if (ret < 0) {
  116. std::cerr << "Error receiving video packet: " << ret << std::endl;
  117. return -1;
  118. }
  119. packet.stream_index = video_stream->index;
  120. packet.pts = av_rescale_q (frame_count, video_codec_context->time_base, video_stream->time_base);
  121. packet.dts = packet.pts;
  122. packet.duration = av_rescale_q (1, video_codec_context->time_base, video_stream->time_base);
  123. frame_count++;
  124. ret = av_interleaved_write_frame (format_context, &packet);
  125. if (ret < 0) {
  126. std::cerr << "Error writing video packet: " << ret << std::endl;
  127. return -1;
  128. }
  129. av_packet_unref (&packet);
  130. return 0;
  131. }
  132. int close_encoder () {
  133. avcodec_flush_buffers (video_codec_context);
  134. av_write_trailer (format_context);
  135. avcodec_free_context (&video_codec_context);
  136. avformat_free_context (format_context);
  137. av_frame_free (&video_frame);
  138. sws_freeContext (sws_context);
  139. return 0;
  140. }
  141. #endif