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
44
46 {
48
54
55 /* convert fourcc to upper case */
57
60 }
61
64 {
68
70 return 0;
71
73
74 // FIXME can parameters change on I-frames?
75 // in that case dst may need a reinit
76 if (!
s->context_initialized) {
78 int err;
79 memcpy(
s,
s1,
sizeof(*
s));
80
81 s->context_initialized = 0;
82 s->context_reinit = 0;
85 s->bitstream_buffer =
NULL;
86 s->bitstream_buffer_size =
s->allocated_bitstream_buffer_size = 0;
87
88 if (
s1->context_initialized) {
90 return err;
91 }
92 }
93
94 if (
s->height !=
s1->height ||
s->width !=
s1->width ||
s->context_reinit) {
95 s->height =
s1->height;
99 }
100
101 s->quarter_sample =
s1->quarter_sample;
102
103 s->coded_picture_number =
s1->coded_picture_number;
104 s->picture_number =
s1->picture_number;
105
110 if (
s1->picture &&
s1->picture[
i].f->buf[0] &&
113 }
114
115 #define UPDATE_PICTURE(pic)\
116 do {\
117 ff_mpeg_unref_picture(&s->pic);\
118 if (s1->pic.f && s1->pic.f->buf[0])\
119 ret = ff_mpeg_ref_picture(&s->pic, &s1->pic);\
120 else\
121 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
122 if (ret < 0)\
123 return ret;\
124 } while (0)
125
129
130 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
131 ((pic && pic >= old_ctx->picture && \
132 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
133 &new_ctx->picture[pic - old_ctx->picture] : NULL)
134
138
139 // Error/bug resilience
140 s->workaround_bugs =
s1->workaround_bugs;
141 s->padding_bug_score =
s1->padding_bug_score;
142
143 // MPEG-4 timing info
144 memcpy(&
s->last_time_base, &
s1->last_time_base,
145 (
char *) &
s1->pb_field_time +
sizeof(
s1->pb_field_time) -
146 (
char *) &
s1->last_time_base);
147
148 // B-frame info
149 s->max_b_frames =
s1->max_b_frames;
150 s->low_delay =
s1->low_delay;
151 s->droppable =
s1->droppable;
152
153 // DivX handling (doesn't work)
154 s->divx_packed =
s1->divx_packed;
155
156 if (
s1->bitstream_buffer) {
158 &
s->allocated_bitstream_buffer_size,
159 s1->bitstream_buffer_size);
160 if (!
s->bitstream_buffer) {
161 s->bitstream_buffer_size = 0;
163 }
164 s->bitstream_buffer_size =
s1->bitstream_buffer_size;
165 memcpy(
s->bitstream_buffer,
s1->bitstream_buffer,
166 s1->bitstream_buffer_size);
167 }
168
169 // linesize-dependent scratch buffer allocation
170 if (!
s->sc.edge_emu_buffer)
173 &
s->sc,
s1->linesize) < 0) {
175 "scratch buffers.\n");
177 }
178 } else {
180 "be allocated due to unknown size.\n");
181 }
182
183 // MPEG-2/interlacing info
184 memcpy(&
s->progressive_sequence, &
s1->progressive_sequence,
185 (
char *) &
s1->rtp_mode - (
char *) &
s1->progressive_sequence);
186
187 return 0;
188 }
189
191 {
192 int err = 0;
193
194 if (!
s->context_initialized)
196
198
201 s->picture[
i].needs_realloc = 1;
202
203 s->last_picture_ptr =
204 s->next_picture_ptr =
205 s->current_picture_ptr =
NULL;
206
207 if ((
s->width ||
s->height) &&
210
211 /* set chroma shifts */
215 if (err < 0)
217
220
221 memset(
s->thread_context, 0,
sizeof(
s->thread_context));
222 s->thread_context[0] =
s;
223
224 if (
s->width &&
s->height) {
226 if (err < 0)
228 }
229 s->context_reinit = 0;
230
231 return 0;
234 s->context_reinit = 1;
235 return err;
236 }
237
239 {
242
244
245 /* WM Image / Screen codecs allocate internal buffers with different
246 * dimensions / colorspaces; ignore user-defined callbacks for these. */
252 } else {
257 }
260
264
266 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
267 &
s->linesize, &
s->uvlinesize);
271 }
272
274 {
275 int h_chroma_shift, v_chroma_shift;
276
279
281 return;
288 }
289 }
290
291 /**
292 * generic function called after decoding
293 * the header and before a frame is decoded.
294 */
296 {
299
301
304 return -1;
305 }
306
307 /* mark & release old frames */
309 s->last_picture_ptr !=
s->next_picture_ptr &&
310 s->last_picture_ptr->f->buf[0]) {
312 }
313
314 /* release non reference/forgotten frames */
316 if (!
s->picture[
i].reference ||
317 (&
s->picture[
i] !=
s->last_picture_ptr &&
318 &
s->picture[
i] !=
s->next_picture_ptr &&
319 !
s->picture[
i].needs_realloc)) {
321 }
322 }
323
327
328 if (
s->current_picture_ptr && !
s->current_picture_ptr->f->buf[0]) {
329 // we already have an unused image
330 // (maybe it was set before reading the header)
331 pic =
s->current_picture_ptr;
332 } else {
334 if (idx < 0) {
336 return idx;
337 }
338 pic = &
s->picture[idx];
339 }
340
345 }
346
348 return -1;
349
350 s->current_picture_ptr = pic;
351 // FIXME use only the vars from current_pic
358 }
360 !
s->progressive_sequence);
361 s->current_picture_ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
362
363 s->current_picture_ptr->f->pict_type =
s->pict_type;
366 else
368
370 s->current_picture_ptr)) < 0)
372
374 s->last_picture_ptr =
s->next_picture_ptr;
376 s->next_picture_ptr =
s->current_picture_ptr;
377 }
378 ff_dlog(
s->avctx,
"L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
379 s->last_picture_ptr,
s->next_picture_ptr,
s->current_picture_ptr,
380 s->last_picture_ptr ?
s->last_picture_ptr->f->data[0] :
NULL,
381 s->next_picture_ptr ?
s->next_picture_ptr->f->data[0] :
NULL,
382 s->current_picture_ptr ?
s->current_picture_ptr->f->data[0] :
NULL,
383 s->pict_type,
s->droppable);
384
385 if ((!
s->last_picture_ptr || !
s->last_picture_ptr->f->buf[0]) &&
389 "allocating dummy last picture for B frame\n");
392 "warning: first frame is no keyframe\n");
393
394 /* Allocate a dummy frame */
396 if (idx < 0) {
398 return idx;
399 }
400 s->last_picture_ptr = &
s->picture[idx];
401
402 s->last_picture_ptr->reference = 3;
405
407 s->last_picture_ptr =
NULL;
408 return -1;
409 }
410
414 }
415
418 }
419 if ((!
s->next_picture_ptr || !
s->next_picture_ptr->f->buf[0]) &&
421 /* Allocate a dummy frame */
423 if (idx < 0) {
425 return idx;
426 }
427 s->next_picture_ptr = &
s->picture[idx];
428
429 s->next_picture_ptr->reference = 3;
432
434 s->next_picture_ptr =
NULL;
435 return -1;
436 }
439 }
440
441 if (
s->last_picture_ptr) {
442 if (
s->last_picture_ptr->f->buf[0] &&
444 s->last_picture_ptr)) < 0)
446 }
447 if (
s->next_picture_ptr) {
448 if (
s->next_picture_ptr->f->buf[0] &&
450 s->next_picture_ptr)) < 0)
452 }
453
455 s->last_picture_ptr->f->buf[0]));
456
458 for (
int i = 0;
i < 4;
i++) {
460 s->current_picture.f->data[
i] =
FF_PTR_ADD(
s->current_picture.f->data[
i],
461 s->current_picture.f->linesize[
i]);
462 }
463 s->current_picture.f->linesize[
i] *= 2;
464 s->last_picture.f->linesize[
i] *= 2;
465 s->next_picture.f->linesize[
i] *= 2;
466 }
467 }
468
469 /* set dequantizer, we can't do it during init as
470 * it might change for MPEG-4 and we can't do it in the header
471 * decode as init is not called for MPEG-4 there yet */
473 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
474 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
476 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
477 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
478 } else {
479 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
480 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
481 }
482
485
486 return 0;
487 }
488
489 /* called after a frame has been decoded. */
491 {
493
494 if (
s->current_picture.reference)
496 }
497
499 {
502 s->mb_width,
s->mb_height,
s->mb_stride,
s->quarter_sample);
503 }
504
506 {
510
512 return 0;
513
515 if (!par)
517
523
528
530 }
531
532 return 0;
533 }
534
536 {
538 s->last_picture_ptr ?
s->last_picture_ptr->f :
NULL,
539 y,
h,
s->picture_structure,
540 s->first_field,
s->low_delay);
541 }
542
544 {
546
548 return;
549
552 s->current_picture_ptr =
s->last_picture_ptr =
s->next_picture_ptr =
NULL;
553
557
558 s->mb_x =
s->mb_y = 0;
559
560 s->bitstream_buffer_size = 0;
562 }
563
565 {
568 }
569
570
572 uint8_t *
dest,
const uint8_t *
src,
574 int src_x, int src_y,
578 int motion_x, int motion_y)
579 {
580 const int lowres =
s->avctx->lowres;
582 const int s_mask = (2 <<
lowres) - 1;
583 int emu = 0;
584 int sx, sy;
585
586 if (
s->quarter_sample) {
587 motion_x /= 2;
588 motion_y /= 2;
589 }
590
591 sx = motion_x & s_mask;
592 sy = motion_y & s_mask;
593 src_x += motion_x >>
lowres + 1;
594 src_y += motion_y >>
lowres + 1;
595
597
600 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer,
src,
601 s->linesize,
s->linesize,
602 w + 1, (
h + 1) << field_based,
603 src_x, src_y * (1 << field_based),
605 src =
s->sc.edge_emu_buffer;
606 emu = 1;
607 }
608
614 return emu;
615 }
616
617 /* apply one mpeg motion vector to the three components */
619 uint8_t *dest_y,
620 uint8_t *dest_cb,
621 uint8_t *dest_cr,
622 int field_based,
623 int bottom_field,
625 uint8_t *const *ref_picture,
627 int motion_x, int motion_y,
629 {
630 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
631 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
633 const int lowres =
s->avctx->lowres;
634 const int op_index =
FFMIN(
lowres - 1 +
s->chroma_x_shift, 3);
635 const int block_s = 8 >>
lowres;
636 const int s_mask = (2 <<
lowres) - 1;
639 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
640 linesize =
s->current_picture.f->linesize[0] << field_based;
641 uvlinesize =
s->current_picture.f->linesize[1] << field_based;
642
643 // FIXME obviously not perfect but qpel will not work in lowres anyway
644 if (
s->quarter_sample) {
645 motion_x /= 2;
646 motion_y /= 2;
647 }
648
649 if (field_based) {
651 }
652
653 sx = motion_x & s_mask;
654 sy = motion_y & s_mask;
655 src_x =
s->mb_x * 2 * block_s + (motion_x >>
lowres + 1);
656 src_y = (
mb_y * 2 * block_s >> field_based) + (motion_y >>
lowres + 1);
657
659 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
660 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
661 uvsrc_x = src_x >> 1;
662 uvsrc_y = src_y >> 1;
664 // even chroma mv's are full pel in H261
665 mx = motion_x / 4;
666 my = motion_y / 4;
667 uvsx = (2 * mx) & s_mask;
668 uvsy = (2 * my) & s_mask;
669 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres);
671 } else {
672 if (
s->chroma_y_shift) {
673 mx = motion_x / 2;
674 my = motion_y / 2;
675 uvsx = mx & s_mask;
676 uvsy = my & s_mask;
677 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres + 1);
678 uvsrc_y = (
mb_y * block_s >> field_based) + (my >>
lowres + 1);
679 } else {
680 if (
s->chroma_x_shift) {
681 //Chroma422
682 mx = motion_x / 2;
683 uvsx = mx & s_mask;
684 uvsy = motion_y & s_mask;
685 uvsrc_y = src_y;
686 uvsrc_x =
s->mb_x*block_s + (mx >> (
lowres+1));
687 } else {
688 //Chroma444
689 uvsx = motion_x & s_mask;
690 uvsy = motion_y & s_mask;
691 uvsrc_x = src_x;
692 uvsrc_y = src_y;
693 }
694 }
695 }
696
697 ptr_y = ref_picture[0] + src_y *
linesize + src_x;
698 ptr_cb = ref_picture[1] + uvsrc_y *
uvlinesize + uvsrc_x;
699 ptr_cr = ref_picture[2] + uvsrc_y *
uvlinesize + uvsrc_x;
700
701 if ((
unsigned) src_x >
FFMAX(
h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
703 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr_y,
705 17, 17 + field_based,
706 src_x, src_y * (1 << field_based),
h_edge_pos,
708 ptr_y =
s->sc.edge_emu_buffer;
710 uint8_t *ubuf =
s->sc.edge_emu_buffer + 18 *
s->linesize;
711 uint8_t *vbuf =ubuf + 10 *
s->uvlinesize;
713 vbuf -=
s->uvlinesize;
714 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
716 9, 9 + field_based,
717 uvsrc_x, uvsrc_y * (1 << field_based),
719 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
721 9, 9 + field_based,
722 uvsrc_x, uvsrc_y * (1 << field_based),
724 ptr_cb = ubuf;
725 ptr_cr = vbuf;
726 }
727 }
728
729 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
730 if (bottom_field) {
731 dest_y +=
s->linesize;
732 dest_cb +=
s->uvlinesize;
733 dest_cr +=
s->uvlinesize;
734 }
735
737 ptr_y +=
s->linesize;
738 ptr_cb +=
s->uvlinesize;
739 ptr_cr +=
s->uvlinesize;
740 }
741
745
747 uvsx = (uvsx << 2) >>
lowres;
748 uvsy = (uvsy << 2) >>
lowres;
749 if (hc) {
750 pix_op[op_index](dest_cb, ptr_cb,
uvlinesize, hc, uvsx, uvsy);
751 pix_op[op_index](dest_cr, ptr_cr,
uvlinesize, hc, uvsx, uvsy);
752 }
753 }
754 // FIXME h261 lowres loop filter
755 }
756
758 uint8_t *dest_cb, uint8_t *dest_cr,
759 uint8_t *const *ref_picture,
761 int mx, int my)
762 {
763 const int lowres =
s->avctx->lowres;
765 const int block_s = 8 >>
lowres;
766 const int s_mask = (2 <<
lowres) - 1;
769 int emu = 0, src_x, src_y, sx, sy;
771 const uint8_t *ptr;
772
773 if (
s->quarter_sample) {
774 mx /= 2;
775 my /= 2;
776 }
777
778 /* In case of 8X8, we construct a single chroma motion vector
779 with a special rounding */
782
783 sx = mx & s_mask;
784 sy = my & s_mask;
785 src_x =
s->mb_x * block_s + (mx >>
lowres + 1);
786 src_y =
s->mb_y * block_s + (my >>
lowres + 1);
787
788 offset = src_y *
s->uvlinesize + src_x;
789 ptr = ref_picture[1] +
offset;
792 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
793 s->uvlinesize,
s->uvlinesize,
794 9, 9,
796 ptr =
s->sc.edge_emu_buffer;
797 emu = 1;
798 }
801 pix_op[op_index](dest_cb, ptr,
s->uvlinesize, block_s, sx, sy);
802
803 ptr = ref_picture[2] +
offset;
804 if (emu) {
805 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
806 s->uvlinesize,
s->uvlinesize,
807 9, 9,
809 ptr =
s->sc.edge_emu_buffer;
810 }
811 pix_op[op_index](dest_cr, ptr,
s->uvlinesize, block_s, sx, sy);
812 }
813
814 /**
815 * motion compensation of a single macroblock
816 * @param s context
817 * @param dest_y luma destination pointer
818 * @param dest_cb chroma cb/u destination pointer
819 * @param dest_cr chroma cr/v destination pointer
820 * @param dir direction (0->forward, 1->backward)
821 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
822 * @param pix_op halfpel motion compensation function (average or put normally)
823 * the motion vectors are taken from s->mv and the MV type from s->mv_type
824 */
826 uint8_t *dest_y, uint8_t *dest_cb,
827 uint8_t *dest_cr,
828 int dir, uint8_t *const *ref_picture,
830 {
831 int mx, my;
833 const int lowres =
s->avctx->lowres;
834 const int block_s = 8 >>
lowres;
835
838
839 switch (
s->mv_type) {
842 0, 0, 0,
843 ref_picture, pix_op,
844 s->mv[dir][0][0],
s->mv[dir][0][1],
846 break;
848 mx = 0;
849 my = 0;
850 for (
int i = 0;
i < 4;
i++) {
852 s->linesize) * block_s,
853 ref_picture[0], 0, 0,
854 (2 *
mb_x + (
i & 1)) * block_s,
855 (2 *
mb_y + (
i >> 1)) * block_s,
856 s->width,
s->height,
s->linesize,
858 block_s, block_s, pix_op,
859 s->mv[dir][
i][0],
s->mv[dir][
i][1]);
860
861 mx +=
s->mv[dir][
i][0];
862 my +=
s->mv[dir][
i][1];
863 }
864
867 pix_op, mx, my);
868 break;
871 /* top field */
873 1, 0,
s->field_select[dir][0],
874 ref_picture, pix_op,
875 s->mv[dir][0][0],
s->mv[dir][0][1],
877 /* bottom field */
879 1, 1,
s->field_select[dir][1],
880 ref_picture, pix_op,
881 s->mv[dir][1][0],
s->mv[dir][1][1],
883 } else {
884 if (
s->picture_structure !=
s->field_select[dir][0] + 1 &&
s->pict_type !=
AV_PICTURE_TYPE_B && !
s->first_field
885 || !ref_picture[0]) {
886 ref_picture =
s->current_picture_ptr->f->data;
887 }
889 0, 0,
s->field_select[dir][0],
890 ref_picture, pix_op,
892 s->mv[dir][0][1], 2 * block_s,
mb_y >> 1);
893 }
894 break;
896 for (
int i = 0;
i < 2;
i++) {
897 uint8_t *const *ref2picture;
898
899 if ((
s->picture_structure ==
s->field_select[dir][
i] + 1 ||
901 ref_picture[0]) {
902 ref2picture = ref_picture;
903 } else {
904 ref2picture =
s->current_picture_ptr->f->data;
905 }
906
908 0, 0,
s->field_select[dir][
i],
909 ref2picture, pix_op,
910 s->mv[dir][
i][0],
s->mv[dir][
i][1] +
911 2 * block_s *
i, block_s,
mb_y >> 1);
912
913 dest_y += 2 * block_s *
s->linesize;
914 dest_cb += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
915 dest_cr += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
916 }
917 break;
920 for (
int i = 0;
i < 2;
i++) {
921 for (int j = 0; j < 2; j++) {
924 ref_picture, pix_op,
925 s->mv[dir][2 *
i + j][0],
926 s->mv[dir][2 *
i + j][1],
928 }
929 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
930 }
931 } else {
932 if (!ref_picture[0]) {
933 ref_picture =
s->current_picture_ptr->f->data;
934 }
935 for (
int i = 0;
i < 2;
i++) {
937 0, 0,
s->picture_structure !=
i + 1,
938 ref_picture, pix_op,
939 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
940 2 * block_s,
mb_y >> 1);
941
942 // after put we make avg of the same block
943 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
944
945 // opposite parity is always in the same
946 // frame if this is second field
947 if (!
s->first_field) {
948 ref_picture =
s->current_picture_ptr->f->data;
949 }
950 }
951 }
952 break;
953 default:
955 }
956 }
957
958 /**
959 * find the lowest MB row referenced in the MVs
960 */
962 {
963 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
964 int off, mvs;
965
967 goto unhandled;
968
969 switch (
s->mv_type) {
971 mvs = 1;
972 break;
974 mvs = 2;
975 break;
977 mvs = 4;
978 break;
979 default:
980 goto unhandled;
981 }
982
983 for (
int i = 0;
i < mvs;
i++) {
984 int my =
s->mv[dir][
i][1];
985 my_max =
FFMAX(my_max, my);
986 my_min =
FFMIN(my_min, my);
987 }
988
989 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
990
991 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
992 unhandled:
993 return s->mb_height - 1;
994 }
995
996 /* add block[] to dest[] */
998 int16_t *
block,
int i, uint8_t *
dest,
int line_size)
999 {
1000 if (
s->block_last_index[
i] >= 0) {
1002 }
1003 }
1004
1005 #define IS_ENCODER 0
1007
1009 {
1011 /* print DCT coefficients */
1013 for (
int i = 0;
i < 6;
i++) {
1014 for (int j = 0; j < 64; j++) {
1016 block[
i][
s->idsp.idct_permutation[j]]);
1017 }
1019 }
1020 }
1021
1022 if (!
s->avctx->lowres) {
1023 #if !CONFIG_SMALL
1026 else
1028 #else
1030 #endif
1031 } else
1033 }