Mercurial > libavcodec.hg
annotate xvidff.c @ 3018:1d22fe43aa78 libavcodec
use h264 MC functions for rectangular blocks too
author | michael |
---|---|
date | Sat, 07 Jan 2006 18:34:19 +0000 |
parents | ef2149182f1c |
children | 0b546eab515d |
rev | line source |
---|---|
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
1 /* |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
2 * Interface to xvidcore for mpeg4 encoding |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
3 * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net> |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
4 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
5 * This library is free software; you can redistribute it and/or |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
6 * modify it under the terms of the GNU Lesser General Public |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
7 * License as published by the Free Software Foundation; either |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
8 * version 2 of the License, or (at your option) any later version. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
9 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
10 * This library is distributed in the hope that it will be useful, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
13 * Lesser General Public License for more details. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
14 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
15 * You should have received a copy of the GNU Lesser General Public |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
16 * License along with this library; if not, write to the Free Software |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
18 */ |
2967 | 19 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
20 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
21 * @file xvidmpeg4.c |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
22 * Interface to xvidcore for MPEG-4 compliant encoding. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
23 * @author Adam Thayer (krevnik@comcast.net) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
24 */ |
2967 | 25 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
26 #include <xvid.h> |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
27 #include <unistd.h> |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
28 #include "common.h" |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
29 #include "avcodec.h" |
2821
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
30 #ifdef CONFIG_WIN32 |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
31 #include <fcntl.h> |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
32 #endif |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
33 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
34 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
35 * Buffer management macros. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
36 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
37 #define BUFFER_SIZE 1024 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
38 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x)) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
39 #define BUFFER_CAT(x) (&((x)[strlen(x)])) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
40 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
41 /* For PPC Use */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
42 #if HAVE_ALTIVEC==1 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
43 extern int has_altivec(void); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
44 #endif |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
45 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
46 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
47 * Structure for the private XviD context. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
48 * This stores all the private context for the codec. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
49 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
50 typedef struct xvid_context { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
51 void *encoder_handle; /** Handle for XviD Encoder */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
52 int xsize, ysize; /** Frame size */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
53 int vop_flags; /** VOP flags for XviD Encoder */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
54 int vol_flags; /** VOL flags for XviD Encoder */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
55 int me_flags; /** Motion Estimation flags */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
56 int qscale; /** Do we use constant scale? */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
57 int quicktime_format; /** Are we in a QT-based format? */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
58 AVFrame encoded_picture; /** Encoded frame information */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
59 char *twopassbuffer; /** Character buffer for two-pass */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
60 char *old_twopassbuffer; /** Old character buffer (two-pass) */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
61 char *twopassfile; /** second pass temp file name */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
62 unsigned char *intra_matrix; /** P-Frame Quant Matrix */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
63 unsigned char *inter_matrix; /** I-Frame Quant Matrix */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
64 } xvid_context_t; |
2967 | 65 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
66 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
67 * Structure for the private first-pass plugin. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
68 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
69 typedef struct xvid_ff_pass1 { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
70 int version; /** XviD version */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
71 xvid_context_t *context; /** Pointer to private context */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
72 } xvid_ff_pass1_t; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
73 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
74 /* Prototypes - See function implementation for details */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
75 int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
76 int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2); |
2967 | 77 void xvid_correct_framerate(AVCodecContext *avctx); |
78 | |
79 /** | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
80 * Creates the private context for the encoder. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
81 * All buffers are allocated, settings are loaded from the user, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
82 * and the encoder context created. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
83 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
84 * @param avctx AVCodecContext pointer to context |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
85 * @return Returns 0 on success, -1 on failure |
2967 | 86 */ |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
87 int ff_xvid_encode_init(AVCodecContext *avctx) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
88 int xerr, i; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
89 int xvid_flags = avctx->flags; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
90 xvid_context_t *x = avctx->priv_data; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
91 uint16_t *intra, *inter; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
92 int fd; |
2967 | 93 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
94 xvid_plugin_single_t single; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
95 xvid_ff_pass1_t rc2pass1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
96 xvid_plugin_2pass2_t rc2pass2; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
97 xvid_gbl_init_t xvid_gbl_init; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
98 xvid_enc_create_t xvid_enc_create; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
99 xvid_enc_plugin_t plugins[7]; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
100 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
101 /* Bring in VOP flags from ffmpeg command-line */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
102 x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */ |
2967 | 103 if( xvid_flags & CODEC_FLAG_4MV ) |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
104 x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */ |
2967 | 105 if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT) |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
106 x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */ |
2967 | 107 if( xvid_flags & CODEC_FLAG_AC_PRED ) |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
108 x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
109 if( xvid_flags & CODEC_FLAG_GRAY ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
110 x->vop_flags |= XVID_VOP_GREYSCALE; |
2967 | 111 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
112 /* Decide which ME quality setting to use */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
113 x->me_flags = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
114 switch( avctx->me_method ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
115 case ME_FULL: /* Quality 6 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
116 x->me_flags |= XVID_ME_EXTSEARCH16 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
117 | XVID_ME_EXTSEARCH8; |
2967 | 118 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
119 case ME_EPZS: /* Quality 4 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
120 x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
121 | XVID_ME_HALFPELREFINE8 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
122 | XVID_ME_CHROMA_PVOP |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
123 | XVID_ME_CHROMA_BVOP; |
2967 | 124 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
125 case ME_LOG: /* Quality 2 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
126 case ME_PHODS: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
127 case ME_X1: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
128 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
129 | XVID_ME_HALFPELREFINE16; |
2967 | 130 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
131 case ME_ZERO: /* Quality 0 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
132 default: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
133 break; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
134 } |
2967 | 135 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
136 /* Decide how we should decide blocks */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
137 switch( avctx->mb_decision ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
138 case 2: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
139 x->vop_flags |= XVID_VOP_MODEDECISION_RD; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
140 x->me_flags |= XVID_ME_HALFPELREFINE8_RD |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
141 | XVID_ME_QUARTERPELREFINE8_RD |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
142 | XVID_ME_EXTSEARCH_RD |
2967 | 143 | XVID_ME_CHECKPREDICTION_RD; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
144 case 1: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
145 if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
146 x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
147 x->me_flags |= XVID_ME_HALFPELREFINE16_RD |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
148 | XVID_ME_QUARTERPELREFINE16_RD; |
2967 | 149 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
150 default: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
151 break; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
152 } |
2967 | 153 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
154 /* Bring in VOL flags from ffmpeg command-line */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
155 x->vol_flags = 0; |
2967 | 156 if( xvid_flags & CODEC_FLAG_GMC ) { |
157 x->vol_flags |= XVID_VOL_GMC; | |
158 x->me_flags |= XVID_ME_GME_REFINE; | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
159 } |
2967 | 160 if( xvid_flags & CODEC_FLAG_QPEL ) { |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
161 x->vol_flags |= XVID_VOL_QUARTERPEL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
162 x->me_flags |= XVID_ME_QUARTERPELREFINE16; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
163 if( x->vop_flags & XVID_VOP_INTER4V ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
164 x->me_flags |= XVID_ME_QUARTERPELREFINE8; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
165 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
166 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
167 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
168 xvid_gbl_init.version = XVID_VERSION; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
169 xvid_gbl_init.debug = 0; |
2967 | 170 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
171 #ifdef ARCH_POWERPC |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
172 /* XviD's PPC support is borked, use libavcodec to detect */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
173 #if HAVE_ALTIVEC==1 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
174 if( has_altivec() ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
175 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
176 } else |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
177 #endif |
2967 | 178 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
179 #else |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
180 /* XviD can detect on x86 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
181 xvid_gbl_init.cpu_flags = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
182 #endif |
2967 | 183 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
184 /* Initialize */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
185 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
186 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
187 /* Create the encoder reference */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
188 memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
189 xvid_enc_create.version = XVID_VERSION; |
2967 | 190 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
191 /* Store the desired frame size */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
192 xvid_enc_create.width = x->xsize = avctx->width; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
193 xvid_enc_create.height = x->ysize = avctx->height; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
194 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
195 /* XviD can determine the proper profile to use */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
196 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */ |
2967 | 197 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
198 /* We don't use zones or threads */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
199 xvid_enc_create.zones = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
200 xvid_enc_create.num_zones = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
201 xvid_enc_create.num_threads = 0; |
2967 | 202 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
203 xvid_enc_create.plugins = plugins; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
204 xvid_enc_create.num_plugins = 0; |
2967 | 205 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
206 /* Initialize Buffers */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
207 x->twopassbuffer = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
208 x->old_twopassbuffer = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
209 x->twopassfile = NULL; |
2967 | 210 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
211 if( xvid_flags & CODEC_FLAG_PASS1 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
212 memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
213 rc2pass1.version = XVID_VERSION; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
214 rc2pass1.context = x; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
215 x->twopassbuffer = av_malloc(BUFFER_SIZE); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
216 x->old_twopassbuffer = av_malloc(BUFFER_SIZE); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
217 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
218 av_log(avctx, AV_LOG_ERROR, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
219 "XviD: Cannot allocate 2-pass log buffers\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
220 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
221 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
222 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0; |
2967 | 223 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
224 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
225 plugins[xvid_enc_create.num_plugins].param = &rc2pass1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
226 xvid_enc_create.num_plugins++; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
227 } else if( xvid_flags & CODEC_FLAG_PASS2 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
228 memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
229 rc2pass2.version = XVID_VERSION; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
230 rc2pass2.bitrate = avctx->bit_rate; |
2967 | 231 |
2821
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
232 #ifdef CONFIG_WIN32 /* Ugly work around */ |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
233 { |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
234 char *tempname; |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
235 |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
236 tempname = tempnam(".", "xvidff"); |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
237 fd = -1; |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
238 if( tempname && |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
239 (fd = open(tempname, _O_RDWR | _O_BINARY)) != -1 ) { |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
240 x->twopassfile = av_strdup(tempname); |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
241 #undef free |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
242 free(tempname); |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
243 #define free please_use_av_free |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
244 if( x->twopassfile == NULL ) { |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
245 av_log(avctx, AV_LOG_ERROR, |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
246 "XviD: Cannot allocate 2-pass buffer\n"); |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
247 return -1; |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
248 } |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
249 } |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
250 } |
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
251 #else |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
252 x->twopassfile = av_malloc(BUFFER_SIZE); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
253 if( x->twopassfile == NULL ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
254 av_log(avctx, AV_LOG_ERROR, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
255 "XviD: Cannot allocate 2-pass buffer\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
256 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
257 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
258 strcpy(x->twopassfile, "/tmp/xvidff.XXXXXX"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
259 fd = mkstemp(x->twopassfile); |
2574
9973475ccf1e
create temporary file in current directory if it failed in /tmp
michael
parents:
2451
diff
changeset
|
260 if(fd < 0){ |
9973475ccf1e
create temporary file in current directory if it failed in /tmp
michael
parents:
2451
diff
changeset
|
261 strcpy(x->twopassfile, "./xvidff.XXXXXX"); |
9973475ccf1e
create temporary file in current directory if it failed in /tmp
michael
parents:
2451
diff
changeset
|
262 fd = mkstemp(x->twopassfile); |
9973475ccf1e
create temporary file in current directory if it failed in /tmp
michael
parents:
2451
diff
changeset
|
263 } |
2821
0f428b024677
mkstemp on win32 workaround by (Ivan Wong: email, ivanwong info)
michael
parents:
2637
diff
changeset
|
264 #endif |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
265 if( fd == -1 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
266 av_log(avctx, AV_LOG_ERROR, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
267 "XviD: Cannot write 2-pass pipe\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
268 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
269 } |
2967 | 270 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
271 if( avctx->stats_in == NULL ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
272 av_log(avctx, AV_LOG_ERROR, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
273 "XviD: No 2-pass information loaded for second pass\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
274 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
275 } |
2967 | 276 |
277 if( strlen(avctx->stats_in) > | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
278 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
279 close(fd); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
280 av_log(avctx, AV_LOG_ERROR, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
281 "XviD: Cannot write to 2-pass pipe\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
282 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
283 } |
2967 | 284 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
285 close(fd); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
286 rc2pass2.filename = x->twopassfile; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
287 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
288 plugins[xvid_enc_create.num_plugins].param = &rc2pass2; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
289 xvid_enc_create.num_plugins++; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
290 } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
291 /* Single Pass Bitrate Control! */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
292 memset(&single, 0, sizeof(xvid_plugin_single_t)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
293 single.version = XVID_VERSION; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
294 single.bitrate = avctx->bit_rate; |
2967 | 295 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
296 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
297 plugins[xvid_enc_create.num_plugins].param = &single; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
298 xvid_enc_create.num_plugins++; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
299 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
300 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
301 /* Luminance Masking */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
302 if( 0.0 != avctx->lumi_masking ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
303 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
304 plugins[xvid_enc_create.num_plugins].param = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
305 xvid_enc_create.num_plugins++; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
306 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
307 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
308 /* Frame Rate and Key Frames */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
309 xvid_correct_framerate(avctx); |
2637 | 310 xvid_enc_create.fincr = avctx->time_base.num; |
311 xvid_enc_create.fbase = avctx->time_base.den; | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
312 if( avctx->gop_size > 0 ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
313 xvid_enc_create.max_key_interval = avctx->gop_size; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
314 else |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
315 xvid_enc_create.max_key_interval = 240; /* XviD's best default */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
316 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
317 /* Quants */ |
2967 | 318 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
319 else x->qscale = 0; |
2967 | 320 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
321 xvid_enc_create.min_quant[0] = avctx->qmin; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
322 xvid_enc_create.min_quant[1] = avctx->qmin; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
323 xvid_enc_create.min_quant[2] = avctx->qmin; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
324 xvid_enc_create.max_quant[0] = avctx->qmax; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
325 xvid_enc_create.max_quant[1] = avctx->qmax; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
326 xvid_enc_create.max_quant[2] = avctx->qmax; |
2967 | 327 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
328 /* Quant Matrices */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
329 x->intra_matrix = x->inter_matrix = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
330 if( avctx->mpeg_quant ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
331 x->vol_flags |= XVID_VOL_MPEGQUANT; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
332 if( (avctx->intra_matrix || avctx->inter_matrix) ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
333 x->vol_flags |= XVID_VOL_MPEGQUANT; |
2967 | 334 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
335 if( avctx->intra_matrix ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
336 intra = avctx->intra_matrix; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
337 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64); |
2967 | 338 } else |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
339 intra = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
340 if( avctx->inter_matrix ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
341 inter = avctx->inter_matrix; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
342 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64); |
2967 | 343 } else |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
344 inter = NULL; |
2967 | 345 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
346 for( i = 0; i < 64; i++ ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
347 if( intra ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
348 x->intra_matrix[i] = (unsigned char)intra[i]; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
349 if( inter ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
350 x->inter_matrix[i] = (unsigned char)inter[i]; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
351 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
352 } |
2967 | 353 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
354 /* Misc Settings */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
355 xvid_enc_create.frame_drop_ratio = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
356 xvid_enc_create.global = 0; |
2967 | 357 if( xvid_flags & CODEC_FLAG_CLOSED_GOP ) |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
358 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
359 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
360 /* Determines which codec mode we are operating in */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
361 avctx->extradata = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
362 avctx->extradata_size = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
363 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
364 /* In this case, we are claiming to be MPEG4 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
365 x->quicktime_format = 1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
366 avctx->codec_id = CODEC_ID_MPEG4; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
367 } else { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
368 /* We are claiming to be XviD */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
369 x->quicktime_format = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
370 avctx->codec_tag = ff_get_fourcc("xvid"); |
2967 | 371 } |
372 | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
373 /* Bframes */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
374 xvid_enc_create.max_bframes = avctx->max_b_frames; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
375 xvid_enc_create.bquant_offset = avctx->b_quant_offset; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
376 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
377 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED; |
2967 | 378 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
379 /* Create encoder context */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
380 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
381 if( xerr ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
382 av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
383 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
384 } |
2967 | 385 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
386 x->encoder_handle = xvid_enc_create.handle; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
387 avctx->coded_frame = &x->encoded_picture; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
388 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
389 return 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
390 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
391 |
2967 | 392 /** |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
393 * Encodes a single frame. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
394 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
395 * @param avctx AVCodecContext pointer to context |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
396 * @param frame Pointer to encoded frame buffer |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
397 * @param buf_size Size of encoded frame buffer |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
398 * @param data Pointer to AVFrame of unencoded frame |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
399 * @return Returns 0 on success, -1 on failure |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
400 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
401 int ff_xvid_encode_frame(AVCodecContext *avctx, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
402 unsigned char *frame, int buf_size, void *data) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
403 int xerr, i; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
404 char *tmp; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
405 xvid_context_t *x = avctx->priv_data; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
406 AVFrame *picture = data; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
407 AVFrame *p = &(x->encoded_picture); |
2967 | 408 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
409 xvid_enc_frame_t xvid_enc_frame; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
410 xvid_enc_stats_t xvid_enc_stats; |
2967 | 411 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
412 /* Start setting up the frame */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
413 memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
414 xvid_enc_frame.version = XVID_VERSION; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
415 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
416 xvid_enc_stats.version = XVID_VERSION; |
2967 | 417 *p = *picture; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
418 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
419 /* Let XviD know where to put the frame. */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
420 xvid_enc_frame.bitstream = frame; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
421 xvid_enc_frame.length = buf_size; |
2967 | 422 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
423 /* Initialize input image fields */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
424 if( avctx->pix_fmt != PIX_FMT_YUV420P ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
425 av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
426 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
427 } |
2967 | 428 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
429 xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
430 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
431 for( i = 0; i < 4; i++ ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
432 xvid_enc_frame.input.plane[i] = picture->data[i]; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
433 xvid_enc_frame.input.stride[i] = picture->linesize[i]; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
434 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
435 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
436 /* Encoder Flags */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
437 xvid_enc_frame.vop_flags = x->vop_flags; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
438 xvid_enc_frame.vol_flags = x->vol_flags; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
439 xvid_enc_frame.motion = x->me_flags; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
440 xvid_enc_frame.type = XVID_TYPE_AUTO; |
2967 | 441 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
442 /* Quant Setting */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
443 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
444 else xvid_enc_frame.quant = 0; |
2967 | 445 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
446 /* Matrices */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
447 xvid_enc_frame.quant_intra_matrix = x->intra_matrix; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
448 xvid_enc_frame.quant_inter_matrix = x->inter_matrix; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
449 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
450 /* Encode */ |
2967 | 451 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE, |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
452 &xvid_enc_frame, &xvid_enc_stats); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
453 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
454 /* Two-pass log buffer swapping */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
455 avctx->stats_out = NULL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
456 if( x->twopassbuffer ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
457 tmp = x->old_twopassbuffer; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
458 x->old_twopassbuffer = x->twopassbuffer; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
459 x->twopassbuffer = tmp; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
460 x->twopassbuffer[0] = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
461 if( x->old_twopassbuffer[0] != 0 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
462 avctx->stats_out = x->old_twopassbuffer; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
463 } |
2967 | 464 } |
465 | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
466 if( 0 <= xerr ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
467 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
468 if( xvid_enc_stats.type == XVID_TYPE_PVOP ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
469 p->pict_type = FF_P_TYPE; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
470 else if( xvid_enc_stats.type == XVID_TYPE_BVOP ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
471 p->pict_type = FF_B_TYPE; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
472 else if( xvid_enc_stats.type == XVID_TYPE_SVOP ) |
2451 | 473 p->pict_type = FF_S_TYPE; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
474 else |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
475 p->pict_type = FF_I_TYPE; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
476 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
477 p->key_frame = 1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
478 if( x->quicktime_format ) |
2967 | 479 return xvid_strip_vol_header(avctx, frame, |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
480 xvid_enc_stats.hlength, xerr); |
2967 | 481 } else |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
482 p->key_frame = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
483 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
484 return xerr; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
485 } else { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
486 av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
487 return -1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
488 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
489 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
490 |
2967 | 491 /** |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
492 * Destroys the private context for the encoder. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
493 * All buffers are freed, and the XviD encoder context is destroyed. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
494 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
495 * @param avctx AVCodecContext pointer to context |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
496 * @return Returns 0, success guaranteed |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
497 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
498 int ff_xvid_encode_close(AVCodecContext *avctx) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
499 xvid_context_t *x = avctx->priv_data; |
2967 | 500 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
501 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
502 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
503 if( avctx->extradata != NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
504 av_free(avctx->extradata); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
505 if( x->twopassbuffer != NULL ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
506 av_free(x->twopassbuffer); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
507 av_free(x->old_twopassbuffer); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
508 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
509 if( x->twopassfile != NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
510 av_free(x->twopassfile); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
511 if( x->intra_matrix != NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
512 av_free(x->intra_matrix); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
513 if( x->inter_matrix != NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
514 av_free(x->inter_matrix); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
515 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
516 return 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
517 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
518 |
2967 | 519 /** |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
520 * Routine to create a global VO/VOL header for MP4 container. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
521 * What we do here is extract the header from the XviD bitstream |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
522 * as it is encoded. We also strip the repeated headers from the |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
523 * bitstream when a global header is requested for MPEG-4 ISO |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
524 * compliance. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
525 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
526 * @param avctx AVCodecContext pointer to context |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
527 * @param frame Pointer to encoded frame data |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
528 * @param header_len Length of header to search |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
529 * @param frame_len Length of encoded frame data |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
530 * @return Returns new length of frame data |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
531 */ |
2967 | 532 int xvid_strip_vol_header(AVCodecContext *avctx, |
533 unsigned char *frame, | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
534 unsigned int header_len, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
535 unsigned int frame_len) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
536 int vo_len = 0, i; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
537 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
538 for( i = 0; i < header_len - 3; i++ ) { |
2967 | 539 if( frame[i] == 0x00 && |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
540 frame[i+1] == 0x00 && |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
541 frame[i+2] == 0x01 && |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
542 frame[i+3] == 0xB6 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
543 vo_len = i; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
544 break; |
2967 | 545 } |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
546 } |
2967 | 547 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
548 if( vo_len > 0 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
549 /* We need to store the header, so extract it */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
550 if( avctx->extradata == NULL ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
551 avctx->extradata = av_malloc(vo_len); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
552 memcpy(avctx->extradata, frame, vo_len); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
553 avctx->extradata_size = vo_len; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
554 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
555 /* Less dangerous now, memmove properly copies the two |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
556 chunks of overlapping data */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
557 memmove(frame, &(frame[vo_len]), frame_len - vo_len); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
558 return frame_len - vo_len; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
559 } else |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
560 return frame_len; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
561 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
562 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
563 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
564 * Routine to correct a possibly erroneous framerate being fed to us. |
2967 | 565 * XviD currently chokes on framerates where the ticks per frame is |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
566 * extremely large. This function works to correct problems in this area |
2967 | 567 * by estimating a new framerate and taking the simpler fraction of |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
568 * the two presented. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
569 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
570 * @param avctx Context that contains the framerate to correct. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
571 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
572 void xvid_correct_framerate(AVCodecContext *avctx) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
573 int frate, fbase; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
574 int est_frate, est_fbase; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
575 int gcd; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
576 float est_fps, fps; |
2967 | 577 |
2637 | 578 frate = avctx->time_base.den; |
579 fbase = avctx->time_base.num; | |
2967 | 580 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
581 gcd = ff_gcd(frate, fbase); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
582 if( gcd > 1 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
583 frate /= gcd; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
584 fbase /= gcd; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
585 } |
2967 | 586 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
587 if( frate <= 65000 && fbase <= 65000 ) { |
2637 | 588 avctx->time_base.den = frate; |
589 avctx->time_base.num = fbase; | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
590 return; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
591 } |
2967 | 592 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
593 fps = (float)frate / (float)fbase; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
594 est_fps = roundf(fps * 1000.0) / 1000.0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
595 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
596 est_frate = (int)est_fps; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
597 if( est_fps > (int)est_fps ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
598 est_frate = (est_frate + 1) * 1000; |
2967 | 599 est_fbase = (int)roundf((float)est_frate / est_fps); |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
600 } else |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
601 est_fbase = 1; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
602 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
603 gcd = ff_gcd(est_frate, est_fbase); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
604 if( gcd > 1 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
605 est_frate /= gcd; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
606 est_fbase /= gcd; |
2967 | 607 } |
608 | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
609 if( fbase > est_fbase ) { |
2637 | 610 avctx->time_base.den = est_frate; |
611 avctx->time_base.num = est_fbase; | |
2967 | 612 av_log(avctx, AV_LOG_DEBUG, |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
613 "XviD: framerate re-estimated: %.2f, %.3f%% correction\n", |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
614 est_fps, (((est_fps - fps)/fps) * 100.0)); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
615 } else { |
2637 | 616 avctx->time_base.den = frate; |
617 avctx->time_base.num = fbase; | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
618 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
619 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
620 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
621 /* |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
622 * XviD 2-Pass Kludge Section |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
623 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
624 * XviD's default 2-pass doesn't allow us to create data as we need to, so |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
625 * this section spends time replacing the first pass plugin so we can write |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
626 * statistic information as libavcodec requests in. We have another kludge |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
627 * that allows us to pass data to the second pass in XviD without a custom |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
628 * rate-control plugin. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
629 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
630 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
631 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
632 * Initializes the two-pass plugin and context. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
633 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
634 * @param param Input construction parameter structure |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
635 * @param handle Private context handle |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
636 * @return Returns XVID_ERR_xxxx on failure, or 0 on success. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
637 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
638 static int xvid_ff_2pass_create(xvid_plg_create_t * param, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
639 void ** handle) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
640 xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
641 char *log = x->context->twopassbuffer; |
2967 | 642 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
643 /* Do a quick bounds check */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
644 if( log == NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
645 return XVID_ERR_FAIL; |
2967 | 646 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
647 /* We use snprintf() */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
648 /* This is because we can safely prevent a buffer overflow */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
649 log[0] = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
650 snprintf(log, BUFFER_REMAINING(log), |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
651 "# ffmpeg 2-pass log file, using xvid codec\n"); |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
652 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
653 "# Do not modify. libxvidcore version: %d.%d.%d\n\n", |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
654 XVID_VERSION_MAJOR(XVID_VERSION), |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
655 XVID_VERSION_MINOR(XVID_VERSION), |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
656 XVID_VERSION_PATCH(XVID_VERSION)); |
2967 | 657 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
658 *handle = x->context; |
2967 | 659 return 0; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
660 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
661 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
662 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
663 * Destroys the two-pass plugin context. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
664 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
665 * @param ref Context pointer for the plugin |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
666 * @param param Destrooy context |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
667 * @return Returns 0, success guaranteed |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
668 */ |
2967 | 669 static int xvid_ff_2pass_destroy(xvid_context_t *ref, |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
670 xvid_plg_destroy_t *param) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
671 /* Currently cannot think of anything to do on destruction */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
672 /* Still, the framework should be here for reference/use */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
673 if( ref->twopassbuffer != NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
674 ref->twopassbuffer[0] = 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
675 return 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
676 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
677 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
678 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
679 * Enables fast encode mode during the first pass. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
680 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
681 * @param ref Context pointer for the plugin |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
682 * @param param Frame data |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
683 * @return Returns 0, success guaranteed |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
684 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
685 static int xvid_ff_2pass_before(xvid_context_t *ref, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
686 xvid_plg_data_t *param) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
687 int motion_remove; |
2967 | 688 int motion_replacements; |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
689 int vop_remove; |
2967 | 690 |
691 /* Nothing to do here, result is changed too much */ | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
692 if( param->zone && param->zone->mode == XVID_ZONE_QUANT ) |
2967 | 693 return 0; |
694 | |
695 /* We can implement a 'turbo' first pass mode here */ | |
696 param->quant = 2; | |
697 | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
698 /* Init values */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
699 motion_remove = ~XVID_ME_CHROMA_PVOP & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
700 ~XVID_ME_CHROMA_BVOP & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
701 ~XVID_ME_EXTSEARCH16 & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
702 ~XVID_ME_ADVANCEDDIAMOND16; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
703 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE | |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
704 XVID_ME_SKIP_DELTASEARCH | |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
705 XVID_ME_FASTREFINE16 | |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
706 XVID_ME_BFRAME_EARLYSTOP; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
707 vop_remove = ~XVID_VOP_MODEDECISION_RD & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
708 ~XVID_VOP_FAST_MODEDECISION_RD & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
709 ~XVID_VOP_TRELLISQUANT & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
710 ~XVID_VOP_INTER4V & |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
711 ~XVID_VOP_HQACPRED; |
2967 | 712 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
713 param->vol_flags &= ~XVID_VOL_GMC; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
714 param->vop_flags &= vop_remove; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
715 param->motion_flags &= motion_remove; |
2967 | 716 param->motion_flags |= motion_replacements; |
717 | |
718 return 0; | |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
719 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
720 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
721 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
722 * Captures statistic data and writes it during first pass. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
723 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
724 * @param ref Context pointer for the plugin |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
725 * @param param Statistic data |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
726 * @return Returns XVID_ERR_xxxx on failure, or 0 on success |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
727 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
728 static int xvid_ff_2pass_after(xvid_context_t *ref, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
729 xvid_plg_data_t *param) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
730 char *log = ref->twopassbuffer; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
731 char *frame_types = " ipbs"; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
732 char frame_type; |
2967 | 733 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
734 /* Quick bounds check */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
735 if( log == NULL ) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
736 return XVID_ERR_FAIL; |
2967 | 737 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
738 /* Convert the type given to us into a character */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
739 if( param->type < 5 && param->type > 0 ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
740 frame_type = frame_types[param->type]; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
741 } else { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
742 return XVID_ERR_FAIL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
743 } |
2967 | 744 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
745 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
746 "%c %d %d %d %d %d %d\n", |
2967 | 747 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks, |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
748 param->stats.ublks, param->stats.length, param->stats.hlength); |
2967 | 749 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
750 return 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
751 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
752 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
753 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
754 * Dispatch function for our custom plugin. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
755 * This handles the dispatch for the XviD plugin. It passes data |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
756 * on to other functions for actual processing. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
757 * |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
758 * @param ref Context pointer for the plugin |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
759 * @param cmd The task given for us to complete |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
760 * @param p1 First parameter (varies) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
761 * @param p2 Second parameter (varies) |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
762 * @return Returns XVID_ERR_xxxx on failure, or 0 on success |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
763 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
764 int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
765 switch( cmd ) { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
766 case XVID_PLG_INFO: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
767 case XVID_PLG_FRAME: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
768 return 0; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
769 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
770 case XVID_PLG_BEFORE: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
771 return xvid_ff_2pass_before(ref, p1); |
2967 | 772 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
773 case XVID_PLG_CREATE: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
774 return xvid_ff_2pass_create(p1, p2); |
2967 | 775 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
776 case XVID_PLG_AFTER: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
777 return xvid_ff_2pass_after(ref, p1); |
2967 | 778 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
779 case XVID_PLG_DESTROY: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
780 return xvid_ff_2pass_destroy(ref, p1); |
2967 | 781 |
2131
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
782 default: |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
783 return XVID_ERR_FAIL; |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
784 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
785 } |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
786 |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
787 /** |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
788 * XviD codec definition for libavcodec. |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
789 */ |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
790 AVCodec xvid_encoder = { |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
791 "xvid", |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
792 CODEC_TYPE_VIDEO, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
793 CODEC_ID_XVID, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
794 sizeof(xvid_context_t), |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
795 ff_xvid_encode_init, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
796 ff_xvid_encode_frame, |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
797 ff_xvid_encode_close |
060053df9538
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
michael
parents:
diff
changeset
|
798 }; |