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