Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * The simplest mpeg encoder (well, it was the simplest!) | |
406792e7 | 3 | * Copyright (c) 2000,2001 Fabrice Bellard |
8f2ab833 | 4 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
de6d9b64 | 5 | * |
7b94177e DB |
6 | * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> |
7 | * | |
2912e87a | 8 | * This file is part of Libav. |
b78e7197 | 9 | * |
2912e87a | 10 | * Libav is free software; you can redistribute it and/or |
ff4ec49e FB |
11 | * modify it under the terms of the GNU Lesser General Public |
12 | * License as published by the Free Software Foundation; either | |
b78e7197 | 13 | * version 2.1 of the License, or (at your option) any later version. |
de6d9b64 | 14 | * |
2912e87a | 15 | * Libav is distributed in the hope that it will be useful, |
de6d9b64 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ff4ec49e FB |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * Lesser General Public License for more details. | |
de6d9b64 | 19 | * |
ff4ec49e | 20 | * You should have received a copy of the GNU Lesser General Public |
2912e87a | 21 | * License along with Libav; if not, write to the Free Software |
5509bffa | 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 | 23 | */ |
115329f1 | 24 | |
983e3246 | 25 | /** |
ba87f080 | 26 | * @file |
983e3246 | 27 | * The simplest mpeg encoder (well, it was the simplest!). |
115329f1 DB |
28 | */ |
29 | ||
6fee1b90 | 30 | #include "libavutil/attributes.h" |
759001c5 | 31 | #include "libavutil/avassert.h" |
737eb597 | 32 | #include "libavutil/imgutils.h" |
19e30a58 | 33 | #include "libavutil/internal.h" |
de6d9b64 FB |
34 | #include "avcodec.h" |
35 | #include "dsputil.h" | |
603a5f04 | 36 | #include "internal.h" |
9734b8ba | 37 | #include "mathops.h" |
de6d9b64 | 38 | #include "mpegvideo.h" |
d9c9259f | 39 | #include "mjpegenc.h" |
15025553 | 40 | #include "msmpeg4.h" |
4440bd0d | 41 | #include "xvmc_internal.h" |
6a9c8594 | 42 | #include "thread.h" |
e96682e6 | 43 | #include <limits.h> |
de6d9b64 | 44 | |
115329f1 | 45 | static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, |
88bd7fdc | 46 | int16_t *block, int n, int qscale); |
115329f1 | 47 | static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, |
88bd7fdc | 48 | int16_t *block, int n, int qscale); |
d50635cd | 49 | static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, |
88bd7fdc | 50 | int16_t *block, int n, int qscale); |
e27b6e62 | 51 | static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s, |
88bd7fdc | 52 | int16_t *block, int n, int qscale); |
d50635cd | 53 | static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, |
88bd7fdc | 54 | int16_t *block, int n, int qscale); |
115329f1 | 55 | static void dct_unquantize_h263_intra_c(MpegEncContext *s, |
88bd7fdc | 56 | int16_t *block, int n, int qscale); |
115329f1 | 57 | static void dct_unquantize_h263_inter_c(MpegEncContext *s, |
88bd7fdc | 58 | int16_t *block, int n, int qscale); |
3d9fccbf | 59 | |
363114e8 KT |
60 | static const uint8_t ff_default_chroma_qscale_table[32] = { |
61 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
62 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, | |
63 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 | |
332f9ac4 MN |
64 | }; |
65 | ||
363114e8 KT |
66 | const uint8_t ff_mpeg1_dc_scale_table[128] = { |
67 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
68 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
69 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
70 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
71 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
72 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
73 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
74 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
75 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, | |
6cbe71bd AJ |
76 | }; |
77 | ||
363114e8 KT |
78 | static const uint8_t mpeg2_dc_scale_table1[128] = { |
79 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
80 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
81 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
82 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
83 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
84 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
85 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
86 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
87 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
6506c3d2 MN |
88 | }; |
89 | ||
363114e8 KT |
90 | static const uint8_t mpeg2_dc_scale_table2[128] = { |
91 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
92 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
93 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
94 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
95 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
96 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
97 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
98 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
99 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
6506c3d2 MN |
100 | }; |
101 | ||
363114e8 KT |
102 | static const uint8_t mpeg2_dc_scale_table3[128] = { |
103 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
104 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
105 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
106 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
107 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
108 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
109 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
110 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
111 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
6506c3d2 MN |
112 | }; |
113 | ||
363114e8 | 114 | const uint8_t *const ff_mpeg2_dc_scale_table[4] = { |
6506c3d2 MN |
115 | ff_mpeg1_dc_scale_table, |
116 | mpeg2_dc_scale_table1, | |
117 | mpeg2_dc_scale_table2, | |
118 | mpeg2_dc_scale_table3, | |
119 | }; | |
120 | ||
716d413c AK |
121 | const enum AVPixelFormat ff_pixfmt_list_420[] = { |
122 | AV_PIX_FMT_YUV420P, | |
123 | AV_PIX_FMT_NONE | |
044f0296 GB |
124 | }; |
125 | ||
54974c62 AK |
126 | static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type, |
127 | int (*mv)[2][4][2], | |
128 | int mb_x, int mb_y, int mb_intra, int mb_skipped) | |
129 | { | |
130 | MpegEncContext *s = opaque; | |
131 | ||
132 | s->mv_dir = mv_dir; | |
133 | s->mv_type = mv_type; | |
134 | s->mb_intra = mb_intra; | |
135 | s->mb_skipped = mb_skipped; | |
136 | s->mb_x = mb_x; | |
137 | s->mb_y = mb_y; | |
138 | memcpy(s->mv, mv, sizeof(*mv)); | |
139 | ||
140 | ff_init_block_index(s); | |
141 | ff_update_block_index(s); | |
142 | ||
143 | s->dsp.clear_blocks(s->block[0]); | |
144 | ||
145 | s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16; | |
146 | s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift); | |
147 | s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift); | |
148 | ||
149 | assert(ref == 0); | |
150 | ff_MPV_decode_mb(s, s->block); | |
151 | } | |
152 | ||
defdfc9a | 153 | /* init common dct for both encoder and decoder */ |
5ef251e5 | 154 | av_cold int ff_dct_common_init(MpegEncContext *s) |
de6d9b64 | 155 | { |
9cf0841e | 156 | ff_dsputil_init(&s->dsp, s->avctx); |
f4fed5a2 | 157 | ff_hpeldsp_init(&s->hdsp, s->avctx->flags); |
1f4ea4e0 | 158 | ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample); |
c3027b4d | 159 | |
d50635cd MN |
160 | s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c; |
161 | s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c; | |
162 | s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c; | |
163 | s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c; | |
164 | s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c; | |
363114e8 | 165 | if (s->flags & CODEC_FLAG_BITEXACT) |
e27b6e62 | 166 | s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact; |
d50635cd | 167 | s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c; |
b0368839 | 168 | |
70dd8892 DB |
169 | if (ARCH_ARM) |
170 | ff_MPV_common_init_arm(s); | |
171 | if (ARCH_BFIN) | |
172 | ff_MPV_common_init_bfin(s); | |
173 | if (ARCH_PPC) | |
174 | ff_MPV_common_init_ppc(s); | |
175 | if (ARCH_X86) | |
176 | ff_MPV_common_init_x86(s); | |
676e200c | 177 | |
2ad1516a | 178 | /* load & permutate scantables |
363114e8 KT |
179 | * note: only wmv uses different ones |
180 | */ | |
181 | if (s->alternate_scan) { | |
bb198e19 MN |
182 | ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan); |
183 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan); | |
363114e8 | 184 | } else { |
bb198e19 MN |
185 | ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct); |
186 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct); | |
187 | } | |
3d2e8cce MN |
188 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan); |
189 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); | |
d930ef19 | 190 | |
defdfc9a AB |
191 | return 0; |
192 | } | |
193 | ||
c99307ca | 194 | static int frame_size_alloc(MpegEncContext *s, int linesize) |
f1d8763a JG |
195 | { |
196 | int alloc_size = FFALIGN(FFABS(linesize) + 32, 32); | |
197 | ||
198 | // edge emu needs blocksize + filter length - 1 | |
199 | // (= 17x17 for halfpel / 21x21 for h264) | |
45635885 JG |
200 | // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9 |
201 | // at uvlinesize. It supports only YUV420 so 24x24 is enough | |
f1d8763a | 202 | // linesize * interlaced * MBsize |
45635885 | 203 | FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24, |
f1d8763a JG |
204 | fail); |
205 | ||
259af1b9 | 206 | FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3, |
f1d8763a JG |
207 | fail) |
208 | s->me.temp = s->me.scratchpad; | |
209 | s->rd_scratchpad = s->me.scratchpad; | |
210 | s->b_scratchpad = s->me.scratchpad; | |
211 | s->obmc_scratchpad = s->me.scratchpad + 16; | |
212 | ||
213 | return 0; | |
214 | fail: | |
215 | av_freep(&s->edge_emu_buffer); | |
216 | return AVERROR(ENOMEM); | |
217 | } | |
218 | ||
34e46c44 | 219 | /** |
49bd8e4b | 220 | * Allocate a frame buffer |
34e46c44 GB |
221 | */ |
222 | static int alloc_frame_buffer(MpegEncContext *s, Picture *pic) | |
223 | { | |
f1d8763a | 224 | int r, ret; |
34e46c44 | 225 | |
759001c5 | 226 | pic->tf.f = &pic->f; |
ee769c6a AD |
227 | if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && |
228 | s->codec_id != AV_CODEC_ID_VC1IMAGE && | |
229 | s->codec_id != AV_CODEC_ID_MSS2) | |
759001c5 AK |
230 | r = ff_thread_get_buffer(s->avctx, &pic->tf, |
231 | pic->reference ? AV_GET_BUFFER_FLAG_REF : 0); | |
232 | else { | |
233 | pic->f.width = s->avctx->width; | |
234 | pic->f.height = s->avctx->height; | |
235 | pic->f.format = s->avctx->pix_fmt; | |
236 | r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0); | |
237 | } | |
238 | ||
a553c6a3 | 239 | if (r < 0 || !pic->f.buf[0]) { |
759001c5 AK |
240 | av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n", |
241 | r, pic->f.data[0]); | |
34e46c44 GB |
242 | return -1; |
243 | } | |
244 | ||
c3ebfcd6 HL |
245 | if (s->avctx->hwaccel) { |
246 | assert(!pic->hwaccel_picture_private); | |
247 | if (s->avctx->hwaccel->priv_data_size) { | |
248 | pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size); | |
249 | if (!pic->hwaccel_priv_buf) { | |
250 | av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n"); | |
251 | return -1; | |
252 | } | |
253 | pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data; | |
254 | } | |
255 | } | |
256 | ||
363114e8 KT |
257 | if (s->linesize && (s->linesize != pic->f.linesize[0] || |
258 | s->uvlinesize != pic->f.linesize[1])) { | |
259 | av_log(s->avctx, AV_LOG_ERROR, | |
260 | "get_buffer() failed (stride changed)\n"); | |
759001c5 | 261 | ff_mpeg_unref_picture(s, pic); |
34e46c44 GB |
262 | return -1; |
263 | } | |
264 | ||
657ccb5a | 265 | if (pic->f.linesize[1] != pic->f.linesize[2]) { |
363114e8 KT |
266 | av_log(s->avctx, AV_LOG_ERROR, |
267 | "get_buffer() failed (uv stride mismatch)\n"); | |
759001c5 | 268 | ff_mpeg_unref_picture(s, pic); |
34e46c44 GB |
269 | return -1; |
270 | } | |
271 | ||
f1d8763a | 272 | if (!s->edge_emu_buffer && |
c99307ca | 273 | (ret = frame_size_alloc(s, pic->f.linesize[0])) < 0) { |
f1d8763a JG |
274 | av_log(s->avctx, AV_LOG_ERROR, |
275 | "get_buffer() failed to allocate context scratch buffers.\n"); | |
759001c5 | 276 | ff_mpeg_unref_picture(s, pic); |
f1d8763a JG |
277 | return ret; |
278 | } | |
279 | ||
34e46c44 GB |
280 | return 0; |
281 | } | |
282 | ||
0b0a7a75 | 283 | void ff_free_picture_tables(Picture *pic) |
363114e8 | 284 | { |
759001c5 | 285 | int i; |
363114e8 | 286 | |
759001c5 AK |
287 | av_buffer_unref(&pic->mb_var_buf); |
288 | av_buffer_unref(&pic->mc_mb_var_buf); | |
289 | av_buffer_unref(&pic->mb_mean_buf); | |
290 | av_buffer_unref(&pic->mbskip_table_buf); | |
291 | av_buffer_unref(&pic->qscale_table_buf); | |
292 | av_buffer_unref(&pic->mb_type_buf); | |
363114e8 | 293 | |
759001c5 AK |
294 | for (i = 0; i < 2; i++) { |
295 | av_buffer_unref(&pic->motion_val_buf[i]); | |
296 | av_buffer_unref(&pic->ref_index_buf[i]); | |
297 | } | |
298 | } | |
299 | ||
300 | static int alloc_picture_tables(MpegEncContext *s, Picture *pic) | |
301 | { | |
302 | const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1; | |
363114e8 KT |
303 | const int mb_array_size = s->mb_stride * s->mb_height; |
304 | const int b8_array_size = s->b8_stride * s->mb_height * 2; | |
0da71265 | 305 | int i; |
759001c5 AK |
306 | |
307 | ||
308 | pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2); | |
309 | pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride); | |
310 | pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) * | |
311 | sizeof(uint32_t)); | |
312 | if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf) | |
313 | return AVERROR(ENOMEM); | |
314 | ||
315 | if (s->encoding) { | |
316 | pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t)); | |
317 | pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t)); | |
318 | pic->mb_mean_buf = av_buffer_allocz(mb_array_size); | |
319 | if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf) | |
320 | return AVERROR(ENOMEM); | |
321 | } | |
322 | ||
ccc71298 | 323 | if (s->out_format == FMT_H263 || s->encoding) { |
759001c5 AK |
324 | int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t); |
325 | int ref_index_size = 4 * mb_array_size; | |
326 | ||
327 | for (i = 0; mv_size && i < 2; i++) { | |
328 | pic->motion_val_buf[i] = av_buffer_allocz(mv_size); | |
329 | pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size); | |
330 | if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) | |
331 | return AVERROR(ENOMEM); | |
332 | } | |
333 | } | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | static int make_tables_writable(Picture *pic) | |
339 | { | |
340 | int ret, i; | |
341 | #define MAKE_WRITABLE(table) \ | |
342 | do {\ | |
343 | if (pic->table &&\ | |
344 | (ret = av_buffer_make_writable(&pic->table)) < 0)\ | |
345 | return ret;\ | |
346 | } while (0) | |
347 | ||
348 | MAKE_WRITABLE(mb_var_buf); | |
349 | MAKE_WRITABLE(mc_mb_var_buf); | |
350 | MAKE_WRITABLE(mb_mean_buf); | |
351 | MAKE_WRITABLE(mbskip_table_buf); | |
352 | MAKE_WRITABLE(qscale_table_buf); | |
353 | MAKE_WRITABLE(mb_type_buf); | |
354 | ||
355 | for (i = 0; i < 2; i++) { | |
356 | MAKE_WRITABLE(motion_val_buf[i]); | |
357 | MAKE_WRITABLE(ref_index_buf[i]); | |
358 | } | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | /** | |
364 | * Allocate a Picture. | |
365 | * The pixels are allocated/set by calling get_buffer() if shared = 0 | |
366 | */ | |
367 | int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared) | |
368 | { | |
369 | int i, ret; | |
115329f1 | 370 | |
363114e8 | 371 | if (shared) { |
657ccb5a | 372 | assert(pic->f.data[0]); |
759001c5 | 373 | pic->shared = 1; |
363114e8 | 374 | } else { |
a553c6a3 | 375 | assert(!pic->f.buf[0]); |
115329f1 | 376 | |
34e46c44 | 377 | if (alloc_frame_buffer(s, pic) < 0) |
4e00e76b | 378 | return -1; |
4e00e76b | 379 | |
657ccb5a DB |
380 | s->linesize = pic->f.linesize[0]; |
381 | s->uvlinesize = pic->f.linesize[1]; | |
1e491e29 | 382 | } |
115329f1 | 383 | |
759001c5 AK |
384 | if (!pic->qscale_table_buf) |
385 | ret = alloc_picture_tables(s, pic); | |
386 | else | |
387 | ret = make_tables_writable(pic); | |
388 | if (ret < 0) | |
389 | goto fail; | |
1e491e29 | 390 | |
759001c5 AK |
391 | if (s->encoding) { |
392 | pic->mb_var = (uint16_t*)pic->mb_var_buf->data; | |
393 | pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data; | |
394 | pic->mb_mean = pic->mb_mean_buf->data; | |
4e00e76b | 395 | } |
0da71265 | 396 | |
759001c5 AK |
397 | pic->mbskip_table = pic->mbskip_table_buf->data; |
398 | pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1; | |
399 | pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1; | |
400 | ||
401 | if (pic->motion_val_buf[0]) { | |
402 | for (i = 0; i < 2; i++) { | |
403 | pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4; | |
404 | pic->ref_index[i] = pic->ref_index_buf[i]->data; | |
405 | } | |
406 | } | |
115329f1 | 407 | |
1e491e29 | 408 | return 0; |
759001c5 AK |
409 | fail: |
410 | av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n"); | |
411 | ff_mpeg_unref_picture(s, pic); | |
0b0a7a75 | 412 | ff_free_picture_tables(pic); |
759001c5 | 413 | return AVERROR(ENOMEM); |
1e491e29 MN |
414 | } |
415 | ||
4e00e76b | 416 | /** |
58c42af7 | 417 | * Deallocate a picture. |
4e00e76b | 418 | */ |
759001c5 | 419 | void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic) |
363114e8 | 420 | { |
759001c5 AK |
421 | int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean); |
422 | ||
423 | pic->tf.f = &pic->f; | |
424 | /* WM Image / Screen codecs allocate internal buffers with different | |
425 | * dimensions / colorspaces; ignore user-defined callbacks for these. */ | |
426 | if (s->codec_id != AV_CODEC_ID_WMV3IMAGE && | |
427 | s->codec_id != AV_CODEC_ID_VC1IMAGE && | |
428 | s->codec_id != AV_CODEC_ID_MSS2) | |
429 | ff_thread_release_buffer(s->avctx, &pic->tf); | |
430 | else | |
431 | av_frame_unref(&pic->f); | |
432 | ||
433 | av_buffer_unref(&pic->hwaccel_priv_buf); | |
4e00e76b | 434 | |
3eae9b03 | 435 | if (pic->needs_realloc) |
0b0a7a75 | 436 | ff_free_picture_tables(pic); |
3eae9b03 | 437 | |
759001c5 AK |
438 | memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); |
439 | } | |
440 | ||
441 | static int update_picture_tables(Picture *dst, Picture *src) | |
442 | { | |
443 | int i; | |
444 | ||
445 | #define UPDATE_TABLE(table)\ | |
446 | do {\ | |
447 | if (src->table &&\ | |
448 | (!dst->table || dst->table->buffer != src->table->buffer)) {\ | |
449 | av_buffer_unref(&dst->table);\ | |
450 | dst->table = av_buffer_ref(src->table);\ | |
451 | if (!dst->table) {\ | |
0b0a7a75 | 452 | ff_free_picture_tables(dst);\ |
759001c5 AK |
453 | return AVERROR(ENOMEM);\ |
454 | }\ | |
455 | }\ | |
456 | } while (0) | |
457 | ||
458 | UPDATE_TABLE(mb_var_buf); | |
459 | UPDATE_TABLE(mc_mb_var_buf); | |
460 | UPDATE_TABLE(mb_mean_buf); | |
461 | UPDATE_TABLE(mbskip_table_buf); | |
462 | UPDATE_TABLE(qscale_table_buf); | |
463 | UPDATE_TABLE(mb_type_buf); | |
363114e8 | 464 | for (i = 0; i < 2; i++) { |
759001c5 AK |
465 | UPDATE_TABLE(motion_val_buf[i]); |
466 | UPDATE_TABLE(ref_index_buf[i]); | |
0da71265 | 467 | } |
115329f1 | 468 | |
759001c5 AK |
469 | dst->mb_var = src->mb_var; |
470 | dst->mc_mb_var = src->mc_mb_var; | |
471 | dst->mb_mean = src->mb_mean; | |
472 | dst->mbskip_table = src->mbskip_table; | |
473 | dst->qscale_table = src->qscale_table; | |
474 | dst->mb_type = src->mb_type; | |
475 | for (i = 0; i < 2; i++) { | |
476 | dst->motion_val[i] = src->motion_val[i]; | |
477 | dst->ref_index[i] = src->ref_index[i]; | |
1e491e29 | 478 | } |
759001c5 AK |
479 | |
480 | return 0; | |
481 | } | |
482 | ||
483 | int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src) | |
484 | { | |
485 | int ret; | |
486 | ||
487 | av_assert0(!dst->f.buf[0]); | |
488 | av_assert0(src->f.buf[0]); | |
489 | ||
490 | src->tf.f = &src->f; | |
491 | dst->tf.f = &dst->f; | |
492 | ret = ff_thread_ref_frame(&dst->tf, &src->tf); | |
493 | if (ret < 0) | |
494 | goto fail; | |
495 | ||
496 | ret = update_picture_tables(dst, src); | |
497 | if (ret < 0) | |
498 | goto fail; | |
499 | ||
500 | if (src->hwaccel_picture_private) { | |
501 | dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf); | |
502 | if (!dst->hwaccel_priv_buf) | |
503 | goto fail; | |
504 | dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data; | |
505 | } | |
506 | ||
507 | dst->field_picture = src->field_picture; | |
508 | dst->mb_var_sum = src->mb_var_sum; | |
509 | dst->mc_mb_var_sum = src->mc_mb_var_sum; | |
510 | dst->b_frame_score = src->b_frame_score; | |
511 | dst->needs_realloc = src->needs_realloc; | |
512 | dst->reference = src->reference; | |
513 | dst->shared = src->shared; | |
514 | ||
515 | return 0; | |
516 | fail: | |
517 | ff_mpeg_unref_picture(s, dst); | |
518 | return ret; | |
1e491e29 MN |
519 | } |
520 | ||
16e7b189 KS |
521 | static void exchange_uv(MpegEncContext *s) |
522 | { | |
523 | int16_t (*tmp)[64]; | |
524 | ||
525 | tmp = s->pblocks[4]; | |
526 | s->pblocks[4] = s->pblocks[5]; | |
527 | s->pblocks[5] = tmp; | |
528 | } | |
529 | ||
12b54a1f | 530 | static int init_duplicate_context(MpegEncContext *s) |
363114e8 | 531 | { |
2cbd734a MR |
532 | int y_size = s->b8_stride * (2 * s->mb_height + 1); |
533 | int c_size = s->mb_stride * (s->mb_height + 1); | |
534 | int yc_size = y_size + 2 * c_size; | |
9c3d33d6 MN |
535 | int i; |
536 | ||
f1d8763a JG |
537 | s->edge_emu_buffer = |
538 | s->me.scratchpad = | |
539 | s->me.temp = | |
540 | s->rd_scratchpad = | |
541 | s->b_scratchpad = | |
542 | s->obmc_scratchpad = NULL; | |
363114e8 | 543 | |
9c3d33d6 | 544 | if (s->encoding) { |
363114e8 KT |
545 | FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map, |
546 | ME_MAP_SIZE * sizeof(uint32_t), fail) | |
547 | FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, | |
548 | ME_MAP_SIZE * sizeof(uint32_t), fail) | |
549 | if (s->avctx->noise_reduction) { | |
550 | FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, | |
551 | 2 * 64 * sizeof(int), fail) | |
9c3d33d6 | 552 | } |
115329f1 | 553 | } |
88bd7fdc | 554 | FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail) |
363114e8 | 555 | s->block = s->blocks[0]; |
9c3d33d6 | 556 | |
363114e8 | 557 | for (i = 0; i < 12; i++) { |
21effaa4 | 558 | s->pblocks[i] = &s->block[i]; |
9c3d33d6 | 559 | } |
16e7b189 KS |
560 | if (s->avctx->codec_tag == AV_RL32("VCR2")) |
561 | exchange_uv(s); | |
2cbd734a | 562 | |
79042a6e MR |
563 | if (s->out_format == FMT_H263) { |
564 | /* ac values */ | |
363114e8 KT |
565 | FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, |
566 | yc_size * sizeof(int16_t) * 16, fail); | |
2cbd734a MR |
567 | s->ac_val[0] = s->ac_val_base + s->b8_stride + 1; |
568 | s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1; | |
569 | s->ac_val[2] = s->ac_val[1] + c_size; | |
570 | } | |
571 | ||
9c3d33d6 MN |
572 | return 0; |
573 | fail: | |
efd29844 | 574 | return -1; // free() through ff_MPV_common_end() |
9c3d33d6 MN |
575 | } |
576 | ||
363114e8 KT |
577 | static void free_duplicate_context(MpegEncContext *s) |
578 | { | |
579 | if (s == NULL) | |
580 | return; | |
9c3d33d6 | 581 | |
330deb75 | 582 | av_freep(&s->edge_emu_buffer); |
9c3d33d6 | 583 | av_freep(&s->me.scratchpad); |
363114e8 KT |
584 | s->me.temp = |
585 | s->rd_scratchpad = | |
586 | s->b_scratchpad = | |
587 | s->obmc_scratchpad = NULL; | |
115329f1 | 588 | |
9c3d33d6 MN |
589 | av_freep(&s->dct_error_sum); |
590 | av_freep(&s->me.map); | |
591 | av_freep(&s->me.score_map); | |
592 | av_freep(&s->blocks); | |
2cbd734a | 593 | av_freep(&s->ac_val_base); |
363114e8 | 594 | s->block = NULL; |
9c3d33d6 MN |
595 | } |
596 | ||
363114e8 KT |
597 | static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src) |
598 | { | |
599 | #define COPY(a) bak->a = src->a | |
9c3d33d6 MN |
600 | COPY(edge_emu_buffer); |
601 | COPY(me.scratchpad); | |
a6f2c0d6 | 602 | COPY(me.temp); |
9c3d33d6 MN |
603 | COPY(rd_scratchpad); |
604 | COPY(b_scratchpad); | |
605 | COPY(obmc_scratchpad); | |
606 | COPY(me.map); | |
607 | COPY(me.score_map); | |
608 | COPY(blocks); | |
609 | COPY(block); | |
610 | COPY(start_mb_y); | |
611 | COPY(end_mb_y); | |
612 | COPY(me.map_generation); | |
613 | COPY(pb); | |
614 | COPY(dct_error_sum); | |
da16b204 MN |
615 | COPY(dct_count[0]); |
616 | COPY(dct_count[1]); | |
2cbd734a MR |
617 | COPY(ac_val_base); |
618 | COPY(ac_val[0]); | |
619 | COPY(ac_val[1]); | |
620 | COPY(ac_val[2]); | |
9c3d33d6 MN |
621 | #undef COPY |
622 | } | |
623 | ||
f1d8763a | 624 | int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src) |
363114e8 | 625 | { |
9c3d33d6 | 626 | MpegEncContext bak; |
f1d8763a | 627 | int i, ret; |
363114e8 KT |
628 | // FIXME copy only needed parts |
629 | // START_TIMER | |
9c3d33d6 MN |
630 | backup_duplicate_context(&bak, dst); |
631 | memcpy(dst, src, sizeof(MpegEncContext)); | |
632 | backup_duplicate_context(dst, &bak); | |
363114e8 | 633 | for (i = 0; i < 12; i++) { |
21effaa4 | 634 | dst->pblocks[i] = &dst->block[i]; |
c62c07d3 | 635 | } |
16e7b189 KS |
636 | if (dst->avctx->codec_tag == AV_RL32("VCR2")) |
637 | exchange_uv(dst); | |
f1d8763a | 638 | if (!dst->edge_emu_buffer && |
c99307ca | 639 | (ret = frame_size_alloc(dst, dst->linesize)) < 0) { |
f1d8763a JG |
640 | av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context " |
641 | "scratch buffers.\n"); | |
642 | return ret; | |
643 | } | |
363114e8 KT |
644 | // STOP_TIMER("update_duplicate_context") |
645 | // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads | |
f1d8763a | 646 | return 0; |
9c3d33d6 MN |
647 | } |
648 | ||
363114e8 KT |
649 | int ff_mpeg_update_thread_context(AVCodecContext *dst, |
650 | const AVCodecContext *src) | |
6a9c8594 | 651 | { |
759001c5 | 652 | int i, ret; |
6a9c8594 AS |
653 | MpegEncContext *s = dst->priv_data, *s1 = src->priv_data; |
654 | ||
363114e8 KT |
655 | if (dst == src || !s1->context_initialized) |
656 | return 0; | |
6a9c8594 | 657 | |
363114e8 KT |
658 | // FIXME can parameters change on I-frames? |
659 | // in that case dst may need a reinit | |
660 | if (!s->context_initialized) { | |
6a9c8594 AS |
661 | memcpy(s, s1, sizeof(MpegEncContext)); |
662 | ||
663 | s->avctx = dst; | |
6a9c8594 AS |
664 | s->bitstream_buffer = NULL; |
665 | s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0; | |
666 | ||
efd29844 | 667 | ff_MPV_common_init(s); |
6a9c8594 AS |
668 | } |
669 | ||
8701f4f8 JG |
670 | if (s->height != s1->height || s->width != s1->width || s->context_reinit) { |
671 | int err; | |
672 | s->context_reinit = 0; | |
673 | s->height = s1->height; | |
674 | s->width = s1->width; | |
675 | if ((err = ff_MPV_common_frame_size_change(s)) < 0) | |
676 | return err; | |
677 | } | |
678 | ||
6a9c8594 AS |
679 | s->avctx->coded_height = s1->avctx->coded_height; |
680 | s->avctx->coded_width = s1->avctx->coded_width; | |
681 | s->avctx->width = s1->avctx->width; | |
682 | s->avctx->height = s1->avctx->height; | |
683 | ||
684 | s->coded_picture_number = s1->coded_picture_number; | |
685 | s->picture_number = s1->picture_number; | |
6a9c8594 | 686 | |
759001c5 AK |
687 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
688 | ff_mpeg_unref_picture(s, &s->picture[i]); | |
a553c6a3 | 689 | if (s1->picture[i].f.buf[0] && |
759001c5 AK |
690 | (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0) |
691 | return ret; | |
692 | } | |
693 | ||
694 | #define UPDATE_PICTURE(pic)\ | |
695 | do {\ | |
696 | ff_mpeg_unref_picture(s, &s->pic);\ | |
a553c6a3 | 697 | if (s1->pic.f.buf[0])\ |
759001c5 AK |
698 | ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\ |
699 | else\ | |
700 | ret = update_picture_tables(&s->pic, &s1->pic);\ | |
701 | if (ret < 0)\ | |
702 | return ret;\ | |
703 | } while (0) | |
704 | ||
705 | UPDATE_PICTURE(current_picture); | |
706 | UPDATE_PICTURE(last_picture); | |
707 | UPDATE_PICTURE(next_picture); | |
1481e198 | 708 | |
4d9ec050 KT |
709 | s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1); |
710 | s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1); | |
711 | s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1); | |
6a9c8594 | 712 | |
4d9ec050 | 713 | // Error/bug resilience |
6a9c8594 AS |
714 | s->next_p_frame_damaged = s1->next_p_frame_damaged; |
715 | s->workaround_bugs = s1->workaround_bugs; | |
716 | ||
4d9ec050 | 717 | // MPEG4 timing info |
e62a43f6 | 718 | memcpy(&s->last_time_base, &s1->last_time_base, |
ee8af2dd | 719 | (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) - |
e62a43f6 | 720 | (char *) &s1->last_time_base); |
4d9ec050 KT |
721 | |
722 | // B-frame info | |
723 | s->max_b_frames = s1->max_b_frames; | |
724 | s->low_delay = s1->low_delay; | |
ba0c8981 | 725 | s->droppable = s1->droppable; |
4d9ec050 KT |
726 | |
727 | // DivX handling (doesn't work) | |
728 | s->divx_packed = s1->divx_packed; | |
729 | ||
730 | if (s1->bitstream_buffer) { | |
731 | if (s1->bitstream_buffer_size + | |
732 | FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size) | |
733 | av_fast_malloc(&s->bitstream_buffer, | |
734 | &s->allocated_bitstream_buffer_size, | |
735 | s1->allocated_bitstream_buffer_size); | |
736 | s->bitstream_buffer_size = s1->bitstream_buffer_size; | |
737 | memcpy(s->bitstream_buffer, s1->bitstream_buffer, | |
738 | s1->bitstream_buffer_size); | |
739 | memset(s->bitstream_buffer + s->bitstream_buffer_size, 0, | |
740 | FF_INPUT_BUFFER_PADDING_SIZE); | |
6a9c8594 AS |
741 | } |
742 | ||
f1d8763a JG |
743 | // linesize dependend scratch buffer allocation |
744 | if (!s->edge_emu_buffer) | |
745 | if (s1->linesize) { | |
c99307ca | 746 | if (frame_size_alloc(s, s1->linesize) < 0) { |
f1d8763a JG |
747 | av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context " |
748 | "scratch buffers.\n"); | |
749 | return AVERROR(ENOMEM); | |
750 | } | |
751 | } else { | |
752 | av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not " | |
753 | "be allocated due to unknown size.\n"); | |
754 | return AVERROR_BUG; | |
755 | } | |
756 | ||
4d9ec050 KT |
757 | // MPEG2/interlacing info |
758 | memcpy(&s->progressive_sequence, &s1->progressive_sequence, | |
759 | (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence); | |
6a9c8594 | 760 | |
4d9ec050 KT |
761 | if (!s1->first_field) { |
762 | s->last_pict_type = s1->pict_type; | |
763 | if (s1->current_picture_ptr) | |
764 | s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality; | |
6a9c8594 AS |
765 | } |
766 | ||
767 | return 0; | |
768 | } | |
769 | ||
3edcacde | 770 | /** |
58c42af7 | 771 | * Set the given MpegEncContext to common defaults |
4d9ec050 | 772 | * (same for encoding and decoding). |
58c42af7 | 773 | * The changed fields will not depend upon the |
4d9ec050 | 774 | * prior state of the MpegEncContext. |
3edcacde | 775 | */ |
efd29844 | 776 | void ff_MPV_common_defaults(MpegEncContext *s) |
4d9ec050 KT |
777 | { |
778 | s->y_dc_scale_table = | |
779 | s->c_dc_scale_table = ff_mpeg1_dc_scale_table; | |
780 | s->chroma_qscale_table = ff_default_chroma_qscale_table; | |
781 | s->progressive_frame = 1; | |
782 | s->progressive_sequence = 1; | |
783 | s->picture_structure = PICT_FRAME; | |
784 | ||
785 | s->coded_picture_number = 0; | |
786 | s->picture_number = 0; | |
7976241a | 787 | |
4d9ec050 KT |
788 | s->f_code = 1; |
789 | s->b_code = 1; | |
6a9c8594 | 790 | |
881a5e04 | 791 | s->slice_context_count = 1; |
3edcacde MN |
792 | } |
793 | ||
794 | /** | |
58c42af7 | 795 | * Set the given MpegEncContext to defaults for decoding. |
4d9ec050 KT |
796 | * the changed fields will not depend upon |
797 | * the prior state of the MpegEncContext. | |
3edcacde | 798 | */ |
efd29844 | 799 | void ff_MPV_decode_defaults(MpegEncContext *s) |
4d9ec050 | 800 | { |
efd29844 | 801 | ff_MPV_common_defaults(s); |
3edcacde MN |
802 | } |
803 | ||
54974c62 AK |
804 | static int init_er(MpegEncContext *s) |
805 | { | |
806 | ERContext *er = &s->er; | |
807 | int mb_array_size = s->mb_height * s->mb_stride; | |
808 | int i; | |
809 | ||
810 | er->avctx = s->avctx; | |
811 | er->dsp = &s->dsp; | |
812 | ||
813 | er->mb_index2xy = s->mb_index2xy; | |
814 | er->mb_num = s->mb_num; | |
815 | er->mb_width = s->mb_width; | |
816 | er->mb_height = s->mb_height; | |
817 | er->mb_stride = s->mb_stride; | |
818 | er->b8_stride = s->b8_stride; | |
819 | ||
820 | er->er_temp_buffer = av_malloc(s->mb_height * s->mb_stride); | |
821 | er->error_status_table = av_mallocz(mb_array_size); | |
822 | if (!er->er_temp_buffer || !er->error_status_table) | |
823 | goto fail; | |
824 | ||
825 | er->mbskip_table = s->mbskip_table; | |
826 | er->mbintra_table = s->mbintra_table; | |
827 | ||
828 | for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++) | |
829 | er->dc_val[i] = s->dc_val[i]; | |
830 | ||
831 | er->decode_mb = mpeg_er_decode_mb; | |
832 | er->opaque = s; | |
833 | ||
834 | return 0; | |
835 | fail: | |
836 | av_freep(&er->er_temp_buffer); | |
837 | av_freep(&er->error_status_table); | |
838 | return AVERROR(ENOMEM); | |
839 | } | |
840 | ||
3edcacde | 841 | /** |
1b3439b3 JG |
842 | * Initialize and allocates MpegEncContext fields dependent on the resolution. |
843 | */ | |
844 | static int init_context_frame(MpegEncContext *s) | |
845 | { | |
846 | int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y; | |
847 | ||
848 | s->mb_width = (s->width + 15) / 16; | |
849 | s->mb_stride = s->mb_width + 1; | |
850 | s->b8_stride = s->mb_width * 2 + 1; | |
851 | s->b4_stride = s->mb_width * 4 + 1; | |
852 | mb_array_size = s->mb_height * s->mb_stride; | |
853 | mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; | |
854 | ||
855 | /* set default edge pos, will be overriden | |
856 | * in decode_header if needed */ | |
857 | s->h_edge_pos = s->mb_width * 16; | |
858 | s->v_edge_pos = s->mb_height * 16; | |
859 | ||
860 | s->mb_num = s->mb_width * s->mb_height; | |
861 | ||
862 | s->block_wrap[0] = | |
863 | s->block_wrap[1] = | |
864 | s->block_wrap[2] = | |
865 | s->block_wrap[3] = s->b8_stride; | |
866 | s->block_wrap[4] = | |
867 | s->block_wrap[5] = s->mb_stride; | |
868 | ||
869 | y_size = s->b8_stride * (2 * s->mb_height + 1); | |
870 | c_size = s->mb_stride * (s->mb_height + 1); | |
871 | yc_size = y_size + 2 * c_size; | |
872 | ||
873 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), | |
874 | fail); // error ressilience code looks cleaner with this | |
875 | for (y = 0; y < s->mb_height; y++) | |
876 | for (x = 0; x < s->mb_width; x++) | |
877 | s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride; | |
878 | ||
879 | s->mb_index2xy[s->mb_height * s->mb_width] = | |
880 | (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed? | |
881 | ||
882 | if (s->encoding) { | |
883 | /* Allocate MV tables */ | |
884 | FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, | |
885 | mv_table_size * 2 * sizeof(int16_t), fail); | |
886 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, | |
887 | mv_table_size * 2 * sizeof(int16_t), fail); | |
888 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, | |
889 | mv_table_size * 2 * sizeof(int16_t), fail); | |
890 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, | |
891 | mv_table_size * 2 * sizeof(int16_t), fail); | |
892 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, | |
893 | mv_table_size * 2 * sizeof(int16_t), fail); | |
894 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, | |
895 | mv_table_size * 2 * sizeof(int16_t), fail); | |
896 | s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; | |
897 | s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; | |
898 | s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; | |
899 | s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + | |
900 | s->mb_stride + 1; | |
901 | s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + | |
902 | s->mb_stride + 1; | |
903 | s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; | |
904 | ||
905 | /* Allocate MB type table */ | |
906 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * | |
907 | sizeof(uint16_t), fail); // needed for encoding | |
908 | ||
909 | FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * | |
910 | sizeof(int), fail); | |
911 | ||
912 | FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab, | |
913 | mb_array_size * sizeof(float), fail); | |
914 | FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab, | |
915 | mb_array_size * sizeof(float), fail); | |
916 | ||
917 | } | |
918 | ||
1b3439b3 JG |
919 | if (s->codec_id == AV_CODEC_ID_MPEG4 || |
920 | (s->flags & CODEC_FLAG_INTERLACED_ME)) { | |
921 | /* interlaced direct mode decoding tables */ | |
922 | for (i = 0; i < 2; i++) { | |
923 | int j, k; | |
924 | for (j = 0; j < 2; j++) { | |
925 | for (k = 0; k < 2; k++) { | |
926 | FF_ALLOCZ_OR_GOTO(s->avctx, | |
927 | s->b_field_mv_table_base[i][j][k], | |
928 | mv_table_size * 2 * sizeof(int16_t), | |
929 | fail); | |
930 | s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + | |
931 | s->mb_stride + 1; | |
932 | } | |
933 | FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], | |
934 | mb_array_size * 2 * sizeof(uint8_t), fail); | |
935 | FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], | |
936 | mv_table_size * 2 * sizeof(int16_t), fail); | |
937 | s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] | |
938 | + s->mb_stride + 1; | |
939 | } | |
940 | FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], | |
941 | mb_array_size * 2 * sizeof(uint8_t), fail); | |
942 | } | |
943 | } | |
944 | if (s->out_format == FMT_H263) { | |
945 | /* cbp values */ | |
946 | FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail); | |
947 | s->coded_block = s->coded_block_base + s->b8_stride + 1; | |
948 | ||
949 | /* cbp, ac_pred, pred_dir */ | |
950 | FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table, | |
951 | mb_array_size * sizeof(uint8_t), fail); | |
952 | FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, | |
953 | mb_array_size * sizeof(uint8_t), fail); | |
954 | } | |
955 | ||
956 | if (s->h263_pred || s->h263_plus || !s->encoding) { | |
957 | /* dc values */ | |
958 | // MN: we need these for error resilience of intra-frames | |
959 | FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, | |
960 | yc_size * sizeof(int16_t), fail); | |
961 | s->dc_val[0] = s->dc_val_base + s->b8_stride + 1; | |
962 | s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1; | |
963 | s->dc_val[2] = s->dc_val[1] + c_size; | |
964 | for (i = 0; i < yc_size; i++) | |
965 | s->dc_val_base[i] = 1024; | |
966 | } | |
967 | ||
968 | /* which mb is a intra block */ | |
969 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail); | |
970 | memset(s->mbintra_table, 1, mb_array_size); | |
971 | ||
972 | /* init macroblock skip table */ | |
973 | FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail); | |
974 | // Note the + 1 is for a quicker mpeg4 slice_end detection | |
975 | ||
54974c62 | 976 | return init_er(s); |
1b3439b3 JG |
977 | fail: |
978 | return AVERROR(ENOMEM); | |
979 | } | |
980 | ||
981 | /** | |
3edcacde MN |
982 | * init common structure for both encoder and decoder. |
983 | * this assumes that some variables like width/height are already set | |
984 | */ | |
efd29844 | 985 | av_cold int ff_MPV_common_init(MpegEncContext *s) |
defdfc9a | 986 | { |
7e76fc52 | 987 | int i; |
881a5e04 JG |
988 | int nb_slices = (HAVE_THREADS && |
989 | s->avctx->active_thread_type & FF_THREAD_SLICE) ? | |
990 | s->avctx->thread_count : 1; | |
991 | ||
992 | if (s->encoding && s->avctx->slices) | |
993 | nb_slices = s->avctx->slices; | |
defdfc9a | 994 | |
36ef5369 | 995 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence) |
0127b861 | 996 | s->mb_height = (s->height + 31) / 32 * 2; |
19cac8e3 | 997 | else |
1b661802 | 998 | s->mb_height = (s->height + 15) / 16; |
fdb52bcc | 999 | |
716d413c | 1000 | if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) { |
4d9ec050 | 1001 | av_log(s->avctx, AV_LOG_ERROR, |
716d413c | 1002 | "decoding to AV_PIX_FMT_NONE is not supported.\n"); |
9cfc1b3a IK |
1003 | return -1; |
1004 | } | |
1005 | ||
881a5e04 JG |
1006 | if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) { |
1007 | int max_slices; | |
1008 | if (s->mb_height) | |
1009 | max_slices = FFMIN(MAX_THREADS, s->mb_height); | |
1010 | else | |
1011 | max_slices = MAX_THREADS; | |
1012 | av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d)," | |
1013 | " reducing to %d\n", nb_slices, max_slices); | |
1014 | nb_slices = max_slices; | |
000a9c02 MN |
1015 | } |
1016 | ||
4d9ec050 KT |
1017 | if ((s->width || s->height) && |
1018 | av_image_check_size(s->width, s->height, 0, s->avctx)) | |
0ecca7a4 MN |
1019 | return -1; |
1020 | ||
6180ade7 | 1021 | ff_dct_common_init(s); |
eb4b3dd3 | 1022 | |
4d9ec050 KT |
1023 | s->flags = s->avctx->flags; |
1024 | s->flags2 = s->avctx->flags2; | |
defdfc9a | 1025 | |
5f24fe82 MS |
1026 | /* set chroma shifts */ |
1027 | av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, | |
1028 | &s->chroma_x_shift, | |
1029 | &s->chroma_y_shift); | |
eb4b3dd3 | 1030 | |
5f24fe82 MS |
1031 | /* convert fourcc to upper case */ |
1032 | s->codec_tag = avpriv_toupper4(s->avctx->codec_tag); | |
115329f1 | 1033 | |
5f24fe82 | 1034 | s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag); |
603a5f04 | 1035 | |
4d9ec050 | 1036 | FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, |
759001c5 AK |
1037 | MAX_PICTURE_COUNT * sizeof(Picture), fail); |
1038 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { | |
2d1f4288 | 1039 | av_frame_unref(&s->picture[i].f); |
747069e2 | 1040 | } |
759001c5 AK |
1041 | memset(&s->next_picture, 0, sizeof(s->next_picture)); |
1042 | memset(&s->last_picture, 0, sizeof(s->last_picture)); | |
1043 | memset(&s->current_picture, 0, sizeof(s->current_picture)); | |
2d1f4288 AK |
1044 | av_frame_unref(&s->next_picture.f); |
1045 | av_frame_unref(&s->last_picture.f); | |
1046 | av_frame_unref(&s->current_picture.f); | |
b465449e | 1047 | |
fb22c237 | 1048 | if (s->width && s->height) { |
7e76fc52 | 1049 | if (init_context_frame(s)) |
1b3439b3 | 1050 | goto fail; |
4d9ec050 KT |
1051 | |
1052 | s->parse_context.state = -1; | |
fb22c237 | 1053 | } |
d7425f59 | 1054 | |
de6d9b64 | 1055 | s->context_initialized = 1; |
4d9ec050 | 1056 | s->thread_context[0] = s; |
9c3d33d6 | 1057 | |
fb22c237 | 1058 | if (s->width && s->height) { |
881a5e04 JG |
1059 | if (nb_slices > 1) { |
1060 | for (i = 1; i < nb_slices; i++) { | |
4d9ec050 KT |
1061 | s->thread_context[i] = av_malloc(sizeof(MpegEncContext)); |
1062 | memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); | |
1063 | } | |
9c3d33d6 | 1064 | |
881a5e04 | 1065 | for (i = 0; i < nb_slices; i++) { |
12b54a1f | 1066 | if (init_duplicate_context(s->thread_context[i]) < 0) |
4d9ec050 KT |
1067 | goto fail; |
1068 | s->thread_context[i]->start_mb_y = | |
881a5e04 | 1069 | (s->mb_height * (i) + nb_slices / 2) / nb_slices; |
4d9ec050 | 1070 | s->thread_context[i]->end_mb_y = |
881a5e04 | 1071 | (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices; |
4d9ec050 KT |
1072 | } |
1073 | } else { | |
12b54a1f | 1074 | if (init_duplicate_context(s) < 0) |
d969e93a | 1075 | goto fail; |
4d9ec050 KT |
1076 | s->start_mb_y = 0; |
1077 | s->end_mb_y = s->mb_height; | |
d969e93a | 1078 | } |
881a5e04 | 1079 | s->slice_context_count = nb_slices; |
fb22c237 RB |
1080 | } |
1081 | ||
de6d9b64 FB |
1082 | return 0; |
1083 | fail: | |
efd29844 | 1084 | ff_MPV_common_end(s); |
de6d9b64 FB |
1085 | return -1; |
1086 | } | |
1087 | ||
1b3439b3 JG |
1088 | /** |
1089 | * Frees and resets MpegEncContext fields depending on the resolution. | |
1090 | * Is used during resolution changes to avoid a full reinitialization of the | |
1091 | * codec. | |
1092 | */ | |
1093 | static int free_context_frame(MpegEncContext *s) | |
de6d9b64 | 1094 | { |
bb198e19 | 1095 | int i, j, k; |
de6d9b64 | 1096 | |
6000abfa | 1097 | av_freep(&s->mb_type); |
7bc9090a MN |
1098 | av_freep(&s->p_mv_table_base); |
1099 | av_freep(&s->b_forw_mv_table_base); | |
1100 | av_freep(&s->b_back_mv_table_base); | |
1101 | av_freep(&s->b_bidir_forw_mv_table_base); | |
1102 | av_freep(&s->b_bidir_back_mv_table_base); | |
1103 | av_freep(&s->b_direct_mv_table_base); | |
4d9ec050 KT |
1104 | s->p_mv_table = NULL; |
1105 | s->b_forw_mv_table = NULL; | |
1106 | s->b_back_mv_table = NULL; | |
1107 | s->b_bidir_forw_mv_table = NULL; | |
1108 | s->b_bidir_back_mv_table = NULL; | |
1109 | s->b_direct_mv_table = NULL; | |
1110 | for (i = 0; i < 2; i++) { | |
1111 | for (j = 0; j < 2; j++) { | |
1112 | for (k = 0; k < 2; k++) { | |
bb198e19 | 1113 | av_freep(&s->b_field_mv_table_base[i][j][k]); |
4d9ec050 | 1114 | s->b_field_mv_table[i][j][k] = NULL; |
bb198e19 MN |
1115 | } |
1116 | av_freep(&s->b_field_select_table[i][j]); | |
1117 | av_freep(&s->p_field_mv_table_base[i][j]); | |
4d9ec050 | 1118 | s->p_field_mv_table[i][j] = NULL; |
bb198e19 MN |
1119 | } |
1120 | av_freep(&s->p_field_select_table[i]); | |
1121 | } | |
115329f1 | 1122 | |
137c8468 | 1123 | av_freep(&s->dc_val_base); |
137c8468 | 1124 | av_freep(&s->coded_block_base); |
6000abfa | 1125 | av_freep(&s->mbintra_table); |
7f2fe444 MN |
1126 | av_freep(&s->cbp_table); |
1127 | av_freep(&s->pred_dir_table); | |
115329f1 | 1128 | |
6000abfa | 1129 | av_freep(&s->mbskip_table); |
0ecca7a4 | 1130 | |
54974c62 AK |
1131 | av_freep(&s->er.error_status_table); |
1132 | av_freep(&s->er.er_temp_buffer); | |
7bc9090a | 1133 | av_freep(&s->mb_index2xy); |
158c7f05 | 1134 | av_freep(&s->lambda_table); |
1b3439b3 JG |
1135 | av_freep(&s->cplx_tab); |
1136 | av_freep(&s->bits_tab); | |
1137 | ||
1138 | s->linesize = s->uvlinesize = 0; | |
1139 | ||
1b3439b3 JG |
1140 | return 0; |
1141 | } | |
1142 | ||
435c0b87 JG |
1143 | int ff_MPV_common_frame_size_change(MpegEncContext *s) |
1144 | { | |
1145 | int i, err = 0; | |
1146 | ||
1147 | if (s->slice_context_count > 1) { | |
1148 | for (i = 0; i < s->slice_context_count; i++) { | |
1149 | free_duplicate_context(s->thread_context[i]); | |
1150 | } | |
1151 | for (i = 1; i < s->slice_context_count; i++) { | |
1152 | av_freep(&s->thread_context[i]); | |
1153 | } | |
1154 | } else | |
1155 | free_duplicate_context(s); | |
1156 | ||
759001c5 AK |
1157 | if ((err = free_context_frame(s)) < 0) |
1158 | return err; | |
435c0b87 JG |
1159 | |
1160 | if (s->picture) | |
759001c5 | 1161 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
435c0b87 JG |
1162 | s->picture[i].needs_realloc = 1; |
1163 | } | |
1164 | ||
1165 | s->last_picture_ptr = | |
1166 | s->next_picture_ptr = | |
1167 | s->current_picture_ptr = NULL; | |
1168 | ||
1169 | // init | |
1170 | if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence) | |
1171 | s->mb_height = (s->height + 31) / 32 * 2; | |
19cac8e3 | 1172 | else |
435c0b87 JG |
1173 | s->mb_height = (s->height + 15) / 16; |
1174 | ||
1175 | if ((s->width || s->height) && | |
1176 | av_image_check_size(s->width, s->height, 0, s->avctx)) | |
1177 | return AVERROR_INVALIDDATA; | |
1178 | ||
1179 | if ((err = init_context_frame(s))) | |
1180 | goto fail; | |
1181 | ||
1182 | s->thread_context[0] = s; | |
1183 | ||
1184 | if (s->width && s->height) { | |
1185 | int nb_slices = s->slice_context_count; | |
1186 | if (nb_slices > 1) { | |
1187 | for (i = 1; i < nb_slices; i++) { | |
1188 | s->thread_context[i] = av_malloc(sizeof(MpegEncContext)); | |
1189 | memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); | |
1190 | } | |
1191 | ||
1192 | for (i = 0; i < nb_slices; i++) { | |
12b54a1f | 1193 | if (init_duplicate_context(s->thread_context[i]) < 0) |
435c0b87 JG |
1194 | goto fail; |
1195 | s->thread_context[i]->start_mb_y = | |
1196 | (s->mb_height * (i) + nb_slices / 2) / nb_slices; | |
1197 | s->thread_context[i]->end_mb_y = | |
1198 | (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices; | |
1199 | } | |
1200 | } else { | |
12b54a1f | 1201 | if (init_duplicate_context(s) < 0) |
435c0b87 JG |
1202 | goto fail; |
1203 | s->start_mb_y = 0; | |
1204 | s->end_mb_y = s->mb_height; | |
1205 | } | |
1206 | s->slice_context_count = nb_slices; | |
1207 | } | |
1208 | ||
1209 | return 0; | |
1210 | fail: | |
1211 | ff_MPV_common_end(s); | |
1212 | return err; | |
1213 | } | |
1214 | ||
1b3439b3 JG |
1215 | /* init common structure for both encoder and decoder */ |
1216 | void ff_MPV_common_end(MpegEncContext *s) | |
1217 | { | |
1218 | int i; | |
1219 | ||
1220 | if (s->slice_context_count > 1) { | |
1221 | for (i = 0; i < s->slice_context_count; i++) { | |
1222 | free_duplicate_context(s->thread_context[i]); | |
1223 | } | |
1224 | for (i = 1; i < s->slice_context_count; i++) { | |
1225 | av_freep(&s->thread_context[i]); | |
1226 | } | |
1227 | s->slice_context_count = 1; | |
1228 | } else free_duplicate_context(s); | |
1229 | ||
1230 | av_freep(&s->parse_context.buffer); | |
1231 | s->parse_context.buffer_size = 0; | |
1232 | ||
1233 | av_freep(&s->bitstream_buffer); | |
1234 | s->allocated_bitstream_buffer_size = 0; | |
1235 | ||
759001c5 AK |
1236 | if (s->picture) { |
1237 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { | |
0b0a7a75 | 1238 | ff_free_picture_tables(&s->picture[i]); |
759001c5 | 1239 | ff_mpeg_unref_picture(s, &s->picture[i]); |
9b4b6e09 | 1240 | } |
de6d9b64 | 1241 | } |
b465449e | 1242 | av_freep(&s->picture); |
0b0a7a75 | 1243 | ff_free_picture_tables(&s->last_picture); |
759001c5 | 1244 | ff_mpeg_unref_picture(s, &s->last_picture); |
0b0a7a75 | 1245 | ff_free_picture_tables(&s->current_picture); |
759001c5 | 1246 | ff_mpeg_unref_picture(s, &s->current_picture); |
0b0a7a75 | 1247 | ff_free_picture_tables(&s->next_picture); |
759001c5 | 1248 | ff_mpeg_unref_picture(s, &s->next_picture); |
32c7589b JG |
1249 | |
1250 | free_context_frame(s); | |
1251 | ||
4d9ec050 KT |
1252 | s->context_initialized = 0; |
1253 | s->last_picture_ptr = | |
1254 | s->next_picture_ptr = | |
1255 | s->current_picture_ptr = NULL; | |
1256 | s->linesize = s->uvlinesize = 0; | |
de6d9b64 FB |
1257 | } |
1258 | ||
6fee1b90 DB |
1259 | av_cold void ff_init_rl(RLTable *rl, |
1260 | uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3]) | |
1d0d55da | 1261 | { |
4d9ec050 KT |
1262 | int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1]; |
1263 | uint8_t index_run[MAX_RUN + 1]; | |
1d0d55da MN |
1264 | int last, run, level, start, end, i; |
1265 | ||
c65dfac4 | 1266 | /* If table is static, we can quit if rl->max_level[0] is not NULL */ |
4d9ec050 | 1267 | if (static_store && rl->max_level[0]) |
073c2593 BP |
1268 | return; |
1269 | ||
1d0d55da | 1270 | /* compute max_level[], max_run[] and index_run[] */ |
4d9ec050 | 1271 | for (last = 0; last < 2; last++) { |
1d0d55da MN |
1272 | if (last == 0) { |
1273 | start = 0; | |
1274 | end = rl->last; | |
1275 | } else { | |
1276 | start = rl->last; | |
1277 | end = rl->n; | |
1278 | } | |
1279 | ||
1280 | memset(max_level, 0, MAX_RUN + 1); | |
1281 | memset(max_run, 0, MAX_LEVEL + 1); | |
1282 | memset(index_run, rl->n, MAX_RUN + 1); | |
4d9ec050 KT |
1283 | for (i = start; i < end; i++) { |
1284 | run = rl->table_run[i]; | |
1d0d55da MN |
1285 | level = rl->table_level[i]; |
1286 | if (index_run[run] == rl->n) | |
1287 | index_run[run] = i; | |
1288 | if (level > max_level[run]) | |
1289 | max_level[run] = level; | |
1290 | if (run > max_run[level]) | |
1291 | max_run[level] = run; | |
1292 | } | |
4d9ec050 | 1293 | if (static_store) |
3502a54f | 1294 | rl->max_level[last] = static_store[last]; |
073c2593 BP |
1295 | else |
1296 | rl->max_level[last] = av_malloc(MAX_RUN + 1); | |
1d0d55da | 1297 | memcpy(rl->max_level[last], max_level, MAX_RUN + 1); |
4d9ec050 KT |
1298 | if (static_store) |
1299 | rl->max_run[last] = static_store[last] + MAX_RUN + 1; | |
073c2593 | 1300 | else |
4d9ec050 | 1301 | rl->max_run[last] = av_malloc(MAX_LEVEL + 1); |
1d0d55da | 1302 | memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1); |
4d9ec050 | 1303 | if (static_store) |
3502a54f | 1304 | rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2; |
073c2593 BP |
1305 | else |
1306 | rl->index_run[last] = av_malloc(MAX_RUN + 1); | |
1d0d55da MN |
1307 | memcpy(rl->index_run[last], index_run, MAX_RUN + 1); |
1308 | } | |
1309 | } | |
1310 | ||
6fee1b90 | 1311 | av_cold void ff_init_vlc_rl(RLTable *rl) |
898d5d5d AJ |
1312 | { |
1313 | int i, q; | |
1314 | ||
4d9ec050 KT |
1315 | for (q = 0; q < 32; q++) { |
1316 | int qmul = q * 2; | |
1317 | int qadd = (q - 1) | 1; | |
898d5d5d | 1318 | |
4d9ec050 KT |
1319 | if (q == 0) { |
1320 | qmul = 1; | |
1321 | qadd = 0; | |
898d5d5d | 1322 | } |
4d9ec050 KT |
1323 | for (i = 0; i < rl->vlc.table_size; i++) { |
1324 | int code = rl->vlc.table[i][0]; | |
1325 | int len = rl->vlc.table[i][1]; | |
898d5d5d AJ |
1326 | int level, run; |
1327 | ||
4d9ec050 KT |
1328 | if (len == 0) { // illegal code |
1329 | run = 66; | |
1330 | level = MAX_LEVEL; | |
1331 | } else if (len < 0) { // more bits needed | |
1332 | run = 0; | |
1333 | level = code; | |
1334 | } else { | |
1335 | if (code == rl->n) { // esc | |
1336 | run = 66; | |
1337 | level = 0; | |
1338 | } else { | |
1339 | run = rl->table_run[code] + 1; | |
1340 | level = rl->table_level[code] * qmul + qadd; | |
1341 | if (code >= rl->last) run += 192; | |
898d5d5d AJ |
1342 | } |
1343 | } | |
4d9ec050 KT |
1344 | rl->rl_vlc[q][i].len = len; |
1345 | rl->rl_vlc[q][i].level = level; | |
1346 | rl->rl_vlc[q][i].run = run; | |
898d5d5d AJ |
1347 | } |
1348 | } | |
1349 | } | |
1350 | ||
282c6a1a | 1351 | static void release_unused_pictures(MpegEncContext *s) |
6a9c8594 AS |
1352 | { |
1353 | int i; | |
1354 | ||
1355 | /* release non reference frames */ | |
759001c5 | 1356 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
282c6a1a | 1357 | if (!s->picture[i].reference) |
759001c5 | 1358 | ff_mpeg_unref_picture(s, &s->picture[i]); |
6a9c8594 AS |
1359 | } |
1360 | } | |
1361 | ||
435c0b87 JG |
1362 | static inline int pic_is_unused(MpegEncContext *s, Picture *pic) |
1363 | { | |
a553c6a3 | 1364 | if (pic->f.buf[0] == NULL) |
435c0b87 | 1365 | return 1; |
759001c5 AK |
1366 | if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF)) |
1367 | return 1; | |
435c0b87 JG |
1368 | return 0; |
1369 | } | |
1370 | ||
1371 | static int find_unused_picture(MpegEncContext *s, int shared) | |
4d9ec050 | 1372 | { |
4e00e76b | 1373 | int i; |
115329f1 | 1374 | |
4d9ec050 | 1375 | if (shared) { |
759001c5 | 1376 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
a553c6a3 | 1377 | if (s->picture[i].f.buf[0] == NULL) |
657ccb5a | 1378 | return i; |
4e00e76b | 1379 | } |
4d9ec050 | 1380 | } else { |
759001c5 | 1381 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { |
435c0b87 | 1382 | if (pic_is_unused(s, &s->picture[i])) |
657ccb5a | 1383 | return i; |
4e00e76b MN |
1384 | } |
1385 | } | |
1386 | ||
4f820131 | 1387 | return AVERROR_INVALIDDATA; |
4e00e76b MN |
1388 | } |
1389 | ||
435c0b87 JG |
1390 | int ff_find_unused_picture(MpegEncContext *s, int shared) |
1391 | { | |
1392 | int ret = find_unused_picture(s, shared); | |
1393 | ||
759001c5 | 1394 | if (ret >= 0 && ret < MAX_PICTURE_COUNT) { |
435c0b87 JG |
1395 | if (s->picture[ret].needs_realloc) { |
1396 | s->picture[ret].needs_realloc = 0; | |
0b0a7a75 | 1397 | ff_free_picture_tables(&s->picture[ret]); |
759001c5 | 1398 | ff_mpeg_unref_picture(s, &s->picture[ret]); |
435c0b87 JG |
1399 | } |
1400 | } | |
1401 | return ret; | |
1402 | } | |
1403 | ||
c65dfac4 KT |
1404 | static void update_noise_reduction(MpegEncContext *s) |
1405 | { | |
821cb11f MN |
1406 | int intra, i; |
1407 | ||
c65dfac4 KT |
1408 | for (intra = 0; intra < 2; intra++) { |
1409 | if (s->dct_count[intra] > (1 << 16)) { | |
1410 | for (i = 0; i < 64; i++) { | |
1411 | s->dct_error_sum[intra][i] >>= 1; | |
821cb11f MN |
1412 | } |
1413 | s->dct_count[intra] >>= 1; | |
1414 | } | |
115329f1 | 1415 | |
c65dfac4 KT |
1416 | for (i = 0; i < 64; i++) { |
1417 | s->dct_offset[intra][i] = (s->avctx->noise_reduction * | |
1418 | s->dct_count[intra] + | |
1419 | s->dct_error_sum[intra][i] / 2) / | |
1420 | (s->dct_error_sum[intra][i] + 1); | |
821cb11f MN |
1421 | } |
1422 | } | |
1423 | } | |
1424 | ||
5f194811 | 1425 | /** |
c65dfac4 KT |
1426 | * generic function for encode/decode called after coding/decoding |
1427 | * the header and before a frame is coded/decoded. | |
5f194811 | 1428 | */ |
efd29844 | 1429 | int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) |
de6d9b64 | 1430 | { |
759001c5 | 1431 | int i, ret; |
357ec71f | 1432 | Picture *pic; |
160d679c | 1433 | s->mb_skipped = 0; |
0da71265 | 1434 | |
c65dfac4 | 1435 | /* mark & release old frames */ |
ee870491 AK |
1436 | if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && |
1437 | s->last_picture_ptr != s->next_picture_ptr && | |
a553c6a3 | 1438 | s->last_picture_ptr->f.buf[0]) { |
ee870491 AK |
1439 | ff_mpeg_unref_picture(s, s->last_picture_ptr); |
1440 | } | |
c65dfac4 | 1441 | |
ee870491 AK |
1442 | /* release forgotten pictures */ |
1443 | /* if (mpeg124/h263) */ | |
1444 | if (!s->encoding) { | |
1445 | for (i = 0; i < MAX_PICTURE_COUNT; i++) { | |
1446 | if (&s->picture[i] != s->last_picture_ptr && | |
1447 | &s->picture[i] != s->next_picture_ptr && | |
1448 | s->picture[i].reference && !s->picture[i].needs_realloc) { | |
1449 | if (!(avctx->active_thread_type & FF_THREAD_FRAME)) | |
1450 | av_log(avctx, AV_LOG_ERROR, | |
1451 | "releasing zombie picture\n"); | |
1452 | ff_mpeg_unref_picture(s, &s->picture[i]); | |
1e491e29 | 1453 | } |
d6db1c9c | 1454 | } |
ee870491 | 1455 | } |
d52b4abe | 1456 | |
4b796681 JG |
1457 | ff_mpeg_unref_picture(s, &s->current_picture); |
1458 | ||
c65dfac4 | 1459 | if (!s->encoding) { |
282c6a1a | 1460 | release_unused_pictures(s); |
e20c4069 | 1461 | |
c65dfac4 | 1462 | if (s->current_picture_ptr && |
a553c6a3 | 1463 | s->current_picture_ptr->f.buf[0] == NULL) { |
c65dfac4 KT |
1464 | // we already have a unused image |
1465 | // (maybe it was set before reading the header) | |
1466 | pic = s->current_picture_ptr; | |
1467 | } else { | |
1468 | i = ff_find_unused_picture(s, 0); | |
01fc5d66 JG |
1469 | if (i < 0) { |
1470 | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1471 | return i; | |
1472 | } | |
c65dfac4 | 1473 | pic = &s->picture[i]; |
5f194811 MN |
1474 | } |
1475 | ||
759001c5 | 1476 | pic->reference = 0; |
ba0c8981 | 1477 | if (!s->droppable) { |
19cac8e3 | 1478 | if (s->pict_type != AV_PICTURE_TYPE_B) |
759001c5 | 1479 | pic->reference = 3; |
2ddcf84b | 1480 | } |
b536d0aa | 1481 | |
657ccb5a | 1482 | pic->f.coded_picture_number = s->coded_picture_number++; |
115329f1 | 1483 | |
c65dfac4 | 1484 | if (ff_alloc_picture(s, pic, 0) < 0) |
f23a68df | 1485 | return -1; |
93a21abd | 1486 | |
c65dfac4 KT |
1487 | s->current_picture_ptr = pic; |
1488 | // FIXME use only the vars from current_pic | |
657ccb5a | 1489 | s->current_picture_ptr->f.top_field_first = s->top_field_first; |
36ef5369 AK |
1490 | if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || |
1491 | s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { | |
c65dfac4 KT |
1492 | if (s->picture_structure != PICT_FRAME) |
1493 | s->current_picture_ptr->f.top_field_first = | |
1494 | (s->picture_structure == PICT_TOP_FIELD) == s->first_field; | |
52654005 | 1495 | } |
c65dfac4 KT |
1496 | s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame && |
1497 | !s->progressive_sequence; | |
1498 | s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME; | |
1e491e29 | 1499 | } |
b7adc711 | 1500 | |
657ccb5a | 1501 | s->current_picture_ptr->f.pict_type = s->pict_type; |
c65dfac4 KT |
1502 | // if (s->flags && CODEC_FLAG_QSCALE) |
1503 | // s->current_picture_ptr->quality = s->new_picture_ptr->quality; | |
657ccb5a | 1504 | s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; |
9f2e61b6 | 1505 | |
759001c5 AK |
1506 | if ((ret = ff_mpeg_ref_picture(s, &s->current_picture, |
1507 | s->current_picture_ptr)) < 0) | |
1508 | return ret; | |
115329f1 | 1509 | |
19cac8e3 | 1510 | if (s->pict_type != AV_PICTURE_TYPE_B) { |
c65dfac4 | 1511 | s->last_picture_ptr = s->next_picture_ptr; |
ba0c8981 | 1512 | if (!s->droppable) |
c65dfac4 | 1513 | s->next_picture_ptr = s->current_picture_ptr; |
de6d9b64 | 1514 | } |
1218777f DB |
1515 | av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", |
1516 | s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr, | |
1517 | s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL, | |
1518 | s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL, | |
1519 | s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL, | |
ba0c8981 | 1520 | s->pict_type, s->droppable); |
c65dfac4 | 1521 | |
ee870491 | 1522 | if ((s->last_picture_ptr == NULL || |
a553c6a3 | 1523 | s->last_picture_ptr->f.buf[0] == NULL) && |
ee870491 AK |
1524 | (s->pict_type != AV_PICTURE_TYPE_I || |
1525 | s->picture_structure != PICT_FRAME)) { | |
1526 | int h_chroma_shift, v_chroma_shift; | |
1527 | av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt, | |
1528 | &h_chroma_shift, &v_chroma_shift); | |
1529 | if (s->pict_type != AV_PICTURE_TYPE_I) | |
1530 | av_log(avctx, AV_LOG_ERROR, | |
1531 | "warning: first frame is no keyframe\n"); | |
1532 | else if (s->picture_structure != PICT_FRAME) | |
1533 | av_log(avctx, AV_LOG_INFO, | |
1534 | "allocate dummy last picture for field based first keyframe\n"); | |
1535 | ||
1536 | /* Allocate a dummy frame */ | |
1537 | i = ff_find_unused_picture(s, 0); | |
1538 | if (i < 0) { | |
1539 | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1540 | return i; | |
d52b4abe | 1541 | } |
ee870491 | 1542 | s->last_picture_ptr = &s->picture[i]; |
feded990 AK |
1543 | |
1544 | s->last_picture_ptr->reference = 3; | |
1545 | s->last_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I; | |
1546 | ||
ee870491 AK |
1547 | if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) { |
1548 | s->last_picture_ptr = NULL; | |
1549 | return -1; | |
d52b4abe | 1550 | } |
d52b4abe | 1551 | |
ee870491 AK |
1552 | memset(s->last_picture_ptr->f.data[0], 0, |
1553 | avctx->height * s->last_picture_ptr->f.linesize[0]); | |
1554 | memset(s->last_picture_ptr->f.data[1], 0x80, | |
1555 | (avctx->height >> v_chroma_shift) * | |
1556 | s->last_picture_ptr->f.linesize[1]); | |
1557 | memset(s->last_picture_ptr->f.data[2], 0x80, | |
1558 | (avctx->height >> v_chroma_shift) * | |
1559 | s->last_picture_ptr->f.linesize[2]); | |
1560 | ||
1561 | ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0); | |
1562 | ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1); | |
1563 | } | |
1564 | if ((s->next_picture_ptr == NULL || | |
a553c6a3 | 1565 | s->next_picture_ptr->f.buf[0] == NULL) && |
ee870491 AK |
1566 | s->pict_type == AV_PICTURE_TYPE_B) { |
1567 | /* Allocate a dummy frame */ | |
1568 | i = ff_find_unused_picture(s, 0); | |
1569 | if (i < 0) { | |
1570 | av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n"); | |
1571 | return i; | |
759001c5 | 1572 | } |
ee870491 | 1573 | s->next_picture_ptr = &s->picture[i]; |
feded990 AK |
1574 | |
1575 | s->next_picture_ptr->reference = 3; | |
1576 | s->next_picture_ptr->f.pict_type = AV_PICTURE_TYPE_I; | |
1577 | ||
ee870491 AK |
1578 | if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) { |
1579 | s->next_picture_ptr = NULL; | |
1580 | return -1; | |
759001c5 | 1581 | } |
ee870491 AK |
1582 | ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0); |
1583 | ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1); | |
1584 | } | |
1585 | ||
1586 | if (s->last_picture_ptr) { | |
1587 | ff_mpeg_unref_picture(s, &s->last_picture); | |
a553c6a3 | 1588 | if (s->last_picture_ptr->f.buf[0] && |
ee870491 AK |
1589 | (ret = ff_mpeg_ref_picture(s, &s->last_picture, |
1590 | s->last_picture_ptr)) < 0) | |
1591 | return ret; | |
1592 | } | |
1593 | if (s->next_picture_ptr) { | |
1594 | ff_mpeg_unref_picture(s, &s->next_picture); | |
a553c6a3 | 1595 | if (s->next_picture_ptr->f.buf[0] && |
ee870491 AK |
1596 | (ret = ff_mpeg_ref_picture(s, &s->next_picture, |
1597 | s->next_picture_ptr)) < 0) | |
1598 | return ret; | |
1599 | } | |
115329f1 | 1600 | |
72072bf9 | 1601 | if (s->pict_type != AV_PICTURE_TYPE_I && |
a553c6a3 | 1602 | !(s->last_picture_ptr && s->last_picture_ptr->f.buf[0])) { |
72072bf9 LB |
1603 | av_log(s, AV_LOG_ERROR, |
1604 | "Non-reference picture received and no reference available\n"); | |
1605 | return AVERROR_INVALIDDATA; | |
1606 | } | |
3ab77000 | 1607 | |
19cac8e3 | 1608 | if (s->picture_structure!= PICT_FRAME) { |
b536d0aa | 1609 | int i; |
c65dfac4 KT |
1610 | for (i = 0; i < 4; i++) { |
1611 | if (s->picture_structure == PICT_BOTTOM_FIELD) { | |
1612 | s->current_picture.f.data[i] += | |
1613 | s->current_picture.f.linesize[i]; | |
115329f1 | 1614 | } |
657ccb5a DB |
1615 | s->current_picture.f.linesize[i] *= 2; |
1616 | s->last_picture.f.linesize[i] *= 2; | |
1617 | s->next_picture.f.linesize[i] *= 2; | |
b536d0aa MN |
1618 | } |
1619 | } | |
115329f1 | 1620 | |
5b22d6e1 | 1621 | s->err_recognition = avctx->err_recognition; |
aa388dba | 1622 | |
c65dfac4 KT |
1623 | /* set dequantizer, we can't do it during init as |
1624 | * it might change for mpeg4 and we can't do it in the header | |
1625 | * decode as init is not called for mpeg4 there yet */ | |
36ef5369 | 1626 | if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
d50635cd MN |
1627 | s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra; |
1628 | s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter; | |
c65dfac4 | 1629 | } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) { |
d50635cd MN |
1630 | s->dct_unquantize_intra = s->dct_unquantize_h263_intra; |
1631 | s->dct_unquantize_inter = s->dct_unquantize_h263_inter; | |
c65dfac4 | 1632 | } else { |
d50635cd MN |
1633 | s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra; |
1634 | s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter; | |
1635 | } | |
d6db1c9c | 1636 | |
c65dfac4 | 1637 | if (s->dct_error_sum) { |
821cb11f | 1638 | assert(s->avctx->noise_reduction && s->encoding); |
821cb11f MN |
1639 | update_noise_reduction(s); |
1640 | } | |
115329f1 | 1641 | |
19e30a58 DB |
1642 | #if FF_API_XVMC |
1643 | FF_DISABLE_DEPRECATION_WARNINGS | |
c65dfac4 | 1644 | if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) |
a002da79 | 1645 | return ff_xvmc_field_start(s, avctx); |
19e30a58 DB |
1646 | FF_ENABLE_DEPRECATION_WARNINGS |
1647 | #endif /* FF_API_XVMC */ | |
83344066 | 1648 | |
d6db1c9c | 1649 | return 0; |
de6d9b64 | 1650 | } |
21af69f7 | 1651 | |
381a7225 | 1652 | /* called after a frame has been decoded. */ |
efd29844 | 1653 | void ff_MPV_frame_end(MpegEncContext *s) |
de6d9b64 | 1654 | { |
19e30a58 DB |
1655 | #if FF_API_XVMC |
1656 | FF_DISABLE_DEPRECATION_WARNINGS | |
6a9c8594 | 1657 | /* redraw edges for the frame if decoding didn't complete */ |
c65dfac4 KT |
1658 | // just to make sure that all data is rendered. |
1659 | if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) { | |
a002da79 | 1660 | ff_xvmc_field_end(s); |
19e30a58 DB |
1661 | } else |
1662 | FF_ENABLE_DEPRECATION_WARNINGS | |
1663 | #endif /* FF_API_XVMC */ | |
381a7225 | 1664 | if (s->er.error_count && |
19e30a58 DB |
1665 | !s->avctx->hwaccel && |
1666 | s->unrestricted_mv && | |
1667 | s->current_picture.reference && | |
1668 | !s->intra_only && | |
1669 | !(s->flags & CODEC_FLAG_EMU_EDGE)) { | |
1670 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt); | |
1671 | int hshift = desc->log2_chroma_w; | |
1672 | int vshift = desc->log2_chroma_h; | |
1673 | s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize, | |
1674 | s->h_edge_pos, s->v_edge_pos, | |
1675 | EDGE_WIDTH, EDGE_WIDTH, | |
1676 | EDGE_TOP | EDGE_BOTTOM); | |
1677 | s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize, | |
1678 | s->h_edge_pos >> hshift, s->v_edge_pos >> vshift, | |
1679 | EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, | |
1680 | EDGE_TOP | EDGE_BOTTOM); | |
1681 | s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize, | |
1682 | s->h_edge_pos >> hshift, s->v_edge_pos >> vshift, | |
1683 | EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, | |
1684 | EDGE_TOP | EDGE_BOTTOM); | |
de6d9b64 | 1685 | } |
6a9c8594 | 1686 | |
5975626d | 1687 | emms_c(); |
115329f1 | 1688 | |
19cac8e3 | 1689 | if (s->current_picture.reference) |
759001c5 | 1690 | ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0); |
de6d9b64 FB |
1691 | } |
1692 | ||
7bc9090a | 1693 | /** |
c65dfac4 | 1694 | * Print debugging info for the given picture. |
7bc9090a | 1695 | */ |
759001c5 | 1696 | void ff_print_debug_info(MpegEncContext *s, Picture *p) |
c65dfac4 | 1697 | { |
759001c5 AK |
1698 | AVFrame *pict; |
1699 | if (s->avctx->hwaccel || !p || !p->mb_type) | |
c65dfac4 | 1700 | return; |
759001c5 | 1701 | pict = &p->f; |
7bc9090a | 1702 | |
c65dfac4 | 1703 | if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) { |
7bc9090a | 1704 | int x,y; |
115329f1 | 1705 | |
0c9bbaec WH |
1706 | av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: "); |
1707 | switch (pict->pict_type) { | |
c65dfac4 KT |
1708 | case AV_PICTURE_TYPE_I: |
1709 | av_log(s->avctx,AV_LOG_DEBUG,"I\n"); | |
1710 | break; | |
1711 | case AV_PICTURE_TYPE_P: | |
1712 | av_log(s->avctx,AV_LOG_DEBUG,"P\n"); | |
1713 | break; | |
1714 | case AV_PICTURE_TYPE_B: | |
1715 | av_log(s->avctx,AV_LOG_DEBUG,"B\n"); | |
1716 | break; | |
1717 | case AV_PICTURE_TYPE_S: | |
1718 | av_log(s->avctx,AV_LOG_DEBUG,"S\n"); | |
1719 | break; | |
1720 | case AV_PICTURE_TYPE_SI: | |
1721 | av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); | |
1722 | break; | |
1723 | case AV_PICTURE_TYPE_SP: | |
1724 | av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); | |
1725 | break; | |
0c9bbaec | 1726 | } |
c65dfac4 KT |
1727 | for (y = 0; y < s->mb_height; y++) { |
1728 | for (x = 0; x < s->mb_width; x++) { | |
1729 | if (s->avctx->debug & FF_DEBUG_SKIP) { | |
1730 | int count = s->mbskip_table[x + y * s->mb_stride]; | |
1731 | if (count > 9) | |
1732 | count = 9; | |
9b879566 | 1733 | av_log(s->avctx, AV_LOG_DEBUG, "%1d", count); |
7bc9090a | 1734 | } |
c65dfac4 KT |
1735 | if (s->avctx->debug & FF_DEBUG_QP) { |
1736 | av_log(s->avctx, AV_LOG_DEBUG, "%2d", | |
759001c5 | 1737 | p->qscale_table[x + y * s->mb_stride]); |
7bc9090a | 1738 | } |
c65dfac4 | 1739 | if (s->avctx->debug & FF_DEBUG_MB_TYPE) { |
759001c5 | 1740 | int mb_type = p->mb_type[x + y * s->mb_stride]; |
c65dfac4 KT |
1741 | // Type & MV direction |
1742 | if (IS_PCM(mb_type)) | |
9b879566 | 1743 | av_log(s->avctx, AV_LOG_DEBUG, "P"); |
c65dfac4 | 1744 | else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type)) |
9b879566 | 1745 | av_log(s->avctx, AV_LOG_DEBUG, "A"); |
c65dfac4 | 1746 | else if (IS_INTRA4x4(mb_type)) |
9b879566 | 1747 | av_log(s->avctx, AV_LOG_DEBUG, "i"); |
c65dfac4 | 1748 | else if (IS_INTRA16x16(mb_type)) |
9b879566 | 1749 | av_log(s->avctx, AV_LOG_DEBUG, "I"); |
c65dfac4 | 1750 | else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) |
9b879566 | 1751 | av_log(s->avctx, AV_LOG_DEBUG, "d"); |
c65dfac4 | 1752 | else if (IS_DIRECT(mb_type)) |
9b879566 | 1753 | av_log(s->avctx, AV_LOG_DEBUG, "D"); |
c65dfac4 | 1754 | else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) |
9b879566 | 1755 | av_log(s->avctx, AV_LOG_DEBUG, "g"); |
c65dfac4 | 1756 | else if (IS_GMC(mb_type)) |
9b879566 | 1757 | av_log(s->avctx, AV_LOG_DEBUG, "G"); |
c65dfac4 | 1758 | else if (IS_SKIP(mb_type)) |
9b879566 | 1759 | av_log(s->avctx, AV_LOG_DEBUG, "S"); |
c65dfac4 | 1760 | else if (!USES_LIST(mb_type, 1)) |
9b879566 | 1761 | av_log(s->avctx, AV_LOG_DEBUG, ">"); |
c65dfac4 | 1762 | else if (!USES_LIST(mb_type, 0)) |
9b879566 | 1763 | av_log(s->avctx, AV_LOG_DEBUG, "<"); |
c65dfac4 | 1764 | else { |
7bc9090a | 1765 | assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1)); |
9b879566 | 1766 | av_log(s->avctx, AV_LOG_DEBUG, "X"); |
7bc9090a | 1767 | } |
115329f1 | 1768 | |
c65dfac4 KT |
1769 | // segmentation |
1770 | if (IS_8X8(mb_type)) | |
9b879566 | 1771 | av_log(s->avctx, AV_LOG_DEBUG, "+"); |
c65dfac4 | 1772 | else if (IS_16X8(mb_type)) |
9b879566 | 1773 | av_log(s->avctx, AV_LOG_DEBUG, "-"); |
c65dfac4 | 1774 | else if (IS_8X16(mb_type)) |
30344a83 | 1775 | av_log(s->avctx, AV_LOG_DEBUG, "|"); |
c65dfac4 | 1776 | else if (IS_INTRA(mb_type) || IS_16X16(mb_type)) |
9b879566 | 1777 | av_log(s->avctx, AV_LOG_DEBUG, " "); |
7bc9090a | 1778 | else |
9b879566 | 1779 | av_log(s->avctx, AV_LOG_DEBUG, "?"); |
115329f1 DB |
1780 | |
1781 | ||
c65dfac4 | 1782 | if (IS_INTERLACED(mb_type)) |
9b879566 | 1783 | av_log(s->avctx, AV_LOG_DEBUG, "="); |
7bc9090a | 1784 | else |
9b879566 | 1785 | av_log(s->avctx, AV_LOG_DEBUG, " "); |
7bc9090a | 1786 | } |
7bc9090a | 1787 | } |
9b879566 | 1788 | av_log(s->avctx, AV_LOG_DEBUG, "\n"); |
7bc9090a MN |
1789 | } |
1790 | } | |
1791 | } | |
1792 | ||
6a9c8594 AS |
1793 | /** |
1794 | * find the lowest MB row referenced in the MVs | |
1795 | */ | |
efd29844 | 1796 | int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir) |
6a9c8594 AS |
1797 | { |
1798 | int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample; | |
1799 | int my, off, i, mvs; | |
1800 | ||
6bcdfe48 MN |
1801 | if (s->picture_structure != PICT_FRAME || s->mcsel) |
1802 | goto unhandled; | |
6a9c8594 AS |
1803 | |
1804 | switch (s->mv_type) { | |
1805 | case MV_TYPE_16X16: | |
1806 | mvs = 1; | |
1807 | break; | |
1808 | case MV_TYPE_16X8: | |
1809 | mvs = 2; | |
1810 | break; | |
1811 | case MV_TYPE_8X8: | |
1812 | mvs = 4; | |
1813 | break; | |
1814 | default: | |
1815 | goto unhandled; | |
1816 | } | |
1817 | ||
1818 | for (i = 0; i < mvs; i++) { | |
1819 | my = s->mv[dir][i][1]<<qpel_shift; | |
1820 | my_max = FFMAX(my_max, my); | |
1821 | my_min = FFMIN(my_min, my); | |
1822 | } | |
1823 | ||
1824 | off = (FFMAX(-my_min, my_max) + 63) >> 6; | |
1825 | ||
1826 | return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1); | |
1827 | unhandled: | |
1828 | return s->mb_height-1; | |
1829 | } | |
1830 | ||
3ada94ba BF |
1831 | /* put block[] to dest[] */ |
1832 | static inline void put_dct(MpegEncContext *s, | |
88bd7fdc | 1833 | int16_t *block, int i, uint8_t *dest, int line_size, int qscale) |
178fcca8 | 1834 | { |
3ada94ba BF |
1835 | s->dct_unquantize_intra(s, block, i, qscale); |
1836 | s->dsp.idct_put (dest, line_size, block); | |
1837 | } | |
da9c9637 | 1838 | |
3ada94ba BF |
1839 | /* add block[] to dest[] */ |
1840 | static inline void add_dct(MpegEncContext *s, | |
88bd7fdc | 1841 | int16_t *block, int i, uint8_t *dest, int line_size) |
3ada94ba BF |
1842 | { |
1843 | if (s->block_last_index[i] >= 0) { | |
1844 | s->dsp.idct_add (dest, line_size, block); | |
1845 | } | |
1846 | } | |
2417652e | 1847 | |
115329f1 | 1848 | static inline void add_dequant_dct(MpegEncContext *s, |
88bd7fdc | 1849 | int16_t *block, int i, uint8_t *dest, int line_size, int qscale) |
0f440e02 | 1850 | { |
de6d9b64 | 1851 | if (s->block_last_index[i] >= 0) { |
d50635cd | 1852 | s->dct_unquantize_inter(s, block, i, qscale); |
9dbcbd92 | 1853 | |
b0368839 | 1854 | s->dsp.idct_add (dest, line_size, block); |
de6d9b64 FB |
1855 | } |
1856 | } | |
1857 | ||
7f2fe444 | 1858 | /** |
58c42af7 | 1859 | * Clean dc, ac, coded_block for the current non-intra MB. |
7f2fe444 MN |
1860 | */ |
1861 | void ff_clean_intra_table_entries(MpegEncContext *s) | |
1862 | { | |
137c8468 | 1863 | int wrap = s->b8_stride; |
7f2fe444 | 1864 | int xy = s->block_index[0]; |
115329f1 DB |
1865 | |
1866 | s->dc_val[0][xy ] = | |
1867 | s->dc_val[0][xy + 1 ] = | |
7f2fe444 MN |
1868 | s->dc_val[0][xy + wrap] = |
1869 | s->dc_val[0][xy + 1 + wrap] = 1024; | |
1870 | /* ac pred */ | |
0c1a9eda ZK |
1871 | memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t)); |
1872 | memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t)); | |
7f2fe444 MN |
1873 | if (s->msmpeg4_version>=3) { |
1874 | s->coded_block[xy ] = | |
1875 | s->coded_block[xy + 1 ] = | |
1876 | s->coded_block[xy + wrap] = | |
1877 | s->coded_block[xy + 1 + wrap] = 0; | |
1878 | } | |
1879 | /* chroma */ | |
137c8468 MN |
1880 | wrap = s->mb_stride; |
1881 | xy = s->mb_x + s->mb_y * wrap; | |
7f2fe444 MN |
1882 | s->dc_val[1][xy] = |
1883 | s->dc_val[2][xy] = 1024; | |
1884 | /* ac pred */ | |
0c1a9eda ZK |
1885 | memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t)); |
1886 | memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t)); | |
115329f1 | 1887 | |
137c8468 | 1888 | s->mbintra_table[xy]= 0; |
7f2fe444 MN |
1889 | } |
1890 | ||
de6d9b64 FB |
1891 | /* generic function called after a macroblock has been parsed by the |
1892 | decoder or after it has been encoded by the encoder. | |
1893 | ||
1894 | Important variables used: | |
1895 | s->mb_intra : true if intra macroblock | |
1896 | s->mv_dir : motion vector direction | |
1897 | s->mv_type : motion vector type | |
1898 | s->mv : motion vector | |
1899 | s->interlaced_dct : true if interlaced dct used (mpeg2) | |
1900 | */ | |
54816a3e | 1901 | static av_always_inline |
88bd7fdc | 1902 | void MPV_decode_mb_internal(MpegEncContext *s, int16_t block[12][64], |
2bcbd984 | 1903 | int is_mpeg12) |
de6d9b64 | 1904 | { |
7bc9090a | 1905 | const int mb_xy = s->mb_y * s->mb_stride + s->mb_x; |
19e30a58 DB |
1906 | |
1907 | #if FF_API_XVMC | |
1908 | FF_DISABLE_DEPRECATION_WARNINGS | |
83344066 | 1909 | if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){ |
78f9a878 | 1910 | ff_xvmc_decode_mb(s);//xvmc uses pblocks |
2e7b4c84 IK |
1911 | return; |
1912 | } | |
19e30a58 DB |
1913 | FF_ENABLE_DEPRECATION_WARNINGS |
1914 | #endif /* FF_API_XVMC */ | |
de6d9b64 | 1915 | |
8289c6fa | 1916 | if(s->avctx->debug&FF_DEBUG_DCT_COEFF) { |
759001c5 | 1917 | /* print DCT coefficients */ |
8289c6fa | 1918 | int i,j; |
c4fb3b03 MN |
1919 | av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y); |
1920 | for(i=0; i<6; i++){ | |
1921 | for(j=0; j<64; j++){ | |
759001c5 | 1922 | av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]); |
c4fb3b03 MN |
1923 | } |
1924 | av_log(s->avctx, AV_LOG_DEBUG, "\n"); | |
1925 | } | |
8289c6fa WH |
1926 | } |
1927 | ||
759001c5 | 1928 | s->current_picture.qscale_table[mb_xy] = s->qscale; |
79e7b305 | 1929 | |
de6d9b64 FB |
1930 | /* update DC predictors for P macroblocks */ |
1931 | if (!s->mb_intra) { | |
bd7c626a | 1932 | if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) { |
0f440e02 | 1933 | if(s->mbintra_table[mb_xy]) |
7f2fe444 | 1934 | ff_clean_intra_table_entries(s); |
de6d9b64 | 1935 | } else { |
7f2fe444 MN |
1936 | s->last_dc[0] = |
1937 | s->last_dc[1] = | |
de6d9b64 FB |
1938 | s->last_dc[2] = 128 << s->intra_dc_precision; |
1939 | } | |
1940 | } | |
bd7c626a | 1941 | else if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) |
0f440e02 | 1942 | s->mbintra_table[mb_xy]=1; |
bff6ecaa | 1943 | |
975a1447 | 1944 | if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc |
0c1a9eda | 1945 | uint8_t *dest_y, *dest_cb, *dest_cr; |
0f440e02 | 1946 | int dct_linesize, dct_offset; |
b3184779 MN |
1947 | op_pixels_func (*op_pix)[4]; |
1948 | qpel_mc_func (*op_qpix)[16]; | |
657ccb5a DB |
1949 | const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics |
1950 | const int uvlinesize = s->current_picture.f.linesize[1]; | |
2bcbd984 MR |
1951 | const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band; |
1952 | const int block_size = 8; | |
3bb4e23a | 1953 | |
1e491e29 | 1954 | /* avoid copy if macroblock skipped in last frame too */ |
1e491e29 MN |
1955 | /* skip only during decoding as we might trash the buffers during encoding a bit */ |
1956 | if(!s->encoding){ | |
0c1a9eda | 1957 | uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy]; |
1e491e29 | 1958 | |
160d679c MM |
1959 | if (s->mb_skipped) { |
1960 | s->mb_skipped= 0; | |
975a1447 | 1961 | assert(s->pict_type!=AV_PICTURE_TYPE_I); |
8400b126 | 1962 | *mbskip_ptr = 1; |
759001c5 | 1963 | } else if(!s->current_picture.reference) { |
8400b126 | 1964 | *mbskip_ptr = 1; |
f943e138 | 1965 | } else{ |
3bb4e23a FB |
1966 | *mbskip_ptr = 0; /* not skipped */ |
1967 | } | |
3994623d | 1968 | } |
115329f1 | 1969 | |
ffdff4d7 | 1970 | dct_linesize = linesize << s->interlaced_dct; |
3dc99a18 | 1971 | dct_offset = s->interlaced_dct ? linesize : linesize * block_size; |
115329f1 | 1972 | |
b68ab260 MN |
1973 | if(readable){ |
1974 | dest_y= s->dest[0]; | |
1975 | dest_cb= s->dest[1]; | |
1976 | dest_cr= s->dest[2]; | |
1977 | }else{ | |
9c3d33d6 | 1978 | dest_y = s->b_scratchpad; |
ae35f5e1 | 1979 | dest_cb= s->b_scratchpad+16*linesize; |
ffdff4d7 | 1980 | dest_cr= s->b_scratchpad+32*linesize; |
b68ab260 | 1981 | } |
178fcca8 | 1982 | |
de6d9b64 FB |
1983 | if (!s->mb_intra) { |
1984 | /* motion handling */ | |
dfb706da | 1985 | /* decoding or more than one mb_type (MC was already done otherwise) */ |
7d1c3fc1 | 1986 | if(!s->encoding){ |
6a9c8594 | 1987 | |
27237d52 | 1988 | if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) { |
6a9c8594 | 1989 | if (s->mv_dir & MV_DIR_FORWARD) { |
759001c5 | 1990 | ff_thread_await_progress(&s->last_picture_ptr->tf, |
47c0ac96 DB |
1991 | ff_MPV_lowest_referenced_row(s, 0), |
1992 | 0); | |
6a9c8594 AS |
1993 | } |
1994 | if (s->mv_dir & MV_DIR_BACKWARD) { | |
759001c5 | 1995 | ff_thread_await_progress(&s->next_picture_ptr->tf, |
47c0ac96 DB |
1996 | ff_MPV_lowest_referenced_row(s, 1), |
1997 | 0); | |
6a9c8594 AS |
1998 | } |
1999 | } | |
2000 | ||
2bcbd984 MR |
2001 | op_qpix= s->me.qpel_put; |
2002 | if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){ | |
f4fed5a2 | 2003 | op_pix = s->hdsp.put_pixels_tab; |
178fcca8 | 2004 | }else{ |
f4fed5a2 | 2005 | op_pix = s->hdsp.put_no_rnd_pixels_tab; |
2bcbd984 MR |
2006 | } |
2007 | if (s->mv_dir & MV_DIR_FORWARD) { | |
7a851153 | 2008 | ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix); |
f4fed5a2 | 2009 | op_pix = s->hdsp.avg_pixels_tab; |
2bcbd984 MR |
2010 | op_qpix= s->me.qpel_avg; |
2011 | } | |
2012 | if (s->mv_dir & MV_DIR_BACKWARD) { | |
7a851153 | 2013 | ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix); |
9dbcbd92 | 2014 | } |
de6d9b64 FB |
2015 | } |
2016 | ||
0f440e02 | 2017 | /* skip dequant / idct if we are really late ;) */ |
8c3eba7c | 2018 | if(s->avctx->skip_idct){ |
975a1447 SS |
2019 | if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) |
2020 | ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) | |
8c3eba7c MN |
2021 | || s->avctx->skip_idct >= AVDISCARD_ALL) |
2022 | goto skip_idct; | |
2023 | } | |
0f440e02 | 2024 | |
de6d9b64 | 2025 | /* add dct residue */ |
36ef5369 AK |
2026 | if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO |
2027 | || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){ | |
178fcca8 MN |
2028 | add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale); |
2029 | add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale); | |
2030 | add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale); | |
2031 | add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale); | |
0f440e02 | 2032 | |
49fb20cb | 2033 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
2d974017 BC |
2034 | if (s->chroma_y_shift){ |
2035 | add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale); | |
2036 | add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale); | |
2037 | }else{ | |
2038 | dct_linesize >>= 1; | |
2039 | dct_offset >>=1; | |
2040 | add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale); | |
2041 | add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale); | |
2042 | add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale); | |
2043 | add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale); | |
2044 | } | |
b50eef3a | 2045 | } |
36ef5369 | 2046 | } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){ |
178fcca8 MN |
2047 | add_dct(s, block[0], 0, dest_y , dct_linesize); |
2048 | add_dct(s, block[1], 1, dest_y + block_size, dct_linesize); | |
2049 | add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize); | |
2050 | add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize); | |
de6d9b64 | 2051 | |
49fb20cb | 2052 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
ffdff4d7 IK |
2053 | if(s->chroma_y_shift){//Chroma420 |
2054 | add_dct(s, block[4], 4, dest_cb, uvlinesize); | |
2055 | add_dct(s, block[5], 5, dest_cr, uvlinesize); | |
2056 | }else{ | |
2057 | //chroma422 | |
2058 | dct_linesize = uvlinesize << s->interlaced_dct; | |
3dc99a18 | 2059 | dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8; |
ffdff4d7 IK |
2060 | |
2061 | add_dct(s, block[4], 4, dest_cb, dct_linesize); | |
2062 | add_dct(s, block[5], 5, dest_cr, dct_linesize); | |
2063 | add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize); | |
2064 | add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize); | |
2065 | if(!s->chroma_x_shift){//Chroma444 | |
2066 | add_dct(s, block[8], 8, dest_cb+8, dct_linesize); | |
2067 | add_dct(s, block[9], 9, dest_cr+8, dct_linesize); | |
2068 | add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize); | |
2069 | add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize); | |
2070 | } | |
2071 | } | |
2072 | }//fi gray | |
2073 | } | |
d702a2e6 | 2074 | else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) { |
1457ab52 | 2075 | ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr); |
0f440e02 | 2076 | } |
de6d9b64 FB |
2077 | } else { |
2078 | /* dct only in intra block */ | |
36ef5369 | 2079 | if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){ |
178fcca8 MN |
2080 | put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale); |
2081 | put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale); | |
2082 | put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale); | |
2083 | put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale); | |
a0201736 | 2084 | |
49fb20cb | 2085 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
2d974017 BC |
2086 | if(s->chroma_y_shift){ |
2087 | put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale); | |
2088 | put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale); | |
2089 | }else{ | |
2090 | dct_offset >>=1; | |
2091 | dct_linesize >>=1; | |
2092 | put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale); | |
2093 | put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale); | |
2094 | put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale); | |
3ada94ba | 2095 | put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale); |
477ab036 MN |
2096 | } |
2097 | } | |
2098 | }else{ | |
3ada94ba BF |
2099 | s->dsp.idct_put(dest_y , dct_linesize, block[0]); |
2100 | s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]); | |
2101 | s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]); | |
2102 | s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]); | |
fbb89806 | 2103 | |
49fb20cb | 2104 | if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){ |
3ada94ba BF |
2105 | if(s->chroma_y_shift){ |
2106 | s->dsp.idct_put(dest_cb, uvlinesize, block[4]); | |
2107 | s->dsp.idct_put(dest_cr, uvlinesize, block[5]); | |
2108 | }else{ | |
fbb89806 | 2109 | |
3ada94ba | 2110 | dct_linesize = uvlinesize << s->interlaced_dct; |
3dc99a18 | 2111 | dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8; |
9981dfc6 | 2112 | |
3ada94ba BF |
2113 | s->dsp.idct_put(dest_cb, dct_linesize, block[4]); |
2114 | s->dsp.idct_put(dest_cr, dct_linesize, block[5]); | |
2115 | s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]); | |
2116 | s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]); | |
2117 | if(!s->chroma_x_shift){//Chroma444 | |
2118 | s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]); | |
2119 | s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]); | |
2120 | s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]); | |
2121 | s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]); | |
2122 | } | |
2123 | } | |
2124 | }//gray | |
9981dfc6 | 2125 | } |
477ab036 | 2126 | } |
3ada94ba BF |
2127 | skip_idct: |
2128 | if(!readable){ | |
f4fed5a2 RB |
2129 | s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16); |
2130 | s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift); | |
2131 | s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift); | |
34f60ee6 | 2132 | } |
477ab036 | 2133 | } |
477ab036 MN |
2134 | } |
2135 | ||
88bd7fdc | 2136 | void ff_MPV_decode_mb(MpegEncContext *s, int16_t block[12][64]){ |
b250f9c6 | 2137 | #if !CONFIG_SMALL |
bd7c626a | 2138 | if(s->out_format == FMT_MPEG1) { |
2bcbd984 | 2139 | MPV_decode_mb_internal(s, block, 1); |
bd7c626a KC |
2140 | } else |
2141 | #endif | |
2bcbd984 | 2142 | MPV_decode_mb_internal(s, block, 0); |
77ea0d4b MN |
2143 | } |
2144 | ||
3ada94ba | 2145 | /** |
3ada94ba BF |
2146 | * @param h is the normal height, this will be reduced automatically if needed for the last row |
2147 | */ | |
1d0feb5d AK |
2148 | void ff_draw_horiz_band(AVCodecContext *avctx, DSPContext *dsp, Picture *cur, |
2149 | Picture *last, int y, int h, int picture_structure, | |
2150 | int first_field, int draw_edges, int low_delay, | |
2151 | int v_edge_pos, int h_edge_pos) | |
2152 | { | |
2153 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); | |
2154 | int hshift = desc->log2_chroma_w; | |
2155 | int vshift = desc->log2_chroma_h; | |
2156 | const int field_pic = picture_structure != PICT_FRAME; | |
6a9c8594 AS |
2157 | if(field_pic){ |
2158 | h <<= 1; | |
2159 | y <<= 1; | |
2160 | } | |
2161 | ||
1d0feb5d | 2162 | if (!avctx->hwaccel && |
1d0feb5d | 2163 | draw_edges && |
759001c5 | 2164 | cur->reference && |
1d0feb5d AK |
2165 | !(avctx->flags & CODEC_FLAG_EMU_EDGE)) { |
2166 | int *linesize = cur->f.linesize; | |
6a9c8594 AS |
2167 | int sides = 0, edge_h; |
2168 | if (y==0) sides |= EDGE_TOP; | |
1d0feb5d AK |
2169 | if (y + h >= v_edge_pos) |
2170 | sides |= EDGE_BOTTOM; | |
6a9c8594 | 2171 | |
1d0feb5d | 2172 | edge_h= FFMIN(h, v_edge_pos - y); |
6a9c8594 | 2173 | |
1d0feb5d AK |
2174 | dsp->draw_edges(cur->f.data[0] + y * linesize[0], |
2175 | linesize[0], h_edge_pos, edge_h, | |
2176 | EDGE_WIDTH, EDGE_WIDTH, sides); | |
2177 | dsp->draw_edges(cur->f.data[1] + (y >> vshift) * linesize[1], | |
2178 | linesize[1], h_edge_pos >> hshift, edge_h >> vshift, | |
2179 | EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides); | |
2180 | dsp->draw_edges(cur->f.data[2] + (y >> vshift) * linesize[2], | |
2181 | linesize[2], h_edge_pos >> hshift, edge_h >> vshift, | |
2182 | EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, sides); | |
6a9c8594 AS |
2183 | } |
2184 | ||
1d0feb5d | 2185 | h = FFMIN(h, avctx->height - y); |
6a9c8594 | 2186 | |
1d0feb5d | 2187 | if(field_pic && first_field && !(avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return; |
6a9c8594 | 2188 | |
1d0feb5d | 2189 | if (avctx->draw_horiz_band) { |
3ada94ba | 2190 | AVFrame *src; |
560f773c JR |
2191 | int offset[AV_NUM_DATA_POINTERS]; |
2192 | int i; | |
77ea0d4b | 2193 | |
1d0feb5d AK |
2194 | if(cur->f.pict_type == AV_PICTURE_TYPE_B || low_delay || |
2195 | (avctx->slice_flags & SLICE_FLAG_CODED_ORDER)) | |
2196 | src = &cur->f; | |
2197 | else if (last) | |
2198 | src = &last->f; | |
3ada94ba BF |
2199 | else |
2200 | return; | |
115329f1 | 2201 | |
1d0feb5d | 2202 | if (cur->f.pict_type == AV_PICTURE_TYPE_B && |
19cac8e3 | 2203 | picture_structure == PICT_FRAME && |
1d0feb5d | 2204 | avctx->codec_id != AV_CODEC_ID_SVQ3) { |
560f773c JR |
2205 | for (i = 0; i < AV_NUM_DATA_POINTERS; i++) |
2206 | offset[i] = 0; | |
77ea0d4b | 2207 | }else{ |
1d0feb5d | 2208 | offset[0]= y * src->linesize[0]; |
3ada94ba | 2209 | offset[1]= |
1d0feb5d | 2210 | offset[2]= (y >> vshift) * src->linesize[1]; |
560f773c JR |
2211 | for (i = 3; i < AV_NUM_DATA_POINTERS; i++) |
2212 | offset[i] = 0; | |
77ea0d4b | 2213 | } |
115329f1 | 2214 | |
3ada94ba | 2215 | emms_c(); |
77ea0d4b | 2216 | |
1d0feb5d AK |
2217 | avctx->draw_horiz_band(avctx, src, offset, |
2218 | y, picture_structure, h); | |
3ada94ba BF |
2219 | } |
2220 | } | |
115329f1 | 2221 | |
1d0feb5d AK |
2222 | void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h) |
2223 | { | |
2224 | int draw_edges = s->unrestricted_mv && !s->intra_only; | |
2225 | ff_draw_horiz_band(s->avctx, &s->dsp, &s->current_picture, | |
2226 | &s->last_picture, y, h, s->picture_structure, | |
2227 | s->first_field, draw_edges, s->low_delay, | |
2228 | s->v_edge_pos, s->h_edge_pos); | |
2229 | } | |
2230 | ||
3ada94ba | 2231 | void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename |
657ccb5a DB |
2232 | const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics |
2233 | const int uvlinesize = s->current_picture.f.linesize[1]; | |
2bcbd984 | 2234 | const int mb_size= 4; |
77ea0d4b | 2235 | |
3ada94ba BF |
2236 | s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2; |
2237 | s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2; | |
2238 | s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2; | |
2239 | s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2; | |
2240 | s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; | |
2241 | s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1; | |
2242 | //block_index is not used by mpeg2, so it is not affected by chroma_format | |
115329f1 | 2243 | |
657ccb5a DB |
2244 | s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size); |
2245 | s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift)); | |
2246 | s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift)); | |
115329f1 | 2247 | |
975a1447 | 2248 | if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME)) |
3ada94ba | 2249 | { |
078cdecf | 2250 | if(s->picture_structure==PICT_FRAME){ |
3ada94ba BF |
2251 | s->dest[0] += s->mb_y * linesize << mb_size; |
2252 | s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); | |
2253 | s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift); | |
078cdecf MN |
2254 | }else{ |
2255 | s->dest[0] += (s->mb_y>>1) * linesize << mb_size; | |
2256 | s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift); | |
2257 | s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift); | |
2258 | assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD)); | |
2259 | } | |
77ea0d4b | 2260 | } |
77ea0d4b | 2261 | } |
115329f1 | 2262 | |
0b016eb9 DB |
2263 | /** |
2264 | * Permute an 8x8 block. | |
2265 | * @param block the block which will be permuted according to the given permutation vector | |
2266 | * @param permutation the permutation vector | |
2267 | * @param last the last non zero coefficient in scantable order, used to speed the permutation up | |
2268 | * @param scantable the used scantable, this is only used to speed the permutation up, the block is not | |
2269 | * (inverse) permutated to scantable order! | |
2270 | */ | |
2271 | void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last) | |
2272 | { | |
2273 | int i; | |
2274 | int16_t temp[64]; | |
2275 | ||
2276 | if(last<=0) return; | |
2277 | //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations | |
2278 | ||
2279 | for(i=0; i<=last; i++){ | |
2280 | const int j= scantable[i]; | |
2281 | temp[j]= block[j]; | |
2282 | block[j]=0; | |
2283 | } | |
2284 | ||
2285 | for(i=0; i<=last; i++){ | |
2286 | const int j= scantable[i]; | |
2287 | const int perm_j= permutation[j]; | |
2288 | block[perm_j]= temp[j]; | |
2289 | } | |
2290 | } | |
2291 | ||
3ada94ba BF |
2292 | void ff_mpeg_flush(AVCodecContext *avctx){ |
2293 | int i; | |
2294 | MpegEncContext *s = avctx->priv_data; | |
77ea0d4b | 2295 | |
3ada94ba BF |
2296 | if(s==NULL || s->picture==NULL) |
2297 | return; | |
77ea0d4b | 2298 | |
759001c5 AK |
2299 | for (i = 0; i < MAX_PICTURE_COUNT; i++) |
2300 | ff_mpeg_unref_picture(s, &s->picture[i]); | |
3ada94ba | 2301 | s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL; |
115329f1 | 2302 | |
feec9349 HL |
2303 | ff_mpeg_unref_picture(s, &s->current_picture); |
2304 | ff_mpeg_unref_picture(s, &s->last_picture); | |
2305 | ff_mpeg_unref_picture(s, &s->next_picture); | |
2306 | ||
3ada94ba | 2307 | s->mb_x= s->mb_y= 0; |
7801d21d | 2308 | |
3ada94ba BF |
2309 | s->parse_context.state= -1; |
2310 | s->parse_context.frame_start_found= 0; | |
2311 | s->parse_context.overread= 0; | |
2312 | s->parse_context.overread_index= 0; | |
2313 | s->parse_context.index= 0; | |
2314 | s->parse_context.last_index= 0; | |
2315 | s->bitstream_buffer_size=0; | |
2316 | s->pp_time=0; | |
de6d9b64 FB |
2317 | } |
2318 | ||
115329f1 | 2319 | static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, |
88bd7fdc | 2320 | int16_t *block, int n, int qscale) |
de6d9b64 | 2321 | { |
badaf88e | 2322 | int i, level, nCoeffs; |
0c1a9eda | 2323 | const uint16_t *quant_matrix; |
de6d9b64 | 2324 | |
2ad1516a | 2325 | nCoeffs= s->block_last_index[n]; |
115329f1 DB |
2326 | |
2327 | if (n < 4) | |
d50635cd MN |
2328 | block[0] = block[0] * s->y_dc_scale; |
2329 | else | |
2330 | block[0] = block[0] * s->c_dc_scale; | |
2331 | /* XXX: only mpeg1 */ | |
2332 | quant_matrix = s->intra_matrix; | |
2333 | for(i=1;i<=nCoeffs;i++) { | |
2334 | int j= s->intra_scantable.permutated[i]; | |
2335 | level = block[j]; | |
2336 | if (level) { | |
2337 | if (level < 0) { | |
2338 | level = -level; | |
2339 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2340 | level = (level - 1) | 1; | |
2341 | level = -level; | |
2342 | } else { | |
2343 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2344 | level = (level - 1) | 1; | |
de6d9b64 | 2345 | } |
d50635cd | 2346 | block[j] = level; |
de6d9b64 | 2347 | } |
d50635cd MN |
2348 | } |
2349 | } | |
2350 | ||
115329f1 | 2351 | static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, |
88bd7fdc | 2352 | int16_t *block, int n, int qscale) |
d50635cd MN |
2353 | { |
2354 | int i, level, nCoeffs; | |
2355 | const uint16_t *quant_matrix; | |
2356 | ||
2357 | nCoeffs= s->block_last_index[n]; | |
115329f1 | 2358 | |
d50635cd MN |
2359 | quant_matrix = s->inter_matrix; |
2360 | for(i=0; i<=nCoeffs; i++) { | |
2361 | int j= s->intra_scantable.permutated[i]; | |
2362 | level = block[j]; | |
2363 | if (level) { | |
2364 | if (level < 0) { | |
2365 | level = -level; | |
2366 | level = (((level << 1) + 1) * qscale * | |
2367 | ((int) (quant_matrix[j]))) >> 4; | |
2368 | level = (level - 1) | 1; | |
2369 | level = -level; | |
2370 | } else { | |
2371 | level = (((level << 1) + 1) * qscale * | |
2372 | ((int) (quant_matrix[j]))) >> 4; | |
2373 | level = (level - 1) | 1; | |
de6d9b64 | 2374 | } |
d50635cd | 2375 | block[j] = level; |
de6d9b64 FB |
2376 | } |
2377 | } | |
2378 | } | |
21af69f7 | 2379 | |
115329f1 | 2380 | static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, |
88bd7fdc | 2381 | int16_t *block, int n, int qscale) |
9dbf1ddd MN |
2382 | { |
2383 | int i, level, nCoeffs; | |
0c1a9eda | 2384 | const uint16_t *quant_matrix; |
9dbf1ddd | 2385 | |
2ad1516a MN |
2386 | if(s->alternate_scan) nCoeffs= 63; |
2387 | else nCoeffs= s->block_last_index[n]; | |
115329f1 DB |
2388 | |
2389 | if (n < 4) | |
d50635cd MN |
2390 | block[0] = block[0] * s->y_dc_scale; |
2391 | else | |
2392 | block[0] = block[0] * s->c_dc_scale; | |
2393 | quant_matrix = s->intra_matrix; | |
2394 | for(i=1;i<=nCoeffs;i++) { | |
2395 | int j= s->intra_scantable.permutated[i]; | |
2396 | level = block[j]; | |
2397 | if (level) { | |
2398 | if (level < 0) { | |
2399 | level = -level; | |
2400 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2401 | level = -level; | |
2402 | } else { | |
2403 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2404 | } | |
2405 | block[j] = level; | |
2406 | } | |
2407 | } | |
2408 | } | |
2409 | ||
e27b6e62 | 2410 | static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s, |
88bd7fdc | 2411 | int16_t *block, int n, int qscale) |
e27b6e62 MN |
2412 | { |
2413 | int i, level, nCoeffs; | |
2414 | const uint16_t *quant_matrix; | |
2415 | int sum=-1; | |
2416 | ||
2417 | if(s->alternate_scan) nCoeffs= 63; | |
2418 | else nCoeffs= s->block_last_index[n]; | |
2419 | ||
2420 | if (n < 4) | |
2421 | block[0] = block[0] * s->y_dc_scale; | |
2422 | else | |
2423 | block[0] = block[0] * s->c_dc_scale; | |
2424 | quant_matrix = s->intra_matrix; | |
2425 | for(i=1;i<=nCoeffs;i++) { | |
2426 | int j= s->intra_scantable.permutated[i]; | |
2427 | level = block[j]; | |
2428 | if (level) { | |
2429 | if (level < 0) { | |
2430 | level = -level; | |
2431 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2432 | level = -level; | |
2433 | } else { | |
2434 | level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2435 | } | |
2436 | block[j] = level; | |
2437 | sum+=level; | |
2438 | } | |
2439 | } | |
2440 | block[63]^=sum&1; | |
2441 | } | |
2442 | ||
115329f1 | 2443 | static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, |
88bd7fdc | 2444 | int16_t *block, int n, int qscale) |
d50635cd MN |
2445 | { |
2446 | int i, level, nCoeffs; | |
2447 | const uint16_t *quant_matrix; | |
2448 | int sum=-1; | |
2449 | ||
2450 | if(s->alternate_scan) nCoeffs= 63; | |
2451 | else nCoeffs= s->block_last_index[n]; | |
115329f1 | 2452 | |
d50635cd MN |
2453 | quant_matrix = s->inter_matrix; |
2454 | for(i=0; i<=nCoeffs; i++) { | |
2455 | int j= s->intra_scantable.permutated[i]; | |
2456 | level = block[j]; | |
2457 | if (level) { | |
2458 | if (level < 0) { | |
2459 | level = -level; | |
2460 | level = (((level << 1) + 1) * qscale * | |
2461 | ((int) (quant_matrix[j]))) >> 4; | |
2462 | level = -level; | |
2463 | } else { | |
2464 | level = (((level << 1) + 1) * qscale * | |
2465 | ((int) (quant_matrix[j]))) >> 4; | |
2466 | } | |
2467 | block[j] = level; | |
2468 | sum+=level; | |
2469 | } | |
2470 | } | |
2471 | block[63]^=sum&1; | |
2472 | } | |
2473 | ||
115329f1 | 2474 | static void dct_unquantize_h263_intra_c(MpegEncContext *s, |
88bd7fdc | 2475 | int16_t *block, int n, int qscale) |
d50635cd MN |
2476 | { |
2477 | int i, level, qmul, qadd; | |
2478 | int nCoeffs; | |
115329f1 | 2479 | |
d50635cd | 2480 | assert(s->block_last_index[n]>=0); |
115329f1 | 2481 | |
d50635cd | 2482 | qmul = qscale << 1; |
115329f1 | 2483 | |
d50635cd | 2484 | if (!s->h263_aic) { |
115329f1 | 2485 | if (n < 4) |
9dbf1ddd MN |
2486 | block[0] = block[0] * s->y_dc_scale; |
2487 | else | |
2488 | block[0] = block[0] * s->c_dc_scale; | |
d50635cd MN |
2489 | qadd = (qscale - 1) | 1; |
2490 | }else{ | |
2491 | qadd = 0; | |
2492 | } | |
2493 | if(s->ac_pred) | |
2494 | nCoeffs=63; | |
2495 | else | |
2496 | nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; | |
2497 | ||
2498 | for(i=1; i<=nCoeffs; i++) { | |
2499 | level = block[i]; | |
2500 | if (level) { | |
2501 | if (level < 0) { | |
2502 | level = level * qmul - qadd; | |
2503 | } else { | |
2504 | level = level * qmul + qadd; | |
9dbf1ddd | 2505 | } |
d50635cd | 2506 | block[i] = level; |
9dbf1ddd | 2507 | } |
9dbf1ddd MN |
2508 | } |
2509 | } | |
2510 | ||
115329f1 | 2511 | static void dct_unquantize_h263_inter_c(MpegEncContext *s, |
88bd7fdc | 2512 | int16_t *block, int n, int qscale) |
21af69f7 FB |
2513 | { |
2514 | int i, level, qmul, qadd; | |
badaf88e | 2515 | int nCoeffs; |
115329f1 | 2516 | |
2ad1516a | 2517 | assert(s->block_last_index[n]>=0); |
115329f1 | 2518 | |
2ad1516a MN |
2519 | qadd = (qscale - 1) | 1; |
2520 | qmul = qscale << 1; | |
115329f1 | 2521 | |
d50635cd | 2522 | nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; |
21af69f7 | 2523 | |
d50635cd | 2524 | for(i=0; i<=nCoeffs; i++) { |
21af69f7 FB |
2525 | level = block[i]; |
2526 | if (level) { | |
2527 | if (level < 0) { | |
2528 | level = level * qmul - qadd; | |
2529 | } else { | |
2530 | level = level * qmul + qadd; | |
2531 | } | |
21af69f7 FB |
2532 | block[i] = level; |
2533 | } | |
2534 | } | |
2535 | } | |
de6d9b64 | 2536 | |
b776e3d1 AJ |
2537 | /** |
2538 | * set qscale and update qscale dependent variables. | |
2539 | */ | |
2540 | void ff_set_qscale(MpegEncContext * s, int qscale) | |
2541 | { | |
2542 | if (qscale < 1) | |
2543 | qscale = 1; | |
2544 | else if (qscale > 31) | |
2545 | qscale = 31; | |
2546 | ||
2547 | s->qscale = qscale; | |
2548 | s->chroma_qscale= s->chroma_qscale_table[qscale]; | |
2549 | ||
2550 | s->y_dc_scale= s->y_dc_scale_table[ qscale ]; | |
2551 | s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ]; | |
2552 | } | |
6a9c8594 | 2553 | |
efd29844 | 2554 | void ff_MPV_report_decode_progress(MpegEncContext *s) |
6a9c8594 | 2555 | { |
54974c62 | 2556 | if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred) |
759001c5 | 2557 | ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0); |
6a9c8594 | 2558 | } |
54974c62 | 2559 | |
d88738e4 | 2560 | #if CONFIG_ERROR_RESILIENCE |
54974c62 AK |
2561 | void ff_mpeg_er_frame_start(MpegEncContext *s) |
2562 | { | |
2563 | ERContext *er = &s->er; | |
2564 | ||
2565 | er->cur_pic = s->current_picture_ptr; | |
2566 | er->last_pic = s->last_picture_ptr; | |
2567 | er->next_pic = s->next_picture_ptr; | |
2568 | ||
2569 | er->pp_time = s->pp_time; | |
2570 | er->pb_time = s->pb_time; | |
2571 | er->quarter_sample = s->quarter_sample; | |
2572 | er->partitioned_frame = s->partitioned_frame; | |
2573 | ||
2574 | ff_er_frame_start(er); | |
2575 | } | |
d88738e4 | 2576 | #endif /* CONFIG_ERROR_RESILIENCE */ |