forked from xbmc/xbmc
/
EncoderFFmpeg.cpp
278 lines (238 loc) · 7.95 KB
/
EncoderFFmpeg.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
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
/*
* Copyright (C) 2005-2012 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#ifdef _LINUX
#include "stdint.h"
#else
#define INT64_C __int64
#endif
#include "EncoderFFmpeg.h"
#include "filesystem/File.h"
#include "utils/log.h"
#include "settings/GUISettings.h"
#include "utils/URIUtils.h"
/* AV_PKT_FLAG_KEY was named PKT_FLAG_KEY in older versions of libavcodec */
#ifndef AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY PKT_FLAG_KEY
#endif
CEncoderFFmpeg::CEncoderFFmpeg():
m_Format (NULL),
m_CodecCtx (NULL),
m_Stream (NULL),
m_Buffer (NULL),
m_BufferSize(0)
{
}
bool CEncoderFFmpeg::Init(const char* strFile, int iInChannels, int iInRate, int iInBits)
{
if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllAvFormat.Load()) return false;
m_dllAvFormat.av_register_all();
m_dllAvCodec.avcodec_register_all();
CStdString filename = URIUtils::GetFileName(strFile);
AVOutputFormat *fmt = NULL;
fmt = m_dllAvFormat.av_guess_format(NULL, filename.c_str(), NULL);
if (!fmt)
{
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Unable to guess the output format for the file %s", filename.c_str());
return false;
}
AVCodec *codec;
codec = m_dllAvCodec.avcodec_find_encoder(
strcmp(fmt->name, "ogg") == 0 ? CODEC_ID_VORBIS : fmt->audio_codec
);
if (!codec)
{
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Unable to find a suitable FFmpeg encoder");
return false;
}
m_Format = m_dllAvFormat.avformat_alloc_context();
m_Format->pb = m_dllAvFormat.avio_alloc_context(m_BCBuffer, sizeof(m_BCBuffer), AVIO_FLAG_READ, this, NULL, MuxerReadPacket, NULL);
if (!m_Format->pb)
{
m_dllAvUtil.av_freep(&m_Format);
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Failed to allocate ByteIOContext");
return false;
}
m_Format->oformat = fmt;
m_Format->bit_rate = g_guiSettings.GetInt("audiocds.bitrate") * 1000;
/* add a stream to it */
m_Stream = m_dllAvFormat.avformat_new_stream(m_Format, codec);
if (!m_Stream)
{
m_dllAvUtil.av_freep(&m_Format->pb);
m_dllAvUtil.av_freep(&m_Format);
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Failed to allocate AVStream context");
return false;
}
/* set the stream's parameters */
m_CodecCtx = m_Stream->codec;
m_CodecCtx->codec_id = codec->id;
m_CodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
m_CodecCtx->bit_rate = m_Format->bit_rate;
m_CodecCtx->sample_rate = iInRate;
m_CodecCtx->channels = iInChannels;
m_CodecCtx->channel_layout = m_dllAvUtil.av_get_default_channel_layout(iInChannels);
m_CodecCtx->time_base = (AVRational){1, iInRate};
if(fmt->flags & AVFMT_GLOBALHEADER)
{
m_CodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
m_Format->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
m_dllAvCodec.av_init_packet(&m_Pkt);
m_Pkt.stream_index = m_Stream->index;
m_Pkt.flags |= AV_PKT_FLAG_KEY;
switch(iInBits)
{
case 8: m_CodecCtx->sample_fmt = AV_SAMPLE_FMT_U8 ; break;
case 16: m_CodecCtx->sample_fmt = AV_SAMPLE_FMT_S16; break;
case 32: m_CodecCtx->sample_fmt = AV_SAMPLE_FMT_S32; break;
default:
m_dllAvUtil.av_freep(&m_Stream);
m_dllAvUtil.av_freep(&m_Format->pb);
m_dllAvUtil.av_freep(&m_Format);
return false;
}
if (m_dllAvCodec.avcodec_open2(m_CodecCtx, codec, NULL))
{
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Failed to open the codec");
m_dllAvUtil.av_freep(&m_Stream);
m_dllAvUtil.av_freep(&m_Format->pb);
m_dllAvUtil.av_freep(&m_Format);
return false;
}
/* calculate how many bytes we need per frame */
m_NeededFrames = m_CodecCtx->frame_size;
m_NeededBytes = m_NeededFrames * iInChannels * (iInBits / 8);
m_Buffer = new uint8_t[m_NeededBytes];
m_BufferSize = 0;
/* set input stream information and open the file */
if (!CEncoder::Init(strFile, iInChannels, iInRate, iInBits))
{
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Failed to call CEncoder::Init");
delete[] m_Buffer;
m_dllAvUtil.av_freep(&m_Stream);
m_dllAvUtil.av_freep(&m_Format->pb);
m_dllAvUtil.av_freep(&m_Format);
return false;
}
/* set the tags */
SetTag("album" , m_strAlbum);
SetTag("album_artist", m_strArtist);
SetTag("genre" , m_strGenre);
SetTag("title" , m_strTitle);
SetTag("track" , m_strTrack);
SetTag("encoder" , "XBMC FFmpeg Encoder");
/* write the header */
if (m_dllAvFormat.avformat_write_header(m_Format, NULL) != 0)
{
CLog::Log(LOGERROR, "CEncoderFFmpeg::Init - Failed to write the header");
delete[] m_Buffer;
m_dllAvUtil.av_freep(&m_Stream);
m_dllAvUtil.av_freep(&m_Format->pb);
m_dllAvUtil.av_freep(&m_Format);
return false;
}
return true;
}
void CEncoderFFmpeg::SetTag(const CStdString tag, const CStdString value)
{
m_dllAvUtil.av_dict_set(&m_Format->metadata, tag.c_str(), value.c_str(), 0);
}
int CEncoderFFmpeg::MuxerReadPacket(void *opaque, uint8_t *buf, int buf_size)
{
CEncoderFFmpeg *enc = (CEncoderFFmpeg*)opaque;
if(enc->WriteStream(buf, buf_size) != buf_size)
{
CLog::Log(LOGERROR, "Error writing FFmpeg buffer to file");
return -1;
}
return buf_size;
}
int CEncoderFFmpeg::Encode(int nNumBytesRead, BYTE* pbtStream)
{
while(nNumBytesRead > 0)
{
unsigned int space = m_NeededBytes - m_BufferSize;
unsigned int copy = (unsigned int)nNumBytesRead > space ? space : nNumBytesRead;
memcpy(&m_Buffer[m_BufferSize], pbtStream, space);
m_BufferSize += copy;
pbtStream += copy;
nNumBytesRead -= copy;
/* only write full packets */
if (m_BufferSize == m_NeededBytes)
if (!WriteFrame()) return 0;
}
return 1;
}
bool CEncoderFFmpeg::WriteFrame()
{
uint8_t outbuf[FF_MIN_BUFFER_SIZE];
int encoded;
encoded = m_dllAvCodec.avcodec_encode_audio(m_CodecCtx, outbuf, FF_MIN_BUFFER_SIZE, (short*)m_Buffer);
m_BufferSize = 0;
if (encoded < 0) {
CLog::Log(LOGERROR, "CEncoderFFmpeg::WriteFrame - Error encoding audio");
return false;
}
m_Pkt.data = outbuf;
m_Pkt.size = encoded;
if (m_CodecCtx->coded_frame && (uint64_t)m_CodecCtx->coded_frame->pts != AV_NOPTS_VALUE)
m_Pkt.pts = m_dllAvUtil.av_rescale_q(m_CodecCtx->coded_frame->pts, m_Stream->time_base, m_CodecCtx->time_base);
if (m_dllAvFormat.av_write_frame(m_Format, &m_Pkt) < 0) {
CLog::Log(LOGERROR, "CEncoderFFMmpeg::WriteFrame - Failed to write the frame data");
return false;
}
return true;
}
bool CEncoderFFmpeg::Close()
{
if (m_Format) {
/* if there is anything still in the buffer */
if (m_BufferSize > 0) {
/* zero the unused space so we dont encode random junk */
memset(&m_Buffer[m_BufferSize], 0, m_NeededBytes - m_BufferSize);
/* write any remaining data */
WriteFrame();
}
/* write the eof flag */
delete[] m_Buffer;
m_Buffer = NULL;
WriteFrame();
/* write the trailer */
m_dllAvFormat.av_write_trailer(m_Format);
FlushStream();
FileClose();
/* cleanup */
m_dllAvCodec.avcodec_close(m_CodecCtx);
m_dllAvUtil.av_freep(&m_Stream );
m_dllAvUtil.av_freep(&m_Format->pb);
m_dllAvUtil.av_freep(&m_Format );
}
m_BufferSize = 0;
m_dllAvFormat.Unload();
m_dllAvUtil .Unload();
m_dllAvCodec .Unload();
return true;
}