You've already forked FFmpeg
							
							
				mirror of
				https://github.com/FFmpeg/FFmpeg.git
				synced 2025-10-30 23:18:11 +02:00 
			
		
		
		
	avcodec/prores_enc : not calculate dct a each quantif search step
Improve encoding speed by 2% (using prores input)
This commit is contained in:
		| @@ -219,7 +219,7 @@ static const uint8_t run_to_cb[16] = { 0x06, 0x06, 0x05, 0x05, 0x04, 0x29, | |||||||
| static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, | static const uint8_t lev_to_cb[10] = { 0x04, 0x0A, 0x05, 0x06, 0x04, 0x28, | ||||||
|         0x28, 0x28, 0x28, 0x4C }; |         0x28, 0x28, 0x28, 0x4C }; | ||||||
|  |  | ||||||
| static void encode_ac_coeffs(AVCodecContext *avctx, PutBitContext *pb, | static void encode_ac_coeffs(PutBitContext *pb, | ||||||
|         int16_t *in, int blocks_per_slice, int *qmat) |         int16_t *in, int blocks_per_slice, int *qmat) | ||||||
| { | { | ||||||
|     int prev_run = 4; |     int prev_run = 4; | ||||||
| @@ -268,16 +268,10 @@ static void fdct_get(FDCTDSPContext *fdsp, uint8_t *pixels, int stride, int16_t* | |||||||
|     fdsp->fdct(block); |     fdsp->fdct(block); | ||||||
| } | } | ||||||
|  |  | ||||||
| static int encode_slice_plane(AVCodecContext *avctx, int mb_count, | static void calc_plane_dct(FDCTDSPContext *fdsp, uint8_t *src, int16_t * blocks, int src_stride, int mb_count, int chroma) | ||||||
|         uint8_t *src, int src_stride, uint8_t *buf, unsigned buf_size, |  | ||||||
|         int *qmat, int chroma) |  | ||||||
| { | { | ||||||
|     ProresContext* ctx = avctx->priv_data; |  | ||||||
|     FDCTDSPContext *fdsp = &ctx->fdsp; |  | ||||||
|     LOCAL_ALIGNED(16, int16_t, blocks, [DEFAULT_SLICE_MB_WIDTH << 8]); |  | ||||||
|     int16_t *block; |     int16_t *block; | ||||||
|     int i, blocks_per_slice; |     int i; | ||||||
|     PutBitContext pb; |  | ||||||
|  |  | ||||||
|     block = blocks; |     block = blocks; | ||||||
|     for (i = 0; i < mb_count; i++) { |     for (i = 0; i < mb_count; i++) { | ||||||
| @@ -291,37 +285,41 @@ static int encode_slice_plane(AVCodecContext *avctx, int mb_count, | |||||||
|         block += (256 >> chroma); |         block += (256 >> chroma); | ||||||
|         src   += (32  >> chroma); |         src   += (32  >> chroma); | ||||||
|     } |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | static int encode_slice_plane(int16_t *blocks, int mb_count, uint8_t *buf, unsigned buf_size, int *qmat, int chroma) | ||||||
|  | { | ||||||
|  |     int blocks_per_slice; | ||||||
|  |     PutBitContext pb; | ||||||
|  |  | ||||||
|     blocks_per_slice = mb_count << (2 - chroma); |     blocks_per_slice = mb_count << (2 - chroma); | ||||||
|     init_put_bits(&pb, buf, buf_size); |     init_put_bits(&pb, buf, buf_size); | ||||||
|  |  | ||||||
|     encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat); |     encode_dc_coeffs(&pb, blocks, blocks_per_slice, qmat); | ||||||
|     encode_ac_coeffs(avctx, &pb, blocks, blocks_per_slice, qmat); |     encode_ac_coeffs(&pb, blocks, blocks_per_slice, qmat); | ||||||
|  |  | ||||||
|     flush_put_bits(&pb); |     flush_put_bits(&pb); | ||||||
|     return put_bits_ptr(&pb) - pb.buf; |     return put_bits_ptr(&pb) - pb.buf; | ||||||
| } | } | ||||||
|  |  | ||||||
| static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx, | static av_always_inline unsigned encode_slice_data(AVCodecContext *avctx, | ||||||
|         uint8_t *dest_y, uint8_t *dest_u, uint8_t *dest_v, int luma_stride, |                                                    int16_t * blocks_y, int16_t * blocks_u, int16_t * blocks_v, | ||||||
|         int chroma_stride, unsigned mb_count, uint8_t *buf, unsigned data_size, |                                                    unsigned mb_count, uint8_t *buf, unsigned data_size, | ||||||
|         unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size, |                                                    unsigned* y_data_size, unsigned* u_data_size, unsigned* v_data_size, | ||||||
|         int qp) |                                                    int qp) | ||||||
| { | { | ||||||
|     ProresContext* ctx = avctx->priv_data; |     ProresContext* ctx = avctx->priv_data; | ||||||
|  |  | ||||||
|     *y_data_size = encode_slice_plane(avctx, mb_count, dest_y, luma_stride, |     *y_data_size = encode_slice_plane(blocks_y, mb_count, | ||||||
|             buf, data_size, ctx->qmat_luma[qp - 1], 0); |                                       buf, data_size, ctx->qmat_luma[qp - 1], 0); | ||||||
|  |  | ||||||
|     if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) { |     if (!(avctx->flags & AV_CODEC_FLAG_GRAY)) { | ||||||
|         *u_data_size = encode_slice_plane(avctx, mb_count, dest_u, |         *u_data_size = encode_slice_plane(blocks_u, mb_count, buf + *y_data_size, data_size - *y_data_size, | ||||||
|                 chroma_stride, buf + *y_data_size, data_size - *y_data_size, |                                           ctx->qmat_chroma[qp - 1], 1); | ||||||
|                 ctx->qmat_chroma[qp - 1], 1); |  | ||||||
|  |  | ||||||
|         *v_data_size = encode_slice_plane(avctx, mb_count, dest_v, |         *v_data_size = encode_slice_plane(blocks_v, mb_count, buf + *y_data_size + *u_data_size, | ||||||
|                 chroma_stride, buf + *y_data_size + *u_data_size, |                                           data_size - *y_data_size - *u_data_size, | ||||||
|                 data_size - *y_data_size - *u_data_size, |                                           ctx->qmat_chroma[qp - 1], 1); | ||||||
|                 ctx->qmat_chroma[qp - 1], 1); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return *y_data_size + *u_data_size + *v_data_size; |     return *y_data_size + *u_data_size + *v_data_size; | ||||||
| @@ -366,10 +364,15 @@ static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, int mb_x, | |||||||
|     uint8_t *dest_y, *dest_u, *dest_v; |     uint8_t *dest_y, *dest_u, *dest_v; | ||||||
|     unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0; |     unsigned y_data_size = 0, u_data_size = 0, v_data_size = 0; | ||||||
|     ProresContext* ctx = avctx->priv_data; |     ProresContext* ctx = avctx->priv_data; | ||||||
|  |     FDCTDSPContext *fdsp = &ctx->fdsp; | ||||||
|     int tgt_bits   = (mb_count * bitrate_table[avctx->profile]) >> 2; |     int tgt_bits   = (mb_count * bitrate_table[avctx->profile]) >> 2; | ||||||
|     int low_bytes  = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation |     int low_bytes  = (tgt_bits - (tgt_bits >> 3)) >> 3; // 12% bitrate fluctuation | ||||||
|     int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3; |     int high_bytes = (tgt_bits + (tgt_bits >> 3)) >> 3; | ||||||
|  |  | ||||||
|  |     LOCAL_ALIGNED(16, int16_t, blocks_y, [DEFAULT_SLICE_MB_WIDTH << 8]); | ||||||
|  |     LOCAL_ALIGNED(16, int16_t, blocks_u, [DEFAULT_SLICE_MB_WIDTH << 8]); | ||||||
|  |     LOCAL_ALIGNED(16, int16_t, blocks_v, [DEFAULT_SLICE_MB_WIDTH << 8]); | ||||||
|  |  | ||||||
|     luma_stride   = pic->linesize[0]; |     luma_stride   = pic->linesize[0]; | ||||||
|     chroma_stride = pic->linesize[1]; |     chroma_stride = pic->linesize[1]; | ||||||
|  |  | ||||||
| @@ -389,32 +392,40 @@ static int encode_slice(AVCodecContext *avctx, const AVFrame *pic, int mb_x, | |||||||
|                 chroma_stride, avctx->width >> 1, avctx->height, |                 chroma_stride, avctx->width >> 1, avctx->height, | ||||||
|                 (uint16_t *) ctx->fill_v, mb_count << 3, 16); |                 (uint16_t *) ctx->fill_v, mb_count << 3, 16); | ||||||
|  |  | ||||||
|         encode_slice_data(avctx, ctx->fill_y, ctx->fill_u, ctx->fill_v, |         calc_plane_dct(fdsp, ctx->fill_y, blocks_y, mb_count << 5, mb_count, 0); | ||||||
|                 mb_count << 5, mb_count << 4, mb_count, buf + hdr_size, |         calc_plane_dct(fdsp, ctx->fill_u, blocks_u, mb_count << 4, mb_count, 1); | ||||||
|                 data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size, |         calc_plane_dct(fdsp, ctx->fill_v, blocks_v, mb_count << 4, mb_count, 1); | ||||||
|                 *qp); |  | ||||||
|  |         encode_slice_data(avctx, blocks_y, blocks_u, blocks_v, | ||||||
|  |                           mb_count, buf + hdr_size, data_size - hdr_size, | ||||||
|  |                           &y_data_size, &u_data_size, &v_data_size, | ||||||
|  |                           *qp); | ||||||
|     } else { |     } else { | ||||||
|         slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v, |         calc_plane_dct(fdsp, dest_y, blocks_y, luma_stride, mb_count, 0); | ||||||
|                 luma_stride, chroma_stride, mb_count, buf + hdr_size, |         calc_plane_dct(fdsp, dest_u, blocks_u, chroma_stride, mb_count, 1); | ||||||
|                 data_size - hdr_size, &y_data_size, &u_data_size, &v_data_size, |         calc_plane_dct(fdsp, dest_v, blocks_v, chroma_stride, mb_count, 1); | ||||||
|                 *qp); |  | ||||||
|  |         slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v, | ||||||
|  |                           mb_count, buf + hdr_size, data_size - hdr_size, | ||||||
|  |                           &y_data_size, &u_data_size, &v_data_size, | ||||||
|  |                           *qp); | ||||||
|  |  | ||||||
|         if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) { |         if (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]) { | ||||||
|             do { |             do { | ||||||
|                 *qp += 1; |                 *qp += 1; | ||||||
|                 slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v, |                 slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v, | ||||||
|                         luma_stride, chroma_stride, mb_count, buf + hdr_size, |                                                mb_count, buf + hdr_size, data_size - hdr_size, | ||||||
|                         data_size - hdr_size, &y_data_size, &u_data_size, |                                                &y_data_size, &u_data_size, &v_data_size, | ||||||
|                         &v_data_size, *qp); |                                                *qp); | ||||||
|             } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]); |             } while (slice_size > high_bytes && *qp < qp_end_table[avctx->profile]); | ||||||
|         } else if (slice_size < low_bytes && *qp |         } else if (slice_size < low_bytes && *qp | ||||||
|                 > qp_start_table[avctx->profile]) { |                 > qp_start_table[avctx->profile]) { | ||||||
|             do { |             do { | ||||||
|                 *qp -= 1; |                 *qp -= 1; | ||||||
|                 slice_size = encode_slice_data(avctx, dest_y, dest_u, dest_v, |                 slice_size = encode_slice_data(avctx, blocks_y, blocks_u, blocks_v, | ||||||
|                         luma_stride, chroma_stride, mb_count, buf + hdr_size, |                                                mb_count, buf + hdr_size, data_size - hdr_size, | ||||||
|                         data_size - hdr_size, &y_data_size, &u_data_size, |                                                &y_data_size, &u_data_size, &v_data_size, | ||||||
|                         &v_data_size, *qp); |                                                *qp); | ||||||
|             } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]); |             } while (slice_size < low_bytes && *qp > qp_start_table[avctx->profile]); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user