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
00036 #define ALT_BITSTREAM_READER
00037 #include "avcodec.h"
00038 #include "dsputil.h"
00039 #include "mpegvideo.h"
00040 #include "simple_idct.h"
00041 #include "dvdata.h"
00042
00043
00044
00045
00046 typedef struct DVVideoContext {
00047 const DVprofile* sys;
00048 AVFrame picture;
00049 AVCodecContext *avctx;
00050 uint8_t *buf;
00051
00052 uint8_t dv_zigzag[2][64];
00053 uint8_t dv_idct_shift[2][2][22][64];
00054
00055 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
00056 void (*fdct[2])(DCTELEM *block);
00057 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
00058 } DVVideoContext;
00059
00060
00061
00062
00063 #define DV_ANCHOR_SIZE (2*12*27)
00064
00065 static void* dv_anchor[DV_ANCHOR_SIZE];
00066
00067 #define TEX_VLC_BITS 9
00068
00069 #ifdef DV_CODEC_TINY_TARGET
00070 #define DV_VLC_MAP_RUN_SIZE 15
00071 #define DV_VLC_MAP_LEV_SIZE 23
00072 #else
00073 #define DV_VLC_MAP_RUN_SIZE 64
00074 #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check
00075 #endif
00076
00077
00078 static RL_VLC_ELEM dv_rl_vlc[1184];
00079
00080 static struct dv_vlc_pair {
00081 uint32_t vlc;
00082 uint8_t size;
00083 } dv_vlc_map[DV_VLC_MAP_RUN_SIZE][DV_VLC_MAP_LEV_SIZE];
00084
00085 static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
00086 {
00087 int i, q, j;
00088
00089
00090 for(q = 0; q < 22; q++) {
00091
00092 for(i = 1; i < 64; i++) {
00093
00094 j = perm[i];
00095 s->dv_idct_shift[0][0][q][j] =
00096 dv_quant_shifts[q][dv_88_areas[i]] + 1;
00097 s->dv_idct_shift[1][0][q][j] = s->dv_idct_shift[0][0][q][j] + 1;
00098 }
00099
00100
00101 for(i = 1; i < 64; i++) {
00102
00103 s->dv_idct_shift[0][1][q][i] =
00104 dv_quant_shifts[q][dv_248_areas[i]] + 1;
00105 s->dv_idct_shift[1][1][q][i] = s->dv_idct_shift[0][1][q][i] + 1;
00106 }
00107 }
00108 }
00109
00110 static int dvvideo_init(AVCodecContext *avctx)
00111 {
00112 DVVideoContext *s = avctx->priv_data;
00113 DSPContext dsp;
00114 static int done=0;
00115 int i, j;
00116
00117 if (!done) {
00118 VLC dv_vlc;
00119 uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
00120 uint8_t new_dv_vlc_len[NB_DV_VLC*2];
00121 uint8_t new_dv_vlc_run[NB_DV_VLC*2];
00122 int16_t new_dv_vlc_level[NB_DV_VLC*2];
00123
00124 done = 1;
00125
00126
00127 for (i=0; i<DV_ANCHOR_SIZE; i++)
00128 dv_anchor[i] = (void*)(size_t)i;
00129
00130
00131 for (i=0, j=0; i<NB_DV_VLC; i++, j++) {
00132 new_dv_vlc_bits[j] = dv_vlc_bits[i];
00133 new_dv_vlc_len[j] = dv_vlc_len[i];
00134 new_dv_vlc_run[j] = dv_vlc_run[i];
00135 new_dv_vlc_level[j] = dv_vlc_level[i];
00136
00137 if (dv_vlc_level[i]) {
00138 new_dv_vlc_bits[j] <<= 1;
00139 new_dv_vlc_len[j]++;
00140
00141 j++;
00142 new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
00143 new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
00144 new_dv_vlc_run[j] = dv_vlc_run[i];
00145 new_dv_vlc_level[j] = -dv_vlc_level[i];
00146 }
00147 }
00148
00149
00150
00151 init_vlc(&dv_vlc, TEX_VLC_BITS, j,
00152 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
00153 assert(dv_vlc.table_size == 1184);
00154
00155 for(i = 0; i < dv_vlc.table_size; i++){
00156 int code= dv_vlc.table[i][0];
00157 int len = dv_vlc.table[i][1];
00158 int level, run;
00159
00160 if(len<0){
00161 run= 0;
00162 level= code;
00163 } else {
00164 run= new_dv_vlc_run[code] + 1;
00165 level= new_dv_vlc_level[code];
00166 }
00167 dv_rl_vlc[i].len = len;
00168 dv_rl_vlc[i].level = level;
00169 dv_rl_vlc[i].run = run;
00170 }
00171 free_vlc(&dv_vlc);
00172
00173 for (i = 0; i < NB_DV_VLC - 1; i++) {
00174 if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE)
00175 continue;
00176 #ifdef DV_CODEC_TINY_TARGET
00177 if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
00178 continue;
00179 #endif
00180
00181 if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
00182 continue;
00183
00184 dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
00185 (!!dv_vlc_level[i]);
00186 dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
00187 (!!dv_vlc_level[i]);
00188 }
00189 for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
00190 #ifdef DV_CODEC_TINY_TARGET
00191 for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
00192 if (dv_vlc_map[i][j].size == 0) {
00193 dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
00194 (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
00195 dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
00196 dv_vlc_map[0][j].size;
00197 }
00198 }
00199 #else
00200 for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
00201 if (dv_vlc_map[i][j].size == 0) {
00202 dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
00203 (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
00204 dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
00205 dv_vlc_map[0][j].size;
00206 }
00207 dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
00208 dv_vlc_map[i][j].vlc | 1;
00209 dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
00210 dv_vlc_map[i][j].size;
00211 }
00212 #endif
00213 }
00214 }
00215
00216
00217 dsputil_init(&dsp, avctx);
00218 s->get_pixels = dsp.get_pixels;
00219
00220
00221 s->fdct[0] = dsp.fdct;
00222 s->idct_put[0] = dsp.idct_put;
00223 for (i=0; i<64; i++)
00224 s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
00225
00226
00227 s->fdct[1] = dsp.fdct248;
00228 s->idct_put[1] = ff_simple_idct248_put;
00229 if(avctx->lowres){
00230 for (i=0; i<64; i++){
00231 int j= ff_zigzag248_direct[i];
00232 s->dv_zigzag[1][i] = dsp.idct_permutation[(j&7) + (j&8)*4 + (j&48)/2];
00233 }
00234 }else
00235 memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
00236
00237
00238 dv_build_unquantize_tables(s, dsp.idct_permutation);
00239
00240 avctx->coded_frame = &s->picture;
00241 s->avctx= avctx;
00242
00243 return 0;
00244 }
00245
00246
00247
00248
00249 typedef struct BlockInfo {
00250 const uint8_t *shift_table;
00251 const uint8_t *scan_table;
00252 const int *iweight_table;
00253 uint8_t pos;
00254 uint8_t dct_mode;
00255 uint8_t partial_bit_count;
00256 uint16_t partial_bit_buffer;
00257 int shift_offset;
00258 } BlockInfo;
00259
00260
00261 static const uint16_t block_sizes[6] = {
00262 112, 112, 112, 112, 80, 80
00263 };
00264
00265 static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
00266
00267 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
00268
00269 static inline int get_bits_left(GetBitContext *s)
00270 {
00271 return s->size_in_bits - get_bits_count(s);
00272 }
00273
00274 static inline int get_bits_size(GetBitContext *s)
00275 {
00276 return s->size_in_bits;
00277 }
00278
00279 static inline int put_bits_left(PutBitContext* s)
00280 {
00281 return (s->buf_end - s->buf) * 8 - put_bits_count(s);
00282 }
00283
00284
00285 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
00286 {
00287 int last_index = get_bits_size(gb);
00288 const uint8_t *scan_table = mb->scan_table;
00289 const uint8_t *shift_table = mb->shift_table;
00290 const int *iweight_table = mb->iweight_table;
00291 int pos = mb->pos;
00292 int partial_bit_count = mb->partial_bit_count;
00293 int level, pos1, run, vlc_len, index;
00294
00295 OPEN_READER(re, gb);
00296 UPDATE_CACHE(re, gb);
00297
00298
00299 if (partial_bit_count > 0) {
00300 re_cache = ((unsigned)re_cache >> partial_bit_count) |
00301 (mb->partial_bit_buffer << (sizeof(re_cache)*8 - partial_bit_count));
00302 re_index -= partial_bit_count;
00303 mb->partial_bit_count = 0;
00304 }
00305
00306
00307 for(;;) {
00308 #ifdef VLC_DEBUG
00309 printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index);
00310 #endif
00311
00312 index = NEG_USR32(re_cache, TEX_VLC_BITS);
00313 vlc_len = dv_rl_vlc[index].len;
00314 if (vlc_len < 0) {
00315 index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
00316 vlc_len = TEX_VLC_BITS - vlc_len;
00317 }
00318 level = dv_rl_vlc[index].level;
00319 run = dv_rl_vlc[index].run;
00320
00321
00322 if (re_index + vlc_len > last_index) {
00323
00324 mb->partial_bit_count = last_index - re_index;
00325 mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
00326 re_index = last_index;
00327 break;
00328 }
00329 re_index += vlc_len;
00330
00331 #ifdef VLC_DEBUG
00332 printf("run=%d level=%d\n", run, level);
00333 #endif
00334 pos += run;
00335 if (pos >= 64)
00336 break;
00337
00338 pos1 = scan_table[pos];
00339 level <<= shift_table[pos1];
00340
00341
00342 level = (level*iweight_table[pos] + (1 << (dv_iweight_bits-1))) >> dv_iweight_bits;
00343
00344 block[pos1] = level;
00345
00346 UPDATE_CACHE(re, gb);
00347 }
00348 CLOSE_READER(re, gb);
00349 mb->pos = pos;
00350 }
00351
00352 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
00353 {
00354 int bits_left = get_bits_left(gb);
00355 while (bits_left >= MIN_CACHE_BITS) {
00356 put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
00357 bits_left -= MIN_CACHE_BITS;
00358 }
00359 if (bits_left > 0) {
00360 put_bits(pb, bits_left, get_bits(gb, bits_left));
00361 }
00362 }
00363
00364
00365 static inline void dv_decode_video_segment(DVVideoContext *s,
00366 const uint8_t *buf_ptr1,
00367 const uint16_t *mb_pos_ptr)
00368 {
00369 int quant, dc, dct_mode, class1, j;
00370 int mb_index, mb_x, mb_y, v, last_index;
00371 DCTELEM *block, *block1;
00372 int c_offset;
00373 uint8_t *y_ptr;
00374 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
00375 const uint8_t *buf_ptr;
00376 PutBitContext pb, vs_pb;
00377 GetBitContext gb;
00378 BlockInfo mb_data[5 * 6], *mb, *mb1;
00379 DECLARE_ALIGNED_8(DCTELEM, sblock[5*6][64]);
00380 DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]);
00381 DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]);
00382 const int log2_blocksize= 3-s->avctx->lowres;
00383
00384 assert((((int)mb_bit_buffer)&7)==0);
00385 assert((((int)vs_bit_buffer)&7)==0);
00386
00387 memset(sblock, 0, sizeof(sblock));
00388
00389
00390 buf_ptr = buf_ptr1;
00391 block1 = &sblock[0][0];
00392 mb1 = mb_data;
00393 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
00394 for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) {
00395
00396 quant = buf_ptr[3] & 0x0f;
00397 buf_ptr += 4;
00398 init_put_bits(&pb, mb_bit_buffer, 80);
00399 mb = mb1;
00400 block = block1;
00401 for(j = 0;j < 6; j++) {
00402 last_index = block_sizes[j];
00403 init_get_bits(&gb, buf_ptr, last_index);
00404
00405
00406 dc = get_sbits(&gb, 9);
00407 dct_mode = get_bits1(&gb);
00408 mb->dct_mode = dct_mode;
00409 mb->scan_table = s->dv_zigzag[dct_mode];
00410 mb->iweight_table = dct_mode ? dv_iweight_248 : dv_iweight_88;
00411 class1 = get_bits(&gb, 2);
00412 mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
00413 [quant + dv_quant_offset[class1]];
00414 dc = dc << 2;
00415
00416
00417 dc += 1024;
00418 block[0] = dc;
00419 buf_ptr += last_index >> 3;
00420 mb->pos = 0;
00421 mb->partial_bit_count = 0;
00422
00423 #ifdef VLC_DEBUG
00424 printf("MB block: %d, %d ", mb_index, j);
00425 #endif
00426 dv_decode_ac(&gb, mb, block);
00427
00428
00429
00430 if (mb->pos >= 64)
00431 bit_copy(&pb, &gb);
00432
00433 block += 64;
00434 mb++;
00435 }
00436
00437
00438 #ifdef VLC_DEBUG
00439 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
00440 #endif
00441 block = block1;
00442 mb = mb1;
00443 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
00444 flush_put_bits(&pb);
00445 for(j = 0;j < 6; j++, block += 64, mb++) {
00446 if (mb->pos < 64 && get_bits_left(&gb) > 0) {
00447 dv_decode_ac(&gb, mb, block);
00448
00449 if (mb->pos < 64)
00450 break;
00451 }
00452 }
00453
00454
00455 if (j >= 6)
00456 bit_copy(&vs_pb, &gb);
00457 }
00458
00459
00460 #ifdef VLC_DEBUG
00461 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
00462 #endif
00463 block = &sblock[0][0];
00464 mb = mb_data;
00465 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
00466 flush_put_bits(&vs_pb);
00467 for(mb_index = 0; mb_index < 5; mb_index++) {
00468 for(j = 0;j < 6; j++) {
00469 if (mb->pos < 64) {
00470 #ifdef VLC_DEBUG
00471 printf("start %d:%d\n", mb_index, j);
00472 #endif
00473 dv_decode_ac(&gb, mb, block);
00474 }
00475 if (mb->pos >= 64 && mb->pos < 127)
00476 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
00477 block += 64;
00478 mb++;
00479 }
00480 }
00481
00482
00483 block = &sblock[0][0];
00484 mb = mb_data;
00485 for(mb_index = 0; mb_index < 5; mb_index++) {
00486 v = *mb_pos_ptr++;
00487 mb_x = v & 0xff;
00488 mb_y = v >> 8;
00489 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
00490 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + (mb_x>>1))<<log2_blocksize);
00491 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
00492 } else {
00493 y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
00494 if (s->sys->pix_fmt == PIX_FMT_YUV411P)
00495 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
00496 else
00497 c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize);
00498 }
00499 for(j = 0;j < 6; j++) {
00500 idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3];
00501 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
00502 if (j == 0 || j == 2) {
00503
00504 idct_put(y_ptr + ((j >> 1)<<log2_blocksize),
00505 s->picture.linesize[0], block);
00506 } else if(j > 3) {
00507
00508 idct_put(s->picture.data[6 - j] + c_offset,
00509 s->picture.linesize[6 - j], block);
00510 }
00511
00512 } else {
00513 if (j < 4) {
00514 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
00515
00516 idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block);
00517 } else {
00518 idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
00519 s->picture.linesize[0], block);
00520 }
00521 } else {
00522 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
00523 uint64_t aligned_pixels[64/8];
00524 uint8_t *pixels= (uint8_t*)aligned_pixels;
00525 uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1;
00526 int x, y, linesize;
00527
00528 idct_put(pixels, 8, block);
00529 linesize = s->picture.linesize[6 - j];
00530 c_ptr = s->picture.data[6 - j] + c_offset;
00531 ptr = pixels;
00532 for(y = 0;y < (1<<log2_blocksize); y++) {
00533 ptr1= ptr + (1<<(log2_blocksize-1));
00534 c_ptr1 = c_ptr + (linesize<<log2_blocksize);
00535 for(x=0; x < (1<<(log2_blocksize-1)); x++){
00536 c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
00537 }
00538 c_ptr += linesize;
00539 ptr += 8;
00540 }
00541 } else {
00542
00543 idct_put(s->picture.data[6 - j] + c_offset,
00544 s->picture.linesize[6 - j], block);
00545 }
00546 }
00547 }
00548 block += 64;
00549 mb++;
00550 }
00551 }
00552 }
00553
00554 #ifdef DV_CODEC_TINY_TARGET
00555
00556 static av_always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
00557 {
00558 int size;
00559 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
00560 *vlc = dv_vlc_map[run][level].vlc | sign;
00561 size = dv_vlc_map[run][level].size;
00562 }
00563 else {
00564 if (level < DV_VLC_MAP_LEV_SIZE) {
00565 *vlc = dv_vlc_map[0][level].vlc | sign;
00566 size = dv_vlc_map[0][level].size;
00567 } else {
00568 *vlc = 0xfe00 | (level << 1) | sign;
00569 size = 16;
00570 }
00571 if (run) {
00572 *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
00573 (0x1f80 | (run - 1))) << size;
00574 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
00575 }
00576 }
00577
00578 return size;
00579 }
00580
00581 static av_always_inline int dv_rl2vlc_size(int run, int level)
00582 {
00583 int size;
00584
00585 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
00586 size = dv_vlc_map[run][level].size;
00587 }
00588 else {
00589 size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
00590 if (run) {
00591 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
00592 }
00593 }
00594 return size;
00595 }
00596 #else
00597 static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
00598 {
00599 *vlc = dv_vlc_map[run][l].vlc | sign;
00600 return dv_vlc_map[run][l].size;
00601 }
00602
00603 static av_always_inline int dv_rl2vlc_size(int run, int l)
00604 {
00605 return dv_vlc_map[run][l].size;
00606 }
00607 #endif
00608
00609 typedef struct EncBlockInfo {
00610 int area_q[4];
00611 int bit_size[4];
00612 int prev[5];
00613 int cur_ac;
00614 int cno;
00615 int dct_mode;
00616 DCTELEM mb[64];
00617 uint8_t next[64];
00618 uint8_t sign[64];
00619 uint8_t partial_bit_count;
00620 uint32_t partial_bit_buffer;
00621 } EncBlockInfo;
00622
00623 static av_always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
00624 PutBitContext* pb_end)
00625 {
00626 int prev;
00627 int bits_left;
00628 PutBitContext* pb = pb_pool;
00629 int size = bi->partial_bit_count;
00630 uint32_t vlc = bi->partial_bit_buffer;
00631
00632 bi->partial_bit_count = bi->partial_bit_buffer = 0;
00633 for(;;){
00634
00635 for (; size > (bits_left = put_bits_left(pb)); pb++) {
00636 if (bits_left) {
00637 size -= bits_left;
00638 put_bits(pb, bits_left, vlc >> size);
00639 vlc = vlc & ((1<<size)-1);
00640 }
00641 if (pb + 1 >= pb_end) {
00642 bi->partial_bit_count = size;
00643 bi->partial_bit_buffer = vlc;
00644 return pb;
00645 }
00646 }
00647
00648
00649 put_bits(pb, size, vlc);
00650
00651 if(bi->cur_ac>=64)
00652 break;
00653
00654
00655 prev= bi->cur_ac;
00656 bi->cur_ac = bi->next[prev];
00657 if(bi->cur_ac < 64){
00658 size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
00659 } else {
00660 size = 4; vlc = 6;
00661 }
00662 }
00663 return pb;
00664 }
00665
00666 static av_always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
00667 const uint8_t* zigzag_scan, const int *weight, int bias)
00668 {
00669 int i, area;
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 #if 0
00681 static const int classes[] = {12, 24, 36, 0xffff};
00682 #else
00683 static const int classes[] = {-1, -1, 255, 0xffff};
00684 #endif
00685 int max=classes[0];
00686 int prev=0;
00687
00688 bi->mb[0] = blk[0];
00689
00690 for (area = 0; area < 4; area++) {
00691 bi->prev[area] = prev;
00692 bi->bit_size[area] = 1;
00693 for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
00694 int level = blk[zigzag_scan[i]];
00695
00696 if (level+15 > 30U) {
00697 bi->sign[i] = (level>>31)&1;
00698
00699
00700
00701 level = (FFABS(level) * weight[i] + (1<<(dv_weight_bits+3))) >> (dv_weight_bits+4);
00702 bi->mb[i] = level;
00703 if(level>max) max= level;
00704 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
00705 bi->next[prev]= i;
00706 prev= i;
00707 }
00708 }
00709 }
00710 bi->next[prev]= i;
00711 for(bi->cno = 0; max > classes[bi->cno]; bi->cno++);
00712
00713 bi->cno += bias;
00714
00715 if (bi->cno >= 3) {
00716 bi->cno = 3;
00717 prev=0;
00718 i= bi->next[prev];
00719 for (area = 0; area < 4; area++) {
00720 bi->prev[area] = prev;
00721 bi->bit_size[area] = 1;
00722 for (; i<mb_area_start[area+1]; i= bi->next[i]) {
00723 bi->mb[i] >>=1;
00724
00725 if (bi->mb[i]) {
00726 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
00727 bi->next[prev]= i;
00728 prev= i;
00729 }
00730 }
00731 }
00732 bi->next[prev]= i;
00733 }
00734 }
00735
00736
00737 #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
00738 static av_always_inline int dv_guess_dct_mode(DCTELEM *blk) {
00739 DCTELEM *s;
00740 int score88 = 0;
00741 int score248 = 0;
00742 int i;
00743
00744
00745 s = blk;
00746 for(i=0; i<7; i++) {
00747 score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
00748 SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
00749 s += 8;
00750 }
00751
00752 s = blk;
00753 for(i=0; i<6; i++) {
00754 score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
00755 SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
00756 s += 8;
00757 }
00758
00759 return (score88 - score248 > -10);
00760 }
00761
00762 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
00763 {
00764 int size[5];
00765 int i, j, k, a, prev, a2;
00766 EncBlockInfo* b;
00767
00768 size[0] = size[1] = size[2] = size[3] = size[4] = 1<<24;
00769 do {
00770 b = blks;
00771 for (i=0; i<5; i++) {
00772 if (!qnos[i])
00773 continue;
00774
00775 qnos[i]--;
00776 size[i] = 0;
00777 for (j=0; j<6; j++, b++) {
00778 for (a=0; a<4; a++) {
00779 if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
00780 b->bit_size[a] = 1;
00781 b->area_q[a]++;
00782 prev= b->prev[a];
00783 assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
00784 for (k= b->next[prev] ; k<mb_area_start[a+1]; k= b->next[k]) {
00785 b->mb[k] >>= 1;
00786 if (b->mb[k]) {
00787 b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
00788 prev= k;
00789 } else {
00790 if(b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
00791 for(a2=a+1; b->next[k] >= mb_area_start[a2+1]; a2++)
00792 b->prev[a2] = prev;
00793 assert(a2<4);
00794 assert(b->mb[b->next[k]]);
00795 b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
00796 -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
00797 assert(b->prev[a2]==k && (a2+1 >= 4 || b->prev[a2+1]!=k));
00798 b->prev[a2] = prev;
00799 }
00800 b->next[prev] = b->next[k];
00801 }
00802 }
00803 b->prev[a+1]= prev;
00804 }
00805 size[i] += b->bit_size[a];
00806 }
00807 }
00808 if(vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
00809 return;
00810 }
00811 } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
00812
00813
00814 for(a=2; a==2 || vs_total_ac_bits < size[0]; a+=a){
00815 b = blks;
00816 size[0] = 5*6*4;
00817 for (j=0; j<6*5; j++, b++) {
00818 prev= b->prev[0];
00819 for (k= b->next[prev]; k<64; k= b->next[k]) {
00820 if(b->mb[k] < a && b->mb[k] > -a){
00821 b->next[prev] = b->next[k];
00822 }else{
00823 size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
00824 prev= k;
00825 }
00826 }
00827 }
00828 }
00829 }
00830
00831 static inline void dv_encode_video_segment(DVVideoContext *s,
00832 uint8_t *dif,
00833 const uint16_t *mb_pos_ptr)
00834 {
00835 int mb_index, i, j, v;
00836 int mb_x, mb_y, c_offset, linesize;
00837 uint8_t* y_ptr;
00838 uint8_t* data;
00839 uint8_t* ptr;
00840 int do_edge_wrap;
00841 DECLARE_ALIGNED_16(DCTELEM, block[64]);
00842 EncBlockInfo enc_blks[5*6];
00843 PutBitContext pbs[5*6];
00844 PutBitContext* pb;
00845 EncBlockInfo* enc_blk;
00846 int vs_bit_size = 0;
00847 int qnos[5];
00848
00849 assert((((int)block) & 15) == 0);
00850
00851 enc_blk = &enc_blks[0];
00852 pb = &pbs[0];
00853 for(mb_index = 0; mb_index < 5; mb_index++) {
00854 v = *mb_pos_ptr++;
00855 mb_x = v & 0xff;
00856 mb_y = v >> 8;
00857 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
00858 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);
00859 } else {
00860 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
00861 }
00862 if (s->sys->pix_fmt == PIX_FMT_YUV420P) {
00863 c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
00864 } else {
00865 c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));
00866 }
00867 do_edge_wrap = 0;
00868 qnos[mb_index] = 15;
00869 ptr = dif + mb_index*80 + 4;
00870 for(j = 0;j < 6; j++) {
00871 int dummy = 0;
00872 if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
00873 if (j == 0 || j == 2) {
00874
00875 data = y_ptr + ((j>>1) * 8);
00876 linesize = s->picture.linesize[0];
00877 } else if (j > 3) {
00878
00879 data = s->picture.data[6 - j] + c_offset;
00880 linesize = s->picture.linesize[6 - j];
00881 } else {
00882
00883 data = 0;
00884 linesize = 0;
00885 dummy = 1;
00886 }
00887 } else {
00888 if (j < 4) {
00889
00890 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
00891 data = y_ptr + (j * 8);
00892 } else {
00893 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
00894 }
00895 linesize = s->picture.linesize[0];
00896 } else {
00897
00898 data = s->picture.data[6 - j] + c_offset;
00899 linesize = s->picture.linesize[6 - j];
00900 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
00901 do_edge_wrap = 1;
00902 }
00903 }
00904
00905
00906 if (do_edge_wrap) {
00907 uint8_t* d;
00908 DCTELEM *b = block;
00909 for (i=0;i<8;i++) {
00910 d = data + 8 * linesize;
00911 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
00912 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
00913 data += linesize;
00914 b += 8;
00915 }
00916 } else {
00917 if (!dummy)
00918 s->get_pixels(block, data, linesize);
00919 }
00920
00921 if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
00922 enc_blk->dct_mode = dv_guess_dct_mode(block);
00923 else
00924 enc_blk->dct_mode = 0;
00925 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
00926 enc_blk->partial_bit_count = 0;
00927 enc_blk->partial_bit_buffer = 0;
00928 enc_blk->cur_ac = 0;
00929
00930 if (dummy) {
00931
00932
00933 memset(block, 0, sizeof(block));
00934 } else {
00935 s->fdct[enc_blk->dct_mode](block);
00936 }
00937
00938 dv_set_class_number(block, enc_blk,
00939 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
00940 enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
00941 j/4);
00942
00943 init_put_bits(pb, ptr, block_sizes[j]/8);
00944 put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
00945 put_bits(pb, 1, enc_blk->dct_mode);
00946 put_bits(pb, 2, enc_blk->cno);
00947
00948 vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
00949 enc_blk->bit_size[2] + enc_blk->bit_size[3];
00950 ++enc_blk;
00951 ++pb;
00952 ptr += block_sizes[j]/8;
00953 }
00954 }
00955
00956 if (vs_total_ac_bits < vs_bit_size)
00957 dv_guess_qnos(&enc_blks[0], &qnos[0]);
00958
00959 for (i=0; i<5; i++) {
00960 dif[i*80 + 3] = qnos[i];
00961 }
00962
00963
00964 for (j=0; j<5*6; j++)
00965 dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
00966
00967
00968 for (j=0; j<5*6; j+=6) {
00969 pb= &pbs[j];
00970 for (i=0; i<6; i++) {
00971 if (enc_blks[i+j].partial_bit_count)
00972 pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);
00973 }
00974 }
00975
00976
00977 pb= &pbs[0];
00978 for (j=0; j<5*6; j++) {
00979 if (enc_blks[j].partial_bit_count)
00980 pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);
00981 if (enc_blks[j].partial_bit_count)
00982 av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
00983 }
00984
00985 for (j=0; j<5*6; j++)
00986 flush_put_bits(&pbs[j]);
00987 }
00988
00989 static int dv_decode_mt(AVCodecContext *avctx, void* sl)
00990 {
00991 DVVideoContext *s = avctx->priv_data;
00992 int slice = (size_t)sl;
00993
00994
00995 int chan = slice / (s->sys->difseg_size * 27);
00996
00997
00998 int chan_slice = slice % (s->sys->difseg_size * 27);
00999
01000
01001 int chan_offset = chan * s->sys->difseg_size * 150 * 80;
01002
01003 dv_decode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
01004 &s->sys->video_place[slice*5]);
01005 return 0;
01006 }
01007
01008 #ifdef CONFIG_ENCODERS
01009 static int dv_encode_mt(AVCodecContext *avctx, void* sl)
01010 {
01011 DVVideoContext *s = avctx->priv_data;
01012 int slice = (size_t)sl;
01013
01014
01015 int chan = slice / (s->sys->difseg_size * 27);
01016
01017
01018 int chan_slice = slice % (s->sys->difseg_size * 27);
01019
01020
01021 int chan_offset = chan * s->sys->difseg_size * 150 * 80;
01022
01023 dv_encode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
01024 &s->sys->video_place[slice*5]);
01025 return 0;
01026 }
01027 #endif
01028
01029 #ifdef CONFIG_DECODERS
01030
01031
01032 static int dvvideo_decode_frame(AVCodecContext *avctx,
01033 void *data, int *data_size,
01034 const uint8_t *buf, int buf_size)
01035 {
01036 DVVideoContext *s = avctx->priv_data;
01037
01038 s->sys = dv_frame_profile(buf);
01039 if (!s->sys || buf_size < s->sys->frame_size)
01040 return -1;
01041
01042 if(s->picture.data[0])
01043 avctx->release_buffer(avctx, &s->picture);
01044
01045 s->picture.reference = 0;
01046 s->picture.key_frame = 1;
01047 s->picture.pict_type = FF_I_TYPE;
01048 avctx->pix_fmt = s->sys->pix_fmt;
01049 avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
01050 if(avctx->get_buffer(avctx, &s->picture) < 0) {
01051 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01052 return -1;
01053 }
01054 s->picture.interlaced_frame = 1;
01055 s->picture.top_field_first = 0;
01056
01057 s->buf = buf;
01058 avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
01059 s->sys->n_difchan * s->sys->difseg_size * 27);
01060
01061 emms_c();
01062
01063
01064 *data_size = sizeof(AVFrame);
01065 *(AVFrame*)data= s->picture;
01066
01067 return s->sys->frame_size;
01068 }
01069 #endif
01070
01071
01072 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, uint8_t* buf)
01073 {
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092 int apt = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1);
01093 int stype = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 4 : 0);
01094
01095 uint8_t aspect = 0;
01096 if((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) == 17)
01097 aspect = 0x02;
01098
01099 buf[0] = (uint8_t)pack_id;
01100 switch (pack_id) {
01101 case dv_header525:
01102 case dv_header625:
01103 buf[1] = 0xf8 |
01104 (apt & 0x07);
01105 buf[2] = (0 << 7) |
01106 (0x0f << 3) |
01107 (apt & 0x07);
01108 buf[3] = (0 << 7) |
01109 (0x0f << 3) |
01110 (apt & 0x07);
01111 buf[4] = (0 << 7) |
01112 (0x0f << 3) |
01113 (apt & 0x07);
01114 break;
01115 case dv_video_source:
01116 buf[1] = 0xff;
01117 buf[2] = (1 << 7) |
01118 (1 << 6) |
01119 (3 << 4) |
01120 0xf;
01121 buf[3] = (3 << 6) |
01122 (c->sys->dsf << 5) |
01123 stype;
01124 buf[4] = 0xff;
01125 break;
01126 case dv_video_control:
01127 buf[1] = (0 << 6) |
01128 0x3f;
01129 buf[2] = 0xc8 |
01130 aspect;
01131 buf[3] = (1 << 7) |
01132 (1 << 6) |
01133 (1 << 5) |
01134 (1 << 4) |
01135 0xc;
01136 buf[4] = 0xff;
01137 break;
01138 default:
01139 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
01140 }
01141 return 5;
01142 }
01143
01144 static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
01145 {
01146 int chan, i, j, k;
01147
01148 for (chan = 0; chan < c->sys->n_difchan; chan++) {
01149 for (i = 0; i < c->sys->difseg_size; i++) {
01150 memset(buf, 0xff, 80 * 6);
01151
01152
01153 buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
01154 buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
01155 buf += 72;
01156
01157
01158 for (j = 0; j < 2; j++) {
01159 buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
01160 for (k = 0; k < 6; k++)
01161 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5;
01162 buf += 29;
01163 }
01164
01165
01166 for (j = 0; j < 3; j++) {
01167 buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
01168 buf += dv_write_pack(dv_video_source, c, buf);
01169 buf += dv_write_pack(dv_video_control, c, buf);
01170 buf += 7*5;
01171 buf += dv_write_pack(dv_video_source, c, buf);
01172 buf += dv_write_pack(dv_video_control, c, buf);
01173 buf += 4*5 + 2;
01174 }
01175
01176
01177 for (j = 0; j < 135; j++) {
01178 if (j%15 == 0) {
01179 memset(buf, 0xff, 80);
01180 buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
01181 buf += 77;
01182 }
01183 buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
01184 buf += 77;
01185
01186
01187
01188 }
01189 }
01190 }
01191 }
01192
01193
01194 #ifdef CONFIG_ENCODERS
01195 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
01196 void *data)
01197 {
01198 DVVideoContext *s = c->priv_data;
01199
01200 s->sys = dv_codec_profile(c);
01201 if (!s->sys)
01202 return -1;
01203 if(buf_size < s->sys->frame_size)
01204 return -1;
01205
01206 c->pix_fmt = s->sys->pix_fmt;
01207 s->picture = *((AVFrame *)data);
01208 s->picture.key_frame = 1;
01209 s->picture.pict_type = FF_I_TYPE;
01210
01211 s->buf = buf;
01212 c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
01213 s->sys->n_difchan * s->sys->difseg_size * 27);
01214
01215 emms_c();
01216
01217 dv_format_frame(s, buf);
01218
01219 return s->sys->frame_size;
01220 }
01221 #endif
01222
01223 static int dvvideo_close(AVCodecContext *c)
01224 {
01225 DVVideoContext *s = c->priv_data;
01226
01227 if(s->picture.data[0])
01228 c->release_buffer(c, &s->picture);
01229
01230 return 0;
01231 }
01232
01233
01234 #ifdef CONFIG_DVVIDEO_ENCODER
01235 AVCodec dvvideo_encoder = {
01236 "dvvideo",
01237 CODEC_TYPE_VIDEO,
01238 CODEC_ID_DVVIDEO,
01239 sizeof(DVVideoContext),
01240 dvvideo_init,
01241 dvvideo_encode_frame,
01242 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, -1},
01243 };
01244 #endif // CONFIG_DVVIDEO_ENCODER
01245
01246 #ifdef CONFIG_DVVIDEO_DECODER
01247 AVCodec dvvideo_decoder = {
01248 "dvvideo",
01249 CODEC_TYPE_VIDEO,
01250 CODEC_ID_DVVIDEO,
01251 sizeof(DVVideoContext),
01252 dvvideo_init,
01253 NULL,
01254 dvvideo_close,
01255 dvvideo_decode_frame,
01256 CODEC_CAP_DR1,
01257 NULL
01258 };
01259 #endif