]> git.sesse.net Git - ffmpeg/blobdiff - doc/examples/decoding_encoding.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / doc / examples / decoding_encoding.c
similarity index 86%
rename from libavcodec/api-example.c
rename to doc/examples/decoding_encoding.c
index 4db92d3702153f2136b6f1b793259198d9191dce..919ee489b633d6910fd46dbddfc9ebe98065daed 100644 (file)
@@ -1,46 +1,42 @@
 /*
- * copyright (c) 2001 Fabrice Bellard
+ * Copyright (c) 2001 Fabrice Bellard
  *
- * This file is part of Libav.
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
  *
- * Libav is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * Libav 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
  */
 
 /**
  * @file
  * libavcodec API use example.
  *
- * @example libavcodec/api-example.c
- * Note that this library only handles codecs (mpeg, mpeg4, etc...),
- * not file formats (avi, vob, etc...). See library 'libavformat' for the
+ * Note that libavcodec only handles codecs (mpeg, mpeg4, etc...),
+ * not file formats (avi, vob, mp4, mov, mkv, mxf, flv, mpegts, mpegps, etc...). See library 'libavformat' for the
  * format handling
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-#ifdef HAVE_AV_CONFIG_H
-#undef HAVE_AV_CONFIG_H
-#endif
+#include <math.h>
 
-#include "libavcodec/avcodec.h"
-#include "libavutil/audioconvert.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/mathematics.h"
-#include "libavutil/samplefmt.h"
+#include <libavutil/opt.h>
+#include <libavcodec/avcodec.h>
+#include <libavutil/audioconvert.h>
+#include <libavutil/imgutils.h>
+#include <libavutil/mathematics.h>
+#include <libavutil/samplefmt.h>
 
 #define INBUF_SIZE 4096
 #define AUDIO_INBUF_SIZE 20480
@@ -114,7 +110,7 @@ static void audio_encode_example(const char *filename)
     uint16_t *samples;
     float t, tincr;
 
-    printf("Audio encoding\n");
+    printf("Encode audio file %s\n", filename);
 
     /* find the MP2 encoder */
     codec = avcodec_find_encoder(AV_CODEC_ID_MP2);
@@ -231,7 +227,7 @@ static void audio_decode_example(const char *outfilename, const char *filename)
 
     av_init_packet(&avpkt);
 
-    printf("Audio decoding\n");
+    printf("Decode audio file %s\n", filename);
 
     /* find the mpeg audio decoder */
     codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
@@ -288,6 +284,8 @@ static void audio_decode_example(const char *outfilename, const char *filename)
         }
         avpkt.size -= len;
         avpkt.data += len;
+        avpkt.dts =
+        avpkt.pts = AV_NOPTS_VALUE;
         if (avpkt.size < AUDIO_REFILL_THRESH) {
             /* Refill the input buffer, to avoid trying to decode
              * incomplete frames. Instead of this, one could also use
@@ -313,7 +311,7 @@ static void audio_decode_example(const char *outfilename, const char *filename)
 /*
  * Video encoding example
  */
-static void video_encode_example(const char *filename)
+static void video_encode_example(const char *filename, int codec_id)
 {
     AVCodec *codec;
     AVCodecContext *c= NULL;
@@ -323,10 +321,10 @@ static void video_encode_example(const char *filename)
     AVPacket pkt;
     uint8_t endcode[] = { 0, 0, 1, 0xb7 };
 
-    printf("Video encoding\n");
+    printf("Encode video file %s\n", filename);
 
     /* find the mpeg1 video encoder */
-    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
+    codec = avcodec_find_encoder(codec_id);
     if (!codec) {
         fprintf(stderr, "codec not found\n");
         exit(1);
@@ -346,6 +344,9 @@ static void video_encode_example(const char *filename)
     c->max_b_frames=1;
     c->pix_fmt = PIX_FMT_YUV420P;
 
+    if(codec_id == AV_CODEC_ID_H264)
+        av_opt_set(c->priv_data, "preset", "slow", 0);
+
     /* open it */
     if (avcodec_open2(c, codec, NULL) < 0) {
         fprintf(stderr, "could not open codec\n");
@@ -358,12 +359,15 @@ static void video_encode_example(const char *filename)
         exit(1);
     }
 
+    /* the image can be allocated by any means and av_image_alloc() is
+     * just the most convenient way if av_malloc() is to be used */
     ret = av_image_alloc(picture->data, picture->linesize, c->width, c->height,
                          c->pix_fmt, 32);
     if (ret < 0) {
         fprintf(stderr, "could not alloc raw picture buffer\n");
         exit(1);
     }
+
     picture->format = c->pix_fmt;
     picture->width  = c->width;
     picture->height = c->height;
@@ -418,7 +422,7 @@ static void video_encode_example(const char *filename)
         }
 
         if (got_output) {
-            printf("encoding frame %3d (size=%5d)\n", i, pkt.size);
+            printf("write frame %3d (size=%5d)\n", i, pkt.size);
             fwrite(pkt.data, 1, pkt.size, f);
             av_free_packet(&pkt);
         }
@@ -468,7 +472,7 @@ static void video_decode_example(const char *outfilename, const char *filename)
     /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
     memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 
-    printf("Video decoding\n");
+    printf("Decode video file %s\n", filename);
 
     /* find the mpeg1 video decoder */
     codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
@@ -582,7 +586,8 @@ int main(int argc, char **argv)
         audio_encode_example("/tmp/test.mp2");
         audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
 
-        video_encode_example("/tmp/test.mpg");
+        video_encode_example("/tmp/test.h264", AV_CODEC_ID_H264);
+        video_encode_example("/tmp/test.mpg", AV_CODEC_ID_MPEG1VIDEO);
         filename = "/tmp/test.mpg";
     } else {
         filename = argv[1];