#include
#include
#include
using namespace std;
extern "C"
{
#include
#include
}
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
int main(int argc, char* argv[])
{
string filename = "test.h264";
ifstream ifs(filename, ios::binary);
if (!ifs) return -1;
unsigned char inbuf[4096] = { 0 };
AVCodecID codec_id = AV_CODEC_ID_H264;
auto codec = avcodec_find_decoder(codec_id);
auto c = avcodec_alloc_context3(codec);
avcodec_open2(c, NULL, NULL);
auto parser = av_parser_init(codec_id);
auto pkt = av_packet_alloc();
auto frame = av_frame_alloc();
while (!ifs.eof())
{
ifs.read((char*)inbuf, sizeof(inbuf));
int data_size = ifs.gcount();
if (data_size <= 0) break;
auto data = inbuf;
while (data_size > 0)
{
int ret = av_parser_parse2(parser, c,
&pkt->data, &pkt->size,
data, data_size,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
data += ret;
data_size -= ret;
if (pkt->size)
{
ret = avcodec_send_packet(c, pkt);
if (ret < 0)
{
break;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(c, frame);
if (ret < 0)
break;
cout << frame->format << " " << flush;
}
}
}
}
av_packet_free(&pkt);
int ret = avcodec_send_packet(c, NULL);
while (ret >= 0)
{
ret = avcodec_receive_frame(c, frame);
if (ret < 0)
break;
cout << frame->format << "-" << flush;
}
av_parser_close(parser);
avcodec_free_context(&c);
av_frame_free(&frame);
av_packet_free(&pkt);
return 0;
}
- 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