25 #include "config_components.h"
72 if (!
s->context_initialized) {
75 memcpy(
s,
s1,
sizeof(*
s));
79 s->bitstream_buffer =
NULL;
80 s->bitstream_buffer_size =
s->allocated_bitstream_buffer_size = 0;
82 if (
s1->context_initialized) {
85 memset(
s, 0,
sizeof(*
s));
88 memcpy(&
s->h264chroma, &
s1->h264chroma,
sizeof(
s->h264chroma));
94 if (
s->height !=
s1->height ||
s->width !=
s1->width ||
s->context_reinit) {
95 s->height =
s1->height;
101 s->quarter_sample =
s1->quarter_sample;
103 s->coded_picture_number =
s1->coded_picture_number;
104 s->picture_number =
s1->picture_number;
110 if (
s1->picture &&
s1->picture[
i].f->buf[0] &&
115 #define UPDATE_PICTURE(pic)\
117 ff_mpeg_unref_picture(s->avctx, &s->pic);\
118 if (s1->pic.f && s1->pic.f->buf[0])\
119 ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
121 ret = ff_update_picture_tables(&s->pic, &s1->pic);\
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)
140 s->workaround_bugs =
s1->workaround_bugs;
141 s->padding_bug_score =
s1->padding_bug_score;
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);
149 s->max_b_frames =
s1->max_b_frames;
150 s->low_delay =
s1->low_delay;
151 s->droppable =
s1->droppable;
154 s->divx_packed =
s1->divx_packed;
156 if (
s1->bitstream_buffer) {
157 if (
s1->bitstream_buffer_size +
160 &
s->allocated_bitstream_buffer_size,
161 s1->allocated_bitstream_buffer_size);
162 if (!
s->bitstream_buffer) {
163 s->bitstream_buffer_size = 0;
167 s->bitstream_buffer_size =
s1->bitstream_buffer_size;
168 memcpy(
s->bitstream_buffer,
s1->bitstream_buffer,
169 s1->bitstream_buffer_size);
170 memset(
s->bitstream_buffer +
s->bitstream_buffer_size, 0,
175 if (!
s->sc.edge_emu_buffer)
178 &
s->sc,
s1->linesize) < 0) {
180 "scratch buffers.\n");
185 "be allocated due to unknown size.\n");
189 memcpy(&
s->progressive_sequence, &
s1->progressive_sequence,
190 (
char *) &
s1->rtp_mode - (
char *) &
s1->progressive_sequence);
199 if (!
s->context_initialized)
206 s->picture[
i].needs_realloc = 1;
208 s->last_picture_ptr =
209 s->next_picture_ptr =
210 s->current_picture_ptr =
NULL;
212 if ((
s->width ||
s->height) &&
226 memset(
s->thread_context, 0,
sizeof(
s->thread_context));
227 s->thread_context[0] =
s;
229 if (
s->width &&
s->height) {
234 s->context_reinit = 0;
239 s->context_reinit = 1;
246 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
247 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
248 &
s->linesize, &
s->uvlinesize);
253 int h_chroma_shift, v_chroma_shift;
257 for (
int i = 0;
i <
frame->height;
i++)
285 s->last_picture_ptr !=
s->next_picture_ptr &&
286 s->last_picture_ptr->f->buf[0]) {
292 if (!
s->picture[
i].reference ||
293 (&
s->picture[
i] !=
s->last_picture_ptr &&
294 &
s->picture[
i] !=
s->next_picture_ptr &&
295 !
s->picture[
i].needs_realloc)) {
304 if (
s->current_picture_ptr && !
s->current_picture_ptr->f->buf[0]) {
307 pic =
s->current_picture_ptr;
314 pic = &
s->picture[idx];
323 #if FF_API_FRAME_PICTURE_NUMBER
332 s->current_picture_ptr = pic;
334 s->current_picture_ptr->f->top_field_first =
s->top_field_first;
338 s->current_picture_ptr->f->top_field_first =
341 s->current_picture_ptr->f->interlaced_frame = !
s->progressive_frame &&
342 !
s->progressive_sequence;
343 s->current_picture_ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
345 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;
385 s->last_picture_ptr->f->key_frame = 0;
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;
427 s->next_picture_ptr->f->key_frame = 0;
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 linesize =
s->current_picture.f->linesize[0] << field_based;
641 uvlinesize =
s->current_picture.f->linesize[1] << field_based;
644 if (
s->quarter_sample) {
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);
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;
667 uvsx = (2 * mx) & s_mask;
668 uvsy = (2 * my) & s_mask;
669 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres);
672 if (
s->chroma_y_shift) {
677 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres + 1);
678 uvsrc_y = (
mb_y * block_s >> field_based) + (my >>
lowres + 1);
680 if (
s->chroma_x_shift) {
684 uvsy = motion_y & s_mask;
686 uvsrc_x =
s->mb_x*block_s + (mx >> (
lowres+1));
689 uvsx = motion_x & s_mask;
690 uvsy = motion_y & s_mask;
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;
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,
717 uvsrc_x, uvsrc_y * (1 << field_based),
719 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
722 uvsrc_x, uvsrc_y * (1 << field_based),
731 dest_y +=
s->linesize;
732 dest_cb +=
s->uvlinesize;
733 dest_cr +=
s->uvlinesize;
737 ptr_y +=
s->linesize;
738 ptr_cb +=
s->uvlinesize;
739 ptr_cr +=
s->uvlinesize;
747 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
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 &&
887 ref_picture =
s->current_picture_ptr->f->data;
891 0, 0,
s->field_select[dir][0],
894 s->mv[dir][0][1], 2 * block_s,
mb_y >> 1);
898 for (
int i = 0;
i < 2;
i++) {
899 uint8_t *
const *ref2picture;
901 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 for (
int i = 0;
i < 2;
i++) {
935 0, 0,
s->picture_structure !=
i + 1,
937 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
938 2 * block_s,
mb_y >> 1);
941 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
945 if (!
s->first_field) {
946 ref_picture =
s->current_picture_ptr->f->data;
961 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
967 switch (
s->mv_type) {
981 for (
int i = 0;
i < mvs;
i++) {
982 int my =
s->mv[dir][
i][1];
983 my_max =
FFMAX(my_max, my);
984 my_min =
FFMIN(my_min, my);
987 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
989 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
991 return s->mb_height - 1;
996 int16_t *
block,
int i, uint8_t *
dest,
int line_size)
998 if (
s->block_last_index[
i] >= 0) {
1003 #define IS_ENCODER 0
1011 for (
int i = 0;
i < 6;
i++) {
1012 for (
int j = 0; j < 64; j++) {
1014 block[
i][
s->idsp.idct_permutation[j]]);
1020 if (!
s->avctx->lowres) {