forked from moutazhaq/Android-Video-Recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoRecorder.cpp
719 lines (573 loc) · 19.7 KB
/
VideoRecorder.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// compiles on MacOS X with: g++ -DTESTING VideoRecorder.cpp -o v -lavcodec -lavformat -lavutil -lswscale -lx264 -g
#ifdef ANDROID
#include <android/log.h>
#define LOG(...) __android_log_print(ANDROID_LOG_INFO,"VideoRecorder",__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"VideoRecorder",__VA_ARGS__)
#endif
#ifdef TESTING
#define LOG(...) fprintf(stderr, __VA_ARGS__)
#define LOGE(...) fprintf(stderr, __VA_ARGS__)
#endif
#include "VideoRecorder.h"
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
// Do not use C++ exceptions, templates, or RTTI
namespace AVR {
class VideoRecorderImpl : public VideoRecorder {
public:
VideoRecorderImpl();
~VideoRecorderImpl();
bool SetVideoOptions(VideoFrameFormat fmt, int width, int height, unsigned long bitrate);
bool SetAudioOptions(AudioSampleFormat fmt, int channels, unsigned long samplerate, unsigned long bitrate);
bool Open(const char *mp4file, bool hasAudio, bool dbg);
bool Close();
bool Start();
void SupplyVideoFrame(const void *frame, unsigned long numBytes, unsigned long timestamp);
void SupplyAudioSamples(const void *samples, unsigned long numSamples);
private:
AVStream *add_audio_stream(enum CodecID codec_id);
void open_audio();
void write_audio_frame(AVStream *st);
AVStream *add_video_stream(enum CodecID codec_id);
AVFrame *alloc_picture(enum PixelFormat pix_fmt, int width, int height);
void open_video();
void write_video_frame(AVStream *st);
// audio related vars
int16_t *samples;
uint8_t *audio_outbuf;
int audio_outbuf_size;
int audio_input_frame_size;
AVStream *audio_st;
unsigned long audio_input_leftover_samples;
int audio_channels; // number of channels (2)
unsigned long audio_bit_rate; // codec's output bitrate
unsigned long audio_sample_rate; // number of samples per second
int audio_sample_size; // size of each sample in bytes (16-bit = 2)
AVSampleFormat audio_sample_format;
// video related vars
uint8_t *video_outbuf;
int video_outbuf_size;
AVStream *video_st;
int video_width;
int video_height;
unsigned long video_bitrate;
PixelFormat video_pixfmt;
AVFrame *picture; // video frame after being converted to x264-friendly YUV420P
AVFrame *tmp_picture; // video frame before conversion (RGB565)
SwsContext *img_convert_ctx;
unsigned long timestamp_base;
// common
AVFormatContext *oc;
};
VideoRecorder::VideoRecorder()
{
}
VideoRecorder::~VideoRecorder()
{
}
VideoRecorderImpl::VideoRecorderImpl()
{
samples = NULL;
audio_outbuf = NULL;
audio_st = NULL;
audio_input_leftover_samples = 0;
video_outbuf = NULL;
video_st = NULL;
picture = NULL;
tmp_picture = NULL;
img_convert_ctx = NULL;
oc = NULL;
}
VideoRecorderImpl::~VideoRecorderImpl()
{
}
bool VideoRecorderImpl::Open(const char *mp4file, bool hasAudio, bool dbg)
{
av_register_all();
avformat_alloc_output_context2(&oc, NULL, NULL, mp4file);
if (!oc) {
LOGE("could not deduce output format from file extension\n");
return false;
}
video_st = add_video_stream(CODEC_ID_H264);
if(hasAudio)
audio_st = add_audio_stream(CODEC_ID_AAC);
if(dbg)
av_dump_format(oc, 0, mp4file, 1);
open_video();
if(hasAudio)
open_audio();
if (avio_open(&oc->pb, mp4file, AVIO_FLAG_WRITE) < 0) {
LOGE("could not open '%s'\n", mp4file);
return false;
}
av_write_header(oc);
return true;
}
AVStream *VideoRecorderImpl::add_audio_stream(enum CodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
st = av_new_stream(oc, 1);
if (!st) {
LOGE("could not alloc stream\n");
return NULL;
}
c = st->codec;
c->codec_id = codec_id;
c->codec_type = AVMEDIA_TYPE_AUDIO;
c->sample_fmt = audio_sample_format;
c->bit_rate = audio_bit_rate;
c->sample_rate = audio_sample_rate;
c->channels = audio_channels;
c->profile = FF_PROFILE_AAC_LOW;
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
void VideoRecorderImpl::open_audio()
{
AVCodecContext *c;
AVCodec *codec;
c = audio_st->codec;
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
LOGE("audio codec not found\n");
return;
}
if (avcodec_open(c, codec) < 0) {
LOGE("could not open audio codec\n");
return;
}
audio_outbuf_size = 10000; // XXX TODO
audio_outbuf = (uint8_t *)av_malloc(audio_outbuf_size);
audio_input_frame_size = c->frame_size;
samples = (int16_t *)av_malloc(audio_input_frame_size * audio_sample_size * c->channels);
audio_input_leftover_samples = 0;
}
AVStream *VideoRecorderImpl::add_video_stream(enum CodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
st = avformat_new_stream(oc, NULL);
if (!st) {
LOGE("could not alloc stream\n");
return NULL;
}
c = st->codec;
c->codec_id = codec_id;
c->codec_type = AVMEDIA_TYPE_VIDEO;
/* put sample parameters */
c->bit_rate = video_bitrate;
c->width = video_width;
c->height = video_height;
c->time_base.num = 1;
c->time_base.den = 90000;
c->pix_fmt = PIX_FMT_YUV420P; // we convert everything to PIX_FMT_YUV420P
/* h264 specific stuff */
/* c->coder_type = 0; // coder = 0
c->me_cmp |= 1; // cmp=+chroma, where CHROMA = 1
c->partitions |= X264_PART_I8X8 + X264_PART_I4X4 + X264_PART_P8X8 + X264_PART_B8X8; // partitions=+parti8x8+parti4x4+partp8x8+partb8x8
c->me_method = ME_HEX; // me_method=hex
c->me_subpel_quality = 7; // subq=7
c->me_range = 16; // me_range=16
c->gop_size = 250; // g=250
c->keyint_min = 25; // keyint_min=25
c->scenechange_threshold = 40; // sc_threshold=40
c->i_quant_factor = 0.71; // i_qfactor=0.71
c->b_frame_strategy = 1; // b_strategy=1
c->qcompress = 0.6; // qcomp=0.6
c->qmin = 10; // qmin=10
c->qmax = 51; // qmax=51
c->max_qdiff = 4; // qdiff=4
c->max_b_frames = 0; // bf=0
c->refs = 3; // refs=3
c->directpred = 1; // directpred=1
c->trellis = 1; // trellis=1
c->weighted_p_pred = 2; // wpredp=2
c->flags |= CODEC_FLAG_LOOP_FILTER + CODEC_FLAG_GLOBAL_HEADER;
c->flags2 |= CODEC_FLAG2_BPYRAMID + CODEC_FLAG2_MIXED_REFS + CODEC_FLAG2_WPRED + CODEC_FLAG2_8X8DCT + CODEC_FLAG2_FASTPSKIP; // flags2=+bpyramid+mixed_refs+wpred+dct8x8+fastpskip
c->flags2 |= CODEC_FLAG2_8X8DCT;
c->flags2 ^= CODEC_FLAG2_8X8DCT;*/
/*x264 ultrafast preset*/
c->aq_mode = 0; // aq-mode = 0
// b-adapt = 0
c->max_b_frames = 0; // bframes = 0
// no cabac
// no deblock
c->me_method = ME_HEX; // me = dia !!!
c->partitions = 0; // partitions = none
c->rc_lookahead = 0; // rc-lookahead = 0
c->refs = 1; // ref = 1
// scenecut = 0
c->scenechange_threshold = 40;
// subme = 0
c->trellis = 0; // trellis = 0
c->weighted_p_pred = 0; // weightp = 0 ??
c->coder_type = 0;
c->me_subpel_quality = 4;
c->me_range = 16;
c->gop_size = 250;
c->keyint_min = 25;
c->i_quant_factor = 0.71;
c->b_frame_strategy = 0;
c->qcompress = 0.6;
c->qmin = 10;
c->qmax = 51;
c->max_qdiff = 4;
c->directpred = 0;
c->flags |= CODEC_FLAG_LOOP_FILTER + CODEC_FLAG_GLOBAL_HEADER;
c->flags2 |= CODEC_FLAG2_8X8DCT; c->flags2 ^= CODEC_FLAG2_8X8DCT; // no 8x8dct
c->flags2 |= CODEC_FLAG2_MIXED_REFS; c->flags2 ^= CODEC_FLAG2_MIXED_REFS; // no mixed refs
c->flags2 |= CODEC_FLAG2_MBTREE; c->flags2 ^= CODEC_FLAG2_MBTREE; // no mbtree
c->flags2 |= CODEC_FLAG2_WPRED; c->flags2 ^= CODEC_FLAG2_WPRED; // no weightb ??
c->profile = FF_PROFILE_H264_BASELINE;
//c->level = 30;
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
AVFrame *VideoRecorderImpl::alloc_picture(enum PixelFormat pix_fmt, int width, int height)
{
AVFrame *pict;
uint8_t *picture_buf;
int size;
pict = avcodec_alloc_frame();
if (!pict) {
LOGE("could not allocate picture frame\n");
return NULL;
}
size = avpicture_get_size(pix_fmt, width, height);
picture_buf = (uint8_t *)av_malloc(size);
if (!picture_buf) {
av_free(pict);
LOGE("could not allocate picture frame buf\n");
return NULL;
}
avpicture_fill((AVPicture *)pict, picture_buf,
pix_fmt, width, height);
return pict;
}
void VideoRecorderImpl::open_video()
{
AVCodec *codec;
AVCodecContext *c;
timestamp_base = 0;
if(!video_st) {
LOGE("tried to open_video without a valid video_st (add_video_stream must have failed)\n");
return;
}
c = video_st->codec;
codec = avcodec_find_encoder(c->codec_id);
if (!codec) {
LOGE("codec not found\n");
return;
}
if (avcodec_open(c, codec) < 0) {
LOGE("could not open codec\n");
return;
}
video_outbuf = NULL;
if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
video_outbuf_size = c->width * c->height * 4; // We assume the encoded frame will be smaller in size than an equivalent raw frame in RGBA8888 format ... a pretty safe assumption!
video_outbuf = (uint8_t *)av_malloc(video_outbuf_size);
if(!video_outbuf) {
LOGE("could not allocate video_outbuf\n");
return;
}
}
// the AVFrame the YUV frame is stored after conversion
picture = alloc_picture(c->pix_fmt, c->width, c->height);
if (!picture) {
LOGE("Could not allocate picture\n");
return;
}
// the src AVFrame before conversion
/*tmp_picture = alloc_picture(video_pixfmt, c->width, c->height);
if (!tmp_picture) {
LOGE("Could not allocate temporary picture\n");
return;
}*/
// Instead of allocating the video frame buffer and attaching it tmp_picture, thereby incurring an unnecessary memcpy() in SupplyVideoFrame,
// we only allocate the tmp_picture structure and set it up with default values. tmp_picture->data[0] is then reassigned to the incoming
// frame data on the SupplyVideoFrame() call.
tmp_picture = avcodec_alloc_frame();
if(!tmp_picture) {
LOGE("Could not allocate temporary picture\n");
return;
}
if(video_pixfmt != PIX_FMT_RGB565LE) {
LOGE("We've hardcoded linesize in tmp_picture for PIX_FMT_RGB565LE only!!\n");
return;
}
tmp_picture->linesize[0] = c->width * 2; // fix the linesize for tmp_picture (assuming RGB565)
img_convert_ctx = sws_getContext(video_width, video_height, video_pixfmt, c->width, c->height, PIX_FMT_YUV420P, /*SWS_BICUBIC*/SWS_FAST_BILINEAR, NULL, NULL, NULL);
if(img_convert_ctx==NULL) {
LOGE("Could not initialize sws context\n");
return;
}
}
bool VideoRecorderImpl::Close()
{
if(oc) {
// flush out delayed frames
AVPacket pkt;
int out_size;
AVCodecContext *c = video_st->codec;
while(out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, NULL)) {
av_init_packet(&pkt);
if (c->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = video_st->index;
pkt.data = video_outbuf;
pkt.size = out_size;
if(av_interleaved_write_frame(oc, &pkt) != 0) {
LOGE("Unable to write video frame when flushing delayed frames\n");
return false;
}
else {
LOG("wrote delayed frame of size %d\n", out_size);
}
}
av_write_trailer(oc);
}
if(video_st)
avcodec_close(video_st->codec);
if(picture) {
av_free(picture->data[0]);
av_free(picture);
}
if(tmp_picture) {
// tmp_picture->data[0] is no longer allocated by us
//av_free(tmp_picture->data[0]);
av_free(tmp_picture);
}
if(img_convert_ctx) {
sws_freeContext(img_convert_ctx);
}
if(video_outbuf)
av_free(video_outbuf);
if(audio_st)
avcodec_close(audio_st->codec);
if(samples)
av_free(samples);
if(audio_outbuf)
av_free(audio_outbuf);
if(oc) {
for(int i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
avio_close(oc->pb);
av_free(oc);
}
}
bool VideoRecorderImpl::SetVideoOptions(VideoFrameFormat fmt, int width, int height, unsigned long bitrate)
{
switch(fmt) {
case VideoFrameFormatYUV420P: video_pixfmt=PIX_FMT_YUV420P; break;
case VideoFrameFormatNV12: video_pixfmt=PIX_FMT_NV12; break;
case VideoFrameFormatNV21: video_pixfmt=PIX_FMT_NV21; break;
case VideoFrameFormatRGB24: video_pixfmt=PIX_FMT_RGB24; break;
case VideoFrameFormatBGR24: video_pixfmt=PIX_FMT_BGR24; break;
case VideoFrameFormatARGB: video_pixfmt=PIX_FMT_ARGB; break;
case VideoFrameFormatRGBA: video_pixfmt=PIX_FMT_RGBA; break;
case VideoFrameFormatABGR: video_pixfmt=PIX_FMT_ABGR; break;
case VideoFrameFormatBGRA: video_pixfmt=PIX_FMT_BGRA; break;
case VideoFrameFormatRGB565LE: video_pixfmt=PIX_FMT_RGB565LE; break;
case VideoFrameFormatRGB565BE: video_pixfmt=PIX_FMT_RGB565BE; break;
case VideoFrameFormatBGR565LE: video_pixfmt=PIX_FMT_BGR565LE; break;
case VideoFrameFormatBGR565BE: video_pixfmt=PIX_FMT_BGR565BE; break;
default: LOGE("Unknown frame format passed to SetVideoOptions!\n"); return false;
}
video_width = width;
video_height = height;
video_bitrate = bitrate;
return true;
}
bool VideoRecorderImpl::SetAudioOptions(AudioSampleFormat fmt, int channels, unsigned long samplerate, unsigned long bitrate)
{
switch(fmt) {
case AudioSampleFormatU8: audio_sample_format=AV_SAMPLE_FMT_U8; audio_sample_size=1; break;
case AudioSampleFormatS16: audio_sample_format=AV_SAMPLE_FMT_S16; audio_sample_size=2; break;
case AudioSampleFormatS32: audio_sample_format=AV_SAMPLE_FMT_S32; audio_sample_size=4; break;
case AudioSampleFormatFLT: audio_sample_format=AV_SAMPLE_FMT_FLT; audio_sample_size=4; break;
case AudioSampleFormatDBL: audio_sample_format=AV_SAMPLE_FMT_DBL; audio_sample_size=8; break;
default: LOGE("Unknown sample format passed to SetAudioOptions!\n"); return false;
}
audio_channels = channels;
audio_bit_rate = bitrate;
audio_sample_rate = samplerate;
return true;
}
bool VideoRecorderImpl::Start()
{
}
void VideoRecorderImpl::SupplyAudioSamples(const void *sampleData, unsigned long numSamples)
{
// check whether there is any audio stream (hasAudio=true)
if(audio_st == NULL) {
LOGE("tried to supply an audio frame when no audio stream was present\n");
return;
}
AVCodecContext *c = audio_st->codec;
uint8_t *samplePtr = (uint8_t *)sampleData; // using a byte pointer
// numSamples is supplied by the codec.. should be c->frame_size (1024 for AAC)
// if it's more we go through it c->frame_size samples at a time
while(numSamples) {
static AVPacket pkt;
av_init_packet(&pkt); // need to init packet every time so all the values (such as pts) are re-initialized
// if we have enough samples for a frame, we write out c->frame_size number of samples (ie: one frame) to the output context
if( (numSamples + audio_input_leftover_samples) >= c->frame_size) {
// audio_input_leftover_samples contains the number of samples already in our "samples" array, left over from last time
// we copy the remaining samples to fill up the frame to the complete frame size
int num_new_samples = c->frame_size - audio_input_leftover_samples;
memcpy((uint8_t *)samples + (audio_input_leftover_samples * audio_sample_size * audio_channels), samplePtr, num_new_samples * audio_sample_size * audio_channels);
numSamples -= num_new_samples;
samplePtr += (num_new_samples * audio_sample_size * audio_channels);
audio_input_leftover_samples = 0;
pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = audio_st->index;
pkt.data = audio_outbuf;
pkt.size = avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st->time_base);
if(av_interleaved_write_frame(oc, &pkt) != 0) {
LOGE("Error while writing audio frame\n");
return;
}
}
else {
// if we didn't have enough samples for a frame, we copy over however many we had and update audio_input_leftover_samples
int num_new_samples = c->frame_size - audio_input_leftover_samples;
if(numSamples < num_new_samples)
num_new_samples = numSamples;
memcpy((uint8_t *)samples + (audio_input_leftover_samples * audio_sample_size * audio_channels), samplePtr, num_new_samples * audio_sample_size * audio_channels);
numSamples -= num_new_samples;
samplePtr += (num_new_samples * audio_sample_size * audio_channels);
audio_input_leftover_samples += num_new_samples;
}
}
}
void VideoRecorderImpl::SupplyVideoFrame(const void *frameData, unsigned long numBytes, unsigned long timestamp)
{
if(!video_st) {
LOGE("tried to SupplyVideoFrame when no video stream was present\n");
return;
}
AVCodecContext *c = video_st->codec;
//memcpy(tmp_picture->data[0], frameData, numBytes);
// Don't copy the frame unnecessarily! Simply point tmp_picture->data[0] to the incoming frame
tmp_picture->data[0]=(uint8_t*)frameData;
// if the input pixel format is not YUV420P, we'll assume
// it's stored in tmp_picture, so we'll convert it to YUV420P
// and store it in "picture"
// if it's already in YUV420P format we'll assume it's stored in
// "picture" from before
if(video_pixfmt != PIX_FMT_YUV420P) {
sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize, 0, video_height, picture->data, picture->linesize);
}
if(timestamp_base == 0)
timestamp_base = timestamp;
picture->pts = 90 * (timestamp - timestamp_base); // assuming millisecond timestamp and 90 kHz timebase
int out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
LOG("avcodec_encode_video returned %d\n", out_size);
if(out_size > 0) {
static AVPacket pkt;
av_init_packet(&pkt);
if (c->coded_frame->pts != AV_NOPTS_VALUE)
pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base);
if(c->coded_frame->key_frame)
pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = video_st->index;
pkt.data = video_outbuf;
pkt.size = out_size;
if(av_interleaved_write_frame(oc, &pkt) != 0) {
LOGE("Unable to write video frame\n");
return;
}
}
}
VideoRecorder* VideoRecorder::New()
{
return (VideoRecorder*)(new VideoRecorderImpl);
}
} // namespace AVR
#ifdef TESTING
float t = 0;
float tincr = 2 * M_PI * 110.0 / 44100;
float tincr2 = 2 * M_PI * 110.0 / 44100 / 44100;
void fill_audio_frame(int16_t *samples, int frame_size, int nb_channels)
{
int j, i, v;
int16_t *q;
q = samples;
for (j = 0; j < frame_size; j++) {
v = (int)(sin(t) * 10000);
for(i = 0; i < nb_channels; i++)
*q++ = v;
t += tincr;
tincr += tincr2;
}
}
void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
{
int x, y, i;
i = frame_index;
/* Y */
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
}
}
/* Cb and Cr */
for (y = 0; y < height/2; y++) {
for (x = 0; x < width/2; x++) {
pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
}
}
}
#define RGB565(r,g,b) (uint16_t)( ((red & 0x1F) << 11) | ((green & 0x3F) << 5) | (blue & 0x1F) )
void fill_rgb_image(uint8_t *pixels, int i, int width, int height)
{
int x, y;
for(y = 0; y < height; y++) {
for(x = 0; x < width; x++) {
uint8_t red = x + y + i * 3;
uint8_t green = x + y + i * 3;
uint8_t blue = x + y + i * 3;
uint16_t pixel = RGB565(red, green, blue);
// assume linesize is width*2
pixels[y * (width*2) + x*2 + 0] = (uint8_t)(pixel); // lower order bits
pixels[y * (width*2) + x*2 + 1] = (uint8_t)(pixel >> 8); // higher order bits
}
}
}
#include <iostream>
int main()
{
AVR::VideoRecorder *recorder = new AVR::VideoRecorderImpl();
recorder->SetAudioOptions(AVR::AudioSampleFormatS16, 2, 44100, 64000);
recorder->SetVideoOptions(AVR::VideoFrameFormatRGB565LE, 640, 480, 400000);
recorder->Open("testing.mp4", true, true);
int16_t *sound_buffer = new int16_t[2048 * 2];
uint8_t *video_buffer = new uint8_t[640 * 480 * 2];
for(int i = 0; i < 200; i++) {
fill_audio_frame(sound_buffer, 900, 2);
recorder->SupplyAudioSamples(sound_buffer, 900);
fill_rgb_image(video_buffer, i, 640, 480);
recorder->SupplyVideoFrame(video_buffer, 640*480*2, (25 * i)+1);
}
delete video_buffer;
delete sound_buffer;
recorder->Close();
std::cout << "Done" << std::endl;
delete recorder;
return 0;
}
#endif /* TESTING */