+ if (!s->last_picture->data[0]) {
+ ret = ff_get_buffer(avctx, s->last_picture, 0);
+ if (ret < 0)
+ return ret;
+ }
+ if (!s->scratchbuf) {
+ s->scratchbuf = av_malloc(s->current_picture->linesize[0] * 16 * 2);
+ if (!s->scratchbuf)
+ return AVERROR(ENOMEM);
+ }
+
+ FFSWAP(AVFrame*, s->current_picture, s->last_picture);
+
+ init_put_bits(&s->pb, pkt->data, pkt->size);
+
+ if (avctx->gop_size && (avctx->frame_number % avctx->gop_size))
+ s->pict_type = AV_PICTURE_TYPE_P;
+ else
+ s->pict_type = AV_PICTURE_TYPE_I;
+ s->quality = pict->quality;
+
+#if FF_API_CODED_FRAME
+FF_DISABLE_DEPRECATION_WARNINGS
+ avctx->coded_frame->pict_type = s->pict_type;
+ avctx->coded_frame->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+
+ sd = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR, sizeof(int));
+ if (!sd)
+ return AVERROR(ENOMEM);
+ *(int *)sd = pict->quality;
+
+ svq1_write_header(s, s->pict_type);
+ for (i = 0; i < 3; i++)
+ if (svq1_encode_plane(s, i,
+ pict->data[i],
+ s->last_picture->data[i],
+ s->current_picture->data[i],
+ s->frame_width / (i ? 4 : 1),
+ s->frame_height / (i ? 4 : 1),
+ pict->linesize[i],
+ s->current_picture->linesize[i]) < 0) {
+ int j;
+ for (j = 0; j < i; j++) {
+ av_freep(&s->motion_val8[j]);
+ av_freep(&s->motion_val16[j]);
+ }
+ av_freep(&s->scratchbuf);
+ return -1;
+ }
+
+ // avpriv_align_put_bits(&s->pb);
+ while (put_bits_count(&s->pb) & 31)
+ put_bits(&s->pb, 1, 0);
+
+ flush_put_bits(&s->pb);
+
+ pkt->size = put_bits_count(&s->pb) / 8;
+ if (s->pict_type == AV_PICTURE_TYPE_I)
+ pkt->flags |= AV_PKT_FLAG_KEY;
+ *got_packet = 1;