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