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];
328 s->current_picture_ptr = pic;
330 s->current_picture_ptr->f->top_field_first =
s->top_field_first;
334 s->current_picture_ptr->f->top_field_first =
337 s->current_picture_ptr->f->interlaced_frame = !
s->progressive_frame &&
338 !
s->progressive_sequence;
339 s->current_picture_ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
341 s->current_picture_ptr->f->pict_type =
s->pict_type;
345 s->current_picture_ptr)) < 0)
349 s->last_picture_ptr =
s->next_picture_ptr;
351 s->next_picture_ptr =
s->current_picture_ptr;
353 ff_dlog(
s->avctx,
"L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
354 s->last_picture_ptr,
s->next_picture_ptr,
s->current_picture_ptr,
355 s->last_picture_ptr ?
s->last_picture_ptr->f->data[0] :
NULL,
356 s->next_picture_ptr ?
s->next_picture_ptr->f->data[0] :
NULL,
357 s->current_picture_ptr ?
s->current_picture_ptr->f->data[0] :
NULL,
358 s->pict_type,
s->droppable);
360 if ((!
s->last_picture_ptr || !
s->last_picture_ptr->f->buf[0]) &&
362 int h_chroma_shift, v_chroma_shift;
364 &h_chroma_shift, &v_chroma_shift);
367 "allocating dummy last picture for B frame\n");
370 "warning: first frame is no keyframe\n");
378 s->last_picture_ptr = &
s->picture[idx];
380 s->last_picture_ptr->reference = 3;
381 s->last_picture_ptr->f->key_frame = 0;
385 s->last_picture_ptr =
NULL;
391 memset(
s->last_picture_ptr->f->data[0] +
s->last_picture_ptr->f->linesize[0]*
i,
393 if (
s->last_picture_ptr->f->data[2]) {
395 memset(
s->last_picture_ptr->f->data[1] +
s->last_picture_ptr->f->linesize[1]*
i,
397 memset(
s->last_picture_ptr->f->data[2] +
s->last_picture_ptr->f->linesize[2]*
i,
404 memset(
s->last_picture_ptr->f->data[0] +
s->last_picture_ptr->f->linesize[0] *
i,
412 if ((!
s->next_picture_ptr || !
s->next_picture_ptr->f->buf[0]) &&
420 s->next_picture_ptr = &
s->picture[idx];
422 s->next_picture_ptr->reference = 3;
423 s->next_picture_ptr->f->key_frame = 0;
427 s->next_picture_ptr =
NULL;
434 #if 0 // BUFREF-FIXME
435 memset(
s->last_picture.f->data, 0,
sizeof(
s->last_picture.f->data));
436 memset(
s->next_picture.f->data, 0,
sizeof(
s->next_picture.f->data));
438 if (
s->last_picture_ptr) {
439 if (
s->last_picture_ptr->f->buf[0] &&
441 s->last_picture_ptr)) < 0)
444 if (
s->next_picture_ptr) {
445 if (
s->next_picture_ptr->f->buf[0] &&
447 s->next_picture_ptr)) < 0)
452 s->last_picture_ptr->f->buf[0]));
455 for (
int i = 0;
i < 4;
i++) {
457 s->current_picture.f->data[
i] =
FF_PTR_ADD(
s->current_picture.f->data[
i],
458 s->current_picture.f->linesize[
i]);
460 s->current_picture.f->linesize[
i] *= 2;
461 s->last_picture.f->linesize[
i] *= 2;
462 s->next_picture.f->linesize[
i] *= 2;
470 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
471 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
473 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
474 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
476 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
477 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
491 if (
s->current_picture.reference)
499 s->mb_width,
s->mb_height,
s->mb_stride,
s->quarter_sample);
535 s->last_picture_ptr ?
s->last_picture_ptr->f :
NULL,
536 y,
h,
s->picture_structure,
537 s->first_field,
s->low_delay);
549 s->current_picture_ptr =
s->last_picture_ptr =
s->next_picture_ptr =
NULL;
555 s->mb_x =
s->mb_y = 0;
557 #if FF_API_FLAG_TRUNCATED
558 s->parse_context.state = -1;
559 s->parse_context.frame_start_found = 0;
560 s->parse_context.overread = 0;
561 s->parse_context.overread_index = 0;
562 s->parse_context.index = 0;
563 s->parse_context.last_index = 0;
565 s->bitstream_buffer_size = 0;
577 uint8_t *
dest,
const uint8_t *
src,
579 int src_x,
int src_y,
583 int motion_x,
int motion_y)
585 const int lowres =
s->avctx->lowres;
587 const int s_mask = (2 <<
lowres) - 1;
591 if (
s->quarter_sample) {
596 sx = motion_x & s_mask;
597 sy = motion_y & s_mask;
598 src_x += motion_x >>
lowres + 1;
599 src_y += motion_y >>
lowres + 1;
605 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer,
src,
606 s->linesize,
s->linesize,
607 w + 1, (
h + 1) << field_based,
608 src_x, src_y * (1 << field_based),
610 src =
s->sc.edge_emu_buffer;
630 uint8_t *
const *ref_picture,
632 int motion_x,
int motion_y,
635 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
636 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
638 const int lowres =
s->avctx->lowres;
639 const int op_index =
FFMIN(
lowres - 1 +
s->chroma_x_shift, 3);
640 const int block_s = 8 >>
lowres;
641 const int s_mask = (2 <<
lowres) - 1;
644 linesize =
s->current_picture.f->linesize[0] << field_based;
645 uvlinesize =
s->current_picture.f->linesize[1] << field_based;
648 if (
s->quarter_sample) {
657 sx = motion_x & s_mask;
658 sy = motion_y & s_mask;
659 src_x =
s->mb_x * 2 * block_s + (motion_x >>
lowres + 1);
660 src_y = (
mb_y * 2 * block_s >> field_based) + (motion_y >>
lowres + 1);
663 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
664 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
665 uvsrc_x = src_x >> 1;
666 uvsrc_y = src_y >> 1;
671 uvsx = (2 * mx) & s_mask;
672 uvsy = (2 * my) & s_mask;
673 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres);
676 if (
s->chroma_y_shift) {
681 uvsrc_x =
s->mb_x * block_s + (mx >>
lowres + 1);
682 uvsrc_y = (
mb_y * block_s >> field_based) + (my >>
lowres + 1);
684 if (
s->chroma_x_shift) {
688 uvsy = motion_y & s_mask;
690 uvsrc_x =
s->mb_x*block_s + (mx >> (
lowres+1));
693 uvsx = motion_x & s_mask;
694 uvsy = motion_y & s_mask;
701 ptr_y = ref_picture[0] + src_y *
linesize + src_x;
702 ptr_cb = ref_picture[1] + uvsrc_y *
uvlinesize + uvsrc_x;
703 ptr_cr = ref_picture[2] + uvsrc_y *
uvlinesize + uvsrc_x;
705 if ((
unsigned) src_x >
FFMAX(
h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
707 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr_y,
709 17, 17 + field_based,
710 src_x, src_y * (1 << field_based),
h_edge_pos,
712 ptr_y =
s->sc.edge_emu_buffer;
714 uint8_t *ubuf =
s->sc.edge_emu_buffer + 18 *
s->linesize;
715 uint8_t *vbuf =ubuf + 10 *
s->uvlinesize;
717 vbuf -=
s->uvlinesize;
718 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
721 uvsrc_x, uvsrc_y * (1 << field_based),
723 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
726 uvsrc_x, uvsrc_y * (1 << field_based),
735 dest_y +=
s->linesize;
736 dest_cb +=
s->uvlinesize;
737 dest_cr +=
s->uvlinesize;
741 ptr_y +=
s->linesize;
742 ptr_cb +=
s->uvlinesize;
743 ptr_cr +=
s->uvlinesize;
751 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
752 uvsx = (uvsx << 2) >>
lowres;
753 uvsy = (uvsy << 2) >>
lowres;
755 pix_op[op_index](dest_cb, ptr_cb,
uvlinesize, hc, uvsx, uvsy);
756 pix_op[op_index](dest_cr, ptr_cr,
uvlinesize, hc, uvsx, uvsy);
763 uint8_t *dest_cb, uint8_t *dest_cr,
764 uint8_t *
const *ref_picture,
768 const int lowres =
s->avctx->lowres;
770 const int block_s = 8 >>
lowres;
771 const int s_mask = (2 <<
lowres) - 1;
774 int emu = 0, src_x, src_y, sx, sy;
778 if (
s->quarter_sample) {
790 src_x =
s->mb_x * block_s + (mx >>
lowres + 1);
791 src_y =
s->mb_y * block_s + (my >>
lowres + 1);
793 offset = src_y *
s->uvlinesize + src_x;
794 ptr = ref_picture[1] +
offset;
797 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
798 s->uvlinesize,
s->uvlinesize,
801 ptr =
s->sc.edge_emu_buffer;
806 pix_op[op_index](dest_cb, ptr,
s->uvlinesize, block_s, sx, sy);
808 ptr = ref_picture[2] +
offset;
810 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
811 s->uvlinesize,
s->uvlinesize,
814 ptr =
s->sc.edge_emu_buffer;
816 pix_op[op_index](dest_cr, ptr,
s->uvlinesize, block_s, sx, sy);
831 uint8_t *dest_y, uint8_t *dest_cb,
833 int dir, uint8_t *
const *ref_picture,
838 const int lowres =
s->avctx->lowres;
839 const int block_s = 8 >>
lowres;
844 switch (
s->mv_type) {
849 s->mv[dir][0][0],
s->mv[dir][0][1],
855 for (
int i = 0;
i < 4;
i++) {
857 s->linesize) * block_s,
858 ref_picture[0], 0, 0,
859 (2 *
mb_x + (
i & 1)) * block_s,
860 (2 *
mb_y + (
i >> 1)) * block_s,
861 s->width,
s->height,
s->linesize,
863 block_s, block_s, pix_op,
864 s->mv[dir][
i][0],
s->mv[dir][
i][1]);
866 mx +=
s->mv[dir][
i][0];
867 my +=
s->mv[dir][
i][1];
878 1, 0,
s->field_select[dir][0],
880 s->mv[dir][0][0],
s->mv[dir][0][1],
884 1, 1,
s->field_select[dir][1],
886 s->mv[dir][1][0],
s->mv[dir][1][1],
889 if (
s->picture_structure !=
s->field_select[dir][0] + 1 &&
891 ref_picture =
s->current_picture_ptr->f->data;
895 0, 0,
s->field_select[dir][0],
898 s->mv[dir][0][1], 2 * block_s,
mb_y >> 1);
902 for (
int i = 0;
i < 2;
i++) {
903 uint8_t *
const *ref2picture;
905 if (
s->picture_structure ==
s->field_select[dir][
i] + 1 ||
907 ref2picture = ref_picture;
909 ref2picture =
s->current_picture_ptr->f->data;
913 0, 0,
s->field_select[dir][
i],
915 s->mv[dir][
i][0],
s->mv[dir][
i][1] +
916 2 * block_s *
i, block_s,
mb_y >> 1);
918 dest_y += 2 * block_s *
s->linesize;
919 dest_cb += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
920 dest_cr += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
925 for (
int i = 0;
i < 2;
i++) {
926 for (
int j = 0; j < 2; j++) {
930 s->mv[dir][2 *
i + j][0],
931 s->mv[dir][2 *
i + j][1],
934 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
937 for (
int i = 0;
i < 2;
i++) {
939 0, 0,
s->picture_structure !=
i + 1,
941 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
942 2 * block_s,
mb_y >> 1);
945 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
949 if (!
s->first_field) {
950 ref_picture =
s->current_picture_ptr->f->data;
965 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
971 switch (
s->mv_type) {
985 for (
int i = 0;
i < mvs;
i++) {
986 int my =
s->mv[dir][
i][1];
987 my_max =
FFMAX(my_max, my);
988 my_min =
FFMIN(my_min, my);
991 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
993 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
995 return s->mb_height - 1;
1000 int16_t *
block,
int i, uint8_t *
dest,
int line_size)
1002 if (
s->block_last_index[
i] >= 0) {
1007 #define IS_ENCODER 0
1015 for (
int i = 0;
i < 6;
i++) {
1016 for (
int j = 0; j < 64; j++) {
1018 block[
i][
s->idsp.idct_permutation[j]]);
1024 if (!
s->avctx->lowres) {