dxva2: Simplify some ifdefs
[libav.git] / libavcodec / libxvid_rc.c
CommitLineData
64b7c5b6 1/*
842eabc5 2 * Xvid rate control wrapper for lavc video encoders
64b7c5b6
MN
3 *
4 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
64b7c5b6
MN
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
64b7c5b6 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
64b7c5b6
MN
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
64b7c5b6
MN
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
5b432d66 23#include "config.h"
0a024268 24
5b432d66 25#include <fcntl.h>
0a024268
GD
26#include <unistd.h>
27#include <xvid.h>
5b432d66 28
6fee1b90 29#include "libavutil/attributes.h"
fee9db1f 30#include "libavutil/internal.h"
0a024268 31
64b7c5b6 32#include "avcodec.h"
c8b4a399 33#include "libxvid.h"
64b7c5b6
MN
34#include "mpegvideo.h"
35
12db2832 36/* Create temporary file using mkstemp(), tries /tmp first, if possible.
5b432d66 37 * *prefix can be a character constant; *filename will be allocated internally.
12db2832 38 * Return file descriptor of opened file (or error code on error)
5b432d66 39 * and opened file name in **filename. */
0a024268
GD
40int ff_tempfile(const char *prefix, char **filename)
41{
42 int fd = -1;
5b432d66 43 size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
0a024268 44 *filename = av_malloc(len);
f929ab05 45 if (!(*filename)) {
5b432d66 46 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
a1e2c47c 47 return AVERROR(ENOMEM);
5b432d66 48 }
5b432d66
DB
49 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
50 fd = mkstemp(*filename);
51 if (fd < 0) {
52 snprintf(*filename, len, "./%sXXXXXX", prefix);
53 fd = mkstemp(*filename);
54 }
5b432d66
DB
55 if (fd < 0) {
56 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
a1e2c47c 57 return AVERROR(EIO);
5b432d66
DB
58 }
59 return fd; /* success */
60}
61
6fee1b90
DB
62av_cold int ff_xvid_rate_control_init(MpegEncContext *s)
63{
64b7c5b6
MN
64 char *tmp_name;
65 int fd, i;
a92be9b8
DB
66 xvid_plg_create_t xvid_plg_create = { 0 };
67 xvid_plugin_2pass2_t xvid_2pass2 = { 0 };
64b7c5b6 68
0a024268 69 fd = ff_tempfile("xvidrc.", &tmp_name);
a1e2c47c 70 if (fd < 0) {
e5e8a26d 71 av_log(s, AV_LOG_ERROR, "Cannot create temporary pass2 file.\n");
a1e2c47c 72 return fd;
64b7c5b6
MN
73 }
74
0a024268 75 for (i = 0; i < s->rc_context.num_entries; i++) {
b0f29db5 76 static const char frame_types[] = " ipbs";
64b7c5b6
MN
77 char tmp[256];
78 RateControlEntry *rce;
79
0a024268 80 rce = &s->rc_context.entry[i];
64b7c5b6
MN
81
82 snprintf(tmp, sizeof(tmp), "%c %d %d %d %d %d %d\n",
0a024268
GD
83 frame_types[rce->pict_type],
84 (int) lrintf(rce->qscale / FF_QP2LAMBDA),
85 rce->i_count, s->mb_num - rce->i_count - rce->skip_count,
86 rce->skip_count,
87 (rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits + 7) / 8,
88 (rce->header_bits + rce->mv_bits + 7) / 8);
64b7c5b6 89
f7d183f0
DB
90 if (strlen(tmp) > write(fd, tmp, strlen(tmp))) {
91 av_log(s, AV_LOG_ERROR, "Cannot write to temporary pass2 file.\n");
92 return AVERROR(EIO);
93 }
64b7c5b6
MN
94 }
95
96 close(fd);
97
0a024268
GD
98 xvid_2pass2.version = XVID_MAKE_VERSION(1, 1, 0);
99 xvid_2pass2.filename = tmp_name;
100 xvid_2pass2.bitrate = s->avctx->bit_rate;
101 xvid_2pass2.vbv_size = s->avctx->rc_buffer_size;
102 xvid_2pass2.vbv_maxrate = s->avctx->rc_max_rate;
103 xvid_2pass2.vbv_initial = s->avctx->rc_initial_buffer_occupancy;
64b7c5b6 104
0a024268
GD
105 xvid_plg_create.version = XVID_MAKE_VERSION(1, 1, 0);
106 xvid_plg_create.fbase = s->avctx->time_base.den;
107 xvid_plg_create.fincr = s->avctx->time_base.num;
108 xvid_plg_create.param = &xvid_2pass2;
64b7c5b6 109
0a024268
GD
110 if (xvid_plugin_2pass2(NULL, XVID_PLG_CREATE, &xvid_plg_create,
111 &s->rc_context.non_lavc_opaque) < 0) {
e5e8a26d 112 av_log(s, AV_LOG_ERROR, "xvid_plugin_2pass2 failed\n");
83378d0d
MN
113 return -1;
114 }
115 return 0;
64b7c5b6
MN
116}
117
0a024268
GD
118float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run)
119{
a92be9b8 120 xvid_plg_data_t xvid_plg_data = { 0 };
64b7c5b6 121
0a024268
GD
122 xvid_plg_data.version = XVID_MAKE_VERSION(1, 1, 0);
123 xvid_plg_data.width = s->width;
124 xvid_plg_data.height = s->height;
125 xvid_plg_data.mb_width = s->mb_width;
126 xvid_plg_data.mb_height = s->mb_height;
127 xvid_plg_data.fbase = s->avctx->time_base.den;
128 xvid_plg_data.fincr = s->avctx->time_base.num;
129 xvid_plg_data.min_quant[0] = s->avctx->qmin;
130 xvid_plg_data.min_quant[1] = s->avctx->qmin;
131 xvid_plg_data.min_quant[2] = s->avctx->qmin; // FIXME i/b factor & offset
132 xvid_plg_data.max_quant[0] = s->avctx->qmax;
133 xvid_plg_data.max_quant[1] = s->avctx->qmax;
134 xvid_plg_data.max_quant[2] = s->avctx->qmax; // FIXME i/b factor & offset
135 xvid_plg_data.bquant_offset = 0; // 100 * s->avctx->b_quant_offset;
136 xvid_plg_data.bquant_ratio = 100; // * s->avctx->b_quant_factor;
137
138 if (!s->rc_context.dry_run_qscale) {
139 if (s->picture_number) {
140 xvid_plg_data.length =
141 xvid_plg_data.stats.length = (s->frame_bits + 7) / 8;
142 xvid_plg_data.frame_num = s->rc_context.last_picture_number;
143 xvid_plg_data.quant = s->qscale;
144 xvid_plg_data.type = s->last_pict_type;
145 if (xvid_plugin_2pass2(s->rc_context.non_lavc_opaque,
146 XVID_PLG_AFTER, &xvid_plg_data, NULL)) {
e5e8a26d 147 av_log(s, AV_LOG_ERROR,
0a024268 148 "xvid_plugin_2pass2(handle, XVID_PLG_AFTER, ...) FAILED\n");
64b7c5b6
MN
149 return -1;
150 }
151 }
0a024268
GD
152 s->rc_context.last_picture_number =
153 xvid_plg_data.frame_num = s->picture_number;
154 xvid_plg_data.quant = 0;
155 if (xvid_plugin_2pass2(s->rc_context.non_lavc_opaque,
156 XVID_PLG_BEFORE, &xvid_plg_data, NULL)) {
e5e8a26d 157 av_log(s, AV_LOG_ERROR,
0a024268 158 "xvid_plugin_2pass2(handle, XVID_PLG_BEFORE, ...) FAILED\n");
64b7c5b6
MN
159 return -1;
160 }
0a024268 161 s->rc_context.dry_run_qscale = xvid_plg_data.quant;
64b7c5b6 162 }
0a024268
GD
163 xvid_plg_data.quant = s->rc_context.dry_run_qscale;
164 if (!dry_run)
165 s->rc_context.dry_run_qscale = 0;
166
167 // FIXME this is not exactly identical to Xvid
168 if (s->pict_type == AV_PICTURE_TYPE_B)
169 return xvid_plg_data.quant * FF_QP2LAMBDA * s->avctx->b_quant_factor +
170 s->avctx->b_quant_offset;
83378d0d
MN
171 else
172 return xvid_plg_data.quant * FF_QP2LAMBDA;
64b7c5b6
MN
173}
174
bd8ac882
DB
175av_cold void ff_xvid_rate_control_uninit(MpegEncContext *s)
176{
64b7c5b6
MN
177 xvid_plg_destroy_t xvid_plg_destroy;
178
0a024268
GD
179 xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_DESTROY,
180 &xvid_plg_destroy, NULL);
64b7c5b6 181}