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
|
#include "libavwrapper.h"
extern Poco::Mutex mutex; //application wide mutex
static Poco::Mutex mutex;
#include <stdexcept>
#include <iostream>
#include <cassert>
using namespace std;
void libav::maybeInitFFMpegLib()
{
if (b_is_one_time_inited)
return;
FFMS_Init(0, 0); //should do for all
//av_register_all();
//avcodec_register_all();
avformat_network_init();
b_is_one_time_inited = true;
}
void libav::video_decoder::cleanup(){
if (loaded) {
mutex.lock();
FFMS_DestroyVideoSource(source);
mutex.unlock();
loaded=false;
}
}
bool libav::video_decoder::open(const std::string& filename){
mutex.lock();
loaded=false;
FFMS_Index *index = FFMS_MakeIndex(filename.c_str(), 0, 0, NULL, NULL, FFMS_IEH_IGNORE, NULL, NULL, &err);
if (index == NULL) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
int trackno = FFMS_GetFirstTrackOfType(index, FFMS_TYPE_VIDEO, &err);
if (trackno < 0) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
source = FFMS_CreateVideoSource(filename.c_str(), trackno, index, 1, FFMS_SEEK_NORMAL, &err);
if (source == NULL) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
FFMS_DestroyIndex(index);
props = FFMS_GetVideoProperties(source);
const FFMS_Frame *propframe = FFMS_GetFrame(source, 0, &err);
w=propframe->EncodedWidth;
h=propframe->EncodedHeight;
//propframe->EncodedPixelFormat;
if (FFMS_SetOutputFormatV2(source, pixfmts, propframe->EncodedWidth, propframe->EncodedHeight, FFMS_RESIZER_BICUBIC, &err)) {
std::cerr<<"ffmpegsource: "<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
int framenumber = 0; /* valid until next call to FFMS_GetFrame* on the same video object */
std::cerr<<"ffmpegsource: successfully opened "<<filename<<std::endl;
loaded=true;
mutex.unlock();
return loaded;
}
bool libav::audio_decoder::open(const std::string& filename){
mutex.lock();
loaded=false;
FFMS_Index *index = FFMS_MakeIndex(filename.c_str(),-1, 0, NULL, NULL, FFMS_IEH_IGNORE, NULL, NULL, &err);
if (index == NULL) {
std::cerr<<"ffmpegsource error making index for "<<filename<<":"<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
int trackno = FFMS_GetFirstTrackOfType(index, FFMS_TYPE_AUDIO, &err);
if (trackno < 0) {
std::cerr<<"ffmpegsource error finding audio track in "<<filename<<":"<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
std::cerr<<"ffmpegsource found audio track "<<trackno<<" in "<<filename<<":"<<err.Buffer<<std::endl;
source = FFMS_CreateAudioSource(filename.c_str(), trackno, index, FFMS_DELAY_TIME_ZERO, &err);
if (source == NULL) {
std::cerr<<"ffmpegsource error creating audio source from "<<filename<<":"<<err.Buffer<<std::endl;
mutex.unlock();
return false;
}
FFMS_DestroyIndex(index);
props = FFMS_GetAudioProperties(source);
std::cerr<<"ffmpegsource: successfully opened "<<filename<<std::endl;
loaded=true;
mutex.unlock();
return loaded;
}
void libav::audio_decoder::cleanup(){
if (loaded) {
mutex.lock();
FFMS_DestroyAudioSource(source);
mutex.unlock();
loaded=false;
}
}
bool libav::exporter::setup(int w,int h, int bitRate, int frameRate, std::string container, bool _fragmentation){
maybeInitFFMpegLib();
fragmentation=_fragmentation;
this->w=w;
this->h=h;
this->bitRate=bitRate;
this->frameRate=frameRate;
this->container=container;
if (NULL != sws_ctx) {
sws_freeContext(sws_ctx);
sws_ctx = NULL;
}
sws_ctx = sws_getContext(w, h, AV_PIX_FMT_RGB24,
w, h, AV_PIX_FMT_YUV420P,
sws_flags, NULL, NULL, NULL);
return true;
}
bool libav::exporter::record(std::string filename){
printf("exporter 1 - using filename '%s'.\n",filename.c_str());
// allocate the output media context //
avformat_alloc_output_context2(&oc, NULL, NULL, filename.c_str());
printf("exporter 2.\n");
if (!oc) {
printf("Could not deduce output format from file extension: using MPEG.\n");
avformat_alloc_output_context2(&oc, NULL, "mpeg", filename.c_str());
}
if (!oc) {
return false;
}
fmt = oc->oformat;
printf("exporter 3.\n");
// Add the audio and video streams using the default format codecs
// * and initialize the codecs. //
video_st = NULL;
audio_st = NULL;
fmt->video_codec=AV_CODEC_ID_H264; //AV_CODEC_ID_MPEG4
if (fmt->video_codec != AV_CODEC_ID_NONE) {
video_st = add_stream(oc, &video_codec, fmt->video_codec);
}
if (fmt->audio_codec != AV_CODEC_ID_NONE) {
//audio_st = add_stream(oc, &audio_codec, fmt->audio_codec);
audio_st = add_stream(oc, &audio_codec, fmt->audio_codec);
}
printf("exporter 4.\n");
//set initial video params
video_st->codec->width=w;
video_st->codec->height=h;
video_st->codec->time_base.num = 1;//codecCtx->ticks_per_frame;
video_st->codec->time_base.den = frameRate;
video_st->time_base = video_st->codec->time_base;
//audioStream->time_base = codecCtx->time_base; //???has the capability of crashing
video_st->codec->gop_size = 75; /* emit one intra frame every ten frames */
video_st->codec->pix_fmt = PIX_FMT_YUV420P;
//video_st->codec->bit_rate = bitRate; //need to deal with resolution etc
//video_st->codec->rc_max_rate = bitRate;
//video_st->codec->rc_min_rate = 0;
//how to set profile????? 190913
//this causes a crash
//av_opt_set(video_st->codec->priv_data,"profile",FF_PROFILE_H264_CONSTRAINED_BASELINE, 0);
//
// http://libav-users.943685.n4.nabble.com/Libav-user-encoding-bitrate-wrong-td4433740.html
// http://libav-users.943685.n4.nabble.com/Libav-user-H264-encoding-set-target-average-bitrate-td3912529.html
// http://trac.ffmpeg.org/wiki/x264EncodingGuide
// Now that all the parameters are set, we can open the audio and
// * video codecs and allocate the necessary encode buffers. //
if (video_st)
open_video(oc, video_codec, video_st);
if (audio_st) {
audioframesize=open_audio(oc, audio_codec, audio_st);
audiostep=((float)audioframesize)/(audio_st->codec->sample_rate);
std::cerr << "opened audio codec with "<<audioframesize<<" frame size and "<<audiostep<<" seconds per frame"<<std::endl;
}
printf("exporter 5.\n");
av_dump_format(oc, 0, filename.c_str(), 1);
// open the output file, if needed //
if (!(fmt->flags & AVFMT_NOFILE)) {
mutex.lock();
int ret = avio_open(&oc->pb, filename.c_str(), AVIO_FLAG_WRITE);
mutex.unlock();
if (ret < 0) {
std::cerr <<"Could not open " << filename.c_str() << std::endl;
return false;
}
}
AVDictionary *opts = NULL; // "create" an empty dictionary
//THIS DOES SEEM TO SET CONTAINER OPTS= AS MOOV_SIZE MAKES THE MOVIE BANJAXED
//av_dict_set(&opts, "moov_size", "20000", 0);
if (fragmentation) {
av_dict_set(&opts, "movflags","frag_keyframe", 0);
}
// Write the stream header, if any. //
int ret = avformat_write_header(oc, &opts);
if (ret < 0) {
std::cerr <<"Error occurred when opening output file:" <<endl; // av_err2str(ret) << std::endl;
return false;
}
//#include <libavformat/movenc.h>
//mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, AVFormatContext *s)
/*ret = mov_write_moov_tag(oc, NULL,NULL);
if (ret < 0) {
std::cerr <<"Error occurred when writing moov atom " <<endl; // << av_err2str(ret) << std::endl;
return false;
}*/
if (frame)
frame->pts = 0;
outputframe=0;
return true;
}
bool libav::exporter::encodeFrame(unsigned char *pixels,uint16_t *samples){
// Compute current audio and video time. //
if (audio_st)
audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
else
audio_pts = 0.0;
if (video_st)
video_pts = (double)video_st->pts.val * video_st->time_base.num /
video_st->time_base.den;
else
video_pts = 0.0;
// write interleaved audio and video frames //
if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
write_audio_frame(oc, audio_st, samples);
} else {
write_video_frame(oc, video_st, pixels);
frame->pts += av_rescale_q(1, video_st->codec->time_base, video_st->time_base);
}
//std::cerr << "encoded frame " << outputframe << std::endl;
outputframe++;
return true;
}
bool libav::exporter::encodeFrame(unsigned char *pixels,AVPacket *audio){
// Compute current audio and video time. //
if (audio_st)
audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
else
audio_pts = 0.0;
if (video_st)
video_pts = (double)video_st->pts.val * video_st->time_base.num /
video_st->time_base.den;
else
video_pts = 0.0;
// write interleaved audio and video frames //
if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
write_audio_frame(oc, audio_st, audio);
} else {
write_video_frame(oc, video_st, pixels);
frame->pts += av_rescale_q(1, video_st->codec->time_base, video_st->time_base);
}
//std::cerr << "encoded frame " << outputframe << std::endl;
outputframe++;
return true;
}
bool libav::exporter::encodeFrame(unsigned char *pixels){
video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
write_video_frame(oc, video_st, pixels);
frame->pts += av_rescale_q(1, video_st->codec->time_base, video_st->time_base);
outputframe++;
return true;
}
bool libav::exporter::encodeFrame(uint16_t *samples){
audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
write_audio_frame(oc, audio_st, samples);
return true;
}
void libav::exporter::finishRecord(){
av_write_trailer(oc);
// Close each codec. //
if (video_st)
close_video(oc, video_st);
if (audio_st)
close_audio(oc, audio_st);
if (!(fmt->flags & AVFMT_NOFILE)) {
// Close the output file. //
mutex.lock();
avio_close(oc->pb);
mutex.unlock();
}
// free the stream //
avformat_free_context(oc);
}
AVStream* libav::exporter::add_stream(AVFormatContext *oc, AVCodec **codec,enum AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
// find the encoder //
*codec = avcodec_find_encoder(codec_id);
if (!(*codec)) {
//fprintf(stderr, "Could not find encoder for '%s'\n",
// avcodec_get_name(codec_id));
exit(1);
}
st = avformat_new_stream(oc, *codec);
if (!st) {
//fprintf(stderr, "Could not allocate stream\n");
exit(1);
}
st->id = oc->nb_streams-1;
c = st->codec;
switch ((*codec)->type) {
case AVMEDIA_TYPE_AUDIO:
st->id = 1;
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->bit_rate = 256000;
c->sample_rate = 44100;
c->channels = 2;
c->channel_layout=AV_CH_LAYOUT_STEREO;
break;
case AVMEDIA_TYPE_VIDEO:
c->codec_id = codec_id;
c->bit_rate = bitRate; //need to deal with resolution etc
c->rc_max_rate = bitRate;
c->rc_min_rate = 0;
//c->rc_buffer_size = Profile()->m_videoMaxVopSize; //??
// Resolution must be a multiple of two. //
//c->width = 352;
//c->height = 288;
// timebase: This is the fundamental unit of time (in seconds) in terms
// * of which frame timestamps are represented. For fixed-fps content,
// * timebase should be 1/framerate and timestamp increments should be
// * identical to 1. //
c->time_base.den = frameRate;
c->time_base.num = 1;
c->gop_size = 12; // emit one intra frame every twelve frames at most //
c->pix_fmt = AV_PIX_FMT_YUV420P; //ADDED HARDCODED TJR 280513
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
// just for testing, we also add B frames //
c->max_b_frames = 2;
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
// Needed to avoid using macroblocks in which some coeffs overflow.
// * This does not happen with normal video, it just happens here as
// * the motion of the chroma plane does not match the luma plane. //
c->mb_decision = 2;
}
break;
default:
break;
}
// Some formats want stream headers to be separate. //
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
void libav::exporter::open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
{
int ret;
AVCodecContext *c = st->codec;
// open the codec //
mutex.lock();
ret = avcodec_open2(c, codec, NULL); //OPTIONS CAN GO HERE
mutex.unlock();
if (ret < 0) {
//fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
exit(1);
}
// allocate and init a re-usable frame //
frame = avcodec_alloc_frame();
// moved to constructor and freeing in destructor -- stills crashes the same
if (!frame) {
//fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
// Allocate the encoded raw picture. //
ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
if (ret < 0) {
//fprintf(stderr, "Could not allocate picture: %s\n", av_err2str(ret));
exit(1);
}
// If the output format is not YUV420P, then a temporary YUV420P
// * picture is needed too. It is then converted to the required
// * output format. //
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
ret = avpicture_alloc(&src_picture, AV_PIX_FMT_RGB24, c->width, c->height);
if (ret < 0) {
//fprintf(stderr, "Could not allocate temporary picture: %s\n",
// av_err2str(ret));
exit(1);
}
}
// copy data and linesize picture pointers to frame //
*((AVPicture *)frame) = dst_picture;
outPixels = (uint8_t*)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, st->codec->width,st->codec->height));
}
int libav::exporter::open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
{
AVCodecContext *c;
int ret;
c = st->codec;
// open it //
mutex.lock();
ret = avcodec_open2(c, codec, NULL);
mutex.unlock();
if (ret < 0) {
//fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
exit(1);
}
// init signal generator //
t = 0;
tincr = 2 * M_PI * 110.0 / c->sample_rate;
// increment frequency by 110 Hz per second //
tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
audio_input_frame_size = 10000;
else
audio_input_frame_size = c->frame_size;
/*
samples = av_malloc(audio_input_frame_size *
av_get_bytes_per_sample(c->sample_fmt) *
c->channels);
if (!samples) {
//fprintf(stderr, "Could not allocate audio samples buffer\n");
exit(1);
}
*/
return audio_input_frame_size;
}
void libav::exporter::write_audio_frame(AVFormatContext *oc, AVStream *st,uint16_t *samples)
{
AVCodecContext *c;
AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame = avcodec_alloc_frame();
int got_packet, ret;
//av_init_packet(&pkt); 111013 NOT NECESSARY
c = st->codec;
//get_audio_frame(samples, audio_input_frame_size, c->channels);
frame->nb_samples = audio_input_frame_size;
uint8_t *sampleptr;
int bufsize=audio_input_frame_size * av_get_bytes_per_sample(c->sample_fmt) *c->channels;
if (samples) {
sampleptr=(uint8_t*)samples;
}
else {
sampleptr=new uint8_t[bufsize];
memset(sampleptr,0,bufsize);
}
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
sampleptr,
audio_input_frame_size *
av_get_bytes_per_sample(c->sample_fmt) *
c->channels, 0); //;
//frame->sample_rate=44100; //hard coded input rate- nope, this doesn't help
//frame->format=AV_SAMPLE_FMT_S16P;
//?? why is ffmpeg reporting fltp as the sample format??? doesn't seem to have an effect to change this though
ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (!samples) {
delete[] sampleptr;
}
if (ret < 0) {
//fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
exit(1);
}
if (!got_packet)
return;
pkt.stream_index = st->index;
//avcodec_free_frame(&frame); ///removed 091013
// Write the compressed frame to the media file. //
ret = av_interleaved_write_frame(oc, &pkt);
av_free_packet(&pkt);
if (ret != 0) {
//fprintf(stderr, "Error while writing audio frame: %s\n",
// av_err2str(ret));
exit(1);
}
}
void libav::exporter::write_audio_frame(AVFormatContext *oc, AVStream *st,AVPacket *pkt)
{
pkt->stream_index = st->index;
// Write the compressed frame to the media file. //
int ret = av_interleaved_write_frame(oc, pkt);
if (ret != 0) {
//fprintf(stderr, "Error while writing audio frame: %s\n",
// av_err2str(ret));
exit(1);
}
//avcodec_free_frame(&frame);
av_free_packet(pkt); ///added 091013
}
void libav::exporter::close_audio(AVFormatContext *oc, AVStream *st)
{
mutex.lock();
avcodec_close(st->codec);
mutex.unlock();
}
void libav::exporter::write_video_frame(AVFormatContext *oc, AVStream *st, uint8_t *pixels)
{
int ret;
AVCodecContext *c = st->codec;
avpicture_fill(&src_picture, pixels, PIX_FMT_RGB24, c->width,c->height);
//avpicture_fill(&dst_picture, outPixels, PIX_FMT_YUV420P, c->width,c->height);
sws_scale(sws_ctx, src_picture.data, src_picture.linesize, 0, c->height, dst_picture.data, dst_picture.linesize);
//fill_yuv_image(&dst_picture, frame_count, c->width, c->height);
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
// Raw video case - directly store the picture in the packet //
AVPacket pkt;
//av_init_packet(&pkt); ///removed 101013 NOT NECESSARY
pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = st->index;
pkt.data = dst_picture.data[0];
pkt.size = sizeof(AVPicture);
ret = av_interleaved_write_frame(oc, &pkt);
av_free_packet(&pkt); ///added 091013
} else {
AVPacket pkt = { 0 };
int got_packet;
ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
if (ret < 0) {
//fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
exit(1);
}
// If size is zero, it means the image was buffered. //
if (!ret && got_packet && pkt.size) {
pkt.stream_index = st->index;
// Write the compressed frame to the media file. //
ret = av_interleaved_write_frame(oc, &pkt);
} else {
ret = 0;
}
av_free_packet(&pkt); ///added 091013
}
if (ret != 0) {
//fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
exit(1);
}
frame_count++;
//avcodec_free_frame(&frame);
}
void libav::exporter::close_video(AVFormatContext *oc, AVStream *st)
{
mutex.lock();
//avcodec_close(st->codec); //change 0706 to trace 2nd render issue
avcodec_close(audio_st->codec);
avcodec_close(video_st->codec);
//
//
//av_free(src_picture.data[0]); //removed to explore weird 2nd render crash.. seems to WORK -- seems that the picture data is owned elsewhere
av_free(dst_picture.data[0]);
av_free(frame); //removed to explore crash 2nd time render
//gives *** Error in `./rotord': corrupted double-linked list: 0x00007fd8b005bd60 ***
//where is frame initialised???
//moved to destructor
av_free(outPixels); //SIGSEV here???
mutex.unlock();
}
|