25 #include "config_components.h"
74 if (!
s->context_initialized) {
77 memcpy(
s,
s1,
sizeof(*
s));
81 s->bitstream_buffer =
NULL;
82 s->bitstream_buffer_size =
s->allocated_bitstream_buffer_size = 0;
84 if (
s1->context_initialized) {
87 memset(
s, 0,
sizeof(*
s));
90 memcpy(&
s->h264chroma, &
s1->h264chroma,
sizeof(
s->h264chroma));
96 if (
s->height !=
s1->height ||
s->width !=
s1->width ||
s->context_reinit) {
97 s->height =
s1->height;
103 s->quarter_sample =
s1->quarter_sample;
105 s->coded_picture_number =
s1->coded_picture_number;
106 s->picture_number =
s1->picture_number;
112 if (
s1->picture &&
s1->picture[
i].f->buf[0] &&
117 #define UPDATE_PICTURE(pic)\
119 ff_mpeg_unref_picture(s->avctx, &s->pic);\
120 if (s1->pic.f && s1->pic.f->buf[0])\
121 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
123 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
132 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
133 ((pic && pic >= old_ctx->picture && \
134 pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
135 &new_ctx->picture[pic - old_ctx->picture] : NULL)
142 s->workaround_bugs =
s1->workaround_bugs;
143 s->padding_bug_score =
s1->padding_bug_score;
146 memcpy(&
s->last_time_base, &
s1->last_time_base,
147 (
char *) &
s1->pb_field_time +
sizeof(
s1->pb_field_time) -
148 (
char *) &
s1->last_time_base);
151 s->max_b_frames =
s1->max_b_frames;
152 s->low_delay =
s1->low_delay;
153 s->droppable =
s1->droppable;
156 s->divx_packed =
s1->divx_packed;
158 if (
s1->bitstream_buffer) {
160 &
s->allocated_bitstream_buffer_size,
161 s1->bitstream_buffer_size);
162 if (!
s->bitstream_buffer) {
163 s->bitstream_buffer_size = 0;
166 s->bitstream_buffer_size =
s1->bitstream_buffer_size;
167 memcpy(
s->bitstream_buffer,
s1->bitstream_buffer,
168 s1->bitstream_buffer_size);
172 if (!
s->sc.edge_emu_buffer)
175 &
s->sc,
s1->linesize) < 0) {
177 "scratch buffers.\n");
182 "be allocated due to unknown size.\n");
186 memcpy(&
s->progressive_sequence, &
s1->progressive_sequence,
187 (
char *) &
s1->rtp_mode - (
char *) &
s1->progressive_sequence);
196 if (!
s->context_initialized)
203 s->picture[
i].needs_realloc = 1;
205 s->last_picture_ptr =
206 s->next_picture_ptr =
207 s->current_picture_ptr =
NULL;
209 if ((
s->width ||
s->height) &&
223 memset(
s->thread_context, 0,
sizeof(
s->thread_context));
224 s->thread_context[0] =
s;
226 if (
s->width &&
s->height) {
231 s->context_reinit = 0;
236 s->context_reinit = 1;
243 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
244 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
245 &
s->linesize, &
s->uvlinesize);
250 int h_chroma_shift, v_chroma_shift;
254 for (
int i = 0;
i <
frame->height;
i++)
282 s->last_picture_ptr !=
s->next_picture_ptr &&
283 s->last_picture_ptr->f->buf[0]) {
289 if (!
s->picture[
i].reference ||
290 (&
s->picture[
i] !=
s->last_picture_ptr &&
291 &
s->picture[
i] !=
s->next_picture_ptr &&
292 !
s->picture[
i].needs_realloc)) {
301 if (
s->current_picture_ptr && !
s->current_picture_ptr->f->buf[0]) {
304 pic =
s->current_picture_ptr;
311 pic = &
s->picture[idx];
320 #if FF_API_FRAME_PICTURE_NUMBER
329 s->current_picture_ptr = pic;
339 !
s->progressive_sequence);
340 s->current_picture_ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
342 s->current_picture_ptr->f->pict_type =
s->pict_type;
349 s->current_picture_ptr)) < 0)
353 s->last_picture_ptr =
s->next_picture_ptr;
355 s->next_picture_ptr =
s->current_picture_ptr;
357 ff_dlog(
s->avctx,
"L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
358 s->last_picture_ptr,
s->next_picture_ptr,
s->current_picture_ptr,
359 s->last_picture_ptr ?
s->last_picture_ptr->f->data[0] :
NULL,
360 s->next_picture_ptr ?
s->next_picture_ptr->f->data[0] :
NULL,
361 s->current_picture_ptr ?
s->current_picture_ptr->f->data[0] :
NULL,
362 s->pict_type,
s->droppable);
364 if ((!
s->last_picture_ptr || !
s->last_picture_ptr->f->buf[0]) &&
366 int h_chroma_shift, v_chroma_shift;
368 &h_chroma_shift, &v_chroma_shift);
371 "allocating dummy last picture for B frame\n");
374 "warning: first frame is no keyframe\n");
382 s->last_picture_ptr = &
s->picture[idx];
384 s->last_picture_ptr->reference = 3;
389 s->last_picture_ptr =
NULL;
395 memset(
s->last_picture_ptr->f->data[0] +
s->last_picture_ptr->f->linesize[0]*
i,
397 if (
s->last_picture_ptr->f->data[2]) {
399 memset(
s->last_picture_ptr->f->data[1] +
s->last_picture_ptr->f->linesize[1]*
i,
401 memset(
s->last_picture_ptr->f->data[2] +
s->last_picture_ptr->f->linesize[2]*
i,
408 memset(
s->last_picture_ptr->f->data[0] +
s->last_picture_ptr->f->linesize[0] *
i,
416 if ((!
s->next_picture_ptr || !
s->next_picture_ptr->f->buf[0]) &&
424 s->next_picture_ptr = &
s->picture[idx];
426 s->next_picture_ptr->reference = 3;
431 s->next_picture_ptr =
NULL;
438 #if 0 // BUFREF-FIXME
439 memset(
s->last_picture.f->data, 0,
sizeof(
s->last_picture.f->data));
440 memset(
s->next_picture.f->data, 0,
sizeof(
s->next_picture.f->data));
442 if (
s->last_picture_ptr) {
443 if (
s->last_picture_ptr->f->buf[0] &&
445 s->last_picture_ptr)) < 0)
448 if (
s->next_picture_ptr) {
449 if (
s->next_picture_ptr->f->buf[0] &&
451 s->next_picture_ptr)) < 0)
456 s->last_picture_ptr->f->buf[0]));
459 for (
int i = 0;
i < 4;
i++) {
461 s->current_picture.f->data[
i] =
FF_PTR_ADD(
s->current_picture.f->data[
i],
462 s->current_picture.f->linesize[
i]);
464 s->current_picture.f->linesize[
i] *= 2;
465 s->last_picture.f->linesize[
i] *= 2;
466 s->next_picture.f->linesize[
i] *= 2;
474 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
475 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
477 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
478 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
480 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
481 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
495 if (
s->current_picture.reference)
503 s->mb_width,
s->mb_height,
s->mb_stride,
s->quarter_sample);
539 s->last_picture_ptr ?
s->last_picture_ptr->f :
NULL,
540 y,
h,
s->picture_structure,
541 s->first_field,
s->low_delay);
553 s->current_picture_ptr =
s->last_picture_ptr =
s->next_picture_ptr =
NULL;
559 s->mb_x =
s->mb_y = 0;
561 s->bitstream_buffer_size = 0;
573 uint8_t *
dest,
const uint8_t *
src,
575 int src_x,
int src_y,
579 int motion_x,
int motion_y)
581 const int lowres =
s->avctx->lowres;
583 const int s_mask = (2 <<
lowres) - 1;
587 if (
s->quarter_sample) {
592 sx = motion_x & s_mask;
593 sy = motion_y & s_mask;
594 src_x += motion_x >>
lowres + 1;
595 src_y += motion_y >>
lowres + 1;
601 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer,
src,
602 s->linesize,
s->linesize,
603 w + 1, (
h + 1) << field_based,
604 src_x, src_y * (1 << field_based),
606 src =
s->sc.edge_emu_buffer;
626 uint8_t *
const *ref_picture,
628 int motion_x,
int motion_y,
631 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
632 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
634 const int lowres =
s->avctx->lowres;
635 const int op_index =
FFMIN(
lowres - 1 +
s->chroma_x_shift, 3);
636 const int block_s = 8 >>
lowres;
637 const int s_mask = (2 <<
lowres) - 1;
640 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
641 linesize =
s->current_picture.f->linesize[0] << field_based;
642 uvlinesize =
s->current_picture.f->linesize[1] << field_based;
645 if (
s->quarter_sample) {
654 sx = motion_x & s_mask;
655 sy = motion_y & s_mask;
656 src_x =
s->mb_x * 2 * block_s + (motion_x >>
lowres + 1);
657 src_y = (
mb_y * 2 * block_s >> field_based) + (motion_y >>
lowres + 1);
660 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
661 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
662 uvsrc_x = src_x >> 1;
663 uvsrc_y = src_y >> 1;
668 uvsx = (2 * mx) & s_mask;
669 uvsy = (2 * my) & s_mask;
670 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres);
673 if (
s->chroma_y_shift) {
678 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres + 1);
679 uvsrc_y = (
mb_y * block_s >> field_based) + (my >>
lowres + 1);
681 if (
s->chroma_x_shift) {
685 uvsy = motion_y & s_mask;
687 uvsrc_x =
s->mb_x*block_s + (mx >> (
lowres+1));
690 uvsx = motion_x & s_mask;
691 uvsy = motion_y & s_mask;
698 ptr_y = ref_picture[0] + src_y *
linesize + src_x;
699 ptr_cb = ref_picture[1] + uvsrc_y *
uvlinesize + uvsrc_x;
700 ptr_cr = ref_picture[2] + uvsrc_y *
uvlinesize + uvsrc_x;
702 if ((
unsigned) src_x >
FFMAX(
h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
704 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr_y,
706 17, 17 + field_based,
707 src_x, src_y * (1 << field_based),
h_edge_pos,
709 ptr_y =
s->sc.edge_emu_buffer;
711 uint8_t *ubuf =
s->sc.edge_emu_buffer + 18 *
s->linesize;
712 uint8_t *vbuf =ubuf + 10 *
s->uvlinesize;
714 vbuf -=
s->uvlinesize;
715 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
718 uvsrc_x, uvsrc_y * (1 << field_based),
720 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
723 uvsrc_x, uvsrc_y * (1 << field_based),
732 dest_y +=
s->linesize;
733 dest_cb +=
s->uvlinesize;
734 dest_cr +=
s->uvlinesize;
738 ptr_y +=
s->linesize;
739 ptr_cb +=
s->uvlinesize;
740 ptr_cr +=
s->uvlinesize;
748 uvsx = (uvsx << 2) >>
lowres;
749 uvsy = (uvsy << 2) >>
lowres;
751 pix_op[op_index](dest_cb, ptr_cb,
uvlinesize, hc, uvsx, uvsy);
752 pix_op[op_index](dest_cr, ptr_cr,
uvlinesize, hc, uvsx, uvsy);
759 uint8_t *dest_cb, uint8_t *dest_cr,
760 uint8_t *
const *ref_picture,
764 const int lowres =
s->avctx->lowres;
766 const int block_s = 8 >>
lowres;
767 const int s_mask = (2 <<
lowres) - 1;
770 int emu = 0, src_x, src_y, sx, sy;
774 if (
s->quarter_sample) {
786 src_x =
s->mb_x * block_s + (mx >>
lowres + 1);
787 src_y =
s->mb_y * block_s + (my >>
lowres + 1);
789 offset = src_y *
s->uvlinesize + src_x;
790 ptr = ref_picture[1] +
offset;
793 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
794 s->uvlinesize,
s->uvlinesize,
797 ptr =
s->sc.edge_emu_buffer;
802 pix_op[op_index](dest_cb, ptr,
s->uvlinesize, block_s, sx, sy);
804 ptr = ref_picture[2] +
offset;
806 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
807 s->uvlinesize,
s->uvlinesize,
810 ptr =
s->sc.edge_emu_buffer;
812 pix_op[op_index](dest_cr, ptr,
s->uvlinesize, block_s, sx, sy);
827 uint8_t *dest_y, uint8_t *dest_cb,
829 int dir, uint8_t *
const *ref_picture,
834 const int lowres =
s->avctx->lowres;
835 const int block_s = 8 >>
lowres;
840 switch (
s->mv_type) {
845 s->mv[dir][0][0],
s->mv[dir][0][1],
851 for (
int i = 0;
i < 4;
i++) {
853 s->linesize) * block_s,
854 ref_picture[0], 0, 0,
855 (2 *
mb_x + (
i & 1)) * block_s,
856 (2 *
mb_y + (
i >> 1)) * block_s,
857 s->width,
s->height,
s->linesize,
859 block_s, block_s, pix_op,
860 s->mv[dir][
i][0],
s->mv[dir][
i][1]);
862 mx +=
s->mv[dir][
i][0];
863 my +=
s->mv[dir][
i][1];
874 1, 0,
s->field_select[dir][0],
876 s->mv[dir][0][0],
s->mv[dir][0][1],
880 1, 1,
s->field_select[dir][1],
882 s->mv[dir][1][0],
s->mv[dir][1][1],
885 if (
s->picture_structure !=
s->field_select[dir][0] + 1 &&
s->pict_type !=
AV_PICTURE_TYPE_B && !
s->first_field
886 || !ref_picture[0]) {
887 ref_picture =
s->current_picture_ptr->f->data;
890 0, 0,
s->field_select[dir][0],
893 s->mv[dir][0][1], 2 * block_s,
mb_y >> 1);
897 for (
int i = 0;
i < 2;
i++) {
898 uint8_t *
const *ref2picture;
900 if ((
s->picture_structure ==
s->field_select[dir][
i] + 1 ||
903 ref2picture = ref_picture;
905 ref2picture =
s->current_picture_ptr->f->data;
909 0, 0,
s->field_select[dir][
i],
911 s->mv[dir][
i][0],
s->mv[dir][
i][1] +
912 2 * block_s *
i, block_s,
mb_y >> 1);
914 dest_y += 2 * block_s *
s->linesize;
915 dest_cb += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
916 dest_cr += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
921 for (
int i = 0;
i < 2;
i++) {
922 for (
int j = 0; j < 2; j++) {
926 s->mv[dir][2 *
i + j][0],
927 s->mv[dir][2 *
i + j][1],
930 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
933 if (!ref_picture[0]) {
934 ref_picture =
s->current_picture_ptr->f->data;
936 for (
int i = 0;
i < 2;
i++) {
938 0, 0,
s->picture_structure !=
i + 1,
940 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
941 2 * block_s,
mb_y >> 1);
944 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
948 if (!
s->first_field) {
949 ref_picture =
s->current_picture_ptr->f->data;
964 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
970 switch (
s->mv_type) {
984 for (
int i = 0;
i < mvs;
i++) {
985 int my =
s->mv[dir][
i][1];
986 my_max =
FFMAX(my_max, my);
987 my_min =
FFMIN(my_min, my);
990 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
992 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
994 return s->mb_height - 1;
999 int16_t *
block,
int i, uint8_t *
dest,
int line_size)
1001 if (
s->block_last_index[
i] >= 0) {
1006 #define IS_ENCODER 0
1014 for (
int i = 0;
i < 6;
i++) {
1015 for (
int j = 0; j < 64; j++) {
1017 block[
i][
s->idsp.idct_permutation[j]]);
1023 if (!
s->avctx->lowres) {