-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFFmpegCodecCtx.cpp
More file actions
48 lines (36 loc) · 1.14 KB
/
Copy pathFFmpegCodecCtx.cpp
File metadata and controls
48 lines (36 loc) · 1.14 KB
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
#include "FFmpegCodecCtx.hpp"
#define STR_DECODER_THREADS_NUM "2"
bool FFmpegCodecCtx::Open(AVCodecID codec_id)
{
_codec_ctx.reset(avcodec_alloc_context3(avcodec_find_decoder(codec_id)), [](AVCodecContext *ptr){
avcodec_free_context(&ptr);
});
using DictionaryRaiiHelper = std::unique_ptr<AVDictionary *, decltype(&av_dict_free)>;
AVDictionary *dict {nullptr};
DictionaryRaiiHelper helper{&dict, av_dict_free};
if (av_dict_set(&dict, "threads", STR_DECODER_THREADS_NUM, 0) < 0) {
return false;
}
return avcodec_open2(_codec_ctx.get(), _codec_ctx->codec, &dict) == 0;
}
FFmpegFrame::Ptr FFmpegCodecCtx::Decode(FFmpegPacket::Ptr &packet)
{
auto ret = avcodec_send_packet(_codec_ctx.get(), packet->get());
if (ret < 0)
{
return nullptr;
}
auto out_frame = std::make_shared<FFmpegFrame>();
ret = avcodec_receive_frame(_codec_ctx.get(), out_frame->get());
if (ret != 0)
{
char buff[512];
av_make_error_string(buff, 512, ret);
return nullptr;
}
return out_frame;
}
AVCodecContext *FFmpegCodecCtx::get()
{
return _codec_ctx.get();
}