]> git.sesse.net Git - ffmpeg/blobdiff - output_example.c
Check RV30/40 slice offsets to be inside buffer.
[ffmpeg] / output_example.c
index 083bbe5cd68dc9e5a4e5d22e66b0ed9e3dc61444..bd9b7c3f6ff2ad76ce283770567aba93225ee67f 100644 (file)
 #include <math.h>
 
 #ifndef M_PI
-#define M_PI 3.1415926535897931
+#define M_PI 3.14159265358979323846
 #endif
 
-#include "avformat.h"
-#include "swscale.h"
+#include "libavformat/avformat.h"
+#include "libswscale/swscale.h"
+
+#undef exit
 
 /* 5 seconds stream duration */
 #define STREAM_DURATION   5.0
@@ -103,7 +105,7 @@ static void open_audio(AVFormatContext *oc, AVStream *st)
     tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
 
     audio_outbuf_size = 10000;
-    audio_outbuf = malloc(audio_outbuf_size);
+    audio_outbuf = av_malloc(audio_outbuf_size);
 
     /* ugly hack for PCM codecs (will be removed ASAP with new PCM
        support to compute the input frame size in samples */
@@ -122,7 +124,7 @@ static void open_audio(AVFormatContext *oc, AVStream *st)
     } else {
         audio_input_frame_size = c->frame_size;
     }
-    samples = malloc(audio_input_frame_size * 2 * c->channels);
+    samples = av_malloc(audio_input_frame_size * 2 * c->channels);
 }
 
 /* prepare a 16 bit dummy audio frame of 'frame_size' samples and
@@ -154,7 +156,8 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
 
     pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
 
-    pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
+    if (c->coded_frame->pts != AV_NOPTS_VALUE)
+        pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
     pkt.flags |= PKT_FLAG_KEY;
     pkt.stream_index= st->index;
     pkt.data= audio_outbuf;
@@ -215,12 +218,12 @@ static AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
         c->max_b_frames = 2;
     }
     if (c->codec_id == CODEC_ID_MPEG1VIDEO){
-        /* needed to avoid using macroblocks in which some coeffs overflow
-           this doesnt happen with normal video, it just happens here as the
-           motion of the chroma plane doesnt match the luma plane */
+        /* Needed to avoid using macroblocks in which some coeffs overflow.
+           This does not happen with normal video, it just happens here as
+           the motion of the chroma plane does not match the luma plane. */
         c->mb_decision=2;
     }
-    // some formats want stream headers to be seperate
+    // some formats want stream headers to be separate
     if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
 
@@ -237,7 +240,7 @@ static AVFrame *alloc_picture(int pix_fmt, int width, int height)
     if (!picture)
         return NULL;
     size = avpicture_get_size(pix_fmt, width, height);
-    picture_buf = malloc(size);
+    picture_buf = av_malloc(size);
     if (!picture_buf) {
         av_free(picture);
         return NULL;
@@ -271,8 +274,12 @@ static void open_video(AVFormatContext *oc, AVStream *st)
     if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
         /* allocate output buffer */
         /* XXX: API change will be done */
+        /* buffers passed into lav* can be allocated any way you prefer,
+           as long as they're aligned enough for the architecture, and
+           they're freed appropriately (such as using av_free for buffers
+           allocated with av_malloc) */
         video_outbuf_size = 200000;
-        video_outbuf = malloc(video_outbuf_size);
+        video_outbuf = av_malloc(video_outbuf_size);
     }
 
     /* allocate the encoded raw picture */
@@ -374,7 +381,8 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
             AVPacket pkt;
             av_init_packet(&pkt);
 
-            pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
+            if (c->coded_frame->pts != AV_NOPTS_VALUE)
+                pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
             if(c->coded_frame->key_frame)
                 pkt.flags |= PKT_FLAG_KEY;
             pkt.stream_index= st->index;
@@ -532,7 +540,7 @@ int main(int argc, char **argv)
 
     if (!(fmt->flags & AVFMT_NOFILE)) {
         /* close the output file */
-        url_fclose(&oc->pb);
+        url_fclose(oc->pb);
     }
 
     /* free the stream */