当前位置: 首页 > 编程日记 > 正文

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

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

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <memory>#ifdef __cplusplus
extern "C" {
#endif#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/mem.h>
#include <libavutil/imgutils.h>#ifdef __cplusplus
}
#endif#include <opencv2/opencv.hpp>
#include <libyuv/convert_argb.h>
#include <libyuv/convert.h>int test_ffmpeg_dshow_mjpeg_encode_libyuv_decode()
{avdevice_register_all();AVCodecID id = AV_CODEC_ID_MJPEG;AVCodec* encoder_id = avcodec_find_encoder(id);if (!encoder_id) {fprintf(stderr, "codec not found: %d\n", id);return -1;}AVFormatContext* format_context = avformat_alloc_context();format_context->video_codec_id = id;AVInputFormat* input_format = av_find_input_format("dshow");AVDictionary* dict = nullptr;if (av_dict_set(&dict, "video_size", "960x540", 0) < 0) fprintf(stderr, "fail to av_dict_set: line: %d\n", __LINE__);int ret = avformat_open_input(&format_context, "video=Integrated Webcam", input_format, &dict);if (ret != 0) {fprintf(stderr, "fail to avformat_open_input: %d\n", ret);return -1;}ret = avformat_find_stream_info(format_context, nullptr);if (ret < 0) {fprintf(stderr, "fail to get stream information: %d\n", ret);return -1;}int video_stream_index = -1;for (unsigned int i = 0; i < format_context->nb_streams; ++i) {const AVStream* stream = format_context->streams[i];if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {video_stream_index = i;fprintf(stdout, "type of the encoded data: %d, dimensions of the video frame in pixels: width: %d, height: %d, pixel format: %d\n",stream->codecpar->codec_id, stream->codecpar->width, stream->codecpar->height, stream->codecpar->format);}}if (video_stream_index == -1) {fprintf(stderr, "no video stream\n");return -1;}AVCodecParameters* codecpar = format_context->streams[video_stream_index]->codecpar;if (codecpar->codec_id != id) {fprintf(stderr, "this test code only support mjpeg encode: %d\n", codecpar->codec_id);return -1;}AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));if (!packet) {fprintf(stderr, "fail to alloc\n");return -1;}std::unique_ptr<unsigned char[]> data(new unsigned char[codecpar->width * codecpar->height * 4]);cv::Mat mat(codecpar->height,codecpar->width, CV_8UC4, data.get());const char* winname = "dshow mjpeg encode and libyuv decode";cv::namedWindow(winname);AVFrame* frame = av_frame_alloc();while (1) {ret = av_read_frame(format_context, packet);if (ret >= 0 && packet->stream_index == video_stream_index && packet->size > 0) {int dst_width, dst_height;libyuv::MJPGSize(packet->data, packet->size, &dst_width, &dst_height);if (dst_width != codecpar->width || dst_height != codecpar->height) {fprintf(stderr, "width or height dismatch: (%d, %d), (%d, %d)\n",dst_width, dst_height, codecpar->width, codecpar->height);return -1;}libyuv::MJPGToARGB(packet->data, packet->size, data.get(), dst_width * 4,codecpar->width, codecpar->height, dst_width, dst_height);cv::imshow(winname, mat);} else if (ret < 0 || packet->size <= 0) {fprintf(stderr, "fail to av_read_frame: %d, packet size: %d\n", ret, packet->size);continue;}av_packet_unref(packet);int key = cv::waitKey(30);if (key == 27) break;}cv::destroyWindow(winname);av_freep(packet);avformat_close_input(&format_context);av_dict_free(&dict);fprintf(stdout, "test finish\n");return 0;
}

执行结果如下:

GitHub:https://github.com//fengbingchun/OpenCV_Test

相关文章:

转:浅谈Linux的内存管理机制

一 物理内存和虚拟内存 我们知道&#xff0c;直接从物理内存读写数据要比从硬盘读写数据要快的多&#xff0c;因此&#xff0c;我们希望所有数据的读取和写入都在内存完成&#xff0c;而内存是有限的&#xff0c;这样就引出了物理内存与虚拟内存的概念。 物理内存就是系统硬件提…

swift3.0阿里百川反馈

闲言少叙 直接上不熟 1.导入自己工程阿里百川demo中的Util文件,并引用其中的头文件 2.剩余就是swift3.0代码.在自己需要的地方书写 (前提是你已经申请了APPKey) 3.代码 //调用意见反馈 func actionOpenFeedback(){ //key self.appKey "此处填写自己申请的key" s…

通俗易懂:8大步骤图解注意力机制

作者 | Raimi Karim译者 | 夕颜出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;【导读】这是一份用图片和代码详解自注意力机制的指南&#xff0c;请收好。BERT、RoBERTa、ALBERT、SpanBERT、DistilBERT、SesameBERT、SemBERT、MobileBERT、TinyBERT和CamemBERT的共同…

Windows上VS2017单步调试FFmpeg源码的方法

之前在https://blog.csdn.net/fengbingchun/article/details/90114411 介绍过如何在Windows7/10上通过MinGW方式编译FFmpeg 4.1.3源码生成库的步骤&#xff0c;那时只能生成最终的库&#xff0c;却不能产生vs工程&#xff0c;无法进行单步调试。GitHub上有个项目ShiftMediaProj…

ormlite 多表联合查询

ormlite 多表联合查询 QueryBuilder shopBrandQueryBuilder shopBrandDao.queryBuilder(); QueryBuilder shopQueryBuilder shopDao.queryBuilder();Where shopBrandWhere shopBrandQueryBuilder.where(); shopBrandWhere .eq(ShopBrand.SHOP_NO, shopNo);Where shopWhere …

C++中关键字volatile和mutable用法

C/C中的volatile关键字和const对应&#xff0c;用来修饰变量&#xff0c;用于告诉编译器该变量值是不稳定的&#xff0c;可能被更改。使用volatile注意事项&#xff1a; (1). 编译器会对带有volatile关键字的变量禁用优化(A volatile specifier is a hint to a compiler that …

基于生成对抗网络(GAN)的人脸变形(附链接) | CSDN博文精选

扫码参与CSDN“原力计划”翻译 | 张一豪校对 | 吴金笛来源 | 数据派THU*点击阅读原文&#xff0c;查看「CSDN原力计划」详细说明。本文详细介绍了生成对抗网络&#xff08;GAN&#xff09;的知识&#xff0c;并用其变换人脸&#xff0c;并探寻如何利用StyleGAN生成不同属性&…

Jmeter连接Oracle数据库

一、Jmeter要连接oracle数据库&#xff0c;就必须复制JDBC驱动jar包文件ojdbc14.jar到Jmeter的lib目录下二、进入Jmeter的bin目录运行Jmeter.bat&#xff0c;启动Jmeter三、Jmeter软件配置如下&#xff1a;1、添加线程组右击线程组&#xff0c;选择“添加--配置元件--JDBC Conn…

swift3.0友盟分享

经过&#xff08;一&#xff09;的讲解&#xff0c;大家应该可以按照友盟提供的测试账号可以集成友盟分享了&#xff0c;友盟目前集合了18个APP共27种分享&#xff0c;可以授权的有10个App&#xff1a;微信、QQ、新浪微博、腾讯微博、人人网、豆瓣、Facebook、Twitter、Linkedi…

C++11中std::future的使用

C11中的std::future是一个模板类。std::future提供了一种用于访问异步操作结果的机制。std::future所引用的共享状态不能与任何其它异步返回的对象共享(与std::shared_future相反)( std::future references shared state that is not shared with any other asynchronous retur…

给算法工程师和研究员的「霸王餐」| 附招聘信息

现在的算法工程师真的是太难了&#xff01;要让AI会看人眼都分辨不清的医疗影像&#xff01;数据又不够&#xff0c;还得用前沿技术&#xff01;好不容易学会看片&#xff0c;还要让AI会分析病理&#xff01;然后模型搞出来了&#xff0c;还要把几十种模型&#xff0c;做N次计算…

swift3.0三种反向传值

一 :通知 1.通知传值所用方法 // MARK: - private methods(内部接口) let NotifMycation NSNotification.Name(rawValue:"MyNSNotification") func tempbuttonAction() { //这个方法可以传一个值 NotificationCenter.default.post(name: NotifMycation, object: &q…

C++11中std::shared_future的使用

C11中的std::shared_future是个模板类。与std::future类似&#xff0c;std::shared_future提供了一种访问异步操作结果的机制&#xff1b;不同于std::future&#xff0c;std::shared_future允许多个线程等待同一个共享状态&#xff1b;不同于std::future仅支持移动操作&#xf…

聊聊抖音、奈飞、Twitch、大疆、快手、B站的多媒体关键技术

随着5G牌照发放&#xff0c;5G终端正在迎来集中上市潮&#xff0c;对于5G带来的变革一触即发。目前互联网上超过七成的流量来自多媒体&#xff0c;未来这个比例将超过八成。音视频就像空气和水一样普及&#xff0c;深度到每个人的生活和工作中。同时&#xff0c;深度学习技术则…

Linux安全事件应急响应排查方法总结

Linux安全事件应急响应排查方法总结 Linux是服务器操作系统中最常用的操作系统&#xff0c;因为其拥有高性能、高扩展性、高安全性&#xff0c;受到了越来越多的运维人员追捧。但是针对Linux服务器操作系统的安全事件也非常多的。攻击方式主要是弱口令攻击、远程溢出攻击及其他…

C++11中std::packaged_task的使用

C11中的std::packaged_task是个模板类。std::packaged_task包装任何可调用目标(函数、lambda表达式、bind表达式、函数对象)以便它可以被异步调用。它的返回值或抛出的异常被存储于能通过std::future对象访问的共享状态中。 std::packaged_task类似于std::function&#xff0c…

Swift3.0和OC桥接方法

1.直接在工程中commandn&#xff0c;出现如图&#xff0c;点击Header File创建桥接文件Bridging-Header.h,如图&#xff1a; 2.点击next&#xff0c;出现如图画面&#xff0c;一定要记得勾选第一项&#xff0c;再点击create创建完成。 3.配置桥接文件&#xff0c;点击target - …

量子算命,在线掷筊:一个IBM量子云计算机的应用实践,代码都有了

整理 | Jane 出品| AI科技大本营&#xff08;ID&#xff1a;rgznai100&#xff09; “算命”&#xff0c;古今中外&#xff0c;亘古不衰的一门学问&#xff0c;哪怕到了今天&#xff0c;大家对算命占卜都抱着一些”敬畏“的信任心理&#xff0c;西方流行塔罗牌&#xff0c;国…

rails应用ajax之二:使用rails自身支持

考虑另一种情况&#xff1a; 1. 页面上半部分显示当前的所有用户&#xff0c;页面下半部分是输入新用户的界面&#xff1b; 2. 每当输入新用户时&#xff0c;页面上半部分会动态更新新加用户的内容&#xff1b; 我们还是用ajax实现&#xff0c;不过这次用rails内部对ajax的支持…

C++11中std::async的使用

C11中的std::async是个模板函数。std::async异步调用函数&#xff0c;在某个时候以Args作为参数(可变长参数)调用Fn&#xff0c;无需等待Fn执行完成就可返回&#xff0c;返回结果是个std::future对象。Fn返回的值可通过std::future对象的get成员函数获取。一旦完成Fn的执行&…

BAT数据披露:缺人!110万AI人才缺口,两者矛盾,凉凉了!

人工智能到底有多火&#xff1f;近日国内首份《BAT人工智能领域人才发展报告》新鲜出炉&#xff0c;此次报告是针对国内人工智能领域的人才争夺情况进行了梳理。并把研究对象锁定在BAT三大巨头的身上。来源&#xff1a;《BAT人工智能领域人才发展报告》其中得出最为核心的结论&…

swift3.0最新拨打电话方法

let alertVC : UIAlertController UIAlertController.init(title: "是否拨打报警电话:10086", message: "", preferredStyle: .alert) let falseAA : UIAlertAction UIAlertAction.init(title: "取消", style: .cancel, handler: nil) let tr…

关于手机已处理里重复单据的处理办法

更新视图 VWFE_TASK去掉 union TWFE_TASK_BAK 的部分&#xff0c;原因是因为后面做了流程预演导致的问题转载于:https://blog.51cto.com/iderun/1602828

swiftswift3.0自己封装的快速构建页面的方法

//#param mark 控件 func creatLabel(frame:CGRect,text:String,textColor:UIColor,textFont:CGFloat,textAlignment:NSTextAlignment) -> UILabel { let label UILabel.init(frame: frame) label.text text label.textColor textColor label.font UIFont.systemFont(of…

Google是如何做Code Review的?| CSDN原力计划

作者 | 帅昕 xindoo 编辑 | 屠敏出品 | CSDN 博客我和几个小伙伴一起翻译了Google前一段时间放出来的Google’s Engineering Practices documentation&#xff08;https://github.com/google/eng-practices&#xff09;&#xff0c;翻译后的GitHub仓库&#xff1a;https://gith…

从FFmpeg 4. 2源码中提取dshow mjpeg code步骤

之前在https://blog.csdn.net/fengbingchun/article/details/103735560 中介绍过在Windows上通过vs2017编译FFmpeg源码进行单步调试的步骤&#xff0c;为了进一步熟悉FFmpeg这里以提取FFmpeg dshow mjpeg源码为例介绍其实现过程及注意事项&#xff1a; FFmpeg是用C实现的&…

ControlButton按钮事件

#ifndef __HControlButton_H__#define __HControlButton_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT; //用于标识当前按钮的状态typedef enum{ touch_begin, touch_down, touch_up,}tagForTouch;class HControlB…

swift3.0UIAlertController使用方法

let alertVC : UIAlertController UIAlertController.init(title: "添加照片", message: "", preferredStyle: .actionSheet) let cleanAction UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel,handler:nil) let photoActi…

Doxygen使用介绍

Doxygen的主页为http://doxygen.nl/&#xff0c;它的license为GPL&#xff0c;最新发布版本为1.8.17&#xff0c;源代码存放在https://github.com/doxygen/doxygen&#xff0c;它支持的语言包括C、C、Objective-C、C#、Java、Python等&#xff0c;它支持的系统平台包括Winodws、…

云计算软件生态圈:摸到一把大牌

作者 | 老姜编辑 | 阿秃出品 | CSDN云计算&#xff08;ID&#xff1a;CSDNcloud&#xff09;“我觉得我摸着了一把大牌。”软件领域的新锐企业——有赞公司创始人兼CEO白鸦在转向SaaS领域的一个细分市场时&#xff0c;曾对天使投资人这样说。而老牌软件企业金蝶创始人徐少春在2…