小言_互联网的博客

FFmpeg在Windows上设置dshow mjpeg编码+libyuv解码显示测试代码

308人阅读  评论(0)

之前在https://blog.csdn.net/fengbingchun/article/details/103444891中介绍过在Windows上通过ffmpeg dshow设置为mjpeg编解码方式进行实时显示的测试代码。这里测试仅调用ffmpeg的mjpeg编码接口,获取到packet后,通过libyuv+libjpeg-turbo对mjpeg进行解码并实时显示的测试代码,代码如下:


  
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <memory>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #include <libavdevice/avdevice.h>
  9. #include <libavformat/avformat.h>
  10. #include <libavcodec/avcodec.h>
  11. #include <libswscale/swscale.h>
  12. #include <libavutil/mem.h>
  13. #include <libavutil/imgutils.h>
  14. #ifdef __cplusplus
  15. }
  16. #endif
  17. #include <opencv2/opencv.hpp>
  18. #include <libyuv/convert_argb.h>
  19. #include <libyuv/convert.h>
  20. int test_ffmpeg_dshow_mjpeg_encode_libyuv_decode()
  21. {
  22. avdevice_register_all();
  23. AVCodecID id = AV_CODEC_ID_MJPEG;
  24. AVCodec* encoder_id = avcodec_find_encoder(id);
  25. if (!encoder_id) {
  26. fprintf( stderr, "codec not found: %d\n", id);
  27. return -1;
  28. }
  29. AVFormatContext* format_context = avformat_alloc_context();
  30. format_context->video_codec_id = id;
  31. AVInputFormat* input_format = av_find_input_format( "dshow");
  32. AVDictionary* dict = nullptr;
  33. if (av_dict_set(&dict, "video_size", "960x540", 0) < 0) fprintf( stderr, "fail to av_dict_set: line: %d\n", __LINE__);
  34. int ret = avformat_open_input(&format_context, "video=Integrated Webcam", input_format, &dict);
  35. if (ret != 0) {
  36. fprintf( stderr, "fail to avformat_open_input: %d\n", ret);
  37. return -1;
  38. }
  39. ret = avformat_find_stream_info(format_context, nullptr);
  40. if (ret < 0) {
  41. fprintf( stderr, "fail to get stream information: %d\n", ret);
  42. return -1;
  43. }
  44. int video_stream_index = -1;
  45. for ( unsigned int i = 0; i < format_context->nb_streams; ++i) {
  46. const AVStream* stream = format_context->streams[i];
  47. if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  48. video_stream_index = i;
  49. fprintf( stdout, "type of the encoded data: %d, dimensions of the video frame in pixels: width: %d, height: %d, pixel format: %d\n",
  50. stream->codecpar->codec_id, stream->codecpar->width, stream->codecpar->height, stream->codecpar->format);
  51. }
  52. }
  53. if (video_stream_index == -1) {
  54. fprintf( stderr, "no video stream\n");
  55. return -1;
  56. }
  57. AVCodecParameters* codecpar = format_context->streams[video_stream_index]->codecpar;
  58. if (codecpar->codec_id != id) {
  59. fprintf( stderr, "this test code only support mjpeg encode: %d\n", codecpar->codec_id);
  60. return -1;
  61. }
  62. AVPacket* packet = (AVPacket*)av_malloc( sizeof(AVPacket));
  63. if (!packet) {
  64. fprintf( stderr, "fail to alloc\n");
  65. return -1;
  66. }
  67. std:: unique_ptr< unsigned char[]> data( new unsigned char[codecpar->width * codecpar->height * 4]);
  68. cv:: Mat mat(codecpar->height,codecpar->width, CV_8UC4, data.get());
  69. const char* winname = "dshow mjpeg encode and libyuv decode";
  70. cv::namedWindow(winname);
  71. AVFrame* frame = av_frame_alloc();
  72. while ( 1) {
  73. ret = av_read_frame(format_context, packet);
  74. if (ret >= 0 && packet->stream_index == video_stream_index && packet->size > 0) {
  75. int dst_width, dst_height;
  76. libyuv::MJPGSize(packet->data, packet->size, &dst_width, &dst_height);
  77. if (dst_width != codecpar->width || dst_height != codecpar->height) {
  78. fprintf( stderr, "width or height dismatch: (%d, %d), (%d, %d)\n",
  79. dst_width, dst_height, codecpar->width, codecpar->height);
  80. return -1;
  81. }
  82. libyuv::MJPGToARGB(packet->data, packet->size, data.get(), dst_width * 4,
  83. codecpar->width, codecpar->height, dst_width, dst_height);
  84. cv::imshow(winname, mat);
  85. } else if (ret < 0 || packet->size <= 0) {
  86. fprintf( stderr, "fail to av_read_frame: %d, packet size: %d\n", ret, packet->size);
  87. continue;
  88. }
  89. av_packet_unref(packet);
  90. int key = cv::waitKey( 30);
  91. if (key == 27) break;
  92. }
  93. cv::destroyWindow(winname);
  94. av_freep(packet);
  95. avformat_close_input(&format_context);
  96. av_dict_free(&dict);
  97. fprintf( stdout, "test finish\n");
  98. return 0;
  99. }

执行结果如下:

GitHubhttps://github.com//fengbingchun/OpenCV_Test


转载:https://blog.csdn.net/fengbingchun/article/details/103583548
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场