Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int renderMovieRequest(movieRequest *movieRequestObj, string outputPath) {
- AVFormatContext *pFormatCtx = NULL;
- size_t i;
- int videoStream;
- AVCodecContext *pCodecCtx = NULL;
- AVCodec *pCodec = NULL;
- AVFrame *pFrame = NULL;
- AVFrame *pFrameRGB = NULL;
- AVPacket packet = { 0 };
- int frameFinished;
- int numBytes;
- uint8_t *buffer = NULL;
- AVDictionary *optionsDict = NULL;
- struct SwsContext *sws_ctx = NULL;
- // read in the protobuf file.
- processProtobuf(movieRequestObj);
- // Register all formats and codecs
- av_register_all();
- vector<clipShPtr> * clips = &(movieRequestObj->clips);
- for (size_t clipIdx = 0; clipIdx < clips->size(); ++clipIdx) {
- shared_ptr<clip> currentClip = clips->at(clipIdx);
- switch (currentClip->getClipType()) {
- case VIDEO_CLIP: {
- shared_ptr<videoClip> vidClip = dynamic_pointer_cast<videoClip>(clips->at(clipIdx));
- if (vidClip->shouldHaveSegments) {
- DLOG("Found segments... :");
- // add clips to new video
- // open the file for reading and create a temporary file for output
- // if (openVideo(pFormatCtx, vidClip->vidFileName.c_str()) != 0) {
- if(avformat_open_input(&pFormatCtx, vidClip->vidFileName.c_str(), NULL, NULL)!=0) {
- ELOG("Can't open file!");
- return -1; // Couldn't open file
- }
- // Retrieve stream information
- if(avformat_find_stream_info(pFormatCtx, NULL)<0)
- return -1; // Couldn't find stream information
- DLOG("Opened file for reading !! ");
- // Find the first video stream
- videoStream=-1;
- for(i=0; i < pFormatCtx->nb_streams; i++) {
- if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
- DLOG("Identified stream %i as video stream", i);
- videoStream=i;
- break;
- }
- }
- if(videoStream==-1) {
- ELOG("Couldn't find a video stream for %s", vidClip->vidFileName.c_str());
- return -1; // Didn't find a video stream
- }
- DLOG("Found video stream getting pointer to codec context");
- // Get a pointer to the codec context for the video stream
- pCodecCtx = pFormatCtx->streams[videoStream]->codec;
- // Find the decoder for the video stream
- pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
- if(pCodec==NULL) {
- ELOG("Unsupported codec!\n");
- return -1; // Codec not found
- }
- // Open codec
- if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<0)
- return -1; // Could not open codec
- // get the timebase
- timeBase = (int64_t(pCodecCtx->time_base.num) * AV_TIME_BASE) / int64_t(pCodecCtx->time_base.den);
- // Allocate video frame
- pFrame=av_frame_alloc();
- // Allocate an AVFrame structure
- pFrameRGB=av_frame_alloc();
- if(pFrameRGB==NULL)
- return -1;
- // Determine required buffer size and allocate buffer
- numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
- DLOG("Buffer size allocated: %i", numBytes);
- buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
- sws_ctx = sws_getContext
- (
- pCodecCtx->width,
- pCodecCtx->height,
- pCodecCtx->pix_fmt,
- pCodecCtx->width,
- pCodecCtx->height,
- PIX_FMT_RGB24,
- SWS_BILINEAR,
- NULL,
- NULL,
- NULL
- );
- // Assign appropriate parts of buffer to image planes in pFrameRGB
- // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
- // of AVPicture
- avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);
- DLOG("Filled buffer ");
- size_t numSegments = vidClip->segments.size();
- DLOG("Found %i segments to process", numSegments);
- for (size_t segmentIdx = 0; segmentIdx < numSegments; ++segmentIdx) {
- // seek to the right position
- DLOG("Processing segment # %i", segmentIdx);
- int frameOffset = vidClip->segments.at(segmentIdx).first;
- int clipDuration = vidClip->segments.at(segmentIdx).second;
- DLOG("Starting Frame Number: %i", frameOffset);
- DLOG("Segment duration: %i", clipDuration);
- // seek(pFormatCtx, frameOffset);
- // loop for X frames where X is < frameOffset + clipDuration
- for (int frameIdx = frameOffset; frameIdx < (frameOffset + clipDuration); ++frameIdx) {
- DLOG("reading in frame %i", frameIdx);
- av_init_packet(&packet);
- int avReadResult = 0;
- int continueRecording = 1;
- while(continueRecording == 1) {
- avReadResult = av_read_frame(pFormatCtx, &packet);
- if(avReadResult != 0){
- if (avReadResult != AVERROR_EOF) {
- ELOG("av_read_frame error: %s", stringForAVErrorNumber(avReadResult));
- } else {
- ILOG("End of input file");
- }
- continueRecording = 0;
- }
- DLOG("Reading packet in stream: %i", videoStream);
- // Is this a packet from the video stream?
- if(packet.stream_index==videoStream) {
- // Decode video frame
- DLOG("Decoding video frame");
- // avcodec_decode_video2(pCodecCtx, pFrameRGB, &frameFinished, &packet);
- // Did we get a video frame?
- if(frameFinished) {
- // Convert the image from its native format to RGB
- // DLOG("Convert Frame to RGB");
- // sws_scale
- // (
- // sws_ctx,
- // (uint8_t const * const *)pFrame->data,
- // pFrame->linesize,
- // 0,
- // pCodecCtx->height,
- // pFrameRGB->data,
- // pFrameRGB->linesize
- // );
- // Save the frame to disk
- if(frameIdx % 10 == 0) {
- // SaveFrameLocal(pFrameRGB, pCodecCtx->width, pCodecCtx->height, frameIdx, vidClip->vidFileName.c_str());
- }
- DLOG("writing frame #%i to file.. ", frameIdx);
- av_interleaved_write_frame(pFormatCtx, &packet);
- DLOG("Done writing frame #%i to file.. ", frameIdx);
- frameIdx++;
- }
- }
- DLOG("Freeing the packet allocated by av_read_frame");
- // Free the packet that was allocated by av_read_frame
- av_free_packet(&packet);
- }
- // Free the RGB image
- DLOG("Cleaning up frame allocations");
- av_free(buffer);
- av_free(pFrameRGB);
- // Free the YUV frame
- av_free(pFrame);
- }
- }
- }
- } // end video clip processing
- break;
- case TITLE_CLIP: {
- break;
- default:
- ELOG("Failed to identify clip");
- break;
- }
- } // end switch statement
- } // end main for loop -> clip iteration
- DLOG("Writing trailer to file");
- av_write_trailer(pFormatCtx);
- // Close the codec
- DLOG("Closing codec");
- avcodec_close(pCodecCtx);
- // Close the video file
- DLOG("Closing output file");
- avformat_close_input(&pFormatCtx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement