]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libxvid.c
lavc: Drop deprecated public symbols
[ffmpeg] / libavcodec / libxvid.c
index 02f1af847510b6e938917f6c79c41ecadb763589..1e8dc5d01b79946e31a84203a7dc520de918065f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Interface to xvidcore for mpeg4 encoding
+ * Interface to xvidcore for MPEG-4 encoding
  * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net>
  *
  * This file is part of Libav.
  * @author Adam Thayer (krevnik@comcast.net)
  */
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <xvid.h>
 
 #include "libavutil/cpu.h"
+#include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
 
 #include "avcodec.h"
 #include "internal.h"
-#include "libxvid.h"
-#include "mpegvideo.h"
+#include "mpegutils.h"
 
 /**
  * Buffer management macros.
@@ -69,6 +74,7 @@ struct xvid_context {
     int ssim_acc;                  /**< SSIM accuracy. 0: accurate. 4: fast. */
     int gmc;
     int me_quality;                /**< Motion estimation quality. 0: fast 6: best. */
+    int mpeg_quant;                /**< Quantization type. 0: H.263, 1: MPEG */
 };
 
 /**
@@ -79,6 +85,10 @@ struct xvid_ff_pass1 {
     struct xvid_context *context;   /**< Pointer to private context */
 };
 
+static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+                             const AVFrame *picture, int *got_packet);
+
+
 /*
  * Xvid 2-Pass Kludge Section
  *
@@ -124,7 +134,7 @@ static int xvid_ff_2pass_create(xvid_plg_create_t *param, void **handle)
  * Destroy the two-pass plugin context.
  *
  * @param ref Context pointer for the plugin
- * @param param Destrooy context
+ * @param param Destroy context
  * @return Returns 0, success guaranteed
  */
 static int xvid_ff_2pass_destroy(struct xvid_context *ref,
@@ -349,6 +359,33 @@ static void xvid_correct_framerate(AVCodecContext *avctx)
     }
 }
 
+/* Create temporary file using mkstemp(), tries /tmp first, if possible.
+ * *prefix can be a character constant; *filename will be allocated internally.
+ * Return file descriptor of opened file (or error code on error)
+ * and opened file name in **filename. */
+static int xvid_tempfile(AVCodecContext *avctx, const char *prefix,
+                         char **filename)
+{
+    int fd = -1;
+    size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
+    *filename  = av_malloc(len);
+    if (!(*filename)) {
+        av_log(avctx, AV_LOG_ERROR, "xvid_tempfile: Cannot allocate file name\n");
+        return AVERROR(ENOMEM);
+    }
+    snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
+    fd = mkstemp(*filename);
+    if (fd < 0) {
+        snprintf(*filename, len, "./%sXXXXXX", prefix);
+        fd = mkstemp(*filename);
+    }
+    if (fd < 0) {
+        av_log(avctx, AV_LOG_ERROR, "xvid_tempfile: Cannot open temporary file %s\n", *filename);
+        return AVERROR(EIO);
+    }
+    return fd; /* success */
+}
+
 static av_cold int xvid_encode_init(AVCodecContext *avctx)
 {
     int xerr, i;
@@ -509,7 +546,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         rc2pass2.version = XVID_VERSION;
         rc2pass2.bitrate = avctx->bit_rate;
 
-        fd = ff_tempfile("xvidff.", &x->twopassfile);
+        fd = xvid_tempfile(avctx, "xvidff.", &x->twopassfile);
         if (fd < 0) {
             av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write 2-pass pipe\n");
             return fd;
@@ -608,7 +645,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
     /* Quant Matrices */
     x->intra_matrix =
     x->inter_matrix = NULL;
+
+#if FF_API_PRIVATE_OPT
+FF_DISABLE_DEPRECATION_WARNINGS
     if (avctx->mpeg_quant)
+        x->mpeg_quant = avctx->mpeg_quant;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+    if (x->mpeg_quant)
         x->vol_flags |= XVID_VOL_MPEGQUANT;
     if ((avctx->intra_matrix || avctx->inter_matrix)) {
         x->vol_flags |= XVID_VOL_MPEGQUANT;
@@ -646,7 +691,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
     avctx->extradata      = NULL;
     avctx->extradata_size = 0;
     if (xvid_flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
-        /* In this case, we are claiming to be MPEG4 */
+        /* In this case, we are claiming to be MPEG-4 */
         x->quicktime_format = 1;
         avctx->codec_id     = AV_CODEC_ID_MPEG4;
     } else {
@@ -663,6 +708,43 @@ FF_ENABLE_DEPRECATION_WARNINGS
     if (avctx->max_b_frames > 0 && !x->quicktime_format)
         xvid_enc_create.global |= XVID_GLOBAL_PACKED;
 
+    /* Encode a dummy frame to get the extradata immediately */
+    if (x->quicktime_format) {
+        AVFrame *picture;
+        AVPacket packet;
+        int got_packet, ret;
+
+        av_init_packet(&packet);
+
+        picture = av_frame_alloc();
+        if (!picture)
+            return AVERROR(ENOMEM);
+
+        xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
+        if (xerr) {
+            av_frame_free(&picture);
+            av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
+            return AVERROR_UNKNOWN;
+        }
+        x->encoder_handle = xvid_enc_create.handle;
+
+        picture->width  = avctx->width;
+        picture->height = avctx->height;
+        picture->format = avctx->pix_fmt;
+
+        if ((ret = av_frame_get_buffer(picture, 32)) < 0) {
+            xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
+            av_frame_free(&picture);
+            return ret;
+        }
+
+        ret = xvid_encode_frame(avctx, &packet, picture, &got_packet);
+        if (!ret && got_packet)
+            av_packet_unref(&packet);
+        av_frame_free(&picture);
+        xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
+    }
+
     /* Create encoder context */
     xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
     if (xerr) {
@@ -688,7 +770,7 @@ static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     xvid_enc_stats_t xvid_enc_stats = { 0 };
 
     if (!user_packet &&
-        (ret = av_new_packet(pkt, mb_width * mb_height * MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0) {
+        (ret = av_new_packet(pkt, mb_width * mb_height * MAX_MB_BYTES + AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
         return ret;
     }
@@ -807,7 +889,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         return 0;
     } else {
         if (!user_packet)
-            av_free_packet(pkt);
+            av_packet_unref(pkt);
         if (!xerr)
             return 0;
         av_log(avctx, AV_LOG_ERROR,
@@ -849,6 +931,7 @@ static const AVOption options[] = {
     { "ssim_acc",    "SSIM accuracy",                   OFFSET(ssim_acc),    AV_OPT_TYPE_INT,   { .i64 = 2 },       0,       4, VE         },
     { "gmc",         "use GMC",                         OFFSET(gmc),         AV_OPT_TYPE_INT,   { .i64 = 0 },       0,       1, VE         },
     { "me_quality",  "Motion estimation quality",       OFFSET(me_quality),  AV_OPT_TYPE_INT,   { .i64 = 0 },       0,       6, VE         },
+    { "mpeg_quant",  "Use MPEG quantizers instead of H.263", OFFSET(mpeg_quant), AV_OPT_TYPE_INT, { .i64 = 0 },     0,       1, VE         },
     { NULL },
 };