00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "get_bits.h"
00032 #include "bytestream.h"
00033 #include "golomb.h"
00034 #include "dirac_arith.h"
00035 #include "mpeg12data.h"
00036 #include "dwt.h"
00037 #include "dirac.h"
00038 #include "diracdsp.h"
00039
00049 #define MAX_DWT_LEVELS 5
00050
00054 #define MAX_REFERENCE_FRAMES 8
00055 #define MAX_DELAY 5
00056 #define MAX_FRAMES (MAX_REFERENCE_FRAMES + MAX_DELAY + 1)
00057 #define MAX_QUANT 68
00058 #define MAX_BLOCKSIZE 32
00059
00063 #define DIRAC_REF_MASK_REF1 1
00064 #define DIRAC_REF_MASK_REF2 2
00065 #define DIRAC_REF_MASK_GLOBAL 4
00066
00071 #define DELAYED_PIC_REF 4
00072
00073 #define ff_emulated_edge_mc ff_emulated_edge_mc_8
00074
00075 #define CALC_PADDING(size, depth) \
00076 (((size + (1 << depth) - 1) >> depth) << depth)
00077
00078 #define DIVRNDUP(a, b) (((a) + (b) - 1) / (b))
00079
00080 typedef struct {
00081 AVFrame avframe;
00082 int interpolated[3];
00083 uint8_t *hpel[3][4];
00084 uint8_t *hpel_base[3][4];
00085 } DiracFrame;
00086
00087 typedef struct {
00088 union {
00089 int16_t mv[2][2];
00090 int16_t dc[3];
00091 } u;
00092 uint8_t ref;
00093 } DiracBlock;
00094
00095 typedef struct SubBand {
00096 int level;
00097 int orientation;
00098 int stride;
00099 int width;
00100 int height;
00101 int quant;
00102 IDWTELEM *ibuf;
00103 struct SubBand *parent;
00104
00105
00106 unsigned length;
00107 const uint8_t *coeff_data;
00108 } SubBand;
00109
00110 typedef struct Plane {
00111 int width;
00112 int height;
00113 int stride;
00114
00115 int idwt_width;
00116 int idwt_height;
00117 int idwt_stride;
00118 IDWTELEM *idwt_buf;
00119 IDWTELEM *idwt_buf_base;
00120 IDWTELEM *idwt_tmp;
00121
00122
00123 uint8_t xblen;
00124 uint8_t yblen;
00125
00126 uint8_t xbsep;
00127 uint8_t ybsep;
00128
00129 uint8_t xoffset;
00130 uint8_t yoffset;
00131
00132 SubBand band[MAX_DWT_LEVELS][4];
00133 } Plane;
00134
00135 typedef struct DiracContext {
00136 AVCodecContext *avctx;
00137 DSPContext dsp;
00138 DiracDSPContext diracdsp;
00139 GetBitContext gb;
00140 dirac_source_params source;
00141 int seen_sequence_header;
00142 int frame_number;
00143 Plane plane[3];
00144 int chroma_x_shift;
00145 int chroma_y_shift;
00146
00147 int zero_res;
00148 int is_arith;
00149 int low_delay;
00150 int globalmc_flag;
00151 int num_refs;
00152
00153
00154 unsigned wavelet_depth;
00155 unsigned wavelet_idx;
00156
00161 unsigned old_delta_quant;
00162 unsigned codeblock_mode;
00163
00164 struct {
00165 unsigned width;
00166 unsigned height;
00167 } codeblock[MAX_DWT_LEVELS+1];
00168
00169 struct {
00170 unsigned num_x;
00171 unsigned num_y;
00172 AVRational bytes;
00173 uint8_t quant[MAX_DWT_LEVELS][4];
00174 } lowdelay;
00175
00176 struct {
00177 int pan_tilt[2];
00178 int zrs[2][2];
00179 int perspective[2];
00180 unsigned zrs_exp;
00181 unsigned perspective_exp;
00182 } globalmc[2];
00183
00184
00185 uint8_t mv_precision;
00186 int16_t weight[2];
00187 unsigned weight_log2denom;
00188
00189 int blwidth;
00190 int blheight;
00191 int sbwidth;
00192 int sbheight;
00193
00194 uint8_t *sbsplit;
00195 DiracBlock *blmotion;
00196
00197 uint8_t *edge_emu_buffer[4];
00198 uint8_t *edge_emu_buffer_base;
00199
00200 uint16_t *mctmp;
00201 uint8_t *mcscratch;
00202
00203 DECLARE_ALIGNED(16, uint8_t, obmc_weight)[3][MAX_BLOCKSIZE*MAX_BLOCKSIZE];
00204
00205 void (*put_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
00206 void (*avg_pixels_tab[4])(uint8_t *dst, const uint8_t *src[5], int stride, int h);
00207 void (*add_obmc)(uint16_t *dst, const uint8_t *src, int stride, const uint8_t *obmc_weight, int yblen);
00208 dirac_weight_func weight_func;
00209 dirac_biweight_func biweight_func;
00210
00211 DiracFrame *current_picture;
00212 DiracFrame *ref_pics[2];
00213
00214 DiracFrame *ref_frames[MAX_REFERENCE_FRAMES+1];
00215 DiracFrame *delay_frames[MAX_DELAY+1];
00216 DiracFrame all_frames[MAX_FRAMES];
00217 } DiracContext;
00218
00223 enum dirac_parse_code {
00224 pc_seq_header = 0x00,
00225 pc_eos = 0x10,
00226 pc_aux_data = 0x20,
00227 pc_padding = 0x30,
00228 };
00229
00230 enum dirac_subband {
00231 subband_ll = 0,
00232 subband_hl = 1,
00233 subband_lh = 2,
00234 subband_hh = 3
00235 };
00236
00237 static const uint8_t default_qmat[][4][4] = {
00238 { { 5, 3, 3, 0}, { 0, 4, 4, 1}, { 0, 5, 5, 2}, { 0, 6, 6, 3} },
00239 { { 4, 2, 2, 0}, { 0, 4, 4, 2}, { 0, 5, 5, 3}, { 0, 7, 7, 5} },
00240 { { 5, 3, 3, 0}, { 0, 4, 4, 1}, { 0, 5, 5, 2}, { 0, 6, 6, 3} },
00241 { { 8, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0} },
00242 { { 8, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0}, { 0, 4, 4, 0} },
00243 { { 0, 4, 4, 8}, { 0, 8, 8, 12}, { 0, 13, 13, 17}, { 0, 17, 17, 21} },
00244 { { 3, 1, 1, 0}, { 0, 4, 4, 2}, { 0, 6, 6, 5}, { 0, 9, 9, 7} },
00245 };
00246
00247 static const int qscale_tab[MAX_QUANT+1] = {
00248 4, 5, 6, 7, 8, 10, 11, 13,
00249 16, 19, 23, 27, 32, 38, 45, 54,
00250 64, 76, 91, 108, 128, 152, 181, 215,
00251 256, 304, 362, 431, 512, 609, 724, 861,
00252 1024, 1218, 1448, 1722, 2048, 2435, 2896, 3444,
00253 4096, 4871, 5793, 6889, 8192, 9742, 11585, 13777,
00254 16384, 19484, 23170, 27554, 32768, 38968, 46341, 55109,
00255 65536, 77936
00256 };
00257
00258 static const int qoffset_intra_tab[MAX_QUANT+1] = {
00259 1, 2, 3, 4, 4, 5, 6, 7,
00260 8, 10, 12, 14, 16, 19, 23, 27,
00261 32, 38, 46, 54, 64, 76, 91, 108,
00262 128, 152, 181, 216, 256, 305, 362, 431,
00263 512, 609, 724, 861, 1024, 1218, 1448, 1722,
00264 2048, 2436, 2897, 3445, 4096, 4871, 5793, 6889,
00265 8192, 9742, 11585, 13777, 16384, 19484, 23171, 27555,
00266 32768, 38968
00267 };
00268
00269 static const int qoffset_inter_tab[MAX_QUANT+1] = {
00270 1, 2, 2, 3, 3, 4, 4, 5,
00271 6, 7, 9, 10, 12, 14, 17, 20,
00272 24, 29, 34, 41, 48, 57, 68, 81,
00273 96, 114, 136, 162, 192, 228, 272, 323,
00274 384, 457, 543, 646, 768, 913, 1086, 1292,
00275 1536, 1827, 2172, 2583, 3072, 3653, 4344, 5166,
00276 6144, 7307, 8689, 10333, 12288, 14613, 17378, 20666,
00277 24576, 29226
00278 };
00279
00280
00281 static inline int divide3(int x)
00282 {
00283 return ((x+1)*21845 + 10922) >> 16;
00284 }
00285
00286 static DiracFrame *remove_frame(DiracFrame *framelist[], int picnum)
00287 {
00288 DiracFrame *remove_pic = NULL;
00289 int i, remove_idx = -1;
00290
00291 for (i = 0; framelist[i]; i++)
00292 if (framelist[i]->avframe.display_picture_number == picnum) {
00293 remove_pic = framelist[i];
00294 remove_idx = i;
00295 }
00296
00297 if (remove_pic)
00298 for (i = remove_idx; framelist[i]; i++)
00299 framelist[i] = framelist[i+1];
00300
00301 return remove_pic;
00302 }
00303
00304 static int add_frame(DiracFrame *framelist[], int maxframes, DiracFrame *frame)
00305 {
00306 int i;
00307 for (i = 0; i < maxframes; i++)
00308 if (!framelist[i]) {
00309 framelist[i] = frame;
00310 return 0;
00311 }
00312 return -1;
00313 }
00314
00315 static int alloc_sequence_buffers(DiracContext *s)
00316 {
00317 int sbwidth = DIVRNDUP(s->source.width, 4);
00318 int sbheight = DIVRNDUP(s->source.height, 4);
00319 int i, w, h, top_padding;
00320
00321
00322 for (i = 0; i < 3; i++) {
00323 int max_xblen = MAX_BLOCKSIZE >> (i ? s->chroma_x_shift : 0);
00324 int max_yblen = MAX_BLOCKSIZE >> (i ? s->chroma_y_shift : 0);
00325 w = s->source.width >> (i ? s->chroma_x_shift : 0);
00326 h = s->source.height >> (i ? s->chroma_y_shift : 0);
00327
00328
00329
00330
00331
00332
00333 top_padding = FFMAX(1<<MAX_DWT_LEVELS, max_yblen/2);
00334 w = FFALIGN(CALC_PADDING(w, MAX_DWT_LEVELS), 8);
00335 h = top_padding + CALC_PADDING(h, MAX_DWT_LEVELS) + max_yblen/2;
00336
00337 s->plane[i].idwt_buf_base = av_mallocz((w+max_xblen)*h * sizeof(IDWTELEM));
00338 s->plane[i].idwt_tmp = av_malloc((w+16) * sizeof(IDWTELEM));
00339 s->plane[i].idwt_buf = s->plane[i].idwt_buf_base + top_padding*w;
00340 if (!s->plane[i].idwt_buf_base || !s->plane[i].idwt_tmp)
00341 return AVERROR(ENOMEM);
00342 }
00343
00344 w = s->source.width;
00345 h = s->source.height;
00346
00347
00348 s->sbsplit = av_malloc(sbwidth * sbheight);
00349 s->blmotion = av_malloc(sbwidth * sbheight * 4 * sizeof(*s->blmotion));
00350 s->edge_emu_buffer_base = av_malloc((w+64)*MAX_BLOCKSIZE);
00351
00352 s->mctmp = av_malloc((w+64+MAX_BLOCKSIZE) * (h*MAX_BLOCKSIZE) * sizeof(*s->mctmp));
00353 s->mcscratch = av_malloc((w+64)*MAX_BLOCKSIZE);
00354
00355 if (!s->sbsplit || !s->blmotion)
00356 return AVERROR(ENOMEM);
00357 return 0;
00358 }
00359
00360 static void free_sequence_buffers(DiracContext *s)
00361 {
00362 int i, j, k;
00363
00364 for (i = 0; i < MAX_FRAMES; i++) {
00365 if (s->all_frames[i].avframe.data[0]) {
00366 s->avctx->release_buffer(s->avctx, &s->all_frames[i].avframe);
00367 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
00368 }
00369
00370 for (j = 0; j < 3; j++)
00371 for (k = 1; k < 4; k++)
00372 av_freep(&s->all_frames[i].hpel_base[j][k]);
00373 }
00374
00375 memset(s->ref_frames, 0, sizeof(s->ref_frames));
00376 memset(s->delay_frames, 0, sizeof(s->delay_frames));
00377
00378 for (i = 0; i < 3; i++) {
00379 av_freep(&s->plane[i].idwt_buf_base);
00380 av_freep(&s->plane[i].idwt_tmp);
00381 }
00382
00383 av_freep(&s->sbsplit);
00384 av_freep(&s->blmotion);
00385 av_freep(&s->edge_emu_buffer_base);
00386
00387 av_freep(&s->mctmp);
00388 av_freep(&s->mcscratch);
00389 }
00390
00391 static av_cold int dirac_decode_init(AVCodecContext *avctx)
00392 {
00393 DiracContext *s = avctx->priv_data;
00394 s->avctx = avctx;
00395 s->frame_number = -1;
00396
00397 if (avctx->flags&CODEC_FLAG_EMU_EDGE) {
00398 av_log(avctx, AV_LOG_ERROR, "Edge emulation not supported!\n");
00399 return AVERROR_PATCHWELCOME;
00400 }
00401
00402 dsputil_init(&s->dsp, avctx);
00403 ff_diracdsp_init(&s->diracdsp);
00404
00405 return 0;
00406 }
00407
00408 static void dirac_decode_flush(AVCodecContext *avctx)
00409 {
00410 DiracContext *s = avctx->priv_data;
00411 free_sequence_buffers(s);
00412 s->seen_sequence_header = 0;
00413 s->frame_number = -1;
00414 }
00415
00416 static av_cold int dirac_decode_end(AVCodecContext *avctx)
00417 {
00418 dirac_decode_flush(avctx);
00419 return 0;
00420 }
00421
00422 #define SIGN_CTX(x) (CTX_SIGN_ZERO + ((x) > 0) - ((x) < 0))
00423
00424 static inline void coeff_unpack_arith(DiracArith *c, int qfactor, int qoffset,
00425 SubBand *b, IDWTELEM *buf, int x, int y)
00426 {
00427 int coeff, sign;
00428 int sign_pred = 0;
00429 int pred_ctx = CTX_ZPZN_F1;
00430
00431
00432 if (b->parent)
00433 pred_ctx += !!b->parent->ibuf[b->parent->stride * (y>>1) + (x>>1)] << 1;
00434
00435 if (b->orientation == subband_hl)
00436 sign_pred = buf[-b->stride];
00437
00438
00439 if (x) {
00440 pred_ctx += !(buf[-1] | buf[-b->stride] | buf[-1-b->stride]);
00441 if (b->orientation == subband_lh)
00442 sign_pred = buf[-1];
00443 } else {
00444 pred_ctx += !buf[-b->stride];
00445 }
00446
00447 coeff = dirac_get_arith_uint(c, pred_ctx, CTX_COEFF_DATA);
00448 if (coeff) {
00449 coeff = (coeff * qfactor + qoffset + 2) >> 2;
00450 sign = dirac_get_arith_bit(c, SIGN_CTX(sign_pred));
00451 coeff = (coeff ^ -sign) + sign;
00452 }
00453 *buf = coeff;
00454 }
00455
00456 static inline int coeff_unpack_golomb(GetBitContext *gb, int qfactor, int qoffset)
00457 {
00458 int sign, coeff;
00459
00460 coeff = svq3_get_ue_golomb(gb);
00461 if (coeff) {
00462 coeff = (coeff * qfactor + qoffset + 2) >> 2;
00463 sign = get_bits1(gb);
00464 coeff = (coeff ^ -sign) + sign;
00465 }
00466 return coeff;
00467 }
00468
00473 static inline void codeblock(DiracContext *s, SubBand *b,
00474 GetBitContext *gb, DiracArith *c,
00475 int left, int right, int top, int bottom,
00476 int blockcnt_one, int is_arith)
00477 {
00478 int x, y, zero_block;
00479 int qoffset, qfactor;
00480 IDWTELEM *buf;
00481
00482
00483 if (!blockcnt_one) {
00484 if (is_arith)
00485 zero_block = dirac_get_arith_bit(c, CTX_ZERO_BLOCK);
00486 else
00487 zero_block = get_bits1(gb);
00488
00489 if (zero_block)
00490 return;
00491 }
00492
00493 if (s->codeblock_mode && !(s->old_delta_quant && blockcnt_one)) {
00494 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].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 seperation 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
00984 if (get_bits1(gb)) {
00985 av_log(s->avctx,AV_LOG_DEBUG,"Low Delay: Has Custom Quantization Matrix!\n");
00986
00987 s->lowdelay.quant[0][0] = svq3_get_ue_golomb(gb);
00988 for (level = 0; level < s->wavelet_depth; level++) {
00989 s->lowdelay.quant[level][1] = svq3_get_ue_golomb(gb);
00990 s->lowdelay.quant[level][2] = svq3_get_ue_golomb(gb);
00991 s->lowdelay.quant[level][3] = svq3_get_ue_golomb(gb);
00992 }
00993 } else {
00994
00995 for (level = 0; level < s->wavelet_depth; level++)
00996 for (i = 0; i < 4; i++) {
00997 s->lowdelay.quant[level][i] = default_qmat[s->wavelet_idx][level][i];
00998
00999 if (s->wavelet_idx == 3)
01000 s->lowdelay.quant[level][i] += 4*(s->wavelet_depth-1 - level);
01001 }
01002 }
01003 }
01004 return 0;
01005 }
01006
01007 static inline int pred_sbsplit(uint8_t *sbsplit, int stride, int x, int y)
01008 {
01009 static const uint8_t avgsplit[7] = { 0, 0, 1, 1, 1, 2, 2 };
01010
01011 if (!(x|y))
01012 return 0;
01013 else if (!y)
01014 return sbsplit[-1];
01015 else if (!x)
01016 return sbsplit[-stride];
01017
01018 return avgsplit[sbsplit[-1] + sbsplit[-stride] + sbsplit[-stride-1]];
01019 }
01020
01021 static inline int pred_block_mode(DiracBlock *block, int stride, int x, int y, int refmask)
01022 {
01023 int pred;
01024
01025 if (!(x|y))
01026 return 0;
01027 else if (!y)
01028 return block[-1].ref & refmask;
01029 else if (!x)
01030 return block[-stride].ref & refmask;
01031
01032
01033 pred = (block[-1].ref & refmask) + (block[-stride].ref & refmask) + (block[-stride-1].ref & refmask);
01034 return (pred >> 1) & refmask;
01035 }
01036
01037 static inline void pred_block_dc(DiracBlock *block, int stride, int x, int y)
01038 {
01039 int i, n = 0;
01040
01041 memset(block->u.dc, 0, sizeof(block->u.dc));
01042
01043 if (x && !(block[-1].ref & 3)) {
01044 for (i = 0; i < 3; i++)
01045 block->u.dc[i] += block[-1].u.dc[i];
01046 n++;
01047 }
01048
01049 if (y && !(block[-stride].ref & 3)) {
01050 for (i = 0; i < 3; i++)
01051 block->u.dc[i] += block[-stride].u.dc[i];
01052 n++;
01053 }
01054
01055 if (x && y && !(block[-1-stride].ref & 3)) {
01056 for (i = 0; i < 3; i++)
01057 block->u.dc[i] += block[-1-stride].u.dc[i];
01058 n++;
01059 }
01060
01061 if (n == 2) {
01062 for (i = 0; i < 3; i++)
01063 block->u.dc[i] = (block->u.dc[i]+1)>>1;
01064 } else if (n == 3) {
01065 for (i = 0; i < 3; i++)
01066 block->u.dc[i] = divide3(block->u.dc[i]);
01067 }
01068 }
01069
01070 static inline void pred_mv(DiracBlock *block, int stride, int x, int y, int ref)
01071 {
01072 int16_t *pred[3];
01073 int refmask = ref+1;
01074 int mask = refmask | DIRAC_REF_MASK_GLOBAL;
01075 int n = 0;
01076
01077 if (x && (block[-1].ref & mask) == refmask)
01078 pred[n++] = block[-1].u.mv[ref];
01079
01080 if (y && (block[-stride].ref & mask) == refmask)
01081 pred[n++] = block[-stride].u.mv[ref];
01082
01083 if (x && y && (block[-stride-1].ref & mask) == refmask)
01084 pred[n++] = block[-stride-1].u.mv[ref];
01085
01086 switch (n) {
01087 case 0:
01088 block->u.mv[ref][0] = 0;
01089 block->u.mv[ref][1] = 0;
01090 break;
01091 case 1:
01092 block->u.mv[ref][0] = pred[0][0];
01093 block->u.mv[ref][1] = pred[0][1];
01094 break;
01095 case 2:
01096 block->u.mv[ref][0] = (pred[0][0] + pred[1][0] + 1) >> 1;
01097 block->u.mv[ref][1] = (pred[0][1] + pred[1][1] + 1) >> 1;
01098 break;
01099 case 3:
01100 block->u.mv[ref][0] = mid_pred(pred[0][0], pred[1][0], pred[2][0]);
01101 block->u.mv[ref][1] = mid_pred(pred[0][1], pred[1][1], pred[2][1]);
01102 break;
01103 }
01104 }
01105
01106 static void global_mv(DiracContext *s, DiracBlock *block, int x, int y, int ref)
01107 {
01108 int ez = s->globalmc[ref].zrs_exp;
01109 int ep = s->globalmc[ref].perspective_exp;
01110 int (*A)[2] = s->globalmc[ref].zrs;
01111 int *b = s->globalmc[ref].pan_tilt;
01112 int *c = s->globalmc[ref].perspective;
01113
01114 int m = (1<<ep) - (c[0]*x + c[1]*y);
01115 int mx = m * ((A[0][0] * x + A[0][1]*y) + (1<<ez) * b[0]);
01116 int my = m * ((A[1][0] * x + A[1][1]*y) + (1<<ez) * b[1]);
01117
01118 block->u.mv[ref][0] = (mx + (1<<(ez+ep))) >> (ez+ep);
01119 block->u.mv[ref][1] = (my + (1<<(ez+ep))) >> (ez+ep);
01120 }
01121
01122 static void decode_block_params(DiracContext *s, DiracArith arith[8], DiracBlock *block,
01123 int stride, int x, int y)
01124 {
01125 int i;
01126
01127 block->ref = pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF1);
01128 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF1);
01129
01130 if (s->num_refs == 2) {
01131 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_REF2);
01132 block->ref ^= dirac_get_arith_bit(arith, CTX_PMODE_REF2) << 1;
01133 }
01134
01135 if (!block->ref) {
01136 pred_block_dc(block, stride, x, y);
01137 for (i = 0; i < 3; i++)
01138 block->u.dc[i] += dirac_get_arith_int(arith+1+i, CTX_DC_F1, CTX_DC_DATA);
01139 return;
01140 }
01141
01142 if (s->globalmc_flag) {
01143 block->ref |= pred_block_mode(block, stride, x, y, DIRAC_REF_MASK_GLOBAL);
01144 block->ref ^= dirac_get_arith_bit(arith, CTX_GLOBAL_BLOCK) << 2;
01145 }
01146
01147 for (i = 0; i < s->num_refs; i++)
01148 if (block->ref & (i+1)) {
01149 if (block->ref & DIRAC_REF_MASK_GLOBAL) {
01150 global_mv(s, block, x, y, i);
01151 } else {
01152 pred_mv(block, stride, x, y, i);
01153 block->u.mv[i][0] += dirac_get_arith_int(arith + 4 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
01154 block->u.mv[i][1] += dirac_get_arith_int(arith + 5 + 2 * i, CTX_MV_F1, CTX_MV_DATA);
01155 }
01156 }
01157 }
01158
01162 static void propagate_block_data(DiracBlock *block, int stride, int size)
01163 {
01164 int x, y;
01165 DiracBlock *dst = block;
01166
01167 for (x = 1; x < size; x++)
01168 dst[x] = *block;
01169
01170 for (y = 1; y < size; y++) {
01171 dst += stride;
01172 for (x = 0; x < size; x++)
01173 dst[x] = *block;
01174 }
01175 }
01176
01181 static int dirac_unpack_block_motion_data(DiracContext *s)
01182 {
01183 GetBitContext *gb = &s->gb;
01184 uint8_t *sbsplit = s->sbsplit;
01185 int i, x, y, q, p;
01186 DiracArith arith[8];
01187
01188 align_get_bits(gb);
01189
01190
01191 s->sbwidth = DIVRNDUP(s->source.width, 4*s->plane[0].xbsep);
01192 s->sbheight = DIVRNDUP(s->source.height, 4*s->plane[0].ybsep);
01193 s->blwidth = 4 * s->sbwidth;
01194 s->blheight = 4 * s->sbheight;
01195
01196
01197
01198 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
01199 for (y = 0; y < s->sbheight; y++) {
01200 for (x = 0; x < s->sbwidth; x++) {
01201 unsigned int split = dirac_get_arith_uint(arith, CTX_SB_F1, CTX_SB_DATA);
01202 if (split > 2)
01203 return -1;
01204 sbsplit[x] = (split + pred_sbsplit(sbsplit+x, s->sbwidth, x, y)) % 3;
01205 }
01206 sbsplit += s->sbwidth;
01207 }
01208
01209
01210 ff_dirac_init_arith_decoder(arith, gb, svq3_get_ue_golomb(gb));
01211 for (i = 0; i < s->num_refs; i++) {
01212 ff_dirac_init_arith_decoder(arith + 4 + 2 * i, gb, svq3_get_ue_golomb(gb));
01213 ff_dirac_init_arith_decoder(arith + 5 + 2 * i, gb, svq3_get_ue_golomb(gb));
01214 }
01215 for (i = 0; i < 3; i++)
01216 ff_dirac_init_arith_decoder(arith+1+i, gb, svq3_get_ue_golomb(gb));
01217
01218 for (y = 0; y < s->sbheight; y++)
01219 for (x = 0; x < s->sbwidth; x++) {
01220 int blkcnt = 1 << s->sbsplit[y * s->sbwidth + x];
01221 int step = 4 >> s->sbsplit[y * s->sbwidth + x];
01222
01223 for (q = 0; q < blkcnt; q++)
01224 for (p = 0; p < blkcnt; p++) {
01225 int bx = 4 * x + p*step;
01226 int by = 4 * y + q*step;
01227 DiracBlock *block = &s->blmotion[by*s->blwidth + bx];
01228 decode_block_params(s, arith, block, s->blwidth, bx, by);
01229 propagate_block_data(block, s->blwidth, step);
01230 }
01231 }
01232
01233 return 0;
01234 }
01235
01236 static int weight(int i, int blen, int offset)
01237 {
01238 #define ROLLOFF(i) offset == 1 ? ((i) ? 5 : 3) : \
01239 (1 + (6*(i) + offset - 1) / (2*offset - 1))
01240
01241 if (i < 2*offset)
01242 return ROLLOFF(i);
01243 else if (i > blen-1 - 2*offset)
01244 return ROLLOFF(blen-1 - i);
01245 return 8;
01246 }
01247
01248 static void init_obmc_weight_row(Plane *p, uint8_t *obmc_weight, int stride,
01249 int left, int right, int wy)
01250 {
01251 int x;
01252 for (x = 0; left && x < p->xblen >> 1; x++)
01253 obmc_weight[x] = wy*8;
01254 for (; x < p->xblen >> right; x++)
01255 obmc_weight[x] = wy*weight(x, p->xblen, p->xoffset);
01256 for (; x < p->xblen; x++)
01257 obmc_weight[x] = wy*8;
01258 for (; x < stride; x++)
01259 obmc_weight[x] = 0;
01260 }
01261
01262 static void init_obmc_weight(Plane *p, uint8_t *obmc_weight, int stride,
01263 int left, int right, int top, int bottom)
01264 {
01265 int y;
01266 for (y = 0; top && y < p->yblen >> 1; y++) {
01267 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
01268 obmc_weight += stride;
01269 }
01270 for (; y < p->yblen >> bottom; y++) {
01271 int wy = weight(y, p->yblen, p->yoffset);
01272 init_obmc_weight_row(p, obmc_weight, stride, left, right, wy);
01273 obmc_weight += stride;
01274 }
01275 for (; y < p->yblen; y++) {
01276 init_obmc_weight_row(p, obmc_weight, stride, left, right, 8);
01277 obmc_weight += stride;
01278 }
01279 }
01280
01281 static void init_obmc_weights(DiracContext *s, Plane *p, int by)
01282 {
01283 int top = !by;
01284 int bottom = by == s->blheight-1;
01285
01286
01287 if (top || bottom || by == 1) {
01288 init_obmc_weight(p, s->obmc_weight[0], MAX_BLOCKSIZE, 1, 0, top, bottom);
01289 init_obmc_weight(p, s->obmc_weight[1], MAX_BLOCKSIZE, 0, 0, top, bottom);
01290 init_obmc_weight(p, s->obmc_weight[2], MAX_BLOCKSIZE, 0, 1, top, bottom);
01291 }
01292 }
01293
01294 static const uint8_t epel_weights[4][4][4] = {
01295 {{ 16, 0, 0, 0 },
01296 { 12, 4, 0, 0 },
01297 { 8, 8, 0, 0 },
01298 { 4, 12, 0, 0 }},
01299 {{ 12, 0, 4, 0 },
01300 { 9, 3, 3, 1 },
01301 { 6, 6, 2, 2 },
01302 { 3, 9, 1, 3 }},
01303 {{ 8, 0, 8, 0 },
01304 { 6, 2, 6, 2 },
01305 { 4, 4, 4, 4 },
01306 { 2, 6, 2, 6 }},
01307 {{ 4, 0, 12, 0 },
01308 { 3, 1, 9, 3 },
01309 { 2, 2, 6, 6 },
01310 { 1, 3, 3, 9 }}
01311 };
01312
01321 static int mc_subpel(DiracContext *s, DiracBlock *block, const uint8_t *src[5],
01322 int x, int y, int ref, int plane)
01323 {
01324 Plane *p = &s->plane[plane];
01325 uint8_t **ref_hpel = s->ref_pics[ref]->hpel[plane];
01326 int motion_x = block->u.mv[ref][0];
01327 int motion_y = block->u.mv[ref][1];
01328 int mx, my, i, epel, nplanes = 0;
01329
01330 if (plane) {
01331 motion_x >>= s->chroma_x_shift;
01332 motion_y >>= s->chroma_y_shift;
01333 }
01334
01335 mx = motion_x & ~(-1 << s->mv_precision);
01336 my = motion_y & ~(-1 << s->mv_precision);
01337 motion_x >>= s->mv_precision;
01338 motion_y >>= s->mv_precision;
01339
01340
01341 mx <<= 3 - s->mv_precision;
01342 my <<= 3 - s->mv_precision;
01343
01344 x += motion_x;
01345 y += motion_y;
01346 epel = (mx|my)&1;
01347
01348
01349 if (!((mx|my)&3)) {
01350 nplanes = 1;
01351 src[0] = ref_hpel[(my>>1)+(mx>>2)] + y*p->stride + x;
01352 } else {
01353
01354 nplanes = 4;
01355 for (i = 0; i < 4; i++)
01356 src[i] = ref_hpel[i] + y*p->stride + x;
01357
01358
01359
01360 if (mx > 4) {
01361 src[0] += 1;
01362 src[2] += 1;
01363 x++;
01364 }
01365 if (my > 4) {
01366 src[0] += p->stride;
01367 src[1] += p->stride;
01368 y++;
01369 }
01370
01371
01372
01373
01374 if (!epel) {
01375
01376
01377 if (!(mx&3)) {
01378
01379
01380 src[!mx] = src[2 + !!mx];
01381 nplanes = 2;
01382 } else if (!(my&3)) {
01383 src[0] = src[(my>>1) ];
01384 src[1] = src[(my>>1)+1];
01385 nplanes = 2;
01386 }
01387 } else {
01388
01389 if (mx > 4) {
01390 FFSWAP(const uint8_t *, src[0], src[1]);
01391 FFSWAP(const uint8_t *, src[2], src[3]);
01392 }
01393 if (my > 4) {
01394 FFSWAP(const uint8_t *, src[0], src[2]);
01395 FFSWAP(const uint8_t *, src[1], src[3]);
01396 }
01397 src[4] = epel_weights[my&3][mx&3];
01398 }
01399 }
01400
01401
01402 if ((unsigned)x > p->width +EDGE_WIDTH/2 - p->xblen ||
01403 (unsigned)y > p->height+EDGE_WIDTH/2 - p->yblen) {
01404 for (i = 0; i < nplanes; i++) {
01405 ff_emulated_edge_mc(s->edge_emu_buffer[i], src[i], p->stride,
01406 p->xblen, p->yblen, x, y,
01407 p->width+EDGE_WIDTH/2, p->height+EDGE_WIDTH/2);
01408 src[i] = s->edge_emu_buffer[i];
01409 }
01410 }
01411 return (nplanes>>1) + epel;
01412 }
01413
01414 static void add_dc(uint16_t *dst, int dc, int stride,
01415 uint8_t *obmc_weight, int xblen, int yblen)
01416 {
01417 int x, y;
01418 dc += 128;
01419
01420 for (y = 0; y < yblen; y++) {
01421 for (x = 0; x < xblen; x += 2) {
01422 dst[x ] += dc * obmc_weight[x ];
01423 dst[x+1] += dc * obmc_weight[x+1];
01424 }
01425 dst += stride;
01426 obmc_weight += MAX_BLOCKSIZE;
01427 }
01428 }
01429
01430 static void block_mc(DiracContext *s, DiracBlock *block,
01431 uint16_t *mctmp, uint8_t *obmc_weight,
01432 int plane, int dstx, int dsty)
01433 {
01434 Plane *p = &s->plane[plane];
01435 const uint8_t *src[5];
01436 int idx;
01437
01438 switch (block->ref&3) {
01439 case 0:
01440 add_dc(mctmp, block->u.dc[plane], p->stride, obmc_weight, p->xblen, p->yblen);
01441 return;
01442 case 1:
01443 case 2:
01444 idx = mc_subpel(s, block, src, dstx, dsty, (block->ref&3)-1, plane);
01445 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01446 if (s->weight_func)
01447 s->weight_func(s->mcscratch, p->stride, s->weight_log2denom,
01448 s->weight[0] + s->weight[1], p->yblen);
01449 break;
01450 case 3:
01451 idx = mc_subpel(s, block, src, dstx, dsty, 0, plane);
01452 s->put_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01453 idx = mc_subpel(s, block, src, dstx, dsty, 1, plane);
01454 if (s->biweight_func) {
01455
01456 s->put_pixels_tab[idx](s->mcscratch + 32, src, p->stride, p->yblen);
01457 s->biweight_func(s->mcscratch, s->mcscratch+32, p->stride, s->weight_log2denom,
01458 s->weight[0], s->weight[1], p->yblen);
01459 } else
01460 s->avg_pixels_tab[idx](s->mcscratch, src, p->stride, p->yblen);
01461 break;
01462 }
01463 s->add_obmc(mctmp, s->mcscratch, p->stride, obmc_weight, p->yblen);
01464 }
01465
01466 static void mc_row(DiracContext *s, DiracBlock *block, uint16_t *mctmp, int plane, int dsty)
01467 {
01468 Plane *p = &s->plane[plane];
01469 int x, dstx = p->xbsep - p->xoffset;
01470
01471 block_mc(s, block, mctmp, s->obmc_weight[0], plane, -p->xoffset, dsty);
01472 mctmp += p->xbsep;
01473
01474 for (x = 1; x < s->blwidth-1; x++) {
01475 block_mc(s, block+x, mctmp, s->obmc_weight[1], plane, dstx, dsty);
01476 dstx += p->xbsep;
01477 mctmp += p->xbsep;
01478 }
01479 block_mc(s, block+x, mctmp, s->obmc_weight[2], plane, dstx, dsty);
01480 }
01481
01482 static void select_dsp_funcs(DiracContext *s, int width, int height, int xblen, int yblen)
01483 {
01484 int idx = 0;
01485 if (xblen > 8)
01486 idx = 1;
01487 if (xblen > 16)
01488 idx = 2;
01489
01490 memcpy(s->put_pixels_tab, s->diracdsp.put_dirac_pixels_tab[idx], sizeof(s->put_pixels_tab));
01491 memcpy(s->avg_pixels_tab, s->diracdsp.avg_dirac_pixels_tab[idx], sizeof(s->avg_pixels_tab));
01492 s->add_obmc = s->diracdsp.add_dirac_obmc[idx];
01493 if (s->weight_log2denom > 1 || s->weight[0] != 1 || s->weight[1] != 1) {
01494 s->weight_func = s->diracdsp.weight_dirac_pixels_tab[idx];
01495 s->biweight_func = s->diracdsp.biweight_dirac_pixels_tab[idx];
01496 } else {
01497 s->weight_func = NULL;
01498 s->biweight_func = NULL;
01499 }
01500 }
01501
01502 static void interpolate_refplane(DiracContext *s, DiracFrame *ref, int plane, int width, int height)
01503 {
01504
01505
01506
01507 int i, edge = EDGE_WIDTH/2;
01508
01509 ref->hpel[plane][0] = ref->avframe.data[plane];
01510 s->dsp.draw_edges(ref->hpel[plane][0], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01511
01512
01513 if (!s->mv_precision)
01514 return;
01515
01516 for (i = 1; i < 4; i++) {
01517 if (!ref->hpel_base[plane][i])
01518 ref->hpel_base[plane][i] = av_malloc((height+2*edge) * ref->avframe.linesize[plane] + 32);
01519
01520 ref->hpel[plane][i] = ref->hpel_base[plane][i] + edge*ref->avframe.linesize[plane] + 16;
01521 }
01522
01523 if (!ref->interpolated[plane]) {
01524 s->diracdsp.dirac_hpel_filter(ref->hpel[plane][1], ref->hpel[plane][2],
01525 ref->hpel[plane][3], ref->hpel[plane][0],
01526 ref->avframe.linesize[plane], width, height);
01527 s->dsp.draw_edges(ref->hpel[plane][1], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01528 s->dsp.draw_edges(ref->hpel[plane][2], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01529 s->dsp.draw_edges(ref->hpel[plane][3], ref->avframe.linesize[plane], width, height, edge, edge, EDGE_TOP | EDGE_BOTTOM);
01530 }
01531 ref->interpolated[plane] = 1;
01532 }
01533
01538 static int dirac_decode_frame_internal(DiracContext *s)
01539 {
01540 DWTContext d;
01541 int y, i, comp, dsty;
01542
01543 if (s->low_delay) {
01544
01545 for (comp = 0; comp < 3; comp++) {
01546 Plane *p = &s->plane[comp];
01547 memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM));
01548 }
01549 if (!s->zero_res)
01550 decode_lowdelay(s);
01551 }
01552
01553 for (comp = 0; comp < 3; comp++) {
01554 Plane *p = &s->plane[comp];
01555 uint8_t *frame = s->current_picture->avframe.data[comp];
01556
01557
01558 for (i = 0; i < 4; i++)
01559 s->edge_emu_buffer[i] = s->edge_emu_buffer_base + i*FFALIGN(p->width, 16);
01560
01561 if (!s->zero_res && !s->low_delay)
01562 {
01563 memset(p->idwt_buf, 0, p->idwt_stride * p->idwt_height * sizeof(IDWTELEM));
01564 decode_component(s, comp);
01565 }
01566 if (ff_spatial_idwt_init2(&d, p->idwt_buf, p->idwt_width, p->idwt_height, p->idwt_stride,
01567 s->wavelet_idx+2, s->wavelet_depth, p->idwt_tmp))
01568 return -1;
01569
01570 if (!s->num_refs) {
01571 for (y = 0; y < p->height; y += 16) {
01572 ff_spatial_idwt_slice2(&d, y+16);
01573 s->diracdsp.put_signed_rect_clamped(frame + y*p->stride, p->stride,
01574 p->idwt_buf + y*p->idwt_stride, p->idwt_stride, p->width, 16);
01575 }
01576 } else {
01577 int rowheight = p->ybsep*p->stride;
01578
01579 select_dsp_funcs(s, p->width, p->height, p->xblen, p->yblen);
01580
01581 for (i = 0; i < s->num_refs; i++)
01582 interpolate_refplane(s, s->ref_pics[i], comp, p->width, p->height);
01583
01584 memset(s->mctmp, 0, 4*p->yoffset*p->stride);
01585
01586 dsty = -p->yoffset;
01587 for (y = 0; y < s->blheight; y++) {
01588 int h = 0,
01589 start = FFMAX(dsty, 0);
01590 uint16_t *mctmp = s->mctmp + y*rowheight;
01591 DiracBlock *blocks = s->blmotion + y*s->blwidth;
01592
01593 init_obmc_weights(s, p, y);
01594
01595 if (y == s->blheight-1 || start+p->ybsep > p->height)
01596 h = p->height - start;
01597 else
01598 h = p->ybsep - (start - dsty);
01599 if (h < 0)
01600 break;
01601
01602 memset(mctmp+2*p->yoffset*p->stride, 0, 2*rowheight);
01603 mc_row(s, blocks, mctmp, comp, dsty);
01604
01605 mctmp += (start - dsty)*p->stride + p->xoffset;
01606 ff_spatial_idwt_slice2(&d, start + h);
01607 s->diracdsp.add_rect_clamped(frame + start*p->stride, mctmp, p->stride,
01608 p->idwt_buf + start*p->idwt_stride, p->idwt_stride, p->width, h);
01609
01610 dsty += p->ybsep;
01611 }
01612 }
01613 }
01614
01615
01616 return 0;
01617 }
01618
01623 static int dirac_decode_picture_header(DiracContext *s)
01624 {
01625 int retire, picnum;
01626 int i, j, refnum, refdist;
01627 GetBitContext *gb = &s->gb;
01628
01629
01630 picnum = s->current_picture->avframe.display_picture_number = get_bits_long(gb, 32);
01631
01632
01633 av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);
01634
01635
01636
01637 if (s->frame_number < 0)
01638 s->frame_number = picnum;
01639
01640 s->ref_pics[0] = s->ref_pics[1] = NULL;
01641 for (i = 0; i < s->num_refs; i++) {
01642 refnum = picnum + dirac_get_se_golomb(gb);
01643 refdist = INT_MAX;
01644
01645
01646
01647 for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)
01648 if (s->ref_frames[j]
01649 && FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum) < refdist) {
01650 s->ref_pics[i] = s->ref_frames[j];
01651 refdist = FFABS(s->ref_frames[j]->avframe.display_picture_number - refnum);
01652 }
01653
01654 if (!s->ref_pics[i] || refdist)
01655 av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");
01656
01657
01658 if (!s->ref_pics[i])
01659 for (j = 0; j < MAX_FRAMES; j++)
01660 if (!s->all_frames[j].avframe.data[0]) {
01661 s->ref_pics[i] = &s->all_frames[j];
01662 s->avctx->get_buffer(s->avctx, &s->ref_pics[i]->avframe);
01663 }
01664 }
01665
01666
01667 if (s->current_picture->avframe.reference) {
01668 retire = picnum + dirac_get_se_golomb(gb);
01669 if (retire != picnum) {
01670 DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);
01671
01672 if (retire_pic)
01673 retire_pic->avframe.reference &= DELAYED_PIC_REF;
01674 else
01675 av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");
01676 }
01677
01678
01679 while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {
01680 av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");
01681 remove_frame(s->ref_frames, s->ref_frames[0]->avframe.display_picture_number)->avframe.reference &= DELAYED_PIC_REF;
01682 }
01683 }
01684
01685 if (s->num_refs) {
01686 if (dirac_unpack_prediction_parameters(s))
01687 return -1;
01688 if (dirac_unpack_block_motion_data(s))
01689 return -1;
01690 }
01691 if (dirac_unpack_idwt_params(s))
01692 return -1;
01693
01694 init_planes(s);
01695 return 0;
01696 }
01697
01698 static int get_delayed_pic(DiracContext *s, AVFrame *picture, int *data_size)
01699 {
01700 DiracFrame *out = s->delay_frames[0];
01701 int i, out_idx = 0;
01702
01703
01704 for (i = 1; s->delay_frames[i]; i++)
01705 if (s->delay_frames[i]->avframe.display_picture_number < out->avframe.display_picture_number) {
01706 out = s->delay_frames[i];
01707 out_idx = i;
01708 }
01709
01710 for (i = out_idx; s->delay_frames[i]; i++)
01711 s->delay_frames[i] = s->delay_frames[i+1];
01712
01713 if (out) {
01714 out->avframe.reference ^= DELAYED_PIC_REF;
01715 *data_size = sizeof(AVFrame);
01716 *(AVFrame *)picture = out->avframe;
01717 }
01718
01719 return 0;
01720 }
01721
01727 #define DATA_UNIT_HEADER_SIZE 13
01728
01729
01730
01731 static int dirac_decode_data_unit(AVCodecContext *avctx, const uint8_t *buf, int size)
01732 {
01733 DiracContext *s = avctx->priv_data;
01734 DiracFrame *pic = NULL;
01735 int i, parse_code = buf[4];
01736 unsigned tmp;
01737
01738 if (size < DATA_UNIT_HEADER_SIZE)
01739 return -1;
01740
01741 init_get_bits(&s->gb, &buf[13], 8*(size - DATA_UNIT_HEADER_SIZE));
01742
01743 if (parse_code == pc_seq_header) {
01744 if (s->seen_sequence_header)
01745 return 0;
01746
01747
01748 if (avpriv_dirac_parse_sequence_header(avctx, &s->gb, &s->source))
01749 return -1;
01750
01751 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &s->chroma_x_shift, &s->chroma_y_shift);
01752
01753 if (alloc_sequence_buffers(s))
01754 return -1;
01755
01756 s->seen_sequence_header = 1;
01757 } else if (parse_code == pc_eos) {
01758 free_sequence_buffers(s);
01759 s->seen_sequence_header = 0;
01760 } else if (parse_code == pc_aux_data) {
01761 if (buf[13] == 1) {
01762 int ver[3];
01763
01764
01765 if (sscanf(buf+14, "Schroedinger %d.%d.%d", ver, ver+1, ver+2) == 3)
01766 if (ver[0] == 1 && ver[1] == 0 && ver[2] <= 7)
01767 s->old_delta_quant = 1;
01768 }
01769 } else if (parse_code & 0x8) {
01770 if (!s->seen_sequence_header) {
01771 av_log(avctx, AV_LOG_DEBUG, "Dropping frame without sequence header\n");
01772 return -1;
01773 }
01774
01775
01776 for (i = 0; i < MAX_FRAMES; i++)
01777 if (s->all_frames[i].avframe.data[0] == NULL)
01778 pic = &s->all_frames[i];
01779 if (!pic) {
01780 av_log(avctx, AV_LOG_ERROR, "framelist full\n");
01781 return -1;
01782 }
01783
01784 avcodec_get_frame_defaults(&pic->avframe);
01785
01786
01787 tmp = parse_code & 0x03;
01788 if (tmp > 2) {
01789 av_log(avctx, AV_LOG_ERROR, "num_refs of 3\n");
01790 return -1;
01791 }
01792 s->num_refs = tmp;
01793 s->is_arith = (parse_code & 0x48) == 0x08;
01794 s->low_delay = (parse_code & 0x88) == 0x88;
01795 pic->avframe.reference = (parse_code & 0x0C) == 0x0C;
01796 pic->avframe.key_frame = s->num_refs == 0;
01797 pic->avframe.pict_type = s->num_refs + 1;
01798
01799 if (avctx->get_buffer(avctx, &pic->avframe) < 0) {
01800 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01801 return -1;
01802 }
01803 s->current_picture = pic;
01804 s->plane[0].stride = pic->avframe.linesize[0];
01805 s->plane[1].stride = pic->avframe.linesize[1];
01806 s->plane[2].stride = pic->avframe.linesize[2];
01807
01808
01809 if (dirac_decode_picture_header(s))
01810 return -1;
01811
01812
01813 if (dirac_decode_frame_internal(s))
01814 return -1;
01815 }
01816 return 0;
01817 }
01818
01819 static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
01820 {
01821 DiracContext *s = avctx->priv_data;
01822 DiracFrame *picture = data;
01823 uint8_t *buf = pkt->data;
01824 int buf_size = pkt->size;
01825 int i, data_unit_size, buf_idx = 0;
01826
01827
01828 for (i = 0; i < MAX_FRAMES; i++)
01829 if (s->all_frames[i].avframe.data[0] && !s->all_frames[i].avframe.reference) {
01830 avctx->release_buffer(avctx, &s->all_frames[i].avframe);
01831 memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
01832 }
01833
01834 s->current_picture = NULL;
01835 *data_size = 0;
01836
01837
01838 if (buf_size == 0)
01839 return get_delayed_pic(s, (AVFrame *)data, data_size);
01840
01841 for (;;) {
01842
01843
01844
01845 for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {
01846 if (buf[buf_idx ] == 'B' && buf[buf_idx+1] == 'B' &&
01847 buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')
01848 break;
01849 }
01850
01851 if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)
01852 break;
01853
01854 data_unit_size = AV_RB32(buf+buf_idx+5);
01855 if (buf_idx + data_unit_size > buf_size || !data_unit_size) {
01856 if(buf_idx + data_unit_size > buf_size)
01857 av_log(s->avctx, AV_LOG_ERROR,
01858 "Data unit with size %d is larger than input buffer, discarding\n",
01859 data_unit_size);
01860 buf_idx += 4;
01861 continue;
01862 }
01863
01864 if (dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size))
01865 {
01866 av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");
01867 return -1;
01868 }
01869 buf_idx += data_unit_size;
01870 }
01871
01872 if (!s->current_picture)
01873 return 0;
01874
01875 if (s->current_picture->avframe.display_picture_number > s->frame_number) {
01876 DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);
01877
01878 s->current_picture->avframe.reference |= DELAYED_PIC_REF;
01879
01880 if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {
01881 int min_num = s->delay_frames[0]->avframe.display_picture_number;
01882
01883 av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");
01884 delayed_frame = s->delay_frames[0];
01885
01886 for (i = 1; s->delay_frames[i]; i++)
01887 if (s->delay_frames[i]->avframe.display_picture_number < min_num)
01888 min_num = s->delay_frames[i]->avframe.display_picture_number;
01889
01890 delayed_frame = remove_frame(s->delay_frames, min_num);
01891 add_frame(s->delay_frames, MAX_DELAY, s->current_picture);
01892 }
01893
01894 if (delayed_frame) {
01895 delayed_frame->avframe.reference ^= DELAYED_PIC_REF;
01896 *(AVFrame*)data = delayed_frame->avframe;
01897 *data_size = sizeof(AVFrame);
01898 }
01899 } else if (s->current_picture->avframe.display_picture_number == s->frame_number) {
01900
01901 *(AVFrame*)data = s->current_picture->avframe;
01902 *data_size = sizeof(AVFrame);
01903 }
01904
01905 if (*data_size)
01906 s->frame_number = picture->avframe.display_picture_number + 1;
01907
01908 return buf_idx;
01909 }
01910
01911 AVCodec ff_dirac_decoder = {
01912 .name = "dirac",
01913 .type = AVMEDIA_TYPE_VIDEO,
01914 .id = CODEC_ID_DIRAC,
01915 .priv_data_size = sizeof(DiracContext),
01916 .init = dirac_decode_init,
01917 .close = dirac_decode_end,
01918 .decode = dirac_decode_frame,
01919 .capabilities = CODEC_CAP_DELAY,
01920 .flush = dirac_decode_flush,
01921 .long_name = NULL_IF_CONFIG_SMALL("BBC Dirac VC-2"),
01922 };