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
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033 #include "mpegvideo.h"
00034 #include "simple_idct.h"
00035 #include "dvdata.h"
00036
00037 typedef struct DVVideoContext {
00038 const DVprofile* sys;
00039 AVFrame picture;
00040 uint8_t *buf;
00041
00042 uint8_t dv_zigzag[2][64];
00043 uint8_t dv_idct_shift[2][2][22][64];
00044
00045 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
00046 void (*fdct[2])(DCTELEM *block);
00047 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
00048 } DVVideoContext;
00049
00050 #define TEX_VLC_BITS 9
00051
00052 #ifdef DV_CODEC_TINY_TARGET
00053 #define DV_VLC_MAP_RUN_SIZE 15
00054 #define DV_VLC_MAP_LEV_SIZE 23
00055 #else
00056 #define DV_VLC_MAP_RUN_SIZE 64
00057 #define DV_VLC_MAP_LEV_SIZE 512
00058 #endif
00059
00060
00061 static uint8_t** dv_anchor;
00062
00063
00064 static RL_VLC_ELEM *dv_rl_vlc;
00065
00066 static struct dv_vlc_pair {
00067 uint32_t vlc;
00068 uint8_t size;
00069 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL;
00070
00071 static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
00072 {
00073 int i, q, j;
00074
00075
00076 for(q = 0; q < 22; q++) {
00077
00078 for(i = 1; i < 64; i++) {
00079
00080 j = perm[i];
00081 s->dv_idct_shift[0][0][q][j] =
00082 dv_quant_shifts[q][dv_88_areas[i]] + 1;
00083 s->dv_idct_shift[1][0][q][j] = s->dv_idct_shift[0][0][q][j] + 1;
00084 }
00085
00086
00087 for(i = 1; i < 64; i++) {
00088
00089 s->dv_idct_shift[0][1][q][i] =
00090 dv_quant_shifts[q][dv_248_areas[i]] + 1;
00091 s->dv_idct_shift[1][1][q][i] = s->dv_idct_shift[0][1][q][i] + 1;
00092 }
00093 }
00094 }
00095
00096 static int dvvideo_init(AVCodecContext *avctx)
00097 {
00098 DVVideoContext *s = avctx->priv_data;
00099 DSPContext dsp;
00100 static int done=0;
00101 int i, j;
00102
00103 if (!done) {
00104 VLC dv_vlc;
00105 uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
00106 uint8_t new_dv_vlc_len[NB_DV_VLC*2];
00107 uint8_t new_dv_vlc_run[NB_DV_VLC*2];
00108 int16_t new_dv_vlc_level[NB_DV_VLC*2];
00109
00110 done = 1;
00111
00112 dv_vlc_map = av_mallocz(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
00113 if (!dv_vlc_map)
00114 return -ENOMEM;
00115
00116
00117 dv_anchor = av_malloc(12*27*sizeof(void*));
00118 if (!dv_anchor) {
00119 av_free(dv_vlc_map);
00120 return -ENOMEM;
00121 }
00122 for (i=0; i<12*27; i++)
00123 dv_anchor[i] = (void*)(size_t)i;
00124
00125
00126 for (i=0, j=0; i<NB_DV_VLC; i++, j++) {
00127 new_dv_vlc_bits[j] = dv_vlc_bits[i];
00128 new_dv_vlc_len[j] = dv_vlc_len[i];
00129 new_dv_vlc_run[j] = dv_vlc_run[i];
00130 new_dv_vlc_level[j] = dv_vlc_level[i];
00131
00132 if (dv_vlc_level[i]) {
00133 new_dv_vlc_bits[j] <<= 1;
00134 new_dv_vlc_len[j]++;
00135
00136 j++;
00137 new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
00138 new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
00139 new_dv_vlc_run[j] = dv_vlc_run[i];
00140 new_dv_vlc_level[j] = -dv_vlc_level[i];
00141 }
00142 }
00143
00144
00145
00146 init_vlc(&dv_vlc, TEX_VLC_BITS, j,
00147 new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
00148
00149 dv_rl_vlc = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
00150 if (!dv_rl_vlc) {
00151 av_free(dv_anchor);
00152 av_free(dv_vlc_map);
00153 return -ENOMEM;
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] = simple_idct248_put;
00229 memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
00230
00231
00232 dv_build_unquantize_tables(s, dsp.idct_permutation);
00233
00234
00235 if (dv_codec_profile(avctx))
00236 avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
00237 avctx->coded_frame = &s->picture;
00238
00239 return 0;
00240 }
00241
00242
00243
00244
00245 typedef struct BlockInfo {
00246 const uint8_t *shift_table;
00247 const uint8_t *scan_table;
00248 uint8_t pos;
00249 uint8_t dct_mode;
00250 uint8_t partial_bit_count;
00251 uint16_t partial_bit_buffer;
00252 int shift_offset;
00253 } BlockInfo;
00254
00255
00256 static const uint16_t block_sizes[6] = {
00257 112, 112, 112, 112, 80, 80
00258 };
00259
00260 static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
00261
00262 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
00263
00264 #ifndef ALT_BITSTREAM_READER
00265 #warning only works with ALT_BITSTREAM_READER
00266 static int re_index;
00267 #endif
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 int pos = mb->pos;
00291 int partial_bit_count = mb->partial_bit_count;
00292 int level, pos1, run, vlc_len, index;
00293
00294 OPEN_READER(re, gb);
00295 UPDATE_CACHE(re, gb);
00296
00297
00298 if (partial_bit_count > 0) {
00299 re_cache = ((unsigned)re_cache >> partial_bit_count) |
00300 (mb->partial_bit_buffer << (sizeof(re_cache)*8 - partial_bit_count));
00301 re_index -= partial_bit_count;
00302 mb->partial_bit_count = 0;
00303 }
00304
00305
00306 for(;;) {
00307 #ifdef VLC_DEBUG
00308 printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index);
00309 #endif
00310
00311 index = NEG_USR32(re_cache, TEX_VLC_BITS);
00312 vlc_len = dv_rl_vlc[index].len;
00313 if (vlc_len < 0) {
00314 index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
00315 vlc_len = TEX_VLC_BITS - vlc_len;
00316 }
00317 level = dv_rl_vlc[index].level;
00318 run = dv_rl_vlc[index].run;
00319
00320
00321 if (re_index + vlc_len > last_index) {
00322
00323 mb->partial_bit_count = last_index - re_index;
00324 mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
00325 re_index = last_index;
00326 break;
00327 }
00328 re_index += vlc_len;
00329
00330 #ifdef VLC_DEBUG
00331 printf("run=%d level=%d\n", run, level);
00332 #endif
00333 pos += run;
00334 if (pos >= 64)
00335 break;
00336
00337 if (level) {
00338 pos1 = scan_table[pos];
00339 block[pos1] = level << shift_table[pos1];
00340 }
00341
00342 UPDATE_CACHE(re, gb);
00343 }
00344 CLOSE_READER(re, gb);
00345 mb->pos = pos;
00346 }
00347
00348 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
00349 {
00350 int bits_left = get_bits_left(gb);
00351 while (bits_left >= 16) {
00352 put_bits(pb, 16, get_bits(gb, 16));
00353 bits_left -= 16;
00354 }
00355 if (bits_left > 0) {
00356 put_bits(pb, bits_left, get_bits(gb, bits_left));
00357 }
00358 }
00359
00360
00361 static inline void dv_decode_video_segment(DVVideoContext *s,
00362 uint8_t *buf_ptr1,
00363 const uint16_t *mb_pos_ptr)
00364 {
00365 int quant, dc, dct_mode, class1, j;
00366 int mb_index, mb_x, mb_y, v, last_index;
00367 DCTELEM *block, *block1;
00368 int c_offset;
00369 uint8_t *y_ptr;
00370 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
00371 uint8_t *buf_ptr;
00372 PutBitContext pb, vs_pb;
00373 GetBitContext gb;
00374 BlockInfo mb_data[5 * 6], *mb, *mb1;
00375 DCTELEM sblock[5*6][64] __align8;
00376 uint8_t mb_bit_buffer[80 + 4];
00377 uint8_t vs_bit_buffer[5 * 80 + 4];
00378
00379 memset(sblock, 0, sizeof(sblock));
00380
00381
00382 buf_ptr = buf_ptr1;
00383 block1 = &sblock[0][0];
00384 mb1 = mb_data;
00385 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
00386 for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) {
00387
00388 quant = buf_ptr[3] & 0x0f;
00389 buf_ptr += 4;
00390 init_put_bits(&pb, mb_bit_buffer, 80);
00391 mb = mb1;
00392 block = block1;
00393 for(j = 0;j < 6; j++) {
00394 last_index = block_sizes[j];
00395 init_get_bits(&gb, buf_ptr, last_index);
00396
00397
00398 dc = get_sbits(&gb, 9);
00399 dct_mode = get_bits1(&gb);
00400 mb->dct_mode = dct_mode;
00401 mb->scan_table = s->dv_zigzag[dct_mode];
00402 class1 = get_bits(&gb, 2);
00403 mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
00404 [quant + dv_quant_offset[class1]];
00405 dc = dc << 2;
00406
00407
00408 dc += 1024;
00409 block[0] = dc;
00410 buf_ptr += last_index >> 3;
00411 mb->pos = 0;
00412 mb->partial_bit_count = 0;
00413
00414 #ifdef VLC_DEBUG
00415 printf("MB block: %d, %d ", mb_index, j);
00416 #endif
00417 dv_decode_ac(&gb, mb, block);
00418
00419
00420
00421 if (mb->pos >= 64)
00422 bit_copy(&pb, &gb);
00423
00424 block += 64;
00425 mb++;
00426 }
00427
00428
00429 #ifdef VLC_DEBUG
00430 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
00431 #endif
00432 block = block1;
00433 mb = mb1;
00434 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
00435 flush_put_bits(&pb);
00436 for(j = 0;j < 6; j++, block += 64, mb++) {
00437 if (mb->pos < 64 && get_bits_left(&gb) > 0) {
00438 dv_decode_ac(&gb, mb, block);
00439
00440 if (mb->pos < 64)
00441 break;
00442 }
00443 }
00444
00445
00446 if (j >= 6)
00447 bit_copy(&vs_pb, &gb);
00448 }
00449
00450
00451 #ifdef VLC_DEBUG
00452 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
00453 #endif
00454 block = &sblock[0][0];
00455 mb = mb_data;
00456 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
00457 flush_put_bits(&vs_pb);
00458 for(mb_index = 0; mb_index < 5; mb_index++) {
00459 for(j = 0;j < 6; j++) {
00460 if (mb->pos < 64) {
00461 #ifdef VLC_DEBUG
00462 printf("start %d:%d\n", mb_index, j);
00463 #endif
00464 dv_decode_ac(&gb, mb, block);
00465 }
00466 if (mb->pos >= 64 && mb->pos < 127)
00467 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
00468 block += 64;
00469 mb++;
00470 }
00471 }
00472
00473
00474 block = &sblock[0][0];
00475 mb = mb_data;
00476 for(mb_index = 0; mb_index < 5; mb_index++) {
00477 v = *mb_pos_ptr++;
00478 mb_x = v & 0xff;
00479 mb_y = v >> 8;
00480 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
00481 if (s->sys->pix_fmt == PIX_FMT_YUV411P)
00482 c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
00483 else
00484 c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
00485 for(j = 0;j < 6; j++) {
00486 idct_put = s->idct_put[mb->dct_mode];
00487 if (j < 4) {
00488 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
00489
00490 idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
00491 } else {
00492 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
00493 s->picture.linesize[0], block);
00494 }
00495 } else {
00496 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
00497 uint64_t aligned_pixels[64/8];
00498 uint8_t *pixels= (uint8_t*)aligned_pixels;
00499 uint8_t *c_ptr, *c_ptr1, *ptr;
00500 int y, linesize;
00501
00502 idct_put(pixels, 8, block);
00503 linesize = s->picture.linesize[6 - j];
00504 c_ptr = s->picture.data[6 - j] + c_offset;
00505 ptr = pixels;
00506 for(y = 0;y < 8; y++) {
00507
00508 c_ptr1 = c_ptr + 8*linesize;
00509 c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
00510 c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
00511 c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
00512 c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
00513 c_ptr += linesize;
00514 ptr += 8;
00515 }
00516 } else {
00517
00518 idct_put(s->picture.data[6 - j] + c_offset,
00519 s->picture.linesize[6 - j], block);
00520 }
00521 }
00522 block += 64;
00523 mb++;
00524 }
00525 }
00526 }
00527
00528 #ifdef DV_CODEC_TINY_TARGET
00529
00530 static always_inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
00531 {
00532 int sign = l >> 8;
00533 int level = (l ^ sign) - sign;
00534 int size;
00535
00536 sign = (sign & 1);
00537
00538 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
00539 *vlc = dv_vlc_map[run][level].vlc | sign;
00540 size = dv_vlc_map[run][level].size;
00541 }
00542 else {
00543 if (level < DV_VLC_MAP_LEV_SIZE) {
00544 *vlc = dv_vlc_map[0][level].vlc | sign;
00545 size = dv_vlc_map[0][level].size;
00546 } else {
00547 *vlc = 0xfe00 | (level << 1) | sign;
00548 size = 16;
00549 }
00550 if (run) {
00551 *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
00552 (0x1f80 | (run - 1))) << size;
00553 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
00554 }
00555 }
00556
00557 return size;
00558 }
00559
00560 static always_inline int dv_rl2vlc_size(int run, int l)
00561 {
00562 int level = (l ^ (l >> 8)) - (l >> 8);
00563 int size;
00564
00565 if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
00566 size = dv_vlc_map[run][level].size;
00567 }
00568 else {
00569 size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
00570 if (run) {
00571 size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
00572 }
00573 }
00574 return size;
00575 }
00576 #else
00577 static always_inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
00578 {
00579 *vlc = dv_vlc_map[run][((uint16_t)l)&0x1ff].vlc;
00580 return dv_vlc_map[run][((uint16_t)l)&0x1ff].size;
00581 }
00582
00583 static always_inline int dv_rl2vlc_size(int run, int l)
00584 {
00585 return dv_vlc_map[run][((uint16_t)l)&0x1ff].size;
00586 }
00587 #endif
00588
00589 typedef struct EncBlockInfo {
00590 int area_q[4];
00591 int bit_size[4];
00592 int prev_run[4];
00593 int cur_ac;
00594 int cno;
00595 int dct_mode;
00596 DCTELEM *mb;
00597 uint8_t partial_bit_count;
00598 uint32_t partial_bit_buffer;
00599 } EncBlockInfo;
00600
00601 static always_inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
00602 int pb_size)
00603 {
00604 int run;
00605 int bits_left;
00606 PutBitContext* pb = pb_pool;
00607 int size = bi->partial_bit_count;
00608 uint32_t vlc = bi->partial_bit_buffer;
00609
00610 bi->partial_bit_count = bi->partial_bit_buffer = 0;
00611 vlc_loop:
00612
00613 for (; size > (bits_left = put_bits_left(pb)); pb++) {
00614 if (bits_left) {
00615 size -= bits_left;
00616 put_bits(pb, bits_left, vlc >> size);
00617 vlc = vlc & ((1<<size)-1);
00618 }
00619 if (pb_size == 1) {
00620 bi->partial_bit_count = size;
00621 bi->partial_bit_buffer = vlc;
00622 return;
00623 }
00624 --pb_size;
00625 }
00626
00627
00628 put_bits(pb, size, vlc);
00629
00630
00631 run = 0;
00632 for (; bi->cur_ac < 64; bi->cur_ac++, run++) {
00633 if (bi->mb[bi->cur_ac]) {
00634 size = dv_rl2vlc(run, bi->mb[bi->cur_ac], &vlc);
00635 bi->cur_ac++;
00636 goto vlc_loop;
00637 }
00638 }
00639
00640 if (bi->cur_ac == 64) {
00641 size = 4; vlc = 6;
00642 bi->cur_ac++;
00643 goto vlc_loop;
00644 }
00645 }
00646
00647 static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
00648 const uint8_t* zigzag_scan, int bias)
00649 {
00650 int i, area;
00651 int run;
00652 int classes[] = {12, 24, 36, 0xffff};
00653
00654 run = 0;
00655 bi->mb[0] = blk[0];
00656 bi->cno = 0;
00657 for (area = 0; area < 4; area++) {
00658 bi->prev_run[area] = run;
00659 bi->bit_size[area] = 0;
00660 for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
00661 bi->mb[i] = (blk[zigzag_scan[i]] / 16);
00662 while ((bi->mb[i] ^ (bi->mb[i] >> 8)) > classes[bi->cno])
00663 bi->cno++;
00664
00665 if (bi->mb[i]) {
00666 bi->bit_size[area] += dv_rl2vlc_size(run, bi->mb[i]);
00667 run = 0;
00668 } else
00669 ++run;
00670 }
00671 }
00672 bi->bit_size[3] += 4;
00673 bi->cno += bias;
00674
00675 if (bi->cno >= 3) {
00676 bi->cno = 3;
00677 for (i=1; i<64; i++)
00678 bi->mb[i] /= 2;
00679 }
00680 }
00681
00682 #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
00683 static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
00684 DCTELEM *s;
00685 int score88 = 0;
00686 int score248 = 0;
00687 int i;
00688
00689
00690 s = blk;
00691 for(i=0; i<7; i++) {
00692 score88 += SC(0, 8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
00693 SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
00694 s += 8;
00695 }
00696
00697 s = blk;
00698 for(i=0; i<6; i++) {
00699 score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
00700 SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
00701 s += 8;
00702 }
00703
00704 return (score88 - score248 > -10);
00705 }
00706
00707 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
00708 {
00709 int size[5];
00710 int i, j, k, a, run;
00711 EncBlockInfo* b;
00712
00713 do {
00714 b = blks;
00715 for (i=0; i<5; i++) {
00716 if (!qnos[i])
00717 continue;
00718
00719 qnos[i]--;
00720 size[i] = 0;
00721 for (j=0; j<6; j++, b++) {
00722 for (a=0; a<4; a++) {
00723 if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
00724 b->bit_size[a] = (a==3)?4:0;
00725 b->area_q[a]++;
00726 run = b->prev_run[a];
00727 for (k=mb_area_start[a]; k<mb_area_start[a+1]; k++) {
00728 b->mb[k] /= 2;
00729 if (b->mb[k]) {
00730 b->bit_size[a] += dv_rl2vlc_size(run, b->mb[k]);
00731 run = 0;
00732 } else
00733 ++run;
00734 }
00735 }
00736 size[i] += b->bit_size[a];
00737 }
00738 }
00739 }
00740 } while ((vs_total_ac_bits < size[0] + size[1] + size[2] + size[3] + size[4]) &&
00741 (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]));
00742 }
00743
00744
00745
00746
00747
00748
00749 static inline void dv_encode_video_segment(DVVideoContext *s,
00750 uint8_t *dif,
00751 const uint16_t *mb_pos_ptr)
00752 {
00753 int mb_index, i, j, v;
00754 int mb_x, mb_y, c_offset, linesize;
00755 uint8_t* y_ptr;
00756 uint8_t* data;
00757 uint8_t* ptr;
00758 int do_edge_wrap;
00759 DCTELEM block[64] __align8;
00760 DCTELEM sblock[5*6][64] __align8;
00761 EncBlockInfo enc_blks[5*6];
00762 PutBitContext pbs[5*6];
00763 PutBitContext* pb;
00764 EncBlockInfo* enc_blk;
00765 int vs_bit_size = 0;
00766 int qnos[5];
00767
00768 enc_blk = &enc_blks[0];
00769 pb = &pbs[0];
00770 for(mb_index = 0; mb_index < 5; mb_index++) {
00771 v = *mb_pos_ptr++;
00772 mb_x = v & 0xff;
00773 mb_y = v >> 8;
00774 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
00775 c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
00776 ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
00777 (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
00778 do_edge_wrap = 0;
00779 qnos[mb_index] = 15;
00780 ptr = dif + mb_index*80 + 4;
00781 for(j = 0;j < 6; j++) {
00782 if (j < 4) {
00783
00784 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
00785 data = y_ptr + (j * 8);
00786 } else {
00787 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
00788 }
00789 linesize = s->picture.linesize[0];
00790 } else {
00791
00792 data = s->picture.data[6 - j] + c_offset;
00793 linesize = s->picture.linesize[6 - j];
00794 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
00795 do_edge_wrap = 1;
00796 }
00797
00798
00799 if (do_edge_wrap) {
00800 uint8_t* d;
00801 DCTELEM *b = block;
00802 for (i=0;i<8;i++) {
00803 d = data + 8 * linesize;
00804 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
00805 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
00806 data += linesize;
00807 b += 8;
00808 }
00809 } else {
00810 s->get_pixels(block, data, linesize);
00811 }
00812
00813 enc_blk->dct_mode = dv_guess_dct_mode(block);
00814 enc_blk->mb = &sblock[mb_index*6+j][0];
00815 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
00816 enc_blk->partial_bit_count = 0;
00817 enc_blk->partial_bit_buffer = 0;
00818 enc_blk->cur_ac = 1;
00819
00820 s->fdct[enc_blk->dct_mode](block);
00821
00822 dv_set_class_number(block, enc_blk,
00823 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
00824 j/4*(j%2));
00825
00826 init_put_bits(pb, ptr, block_sizes[j]/8);
00827 put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024) >> 2));
00828 put_bits(pb, 1, enc_blk->dct_mode);
00829 put_bits(pb, 2, enc_blk->cno);
00830
00831 vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
00832 enc_blk->bit_size[2] + enc_blk->bit_size[3];
00833 ++enc_blk;
00834 ++pb;
00835 ptr += block_sizes[j]/8;
00836 }
00837 }
00838
00839 if (vs_total_ac_bits < vs_bit_size)
00840 dv_guess_qnos(&enc_blks[0], &qnos[0]);
00841
00842 for (i=0; i<5; i++) {
00843 dif[i*80 + 3] = qnos[i];
00844 }
00845
00846
00847 for (j=0; j<5*6; j++)
00848 dv_encode_ac(&enc_blks[j], &pbs[j], 1);
00849
00850
00851 for (j=0; j<5*6; j++) {
00852 if (enc_blks[j].cur_ac < 65 || enc_blks[j].partial_bit_count)
00853 dv_encode_ac(&enc_blks[j], &pbs[(j/6)*6], 6);
00854 }
00855
00856
00857 for (j=0; j<5*6; j++) {
00858 if (enc_blks[j].cur_ac < 65 || enc_blks[j].partial_bit_count)
00859 dv_encode_ac(&enc_blks[j], &pbs[0], 6*5);
00860 }
00861
00862 for (j=0; j<5*6; j++)
00863 flush_put_bits(&pbs[j]);
00864 }
00865
00866 static int dv_decode_mt(AVCodecContext *avctx, void* sl)
00867 {
00868 DVVideoContext *s = avctx->priv_data;
00869 int slice = (size_t)sl;
00870 dv_decode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80],
00871 &s->sys->video_place[slice*5]);
00872 return 0;
00873 }
00874
00875 static int dv_encode_mt(AVCodecContext *avctx, void* sl)
00876 {
00877 DVVideoContext *s = avctx->priv_data;
00878 int slice = (size_t)sl;
00879 dv_encode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80],
00880 &s->sys->video_place[slice*5]);
00881 return 0;
00882 }
00883
00884
00885
00886 static int dvvideo_decode_frame(AVCodecContext *avctx,
00887 void *data, int *data_size,
00888 uint8_t *buf, int buf_size)
00889 {
00890 DVVideoContext *s = avctx->priv_data;
00891
00892 s->sys = dv_frame_profile(buf);
00893 if (!s->sys || buf_size < s->sys->frame_size)
00894 return -1;
00895
00896 if(s->picture.data[0])
00897 avctx->release_buffer(avctx, &s->picture);
00898
00899 s->picture.reference = 0;
00900 avctx->pix_fmt = s->sys->pix_fmt;
00901 avctx->width = s->sys->width;
00902 avctx->height = s->sys->height;
00903 if(avctx->get_buffer(avctx, &s->picture) < 0) {
00904 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00905 return -1;
00906 }
00907 s->picture.interlaced_frame = 1;
00908 s->picture.top_field_first = 0;
00909
00910 s->buf = buf;
00911 avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
00912 s->sys->difseg_size * 27);
00913
00914 emms_c();
00915
00916
00917 *data_size = sizeof(AVFrame);
00918 *(AVFrame*)data= s->picture;
00919
00920 return s->sys->frame_size;
00921 }
00922
00923 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
00924 void *data)
00925 {
00926 DVVideoContext *s = c->priv_data;
00927
00928 s->sys = dv_codec_profile(c);
00929 if (!s->sys)
00930 return -1;
00931 if(buf_size < s->sys->frame_size)
00932 return -1;
00933
00934 c->pix_fmt = s->sys->pix_fmt;
00935 s->picture = *((AVFrame *)data);
00936
00937 s->buf = buf;
00938 c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
00939 s->sys->difseg_size * 27);
00940
00941 emms_c();
00942 return s->sys->frame_size;
00943 }
00944
00945 #ifdef CONFIG_DVVIDEO_ENCODER
00946 AVCodec dvvideo_encoder = {
00947 "dvvideo",
00948 CODEC_TYPE_VIDEO,
00949 CODEC_ID_DVVIDEO,
00950 sizeof(DVVideoContext),
00951 dvvideo_init,
00952 dvvideo_encode_frame,
00953 NULL,
00954 NULL,
00955 CODEC_CAP_DR1,
00956 NULL
00957 };
00958 #endif // CONFIG_DVVIDEO_ENCODER
00959
00960 AVCodec dvvideo_decoder = {
00961 "dvvideo",
00962 CODEC_TYPE_VIDEO,
00963 CODEC_ID_DVVIDEO,
00964 sizeof(DVVideoContext),
00965 dvvideo_init,
00966 NULL,
00967 NULL,
00968 dvvideo_decode_frame,
00969 CODEC_CAP_DR1,
00970 NULL
00971 };