00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "get_bits.h"
00032 #include "bytestream.h"
00033 #include "golomb.h"
00034 #include "dirac_arith.h"
00035 #include "mpeg12data.h"
00036 #include "dwt.h"
00037 #include "dirac.h"
00038 #include "diracdsp.h"
00039
00049 #define MAX_DWT_LEVELS 5
00050
00054 #define MAX_REFERENCE_FRAMES 8
00055 #define MAX_DELAY 5
00056 #define MAX_FRAMES (MAX_REFERENCE_FRAMES + MAX_DELAY + 1)
00057 #define MAX_QUANT 68
00058 #define MAX_BLOCKSIZE 32
00059
00063 #define DIRAC_REF_MASK_REF1 1
00064 #define DIRAC_REF_MASK_REF2 2
00065 #define DIRAC_REF_MASK_GLOBAL 4
00066
00071 #define DELAYED_PIC_REF 4
00072
00073 #define ff_emulated_edge_mc ff_emulated_edge_mc_8
00074
00075 #define CALC_PADDING(size, depth) \
00076 (((size + (1 << depth) - 1) >> depth) << depth)
00077
00078 #define DIVRNDUP(a, b) (((a) + (b) - 1) / (b))
00079
00080 typedef struct {
00081 AVFrame avframe;
00082 int interpolated[3];
00083 uint8_t *hpel[3][4];
00084 uint8_t *hpel_base[3][4];
00085 } DiracFrame;
00086
00087 typedef struct {
00088 union {
00089 int16_t mv[2][2];
00090 int16_t dc[3];
00091 } u;
00092 uint8_t ref;
00093 } DiracBlock;
00094
00095 typedef struct SubBand {
00096 int level;
00097 int orientation;
00098 int stride;
00099 int width;
00100 int height;
00101 int quant;
00102 IDWTELEM *ibuf;
00103 struct SubBand *parent;
00104
00105
00106 unsigned length;
00107 const uint8_t *coeff_data;
00108 } SubBand;
00109
00110 typedef struct Plane {
00111 int width;
00112 int height;
00113 int stride;
00114
00115 int idwt_width;
00116 int idwt_height;
00117 int idwt_stride;
00118 IDWTELEM *idwt_buf;
00119 IDWTELEM *idwt_buf_base;
00120 IDWTELEM *idwt_tmp;
00121
00122
00123 uint8_t xblen;
00124 uint8_t yblen;
00125
00126 uint8_t xbsep;
00127 uint8_t ybsep;
00128
00129 uint8_t xoffset;
00130 uint8_t yoffset;
00131
00132 SubBand band[MAX_DWT_LEVELS][4];
00133 } Plane;
00134
00135 typedef struct DiracContext {
00136 AVCodecContext *avctx;
00137 DSPContext dsp;
00138 DiracDSPContext diracdsp;
00139 GetBitContext gb;
00140 dirac_source_params source;
00141 int seen_sequence_header;
00142 int frame_number;
00143 Plane plane[3];
00144 int chroma_x_shift;
00145 int chroma_y_shift;
00146
00147 int zero_res;
00148 int is_arith;
00149 int low_delay;
00150 int globalmc_flag;
00151 int num_refs;
00152
00153
00154 unsigned wavelet_depth;
00155 unsigned wavelet_idx;
00156
00161 unsigned old_delta_quant;
00162 unsigned codeblock_mode;
00163
00164 struct {
00165 unsigned width;
00166 unsigned height;
00167 } codeblock[MAX_DWT_LEVELS+1];
00168
00169 struct {
00170 unsigned num_x;
00171 unsigned num_y;
00172 AVRational bytes;
00173 uint8_t quant[MAX_DWT_LEVELS][4];
00174 } lowdelay;
00175
00176 struct {
00177 int pan_tilt[2];
00178 int zrs[2][2];
00179 int perspective[2];
00180 unsigned zrs_exp;
00181 unsigned perspective_exp;
00182 } globalmc[2];
00183
00184
00185 uint8_t mv_precision;
00186 int16_t weight[2];
00187 unsigned weight_log2denom;
00188
00189 int blwidth;
00190 int blheight;
00191 int sbwidth;
00192 int sbheight;
00193
00194 uint8_t *sbsplit;
00195 DiracBlock *blmotion;
00196
00197 uint8_t *edge_emu_buffer[4];
00198 uint8_t *edge_emu_buffer_base;
00199
00200 uint16_t *mctmp;
00201 uint8_t *mcscratch;
00202
00203 DECLARE_ALIGNED(16, uint8_t, obmc_weight)[3][MAX_BLOCKSIZE*MAX_BLOCKSIZE];
00204
00205 void (*put_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
00206 void (*avg_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
00207 void (*add_obmc)(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen);
00208 dirac_weight_func weight_func;
00209 dirac_biweight_func biweight_func;
00210
00211 DiracFrame *current_picture;
00212 DiracFrame *ref_pics[2];
00213
00214 DiracFrame *ref_frames[MAX_REFERENCE_FRAMES+1];
00215 DiracFrame *delay_frames[MAX_DELAY+1];
00216 DiracFrame all_frames[MAX_FRAMES];
00217 } DiracContext;
00218
00223 enum dirac_parse_code {
00224 pc_seq_header = 0x00,
00225 pc_eos = 0x10,
00226 pc_aux_data = 0x20,
00227 pc_padding = 0x30,
00228 };
00229
00230 enum dirac_subband {
00231 subband_ll = 0,
00232 subband_hl = 1,
00233 subband_lh = 2,
00234 subband_hh = 3
00235 };
00236
00237 static const uint8_t default_qmat[][4][4] = {
00238 { { 5, 3, 3, 0}, { 0, 4, 4, 1}, { 0, 5, 5, 2}, { 0, 6, 6, 3} },
00239 { { 4, 2, 2, 0}, { 0, 4, 4, 2}, { 0, 5, 5, 3}, { 0, 7, 7, 5} },
00240 { { 5, 3, 3, 0}, { 0, 4, 4, 1}, { 0, 5, 5, 2}, { 0, 6, 6, 3} },
00241 { { 8, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0} },
00242 { { 8, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0} },
00243 { { 0, 4, 4, 8}, { 0, 8, 8, 12}, { 0, 13, 13, 17}, { 0, 17, 17, 21} },
00244 { { 3, 1, 1, 0}, { 0, 4, 4, 2}, { 0, 6, 6, 5}, { 0, 9, 9, 7} },
00245 };
00246
00247 static const int qscale_tab[MAX_QUANT+1] = {
00248 4, 5, 6, 7, 8, 10, 11, 13,
00249 16, 19, 23, 27, 32, 38, 45, 54,
00250 64, 76, 91, 108, 128, 152, 181, 215,
00251 256, 304, 362, 431, 512, 609, 724, 861,
00252 1024, 1218, 1448, 1722, 2048, 2435, 2896, 3444,
00253 4096, 4871, 5793, 6889, 8192, 9742, 11585, 13777,
00254 16384, 19484, 23170, 27554, 32768, 38968, 46341, 55109,
00255 65536, 77936
00256 };
00257
00258 static const int qoffset_intra_tab[MAX_QUANT+1] = {
00259 1, 2, 3, 4, 4, 5, 6, 7,
00260 8, 10, 12, 14, 16, 19, 23, 27,
00261 32, 38, 46, 54, 64, 76, 91, 108,
00262 128, 152, 181, 216, 256, 305, 362, 431,
00263 512, 609, 724, 861, 1024, 1218, 1448, 1722,
00264 2048, 2436, 2897, 3445, 4096, 4871, 5793, 6889,
00265 8192, 9742, 11585, 13777, 16384, 19484, 23171, 27555,
00266 32768, 38968
00267 };
00268
00269 static const int qoffset_inter_tab[MAX_QUANT+1] = {
00270 1, 2, 2, 3, 3, 4, 4, 5,
00271 6, 7, 9, 10, 12, 14, 17, 20,
00272 24, 29, 34, 41, 48, 57, 68, 81,
00273 96, 114, 136, 162, 192, 228, 272, 323,
00274 384, 457, 543, 646, 768, 913, 1086, 1292,
00275 1536, 1827, 2172, 2583, 3072, 3653, 4344, 5166,
00276 6144, 7307, 8689, 10333, 12288, 14613, 17378, 20666,
00277 24576, 29226
00278 };
00279
00280
00281 static inline int divide3(int x)
00282 {
00283 return ((x+1)*21845 + 10922) >> 16;
00284 }
00285
00286 static DiracFrame *remove_frame(DiracFrame *framelist[], int picnum)
00287 {
00288 DiracFrame *remove_pic = NULL;
00289 int i, remove_idx = -1;
00290
00291 for (i = 0; framelist[i]; i++)
00292 if (framelist[i]->avframe.display_picture_number == picnum) {
00293 remove_pic = framelist[i];
00294 remove_idx = i;
00295 }
00296
00297 if (remove_pic)
00298 for (i = remove_idx; framelist[i]; i++)
00299 framelist[i] = framelist[i+1];
00300
00301 return remove_pic;
00302 }
00303
00304 static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame)
00305 {
00306 int i;
00307 for (i = 0; i < maxframes; i++)
00308 if (!framelist[i]) {
00309 framelist[i] = frame;
00310 return 0;
00311 }
00312 return -1;
00313 }
00314
00315 static int alloc_sequence_buffers(DiracContext *s)
00316 {
00317 int sbwidth = DIVRNDUP(s->source.width, 4);
00318 int sbheight = DIVRNDUP(s->source.height, 4);
00319 int i, w, h, top_padding;
00320
00321
00322 for (i = 0; i < 3; i++) {
00323 int max_xblen = MAX_BLOCKSIZE >> (i ? s->chroma_x_shift : 0);
00324 int max_yblen = MAX_BLOCKSIZE >> (i ? s->chroma_y_shift : 0);
00325 w = s->source.width >> (i ? s->chroma_x_shift : 0);
00326 h = s->source.height >> (i ? s->chroma_y_shift : 0);
00327
00328
00329
00330
00331
00332
00333 top_padding = FFMAX(1<<MAX_DWT_LEVELS, max_yblen/2);
00334 w = FFALIGN(CALC_PADDING(w, MAX_DWT_LEVELS), 8);
00335 h = top_padding + CALC_PADDING(h, MAX_DWT_LEVELS) + max_yblen/2;
00336
00337 s->plane[i].idwt_buf_base = av_mallocz((w+max_xblen)*h * sizeof(IDWTELEM));
00338 s->plane[i].idwt_tmp = av_malloc((w+16) * sizeof(IDWTELEM));
00339 s->plane[i].idwt_buf = s->plane[i].idwt_buf_base + top_padding*w;
00340 if (!s->plane[i].idwt_buf_base || !s->plane[i].idwt_tmp)
00341 return AVERROR(ENOMEM);
00342 }
00343
00344 w = s->source.width;
00345 h = s->source.height;
00346
00347
00348 s->sbsplit = av_malloc(sbwidth * sbheight);
00349 s->blmotion = av_malloc(sbwidth * sbheight * 4 * sizeof(*s->blmotion));
00350 s->edge_emu_buffer_base = av_malloc((w+64)*MAX_BLOCKSIZE);
00351
00352 s->mctmp = av_malloc((w+64+MAX_BLOCKSIZE) * (h*MAX_BLOCKSIZE) * sizeof(*s->mctmp));
00353 s->mcscratch = av_malloc((w+64)*MAX_BLOCKSIZE);
00354
00355 if (!s->sbsplit || !s->blmotion)
00356 return AVERROR(ENOMEM);
00357 return 0;
00358 }
00359
00360 static void free_sequence_buffers(DiracContext *s)
00361 {
00362 int i, j, k;
00363
00364 for (i = 0; i < MAX_FRAMES; i++) {
00365 if (s->all_frames[i].avframe.data[0]) {
00366 s->avctx->release_buffer(s->avctx, &s->all_frames[i].avframe);
00367 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
00368 }
00369
00370 for (j = 0; j < 3; j++)
00371 for (k = 1; k < 4; k++)
00372 av_freep(&s->all_frames[i].hpel_base[j][k]);
00373 }
00374
00375 memset(s->ref_frames, 0, sizeof(s->ref_frames));
00376 memset(s->delay_frames, 0, sizeof(s->delay_frames));
00377
00378 for (i = 0; i < 3; i++) {
00379 av_freep(&s->plane[i].idwt_buf_base);
00380 av_freep(&s->plane[i].idwt_tmp);
00381 }
00382
00383 av_freep(&s->sbsplit);
00384 av_freep(&s->blmotion);
00385 av_freep(&s->edge_emu_buffer_base);
00386
00387 av_freep(&s->mctmp);
00388 av_freep(&s->mcscratch);
00389 }
00390
00391 static av_cold int dirac_decode_init(AVCodecContext *avctx)
00392 {
00393 DiracContext *s = avctx->priv_data;
00394 s->avctx = avctx;
00395 s->frame_number = -1;
00396
00397 if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
00398 av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported!\n");
00399 return AVERROR_PATCHWELCOME;
00400 }
00401
00402 dsputil_init(&s->dsp, avctx);
00403 ff_diracdsp_init(&s->diracdsp);
00404
00405 return 0;
00406 }
00407
00408 static void dirac_decode_flush(AVCodecContext *avctx)
00409 {
00410 DiracContext *s = avctx->priv_data;
00411 free_sequence_buffers(s);
00412 s->seen_sequence_header = 0;
00413 s->frame_number = -1;
00414 }
00415
00416 static av_cold int dirac_decode_end(AVCodecContext *avctx)
00417 {
00418 dirac_decode_flush(avctx);
00419 return 0;
00420 }
00421
00422 #define SIGN_CTX(x) (CTX_SIGN_ZERO + ((x) > 0) - ((x) < 0))
00423
00424 static inline void coeff_unpack_arith(DiracArith *c, int qfactor, int qoffset,
00425 SubBand *b, IDWTELEM *buf, int x, int y)
00426 {
00427 int coeff, sign;
00428 int sign_pred = 0;
00429 int pred_ctx = CTX_ZPZN_F1;
00430
00431
00432 if (b->parent)
00433 pred_ctx += !!b->parent->ibuf[b->parent->stride * (y>>1) + (x>>1)] << 1;
00434
00435 if (b->orientation == subband_hl)
00436 sign_pred = buf[-b->stride];
00437
00438
00439 if (x) {
00440 pred_ctx += !(buf[-1] | buf[-b->stride] | buf[-1-b->stride]);
00441 if (b->orientation == subband_lh)
00442 sign_pred = buf[-1];
00443 } else {
00444 pred_ctx += !buf[-b->stride];
00445 }
00446
00447 coeff = dirac_get_arith_uint(c, pred_ctx, CTX_COEFF_DATA);
00448 if (coeff) {
00449 coeff = (coeff * qfactor + qoffset + 2) >> 2;
00450 sign = dirac_get_arith_bit(c, SIGN_CTX(sign_pred));
00451 coeff = (coeff ^ -sign) + sign;
00452 }
00453 *buf = coeff;
00454 }
00455
00456 static inline int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset)
00457 {
00458 int sign, coeff;
00459
00460 coeff = svq3_get_ue_golomb(gb);
00461 if (coeff) {
00462 coeff = (coeff * qfactor + qoffset + 2) >> 2;
00463 sign = get_bits1(gb);
00464 coeff = (coeff ^ -sign) + sign;
00465 }
00466 return coeff;
00467 }
00468
00473 static inline void codeblock(DiracContext *s, SubBand *b,
00474 GetBitContext *gb, DiracArith *c,
00475 int left, int right, int top, int bottom,
00476 int blockcnt_one, int is_arith)
00477 {
00478 int x, y, zero_block;
00479 int qoffset, qfactor;
00480 IDWTELEM *buf;
00481
00482
00483 if (!blockcnt_one) {
00484 if (is_arith)
00485 zero_block = dirac_get_arith_bit(c, CTX_ZERO_BLOCK);
00486 else
00487 zero_block = get_bits1(gb);
00488
00489 if (zero_block)
00490 return;
00491 }
00492
00493 if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) {
00494 if (is_arith)
00495 b->quant += dirac_get_arith_int(c, CTX_DELTA_Q_F, CTX_DELTA_Q_DATA);
00496 else
00497 b->quant += dirac_get_se_golomb(gb);
00498 }
00499
00500 b->quant = FFMIN(b->quant, MAX_QUANT);
00501
00502 qfactor = qscale_tab[b->quant];
00503
00504 if (!s->num_refs)
00505 qoffset = qoffset_intra_tab[b->quant];
00506 else
00507 qoffset = qoffset_inter_tab[b->quant];
00508
00509 buf = b->ibuf + top * b->stride;
00510 for (y = top; y < bottom; y++) {
00511 for (x = left; x < right; x++) {
00512
00513 if (is_arith)
00514 coeff_unpack_arith(c, qfactor, qoffset, b, buf+x, x, y);
00515 else
00516 buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset);
00517 }
00518 buf += b->stride;
00519 }
00520 }
00521
00526 static inline void intra_dc_prediction(SubBand *b)
00527 {
00528 IDWTELEM *buf = b->ibuf;
00529 int x, y;
00530
00531 for (x = 1; x < b->width; x++)
00532 buf[x] += buf[x-1];
00533 buf += b->stride;
00534
00535 for (y = 1; y < b->height; y++) {
00536 buf[0] += buf[-b->stride];
00537
00538 for (x = 1; x < b->width; x++) {
00539 int pred = buf[x - 1] + buf[x - b->stride] + buf[x - b->stride-1];
00540 buf[x] += divide3(pred);
00541 }
00542 buf += b->stride;
00543 }
00544 }
00545
00550 static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith)
00551 {
00552 int cb_x, cb_y, left, right, top, bottom;
00553 DiracArith c;
00554 GetBitContext gb;
00555 int cb_width = s->codeblock[b->level + (b->orientation != subband_ll)].width;
00556 int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height;
00557 int blockcnt_one = (cb_width + cb_height) == 2;
00558
00559 if (!b->length)
00560 return;
00561
00562 init_get_bits(&gb, b->coeff_data, b->length*8);
00563
00564 if (is_arith)
00565 ff_dirac_init_arith_decoder(&c, &gb, b->length);
00566
00567 top = 0;
00568 for (cb_y = 0; cb_y < cb_height; cb_y++) {
00569 bottom = (b->height * (cb_y+1)) / cb_height;
00570 left = 0;
00571 for (cb_x = 0; cb_x < cb_width; cb_x++) {
00572 right = (b->width * (cb_x+1)) / cb_width;
00573 codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith);
00574 left = right;
00575 }
00576 top = bottom;
00577 }
00578
00579 if (b->orientation == subband_ll && s->num_refs == 0)
00580 intra_dc_prediction(b);
00581 }
00582
00583 static int decode_subband_arith(AVCodecContext *avctx, void *b)
00584 {
00585 DiracContext *s = avctx->priv_data;
00586 decode_subband_internal(s, b, 1);
00587 return 0;
00588 }
00589
00590 static int decode_subband_golomb(AVCodecContext *avctx, void *arg)
00591 {
00592 DiracContext *s = avctx->priv_data;
00593 SubBand **b = arg;
00594 decode_subband_internal(s, *b, 0);
00595 return 0;
00596 }
00597
00602 static void decode_component(DiracContext *s, int comp)
00603 {
00604 AVCodecContext *avctx = s->avctx;
00605 SubBand *bands[3*MAX_DWT_LEVELS+1];
00606 enum dirac_subband orientation;
00607 int level, num_bands = 0;
00608
00609
00610 for (level = 0; level < s->wavelet_depth; level++) {
00611 for (orientation = !!level; orientation < 4; orientation++) {
00612 SubBand *b = &s->plane[comp].band[level][orientation];
00613 bands[num_bands++] = b;
00614
00615 align_get_bits(&s->gb);
00616
00617 b->length = svq3_get_ue_golomb(&s->gb);
00618 if (b->length) {
00619 b->quant = svq3_get_ue_golomb(&s->gb);
00620 align_get_bits(&s->gb);
00621 b->coeff_data = s->gb.buffer + get_bits_count(&s->gb)/8;
00622 b->length = FFMIN(b->length, get_bits_left(&s->gb)/8);
00623 skip_bits_long(&s->gb, b->length*8);
00624 }
00625 }
00626
00627 if (s->is_arith)
00628 avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level],
00629 NULL, 4-!!level, sizeof(SubBand));
00630 }
00631
00632 if (!s->is_arith)
00633 avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*));
00634 }
00635
00636
00637
00638 static void lowdelay_subband(DiracContext *s, GetBitContext *gb, int quant,
00639 int slice_x, int slice_y, int bits_end,
00640 SubBand *b1, SubBand *b2)
00641 {
00642 int left = b1->width * slice_x / s->lowdelay.num_x;
00643 int right = b1->width *(slice_x+1) / s->lowdelay.num_x;
00644 int top = b1->height * slice_y / s->lowdelay.num_y;
00645 int bottom = b1->height *(slice_y+1) / s->lowdelay.num_y;
00646
00647 int qfactor = qscale_tab[FFMIN(quant, MAX_QUANT)];
00648 int qoffset = qoffset_intra_tab[FFMIN(quant, MAX_QUANT)];
00649
00650 IDWTELEM *buf1 = b1->ibuf + top * b1->stride;
00651 IDWTELEM *buf2 = b2 ? b2->ibuf + top * b2->stride : NULL;
00652 int x, y;
00653
00654
00655 if (get_bits_count(gb) >= bits_end)
00656 return;
00657
00658 for (y = top; y < bottom; y++) {
00659 for (x = left; x < right; x++) {
00660 buf1[x] = coeff_unpack_golomb(gb, qfactor, qoffset);
00661 if (get_bits_count(gb) >= bits_end)
00662 return;
00663 if (buf2) {
00664 buf2[x] = coeff_unpack_golomb(gb, qfactor, qoffset);
00665 if (get_bits_count(gb) >= bits_end)
00666 return;
00667 }
00668 }
00669 buf1 += b1->stride;
00670 if (buf2)
00671 buf2 += b2->stride;
00672 }
00673 }
00674
00675 struct lowdelay_slice {
00676 GetBitContext gb;
00677 int slice_x;
00678 int slice_y;
00679 int bytes;
00680 };
00681
00682
00687 static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg)
00688 {
00689 DiracContext *s = avctx->priv_data;
00690 struct lowdelay_slice *slice = arg;
00691 GetBitContext *gb = &slice->gb;
00692 enum dirac_subband orientation;
00693 int level, quant, chroma_bits, chroma_end;
00694
00695 int quant_base = get_bits(gb, 7);
00696 int length_bits = av_log2(8 * slice->bytes)+1;
00697 int luma_bits = get_bits_long(gb, length_bits);
00698 int luma_end = get_bits_count(gb) + FFMIN(luma_bits, get_bits_left(gb));
00699
00700
00701 for (level = 0; level < s->wavelet_depth; level++)
00702 for (orientation = !!level; orientation < 4; orientation++) {
00703 quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
00704 lowdelay_subband(s, gb, quant, slice->slice_x, slice->slice_y, luma_end,
00705 &s->plane[0].band[level][orientation], NULL);
00706 }
00707
00708
00709 skip_bits_long(gb, get_bits_count(gb) - luma_end);
00710
00711 chroma_bits = 8*slice->bytes - 7 - length_bits - luma_bits;
00712 chroma_end = get_bits_count(gb) + FFMIN(chroma_bits, get_bits_left(gb));
00713
00714 for (level = 0; level < s->wavelet_depth; level++)
00715 for (orientation = !!level; orientation < 4; orientation++) {
00716 quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
00717 lowdelay_subband(s, gb, quant, slice->slice_x, slice->slice_y, chroma_end,
00718 &s->plane[1].band[level][orientation],
00719 &s->plane[2].band[level][orientation]);
00720 }
00721
00722 return 0;
00723 }
00724
00729 static void decode_lowdelay(DiracContext *s)
00730 {
00731 AVCodecContext *avctx = s->avctx;
00732 int slice_x, slice_y, bytes, bufsize;
00733 const uint8_t *buf;
00734 struct lowdelay_slice *slices;
00735 int slice_num = 0;
00736
00737 slices = av_mallocz(s->lowdelay.num_x * s->lowdelay.num_y * sizeof(struct lowdelay_slice));
00738
00739 align_get_bits(&s->gb);
00740
00741 buf = s->gb.buffer + get_bits_count(&s->gb)/8;
00742 bufsize = get_bits_left(&s->gb);
00743
00744 for (slice_y = 0; bufsize > 0 && slice_y < s->lowdelay.num_y; slice_y++)
00745 for (slice_x = 0; bufsize > 0 && slice_x < s->lowdelay.num_x; slice_x++) {
00746 bytes = (slice_num+1) * s->lowdelay.bytes.num / s->lowdelay.bytes.den
00747 - slice_num * s->lowdelay.bytes.num / s->lowdelay.bytes.den;
00748
00749 slices[slice_num].bytes = bytes;
00750 slices[slice_num].slice_x = slice_x;
00751 slices[slice_num].slice_y = slice_y;
00752 init_get_bits(&slices[slice_num].gb, buf, bufsize);
00753 slice_num++;
00754
00755 buf += bytes;
00756 bufsize -= bytes*8;
00757 }
00758
00759 avctx->execute(avctx, decode_lowdelay_slice, slices, NULL, slice_num,
00760 sizeof(struct lowdelay_slice));
00761 intra_dc_prediction(&s->plane[0].band[0][0]);
00762 intra_dc_prediction(&s->plane[1].band[0][0]);
00763 intra_dc_prediction(&s->plane[2].band[0][0]);
00764 av_free(slices);
00765 }
00766
00767 static void init_planes(DiracContext *s)
00768 {
00769 int i, w, h, level, orientation;
00770
00771 for (i = 0; i < 3; i++) {
00772 Plane *p = &s->plane[i];
00773
00774 p->width = s->source.width >> (i ? s->chroma_x_shift : 0);
00775 p->height = s->source.height >> (i ? s->chroma_y_shift : 0);
00776 p->idwt_width = w = CALC_PADDING(p->width , s->wavelet_depth);
00777 p->idwt_height = h = CALC_PADDING(p->height, s->wavelet_depth);
00778 p->idwt_stride = FFALIGN(p->idwt_width, 8);
00779
00780 for (level = s->wavelet_depth-1; level >= 0; level--) {
00781 w = w>>1;
00782 h = h>>1;
00783 for (orientation = !!level; orientation < 4; orientation++) {
00784 SubBand *b = &p->band[level][orientation];
00785
00786 b->ibuf = p->idwt_buf;
00787 b->level = level;
00788 b->stride = p->idwt_stride << (s->wavelet_depth - level);
00789 b->width = w;
00790 b->height = h;
00791 b->orientation = orientation;
00792
00793 if (orientation & 1)
00794 b->ibuf += w;
00795 if (orientation > 1)
00796 b->ibuf += b->stride>>1;
00797
00798 if (level)
00799 b->parent = &p->band[level-1][orientation];
00800 }
00801 }
00802
00803 if (i > 0) {
00804 p->xblen = s->plane[0].xblen >> s->chroma_x_shift;
00805 p->yblen = s->plane[0].yblen >> s->chroma_y_shift;
00806 p->xbsep = s->plane[0].xbsep >> s->chroma_x_shift;
00807 p->ybsep = s->plane[0].ybsep >> s->chroma_y_shift;
00808 }
00809
00810 p->xoffset = (p->xblen - p->xbsep)/2;
00811 p->yoffset = (p->yblen - p->ybsep)/2;
00812 }
00813 }
00814
00820 static int dirac_unpack_prediction_parameters(DiracContext *s)
00821 {
00822 static const uint8_t default_blen[] = { 4, 12, 16, 24 };
00823 static const uint8_t default_bsep[] = { 4, 8, 12, 16 };
00824
00825 GetBitContext *gb = &s->gb;
00826 unsigned idx, ref;
00827
00828 align_get_bits(gb);
00829
00830
00831 idx = svq3_get_ue_golomb(gb);
00832
00833 if (idx > 4) {
00834 av_log(s->avctx, AV_LOG_ERROR, "Block prediction index too high\n");
00835 return -1;
00836 }
00837
00838 if (idx == 0) {
00839 s->plane[0].xblen = svq3_get_ue_golomb(gb);
00840 s->plane[0].yblen = svq3_get_ue_golomb(gb);
00841 s->plane[0].xbsep = svq3_get_ue_golomb(gb);
00842 s->plane[0].ybsep = svq3_get_ue_golomb(gb);
00843 } else {
00844
00845 s->plane[0].xblen = default_blen[idx-1];
00846 s->plane[0].yblen = default_blen[idx-1];
00847 s->plane[0].xbsep = default_bsep[idx-1];
00848 s->plane[0].ybsep = default_bsep[idx-1];
00849 }
00850
00851
00852
00853 if (s->plane[0].xbsep < s->plane[0].xblen/2 || s->plane[0].ybsep < s->plane[0].yblen/2) {
00854 av_log(s->avctx, AV_LOG_ERROR, "Block separation too small\n");
00855 return -1;
00856 }
00857 if (s->plane[0].xbsep > s->plane[0].xblen || s->plane[0].ybsep > s->plane[0].yblen) {
00858 av_log(s->avctx, AV_LOG_ERROR, "Block seperation greater than size\n");
00859 return -1;
00860 }
00861 if (FFMAX(s->plane[0].xblen, s->plane[0].yblen) > MAX_BLOCKSIZE) {
00862 av_log(s->avctx, AV_LOG_ERROR, "Unsupported large block size\n");
00863 return -1;
00864 }
00865
00866
00867
00868 s->mv_precision = svq3_get_ue_golomb(gb);
00869 if (s->mv_precision > 3) {
00870 av_log(s->avctx, AV_LOG_ERROR, "MV precision finer than eighth-pel\n");
00871 return -1;
00872 }
00873
00874
00875
00876 s->globalmc_flag = get_bits1(gb);
00877 if (s->globalmc_flag) {
00878 memset(s->globalmc, 0, sizeof(s->globalmc));
00879
00880 for (ref = 0; ref < s->num_refs; ref++) {
00881 if (get_bits1(gb)) {
00882 s->globalmc[ref].pan_tilt[0] = dirac_get_se_golomb(gb);
00883 s->globalmc[ref].pan_tilt[1] = dirac_get_se_golomb(gb);
00884 }
00885
00886
00887 if (get_bits1(gb)) {
00888 s->globalmc[ref].zrs_exp = svq3_get_ue_golomb(gb);
00889 s->globalmc[ref].zrs[0][0] = dirac_get_se_golomb(gb);
00890 s->globalmc[ref].zrs[0][1] = dirac_get_se_golomb(gb);
00891 s->globalmc[ref].zrs[1][0] = dirac_get_se_golomb(gb);
00892 s->globalmc[ref].zrs[1][1] = dirac_get_se_golomb(gb);
00893 } else {
00894 s->globalmc[ref].zrs[0][0] = 1;
00895 s->globalmc[ref].zrs[1][1] = 1;
00896 }
00897
00898 if (get_bits1(gb)) {
00899 s->globalmc[ref].perspective_exp = svq3_get_ue_golomb(gb);
00900 s->globalmc[ref].perspective[0] = dirac_get_se_golomb(gb);
00901 s->globalmc[ref].perspective[1] = dirac_get_se_golomb(gb);
00902 }
00903 }
00904 }
00905
00906
00907
00908 if (svq3_get_ue_golomb(gb)) {
00909 av_log(s->avctx, AV_LOG_ERROR, "Unknown picture prediction mode\n");
00910 return -1;
00911 }
00912
00913
00914
00915 s->weight_log2denom = 1;
00916 s->weight[0] = 1;
00917 s->weight[1] = 1;
00918
00919 if (get_bits1(gb)) {
00920 s->weight_log2denom = svq3_get_ue_golomb(gb);
00921 s->weight[0] = dirac_get_se_golomb(gb);
00922 if (s->num_refs == 2)
00923 s->weight[1] = dirac_get_se_golomb(gb);
00924 }
00925 return 0;
00926 }
00927
00932 static int dirac_unpack_idwt_params(DiracContext *s)
00933 {
00934 GetBitContext *gb = &s->gb;
00935 int i, level;
00936
00937 align_get_bits(gb);
00938
00939 s->zero_res = s->num_refs ? get_bits1(gb) : 0;
00940 if (s->zero_res)
00941 return 0;
00942
00943
00944 s->wavelet_idx = svq3_get_ue_golomb(gb);
00945 if (s->wavelet_idx > 6)
00946 return -1;
00947
00948 s->wavelet_depth = svq3_get_ue_golomb(gb);
00949 if (s->wavelet_depth > MAX_DWT_LEVELS) {
00950 av_log(s->avctx, AV_LOG_ERROR, "too many dwt decompositions\n");
00951 return -1;
00952 }
00953
00954 if (!s->low_delay) {
00955
00956 if (get_bits1(gb)) {
00957 for (i = 0; i <= s->wavelet_depth; i++) {
00958 s->codeblock[i].width = svq3_get_ue_golomb(gb);
00959 s->codeblock[i].height = svq3_get_ue_golomb(gb);
00960 }
00961
00962 s->codeblock_mode = svq3_get_ue_golomb(gb);
00963 if (s->codeblock_mode > 1) {
00964 av_log(s->avctx, AV_LOG_ERROR, "unknown codeblock mode\n");
00965 return -1;
00966 }
00967 } else
00968 for (i = 0; i <= s->wavelet_depth; i++)
00969 s->codeblock[i].width = s->codeblock[i].height = 1;
00970 } else {
00971
00972
00973 s->lowdelay.num_x = svq3_get_ue_golomb(gb);
00974 s->lowdelay.num_y = svq3_get_ue_golomb(gb);
00975 s->lowdelay.bytes.num = svq3_get_ue_golomb(gb);
00976 s->lowdelay.bytes.den = svq3_get_ue_golomb(gb);
00977
00978
00979 if (get_bits1(gb)) {
00980 av_log(s->avctx,AV_LOG_DEBUG,"Low Delay: Has Custom Quantization Matrix!\n");
00981
00982 s->lowdelay.quant[0][0] = svq3_get_ue_golomb(gb);
00983 for (level = 0; level < s->wavelet_depth; level++) {
00984 s->lowdelay.quant[level][1] = svq3_get_ue_golomb(gb);
00985 s->lowdelay.quant[level][2] = svq3_get_ue_golomb(gb);
00986 s->lowdelay.quant[level][3] = svq3_get_ue_golomb(gb);
00987 }
00988 } else {
00989
00990 for (level = 0; level < s->wavelet_depth; level++)
00991 for (i = 0; i < 4; i++) {
00992 s->lowdelay.quant[level][i] = default_qmat[s->wavelet_idx][level][i];
00993
00994 if (s->wavelet_idx == 3)
00995 s->lowdelay.quant[level][i] += 4*(s->wavelet_depth-1 - level);
00996 }
00997 }
00998 }
00999 return 0;
01000 }
01001
01002 static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
01003 {
01004 static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 };
01005
01006 if (!(x|y))
01007 return 0;
01008 else if (!y)
01009 return sbsplit[-1];
01010 else if (!x)
01011 return sbsplit[-stride];
01012
01013 return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]];
01014 }
01015
01016 static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
01017 {
01018 int pred;
01019
01020 if (!(x|y))
01021 return 0;
01022 else if (!y)
01023 return block[-1].ref & refmask;
01024 else if (!x)
01025 return block[-stride].ref & refmask;
01026
01027
01028 pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask);
01029 return (pred >> 1) & refmask;
01030 }
01031
01032 static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y)
01033 {
01034 int i, n = 0;
01035
01036 memset(block->u.dc, 0, sizeof(block->u.dc));
01037
01038 if (x && !(block[-1].ref & 3)) {
01039 for (i = 0; i < 3; i++)
01040 block->u.dc[i] += block[-1].u.dc[i];
01041 n++;
01042 }
01043
01044 if (y && !(block[-stride].ref & 3)) {
01045 for (i = 0; i < 3; i++)
01046 block->u.dc[i] += block[-stride].u.dc[i];
01047 n++;
01048 }
01049
01050 if (x && y && !(block[-1-stride].ref & 3)) {
01051 for (i = 0; i < 3; i++)
01052 block->u.dc[i] += block[-1-stride].u.dc[i];
01053 n++;
01054 }
01055
01056 if (n == 2) {
01057 for (i = 0; i < 3; i++)
01058 block->u.dc[i] = (block->u.dc[i]+1)>>1;
01059 } else if (n == 3) {
01060 for (i = 0; i < 3; i++)
01061 block->u.dc[i] = divide3(block->u.dc[i]);
01062 }
01063 }
01064
01065 static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
01066 {
01067 int16_t *pred[3];
01068 int refmask = ref+1;
01069 int mask = refmask | DIRAC_REF_MASK_GLOBAL;
01070 int n = 0;
01071
01072 if (x && (block[-1].ref & mask) == refmask)
01073 pred[n++] = block[-1].u.mv[ref];
01074
01075 if (y && (block[-stride].ref & mask) == refmask)
01076 pred[n++] = block[-stride].u.mv[ref];
01077
01078 if (x && y && (block[-stride-1].ref & mask) == refmask)
01079 pred[n++] = block[-stride-1].u.mv[ref];
01080
01081 switch (n) {
01082 case 0:
01083 block->u.mv[ref][0] = 0;
01084 block->u.mv[ref][1] = 0;
01085 break;
01086 case 1:
01087 block->u.mv[ref][0] = pred[0][0];
01088 block->u.mv[ref][1] = pred[0][1];
01089 break;
01090 case 2:
01091 block->u.mv[ref][0] = (pred[0][0] + pred[1][0] + 1) >> 1;
01092 block->u.mv[ref][1] = (pred[0][1] + pred[1][1] + 1) >> 1;
01093 break;
01094 case 3:
01095 block->u.mv[ref][0] = mid_pred(pred[0][0], pred[1][0], pred[2][0]);
01096 block->u.mv[ref][1] = mid_pred(pred[0][1], pred[1][1], pred[2][1]);
01097 break;
01098 }
01099 }
01100
01101 static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
01102 {
01103 int ez = s->globalmc[ref].zrs_exp;
01104 int ep = s->globalmc[ref].perspective_exp;
01105 int (*A)[2] = s->globalmc[ref].zrs;
01106 int *b = s->globalmc[ref].pan_tilt;
01107 int *c = s->globalmc[ref].perspective;
01108
01109 int m = (1<<ep) - (c[0]*x + c[1]*y);
01110 int mx = m * ((A[0][0] * x + A[0][1]*y) + (1<<ez) * b[0]);
01111 int my = m * ((A[1][0] * x + A[1][1]*y) + (1<<ez) * b[1]);
01112
01113 block->u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep);
01114 block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep);
01115 }
01116
01117 static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block,
01118 int stride, int x, int y)
01119 {
01120 int i;
01121
01122 block->ref = pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF1);
01123 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF1);
01124
01125 if (s->num_refs == 2) {
01126 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF2);
01127 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF2) << 1;
01128 }
01129
01130 if (!block->ref) {
01131 pred_block_dc(block, stride, x, y);
01132 for (i = 0; i < 3; i++)
01133 block->u.dc[i] += dirac_get_arith_int(arith+1+i, CTX_DC_F1, CTX_DC_DATA);
01134 return;
01135 }
01136
01137 if (s->globalmc_flag) {
01138 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_GLOBAL);
01139 block->ref ^= dirac_get_arith_bit(arith, CTX_GLOBAL_BLOCK) << 2;
01140 }
01141
01142 for (i = 0; i < s->num_refs; i++)
01143 if (block->ref & (i+1)) {
01144 if (block->ref & DIRAC_REF_MASK_GLOBAL) {
01145 global_mv(s, block, x, y, i);
01146 } else {
01147 pred_mv(block, stride, x, y, i);
01148 block->u.mv[i][0] += dirac_get_arith_int(arith + 4 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
01149 block->u.mv[i][1] += dirac_get_arith_int(arith + 5 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
01150 }
01151 }
01152 }
01153
01157 static void propagate_block_data(DiracBlock *block, int stride, int size)
01158 {
01159 int x, y;
01160 DiracBlock *dst = block;
01161
01162 for (x = 1; x < size; x++)
01163 dst[x] = *block;
01164
01165 for (y = 1; y < size; y++) {
01166 dst += stride;
01167 for (x = 0; x < size; x++)
01168 dst[x] = *block;
01169 }
01170 }
01171
01176 static void dirac_unpack_block_motion_data(DiracContext *s)
01177 {
01178 GetBitContext *gb = &s->gb;
01179 uint8_t *sbsplit = s->sbsplit;
01180 int i, x, y, q, p;
01181 DiracArith arith[8];
01182
01183 align_get_bits(gb);
01184
01185
01186 s->sbwidth = DIVRNDUP(s->source.width, 4*s->plane[0].xbsep);
01187 s->sbheight = DIVRNDUP(s->source.height, 4*s->plane[0].ybsep);
01188 s->blwidth = 4 * s->sbwidth;
01189 s->blheight = 4 * s->sbheight;
01190
01191
01192
01193 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
01194 for (y = 0; y < s->sbheight; y++) {
01195 for (x = 0; x < s->sbwidth; x++) {
01196 int split = dirac_get_arith_uint(arith, CTX_SB_F1, CTX_SB_DATA);
01197 sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3;
01198 }
01199 sbsplit += s->sbwidth;
01200 }
01201
01202
01203 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
01204 for (i = 0; i < s->num_refs; i++) {
01205 ff_dirac_init_arith_decoder(arith + 4 + 2 * i, gb, svq3_get_ue_golomb(gb));
01206 ff_dirac_init_arith_decoder(arith + 5 + 2 * i, gb, svq3_get_ue_golomb(gb));
01207 }
01208 for (i = 0; i < 3; i++)
01209 ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb));
01210
01211 for (y = 0; y < s->sbheight; y++)
01212 for (x = 0; x < s->sbwidth; x++) {
01213 int blkcnt = 1 << s->sbsplit[y * s->sbwidth + x];
01214 int step = 4 >> s->sbsplit[y * s->sbwidth + x];
01215
01216 for (q = 0; q < blkcnt; q++)
01217 for (p = 0; p < blkcnt; p++) {
01218 int bx = 4 * x + p*step;
01219 int by = 4 * y + q*step;
01220 DiracBlock *block = &s->blmotion[by*s->blwidth + bx];
01221 decode_block_params(s, arith, block, s->blwidth, bx, by);
01222 propagate_block_data(block, s->blwidth, step);
01223 }
01224 }
01225 }
01226
01227 static int weight(int i, int blen, int offset)
01228 {
01229 #define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) : \
01230 (1 + (6*(i) + offset - 1) / (2*offset - 1))
01231
01232 if (i < 2*offset)
01233 return ROLLOFF(i);
01234 else if (i > blen-1 - 2*offset)
01235 return ROLLOFF(blen-1 - i);
01236 return 8;
01237 }
01238
01239 static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride,
01240 int left, int right, int wy)
01241 {
01242 int x;
01243 for (x = 0; left && x < p->xblen >> 1; x++)
01244 obmc_weight[x] = wy*8;
01245 for (; x < p->xblen >> right; x++)
01246 obmc_weight[x] = wy*weight(x, p->xblen, p->xoffset);
01247 for (; x < p->xblen; x++)
01248 obmc_weight[x] = wy*8;
01249 for (; x < stride; x++)
01250 obmc_weight[x] = 0;
01251 }
01252
01253 static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride,
01254 int left, int right, int top, int bottom)
01255 {
01256 int y;
01257 for (y = 0; top && y < p->yblen >> 1; y++) {
01258 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
01259 obmc_weight += stride;
01260 }
01261 for (; y < p->yblen >> bottom; y++) {
01262 int wy = weight(y, p->yblen, p->yoffset);
01263 init_obmc_weight_row(p, obmc_weight, stride, left, right, wy);
01264 obmc_weight += stride;
01265 }
01266 for (; y < p->yblen; y++) {
01267 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
01268 obmc_weight += stride;
01269 }
01270 }
01271
01272 static void init_obmc_weights(DiracContext *s, Plane *p, int by)
01273 {
01274 int top = !by;
01275 int bottom = by == s->blheight-1;
01276
01277
01278 if (top || bottom || by == 1) {
01279 init_obmc_weight(p, s->obmc_weight[0], MAX_BLOCKSIZE, 1, 0, top, bottom);
01280 init_obmc_weight(p, s->obmc_weight[1], MAX_BLOCKSIZE, 0, 0, top, bottom);
01281 init_obmc_weight(p, s->obmc_weight[2], MAX_BLOCKSIZE, 0, 1, top, bottom);
01282 }
01283 }
01284
01285 static const uint8_t epel_weights[4][4][4] = {
01286 {{ 16, 0, 0, 0 },
01287 { 12, 4, 0, 0 },
01288 { 8, 8, 0, 0 },
01289 { 4, 12, 0, 0 }},
01290 {{ 12, 0, 4, 0 },
01291 { 9, 3, 3, 1 },
01292 { 6, 6, 2, 2 },
01293 { 3, 9, 1, 3 }},
01294 {{ 8, 0, 8, 0 },
01295 { 6, 2, 6, 2 },
01296 { 4, 4, 4, 4 },
01297 { 2, 6, 2, 6 }},
01298 {{ 4, 0, 12, 0 },
01299 { 3, 1, 9, 3 },
01300 { 2, 2, 6, 6 },
01301 { 1, 3, 3, 9 }}
01302 };
01303
01312 static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5],
01313 int x, int y, int ref, int plane)
01314 {
01315 Plane *p = &s->plane[plane];
01316 uint8_t **ref_hpel = s->ref_pics[ref]->hpel[plane];
01317 int motion_x = block->u.mv[ref][0];
01318 int motion_y = block->u.mv[ref][1];
01319 int mx, my, i, epel, nplanes = 0;
01320
01321 if (plane) {
01322 motion_x >>= s->chroma_x_shift;
01323 motion_y >>= s->chroma_y_shift;
01324 }
01325
01326 mx = motion_x & ~(-1 << s->mv_precision);
01327 my = motion_y & ~(-1 << s->mv_precision);
01328 motion_x >>= s->mv_precision;
01329 motion_y >>= s->mv_precision;
01330
01331
01332 mx <<= 3 - s->mv_precision;
01333 my <<= 3 - s->mv_precision;
01334
01335 x += motion_x;
01336 y += motion_y;
01337 epel = (mx|my)&1;
01338
01339
01340 if (!((mx|my)&3)) {
01341 nplanes = 1;
01342 src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x;
01343 } else {
01344
01345 nplanes = 4;
01346 for (i = 0; i < 4; i++)
01347 src[i] = ref_hpel[i] + y*p->stride + x;
01348
01349
01350
01351 if (mx > 4) {
01352 src[0] += 1;
01353 src[2] += 1;
01354 x++;
01355 }
01356 if (my > 4) {
01357 src[0] += p->stride;
01358 src[1] += p->stride;
01359 y++;
01360 }
01361
01362
01363
01364
01365 if (!epel) {
01366
01367
01368 if (!(mx&3)) {
01369
01370
01371 src[!mx] = src[2 + !!mx];
01372 nplanes = 2;
01373 } else if (!(my&3)) {
01374 src[0] = src[(my>>1) ];
01375 src[1] = src[(my>>1)+1];
01376 nplanes = 2;
01377 }
01378 } else {
01379
01380 if (mx > 4) {
01381 FFSWAP(const uint8_t *, src[0], src[1]);
01382 FFSWAP(const uint8_t *, src[2], src[3]);
01383 }
01384 if (my > 4) {
01385 FFSWAP(const uint8_t *, src[0], src[2]);
01386 FFSWAP(const uint8_t *, src[1], src[3]);
01387 }
01388 src[4] = epel_weights[my&3][mx&3];
01389 }
01390 }
01391
01392
01393 if ((unsigned)x > p->width +EDGE_WIDTH/2 - p->xblen ||
01394 (unsigned)y > p->height+EDGE_WIDTH/2 - p->yblen) {
01395 for (i = 0; i < nplanes; i++) {
01396 ff_emulated_edge_mc(s->edge_emu_buffer[i], src[i], p->stride,
01397 p->xblen, p->yblen, x, y,
01398 p->width+EDGE_WIDTH/2, p->height+EDGE_WIDTH/2);
01399 src[i] = s->edge_emu_buffer[i];
01400 }
01401 }
01402 return (nplanes>>1) + epel;
01403 }
01404
01405 static void add_dc(uint16_t *dst, int dc, int stride,
01406 uint8_t *obmc_weight, int xblen, int yblen)
01407 {
01408 int x, y;
01409 dc += 128;
01410
01411 for (y = 0; y < yblen; y++) {
01412 for (x = 0; x < xblen; x += 2) {
01413 dst[x ] += dc * obmc_weight[x ];
01414 dst[x+1] += dc * obmc_weight[x+1];
01415 }
01416 dst += stride;
01417 obmc_weight += MAX_BLOCKSIZE;
01418 }
01419 }
01420
01421 static void block_mc(DiracContext *s, DiracBlock *block,
01422 uint16_t *mctmp, uint8_t *obmc_weight,
01423 int plane, int dstx, int dsty)
01424 {
01425 Plane *p = &s->plane[plane];
01426 const uint8_t *src[5];
01427 int idx;
01428
01429 switch (block->ref&3) {
01430 case 0:
01431 add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen);
01432 return;
01433 case 1:
01434 case 2:
01435 idx = mc_subpel(s, block, src, dstx, dsty, (block->ref&3)-1, plane);
01436 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01437 if (s->weight_func)
01438 s->weight_func(s->mcscratch, p->stride, s->weight_log2denom,
01439 s->weight[0] + s->weight[1], p->yblen);
01440 break;
01441 case 3:
01442 idx = mc_subpel(s, block, src, dstx, dsty, 0, plane);
01443 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01444 idx = mc_subpel(s, block, src, dstx, dsty, 1, plane);
01445 if (s->biweight_func) {
01446
01447 s->put_pixels_tab[idx](s->mcscratch + 32, src, p->stride, p->yblen);
01448 s->biweight_func(s->mcscratch, s->mcscratch+32, p->stride, s->weight_log2denom,
01449 s->weight[0], s->weight[1], p->yblen);
01450 } else
01451 s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01452 break;
01453 }
01454 s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen);
01455 }
01456
01457 static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
01458 {
01459 Plane *p = &s->plane[plane];
01460 int x, dstx = p->xbsep - p->xoffset;
01461
01462 block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty);
01463 mctmp += p->xbsep;
01464
01465 for (x = 1; x < s->blwidth-1; x++) {
01466 block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty);
01467 dstx += p->xbsep;
01468 mctmp += p->xbsep;
01469 }
01470 block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty);
01471 }
01472
01473 static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
01474 {
01475 int idx = 0;
01476 if (xblen > 8)
01477 idx = 1;
01478 if (xblen > 16)
01479 idx = 2;
01480
01481 memcpy(s->put_pixels_tab, s->diracdsp.put_dirac_pixels_tab[idx], sizeof(s->put_pixels_tab));
01482 memcpy(s->avg_pixels_tab, s->diracdsp.avg_dirac_pixels_tab[idx], sizeof(s->avg_pixels_tab));
01483 s->add_obmc = s->diracdsp.add_dirac_obmc[idx];
01484 if (s->weight_log2denom > 1 || s->weight[0] != 1 || s->weight[1] != 1) {
01485 s->weight_func = s->diracdsp.weight_dirac_pixels_tab[idx];
01486 s->biweight_func = s->diracdsp.biweight_dirac_pixels_tab[idx];
01487 } else {
01488 s->weight_func = NULL;
01489 s->biweight_func = NULL;
01490 }
01491 }
01492
01493 static void interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height)
01494 {
01495
01496
01497
01498 int i, edge = EDGE_WIDTH/2;
01499
01500 ref->hpel[plane][0] = ref->avframe.data[plane];
01501 s->dsp.draw_edges(ref->hpel[plane][0], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01502
01503
01504 if (!s->mv_precision)
01505 return;
01506
01507 for (i = 1; i < 4; i++) {
01508 if (!ref->hpel_base[plane][i])
01509 ref->hpel_base[plane][i] = av_malloc((height+2*edge) * ref->avframe.linesize[plane] + 32);
01510
01511 ref->hpel[plane][i] = ref->hpel_base[plane][i] + edge*ref->avframe.linesize[plane] + 16;
01512 }
01513
01514 if (!ref->interpolated[plane]) {
01515 s->diracdsp.dirac_hpel_filter(ref->hpel[plane][1], ref->hpel[plane][2],
01516 ref->hpel[plane][3], ref->hpel[plane][0],
01517 ref->avframe.linesize[plane], width, height);
01518 s->dsp.draw_edges(ref->hpel[plane][1], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01519 s->dsp.draw_edges(ref->hpel[plane][2], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01520 s->dsp.draw_edges(ref->hpel[plane][3], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01521 }
01522 ref->interpolated[plane] = 1;
01523 }
01524
01529 static int dirac_decode_frame_internal(DiracContext *s)
01530 {
01531 DWTContext d;
01532 int y, i, comp, dsty;
01533
01534 if (s->low_delay) {
01535
01536 for (comp = 0; comp < 3; comp++) {
01537 Plane *p = &s->plane[comp];
01538 memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM));
01539 }
01540 if (!s->zero_res)
01541 decode_lowdelay(s);
01542 }
01543
01544 for (comp = 0; comp < 3; comp++) {
01545 Plane *p = &s->plane[comp];
01546 uint8_t *frame = s->current_picture->avframe.data[comp];
01547
01548
01549 for (i = 0; i < 4; i++)
01550 s->edge_emu_buffer[i] = s->edge_emu_buffer_base + i*FFALIGN(p->width, 16);
01551
01552 if (!s->zero_res && !s->low_delay)
01553 {
01554 memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM));
01555 decode_component(s, comp);
01556 }
01557 if (ff_spatial_idwt_init2(&d, p->idwt_buf, p->idwt_width, p->idwt_height, p->idwt_stride,
01558 s->wavelet_idx+2, s->wavelet_depth, p->idwt_tmp))
01559 return -1;
01560
01561 if (!s->num_refs) {
01562 for (y = 0; y < p->height; y += 16) {
01563 ff_spatial_idwt_slice2(&d, y+16);
01564 s->diracdsp.put_signed_rect_clamped(frame + y*p->stride, p->stride,
01565 p->idwt_buf + y*p->idwt_stride, p->idwt_stride, p->width, 16);
01566 }
01567 } else {
01568 int rowheight = p->ybsep*p->stride;
01569
01570 select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen);
01571
01572 for (i = 0; i < s->num_refs; i++)
01573 interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height);
01574
01575 memset(s->mctmp, 0, 4*p->yoffset*p->stride);
01576
01577 dsty = -p->yoffset;
01578 for (y = 0; y < s->blheight; y++) {
01579 int h = 0,
01580 start = FFMAX(dsty, 0);
01581 uint16_t *mctmp = s->mctmp + y*rowheight;
01582 DiracBlock *blocks = s->blmotion + y*s->blwidth;
01583
01584 init_obmc_weights(s, p, y);
01585
01586 if (y == s->blheight-1 || start+p->ybsep > p->height)
01587 h = p->height - start;
01588 else
01589 h = p->ybsep - (start - dsty);
01590 if (h < 0)
01591 break;
01592
01593 memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight);
01594 mc_row(s, blocks, mctmp, comp, dsty);
01595
01596 mctmp += (start - dsty)*p->stride + p->xoffset;
01597 ff_spatial_idwt_slice2(&d, start + h);
01598 s->diracdsp.add_rect_clamped(frame + start*p->stride, mctmp, p->stride,
01599 p->idwt_buf + start*p->idwt_stride, p->idwt_stride, p->width, h);
01600
01601 dsty += p->ybsep;
01602 }
01603 }
01604 }
01605
01606
01607 return 0;
01608 }
01609
01614 static int dirac_decode_picture_header(DiracContext *s)
01615 {
01616 int retire, picnum;
01617 int i, j, refnum, refdist;
01618 GetBitContext *gb = &s->gb;
01619
01620
01621 picnum = s->current_picture->avframe.display_picture_number = get_bits_long(gb, 32);
01622
01623
01624 av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);
01625
01626
01627
01628 if (s->frame_number < 0)
01629 s->frame_number = picnum;
01630
01631 s->ref_pics[0] = s->ref_pics[1] = NULL;
01632 for (i = 0; i < s->num_refs; i++) {
01633 refnum = picnum + dirac_get_se_golomb(gb);
01634 refdist = INT_MAX;
01635
01636
01637
01638 for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)
01639 if (s->ref_frames[j]
01640 && FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum) < refdist) {
01641 s->ref_pics[i] = s->ref_frames[j];
01642 refdist = FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum);
01643 }
01644
01645 if (!s->ref_pics[i] || refdist)
01646 av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");
01647
01648
01649 if (!s->ref_pics[i])
01650 for (j = 0; j < MAX_FRAMES; j++)
01651 if (!s->all_frames[j].avframe.data[0]) {
01652 s->ref_pics[i] = &s->all_frames[j];
01653 s->avctx->get_buffer(s->avctx, &s->ref_pics[i]->avframe);
01654 }
01655 }
01656
01657
01658 if (s->current_picture->avframe.reference) {
01659 retire = picnum + dirac_get_se_golomb(gb);
01660 if (retire != picnum) {
01661 DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);
01662
01663 if (retire_pic)
01664 retire_pic->avframe.reference &= DELAYED_PIC_REF;
01665 else
01666 av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");
01667 }
01668
01669
01670 while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {
01671 av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");
01672 remove_frame(s->ref_frames, s->ref_frames[0]->avframe.display_picture_number)->avframe.reference &= DELAYED_PIC_REF;
01673 }
01674 }
01675
01676 if (s->num_refs) {
01677 if (dirac_unpack_prediction_parameters(s))
01678 return -1;
01679 dirac_unpack_block_motion_data(s);
01680 }
01681 if (dirac_unpack_idwt_params(s))
01682 return -1;
01683
01684 init_planes(s);
01685 return 0;
01686 }
01687
01688 static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *data_size)
01689 {
01690 DiracFrame *out = s->delay_frames[0];
01691 int i, out_idx = 0;
01692
01693
01694 for (i = 1; s->delay_frames[i]; i++)
01695 if (s->delay_frames[i]->avframe.display_picture_number < out->avframe.display_picture_number) {
01696 out = s->delay_frames[i];
01697 out_idx = i;
01698 }
01699
01700 for (i = out_idx; s->delay_frames[i]; i++)
01701 s->delay_frames[i] = s->delay_frames[i+1];
01702
01703 if (out) {
01704 out->avframe.reference ^= DELAYED_PIC_REF;
01705 *data_size = sizeof(AVFrame);
01706 *(AVFrame *)picture = out->avframe;
01707 }
01708
01709 return 0;
01710 }
01711
01717 #define DATA_UNIT_HEADER_SIZE 13
01718
01719
01720
01721 static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size)
01722 {
01723 DiracContext *s = avctx->priv_data;
01724 DiracFrame *pic = NULL;
01725 int i, parse_code = buf[4];
01726
01727 if (size < DATA_UNIT_HEADER_SIZE)
01728 return -1;
01729
01730 init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE));
01731
01732 if (parse_code == pc_seq_header) {
01733 if (s->seen_sequence_header)
01734 return 0;
01735
01736
01737 if (avpriv_dirac_parse_sequence_header(avctx, &s->gb, &s->source))
01738 return -1;
01739
01740 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
01741
01742 if (alloc_sequence_buffers(s))
01743 return -1;
01744
01745 s->seen_sequence_header = 1;
01746 } else if (parse_code == pc_eos) {
01747 free_sequence_buffers(s);
01748 s->seen_sequence_header = 0;
01749 } else if (parse_code == pc_aux_data) {
01750 if (buf[13] == 1) {
01751 int ver[3];
01752
01753
01754 if (sscanf(buf+14, "Schroedinger %d.%d.%d", ver, ver+1, ver+2) == 3)
01755 if (ver[0] == 1 && ver[1] == 0 && ver[2] <= 7)
01756 s->old_delta_quant = 1;
01757 }
01758 } else if (parse_code & 0x8) {
01759 if (!s->seen_sequence_header) {
01760 av_log(avctx, AV_LOG_DEBUG, "Dropping frame without sequence header\n");
01761 return -1;
01762 }
01763
01764
01765 for (i = 0; i < MAX_FRAMES; i++)
01766 if (s->all_frames[i].avframe.data[0] == NULL)
01767 pic = &s->all_frames[i];
01768 if (!pic) {
01769 av_log(avctx, AV_LOG_ERROR, "framelist full\n");
01770 return -1;
01771 }
01772
01773 avcodec_get_frame_defaults(&pic->avframe);
01774
01775
01776 s->num_refs = parse_code & 0x03;
01777 s->is_arith = (parse_code & 0x48) == 0x08;
01778 s->low_delay = (parse_code & 0x88) == 0x88;
01779 pic->avframe.reference = (parse_code & 0x0C) == 0x0C;
01780 pic->avframe.key_frame = s->num_refs == 0;
01781 pic->avframe.pict_type = s->num_refs + 1;
01782
01783 if (avctx->get_buffer(avctx, &pic->avframe) < 0) {
01784 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01785 return -1;
01786 }
01787 s->current_picture = pic;
01788 s->plane[0].stride = pic->avframe.linesize[0];
01789 s->plane[1].stride = pic->avframe.linesize[1];
01790 s->plane[2].stride = pic->avframe.linesize[2];
01791
01792
01793 if (dirac_decode_picture_header(s))
01794 return -1;
01795
01796
01797 if (dirac_decode_frame_internal(s))
01798 return -1;
01799 }
01800 return 0;
01801 }
01802
01803 static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
01804 {
01805 DiracContext *s = avctx->priv_data;
01806 DiracFrame *picture = data;
01807 uint8_t *buf = pkt->data;
01808 int buf_size = pkt->size;
01809 int i, data_unit_size, buf_idx = 0;
01810
01811
01812 for (i = 0; i < MAX_FRAMES; i++)
01813 if (s->all_frames[i].avframe.data[0] && !s->all_frames[i].avframe.reference) {
01814 avctx->release_buffer(avctx, &s->all_frames[i].avframe);
01815 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
01816 }
01817
01818 s->current_picture = NULL;
01819 *data_size = 0;
01820
01821
01822 if (buf_size == 0)
01823 return get_delayed_pic(s, (AVFrame *)data, data_size);
01824
01825 for (;;) {
01826
01827
01828
01829 for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {
01830 if (buf[buf_idx ] == 'B' && buf[buf_idx+1] == 'B' &&
01831 buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')
01832 break;
01833 }
01834
01835 if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)
01836 break;
01837
01838 data_unit_size = AV_RB32(buf+buf_idx+5);
01839 if (buf_idx + data_unit_size > buf_size || !data_unit_size) {
01840 if(buf_idx + data_unit_size > buf_size)
01841 av_log(s->avctx, AV_LOG_ERROR,
01842 "Data unit with size %d is larger than input buffer, discarding\n",
01843 data_unit_size);
01844 buf_idx += 4;
01845 continue;
01846 }
01847
01848 if (dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size))
01849 {
01850 av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");
01851 return -1;
01852 }
01853 buf_idx += data_unit_size;
01854 }
01855
01856 if (!s->current_picture)
01857 return 0;
01858
01859 if (s->current_picture->avframe.display_picture_number > s->frame_number) {
01860 DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);
01861
01862 s->current_picture->avframe.reference |= DELAYED_PIC_REF;
01863
01864 if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {
01865 int min_num = s->delay_frames[0]->avframe.display_picture_number;
01866
01867 av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");
01868 delayed_frame = s->delay_frames[0];
01869
01870 for (i = 1; s->delay_frames[i]; i++)
01871 if (s->delay_frames[i]->avframe.display_picture_number < min_num)
01872 min_num = s->delay_frames[i]->avframe.display_picture_number;
01873
01874 delayed_frame = remove_frame(s->delay_frames, min_num);
01875 add_frame(s->delay_frames, MAX_DELAY, s->current_picture);
01876 }
01877
01878 if (delayed_frame) {
01879 delayed_frame->avframe.reference ^= DELAYED_PIC_REF;
01880 *(AVFrame*)data = delayed_frame->avframe;
01881 *data_size = sizeof(AVFrame);
01882 }
01883 } else if (s->current_picture->avframe.display_picture_number == s->frame_number) {
01884
01885 *(AVFrame*)data = s->current_picture->avframe;
01886 *data_size = sizeof(AVFrame);
01887 }
01888
01889 if (*data_size)
01890 s->frame_number = picture->avframe.display_picture_number + 1;
01891
01892 return buf_idx;
01893 }
01894
01895 AVCodec ff_dirac_decoder = {
01896 .name = "dirac",
01897 .type = AVMEDIA_TYPE_VIDEO,
01898 .id = CODEC_ID_DIRAC,
01899 .priv_data_size = sizeof(DiracContext),
01900 .init = dirac_decode_init,
01901 .encode = NULL,
01902 .close = dirac_decode_end,
01903 .decode = dirac_decode_frame,
01904 .capabilities = CODEC_CAP_DELAY,
01905 .flush = dirac_decode_flush,
01906 .long_name = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"),
01907 };