-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMp4WriterLinux.cpp
444 lines (400 loc) · 13.4 KB
/
Mp4WriterLinux.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//
// Copyright (c) 2015, Pleora Technologies Inc., All rights reserved.
//
// mp4 file recording (of just video) using libav involves doing these:
// -setting up the codec (h264 or mpeg4) and the parameters (bitrate, size, threads, quality, etc)
// -getting 'extra data' from the codec that is to be included in mp4 file header in avcc format
// in case of h264 this is the SPS/PPS written in a particular format (called avcc)
// the codec does not give the extra data in avcc format, libav converts the SPS/PPS to avcc
// -setting up the format context with the appropriate codec for mp4 file saving
// -opening the mp4 file
// -encoding incoming I420 images into h264/mpeg4 and writing the results into the file
// taking note to set timestamps appropriate relative to the set fps
// fps of the file is set once but the timestamp values can vary the playing time
// -when the file is finished (taking care of flushing the encoder), a trailer must be written
// that contains moov atom and other atoms in mp4 that are tables for seeking and finding frames
// -it is also important to know that while the encoder may be writing the frames in "network format"
// or annexb format (h264), they must be written with avc format into the file. This is currently handled
// automatically by libav however
// *****************************************************************************
#include "eBUSPlayerShared.h"
#include "Mp4WriterLinux.h"
#include <PvSampleUtils.h>
#include <PvString.h>
#include <PvImage.h>
#include <iostream>
#ifdef PV_ENABLE_MP4
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/opt.h"
#include "libswscale/swscale.h"
};
const uint32_t DEFAULT_VIDEO_BIT_RATE = 2000000;
const uint32_t VIDEO_FPS = 10;
const PvPixelType DST_PIXEL_TYPE = PvPixelBGRa8;
///
/// \brief Constructor
///
Mp4WriterLinux::Mp4WriterLinux()
:mI420Converter( NULL )
,mCodecEncode( NULL )
,mCtxEncode( NULL )
,mPictureEncoded( NULL )
,mFormatContext( NULL )
,mInitialized( false )
,mStartTm( 0 )
,mVideoTimestamp( 0 )
,mWidth( 0 )
,mHeight( 0 )
,mAvgBitrate( DEFAULT_VIDEO_BIT_RATE )
,mRecordFps( VIDEO_FPS )
{
//these two functions need to be called once per process and are not thread safe
//if different threads create their own Mp4Writer instance, it may cause an issue
av_log_set_level( AV_LOG_ERROR );
av_register_all();
mI420Data[ 0 ] = NULL;
mI420Data[ 1 ] = NULL;
mI420Data[ 2 ] = NULL;
mI420Stride[ 0 ] = 0;
mI420Stride[ 1 ] = 0;
mI420Stride[ 2 ] = 0;
}
///
/// \brief Destructor
///
Mp4WriterLinux::~Mp4WriterLinux()
{
Close();
}
///
/// \brief Open the MP4, prepares for receiving frames
///
bool Mp4WriterLinux::Open( const std::string &aFilename, PvImage *aImage )
{
if ( !IsAvailable() )
{
return false;
}
if ( mInitialized || mWidth || mHeight )
{
//an already open encoder should be closed first
return false;
}
MutexHolder m( &mMutex );
//////////////////////////////////
mWidth = aImage->GetWidth();
mHeight = aImage->GetHeight();
//YUV420planer output data
mI420Data[ 0 ]= (uint8_t*) malloc( mWidth * mHeight ); //Y plane
mI420Data[ 1 ]= (uint8_t*) malloc( mWidth * mHeight / 4 ); //U plane
mI420Data[ 2 ]= (uint8_t*) malloc( mWidth * mHeight / 4 ); //V plane
mI420Stride[ 0 ] = mWidth;
mI420Stride[ 1 ] = mWidth / 2;
mI420Stride[ 2 ] = mWidth / 2;
//use swscale for BGRa (32bit) to YUV420 planar conversion
//assumption is that incoming format will always be BGRa
mI420Converter = sws_getContext(
mWidth, mHeight, PIX_FMT_BGR32,
mWidth, mHeight, PIX_FMT_YUV420P,
SWS_FAST_BILINEAR, NULL, NULL, NULL );
if ( mI420Converter == NULL )
{
return false;
}
//todo: experiment and allow for mpeg4 (CODEC_ID_MPEG4) recording
AVCodecID lCodecId = AV_CODEC_ID_H264;
mCodecEncode = avcodec_find_encoder( lCodecId );
if ( mCodecEncode == NULL )
{
return false;
}
mCtxEncode = avcodec_alloc_context3( mCodecEncode );
if ( mCtxEncode != NULL )
{
//These are encoding parameters and determine the speed, quality and bitrate of the produced video
//the encoder will encode assuming frame rate of 30fps. If incoming fps is different, then
//bitrate will be different (eg if incoming fps is 10fps, then bitrate will be mAvgBitrate/3 instead of mAvgBitrate)
mCtxEncode->width = mWidth;
mCtxEncode->height = mHeight;
mCtxEncode->time_base.num = 1;
mCtxEncode->time_base.den = mRecordFps;
mCtxEncode->pix_fmt = PIX_FMT_YUV420P;
mCtxEncode->max_b_frames = 0;
mCtxEncode->has_b_frames = 0;
//todo: bitrate should be adapted to the size of image
mCtxEncode->bit_rate = (int)( mAvgBitrate * 0.80f );
mCtxEncode->bit_rate_tolerance = (int) ( mAvgBitrate * 0.20f );
//It is important to set this flag so that headers (SPS and PPS for h264) are written into
//the extra bytes on open and not for every IFrame
mCtxEncode->flags |= CODEC_FLAG_GLOBAL_HEADER;
//one Iframe per 30 frames (or once a second if at 30fps)
//if incoming fps is different than 30fps, IFrame period will vary
mCtxEncode->gop_size = mRecordFps * 3;
mCtxEncode->keyint_min = mRecordFps;
}
else
{
return false;
}
//If CODEC_FLAG_GLOBAL_HEADER flag was set in open, the headers (SPS PPS of h264) are written
//into the extra data of mCtxEncode which is needed to be written as header of the mp4 file
// open codec for h264 encoder
if ( avcodec_open2( mCtxEncode, mCodecEncode, NULL ) < 0 )
{
return false;
}
// alloc image and output buffer for encoder
mPictureEncoded = avcodec_alloc_frame();
if ( mPictureEncoded == NULL )
{
return false;
}
AVOutputFormat *lOutputFormat = NULL;
// Create container
lOutputFormat = av_guess_format( 0, aFilename.c_str(), 0 );
if ( lOutputFormat == NULL )
{
return false;
}
mFormatContext = avformat_alloc_context();
if ( mFormatContext == NULL )
{
return false;
}
mFormatContext->oformat = lOutputFormat;
strcpy( mFormatContext->filename, aFilename.c_str() );
// Add video stream
AVStream *pst = avformat_new_stream( mFormatContext, NULL );
if ( pst != NULL && pst->codec != NULL )
{
pst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
pst->codec->pix_fmt = AV_PIX_FMT_NV12;
pst->codec->codec_id = lCodecId;
pst->codec->bit_rate = mAvgBitrate;
pst->codec->width = mWidth;
pst->codec->height = mHeight;
pst->codec->time_base.num = 1;
pst->codec->time_base.den = mRecordFps;
pst->codec->thread_count = 1;
// setup extradata to be those written by codec into mCtxEncode
// this should be SPS/PPS of h264
pst->codec->extradata_size = mCtxEncode->extradata_size;
pst->codec->extradata = mCtxEncode->extradata;
if(mFormatContext->oformat->flags & AVFMT_GLOBALHEADER)
{
pst->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
if ( !( mFormatContext->oformat->flags & AVFMT_NOFILE ) )
{
if ( avio_open( &mFormatContext->pb, mFormatContext->filename, AVIO_FLAG_WRITE ) )
{
return false;
}
}
//this is where the mp4 headers (avcc header is written based on extradata)
avformat_write_header( mFormatContext , NULL );
mInitialized = true;
}
else
{
return false;
}
return true;
}
///
/// \brief Closes the MP4
///
void Mp4WriterLinux::Close()
{
if ( !IsAvailable() )
{
return;
}
MutexHolder m(&mMutex);
//////////////////////////////////
if ( mCtxEncode != NULL )
{
//flush the encoder for any additional frames
for (int lGotOutput = 1; lGotOutput; )
{
AVPacket lAvpkt;
av_init_packet( &lAvpkt );
lAvpkt.size = 0;
lAvpkt.data = NULL;
fflush(stdout);
if ( avcodec_encode_video2( mCtxEncode, &lAvpkt, NULL, &lGotOutput ) < 0 )
{
//this is an error condition. should stop encode
lGotOutput = 0;
}
if ( lGotOutput )
{
if ( mFormatContext != NULL )
{
lAvpkt.pts = lAvpkt.dts = mVideoTimestamp++;
int lResult2 = av_write_frame( mFormatContext, &lAvpkt );
}
av_free_packet( &lAvpkt );
}
}
}
if ( mFormatContext != NULL )
{
//must write the trailer for moov atom to be written
av_write_trailer( mFormatContext );
if ( mFormatContext->oformat && !( mFormatContext->oformat->flags & AVFMT_NOFILE ) && mFormatContext->pb )
{
avio_close( mFormatContext->pb );
}
if ( mFormatContext->streams )
{
if (mFormatContext->streams[0]->codec)
{
//extradata was part of mCtxEncode and will be freed when mCtxEncode is freed
mFormatContext->streams[0]->codec->extradata = NULL;
mFormatContext->streams[0]->codec->extradata_size = 0;
}
}
avformat_free_context( mFormatContext );
mFormatContext = NULL;
}
mCodecEncode = NULL;
if ( mCtxEncode != NULL )
{
avcodec_close( mCtxEncode );
av_free( mCtxEncode );
mCtxEncode = NULL;
}
if ( mPictureEncoded != NULL)
{
avcodec_free_frame( &mPictureEncoded );
mPictureEncoded = NULL;
}
if ( mI420Converter != NULL )
{
sws_freeContext( mI420Converter );
mI420Converter = NULL;
}
if ( mI420Data[ 0 ] != NULL )
{
free( mI420Data[ 0 ] );
mI420Data[ 0 ] = NULL;
}
if ( mI420Data[ 1 ] != NULL )
{
free( mI420Data[ 1 ] );
mI420Data[ 1 ] = NULL;
}
if ( mI420Data[ 2 ] != NULL )
{
free( mI420Data[ 2 ] );
mI420Data[ 2 ] = NULL;
}
mI420Stride[ 0 ] = 0;
mI420Stride[ 1 ] = 0;
mI420Stride[ 2 ] = 0;
mInitialized = false;
mWidth = 0;
mHeight = 0;
mVideoTimestamp = 0;
mStartTm = 0;
//////////////////////////////////
}
///
/// \brief Writes a PvImage to the MP4/H.264 container
///
bool Mp4WriterLinux::WriteFrame( PvImage *aImage, uint32_t *aFileSizeDelta )
{
MutexHolder m(&mMutex);
if ( !IsAvailable() || !mInitialized )
{
return false;
}
if ( aImage->GetWidth() != mWidth || aImage->GetHeight() != mHeight )
{
//can not change size of encode midstream
return false;
}
if ( aImage->GetPixelType() != DST_PIXEL_TYPE)
{
//currently only support BGRa input
return false;
}
//convert to YUV420 planar
int pic_size = mWidth * mHeight;
avcodec_get_frame_defaults( mPictureEncoded );
mPictureEncoded->format = PIX_FMT_YUV420P;
mPictureEncoded->width = mWidth;
mPictureEncoded->height = mHeight;
mPictureEncoded->linesize[ 0 ] = mWidth;
mPictureEncoded->linesize[ 1 ] = mWidth / 2;
mPictureEncoded->linesize[ 2 ] = mWidth / 2;
mPictureEncoded->data[ 0 ] = mI420Data[ 0 ];
mPictureEncoded->data[ 1 ] = mI420Data[ 1 ];
mPictureEncoded->data[ 2 ] = mI420Data[ 2 ];
uint8_t *lData[ AV_NUM_DATA_POINTERS ] = { 0 };
lData[ 0 ] = aImage->GetDataPointer();
int lLinesize[ AV_NUM_DATA_POINTERS ] = { 0 };
lLinesize[ 0 ] = mWidth * 4;
sws_scale( mI420Converter, lData, lLinesize, 0, mHeight, mI420Data, mI420Stride );
//timestamps in libav are based on the time_scale set in FormatContext (ie mRecordFps).
//for example for 10fps, timescale is 10. So frames coming at 0, 100ms, 200ms (perfect 10ms)
//will have timestamps 0, 1, 2,
//so the millisecond timestamp is calculated as = (timestamp * 1000)/timescale
//and so the timestamp is calculated as = (time diff * timescale)/1000
//note that time diff should be calculated from a start time and not just relative
//to last frame to avoid long term rounding off problem
uint64_t lNow = PvGetTickCountMs();
if ( mStartTm )
{
if ( lNow > mStartTm )
{
mVideoTimestamp = (( lNow - mStartTm ) * mRecordFps ) / 1000;
}
else
{
++mVideoTimestamp;
}
}
else
{
mStartTm = lNow;
}
mPictureEncoded->pts = mVideoTimestamp;
//avpkt will get allocated by the encode function
//it should be freed only if "lGotPacketPtr" is set
AVPacket lAvpkt;
av_init_packet( &lAvpkt );
lAvpkt.size = 0;
lAvpkt.data = NULL;
int lGotPacketPtr = 0;
// encode frame
int lEncResult = avcodec_encode_video2( mCtxEncode, &lAvpkt, mPictureEncoded, &lGotPacketPtr );
if( lEncResult < 0 )
{
return false;
}
else if ( lGotPacketPtr == 0 )
{
lEncResult = avcodec_encode_video2( mCtxEncode, &lAvpkt, NULL, &lGotPacketPtr );
}
if ( lAvpkt.size )
{
lAvpkt.stream_index = mFormatContext->streams[0]->index;
if ( av_write_frame( mFormatContext, &lAvpkt ) )
{
av_free_packet(&lAvpkt);
return false;
}
av_free_packet(&lAvpkt);
}
else
{
return false;
}
return true;
}
#endif