-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKDemuxer.cpp
133 lines (114 loc) · 2.68 KB
/
KDemuxer.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
#include "KDemuxer.h"
#include "stdio.h"
KDemuxer::KDemuxer() :
mInputFileName(NULL),
mVideoOutputFileName(NULL),
mAudioOutputFileName(NULL),
pFileIn(NULL),
pVideoFileOut(NULL),
pAudioFileOut(NULL),
mFmtCxt(NULL),
mSt(NULL),
mCodecCtx(NULL),
video_index(-1),
audio_index(-1)
{
printf("new KDemuxer: %p\n", this);
}
KDemuxer::~KDemuxer()
{
printf("delete KDemuxer: %p\n", this);
// if (!pFileIn)
// {
// fclose(pFileIn);
// pFileIn = NULL;
// }
if (!pVideoFileOut)
{
fclose(pVideoFileOut);
pVideoFileOut = NULL;
}
if (!pAudioFileOut)
{
fclose(pAudioFileOut);
pAudioFileOut = NULL;
}
}
KErrors KDemuxer::configure(InputParameter *pParameter)
{
mInputFileName = pParameter->mInputFileName;
mVideoOutputFileName = pParameter->mVideoOutputFileName;
mAudioOutputFileName = pParameter->mAudioOutputFileName;
// pFileIn = fopen(mInputFileName, "rb+");
// if (!pFileIn)
// {
// return ERROR_CAN_NOT_OPEN_INPUT_FILE;
// }
pVideoFileOut = fopen(mVideoOutputFileName, "wb+");
if (!pVideoFileOut)
{
return ERROR_CAN_NOT_OPEN_OUTPUT_FILE;
}
pAudioFileOut = fopen(mAudioOutputFileName, "wb+");
if (!pAudioFileOut)
{
return ERROR_CAN_NOT_OPEN_OUTPUT_FILE;
}
av_register_all();
if (avformat_open_input(&mFmtCxt, mInputFileName, NULL, NULL) < 0)
{
return ERROR_FF_AVFORMAT_OPEN_INPUT;
}
if (avformat_find_stream_info(mFmtCxt, NULL) < 0)
{
return ERROR_FF_FIND_STREAM_INFO;
}
video_index = av_find_best_stream(mFmtCxt, (AVMediaType) AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (video_index >= 0)
{
mSt = mFmtCxt->streams[video_index];
mCodecCtx = mSt->codec;
printf("vcodec_id: 0x%x, AV_CODEC_ID_H264: 0x%x\n", mCodecCtx->codec_id, AV_CODEC_ID_H264);
printf("w: %d, h: %d\n", mCodecCtx->width, mCodecCtx->height);
printf("pix_fmt: %d, AV_PIX_FMT_YUV420P: %d\n", mCodecCtx->pix_fmt, AV_PIX_FMT_YUV420P);
}
audio_index = av_find_best_stream(mFmtCxt, (AVMediaType) AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (audio_index >= 0)
{
mSt = mFmtCxt->streams[audio_index];
mCodecCtx = mSt->codec;
printf("acodec_id: 0x%x\n", mCodecCtx->codec_id);
}
av_dump_format(mFmtCxt, 0, mInputFileName, 0);
return NO_ERROR;
}
KErrors KDemuxer::start()
{
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
while (av_read_frame(mFmtCxt, &packet) >= 0)
{
if (packet.stream_index == video_index)
{
if (pVideoFileOut)
{
fwrite(packet.data, 1, packet.size, pVideoFileOut);
}
} else if (packet.stream_index == audio_index)
{
if (pAudioFileOut)
{
fwrite(packet.data, 1, packet.size, pAudioFileOut);
}
}
}
if (pVideoFileOut)
{
fflush(pVideoFileOut);
}
if (pAudioFileOut)
{
fflush(pAudioFileOut);
}
}