annotate mpegvideo.c @ 225:ae145876789d libavcodec

use multiply instead of divides for DC prediction on X86
author michaelni
date Tue, 05 Feb 2002 22:51:23 +0000
parents 0b234715e205
children ec1bc02a0a47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * The simplest mpeg encoder (well, it was the simplest!)
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 * Copyright (c) 2000,2001 Gerard Lantau.
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * (at your option) any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * along with this program; if not, write to the Free Software
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 #include <stdlib.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 #include <stdio.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 #include <math.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 #include <string.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 #include "avcodec.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
24 #include "dsputil.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include "mpegvideo.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
26
17
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 13
diff changeset
27 #ifdef USE_FASTMEMCPY
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 13
diff changeset
28 #include "fastmemcpy.h"
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 13
diff changeset
29 #endif
b69fe46fd708 Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents: 13
diff changeset
30
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
31 static void encode_picture(MpegEncContext *s, int picture_number);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
32 static void rate_control_init(MpegEncContext *s);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
33 static int rate_estimate_qscale(MpegEncContext *s);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
34 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
35 DCTELEM *block, int n, int qscale);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
36 static void dct_unquantize_h263_c(MpegEncContext *s,
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
37 DCTELEM *block, int n, int qscale);
206
994aa8623443 (commit by michael)
arpi_esp
parents: 200
diff changeset
38 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
39 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale);
206
994aa8623443 (commit by michael)
arpi_esp
parents: 200
diff changeset
40
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
41 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c;
206
994aa8623443 (commit by michael)
arpi_esp
parents: 200
diff changeset
42 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
994aa8623443 (commit by michael)
arpi_esp
parents: 200
diff changeset
43
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 #define EDGE_WIDTH 16
986e461dc072 Initial revision
glantau
parents:
diff changeset
45
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 /* enable all paranoid tests for rounding, overflows, etc... */
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 //#define PARANOID
986e461dc072 Initial revision
glantau
parents:
diff changeset
48
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 //#define DEBUG
986e461dc072 Initial revision
glantau
parents:
diff changeset
50
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 /* for jpeg fast DCT */
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 #define CONST_BITS 14
986e461dc072 Initial revision
glantau
parents:
diff changeset
53
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 static const unsigned short aanscales[64] = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
55 /* precomputed values scaled up by 14 bits */
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
986e461dc072 Initial revision
glantau
parents:
diff changeset
58 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
986e461dc072 Initial revision
glantau
parents:
diff changeset
62 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
65
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 static UINT8 h263_chroma_roundtab[16] = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
67 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
69
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 /* default motion estimation */
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 int motion_estimation_method = ME_LOG;
986e461dc072 Initial revision
glantau
parents:
diff changeset
72
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
73 extern UINT8 zigzag_end[64];
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
74
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
75 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
78
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 if (av_fdct == jpeg_fdct_ifast) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 /* 16 <= qscale * quant_matrix[i] <= 7905 */
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
82 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
83 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
84 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
85
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
86 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
87 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)]));
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
88 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 for(i=0;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
92 So 16 <= qscale * quant_matrix[i] <= 7905
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
93 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
94 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 */
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
96 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
97 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
100 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
101
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 /* init common structure for both encoder and decoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
103 int MPV_common_init(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 int c_size, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
106 UINT8 *pict;
986e461dc072 Initial revision
glantau
parents:
diff changeset
107
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
108 if (s->out_format == FMT_H263)
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
109 s->dct_unquantize = dct_unquantize_h263_c;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
110 else
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
111 s->dct_unquantize = dct_unquantize_mpeg1_c;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
112
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
113 #ifdef HAVE_MMX
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
114 MPV_common_init_mmx(s);
8
1b4461b5a7fb Sync with mplayer's stuff
nickols_k
parents: 7
diff changeset
115 #endif
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 s->mb_width = (s->width + 15) / 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 s->mb_height = (s->height + 15) / 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
986e461dc072 Initial revision
glantau
parents:
diff changeset
119
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 int w, h, shift, pict_start;
986e461dc072 Initial revision
glantau
parents:
diff changeset
122
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 w = s->linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 h = s->mb_height * 16 + 2 * EDGE_WIDTH;
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 shift = (i == 0) ? 0 : 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 c_size = (w >> shift) * (h >> shift);
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
986e461dc072 Initial revision
glantau
parents:
diff changeset
128
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 pict = av_mallocz(c_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 if (pict == NULL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 s->last_picture_base[i] = pict;
986e461dc072 Initial revision
glantau
parents:
diff changeset
133 s->last_picture[i] = pict + pict_start;
986e461dc072 Initial revision
glantau
parents:
diff changeset
134
986e461dc072 Initial revision
glantau
parents:
diff changeset
135 pict = av_mallocz(c_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 if (pict == NULL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 s->next_picture_base[i] = pict;
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 s->next_picture[i] = pict + pict_start;
986e461dc072 Initial revision
glantau
parents:
diff changeset
140
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 if (s->has_b_frames) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 pict = av_mallocz(c_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
143 if (pict == NULL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 s->aux_picture_base[i] = pict;
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 s->aux_picture[i] = pict + pict_start;
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
149
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 if (s->out_format == FMT_H263) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 int size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
152 /* MV prediction */
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 s->motion_val = malloc(size * 2 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 if (s->motion_val == NULL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
157 memset(s->motion_val, 0, size * 2 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
159
986e461dc072 Initial revision
glantau
parents:
diff changeset
160 if (s->h263_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 int y_size, c_size, i, size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
162
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 /* dc values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
164
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 c_size = (s->mb_width + 2) * (s->mb_height + 2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 size = y_size + 2 * c_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 s->dc_val[0] = malloc(size * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 if (s->dc_val[0] == NULL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 s->dc_val[1] = s->dc_val[0] + y_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 s->dc_val[2] = s->dc_val[1] + c_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 for(i=0;i<size;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
174 s->dc_val[0][i] = 1024;
986e461dc072 Initial revision
glantau
parents:
diff changeset
175
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 /* ac values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
178 if (s->ac_val[0] == NULL)
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 goto fail;
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 s->ac_val[1] = s->ac_val[0] + y_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 s->ac_val[2] = s->ac_val[1] + c_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 /* cbp values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 s->coded_block = av_mallocz(y_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 if (!s->coded_block)
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 goto fail;
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
187
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
188 /* which mb is a intra block */
196
9ffa69cd4ed6 This fixes segfaults because of uninitialized s->mbintra_table variable - patch by Vladimir Dergachev <volodya@mindspring.com>
arpi_esp
parents: 194
diff changeset
189 s->mbintra_table = av_mallocz(s->mb_width * s->mb_height);
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
190 if (!s->mbintra_table)
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
191 goto fail;
196
9ffa69cd4ed6 This fixes segfaults because of uninitialized s->mbintra_table variable - patch by Vladimir Dergachev <volodya@mindspring.com>
arpi_esp
parents: 194
diff changeset
192 memset(s->mbintra_table, 1, s->mb_width * s->mb_height);
197
21abf1b20016 different fix, s->mbintra_table used only if h263_pred set. - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 196
diff changeset
193 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 /* default structure is frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 s->picture_structure = PICT_FRAME;
986e461dc072 Initial revision
glantau
parents:
diff changeset
196
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
197 /* init macroblock skip table */
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
198 if (!s->encoding) {
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
199 s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
200 if (!s->mbskip_table)
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
201 goto fail;
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
202 }
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
203
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 s->context_initialized = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
206 fail:
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 if (s->motion_val)
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 free(s->motion_val);
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 if (s->dc_val[0])
986e461dc072 Initial revision
glantau
parents:
diff changeset
210 free(s->dc_val[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 if (s->ac_val[0])
986e461dc072 Initial revision
glantau
parents:
diff changeset
212 free(s->ac_val[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 if (s->coded_block)
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 free(s->coded_block);
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
215 if (s->mbintra_table)
197
21abf1b20016 different fix, s->mbintra_table used only if h263_pred set. - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 196
diff changeset
216 free(s->mbintra_table);
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
217 if (s->mbskip_table)
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
218 free(s->mbskip_table);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 if (s->last_picture_base[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 free(s->last_picture_base[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 if (s->next_picture_base[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 free(s->next_picture_base[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 if (s->aux_picture_base[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 free(s->aux_picture_base[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
227 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
229
986e461dc072 Initial revision
glantau
parents:
diff changeset
230 /* init common structure for both encoder and decoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 void MPV_common_end(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 if (s->motion_val)
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 free(s->motion_val);
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 if (s->h263_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 free(s->dc_val[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 free(s->ac_val[0]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 free(s->coded_block);
197
21abf1b20016 different fix, s->mbintra_table used only if h263_pred set. - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 196
diff changeset
241 free(s->mbintra_table);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 }
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
243 if (s->mbskip_table)
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
244 free(s->mbskip_table);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 free(s->last_picture_base[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 free(s->next_picture_base[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 if (s->has_b_frames)
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 free(s->aux_picture_base[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
250 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 s->context_initialized = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
253
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 /* init video encoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 int MPV_encode_init(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 MpegEncContext *s = avctx->priv_data;
60
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
258 int i;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
259
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 s->bit_rate = avctx->bit_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 s->frame_rate = avctx->frame_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 s->width = avctx->width;
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 s->height = avctx->height;
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 s->gop_size = avctx->gop_size;
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
265 s->rtp_mode = avctx->rtp_mode;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
266 s->rtp_payload_size = avctx->rtp_payload_size;
194
27d1773552c9 mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 191
diff changeset
267 s->avctx = avctx;
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
268
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 if (s->gop_size <= 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 s->intra_only = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 s->gop_size = 12;
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 s->intra_only = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 s->full_search = motion_estimation_method;
986e461dc072 Initial revision
glantau
parents:
diff changeset
276
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
986e461dc072 Initial revision
glantau
parents:
diff changeset
278
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 switch(avctx->codec->id) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 case CODEC_ID_MPEG1VIDEO:
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 s->out_format = FMT_MPEG1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 case CODEC_ID_MJPEG:
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 s->out_format = FMT_MJPEG;
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 s->intra_only = 1; /* force intra only for jpeg */
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 if (mjpeg_init(s) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 case CODEC_ID_H263:
178
374f80a78f0d informal msg
arpi_esp
parents: 176
diff changeset
290 if (h263_get_picture_format(s->width, s->height) == 7){
374f80a78f0d informal msg
arpi_esp
parents: 176
diff changeset
291 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 return -1;
178
374f80a78f0d informal msg
arpi_esp
parents: 176
diff changeset
293 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 s->out_format = FMT_H263;
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 case CODEC_ID_H263P:
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 s->out_format = FMT_H263;
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
298 s->rtp_mode = 1;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
299 s->rtp_payload_size = 1200;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 s->h263_plus = 1;
78
92ed1c4f94fa Added support to Unrestricted Motion Vectors (UMV)
pulento
parents: 71
diff changeset
301 s->unrestricted_mv = 1;
79
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
302
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
303 /* These are just to be sure */
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
304 s->umvplus = 0;
82e579c37bc3 Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents: 78
diff changeset
305 s->umvplus_dec = 0;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 case CODEC_ID_RV10:
986e461dc072 Initial revision
glantau
parents:
diff changeset
308 s->out_format = FMT_H263;
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 s->h263_rv10 = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 break;
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 64
diff changeset
311 case CODEC_ID_MPEG4:
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
312 s->out_format = FMT_H263;
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 s->h263_pred = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 s->unrestricted_mv = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 case CODEC_ID_MSMPEG4:
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 s->out_format = FMT_H263;
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 s->h263_msmpeg4 = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 s->h263_pred = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 s->unrestricted_mv = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
321 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
323 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
325
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 if (s->out_format == FMT_H263)
986e461dc072 Initial revision
glantau
parents:
diff changeset
327 h263_encode_init_vlc(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
328
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
329 s->encoding = 1;
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
330
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
331 /* init */
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 if (MPV_common_init(s) < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 return -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
334
60
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
335 /* init default q matrix */
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
336 for(i=0;i<64;i++) {
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
337 s->intra_matrix[i] = default_intra_matrix[i];
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
338 s->non_intra_matrix[i] = default_non_intra_matrix[i];
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
339 }
35c1141e23d9 moved matrix init away from MPV_common_init()
glantau
parents: 57
diff changeset
340
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 /* rate control init */
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 rate_control_init(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
343
986e461dc072 Initial revision
glantau
parents:
diff changeset
344 s->picture_number = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 s->fake_picture_number = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 /* motion detector init */
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 s->f_code = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
348
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
351
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 int MPV_encode_end(AVCodecContext *avctx)
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 MpegEncContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
355
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 #ifdef STATS
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 print_stats();
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 MPV_common_end(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 if (s->out_format == FMT_MJPEG)
986e461dc072 Initial revision
glantau
parents:
diff changeset
361 mjpeg_close(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
364
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 /* draw the edges of width 'w' of an image of size width, height */
206
994aa8623443 (commit by michael)
arpi_esp
parents: 200
diff changeset
366 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 UINT8 *ptr, *last_line;
986e461dc072 Initial revision
glantau
parents:
diff changeset
369 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
370
986e461dc072 Initial revision
glantau
parents:
diff changeset
371 last_line = buf + (height - 1) * wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 for(i=0;i<w;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 /* top and bottom */
986e461dc072 Initial revision
glantau
parents:
diff changeset
374 memcpy(buf - (i + 1) * wrap, buf, width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 memcpy(last_line + (i + 1) * wrap, last_line, width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
377 /* left and right */
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 ptr = buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 for(i=0;i<height;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 memset(ptr - w, ptr[0], w);
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 memset(ptr + width, ptr[width-1], w);
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 ptr += wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
383 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 /* corners */
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 for(i=0;i<w;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
392
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 /* generic function for encode/decode called before a frame is coded/decoded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 void MPV_frame_start(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 UINT8 *tmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
398
46
931417475f5b fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents: 40
diff changeset
399 s->mb_skiped = 0;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 if (s->pict_type == B_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 s->current_picture[i] = s->aux_picture[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
403 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 /* swap next and last */
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 tmp = s->last_picture[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 s->last_picture[i] = s->next_picture[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 s->next_picture[i] = tmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
410 s->current_picture[i] = tmp;
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 }
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
414
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 /* generic function for encode/decode called after a frame has been coded/decoded */
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 void MPV_frame_end(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
418 /* draw edge for correct motion prediction if outside */
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
419 if (s->pict_type != B_TYPE && !s->intra_only) {
198
82ba367b1827 rv10 sig11 fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 197
diff changeset
420 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
176
9ce215ee9216 unrestricted MC fixed - thanks to Michael Niedermayer for idea
arpi_esp
parents: 162
diff changeset
421 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH);
9ce215ee9216 unrestricted MC fixed - thanks to Michael Niedermayer for idea
arpi_esp
parents: 162
diff changeset
422 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
9ce215ee9216 unrestricted MC fixed - thanks to Michael Niedermayer for idea
arpi_esp
parents: 162
diff changeset
423 draw_edges(s->current_picture[2], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2);
189
42552c1cf360 fix different UMV handling for mpeg4 vs. h263 - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 187
diff changeset
424 }else{
42552c1cf360 fix different UMV handling for mpeg4 vs. h263 - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 187
diff changeset
425 /* OpenDivx, but i dunno how to distinguish it from mpeg4 */
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
986e461dc072 Initial revision
glantau
parents:
diff changeset
427 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
189
42552c1cf360 fix different UMV handling for mpeg4 vs. h263 - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 187
diff changeset
429 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 }
207
6954f2830e4a (commit by michael)
arpi_esp
parents: 206
diff changeset
431 emms_c();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
433
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 int MPV_encode_picture(AVCodecContext *avctx,
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 unsigned char *buf, int buf_size, void *data)
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 MpegEncContext *s = avctx->priv_data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 AVPicture *pict = data;
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 int i, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
440
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 if (s->fixed_qscale)
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 s->qscale = avctx->quality;
986e461dc072 Initial revision
glantau
parents:
diff changeset
443
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
986e461dc072 Initial revision
glantau
parents:
diff changeset
445
986e461dc072 Initial revision
glantau
parents:
diff changeset
446 if (!s->intra_only) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 /* first picture of GOP is intra */
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 if ((s->picture_number % s->gop_size) == 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
449 s->pict_type = I_TYPE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
450 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 s->pict_type = P_TYPE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 s->pict_type = I_TYPE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 avctx->key_frame = (s->pict_type == I_TYPE);
986e461dc072 Initial revision
glantau
parents:
diff changeset
456
986e461dc072 Initial revision
glantau
parents:
diff changeset
457 MPV_frame_start(s);
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
458
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
459 for(i=0;i<3;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
460 UINT8 *src = pict->data[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
461 UINT8 *dest = s->current_picture[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 int src_wrap = pict->linesize[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 int dest_wrap = s->linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 int w = s->width;
986e461dc072 Initial revision
glantau
parents:
diff changeset
465 int h = s->height;
986e461dc072 Initial revision
glantau
parents:
diff changeset
466
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 if (i >= 1) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 dest_wrap >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 w >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 h >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
472
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
473 if(s->intra_only && dest_wrap==src_wrap){
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
474 s->current_picture[i] = pict->data[i];
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
475 }else {
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
476 for(j=0;j<h;j++) {
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
477 memcpy(dest, src, w);
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
478 dest += dest_wrap;
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
479 src += src_wrap;
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
480 }
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
481 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 s->new_picture[i] = s->current_picture[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
484
986e461dc072 Initial revision
glantau
parents:
diff changeset
485 encode_picture(s, s->picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
486
986e461dc072 Initial revision
glantau
parents:
diff changeset
487 MPV_frame_end(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 s->picture_number++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
489
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 if (s->out_format == FMT_MJPEG)
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 mjpeg_picture_trailer(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
492
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 flush_put_bits(&s->pb);
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 avctx->quality = s->qscale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
496 return s->pb.buf_ptr - s->pb.buf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
498
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 static inline int clip(int a, int amin, int amax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
500 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 if (a < amin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 return amin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 else if (a > amax)
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 return amax;
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 return a;
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
508
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 /* apply one mpeg motion vector to the three components */
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 static inline void mpeg_motion(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 int dest_offset,
986e461dc072 Initial revision
glantau
parents:
diff changeset
513 UINT8 **ref_picture, int src_offset,
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 int field_based, op_pixels_func *pix_op,
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 int motion_x, int motion_y, int h)
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 UINT8 *ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 int dxy, offset, mx, my, src_x, src_y, height, linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
519
986e461dc072 Initial revision
glantau
parents:
diff changeset
520 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 src_x = s->mb_x * 16 + (motion_x >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
523
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 /* WARNING: do no forget half pels */
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 height = s->height >> field_based;
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 src_x = clip(src_x, -16, s->width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 if (src_x == s->width)
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 dxy &= ~1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 src_y = clip(src_y, -16, height);
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 if (src_y == height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
531 dxy &= ~2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 linesize = s->linesize << field_based;
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 dest_y += dest_offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 pix_op[dxy](dest_y, ptr, linesize, h);
986e461dc072 Initial revision
glantau
parents:
diff changeset
536 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
986e461dc072 Initial revision
glantau
parents:
diff changeset
537
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 if (s->out_format == FMT_H263) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 dxy = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 if ((motion_x & 3) != 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 dxy |= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 if ((motion_y & 3) != 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
543 dxy |= 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 mx = motion_x >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 my = motion_y >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 mx = motion_x / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 my = motion_y / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 dxy = ((my & 1) << 1) | (mx & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 mx >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 my >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
553
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 src_x = s->mb_x * 8 + mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 src_y = s->mb_y * (8 >> field_based) + my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 src_x = clip(src_x, -8, s->width >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 if (src_x == (s->width >> 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
558 dxy &= ~1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 src_y = clip(src_y, -8, height >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 if (src_y == (height >> 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
561 dxy &= ~2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
562
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 ptr = ref_picture[1] + offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 ptr = ref_picture[2] + offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
569
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 static inline void MPV_motion(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 int dir, UINT8 **ref_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 op_pixels_func *pix_op)
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 int mb_x, mb_y, i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 UINT8 *ptr, *dest;
986e461dc072 Initial revision
glantau
parents:
diff changeset
578
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 mb_x = s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
580 mb_y = s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
581
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 switch(s->mv_type) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 case MV_TYPE_16X16:
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 ref_picture, 0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
586 0, pix_op,
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 s->mv[dir][0][0], s->mv[dir][0][1], 16);
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
589 case MV_TYPE_8X8:
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 for(i=0;i<4;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 motion_x = s->mv[dir][i][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
592 motion_y = s->mv[dir][i][1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
593
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
595 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
597
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 /* WARNING: do no forget half pels */
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 src_x = clip(src_x, -16, s->width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
600 if (src_x == s->width)
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 dxy &= ~1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
602 src_y = clip(src_y, -16, s->height);
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 if (src_y == s->height)
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 dxy &= ~2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
605
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 pix_op[dxy](dest, ptr, s->linesize, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 /* In case of 8X8, we construct a single chroma motion vector
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 with a special rounding */
986e461dc072 Initial revision
glantau
parents:
diff changeset
612 mx = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 my = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 for(i=0;i<4;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 mx += s->mv[dir][i][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 my += s->mv[dir][i][1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 if (mx >= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 mx = -mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
623 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 if (my >= 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 my = -my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 dxy = ((my & 1) << 1) | (mx & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 mx >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 my >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
633
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 src_x = mb_x * 8 + mx;
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 src_y = mb_y * 8 + my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
636 src_x = clip(src_x, -8, s->width/2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 if (src_x == s->width/2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 dxy &= ~1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
639 src_y = clip(src_y, -8, s->height/2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 if (src_y == s->height/2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 dxy &= ~2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
642
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 offset = (src_y * (s->linesize >> 1)) + src_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 ptr = ref_picture[1] + offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 ptr = ref_picture[2] + offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 case MV_TYPE_FIELD:
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 if (s->picture_structure == PICT_FRAME) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 /* top field */
986e461dc072 Initial revision
glantau
parents:
diff changeset
652 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 ref_picture, s->field_select[dir][0] ? s->linesize : 0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 1, pix_op,
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 s->mv[dir][0][0], s->mv[dir][0][1], 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 /* bottom field */
986e461dc072 Initial revision
glantau
parents:
diff changeset
657 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 ref_picture, s->field_select[dir][1] ? s->linesize : 0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 1, pix_op,
986e461dc072 Initial revision
glantau
parents:
diff changeset
660 s->mv[dir][1][0], s->mv[dir][1][1], 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
662
986e461dc072 Initial revision
glantau
parents:
diff changeset
663
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
668
986e461dc072 Initial revision
glantau
parents:
diff changeset
669
986e461dc072 Initial revision
glantau
parents:
diff changeset
670 /* put block[] to dest[] */
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 static inline void put_dct(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 DCTELEM *block, int i, UINT8 *dest, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
674 if (!s->mpeg2)
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
675 s->dct_unquantize(s, block, i, s->qscale);
19
82d4c9be9873 MMX/MMXEXT iDCT support, using external functions currently defined in libmpeg2
arpi_esp
parents: 18
diff changeset
676 ff_idct (block);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 put_pixels_clamped(block, dest, line_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
679
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 /* add block[] to dest[] */
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 static inline void add_dct(MpegEncContext *s,
986e461dc072 Initial revision
glantau
parents:
diff changeset
682 DCTELEM *block, int i, UINT8 *dest, int line_size)
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 if (s->block_last_index[i] >= 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
685 if (!s->mpeg2)
206
994aa8623443 (commit by michael)
arpi_esp
parents: 200
diff changeset
686 if(s->encoding || (!s->h263_msmpeg4))
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
687 s->dct_unquantize(s, block, i, s->qscale);
19
82d4c9be9873 MMX/MMXEXT iDCT support, using external functions currently defined in libmpeg2
arpi_esp
parents: 18
diff changeset
688 ff_idct (block);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
689 add_pixels_clamped(block, dest, line_size);
986e461dc072 Initial revision
glantau
parents:
diff changeset
690 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
691 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
692
986e461dc072 Initial revision
glantau
parents:
diff changeset
693 /* generic function called after a macroblock has been parsed by the
986e461dc072 Initial revision
glantau
parents:
diff changeset
694 decoder or after it has been encoded by the encoder.
986e461dc072 Initial revision
glantau
parents:
diff changeset
695
986e461dc072 Initial revision
glantau
parents:
diff changeset
696 Important variables used:
986e461dc072 Initial revision
glantau
parents:
diff changeset
697 s->mb_intra : true if intra macroblock
986e461dc072 Initial revision
glantau
parents:
diff changeset
698 s->mv_dir : motion vector direction
986e461dc072 Initial revision
glantau
parents:
diff changeset
699 s->mv_type : motion vector type
986e461dc072 Initial revision
glantau
parents:
diff changeset
700 s->mv : motion vector
986e461dc072 Initial revision
glantau
parents:
diff changeset
701 s->interlaced_dct : true if interlaced dct used (mpeg2)
986e461dc072 Initial revision
glantau
parents:
diff changeset
702 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
703 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
986e461dc072 Initial revision
glantau
parents:
diff changeset
704 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
705 int mb_x, mb_y, motion_x, motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
706 int dct_linesize, dct_offset;
986e461dc072 Initial revision
glantau
parents:
diff changeset
707 op_pixels_func *op_pix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
708
986e461dc072 Initial revision
glantau
parents:
diff changeset
709 mb_x = s->mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
710 mb_y = s->mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
711
108
1e4a4af694d1 exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents: 79
diff changeset
712 #ifdef FF_POSTPROCESS
1e4a4af694d1 exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents: 79
diff changeset
713 quant_store[mb_y][mb_x]=s->qscale;
1e4a4af694d1 exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents: 79
diff changeset
714 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale);
1e4a4af694d1 exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents: 79
diff changeset
715 #endif
1e4a4af694d1 exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents: 79
diff changeset
716
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
717 /* update DC predictors for P macroblocks */
986e461dc072 Initial revision
glantau
parents:
diff changeset
718 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
719 if (s->h263_pred) {
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
720 if(s->mbintra_table[mb_x + mb_y*s->mb_width])
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
721 {
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
722 int wrap, x, y, v;
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
723 s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
724
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
725 wrap = 2 * s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
726 v = 1024;
986e461dc072 Initial revision
glantau
parents:
diff changeset
727 x = 2 * mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
728 y = 2 * mb_y + 1;
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
729
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
730 s->dc_val[0][(x) + (y) * wrap] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
731 s->dc_val[0][(x + 1) + (y) * wrap] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
732 s->dc_val[0][(x) + (y + 1) * wrap] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
733 s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
734 /* ac pred */
986e461dc072 Initial revision
glantau
parents:
diff changeset
735 memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
736 memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
737 memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
738 memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
739 if (s->h263_msmpeg4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
740 s->coded_block[(x) + (y) * wrap] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
741 s->coded_block[(x + 1) + (y) * wrap] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
742 s->coded_block[(x) + (y + 1) * wrap] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
743 s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
744 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
745 /* chroma */
986e461dc072 Initial revision
glantau
parents:
diff changeset
746 wrap = s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
747 x = mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
748 y = mb_y + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
749 s->dc_val[1][(x) + (y) * wrap] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
750 s->dc_val[2][(x) + (y) * wrap] = v;
986e461dc072 Initial revision
glantau
parents:
diff changeset
751 /* ac pred */
986e461dc072 Initial revision
glantau
parents:
diff changeset
752 memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
986e461dc072 Initial revision
glantau
parents:
diff changeset
753 memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
754 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
755 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
756 s->last_dc[0] = 128 << s->intra_dc_precision;
986e461dc072 Initial revision
glantau
parents:
diff changeset
757 s->last_dc[1] = 128 << s->intra_dc_precision;
986e461dc072 Initial revision
glantau
parents:
diff changeset
758 s->last_dc[2] = 128 << s->intra_dc_precision;
986e461dc072 Initial revision
glantau
parents:
diff changeset
759 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
760 }
197
21abf1b20016 different fix, s->mbintra_table used only if h263_pred set. - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents: 196
diff changeset
761 else if (s->h263_pred)
191
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
762 s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
883f184537e6 AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents: 189
diff changeset
763
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
764 /* update motion predictor */
986e461dc072 Initial revision
glantau
parents:
diff changeset
765 if (s->out_format == FMT_H263) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
766 int x, y, wrap;
986e461dc072 Initial revision
glantau
parents:
diff changeset
767
986e461dc072 Initial revision
glantau
parents:
diff changeset
768 x = 2 * mb_x + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
769 y = 2 * mb_y + 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
770 wrap = 2 * s->mb_width + 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
771 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
772 motion_x = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
773 motion_y = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
774 goto motion_init;
986e461dc072 Initial revision
glantau
parents:
diff changeset
775 } else if (s->mv_type == MV_TYPE_16X16) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
776 motion_x = s->mv[0][0][0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
777 motion_y = s->mv[0][0][1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
778 motion_init:
986e461dc072 Initial revision
glantau
parents:
diff changeset
779 /* no update if 8X8 because it has been done during parsing */
986e461dc072 Initial revision
glantau
parents:
diff changeset
780 s->motion_val[(x) + (y) * wrap][0] = motion_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
781 s->motion_val[(x) + (y) * wrap][1] = motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
782 s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
783 s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
784 s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
785 s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
786 s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
787 s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
788 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
789 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
790
986e461dc072 Initial revision
glantau
parents:
diff changeset
791 if (!s->intra_only) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
792 UINT8 *dest_y, *dest_cb, *dest_cr;
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
793 UINT8 *mbskip_ptr;
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
794
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
795 /* avoid copy if macroblock skipped in last frame too */
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
796 if (!s->encoding && s->pict_type != B_TYPE) {
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
797 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
798 if (s->mb_skiped) {
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
799 s->mb_skiped = 0;
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
800 /* if previous was skipped too, then nothing to do ! */
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
801 if (*mbskip_ptr != 0)
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
802 goto the_end;
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
803 *mbskip_ptr = 1; /* indicate that this time we skiped it */
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
804 } else {
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
805 *mbskip_ptr = 0; /* not skipped */
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
806 }
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
807 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
808
986e461dc072 Initial revision
glantau
parents:
diff changeset
809 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
810 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
811 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
812
986e461dc072 Initial revision
glantau
parents:
diff changeset
813 if (s->interlaced_dct) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
814 dct_linesize = s->linesize * 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
815 dct_offset = s->linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
816 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
817 dct_linesize = s->linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
818 dct_offset = s->linesize * 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
819 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
820
986e461dc072 Initial revision
glantau
parents:
diff changeset
821 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
822 /* motion handling */
986e461dc072 Initial revision
glantau
parents:
diff changeset
823 if (!s->no_rounding)
986e461dc072 Initial revision
glantau
parents:
diff changeset
824 op_pix = put_pixels_tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
825 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
826 op_pix = put_no_rnd_pixels_tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
827
986e461dc072 Initial revision
glantau
parents:
diff changeset
828 if (s->mv_dir & MV_DIR_FORWARD) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
829 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
986e461dc072 Initial revision
glantau
parents:
diff changeset
830 if (!s->no_rounding)
986e461dc072 Initial revision
glantau
parents:
diff changeset
831 op_pix = avg_pixels_tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
832 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
833 op_pix = avg_no_rnd_pixels_tab;
986e461dc072 Initial revision
glantau
parents:
diff changeset
834 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
835 if (s->mv_dir & MV_DIR_BACKWARD) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
836 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
986e461dc072 Initial revision
glantau
parents:
diff changeset
837 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
838
986e461dc072 Initial revision
glantau
parents:
diff changeset
839 /* add dct residue */
986e461dc072 Initial revision
glantau
parents:
diff changeset
840 add_dct(s, block[0], 0, dest_y, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
841 add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
842 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
843 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
844
57
4efb718f4dc6 fixed mpeg2 interlaced DCT
glantau
parents: 46
diff changeset
845 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
4efb718f4dc6 fixed mpeg2 interlaced DCT
glantau
parents: 46
diff changeset
846 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
847 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
848 /* dct only in intra block */
986e461dc072 Initial revision
glantau
parents:
diff changeset
849 put_dct(s, block[0], 0, dest_y, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
850 put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
851 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
852 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
986e461dc072 Initial revision
glantau
parents:
diff changeset
853
57
4efb718f4dc6 fixed mpeg2 interlaced DCT
glantau
parents: 46
diff changeset
854 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
4efb718f4dc6 fixed mpeg2 interlaced DCT
glantau
parents: 46
diff changeset
855 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
856 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
857 }
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
858 the_end:
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
859 emms_c();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
860 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
861
986e461dc072 Initial revision
glantau
parents:
diff changeset
862 static void encode_picture(MpegEncContext *s, int picture_number)
986e461dc072 Initial revision
glantau
parents:
diff changeset
863 {
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
864 int mb_x, mb_y, wrap, last_gob;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
865 UINT8 *ptr;
986e461dc072 Initial revision
glantau
parents:
diff changeset
866 int i, motion_x, motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
867
986e461dc072 Initial revision
glantau
parents:
diff changeset
868 s->picture_number = picture_number;
986e461dc072 Initial revision
glantau
parents:
diff changeset
869 if (!s->fixed_qscale)
986e461dc072 Initial revision
glantau
parents:
diff changeset
870 s->qscale = rate_estimate_qscale(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
871
986e461dc072 Initial revision
glantau
parents:
diff changeset
872 /* precompute matrix */
986e461dc072 Initial revision
glantau
parents:
diff changeset
873 if (s->out_format == FMT_MJPEG) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
874 /* for mjpeg, we do include qscale in the matrix */
986e461dc072 Initial revision
glantau
parents:
diff changeset
875 s->intra_matrix[0] = default_intra_matrix[0];
986e461dc072 Initial revision
glantau
parents:
diff changeset
876 for(i=1;i<64;i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
877 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
878 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
879 } else {
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
880 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
881 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
882 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
883
986e461dc072 Initial revision
glantau
parents:
diff changeset
884 switch(s->out_format) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
885 case FMT_MJPEG:
986e461dc072 Initial revision
glantau
parents:
diff changeset
886 mjpeg_picture_header(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
887 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
888 case FMT_H263:
986e461dc072 Initial revision
glantau
parents:
diff changeset
889 if (s->h263_msmpeg4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
890 msmpeg4_encode_picture_header(s, picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
891 else if (s->h263_pred)
986e461dc072 Initial revision
glantau
parents:
diff changeset
892 mpeg4_encode_picture_header(s, picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
893 else if (s->h263_rv10)
986e461dc072 Initial revision
glantau
parents:
diff changeset
894 rv10_encode_picture_header(s, picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
895 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
896 h263_encode_picture_header(s, picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
897 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
898 case FMT_MPEG1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
899 mpeg1_encode_picture_header(s, picture_number);
986e461dc072 Initial revision
glantau
parents:
diff changeset
900 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
901 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
902
986e461dc072 Initial revision
glantau
parents:
diff changeset
903 /* init last dc values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
904 /* note: quant matrix value (8) is implied here */
986e461dc072 Initial revision
glantau
parents:
diff changeset
905 s->last_dc[0] = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
906 s->last_dc[1] = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
907 s->last_dc[2] = 128;
986e461dc072 Initial revision
glantau
parents:
diff changeset
908 s->mb_incr = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
909 s->last_mv[0][0][0] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
910 s->last_mv[0][0][1] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
911 s->mv_type = MV_TYPE_16X16;
986e461dc072 Initial revision
glantau
parents:
diff changeset
912 s->mv_dir = MV_DIR_FORWARD;
986e461dc072 Initial revision
glantau
parents:
diff changeset
913
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
914 /* Get the GOB height based on picture height */
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
915 if (s->out_format == FMT_H263 && s->h263_plus) {
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
916 if (s->height <= 400)
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
917 s->gob_index = 1;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
918 else if (s->height <= 800)
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
919 s->gob_index = 2;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
920 else
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
921 s->gob_index = 4;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
922 }
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
923
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
924 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
925 /* Put GOB header based on RTP MTU */
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
926 if (!mb_y) {
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
927 s->ptr_lastgob = s->pb.buf_ptr;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
928 s->ptr_last_mb_line = s->pb.buf_ptr;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
929 } else if (s->out_format == FMT_H263 && s->h263_plus) {
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
930 last_gob = h263_encode_gob_header(s, mb_y);
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
931 if (last_gob) {
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
932 //fprintf(stderr,"\nLast GOB size: %d", last_gob);
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
933 s->first_gob_line = 1;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
934 } else
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
935 s->first_gob_line = 0;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
936 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
937 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
938
986e461dc072 Initial revision
glantau
parents:
diff changeset
939 s->mb_x = mb_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
940 s->mb_y = mb_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
941
986e461dc072 Initial revision
glantau
parents:
diff changeset
942 /* compute motion vector and macro block type (intra or non intra) */
986e461dc072 Initial revision
glantau
parents:
diff changeset
943 motion_x = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
944 motion_y = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
945 if (s->pict_type == P_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
946 s->mb_intra = estimate_motion(s, mb_x, mb_y,
986e461dc072 Initial revision
glantau
parents:
diff changeset
947 &motion_x,
986e461dc072 Initial revision
glantau
parents:
diff changeset
948 &motion_y);
986e461dc072 Initial revision
glantau
parents:
diff changeset
949 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
950 s->mb_intra = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
951 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
952
986e461dc072 Initial revision
glantau
parents:
diff changeset
953 /* get the pixels */
986e461dc072 Initial revision
glantau
parents:
diff changeset
954 wrap = s->linesize;
986e461dc072 Initial revision
glantau
parents:
diff changeset
955 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
956 get_pixels(s->block[0], ptr, wrap);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
957 get_pixels(s->block[1], ptr + 8, wrap);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
958 get_pixels(s->block[2], ptr + 8 * wrap, wrap);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
959 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
960 wrap = s->linesize >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
961 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
962 get_pixels(s->block[4], ptr, wrap);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
963
986e461dc072 Initial revision
glantau
parents:
diff changeset
964 wrap = s->linesize >> 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
965 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
966 get_pixels(s->block[5], ptr, wrap);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
967
986e461dc072 Initial revision
glantau
parents:
diff changeset
968 /* subtract previous frame if non intra */
986e461dc072 Initial revision
glantau
parents:
diff changeset
969 if (!s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
970 int dxy, offset, mx, my;
986e461dc072 Initial revision
glantau
parents:
diff changeset
971
986e461dc072 Initial revision
glantau
parents:
diff changeset
972 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
973 ptr = s->last_picture[0] +
986e461dc072 Initial revision
glantau
parents:
diff changeset
974 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
986e461dc072 Initial revision
glantau
parents:
diff changeset
975 (mb_x * 16 + (motion_x >> 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
976
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
977 sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
978 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
979 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
980 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
981
986e461dc072 Initial revision
glantau
parents:
diff changeset
982 if (s->out_format == FMT_H263) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
983 /* special rounding for h263 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
984 dxy = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
985 if ((motion_x & 3) != 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
986 dxy |= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
987 if ((motion_y & 3) != 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
988 dxy |= 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
989 mx = motion_x >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
990 my = motion_y >> 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
991 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
992 mx = motion_x / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
993 my = motion_y / 2;
986e461dc072 Initial revision
glantau
parents:
diff changeset
994 dxy = ((my & 1) << 1) | (mx & 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
995 mx >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
996 my >>= 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
997 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
998 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
986e461dc072 Initial revision
glantau
parents:
diff changeset
999 ptr = s->last_picture[1] + offset;
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1000 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1001 ptr = s->last_picture[2] + offset;
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1002 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1003 }
7
1d3ac9654178 added skip macroblock optimization (big perf win on black regions for example)
glantau
parents: 0
diff changeset
1004 emms_c();
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1005
986e461dc072 Initial revision
glantau
parents:
diff changeset
1006 /* DCT & quantize */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1007 if (s->h263_msmpeg4) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1008 msmpeg4_dc_scale(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1009 } else if (s->h263_pred) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1010 h263_dc_scale(s);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1011 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1012 /* default quantization values */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1013 s->y_dc_scale = 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1014 s->c_dc_scale = 8;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1015 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1016 for(i=0;i<6;i++) {
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
1017 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1018 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1019
986e461dc072 Initial revision
glantau
parents:
diff changeset
1020 /* huffman encode */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1021 switch(s->out_format) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1022 case FMT_MPEG1:
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1023 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1024 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1025 case FMT_H263:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1026 if (s->h263_msmpeg4)
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1027 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1028 else
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1029 h263_encode_mb(s, s->block, motion_x, motion_y);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1030 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1031 case FMT_MJPEG:
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1032 mjpeg_encode_mb(s, s->block);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1033 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1034 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1035
986e461dc072 Initial revision
glantau
parents:
diff changeset
1036 /* decompress blocks so that we keep the state of the decoder */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1037 s->mv[0][0][0] = motion_x;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1038 s->mv[0][0][1] = motion_y;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1039
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1040 MPV_decode_mb(s, s->block);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1041 }
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1042 /* Obtain average MB line size for RTP */
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1043 if (!mb_y)
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1044 s->mb_line_avgsize = s->pb.buf_ptr - s->ptr_last_mb_line;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1045 else
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1046 s->mb_line_avgsize = (s->mb_line_avgsize + s->pb.buf_ptr - s->ptr_last_mb_line) >> 1;
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1047 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y,
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1048 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1049 s->ptr_last_mb_line = s->pb.buf_ptr;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1050 }
208
2eb04d6be309 (commit by michael)
arpi_esp
parents: 207
diff changeset
1051
2eb04d6be309 (commit by michael)
arpi_esp
parents: 207
diff changeset
1052 if (s->h263_msmpeg4)
2eb04d6be309 (commit by michael)
arpi_esp
parents: 207
diff changeset
1053 msmpeg4_encode_ext_header(s);
2eb04d6be309 (commit by michael)
arpi_esp
parents: 207
diff changeset
1054
162
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1055 //if (s->gob_number)
de80712db90b - Preliminary RTP friendly mode for H.263.
pulento
parents: 108
diff changeset
1056 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1057 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1058
220
0b234715e205 (commit by michael)
arpi_esp
parents: 217
diff changeset
1059 static int dct_quantize_c(MpegEncContext *s,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1060 DCTELEM *block, int n,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1061 int qscale)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1062 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1063 int i, j, level, last_non_zero, q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1064 const int *qmat;
216
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1065 int minLevel, maxLevel;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1066
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1067 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1068 /* mpeg4 */
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1069 minLevel= -2048;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1070 maxLevel= 2047;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1071 }else if(s->out_format==FMT_MPEG1){
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1072 /* mpeg1 */
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1073 minLevel= -255;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1074 maxLevel= 255;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1075 }else{
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1076 /* h263 / msmpeg4 */
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1077 minLevel= -128;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1078 maxLevel= 127;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1079 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1080
986e461dc072 Initial revision
glantau
parents:
diff changeset
1081 av_fdct (block);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1082
64
5aa6292a1660 win32 fixes
glantau
parents: 60
diff changeset
1083 /* we need this permutation so that we correct the IDCT
5aa6292a1660 win32 fixes
glantau
parents: 60
diff changeset
1084 permutation. will be moved into DCT code */
5aa6292a1660 win32 fixes
glantau
parents: 60
diff changeset
1085 block_permute(block);
5aa6292a1660 win32 fixes
glantau
parents: 60
diff changeset
1086
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1087 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1088 if (n < 4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1089 q = s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1090 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1091 q = s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1092 q = q << 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1093
986e461dc072 Initial revision
glantau
parents:
diff changeset
1094 /* note: block[0] is assumed to be positive */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1095 block[0] = (block[0] + (q >> 1)) / q;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1096 i = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1097 last_non_zero = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1098 if (s->out_format == FMT_H263) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1099 qmat = s->q_non_intra_matrix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1100 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1101 qmat = s->q_intra_matrix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1102 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1103 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1104 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1105 last_non_zero = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1106 qmat = s->q_non_intra_matrix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1107 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1108
986e461dc072 Initial revision
glantau
parents:
diff changeset
1109 for(;i<64;i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1110 j = zigzag_direct[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1111 level = block[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1112 level = level * qmat[j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
1113 #ifdef PARANOID
986e461dc072 Initial revision
glantau
parents:
diff changeset
1114 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1115 static int count = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1116 int level1, level2, qmat1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1117 double val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1118 if (qmat == s->q_non_intra_matrix) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1119 qmat1 = default_non_intra_matrix[j] * s->qscale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1120 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1121 qmat1 = default_intra_matrix[j] * s->qscale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1122 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1123 if (av_fdct != jpeg_fdct_ifast)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1124 val = ((double)block[j] * 8.0) / (double)qmat1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1125 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1126 val = ((double)block[j] * 8.0 * 2048.0) /
986e461dc072 Initial revision
glantau
parents:
diff changeset
1127 ((double)qmat1 * aanscales[j]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1128 level1 = (int)val;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1129 level2 = level / (1 << (QMAT_SHIFT - 3));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1130 if (level1 != level2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1131 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1132 count, level2, level1, block[j], qmat1, qmat[j],
986e461dc072 Initial revision
glantau
parents:
diff changeset
1133 val);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1134 count++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1135 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1136
986e461dc072 Initial revision
glantau
parents:
diff changeset
1137 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1138 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
1139 /* XXX: slight error for the low range. Test should be equivalent to
986e461dc072 Initial revision
glantau
parents:
diff changeset
1140 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
986e461dc072 Initial revision
glantau
parents:
diff changeset
1141 (QMAT_SHIFT - 3)))
986e461dc072 Initial revision
glantau
parents:
diff changeset
1142 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1143 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
986e461dc072 Initial revision
glantau
parents:
diff changeset
1144 level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1145 level = level / (1 << (QMAT_SHIFT - 3));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1146 /* XXX: currently, this code is not optimal. the range should be:
986e461dc072 Initial revision
glantau
parents:
diff changeset
1147 mpeg1: -255..255
986e461dc072 Initial revision
glantau
parents:
diff changeset
1148 mpeg2: -2048..2047
986e461dc072 Initial revision
glantau
parents:
diff changeset
1149 h263: -128..127
986e461dc072 Initial revision
glantau
parents:
diff changeset
1150 mpeg4: -2048..2047
986e461dc072 Initial revision
glantau
parents:
diff changeset
1151 */
216
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1152 if (level > maxLevel)
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1153 level = maxLevel;
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1154 else if (level < minLevel)
f8802ed5332b (commit by michael)
arpi_esp
parents: 208
diff changeset
1155 level = minLevel;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1156
986e461dc072 Initial revision
glantau
parents:
diff changeset
1157 block[j] = level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1158 last_non_zero = i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1159 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1160 block[j] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1161 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1162 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1163 return last_non_zero;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1164 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1165
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1166 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1167 DCTELEM *block, int n, int qscale)
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1168 {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1169 int i, level, nCoeffs;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1170 const UINT16 *quant_matrix;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1171
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1172 if(s->alternate_scan) nCoeffs= 64;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1173 else nCoeffs= s->block_last_index[n]+1;
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1174
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1175 if (s->mb_intra) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1176 if (n < 4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1177 block[0] = block[0] * s->y_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1178 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
1179 block[0] = block[0] * s->c_dc_scale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1180 /* XXX: only mpeg1 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1181 quant_matrix = s->intra_matrix;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1182 for(i=1;i<nCoeffs;i++) {
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1183 int j= zigzag_direct[i];
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1184 level = block[j];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1185 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1186 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1187 level = -level;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1188 level = (int)(level * qscale * quant_matrix[j]) >> 3;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1189 level = (level - 1) | 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1190 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1191 } else {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1192 level = (int)(level * qscale * quant_matrix[j]) >> 3;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1193 level = (level - 1) | 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1194 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1195 #ifdef PARANOID
986e461dc072 Initial revision
glantau
parents:
diff changeset
1196 if (level < -2048 || level > 2047)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1197 fprintf(stderr, "unquant error %d %d\n", i, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1198 #endif
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1199 block[j] = level;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1200 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1201 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1202 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1203 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1204 quant_matrix = s->non_intra_matrix;
217
de372d04039c (commit by michael)
arpi_esp
parents: 216
diff changeset
1205 for(;i<nCoeffs;i++) {
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1206 int j= zigzag_direct[i];
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1207 level = block[j];
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1208 if (level) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1209 if (level < 0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1210 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1211 level = (((level << 1) + 1) * qscale *
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1212 ((int) (quant_matrix[j]))) >> 4;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1213 level = (level - 1) | 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1214 level = -level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1215 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1216 level = (((level << 1) + 1) * qscale *
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1217 ((int) (quant_matrix[j]))) >> 4;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1218 level = (level - 1) | 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1219 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1220 #ifdef PARANOID
986e461dc072 Initial revision
glantau
parents:
diff changeset
1221 if (level < -2048 || level > 2047)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1222 fprintf(stderr, "unquant error %d %d\n", i, level);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1223 #endif
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1224 block[j] = level;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1225 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1226 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1227 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1228 }
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1229
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1230 static void dct_unquantize_h263_c(MpegEncContext *s,
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1231 DCTELEM *block, int n, int qscale)
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1232 {
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1233 int i, level, qmul, qadd;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1234 int nCoeffs;
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1235
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1236 if (s->mb_intra) {
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1237 if (n < 4)
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1238 block[0] = block[0] * s->y_dc_scale;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1239 else
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1240 block[0] = block[0] * s->c_dc_scale;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1241 i = 1;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1242 nCoeffs= 64; //does not allways use zigzag table
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1243 } else {
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1244 i = 0;
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1245 nCoeffs= zigzag_end[ s->block_last_index[n] ];
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1246 }
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1247
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1248 qmul = s->qscale << 1;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1249 qadd = (s->qscale - 1) | 1;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1250
200
6ab301aaa652 (commit by michael)
arpi_esp
parents: 198
diff changeset
1251 for(;i<nCoeffs;i++) {
13
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1252 level = block[i];
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1253 if (level) {
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1254 if (level < 0) {
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1255 level = level * qmul - qadd;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1256 } else {
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1257 level = level * qmul + qadd;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1258 }
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1259 #ifdef PARANOID
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1260 if (level < -2048 || level > 2047)
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1261 fprintf(stderr, "unquant error %d %d\n", i, level);
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1262 #endif
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1263 block[i] = level;
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1264 }
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1265 }
174ef88f619a use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents: 8
diff changeset
1266 }
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1267
986e461dc072 Initial revision
glantau
parents:
diff changeset
1268 /* rate control */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1269
986e461dc072 Initial revision
glantau
parents:
diff changeset
1270 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1271 #define I_FRAME_SIZE_RATIO 3.0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1272 #define QSCALE_K 20
986e461dc072 Initial revision
glantau
parents:
diff changeset
1273
986e461dc072 Initial revision
glantau
parents:
diff changeset
1274 static void rate_control_init(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1275 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1276 s->wanted_bits = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1277
986e461dc072 Initial revision
glantau
parents:
diff changeset
1278 if (s->intra_only) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1279 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1280 s->P_frame_bits = s->I_frame_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1281 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1282 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
986e461dc072 Initial revision
glantau
parents:
diff changeset
1283 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
986e461dc072 Initial revision
glantau
parents:
diff changeset
1284 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1285 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1286
986e461dc072 Initial revision
glantau
parents:
diff changeset
1287 #if defined(DEBUG)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1288 printf("I_frame_size=%d P_frame_size=%d\n",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1289 s->I_frame_bits, s->P_frame_bits);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1290 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
1291 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1292
986e461dc072 Initial revision
glantau
parents:
diff changeset
1293
986e461dc072 Initial revision
glantau
parents:
diff changeset
1294 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
1295 * This heuristic is rather poor, but at least we do not have to
986e461dc072 Initial revision
glantau
parents:
diff changeset
1296 * change the qscale at every macroblock.
986e461dc072 Initial revision
glantau
parents:
diff changeset
1297 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1298 static int rate_estimate_qscale(MpegEncContext *s)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1299 {
187
3f3b14d3a23d qscale estimate fix, diff extended to 64bit wide. patch by Stephen Davies <steve@daviesfam.org>
arpi_esp
parents: 178
diff changeset
1300 INT64 diff, total_bits = s->total_bits;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1301 float q;
187
3f3b14d3a23d qscale estimate fix, diff extended to 64bit wide. patch by Stephen Davies <steve@daviesfam.org>
arpi_esp
parents: 178
diff changeset
1302 int qscale, qmin;
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1303
986e461dc072 Initial revision
glantau
parents:
diff changeset
1304 if (s->pict_type == I_TYPE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1305 s->wanted_bits += s->I_frame_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1306 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1307 s->wanted_bits += s->P_frame_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1308 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1309 diff = s->wanted_bits - total_bits;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1310 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1311 /* adjust for I frame */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1312 if (s->pict_type == I_TYPE && !s->intra_only) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1313 q /= I_FRAME_SIZE_RATIO;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1314 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1315
986e461dc072 Initial revision
glantau
parents:
diff changeset
1316 /* using a too small Q scale leeds to problems in mpeg1 and h263
986e461dc072 Initial revision
glantau
parents:
diff changeset
1317 because AC coefficients are clamped to 255 or 127 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
1318 qmin = 3;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1319 if (q < qmin)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1320 q = qmin;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1321 else if (q > 31)
986e461dc072 Initial revision
glantau
parents:
diff changeset
1322 q = 31;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1323 qscale = (int)(q + 0.5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1324 #if defined(DEBUG)
64
5aa6292a1660 win32 fixes
glantau
parents: 60
diff changeset
1325 printf("%d: total=%0.0f br=%0.1f diff=%d qest=%0.1f\n",
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1326 s->picture_number,
64
5aa6292a1660 win32 fixes
glantau
parents: 60
diff changeset
1327 (double)total_bits,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1328 (float)s->frame_rate / FRAME_RATE_BASE *
986e461dc072 Initial revision
glantau
parents:
diff changeset
1329 total_bits / s->picture_number,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1330 diff, q);
986e461dc072 Initial revision
glantau
parents:
diff changeset
1331 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
1332 return qscale;
986e461dc072 Initial revision
glantau
parents:
diff changeset
1333 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
1334
986e461dc072 Initial revision
glantau
parents:
diff changeset
1335 AVCodec mpeg1video_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1336 "mpeg1video",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1337 CODEC_TYPE_VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1338 CODEC_ID_MPEG1VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1339 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1340 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1341 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1342 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1343 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
1344
986e461dc072 Initial revision
glantau
parents:
diff changeset
1345 AVCodec h263_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1346 "h263",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1347 CODEC_TYPE_VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1348 CODEC_ID_H263,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1349 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1350 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1351 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1352 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1353 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
1354
986e461dc072 Initial revision
glantau
parents:
diff changeset
1355 AVCodec h263p_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1356 "h263p",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1357 CODEC_TYPE_VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1358 CODEC_ID_H263P,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1359 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1360 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1361 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1362 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1363 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
1364
986e461dc072 Initial revision
glantau
parents:
diff changeset
1365 AVCodec rv10_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1366 "rv10",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1367 CODEC_TYPE_VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1368 CODEC_ID_RV10,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1369 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1370 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1371 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1372 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1373 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
1374
986e461dc072 Initial revision
glantau
parents:
diff changeset
1375 AVCodec mjpeg_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1376 "mjpeg",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1377 CODEC_TYPE_VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1378 CODEC_ID_MJPEG,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1379 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1380 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1381 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1382 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1383 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
1384
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 64
diff changeset
1385 AVCodec mpeg4_encoder = {
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 64
diff changeset
1386 "mpeg4",
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1387 CODEC_TYPE_VIDEO,
71
79be2c581c01 changed opendivx to mpeg4
glantau
parents: 64
diff changeset
1388 CODEC_ID_MPEG4,
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1389 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1390 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1391 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1392 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1393 };
986e461dc072 Initial revision
glantau
parents:
diff changeset
1394
986e461dc072 Initial revision
glantau
parents:
diff changeset
1395 AVCodec msmpeg4_encoder = {
986e461dc072 Initial revision
glantau
parents:
diff changeset
1396 "msmpeg4",
986e461dc072 Initial revision
glantau
parents:
diff changeset
1397 CODEC_TYPE_VIDEO,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1398 CODEC_ID_MSMPEG4,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1399 sizeof(MpegEncContext),
986e461dc072 Initial revision
glantau
parents:
diff changeset
1400 MPV_encode_init,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1401 MPV_encode_picture,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1402 MPV_encode_end,
986e461dc072 Initial revision
glantau
parents:
diff changeset
1403 };