From 74d1f70bcde75dd1c1ef4d4a1673aa62014d4278 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Thu, 11 Apr 2013 18:34:59 +0100 Subject: first attempt to output video craqshes --- rotord/rotor.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 135 insertions(+), 7 deletions(-) (limited to 'rotord/rotor.cpp') diff --git a/rotord/rotor.cpp b/rotord/rotor.cpp index 5736c2f..2da6a8b 100755 --- a/rotord/rotor.cpp +++ b/rotord/rotor.cpp @@ -25,7 +25,7 @@ void Render_context::runTask() { processors.push_back(audio_thumb); vector analysers=graph.find_nodes("audio_analysis"); for (auto a: analysers) { - processors.push_back(a); + processors.push_back(dynamic_cast(a)); } if (load_audio(audio_filename,processors)) { state=AUDIO_READY; @@ -308,7 +308,7 @@ bool Render_context::load_audio(const string &filename,vectorcodec == NULL) { av_free(frame); - av_close_input_file(formatContext); + avformat_close_input(&formatContext); std::cout << "Couldn't find a proper decoder" << std::endl; return false; } else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0) { av_free(frame); - av_close_input_file(formatContext); + avformat_close_input(&formatContext); std::cout << "Couldn't open the context with the decoder" << std::endl; return false; } @@ -378,7 +378,8 @@ bool Render_context::load_audio(const string &filename,vector"+ofToString(u)+"\n"); + v=u; + } + } + return true; + */ + //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; + + /* find the mpeg1 video encoder */ + 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 = 250; + /* 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; + + /* 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<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<25;i++) { + fflush(stdout); + /* prepare a dummy image */ + /* Y */ + for(y=0;yheight;y++) { + for(x=0;xwidth;x++) { + picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3; + } + } + + /* Cb and Cr */ + for(y=0;yheight/2;y++) { + for(x=0;xwidth/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; } \ No newline at end of file -- cgit v1.2.3