FFMPEG ffmpeg + cocos2d video test

//核心

{

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void AVdio::update(float delta)
{
    auto video = (Sprite*)(this->getChildByName("video"));
    if (video != nullptr)
    {
        double      tims = fl->_pts * fl->_timeBase * 1000;
        double      elsped = _timestamp.getElapsedTimeInMilliSec();
        double      sleeps = (tims - elsped);
 
        s = sleeps + elsped;
 
        if (elsped - s > 0.0001)
        {
            is = ffReader.readFrame(*fl);
            if (is)
            {
                texture->initWithData((const unsigned char*)fl->_data, fl->_dataSize, Texture2D::PixelFormat::RGB888,
                    fl->_width, fl->_height,
                    Size(fl->_width, fl->_height));
 
                video->initWithTexture(texture);
                video->setContentSize(Size(displayex.width - 10, displayex.height - 50));
            }
            else
            {
                video->removeFromParent();
            }
        }
    }
 
}

 

}

 

.h

{

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef __AVDIO_H__
#define __AVDIO_H__
 
#include "cocos2d.h"
using namespace cocos2d;
#include <string.h>
#include <iostream>
#include "cocos/ui/CocosGUI.h"
#include "socket/Private/ThreadPool.h"
#include "Tools/Tools.h"
#include "xml/XML.h"
#include "iconv/UTF8.h"
#ifdef _WIN32
#include "ATBAudioEngine/ATBAudioEngine.h"
#endif
using namespace std;
using namespace ui;
 
 
class AVdio :public LayerColor, EditBoxDelegate
{
    Tools m_Tools;
public:
 
    static cocos2d::LayerColor* createAVdio();
 
    ~AVdio();
 
    virtual bool init();
 
    CREATE_FUNC(AVdio);
 
 
 
    void OnCallback(cocos2d::Ref* pSender);
    virtual bool onTouchBegan(Touch *touch, Event *unused_event) override;
    virtual void update(float delta);
 
    virtual void editBoxEditingDidBegin(EditBox* editBox)override;
    CC_DEPRECATED_ATTRIBUTE virtual void editBoxEditingDidEnd(EditBox* editBox)override;
    virtual void editBoxTextChanged(EditBox* editBox, const std::string& text)override;
    virtual void editBoxReturn(EditBox* editBox)override;
    virtual void editBoxEditingDidEndWithAction(EditBox* editBox, EditBoxDelegate::EditBoxEndAction action)override;
private:
    void initVideoStream(string filename);
    void initInput();
};
 
 
 
 
 
 
 
 
 
extern "C"
{
#include <libavutil/imgutils.h>
#include <libavutil/parseutils.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
#include <libavdevice/avdevice.h>
}
 
struct  FrameInfor
{
    void*   _data;
    int     _dataSize;
    int     _width;
    int     _height;
    int64_t _pts;
    double  _timeBase;
 
};
class   FFVideoReader
{
public:
    AVFormatContext*_formatCtx;
    int             _videoIndex;
    AVCodecContext* _codecCtx;
    AVCodec*        _codec;
    AVFrame*        _frame;
    AVFrame*        _frameRGB;
    SwsContext*     _convertCtx;
public:
    int             _screenW;
    int             _screenH;
 
    int             _imageSize;
public:
    FFVideoReader()
    {
        _formatCtx = 0;
        _videoIndex = -1;
        _codecCtx = 0;
        _codec = 0;
        _frame = 0;
        _frameRGB = 0;
        _convertCtx = 0;
        _screenW = 0;
        _screenH = 0;
 
    }
 
    ~FFVideoReader()
    {
        sws_freeContext(_convertCtx);
        av_free(_frameRGB);
        av_free(_frame);
        avcodec_close(_codecCtx);
        avformat_close_input(&_formatCtx);
    }
 
    void    setup()
    {
        av_register_all();
        _formatCtx = avformat_alloc_context();
    }
    int     load(const char* filepath = "11.flv")
    {
        int     ret = 0;
 
        //! 打开文件
        if (avformat_open_input(&_formatCtx, filepath, NULL, NULL) != 0)
        {
            return -1;
        }
        //! 检测文件中是否存在数据流
        if (avformat_find_stream_info(_formatCtx, NULL) < 0)
        {
            printf("检测文件中是否存在数据流");
            return -1;
        }
        //! 获取视频流索引
        _videoIndex = -1;
        for (int i = 0; i < _formatCtx->nb_streams; i++)
        {
            if (_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            {
                _videoIndex = i;
                break;
            }
        }
        /**
        *   没有视频流,则返回
        */
        if (_videoIndex == -1)
        {
            return -1;
        }
        _codecCtx = _formatCtx->streams[_videoIndex]->codec;
 
        double dur = _formatCtx->duration / double(AV_TIME_BASE);
        _codec = avcodec_find_decoder(_codecCtx->codec_id);
        if (_codec == NULL)
        {
            printf("find decoder faild !!\n");
            return -1;
        }
        /**
        *   打开解码器
        */
        if (avcodec_open2(_codecCtx, _codec, NULL) < 0)
        {
            return -1;
        }
        _frame = av_frame_alloc();
        _frameRGB = av_frame_alloc();
 
        _screenW = _codecCtx->width;
        _screenH = _codecCtx->height;
 
        _convertCtx = sws_getContext(
            _codecCtx->width
            , _codecCtx->height
            , _codecCtx->pix_fmt
            , _codecCtx->width
            , _codecCtx->height
            , AV_PIX_FMT_RGB24
            , SWS_BICUBIC
            , NULL
            , NULL
            , NULL
        );
 
        int     numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, _codecCtx->width, _codecCtx->height);
        uint8_t*buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
        avpicture_fill((AVPicture *)_frameRGB, buffer, AV_PIX_FMT_RGB24, _codecCtx->width, _codecCtx->height);
        _imageSize = numBytes;
        return  0;
    }
 
    bool    readFrame(FrameInfor& infor)
    {
        AVPacket packet;
        av_init_packet(&packet);
        for (;;)
        {
            if (av_read_frame(_formatCtx, &packet))
            {
                av_free_packet(&packet);
                return false;
            }
            if (packet.stream_index != _videoIndex)
            {
                continue;
            }
            int frame_finished = 0;
 
            int res = avcodec_decode_video2(_codecCtx, _frame, &frame_finished, &packet);
 
            if (frame_finished)
            {
                AVStream*   streams = _formatCtx->streams[_videoIndex];
                double      tmbase = av_q2d(streams->time_base);
                int64_t     pts = _frame->pts;
 
                char        buf[128];
                sprintf(buf, "pts = %I64d     dts =  %I64d\n", packet.pts, packet.dts);
                int res = sws_scale(
                    _convertCtx
                    , (const uint8_t* const*)_frame->data
                    , _frame->linesize
                    , 0
                    , _codecCtx->height
                    , _frameRGB->data
                    , _frameRGB->linesize
                );
                av_packet_unref(&packet);
 
                infor._data = _frameRGB->data[0];
                infor._dataSize = _imageSize;
                infor._width = _screenW;
                infor._height = _screenH;
                infor._pts = _frame->pts;
                infor._timeBase = av_q2d(streams->time_base);
 
                return  true;
            }
        }
        return  false;
    }
    void*   readFrame()
    {
        AVPacket packet;
        av_init_packet(&packet);
        for (;;)
        {
            if (av_read_frame(_formatCtx, &packet))
            {
                av_free_packet(&packet);
                return 0;
            }
            if (packet.stream_index != _videoIndex)
            {
                continue;
            }
            int frame_finished = 0;
 
            int res = avcodec_decode_video2(_codecCtx, _frame, &frame_finished, &packet);
 
            if (frame_finished)
            {
                AVStream*   streams = _formatCtx->streams[_videoIndex];
                double      tmbase = av_q2d(streams->time_base);
                int64_t     pts = _frame->pts;
 
                char        buf[128];
                sprintf(buf, "pts = %I64d     dts =  %I64d\n", packet.pts, packet.dts);
                int res = sws_scale(
                    _convertCtx
                    , (const uint8_t* const*)_frame->data
                    , _frame->linesize
                    , 0
                    , _codecCtx->height
                    , _frameRGB->data
                    , _frameRGB->linesize
                );
                av_packet_unref(&packet);
 
                return  _frameRGB->data[0];
            }
        }
        return  0;
    }
};
 
 
 
#include <windows.h>
 
class Timestamp
{
public:
    Timestamp()
    {
        QueryPerformanceFrequency(&_frequency);
        QueryPerformanceCounter(&_startCount);
    }
    ~Timestamp()
    {}
 
    void    update()
    {
        QueryPerformanceCounter(&_startCount);
    }
    /**
    *   获取当前秒
    */
    double getElapsedSecond()
    {
        return  getElapsedTimeInMicroSec() * 0.000001;
    }
    /**
    *   获取毫秒
    */
    double getElapsedTimeInMilliSec()
    {
        return this->getElapsedTimeInMicroSec() * 0.001;
    }
    /**
    *   获取微妙
    */
    double getElapsedTimeInMicroSec()
    {
        LARGE_INTEGER endCount;
        QueryPerformanceCounter(&endCount);
 
        double  startTimeInMicroSec = _startCount.QuadPart * (1000000.0 / _frequency.QuadPart);
        double  endTimeInMicroSec = endCount.QuadPart * (1000000.0 / _frequency.QuadPart);
 
        return  endTimeInMicroSec - startTimeInMicroSec;
    }
protected:
    LARGE_INTEGER   _frequency;
    LARGE_INTEGER   _startCount;
};
 
 
 
 
 
#endif

 

}

 

.cpp

{

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "AVdio.h"
 
#ifndef DISPLAY
#define DISPLAY Director::getInstance()->getVisibleSize()
#endif
#ifndef displayex
#define displayex Director::getInstance()->getVisibleSize()
#endif
 
 
FFVideoReader   ffReader;
FrameInfor *fl = new (std::nothrow) FrameInfor;
auto texture = new (std::nothrow) Texture2D();
bool is = false;
Timestamp       _timestamp;
double s = 0;
 
 
cocos2d::LayerColor* AVdio::createAVdio()
{
    auto LC = AVdio::create();
    if (LC)
    {
        return LC;
    }
    else
    {
        return nullptr;
    }
}
 
AVdio::~AVdio()
{
 
}
 
void AVdio::update(float delta)
{
    auto video = (Sprite*)(this->getChildByName("video"));
    if (video != nullptr)
    {
        double      tims = fl->_pts * fl->_timeBase * 1000;
        double      elsped = _timestamp.getElapsedTimeInMilliSec();
        double      sleeps = (tims - elsped);
 
        s = sleeps + elsped;
 
        if (elsped - s > 0.0001)
        {
            is = ffReader.readFrame(*fl);
            if (is)
            {
                texture->initWithData((const unsigned char*)fl->_data, fl->_dataSize, Texture2D::PixelFormat::RGB888,
                    fl->_width, fl->_height,
                    Size(fl->_width, fl->_height));
 
                video->initWithTexture(texture);
                video->setContentSize(Size(displayex.width - 10, displayex.height - 50));
            }
            else
            {
                video->removeFromParent();
            }
        }
    }
 
}
 
bool AVdio::init()
{
    if (!LayerColor::init())
    {
        return false;
    }
 
    auto display = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
 
    this->setTouchEnabled(true);
    auto ELTOBO = EventListenerTouchOneByOne::create();
    ELTOBO->setSwallowTouches(true);
    ELTOBO->onTouchBegan = std::move(std::bind(&AVdio::onTouchBegan, this, std::placeholders::_1, std::placeholders::_2));
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(ELTOBO, this);
 
 
 
 
    auto background = cocos2d::LayerColor::create(cocos2d::Color4B(0, 0, 0, 255));
    this->addChild(background);
 
    //close button
    string file("res/Button.png");
    auto btn = cocos2d::ui::Button::create(file, file, file);
    btn->setColor(Color3B(24, 48, 64));
    btn->setPressedActionEnabled(true);
    btn->setScale9Enabled(true);
    btn->setContentSize(Size(100, 50));
    btn->setPosition(Vec2(displayex.width - btn->getContentSize().width / 2,
        displayex.height - btn->getContentSize().height / 2));
    btn->setTitleColor(Color3B::RED);
    btn->setTitleFontSize(50);
    btn->setName("X");
    btn->setTitleText("X");
    btn->addClickEventListener(std::bind(&AVdio::OnCallback, this, std::placeholders::_1));
    this->addChild(btn);
 
 
    //this->initVideoStream();
 
    this->initInput();
}
 
void AVdio::initInput()
{
    auto edit = ui::EditBox::create(Size(800, 50), "res/input.png");
    edit->setPosition(Vec2(0, displayex.height - edit->getContentSize().height / 2));
    edit->setAnchorPoint(Vec2(0.0f, 0.5f));
    edit->setName("videoPath");
    edit->setFontColor(Color4B::YELLOW);
    edit->setDelegate(this);
    this->addChild(edit);
 
    string file("res/Button.png");
    auto btn = cocos2d::ui::Button::create(file, file, file);
    btn->setColor(Color3B(24, 48, 64));
    btn->setPressedActionEnabled(true);
    btn->setScale9Enabled(true);
    btn->setContentSize(Size(150, 50));
    btn->setPosition(Vec2(displayex.width - (displayex.width - edit->getContentSize().width) + btn->getContentSize().width / 2,
        edit->getPosition().y));
    btn->setTitleColor(Color3B::RED);
    btn->setTitleFontSize(50);
    btn->setName("PLAY");
    btn->setTitleText("PLAY");
    btn->addClickEventListener(std::bind(&AVdio::OnCallback, this, std::placeholders::_1));
    this->addChild(btn);
}
 
void AVdio::initVideoStream(string filename)
{
    this->unscheduleUpdate();
 
    _timestamp.update();
     
     
 
    ATBAE::GetInstance()->PauseAllMusicAndEffects();
    auto video = (Sprite*)(this->getChildByName("video"));
    if (video != nullptr)
    {
        video->removeFromParent();
    }
 
    s = 0;
 
    auto display = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
 
    auto ret = avdevice_version();
    std::cout << ret << std::endl;
 
    ffReader.~FFVideoReader();
    ffReader.setup();
    ffReader.load(filename.c_str());
 
    ATBAE::GetInstance()->LoadMusicsAndPlay(filename.c_str());
 
 
    is = ffReader.readFrame(*fl);
 
    texture->initWithData((const unsigned char*)fl->_data, fl->_dataSize, Texture2D::PixelFormat::RGB888,
        fl->_width - 1, fl->_height - 1,
        Size(500, 500));
 
    auto s = Sprite::create("06a03.jpg");
    s->initWithTexture(texture);
    s->setName("video");
    s->setPosition(Vec2(display.width / 2, display.height / 2 - 25));
    this->addChild(s);
 
    this->scheduleUpdate();
}
 
 
void AVdio::OnCallback(cocos2d::Ref* pSender)
{
    string name = ((Node*)(pSender))->getName();
    if (name == "X")
    {
        auto action = Sequence::create(MoveTo::create(0.2f, Vec3(-(displayex.width), 0, 0)),
            DelayTime::create(0.1f),
            CallFunc::create([=]()
        {
            this->removeFromParent();
            ATBAE::GetInstance()->PauseAllMusicAndEffects();
        }), nullptr);
        this->runAction(action);
    }
    else if (name == "PLAY")
    {
        auto edit = (ui::EditBox*)this->getChildByName("videoPath");
        string path = edit->getText();
        if (path.length() > 0)
        {
            this->initVideoStream(path);
        }
         
    }
}
bool AVdio::onTouchBegan(Touch *touch, Event *unused_event)
{
    return true;
}
 
 
void AVdio::editBoxEditingDidBegin(EditBox* editBox)
{
 
}
void AVdio::editBoxEditingDidEnd(EditBox* editBox)
{
 
}
void AVdio::editBoxTextChanged(EditBox* editBox, const std::string& text)
{
 
}
void AVdio::editBoxReturn(EditBox* editBox)
{
 
}
void AVdio::editBoxEditingDidEndWithAction(EditBox* editBox, EditBoxDelegate::EditBoxEndAction action)
{
 
}

 

}

posted @   YZFHKMS-X  阅读(628)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示