1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <sstream>
  6. extern "C" {
  7. #include <libavcodec/avcodec.h>
  8. #include <libavformat/avformat.h>
  9. #include <libavformat/avio.h>
  10. #include <libswscale/swscale.h>
  11. }
  12. #define log(msg) std::wcout << L"[FFmpegRTSP] " << msg << std::endl << std::flush
  13. #define logError(msg)std:: wcout << L"[Error] [FFmpegRTSP] " << msg << std::endl << std::flush
  14. AVFormatContext* avFormatContext = nullptr;
  15. SwsContext *swsContext = nullptr;
  16. AVCodecContext* avCodecContext;
  17. AVCodec *codec = nullptr;
  18. int frameCount = 0;
  19. void writePPM(AVFrame* frameRGB, int fileSuffix) {
  20. std::stringstream file_name;
  21. std::ofstream output_file;
  22. file_name << "test" << fileSuffix << ".ppm";
  23. output_file.open(file_name.str().c_str());
  24. output_file << "P3 " << avCodecContext->width << " " << avCodecContext->height
  25. << " 255\n";
  26. for (int y = 0; y < avCodecContext->height; y++) {
  27. for (int x = 0; x < avCodecContext->width * 3; x++) {
  28. output_file
  29. << (int)(frameRGB->data[0]
  30. + y * frameRGB->linesize[0])[x] << " ";
  31. }
  32. }
  33. output_file.close();
  34. }
  35. int main(int argc, char** argv) {
  36. avFormatContext = avformat_alloc_context();
  37. if (avformat_open_input(&avFormatContext, "rtsp://admin:123456@192.168.2.109:554/h264Preview_01_main",
  38. NULL, NULL) != 0) {
  39. logError("Failed to open Stream");
  40. return EXIT_FAILURE;
  41. }
  42. if (avformat_find_stream_info(avFormatContext, NULL) < 0) {
  43. return EXIT_FAILURE;
  44. }
  45. int videoStreamIndex = -1;
  46. for (int i = 0; i < avFormatContext->nb_streams; i++) {
  47. if (avFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  48. videoStreamIndex = i;
  49. }
  50. }
  51. if (videoStreamIndex == -1) {
  52. return EXIT_FAILURE;
  53. }
  54. av_read_play(avFormatContext);
  55. codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  56. if (!codec) {
  57. logError("H264 codec not found, compile FFmpeg with libopenh264 or libx264");
  58. return EXIT_FAILURE;
  59. }
  60. avCodecContext = avcodec_alloc_context3(codec);
  61. avcodec_get_context_defaults3(avCodecContext, codec);
  62. avcodec_copy_context(avCodecContext, avFormatContext->streams[videoStreamIndex]->codec);
  63. if (avcodec_open2(avCodecContext, codec, NULL) != 0) {
  64. logError("Failed to open codec");
  65. return EXIT_FAILURE;
  66. }
  67. int width = avCodecContext->width;
  68. int height = avCodecContext->height;
  69. swsContext = sws_getContext(width, height, avCodecContext->pix_fmt, width,
  70. height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  71. AVFrame* frame = av_frame_alloc();
  72. frame->format = avCodecContext->pix_fmt;
  73. frame->width = width;
  74. frame->height = height;
  75. av_frame_get_buffer(frame, 0);
  76. AVFrame* frameRGB = av_frame_alloc();
  77. frameRGB->format = AV_PIX_FMT_RGB24;
  78. frameRGB->width = width;
  79. frameRGB->height = height;
  80. av_frame_get_buffer(frameRGB, 0);
  81. AVPacket packet;
  82. av_init_packet(&packet);
  83. while (av_read_frame(avFormatContext, &packet) >= 0 && frameCount < 1000) {
  84. if (packet.stream_index == videoStreamIndex) {
  85. int gotFrame = 0;
  86. packet.stream_index = frameCount;
  87. int result = avcodec_decode_video2(avCodecContext, frame, &gotFrame, &packet);
  88. if (frameCount % 100 == 1 && gotFrame){
  89. sws_scale(swsContext, frame->data, frame->linesize, 0,
  90. avCodecContext->height, frameRGB->data, frameRGB->linesize);
  91. writePPM(frameRGB, frameCount);
  92. }
  93. frameCount++;
  94. }
  95. av_free_packet(&packet);
  96. av_init_packet(&packet);
  97. }
  98. av_frame_free(&frame);
  99. av_frame_free(&frameRGB);
  100. av_read_pause(avFormatContext);
  101. avcodec_free_context(&avCodecContext);
  102. avformat_free_context(avFormatContext);
  103. return EXIT_SUCCESS;
  104. }

goto line:
Compare with:
text copy window edit this code post new code