00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00041 #include "libavutil/pixdesc.h"
00042 #include "avcodec.h"
00043 #include "dsputil.h"
00044 #include "get_bits.h"
00045 #include "internal.h"
00046 #include "put_bits.h"
00047 #include "simple_idct.h"
00048 #include "dvdata.h"
00049 #include "dv_tablegen.h"
00050
00051
00052 RL_VLC_ELEM ff_dv_rl_vlc[1184];
00053
00054 static inline void dv_calc_mb_coordinates(const DVprofile *d, int chan, int seq, int slot,
00055 uint16_t *tbl)
00056 {
00057 static const uint8_t off[] = { 2, 6, 8, 0, 4 };
00058 static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };
00059 static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };
00060 static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };
00061
00062 static const uint8_t l_start[] = {0, 4, 9, 13, 18, 22, 27, 31, 36, 40};
00063 static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };
00064
00065 static const uint8_t serpent1[] = {0, 1, 2, 2, 1, 0,
00066 0, 1, 2, 2, 1, 0,
00067 0, 1, 2, 2, 1, 0,
00068 0, 1, 2, 2, 1, 0,
00069 0, 1, 2};
00070 static const uint8_t serpent2[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
00071 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
00072 0, 1, 2, 3, 4, 5};
00073
00074 static const uint8_t remap[][2] = {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0},
00075 { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3}, {10, 0},
00076 {10, 1}, {10, 2}, {10, 3}, {20, 0}, {20, 1},
00077 {20, 2}, {20, 3}, {30, 0}, {30, 1}, {30, 2},
00078 {30, 3}, {40, 0}, {40, 1}, {40, 2}, {40, 3},
00079 {50, 0}, {50, 1}, {50, 2}, {50, 3}, {60, 0},
00080 {60, 1}, {60, 2}, {60, 3}, {70, 0}, {70, 1},
00081 {70, 2}, {70, 3}, { 0,64}, { 0,65}, { 0,66},
00082 {10,64}, {10,65}, {10,66}, {20,64}, {20,65},
00083 {20,66}, {30,64}, {30,65}, {30,66}, {40,64},
00084 {40,65}, {40,66}, {50,64}, {50,65}, {50,66},
00085 {60,64}, {60,65}, {60,66}, {70,64}, {70,65},
00086 {70,66}, { 0,67}, {20,67}, {40,67}, {60,67}};
00087
00088 int i, k, m;
00089 int x, y, blk;
00090
00091 for (m=0; m<5; m++) {
00092 switch (d->width) {
00093 case 1440:
00094 blk = (chan*11+seq)*27+slot;
00095
00096 if (chan == 0 && seq == 11) {
00097 x = m*27+slot;
00098 if (x<90) {
00099 y = 0;
00100 } else {
00101 x = (x - 90)*2;
00102 y = 67;
00103 }
00104 } else {
00105 i = (4*chan + blk + off[m])%11;
00106 k = (blk/11)%27;
00107
00108 x = shuf1[m] + (chan&1)*9 + k%9;
00109 y = (i*3+k/9)*2 + (chan>>1) + 1;
00110 }
00111 tbl[m] = (x<<1)|(y<<9);
00112 break;
00113 case 1280:
00114 blk = (chan*10+seq)*27+slot;
00115
00116 i = (4*chan + (seq/5) + 2*blk + off[m])%10;
00117 k = (blk/5)%27;
00118
00119 x = shuf1[m]+(chan&1)*9 + k%9;
00120 y = (i*3+k/9)*2 + (chan>>1) + 4;
00121
00122 if (x >= 80) {
00123 x = remap[y][0]+((x-80)<<(y>59));
00124 y = remap[y][1];
00125 }
00126 tbl[m] = (x<<1)|(y<<9);
00127 break;
00128 case 960:
00129 blk = (chan*10+seq)*27+slot;
00130
00131 i = (4*chan + (seq/5) + 2*blk + off[m])%10;
00132 k = (blk/5)%27 + (i&1)*3;
00133
00134 x = shuf2[m] + k%6 + 6*(chan&1);
00135 y = l_start[i] + k/6 + 45*(chan>>1);
00136 tbl[m] = (x<<1)|(y<<9);
00137 break;
00138 case 720:
00139 switch (d->pix_fmt) {
00140 case PIX_FMT_YUV422P:
00141 x = shuf3[m] + slot/3;
00142 y = serpent1[slot] +
00143 ((((seq + off[m]) % d->difseg_size)<<1) + chan)*3;
00144 tbl[m] = (x<<1)|(y<<8);
00145 break;
00146 case PIX_FMT_YUV420P:
00147 x = shuf3[m] + slot/3;
00148 y = serpent1[slot] +
00149 ((seq + off[m]) % d->difseg_size)*3;
00150 tbl[m] = (x<<1)|(y<<9);
00151 break;
00152 case PIX_FMT_YUV411P:
00153 i = (seq + off[m]) % d->difseg_size;
00154 k = slot + ((m==1||m==2)?3:0);
00155
00156 x = l_start_shuffled[m] + k/6;
00157 y = serpent2[k] + i*6;
00158 if (x>21)
00159 y = y*2 - i*6;
00160 tbl[m] = (x<<2)|(y<<8);
00161 break;
00162 }
00163 default:
00164 break;
00165 }
00166 }
00167 }
00168
00169
00170 static const uint8_t dv100_qstep[16] = {
00171 1,
00172 1,
00173 2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
00174 };
00175
00176 static const uint8_t dv_quant_areas[4] = { 6, 21, 43, 64 };
00177
00178 int ff_dv_init_dynamic_tables(const DVprofile *d)
00179 {
00180 int j,i,c,s,p;
00181 uint32_t *factor1, *factor2;
00182 const int *iweight1, *iweight2;
00183
00184 if (!d->work_chunks[dv_work_pool_size(d)-1].buf_offset) {
00185 p = i = 0;
00186 for (c=0; c<d->n_difchan; c++) {
00187 for (s=0; s<d->difseg_size; s++) {
00188 p += 6;
00189 for (j=0; j<27; j++) {
00190 p += !(j%3);
00191 if (!(DV_PROFILE_IS_1080i50(d) && c != 0 && s == 11) &&
00192 !(DV_PROFILE_IS_720p50(d) && s > 9)) {
00193 dv_calc_mb_coordinates(d, c, s, j, &d->work_chunks[i].mb_coordinates[0]);
00194 d->work_chunks[i++].buf_offset = p;
00195 }
00196 p += 5;
00197 }
00198 }
00199 }
00200 }
00201
00202 if (!d->idct_factor[DV_PROFILE_IS_HD(d)?8191:5631]) {
00203 factor1 = &d->idct_factor[0];
00204 factor2 = &d->idct_factor[DV_PROFILE_IS_HD(d)?4096:2816];
00205 if (d->height == 720) {
00206 iweight1 = &ff_dv_iweight_720_y[0];
00207 iweight2 = &ff_dv_iweight_720_c[0];
00208 } else {
00209 iweight1 = &ff_dv_iweight_1080_y[0];
00210 iweight2 = &ff_dv_iweight_1080_c[0];
00211 }
00212 if (DV_PROFILE_IS_HD(d)) {
00213 for (c = 0; c < 4; c++) {
00214 for (s = 0; s < 16; s++) {
00215 for (i = 0; i < 64; i++) {
00216 *factor1++ = (dv100_qstep[s] << (c + 9)) * iweight1[i];
00217 *factor2++ = (dv100_qstep[s] << (c + 9)) * iweight2[i];
00218 }
00219 }
00220 }
00221 } else {
00222 iweight1 = &ff_dv_iweight_88[0];
00223 for (j = 0; j < 2; j++, iweight1 = &ff_dv_iweight_248[0]) {
00224 for (s = 0; s < 22; s++) {
00225 for (i = c = 0; c < 4; c++) {
00226 for (; i < dv_quant_areas[c]; i++) {
00227 *factor1 = iweight1[i] << (ff_dv_quant_shifts[s][c] + 1);
00228 *factor2++ = (*factor1++) << 1;
00229 }
00230 }
00231 }
00232 }
00233 }
00234 }
00235
00236 return 0;
00237 }
00238
00239 av_cold int ff_dvvideo_init(AVCodecContext *avctx)
00240 {
00241 DVVideoContext *s = avctx->priv_data;
00242 DSPContext dsp;
00243 static int done = 0;
00244 int i, j;
00245
00246 if (!done) {
00247 VLC dv_vlc;
00248 uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
00249 uint8_t new_dv_vlc_len[NB_DV_VLC*2];
00250 uint8_t new_dv_vlc_run[NB_DV_VLC*2];
00251 int16_t new_dv_vlc_level[NB_DV_VLC*2];
00252
00253 done = 1;
00254
00255
00256 for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) {
00257 new_dv_vlc_bits[j] = dv_vlc_bits[i];
00258 new_dv_vlc_len[j] = dv_vlc_len[i];
00259 new_dv_vlc_run[j] = dv_vlc_run[i];
00260 new_dv_vlc_level[j] = dv_vlc_level[i];
00261
00262 if (dv_vlc_level[i]) {
00263 new_dv_vlc_bits[j] <<= 1;
00264 new_dv_vlc_len[j]++;
00265
00266 j++;
00267 new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
00268 new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
00269 new_dv_vlc_run[j] = dv_vlc_run[i];
00270 new_dv_vlc_level[j] = -dv_vlc_level[i];
00271 }
00272 }
00273
00274
00275
00276 init_vlc(&dv_vlc, TEX_VLC_BITS, j,
00277 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
00278 assert(dv_vlc.table_size == 1184);
00279
00280 for (i = 0; i < dv_vlc.table_size; i++){
00281 int code = dv_vlc.table[i][0];
00282 int len = dv_vlc.table[i][1];
00283 int level, run;
00284
00285 if (len < 0){
00286 run = 0;
00287 level = code;
00288 } else {
00289 run = new_dv_vlc_run [code] + 1;
00290 level = new_dv_vlc_level[code];
00291 }
00292 ff_dv_rl_vlc[i].len = len;
00293 ff_dv_rl_vlc[i].level = level;
00294 ff_dv_rl_vlc[i].run = run;
00295 }
00296 ff_free_vlc(&dv_vlc);
00297 }
00298
00299
00300 ff_dsputil_init(&dsp, avctx);
00301 ff_set_cmp(&dsp, dsp.ildct_cmp, avctx->ildct_cmp);
00302 s->get_pixels = dsp.get_pixels;
00303 s->ildct_cmp = dsp.ildct_cmp[5];
00304
00305
00306 s->fdct[0] = dsp.fdct;
00307 s->idct_put[0] = dsp.idct_put;
00308 for (i = 0; i < 64; i++)
00309 s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
00310
00311
00312 s->fdct[1] = dsp.fdct248;
00313 s->idct_put[1] = ff_simple_idct248_put;
00314 if (avctx->lowres){
00315 for (i = 0; i < 64; i++){
00316 int j = ff_zigzag248_direct[i];
00317 s->dv_zigzag[1][i] = dsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2];
00318 }
00319 }else
00320 memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
00321
00322 avctx->coded_frame = &s->picture;
00323 s->avctx = avctx;
00324 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
00325
00326 return 0;
00327 }
00328
00329 static av_cold int dvvideo_init_encoder(AVCodecContext *avctx)
00330 {
00331 if (!avpriv_dv_codec_profile(avctx)) {
00332 av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
00333 "Valid DV profiles are:\n",
00334 avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
00335 ff_dv_print_profiles(avctx, AV_LOG_ERROR);
00336 return AVERROR(EINVAL);
00337 }
00338
00339 dv_vlc_map_tableinit();
00340
00341 return ff_dvvideo_init(avctx);
00342 }
00343
00344
00345 static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
00346 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
00347
00348 static inline int put_bits_left(PutBitContext* s)
00349 {
00350 return (s->buf_end - s->buf) * 8 - put_bits_count(s);
00351 }
00352
00353 #if CONFIG_SMALL
00354
00355 static av_always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
00356 {
00357 int size;
00358 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
00359 *vlc = dv_vlc_map[run][level].vlc | sign;
00360 size = dv_vlc_map[run][level].size;
00361 }
00362 else {
00363 if (level < DV_VLC_MAP_LEV_SIZE) {
00364 *vlc = dv_vlc_map[0][level].vlc | sign;
00365 size = dv_vlc_map[0][level].size;
00366 } else {
00367 *vlc = 0xfe00 | (level << 1) | sign;
00368 size = 16;
00369 }
00370 if (run) {
00371 *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
00372 (0x1f80 | (run - 1))) << size;
00373 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
00374 }
00375 }
00376
00377 return size;
00378 }
00379
00380 static av_always_inline int dv_rl2vlc_size(int run, int level)
00381 {
00382 int size;
00383
00384 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
00385 size = dv_vlc_map[run][level].size;
00386 }
00387 else {
00388 size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
00389 if (run) {
00390 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
00391 }
00392 }
00393 return size;
00394 }
00395 #else
00396 static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
00397 {
00398 *vlc = dv_vlc_map[run][l].vlc | sign;
00399 return dv_vlc_map[run][l].size;
00400 }
00401
00402 static av_always_inline int dv_rl2vlc_size(int run, int l)
00403 {
00404 return dv_vlc_map[run][l].size;
00405 }
00406 #endif
00407
00408 typedef struct EncBlockInfo {
00409 int area_q[4];
00410 int bit_size[4];
00411 int prev[5];
00412 int cur_ac;
00413 int cno;
00414 int dct_mode;
00415 DCTELEM mb[64];
00416 uint8_t next[64];
00417 uint8_t sign[64];
00418 uint8_t partial_bit_count;
00419 uint32_t partial_bit_buffer;
00420 } EncBlockInfo;
00421
00422 static av_always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi,
00423 PutBitContext* pb_pool,
00424 PutBitContext* pb_end)
00425 {
00426 int prev, bits_left;
00427 PutBitContext* pb = pb_pool;
00428 int size = bi->partial_bit_count;
00429 uint32_t vlc = bi->partial_bit_buffer;
00430
00431 bi->partial_bit_count = bi->partial_bit_buffer = 0;
00432 for (;;){
00433
00434 for (; size > (bits_left = put_bits_left(pb)); pb++) {
00435 if (bits_left) {
00436 size -= bits_left;
00437 put_bits(pb, bits_left, vlc >> size);
00438 vlc = vlc & ((1 << size) - 1);
00439 }
00440 if (pb + 1 >= pb_end) {
00441 bi->partial_bit_count = size;
00442 bi->partial_bit_buffer = vlc;
00443 return pb;
00444 }
00445 }
00446
00447
00448 put_bits(pb, size, vlc);
00449
00450 if (bi->cur_ac >= 64)
00451 break;
00452
00453
00454 prev = bi->cur_ac;
00455 bi->cur_ac = bi->next[prev];
00456 if (bi->cur_ac < 64){
00457 size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
00458 } else {
00459 size = 4; vlc = 6;
00460 }
00461 }
00462 return pb;
00463 }
00464
00465 static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data, int linesize) {
00466 if (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
00467 int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
00468 if (ps > 0) {
00469 int is = s->ildct_cmp(NULL, data , NULL, linesize<<1, 4) +
00470 s->ildct_cmp(NULL, data + linesize, NULL, linesize<<1, 4);
00471 return ps > is;
00472 }
00473 }
00474
00475 return 0;
00476 }
00477
00478 static const int dv_weight_bits = 18;
00479 static const int dv_weight_88[64] = {
00480 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
00481 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
00482 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
00483 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
00484 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
00485 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
00486 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
00487 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
00488 };
00489 static const int dv_weight_248[64] = {
00490 131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
00491 224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
00492 211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
00493 242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
00494 200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
00495 229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
00496 175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
00497 195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
00498 };
00499
00500 static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int bias)
00501 {
00502 const int *weight;
00503 const uint8_t* zigzag_scan;
00504 LOCAL_ALIGNED_16(DCTELEM, blk, [64]);
00505 int i, area;
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 #if 0
00517 static const int classes[] = {12, 24, 36, 0xffff};
00518 #else
00519 static const int classes[] = {-1, -1, 255, 0xffff};
00520 #endif
00521 int max = classes[0];
00522 int prev = 0;
00523
00524 assert((((int)blk) & 15) == 0);
00525
00526 bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
00527 bi->partial_bit_count = 0;
00528 bi->partial_bit_buffer = 0;
00529 bi->cur_ac = 0;
00530 if (data) {
00531 bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
00532 s->get_pixels(blk, data, linesize);
00533 s->fdct[bi->dct_mode](blk);
00534 } else {
00535
00536
00537 memset(blk, 0, 64*sizeof(*blk));
00538 bi->dct_mode = 0;
00539 }
00540 bi->mb[0] = blk[0];
00541
00542 zigzag_scan = bi->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct;
00543 weight = bi->dct_mode ? dv_weight_248 : dv_weight_88;
00544
00545 for (area = 0; area < 4; area++) {
00546 bi->prev[area] = prev;
00547 bi->bit_size[area] = 1;
00548 for (i = mb_area_start[area]; i < mb_area_start[area+1]; i++) {
00549 int level = blk[zigzag_scan[i]];
00550
00551 if (level + 15 > 30U) {
00552 bi->sign[i] = (level >> 31) & 1;
00553
00554
00555
00556 level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits+3))) >> (dv_weight_bits+4);
00557 bi->mb[i] = level;
00558 if (level > max)
00559 max = level;
00560 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
00561 bi->next[prev]= i;
00562 prev = i;
00563 }
00564 }
00565 }
00566 bi->next[prev]= i;
00567 for (bi->cno = 0; max > classes[bi->cno]; bi->cno++);
00568
00569 bi->cno += bias;
00570
00571 if (bi->cno >= 3) {
00572 bi->cno = 3;
00573 prev = 0;
00574 i = bi->next[prev];
00575 for (area = 0; area < 4; area++) {
00576 bi->prev[area] = prev;
00577 bi->bit_size[area] = 1;
00578 for (; i < mb_area_start[area+1]; i = bi->next[i]) {
00579 bi->mb[i] >>= 1;
00580
00581 if (bi->mb[i]) {
00582 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
00583 bi->next[prev]= i;
00584 prev = i;
00585 }
00586 }
00587 }
00588 bi->next[prev]= i;
00589 }
00590
00591 return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
00592 }
00593
00594 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
00595 {
00596 int size[5];
00597 int i, j, k, a, prev, a2;
00598 EncBlockInfo* b;
00599
00600 size[0] = size[1] = size[2] = size[3] = size[4] = 1 << 24;
00601 do {
00602 b = blks;
00603 for (i = 0; i < 5; i++) {
00604 if (!qnos[i])
00605 continue;
00606
00607 qnos[i]--;
00608 size[i] = 0;
00609 for (j = 0; j < 6; j++, b++) {
00610 for (a = 0; a < 4; a++) {
00611 if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
00612 b->bit_size[a] = 1;
00613 b->area_q[a]++;
00614 prev = b->prev[a];
00615 assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
00616 for (k = b->next[prev] ; k < mb_area_start[a+1]; k = b->next[k]) {
00617 b->mb[k] >>= 1;
00618 if (b->mb[k]) {
00619 b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
00620 prev = k;
00621 } else {
00622 if (b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
00623 for (a2 = a + 1; b->next[k] >= mb_area_start[a2+1]; a2++)
00624 b->prev[a2] = prev;
00625 assert(a2 < 4);
00626 assert(b->mb[b->next[k]]);
00627 b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
00628 -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
00629 assert(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2+1] != k));
00630 b->prev[a2] = prev;
00631 }
00632 b->next[prev] = b->next[k];
00633 }
00634 }
00635 b->prev[a+1]= prev;
00636 }
00637 size[i] += b->bit_size[a];
00638 }
00639 }
00640 if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
00641 return;
00642 }
00643 } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
00644
00645
00646 for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a){
00647 b = blks;
00648 size[0] = 5 * 6 * 4;
00649 for (j = 0; j < 6 *5; j++, b++) {
00650 prev = b->prev[0];
00651 for (k = b->next[prev]; k < 64; k = b->next[k]) {
00652 if (b->mb[k] < a && b->mb[k] > -a){
00653 b->next[prev] = b->next[k];
00654 }else{
00655 size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
00656 prev = k;
00657 }
00658 }
00659 }
00660 }
00661 }
00662
00663 static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
00664 {
00665 DVVideoContext *s = avctx->priv_data;
00666 DVwork_chunk *work_chunk = arg;
00667 int mb_index, i, j;
00668 int mb_x, mb_y, c_offset, linesize, y_stride;
00669 uint8_t* y_ptr;
00670 uint8_t* dif;
00671 LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
00672 EncBlockInfo enc_blks[5*DV_MAX_BPM];
00673 PutBitContext pbs[5*DV_MAX_BPM];
00674 PutBitContext* pb;
00675 EncBlockInfo* enc_blk;
00676 int vs_bit_size = 0;
00677 int qnos[5] = {15, 15, 15, 15, 15};
00678 int* qnosp = &qnos[0];
00679
00680 dif = &s->buf[work_chunk->buf_offset*80];
00681 enc_blk = &enc_blks[0];
00682 for (mb_index = 0; mb_index < 5; mb_index++) {
00683 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
00684
00685
00686 if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
00687 (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
00688 (s->sys->height >= 720 && mb_y != 134)) {
00689 y_stride = s->picture.linesize[0] << 3;
00690 } else {
00691 y_stride = 16;
00692 }
00693 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << 3);
00694 linesize = s->picture.linesize[0];
00695
00696 if (s->sys->video_stype == 4) {
00697 vs_bit_size +=
00698 dv_init_enc_block(enc_blk+0, y_ptr , linesize, s, 0) +
00699 dv_init_enc_block(enc_blk+1, NULL , linesize, s, 0) +
00700 dv_init_enc_block(enc_blk+2, y_ptr + 8 , linesize, s, 0) +
00701 dv_init_enc_block(enc_blk+3, NULL , linesize, s, 0);
00702 } else {
00703 vs_bit_size +=
00704 dv_init_enc_block(enc_blk+0, y_ptr , linesize, s, 0) +
00705 dv_init_enc_block(enc_blk+1, y_ptr + 8 , linesize, s, 0) +
00706 dv_init_enc_block(enc_blk+2, y_ptr + y_stride, linesize, s, 0) +
00707 dv_init_enc_block(enc_blk+3, y_ptr + 8 + y_stride, linesize, s, 0);
00708 }
00709 enc_blk += 4;
00710
00711
00712 c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
00713 (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << 3);
00714 for (j = 2; j; j--) {
00715 uint8_t *c_ptr = s->picture.data[j] + c_offset;
00716 linesize = s->picture.linesize[j];
00717 y_stride = (mb_y == 134) ? 8 : (s->picture.linesize[j] << 3);
00718 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
00719 uint8_t* d;
00720 uint8_t* b = scratch;
00721 for (i = 0; i < 8; i++) {
00722 d = c_ptr + (linesize << 3);
00723 b[0] = c_ptr[0]; b[1] = c_ptr[1]; b[2] = c_ptr[2]; b[3] = c_ptr[3];
00724 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
00725 c_ptr += linesize;
00726 b += 16;
00727 }
00728 c_ptr = scratch;
00729 linesize = 16;
00730 }
00731
00732 vs_bit_size += dv_init_enc_block( enc_blk++, c_ptr , linesize, s, 1);
00733 if (s->sys->bpm == 8) {
00734 vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride, linesize, s, 1);
00735 }
00736 }
00737 }
00738
00739 if (vs_total_ac_bits < vs_bit_size)
00740 dv_guess_qnos(&enc_blks[0], qnosp);
00741
00742
00743 for (j=0; j<5*s->sys->bpm;) {
00744 int start_mb = j;
00745
00746 dif[3] = *qnosp++;
00747 dif += 4;
00748
00749
00750 for (i=0; i<s->sys->bpm; i++, j++) {
00751 int sz = s->sys->block_sizes[i]>>3;
00752
00753 init_put_bits(&pbs[j], dif, sz);
00754 put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2);
00755 put_bits(&pbs[j], 1, enc_blks[j].dct_mode);
00756 put_bits(&pbs[j], 2, enc_blks[j].cno);
00757
00758 dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
00759 dif += sz;
00760 }
00761
00762
00763 pb = &pbs[start_mb];
00764 for (i=0; i<s->sys->bpm; i++) {
00765 if (enc_blks[start_mb+i].partial_bit_count)
00766 pb = dv_encode_ac(&enc_blks[start_mb+i], pb, &pbs[start_mb+s->sys->bpm]);
00767 }
00768 }
00769
00770
00771 pb = &pbs[0];
00772 for (j=0; j<5*s->sys->bpm; j++) {
00773 if (enc_blks[j].partial_bit_count)
00774 pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm*5]);
00775 if (enc_blks[j].partial_bit_count)
00776 av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n");
00777 }
00778
00779 for (j=0; j<5*s->sys->bpm; j++) {
00780 int pos;
00781 int size = pbs[j].size_in_bits >> 3;
00782 flush_put_bits(&pbs[j]);
00783 pos = put_bits_count(&pbs[j]) >> 3;
00784 if (pos > size) {
00785 av_log(avctx, AV_LOG_ERROR, "bitstream written beyond buffer size\n");
00786 return -1;
00787 }
00788 memset(pbs[j].buf + pos, 0xff, size - pos);
00789 }
00790
00791 return 0;
00792 }
00793
00794 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
00795 uint8_t* buf)
00796 {
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815 int apt = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1);
00816
00817 uint8_t aspect = 0;
00818 if ((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) >= 17)
00819 aspect = 0x02;
00820
00821 buf[0] = (uint8_t)pack_id;
00822 switch (pack_id) {
00823 case dv_header525:
00824 case dv_header625:
00825 buf[1] = 0xf8 |
00826 (apt & 0x07);
00827 buf[2] = (0 << 7) |
00828 (0x0f << 3) |
00829 (apt & 0x07);
00830 buf[3] = (0 << 7) |
00831 (0x0f << 3) |
00832 (apt & 0x07);
00833 buf[4] = (0 << 7) |
00834 (0x0f << 3) |
00835 (apt & 0x07);
00836 break;
00837 case dv_video_source:
00838 buf[1] = 0xff;
00839 buf[2] = (1 << 7) |
00840 (1 << 6) |
00841 (3 << 4) |
00842 0xf;
00843 buf[3] = (3 << 6) |
00844 (c->sys->dsf << 5) |
00845 c->sys->video_stype;
00846 buf[4] = 0xff;
00847 break;
00848 case dv_video_control:
00849 buf[1] = (0 << 6) |
00850 0x3f;
00851 buf[2] = 0xc8 |
00852 aspect;
00853 buf[3] = (1 << 7) |
00854 (1 << 6) |
00855 (1 << 5) |
00856 (1 << 4) |
00857 0xc;
00858 buf[4] = 0xff;
00859 break;
00860 default:
00861 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
00862 }
00863 return 5;
00864 }
00865
00866 #if CONFIG_DVVIDEO_ENCODER
00867 static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
00868 uint8_t seq_num, uint8_t dif_num,
00869 uint8_t* buf)
00870 {
00871 buf[0] = (uint8_t)t;
00872 buf[1] = (seq_num << 4) |
00873 (chan_num << 3) |
00874 7;
00875 buf[2] = dif_num;
00876 return 3;
00877 }
00878
00879
00880 static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
00881 {
00882 if (syb_num == 0 || syb_num == 6) {
00883 buf[0] = (fr << 7) |
00884 (0 << 4) |
00885 0x0f;
00886 }
00887 else if (syb_num == 11) {
00888 buf[0] = (fr << 7) |
00889 0x7f;
00890 }
00891 else {
00892 buf[0] = (fr << 7) |
00893 (0 << 4) |
00894 0x0f;
00895 }
00896 buf[1] = 0xf0 |
00897 (syb_num & 0x0f);
00898 buf[2] = 0xff;
00899 return 3;
00900 }
00901
00902 static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
00903 {
00904 int chan, i, j, k;
00905
00906 for (chan = 0; chan < c->sys->n_difchan; chan++) {
00907 for (i = 0; i < c->sys->difseg_size; i++) {
00908 memset(buf, 0xff, 80 * 6);
00909
00910
00911 buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
00912 buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
00913 buf += 72;
00914
00915
00916 for (j = 0; j < 2; j++) {
00917 buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
00918 for (k = 0; k < 6; k++)
00919 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5;
00920 buf += 29;
00921 }
00922
00923
00924 for (j = 0; j < 3; j++) {
00925 buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
00926 buf += dv_write_pack(dv_video_source, c, buf);
00927 buf += dv_write_pack(dv_video_control, c, buf);
00928 buf += 7*5;
00929 buf += dv_write_pack(dv_video_source, c, buf);
00930 buf += dv_write_pack(dv_video_control, c, buf);
00931 buf += 4*5 + 2;
00932 }
00933
00934
00935 for (j = 0; j < 135; j++) {
00936 if (j%15 == 0) {
00937 memset(buf, 0xff, 80);
00938 buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
00939 buf += 77;
00940 }
00941 buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
00942 buf += 77;
00943
00944
00945
00946 }
00947 }
00948 }
00949 }
00950
00951
00952 static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt,
00953 const AVFrame *frame, int *got_packet)
00954 {
00955 DVVideoContext *s = c->priv_data;
00956 int ret;
00957
00958 s->sys = avpriv_dv_codec_profile(c);
00959 if (!s->sys || ff_dv_init_dynamic_tables(s->sys))
00960 return -1;
00961 if ((ret = ff_alloc_packet2(c, pkt, s->sys->frame_size)) < 0)
00962 return ret;
00963
00964 c->pix_fmt = s->sys->pix_fmt;
00965 s->picture = *frame;
00966 s->picture.key_frame = 1;
00967 s->picture.pict_type = AV_PICTURE_TYPE_I;
00968
00969 s->buf = pkt->data;
00970 c->execute(c, dv_encode_video_segment, s->sys->work_chunks, NULL,
00971 dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
00972
00973 emms_c();
00974
00975 dv_format_frame(s, pkt->data);
00976
00977 pkt->flags |= AV_PKT_FLAG_KEY;
00978 *got_packet = 1;
00979
00980 return 0;
00981 }
00982
00983 AVCodec ff_dvvideo_encoder = {
00984 .name = "dvvideo",
00985 .type = AVMEDIA_TYPE_VIDEO,
00986 .id = CODEC_ID_DVVIDEO,
00987 .priv_data_size = sizeof(DVVideoContext),
00988 .init = dvvideo_init_encoder,
00989 .encode2 = dvvideo_encode_frame,
00990 .capabilities = CODEC_CAP_SLICE_THREADS,
00991 .pix_fmts = (const enum PixelFormat[]) {
00992 PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, PIX_FMT_NONE
00993 },
00994 .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
00995 };
00996 #endif // CONFIG_DVVIDEO_ENCODER