+ s->picture_number = s->new_picture.f->display_picture_number;
+ } else {
+ ff_mpeg_unref_picture(s->avctx, &s->new_picture);
+ }
+ return 0;
+}
+
+static void frame_end(MpegEncContext *s)
+{
+ int i;
+
+ if (s->unrestricted_mv &&
+ s->current_picture.reference &&
+ !s->intra_only) {
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
+ int hshift = desc->log2_chroma_w;
+ int vshift = desc->log2_chroma_h;
+ s->mpvencdsp.draw_edges(s->current_picture.f->data[0], s->linesize,
+ s->h_edge_pos, s->v_edge_pos,
+ EDGE_WIDTH, EDGE_WIDTH,
+ EDGE_TOP | EDGE_BOTTOM);
+ s->mpvencdsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
+ s->h_edge_pos >> hshift,
+ s->v_edge_pos >> vshift,
+ EDGE_WIDTH >> hshift,
+ EDGE_WIDTH >> vshift,
+ EDGE_TOP | EDGE_BOTTOM);
+ s->mpvencdsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
+ s->h_edge_pos >> hshift,
+ s->v_edge_pos >> vshift,
+ EDGE_WIDTH >> hshift,
+ EDGE_WIDTH >> vshift,
+ EDGE_TOP | EDGE_BOTTOM);
+ }
+
+ emms_c();
+
+ s->last_pict_type = s->pict_type;
+ s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
+ if (s->pict_type!= AV_PICTURE_TYPE_B)
+ s->last_non_b_pict_type = s->pict_type;
+
+ if (s->encoding) {
+ /* release non-reference frames */
+ for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+ if (!s->picture[i].reference)
+ ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
+ }
+ }
+
+#if FF_API_CODED_FRAME
+FF_DISABLE_DEPRECATION_WARNINGS
+ av_frame_copy_props(s->avctx->coded_frame, s->current_picture.f);
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+}
+
+static void update_noise_reduction(MpegEncContext *s)
+{
+ int intra, i;
+
+ for (intra = 0; intra < 2; intra++) {
+ if (s->dct_count[intra] > (1 << 16)) {
+ for (i = 0; i < 64; i++) {
+ s->dct_error_sum[intra][i] >>= 1;
+ }
+ s->dct_count[intra] >>= 1;
+ }
+
+ for (i = 0; i < 64; i++) {
+ s->dct_offset[intra][i] = (s->avctx->noise_reduction *
+ s->dct_count[intra] +
+ s->dct_error_sum[intra][i] / 2) /
+ (s->dct_error_sum[intra][i] + 1);
+ }
+ }
+}
+
+static int frame_start(MpegEncContext *s)
+{
+ int ret;
+
+ /* mark & release old frames */
+ if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
+ s->last_picture_ptr != s->next_picture_ptr &&
+ s->last_picture_ptr->f->buf[0]) {
+ ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
+ }
+
+ s->current_picture_ptr->f->pict_type = s->pict_type;
+ s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
+
+ ff_mpeg_unref_picture(s->avctx, &s->current_picture);
+ if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
+ s->current_picture_ptr)) < 0)
+ return ret;
+
+ if (s->pict_type != AV_PICTURE_TYPE_B) {
+ s->last_picture_ptr = s->next_picture_ptr;
+ if (!s->droppable)
+ s->next_picture_ptr = s->current_picture_ptr;
+ }
+
+ if (s->last_picture_ptr) {
+ ff_mpeg_unref_picture(s->avctx, &s->last_picture);
+ if (s->last_picture_ptr->f->buf[0] &&
+ (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
+ s->last_picture_ptr)) < 0)
+ return ret;
+ }
+ if (s->next_picture_ptr) {
+ ff_mpeg_unref_picture(s->avctx, &s->next_picture);
+ if (s->next_picture_ptr->f->buf[0] &&
+ (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
+ s->next_picture_ptr)) < 0)
+ return ret;
+ }
+
+ if (s->picture_structure!= PICT_FRAME) {
+ int i;
+ for (i = 0; i < 4; i++) {
+ if (s->picture_structure == PICT_BOTTOM_FIELD) {
+ s->current_picture.f->data[i] +=
+ s->current_picture.f->linesize[i];
+ }
+ s->current_picture.f->linesize[i] *= 2;
+ s->last_picture.f->linesize[i] *= 2;
+ s->next_picture.f->linesize[i] *= 2;
+ }
+ }
+
+ if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
+ s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
+ s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
+ } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
+ s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
+ s->dct_unquantize_inter = s->dct_unquantize_h263_inter;