ae6fb7e9a5a05387ab190cd9fbf10b4843eda1a7
[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 "libavutil/timer.h"
35 #include "avcodec.h"
36 #include "blockdsp.h"
37 #include "idctdsp.h"
38 #include "internal.h"
39 #include "mathops.h"
40 #include "mpeg_er.h"
41 #include "mpegutils.h"
42 #include "mpegvideo.h"
43 #include "mpegvideodata.h"
44 #include "mjpegenc.h"
45 #include "msmpeg4.h"
46 #include "qpeldsp.h"
47 #include "xvmc_internal.h"
48 #include "thread.h"
49 #include "wmv2.h"
50 #include <limits.h>
51
52 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
53 int16_t *block, int n, int qscale)
54 {
55 int i, level, nCoeffs;
56 const uint16_t *quant_matrix;
57
58 nCoeffs= s->block_last_index[n];
59
60 if (n < 4)
61 block[0] = block[0] * s->y_dc_scale;
62 else
63 block[0] = block[0] * s->c_dc_scale;
64 /* XXX: only mpeg1 */
65 quant_matrix = s->intra_matrix;
66 for(i=1;i<=nCoeffs;i++) {
67 int j= s->intra_scantable.permutated[i];
68 level = block[j];
69 if (level) {
70 if (level < 0) {
71 level = -level;
72 level = (int)(level * qscale * quant_matrix[j]) >> 3;
73 level = (level - 1) | 1;
74 level = -level;
75 } else {
76 level = (int)(level * qscale * quant_matrix[j]) >> 3;
77 level = (level - 1) | 1;
78 }
79 block[j] = level;
80 }
81 }
82 }
83
84 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
85 int16_t *block, int n, int qscale)
86 {
87 int i, level, nCoeffs;
88 const uint16_t *quant_matrix;
89
90 nCoeffs= s->block_last_index[n];
91
92 quant_matrix = s->inter_matrix;
93 for(i=0; i<=nCoeffs; i++) {
94 int j= s->intra_scantable.permutated[i];
95 level = block[j];
96 if (level) {
97 if (level < 0) {
98 level = -level;
99 level = (((level << 1) + 1) * qscale *
100 ((int) (quant_matrix[j]))) >> 4;
101 level = (level - 1) | 1;
102 level = -level;
103 } else {
104 level = (((level << 1) + 1) * qscale *
105 ((int) (quant_matrix[j]))) >> 4;
106 level = (level - 1) | 1;
107 }
108 block[j] = level;
109 }
110 }
111 }
112
113 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
114 int16_t *block, int n, int qscale)
115 {
116 int i, level, nCoeffs;
117 const uint16_t *quant_matrix;
118
119 if(s->alternate_scan) nCoeffs= 63;
120 else nCoeffs= s->block_last_index[n];
121
122 if (n < 4)
123 block[0] = block[0] * s->y_dc_scale;
124 else
125 block[0] = block[0] * s->c_dc_scale;
126 quant_matrix = s->intra_matrix;
127 for(i=1;i<=nCoeffs;i++) {
128 int j= s->intra_scantable.permutated[i];
129 level = block[j];
130 if (level) {
131 if (level < 0) {
132 level = -level;
133 level = (int)(level * qscale * quant_matrix[j]) >> 3;
134 level = -level;
135 } else {
136 level = (int)(level * qscale * quant_matrix[j]) >> 3;
137 }
138 block[j] = level;
139 }
140 }
141 }
142
143 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
144 int16_t *block, int n, int qscale)
145 {
146 int i, level, nCoeffs;
147 const uint16_t *quant_matrix;
148 int sum=-1;
149
150 if(s->alternate_scan) nCoeffs= 63;
151 else nCoeffs= s->block_last_index[n];
152
153 if (n < 4)
154 block[0] = block[0] * s->y_dc_scale;
155 else
156 block[0] = block[0] * s->c_dc_scale;
157 quant_matrix = s->intra_matrix;
158 for(i=1;i<=nCoeffs;i++) {
159 int j= s->intra_scantable.permutated[i];
160 level = block[j];
161 if (level) {
162 if (level < 0) {
163 level = -level;
164 level = (int)(level * qscale * quant_matrix[j]) >> 3;
165 level = -level;
166 } else {
167 level = (int)(level * qscale * quant_matrix[j]) >> 3;
168 }
169 block[j] = level;
170 sum+=level;
171 }
172 }
173 block[63]^=sum&1;
174 }
175
176 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
177 int16_t *block, int n, int qscale)
178 {
179 int i, level, nCoeffs;
180 const uint16_t *quant_matrix;
181 int sum=-1;
182
183 if(s->alternate_scan) nCoeffs= 63;
184 else nCoeffs= s->block_last_index[n];
185
186 quant_matrix = s->inter_matrix;
187 for(i=0; i<=nCoeffs; i++) {
188 int j= s->intra_scantable.permutated[i];
189 level = block[j];
190 if (level) {
191 if (level < 0) {
192 level = -level;
193 level = (((level << 1) + 1) * qscale *
194 ((int) (quant_matrix[j]))) >> 4;
195 level = -level;
196 } else {
197 level = (((level << 1) + 1) * qscale *
198 ((int) (quant_matrix[j]))) >> 4;
199 }
200 block[j] = level;
201 sum+=level;
202 }
203 }
204 block[63]^=sum&1;
205 }
206
207 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
208 int16_t *block, int n, int qscale)
209 {
210 int i, level, qmul, qadd;
211 int nCoeffs;
212
213 assert(s->block_last_index[n]>=0);
214
215 qmul = qscale << 1;
216
217 if (!s->h263_aic) {
218 if (n < 4)
219 block[0] = block[0] * s->y_dc_scale;
220 else
221 block[0] = block[0] * s->c_dc_scale;
222 qadd = (qscale - 1) | 1;
223 }else{
224 qadd = 0;
225 }
226 if(s->ac_pred)
227 nCoeffs=63;
228 else
229 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
230
231 for(i=1; i<=nCoeffs; i++) {
232 level = block[i];
233 if (level) {
234 if (level < 0) {
235 level = level * qmul - qadd;
236 } else {
237 level = level * qmul + qadd;
238 }
239 block[i] = level;
240 }
241 }
242 }
243
244 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
245 int16_t *block, int n, int qscale)
246 {
247 int i, level, qmul, qadd;
248 int nCoeffs;
249
250 assert(s->block_last_index[n]>=0);
251
252 qadd = (qscale - 1) | 1;
253 qmul = qscale << 1;
254
255 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
256
257 for(i=0; i<=nCoeffs; i++) {
258 level = block[i];
259 if (level) {
260 if (level < 0) {
261 level = level * qmul - qadd;
262 } else {
263 level = level * qmul + qadd;
264 }
265 block[i] = level;
266 }
267 }
268 }
269
270 /* init common dct for both encoder and decoder */
271 static av_cold int dct_init(MpegEncContext *s)
272 {
273 ff_blockdsp_init(&s->bdsp, s->avctx);
274 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
275 ff_mpegvideodsp_init(&s->mdsp);
276 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
277
278 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
279 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
280 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
281 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
282 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
283 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
284 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
285 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
286
287 if (HAVE_INTRINSICS_NEON)
288 ff_mpv_common_init_neon(s);
289
290 if (ARCH_ARM)
291 ff_mpv_common_init_arm(s);
292 if (ARCH_PPC)
293 ff_mpv_common_init_ppc(s);
294 if (ARCH_X86)
295 ff_mpv_common_init_x86(s);
296
297 return 0;
298 }
299
300 av_cold void ff_mpv_idct_init(MpegEncContext *s)
301 {
302 ff_idctdsp_init(&s->idsp, s->avctx);
303
304 /* load & permutate scantables
305 * note: only wmv uses different ones
306 */
307 if (s->alternate_scan) {
308 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
309 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
310 } else {
311 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
312 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
313 }
314 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
315 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
316 }
317
318 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
319 {
320 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
321 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
322 s->mb_stride, s->mb_height, s->b8_stride,
323 &s->linesize, &s->uvlinesize);
324 }
325
326 static int init_duplicate_context(MpegEncContext *s)
327 {
328 int y_size = s->b8_stride * (2 * s->mb_height + 1);
329 int c_size = s->mb_stride * (s->mb_height + 1);
330 int yc_size = y_size + 2 * c_size;
331 int i;
332
333 s->sc.edge_emu_buffer =
334 s->me.scratchpad =
335 s->me.temp =
336 s->sc.rd_scratchpad =
337 s->sc.b_scratchpad =
338 s->sc.obmc_scratchpad = NULL;
339
340 if (s->encoding) {
341 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
342 ME_MAP_SIZE * sizeof(uint32_t), fail)
343 FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map,
344 ME_MAP_SIZE * sizeof(uint32_t), fail)
345 if (s->avctx->noise_reduction) {
346 FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum,
347 2 * 64 * sizeof(int), fail)
348 }
349 }
350 FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
351 s->block = s->blocks[0];
352
353 for (i = 0; i < 12; i++) {
354 s->pblocks[i] = &s->block[i];
355 }
356 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
357 // exchange uv
358 int16_t (*tmp)[64];
359 tmp = s->pblocks[4];
360 s->pblocks[4] = s->pblocks[5];
361 s->pblocks[5] = tmp;
362 }
363
364 if (s->out_format == FMT_H263) {
365 /* ac values */
366 FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base,
367 yc_size * sizeof(int16_t) * 16, fail);
368 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
369 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
370 s->ac_val[2] = s->ac_val[1] + c_size;
371 }
372
373 return 0;
374 fail:
375 return -1; // free() through ff_mpv_common_end()
376 }
377
378 static void free_duplicate_context(MpegEncContext *s)
379 {
380 if (!s)
381 return;
382
383 av_freep(&s->sc.edge_emu_buffer);
384 av_freep(&s->me.scratchpad);
385 s->me.temp =
386 s->sc.rd_scratchpad =
387 s->sc.b_scratchpad =
388 s->sc.obmc_scratchpad = NULL;
389
390 av_freep(&s->dct_error_sum);
391 av_freep(&s->me.map);
392 av_freep(&s->me.score_map);
393 av_freep(&s->blocks);
394 av_freep(&s->ac_val_base);
395 s->block = NULL;
396 }
397
398 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
399 {
400 #define COPY(a) bak->a = src->a
401 COPY(sc.edge_emu_buffer);
402 COPY(me.scratchpad);
403 COPY(me.temp);
404 COPY(sc.rd_scratchpad);
405 COPY(sc.b_scratchpad);
406 COPY(sc.obmc_scratchpad);
407 COPY(me.map);
408 COPY(me.score_map);
409 COPY(blocks);
410 COPY(block);
411 COPY(start_mb_y);
412 COPY(end_mb_y);
413 COPY(me.map_generation);
414 COPY(pb);
415 COPY(dct_error_sum);
416 COPY(dct_count[0]);
417 COPY(dct_count[1]);
418 COPY(ac_val_base);
419 COPY(ac_val[0]);
420 COPY(ac_val[1]);
421 COPY(ac_val[2]);
422 #undef COPY
423 }
424
425 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
426 {
427 MpegEncContext bak;
428 int i, ret;
429 // FIXME copy only needed parts
430 // START_TIMER
431 backup_duplicate_context(&bak, dst);
432 memcpy(dst, src, sizeof(MpegEncContext));
433 backup_duplicate_context(dst, &bak);
434 for (i = 0; i < 12; i++) {
435 dst->pblocks[i] = &dst->block[i];
436 }
437 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
438 // exchange uv
439 int16_t (*tmp)[64];
440 tmp = dst->pblocks[4];
441 dst->pblocks[4] = dst->pblocks[5];
442 dst->pblocks[5] = tmp;
443 }
444 if (!dst->sc.edge_emu_buffer &&
445 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
446 &dst->sc, dst->linesize)) < 0) {
447 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
448 "scratch buffers.\n");
449 return ret;
450 }
451 // STOP_TIMER("update_duplicate_context")
452 // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
453 return 0;
454 }
455
456 int ff_mpeg_update_thread_context(AVCodecContext *dst,
457 const AVCodecContext *src)
458 {
459 int i, ret;
460 MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
461
462 if (dst == src || !s1->context_initialized)
463 return 0;
464
465 // FIXME can parameters change on I-frames?
466 // in that case dst may need a reinit
467 if (!s->context_initialized) {
468 int err;
469 memcpy(s, s1, sizeof(MpegEncContext));
470
471 s->avctx = dst;
472 s->bitstream_buffer = NULL;
473 s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
474
475 ff_mpv_idct_init(s);
476 if ((err = ff_mpv_common_init(s)) < 0)
477 return err;
478 }
479
480 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
481 int err;
482 s->context_reinit = 0;
483 s->height = s1->height;
484 s->width = s1->width;
485 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
486 return err;
487 }
488
489 s->avctx->coded_height = s1->avctx->coded_height;
490 s->avctx->coded_width = s1->avctx->coded_width;
491 s->avctx->width = s1->avctx->width;
492 s->avctx->height = s1->avctx->height;
493
494 s->coded_picture_number = s1->coded_picture_number;
495 s->picture_number = s1->picture_number;
496
497 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
498 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
499 if (s1->picture[i].f->buf[0] &&
500 (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
501 return ret;
502 }
503
504 #define UPDATE_PICTURE(pic)\
505 do {\
506 ff_mpeg_unref_picture(s->avctx, &s->pic);\
507 if (s1->pic.f->buf[0])\
508 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
509 else\
510 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
511 if (ret < 0)\
512 return ret;\
513 } while (0)
514
515 UPDATE_PICTURE(current_picture);
516 UPDATE_PICTURE(last_picture);
517 UPDATE_PICTURE(next_picture);
518
519 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
520 ((pic && pic >= old_ctx->picture && \
521 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
522 &new_ctx->picture[pic - old_ctx->picture] : NULL)
523
524 s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
525 s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
526 s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
527
528 // Error/bug resilience
529 s->next_p_frame_damaged = s1->next_p_frame_damaged;
530 s->workaround_bugs = s1->workaround_bugs;
531
532 // MPEG4 timing info
533 memcpy(&s->last_time_base, &s1->last_time_base,
534 (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
535 (char *) &s1->last_time_base);
536
537 // B-frame info
538 s->max_b_frames = s1->max_b_frames;
539 s->low_delay = s1->low_delay;
540 s->droppable = s1->droppable;
541
542 // DivX handling (doesn't work)
543 s->divx_packed = s1->divx_packed;
544
545 if (s1->bitstream_buffer) {
546 if (s1->bitstream_buffer_size +
547 AV_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
548 av_fast_malloc(&s->bitstream_buffer,
549 &s->allocated_bitstream_buffer_size,
550 s1->allocated_bitstream_buffer_size);
551 s->bitstream_buffer_size = s1->bitstream_buffer_size;
552 memcpy(s->bitstream_buffer, s1->bitstream_buffer,
553 s1->bitstream_buffer_size);
554 memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
555 AV_INPUT_BUFFER_PADDING_SIZE);
556 }
557
558 // linesize dependend scratch buffer allocation
559 if (!s->sc.edge_emu_buffer)
560 if (s1->linesize) {
561 if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
562 &s->sc, s1->linesize) < 0) {
563 av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
564 "scratch buffers.\n");
565 return AVERROR(ENOMEM);
566 }
567 } else {
568 av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
569 "be allocated due to unknown size.\n");
570 return AVERROR_BUG;
571 }
572
573 // MPEG2/interlacing info
574 memcpy(&s->progressive_sequence, &s1->progressive_sequence,
575 (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
576
577 if (!s1->first_field) {
578 s->last_pict_type = s1->pict_type;
579 if (s1->current_picture_ptr)
580 s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
581 }
582
583 return 0;
584 }
585
586 /**
587 * Set the given MpegEncContext to common defaults
588 * (same for encoding and decoding).
589 * The changed fields will not depend upon the
590 * prior state of the MpegEncContext.
591 */
592 void ff_mpv_common_defaults(MpegEncContext *s)
593 {
594 s->y_dc_scale_table =
595 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
596 s->chroma_qscale_table = ff_default_chroma_qscale_table;
597 s->progressive_frame = 1;
598 s->progressive_sequence = 1;
599 s->picture_structure = PICT_FRAME;
600
601 s->coded_picture_number = 0;
602 s->picture_number = 0;
603
604 s->f_code = 1;
605 s->b_code = 1;
606
607 s->slice_context_count = 1;
608 }
609
610 /**
611 * Set the given MpegEncContext to defaults for decoding.
612 * the changed fields will not depend upon
613 * the prior state of the MpegEncContext.
614 */
615 void ff_mpv_decode_defaults(MpegEncContext *s)
616 {
617 ff_mpv_common_defaults(s);
618 }
619
620 /**
621 * Initialize and allocates MpegEncContext fields dependent on the resolution.
622 */
623 static int init_context_frame(MpegEncContext *s)
624 {
625 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
626
627 s->mb_width = (s->width + 15) / 16;
628 s->mb_stride = s->mb_width + 1;
629 s->b8_stride = s->mb_width * 2 + 1;
630 mb_array_size = s->mb_height * s->mb_stride;
631 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
632
633 /* set default edge pos, will be overriden
634 * in decode_header if needed */
635 s->h_edge_pos = s->mb_width * 16;
636 s->v_edge_pos = s->mb_height * 16;
637
638 s->mb_num = s->mb_width * s->mb_height;
639
640 s->block_wrap[0] =
641 s->block_wrap[1] =
642 s->block_wrap[2] =
643 s->block_wrap[3] = s->b8_stride;
644 s->block_wrap[4] =
645 s->block_wrap[5] = s->mb_stride;
646
647 y_size = s->b8_stride * (2 * s->mb_height + 1);
648 c_size = s->mb_stride * (s->mb_height + 1);
649 yc_size = y_size + 2 * c_size;
650
651 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
652 fail); // error ressilience code looks cleaner with this
653 for (y = 0; y < s->mb_height; y++)
654 for (x = 0; x < s->mb_width; x++)
655 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
656
657 s->mb_index2xy[s->mb_height * s->mb_width] =
658 (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
659
660 if (s->encoding) {
661 /* Allocate MV tables */
662 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
663 mv_table_size * 2 * sizeof(int16_t), fail);
664 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
665 mv_table_size * 2 * sizeof(int16_t), fail);
666 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
667 mv_table_size * 2 * sizeof(int16_t), fail);
668 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
669 mv_table_size * 2 * sizeof(int16_t), fail);
670 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
671 mv_table_size * 2 * sizeof(int16_t), fail);
672 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
673 mv_table_size * 2 * sizeof(int16_t), fail);
674 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
675 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
676 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
677 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
678 s->mb_stride + 1;
679 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
680 s->mb_stride + 1;
681 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
682
683 /* Allocate MB type table */
684 FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
685 sizeof(uint16_t), fail); // needed for encoding
686
687 FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
688 sizeof(int), fail);
689
690 FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
691 mb_array_size * sizeof(float), fail);
692 FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
693 mb_array_size * sizeof(float), fail);
694
695 }
696
697 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
698 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
699 /* interlaced direct mode decoding tables */
700 for (i = 0; i < 2; i++) {
701 int j, k;
702 for (j = 0; j < 2; j++) {
703 for (k = 0; k < 2; k++) {
704 FF_ALLOCZ_OR_GOTO(s->avctx,
705 s->b_field_mv_table_base[i][j][k],
706 mv_table_size * 2 * sizeof(int16_t),
707 fail);
708 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
709 s->mb_stride + 1;
710 }
711 FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
712 mb_array_size * 2 * sizeof(uint8_t), fail);
713 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
714 mv_table_size * 2 * sizeof(int16_t), fail);
715 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
716 + s->mb_stride + 1;
717 }
718 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
719 mb_array_size * 2 * sizeof(uint8_t), fail);
720 }
721 }
722 if (s->out_format == FMT_H263) {
723 /* cbp values */
724 FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
725 s->coded_block = s->coded_block_base + s->b8_stride + 1;
726
727 /* cbp, ac_pred, pred_dir */
728 FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
729 mb_array_size * sizeof(uint8_t), fail);
730 FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
731 mb_array_size * sizeof(uint8_t), fail);
732 }
733
734 if (s->h263_pred || s->h263_plus || !s->encoding) {
735 /* dc values */
736 // MN: we need these for error resilience of intra-frames
737 FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
738 yc_size * sizeof(int16_t), fail);
739 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
740 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
741 s->dc_val[2] = s->dc_val[1] + c_size;
742 for (i = 0; i < yc_size; i++)
743 s->dc_val_base[i] = 1024;
744 }
745
746 /* which mb is a intra block */
747 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
748 memset(s->mbintra_table, 1, mb_array_size);
749
750 /* init macroblock skip table */
751 FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
752 // Note the + 1 is for a quicker mpeg4 slice_end detection
753
754 return ff_mpeg_er_init(s);
755 fail:
756 return AVERROR(ENOMEM);
757 }
758
759 /**
760 * init common structure for both encoder and decoder.
761 * this assumes that some variables like width/height are already set
762 */
763 av_cold int ff_mpv_common_init(MpegEncContext *s)
764 {
765 int i;
766 int nb_slices = (HAVE_THREADS &&
767 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
768 s->avctx->thread_count : 1;
769
770 if (s->encoding && s->avctx->slices)
771 nb_slices = s->avctx->slices;
772
773 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
774 s->mb_height = (s->height + 31) / 32 * 2;
775 else
776 s->mb_height = (s->height + 15) / 16;
777
778 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
779 av_log(s->avctx, AV_LOG_ERROR,
780 "decoding to AV_PIX_FMT_NONE is not supported.\n");
781 return -1;
782 }
783
784 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
785 int max_slices;
786 if (s->mb_height)
787 max_slices = FFMIN(MAX_THREADS, s->mb_height);
788 else
789 max_slices = MAX_THREADS;
790 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
791 " reducing to %d\n", nb_slices, max_slices);
792 nb_slices = max_slices;
793 }
794
795 if ((s->width || s->height) &&
796 av_image_check_size(s->width, s->height, 0, s->avctx))
797 return -1;
798
799 dct_init(s);
800
801 /* set chroma shifts */
802 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
803 &s->chroma_x_shift,
804 &s->chroma_y_shift);
805
806 /* convert fourcc to upper case */
807 s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
808
809 FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
810 MAX_PICTURE_COUNT * sizeof(Picture), fail);
811 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
812 s->picture[i].f = av_frame_alloc();
813 if (!s->picture[i].f)
814 goto fail;
815 }
816 memset(&s->next_picture, 0, sizeof(s->next_picture));
817 memset(&s->last_picture, 0, sizeof(s->last_picture));
818 memset(&s->current_picture, 0, sizeof(s->current_picture));
819 memset(&s->new_picture, 0, sizeof(s->new_picture));
820 s->next_picture.f = av_frame_alloc();
821 if (!s->next_picture.f)
822 goto fail;
823 s->last_picture.f = av_frame_alloc();
824 if (!s->last_picture.f)
825 goto fail;
826 s->current_picture.f = av_frame_alloc();
827 if (!s->current_picture.f)
828 goto fail;
829 s->new_picture.f = av_frame_alloc();
830 if (!s->new_picture.f)
831 goto fail;
832
833 if (s->width && s->height) {
834 if (init_context_frame(s))
835 goto fail;
836
837 s->parse_context.state = -1;
838 }
839
840 s->context_initialized = 1;
841 s->thread_context[0] = s;
842
843 if (s->width && s->height) {
844 if (nb_slices > 1) {
845 for (i = 1; i < nb_slices; i++) {
846 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
847 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
848 }
849
850 for (i = 0; i < nb_slices; i++) {
851 if (init_duplicate_context(s->thread_context[i]) < 0)
852 goto fail;
853 s->thread_context[i]->start_mb_y =
854 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
855 s->thread_context[i]->end_mb_y =
856 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
857 }
858 } else {
859 if (init_duplicate_context(s) < 0)
860 goto fail;
861 s->start_mb_y = 0;
862 s->end_mb_y = s->mb_height;
863 }
864 s->slice_context_count = nb_slices;
865 }
866
867 return 0;
868 fail:
869 ff_mpv_common_end(s);
870 return -1;
871 }
872
873 /**
874 * Frees and resets MpegEncContext fields depending on the resolution.
875 * Is used during resolution changes to avoid a full reinitialization of the
876 * codec.
877 */
878 static void free_context_frame(MpegEncContext *s)
879 {
880 int i, j, k;
881
882 av_freep(&s->mb_type);
883 av_freep(&s->p_mv_table_base);
884 av_freep(&s->b_forw_mv_table_base);
885 av_freep(&s->b_back_mv_table_base);
886 av_freep(&s->b_bidir_forw_mv_table_base);
887 av_freep(&s->b_bidir_back_mv_table_base);
888 av_freep(&s->b_direct_mv_table_base);
889 s->p_mv_table = NULL;
890 s->b_forw_mv_table = NULL;
891 s->b_back_mv_table = NULL;
892 s->b_bidir_forw_mv_table = NULL;
893 s->b_bidir_back_mv_table = NULL;
894 s->b_direct_mv_table = NULL;
895 for (i = 0; i < 2; i++) {
896 for (j = 0; j < 2; j++) {
897 for (k = 0; k < 2; k++) {
898 av_freep(&s->b_field_mv_table_base[i][j][k]);
899 s->b_field_mv_table[i][j][k] = NULL;
900 }
901 av_freep(&s->b_field_select_table[i][j]);
902 av_freep(&s->p_field_mv_table_base[i][j]);
903 s->p_field_mv_table[i][j] = NULL;
904 }
905 av_freep(&s->p_field_select_table[i]);
906 }
907
908 av_freep(&s->dc_val_base);
909 av_freep(&s->coded_block_base);
910 av_freep(&s->mbintra_table);
911 av_freep(&s->cbp_table);
912 av_freep(&s->pred_dir_table);
913
914 av_freep(&s->mbskip_table);
915
916 av_freep(&s->er.error_status_table);
917 av_freep(&s->er.er_temp_buffer);
918 av_freep(&s->mb_index2xy);
919 av_freep(&s->lambda_table);
920 av_freep(&s->cplx_tab);
921 av_freep(&s->bits_tab);
922
923 s->linesize = s->uvlinesize = 0;
924 }
925
926 int ff_mpv_common_frame_size_change(MpegEncContext *s)
927 {
928 int i, err = 0;
929
930 if (s->slice_context_count > 1) {
931 for (i = 0; i < s->slice_context_count; i++) {
932 free_duplicate_context(s->thread_context[i]);
933 }
934 for (i = 1; i < s->slice_context_count; i++) {
935 av_freep(&s->thread_context[i]);
936 }
937 } else
938 free_duplicate_context(s);
939
940 free_context_frame(s);
941
942 if (s->picture)
943 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
944 s->picture[i].needs_realloc = 1;
945 }
946
947 s->last_picture_ptr =
948 s->next_picture_ptr =
949 s->current_picture_ptr = NULL;
950
951 // init
952 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
953 s->mb_height = (s->height + 31) / 32 * 2;
954 else
955 s->mb_height = (s->height + 15) / 16;
956
957 if ((s->width || s->height) &&
958 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
959 goto fail;
960
961 if ((err = init_context_frame(s)))
962 goto fail;
963
964 s->thread_context[0] = s;
965
966 if (s->width && s->height) {
967 int nb_slices = s->slice_context_count;
968 if (nb_slices > 1) {
969 for (i = 1; i < nb_slices; i++) {
970 s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
971 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
972 }
973
974 for (i = 0; i < nb_slices; i++) {
975 if ((err = init_duplicate_context(s->thread_context[i])) < 0)
976 goto fail;
977 s->thread_context[i]->start_mb_y =
978 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
979 s->thread_context[i]->end_mb_y =
980 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
981 }
982 } else {
983 if (init_duplicate_context(s) < 0)
984 goto fail;
985 s->start_mb_y = 0;
986 s->end_mb_y = s->mb_height;
987 }
988 s->slice_context_count = nb_slices;
989 }
990
991 return 0;
992 fail:
993 ff_mpv_common_end(s);
994 return err;
995 }
996
997 /* init common structure for both encoder and decoder */
998 void ff_mpv_common_end(MpegEncContext *s)
999 {
1000 int i;
1001
1002 if (s->slice_context_count > 1) {
1003 for (i = 0; i < s->slice_context_count; i++) {
1004 free_duplicate_context(s->thread_context[i]);
1005 }
1006 for (i = 1; i < s->slice_context_count; i++) {
1007 av_freep(&s->thread_context[i]);
1008 }
1009 s->slice_context_count = 1;
1010 } else free_duplicate_context(s);
1011
1012 av_freep(&s->parse_context.buffer);
1013 s->parse_context.buffer_size = 0;
1014
1015 av_freep(&s->bitstream_buffer);
1016 s->allocated_bitstream_buffer_size = 0;
1017
1018 if (s->picture) {
1019 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1020 ff_free_picture_tables(&s->picture[i]);
1021 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1022 av_frame_free(&s->picture[i].f);
1023 }
1024 }
1025 av_freep(&s->picture);
1026 ff_free_picture_tables(&s->last_picture);
1027 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1028 av_frame_free(&s->last_picture.f);
1029 ff_free_picture_tables(&s->current_picture);
1030 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1031 av_frame_free(&s->current_picture.f);
1032 ff_free_picture_tables(&s->next_picture);
1033 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1034 av_frame_free(&s->next_picture.f);
1035 ff_free_picture_tables(&s->new_picture);
1036 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1037 av_frame_free(&s->new_picture.f);
1038
1039 free_context_frame(s);
1040
1041 s->context_initialized = 0;
1042 s->last_picture_ptr =
1043 s->next_picture_ptr =
1044 s->current_picture_ptr = NULL;
1045 s->linesize = s->uvlinesize = 0;
1046 }
1047
1048 /**
1049 * generic function called after decoding
1050 * the header and before a frame is decoded.
1051 */
1052 int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1053 {
1054 int i, ret;
1055 Picture *pic;
1056 s->mb_skipped = 0;
1057
1058 /* mark & release old frames */
1059 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1060 s->last_picture_ptr != s->next_picture_ptr &&
1061 s->last_picture_ptr->f->buf[0]) {
1062 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1063 }
1064
1065 /* release forgotten pictures */
1066 /* if (mpeg124/h263) */
1067 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1068 if (&s->picture[i] != s->last_picture_ptr &&
1069 &s->picture[i] != s->next_picture_ptr &&
1070 s->picture[i].reference && !s->picture[i].needs_realloc) {
1071 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1072 av_log(avctx, AV_LOG_ERROR,
1073 "releasing zombie picture\n");
1074 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1075 }
1076 }
1077
1078 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1079
1080 /* release non reference frames */
1081 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1082 if (!s->picture[i].reference)
1083 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1084 }
1085
1086 if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
1087 // we already have a unused image
1088 // (maybe it was set before reading the header)
1089 pic = s->current_picture_ptr;
1090 } else {
1091 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1092 if (i < 0) {
1093 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1094 return i;
1095 }
1096 pic = &s->picture[i];
1097 }
1098
1099 pic->reference = 0;
1100 if (!s->droppable) {
1101 if (s->pict_type != AV_PICTURE_TYPE_B)
1102 pic->reference = 3;
1103 }
1104
1105 pic->f->coded_picture_number = s->coded_picture_number++;
1106
1107 if (alloc_picture(s, pic, 0) < 0)
1108 return -1;
1109
1110 s->current_picture_ptr = pic;
1111 // FIXME use only the vars from current_pic
1112 s->current_picture_ptr->f->top_field_first = s->top_field_first;
1113 if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1114 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1115 if (s->picture_structure != PICT_FRAME)
1116 s->current_picture_ptr->f->top_field_first =
1117 (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1118 }
1119 s->current_picture_ptr->f->interlaced_frame = !s->progressive_frame &&
1120 !s->progressive_sequence;
1121 s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
1122
1123 s->current_picture_ptr->f->pict_type = s->pict_type;
1124 // if (s->avctx->flags && AV_CODEC_FLAG_QSCALE)
1125 // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1126 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1127
1128 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1129 s->current_picture_ptr)) < 0)
1130 return ret;
1131
1132 if (s->pict_type != AV_PICTURE_TYPE_B) {
1133 s->last_picture_ptr = s->next_picture_ptr;
1134 if (!s->droppable)
1135 s->next_picture_ptr = s->current_picture_ptr;
1136 }
1137 ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1138 s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1139 s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1140 s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1141 s->current_picture_ptr ? s->current_picture_ptr->f->data[0] : NULL,
1142 s->pict_type, s->droppable);
1143
1144 if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
1145 (s->pict_type != AV_PICTURE_TYPE_I ||
1146 s->picture_structure != PICT_FRAME)) {
1147 int h_chroma_shift, v_chroma_shift;
1148 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1149 &h_chroma_shift, &v_chroma_shift);
1150 if (s->pict_type != AV_PICTURE_TYPE_I)
1151 av_log(avctx, AV_LOG_ERROR,
1152 "warning: first frame is no keyframe\n");
1153 else if (s->picture_structure != PICT_FRAME)
1154 av_log(avctx, AV_LOG_INFO,
1155 "allocate dummy last picture for field based first keyframe\n");
1156
1157 /* Allocate a dummy frame */
1158 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1159 if (i < 0) {
1160 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1161 return i;
1162 }
1163 s->last_picture_ptr = &s->picture[i];
1164
1165 s->last_picture_ptr->reference = 3;
1166 s->last_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
1167
1168 if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1169 s->last_picture_ptr = NULL;
1170 return -1;
1171 }
1172
1173 memset(s->last_picture_ptr->f->data[0], 0,
1174 avctx->height * s->last_picture_ptr->f->linesize[0]);
1175 memset(s->last_picture_ptr->f->data[1], 0x80,
1176 (avctx->height >> v_chroma_shift) *
1177 s->last_picture_ptr->f->linesize[1]);
1178 memset(s->last_picture_ptr->f->data[2], 0x80,
1179 (avctx->height >> v_chroma_shift) *
1180 s->last_picture_ptr->f->linesize[2]);
1181
1182 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1183 ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1184 }
1185 if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
1186 s->pict_type == AV_PICTURE_TYPE_B) {
1187 /* Allocate a dummy frame */
1188 i = ff_find_unused_picture(s->avctx, s->picture, 0);
1189 if (i < 0) {
1190 av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1191 return i;
1192 }
1193 s->next_picture_ptr = &s->picture[i];
1194
1195 s->next_picture_ptr->reference = 3;
1196 s->next_picture_ptr->f->pict_type = AV_PICTURE_TYPE_I;
1197
1198 if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1199 s->next_picture_ptr = NULL;
1200 return -1;
1201 }
1202 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1203 ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1204 }
1205
1206 if (s->last_picture_ptr) {
1207 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1208 if (s->last_picture_ptr->f->buf[0] &&
1209 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1210 s->last_picture_ptr)) < 0)
1211 return ret;
1212 }
1213 if (s->next_picture_ptr) {
1214 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1215 if (s->next_picture_ptr->f->buf[0] &&
1216 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1217 s->next_picture_ptr)) < 0)
1218 return ret;
1219 }
1220
1221 if (s->pict_type != AV_PICTURE_TYPE_I &&
1222 !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
1223 av_log(s, AV_LOG_ERROR,
1224 "Non-reference picture received and no reference available\n");
1225 return AVERROR_INVALIDDATA;
1226 }
1227
1228 if (s->picture_structure!= PICT_FRAME) {
1229 int i;
1230 for (i = 0; i < 4; i++) {
1231 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1232 s->current_picture.f->data[i] +=
1233 s->current_picture.f->linesize[i];
1234 }
1235 s->current_picture.f->linesize[i] *= 2;
1236 s->last_picture.f->linesize[i] *= 2;
1237 s->next_picture.f->linesize[i] *= 2;
1238 }
1239 }
1240
1241 /* set dequantizer, we can't do it during init as
1242 * it might change for mpeg4 and we can't do it in the header
1243 * decode as init is not called for mpeg4 there yet */
1244 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1245 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1246 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1247 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1248 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1249 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1250 } else {
1251 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1252 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1253 }
1254
1255 #if FF_API_XVMC
1256 FF_DISABLE_DEPRECATION_WARNINGS
1257 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1258 return ff_xvmc_field_start(s, avctx);
1259 FF_ENABLE_DEPRECATION_WARNINGS
1260 #endif /* FF_API_XVMC */
1261
1262 return 0;
1263 }
1264
1265 /* called after a frame has been decoded. */
1266 void ff_mpv_frame_end(MpegEncContext *s)
1267 {
1268 #if FF_API_XVMC
1269 FF_DISABLE_DEPRECATION_WARNINGS
1270 /* redraw edges for the frame if decoding didn't complete */
1271 // just to make sure that all data is rendered.
1272 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1273 ff_xvmc_field_end(s);
1274 } else
1275 FF_ENABLE_DEPRECATION_WARNINGS
1276 #endif /* FF_API_XVMC */
1277
1278 emms_c();
1279
1280 if (s->current_picture.reference)
1281 ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1282 }
1283
1284 /**
1285 * Print debugging info for the given picture.
1286 */
1287 void ff_print_debug_info(MpegEncContext *s, Picture *p)
1288 {
1289 AVFrame *pict;
1290 if (s->avctx->hwaccel || !p || !p->mb_type)
1291 return;
1292 pict = p->f;
1293
1294 if (s->avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
1295 int x,y;
1296
1297 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1298 switch (pict->pict_type) {
1299 case AV_PICTURE_TYPE_I:
1300 av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1301 break;
1302 case AV_PICTURE_TYPE_P:
1303 av_log(s->avctx,AV_LOG_DEBUG,"P\n");
1304 break;
1305 case AV_PICTURE_TYPE_B:
1306 av_log(s->avctx,AV_LOG_DEBUG,"B\n");
1307 break;
1308 case AV_PICTURE_TYPE_S:
1309 av_log(s->avctx,AV_LOG_DEBUG,"S\n");
1310 break;
1311 case AV_PICTURE_TYPE_SI:
1312 av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
1313 break;
1314 case AV_PICTURE_TYPE_SP:
1315 av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
1316 break;
1317 }
1318 for (y = 0; y < s->mb_height; y++) {
1319 for (x = 0; x < s->mb_width; x++) {
1320 if (s->avctx->debug & FF_DEBUG_SKIP) {
1321 int count = s->mbskip_table[x + y * s->mb_stride];
1322 if (count > 9)
1323 count = 9;
1324 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
1325 }
1326 if (s->avctx->debug & FF_DEBUG_QP) {
1327 av_log(s->avctx, AV_LOG_DEBUG, "%2d",
1328 p->qscale_table[x + y * s->mb_stride]);
1329 }
1330 if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
1331 int mb_type = p->mb_type[x + y * s->mb_stride];
1332 // Type & MV direction
1333 if (IS_PCM(mb_type))
1334 av_log(s->avctx, AV_LOG_DEBUG, "P");
1335 else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
1336 av_log(s->avctx, AV_LOG_DEBUG, "A");
1337 else if (IS_INTRA4x4(mb_type))
1338 av_log(s->avctx, AV_LOG_DEBUG, "i");
1339 else if (IS_INTRA16x16(mb_type))
1340 av_log(s->avctx, AV_LOG_DEBUG, "I");
1341 else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
1342 av_log(s->avctx, AV_LOG_DEBUG, "d");
1343 else if (IS_DIRECT(mb_type))
1344 av_log(s->avctx, AV_LOG_DEBUG, "D");
1345 else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
1346 av_log(s->avctx, AV_LOG_DEBUG, "g");
1347 else if (IS_GMC(mb_type))
1348 av_log(s->avctx, AV_LOG_DEBUG, "G");
1349 else if (IS_SKIP(mb_type))
1350 av_log(s->avctx, AV_LOG_DEBUG, "S");
1351 else if (!USES_LIST(mb_type, 1))
1352 av_log(s->avctx, AV_LOG_DEBUG, ">");
1353 else if (!USES_LIST(mb_type, 0))
1354 av_log(s->avctx, AV_LOG_DEBUG, "<");
1355 else {
1356 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
1357 av_log(s->avctx, AV_LOG_DEBUG, "X");
1358 }
1359
1360 // segmentation
1361 if (IS_8X8(mb_type))
1362 av_log(s->avctx, AV_LOG_DEBUG, "+");
1363 else if (IS_16X8(mb_type))
1364 av_log(s->avctx, AV_LOG_DEBUG, "-");
1365 else if (IS_8X16(mb_type))
1366 av_log(s->avctx, AV_LOG_DEBUG, "|");
1367 else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
1368 av_log(s->avctx, AV_LOG_DEBUG, " ");
1369 else
1370 av_log(s->avctx, AV_LOG_DEBUG, "?");
1371
1372
1373 if (IS_INTERLACED(mb_type))
1374 av_log(s->avctx, AV_LOG_DEBUG, "=");
1375 else
1376 av_log(s->avctx, AV_LOG_DEBUG, " ");
1377 }
1378 }
1379 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1380 }
1381 }
1382 }
1383
1384 /**
1385 * find the lowest MB row referenced in the MVs
1386 */
1387 static int lowest_referenced_row(MpegEncContext *s, int dir)
1388 {
1389 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1390 int my, off, i, mvs;
1391
1392 if (s->picture_structure != PICT_FRAME || s->mcsel)
1393 goto unhandled;
1394
1395 switch (s->mv_type) {
1396 case MV_TYPE_16X16:
1397 mvs = 1;
1398 break;
1399 case MV_TYPE_16X8:
1400 mvs = 2;
1401 break;
1402 case MV_TYPE_8X8:
1403 mvs = 4;
1404 break;
1405 default:
1406 goto unhandled;
1407 }
1408
1409 for (i = 0; i < mvs; i++) {
1410 my = s->mv[dir][i][1]<<qpel_shift;
1411 my_max = FFMAX(my_max, my);
1412 my_min = FFMIN(my_min, my);
1413 }
1414
1415 off = (FFMAX(-my_min, my_max) + 63) >> 6;
1416
1417 return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
1418 unhandled:
1419 return s->mb_height-1;
1420 }
1421
1422 /* put block[] to dest[] */
1423 static inline void put_dct(MpegEncContext *s,
1424 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1425 {
1426 s->dct_unquantize_intra(s, block, i, qscale);
1427 s->idsp.idct_put(dest, line_size, block);
1428 }
1429
1430 /* add block[] to dest[] */
1431 static inline void add_dct(MpegEncContext *s,
1432 int16_t *block, int i, uint8_t *dest, int line_size)
1433 {
1434 if (s->block_last_index[i] >= 0) {
1435 s->idsp.idct_add(dest, line_size, block);
1436 }
1437 }
1438
1439 static inline void add_dequant_dct(MpegEncContext *s,
1440 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1441 {
1442 if (s->block_last_index[i] >= 0) {
1443 s->dct_unquantize_inter(s, block, i, qscale);
1444
1445 s->idsp.idct_add(dest, line_size, block);
1446 }
1447 }
1448
1449 /**
1450 * Clean dc, ac, coded_block for the current non-intra MB.
1451 */
1452 void ff_clean_intra_table_entries(MpegEncContext *s)
1453 {
1454 int wrap = s->b8_stride;
1455 int xy = s->block_index[0];
1456
1457 s->dc_val[0][xy ] =
1458 s->dc_val[0][xy + 1 ] =
1459 s->dc_val[0][xy + wrap] =
1460 s->dc_val[0][xy + 1 + wrap] = 1024;
1461 /* ac pred */
1462 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1463 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1464 if (s->msmpeg4_version>=3) {
1465 s->coded_block[xy ] =
1466 s->coded_block[xy + 1 ] =
1467 s->coded_block[xy + wrap] =
1468 s->coded_block[xy + 1 + wrap] = 0;
1469 }
1470 /* chroma */
1471 wrap = s->mb_stride;
1472 xy = s->mb_x + s->mb_y * wrap;
1473 s->dc_val[1][xy] =
1474 s->dc_val[2][xy] = 1024;
1475 /* ac pred */
1476 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1477 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1478
1479 s->mbintra_table[xy]= 0;
1480 }
1481
1482 /* generic function called after a macroblock has been parsed by the
1483 decoder or after it has been encoded by the encoder.
1484
1485 Important variables used:
1486 s->mb_intra : true if intra macroblock
1487 s->mv_dir : motion vector direction
1488 s->mv_type : motion vector type
1489 s->mv : motion vector
1490 s->interlaced_dct : true if interlaced dct used (mpeg2)
1491 */
1492 static av_always_inline
1493 void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64],
1494 int is_mpeg12)
1495 {
1496 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
1497
1498 #if FF_API_XVMC
1499 FF_DISABLE_DEPRECATION_WARNINGS
1500 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
1501 ff_xvmc_decode_mb(s);//xvmc uses pblocks
1502 return;
1503 }
1504 FF_ENABLE_DEPRECATION_WARNINGS
1505 #endif /* FF_API_XVMC */
1506
1507 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
1508 /* print DCT coefficients */
1509 int i,j;
1510 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1511 for(i=0; i<6; i++){
1512 for(j=0; j<64; j++){
1513 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1514 block[i][s->idsp.idct_permutation[j]]);
1515 }
1516 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1517 }
1518 }
1519
1520 s->current_picture.qscale_table[mb_xy] = s->qscale;
1521
1522 /* update DC predictors for P macroblocks */
1523 if (!s->mb_intra) {
1524 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
1525 if(s->mbintra_table[mb_xy])
1526 ff_clean_intra_table_entries(s);
1527 } else {
1528 s->last_dc[0] =
1529 s->last_dc[1] =
1530 s->last_dc[2] = 128 << s->intra_dc_precision;
1531 }
1532 }
1533 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
1534 s->mbintra_table[mb_xy]=1;
1535
1536 if ((s->avctx->flags & AV_CODEC_FLAG_PSNR) ||
1537 !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
1538 s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
1539 uint8_t *dest_y, *dest_cb, *dest_cr;
1540 int dct_linesize, dct_offset;
1541 op_pixels_func (*op_pix)[4];
1542 qpel_mc_func (*op_qpix)[16];
1543 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1544 const int uvlinesize = s->current_picture.f->linesize[1];
1545 const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
1546 const int block_size = 8;
1547
1548 /* avoid copy if macroblock skipped in last frame too */
1549 /* skip only during decoding as we might trash the buffers during encoding a bit */
1550 if(!s->encoding){
1551 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1552
1553 if (s->mb_skipped) {
1554 s->mb_skipped= 0;
1555 assert(s->pict_type!=AV_PICTURE_TYPE_I);
1556 *mbskip_ptr = 1;
1557 } else if(!s->current_picture.reference) {
1558 *mbskip_ptr = 1;
1559 } else{
1560 *mbskip_ptr = 0; /* not skipped */
1561 }
1562 }
1563
1564 dct_linesize = linesize << s->interlaced_dct;
1565 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
1566
1567 if(readable){
1568 dest_y= s->dest[0];
1569 dest_cb= s->dest[1];
1570 dest_cr= s->dest[2];
1571 }else{
1572 dest_y = s->sc.b_scratchpad;
1573 dest_cb= s->sc.b_scratchpad+16*linesize;
1574 dest_cr= s->sc.b_scratchpad+32*linesize;
1575 }
1576
1577 if (!s->mb_intra) {
1578 /* motion handling */
1579 /* decoding or more than one mb_type (MC was already done otherwise) */
1580 if(!s->encoding){
1581
1582 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1583 if (s->mv_dir & MV_DIR_FORWARD) {
1584 ff_thread_await_progress(&s->last_picture_ptr->tf,
1585 lowest_referenced_row(s, 0),
1586 0);
1587 }
1588 if (s->mv_dir & MV_DIR_BACKWARD) {
1589 ff_thread_await_progress(&s->next_picture_ptr->tf,
1590 lowest_referenced_row(s, 1),
1591 0);
1592 }
1593 }
1594
1595 op_qpix= s->me.qpel_put;
1596 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
1597 op_pix = s->hdsp.put_pixels_tab;
1598 }else{
1599 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1600 }
1601 if (s->mv_dir & MV_DIR_FORWARD) {
1602 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
1603 op_pix = s->hdsp.avg_pixels_tab;
1604 op_qpix= s->me.qpel_avg;
1605 }
1606 if (s->mv_dir & MV_DIR_BACKWARD) {
1607 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
1608 }
1609 }
1610
1611 /* skip dequant / idct if we are really late ;) */
1612 if(s->avctx->skip_idct){
1613 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
1614 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
1615 || s->avctx->skip_idct >= AVDISCARD_ALL)
1616 goto skip_idct;
1617 }
1618
1619 /* add dct residue */
1620 if(s->encoding || !( s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
1621 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
1622 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1623 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1624 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1625 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1626
1627 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1628 if (s->chroma_y_shift){
1629 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1630 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1631 }else{
1632 dct_linesize >>= 1;
1633 dct_offset >>=1;
1634 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1635 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1636 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1637 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1638 }
1639 }
1640 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
1641 add_dct(s, block[0], 0, dest_y , dct_linesize);
1642 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
1643 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
1644 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
1645
1646 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1647 if(s->chroma_y_shift){//Chroma420
1648 add_dct(s, block[4], 4, dest_cb, uvlinesize);
1649 add_dct(s, block[5], 5, dest_cr, uvlinesize);
1650 }else{
1651 //chroma422
1652 dct_linesize = uvlinesize << s->interlaced_dct;
1653 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
1654
1655 add_dct(s, block[4], 4, dest_cb, dct_linesize);
1656 add_dct(s, block[5], 5, dest_cr, dct_linesize);
1657 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
1658 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
1659 if(!s->chroma_x_shift){//Chroma444
1660 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
1661 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
1662 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
1663 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
1664 }
1665 }
1666 }//fi gray
1667 }
1668 else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
1669 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
1670 }
1671 } else {
1672 /* dct only in intra block */
1673 if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
1674 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1675 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1676 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1677 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1678
1679 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1680 if(s->chroma_y_shift){
1681 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1682 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1683 }else{
1684 dct_offset >>=1;
1685 dct_linesize >>=1;
1686 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1687 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1688 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1689 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1690 }
1691 }
1692 }else{
1693 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1694 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1695 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1696 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
1697
1698 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1699 if(s->chroma_y_shift){
1700 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1701 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
1702 }else{
1703
1704 dct_linesize = uvlinesize << s->interlaced_dct;
1705 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
1706
1707 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1708 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1709 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1710 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
1711 if(!s->chroma_x_shift){//Chroma444
1712 s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
1713 s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
1714 s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
1715 s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
1716 }
1717 }
1718 }//gray
1719 }
1720 }
1721 skip_idct:
1722 if(!readable){
1723 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
1724 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
1725 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
1726 }
1727 }
1728 }
1729
1730 void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
1731 {
1732 #if !CONFIG_SMALL
1733 if(s->out_format == FMT_MPEG1) {
1734 mpv_decode_mb_internal(s, block, 1);
1735 } else
1736 #endif
1737 mpv_decode_mb_internal(s, block, 0);
1738 }
1739
1740 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
1741 {
1742 ff_draw_horiz_band(s->avctx, s->current_picture.f,
1743 s->last_picture.f, y, h, s->picture_structure,
1744 s->first_field, s->low_delay);
1745 }
1746
1747 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
1748 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1749 const int uvlinesize = s->current_picture.f->linesize[1];
1750 const int mb_size= 4;
1751
1752 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
1753 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
1754 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
1755 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1756 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1757 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;
1758 //block_index is not used by mpeg2, so it is not affected by chroma_format
1759
1760 s->dest[0] = s->current_picture.f->data[0] + ((s->mb_x - 1) << mb_size);
1761 s->dest[1] = s->current_picture.f->data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
1762 s->dest[2] = s->current_picture.f->data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
1763
1764 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
1765 {
1766 if(s->picture_structure==PICT_FRAME){
1767 s->dest[0] += s->mb_y * linesize << mb_size;
1768 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1769 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
1770 }else{
1771 s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
1772 s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1773 s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
1774 assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
1775 }
1776 }
1777 }
1778
1779 void ff_mpeg_flush(AVCodecContext *avctx){
1780 int i;
1781 MpegEncContext *s = avctx->priv_data;
1782
1783 if (!s || !s->picture)
1784 return;
1785
1786 for (i = 0; i < MAX_PICTURE_COUNT; i++)
1787 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1788 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
1789
1790 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1791 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1792 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1793
1794 s->mb_x= s->mb_y= 0;
1795
1796 s->parse_context.state= -1;
1797 s->parse_context.frame_start_found= 0;
1798 s->parse_context.overread= 0;
1799 s->parse_context.overread_index= 0;
1800 s->parse_context.index= 0;
1801 s->parse_context.last_index= 0;
1802 s->bitstream_buffer_size=0;
1803 s->pp_time=0;
1804 }
1805
1806 /**
1807 * set qscale and update qscale dependent variables.
1808 */
1809 void ff_set_qscale(MpegEncContext * s, int qscale)
1810 {
1811 if (qscale < 1)
1812 qscale = 1;
1813 else if (qscale > 31)
1814 qscale = 31;
1815
1816 s->qscale = qscale;
1817 s->chroma_qscale= s->chroma_qscale_table[qscale];
1818
1819 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
1820 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
1821 }
1822
1823 void ff_mpv_report_decode_progress(MpegEncContext *s)
1824 {
1825 if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
1826 ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
1827 }