Mercurial > libavcodec.hg
annotate dnxhdenc.c @ 8247:e8cb66bdae99 libavcodec
Trivial rephrasing, avoids too long line
author | reynaldo |
---|---|
date | Tue, 02 Dec 2008 18:26:55 +0000 |
parents | a9734fe0811e |
children | d77ee820a5f1 |
rev | line source |
---|---|
5790 | 1 /* |
2 * VC3/DNxHD encoder | |
3 * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at smartjog dot com> | |
4 * | |
5 * VC-3 encoder funded by the British Broadcasting Corporation | |
6 * | |
7 * This file is part of FFmpeg. | |
8 * | |
9 * FFmpeg is free software; you can redistribute it and/or | |
10 * modify it under the terms of the GNU Lesser General Public | |
11 * License as published by the Free Software Foundation; either | |
12 * version 2.1 of the License, or (at your option) any later version. | |
13 * | |
14 * FFmpeg is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * Lesser General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU Lesser General Public | |
20 * License along with FFmpeg; if not, write to the Free Software | |
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 */ | |
23 | |
24 //#define DEBUG | |
25 #define RC_VARIANCE 1 // use variance or ssd for fast rc | |
26 | |
27 #include "avcodec.h" | |
28 #include "dsputil.h" | |
29 #include "mpegvideo.h" | |
30 #include "dnxhddata.h" | |
31 | |
32 typedef struct { | |
33 uint16_t mb; | |
34 int value; | |
35 } RCCMPEntry; | |
36 | |
37 typedef struct { | |
38 int ssd; | |
39 int bits; | |
40 } RCEntry; | |
41 | |
42 int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); | |
43 | |
44 typedef struct DNXHDEncContext { | |
45 MpegEncContext m; ///< Used for quantization dsp functions | |
46 | |
47 AVFrame frame; | |
48 int cid; | |
49 const CIDEntry *cid_table; | |
6903 | 50 uint8_t *msip; ///< Macroblock Scan Indexes Payload |
5790 | 51 uint32_t *slice_size; |
52 | |
53 struct DNXHDEncContext *thread[MAX_THREADS]; | |
54 | |
55 unsigned dct_y_offset; | |
56 unsigned dct_uv_offset; | |
57 int interlaced; | |
58 int cur_field; | |
59 | |
60 DECLARE_ALIGNED_16(DCTELEM, blocks[8][64]); | |
61 | |
62 int (*qmatrix_c) [64]; | |
63 int (*qmatrix_l) [64]; | |
64 uint16_t (*qmatrix_l16)[2][64]; | |
65 uint16_t (*qmatrix_c16)[2][64]; | |
66 | |
67 unsigned frame_bits; | |
68 uint8_t *src[3]; | |
69 | |
6981 | 70 uint32_t *vlc_codes; |
71 uint8_t *vlc_bits; | |
72 uint16_t *run_codes; | |
73 uint8_t *run_bits; | |
5790 | 74 |
75 /** Rate control */ | |
76 unsigned slice_bits; | |
77 unsigned qscale; | |
78 unsigned lambda; | |
79 | |
80 unsigned thread_size; | |
81 | |
82 uint16_t *mb_bits; | |
83 uint8_t *mb_qscale; | |
84 | |
85 RCCMPEntry *mb_cmp; | |
86 RCEntry (*mb_rc)[8160]; | |
87 } DNXHDEncContext; | |
88 | |
89 #define LAMBDA_FRAC_BITS 10 | |
90 | |
91 static int dnxhd_init_vlc(DNXHDEncContext *ctx) | |
92 { | |
6978 | 93 int i, j, level, run; |
94 int max_level = 1<<(ctx->cid_table->bit_depth+2); | |
5790 | 95 |
6981 | 96 CHECKED_ALLOCZ(ctx->vlc_codes, max_level*4*sizeof(*ctx->vlc_codes)); |
97 CHECKED_ALLOCZ(ctx->vlc_bits, max_level*4*sizeof(*ctx->vlc_bits)); | |
98 CHECKED_ALLOCZ(ctx->run_codes, 63*2); | |
99 CHECKED_ALLOCZ(ctx->run_bits, 63); | |
6978 | 100 |
6981 | 101 ctx->vlc_codes += max_level*2; |
102 ctx->vlc_bits += max_level*2; | |
6978 | 103 for (level = -max_level; level < max_level; level++) { |
104 for (run = 0; run < 2; run++) { | |
105 int index = (level<<1)|run; | |
106 int sign, offset = 0, alevel = level; | |
5790 | 107 |
6978 | 108 MASK_ABS(sign, alevel); |
109 if (alevel > 64) { | |
110 offset = (alevel-1)>>6; | |
111 alevel -= offset<<6; | |
112 } | |
113 for (j = 0; j < 257; j++) { | |
114 if (ctx->cid_table->ac_level[j] == alevel && | |
115 (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) && | |
116 (!run || (ctx->cid_table->ac_run_flag [j] && run))) { | |
6981 | 117 assert(!ctx->vlc_codes[index]); |
6978 | 118 if (alevel) { |
6981 | 119 ctx->vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1); |
120 ctx->vlc_bits [index] = ctx->cid_table->ac_bits[j]+1; | |
6978 | 121 } else { |
6981 | 122 ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j]; |
123 ctx->vlc_bits [index] = ctx->cid_table->ac_bits [j]; | |
6978 | 124 } |
125 break; | |
126 } | |
127 } | |
128 assert(!alevel || j < 257); | |
129 if (offset) { | |
6981 | 130 ctx->vlc_codes[index] = (ctx->vlc_codes[index]<<ctx->cid_table->index_bits)|offset; |
131 ctx->vlc_bits [index]+= ctx->cid_table->index_bits; | |
6978 | 132 } |
133 } | |
5790 | 134 } |
135 for (i = 0; i < 62; i++) { | |
136 int run = ctx->cid_table->run[i]; | |
137 assert(run < 63); | |
6981 | 138 ctx->run_codes[run] = ctx->cid_table->run_codes[i]; |
139 ctx->run_bits [run] = ctx->cid_table->run_bits[i]; | |
5790 | 140 } |
141 return 0; | |
142 fail: | |
143 return -1; | |
144 } | |
145 | |
146 static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias) | |
147 { | |
148 // init first elem to 1 to avoid div by 0 in convert_matrix | |
149 uint16_t weight_matrix[64] = {1,}; // convert_matrix needs uint16_t* | |
150 int qscale, i; | |
151 | |
152 CHECKED_ALLOCZ(ctx->qmatrix_l, (ctx->m.avctx->qmax+1) * 64 * sizeof(int)); | |
153 CHECKED_ALLOCZ(ctx->qmatrix_c, (ctx->m.avctx->qmax+1) * 64 * sizeof(int)); | |
154 CHECKED_ALLOCZ(ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t)); | |
155 CHECKED_ALLOCZ(ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t)); | |
156 | |
157 for (i = 1; i < 64; i++) { | |
158 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]]; | |
5795 | 159 weight_matrix[j] = ctx->cid_table->luma_weight[i]; |
5790 | 160 } |
161 ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix, | |
162 ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1); | |
163 for (i = 1; i < 64; i++) { | |
164 int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]]; | |
5795 | 165 weight_matrix[j] = ctx->cid_table->chroma_weight[i]; |
5790 | 166 } |
167 ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix, | |
168 ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1); | |
169 for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) { | |
170 for (i = 0; i < 64; i++) { | |
171 ctx->qmatrix_l [qscale] [i] <<= 2; ctx->qmatrix_c [qscale] [i] <<= 2; | |
172 ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2; | |
173 ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2; | |
174 } | |
175 } | |
176 return 0; | |
177 fail: | |
178 return -1; | |
179 } | |
180 | |
181 static int dnxhd_init_rc(DNXHDEncContext *ctx) | |
182 { | |
183 CHECKED_ALLOCZ(ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry)); | |
184 if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD) | |
185 CHECKED_ALLOCZ(ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry)); | |
186 | |
187 ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4) * 8; | |
188 ctx->qscale = 1; | |
189 ctx->lambda = 2<<LAMBDA_FRAC_BITS; // qscale 2 | |
190 return 0; | |
191 fail: | |
192 return -1; | |
193 } | |
194 | |
195 static int dnxhd_encode_init(AVCodecContext *avctx) | |
196 { | |
197 DNXHDEncContext *ctx = avctx->priv_data; | |
198 int i, index; | |
199 | |
6041
bb4b486c6775
add bitrate helper to choose all dnxhd variants
bcoudurier
parents:
5972
diff
changeset
|
200 ctx->cid = ff_dnxhd_find_cid(avctx); |
5971 | 201 if (!ctx->cid || avctx->pix_fmt != PIX_FMT_YUV422P) { |
5790 | 202 av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\n"); |
203 return -1; | |
204 } | |
6041
bb4b486c6775
add bitrate helper to choose all dnxhd variants
bcoudurier
parents:
5972
diff
changeset
|
205 av_log(avctx, AV_LOG_DEBUG, "cid %d\n", ctx->cid); |
5790 | 206 |
207 index = ff_dnxhd_get_cid_table(ctx->cid); | |
208 ctx->cid_table = &ff_dnxhd_cid_table[index]; | |
209 | |
210 ctx->m.avctx = avctx; | |
211 ctx->m.mb_intra = 1; | |
212 ctx->m.h263_aic = 1; | |
213 | |
214 dsputil_init(&ctx->m.dsp, avctx); | |
215 ff_dct_common_init(&ctx->m); | |
216 if (!ctx->m.dct_quantize) | |
217 ctx->m.dct_quantize = dct_quantize_c; | |
218 | |
219 ctx->m.mb_height = (avctx->height + 15) / 16; | |
220 ctx->m.mb_width = (avctx->width + 15) / 16; | |
221 | |
222 if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) { | |
223 ctx->interlaced = 1; | |
224 ctx->m.mb_height /= 2; | |
225 } | |
226 | |
227 ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width; | |
228 | |
229 if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS) | |
230 ctx->m.intra_quant_bias = avctx->intra_quant_bias; | |
231 if (dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0) < 0) // XXX tune lbias/cbias | |
232 return -1; | |
233 | |
234 if (dnxhd_init_vlc(ctx) < 0) | |
235 return -1; | |
236 if (dnxhd_init_rc(ctx) < 0) | |
237 return -1; | |
238 | |
239 CHECKED_ALLOCZ(ctx->slice_size, ctx->m.mb_height*sizeof(uint32_t)); | |
240 CHECKED_ALLOCZ(ctx->mb_bits, ctx->m.mb_num *sizeof(uint16_t)); | |
241 CHECKED_ALLOCZ(ctx->mb_qscale, ctx->m.mb_num *sizeof(uint8_t)); | |
242 | |
243 ctx->frame.key_frame = 1; | |
244 ctx->frame.pict_type = FF_I_TYPE; | |
245 ctx->m.avctx->coded_frame = &ctx->frame; | |
246 | |
247 if (avctx->thread_count > MAX_THREADS || (avctx->thread_count > ctx->m.mb_height)) { | |
248 av_log(avctx, AV_LOG_ERROR, "too many threads\n"); | |
249 return -1; | |
250 } | |
251 | |
252 ctx->thread[0] = ctx; | |
253 for (i = 1; i < avctx->thread_count; i++) { | |
254 ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext)); | |
255 memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext)); | |
256 } | |
257 | |
258 for (i = 0; i < avctx->thread_count; i++) { | |
259 ctx->thread[i]->m.start_mb_y = (ctx->m.mb_height*(i ) + avctx->thread_count/2) / avctx->thread_count; | |
260 ctx->thread[i]->m.end_mb_y = (ctx->m.mb_height*(i+1) + avctx->thread_count/2) / avctx->thread_count; | |
261 } | |
262 | |
263 return 0; | |
264 fail: //for CHECKED_ALLOCZ | |
265 return -1; | |
266 } | |
267 | |
268 static int dnxhd_write_header(AVCodecContext *avctx, uint8_t *buf) | |
269 { | |
270 DNXHDEncContext *ctx = avctx->priv_data; | |
271 const uint8_t header_prefix[5] = { 0x00,0x00,0x02,0x80,0x01 }; | |
272 | |
273 memcpy(buf, header_prefix, 5); | |
274 buf[5] = ctx->interlaced ? ctx->cur_field+2 : 0x01; | |
275 buf[6] = 0x80; // crc flag off | |
276 buf[7] = 0xa0; // reserved | |
277 AV_WB16(buf + 0x18, avctx->height); // ALPF | |
278 AV_WB16(buf + 0x1a, avctx->width); // SPL | |
279 AV_WB16(buf + 0x1d, avctx->height); // NAL | |
280 | |
281 buf[0x21] = 0x38; // FIXME 8 bit per comp | |
282 buf[0x22] = 0x88 + (ctx->frame.interlaced_frame<<2); | |
283 AV_WB32(buf + 0x28, ctx->cid); // CID | |
284 buf[0x2c] = ctx->interlaced ? 0 : 0x80; | |
285 | |
286 buf[0x5f] = 0x01; // UDL | |
287 | |
288 buf[0x167] = 0x02; // reserved | |
289 AV_WB16(buf + 0x16a, ctx->m.mb_height * 4 + 4); // MSIPS | |
290 buf[0x16d] = ctx->m.mb_height; // Ns | |
291 buf[0x16f] = 0x10; // reserved | |
292 | |
293 ctx->msip = buf + 0x170; | |
294 return 0; | |
295 } | |
296 | |
297 static av_always_inline void dnxhd_encode_dc(DNXHDEncContext *ctx, int diff) | |
298 { | |
299 int nbits; | |
300 if (diff < 0) { | |
301 nbits = av_log2_16bit(-2*diff); | |
302 diff--; | |
303 } else { | |
304 nbits = av_log2_16bit(2*diff); | |
305 } | |
306 put_bits(&ctx->m.pb, ctx->cid_table->dc_bits[nbits] + nbits, | |
307 (ctx->cid_table->dc_codes[nbits]<<nbits) + (diff & ((1 << nbits) - 1))); | |
308 } | |
309 | |
310 static av_always_inline void dnxhd_encode_block(DNXHDEncContext *ctx, DCTELEM *block, int last_index, int n) | |
311 { | |
312 int last_non_zero = 0; | |
313 int slevel, i, j; | |
314 | |
315 dnxhd_encode_dc(ctx, block[0] - ctx->m.last_dc[n]); | |
316 ctx->m.last_dc[n] = block[0]; | |
317 | |
318 for (i = 1; i <= last_index; i++) { | |
319 j = ctx->m.intra_scantable.permutated[i]; | |
320 slevel = block[j]; | |
321 if (slevel) { | |
322 int run_level = i - last_non_zero - 1; | |
6978 | 323 int rlevel = (slevel<<1)|!!run_level; |
6981 | 324 put_bits(&ctx->m.pb, ctx->vlc_bits[rlevel], ctx->vlc_codes[rlevel]); |
5790 | 325 if (run_level) |
6981 | 326 put_bits(&ctx->m.pb, ctx->run_bits[run_level], ctx->run_codes[run_level]); |
5790 | 327 last_non_zero = i; |
328 } | |
329 } | |
6981 | 330 put_bits(&ctx->m.pb, ctx->vlc_bits[0], ctx->vlc_codes[0]); // EOB |
5790 | 331 } |
332 | |
333 static av_always_inline void dnxhd_unquantize_c(DNXHDEncContext *ctx, DCTELEM *block, int n, int qscale, int last_index) | |
334 { | |
5795 | 335 const uint8_t *weight_matrix; |
5790 | 336 int level; |
337 int i; | |
338 | |
5795 | 339 weight_matrix = (n&2) ? ctx->cid_table->chroma_weight : ctx->cid_table->luma_weight; |
5790 | 340 |
341 for (i = 1; i <= last_index; i++) { | |
342 int j = ctx->m.intra_scantable.permutated[i]; | |
343 level = block[j]; | |
344 if (level) { | |
345 if (level < 0) { | |
5795 | 346 level = (1-2*level) * qscale * weight_matrix[i]; |
347 if (weight_matrix[i] != 32) | |
5790 | 348 level += 32; |
349 level >>= 6; | |
350 level = -level; | |
351 } else { | |
5795 | 352 level = (2*level+1) * qscale * weight_matrix[i]; |
353 if (weight_matrix[i] != 32) | |
5790 | 354 level += 32; |
355 level >>= 6; | |
356 } | |
357 block[j] = level; | |
358 } | |
359 } | |
360 } | |
361 | |
362 static av_always_inline int dnxhd_ssd_block(DCTELEM *qblock, DCTELEM *block) | |
363 { | |
364 int score = 0; | |
365 int i; | |
366 for (i = 0; i < 64; i++) | |
367 score += (block[i]-qblock[i])*(block[i]-qblock[i]); | |
368 return score; | |
369 } | |
370 | |
371 static av_always_inline int dnxhd_calc_ac_bits(DNXHDEncContext *ctx, DCTELEM *block, int last_index) | |
372 { | |
373 int last_non_zero = 0; | |
374 int bits = 0; | |
375 int i, j, level; | |
376 for (i = 1; i <= last_index; i++) { | |
377 j = ctx->m.intra_scantable.permutated[i]; | |
378 level = block[j]; | |
379 if (level) { | |
380 int run_level = i - last_non_zero - 1; | |
6981 | 381 bits += ctx->vlc_bits[(level<<1)|!!run_level]+ctx->run_bits[run_level]; |
5790 | 382 last_non_zero = i; |
383 } | |
384 } | |
385 return bits; | |
386 } | |
387 | |
388 static av_always_inline void dnxhd_get_pixels_4x8(DCTELEM *restrict block, const uint8_t *pixels, int line_size) | |
389 { | |
390 int i; | |
391 for (i = 0; i < 4; i++) { | |
392 block[0] = pixels[0]; | |
393 block[1] = pixels[1]; | |
394 block[2] = pixels[2]; | |
395 block[3] = pixels[3]; | |
396 block[4] = pixels[4]; | |
397 block[5] = pixels[5]; | |
398 block[6] = pixels[6]; | |
399 block[7] = pixels[7]; | |
400 pixels += line_size; | |
401 block += 8; | |
402 } | |
403 memcpy(block , block- 8, sizeof(*block)*8); | |
404 memcpy(block+ 8, block-16, sizeof(*block)*8); | |
405 memcpy(block+16, block-24, sizeof(*block)*8); | |
406 memcpy(block+24, block-32, sizeof(*block)*8); | |
407 } | |
408 | |
409 static av_always_inline void dnxhd_get_blocks(DNXHDEncContext *ctx, int mb_x, int mb_y) | |
410 { | |
411 const uint8_t *ptr_y = ctx->thread[0]->src[0] + ((mb_y << 4) * ctx->m.linesize) + (mb_x << 4); | |
412 const uint8_t *ptr_u = ctx->thread[0]->src[1] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3); | |
413 const uint8_t *ptr_v = ctx->thread[0]->src[2] + ((mb_y << 4) * ctx->m.uvlinesize) + (mb_x << 3); | |
414 DSPContext *dsp = &ctx->m.dsp; | |
415 | |
416 dsp->get_pixels(ctx->blocks[0], ptr_y , ctx->m.linesize); | |
417 dsp->get_pixels(ctx->blocks[1], ptr_y + 8, ctx->m.linesize); | |
418 dsp->get_pixels(ctx->blocks[2], ptr_u , ctx->m.uvlinesize); | |
419 dsp->get_pixels(ctx->blocks[3], ptr_v , ctx->m.uvlinesize); | |
420 | |
5971 | 421 if (mb_y+1 == ctx->m.mb_height && ctx->m.avctx->height == 1080) { |
5790 | 422 if (ctx->interlaced) { |
423 dnxhd_get_pixels_4x8(ctx->blocks[4], ptr_y + ctx->dct_y_offset , ctx->m.linesize); | |
424 dnxhd_get_pixels_4x8(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize); | |
425 dnxhd_get_pixels_4x8(ctx->blocks[6], ptr_u + ctx->dct_uv_offset , ctx->m.uvlinesize); | |
426 dnxhd_get_pixels_4x8(ctx->blocks[7], ptr_v + ctx->dct_uv_offset , ctx->m.uvlinesize); | |
427 } else | |
428 memset(ctx->blocks[4], 0, 4*64*sizeof(DCTELEM)); | |
429 } else { | |
430 dsp->get_pixels(ctx->blocks[4], ptr_y + ctx->dct_y_offset , ctx->m.linesize); | |
431 dsp->get_pixels(ctx->blocks[5], ptr_y + ctx->dct_y_offset + 8, ctx->m.linesize); | |
432 dsp->get_pixels(ctx->blocks[6], ptr_u + ctx->dct_uv_offset , ctx->m.uvlinesize); | |
433 dsp->get_pixels(ctx->blocks[7], ptr_v + ctx->dct_uv_offset , ctx->m.uvlinesize); | |
434 } | |
435 } | |
436 | |
437 static av_always_inline int dnxhd_switch_matrix(DNXHDEncContext *ctx, int i) | |
438 { | |
439 if (i&2) { | |
440 ctx->m.q_intra_matrix16 = ctx->qmatrix_c16; | |
441 ctx->m.q_intra_matrix = ctx->qmatrix_c; | |
442 return 1 + (i&1); | |
443 } else { | |
444 ctx->m.q_intra_matrix16 = ctx->qmatrix_l16; | |
445 ctx->m.q_intra_matrix = ctx->qmatrix_l; | |
446 return 0; | |
447 } | |
448 } | |
449 | |
450 static int dnxhd_calc_bits_thread(AVCodecContext *avctx, void *arg) | |
451 { | |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
452 DNXHDEncContext *ctx = *(void**)arg; |
5790 | 453 int mb_y, mb_x; |
454 int qscale = ctx->thread[0]->qscale; | |
455 | |
456 for (mb_y = ctx->m.start_mb_y; mb_y < ctx->m.end_mb_y; mb_y++) { | |
457 ctx->m.last_dc[0] = | |
458 ctx->m.last_dc[1] = | |
459 ctx->m.last_dc[2] = 1024; | |
460 | |
461 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) { | |
462 unsigned mb = mb_y * ctx->m.mb_width + mb_x; | |
463 int ssd = 0; | |
464 int ac_bits = 0; | |
465 int dc_bits = 0; | |
466 int i; | |
467 | |
468 dnxhd_get_blocks(ctx, mb_x, mb_y); | |
469 | |
470 for (i = 0; i < 8; i++) { | |
471 DECLARE_ALIGNED_16(DCTELEM, block[64]); | |
472 DCTELEM *src_block = ctx->blocks[i]; | |
473 int overflow, nbits, diff, last_index; | |
474 int n = dnxhd_switch_matrix(ctx, i); | |
475 | |
476 memcpy(block, src_block, sizeof(block)); | |
477 last_index = ctx->m.dct_quantize((MpegEncContext*)ctx, block, i, qscale, &overflow); | |
478 ac_bits += dnxhd_calc_ac_bits(ctx, block, last_index); | |
479 | |
480 diff = block[0] - ctx->m.last_dc[n]; | |
481 if (diff < 0) nbits = av_log2_16bit(-2*diff); | |
482 else nbits = av_log2_16bit( 2*diff); | |
483 dc_bits += ctx->cid_table->dc_bits[nbits] + nbits; | |
484 | |
485 ctx->m.last_dc[n] = block[0]; | |
486 | |
487 if (avctx->mb_decision == FF_MB_DECISION_RD || !RC_VARIANCE) { | |
488 dnxhd_unquantize_c(ctx, block, i, qscale, last_index); | |
489 ctx->m.dsp.idct(block); | |
490 ssd += dnxhd_ssd_block(block, src_block); | |
491 } | |
492 } | |
493 ctx->mb_rc[qscale][mb].ssd = ssd; | |
6981 | 494 ctx->mb_rc[qscale][mb].bits = ac_bits+dc_bits+12+8*ctx->vlc_bits[0]; |
5790 | 495 } |
496 } | |
497 return 0; | |
498 } | |
499 | |
500 static int dnxhd_encode_thread(AVCodecContext *avctx, void *arg) | |
501 { | |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
502 DNXHDEncContext *ctx = *(void**)arg; |
5790 | 503 int mb_y, mb_x; |
504 | |
505 for (mb_y = ctx->m.start_mb_y; mb_y < ctx->m.end_mb_y; mb_y++) { | |
506 ctx->m.last_dc[0] = | |
507 ctx->m.last_dc[1] = | |
508 ctx->m.last_dc[2] = 1024; | |
509 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) { | |
510 unsigned mb = mb_y * ctx->m.mb_width + mb_x; | |
511 int qscale = ctx->mb_qscale[mb]; | |
512 int i; | |
513 | |
514 put_bits(&ctx->m.pb, 12, qscale<<1); | |
515 | |
516 dnxhd_get_blocks(ctx, mb_x, mb_y); | |
517 | |
518 for (i = 0; i < 8; i++) { | |
519 DCTELEM *block = ctx->blocks[i]; | |
520 int last_index, overflow; | |
521 int n = dnxhd_switch_matrix(ctx, i); | |
522 last_index = ctx->m.dct_quantize((MpegEncContext*)ctx, block, i, qscale, &overflow); | |
6978 | 523 //START_TIMER; |
5790 | 524 dnxhd_encode_block(ctx, block, last_index, n); |
6978 | 525 //STOP_TIMER("encode_block"); |
5790 | 526 } |
527 } | |
528 if (put_bits_count(&ctx->m.pb)&31) | |
529 put_bits(&ctx->m.pb, 32-(put_bits_count(&ctx->m.pb)&31), 0); | |
530 } | |
531 flush_put_bits(&ctx->m.pb); | |
532 return 0; | |
533 } | |
534 | |
535 static void dnxhd_setup_threads_slices(DNXHDEncContext *ctx, uint8_t *buf) | |
536 { | |
537 int mb_y, mb_x; | |
538 int i, offset = 0; | |
539 for (i = 0; i < ctx->m.avctx->thread_count; i++) { | |
540 int thread_size = 0; | |
541 for (mb_y = ctx->thread[i]->m.start_mb_y; mb_y < ctx->thread[i]->m.end_mb_y; mb_y++) { | |
542 ctx->slice_size[mb_y] = 0; | |
543 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) { | |
544 unsigned mb = mb_y * ctx->m.mb_width + mb_x; | |
545 ctx->slice_size[mb_y] += ctx->mb_bits[mb]; | |
546 } | |
547 ctx->slice_size[mb_y] = (ctx->slice_size[mb_y]+31)&~31; | |
548 ctx->slice_size[mb_y] >>= 3; | |
549 thread_size += ctx->slice_size[mb_y]; | |
550 } | |
551 init_put_bits(&ctx->thread[i]->m.pb, buf + 640 + offset, thread_size); | |
552 offset += thread_size; | |
553 } | |
554 } | |
555 | |
556 static int dnxhd_mb_var_thread(AVCodecContext *avctx, void *arg) | |
557 { | |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
558 DNXHDEncContext *ctx = *(void**)arg; |
5790 | 559 int mb_y, mb_x; |
560 for (mb_y = ctx->m.start_mb_y; mb_y < ctx->m.end_mb_y; mb_y++) { | |
561 for (mb_x = 0; mb_x < ctx->m.mb_width; mb_x++) { | |
562 unsigned mb = mb_y * ctx->m.mb_width + mb_x; | |
563 uint8_t *pix = ctx->thread[0]->src[0] + ((mb_y<<4) * ctx->m.linesize) + (mb_x<<4); | |
564 int sum = ctx->m.dsp.pix_sum(pix, ctx->m.linesize); | |
565 int varc = (ctx->m.dsp.pix_norm1(pix, ctx->m.linesize) - (((unsigned)(sum*sum))>>8)+128)>>8; | |
566 ctx->mb_cmp[mb].value = varc; | |
567 ctx->mb_cmp[mb].mb = mb; | |
568 } | |
569 } | |
570 return 0; | |
571 } | |
572 | |
573 static int dnxhd_encode_rdo(AVCodecContext *avctx, DNXHDEncContext *ctx) | |
574 { | |
5802 | 575 int lambda, up_step, down_step; |
576 int last_lower = INT_MAX, last_higher = 0; | |
5790 | 577 int x, y, q; |
578 | |
579 for (q = 1; q < avctx->qmax; q++) { | |
580 ctx->qscale = q; | |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
581 avctx->execute(avctx, dnxhd_calc_bits_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count, sizeof(void*)); |
5790 | 582 } |
5802 | 583 up_step = down_step = 2<<LAMBDA_FRAC_BITS; |
5790 | 584 lambda = ctx->lambda; |
585 | |
586 for (;;) { | |
587 int bits = 0; | |
588 int end = 0; | |
5802 | 589 if (lambda == last_higher) { |
590 lambda++; | |
5790 | 591 end = 1; // need to set final qscales/bits |
592 } | |
593 for (y = 0; y < ctx->m.mb_height; y++) { | |
594 for (x = 0; x < ctx->m.mb_width; x++) { | |
595 unsigned min = UINT_MAX; | |
596 int qscale = 1; | |
597 int mb = y*ctx->m.mb_width+x; | |
598 for (q = 1; q < avctx->qmax; q++) { | |
599 unsigned score = ctx->mb_rc[q][mb].bits*lambda+(ctx->mb_rc[q][mb].ssd<<LAMBDA_FRAC_BITS); | |
600 if (score < min) { | |
601 min = score; | |
602 qscale = q; | |
603 } | |
604 } | |
605 bits += ctx->mb_rc[qscale][mb].bits; | |
606 ctx->mb_qscale[mb] = qscale; | |
607 ctx->mb_bits[mb] = ctx->mb_rc[qscale][mb].bits; | |
608 } | |
609 bits = (bits+31)&~31; // padding | |
610 if (bits > ctx->frame_bits) | |
611 break; | |
612 } | |
5802 | 613 //dprintf(ctx->m.avctx, "lambda %d, up %u, down %u, bits %d, frame %d\n", |
614 // lambda, last_higher, last_lower, bits, ctx->frame_bits); | |
5790 | 615 if (end) { |
616 if (bits > ctx->frame_bits) | |
617 return -1; | |
618 break; | |
619 } | |
620 if (bits < ctx->frame_bits) { | |
5802 | 621 last_lower = FFMIN(lambda, last_lower); |
622 if (last_higher != 0) | |
623 lambda = (lambda+last_higher)>>1; | |
624 else | |
625 lambda -= down_step; | |
626 down_step *= 5; // XXX tune ? | |
627 up_step = 1<<LAMBDA_FRAC_BITS; | |
628 lambda = FFMAX(1, lambda); | |
629 if (lambda == last_lower) | |
630 break; | |
5790 | 631 } else { |
5802 | 632 last_higher = FFMAX(lambda, last_higher); |
633 if (last_lower != INT_MAX) | |
634 lambda = (lambda+last_lower)>>1; | |
635 else | |
636 lambda += up_step; | |
637 up_step *= 5; | |
638 down_step = 1<<LAMBDA_FRAC_BITS; | |
5790 | 639 } |
640 } | |
5802 | 641 //dprintf(ctx->m.avctx, "out lambda %d\n", lambda); |
5790 | 642 ctx->lambda = lambda; |
643 return 0; | |
644 } | |
645 | |
646 static int dnxhd_find_qscale(DNXHDEncContext *ctx) | |
647 { | |
648 int bits = 0; | |
649 int up_step = 1; | |
650 int down_step = 1; | |
651 int last_higher = 0; | |
652 int last_lower = INT_MAX; | |
653 int qscale; | |
654 int x, y; | |
655 | |
656 qscale = ctx->qscale; | |
657 for (;;) { | |
658 bits = 0; | |
659 ctx->qscale = qscale; | |
660 // XXX avoid recalculating bits | |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
661 ctx->m.avctx->execute(ctx->m.avctx, dnxhd_calc_bits_thread, (void**)&ctx->thread[0], NULL, ctx->m.avctx->thread_count, sizeof(void*)); |
5790 | 662 for (y = 0; y < ctx->m.mb_height; y++) { |
663 for (x = 0; x < ctx->m.mb_width; x++) | |
664 bits += ctx->mb_rc[qscale][y*ctx->m.mb_width+x].bits; | |
665 bits = (bits+31)&~31; // padding | |
666 if (bits > ctx->frame_bits) | |
667 break; | |
668 } | |
669 //dprintf(ctx->m.avctx, "%d, qscale %d, bits %d, frame %d, higher %d, lower %d\n", | |
670 // ctx->m.avctx->frame_number, qscale, bits, ctx->frame_bits, last_higher, last_lower); | |
671 if (bits < ctx->frame_bits) { | |
672 if (qscale == 1) | |
5969
c5d11f6f6a3d
fix corner case when qscale 1 bits < frame bits but max bits with worst padding > frame bits
bcoudurier
parents:
5802
diff
changeset
|
673 return 1; |
5790 | 674 if (last_higher == qscale - 1) { |
675 qscale = last_higher; | |
676 break; | |
677 } | |
678 last_lower = FFMIN(qscale, last_lower); | |
679 if (last_higher != 0) | |
680 qscale = (qscale+last_higher)>>1; | |
681 else | |
682 qscale -= down_step++; | |
683 if (qscale < 1) | |
684 qscale = 1; | |
685 up_step = 1; | |
686 } else { | |
687 if (last_lower == qscale + 1) | |
688 break; | |
689 last_higher = FFMAX(qscale, last_higher); | |
690 if (last_lower != INT_MAX) | |
691 qscale = (qscale+last_lower)>>1; | |
692 else | |
693 qscale += up_step++; | |
694 down_step = 1; | |
695 if (qscale >= ctx->m.avctx->qmax) | |
696 return -1; | |
697 } | |
698 } | |
699 //dprintf(ctx->m.avctx, "out qscale %d\n", qscale); | |
700 ctx->qscale = qscale; | |
701 return 0; | |
702 } | |
703 | |
704 static int dnxhd_rc_cmp(const void *a, const void *b) | |
705 { | |
6218 | 706 return ((const RCCMPEntry *)b)->value - ((const RCCMPEntry *)a)->value; |
5790 | 707 } |
708 | |
5970 | 709 static int dnxhd_encode_fast(AVCodecContext *avctx, DNXHDEncContext *ctx) |
5790 | 710 { |
711 int max_bits = 0; | |
5969
c5d11f6f6a3d
fix corner case when qscale 1 bits < frame bits but max bits with worst padding > frame bits
bcoudurier
parents:
5802
diff
changeset
|
712 int ret, x, y; |
c5d11f6f6a3d
fix corner case when qscale 1 bits < frame bits but max bits with worst padding > frame bits
bcoudurier
parents:
5802
diff
changeset
|
713 if ((ret = dnxhd_find_qscale(ctx)) < 0) |
5790 | 714 return -1; |
715 for (y = 0; y < ctx->m.mb_height; y++) { | |
716 for (x = 0; x < ctx->m.mb_width; x++) { | |
717 int mb = y*ctx->m.mb_width+x; | |
718 int delta_bits; | |
719 ctx->mb_qscale[mb] = ctx->qscale; | |
720 ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale][mb].bits; | |
721 max_bits += ctx->mb_rc[ctx->qscale][mb].bits; | |
722 if (!RC_VARIANCE) { | |
723 delta_bits = ctx->mb_rc[ctx->qscale][mb].bits-ctx->mb_rc[ctx->qscale+1][mb].bits; | |
724 ctx->mb_cmp[mb].mb = mb; | |
725 ctx->mb_cmp[mb].value = delta_bits ? | |
726 ((ctx->mb_rc[ctx->qscale][mb].ssd-ctx->mb_rc[ctx->qscale+1][mb].ssd)*100)/delta_bits | |
727 : INT_MIN; //avoid increasing qscale | |
728 } | |
729 } | |
730 max_bits += 31; //worst padding | |
731 } | |
5969
c5d11f6f6a3d
fix corner case when qscale 1 bits < frame bits but max bits with worst padding > frame bits
bcoudurier
parents:
5802
diff
changeset
|
732 if (!ret) { |
5790 | 733 if (RC_VARIANCE) |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
734 avctx->execute(avctx, dnxhd_mb_var_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count, sizeof(void*)); |
5790 | 735 qsort(ctx->mb_cmp, ctx->m.mb_num, sizeof(RCEntry), dnxhd_rc_cmp); |
736 for (x = 0; x < ctx->m.mb_num && max_bits > ctx->frame_bits; x++) { | |
737 int mb = ctx->mb_cmp[x].mb; | |
738 max_bits -= ctx->mb_rc[ctx->qscale][mb].bits - ctx->mb_rc[ctx->qscale+1][mb].bits; | |
739 ctx->mb_qscale[mb] = ctx->qscale+1; | |
740 ctx->mb_bits[mb] = ctx->mb_rc[ctx->qscale+1][mb].bits; | |
741 } | |
742 } | |
743 return 0; | |
744 } | |
745 | |
6218 | 746 static void dnxhd_load_picture(DNXHDEncContext *ctx, const AVFrame *frame) |
5790 | 747 { |
748 int i; | |
749 | |
750 for (i = 0; i < 3; i++) { | |
751 ctx->frame.data[i] = frame->data[i]; | |
752 ctx->frame.linesize[i] = frame->linesize[i]; | |
753 } | |
754 | |
755 for (i = 0; i < ctx->m.avctx->thread_count; i++) { | |
756 ctx->thread[i]->m.linesize = ctx->frame.linesize[0]<<ctx->interlaced; | |
757 ctx->thread[i]->m.uvlinesize = ctx->frame.linesize[1]<<ctx->interlaced; | |
758 ctx->thread[i]->dct_y_offset = ctx->m.linesize *8; | |
759 ctx->thread[i]->dct_uv_offset = ctx->m.uvlinesize*8; | |
760 } | |
761 | |
762 ctx->frame.interlaced_frame = frame->interlaced_frame; | |
763 ctx->cur_field = frame->interlaced_frame && !frame->top_field_first; | |
764 } | |
765 | |
6218 | 766 static int dnxhd_encode_picture(AVCodecContext *avctx, unsigned char *buf, int buf_size, const void *data) |
5790 | 767 { |
768 DNXHDEncContext *ctx = avctx->priv_data; | |
769 int first_field = 1; | |
770 int offset, i, ret; | |
771 | |
772 if (buf_size < ctx->cid_table->frame_size) { | |
773 av_log(avctx, AV_LOG_ERROR, "output buffer is too small to compress picture\n"); | |
774 return -1; | |
775 } | |
776 | |
777 dnxhd_load_picture(ctx, data); | |
778 | |
779 encode_coding_unit: | |
780 for (i = 0; i < 3; i++) { | |
781 ctx->src[i] = ctx->frame.data[i]; | |
782 if (ctx->interlaced && ctx->cur_field) | |
783 ctx->src[i] += ctx->frame.linesize[i]; | |
784 } | |
785 | |
786 dnxhd_write_header(avctx, buf); | |
787 | |
788 if (avctx->mb_decision == FF_MB_DECISION_RD) | |
789 ret = dnxhd_encode_rdo(avctx, ctx); | |
790 else | |
5970 | 791 ret = dnxhd_encode_fast(avctx, ctx); |
5790 | 792 if (ret < 0) { |
793 av_log(avctx, AV_LOG_ERROR, "picture could not fit ratecontrol constraints\n"); | |
794 return -1; | |
795 } | |
796 | |
797 dnxhd_setup_threads_slices(ctx, buf); | |
798 | |
799 offset = 0; | |
800 for (i = 0; i < ctx->m.mb_height; i++) { | |
801 AV_WB32(ctx->msip + i * 4, offset); | |
802 offset += ctx->slice_size[i]; | |
803 assert(!(ctx->slice_size[i] & 3)); | |
804 } | |
805 | |
8129
a9734fe0811e
Making it easier to send arbitrary structures as work orders to MT workers
romansh
parents:
7040
diff
changeset
|
806 avctx->execute(avctx, dnxhd_encode_thread, (void**)&ctx->thread[0], NULL, avctx->thread_count, sizeof(void*)); |
5790 | 807 |
808 AV_WB32(buf + ctx->cid_table->coding_unit_size - 4, 0x600DC0DE); // EOF | |
809 | |
810 if (ctx->interlaced && first_field) { | |
811 first_field = 0; | |
812 ctx->cur_field ^= 1; | |
813 buf += ctx->cid_table->coding_unit_size; | |
814 buf_size -= ctx->cid_table->coding_unit_size; | |
815 goto encode_coding_unit; | |
816 } | |
817 | |
6752 | 818 ctx->frame.quality = ctx->qscale*FF_QP2LAMBDA; |
819 | |
5790 | 820 return ctx->cid_table->frame_size; |
821 } | |
822 | |
823 static int dnxhd_encode_end(AVCodecContext *avctx) | |
824 { | |
825 DNXHDEncContext *ctx = avctx->priv_data; | |
6978 | 826 int max_level = 1<<(ctx->cid_table->bit_depth+2); |
5790 | 827 int i; |
828 | |
6981 | 829 av_free(ctx->vlc_codes-max_level*2); |
830 av_free(ctx->vlc_bits -max_level*2); | |
831 av_freep(&ctx->run_codes); | |
832 av_freep(&ctx->run_bits); | |
5790 | 833 |
834 av_freep(&ctx->mb_bits); | |
835 av_freep(&ctx->mb_qscale); | |
836 av_freep(&ctx->mb_rc); | |
837 av_freep(&ctx->mb_cmp); | |
838 av_freep(&ctx->slice_size); | |
839 | |
840 av_freep(&ctx->qmatrix_c); | |
841 av_freep(&ctx->qmatrix_l); | |
842 av_freep(&ctx->qmatrix_c16); | |
843 av_freep(&ctx->qmatrix_l16); | |
844 | |
845 for (i = 1; i < avctx->thread_count; i++) | |
846 av_freep(&ctx->thread[i]); | |
847 | |
848 return 0; | |
849 } | |
850 | |
851 AVCodec dnxhd_encoder = { | |
852 "dnxhd", | |
853 CODEC_TYPE_VIDEO, | |
854 CODEC_ID_DNXHD, | |
855 sizeof(DNXHDEncContext), | |
856 dnxhd_encode_init, | |
857 dnxhd_encode_picture, | |
858 dnxhd_encode_end, | |
6792
13191e6b439c
Replace one more occurrence of -1 with PIX_FMT_NONE.
cehoyos
parents:
6752
diff
changeset
|
859 .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_NONE}, |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6981
diff
changeset
|
860 .long_name = NULL_IF_CONFIG_SMALL("VC3/DNxHD"), |
5790 | 861 }; |