summaryrefslogtreecommitdiff
path: root/ffmpeg/libavcodec/mpegvideo_enc.c
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2013-12-29 12:19:38 +0000
committerTim Redfern <tim@eclectronics.org>2013-12-29 12:19:38 +0000
commitf7813a5324be39d13ab536c245d15dfc602a7849 (patch)
treefad99148b88823d34a5df2f0a25881a002eb291b /ffmpeg/libavcodec/mpegvideo_enc.c
parentb7a5a477b8ff4d4e3028b9dfb9a9df0a41463f92 (diff)
basic type mechanism working
Diffstat (limited to 'ffmpeg/libavcodec/mpegvideo_enc.c')
-rw-r--r--ffmpeg/libavcodec/mpegvideo_enc.c343
1 files changed, 211 insertions, 132 deletions
diff --git a/ffmpeg/libavcodec/mpegvideo_enc.c b/ffmpeg/libavcodec/mpegvideo_enc.c
index dd66943..6bf3e38 100644
--- a/ffmpeg/libavcodec/mpegvideo_enc.c
+++ b/ffmpeg/libavcodec/mpegvideo_enc.c
@@ -27,6 +27,8 @@
* The simplest mpeg encoder (well, it was the simplest!).
*/
+#include <stdint.h>
+
#include "libavutil/internal.h"
#include "libavutil/intmath.h"
#include "libavutil/mathematics.h"
@@ -35,7 +37,9 @@
#include "avcodec.h"
#include "dct.h"
#include "dsputil.h"
+#include "mpeg12.h"
#include "mpegvideo.h"
+#include "h261.h"
#include "h263.h"
#include "mathops.h"
#include "mjpegenc.h"
@@ -50,17 +54,12 @@
#include <limits.h>
#include "sp5x.h"
-//#undef NDEBUG
-//#include <assert.h>
-
static int encode_picture(MpegEncContext *s, int picture_number);
static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
static int sse_mb(MpegEncContext *s);
static void denoise_dct_c(MpegEncContext *s, int16_t *block);
static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
-//#define DEBUG
-
static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
@@ -185,19 +184,6 @@ void ff_init_qscale_tab(MpegEncContext *s)
}
}
-static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst,
- const AVFrame *src)
-{
- dst->pict_type = src->pict_type;
- dst->quality = src->quality;
- dst->coded_picture_number = src->coded_picture_number;
- dst->display_picture_number = src->display_picture_number;
- //dst->reference = src->reference;
- dst->pts = src->pts;
- dst->interlaced_frame = src->interlaced_frame;
- dst->top_field_first = src->top_field_first;
-}
-
static void update_duplicate_context_after_me(MpegEncContext *dst,
MpegEncContext *src)
{
@@ -231,12 +217,16 @@ static void MPV_encode_defaults(MpegEncContext *s)
}
s->me.mv_penalty = default_mv_penalty;
s->fcode_tab = default_fcode_tab;
+
+ s->input_picture_number = 0;
+ s->picture_in_gop_number = 0;
}
av_cold int ff_dct_encode_init(MpegEncContext *s) {
if (ARCH_X86)
ff_dct_encode_init_x86(s);
+ ff_h263dsp_init(&s->h263dsp);
if (!s->dct_quantize)
s->dct_quantize = ff_dct_quantize_c;
if (!s->denoise_dct)
@@ -252,8 +242,7 @@ av_cold int ff_dct_encode_init(MpegEncContext *s) {
av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
- int i;
- int chroma_h_shift, chroma_v_shift;
+ int i, ret;
MPV_encode_defaults(s);
@@ -266,21 +255,6 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
return -1;
}
break;
- case AV_CODEC_ID_LJPEG:
- if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
- avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
- avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
- avctx->pix_fmt != AV_PIX_FMT_BGR0 &&
- avctx->pix_fmt != AV_PIX_FMT_BGRA &&
- avctx->pix_fmt != AV_PIX_FMT_BGR24 &&
- ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
- avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
- avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
- avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
- av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
- return -1;
- }
- break;
case AV_CODEC_ID_MJPEG:
case AV_CODEC_ID_AMV:
if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
@@ -331,6 +305,11 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
s->avctx = avctx;
s->flags = avctx->flags;
s->flags2 = avctx->flags2;
+ if (avctx->max_b_frames > MAX_B_FRAMES) {
+ av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
+ "is %d.\n", MAX_B_FRAMES);
+ avctx->max_b_frames = MAX_B_FRAMES;
+ }
s->max_b_frames = avctx->max_b_frames;
s->codec_id = avctx->codec->id;
s->strict_std_compliance = avctx->strict_std_compliance;
@@ -468,6 +447,11 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
return -1;
}
+ if (s->max_b_frames < 0) {
+ av_log(avctx, AV_LOG_ERROR,
+ "max b frames must be 0 or positive for mpegvideo based encoders\n");
+ return -1;
+ }
if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
s->codec_id == AV_CODEC_ID_H263 ||
@@ -538,7 +522,8 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
}
// FIXME mpeg2 uses that too
- if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
+ if (s->mpeg_quant && ( s->codec_id != AV_CODEC_ID_MPEG4
+ && s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
av_log(avctx, AV_LOG_ERROR,
"mpeg2 style quantization not supported by codec\n");
return -1;
@@ -644,6 +629,11 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
}
+ if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
+ av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
+ return AVERROR(EINVAL);
+ }
+
if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
s->intra_quant_bias = avctx->intra_quant_bias;
if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
@@ -651,8 +641,6 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
- avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
-
if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
s->avctx->time_base.den > (1 << 16) - 1) {
av_log(avctx, AV_LOG_ERROR,
@@ -676,30 +664,11 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
s->rtp_mode = 1;
break;
- case AV_CODEC_ID_LJPEG:
case AV_CODEC_ID_MJPEG:
case AV_CODEC_ID_AMV:
s->out_format = FMT_MJPEG;
s->intra_only = 1; /* force intra only for jpeg */
- if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
- (avctx->pix_fmt == AV_PIX_FMT_BGR0
- || s->avctx->pix_fmt == AV_PIX_FMT_BGRA
- || s->avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
- s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
- s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
- s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
- } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
- s->mjpeg_vsample[0] = s->mjpeg_vsample[1] = s->mjpeg_vsample[2] = 2;
- s->mjpeg_hsample[0] = s->mjpeg_hsample[1] = s->mjpeg_hsample[2] = 1;
- } else {
- s->mjpeg_vsample[0] = 2;
- s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
- s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
- s->mjpeg_hsample[0] = 2;
- s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
- s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
- }
- if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
+ if (!CONFIG_MJPEG_ENCODER ||
ff_mjpeg_encode_init(s) < 0)
return -1;
avctx->delay = 0;
@@ -831,6 +800,31 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
if (ff_MPV_common_init(s) < 0)
return -1;
+ s->avctx->coded_frame = &s->current_picture.f;
+
+ if (s->msmpeg4_version) {
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
+ 2 * 2 * (MAX_LEVEL + 1) *
+ (MAX_RUN + 1) * 2 * sizeof(int), fail);
+ }
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
+
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
+ MAX_PICTURE_COUNT * sizeof(Picture *), fail);
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
+ MAX_PICTURE_COUNT * sizeof(Picture *), fail);
+
+ if (s->avctx->noise_reduction) {
+ FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
+ 2 * 64 * sizeof(uint16_t), fail);
+ }
+
ff_dct_encode_init(s);
if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
@@ -886,22 +880,70 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
if (ff_rate_control_init(s) < 0)
return -1;
+#if FF_API_ERROR_RATE
+ FF_DISABLE_DEPRECATION_WARNINGS
+ if (avctx->error_rate)
+ s->error_rate = avctx->error_rate;
+ FF_ENABLE_DEPRECATION_WARNINGS;
+#endif
+
+ if (avctx->b_frame_strategy == 2) {
+ for (i = 0; i < s->max_b_frames + 2; i++) {
+ s->tmp_frames[i] = av_frame_alloc();
+ if (!s->tmp_frames[i])
+ return AVERROR(ENOMEM);
+
+ s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
+ s->tmp_frames[i]->width = s->width >> avctx->brd_scale;
+ s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
+
+ ret = av_frame_get_buffer(s->tmp_frames[i], 32);
+ if (ret < 0)
+ return ret;
+ }
+ }
+
return 0;
+fail:
+ ff_MPV_encode_end(avctx);
+ return AVERROR_UNKNOWN;
}
av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
{
MpegEncContext *s = avctx->priv_data;
+ int i;
ff_rate_control_uninit(s);
ff_MPV_common_end(s);
- if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
+ if (CONFIG_MJPEG_ENCODER &&
s->out_format == FMT_MJPEG)
ff_mjpeg_encode_close(s);
av_freep(&avctx->extradata);
+ for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
+ av_frame_free(&s->tmp_frames[i]);
+
+ ff_free_picture_tables(&s->new_picture);
+ ff_mpeg_unref_picture(s, &s->new_picture);
+
+ av_freep(&s->avctx->stats_out);
+ av_freep(&s->ac_stats);
+
+ if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
+ if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
+ s->q_chroma_intra_matrix= NULL;
+ s->q_chroma_intra_matrix16= NULL;
+ av_freep(&s->q_intra_matrix);
+ av_freep(&s->q_inter_matrix);
+ av_freep(&s->q_intra_matrix16);
+ av_freep(&s->q_inter_matrix16);
+ av_freep(&s->input_picture);
+ av_freep(&s->reordered_input_picture);
+ av_freep(&s->dct_offset);
+
return 0;
}
@@ -958,18 +1000,17 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
if (pts != AV_NOPTS_VALUE) {
if (s->user_specified_pts != AV_NOPTS_VALUE) {
- int64_t time = pts;
int64_t last = s->user_specified_pts;
- if (time <= last) {
+ if (pts <= last) {
av_log(s->avctx, AV_LOG_ERROR,
- "Error, Invalid timestamp=%"PRId64", "
- "last=%"PRId64"\n", pts, s->user_specified_pts);
- return -1;
+ "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
+ pts, last);
+ return AVERROR(EINVAL);
}
if (!s->low_delay && display_picture_number == 1)
- s->dts_delta = time - last;
+ s->dts_delta = pts - last;
}
s->user_specified_pts = pts;
} else {
@@ -995,7 +1036,7 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
if (pic_arg->linesize[2] != s->uvlinesize)
direct = 0;
- av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
+ av_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
pic_arg->linesize[1], s->linesize, s->uvlinesize);
if (direct) {
@@ -1071,7 +1112,10 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
}
}
}
- copy_picture_attributes(s, &pic->f, pic_arg);
+ ret = av_frame_copy_props(&pic->f, pic_arg);
+ if (ret < 0)
+ return ret;
+
pic->f.display_picture_number = display_picture_number;
pic->f.pts = pts; // we set this here to avoid modifiying pic_arg
}
@@ -1104,9 +1148,9 @@ static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
switch (s->avctx->frame_skip_exp) {
case 0: score = FFMAX(score, v); break;
case 1: score += FFABS(v); break;
- case 2: score += v * v; break;
- case 3: score64 += FFABS(v * v * (int64_t)v); break;
- case 4: score64 += v * v * (int64_t)(v * v); break;
+ case 2: score64 += v * (int64_t)v; break;
+ case 3: score64 += FFABS(v * (int64_t)v * v); break;
+ case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
}
}
}
@@ -1141,7 +1185,6 @@ static int estimate_best_b_count(MpegEncContext *s)
{
AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
AVCodecContext *c = avcodec_alloc_context3(NULL);
- AVFrame input[FF_MAX_B_FRAMES + 2];
const int scale = s->avctx->brd_scale;
int i, j, out_size, p_lambda, b_lambda, lambda2;
int64_t best_rd = INT64_MAX;
@@ -1176,19 +1219,9 @@ static int estimate_best_b_count(MpegEncContext *s)
return -1;
for (i = 0; i < s->max_b_frames + 2; i++) {
- int ysize = c->width * c->height;
- int csize = (c->width / 2) * (c->height / 2);
Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
s->next_picture_ptr;
- avcodec_get_frame_defaults(&input[i]);
- input[i].data[0] = av_malloc(ysize + 2 * csize);
- input[i].data[1] = input[i].data[0] + ysize;
- input[i].data[2] = input[i].data[1] + csize;
- input[i].linesize[0] = c->width;
- input[i].linesize[1] =
- input[i].linesize[2] = c->width / 2;
-
if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
pre_input = *pre_input_ptr;
@@ -1198,13 +1231,13 @@ static int estimate_best_b_count(MpegEncContext *s)
pre_input.f.data[2] += INPLACE_OFFSET;
}
- s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
+ s->dsp.shrink[scale](s->tmp_frames[i]->data[0], s->tmp_frames[i]->linesize[0],
pre_input.f.data[0], pre_input.f.linesize[0],
c->width, c->height);
- s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
+ s->dsp.shrink[scale](s->tmp_frames[i]->data[1], s->tmp_frames[i]->linesize[1],
pre_input.f.data[1], pre_input.f.linesize[1],
c->width >> 1, c->height >> 1);
- s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
+ s->dsp.shrink[scale](s->tmp_frames[i]->data[2], s->tmp_frames[i]->linesize[2],
pre_input.f.data[2], pre_input.f.linesize[2],
c->width >> 1, c->height >> 1);
}
@@ -1218,21 +1251,21 @@ static int estimate_best_b_count(MpegEncContext *s)
c->error[0] = c->error[1] = c->error[2] = 0;
- input[0].pict_type = AV_PICTURE_TYPE_I;
- input[0].quality = 1 * FF_QP2LAMBDA;
+ s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
+ s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
- out_size = encode_frame(c, &input[0]);
+ out_size = encode_frame(c, s->tmp_frames[0]);
//rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
for (i = 0; i < s->max_b_frames + 1; i++) {
int is_p = i % (j + 1) == j || i == s->max_b_frames;
- input[i + 1].pict_type = is_p ?
+ s->tmp_frames[i + 1]->pict_type = is_p ?
AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
- input[i + 1].quality = is_p ? p_lambda : b_lambda;
+ s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
- out_size = encode_frame(c, &input[i + 1]);
+ out_size = encode_frame(c, s->tmp_frames[i + 1]);
rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
}
@@ -1254,10 +1287,6 @@ static int estimate_best_b_count(MpegEncContext *s)
avcodec_close(c);
av_freep(&c);
- for (i = 0; i < s->max_b_frames + 2; i++) {
- av_freep(&input[i].data[0]);
- }
-
return best_b_count;
}
@@ -1271,6 +1300,20 @@ static int select_input_picture(MpegEncContext *s)
/* set next picture type & ordering */
if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
+ if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
+ if (s->picture_in_gop_number < s->gop_size &&
+ s->next_picture_ptr &&
+ skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
+ // FIXME check that te gop check above is +-1 correct
+ av_frame_unref(&s->input_picture[0]->f);
+
+ emms_c();
+ ff_vbv_update(s, 0);
+
+ goto no_output_pic;
+ }
+ }
+
if (/*s->picture_in_gop_number >= s->gop_size ||*/
s->next_picture_ptr == NULL || s->intra_only) {
s->reordered_input_picture[0] = s->input_picture[0];
@@ -1280,19 +1323,6 @@ static int select_input_picture(MpegEncContext *s)
} else {
int b_frames;
- if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
- if (s->picture_in_gop_number < s->gop_size &&
- skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
- // FIXME check that te gop check above is +-1 correct
- av_frame_unref(&s->input_picture[0]->f);
-
- emms_c();
- ff_vbv_update(s, 0);
-
- goto no_output_pic;
- }
- }
-
if (s->flags & CODEC_FLAG_PASS2) {
for (i = 0; i < s->max_b_frames + 1; i++) {
int pict_num = s->input_picture[0]->f.display_picture_number + i;
@@ -1411,8 +1441,9 @@ no_output_pic:
return -1;
}
- copy_picture_attributes(s, &pic->f,
- &s->reordered_input_picture[0]->f);
+ ret = av_frame_copy_props(&pic->f, &s->reordered_input_picture[0]->f);
+ if (ret < 0)
+ return ret;
/* mark us unused / free shared pic */
av_frame_unref(&s->reordered_input_picture[0]->f);
@@ -1438,6 +1469,39 @@ no_output_pic:
return 0;
}
+static void frame_end(MpegEncContext *s)
+{
+ 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->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
+ s->h_edge_pos, s->v_edge_pos,
+ EDGE_WIDTH, EDGE_WIDTH,
+ EDGE_TOP | EDGE_BOTTOM);
+ s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
+ s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
+ EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
+ EDGE_TOP | EDGE_BOTTOM);
+ s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
+ 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;
+
+ s->avctx->coded_frame = &s->current_picture_ptr->f;
+
+}
+
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
AVFrame *pic_arg, int *got_packet)
{
@@ -1493,10 +1557,10 @@ vbv_retry:
avctx->p_count = s->mb_num - s->i_count - s->skip_count;
avctx->skip_count = s->skip_count;
- ff_MPV_frame_end(s);
+ frame_end(s);
if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
- ff_mjpeg_encode_picture_trailer(s);
+ ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
if (avctx->rc_buffer_size) {
RateControlContext *rcc = &s->rc_context;
@@ -1635,6 +1699,13 @@ vbv_retry:
} else {
s->frame_bits = 0;
}
+
+ /* release non-reference frames */
+ for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+ if (!s->picture[i].reference)
+ ff_mpeg_unref_picture(s, &s->picture[i]);
+ }
+
assert((s->frame_bits & 7) == 0);
pkt->size = s->frame_bits / 8;
@@ -1771,7 +1842,7 @@ static av_always_inline void encode_mb_internal(MpegEncContext *s,
int dct_offset = s->linesize * 8; // default for progressive frames
int uv_dct_offset = s->uvlinesize * 8;
uint8_t *ptr_y, *ptr_cb, *ptr_cr;
- int wrap_y, wrap_c;
+ ptrdiff_t wrap_y, wrap_c;
for (i = 0; i < mb_block_count; i++)
skip_dct[i] = s->skipdct;
@@ -1815,19 +1886,25 @@ static av_always_inline void encode_mb_internal(MpegEncContext *s,
ptr_cr = s->new_picture.f.data[2] +
(mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
- if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
+ if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
uint8_t *ebuf = s->edge_emu_buffer + 32;
int cw = (s->width + s->chroma_x_shift) >> s->chroma_x_shift;
int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
- s->vdsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
- mb_y * 16, s->width, s->height);
+ s->vdsp.emulated_edge_mc(ebuf, ptr_y,
+ wrap_y, wrap_y,
+ 16, 16, mb_x * 16, mb_y * 16,
+ s->width, s->height);
ptr_y = ebuf;
- s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, mb_block_width,
- mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
+ s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
+ wrap_c, wrap_c,
+ mb_block_width, mb_block_height,
+ mb_x * mb_block_width, mb_y * mb_block_height,
cw, ch);
ptr_cb = ebuf + 18 * wrap_y;
- s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, ptr_cr, wrap_c, mb_block_width,
- mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
+ s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, ptr_cr,
+ wrap_c, wrap_c,
+ mb_block_width, mb_block_height,
+ mb_x * mb_block_width, mb_y * mb_block_height,
cw, ch);
ptr_cr = ebuf + 18 * wrap_y + 16;
}
@@ -2567,6 +2644,7 @@ static int encode_thread(AVCodecContext *c, void *arg){
if(is_gob_start){
if(s->start_mb_y != mb_y || mb_x!=0){
write_slice_end(s);
+
if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
ff_mpeg4_init_partitions(s);
}
@@ -2575,9 +2653,9 @@ static int encode_thread(AVCodecContext *c, void *arg){
av_assert2((put_bits_count(&s->pb)&7) == 0);
current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
- if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
+ if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
- int d= 100 / s->avctx->error_rate;
+ int d = 100 / s->error_rate;
if(r % d == 0){
current_packet_size=0;
s->pb.buf_ptr= s->ptr_lastgob;
@@ -3372,7 +3450,8 @@ static int encode_picture(MpegEncContext *s, int picture_number)
switch(s->out_format) {
case FMT_MJPEG:
if (CONFIG_MJPEG_ENCODER)
- ff_mjpeg_encode_picture_header(s);
+ ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
+ s->intra_matrix);
break;
case FMT_H261:
if (CONFIG_H261_ENCODER)
@@ -3572,7 +3651,7 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
av_assert2(level);
- if(s->out_format == FMT_H263){
+ if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
unquant_coeff= alevel*qmul + qadd;
}else{ //MPEG1
j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
@@ -3601,7 +3680,7 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
}
}
- if(s->out_format == FMT_H263){
+ if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
for(j=survivor_count-1; j>=0; j--){
int run= i - survivor[j];
int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
@@ -3627,7 +3706,7 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
}
}
- if(s->out_format == FMT_H263){
+ if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
for(j=survivor_count-1; j>=0; j--){
int run= i - survivor[j];
int score= distortion + score_tab[i-run];
@@ -3660,7 +3739,7 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
survivor[ survivor_count++ ]= i+1;
}
- if(s->out_format != FMT_H263){
+ if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
last_score= 256*256*256*120;
for(i= survivor[0]; i<=last_non_zero + 1; i++){
int score= score_tab[i];
@@ -3693,7 +3772,7 @@ static int dct_quantize_trellis_c(MpegEncContext *s,
int alevel= FFABS(level);
int unquant_coeff, score, distortion;
- if(s->out_format == FMT_H263){
+ if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
unquant_coeff= (alevel*qmul + qadd)>>3;
}else{ //MPEG1
unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
@@ -4225,6 +4304,7 @@ static const AVClass h263_class = {
AVCodec ff_h263_encoder = {
.name = "h263",
+ .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_H263,
.priv_data_size = sizeof(MpegEncContext),
@@ -4232,7 +4312,6 @@ AVCodec ff_h263_encoder = {
.encode2 = ff_MPV_encode_picture,
.close = ff_MPV_encode_end,
.pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
- .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
.priv_class = &h263_class,
};
@@ -4253,6 +4332,7 @@ static const AVClass h263p_class = {
AVCodec ff_h263p_encoder = {
.name = "h263p",
+ .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_H263P,
.priv_data_size = sizeof(MpegEncContext),
@@ -4261,7 +4341,6 @@ AVCodec ff_h263p_encoder = {
.close = ff_MPV_encode_end,
.capabilities = CODEC_CAP_SLICE_THREADS,
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
- .long_name = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
.priv_class = &h263p_class,
};
@@ -4269,6 +4348,7 @@ FF_MPV_GENERIC_CLASS(msmpeg4v2)
AVCodec ff_msmpeg4v2_encoder = {
.name = "msmpeg4v2",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_MSMPEG4V2,
.priv_data_size = sizeof(MpegEncContext),
@@ -4276,7 +4356,6 @@ AVCodec ff_msmpeg4v2_encoder = {
.encode2 = ff_MPV_encode_picture,
.close = ff_MPV_encode_end,
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
- .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
.priv_class = &msmpeg4v2_class,
};
@@ -4284,6 +4363,7 @@ FF_MPV_GENERIC_CLASS(msmpeg4v3)
AVCodec ff_msmpeg4v3_encoder = {
.name = "msmpeg4",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_MSMPEG4V3,
.priv_data_size = sizeof(MpegEncContext),
@@ -4291,7 +4371,6 @@ AVCodec ff_msmpeg4v3_encoder = {
.encode2 = ff_MPV_encode_picture,
.close = ff_MPV_encode_end,
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
- .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
.priv_class = &msmpeg4v3_class,
};
@@ -4299,6 +4378,7 @@ FF_MPV_GENERIC_CLASS(wmv1)
AVCodec ff_wmv1_encoder = {
.name = "wmv1",
+ .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_WMV1,
.priv_data_size = sizeof(MpegEncContext),
@@ -4306,6 +4386,5 @@ AVCodec ff_wmv1_encoder = {
.encode2 = ff_MPV_encode_picture,
.close = ff_MPV_encode_end,
.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
- .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
.priv_class = &wmv1_class,
};