00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00036 #include "avcodec.h"
00037 #include "dsputil.h"
00038 #include "mpegvideo.h"
00039 #include "mathops.h"
00040
00041 #include "svq1.h"
00042
00043 #undef NDEBUG
00044 #include <assert.h>
00045
00046 extern const uint8_t ff_mvtab[33][2];
00047
00048 static VLC svq1_block_type;
00049 static VLC svq1_motion_component;
00050 static VLC svq1_intra_multistage[6];
00051 static VLC svq1_inter_multistage[6];
00052 static VLC svq1_intra_mean;
00053 static VLC svq1_inter_mean;
00054
00055
00056 typedef struct svq1_pmv_s {
00057 int x;
00058 int y;
00059 } svq1_pmv;
00060
00061 static const uint8_t string_table[256] = {
00062 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
00063 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
00064 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
00065 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
00066 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
00067 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
00068 0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
00069 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
00070 0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
00071 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
00072 0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
00073 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
00074 0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
00075 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
00076 0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
00077 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
00078 0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
00079 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
00080 0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
00081 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
00082 0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
00083 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
00084 0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
00085 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
00086 0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
00087 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
00088 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
00089 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
00090 0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
00091 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
00092 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
00093 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
00094 };
00095
00096 #define SVQ1_PROCESS_VECTOR()\
00097 for (; level > 0; i++) {\
00098 \
00099 if (i == m) {\
00100 m = n;\
00101 if (--level == 0)\
00102 break;\
00103 }\
00104 \
00105 if (get_bits1 (bitbuf) == 0)\
00106 break;\
00107 \
00108 list[n++] = list[i];\
00109 list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level >> 1) + 1));\
00110 }
00111
00112 #define SVQ1_ADD_CODEBOOK()\
00113 \
00114 for (j=0; j < stages; j++) {\
00115 n3 = codebook[entries[j]] ^ 0x80808080;\
00116 n1 += ((n3 & 0xFF00FF00) >> 8);\
00117 n2 += (n3 & 0x00FF00FF);\
00118 }\
00119 \
00120 \
00121 if (n1 & 0xFF00FF00) {\
00122 n3 = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00123 n1 += 0x7F007F00;\
00124 n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00125 n1 &= (n3 & 0x00FF00FF);\
00126 }\
00127 \
00128 if (n2 & 0xFF00FF00) {\
00129 n3 = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00130 n2 += 0x7F007F00;\
00131 n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00132 n2 &= (n3 & 0x00FF00FF);\
00133 }
00134
00135 #define SVQ1_DO_CODEBOOK_INTRA()\
00136 for (y=0; y < height; y++) {\
00137 for (x=0; x < (width / 4); x++, codebook++) {\
00138 n1 = n4;\
00139 n2 = n4;\
00140 SVQ1_ADD_CODEBOOK()\
00141 \
00142 dst[x] = (n1 << 8) | n2;\
00143 }\
00144 dst += (pitch / 4);\
00145 }
00146
00147 #define SVQ1_DO_CODEBOOK_NONINTRA()\
00148 for (y=0; y < height; y++) {\
00149 for (x=0; x < (width / 4); x++, codebook++) {\
00150 n3 = dst[x];\
00151 \
00152 n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
00153 n2 = (n3 & 0x00FF00FF) + n4;\
00154 SVQ1_ADD_CODEBOOK()\
00155 \
00156 dst[x] = (n1 << 8) | n2;\
00157 }\
00158 dst += (pitch / 4);\
00159 }
00160
00161 #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
00162 codebook = (const uint32_t *) cbook[level];\
00163 if (stages > 0)\
00164 bit_cache = get_bits (bitbuf, 4*stages);\
00165 \
00166 for (j=0; j < stages; j++) {\
00167 entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
00168 }\
00169 mean -= (stages * 128);\
00170 n4 = (mean << 16) + mean;
00171
00172 static int svq1_decode_block_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
00173 uint32_t bit_cache;
00174 uint8_t *list[63];
00175 uint32_t *dst;
00176 const uint32_t *codebook;
00177 int entries[6];
00178 int i, j, m, n;
00179 int mean, stages;
00180 unsigned x, y, width, height, level;
00181 uint32_t n1, n2, n3, n4;
00182
00183
00184 list[0] = pixels;
00185
00186
00187 for (i=0, m=1, n=1, level=5; i < n; i++) {
00188 SVQ1_PROCESS_VECTOR();
00189
00190
00191 dst = (uint32_t *) list[i];
00192 width = 1 << ((4 + level) /2);
00193 height = 1 << ((3 + level) /2);
00194
00195
00196 stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
00197
00198 if (stages == -1) {
00199 for (y=0; y < height; y++) {
00200 memset (&dst[y*(pitch / 4)], 0, width);
00201 }
00202 continue;
00203 }
00204
00205 if ((stages > 0) && (level >= 4)) {
00206 av_dlog(NULL,
00207 "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",
00208 stages, level);
00209 return -1;
00210 }
00211
00212 mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
00213
00214 if (stages == 0) {
00215 for (y=0; y < height; y++) {
00216 memset (&dst[y*(pitch / 4)], mean, width);
00217 }
00218 } else {
00219 SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_intra_codebooks);
00220 SVQ1_DO_CODEBOOK_INTRA()
00221 }
00222 }
00223
00224 return 0;
00225 }
00226
00227 static int svq1_decode_block_non_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
00228 uint32_t bit_cache;
00229 uint8_t *list[63];
00230 uint32_t *dst;
00231 const uint32_t *codebook;
00232 int entries[6];
00233 int i, j, m, n;
00234 int mean, stages;
00235 int x, y, width, height, level;
00236 uint32_t n1, n2, n3, n4;
00237
00238
00239 list[0] = pixels;
00240
00241
00242 for (i=0, m=1, n=1, level=5; i < n; i++) {
00243 SVQ1_PROCESS_VECTOR();
00244
00245
00246 dst = (uint32_t *) list[i];
00247 width = 1 << ((4 + level) /2);
00248 height = 1 << ((3 + level) /2);
00249
00250
00251 stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
00252
00253 if (stages == -1) continue;
00254
00255 if ((stages > 0) && (level >= 4)) {
00256 av_dlog(NULL,
00257 "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",
00258 stages, level);
00259 return -1;
00260 }
00261
00262 mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
00263
00264 SVQ1_CALC_CODEBOOK_ENTRIES(ff_svq1_inter_codebooks);
00265 SVQ1_DO_CODEBOOK_NONINTRA()
00266 }
00267 return 0;
00268 }
00269
00270 static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv *mv, svq1_pmv **pmv) {
00271 int diff;
00272 int i;
00273
00274 for (i=0; i < 2; i++) {
00275
00276
00277 diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
00278 if(diff<0)
00279 return -1;
00280 else if(diff){
00281 if(get_bits1(bitbuf)) diff= -diff;
00282 }
00283
00284
00285 if (i == 1)
00286 mv->y = sign_extend(diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y), 6);
00287 else
00288 mv->x = sign_extend(diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x), 6);
00289 }
00290
00291 return 0;
00292 }
00293
00294 static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
00295 uint8_t *src;
00296 uint8_t *dst;
00297 int i;
00298
00299 src = &previous[x + y*pitch];
00300 dst = current;
00301
00302 for (i=0; i < 16; i++) {
00303 memcpy (dst, src, 16);
00304 src += pitch;
00305 dst += pitch;
00306 }
00307 }
00308
00309 static int svq1_motion_inter_block (MpegEncContext *s, GetBitContext *bitbuf,
00310 uint8_t *current, uint8_t *previous, int pitch,
00311 svq1_pmv *motion, int x, int y) {
00312 uint8_t *src;
00313 uint8_t *dst;
00314 svq1_pmv mv;
00315 svq1_pmv *pmv[3];
00316 int result;
00317
00318
00319 pmv[0] = &motion[0];
00320 if (y == 0) {
00321 pmv[1] =
00322 pmv[2] = pmv[0];
00323 }
00324 else {
00325 pmv[1] = &motion[(x / 8) + 2];
00326 pmv[2] = &motion[(x / 8) + 4];
00327 }
00328
00329 result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
00330
00331 if (result != 0)
00332 return result;
00333
00334 motion[0].x =
00335 motion[(x / 8) + 2].x =
00336 motion[(x / 8) + 3].x = mv.x;
00337 motion[0].y =
00338 motion[(x / 8) + 2].y =
00339 motion[(x / 8) + 3].y = mv.y;
00340
00341 if(y + (mv.y >> 1)<0)
00342 mv.y= 0;
00343 if(x + (mv.x >> 1)<0)
00344 mv.x= 0;
00345
00346 src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
00347 dst = current;
00348
00349 s->dsp.put_pixels_tab[0][((mv.y & 1) << 1) | (mv.x & 1)](dst,src,pitch,16);
00350
00351 return 0;
00352 }
00353
00354 static int svq1_motion_inter_4v_block (MpegEncContext *s, GetBitContext *bitbuf,
00355 uint8_t *current, uint8_t *previous, int pitch,
00356 svq1_pmv *motion,int x, int y) {
00357 uint8_t *src;
00358 uint8_t *dst;
00359 svq1_pmv mv;
00360 svq1_pmv *pmv[4];
00361 int i, result;
00362
00363
00364 pmv[0] = &motion[0];
00365 if (y == 0) {
00366 pmv[1] =
00367 pmv[2] = pmv[0];
00368 }
00369 else {
00370 pmv[1] = &motion[(x / 8) + 2];
00371 pmv[2] = &motion[(x / 8) + 4];
00372 }
00373
00374 result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
00375
00376 if (result != 0)
00377 return result;
00378
00379
00380 pmv[0] = &mv;
00381 if (y == 0) {
00382 pmv[1] =
00383 pmv[2] = pmv[0];
00384 }
00385 else {
00386 pmv[1] = &motion[(x / 8) + 3];
00387 }
00388 result = svq1_decode_motion_vector (bitbuf, &motion[0], pmv);
00389
00390 if (result != 0)
00391 return result;
00392
00393
00394 pmv[1] = &motion[0];
00395 pmv[2] = &motion[(x / 8) + 1];
00396
00397 result = svq1_decode_motion_vector (bitbuf, &motion[(x / 8) + 2], pmv);
00398
00399 if (result != 0)
00400 return result;
00401
00402
00403 pmv[2] = &motion[(x / 8) + 2];
00404 pmv[3] = &motion[(x / 8) + 3];
00405
00406 result = svq1_decode_motion_vector (bitbuf, pmv[3], pmv);
00407
00408 if (result != 0)
00409 return result;
00410
00411
00412 for (i=0; i < 4; i++) {
00413 int mvx= pmv[i]->x + (i&1)*16;
00414 int mvy= pmv[i]->y + (i>>1)*16;
00415
00417 if(y + (mvy >> 1)<0)
00418 mvy= 0;
00419 if(x + (mvx >> 1)<0)
00420 mvx= 0;
00421
00422 src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
00423 dst = current;
00424
00425 s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
00426
00427
00428 if (i & 1) {
00429 current += 8*(pitch - 1);
00430 } else {
00431 current += 8;
00432 }
00433 }
00434
00435 return 0;
00436 }
00437
00438 static int svq1_decode_delta_block (MpegEncContext *s, GetBitContext *bitbuf,
00439 uint8_t *current, uint8_t *previous, int pitch,
00440 svq1_pmv *motion, int x, int y) {
00441 uint32_t block_type;
00442 int result = 0;
00443
00444
00445 block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
00446
00447
00448 if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
00449 motion[0].x =
00450 motion[0].y =
00451 motion[(x / 8) + 2].x =
00452 motion[(x / 8) + 2].y =
00453 motion[(x / 8) + 3].x =
00454 motion[(x / 8) + 3].y = 0;
00455 }
00456
00457 switch (block_type) {
00458 case SVQ1_BLOCK_SKIP:
00459 svq1_skip_block (current, previous, pitch, x, y);
00460 break;
00461
00462 case SVQ1_BLOCK_INTER:
00463 result = svq1_motion_inter_block (s, bitbuf, current, previous, pitch, motion, x, y);
00464
00465 if (result != 0)
00466 {
00467 av_dlog(s->avctx, "Error in svq1_motion_inter_block %i\n", result);
00468 break;
00469 }
00470 result = svq1_decode_block_non_intra (bitbuf, current, pitch);
00471 break;
00472
00473 case SVQ1_BLOCK_INTER_4V:
00474 result = svq1_motion_inter_4v_block (s, bitbuf, current, previous, pitch, motion, x, y);
00475
00476 if (result != 0)
00477 {
00478 av_dlog(s->avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
00479 break;
00480 }
00481 result = svq1_decode_block_non_intra (bitbuf, current, pitch);
00482 break;
00483
00484 case SVQ1_BLOCK_INTRA:
00485 result = svq1_decode_block_intra (bitbuf, current, pitch);
00486 break;
00487 }
00488
00489 return result;
00490 }
00491
00492 static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) {
00493 uint8_t seed;
00494 int i;
00495
00496 out[0] = get_bits (bitbuf, 8);
00497
00498 seed = string_table[out[0]];
00499
00500 for (i=1; i <= out[0]; i++) {
00501 out[i] = get_bits (bitbuf, 8) ^ seed;
00502 seed = string_table[out[i] ^ seed];
00503 }
00504 }
00505
00506 static int svq1_decode_frame_header (GetBitContext *bitbuf,MpegEncContext *s) {
00507 int frame_size_code;
00508
00509 skip_bits(bitbuf, 8);
00510
00511
00512 s->pict_type= get_bits (bitbuf, 2)+1;
00513 if(s->pict_type==4)
00514 return -1;
00515
00516 if (s->pict_type == AV_PICTURE_TYPE_I) {
00517
00518
00519 if (s->f_code == 0x50 || s->f_code == 0x60) {
00520 int csum = get_bits (bitbuf, 16);
00521
00522 csum = ff_svq1_packet_checksum (bitbuf->buffer, bitbuf->size_in_bits>>3, csum);
00523
00524
00525
00526 }
00527
00528 if ((s->f_code ^ 0x10) >= 0x50) {
00529 uint8_t msg[256];
00530
00531 svq1_parse_string (bitbuf, msg);
00532
00533 av_log(s->avctx, AV_LOG_INFO, "embedded message: \"%s\"\n", (char *) msg);
00534 }
00535
00536 skip_bits (bitbuf, 2);
00537 skip_bits (bitbuf, 2);
00538 skip_bits1 (bitbuf);
00539
00540
00541 frame_size_code = get_bits (bitbuf, 3);
00542
00543 if (frame_size_code == 7) {
00544
00545 s->width = get_bits (bitbuf, 12);
00546 s->height = get_bits (bitbuf, 12);
00547
00548 if (!s->width || !s->height)
00549 return -1;
00550 } else {
00551
00552 s->width = ff_svq1_frame_size_table[frame_size_code].width;
00553 s->height = ff_svq1_frame_size_table[frame_size_code].height;
00554 }
00555 }
00556
00557
00558 if (get_bits1 (bitbuf) == 1) {
00559 skip_bits1 (bitbuf);
00560 skip_bits1 (bitbuf);
00561
00562 if (get_bits (bitbuf, 2) != 0)
00563 return -1;
00564 }
00565
00566 if (get_bits1 (bitbuf) == 1) {
00567 skip_bits1 (bitbuf);
00568 skip_bits (bitbuf, 4);
00569 skip_bits1 (bitbuf);
00570 skip_bits (bitbuf, 2);
00571
00572 while (get_bits1 (bitbuf) == 1) {
00573 skip_bits (bitbuf, 8);
00574 }
00575 }
00576
00577 return 0;
00578 }
00579
00580 static int svq1_decode_frame(AVCodecContext *avctx,
00581 void *data, int *data_size,
00582 AVPacket *avpkt)
00583 {
00584 const uint8_t *buf = avpkt->data;
00585 int buf_size = avpkt->size;
00586 MpegEncContext *s=avctx->priv_data;
00587 uint8_t *current, *previous;
00588 int result, i, x, y, width, height;
00589 AVFrame *pict = data;
00590 svq1_pmv *pmv;
00591
00592
00593 init_get_bits(&s->gb,buf,buf_size*8);
00594
00595
00596 s->f_code = get_bits (&s->gb, 22);
00597
00598 if ((s->f_code & ~0x70) || !(s->f_code & 0x60))
00599 return -1;
00600
00601
00602 if (s->f_code != 0x20) {
00603 uint32_t *src = (uint32_t *) (buf + 4);
00604
00605 if (buf_size < 36)
00606 return AVERROR_INVALIDDATA;
00607
00608 for (i=0; i < 4; i++) {
00609 src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
00610 }
00611 }
00612
00613 result = svq1_decode_frame_header (&s->gb, s);
00614
00615 if (result != 0)
00616 {
00617 av_dlog(s->avctx, "Error in svq1_decode_frame_header %i\n",result);
00618 return result;
00619 }
00620 avcodec_set_dimensions(avctx, s->width, s->height);
00621
00622
00623
00624 if(s->pict_type==AV_PICTURE_TYPE_B && s->last_picture_ptr==NULL) return buf_size;
00625
00626 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
00627 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
00628 || avctx->skip_frame >= AVDISCARD_ALL)
00629 return buf_size;
00630
00631 if(ff_MPV_frame_start(s, avctx) < 0)
00632 return -1;
00633
00634 pmv = av_malloc((FFALIGN(s->width, 16)/8 + 3) * sizeof(*pmv));
00635 if (!pmv)
00636 return -1;
00637
00638
00639 for (i=0; i < 3; i++) {
00640 int linesize;
00641 if (i == 0) {
00642 width = FFALIGN(s->width, 16);
00643 height = FFALIGN(s->height, 16);
00644 linesize= s->linesize;
00645 } else {
00646 if(s->flags&CODEC_FLAG_GRAY) break;
00647 width = FFALIGN(s->width/4, 16);
00648 height = FFALIGN(s->height/4, 16);
00649 linesize= s->uvlinesize;
00650 }
00651
00652 current = s->current_picture.f.data[i];
00653
00654 if(s->pict_type==AV_PICTURE_TYPE_B){
00655 previous = s->next_picture.f.data[i];
00656 }else{
00657 previous = s->last_picture.f.data[i];
00658 }
00659
00660 if (s->pict_type == AV_PICTURE_TYPE_I) {
00661
00662 for (y=0; y < height; y+=16) {
00663 for (x=0; x < width; x+=16) {
00664 result = svq1_decode_block_intra (&s->gb, ¤t[x], linesize);
00665 if (result != 0)
00666 {
00667 av_log(s->avctx, AV_LOG_ERROR, "Error in svq1_decode_block %i (keyframe)\n",result);
00668 goto err;
00669 }
00670 }
00671 current += 16*linesize;
00672 }
00673 } else {
00674
00675 memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv));
00676
00677 for (y=0; y < height; y+=16) {
00678 for (x=0; x < width; x+=16) {
00679 result = svq1_decode_delta_block (s, &s->gb, ¤t[x], previous,
00680 linesize, pmv, x, y);
00681 if (result != 0)
00682 {
00683 av_dlog(s->avctx, "Error in svq1_decode_delta_block %i\n",result);
00684 goto err;
00685 }
00686 }
00687
00688 pmv[0].x =
00689 pmv[0].y = 0;
00690
00691 current += 16*linesize;
00692 }
00693 }
00694 }
00695
00696 *pict = s->current_picture.f;
00697
00698
00699 ff_MPV_frame_end(s);
00700
00701 *data_size=sizeof(AVFrame);
00702 result = buf_size;
00703 err:
00704 av_free(pmv);
00705 return result;
00706 }
00707
00708 static av_cold int svq1_decode_init(AVCodecContext *avctx)
00709 {
00710 MpegEncContext *s = avctx->priv_data;
00711 int i;
00712 int offset = 0;
00713
00714 ff_MPV_decode_defaults(s);
00715
00716 s->avctx = avctx;
00717 s->width = (avctx->width+3)&~3;
00718 s->height = (avctx->height+3)&~3;
00719 s->codec_id= avctx->codec->id;
00720 avctx->pix_fmt = PIX_FMT_YUV410P;
00721 avctx->has_b_frames= 1;
00722 s->flags= avctx->flags;
00723 if (ff_MPV_common_init(s) < 0) return -1;
00724
00725 INIT_VLC_STATIC(&svq1_block_type, 2, 4,
00726 &ff_svq1_block_type_vlc[0][1], 2, 1,
00727 &ff_svq1_block_type_vlc[0][0], 2, 1, 6);
00728
00729 INIT_VLC_STATIC(&svq1_motion_component, 7, 33,
00730 &ff_mvtab[0][1], 2, 1,
00731 &ff_mvtab[0][0], 2, 1, 176);
00732
00733 for (i = 0; i < 6; i++) {
00734 static const uint8_t sizes[2][6] = {{14, 10, 14, 18, 16, 18}, {10, 10, 14, 14, 14, 16}};
00735 static VLC_TYPE table[168][2];
00736 svq1_intra_multistage[i].table = &table[offset];
00737 svq1_intra_multistage[i].table_allocated = sizes[0][i];
00738 offset += sizes[0][i];
00739 init_vlc(&svq1_intra_multistage[i], 3, 8,
00740 &ff_svq1_intra_multistage_vlc[i][0][1], 2, 1,
00741 &ff_svq1_intra_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
00742 svq1_inter_multistage[i].table = &table[offset];
00743 svq1_inter_multistage[i].table_allocated = sizes[1][i];
00744 offset += sizes[1][i];
00745 init_vlc(&svq1_inter_multistage[i], 3, 8,
00746 &ff_svq1_inter_multistage_vlc[i][0][1], 2, 1,
00747 &ff_svq1_inter_multistage_vlc[i][0][0], 2, 1, INIT_VLC_USE_NEW_STATIC);
00748 }
00749
00750 INIT_VLC_STATIC(&svq1_intra_mean, 8, 256,
00751 &ff_svq1_intra_mean_vlc[0][1], 4, 2,
00752 &ff_svq1_intra_mean_vlc[0][0], 4, 2, 632);
00753
00754 INIT_VLC_STATIC(&svq1_inter_mean, 9, 512,
00755 &ff_svq1_inter_mean_vlc[0][1], 4, 2,
00756 &ff_svq1_inter_mean_vlc[0][0], 4, 2, 1434);
00757
00758 return 0;
00759 }
00760
00761 static av_cold int svq1_decode_end(AVCodecContext *avctx)
00762 {
00763 MpegEncContext *s = avctx->priv_data;
00764
00765 ff_MPV_common_end(s);
00766 return 0;
00767 }
00768
00769
00770 AVCodec ff_svq1_decoder = {
00771 .name = "svq1",
00772 .type = AVMEDIA_TYPE_VIDEO,
00773 .id = AV_CODEC_ID_SVQ1,
00774 .priv_data_size = sizeof(MpegEncContext),
00775 .init = svq1_decode_init,
00776 .close = svq1_decode_end,
00777 .decode = svq1_decode_frame,
00778 .capabilities = CODEC_CAP_DR1,
00779 .flush = ff_mpeg_flush,
00780 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV410P, PIX_FMT_NONE },
00781 .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
00782 };