Mercurial > libavcodec.hg
comparison dv.c @ 1887:85fe2f4633ec libavcodec
* DV decoding/encoding now supports MultiThreading for up to 324 CPUs ;-)
| author | romansh |
|---|---|
| date | Sun, 14 Mar 2004 23:45:42 +0000 |
| parents | fbcf02596520 |
| children | e5687117cc7f |
comparison
equal
deleted
inserted
replaced
| 1886:fbcf02596520 | 1887:85fe2f4633ec |
|---|---|
| 31 #include "dsputil.h" | 31 #include "dsputil.h" |
| 32 #include "mpegvideo.h" | 32 #include "mpegvideo.h" |
| 33 #include "simple_idct.h" | 33 #include "simple_idct.h" |
| 34 #include "dvdata.h" | 34 #include "dvdata.h" |
| 35 | 35 |
| 36 typedef struct DVVideoDecodeContext { | 36 typedef struct DVVideoContext { |
| 37 const DVprofile* sys; | 37 const DVprofile* sys; |
| 38 AVFrame picture; | 38 AVFrame picture; |
| 39 uint8_t *buf; | |
| 39 | 40 |
| 40 uint8_t dv_zigzag[2][64]; | 41 uint8_t dv_zigzag[2][64]; |
| 41 uint8_t dv_idct_shift[2][22][64]; | 42 uint8_t dv_idct_shift[2][22][64]; |
| 42 | 43 |
| 43 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size); | 44 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size); |
| 44 void (*fdct[2])(DCTELEM *block); | 45 void (*fdct[2])(DCTELEM *block); |
| 45 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block); | 46 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block); |
| 46 | 47 } DVVideoContext; |
| 47 GetBitContext gb; | |
| 48 DCTELEM block[5*6][64] __align8; | |
| 49 } DVVideoDecodeContext; | |
| 50 | 48 |
| 51 #define TEX_VLC_BITS 9 | 49 #define TEX_VLC_BITS 9 |
| 52 | 50 |
| 53 #ifdef DV_CODEC_TINY_TARGET | 51 #ifdef DV_CODEC_TINY_TARGET |
| 54 #define DV_VLC_MAP_RUN_SIZE 15 | 52 #define DV_VLC_MAP_RUN_SIZE 15 |
| 55 #define DV_VLC_MAP_LEV_SIZE 23 | 53 #define DV_VLC_MAP_LEV_SIZE 23 |
| 56 #else | 54 #else |
| 57 #define DV_VLC_MAP_RUN_SIZE 64 | 55 #define DV_VLC_MAP_RUN_SIZE 64 |
| 58 #define DV_VLC_MAP_LEV_SIZE 512 | 56 #define DV_VLC_MAP_LEV_SIZE 512 |
| 59 #endif | 57 #endif |
| 58 | |
| 59 /* MultiThreading */ | |
| 60 static uint8_t** dv_anchor; | |
| 60 | 61 |
| 61 /* XXX: also include quantization */ | 62 /* XXX: also include quantization */ |
| 62 static RL_VLC_ELEM *dv_rl_vlc; | 63 static RL_VLC_ELEM *dv_rl_vlc; |
| 63 /* VLC encoding lookup table */ | 64 /* VLC encoding lookup table */ |
| 64 static struct dv_vlc_pair { | 65 static struct dv_vlc_pair { |
| 65 uint32_t vlc; | 66 uint32_t vlc; |
| 66 uint8_t size; | 67 uint8_t size; |
| 67 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL; | 68 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL; |
| 68 | 69 |
| 69 static void dv_build_unquantize_tables(DVVideoDecodeContext *s, uint8_t* perm) | 70 static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm) |
| 70 { | 71 { |
| 71 int i, q, j; | 72 int i, q, j; |
| 72 | 73 |
| 73 /* NOTE: max left shift is 6 */ | 74 /* NOTE: max left shift is 6 */ |
| 74 for(q = 0; q < 22; q++) { | 75 for(q = 0; q < 22; q++) { |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 92 static int dvvideo_init(AVCodecContext *avctx) | 93 static int dvvideo_init(AVCodecContext *avctx) |
| 93 { | 94 { |
| 94 DVVideoDecodeContext *s = avctx->priv_data; | 95 DVVideoContext *s = avctx->priv_data; |
| 95 DSPContext dsp; | 96 DSPContext dsp; |
| 96 static int done=0; | 97 static int done=0; |
| 97 int i, j; | 98 int i, j; |
| 98 | 99 |
| 99 if (!done) { | 100 if (!done) { |
| 104 | 105 |
| 105 dv_vlc_map = av_mallocz(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair)); | 106 dv_vlc_map = av_mallocz(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair)); |
| 106 if (!dv_vlc_map) | 107 if (!dv_vlc_map) |
| 107 return -ENOMEM; | 108 return -ENOMEM; |
| 108 | 109 |
| 110 dv_anchor = av_malloc(12*27*sizeof(void*)); | |
| 111 if (!dv_anchor) { | |
| 112 av_free(dv_vlc_map); | |
| 113 return -ENOMEM; | |
| 114 } | |
| 115 for (i=0; i<12*27; i++) | |
| 116 (int)dv_anchor[i] = i; | |
| 117 | |
| 109 /* NOTE: as a trick, we use the fact the no codes are unused | 118 /* NOTE: as a trick, we use the fact the no codes are unused |
| 110 to accelerate the parsing of partial codes */ | 119 to accelerate the parsing of partial codes */ |
| 111 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, | 120 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, |
| 112 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); | 121 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2); |
| 113 | 122 |
| 114 dv_rl_vlc = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); | 123 dv_rl_vlc = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM)); |
| 115 if (!dv_rl_vlc) { | 124 if (!dv_rl_vlc) { |
| 125 av_free(dv_anchor); | |
| 116 av_free(dv_vlc_map); | 126 av_free(dv_vlc_map); |
| 117 return -ENOMEM; | 127 return -ENOMEM; |
| 118 } | 128 } |
| 119 for(i = 0; i < dv_vlc.table_size; i++){ | 129 for(i = 0; i < dv_vlc.table_size; i++){ |
| 120 int code= dv_vlc.table[i][0]; | 130 int code= dv_vlc.table[i][0]; |
| 197 avctx->coded_frame = &s->picture; | 207 avctx->coded_frame = &s->picture; |
| 198 | 208 |
| 199 return 0; | 209 return 0; |
| 200 } | 210 } |
| 201 | 211 |
| 212 static int dvvideo_end(AVCodecContext *avctx) | |
| 213 { | |
| 214 avcodec_default_free_buffers(avctx); | |
| 215 return 0; | |
| 216 } | |
| 217 | |
| 202 // #define VLC_DEBUG | 218 // #define VLC_DEBUG |
| 203 | 219 |
| 204 typedef struct BlockInfo { | 220 typedef struct BlockInfo { |
| 205 const uint8_t *shift_table; | 221 const uint8_t *shift_table; |
| 206 const uint8_t *scan_table; | 222 const uint8_t *scan_table; |
| 223 #ifndef ALT_BITSTREAM_READER | 239 #ifndef ALT_BITSTREAM_READER |
| 224 #warning only works with ALT_BITSTREAM_READER | 240 #warning only works with ALT_BITSTREAM_READER |
| 225 #endif | 241 #endif |
| 226 | 242 |
| 227 /* decode ac coefs */ | 243 /* decode ac coefs */ |
| 228 static void dv_decode_ac(DVVideoDecodeContext *s, BlockInfo *mb, DCTELEM *block) | 244 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block) |
| 229 { | 245 { |
| 230 int last_index = get_bits_size(&s->gb); | 246 int last_index = get_bits_size(gb); |
| 231 int last_re_index; | 247 int last_re_index; |
| 232 int shift_offset = mb->shift_offset; | 248 int shift_offset = mb->shift_offset; |
| 233 const uint8_t *scan_table = mb->scan_table; | 249 const uint8_t *scan_table = mb->scan_table; |
| 234 const uint8_t *shift_table = mb->shift_table; | 250 const uint8_t *shift_table = mb->shift_table; |
| 235 int pos = mb->pos; | 251 int pos = mb->pos; |
| 238 int sign = 0; | 254 int sign = 0; |
| 239 #ifndef ALT_BITSTREAM_READER //FIXME | 255 #ifndef ALT_BITSTREAM_READER //FIXME |
| 240 int re_index=0; | 256 int re_index=0; |
| 241 int re1_index=0; | 257 int re1_index=0; |
| 242 #endif | 258 #endif |
| 243 OPEN_READER(re, &s->gb); | 259 OPEN_READER(re, gb); |
| 244 | 260 |
| 245 #ifdef VLC_DEBUG | 261 #ifdef VLC_DEBUG |
| 246 printf("start\n"); | 262 printf("start\n"); |
| 247 #endif | 263 #endif |
| 248 | 264 |
| 254 int l; | 270 int l; |
| 255 GetBitContext gb1; | 271 GetBitContext gb1; |
| 256 | 272 |
| 257 /* build the dummy bit buffer */ | 273 /* build the dummy bit buffer */ |
| 258 l = 16 - partial_bit_count; | 274 l = 16 - partial_bit_count; |
| 259 UPDATE_CACHE(re, &s->gb); | 275 UPDATE_CACHE(re, gb); |
| 260 #ifdef VLC_DEBUG | 276 #ifdef VLC_DEBUG |
| 261 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16)); | 277 printf("show=%04x\n", SHOW_UBITS(re, gb, 16)); |
| 262 #endif | 278 #endif |
| 263 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l); | 279 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, gb, l); |
| 264 buf[0] = v >> 8; | 280 buf[0] = v >> 8; |
| 265 buf[1] = v; | 281 buf[1] = v; |
| 266 #ifdef VLC_DEBUG | 282 #ifdef VLC_DEBUG |
| 267 printf("v=%04x cnt=%d %04x\n", | 283 printf("v=%04x cnt=%d %04x\n", |
| 268 v, partial_bit_count, (mb->partial_bit_buffer << l)); | 284 v, partial_bit_count, (mb->partial_bit_buffer << l)); |
| 292 last_re_index = 0; /* avoid warning */ | 308 last_re_index = 0; /* avoid warning */ |
| 293 re_index += l; | 309 re_index += l; |
| 294 /* by definition, if we can read the vlc, all partial bits | 310 /* by definition, if we can read the vlc, all partial bits |
| 295 will be read (otherwise we could have read the vlc before) */ | 311 will be read (otherwise we could have read the vlc before) */ |
| 296 mb->partial_bit_count = 0; | 312 mb->partial_bit_count = 0; |
| 297 UPDATE_CACHE(re, &s->gb); | 313 UPDATE_CACHE(re, gb); |
| 298 goto handle_vlc; | 314 goto handle_vlc; |
| 299 } | 315 } |
| 300 | 316 |
| 301 /* get the AC coefficients until last_index is reached */ | 317 /* get the AC coefficients until last_index is reached */ |
| 302 for(;;) { | 318 for(;;) { |
| 303 UPDATE_CACHE(re, &s->gb); | 319 UPDATE_CACHE(re, gb); |
| 304 #ifdef VLC_DEBUG | 320 #ifdef VLC_DEBUG |
| 305 printf("%2d: bits=%04x index=%d\n", | 321 printf("%2d: bits=%04x index=%d\n", |
| 306 pos, SHOW_UBITS(re, &s->gb, 16), re_index); | 322 pos, SHOW_UBITS(re, gb, 16), re_index); |
| 307 #endif | 323 #endif |
| 308 last_re_index = re_index; | 324 last_re_index = re_index; |
| 309 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc, | 325 GET_RL_VLC(level, run, re, gb, dv_rl_vlc, |
| 310 TEX_VLC_BITS, 2); | 326 TEX_VLC_BITS, 2); |
| 311 handle_vlc: | 327 handle_vlc: |
| 312 #ifdef VLC_DEBUG | 328 #ifdef VLC_DEBUG |
| 313 printf("run=%d level=%d\n", run, level); | 329 printf("run=%d level=%d\n", run, level); |
| 314 #endif | 330 #endif |
| 315 if (level) { | 331 if (level) { |
| 316 sign = SHOW_SBITS(re, &s->gb, 1); | 332 sign = SHOW_SBITS(re, gb, 1); |
| 317 LAST_SKIP_BITS(re, &s->gb, 1); | 333 LAST_SKIP_BITS(re, gb, 1); |
| 318 } | 334 } |
| 319 if (re_index > last_index) { | 335 if (re_index > last_index) { |
| 320 /* should be < 16 bits otherwise a codeword could have been parsed */ | 336 /* should be < 16 bits otherwise a codeword could have been parsed */ |
| 321 re_index = last_re_index; | 337 re_index = last_re_index; |
| 322 UPDATE_CACHE(re, &s->gb); | 338 UPDATE_CACHE(re, gb); |
| 323 mb->partial_bit_count = last_index - re_index; | 339 mb->partial_bit_count = last_index - re_index; |
| 324 mb->partial_bit_buffer = SHOW_UBITS(re, &s->gb, mb->partial_bit_count); | 340 mb->partial_bit_buffer = SHOW_UBITS(re, gb, mb->partial_bit_count); |
| 325 re_index = last_index; | 341 re_index = last_index; |
| 326 break; | 342 break; |
| 327 } | 343 } |
| 328 | 344 |
| 329 pos += run; | 345 pos += run; |
| 336 level = level << (shift_table[pos1] + shift_offset); | 352 level = level << (shift_table[pos1] + shift_offset); |
| 337 block[pos1] = level; | 353 block[pos1] = level; |
| 338 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); | 354 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]); |
| 339 } | 355 } |
| 340 } | 356 } |
| 341 CLOSE_READER(re, &s->gb); | 357 CLOSE_READER(re, gb); |
| 342 mb->pos = pos; | 358 mb->pos = pos; |
| 343 } | 359 } |
| 344 | 360 |
| 345 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb) | 361 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb) |
| 346 { | 362 { |
| 353 put_bits(pb, bits_left, get_bits(gb, bits_left)); | 369 put_bits(pb, bits_left, get_bits(gb, bits_left)); |
| 354 } | 370 } |
| 355 } | 371 } |
| 356 | 372 |
| 357 /* mb_x and mb_y are in units of 8 pixels */ | 373 /* mb_x and mb_y are in units of 8 pixels */ |
| 358 static inline void dv_decode_video_segment(DVVideoDecodeContext *s, | 374 static inline void dv_decode_video_segment(DVVideoContext *s, |
| 359 uint8_t *buf_ptr1, | 375 uint8_t *buf_ptr1, |
| 360 const uint16_t *mb_pos_ptr) | 376 const uint16_t *mb_pos_ptr) |
| 361 { | 377 { |
| 362 int quant, dc, dct_mode, class1, j; | 378 int quant, dc, dct_mode, class1, j; |
| 363 int mb_index, mb_x, mb_y, v, last_index; | 379 int mb_index, mb_x, mb_y, v, last_index; |
| 364 DCTELEM *block, *block1; | 380 DCTELEM *block, *block1; |
| 365 int c_offset; | 381 int c_offset; |
| 366 uint8_t *y_ptr; | 382 uint8_t *y_ptr; |
| 367 BlockInfo mb_data[5 * 6], *mb, *mb1; | |
| 368 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); | 383 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block); |
| 369 uint8_t *buf_ptr; | 384 uint8_t *buf_ptr; |
| 370 PutBitContext pb, vs_pb; | 385 PutBitContext pb, vs_pb; |
| 386 GetBitContext gb; | |
| 387 BlockInfo mb_data[5 * 6], *mb, *mb1; | |
| 388 DCTELEM sblock[5*6][64] __align8; | |
| 371 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ | 389 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */ |
| 372 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ | 390 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */ |
| 373 | 391 |
| 374 memset(s->block, 0, sizeof(s->block)); | 392 memset(sblock, 0, sizeof(sblock)); |
| 375 | 393 |
| 376 /* pass 1 : read DC and AC coefficients in blocks */ | 394 /* pass 1 : read DC and AC coefficients in blocks */ |
| 377 buf_ptr = buf_ptr1; | 395 buf_ptr = buf_ptr1; |
| 378 block1 = &s->block[0][0]; | 396 block1 = &sblock[0][0]; |
| 379 mb1 = mb_data; | 397 mb1 = mb_data; |
| 380 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80); | 398 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80); |
| 381 for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) { | 399 for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) { |
| 382 /* skip header */ | 400 /* skip header */ |
| 383 quant = buf_ptr[3] & 0x0f; | 401 quant = buf_ptr[3] & 0x0f; |
| 385 init_put_bits(&pb, mb_bit_buffer, 80); | 403 init_put_bits(&pb, mb_bit_buffer, 80); |
| 386 mb = mb1; | 404 mb = mb1; |
| 387 block = block1; | 405 block = block1; |
| 388 for(j = 0;j < 6; j++) { | 406 for(j = 0;j < 6; j++) { |
| 389 last_index = block_sizes[j]; | 407 last_index = block_sizes[j]; |
| 390 init_get_bits(&s->gb, buf_ptr, last_index); | 408 init_get_bits(&gb, buf_ptr, last_index); |
| 391 | 409 |
| 392 /* get the dc */ | 410 /* get the dc */ |
| 393 dc = get_bits(&s->gb, 9); | 411 dc = get_bits(&gb, 9); |
| 394 dc = (dc << (32 - 9)) >> (32 - 9); | 412 dc = (dc << (32 - 9)) >> (32 - 9); |
| 395 dct_mode = get_bits1(&s->gb); | 413 dct_mode = get_bits1(&gb); |
| 396 mb->dct_mode = dct_mode; | 414 mb->dct_mode = dct_mode; |
| 397 mb->scan_table = s->dv_zigzag[dct_mode]; | 415 mb->scan_table = s->dv_zigzag[dct_mode]; |
| 398 class1 = get_bits(&s->gb, 2); | 416 class1 = get_bits(&gb, 2); |
| 399 mb->shift_offset = (class1 == 3); | 417 mb->shift_offset = (class1 == 3); |
| 400 mb->shift_table = s->dv_idct_shift[dct_mode] | 418 mb->shift_table = s->dv_idct_shift[dct_mode] |
| 401 [quant + dv_quant_offset[class1]]; | 419 [quant + dv_quant_offset[class1]]; |
| 402 dc = dc << 2; | 420 dc = dc << 2; |
| 403 /* convert to unsigned because 128 is not added in the | 421 /* convert to unsigned because 128 is not added in the |
| 409 mb->partial_bit_count = 0; | 427 mb->partial_bit_count = 0; |
| 410 | 428 |
| 411 #ifdef VLC_DEBUG | 429 #ifdef VLC_DEBUG |
| 412 printf("MB block: %d, %d ", mb_index, j); | 430 printf("MB block: %d, %d ", mb_index, j); |
| 413 #endif | 431 #endif |
| 414 dv_decode_ac(s, mb, block); | 432 dv_decode_ac(&gb, mb, block); |
| 415 | 433 |
| 416 /* write the remaining bits in a new buffer only if the | 434 /* write the remaining bits in a new buffer only if the |
| 417 block is finished */ | 435 block is finished */ |
| 418 if (mb->pos >= 64) | 436 if (mb->pos >= 64) |
| 419 bit_copy(&pb, &s->gb); | 437 bit_copy(&pb, &gb); |
| 420 | 438 |
| 421 block += 64; | 439 block += 64; |
| 422 mb++; | 440 mb++; |
| 423 } | 441 } |
| 424 | 442 |
| 426 #ifdef VLC_DEBUG | 444 #ifdef VLC_DEBUG |
| 427 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index); | 445 printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index); |
| 428 #endif | 446 #endif |
| 429 block = block1; | 447 block = block1; |
| 430 mb = mb1; | 448 mb = mb1; |
| 431 init_get_bits(&s->gb, mb_bit_buffer, put_bits_count(&pb)); | 449 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb)); |
| 432 flush_put_bits(&pb); | 450 flush_put_bits(&pb); |
| 433 for(j = 0;j < 6; j++, block += 64, mb++) { | 451 for(j = 0;j < 6; j++, block += 64, mb++) { |
| 434 if (mb->pos < 64 && get_bits_left(&s->gb) > 0) { | 452 if (mb->pos < 64 && get_bits_left(&gb) > 0) { |
| 435 dv_decode_ac(s, mb, block); | 453 dv_decode_ac(&gb, mb, block); |
| 436 /* if still not finished, no need to parse other blocks */ | 454 /* if still not finished, no need to parse other blocks */ |
| 437 if (mb->pos < 64) | 455 if (mb->pos < 64) |
| 438 break; | 456 break; |
| 439 } | 457 } |
| 440 } | 458 } |
| 441 /* all blocks are finished, so the extra bytes can be used at | 459 /* all blocks are finished, so the extra bytes can be used at |
| 442 the video segment level */ | 460 the video segment level */ |
| 443 if (j >= 6) | 461 if (j >= 6) |
| 444 bit_copy(&vs_pb, &s->gb); | 462 bit_copy(&vs_pb, &gb); |
| 445 } | 463 } |
| 446 | 464 |
| 447 /* we need a pass other the whole video segment */ | 465 /* we need a pass other the whole video segment */ |
| 448 #ifdef VLC_DEBUG | 466 #ifdef VLC_DEBUG |
| 449 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb)); | 467 printf("***pass 3 size=%d\n", put_bits_count(&vs_pb)); |
| 450 #endif | 468 #endif |
| 451 block = &s->block[0][0]; | 469 block = &sblock[0][0]; |
| 452 mb = mb_data; | 470 mb = mb_data; |
| 453 init_get_bits(&s->gb, vs_bit_buffer, put_bits_count(&vs_pb)); | 471 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb)); |
| 454 flush_put_bits(&vs_pb); | 472 flush_put_bits(&vs_pb); |
| 455 for(mb_index = 0; mb_index < 5; mb_index++) { | 473 for(mb_index = 0; mb_index < 5; mb_index++) { |
| 456 for(j = 0;j < 6; j++) { | 474 for(j = 0;j < 6; j++) { |
| 457 if (mb->pos < 64) { | 475 if (mb->pos < 64) { |
| 458 #ifdef VLC_DEBUG | 476 #ifdef VLC_DEBUG |
| 459 printf("start %d:%d\n", mb_index, j); | 477 printf("start %d:%d\n", mb_index, j); |
| 460 #endif | 478 #endif |
| 461 dv_decode_ac(s, mb, block); | 479 dv_decode_ac(&gb, mb, block); |
| 462 } | 480 } |
| 463 if (mb->pos >= 64 && mb->pos < 127) | 481 if (mb->pos >= 64 && mb->pos < 127) |
| 464 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos); | 482 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos); |
| 465 block += 64; | 483 block += 64; |
| 466 mb++; | 484 mb++; |
| 467 } | 485 } |
| 468 } | 486 } |
| 469 | 487 |
| 470 /* compute idct and place blocks */ | 488 /* compute idct and place blocks */ |
| 471 block = &s->block[0][0]; | 489 block = &sblock[0][0]; |
| 472 mb = mb_data; | 490 mb = mb_data; |
| 473 for(mb_index = 0; mb_index < 5; mb_index++) { | 491 for(mb_index = 0; mb_index < 5; mb_index++) { |
| 474 v = *mb_pos_ptr++; | 492 v = *mb_pos_ptr++; |
| 475 mb_x = v & 0xff; | 493 mb_x = v & 0xff; |
| 476 mb_y = v >> 8; | 494 mb_y = v >> 8; |
| 741 /* | 759 /* |
| 742 * This is a very rough initial implementaion. The performance is | 760 * This is a very rough initial implementaion. The performance is |
| 743 * horrible and the weighting is missing. But it's missing from the | 761 * horrible and the weighting is missing. But it's missing from the |
| 744 * decoding step also -- so at least we're on the same page with decoder ;-) | 762 * decoding step also -- so at least we're on the same page with decoder ;-) |
| 745 */ | 763 */ |
| 746 static inline void dv_encode_video_segment(DVVideoDecodeContext *s, | 764 static inline void dv_encode_video_segment(DVVideoContext *s, |
| 747 uint8_t *dif, | 765 uint8_t *dif, |
| 748 const uint16_t *mb_pos_ptr) | 766 const uint16_t *mb_pos_ptr) |
| 749 { | 767 { |
| 750 int mb_index, i, j, v; | 768 int mb_index, i, j, v; |
| 751 int mb_x, mb_y, c_offset, linesize; | 769 int mb_x, mb_y, c_offset, linesize; |
| 752 uint8_t* y_ptr; | 770 uint8_t* y_ptr; |
| 753 uint8_t* data; | 771 uint8_t* data; |
| 754 uint8_t* ptr; | 772 uint8_t* ptr; |
| 755 int do_edge_wrap; | 773 int do_edge_wrap; |
| 756 DCTELEM block[64] __align8; | 774 DCTELEM block[64] __align8; |
| 775 DCTELEM sblock[5*6][64] __align8; | |
| 757 EncBlockInfo enc_blks[5*6]; | 776 EncBlockInfo enc_blks[5*6]; |
| 758 PutBitContext pbs[5*6]; | 777 PutBitContext pbs[5*6]; |
| 759 PutBitContext* pb; | 778 PutBitContext* pb; |
| 760 EncBlockInfo* enc_blk; | 779 EncBlockInfo* enc_blk; |
| 761 int vs_bit_size = 0; | 780 int vs_bit_size = 0; |
| 805 } else { /* Simple copy: 8x8 -> 8x8 */ | 824 } else { /* Simple copy: 8x8 -> 8x8 */ |
| 806 s->get_pixels(block, data, linesize); | 825 s->get_pixels(block, data, linesize); |
| 807 } | 826 } |
| 808 | 827 |
| 809 enc_blk->dct_mode = dv_guess_dct_mode(block); | 828 enc_blk->dct_mode = dv_guess_dct_mode(block); |
| 810 enc_blk->mb = &s->block[mb_index*6+j][0]; | 829 enc_blk->mb = &sblock[mb_index*6+j][0]; |
| 811 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0; | 830 enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0; |
| 812 enc_blk->partial_bit_count = 0; | 831 enc_blk->partial_bit_count = 0; |
| 813 enc_blk->partial_bit_buffer = 0; | 832 enc_blk->partial_bit_buffer = 0; |
| 814 enc_blk->cur_ac = 1; | 833 enc_blk->cur_ac = 1; |
| 815 | 834 |
| 857 | 876 |
| 858 for (j=0; j<5*6; j++) | 877 for (j=0; j<5*6; j++) |
| 859 flush_put_bits(&pbs[j]); | 878 flush_put_bits(&pbs[j]); |
| 860 } | 879 } |
| 861 | 880 |
| 881 static int dv_decode_mt(AVCodecContext *avctx, void* sl) | |
| 882 { | |
| 883 DVVideoContext *s = avctx->priv_data; | |
| 884 int slice = (int)sl; | |
| 885 dv_decode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80], | |
| 886 &s->sys->video_place[slice*5]); | |
| 887 return 0; | |
| 888 } | |
| 889 | |
| 890 static int dv_encode_mt(AVCodecContext *avctx, void* sl) | |
| 891 { | |
| 892 DVVideoContext *s = avctx->priv_data; | |
| 893 int slice = (int)sl; | |
| 894 dv_encode_video_segment(s, &s->buf[((slice/27)*6+(slice/3)+slice*5+7)*80], | |
| 895 &s->sys->video_place[slice*5]); | |
| 896 return 0; | |
| 897 } | |
| 898 | |
| 862 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, | 899 /* NOTE: exactly one frame must be given (120000 bytes for NTSC, |
| 863 144000 bytes for PAL) */ | 900 144000 bytes for PAL) */ |
| 864 static int dvvideo_decode_frame(AVCodecContext *avctx, | 901 static int dvvideo_decode_frame(AVCodecContext *avctx, |
| 865 void *data, int *data_size, | 902 void *data, int *data_size, |
| 866 uint8_t *buf, int buf_size) | 903 uint8_t *buf, int buf_size) |
| 867 { | 904 { |
| 868 DVVideoDecodeContext *s = avctx->priv_data; | 905 DVVideoContext *s = avctx->priv_data; |
| 869 int ds, vs; | |
| 870 const uint16_t *mb_pos_ptr; | |
| 871 | 906 |
| 872 *data_size=0; | 907 *data_size=0; |
| 873 /* special case for last picture */ | 908 /* special case for last picture */ |
| 874 if(buf_size==0) | 909 if(buf_size==0) |
| 875 return 0; | 910 return 0; |
| 876 | 911 |
| 877 s->sys = dv_frame_profile(buf); | 912 s->sys = dv_frame_profile(buf); |
| 878 if (!s->sys || buf_size < s->sys->frame_size) | 913 if (!s->sys || buf_size < s->sys->frame_size) |
| 879 return -1; /* NOTE: we only accept several full frames */ | 914 return -1; /* NOTE: we only accept several full frames */ |
| 880 | 915 |
| 881 | |
| 882 if(s->picture.data[0]) | 916 if(s->picture.data[0]) |
| 883 avctx->release_buffer(avctx, &s->picture); | 917 avctx->release_buffer(avctx, &s->picture); |
| 884 | 918 |
| 885 s->picture.reference = 0; | 919 s->picture.reference = 0; |
| 886 avctx->pix_fmt = s->sys->pix_fmt; | 920 avctx->pix_fmt = s->sys->pix_fmt; |
| 891 return -1; | 925 return -1; |
| 892 } | 926 } |
| 893 s->picture.interlaced_frame = 1; | 927 s->picture.interlaced_frame = 1; |
| 894 s->picture.top_field_first = 0; | 928 s->picture.top_field_first = 0; |
| 895 | 929 |
| 896 /* for each DIF segment */ | 930 s->buf = buf; |
| 897 mb_pos_ptr = s->sys->video_place; | 931 avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL, |
| 898 for (ds = 0; ds < s->sys->difseg_size; ds++) { | 932 s->sys->difseg_size * 27); |
| 899 buf += 6 * 80; /* skip DIF segment header */ | 933 |
| 900 | |
| 901 for(vs = 0; vs < 27; vs++) { | |
| 902 if ((vs % 3) == 0) | |
| 903 buf += 80; /* skip audio block */ | |
| 904 | |
| 905 #ifdef VLC_DEBUG | |
| 906 printf("********************* %d, %d **********************\n", ds, vs); | |
| 907 #endif | |
| 908 dv_decode_video_segment(s, buf, mb_pos_ptr); | |
| 909 buf += 5 * 80; | |
| 910 mb_pos_ptr += 5; | |
| 911 } | |
| 912 } | |
| 913 | |
| 914 emms_c(); | 934 emms_c(); |
| 915 | 935 |
| 916 /* return image */ | 936 /* return image */ |
| 917 *data_size = sizeof(AVFrame); | 937 *data_size = sizeof(AVFrame); |
| 918 *(AVFrame*)data= s->picture; | 938 *(AVFrame*)data= s->picture; |
| 921 } | 941 } |
| 922 | 942 |
| 923 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, | 943 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, |
| 924 void *data) | 944 void *data) |
| 925 { | 945 { |
| 926 DVVideoDecodeContext *s = c->priv_data; | 946 DVVideoContext *s = c->priv_data; |
| 927 const uint16_t *mb_pos_ptr; | |
| 928 int ds, vs; | |
| 929 | 947 |
| 930 s->sys = dv_codec_profile(c); | 948 s->sys = dv_codec_profile(c); |
| 931 if (!s->sys) | 949 if (!s->sys) |
| 932 return -1; | 950 return -1; |
| 933 | 951 |
| 934 c->pix_fmt = s->sys->pix_fmt; | 952 c->pix_fmt = s->sys->pix_fmt; |
| 935 s->picture = *((AVFrame *)data); | 953 s->picture = *((AVFrame *)data); |
| 936 | 954 |
| 937 /* for each DIF segment */ | 955 s->buf = buf; |
| 938 mb_pos_ptr = s->sys->video_place; | 956 c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL, |
| 939 for (ds = 0; ds < s->sys->difseg_size; ds++) { | 957 s->sys->difseg_size * 27); |
| 940 buf += 6 * 80; /* skip DIF segment header */ | |
| 941 | |
| 942 for(vs = 0; vs < 27; vs++) { | |
| 943 if ((vs % 3) == 0) | |
| 944 buf += 80; /* skip audio block */ | |
| 945 | |
| 946 #ifdef VLC_DEBUG | |
| 947 printf("********************* %d, %d **********************\n", ds, vs); | |
| 948 #endif | |
| 949 dv_encode_video_segment(s, buf, mb_pos_ptr); | |
| 950 buf += 5 * 80; | |
| 951 mb_pos_ptr += 5; | |
| 952 } | |
| 953 } | |
| 954 | 958 |
| 955 emms_c(); | 959 emms_c(); |
| 956 return s->sys->frame_size; | 960 return s->sys->frame_size; |
| 957 } | 961 } |
| 958 | 962 |
| 959 static int dvvideo_end(AVCodecContext *avctx) | 963 AVCodec dvvideo_encoder = { |
| 960 { | 964 "dvvideo", |
| 961 avcodec_default_free_buffers(avctx); | 965 CODEC_TYPE_VIDEO, |
| 962 return 0; | 966 CODEC_ID_DVVIDEO, |
| 963 } | 967 sizeof(DVVideoContext), |
| 968 dvvideo_init, | |
| 969 dvvideo_encode_frame, | |
| 970 dvvideo_end, | |
| 971 NULL, | |
| 972 CODEC_CAP_DR1, | |
| 973 NULL | |
| 974 }; | |
| 964 | 975 |
| 965 AVCodec dvvideo_decoder = { | 976 AVCodec dvvideo_decoder = { |
| 966 "dvvideo", | 977 "dvvideo", |
| 967 CODEC_TYPE_VIDEO, | 978 CODEC_TYPE_VIDEO, |
| 968 CODEC_ID_DVVIDEO, | 979 CODEC_ID_DVVIDEO, |
| 969 sizeof(DVVideoDecodeContext), | 980 sizeof(DVVideoContext), |
| 970 dvvideo_init, | 981 dvvideo_init, |
| 971 dvvideo_encode_frame, | 982 NULL, |
| 972 dvvideo_end, | 983 dvvideo_end, |
| 973 dvvideo_decode_frame, | 984 dvvideo_decode_frame, |
| 974 CODEC_CAP_DR1, | 985 CODEC_CAP_DR1, |
| 975 NULL | 986 NULL |
| 976 }; | 987 }; |
