Don't ignore target init() failures, causes mod_libavcodec to crash synfig
[synfig.git] / synfig-core / trunk / src / modules / mod_libavcodec / trgt_av.cpp
index edd25d4..7ceec6e 100644 (file)
@@ -5,16 +5,17 @@
 **     $Id: trgt_av.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $
 **
 **     \legal
-**     Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **
-**     This software and associated documentation
-**     are CONFIDENTIAL and PROPRIETARY property of
-**     the above-mentioned copyright holder.
+**     This package is free software; you can redistribute it and/or
+**     modify it under the terms of the GNU General Public License as
+**     published by the Free Software Foundation; either version 2 of
+**     the License, or (at your option) any later version.
 **
-**     You may not copy, print, publish, or in any
-**     other way distribute this software without
-**     a prior written agreement with
-**     the copyright holder.
+**     This package is distributed in the hope that it will be useful,
+**     but WITHOUT ANY WARRANTY; without even the implied warranty of
+**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+**     General Public License for more details.
 **     \endlegal
 */
 /* ========================================================================= */
@@ -34,7 +35,7 @@
 
 extern "C"
 {
-#include "libavformat/avformat.h"
+#include <avformat.h>
 }
 
 #include <synfig/general.h>
@@ -306,7 +307,7 @@ public:
                AVCodecContext *context;
        
                //get from inside stream
-               context = &stream->codec;
+               context = stream->codec;
        
                //search for desired codec (contained in the stream)
                codec = avcodec_find_encoder(context->codec_id);
@@ -373,7 +374,7 @@ public:
                
                int                     size, 
                                                ret = 0;
-               AVCodecContext  *context = &stream->codec;
+               AVCodecContext  *context = stream->codec;
                
                /*
                If pict is invalid (NULL), then we are done compressing frames and we are trying to get
@@ -395,10 +396,20 @@ public:
                        pict = encodable;
                }
                
+               AVPacket pkt;
+               av_init_packet(&pkt);
+               pkt.stream_index = stream->index;
+               pkt.data = (uint8_t *)pict;
+               pkt.size = sizeof(AVPicture);
+               if( context->coded_frame )
+                       pkt.pts = context->coded_frame->pts;
+               if( context->coded_frame && context->coded_frame->key_frame)
+                       pkt.flags |= PKT_FLAG_KEY;
+               
                //cludge for raw picture format (they said they'd fix)
                if (formatc->oformat->flags & AVFMT_RAWPICTURE)
                {
-                       ret = av_write_frame(formatc, stream->index, (uint8_t *)pict, sizeof(AVPicture));
+                       ret = av_write_frame(formatc, &pkt);
                }
                else 
                {
@@ -408,7 +419,16 @@ public:
                        //if not zero we've got stuff to write
                        if (size != 0)
                        {
-                               ret = av_write_frame(formatc, stream->index, &videobuffer[0], size);
+                               av_init_packet(&pkt);
+                               pkt.stream_index = stream->index;
+                               pkt.data = &videobuffer[0];
+                               pkt.size = size;
+                               if( context->coded_frame )
+                                       pkt.pts = context->coded_frame->pts;
+                               if( context->coded_frame && context->coded_frame->key_frame)
+                                       pkt.flags |= PKT_FLAG_KEY;
+
+                               ret = av_write_frame(formatc, &pkt);
                                
                                //error detect - possibly throw later...
                                if(ret != 0)
@@ -437,7 +457,7 @@ public:
        void close(AVFormatContext *formatc, AVStream *stream)
        {
                if(stream)
-                       avcodec_close(&stream->codec);
+                       avcodec_close(stream->codec);
                
                if (encodable) 
                {
@@ -566,8 +586,9 @@ public:
                
                AVFormatParameters      fmtparam,*ap = &fmtparam;
                memset(ap, 0, sizeof(*ap));
-               ap->frame_rate = vInfo.fps;
-               ap->frame_rate_base = 1;
+               // FIXME: Port next two lines to recent libavcodec versions
+               //ap->frame_rate = vInfo.fps;
+               //ap->frame_rate_base = 1;
                ap->width = vInfo.w;
                ap->height = vInfo.h;
                //ap->pix_fmt = frame_pix_fmt;
@@ -699,7 +720,7 @@ public:
                        return 0;
                }
                
-               context = &st->codec;
+               context = st->codec;
                context->codec_id = (CodecID)codec_id;
                context->codec_type = CODEC_TYPE_VIDEO;
        
@@ -713,8 +734,9 @@ public:
                context->bit_rate = info.bitrate; //TODO: Make dependant on the quality
                
                /* frames per second */
-               context->frame_rate = info.fps;
-               context->frame_rate_base = 1;
+               // FIXME: Port next two lines to recent libavcodec versions
+               //context->frame_rate = info.fps;
+               //context->frame_rate_base = 1;
                
                /* "High Quality" */
                context->mb_decision=FF_MB_DECISION_BITS;
@@ -745,7 +767,7 @@ public:
                        return 0;
                }
        
-               context = &stream->codec;
+               context = stream->codec;
                context->codec_id = (CodecID)codec_id;  
                context->codec_type = CODEC_TYPE_AUDIO;