static inline void dv_calc_mb_coordinates(const AVDVProfile *d, int chan, int seq, int slot,
                                          uint16_t *tbl)
{
    static const uint8_t off[] = { 2, 6, 8, 0, 4 };
    static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };
    static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };
    static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };
    static const uint8_t l_start[] = {0, 4, 9, 13, 18, 22, 27, 31, 36, 40};
    static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };
    static const uint8_t serpent1[] = {0, 1, 2, 2, 1, 0,
                                       0, 1, 2, 2, 1, 0,
                                       0, 1, 2, 2, 1, 0,
                                       0, 1, 2, 2, 1, 0,
                                       0, 1, 2};
    static const uint8_t serpent2[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
                                       0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
                                       0, 1, 2, 3, 4, 5};
    static const uint8_t remap[][2] = {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0},
                                       { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3}, {10, 0},
                                       {10, 1}, {10, 2}, {10, 3}, {20, 0}, {20, 1},
                                       {20, 2}, {20, 3}, {30, 0}, {30, 1}, {30, 2},
                                       {30, 3}, {40, 0}, {40, 1}, {40, 2}, {40, 3},
                                       {50, 0}, {50, 1}, {50, 2}, {50, 3}, {60, 0},
                                       {60, 1}, {60, 2}, {60, 3}, {70, 0}, {70, 1},
                                       {70, 2}, {70, 3}, { 0,64}, { 0,65}, { 0,66},
                                       {10,64}, {10,65}, {10,66}, {20,64}, {20,65},
                                       {20,66}, {30,64}, {30,65}, {30,66}, {40,64},
                                       {40,65}, {40,66}, {50,64}, {50,65}, {50,66},
                                       {60,64}, {60,65}, {60,66}, {70,64}, {70,65},
                                       {70,66}, { 0,67}, {20,67}, {40,67}, {60,67}};
    int i, k, m;
    int x, y, blk;
    for (m=0; m<5; m++) {
         switch (d->width) {
         case 1440:
              blk = (chan*11+seq)*27+slot;
              if (chan == 0 && seq == 11) {
                  x = m*27+slot;
                  if (x<90) {
                      y = 0;
                  } else {
                      x = (x - 90)*2;
                      y = 67;
                  }
              } else {
                  i = (4*chan + blk + off[m])%11;
                  k = (blk/11)%27;
                  x = shuf1[m] + (chan&1)*9 + k%9;
                  y = (i*3+k/9)*2 + (chan>>1) + 1;
              }
              tbl[m] = (x<<1)|(y<<9);
              break;
         case 1280:
              blk = (chan*10+seq)*27+slot;
              i = (4*chan + (seq/5) + 2*blk + off[m])%10;
              k = (blk/5)%27;
              x = shuf1[m]+(chan&1)*9 + k%9;
              y = (i*3+k/9)*2 + (chan>>1) + 4;
              if (x >= 80) {
                  x = remap[y][0]+((x-80)<<(y>59));
                  y = remap[y][1];
              }
              tbl[m] = (x<<1)|(y<<9);
              break;
       case 960:
              blk = (chan*10+seq)*27+slot;
              i = (4*chan + (seq/5) + 2*blk + off[m])%10;
              k = (blk/5)%27 + (i&1)*3;
              x = shuf2[m] + k%6 + 6*(chan&1);
              y = l_start[i] + k/6 + 45*(chan>>1);
              tbl[m] = (x<<1)|(y<<9);
              break;
        case 720:
              switch (d->pix_fmt) {
              case AV_PIX_FMT_YUV422P:
                   x = shuf3[m] + slot/3;
                   y = serpent1[slot] +
                       ((((seq + off[m]) % d->difseg_size)<<1) + chan)*3;
                   tbl[m] = (x<<1)|(y<<8);
                   break;
              case AV_PIX_FMT_YUV420P:
                   x = shuf3[m] + slot/3;
                   y = serpent1[slot] +
                       ((seq + off[m]) % d->difseg_size)*3;
                   tbl[m] = (x<<1)|(y<<9);
                   break;
              case AV_PIX_FMT_YUV411P:
                   i = (seq + off[m]) % d->difseg_size;
                   k = slot + ((m==1||m==2)?3:0);
                   x = l_start_shuffled[m] + k/6;
                   y = serpent2[k] + i*6;
                   if (x>21)
                       y = y*2 - i*6;
                   tbl[m] = (x<<2)|(y<<8);
                   break;
              }
        default:
              break;
        }
    }
}
['static int dvvideo_decode_frame(AVCodecContext *avctx,\n                                 void *data, int *got_frame,\n                                 AVPacket *avpkt)\n{\n    uint8_t *buf = avpkt->data;\n    int buf_size = avpkt->size;\n    DVVideoContext *s = avctx->priv_data;\n    const uint8_t* vsc_pack;\n    int apt, is16_9, ret;\n    const AVDVProfile *sys;\n    sys = av_dv_frame_profile(s->sys, buf, buf_size);\n    if (!sys || buf_size < sys->frame_size) {\n        av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\\n");\n        return -1;\n    }\n    if (sys != s->sys) {\n        ret = ff_dv_init_dynamic_tables(s, sys);\n        if (ret < 0) {\n            av_log(avctx, AV_LOG_ERROR, "Error initializing the work tables.\\n");\n            return ret;\n        }\n        s->sys = sys;\n    }\n    s->frame            = data;\n    s->frame->key_frame = 1;\n    s->frame->pict_type = AV_PICTURE_TYPE_I;\n    avctx->pix_fmt   = s->sys->pix_fmt;\n    avctx->time_base = s->sys->time_base;\n    ret = ff_set_dimensions(avctx, s->sys->width, s->sys->height);\n    if (ret < 0)\n        return ret;\n    vsc_pack = buf + 80*5 + 48 + 5;\n    if ( *vsc_pack == dv_video_control ) {\n        apt = buf[4] & 0x07;\n        is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || (!apt && (vsc_pack[2] & 0x07) == 0x07)));\n        ff_set_sar(avctx, s->sys->sar[is16_9]);\n    }\n    if (ff_get_buffer(avctx, s->frame, 0) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n        return -1;\n    }\n    s->frame->interlaced_frame = 1;\n    s->frame->top_field_first  = 0;\n    s->buf = buf;\n    avctx->execute(avctx, dv_decode_video_segment, s->work_chunks, NULL,\n                   dv_work_pool_size(s->sys), sizeof(DVwork_chunk));\n    emms_c();\n    *got_frame = 1;\n    return s->sys->frame_size;\n}', 'const AVDVProfile *av_dv_frame_profile(const AVDVProfile *sys,\n                                       const uint8_t* frame, unsigned buf_size)\n{\n#if CONFIG_DVPROFILE\n    int i, dsf, stype;\n    if (buf_size < 80 * 5 + 48 + 4)\n        return NULL;\n    dsf = (frame[3] & 0x80) >> 7;\n    stype = frame[80 * 5 + 48 + 3] & 0x1f;\n    if (dsf == 1 && stype == 0 && frame[4] & 0x07  ) {\n        return &dv_profiles[2];\n    }\n    for (i = 0; i < FF_ARRAY_ELEMS(dv_profiles); i++)\n        if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)\n            return &dv_profiles[i];\n    if (sys && buf_size == sys->frame_size)\n        return sys;\n#endif\n    return NULL;\n}', 'int ff_dv_init_dynamic_tables(DVVideoContext *ctx, const AVDVProfile *d)\n{\n    int j,i,c,s,p;\n    uint32_t *factor1, *factor2;\n    const int *iweight1, *iweight2;\n    p = i = 0;\n    for (c = 0; c < d->n_difchan; c++) {\n        for (s = 0; s < d->difseg_size; s++) {\n            p += 6;\n            for (j = 0; j < 27; j++) {\n                p += !(j % 3);\n                if (!(DV_PROFILE_IS_1080i50(d) && c != 0 && s == 11) &&\n                    !(DV_PROFILE_IS_720p50(d) && s > 9)) {\n                      dv_calc_mb_coordinates(d, c, s, j, &ctx->work_chunks[i].mb_coordinates[0]);\n                      ctx->work_chunks[i++].buf_offset = p;\n                }\n                p += 5;\n            }\n        }\n    }\n    factor1 = &ctx->idct_factor[0];\n    factor2 = &ctx->idct_factor[DV_PROFILE_IS_HD(d) ? 4096 : 2816];\n    if (d->height == 720) {\n        iweight1 = &ff_dv_iweight_720_y[0];\n        iweight2 = &ff_dv_iweight_720_c[0];\n    } else {\n        iweight1 = &ff_dv_iweight_1080_y[0];\n        iweight2 = &ff_dv_iweight_1080_c[0];\n    }\n    if (DV_PROFILE_IS_HD(d)) {\n        for (c = 0; c < 4; c++) {\n            for (s = 0; s < 16; s++) {\n                for (i = 0; i < 64; i++) {\n                    *factor1++ = (dv100_qstep[s] << (c + 9)) * iweight1[i];\n                    *factor2++ = (dv100_qstep[s] << (c + 9)) * iweight2[i];\n                }\n            }\n        }\n    } else {\n        iweight1 = &ff_dv_iweight_88[0];\n        for (j = 0; j < 2; j++, iweight1 = &ff_dv_iweight_248[0]) {\n            for (s = 0; s < 22; s++) {\n                for (i = c = 0; c < 4; c++) {\n                    for (; i < dv_quant_areas[c]; i++) {\n                        *factor1   = iweight1[i] << (ff_dv_quant_shifts[s][c] + 1);\n                        *factor2++ = (*factor1++) << 1;\n                    }\n                }\n            }\n        }\n    }\n    return 0;\n}', 'static inline void dv_calc_mb_coordinates(const AVDVProfile *d, int chan, int seq, int slot,\n                                          uint16_t *tbl)\n{\n    static const uint8_t off[] = { 2, 6, 8, 0, 4 };\n    static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };\n    static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };\n    static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };\n    static const uint8_t l_start[] = {0, 4, 9, 13, 18, 22, 27, 31, 36, 40};\n    static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };\n    static const uint8_t serpent1[] = {0, 1, 2, 2, 1, 0,\n                                       0, 1, 2, 2, 1, 0,\n                                       0, 1, 2, 2, 1, 0,\n                                       0, 1, 2, 2, 1, 0,\n                                       0, 1, 2};\n    static const uint8_t serpent2[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,\n                                       0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,\n                                       0, 1, 2, 3, 4, 5};\n    static const uint8_t remap[][2] = {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0},\n                                       { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3}, {10, 0},\n                                       {10, 1}, {10, 2}, {10, 3}, {20, 0}, {20, 1},\n                                       {20, 2}, {20, 3}, {30, 0}, {30, 1}, {30, 2},\n                                       {30, 3}, {40, 0}, {40, 1}, {40, 2}, {40, 3},\n                                       {50, 0}, {50, 1}, {50, 2}, {50, 3}, {60, 0},\n                                       {60, 1}, {60, 2}, {60, 3}, {70, 0}, {70, 1},\n                                       {70, 2}, {70, 3}, { 0,64}, { 0,65}, { 0,66},\n                                       {10,64}, {10,65}, {10,66}, {20,64}, {20,65},\n                                       {20,66}, {30,64}, {30,65}, {30,66}, {40,64},\n                                       {40,65}, {40,66}, {50,64}, {50,65}, {50,66},\n                                       {60,64}, {60,65}, {60,66}, {70,64}, {70,65},\n                                       {70,66}, { 0,67}, {20,67}, {40,67}, {60,67}};\n    int i, k, m;\n    int x, y, blk;\n    for (m=0; m<5; m++) {\n         switch (d->width) {\n         case 1440:\n              blk = (chan*11+seq)*27+slot;\n              if (chan == 0 && seq == 11) {\n                  x = m*27+slot;\n                  if (x<90) {\n                      y = 0;\n                  } else {\n                      x = (x - 90)*2;\n                      y = 67;\n                  }\n              } else {\n                  i = (4*chan + blk + off[m])%11;\n                  k = (blk/11)%27;\n                  x = shuf1[m] + (chan&1)*9 + k%9;\n                  y = (i*3+k/9)*2 + (chan>>1) + 1;\n              }\n              tbl[m] = (x<<1)|(y<<9);\n              break;\n         case 1280:\n              blk = (chan*10+seq)*27+slot;\n              i = (4*chan + (seq/5) + 2*blk + off[m])%10;\n              k = (blk/5)%27;\n              x = shuf1[m]+(chan&1)*9 + k%9;\n              y = (i*3+k/9)*2 + (chan>>1) + 4;\n              if (x >= 80) {\n                  x = remap[y][0]+((x-80)<<(y>59));\n                  y = remap[y][1];\n              }\n              tbl[m] = (x<<1)|(y<<9);\n              break;\n       case 960:\n              blk = (chan*10+seq)*27+slot;\n              i = (4*chan + (seq/5) + 2*blk + off[m])%10;\n              k = (blk/5)%27 + (i&1)*3;\n              x = shuf2[m] + k%6 + 6*(chan&1);\n              y = l_start[i] + k/6 + 45*(chan>>1);\n              tbl[m] = (x<<1)|(y<<9);\n              break;\n        case 720:\n              switch (d->pix_fmt) {\n              case AV_PIX_FMT_YUV422P:\n                   x = shuf3[m] + slot/3;\n                   y = serpent1[slot] +\n                       ((((seq + off[m]) % d->difseg_size)<<1) + chan)*3;\n                   tbl[m] = (x<<1)|(y<<8);\n                   break;\n              case AV_PIX_FMT_YUV420P:\n                   x = shuf3[m] + slot/3;\n                   y = serpent1[slot] +\n                       ((seq + off[m]) % d->difseg_size)*3;\n                   tbl[m] = (x<<1)|(y<<9);\n                   break;\n              case AV_PIX_FMT_YUV411P:\n                   i = (seq + off[m]) % d->difseg_size;\n                   k = slot + ((m==1||m==2)?3:0);\n                   x = l_start_shuffled[m] + k/6;\n                   y = serpent2[k] + i*6;\n                   if (x>21)\n                       y = y*2 - i*6;\n                   tbl[m] = (x<<2)|(y<<8);\n                   break;\n              }\n        default:\n              break;\n        }\n    }\n}']