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