1 /*
2 * Common mpeg video decoding code
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
24
25 #include "config_components.h"
26
32
45
47 {
49
51
57
58 /* convert fourcc to upper case */
60
62
64
65 if (
s->picture_pool)
// VC-1 can call this multiple times
66 return 0;
67
73 }
74 return 0;
75 }
76
79 {
83
85 return 0;
86
88
89 // FIXME can parameters change on I-frames?
90 // in that case dst may need a reinit
91 if (!
s->context_initialized) {
93 int err;
94 memcpy(
s, s1,
sizeof(*
s));
95
96 s->context_initialized = 0;
97 s->context_reinit = 0;
100 s->bitstream_buffer =
NULL;
101 s->bitstream_buffer_size =
s->allocated_bitstream_buffer_size = 0;
102
105 return err;
106 }
107 }
108
109 if (
s->height != s1->
height ||
s->width != s1->
width ||
s->context_reinit) {
114 }
115
117
119
123
126
127 // Error/bug resilience
130
131 // MPEG-4 timing info
135
136 // B-frame info
139
140 // DivX handling (doesn't work)
142
145 &
s->allocated_bitstream_buffer_size,
147 if (!
s->bitstream_buffer) {
148 s->bitstream_buffer_size = 0;
150 }
154 }
155
156 // MPEG-2/interlacing info
159
160 return 0;
161 }
162
164 {
166
169 return 0;
170 }
171
173 {
174 int err = 0;
175
176 if (!
s->context_initialized)
178
180
184
185 if ((
s->width ||
s->height) &&
188
189 /* set chroma shifts */
193 if (err < 0)
195
198
199 memset(
s->thread_context, 0,
sizeof(
s->thread_context));
200 s->thread_context[0] =
s;
201
202 if (
s->width &&
s->height) {
204 if (err < 0)
206 }
207 s->context_reinit = 0;
208
209 return 0;
212 s->context_reinit = 1;
213 return err;
214 }
215
217 {
221
222 if (!pic)
224
226
228
229 /* WM Image / Screen codecs allocate internal buffers with different
230 * dimensions / colorspaces; ignore user-defined callbacks for these. */
236 } else {
241 }
244
248
252
253 av_assert1(
s->mb_width ==
s->buffer_pools.alloc_mb_width);
254 av_assert1(
s->mb_height ==
s->buffer_pools.alloc_mb_height ||
255 FFALIGN(
s->mb_height, 2) ==
s->buffer_pools.alloc_mb_height);
256 av_assert1(
s->mb_stride ==
s->buffer_pools.alloc_mb_stride);
258 &
s->buffer_pools,
s->mb_height);
261
262 return 0;
266 }
267
269 {
274
277
279
280 return 0;
281 }
282
284 {
285 int h_chroma_shift, v_chroma_shift;
286
287 for (
int i = 0;
i <
frame->height;
i++)
289
291 return;
298 }
299 }
300
302 {
305
306 av_assert1(!
s->last_pic.ptr ||
s->last_pic.ptr->f->buf[0]);
307 av_assert1(!
s->next_pic.ptr ||
s->next_pic.ptr->f->buf[0]);
311 "allocating dummy last picture for B frame\n");
315 "warning: first frame is no keyframe\n");
316
317 /* Allocate a dummy frame */
321
325 }
326 }
328 /* Allocate a dummy frame */
332 }
333
335 s->last_pic.ptr->f->buf[0]));
336
337 return 0;
338 }
339
340 /**
341 * generic function called after decoding
342 * the header and before a frame is decoded.
343 */
345 {
347
349
353 }
354
360
363 (!
s->progressive_frame && !
s->progressive_sequence);
364 s->cur_pic.ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
365
366 s->cur_pic.ptr->f->pict_type =
s->pict_type;
369 else
371
376 }
377 ff_dlog(
s->avctx,
"L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
378 (
void*)
s->last_pic.ptr, (
void*)
s->next_pic.ptr, (
void*)
s->cur_pic.ptr,
379 s->last_pic.ptr ?
s->last_pic.ptr->f->data[0] :
NULL,
380 s->next_pic.ptr ?
s->next_pic.ptr->f->data[0] :
NULL,
381 s->cur_pic.ptr ?
s->cur_pic.ptr->f->data[0] :
NULL,
382 s->pict_type,
s->droppable);
383
387
390
391 return 0;
392 }
393
394 /* called after a frame has been decoded. */
396 {
398
399 if (
s->cur_pic.reference)
401 }
402
404 {
407 s->mb_width,
s->mb_height,
s->mb_stride,
s->quarter_sample);
408 }
409
412 {
416
418 return 0;
419
421 if (!par)
423
424 for (
unsigned y = 0; y < p->
mb_height; y++)
425 for (
unsigned x = 0; x < p->
mb_width; x++) {
426 const unsigned int block_idx = y * p->
mb_width + x;
427 const unsigned int mb_xy = y * p->
mb_stride + x;
429
434
436 }
437
438 return 0;
439 }
440
442 {
444 s->last_pic.ptr ?
s->last_pic.ptr->f :
NULL,
445 y,
h,
s->picture_structure,
446 s->first_field,
s->low_delay);
447 }
448
450 {
452
456
457 s->mb_x =
s->mb_y = 0;
458
459 s->bitstream_buffer_size = 0;
461 }
462
464 {
467 }
468
469
471 uint8_t *
dest,
const uint8_t *
src,
473 int src_x, int src_y,
477 int motion_x, int motion_y)
478 {
479 const int lowres =
s->avctx->lowres;
480 const int op_index =
lowres;
481 const int s_mask = (2 <<
lowres) - 1;
482 int emu = 0;
483 int sx, sy;
484
486
487 if (
s->quarter_sample) {
488 motion_x /= 2;
489 motion_y /= 2;
490 }
491
492 sx = motion_x & s_mask;
493 sy = motion_y & s_mask;
494 src_x += motion_x >>
lowres + 1;
495 src_y += motion_y >>
lowres + 1;
496
498
501 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer,
src,
502 s->linesize,
s->linesize,
503 w + 1, (
h + 1) << field_based,
504 src_x, src_y * (1 << field_based),
506 src =
s->sc.edge_emu_buffer;
507 emu = 1;
508 }
509
515 return emu;
516 }
517
518 /* apply one mpeg motion vector to the three components */
520 uint8_t *dest_y,
521 uint8_t *dest_cb,
522 uint8_t *dest_cr,
523 int field_based,
524 int bottom_field,
526 uint8_t *const *ref_picture,
528 int motion_x, int motion_y,
530 {
531 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
532 int mx,
my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
534 const int lowres =
s->avctx->lowres;
535 const int op_index =
lowres - 1 +
s->chroma_x_shift;
536 const int block_s = 8 >>
lowres;
537 const int s_mask = (2 <<
lowres) - 1;
540 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
541
543
544 linesize =
s->cur_pic.linesize[0] << field_based;
545 uvlinesize =
s->cur_pic.linesize[1] << field_based;
546
547 // FIXME obviously not perfect but qpel will not work in lowres anyway
548 if (
s->quarter_sample) {
549 motion_x /= 2;
550 motion_y /= 2;
551 }
552
553 if (field_based) {
555 }
556
557 sx = motion_x & s_mask;
558 sy = motion_y & s_mask;
559 src_x =
s->mb_x * 2 * block_s + (motion_x >>
lowres + 1);
560 src_y = (
mb_y * 2 * block_s >> field_based) + (motion_y >>
lowres + 1);
561
563 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
564 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
565 uvsrc_x = src_x >> 1;
566 uvsrc_y = src_y >> 1;
568 // even chroma mv's are full pel in H261
571 uvsx = (2 *
mx) & s_mask;
572 uvsy = (2 *
my) & s_mask;
573 uvsrc_x =
s->mb_x * block_s + (
mx >>
lowres);
575 } else {
576 if (
s->chroma_y_shift) {
581 uvsrc_x =
s->mb_x * block_s + (
mx >>
lowres + 1);
582 uvsrc_y = (
mb_y * block_s >> field_based) + (
my >>
lowres + 1);
583 } else {
584 if (
s->chroma_x_shift) {
585 //Chroma422
588 uvsy = motion_y & s_mask;
589 uvsrc_y = src_y;
590 uvsrc_x =
s->mb_x*block_s + (
mx >> (
lowres+1));
591 } else {
592 //Chroma444
593 uvsx = motion_x & s_mask;
594 uvsy = motion_y & s_mask;
595 uvsrc_x = src_x;
596 uvsrc_y = src_y;
597 }
598 }
599 }
600
601 ptr_y = ref_picture[0] + src_y *
linesize + src_x;
602 ptr_cb = ref_picture[1] + uvsrc_y *
uvlinesize + uvsrc_x;
603 ptr_cr = ref_picture[2] + uvsrc_y *
uvlinesize + uvsrc_x;
604
605 if ((
unsigned) src_x >
FFMAX(
h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
607 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr_y,
609 17, 17 + field_based,
610 src_x, src_y * (1 << field_based),
h_edge_pos,
612 ptr_y =
s->sc.edge_emu_buffer;
614 uint8_t *ubuf =
s->sc.edge_emu_buffer + 18 *
s->linesize;
615 uint8_t *vbuf =ubuf + 10 *
s->uvlinesize;
617 vbuf -=
s->uvlinesize;
618 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
620 9, 9 + field_based,
621 uvsrc_x, uvsrc_y * (1 << field_based),
623 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
625 9, 9 + field_based,
626 uvsrc_x, uvsrc_y * (1 << field_based),
628 ptr_cb = ubuf;
629 ptr_cr = vbuf;
630 }
631 }
632
633 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
634 if (bottom_field) {
635 dest_y +=
s->linesize;
636 dest_cb +=
s->uvlinesize;
637 dest_cr +=
s->uvlinesize;
638 }
639
641 ptr_y +=
s->linesize;
642 ptr_cb +=
s->uvlinesize;
643 ptr_cr +=
s->uvlinesize;
644 }
645
649
651 uvsx = (uvsx << 2) >>
lowres;
652 uvsy = (uvsy << 2) >>
lowres;
653 if (hc) {
654 pix_op[op_index](dest_cb, ptr_cb,
uvlinesize, hc, uvsx, uvsy);
655 pix_op[op_index](dest_cr, ptr_cr,
uvlinesize, hc, uvsx, uvsy);
656 }
657 }
658 // FIXME h261 lowres loop filter
659 }
660
662 uint8_t *dest_cb, uint8_t *dest_cr,
663 uint8_t *const *ref_picture,
666 {
667 const int lowres =
s->avctx->lowres;
668 const int op_index =
lowres;
669 const int block_s = 8 >>
lowres;
670 const int s_mask = (2 <<
lowres) - 1;
673 int emu = 0, src_x, src_y, sx, sy;
675 const uint8_t *ptr;
676
678
679 if (
s->quarter_sample) {
682 }
683
684 /* In case of 8X8, we construct a single chroma motion vector
685 with a special rounding */
688
691 src_x =
s->mb_x * block_s + (
mx >>
lowres + 1);
692 src_y =
s->mb_y * block_s + (
my >>
lowres + 1);
693
694 offset = src_y *
s->uvlinesize + src_x;
695 ptr = ref_picture[1] +
offset;
698 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
699 s->uvlinesize,
s->uvlinesize,
700 9, 9,
702 ptr =
s->sc.edge_emu_buffer;
703 emu = 1;
704 }
707 pix_op[op_index](dest_cb, ptr,
s->uvlinesize, block_s, sx, sy);
708
709 ptr = ref_picture[2] +
offset;
710 if (emu) {
711 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
712 s->uvlinesize,
s->uvlinesize,
713 9, 9,
715 ptr =
s->sc.edge_emu_buffer;
716 }
717 pix_op[op_index](dest_cr, ptr,
s->uvlinesize, block_s, sx, sy);
718 }
719
720 /**
721 * motion compensation of a single macroblock
722 * @param s context
723 * @param dest_y luma destination pointer
724 * @param dest_cb chroma cb/u destination pointer
725 * @param dest_cr chroma cr/v destination pointer
726 * @param dir direction (0->forward, 1->backward)
727 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
728 * @param pix_op halfpel motion compensation function (average or put normally)
729 * the motion vectors are taken from s->mv and the MV type from s->mv_type
730 */
732 uint8_t *dest_y, uint8_t *dest_cb,
733 uint8_t *dest_cr,
734 int dir, uint8_t *const *ref_picture,
736 {
739 const int lowres =
s->avctx->lowres;
740 const int block_s = 8 >>
lowres;
741
744
745 switch (
s->mv_type) {
748 0, 0, 0,
749 ref_picture, pix_op,
750 s->mv[dir][0][0],
s->mv[dir][0][1],
752 break;
756 for (
int i = 0;
i < 4;
i++) {
758 s->linesize) * block_s,
759 ref_picture[0], 0, 0,
760 (2 *
mb_x + (
i & 1)) * block_s,
761 (2 *
mb_y + (
i >> 1)) * block_s,
762 s->width,
s->height,
s->linesize,
764 block_s, block_s, pix_op,
765 s->mv[dir][
i][0],
s->mv[dir][
i][1]);
766
767 mx +=
s->mv[dir][
i][0];
768 my +=
s->mv[dir][
i][1];
769 }
770
774 break;
777 /* top field */
779 1, 0,
s->field_select[dir][0],
780 ref_picture, pix_op,
781 s->mv[dir][0][0],
s->mv[dir][0][1],
783 /* bottom field */
785 1, 1,
s->field_select[dir][1],
786 ref_picture, pix_op,
787 s->mv[dir][1][0],
s->mv[dir][1][1],
789 } else {
790 if (
s->picture_structure !=
s->field_select[dir][0] + 1 &&
792 ref_picture =
s->cur_pic.ptr->f->data;
793 }
795 0, 0,
s->field_select[dir][0],
796 ref_picture, pix_op,
798 s->mv[dir][0][1], 2 * block_s,
mb_y >> 1);
799 }
800 break;
802 for (
int i = 0;
i < 2;
i++) {
803 uint8_t *const *ref2picture;
804
805 if (
s->picture_structure ==
s->field_select[dir][
i] + 1 ||
807 ref2picture = ref_picture;
808 } else {
809 ref2picture =
s->cur_pic.ptr->f->data;
810 }
811
813 0, 0,
s->field_select[dir][
i],
814 ref2picture, pix_op,
815 s->mv[dir][
i][0],
s->mv[dir][
i][1] +
816 2 * block_s *
i, block_s,
mb_y >> 1);
817
818 dest_y += 2 * block_s *
s->linesize;
819 dest_cb += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
820 dest_cr += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
821 }
822 break;
825 for (
int i = 0;
i < 2;
i++) {
826 for (int j = 0; j < 2; j++) {
829 ref_picture, pix_op,
830 s->mv[dir][2 *
i + j][0],
831 s->mv[dir][2 *
i + j][1],
833 }
834 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
835 }
836 } else {
837 for (
int i = 0;
i < 2;
i++) {
839 0, 0,
s->picture_structure !=
i + 1,
840 ref_picture, pix_op,
841 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
842 2 * block_s,
mb_y >> 1);
843
844 // after put we make avg of the same block
845 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
846
847 // opposite parity is always in the same
848 // frame if this is second field
849 if (!
s->first_field) {
850 ref_picture =
s->cur_pic.ptr->f->data;
851 }
852 }
853 }
854 break;
855 default:
857 }
858 }
859
860 /**
861 * find the lowest MB row referenced in the MVs
862 */
864 {
865 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
866 int off, mvs;
867
869 goto unhandled;
870
871 switch (
s->mv_type) {
873 mvs = 1;
874 break;
876 mvs = 2;
877 break;
879 mvs = 4;
880 break;
881 default:
882 goto unhandled;
883 }
884
885 for (
int i = 0;
i < mvs;
i++) {
886 int my =
s->mv[dir][
i][1];
889 }
890
891 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
892
893 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
894 unhandled:
895 return s->mb_height - 1;
896 }
897
898 /* add block[] to dest[] */
900 int16_t *
block,
int i, uint8_t *
dest,
int line_size)
901 {
902 if (
s->block_last_index[
i] >= 0) {
904 }
905 }
906
909
911 {
913 /* print DCT coefficients */
915 for (
int i = 0;
i < 6;
i++) {
916 for (int j = 0; j < 64; j++) {
918 block[
i][
s->idsp.idct_permutation[j]]);
919 }
921 }
922 }
923
925 if (!
s->avctx->lowres) {
926 #if !CONFIG_SMALL
929 else
931 #else
933 #endif
934 } else
936 }