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