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
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <limits.h>
00041
00042 #include "common.h"
00043 #include "avcodec.h"
00044 #include "dsputil.h"
00045 #include "mpegvideo.h"
00046 #include "bswap.h"
00047
00048 #undef NDEBUG
00049 #include <assert.h>
00050
00051 extern const uint8_t mvtab[33][2];
00052
00053 static VLC svq1_block_type;
00054 static VLC svq1_motion_component;
00055 static VLC svq1_intra_multistage[6];
00056 static VLC svq1_inter_multistage[6];
00057 static VLC svq1_intra_mean;
00058 static VLC svq1_inter_mean;
00059
00060 #define SVQ1_BLOCK_SKIP 0
00061 #define SVQ1_BLOCK_INTER 1
00062 #define SVQ1_BLOCK_INTER_4V 2
00063 #define SVQ1_BLOCK_INTRA 3
00064
00065 typedef struct SVQ1Context {
00066 MpegEncContext m;
00067 AVCodecContext *avctx;
00068 DSPContext dsp;
00069 AVFrame picture;
00070 AVFrame current_picture;
00071 AVFrame last_picture;
00072 PutBitContext pb;
00073 GetBitContext gb;
00074
00075 PutBitContext reorder_pb[6];
00076
00077 int frame_width;
00078 int frame_height;
00079
00080
00081 int y_block_width;
00082 int y_block_height;
00083
00084
00085 int c_block_width;
00086 int c_block_height;
00087
00088 uint16_t *mb_type;
00089 uint32_t *dummy;
00090 int16_t (*motion_val8[3])[2];
00091 int16_t (*motion_val16[3])[2];
00092
00093 int64_t rd_total;
00094 } SVQ1Context;
00095
00096
00097 typedef struct svq1_pmv_s {
00098 int x;
00099 int y;
00100 } svq1_pmv_t;
00101
00102 #include "svq1_cb.h"
00103 #include "svq1_vlc.h"
00104
00105 static const uint16_t checksum_table[256] = {
00106 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
00107 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
00108 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
00109 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
00110 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
00111 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
00112 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
00113 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
00114 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
00115 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
00116 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
00117 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
00118 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
00119 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
00120 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
00121 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
00122 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
00123 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
00124 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
00125 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
00126 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
00127 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
00128 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
00129 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
00130 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
00131 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
00132 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
00133 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
00134 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
00135 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
00136 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
00137 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
00138 };
00139
00140 static const uint8_t string_table[256] = {
00141 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54,
00142 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D,
00143 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06,
00144 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F,
00145 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0,
00146 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9,
00147 0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2,
00148 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B,
00149 0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9,
00150 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0,
00151 0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B,
00152 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2,
00153 0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D,
00154 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44,
00155 0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F,
00156 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16,
00157 0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB,
00158 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92,
00159 0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9,
00160 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0,
00161 0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F,
00162 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36,
00163 0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D,
00164 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64,
00165 0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26,
00166 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F,
00167 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74,
00168 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D,
00169 0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82,
00170 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB,
00171 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0,
00172 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9
00173 };
00174
00175 #define SVQ1_PROCESS_VECTOR()\
00176 for (; level > 0; i++) {\
00177 \
00178 if (i == m) {\
00179 m = n;\
00180 if (--level == 0)\
00181 break;\
00182 }\
00183 \
00184 if (get_bits (bitbuf, 1) == 0)\
00185 break;\
00186 \
00187 list[n++] = list[i];\
00188 list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level / 2) + 1));\
00189 }
00190
00191 #define SVQ1_ADD_CODEBOOK()\
00192 \
00193 for (j=0; j < stages; j++) {\
00194 n3 = codebook[entries[j]] ^ 0x80808080;\
00195 n1 += ((n3 & 0xFF00FF00) >> 8);\
00196 n2 += (n3 & 0x00FF00FF);\
00197 }\
00198 \
00199 \
00200 if (n1 & 0xFF00FF00) {\
00201 n3 = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00202 n1 += 0x7F007F00;\
00203 n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00204 n1 &= (n3 & 0x00FF00FF);\
00205 }\
00206 \
00207 if (n2 & 0xFF00FF00) {\
00208 n3 = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00209 n2 += 0x7F007F00;\
00210 n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
00211 n2 &= (n3 & 0x00FF00FF);\
00212 }
00213
00214 #define SVQ1_DO_CODEBOOK_INTRA()\
00215 for (y=0; y < height; y++) {\
00216 for (x=0; x < (width / 4); x++, codebook++) {\
00217 n1 = n4;\
00218 n2 = n4;\
00219 SVQ1_ADD_CODEBOOK()\
00220 \
00221 dst[x] = (n1 << 8) | n2;\
00222 }\
00223 dst += (pitch / 4);\
00224 }
00225
00226 #define SVQ1_DO_CODEBOOK_NONINTRA()\
00227 for (y=0; y < height; y++) {\
00228 for (x=0; x < (width / 4); x++, codebook++) {\
00229 n3 = dst[x];\
00230 \
00231 n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
00232 n2 = (n3 & 0x00FF00FF) + n4;\
00233 SVQ1_ADD_CODEBOOK()\
00234 \
00235 dst[x] = (n1 << 8) | n2;\
00236 }\
00237 dst += (pitch / 4);\
00238 }
00239
00240 #define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
00241 codebook = (const uint32_t *) cbook[level];\
00242 bit_cache = get_bits (bitbuf, 4*stages);\
00243 \
00244 for (j=0; j < stages; j++) {\
00245 entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
00246 }\
00247 mean -= (stages * 128);\
00248 n4 = ((mean + (mean >> 31)) << 16) | (mean & 0xFFFF);
00249
00250 static int svq1_decode_block_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
00251 uint32_t bit_cache;
00252 uint8_t *list[63];
00253 uint32_t *dst;
00254 const uint32_t *codebook;
00255 int entries[6];
00256 int i, j, m, n;
00257 int mean, stages;
00258 unsigned x, y, width, height, level;
00259 uint32_t n1, n2, n3, n4;
00260
00261
00262 list[0] = pixels;
00263
00264
00265 for (i=0, m=1, n=1, level=5; i < n; i++) {
00266 SVQ1_PROCESS_VECTOR();
00267
00268
00269 dst = (uint32_t *) list[i];
00270 width = 1 << ((4 + level) /2);
00271 height = 1 << ((3 + level) /2);
00272
00273
00274 stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
00275
00276 if (stages == -1) {
00277 for (y=0; y < height; y++) {
00278 memset (&dst[y*(pitch / 4)], 0, width);
00279 }
00280 continue;
00281 }
00282
00283 if ((stages > 0) && (level >= 4)) {
00284 #ifdef DEBUG_SVQ1
00285 av_log(s->avctx, AV_LOG_INFO, "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",stages,level);
00286 #endif
00287 return -1;
00288 }
00289
00290 mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
00291
00292 if (stages == 0) {
00293 for (y=0; y < height; y++) {
00294 memset (&dst[y*(pitch / 4)], mean, width);
00295 }
00296 } else {
00297 SVQ1_CALC_CODEBOOK_ENTRIES(svq1_intra_codebooks);
00298 SVQ1_DO_CODEBOOK_INTRA()
00299 }
00300 }
00301
00302 return 0;
00303 }
00304
00305 static int svq1_decode_block_non_intra (GetBitContext *bitbuf, uint8_t *pixels, int pitch ) {
00306 uint32_t bit_cache;
00307 uint8_t *list[63];
00308 uint32_t *dst;
00309 const uint32_t *codebook;
00310 int entries[6];
00311 int i, j, m, n;
00312 int mean, stages;
00313 int x, y, width, height, level;
00314 uint32_t n1, n2, n3, n4;
00315
00316
00317 list[0] = pixels;
00318
00319
00320 for (i=0, m=1, n=1, level=5; i < n; i++) {
00321 SVQ1_PROCESS_VECTOR();
00322
00323
00324 dst = (uint32_t *) list[i];
00325 width = 1 << ((4 + level) /2);
00326 height = 1 << ((3 + level) /2);
00327
00328
00329 stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
00330
00331 if (stages == -1) continue;
00332
00333 if ((stages > 0) && (level >= 4)) {
00334 #ifdef DEBUG_SVQ1
00335 av_log(s->avctx, AV_LOG_INFO, "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",stages,level);
00336 #endif
00337 return -1;
00338 }
00339
00340 mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
00341
00342 SVQ1_CALC_CODEBOOK_ENTRIES(svq1_inter_codebooks);
00343 SVQ1_DO_CODEBOOK_NONINTRA()
00344 }
00345 return 0;
00346 }
00347
00348 static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv_t *mv, svq1_pmv_t **pmv) {
00349 int diff;
00350 int i;
00351
00352 for (i=0; i < 2; i++) {
00353
00354
00355 diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
00356 if(diff<0)
00357 return -1;
00358 else if(diff){
00359 if(get_bits1(bitbuf)) diff= -diff;
00360 }
00361
00362
00363 if (i == 1)
00364 mv->y = ((diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y)) << 26) >> 26;
00365 else
00366 mv->x = ((diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x)) << 26) >> 26;
00367 }
00368
00369 return 0;
00370 }
00371
00372 static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
00373 uint8_t *src;
00374 uint8_t *dst;
00375 int i;
00376
00377 src = &previous[x + y*pitch];
00378 dst = current;
00379
00380 for (i=0; i < 16; i++) {
00381 memcpy (dst, src, 16);
00382 src += pitch;
00383 dst += pitch;
00384 }
00385 }
00386
00387 static int svq1_motion_inter_block (MpegEncContext *s, GetBitContext *bitbuf,
00388 uint8_t *current, uint8_t *previous, int pitch,
00389 svq1_pmv_t *motion, int x, int y) {
00390 uint8_t *src;
00391 uint8_t *dst;
00392 svq1_pmv_t mv;
00393 svq1_pmv_t *pmv[3];
00394 int result;
00395
00396
00397 pmv[0] = &motion[0];
00398 if (y == 0) {
00399 pmv[1] =
00400 pmv[2] = pmv[0];
00401 }
00402 else {
00403 pmv[1] = &motion[(x / 8) + 2];
00404 pmv[2] = &motion[(x / 8) + 4];
00405 }
00406
00407 result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
00408
00409 if (result != 0)
00410 return result;
00411
00412 motion[0].x =
00413 motion[(x / 8) + 2].x =
00414 motion[(x / 8) + 3].x = mv.x;
00415 motion[0].y =
00416 motion[(x / 8) + 2].y =
00417 motion[(x / 8) + 3].y = mv.y;
00418
00419 if(y + (mv.y >> 1)<0)
00420 mv.y= 0;
00421 if(x + (mv.x >> 1)<0)
00422 mv.x= 0;
00423
00424 #if 0
00425 int w= (s->width+15)&~15;
00426 int h= (s->height+15)&~15;
00427 if(x + (mv.x >> 1)<0 || y + (mv.y >> 1)<0 || x + (mv.x >> 1) + 16 > w || y + (mv.y >> 1) + 16> h)
00428 av_log(s->avctx, AV_LOG_INFO, "%d %d %d %d\n", x, y, x + (mv.x >> 1), y + (mv.y >> 1));
00429 #endif
00430
00431 src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
00432 dst = current;
00433
00434 s->dsp.put_pixels_tab[0][((mv.y & 1) << 1) | (mv.x & 1)](dst,src,pitch,16);
00435
00436 return 0;
00437 }
00438
00439 static int svq1_motion_inter_4v_block (MpegEncContext *s, GetBitContext *bitbuf,
00440 uint8_t *current, uint8_t *previous, int pitch,
00441 svq1_pmv_t *motion,int x, int y) {
00442 uint8_t *src;
00443 uint8_t *dst;
00444 svq1_pmv_t mv;
00445 svq1_pmv_t *pmv[4];
00446 int i, result;
00447
00448
00449 pmv[0] = &motion[0];
00450 if (y == 0) {
00451 pmv[1] =
00452 pmv[2] = pmv[0];
00453 }
00454 else {
00455 pmv[1] = &motion[(x / 8) + 2];
00456 pmv[2] = &motion[(x / 8) + 4];
00457 }
00458
00459 result = svq1_decode_motion_vector (bitbuf, &mv, pmv);
00460
00461 if (result != 0)
00462 return result;
00463
00464
00465 pmv[0] = &mv;
00466 if (y == 0) {
00467 pmv[1] =
00468 pmv[2] = pmv[0];
00469 }
00470 else {
00471 pmv[1] = &motion[(x / 8) + 3];
00472 }
00473 result = svq1_decode_motion_vector (bitbuf, &motion[0], pmv);
00474
00475 if (result != 0)
00476 return result;
00477
00478
00479 pmv[1] = &motion[0];
00480 pmv[2] = &motion[(x / 8) + 1];
00481
00482 result = svq1_decode_motion_vector (bitbuf, &motion[(x / 8) + 2], pmv);
00483
00484 if (result != 0)
00485 return result;
00486
00487
00488 pmv[2] = &motion[(x / 8) + 2];
00489 pmv[3] = &motion[(x / 8) + 3];
00490
00491 result = svq1_decode_motion_vector (bitbuf, pmv[3], pmv);
00492
00493 if (result != 0)
00494 return result;
00495
00496
00497 for (i=0; i < 4; i++) {
00498 int mvx= pmv[i]->x + (i&1)*16;
00499 int mvy= pmv[i]->y + (i>>1)*16;
00500
00502 if(y + (mvy >> 1)<0)
00503 mvy= 0;
00504 if(x + (mvx >> 1)<0)
00505 mvx= 0;
00506
00507 #if 0
00508 int w= (s->width+15)&~15;
00509 int h= (s->height+15)&~15;
00510 if(x + (mvx >> 1)<0 || y + (mvy >> 1)<0 || x + (mvx >> 1) + 8 > w || y + (mvy >> 1) + 8> h)
00511 av_log(s->avctx, AV_LOG_INFO, "%d %d %d %d\n", x, y, x + (mvx >> 1), y + (mvy >> 1));
00512 #endif
00513 src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
00514 dst = current;
00515
00516 s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
00517
00518
00519 if (i & 1) {
00520 current += 8*(pitch - 1);
00521 } else {
00522 current += 8;
00523 }
00524 }
00525
00526 return 0;
00527 }
00528
00529 static int svq1_decode_delta_block (MpegEncContext *s, GetBitContext *bitbuf,
00530 uint8_t *current, uint8_t *previous, int pitch,
00531 svq1_pmv_t *motion, int x, int y) {
00532 uint32_t block_type;
00533 int result = 0;
00534
00535
00536 block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
00537
00538
00539 if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
00540 motion[0].x =
00541 motion[0].y =
00542 motion[(x / 8) + 2].x =
00543 motion[(x / 8) + 2].y =
00544 motion[(x / 8) + 3].x =
00545 motion[(x / 8) + 3].y = 0;
00546 }
00547
00548 switch (block_type) {
00549 case SVQ1_BLOCK_SKIP:
00550 svq1_skip_block (current, previous, pitch, x, y);
00551 break;
00552
00553 case SVQ1_BLOCK_INTER:
00554 result = svq1_motion_inter_block (s, bitbuf, current, previous, pitch, motion, x, y);
00555
00556 if (result != 0)
00557 {
00558 #ifdef DEBUG_SVQ1
00559 av_log(s->avctx, AV_LOG_INFO, "Error in svq1_motion_inter_block %i\n",result);
00560 #endif
00561 break;
00562 }
00563 result = svq1_decode_block_non_intra (bitbuf, current, pitch);
00564 break;
00565
00566 case SVQ1_BLOCK_INTER_4V:
00567 result = svq1_motion_inter_4v_block (s, bitbuf, current, previous, pitch, motion, x, y);
00568
00569 if (result != 0)
00570 {
00571 #ifdef DEBUG_SVQ1
00572 av_log(s->avctx, AV_LOG_INFO, "Error in svq1_motion_inter_4v_block %i\n",result);
00573 #endif
00574 break;
00575 }
00576 result = svq1_decode_block_non_intra (bitbuf, current, pitch);
00577 break;
00578
00579 case SVQ1_BLOCK_INTRA:
00580 result = svq1_decode_block_intra (bitbuf, current, pitch);
00581 break;
00582 }
00583
00584 return result;
00585 }
00586
00587
00588 static struct { int width; int height; } svq1_frame_size_table[8] = {
00589 { 160, 120 }, { 128, 96 }, { 176, 144 }, { 352, 288 },
00590 { 704, 576 }, { 240, 180 }, { 320, 240 }, { -1, -1 }
00591 };
00592
00593 static uint16_t svq1_packet_checksum (uint8_t *data, int length, int value) {
00594 int i;
00595
00596 for (i=0; i < length; i++) {
00597 value = checksum_table[data[i] ^ (value >> 8)] ^ ((value & 0xFF) << 8);
00598 }
00599
00600 return value;
00601 }
00602
00603 #if 0
00604 static uint16_t svq1_component_checksum (uint16_t *pixels, int pitch,
00605 int width, int height, int value) {
00606 int x, y;
00607
00608 for (y=0; y < height; y++) {
00609 for (x=0; x < width; x++) {
00610 value = checksum_table[pixels[x] ^ (value >> 8)] ^ ((value & 0xFF) << 8);
00611 }
00612
00613 pixels += pitch;
00614 }
00615
00616 return value;
00617 }
00618 #endif
00619
00620 static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) {
00621 uint8_t seed;
00622 int i;
00623
00624 out[0] = get_bits (bitbuf, 8);
00625
00626 seed = string_table[out[0]];
00627
00628 for (i=1; i <= out[0]; i++) {
00629 out[i] = get_bits (bitbuf, 8) ^ seed;
00630 seed = string_table[out[i] ^ seed];
00631 }
00632 }
00633
00634 static int svq1_decode_frame_header (GetBitContext *bitbuf,MpegEncContext *s) {
00635 int frame_size_code;
00636 int temporal_reference;
00637
00638 temporal_reference = get_bits (bitbuf, 8);
00639
00640
00641 s->pict_type= get_bits (bitbuf, 2)+1;
00642 if(s->pict_type==4)
00643 return -1;
00644
00645 if (s->pict_type == I_TYPE) {
00646
00647
00648 if (s->f_code == 0x50 || s->f_code == 0x60) {
00649 int csum = get_bits (bitbuf, 16);
00650
00651 csum = svq1_packet_checksum ((uint8_t *)bitbuf->buffer, bitbuf->size_in_bits>>3, csum);
00652
00653
00654
00655 }
00656
00657 if ((s->f_code ^ 0x10) >= 0x50) {
00658 char msg[256];
00659
00660 svq1_parse_string (bitbuf, (char *) msg);
00661
00662 av_log(s->avctx, AV_LOG_INFO, "embedded message: \"%s\"\n", (char *) msg);
00663 }
00664
00665 skip_bits (bitbuf, 2);
00666 skip_bits (bitbuf, 2);
00667 skip_bits1 (bitbuf);
00668
00669
00670 frame_size_code = get_bits (bitbuf, 3);
00671
00672 if (frame_size_code == 7) {
00673
00674 s->width = get_bits (bitbuf, 12);
00675 s->height = get_bits (bitbuf, 12);
00676
00677 if (!s->width || !s->height)
00678 return -1;
00679 } else {
00680
00681 s->width = svq1_frame_size_table[frame_size_code].width;
00682 s->height = svq1_frame_size_table[frame_size_code].height;
00683 }
00684 }
00685
00686
00687 if (get_bits (bitbuf, 1) == 1) {
00688 skip_bits1 (bitbuf);
00689 skip_bits1 (bitbuf);
00690
00691 if (get_bits (bitbuf, 2) != 0)
00692 return -1;
00693 }
00694
00695 if (get_bits (bitbuf, 1) == 1) {
00696 skip_bits1 (bitbuf);
00697 skip_bits (bitbuf, 4);
00698 skip_bits1 (bitbuf);
00699 skip_bits (bitbuf, 2);
00700
00701 while (get_bits (bitbuf, 1) == 1) {
00702 skip_bits (bitbuf, 8);
00703 }
00704 }
00705
00706 return 0;
00707 }
00708
00709 static int svq1_decode_frame(AVCodecContext *avctx,
00710 void *data, int *data_size,
00711 uint8_t *buf, int buf_size)
00712 {
00713 MpegEncContext *s=avctx->priv_data;
00714 uint8_t *current, *previous;
00715 int result, i, x, y, width, height;
00716 AVFrame *pict = data;
00717
00718
00719 init_get_bits(&s->gb,buf,buf_size*8);
00720
00721
00722 s->f_code = get_bits (&s->gb, 22);
00723
00724 if ((s->f_code & ~0x70) || !(s->f_code & 0x60))
00725 return -1;
00726
00727
00728 if (s->f_code != 0x20) {
00729 uint32_t *src = (uint32_t *) (buf + 4);
00730
00731 for (i=0; i < 4; i++) {
00732 src[i] = ((src[i] << 16) | (src[i] >> 16)) ^ src[7 - i];
00733 }
00734 }
00735
00736 result = svq1_decode_frame_header (&s->gb, s);
00737
00738 if (result != 0)
00739 {
00740 #ifdef DEBUG_SVQ1
00741 av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_frame_header %i\n",result);
00742 #endif
00743 return result;
00744 }
00745
00746
00747
00748 if(s->pict_type==B_TYPE && s->last_picture_ptr==NULL) return buf_size;
00749
00750 if(avctx->hurry_up && s->pict_type==B_TYPE) return buf_size;
00751 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
00752 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
00753 || avctx->skip_frame >= AVDISCARD_ALL)
00754 return buf_size;
00755
00756 if(MPV_frame_start(s, avctx) < 0)
00757 return -1;
00758
00759
00760 for (i=0; i < 3; i++) {
00761 int linesize;
00762 if (i == 0) {
00763 width = (s->width+15)&~15;
00764 height = (s->height+15)&~15;
00765 linesize= s->linesize;
00766 } else {
00767 if(s->flags&CODEC_FLAG_GRAY) break;
00768 width = (s->width/4+15)&~15;
00769 height = (s->height/4+15)&~15;
00770 linesize= s->uvlinesize;
00771 }
00772
00773 current = s->current_picture.data[i];
00774
00775 if(s->pict_type==B_TYPE){
00776 previous = s->next_picture.data[i];
00777 }else{
00778 previous = s->last_picture.data[i];
00779 }
00780
00781 if (s->pict_type == I_TYPE) {
00782
00783 for (y=0; y < height; y+=16) {
00784 for (x=0; x < width; x+=16) {
00785 result = svq1_decode_block_intra (&s->gb, ¤t[x], linesize);
00786 if (result != 0)
00787 {
00788
00789 av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_block %i (keyframe)\n",result);
00790
00791 return result;
00792 }
00793 }
00794 current += 16*linesize;
00795 }
00796 } else {
00797 svq1_pmv_t pmv[width/8+3];
00798
00799 memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv_t));
00800
00801 for (y=0; y < height; y+=16) {
00802 for (x=0; x < width; x+=16) {
00803 result = svq1_decode_delta_block (s, &s->gb, ¤t[x], previous,
00804 linesize, pmv, x, y);
00805 if (result != 0)
00806 {
00807 #ifdef DEBUG_SVQ1
00808 av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_delta_block %i\n",result);
00809 #endif
00810 return result;
00811 }
00812 }
00813
00814 pmv[0].x =
00815 pmv[0].y = 0;
00816
00817 current += 16*linesize;
00818 }
00819 }
00820 }
00821
00822 *pict = *(AVFrame*)&s->current_picture;
00823
00824
00825 MPV_frame_end(s);
00826
00827 *data_size=sizeof(AVFrame);
00828 return buf_size;
00829 }
00830
00831 static int svq1_decode_init(AVCodecContext *avctx)
00832 {
00833 MpegEncContext *s = avctx->priv_data;
00834 int i;
00835
00836 MPV_decode_defaults(s);
00837
00838 s->avctx = avctx;
00839 s->width = (avctx->width+3)&~3;
00840 s->height = (avctx->height+3)&~3;
00841 s->codec_id= avctx->codec->id;
00842 avctx->pix_fmt = PIX_FMT_YUV410P;
00843 avctx->has_b_frames= 1;
00844 s->flags= avctx->flags;
00845 if (MPV_common_init(s) < 0) return -1;
00846
00847 init_vlc(&svq1_block_type, 2, 4,
00848 &svq1_block_type_vlc[0][1], 2, 1,
00849 &svq1_block_type_vlc[0][0], 2, 1, 1);
00850
00851 init_vlc(&svq1_motion_component, 7, 33,
00852 &mvtab[0][1], 2, 1,
00853 &mvtab[0][0], 2, 1, 1);
00854
00855 for (i = 0; i < 6; i++) {
00856 init_vlc(&svq1_intra_multistage[i], 3, 8,
00857 &svq1_intra_multistage_vlc[i][0][1], 2, 1,
00858 &svq1_intra_multistage_vlc[i][0][0], 2, 1, 1);
00859 init_vlc(&svq1_inter_multistage[i], 3, 8,
00860 &svq1_inter_multistage_vlc[i][0][1], 2, 1,
00861 &svq1_inter_multistage_vlc[i][0][0], 2, 1, 1);
00862 }
00863
00864 init_vlc(&svq1_intra_mean, 8, 256,
00865 &svq1_intra_mean_vlc[0][1], 4, 2,
00866 &svq1_intra_mean_vlc[0][0], 4, 2, 1);
00867
00868 init_vlc(&svq1_inter_mean, 9, 512,
00869 &svq1_inter_mean_vlc[0][1], 4, 2,
00870 &svq1_inter_mean_vlc[0][0], 4, 2, 1);
00871
00872 return 0;
00873 }
00874
00875 static int svq1_decode_end(AVCodecContext *avctx)
00876 {
00877 MpegEncContext *s = avctx->priv_data;
00878
00879 MPV_common_end(s);
00880 return 0;
00881 }
00882
00883 static void svq1_write_header(SVQ1Context *s, int frame_type)
00884 {
00885 int i;
00886
00887
00888 put_bits(&s->pb, 22, 0x20);
00889
00890
00891 put_bits(&s->pb, 8, 0x00);
00892
00893
00894 put_bits(&s->pb, 2, frame_type - 1);
00895
00896 if (frame_type == I_TYPE) {
00897
00898
00899
00900
00901
00902
00903 put_bits(&s->pb, 5, 0);
00904
00905 for (i = 0; i < 7; i++)
00906 {
00907 if ((svq1_frame_size_table[i].width == s->frame_width) &&
00908 (svq1_frame_size_table[i].height == s->frame_height))
00909 {
00910 put_bits(&s->pb, 3, i);
00911 break;
00912 }
00913 }
00914
00915 if (i == 7)
00916 {
00917 put_bits(&s->pb, 3, 7);
00918 put_bits(&s->pb, 12, s->frame_width);
00919 put_bits(&s->pb, 12, s->frame_height);
00920 }
00921 }
00922
00923
00924 put_bits(&s->pb, 2, 0);
00925 }
00926
00927
00928 #define QUALITY_THRESHOLD 100
00929 #define THRESHOLD_MULTIPLIER 0.6
00930
00931 #if defined(HAVE_ALTIVEC)
00932 #undef vector
00933 #endif
00934
00935 static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra){
00936 int count, y, x, i, j, split, best_mean, best_score, best_count;
00937 int best_vector[6];
00938 int block_sum[7]= {0, 0, 0, 0, 0, 0};
00939 int w= 2<<((level+2)>>1);
00940 int h= 2<<((level+1)>>1);
00941 int size=w*h;
00942 int16_t block[7][256];
00943 const int8_t *codebook_sum, *codebook;
00944 const uint16_t (*mean_vlc)[2];
00945 const uint8_t (*multistage_vlc)[2];
00946
00947 best_score=0;
00948
00949 if(intra){
00950 codebook_sum= svq1_intra_codebook_sum[level];
00951 codebook= svq1_intra_codebooks[level];
00952 mean_vlc= svq1_intra_mean_vlc;
00953 multistage_vlc= svq1_intra_multistage_vlc[level];
00954 for(y=0; y<h; y++){
00955 for(x=0; x<w; x++){
00956 int v= src[x + y*stride];
00957 block[0][x + w*y]= v;
00958 best_score += v*v;
00959 block_sum[0] += v;
00960 }
00961 }
00962 }else{
00963 codebook_sum= svq1_inter_codebook_sum[level];
00964 codebook= svq1_inter_codebooks[level];
00965 mean_vlc= svq1_inter_mean_vlc + 256;
00966 multistage_vlc= svq1_inter_multistage_vlc[level];
00967 for(y=0; y<h; y++){
00968 for(x=0; x<w; x++){
00969 int v= src[x + y*stride] - ref[x + y*stride];
00970 block[0][x + w*y]= v;
00971 best_score += v*v;
00972 block_sum[0] += v;
00973 }
00974 }
00975 }
00976
00977 best_count=0;
00978 best_score -= ((block_sum[0]*block_sum[0])>>(level+3));
00979 best_mean= (block_sum[0] + (size>>1)) >> (level+3);
00980
00981 if(level<4){
00982 for(count=1; count<7; count++){
00983 int best_vector_score= INT_MAX;
00984 int best_vector_sum=-999, best_vector_mean=-999;
00985 const int stage= count-1;
00986 const int8_t *vector;
00987
00988 for(i=0; i<16; i++){
00989 int sum= codebook_sum[stage*16 + i];
00990 int sqr=0;
00991 int diff, mean, score;
00992
00993 vector = codebook + stage*size*16 + i*size;
00994
00995 for(j=0; j<size; j++){
00996 int v= vector[j];
00997 sqr += (v - block[stage][j])*(v - block[stage][j]);
00998 }
00999 diff= block_sum[stage] - sum;
01000 mean= (diff + (size>>1)) >> (level+3);
01001 assert(mean >-300 && mean<300);
01002 if(intra) mean= clip(mean, 0, 255);
01003 else mean= clip(mean, -256, 255);
01004 score= sqr - ((diff*(int64_t)diff)>>(level+3));
01005 if(score < best_vector_score){
01006 best_vector_score= score;
01007 best_vector[stage]= i;
01008 best_vector_sum= sum;
01009 best_vector_mean= mean;
01010 }
01011 }
01012 assert(best_vector_mean != -999);
01013 vector= codebook + stage*size*16 + best_vector[stage]*size;
01014 for(j=0; j<size; j++){
01015 block[stage+1][j] = block[stage][j] - vector[j];
01016 }
01017 block_sum[stage+1]= block_sum[stage] - best_vector_sum;
01018 best_vector_score +=
01019 lambda*(+ 1 + 4*count
01020 + multistage_vlc[1+count][1]
01021 + mean_vlc[best_vector_mean][1]);
01022
01023 if(best_vector_score < best_score){
01024 best_score= best_vector_score;
01025 best_count= count;
01026 best_mean= best_vector_mean;
01027 }
01028 }
01029 }
01030
01031 split=0;
01032 if(best_score > threshold && level){
01033 int score=0;
01034 int offset= (level&1) ? stride*h/2 : w/2;
01035 PutBitContext backup[6];
01036
01037 for(i=level-1; i>=0; i--){
01038 backup[i]= s->reorder_pb[i];
01039 }
01040 score += encode_block(s, src , ref , decoded , stride, level-1, threshold>>1, lambda, intra);
01041 score += encode_block(s, src + offset, ref + offset, decoded + offset, stride, level-1, threshold>>1, lambda, intra);
01042 score += lambda;
01043
01044 if(score < best_score){
01045 best_score= score;
01046 split=1;
01047 }else{
01048 for(i=level-1; i>=0; i--){
01049 s->reorder_pb[i]= backup[i];
01050 }
01051 }
01052 }
01053 if (level > 0)
01054 put_bits(&s->reorder_pb[level], 1, split);
01055
01056 if(!split){
01057 assert((best_mean >= 0 && best_mean<256) || !intra);
01058 assert(best_mean >= -256 && best_mean<256);
01059 assert(best_count >=0 && best_count<7);
01060 assert(level<4 || best_count==0);
01061
01062
01063 put_bits(&s->reorder_pb[level],
01064 multistage_vlc[1 + best_count][1],
01065 multistage_vlc[1 + best_count][0]);
01066 put_bits(&s->reorder_pb[level], mean_vlc[best_mean][1],
01067 mean_vlc[best_mean][0]);
01068
01069 for (i = 0; i < best_count; i++){
01070 assert(best_vector[i]>=0 && best_vector[i]<16);
01071 put_bits(&s->reorder_pb[level], 4, best_vector[i]);
01072 }
01073
01074 for(y=0; y<h; y++){
01075 for(x=0; x<w; x++){
01076 decoded[x + y*stride]= src[x + y*stride] - block[best_count][x + w*y] + best_mean;
01077 }
01078 }
01079 }
01080
01081 return best_score;
01082 }
01083
01084 #ifdef CONFIG_ENCODERS
01085
01086 static int svq1_encode_plane(SVQ1Context *s, int plane, unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane,
01087 int width, int height, int src_stride, int stride)
01088 {
01089 int x, y;
01090 int i;
01091 int block_width, block_height;
01092 int level;
01093 int threshold[6];
01094 const int lambda= (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT);
01095
01096
01097 threshold[5] = QUALITY_THRESHOLD;
01098 for (level = 4; level >= 0; level--)
01099 threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
01100
01101 block_width = (width + 15) / 16;
01102 block_height = (height + 15) / 16;
01103
01104 if(s->picture.pict_type == P_TYPE){
01105 s->m.avctx= s->avctx;
01106 s->m.current_picture_ptr= &s->m.current_picture;
01107 s->m.last_picture_ptr = &s->m.last_picture;
01108 s->m.last_picture.data[0]= ref_plane;
01109 s->m.linesize=
01110 s->m.last_picture.linesize[0]=
01111 s->m.new_picture.linesize[0]=
01112 s->m.current_picture.linesize[0]= stride;
01113 s->m.width= width;
01114 s->m.height= height;
01115 s->m.mb_width= block_width;
01116 s->m.mb_height= block_height;
01117 s->m.mb_stride= s->m.mb_width+1;
01118 s->m.b8_stride= 2*s->m.mb_width+1;
01119 s->m.f_code=1;
01120 s->m.pict_type= s->picture.pict_type;
01121 s->m.qscale= s->picture.quality/FF_QP2LAMBDA;
01122 s->m.me_method= s->avctx->me_method;
01123
01124 if(!s->motion_val8[plane]){
01125 s->motion_val8 [plane]= av_mallocz((s->m.b8_stride*block_height*2 + 2)*2*sizeof(int16_t));
01126 s->motion_val16[plane]= av_mallocz((s->m.mb_stride*(block_height + 2) + 1)*2*sizeof(int16_t));
01127 }
01128
01129 s->m.mb_type= s->mb_type;
01130
01131
01132 s->m.current_picture.mb_mean= (uint8_t *)s->dummy;
01133 s->m.current_picture.mb_var= (uint16_t*)s->dummy;
01134 s->m.current_picture.mc_mb_var= (uint16_t*)s->dummy;
01135 s->m.current_picture.mb_type= s->dummy;
01136
01137 s->m.current_picture.motion_val[0]= s->motion_val8[plane] + 2;
01138 s->m.p_mv_table= s->motion_val16[plane] + s->m.mb_stride + 1;
01139 s->m.dsp= s->dsp;
01140 ff_init_me(&s->m);
01141
01142 s->m.me.dia_size= s->avctx->dia_size;
01143 s->m.first_slice_line=1;
01144 for (y = 0; y < block_height; y++) {
01145 uint8_t src[stride*16];
01146
01147 s->m.new_picture.data[0]= src - y*16*stride;
01148 s->m.mb_y= y;
01149
01150 for(i=0; i<16 && i + 16*y<height; i++){
01151 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
01152 for(x=width; x<16*block_width; x++)
01153 src[i*stride+x]= src[i*stride+x-1];
01154 }
01155 for(; i<16 && i + 16*y<16*block_height; i++)
01156 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
01157
01158 for (x = 0; x < block_width; x++) {
01159 s->m.mb_x= x;
01160 ff_init_block_index(&s->m);
01161 ff_update_block_index(&s->m);
01162
01163 ff_estimate_p_frame_motion(&s->m, x, y);
01164 }
01165 s->m.first_slice_line=0;
01166 }
01167
01168 ff_fix_long_p_mvs(&s->m);
01169 ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code, CANDIDATE_MB_TYPE_INTER, 0);
01170 }
01171
01172 s->m.first_slice_line=1;
01173 for (y = 0; y < block_height; y++) {
01174 uint8_t src[stride*16];
01175
01176 for(i=0; i<16 && i + 16*y<height; i++){
01177 memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
01178 for(x=width; x<16*block_width; x++)
01179 src[i*stride+x]= src[i*stride+x-1];
01180 }
01181 for(; i<16 && i + 16*y<16*block_height; i++)
01182 memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
01183
01184 s->m.mb_y= y;
01185 for (x = 0; x < block_width; x++) {
01186 uint8_t reorder_buffer[3][6][7*32];
01187 int count[3][6];
01188 int offset = y * 16 * stride + x * 16;
01189 uint8_t *decoded= decoded_plane + offset;
01190 uint8_t *ref= ref_plane + offset;
01191 int score[4]={0,0,0,0}, best;
01192 uint8_t temp[16*stride];
01193
01194 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3000){
01195 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
01196 return -1;
01197 }
01198
01199 s->m.mb_x= x;
01200 ff_init_block_index(&s->m);
01201 ff_update_block_index(&s->m);
01202
01203 if(s->picture.pict_type == I_TYPE || (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTRA)){
01204 for(i=0; i<6; i++){
01205 init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i], 7*32);
01206 }
01207 if(s->picture.pict_type == P_TYPE){
01208 const uint8_t *vlc= svq1_block_type_vlc[SVQ1_BLOCK_INTRA];
01209 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
01210 score[0]= vlc[1]*lambda;
01211 }
01212 score[0]+= encode_block(s, src+16*x, NULL, temp, stride, 5, 64, lambda, 1);
01213 for(i=0; i<6; i++){
01214 count[0][i]= put_bits_count(&s->reorder_pb[i]);
01215 flush_put_bits(&s->reorder_pb[i]);
01216 }
01217 }else
01218 score[0]= INT_MAX;
01219
01220 best=0;
01221
01222 if(s->picture.pict_type == P_TYPE){
01223 const uint8_t *vlc= svq1_block_type_vlc[SVQ1_BLOCK_INTER];
01224 int mx, my, pred_x, pred_y, dxy;
01225 int16_t *motion_ptr;
01226
01227 motion_ptr= h263_pred_motion(&s->m, 0, 0, &pred_x, &pred_y);
01228 if(s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER){
01229 for(i=0; i<6; i++)
01230 init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i], 7*32);
01231
01232 put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
01233
01234 s->m.pb= s->reorder_pb[5];
01235 mx= motion_ptr[0];
01236 my= motion_ptr[1];
01237 assert(mx>=-32 && mx<=31);
01238 assert(my>=-32 && my<=31);
01239 assert(pred_x>=-32 && pred_x<=31);
01240 assert(pred_y>=-32 && pred_y<=31);
01241 ff_h263_encode_motion(&s->m, mx - pred_x, 1);
01242 ff_h263_encode_motion(&s->m, my - pred_y, 1);
01243 s->reorder_pb[5]= s->m.pb;
01244 score[1] += lambda*put_bits_count(&s->reorder_pb[5]);
01245
01246 dxy= (mx&1) + 2*(my&1);
01247
01248 s->dsp.put_pixels_tab[0][dxy](temp+16, ref + (mx>>1) + stride*(my>>1), stride, 16);
01249
01250 score[1]+= encode_block(s, src+16*x, temp+16, decoded, stride, 5, 64, lambda, 0);
01251 best= score[1] <= score[0];
01252
01253 vlc= svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
01254 score[2]= s->dsp.sse[0](NULL, src+16*x, ref, stride, 16);
01255 score[2]+= vlc[1]*lambda;
01256 if(score[2] < score[best] && mx==0 && my==0){
01257 best=2;
01258 s->dsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
01259 for(i=0; i<6; i++){
01260 count[2][i]=0;
01261 }
01262 put_bits(&s->pb, vlc[1], vlc[0]);
01263 }
01264 }
01265
01266 if(best==1){
01267 for(i=0; i<6; i++){
01268 count[1][i]= put_bits_count(&s->reorder_pb[i]);
01269 flush_put_bits(&s->reorder_pb[i]);
01270 }
01271 }else{
01272 motion_ptr[0 ] = motion_ptr[1 ]=
01273 motion_ptr[2 ] = motion_ptr[3 ]=
01274 motion_ptr[0+2*s->m.b8_stride] = motion_ptr[1+2*s->m.b8_stride]=
01275 motion_ptr[2+2*s->m.b8_stride] = motion_ptr[3+2*s->m.b8_stride]=0;
01276 }
01277 }
01278
01279 s->rd_total += score[best];
01280
01281 for(i=5; i>=0; i--){
01282 ff_copy_bits(&s->pb, reorder_buffer[best][i], count[best][i]);
01283 }
01284 if(best==0){
01285 s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
01286 }
01287 }
01288 s->m.first_slice_line=0;
01289 }
01290 return 0;
01291 }
01292
01293 static int svq1_encode_init(AVCodecContext *avctx)
01294 {
01295 SVQ1Context * const s = avctx->priv_data;
01296
01297 dsputil_init(&s->dsp, avctx);
01298 avctx->coded_frame= (AVFrame*)&s->picture;
01299
01300 s->frame_width = avctx->width;
01301 s->frame_height = avctx->height;
01302
01303 s->y_block_width = (s->frame_width + 15) / 16;
01304 s->y_block_height = (s->frame_height + 15) / 16;
01305
01306 s->c_block_width = (s->frame_width / 4 + 15) / 16;
01307 s->c_block_height = (s->frame_height / 4 + 15) / 16;
01308
01309 s->avctx= avctx;
01310 s->m.avctx= avctx;
01311 s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
01312 s->m.me.map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
01313 s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
01314 s->mb_type = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int16_t));
01315 s->dummy = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int32_t));
01316 h263_encode_init(&s->m);
01317
01318 return 0;
01319 }
01320
01321 static int svq1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
01322 int buf_size, void *data)
01323 {
01324 SVQ1Context * const s = avctx->priv_data;
01325 AVFrame *pict = data;
01326 AVFrame * const p= (AVFrame*)&s->picture;
01327 AVFrame temp;
01328 int i;
01329
01330 if(avctx->pix_fmt != PIX_FMT_YUV410P){
01331 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
01332 return -1;
01333 }
01334
01335 if(!s->current_picture.data[0]){
01336 avctx->get_buffer(avctx, &s->current_picture);
01337 avctx->get_buffer(avctx, &s->last_picture);
01338 }
01339
01340 temp= s->current_picture;
01341 s->current_picture= s->last_picture;
01342 s->last_picture= temp;
01343
01344 init_put_bits(&s->pb, buf, buf_size);
01345
01346 *p = *pict;
01347 p->pict_type = avctx->frame_number % avctx->gop_size ? P_TYPE : I_TYPE;
01348 p->key_frame = p->pict_type == I_TYPE;
01349
01350 svq1_write_header(s, p->pict_type);
01351 for(i=0; i<3; i++){
01352 if(svq1_encode_plane(s, i,
01353 s->picture.data[i], s->last_picture.data[i], s->current_picture.data[i],
01354 s->frame_width / (i?4:1), s->frame_height / (i?4:1),
01355 s->picture.linesize[i], s->current_picture.linesize[i]) < 0)
01356 return -1;
01357 }
01358
01359
01360 while(put_bits_count(&s->pb) & 31)
01361 put_bits(&s->pb, 1, 0);
01362
01363 flush_put_bits(&s->pb);
01364
01365 return (put_bits_count(&s->pb) / 8);
01366 }
01367
01368 static int svq1_encode_end(AVCodecContext *avctx)
01369 {
01370 SVQ1Context * const s = avctx->priv_data;
01371 int i;
01372
01373 av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", s->rd_total/(double)(avctx->width*avctx->height*avctx->frame_number));
01374
01375 av_freep(&s->m.me.scratchpad);
01376 av_freep(&s->m.me.map);
01377 av_freep(&s->m.me.score_map);
01378 av_freep(&s->mb_type);
01379 av_freep(&s->dummy);
01380
01381 for(i=0; i<3; i++){
01382 av_freep(&s->motion_val8[i]);
01383 av_freep(&s->motion_val16[i]);
01384 }
01385
01386 return 0;
01387 }
01388
01389 #endif //CONFIG_ENCODERS
01390
01391 AVCodec svq1_decoder = {
01392 "svq1",
01393 CODEC_TYPE_VIDEO,
01394 CODEC_ID_SVQ1,
01395 sizeof(MpegEncContext),
01396 svq1_decode_init,
01397 NULL,
01398 svq1_decode_end,
01399 svq1_decode_frame,
01400 CODEC_CAP_DR1,
01401 .flush= ff_mpeg_flush,
01402 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV410P, -1},
01403 };
01404
01405 #ifdef CONFIG_ENCODERS
01406
01407 AVCodec svq1_encoder = {
01408 "svq1",
01409 CODEC_TYPE_VIDEO,
01410 CODEC_ID_SVQ1,
01411 sizeof(SVQ1Context),
01412 svq1_encode_init,
01413 svq1_encode_frame,
01414 svq1_encode_end,
01415 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV410P, -1},
01416 };
01417
01418 #endif //CONFIG_ENCODERS