tomwalters@397: // Copyright 2007, Thomas Walters tomwalters@397: // tomwalters@397: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@397: // http://www.acousticscale.org/AIMC tomwalters@397: // tomwalters@397: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@397: // you may not use this file except in compliance with the License. tomwalters@397: // You may obtain a copy of the License at tomwalters@397: // tomwalters@397: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@397: // tomwalters@397: // Unless required by applicable law or agreed to in writing, software tomwalters@397: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@397: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@397: // See the License for the specific language governing permissions and tomwalters@397: // limitations under the License. tomwalters@397: tomwalters@397: /*! tomwalters@397: * \file tomwalters@397: * \brief Output device for output direct to a movie via local calls to libavcodec tomwalters@397: * tomwalters@397: * \author Tom Walters tomwalters@397: * \date created 2007/10/05 tomwalters@397: * \version \$Id: $ tomwalters@397: */ tomwalters@397: tomwalters@397: #include "Support/Common.h" tomwalters@397: tomwalters@397: #include tomwalters@397: #include tomwalters@397: #include tomwalters@397: #include tomwalters@397: tom@400: #include "Modules/Output/Graphics/Devices/GraphicsOutputDeviceMovieDirect.h" tom@400: tom@400: namespace aimc { tomwalters@397: tomwalters@397: GraphicsOutputDeviceMovieDirect::GraphicsOutputDeviceMovieDirect(Parameters *params) tomwalters@398: : GraphicsOutputDeviceMovie(params) { tomwalters@398: m_sMovieFile[0] = '\0'; tomwalters@398: m_sSoundFile[0] = '\0'; tomwalters@397: } tomwalters@397: tomwalters@397: bool GraphicsOutputDeviceMovieDirect::Initialize(const char *sSoundFile, tomwalters@397: const char *sMovieFile) { tomwalters@398: // We want pnm for direct movie conversion as the data format is nice and simple tomwalters@398: //! \bug This may change the user preference in GUI, hmm what to do? See TODO.txt tomwalters@398: //m_pParam->SetString("output.img.format", "pnm"); tomwalters@397: tomwalters@398: // Initialise GraphicsOutputDevicePlotutils for memory buffer use tomwalters@398: if(!GraphicsOutputDeviceCairo::Initialize()) tomwalters@397: return false; tomwalters@397: tomwalters@397: int width = m_pParam->GetUInt("output.img.width"); tomwalters@398: int height = m_pParam->GetUInt("output.img.height"); tomwalters@398: //float framerate = 1000.0f/m_pParam->GetFloat("output.frameperiod"); tomwalters@398: float framerate=1000.0f/20.0f; tomwalters@397: tomwalters@397: m_pOutputMovie = new LibavformatWriter; tomwalters@398: m_pOutputMovie->Init(sMovieFile, width, height, framerate); tomwalters@397: tomwalters@398: return true; tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceMovieDirect::Stop() { tomwalters@398: // Make sure Plotutils is really done writing. tomwalters@398: GraphicsOutputDeviceCairo::Stop(); tomwalters@398: m_pOutputMovie->End(); tomwalters@398: delete m_pOutputMovie; tomwalters@397: tomwalters@397: } tomwalters@397: tomwalters@397: void GraphicsOutputDeviceMovieDirect::gRelease() { tomwalters@397: // write the buffer tomwalters@397: unsigned char *buf = GraphicsOutputDeviceCairo::GetBuffer(); tomwalters@397: tomwalters@397: if (buf != NULL) tomwalters@397: m_pOutputMovie->WriteFrame(buf); tomwalters@397: tomwalters@397: GraphicsOutputDeviceCairo::gRelease(); tomwalters@397: } tomwalters@397: tomwalters@397: tomwalters@397: // Everything below here is hacked from the Libavformat API example: tomwalters@397: /* tomwalters@397: * Libavformat API example: Output a media file in any supported tomwalters@397: * libavformat format. The default codecs are used. tomwalters@397: * tomwalters@397: * Copyright (c) 2003 Fabrice Bellard tomwalters@397: * tomwalters@397: * Permission is hereby granted, free of charge, to any person obtaining a copy tomwalters@397: * of this software and associated documentation files (the "Software"), to deal tomwalters@397: * in the Software without restriction, including without limitation the rights tomwalters@397: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell tomwalters@397: * copies of the Software, and to permit persons to whom the Software is tomwalters@397: * furnished to do so, subject to the following conditions: tomwalters@397: * tomwalters@397: * The above copyright notice and this permission notice shall be included in tomwalters@397: * all copies or substantial portions of the Software. tomwalters@397: * tomwalters@397: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR tomwalters@397: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, tomwalters@397: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL tomwalters@397: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER tomwalters@397: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, tomwalters@397: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN tomwalters@397: * THE SOFTWARE. tomwalters@397: */ tomwalters@397: tomwalters@397: tomwalters@397: LibavformatWriter::LibavformatWriter() { tomwalters@397: sws_flags = SWS_BICUBIC; tomwalters@397: pixfmt= PIX_FMT_RGB24; tomwalters@397: fmt = NULL; tomwalters@397: } tomwalters@397: tomwalters@397: bool LibavformatWriter::Init(const char *sMovieFile, tomwalters@397: int width, tomwalters@397: int height, tomwalters@397: float framerate) { tomwalters@397: /* initialize libavcodec, and register all codecs and formats */ tomwalters@397: av_register_all(); tomwalters@397: tomwalters@397: /* auto detect the output format from the name. default is mpeg. */ tomwalters@397: fmt = guess_format(NULL, sMovieFile, NULL); tomwalters@397: if (!fmt) { tomwalters@397: printf("Could not deduce output format from file extension: using MPEG.\n"); tomwalters@397: fmt = guess_format("mpeg", NULL, NULL); tomwalters@397: } tomwalters@397: if (!fmt) { tomwalters@397: fprintf(stderr, "Could not find suitable output format\n"); tomwalters@397: return false; tomwalters@397: } tomwalters@397: tomwalters@397: /* allocate the output media context */ tomwalters@397: oc = av_alloc_format_context(); tomwalters@397: if (!oc) { tomwalters@397: fprintf(stderr, "Memory error\n"); tomwalters@397: return false; tomwalters@397: } tomwalters@397: oc->oformat = fmt; tomwalters@397: snprintf(oc->filename, sizeof(oc->filename), "%s", sMovieFile); tomwalters@397: tomwalters@397: /* add the audio and video streams using the default format codecs tomwalters@397: and initialize the codecs */ tomwalters@397: video_st = NULL; tomwalters@397: fmt->video_codec=CODEC_ID_PNG; tomwalters@397: if (fmt->video_codec != CODEC_ID_NONE) { tomwalters@397: video_st = add_video_stream(oc, fmt->video_codec, width, height, framerate); tomwalters@397: } tomwalters@397: tomwalters@397: /* set the output parameters (must be done even if no tomwalters@397: parameters). */ tomwalters@397: if (av_set_parameters(oc, NULL) < 0) { tomwalters@397: fprintf(stderr, "Invalid output format parameters\n"); tomwalters@397: return false; tomwalters@397: } tomwalters@397: tomwalters@397: dump_format(oc, 0, sMovieFile, 1); tomwalters@397: tomwalters@397: /* now that all the parameters are set, we can open the audio and tomwalters@397: video codecs and allocate the necessary encode buffers */ tomwalters@397: if (video_st) { tomwalters@397: open_video(oc, video_st); tomwalters@397: } tomwalters@397: tomwalters@397: /* open the output file, if needed */ tomwalters@397: if (!(fmt->flags & AVFMT_NOFILE)) { tomwalters@397: if (url_fopen(&oc->pb, sMovieFile, URL_WRONLY) < 0) { tomwalters@397: fprintf(stderr, "Could not open '%s'\n", sMovieFile); tomwalters@397: return false; tomwalters@397: } tomwalters@397: } tomwalters@397: tomwalters@397: /* write the stream header, if any */ tomwalters@397: av_write_header(oc); tomwalters@397: return true; tomwalters@397: } tomwalters@397: tomwalters@397: tomwalters@397: /**************************************************************/ tomwalters@397: /* video output */ tomwalters@397: tomwalters@397: /* add a video output stream */ tomwalters@397: AVStream* LibavformatWriter::add_video_stream(AVFormatContext *oc, tomwalters@397: CodecID codec_id, tomwalters@397: int width, tomwalters@397: int height, tomwalters@397: float framerate) { tomwalters@397: AVCodecContext *c; tomwalters@397: AVStream *st; tomwalters@397: tomwalters@397: st = av_new_stream(oc, 0); tomwalters@397: if (!st) { tomwalters@397: fprintf(stderr, "Could not alloc stream\n"); tomwalters@397: return NULL; tomwalters@397: } tomwalters@397: tomwalters@397: c = st->codec; tomwalters@397: c->codec_id = codec_id; tomwalters@397: c->codec_type = CODEC_TYPE_VIDEO; tomwalters@397: tomwalters@397: /* put sample parameters */ tomwalters@397: /* resolution must be a multiple of two */ tomwalters@397: c->width = width; tomwalters@397: c->height = height; tomwalters@397: /* time base: this is the fundamental unit of time (in seconds) in terms tomwalters@397: of which frame timestamps are represented. for fixed-fps content, tomwalters@397: timebase should be 1/framerate and timestamp increments should be tomwalters@397: identically 1. */ tomwalters@397: c->time_base.den = (int)framerate; tomwalters@397: c->time_base.num = 1; tomwalters@397: c->gop_size = 12; /* emit one intra frame every twelve frames at most */ tomwalters@397: c->pix_fmt = pixfmt; tomwalters@397: // some formats want stream headers to be separate tomwalters@397: if(!strcmp(oc->oformat->name, "mp4") tomwalters@397: || !strcmp(oc->oformat->name, "mov") tomwalters@397: || !strcmp(oc->oformat->name, "3gp")) { tomwalters@397: c->flags |= CODEC_FLAG_GLOBAL_HEADER; tomwalters@397: } tomwalters@397: return st; tomwalters@397: } tomwalters@397: tomwalters@397: AVFrame* LibavformatWriter::alloc_picture(int pix_fmt, tomwalters@397: int width, tomwalters@397: int height) { tomwalters@397: AVFrame *picture; tomwalters@397: uint8_t *picture_buf; tomwalters@397: int size; tomwalters@397: tomwalters@397: picture = avcodec_alloc_frame(); tomwalters@397: if (!picture) { tomwalters@397: return NULL; tomwalters@397: } tomwalters@397: size = avpicture_get_size(pix_fmt, width, height); tomwalters@397: picture_buf = (uint8_t*)av_malloc(size); tomwalters@397: if (!picture_buf) { tomwalters@397: av_free(picture); tomwalters@397: return NULL; tomwalters@397: } tomwalters@397: avpicture_fill((AVPicture *)picture, picture_buf, tomwalters@397: pix_fmt, width, height); tomwalters@397: return picture; tomwalters@397: } tomwalters@397: tomwalters@397: void LibavformatWriter::open_video(AVFormatContext *oc, AVStream *st) { tomwalters@397: AVCodec *codec; tomwalters@397: AVCodecContext *c; tomwalters@397: c = st->codec; tomwalters@397: tomwalters@397: /* find the video encoder */ tomwalters@397: codec = avcodec_find_encoder(c->codec_id); tomwalters@397: if (!codec) { tomwalters@397: fprintf(stderr, "codec not found\n"); tomwalters@397: exit(1); tomwalters@397: } tomwalters@397: tomwalters@397: /* open the codec */ tomwalters@397: if (avcodec_open(c, codec) < 0) { tomwalters@397: fprintf(stderr, "could not open codec\n"); tomwalters@397: exit(1); tomwalters@397: } tomwalters@397: tomwalters@397: video_outbuf = NULL; tomwalters@397: if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) { tomwalters@397: /* allocate output buffer */ tomwalters@397: /* XXX: API change will be done */ tomwalters@397: /* buffers passed into lav* can be allocated any way you prefer, tomwalters@397: as long as they're aligned enough for the architecture, and tomwalters@397: they're freed appropriately (such as using av_free for buffers tomwalters@397: allocated with av_malloc) */ tomwalters@397: video_outbuf_size = 200000; tomwalters@397: video_outbuf = (uint8_t*)av_malloc(video_outbuf_size); tomwalters@397: } tomwalters@397: tomwalters@397: /* allocate the encoded raw picture */ tomwalters@397: picture = alloc_picture(c->pix_fmt, c->width, c->height); tomwalters@397: if (!picture) { tomwalters@397: fprintf(stderr, "Could not allocate picture\n"); tomwalters@397: exit(1); tomwalters@397: } tomwalters@397: tomwalters@397: /* if the output format is not RGB32, then a temporary RGB32 tomwalters@397: picture is needed too. It is then converted to the required tomwalters@397: output format */ tomwalters@397: tmp_picture = NULL; tomwalters@397: if (c->pix_fmt != PIX_FMT_RGB32) { tomwalters@397: tmp_picture = alloc_picture(PIX_FMT_RGB32, c->width, c->height); tomwalters@397: if (!tmp_picture) { tomwalters@397: fprintf(stderr, "Could not allocate temporary picture\n"); tomwalters@397: exit(1); tomwalters@397: } tomwalters@397: } tomwalters@397: } tomwalters@397: tomwalters@397: tomwalters@397: void LibavformatWriter::close_video(AVFormatContext *oc, tomwalters@397: AVStream *st) { tomwalters@397: avcodec_close(st->codec); tomwalters@397: av_free(picture->data[0]); tomwalters@397: av_free(picture); tomwalters@397: if (tmp_picture) { tomwalters@397: av_free(tmp_picture->data[0]); tomwalters@397: av_free(tmp_picture); tomwalters@397: } tomwalters@397: av_free(video_outbuf); tomwalters@397: } tomwalters@397: tomwalters@397: void LibavformatWriter::WriteFrame(unsigned char *pFrameBuffer) { tomwalters@397: /* compute current video time */ tomwalters@397: if (video_st) tomwalters@397: video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den; tomwalters@397: else tomwalters@397: video_pts = 0.0; tomwalters@397: tomwalters@397: int out_size, ret; tomwalters@397: AVCodecContext *c; tomwalters@397: static struct SwsContext *img_convert_ctx; tomwalters@397: c = video_st->codec; tomwalters@397: if (c->pix_fmt != PIX_FMT_RGB32) { tomwalters@397: /* as we only generate a RGB32 picture, we must convert it tomwalters@397: to the codec pixel format if needed */ tomwalters@397: if (img_convert_ctx == NULL) { tomwalters@397: img_convert_ctx = sws_getContext(c->width, tomwalters@397: c->height, tomwalters@397: PIX_FMT_RGB32, tomwalters@397: c->width, tomwalters@397: c->height, tomwalters@397: c->pix_fmt, tomwalters@397: sws_flags, tomwalters@397: NULL, tomwalters@397: NULL, tomwalters@397: NULL); tomwalters@397: if (img_convert_ctx == NULL) { tomwalters@397: fprintf(stderr, "Cannot initialize the conversion context\n"); tomwalters@397: exit(1); tomwalters@397: } tomwalters@397: } tomwalters@397: fill_image(tmp_picture, pFrameBuffer, c->width, c->height); tomwalters@397: sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize, tomwalters@397: 0, c->height, picture->data, picture->linesize); tomwalters@397: } else { tomwalters@397: fill_image(picture, pFrameBuffer, c->width, c->height); tomwalters@397: } tomwalters@397: /* encode the image */ tomwalters@397: out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture); tomwalters@397: /* if zero size, it means the image was buffered */ tomwalters@397: if (out_size > 0) { tomwalters@397: AVPacket pkt; tomwalters@397: av_init_packet(&pkt); tomwalters@397: tomwalters@397: pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, video_st->time_base); tomwalters@397: if(c->coded_frame->key_frame) { tomwalters@397: pkt.flags |= PKT_FLAG_KEY; tomwalters@397: pkt.stream_index= video_st->index; tomwalters@397: pkt.data= video_outbuf; tomwalters@397: pkt.size= out_size; tomwalters@397: /* write the compressed frame in the media file */ tomwalters@397: ret = av_write_frame(oc, &pkt); tomwalters@397: } tomwalters@397: } else { tomwalters@397: ret = 0; tomwalters@397: } tomwalters@397: if (ret != 0) { tomwalters@397: fprintf(stderr, "Error while writing video frame\n"); tomwalters@397: exit(1); tomwalters@397: } tomwalters@397: } tomwalters@397: tomwalters@397: void LibavformatWriter::End() { tomwalters@397: /* close each codec */ tomwalters@397: if (video_st) { tomwalters@397: close_video(oc, video_st); tomwalters@397: } tomwalters@397: /* write the trailer, if any */ tomwalters@397: av_write_trailer(oc); tomwalters@397: tomwalters@397: /* free the streams */ tomwalters@397: for(i = 0; i < oc->nb_streams; i++) { tomwalters@397: av_freep(&oc->streams[i]->codec); tomwalters@397: av_freep(&oc->streams[i]); tomwalters@397: } tomwalters@397: tomwalters@397: if (!(fmt->flags & AVFMT_NOFILE)) { tomwalters@397: /* close the output file */ tomwalters@397: url_fclose(&oc->pb); tomwalters@397: } tomwalters@397: /* free the stream */ tomwalters@397: av_free(oc); tomwalters@397: } tomwalters@397: tomwalters@397: void LibavformatWriter::fill_image(AVFrame *pict, tomwalters@397: unsigned char *pFrameBuffer, tomwalters@397: int width, tomwalters@397: int height) { tomwalters@398: memcpy((void*)&(pict->data[0][0]), (void*)pFrameBuffer, width*height*4); tomwalters@397: } tom@400: } // namespace aimc