diff options
Diffstat (limited to 'libavcodec/mpegvideo_enc.c')
-rw-r--r-- | libavcodec/mpegvideo_enc.c | 109 |
1 files changed, 76 insertions, 33 deletions
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index f88df92d8d..15fa9a22b8 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -5,20 +5,20 @@ * * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -45,6 +45,7 @@ #include "internal.h" #include "bytestream.h" #include <limits.h> +#include "sp5x.h" //#undef NDEBUG //#include <assert.h> @@ -102,8 +103,7 @@ void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64], * 3444240 >= (1 << 36) / (x) >= 275 */ qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / - (ff_aanscales[i] * qscale * - quant_matrix[j])); + (ff_aanscales[i] * qscale * quant_matrix[j])); } } else { for (i = 0; i < 64; i++) { @@ -290,7 +290,9 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) if (avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P && avctx->pix_fmt != PIX_FMT_YUVJ444P && + avctx->pix_fmt != PIX_FMT_BGR0 && avctx->pix_fmt != PIX_FMT_BGRA && + avctx->pix_fmt != PIX_FMT_BGR24 && ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P && avctx->pix_fmt != PIX_FMT_YUV444P) || @@ -300,6 +302,7 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) } break; case CODEC_ID_MJPEG: + case CODEC_ID_AMV: if (avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P && ((avctx->pix_fmt != PIX_FMT_YUV420P && @@ -333,8 +336,9 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) s->height = avctx->height; if (avctx->gop_size > 600 && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { - av_log(avctx, AV_LOG_ERROR, - "Warning keyframe interval too large! reducing it ...\n"); + av_log(avctx, AV_LOG_WARNING, + "keyframe interval too large!, reducing it from %d to %d\n", + avctx->gop_size, 600); avctx->gop_size = 600; } s->gop_size = avctx->gop_size; @@ -384,11 +388,10 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) s->loop_filter = !!(s->flags & CODEC_FLAG_LOOP_FILTER); - if (avctx->rc_max_rate && !avctx->rc_buffer_size) { - av_log(avctx, AV_LOG_ERROR, - "a vbv buffer size is needed, " - "for encoding with a maximum bitrate\n"); - return -1; + if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) { + av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n"); + if (avctx->rc_max_rate && !avctx->rc_buffer_size) + return -1; } if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) { @@ -402,7 +405,7 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) } if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) { - av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n"); + av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n"); return -1; } @@ -470,10 +473,11 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) s->codec_id == CODEC_ID_H263P) && (avctx->sample_aspect_ratio.num > 255 || avctx->sample_aspect_ratio.den > 255)) { - av_log(avctx, AV_LOG_ERROR, - "Invalid pixel aspect ratio %i/%i, limit is 255/255\n", + av_log(avctx, AV_LOG_WARNING, + "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n", avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den); - return -1; + av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den, + avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255); } if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) && @@ -546,7 +550,7 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) if (s->avctx->thread_count < 1) { av_log(avctx, AV_LOG_ERROR, - "automatic thread number detection not supported by codec," + "automatic thread number detection not supported by codec, " "patch welcome\n"); return -1; } @@ -585,8 +589,7 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) //return -1; } - if (s->mpeg_quant || s->codec_id == CODEC_ID_MPEG1VIDEO || - s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG) { + if (s->mpeg_quant || s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id==CODEC_ID_AMV) { // (a + x * 3 / 8) / x s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3); s->inter_quant_bias = 0; @@ -601,6 +604,8 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS) s->inter_quant_bias = avctx->inter_quant_bias; + 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); @@ -638,10 +643,13 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) break; case CODEC_ID_LJPEG: case CODEC_ID_MJPEG: + case CODEC_ID_AMV: s->out_format = FMT_MJPEG; s->intra_only = 1; /* force intra only for jpeg */ if (avctx->codec->id == CODEC_ID_LJPEG && - avctx->pix_fmt == PIX_FMT_BGRA) { + (avctx->pix_fmt == PIX_FMT_BGR0 + || s->avctx->pix_fmt == PIX_FMT_BGRA + || s->avctx->pix_fmt == 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; @@ -675,13 +683,13 @@ av_cold int ff_MPV_encode_init(AVCodecContext *avctx) break; case CODEC_ID_H263: if (!CONFIG_H263_ENCODER) - return -1; + return -1; if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format), s->width, s->height) == 8) { - av_log(avctx, AV_LOG_INFO, + av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for " "the H.263 codec.\nValid sizes are 128x96, 176x144, " - "352x288, 704x576, and 1408x1152." + "352x288, 704x576, and 1408x1152. " "Try H.263+.\n", s->width, s->height); return -1; } @@ -1004,6 +1012,10 @@ static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg) uint8_t *src = pic_arg->data[i]; uint8_t *dst = pic->data[i]; + if(s->codec_id == CODEC_ID_AMV && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)){ + h= ((s->height+15)/16*16)>>v_shift; + } + if (!s->avctx->rc_buffer_size) dst += INPLACE_OFFSET; @@ -1396,7 +1408,7 @@ no_output_pic: } int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt, - const AVFrame *pic_arg, int *got_packet) + AVFrame *pic_arg, int *got_packet) { MpegEncContext *s = avctx->priv_data; int i, stuffing_count, ret; @@ -1413,8 +1425,7 @@ int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt, /* output? */ if (s->new_picture.f.data[0]) { - if (!pkt->data && - (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0) + if ((ret = ff_alloc_packet2(avctx, pkt, s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000)) < 0) return ret; if (s->mb_info) { s->mb_info_ptr = av_packet_new_side_data(pkt, @@ -1579,12 +1590,12 @@ vbv_retry: avctx->frame_bits = s->frame_bits; pkt->pts = s->current_picture.f.pts; - if (!s->low_delay) { + if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) { if (!s->current_picture.f.coded_picture_number) pkt->dts = pkt->pts - s->dts_delta; else pkt->dts = s->reordered_pts; - s->reordered_pts = s->input_picture[0]->f.pts; + s->reordered_pts = pkt->pts; } else pkt->dts = pkt->pts; if (s->current_picture.f.key_frame) @@ -1773,7 +1784,7 @@ 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 * 8; - if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) { + if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != CODEC_ID_AMV){ uint8_t *ebuf = s->edge_emu_buffer + 32; s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16, mb_y * 16, s->width, s->height); @@ -2070,6 +2081,7 @@ static av_always_inline void encode_mb_internal(MpegEncContext *s, ff_h263_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MJPEG: + case CODEC_ID_AMV: if (CONFIG_MJPEG_ENCODER) ff_mjpeg_encode_mb(s, s->block); break; @@ -2414,6 +2426,11 @@ static int encode_thread(AVCodecContext *c, void *arg){ s->current_picture.f.error[i] = 0; } + if(s->codec_id==CODEC_ID_AMV){ + s->last_dc[0] = 128*8/13; + s->last_dc[1] = 128*8/14; + s->last_dc[2] = 128*8/14; + } s->mb_skip_run = 0; memset(s->last_mv, 0, sizeof(s->last_mv)); @@ -2458,7 +2475,7 @@ static int encode_thread(AVCodecContext *c, void *arg){ if(s->data_partitioning){ if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){ - av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n"); + av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n"); return -1; } } @@ -3142,6 +3159,13 @@ static int encode_picture(MpegEncContext *s, int picture_number) update_qscale(s); } + if(s->codec_id != CODEC_ID_AMV){ + 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 = s->q_intra_matrix; + s->q_chroma_intra_matrix16 = s->q_intra_matrix16; + } + s->mb_intra=0; //for the rate distortion & bit compare functions for(i=1; i<context_count; i++){ ff_update_duplicate_context(s->thread_context[i], s); @@ -3259,6 +3283,25 @@ static int encode_picture(MpegEncContext *s, int picture_number) s->intra_matrix, s->intra_quant_bias, 8, 8, 1); s->qscale= 8; } + if(s->codec_id == CODEC_ID_AMV){ + static const uint8_t y[32]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13}; + static const uint8_t c[32]={14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14}; + for(i=1;i<64;i++){ + int j= s->dsp.idct_permutation[ff_zigzag_direct[i]]; + + s->intra_matrix[j] = sp5x_quant_table[5*2+0][i]; + s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i]; + } + s->y_dc_scale_table= y; + s->c_dc_scale_table= c; + s->intra_matrix[0] = 13; + s->chroma_intra_matrix[0] = 14; + ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16, + s->intra_matrix, s->intra_quant_bias, 8, 8, 1); + ff_convert_matrix(&s->dsp, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16, + s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1); + s->qscale= 8; + } //FIXME var duplication s->current_picture_ptr->f.key_frame = @@ -3393,7 +3436,7 @@ static int dct_quantize_trellis_c(MpegEncContext *s, block[0] = (block[0] + (q >> 1)) / q; start_i = 1; last_non_zero = 0; - qmat = s->q_intra_matrix[qscale]; + qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale]; if(s->mpeg_quant || s->out_format == FMT_MPEG1) bias= 1<<(QMAT_SHIFT-1); length = s->intra_ac_vlc_length; @@ -4060,7 +4103,7 @@ int ff_dct_quantize_c(MpegEncContext *s, block[0] = (block[0] + (q >> 1)) / q; start_i = 1; last_non_zero = 0; - qmat = s->q_intra_matrix[qscale]; + qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale]; bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); } else { start_i = 0; |