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
|
#include "rotor.h"
//float equality
bool fequal(const float u,const float v){
if (abs(u-v)<.001) return true;
else return false;
};
using namespace Rotor;
Node_factory::Node_factory(){
//for now, statically load prototype map in constructor
add_type("audio_analysis",new Audio_analysis());
add_type("divide",new Signal_divide());
add_type("bang",new Is_new_integer());
add_type("signal_output",new Signal_output());
add_type("testcard",new Testcard());
add_type("video_output",new Video_output());
add_type("video_input",new Video_input());
}
bool Signal_input::connect(Signal_node* source) {
if (source->output_type=="signal") {
connection=(Node*)source;
return true;
}
else return false;
}
bool Image_input::connect(Image_node* source) {
if (source->output_type=="image") {
connection=(Node*)source;
return true;
}
else return false;
}
bool Signal_output::render(const float duration, const float framerate,string &xml_out){
//testing signal routes
cerr << "Rotor: Signal_output rendering " << duration << " seconds at " << framerate << " frames per second" << endl;
float step=1.0f/framerate;
float v=0.0f;
for (float f=0.0f;f<duration;f+=step) {
float u=get_output(Time_spec(f,framerate));
if (!fequal(u,v)) {
xml_out+=("<signal time='"+ofToString(f)+"'>"+ofToString(u)+"</signal>\n");
v=u;
}
}
return true;
}
bool Audio_thumbnailer::init(int _channels,int _bits,int _samples,int _rate) {
//base_audio_processor::init(_channels,_bits,_samples);
channels=_channels;
bits=_bits;
samples=_samples;
samples_per_column=samples/width;
column=0; //point thumbnail bitmap
out_sample=0; //sample in whole track
offset=0x1<<(bits-1); //signed audio
scale=1.0/offset;
sample=0;
samples=0;
accum=0.0;
return true;
}
int Audio_thumbnailer::process_frame(uint8_t *_data,int samples_in_frame){
//begin by processing remaining samples
//samples per column could be larger than a frame! (probably is)
//but all we are doing is averaging
int bytes=(bits>>3);
int stride=channels*bytes;
int in_sample=0;
while (in_sample<samples_in_frame&&column<width) {
//continue the column
while (sample<samples_per_column&&in_sample<samples_in_frame) {
//accumulate samples for this column until we run out of samples
for (int i=0;i<channels;i++) {
unsigned int this_val=0;
for (int j=0;j<bytes;j++) {
this_val+=_data[(in_sample*stride)+(i*bytes)+j]<<(j*8);
}
//convert from integer data format - i.e s16p - to audio signal in -1..1 range
//presume 16 bits for now...
double val=((double)((int16_t)this_val))*scale;
accum+=val*val;
samples++;
}
in_sample++;
sample++;
out_sample++;
}
if (sample==samples_per_column) { //finished a column
//get root-mean
double mean=pow(accum/samples,0.5);
//if (column==0) {
// cerr << "first column total: "<< accum << " in " << samples << " samples, average " << (accum/samples)<<endl;
//}
int colheight=height*mean*0.5;
int hh=height>>1;
for (int i=0;i<height;i++) {
data[i*width+column]=abs(i-hh)<colheight?0xff:0x00;
}
column++;
sample=0;
samples=0;
accum=0.0;
}
}
return out_sample;
}
string Audio_thumbnailer::print(){
//base64 encode the image data output it
stringstream output;
Poco::Base64Encoder *enc=new Poco::Base64Encoder(output);
enc->write(data,width*height);
//tring output;
/*
for (int j=0;j<height;j++) {
for (int i=0;i<width;i++) {
output+=data[j*width+i]<0x7f?"0":"1";
}
output +="\n";
}
*/
enc->close();
delete enc;
return output.str();
}
bool Audio_analysis::init(int _channels,int _bits,int _samples, int _rate) {
//need these to make sense of data
channels=_channels;
bits=_bits;
samples=_samples;
return analyser.init(soname,id,_channels,_bits,_samples,_rate);
//attempt to load vamp plugin and prepare to receive frames of data
//should the audio analysis contain a vamphost or should it inherit?
//maybe neater to contain it in terms of headers etc
}
int Audio_analysis::process_frame(uint8_t *data,int samples_in_frame) {
analyser.process_frame(data,samples_in_frame);
return 1;
}
void Audio_analysis::cleanup() {
analyser.cleanup();
//print_features();
}
void Audio_analysis::print_features(){
for (auto i: analyser.features) {
cerr<<i.second<<" "<<i.first<<endl;
}
}
/*
//testing signal routes
cerr << "Rotor: Signal_output rendering " << duration << " seconds at " << framerate << " frames per second" << endl;
float step=1.0f/framerate;
float v=0.0f;
for (float f=0.0f;f<duration;f+=step) {
float u=get_output(Time_spec(f,framerate));
if (!fequal(u,v)) {
xml_out+=("<signal time='"+ofToString(f)+"'>"+ofToString(u)+"</signal>\n");
v=u;
}
}
return true;
*/
/*
bool Video_output::render(const float duration, const float framerate,const string &output_filename,const string &audio_filename){
//render out the network
//set up output context
//then iterate through frames
//querying graph at each frame
av_register_all();
AVCodec *codec;
AVCodecContext *c= NULL;
int i, out_size, size, x, y, outbuf_size;
FILE *f;
AVFrame *picture;
uint8_t *outbuf, *picture_buf;
cerr << "Rotor: rendering " << output_filename << " , " << duration << " seconds at " << framerate << " frames per second" << endl;
codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec) {
cerr<< "codec not found" << endl;
return false;
}
c= avcodec_alloc_context3(codec);
picture= avcodec_alloc_frame();
// put sample parameters /
c->bit_rate = 400000;
// resolution must be a multiple of two /
c->width = 640;
c->height = 480;
// frames per second /
c->time_base= (AVRational){1,25};
c->gop_size = 10; // emit one intra frame every ten frames /
c->max_b_frames=1;
c->pix_fmt = PIX_FMT_YUV420P; //AV_PIX_FMT_RGB24
AVDictionary *options; //= NULL; causes a forward declaration error!?
options=NULL;
// open it /
if (avcodec_open2(c, codec, &options) < 0) {
cerr << "could not open codec" << endl;
return false;
}
f = fopen(output_filename.c_str(), "wb");
if (!f) {
cerr << "could not open "<< output_filename<<endl;
return false;
}
// alloc image and output buffer/
outbuf_size = 100000;
outbuf = malloc(outbuf_size);
size = c->width * c->height;
picture_buf = malloc((size * 3) / 2); // size for YUV 420 /
picture->data[0] = picture_buf;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size / 4;
picture->linesize[0] = c->width;
picture->linesize[1] = c->width / 2;
picture->linesize[2] = c->width / 2;
// encode 1 second of video /
for(i=0;i<250;i++) {
fflush(stdout);
// prepare a dummy image /
// Y /
for(y=0;y<c->height;y++) {
for(x=0;x<c->width;x++) {
picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
}
}
// Cb and Cr /
for(y=0;y<c->height/2;y++) {
for(x=0;x<c->width/2;x++) {
picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
}
}
// encode the image /
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
printf("encoding frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
// get the delayed frames /
for(; out_size; i++) {
fflush(stdout);
out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
printf("write frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
// add sequence end code to have a real mpeg file /
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, f);
fclose(f);
free(picture_buf);
free(outbuf);
avcodec_close(c);
av_free(c);
av_free(picture);
printf("\n");
return true;
}
*/
bool Video_output::render(const float duration, const float framerate,const string &output_filename,const string &audio_filename){
//
//setup defaults
int outW=640;
int outH=480;
int bitRate=4000000;
int frameRate=25;
AVCodecID codecId=AV_CODEC_ID_MPEG4;
std::string container ="mov";
std::string input ="01.mp3";
bool usingaudio=audioloader.setup(input);
if (exporter->setup(outW,outH,bitRate,frameRate,container)) { //codecId,
if (exporter->record(output_filename)) {
cerr << "Rotor: Video_output rendering " << duration << " seconds at " << framerate << " fps" << endl;
float step=1.0f/framerate;
float v=0.0f;
for (float f=0.0f;f<duration;f+=step) {
if (!exporter->encodeFrame(get_output(Frame_spec(f,framerate,outW,outH))->RGBdata,audioloader.get_samples(exporter->get_audio_framesize()))){
//if (!exporter->encodeFrame(get_output(Frame_spec(f,framerate,outW,outH))->RGBdata,audioloader.get_packet())){
cerr << "Rotor: video output failed"<<endl;
break;
}
}
exporter->finishRecord();
cerr << "Rotor: Video_output finished "<< endl;
return true;
}
}
return false;
}
//new version from libav examples
/*
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *audio_st, *video_st;
double audio_pts, video_pts;
int i;
//Initialize libavcodec, and register all codecs and formats. //
av_register_all();
//think about this: when to register and unregister?
//Autodetect the output format from the name. default is MPEG. //
fmt = av_guess_format(NULL, output_filename.c_str(), NULL);
if (!fmt) {
printf("Could not deduce output format from file extension: using MPEG.\n");
fmt = av_guess_format("mpeg", NULL, NULL);
}
if (!fmt) {
cerr << "Rotor: could not find suitable output format" << endl;
return false;
}
//Allocate the output media context. //
oc = avformat_alloc_context();
if (!oc) {
cerr <<"Rotor: memory error"<< endl;
return false;
}
oc->oformat = fmt;
snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
//Add the audio and video streams using the default format codecs
* and initialize the codecs. //
video_st = NULL;
audio_st = NULL;
if (fmt->video_codec != AV_CODEC_ID_NONE) {
video_st = add_video_stream(oc, fmt->video_codec);
}
if (fmt->audio_codec != AV_CODEC_ID_NONE) {
audio_st = add_audio_stream(oc, fmt->audio_codec);
}
//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_st);
if (audio_st)
open_audio(oc, audio_st);
av_dump_format(oc, 0, filename, 1);
//open the output file, if needed //
if (!(fmt->flags & AVFMT_NOFILE)) {
if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
cerr <<"Could not open "<<output_filename<<endl;
return false;
}
}
//Write the stream header, if any. //
avformat_write_header(oc, NULL);
for (;;) {
//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;
if ((!audio_st || audio_pts >= STREAM_DURATION) &&
(!video_st || video_pts >= STREAM_DURATION))
break;
//write interleaved audio and video frames //
if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
write_audio_frame(oc, audio_st);
} else {
write_video_frame(oc, video_st);
}
}
//Write the trailer, if any. The trailer must be written before you
// close the CodecContexts open when you wrote the header; otherwise
// av_write_trailer() may try to use memory that was freed on
// av_codec_close(). //
//av_write_trailer(oc);
//Close each codec. //
if (video_st)
close_video(oc, video_st);
if (audio_st)
close_audio(oc, audio_st);
//Free the streams. //
for (i = 0; i < oc->nb_streams; i++) {
av_freep(&oc->streams[i]->codec);
av_freep(&oc->streams[i]);
}
if (!(fmt->flags & AVFMT_NOFILE))
//Close the output file. //
avio_close(oc->pb);
//free the stream //
av_free(oc);
return true;
*/
bool Video_input::load(const string &filename){
}
|