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 ff_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 int quant = b->quant;
00495 if (is_arith)
00496 quant += dirac_get_arith_int(c, CTX_DELTA_Q_F, CTX_DELTA_Q_DATA);
00497 else
00498 quant += dirac_get_se_golomb(gb);
00499 if (quant < 0) {
00500 av_log(s->avctx, AV_LOG_ERROR, "Invalid quant\n");
00501 return;
00502 }
00503 b->quant = quant;
00504 }
00505
00506 b->quant = FFMIN(b->quant, MAX_QUANT);
00507
00508 qfactor = qscale_tab[b->quant];
00509
00510 if (!s->num_refs)
00511 qoffset = qoffset_intra_tab[b->quant];
00512 else
00513 qoffset = qoffset_inter_tab[b->quant];
00514
00515 buf = b->ibuf + top * b->stride;
00516 for (y = top; y < bottom; y++) {
00517 for (x = left; x < right; x++) {
00518
00519 if (is_arith)
00520 coeff_unpack_arith(c, qfactor, qoffset, b, buf+x, x, y);
00521 else
00522 buf[x] = coeff_unpack_golomb(gb, qfactor, qoffset);
00523 }
00524 buf += b->stride;
00525 }
00526 }
00527
00532 static inline void intra_dc_prediction(SubBand *b)
00533 {
00534 IDWTELEM *buf = b->ibuf;
00535 int x, y;
00536
00537 for (x = 1; x < b->width; x++)
00538 buf[x] += buf[x-1];
00539 buf += b->stride;
00540
00541 for (y = 1; y < b->height; y++) {
00542 buf[0] += buf[-b->stride];
00543
00544 for (x = 1; x < b->width; x++) {
00545 int pred = buf[x - 1] + buf[x - b->stride] + buf[x - b->stride-1];
00546 buf[x] += divide3(pred);
00547 }
00548 buf += b->stride;
00549 }
00550 }
00551
00556 static av_always_inline void decode_subband_internal(DiracContext *s, SubBand *b, int is_arith)
00557 {
00558 int cb_x, cb_y, left, right, top, bottom;
00559 DiracArith c;
00560 GetBitContext gb;
00561 int cb_width = s->codeblock[b->level + (b->orientation != subband_ll)].width;
00562 int cb_height = s->codeblock[b->level + (b->orientation != subband_ll)].height;
00563 int blockcnt_one = (cb_width + cb_height) == 2;
00564
00565 if (!b->length)
00566 return;
00567
00568 init_get_bits(&gb, b->coeff_data, b->length*8);
00569
00570 if (is_arith)
00571 ff_dirac_init_arith_decoder(&c, &gb, b->length);
00572
00573 top = 0;
00574 for (cb_y = 0; cb_y < cb_height; cb_y++) {
00575 bottom = (b->height * (cb_y+1)) / cb_height;
00576 left = 0;
00577 for (cb_x = 0; cb_x < cb_width; cb_x++) {
00578 right = (b->width * (cb_x+1)) / cb_width;
00579 codeblock(s, b, &gb, &c, left, right, top, bottom, blockcnt_one, is_arith);
00580 left = right;
00581 }
00582 top = bottom;
00583 }
00584
00585 if (b->orientation == subband_ll && s->num_refs == 0)
00586 intra_dc_prediction(b);
00587 }
00588
00589 static int decode_subband_arith(AVCodecContext *avctx, void *b)
00590 {
00591 DiracContext *s = avctx->priv_data;
00592 decode_subband_internal(s, b, 1);
00593 return 0;
00594 }
00595
00596 static int decode_subband_golomb(AVCodecContext *avctx, void *arg)
00597 {
00598 DiracContext *s = avctx->priv_data;
00599 SubBand **b = arg;
00600 decode_subband_internal(s, *b, 0);
00601 return 0;
00602 }
00603
00608 static void decode_component(DiracContext *s, int comp)
00609 {
00610 AVCodecContext *avctx = s->avctx;
00611 SubBand *bands[3*MAX_DWT_LEVELS+1];
00612 enum dirac_subband orientation;
00613 int level, num_bands = 0;
00614
00615
00616 for (level = 0; level < s->wavelet_depth; level++) {
00617 for (orientation = !!level; orientation < 4; orientation++) {
00618 SubBand *b = &s->plane[comp].band[level][orientation];
00619 bands[num_bands++] = b;
00620
00621 align_get_bits(&s->gb);
00622
00623 b->length = svq3_get_ue_golomb(&s->gb);
00624 if (b->length) {
00625 b->quant = svq3_get_ue_golomb(&s->gb);
00626 align_get_bits(&s->gb);
00627 b->coeff_data = s->gb.buffer + get_bits_count(&s->gb)/8;
00628 b->length = FFMIN(b->length, FFMAX(get_bits_left(&s->gb)/8, 0));
00629 skip_bits_long(&s->gb, b->length*8);
00630 }
00631 }
00632
00633 if (s->is_arith)
00634 avctx->execute(avctx, decode_subband_arith, &s->plane[comp].band[level][!!level],
00635 NULL, 4-!!level, sizeof(SubBand));
00636 }
00637
00638 if (!s->is_arith)
00639 avctx->execute(avctx, decode_subband_golomb, bands, NULL, num_bands, sizeof(SubBand*));
00640 }
00641
00642
00643
00644 static void lowdelay_subband(DiracContext *s, GetBitContext *gb, int quant,
00645 int slice_x, int slice_y, int bits_end,
00646 SubBand *b1, SubBand *b2)
00647 {
00648 int left = b1->width * slice_x / s->lowdelay.num_x;
00649 int right = b1->width *(slice_x+1) / s->lowdelay.num_x;
00650 int top = b1->height * slice_y / s->lowdelay.num_y;
00651 int bottom = b1->height *(slice_y+1) / s->lowdelay.num_y;
00652
00653 int qfactor = qscale_tab[FFMIN(quant, MAX_QUANT)];
00654 int qoffset = qoffset_intra_tab[FFMIN(quant, MAX_QUANT)];
00655
00656 IDWTELEM *buf1 = b1->ibuf + top * b1->stride;
00657 IDWTELEM *buf2 = b2 ? b2->ibuf + top * b2->stride : NULL;
00658 int x, y;
00659
00660
00661 if (get_bits_count(gb) >= bits_end)
00662 return;
00663
00664 for (y = top; y < bottom; y++) {
00665 for (x = left; x < right; x++) {
00666 buf1[x] = coeff_unpack_golomb(gb, qfactor, qoffset);
00667 if (get_bits_count(gb) >= bits_end)
00668 return;
00669 if (buf2) {
00670 buf2[x] = coeff_unpack_golomb(gb, qfactor, qoffset);
00671 if (get_bits_count(gb) >= bits_end)
00672 return;
00673 }
00674 }
00675 buf1 += b1->stride;
00676 if (buf2)
00677 buf2 += b2->stride;
00678 }
00679 }
00680
00681 struct lowdelay_slice {
00682 GetBitContext gb;
00683 int slice_x;
00684 int slice_y;
00685 int bytes;
00686 };
00687
00688
00693 static int decode_lowdelay_slice(AVCodecContext *avctx, void *arg)
00694 {
00695 DiracContext *s = avctx->priv_data;
00696 struct lowdelay_slice *slice = arg;
00697 GetBitContext *gb = &slice->gb;
00698 enum dirac_subband orientation;
00699 int level, quant, chroma_bits, chroma_end;
00700
00701 int quant_base = get_bits(gb, 7);
00702 int length_bits = av_log2(8 * slice->bytes)+1;
00703 int luma_bits = get_bits_long(gb, length_bits);
00704 int luma_end = get_bits_count(gb) + FFMIN(luma_bits, get_bits_left(gb));
00705
00706
00707 for (level = 0; level < s->wavelet_depth; level++)
00708 for (orientation = !!level; orientation < 4; orientation++) {
00709 quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
00710 lowdelay_subband(s, gb, quant, slice->slice_x, slice->slice_y, luma_end,
00711 &s->plane[0].band[level][orientation], NULL);
00712 }
00713
00714
00715 skip_bits_long(gb, get_bits_count(gb) - luma_end);
00716
00717 chroma_bits = 8*slice->bytes - 7 - length_bits - luma_bits;
00718 chroma_end = get_bits_count(gb) + FFMIN(chroma_bits, get_bits_left(gb));
00719
00720 for (level = 0; level < s->wavelet_depth; level++)
00721 for (orientation = !!level; orientation < 4; orientation++) {
00722 quant = FFMAX(quant_base - s->lowdelay.quant[level][orientation], 0);
00723 lowdelay_subband(s, gb, quant, slice->slice_x, slice->slice_y, chroma_end,
00724 &s->plane[1].band[level][orientation],
00725 &s->plane[2].band[level][orientation]);
00726 }
00727
00728 return 0;
00729 }
00730
00735 static void decode_lowdelay(DiracContext *s)
00736 {
00737 AVCodecContext *avctx = s->avctx;
00738 int slice_x, slice_y, bytes, bufsize;
00739 const uint8_t *buf;
00740 struct lowdelay_slice *slices;
00741 int slice_num = 0;
00742
00743 slices = av_mallocz(s->lowdelay.num_x * s->lowdelay.num_y * sizeof(struct lowdelay_slice));
00744
00745 align_get_bits(&s->gb);
00746
00747 buf = s->gb.buffer + get_bits_count(&s->gb)/8;
00748 bufsize = get_bits_left(&s->gb);
00749
00750 for (slice_y = 0; bufsize > 0 && slice_y < s->lowdelay.num_y; slice_y++)
00751 for (slice_x = 0; bufsize > 0 && slice_x < s->lowdelay.num_x; slice_x++) {
00752 bytes = (slice_num+1) * s->lowdelay.bytes.num / s->lowdelay.bytes.den
00753 - slice_num * s->lowdelay.bytes.num / s->lowdelay.bytes.den;
00754
00755 slices[slice_num].bytes = bytes;
00756 slices[slice_num].slice_x = slice_x;
00757 slices[slice_num].slice_y = slice_y;
00758 init_get_bits(&slices[slice_num].gb, buf, bufsize);
00759 slice_num++;
00760
00761 buf += bytes;
00762 bufsize -= bytes*8;
00763 }
00764
00765 avctx->execute(avctx, decode_lowdelay_slice, slices, NULL, slice_num,
00766 sizeof(struct lowdelay_slice));
00767 intra_dc_prediction(&s->plane[0].band[0][0]);
00768 intra_dc_prediction(&s->plane[1].band[0][0]);
00769 intra_dc_prediction(&s->plane[2].band[0][0]);
00770 av_free(slices);
00771 }
00772
00773 static void init_planes(DiracContext *s)
00774 {
00775 int i, w, h, level, orientation;
00776
00777 for (i = 0; i < 3; i++) {
00778 Plane *p = &s->plane[i];
00779
00780 p->width = s->source.width >> (i ? s->chroma_x_shift : 0);
00781 p->height = s->source.height >> (i ? s->chroma_y_shift : 0);
00782 p->idwt_width = w = CALC_PADDING(p->width , s->wavelet_depth);
00783 p->idwt_height = h = CALC_PADDING(p->height, s->wavelet_depth);
00784 p->idwt_stride = FFALIGN(p->idwt_width, 8);
00785
00786 for (level = s->wavelet_depth-1; level >= 0; level--) {
00787 w = w>>1;
00788 h = h>>1;
00789 for (orientation = !!level; orientation < 4; orientation++) {
00790 SubBand *b = &p->band[level][orientation];
00791
00792 b->ibuf = p->idwt_buf;
00793 b->level = level;
00794 b->stride = p->idwt_stride << (s->wavelet_depth - level);
00795 b->width = w;
00796 b->height = h;
00797 b->orientation = orientation;
00798
00799 if (orientation & 1)
00800 b->ibuf += w;
00801 if (orientation > 1)
00802 b->ibuf += b->stride>>1;
00803
00804 if (level)
00805 b->parent = &p->band[level-1][orientation];
00806 }
00807 }
00808
00809 if (i > 0) {
00810 p->xblen = s->plane[0].xblen >> s->chroma_x_shift;
00811 p->yblen = s->plane[0].yblen >> s->chroma_y_shift;
00812 p->xbsep = s->plane[0].xbsep >> s->chroma_x_shift;
00813 p->ybsep = s->plane[0].ybsep >> s->chroma_y_shift;
00814 }
00815
00816 p->xoffset = (p->xblen - p->xbsep)/2;
00817 p->yoffset = (p->yblen - p->ybsep)/2;
00818 }
00819 }
00820
00826 static int dirac_unpack_prediction_parameters(DiracContext *s)
00827 {
00828 static const uint8_t default_blen[] = { 4, 12, 16, 24 };
00829 static const uint8_t default_bsep[] = { 4, 8, 12, 16 };
00830
00831 GetBitContext *gb = &s->gb;
00832 unsigned idx, ref;
00833
00834 align_get_bits(gb);
00835
00836
00837 idx = svq3_get_ue_golomb(gb);
00838
00839 if (idx > 4) {
00840 av_log(s->avctx, AV_LOG_ERROR, "Block prediction index too high\n");
00841 return -1;
00842 }
00843
00844 if (idx == 0) {
00845 s->plane[0].xblen = svq3_get_ue_golomb(gb);
00846 s->plane[0].yblen = svq3_get_ue_golomb(gb);
00847 s->plane[0].xbsep = svq3_get_ue_golomb(gb);
00848 s->plane[0].ybsep = svq3_get_ue_golomb(gb);
00849 } else {
00850
00851 s->plane[0].xblen = default_blen[idx-1];
00852 s->plane[0].yblen = default_blen[idx-1];
00853 s->plane[0].xbsep = default_bsep[idx-1];
00854 s->plane[0].ybsep = default_bsep[idx-1];
00855 }
00856
00857
00858
00859 if (!s->plane[0].xbsep || !s->plane[0].ybsep || s->plane[0].xbsep < s->plane[0].xblen/2 || s->plane[0].ybsep < s->plane[0].yblen/2) {
00860 av_log(s->avctx, AV_LOG_ERROR, "Block separation too small\n");
00861 return -1;
00862 }
00863 if (s->plane[0].xbsep > s->plane[0].xblen || s->plane[0].ybsep > s->plane[0].yblen) {
00864 av_log(s->avctx, AV_LOG_ERROR, "Block separation greater than size\n");
00865 return -1;
00866 }
00867 if (FFMAX(s->plane[0].xblen, s->plane[0].yblen) > MAX_BLOCKSIZE) {
00868 av_log(s->avctx, AV_LOG_ERROR, "Unsupported large block size\n");
00869 return -1;
00870 }
00871
00872
00873
00874 s->mv_precision = svq3_get_ue_golomb(gb);
00875 if (s->mv_precision > 3) {
00876 av_log(s->avctx, AV_LOG_ERROR, "MV precision finer than eighth-pel\n");
00877 return -1;
00878 }
00879
00880
00881
00882 s->globalmc_flag = get_bits1(gb);
00883 if (s->globalmc_flag) {
00884 memset(s->globalmc, 0, sizeof(s->globalmc));
00885
00886 for (ref = 0; ref < s->num_refs; ref++) {
00887 if (get_bits1(gb)) {
00888 s->globalmc[ref].pan_tilt[0] = dirac_get_se_golomb(gb);
00889 s->globalmc[ref].pan_tilt[1] = dirac_get_se_golomb(gb);
00890 }
00891
00892
00893 if (get_bits1(gb)) {
00894 s->globalmc[ref].zrs_exp = svq3_get_ue_golomb(gb);
00895 s->globalmc[ref].zrs[0][0] = dirac_get_se_golomb(gb);
00896 s->globalmc[ref].zrs[0][1] = dirac_get_se_golomb(gb);
00897 s->globalmc[ref].zrs[1][0] = dirac_get_se_golomb(gb);
00898 s->globalmc[ref].zrs[1][1] = dirac_get_se_golomb(gb);
00899 } else {
00900 s->globalmc[ref].zrs[0][0] = 1;
00901 s->globalmc[ref].zrs[1][1] = 1;
00902 }
00903
00904 if (get_bits1(gb)) {
00905 s->globalmc[ref].perspective_exp = svq3_get_ue_golomb(gb);
00906 s->globalmc[ref].perspective[0] = dirac_get_se_golomb(gb);
00907 s->globalmc[ref].perspective[1] = dirac_get_se_golomb(gb);
00908 }
00909 }
00910 }
00911
00912
00913
00914 if (svq3_get_ue_golomb(gb)) {
00915 av_log(s->avctx, AV_LOG_ERROR, "Unknown picture prediction mode\n");
00916 return -1;
00917 }
00918
00919
00920
00921 s->weight_log2denom = 1;
00922 s->weight[0] = 1;
00923 s->weight[1] = 1;
00924
00925 if (get_bits1(gb)) {
00926 s->weight_log2denom = svq3_get_ue_golomb(gb);
00927 s->weight[0] = dirac_get_se_golomb(gb);
00928 if (s->num_refs == 2)
00929 s->weight[1] = dirac_get_se_golomb(gb);
00930 }
00931 return 0;
00932 }
00933
00938 static int dirac_unpack_idwt_params(DiracContext *s)
00939 {
00940 GetBitContext *gb = &s->gb;
00941 int i, level;
00942 unsigned tmp;
00943
00944 #define CHECKEDREAD(dst, cond, errmsg) \
00945 tmp = svq3_get_ue_golomb(gb); \
00946 if (cond) { \
00947 av_log(s->avctx, AV_LOG_ERROR, errmsg); \
00948 return -1; \
00949 }\
00950 dst = tmp;
00951
00952 align_get_bits(gb);
00953
00954 s->zero_res = s->num_refs ? get_bits1(gb) : 0;
00955 if (s->zero_res)
00956 return 0;
00957
00958
00959 CHECKEDREAD(s->wavelet_idx, tmp > 6, "wavelet_idx is too big\n")
00960
00961 CHECKEDREAD(s->wavelet_depth, tmp > MAX_DWT_LEVELS || tmp < 1, "invalid number of DWT decompositions\n")
00962
00963 if (!s->low_delay) {
00964
00965 if (get_bits1(gb)) {
00966 for (i = 0; i <= s->wavelet_depth; i++) {
00967 CHECKEDREAD(s->codeblock[i].width , tmp < 1, "codeblock width invalid\n")
00968 CHECKEDREAD(s->codeblock[i].height, tmp < 1, "codeblock height invalid\n")
00969 }
00970
00971 CHECKEDREAD(s->codeblock_mode, tmp > 1, "unknown codeblock mode\n")
00972 } else
00973 for (i = 0; i <= s->wavelet_depth; i++)
00974 s->codeblock[i].width = s->codeblock[i].height = 1;
00975 } else {
00976
00977
00978 s->lowdelay.num_x = svq3_get_ue_golomb(gb);
00979 s->lowdelay.num_y = svq3_get_ue_golomb(gb);
00980 s->lowdelay.bytes.num = svq3_get_ue_golomb(gb);
00981 s->lowdelay.bytes.den = svq3_get_ue_golomb(gb);
00982
00983 if (s->lowdelay.bytes.den <= 0) {
00984 av_log(s->avctx,AV_LOG_ERROR,"Invalid lowdelay.bytes.den\n");
00985 return AVERROR_INVALIDDATA;
00986 }
00987
00988
00989 if (get_bits1(gb)) {
00990 av_log(s->avctx,AV_LOG_DEBUG,"Low Delay: Has Custom Quantization Matrix!\n");
00991
00992 s->lowdelay.quant[0][0] = svq3_get_ue_golomb(gb);
00993 for (level = 0; level < s->wavelet_depth; level++) {
00994 s->lowdelay.quant[level][1] = svq3_get_ue_golomb(gb);
00995 s->lowdelay.quant[level][2] = svq3_get_ue_golomb(gb);
00996 s->lowdelay.quant[level][3] = svq3_get_ue_golomb(gb);
00997 }
00998 } else {
00999
01000 for (level = 0; level < s->wavelet_depth; level++)
01001 for (i = 0; i < 4; i++) {
01002 s->lowdelay.quant[level][i] = default_qmat[s->wavelet_idx][level][i];
01003
01004 if (s->wavelet_idx == 3)
01005 s->lowdelay.quant[level][i] += 4*(s->wavelet_depth-1 - level);
01006 }
01007 }
01008 }
01009 return 0;
01010 }
01011
01012 static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
01013 {
01014 static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 };
01015
01016 if (!(x|y))
01017 return 0;
01018 else if (!y)
01019 return sbsplit[-1];
01020 else if (!x)
01021 return sbsplit[-stride];
01022
01023 return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]];
01024 }
01025
01026 static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
01027 {
01028 int pred;
01029
01030 if (!(x|y))
01031 return 0;
01032 else if (!y)
01033 return block[-1].ref & refmask;
01034 else if (!x)
01035 return block[-stride].ref & refmask;
01036
01037
01038 pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask);
01039 return (pred >> 1) & refmask;
01040 }
01041
01042 static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y)
01043 {
01044 int i, n = 0;
01045
01046 memset(block->u.dc, 0, sizeof(block->u.dc));
01047
01048 if (x && !(block[-1].ref & 3)) {
01049 for (i = 0; i < 3; i++)
01050 block->u.dc[i] += block[-1].u.dc[i];
01051 n++;
01052 }
01053
01054 if (y && !(block[-stride].ref & 3)) {
01055 for (i = 0; i < 3; i++)
01056 block->u.dc[i] += block[-stride].u.dc[i];
01057 n++;
01058 }
01059
01060 if (x && y && !(block[-1-stride].ref & 3)) {
01061 for (i = 0; i < 3; i++)
01062 block->u.dc[i] += block[-1-stride].u.dc[i];
01063 n++;
01064 }
01065
01066 if (n == 2) {
01067 for (i = 0; i < 3; i++)
01068 block->u.dc[i] = (block->u.dc[i]+1)>>1;
01069 } else if (n == 3) {
01070 for (i = 0; i < 3; i++)
01071 block->u.dc[i] = divide3(block->u.dc[i]);
01072 }
01073 }
01074
01075 static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
01076 {
01077 int16_t *pred[3];
01078 int refmask = ref+1;
01079 int mask = refmask | DIRAC_REF_MASK_GLOBAL;
01080 int n = 0;
01081
01082 if (x && (block[-1].ref & mask) == refmask)
01083 pred[n++] = block[-1].u.mv[ref];
01084
01085 if (y && (block[-stride].ref & mask) == refmask)
01086 pred[n++] = block[-stride].u.mv[ref];
01087
01088 if (x && y && (block[-stride-1].ref & mask) == refmask)
01089 pred[n++] = block[-stride-1].u.mv[ref];
01090
01091 switch (n) {
01092 case 0:
01093 block->u.mv[ref][0] = 0;
01094 block->u.mv[ref][1] = 0;
01095 break;
01096 case 1:
01097 block->u.mv[ref][0] = pred[0][0];
01098 block->u.mv[ref][1] = pred[0][1];
01099 break;
01100 case 2:
01101 block->u.mv[ref][0] = (pred[0][0] + pred[1][0] + 1) >> 1;
01102 block->u.mv[ref][1] = (pred[0][1] + pred[1][1] + 1) >> 1;
01103 break;
01104 case 3:
01105 block->u.mv[ref][0] = mid_pred(pred[0][0], pred[1][0], pred[2][0]);
01106 block->u.mv[ref][1] = mid_pred(pred[0][1], pred[1][1], pred[2][1]);
01107 break;
01108 }
01109 }
01110
01111 static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
01112 {
01113 int ez = s->globalmc[ref].zrs_exp;
01114 int ep = s->globalmc[ref].perspective_exp;
01115 int (*A)[2] = s->globalmc[ref].zrs;
01116 int *b = s->globalmc[ref].pan_tilt;
01117 int *c = s->globalmc[ref].perspective;
01118
01119 int m = (1<<ep) - (c[0]*x + c[1]*y);
01120 int mx = m * ((A[0][0] * x + A[0][1]*y) + (1<<ez) * b[0]);
01121 int my = m * ((A[1][0] * x + A[1][1]*y) + (1<<ez) * b[1]);
01122
01123 block->u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep);
01124 block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep);
01125 }
01126
01127 static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block,
01128 int stride, int x, int y)
01129 {
01130 int i;
01131
01132 block->ref = pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF1);
01133 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF1);
01134
01135 if (s->num_refs == 2) {
01136 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF2);
01137 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF2) << 1;
01138 }
01139
01140 if (!block->ref) {
01141 pred_block_dc(block, stride, x, y);
01142 for (i = 0; i < 3; i++)
01143 block->u.dc[i] += dirac_get_arith_int(arith+1+i, CTX_DC_F1, CTX_DC_DATA);
01144 return;
01145 }
01146
01147 if (s->globalmc_flag) {
01148 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_GLOBAL);
01149 block->ref ^= dirac_get_arith_bit(arith, CTX_GLOBAL_BLOCK) << 2;
01150 }
01151
01152 for (i = 0; i < s->num_refs; i++)
01153 if (block->ref & (i+1)) {
01154 if (block->ref & DIRAC_REF_MASK_GLOBAL) {
01155 global_mv(s, block, x, y, i);
01156 } else {
01157 pred_mv(block, stride, x, y, i);
01158 block->u.mv[i][0] += dirac_get_arith_int(arith + 4 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
01159 block->u.mv[i][1] += dirac_get_arith_int(arith + 5 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
01160 }
01161 }
01162 }
01163
01167 static void propagate_block_data(DiracBlock *block, int stride, int size)
01168 {
01169 int x, y;
01170 DiracBlock *dst = block;
01171
01172 for (x = 1; x < size; x++)
01173 dst[x] = *block;
01174
01175 for (y = 1; y < size; y++) {
01176 dst += stride;
01177 for (x = 0; x < size; x++)
01178 dst[x] = *block;
01179 }
01180 }
01181
01186 static int dirac_unpack_block_motion_data(DiracContext *s)
01187 {
01188 GetBitContext *gb = &s->gb;
01189 uint8_t *sbsplit = s->sbsplit;
01190 int i, x, y, q, p;
01191 DiracArith arith[8];
01192
01193 align_get_bits(gb);
01194
01195
01196 s->sbwidth = DIVRNDUP(s->source.width, 4*s->plane[0].xbsep);
01197 s->sbheight = DIVRNDUP(s->source.height, 4*s->plane[0].ybsep);
01198 s->blwidth = 4 * s->sbwidth;
01199 s->blheight = 4 * s->sbheight;
01200
01201
01202
01203 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
01204 for (y = 0; y < s->sbheight; y++) {
01205 for (x = 0; x < s->sbwidth; x++) {
01206 unsigned int split = dirac_get_arith_uint(arith, CTX_SB_F1, CTX_SB_DATA);
01207 if (split > 2)
01208 return -1;
01209 sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3;
01210 }
01211 sbsplit += s->sbwidth;
01212 }
01213
01214
01215 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
01216 for (i = 0; i < s->num_refs; i++) {
01217 ff_dirac_init_arith_decoder(arith + 4 + 2 * i, gb, svq3_get_ue_golomb(gb));
01218 ff_dirac_init_arith_decoder(arith + 5 + 2 * i, gb, svq3_get_ue_golomb(gb));
01219 }
01220 for (i = 0; i < 3; i++)
01221 ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb));
01222
01223 for (y = 0; y < s->sbheight; y++)
01224 for (x = 0; x < s->sbwidth; x++) {
01225 int blkcnt = 1 << s->sbsplit[y * s->sbwidth + x];
01226 int step = 4 >> s->sbsplit[y * s->sbwidth + x];
01227
01228 for (q = 0; q < blkcnt; q++)
01229 for (p = 0; p < blkcnt; p++) {
01230 int bx = 4 * x + p*step;
01231 int by = 4 * y + q*step;
01232 DiracBlock *block = &s->blmotion[by*s->blwidth + bx];
01233 decode_block_params(s, arith, block, s->blwidth, bx, by);
01234 propagate_block_data(block, s->blwidth, step);
01235 }
01236 }
01237
01238 return 0;
01239 }
01240
01241 static int weight(int i, int blen, int offset)
01242 {
01243 #define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) : \
01244 (1 + (6*(i) + offset - 1) / (2*offset - 1))
01245
01246 if (i < 2*offset)
01247 return ROLLOFF(i);
01248 else if (i > blen-1 - 2*offset)
01249 return ROLLOFF(blen-1 - i);
01250 return 8;
01251 }
01252
01253 static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride,
01254 int left, int right, int wy)
01255 {
01256 int x;
01257 for (x = 0; left && x < p->xblen >> 1; x++)
01258 obmc_weight[x] = wy*8;
01259 for (; x < p->xblen >> right; x++)
01260 obmc_weight[x] = wy*weight(x, p->xblen, p->xoffset);
01261 for (; x < p->xblen; x++)
01262 obmc_weight[x] = wy*8;
01263 for (; x < stride; x++)
01264 obmc_weight[x] = 0;
01265 }
01266
01267 static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride,
01268 int left, int right, int top, int bottom)
01269 {
01270 int y;
01271 for (y = 0; top && y < p->yblen >> 1; y++) {
01272 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
01273 obmc_weight += stride;
01274 }
01275 for (; y < p->yblen >> bottom; y++) {
01276 int wy = weight(y, p->yblen, p->yoffset);
01277 init_obmc_weight_row(p, obmc_weight, stride, left, right, wy);
01278 obmc_weight += stride;
01279 }
01280 for (; y < p->yblen; y++) {
01281 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
01282 obmc_weight += stride;
01283 }
01284 }
01285
01286 static void init_obmc_weights(DiracContext *s, Plane *p, int by)
01287 {
01288 int top = !by;
01289 int bottom = by == s->blheight-1;
01290
01291
01292 if (top || bottom || by == 1) {
01293 init_obmc_weight(p, s->obmc_weight[0], MAX_BLOCKSIZE, 1, 0, top, bottom);
01294 init_obmc_weight(p, s->obmc_weight[1], MAX_BLOCKSIZE, 0, 0, top, bottom);
01295 init_obmc_weight(p, s->obmc_weight[2], MAX_BLOCKSIZE, 0, 1, top, bottom);
01296 }
01297 }
01298
01299 static const uint8_t epel_weights[4][4][4] = {
01300 {{ 16, 0, 0, 0 },
01301 { 12, 4, 0, 0 },
01302 { 8, 8, 0, 0 },
01303 { 4, 12, 0, 0 }},
01304 {{ 12, 0, 4, 0 },
01305 { 9, 3, 3, 1 },
01306 { 6, 6, 2, 2 },
01307 { 3, 9, 1, 3 }},
01308 {{ 8, 0, 8, 0 },
01309 { 6, 2, 6, 2 },
01310 { 4, 4, 4, 4 },
01311 { 2, 6, 2, 6 }},
01312 {{ 4, 0, 12, 0 },
01313 { 3, 1, 9, 3 },
01314 { 2, 2, 6, 6 },
01315 { 1, 3, 3, 9 }}
01316 };
01317
01326 static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5],
01327 int x, int y, int ref, int plane)
01328 {
01329 Plane *p = &s->plane[plane];
01330 uint8_t **ref_hpel = s->ref_pics[ref]->hpel[plane];
01331 int motion_x = block->u.mv[ref][0];
01332 int motion_y = block->u.mv[ref][1];
01333 int mx, my, i, epel, nplanes = 0;
01334
01335 if (plane) {
01336 motion_x >>= s->chroma_x_shift;
01337 motion_y >>= s->chroma_y_shift;
01338 }
01339
01340 mx = motion_x & ~(-1 << s->mv_precision);
01341 my = motion_y & ~(-1 << s->mv_precision);
01342 motion_x >>= s->mv_precision;
01343 motion_y >>= s->mv_precision;
01344
01345
01346 mx <<= 3 - s->mv_precision;
01347 my <<= 3 - s->mv_precision;
01348
01349 x += motion_x;
01350 y += motion_y;
01351 epel = (mx|my)&1;
01352
01353
01354 if (!((mx|my)&3)) {
01355 nplanes = 1;
01356 src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x;
01357 } else {
01358
01359 nplanes = 4;
01360 for (i = 0; i < 4; i++)
01361 src[i] = ref_hpel[i] + y*p->stride + x;
01362
01363
01364
01365 if (mx > 4) {
01366 src[0] += 1;
01367 src[2] += 1;
01368 x++;
01369 }
01370 if (my > 4) {
01371 src[0] += p->stride;
01372 src[1] += p->stride;
01373 y++;
01374 }
01375
01376
01377
01378
01379 if (!epel) {
01380
01381
01382 if (!(mx&3)) {
01383
01384
01385 src[!mx] = src[2 + !!mx];
01386 nplanes = 2;
01387 } else if (!(my&3)) {
01388 src[0] = src[(my>>1) ];
01389 src[1] = src[(my>>1)+1];
01390 nplanes = 2;
01391 }
01392 } else {
01393
01394 if (mx > 4) {
01395 FFSWAP(const uint8_t *, src[0], src[1]);
01396 FFSWAP(const uint8_t *, src[2], src[3]);
01397 }
01398 if (my > 4) {
01399 FFSWAP(const uint8_t *, src[0], src[2]);
01400 FFSWAP(const uint8_t *, src[1], src[3]);
01401 }
01402 src[4] = epel_weights[my&3][mx&3];
01403 }
01404 }
01405
01406
01407 if ((unsigned)x > p->width +EDGE_WIDTH/2 - p->xblen ||
01408 (unsigned)y > p->height+EDGE_WIDTH/2 - p->yblen) {
01409 for (i = 0; i < nplanes; i++) {
01410 ff_emulated_edge_mc(s->edge_emu_buffer[i], src[i], p->stride,
01411 p->xblen, p->yblen, x, y,
01412 p->width+EDGE_WIDTH/2, p->height+EDGE_WIDTH/2);
01413 src[i] = s->edge_emu_buffer[i];
01414 }
01415 }
01416 return (nplanes>>1) + epel;
01417 }
01418
01419 static void add_dc(uint16_t *dst, int dc, int stride,
01420 uint8_t *obmc_weight, int xblen, int yblen)
01421 {
01422 int x, y;
01423 dc += 128;
01424
01425 for (y = 0; y < yblen; y++) {
01426 for (x = 0; x < xblen; x += 2) {
01427 dst[x ] += dc * obmc_weight[x ];
01428 dst[x+1] += dc * obmc_weight[x+1];
01429 }
01430 dst += stride;
01431 obmc_weight += MAX_BLOCKSIZE;
01432 }
01433 }
01434
01435 static void block_mc(DiracContext *s, DiracBlock *block,
01436 uint16_t *mctmp, uint8_t *obmc_weight,
01437 int plane, int dstx, int dsty)
01438 {
01439 Plane *p = &s->plane[plane];
01440 const uint8_t *src[5];
01441 int idx;
01442
01443 switch (block->ref&3) {
01444 case 0:
01445 add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen);
01446 return;
01447 case 1:
01448 case 2:
01449 idx = mc_subpel(s, block, src, dstx, dsty, (block->ref&3)-1, plane);
01450 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01451 if (s->weight_func)
01452 s->weight_func(s->mcscratch, p->stride, s->weight_log2denom,
01453 s->weight[0] + s->weight[1], p->yblen);
01454 break;
01455 case 3:
01456 idx = mc_subpel(s, block, src, dstx, dsty, 0, plane);
01457 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01458 idx = mc_subpel(s, block, src, dstx, dsty, 1, plane);
01459 if (s->biweight_func) {
01460
01461 s->put_pixels_tab[idx](s->mcscratch + 32, src, p->stride, p->yblen);
01462 s->biweight_func(s->mcscratch, s->mcscratch+32, p->stride, s->weight_log2denom,
01463 s->weight[0], s->weight[1], p->yblen);
01464 } else
01465 s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01466 break;
01467 }
01468 s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen);
01469 }
01470
01471 static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
01472 {
01473 Plane *p = &s->plane[plane];
01474 int x, dstx = p->xbsep - p->xoffset;
01475
01476 block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty);
01477 mctmp += p->xbsep;
01478
01479 for (x = 1; x < s->blwidth-1; x++) {
01480 block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty);
01481 dstx += p->xbsep;
01482 mctmp += p->xbsep;
01483 }
01484 block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty);
01485 }
01486
01487 static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
01488 {
01489 int idx = 0;
01490 if (xblen > 8)
01491 idx = 1;
01492 if (xblen > 16)
01493 idx = 2;
01494
01495 memcpy(s->put_pixels_tab, s->diracdsp.put_dirac_pixels_tab[idx], sizeof(s->put_pixels_tab));
01496 memcpy(s->avg_pixels_tab, s->diracdsp.avg_dirac_pixels_tab[idx], sizeof(s->avg_pixels_tab));
01497 s->add_obmc = s->diracdsp.add_dirac_obmc[idx];
01498 if (s->weight_log2denom > 1 || s->weight[0] != 1 || s->weight[1] != 1) {
01499 s->weight_func = s->diracdsp.weight_dirac_pixels_tab[idx];
01500 s->biweight_func = s->diracdsp.biweight_dirac_pixels_tab[idx];
01501 } else {
01502 s->weight_func = NULL;
01503 s->biweight_func = NULL;
01504 }
01505 }
01506
01507 static void interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height)
01508 {
01509
01510
01511
01512 int i, edge = EDGE_WIDTH/2;
01513
01514 ref->hpel[plane][0] = ref->avframe.data[plane];
01515 s->dsp.draw_edges(ref->hpel[plane][0], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01516
01517
01518 if (!s->mv_precision)
01519 return;
01520
01521 for (i = 1; i < 4; i++) {
01522 if (!ref->hpel_base[plane][i])
01523 ref->hpel_base[plane][i] = av_malloc((height+2*edge) * ref->avframe.linesize[plane] + 32);
01524
01525 ref->hpel[plane][i] = ref->hpel_base[plane][i] + edge*ref->avframe.linesize[plane] + 16;
01526 }
01527
01528 if (!ref->interpolated[plane]) {
01529 s->diracdsp.dirac_hpel_filter(ref->hpel[plane][1], ref->hpel[plane][2],
01530 ref->hpel[plane][3], ref->hpel[plane][0],
01531 ref->avframe.linesize[plane], width, height);
01532 s->dsp.draw_edges(ref->hpel[plane][1], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01533 s->dsp.draw_edges(ref->hpel[plane][2], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01534 s->dsp.draw_edges(ref->hpel[plane][3], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01535 }
01536 ref->interpolated[plane] = 1;
01537 }
01538
01543 static int dirac_decode_frame_internal(DiracContext *s)
01544 {
01545 DWTContext d;
01546 int y, i, comp, dsty;
01547
01548 if (s->low_delay) {
01549
01550 for (comp = 0; comp < 3; comp++) {
01551 Plane *p = &s->plane[comp];
01552 memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM));
01553 }
01554 if (!s->zero_res)
01555 decode_lowdelay(s);
01556 }
01557
01558 for (comp = 0; comp < 3; comp++) {
01559 Plane *p = &s->plane[comp];
01560 uint8_t *frame = s->current_picture->avframe.data[comp];
01561
01562
01563 for (i = 0; i < 4; i++)
01564 s->edge_emu_buffer[i] = s->edge_emu_buffer_base + i*FFALIGN(p->width, 16);
01565
01566 if (!s->zero_res && !s->low_delay)
01567 {
01568 memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM));
01569 decode_component(s, comp);
01570 }
01571 if (ff_spatial_idwt_init2(&d, p->idwt_buf, p->idwt_width, p->idwt_height, p->idwt_stride,
01572 s->wavelet_idx+2, s->wavelet_depth, p->idwt_tmp))
01573 return -1;
01574
01575 if (!s->num_refs) {
01576 for (y = 0; y < p->height; y += 16) {
01577 ff_spatial_idwt_slice2(&d, y+16);
01578 s->diracdsp.put_signed_rect_clamped(frame + y*p->stride, p->stride,
01579 p->idwt_buf + y*p->idwt_stride, p->idwt_stride, p->width, 16);
01580 }
01581 } else {
01582 int rowheight = p->ybsep*p->stride;
01583
01584 select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen);
01585
01586 for (i = 0; i < s->num_refs; i++)
01587 interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height);
01588
01589 memset(s->mctmp, 0, 4*p->yoffset*p->stride);
01590
01591 dsty = -p->yoffset;
01592 for (y = 0; y < s->blheight; y++) {
01593 int h = 0,
01594 start = FFMAX(dsty, 0);
01595 uint16_t *mctmp = s->mctmp + y*rowheight;
01596 DiracBlock *blocks = s->blmotion + y*s->blwidth;
01597
01598 init_obmc_weights(s, p, y);
01599
01600 if (y == s->blheight-1 || start+p->ybsep > p->height)
01601 h = p->height - start;
01602 else
01603 h = p->ybsep - (start - dsty);
01604 if (h < 0)
01605 break;
01606
01607 memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight);
01608 mc_row(s, blocks, mctmp, comp, dsty);
01609
01610 mctmp += (start - dsty)*p->stride + p->xoffset;
01611 ff_spatial_idwt_slice2(&d, start + h);
01612 s->diracdsp.add_rect_clamped(frame + start*p->stride, mctmp, p->stride,
01613 p->idwt_buf + start*p->idwt_stride, p->idwt_stride, p->width, h);
01614
01615 dsty += p->ybsep;
01616 }
01617 }
01618 }
01619
01620
01621 return 0;
01622 }
01623
01628 static int dirac_decode_picture_header(DiracContext *s)
01629 {
01630 int retire, picnum;
01631 int i, j, refnum, refdist;
01632 GetBitContext *gb = &s->gb;
01633
01634
01635 picnum = s->current_picture->avframe.display_picture_number = get_bits_long(gb, 32);
01636
01637
01638 av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);
01639
01640
01641
01642 if (s->frame_number < 0)
01643 s->frame_number = picnum;
01644
01645 s->ref_pics[0] = s->ref_pics[1] = NULL;
01646 for (i = 0; i < s->num_refs; i++) {
01647 refnum = picnum + dirac_get_se_golomb(gb);
01648 refdist = INT_MAX;
01649
01650
01651
01652 for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)
01653 if (s->ref_frames[j]
01654 && FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum) < refdist) {
01655 s->ref_pics[i] = s->ref_frames[j];
01656 refdist = FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum);
01657 }
01658
01659 if (!s->ref_pics[i] || refdist)
01660 av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");
01661
01662
01663 if (!s->ref_pics[i])
01664 for (j = 0; j < MAX_FRAMES; j++)
01665 if (!s->all_frames[j].avframe.data[0]) {
01666 s->ref_pics[i] = &s->all_frames[j];
01667 s->avctx->get_buffer(s->avctx, &s->ref_pics[i]->avframe);
01668 }
01669 }
01670
01671
01672 if (s->current_picture->avframe.reference) {
01673 retire = picnum + dirac_get_se_golomb(gb);
01674 if (retire != picnum) {
01675 DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);
01676
01677 if (retire_pic)
01678 retire_pic->avframe.reference &= DELAYED_PIC_REF;
01679 else
01680 av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");
01681 }
01682
01683
01684 while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {
01685 av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");
01686 remove_frame(s->ref_frames, s->ref_frames[0]->avframe.display_picture_number)->avframe.reference &= DELAYED_PIC_REF;
01687 }
01688 }
01689
01690 if (s->num_refs) {
01691 if (dirac_unpack_prediction_parameters(s))
01692 return -1;
01693 if (dirac_unpack_block_motion_data(s))
01694 return -1;
01695 }
01696 if (dirac_unpack_idwt_params(s))
01697 return -1;
01698
01699 init_planes(s);
01700 return 0;
01701 }
01702
01703 static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *data_size)
01704 {
01705 DiracFrame *out = s->delay_frames[0];
01706 int i, out_idx = 0;
01707
01708
01709 for (i = 1; s->delay_frames[i]; i++)
01710 if (s->delay_frames[i]->avframe.display_picture_number < out->avframe.display_picture_number) {
01711 out = s->delay_frames[i];
01712 out_idx = i;
01713 }
01714
01715 for (i = out_idx; s->delay_frames[i]; i++)
01716 s->delay_frames[i] = s->delay_frames[i+1];
01717
01718 if (out) {
01719 out->avframe.reference ^= DELAYED_PIC_REF;
01720 *data_size = sizeof(AVFrame);
01721 *(AVFrame *)picture = out->avframe;
01722 }
01723
01724 return 0;
01725 }
01726
01732 #define DATA_UNIT_HEADER_SIZE 13
01733
01734
01735
01736 static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size)
01737 {
01738 DiracContext *s = avctx->priv_data;
01739 DiracFrame *pic = NULL;
01740 int i, parse_code = buf[4];
01741 unsigned tmp;
01742
01743 if (size < DATA_UNIT_HEADER_SIZE)
01744 return -1;
01745
01746 init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE));
01747
01748 if (parse_code == pc_seq_header) {
01749 if (s->seen_sequence_header)
01750 return 0;
01751
01752
01753 if (avpriv_dirac_parse_sequence_header(avctx, &s->gb, &s->source))
01754 return -1;
01755
01756 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
01757
01758 if (alloc_sequence_buffers(s))
01759 return -1;
01760
01761 s->seen_sequence_header = 1;
01762 } else if (parse_code == pc_eos) {
01763 free_sequence_buffers(s);
01764 s->seen_sequence_header = 0;
01765 } else if (parse_code == pc_aux_data) {
01766 if (buf[13] == 1) {
01767 int ver[3];
01768
01769
01770 if (sscanf(buf+14, "Schroedinger %d.%d.%d", ver, ver+1, ver+2) == 3)
01771 if (ver[0] == 1 && ver[1] == 0 && ver[2] <= 7)
01772 s->old_delta_quant = 1;
01773 }
01774 } else if (parse_code & 0x8) {
01775 if (!s->seen_sequence_header) {
01776 av_log(avctx, AV_LOG_DEBUG, "Dropping frame without sequence header\n");
01777 return -1;
01778 }
01779
01780
01781 for (i = 0; i < MAX_FRAMES; i++)
01782 if (s->all_frames[i].avframe.data[0] == NULL)
01783 pic = &s->all_frames[i];
01784 if (!pic) {
01785 av_log(avctx, AV_LOG_ERROR, "framelist full\n");
01786 return -1;
01787 }
01788
01789 avcodec_get_frame_defaults(&pic->avframe);
01790
01791
01792 tmp = parse_code & 0x03;
01793 if (tmp > 2) {
01794 av_log(avctx, AV_LOG_ERROR, "num_refs of 3\n");
01795 return -1;
01796 }
01797 s->num_refs = tmp;
01798 s->is_arith = (parse_code & 0x48) == 0x08;
01799 s->low_delay = (parse_code & 0x88) == 0x88;
01800 pic->avframe.reference = (parse_code & 0x0C) == 0x0C;
01801 pic->avframe.key_frame = s->num_refs == 0;
01802 pic->avframe.pict_type = s->num_refs + 1;
01803
01804 if (avctx->get_buffer(avctx, &pic->avframe) < 0) {
01805 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01806 return -1;
01807 }
01808 s->current_picture = pic;
01809 s->plane[0].stride = pic->avframe.linesize[0];
01810 s->plane[1].stride = pic->avframe.linesize[1];
01811 s->plane[2].stride = pic->avframe.linesize[2];
01812
01813
01814 if (dirac_decode_picture_header(s))
01815 return -1;
01816
01817
01818 if (dirac_decode_frame_internal(s))
01819 return -1;
01820 }
01821 return 0;
01822 }
01823
01824 static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
01825 {
01826 DiracContext *s = avctx->priv_data;
01827 DiracFrame *picture = data;
01828 uint8_t *buf = pkt->data;
01829 int buf_size = pkt->size;
01830 int i, data_unit_size, buf_idx = 0;
01831
01832
01833 for (i = 0; i < MAX_FRAMES; i++)
01834 if (s->all_frames[i].avframe.data[0] && !s->all_frames[i].avframe.reference) {
01835 avctx->release_buffer(avctx, &s->all_frames[i].avframe);
01836 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
01837 }
01838
01839 s->current_picture = NULL;
01840 *data_size = 0;
01841
01842
01843 if (buf_size == 0)
01844 return get_delayed_pic(s, (AVFrame *)data, data_size);
01845
01846 for (;;) {
01847
01848
01849
01850 for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {
01851 if (buf[buf_idx ] == 'B' && buf[buf_idx+1] == 'B' &&
01852 buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')
01853 break;
01854 }
01855
01856 if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)
01857 break;
01858
01859 data_unit_size = AV_RB32(buf+buf_idx+5);
01860 if (buf_idx + data_unit_size > buf_size || !data_unit_size) {
01861 if(buf_idx + data_unit_size > buf_size)
01862 av_log(s->avctx, AV_LOG_ERROR,
01863 "Data unit with size %d is larger than input buffer, discarding\n",
01864 data_unit_size);
01865 buf_idx += 4;
01866 continue;
01867 }
01868
01869 if (dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size))
01870 {
01871 av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");
01872 return -1;
01873 }
01874 buf_idx += data_unit_size;
01875 }
01876
01877 if (!s->current_picture)
01878 return buf_size;
01879
01880 if (s->current_picture->avframe.display_picture_number > s->frame_number) {
01881 DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);
01882
01883 s->current_picture->avframe.reference |= DELAYED_PIC_REF;
01884
01885 if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {
01886 int min_num = s->delay_frames[0]->avframe.display_picture_number;
01887
01888 av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");
01889 delayed_frame = s->delay_frames[0];
01890
01891 for (i = 1; s->delay_frames[i]; i++)
01892 if (s->delay_frames[i]->avframe.display_picture_number < min_num)
01893 min_num = s->delay_frames[i]->avframe.display_picture_number;
01894
01895 delayed_frame = remove_frame(s->delay_frames, min_num);
01896 add_frame(s->delay_frames, MAX_DELAY, s->current_picture);
01897 }
01898
01899 if (delayed_frame) {
01900 delayed_frame->avframe.reference ^= DELAYED_PIC_REF;
01901 *(AVFrame*)data = delayed_frame->avframe;
01902 *data_size = sizeof(AVFrame);
01903 }
01904 } else if (s->current_picture->avframe.display_picture_number == s->frame_number) {
01905
01906 *(AVFrame*)data = s->current_picture->avframe;
01907 *data_size = sizeof(AVFrame);
01908 }
01909
01910 if (*data_size)
01911 s->frame_number = picture->avframe.display_picture_number + 1;
01912
01913 return buf_idx;
01914 }
01915
01916 AVCodec ff_dirac_decoder = {
01917 .name = "dirac",
01918 .type = AVMEDIA_TYPE_VIDEO,
01919 .id = AV_CODEC_ID_DIRAC,
01920 .priv_data_size = sizeof(DiracContext),
01921 .init = dirac_decode_init,
01922 .close = dirac_decode_end,
01923 .decode = dirac_decode_frame,
01924 .capabilities = CODEC_CAP_DELAY,
01925 .flush = dirac_decode_flush,
01926 .long_name = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"),
01927 };