~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/svq1.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * 
 
2
 *
3
3
 * Copyright (C) 2002 the xine project
4
4
 * Copyright (C) 2002 the ffmpeg project
5
 
 * 
6
 
 * This library is free software; you can redistribute it and/or
 
5
 *
 
6
 * This file is part of FFmpeg.
 
7
 *
 
8
 * FFmpeg is free software; you can redistribute it and/or
7
9
 * modify it under the terms of the GNU Lesser General Public
8
10
 * License as published by the Free Software Foundation; either
9
 
 * version 2 of the License, or (at your option) any later version.
 
11
 * version 2.1 of the License, or (at your option) any later version.
10
12
 *
11
 
 * This library is distributed in the hope that it will be useful,
 
13
 * FFmpeg is distributed in the hope that it will be useful,
12
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
16
 * Lesser General Public License for more details.
15
17
 *
16
18
 * You should have received a copy of the GNU Lesser General Public
17
 
 * License along with this library; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 * License along with FFmpeg; if not, write to the Free Software
 
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
21
 *
 
22
 * (SVQ1 Decoder)
20
23
 * Ported to mplayer by Arpi <arpi@thot.banki.hu>
21
24
 * Ported to libavcodec by Nick Kurshev <nickols_k@mail.ru>
22
25
 *
 
26
 * SVQ1 Encoder (c) 2004 Mike Melanson <melanson@pcisys.net>
23
27
 */
24
28
 
25
29
/**
26
30
 * @file svq1.c
27
 
 * Sorenson Vector Quantizer #1 (SVQ1) video decoder.
 
31
 * Sorenson Vector Quantizer #1 (SVQ1) video codec.
 
32
 * For more information of the SVQ1 algorithm, visit:
 
33
 *   http://www.pcisys.net/~melanson/codecs/
28
34
 */
29
35
 
30
36
 
33
39
#include <stdlib.h>
34
40
#include <string.h>
35
41
#include <unistd.h>
 
42
#include <limits.h>
36
43
 
37
44
#include "common.h"
38
45
#include "avcodec.h"
40
47
#include "mpegvideo.h"
41
48
#include "bswap.h"
42
49
 
 
50
#undef NDEBUG
 
51
#include <assert.h>
 
52
 
 
53
extern const uint8_t mvtab[33][2];
 
54
 
43
55
static VLC svq1_block_type;
44
56
static VLC svq1_motion_component;
45
57
static VLC svq1_intra_multistage[6];
47
59
static VLC svq1_intra_mean;
48
60
static VLC svq1_inter_mean;
49
61
 
50
 
#define MEDIAN(a,b,c)   (((a < b) != (b >= c)) ? b : (((a < c) != (c > b)) ? c : a))
51
 
 
52
 
#define SVQ1_BLOCK_SKIP         0
53
 
#define SVQ1_BLOCK_INTER        1
54
 
#define SVQ1_BLOCK_INTER_4V     2
55
 
#define SVQ1_BLOCK_INTRA        3
 
62
#define SVQ1_BLOCK_SKIP         0
 
63
#define SVQ1_BLOCK_INTER        1
 
64
#define SVQ1_BLOCK_INTER_4V     2
 
65
#define SVQ1_BLOCK_INTRA        3
 
66
 
 
67
typedef struct SVQ1Context {
 
68
    MpegEncContext m; // needed for motion estimation, should not be used for anything else, the idea is to make the motion estimation eventually independent of MpegEncContext, so this will be removed then (FIXME/XXX)
 
69
    AVCodecContext *avctx;
 
70
    DSPContext dsp;
 
71
    AVFrame picture;
 
72
    AVFrame current_picture;
 
73
    AVFrame last_picture;
 
74
    PutBitContext pb;
 
75
    GetBitContext gb;
 
76
 
 
77
    PutBitContext reorder_pb[6]; //why ooh why this sick breadth first order, everything is slower and more complex
 
78
 
 
79
    int frame_width;
 
80
    int frame_height;
 
81
 
 
82
    /* Y plane block dimensions */
 
83
    int y_block_width;
 
84
    int y_block_height;
 
85
 
 
86
    /* U & V plane (C planes) block dimensions */
 
87
    int c_block_width;
 
88
    int c_block_height;
 
89
 
 
90
    uint16_t *mb_type;
 
91
    uint32_t *dummy;
 
92
    int16_t (*motion_val8[3])[2];
 
93
    int16_t (*motion_val16[3])[2];
 
94
 
 
95
    int64_t rd_total;
 
96
} SVQ1Context;
56
97
 
57
98
/* motion vector (prediction) */
58
99
typedef struct svq1_pmv_s {
59
 
  int            x;
60
 
  int            y;
 
100
  int           x;
 
101
  int           y;
61
102
} svq1_pmv_t;
62
103
 
63
104
#include "svq1_cb.h"
137
178
    for (; level > 0; i++) {\
138
179
      /* process next depth */\
139
180
      if (i == m) {\
140
 
        m = n;\
141
 
        if (--level == 0)\
142
 
          break;\
 
181
        m = n;\
 
182
        if (--level == 0)\
 
183
          break;\
143
184
      }\
144
185
      /* divide block if next bit set */\
145
186
      if (get_bits (bitbuf, 1) == 0)\
146
 
        break;\
 
187
        break;\
147
188
      /* add child nodes */\
148
189
      list[n++] = list[i];\
149
190
      list[n++] = list[i] + (((level & 1) ? pitch : 1) << ((level / 2) + 1));\
150
191
    }
151
192
 
152
193
#define SVQ1_ADD_CODEBOOK()\
153
 
          /* add codebook entries to vector */\
154
 
          for (j=0; j < stages; j++) {\
155
 
            n3  = codebook[entries[j]] ^ 0x80808080;\
156
 
            n1 += ((n3 & 0xFF00FF00) >> 8);\
157
 
            n2 +=  (n3 & 0x00FF00FF);\
158
 
          }\
159
 
\
160
 
          /* clip to [0..255] */\
161
 
          if (n1 & 0xFF00FF00) {\
162
 
            n3  = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
163
 
            n1 += 0x7F007F00;\
164
 
            n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
165
 
            n1 &= (n3 & 0x00FF00FF);\
166
 
          }\
167
 
\
168
 
          if (n2 & 0xFF00FF00) {\
169
 
            n3  = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
170
 
            n2 += 0x7F007F00;\
171
 
            n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
172
 
            n2 &= (n3 & 0x00FF00FF);\
173
 
          }
 
194
          /* add codebook entries to vector */\
 
195
          for (j=0; j < stages; j++) {\
 
196
            n3  = codebook[entries[j]] ^ 0x80808080;\
 
197
            n1 += ((n3 & 0xFF00FF00) >> 8);\
 
198
            n2 +=  (n3 & 0x00FF00FF);\
 
199
          }\
 
200
\
 
201
          /* clip to [0..255] */\
 
202
          if (n1 & 0xFF00FF00) {\
 
203
            n3  = ((( n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
 
204
            n1 += 0x7F007F00;\
 
205
            n1 |= (((~n1 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
 
206
            n1 &= (n3 & 0x00FF00FF);\
 
207
          }\
 
208
\
 
209
          if (n2 & 0xFF00FF00) {\
 
210
            n3  = ((( n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
 
211
            n2 += 0x7F007F00;\
 
212
            n2 |= (((~n2 >> 15) & 0x00010001) | 0x01000100) - 0x00010001;\
 
213
            n2 &= (n3 & 0x00FF00FF);\
 
214
          }
174
215
 
175
216
#define SVQ1_DO_CODEBOOK_INTRA()\
176
217
      for (y=0; y < height; y++) {\
177
 
        for (x=0; x < (width / 4); x++, codebook++) {\
178
 
        n1 = n4;\
179
 
        n2 = n4;\
180
 
        SVQ1_ADD_CODEBOOK()\
181
 
        /* store result */\
182
 
        dst[x] = (n1 << 8) | n2;\
183
 
        }\
184
 
        dst += (pitch / 4);\
 
218
        for (x=0; x < (width / 4); x++, codebook++) {\
 
219
        n1 = n4;\
 
220
        n2 = n4;\
 
221
        SVQ1_ADD_CODEBOOK()\
 
222
        /* store result */\
 
223
        dst[x] = (n1 << 8) | n2;\
 
224
        }\
 
225
        dst += (pitch / 4);\
185
226
      }
186
227
 
187
228
#define SVQ1_DO_CODEBOOK_NONINTRA()\
188
229
      for (y=0; y < height; y++) {\
189
 
        for (x=0; x < (width / 4); x++, codebook++) {\
190
 
        n3 = dst[x];\
191
 
        /* add mean value to vector */\
192
 
        n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
193
 
        n2 =  (n3 & 0x00FF00FF)   + n4;\
194
 
        SVQ1_ADD_CODEBOOK()\
195
 
        /* store result */\
196
 
        dst[x] = (n1 << 8) | n2;\
197
 
        }\
198
 
        dst += (pitch / 4);\
 
230
        for (x=0; x < (width / 4); x++, codebook++) {\
 
231
        n3 = dst[x];\
 
232
        /* add mean value to vector */\
 
233
        n1 = ((n3 & 0xFF00FF00) >> 8) + n4;\
 
234
        n2 =  (n3 & 0x00FF00FF)          + n4;\
 
235
        SVQ1_ADD_CODEBOOK()\
 
236
        /* store result */\
 
237
        dst[x] = (n1 << 8) | n2;\
 
238
        }\
 
239
        dst += (pitch / 4);\
199
240
      }
200
241
 
201
242
#define SVQ1_CALC_CODEBOOK_ENTRIES(cbook)\
203
244
      bit_cache = get_bits (bitbuf, 4*stages);\
204
245
      /* calculate codebook entries for this vector */\
205
246
      for (j=0; j < stages; j++) {\
206
 
        entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
 
247
        entries[j] = (((bit_cache >> (4*(stages - j - 1))) & 0xF) + 16*j) << (level + 1);\
207
248
      }\
208
249
      mean -= (stages * 128);\
209
250
      n4    = ((mean + (mean >> 31)) << 16) | (mean & 0xFFFF);
213
254
  uint8_t    *list[63];
214
255
  uint32_t   *dst;
215
256
  const uint32_t *codebook;
216
 
  int         entries[6];
217
 
  int         i, j, m, n;
218
 
  int         mean, stages;
 
257
  int         entries[6];
 
258
  int         i, j, m, n;
 
259
  int         mean, stages;
219
260
  unsigned    x, y, width, height, level;
220
261
  uint32_t    n1, n2, n3, n4;
221
262
 
235
276
    stages = get_vlc2(bitbuf, svq1_intra_multistage[level].table, 3, 3) - 1;
236
277
 
237
278
    if (stages == -1) {
238
 
        for (y=0; y < height; y++) {
239
 
          memset (&dst[y*(pitch / 4)], 0, width);
240
 
        }
241
 
      continue;         /* skip vector */
 
279
        for (y=0; y < height; y++) {
 
280
          memset (&dst[y*(pitch / 4)], 0, width);
 
281
        }
 
282
      continue;                 /* skip vector */
242
283
    }
243
284
 
244
285
    if ((stages > 0) && (level >= 4)) {
245
286
#ifdef DEBUG_SVQ1
246
 
    printf("Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",stages,level);
 
287
    av_log(s->avctx, AV_LOG_INFO, "Error (svq1_decode_block_intra): invalid vector: stages=%i level=%i\n",stages,level);
247
288
#endif
248
 
      return -1;        /* invalid vector */
 
289
      return -1;        /* invalid vector */
249
290
    }
250
291
 
251
292
    mean = get_vlc2(bitbuf, svq1_intra_mean.table, 8, 3);
252
293
 
253
294
    if (stages == 0) {
254
295
      for (y=0; y < height; y++) {
255
 
        memset (&dst[y*(pitch / 4)], mean, width);
 
296
        memset (&dst[y*(pitch / 4)], mean, width);
256
297
      }
257
298
    } else {
258
299
      SVQ1_CALC_CODEBOOK_ENTRIES(svq1_intra_codebooks);
268
309
  uint8_t    *list[63];
269
310
  uint32_t   *dst;
270
311
  const uint32_t *codebook;
271
 
  int         entries[6];
272
 
  int         i, j, m, n;
273
 
  int         mean, stages;
274
 
  int         x, y, width, height, level;
 
312
  int         entries[6];
 
313
  int         i, j, m, n;
 
314
  int         mean, stages;
 
315
  int         x, y, width, height, level;
275
316
  uint32_t    n1, n2, n3, n4;
276
317
 
277
318
  /* initialize list for breadth first processing of vectors */
289
330
    /* get number of stages (-1 skips vector, 0 for mean only) */
290
331
    stages = get_vlc2(bitbuf, svq1_inter_multistage[level].table, 3, 2) - 1;
291
332
 
292
 
    if (stages == -1) continue; /* skip vector */
 
333
    if (stages == -1) continue; /* skip vector */
293
334
 
294
335
    if ((stages > 0) && (level >= 4)) {
295
336
#ifdef DEBUG_SVQ1
296
 
    printf("Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",stages,level);
 
337
    av_log(s->avctx, AV_LOG_INFO, "Error (svq1_decode_block_non_intra): invalid vector: stages=%i level=%i\n",stages,level);
297
338
#endif
298
 
      return -1;        /* invalid vector */
 
339
      return -1;        /* invalid vector */
299
340
    }
300
341
 
301
342
    mean = get_vlc2(bitbuf, svq1_inter_mean.table, 9, 3) - 256;
307
348
}
308
349
 
309
350
static int svq1_decode_motion_vector (GetBitContext *bitbuf, svq1_pmv_t *mv, svq1_pmv_t **pmv) {
310
 
  int         diff;
311
 
  int         i;
 
351
  int        diff;
 
352
  int        i;
312
353
 
313
354
  for (i=0; i < 2; i++) {
314
355
 
315
356
    /* get motion code */
316
 
    diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2) - 32;
 
357
    diff = get_vlc2(bitbuf, svq1_motion_component.table, 7, 2);
 
358
    if(diff<0)
 
359
        return -1;
 
360
    else if(diff){
 
361
        if(get_bits1(bitbuf)) diff= -diff;
 
362
    }
317
363
 
318
364
    /* add median of motion vector predictors and clip result */
319
365
    if (i == 1)
320
 
      mv->y = ((diff + MEDIAN(pmv[0]->y, pmv[1]->y, pmv[2]->y)) << 26) >> 26;
 
366
      mv->y = ((diff + mid_pred(pmv[0]->y, pmv[1]->y, pmv[2]->y)) << 26) >> 26;
321
367
    else
322
 
      mv->x = ((diff + MEDIAN(pmv[0]->x, pmv[1]->x, pmv[2]->x)) << 26) >> 26;
 
368
      mv->x = ((diff + mid_pred(pmv[0]->x, pmv[1]->x, pmv[2]->x)) << 26) >> 26;
323
369
  }
324
370
 
325
371
  return 0;
328
374
static void svq1_skip_block (uint8_t *current, uint8_t *previous, int pitch, int x, int y) {
329
375
  uint8_t *src;
330
376
  uint8_t *dst;
331
 
  int      i;
 
377
  int      i;
332
378
 
333
379
  src = &previous[x + y*pitch];
334
380
  dst = current;
341
387
}
342
388
 
343
389
static int svq1_motion_inter_block (MpegEncContext *s, GetBitContext *bitbuf,
344
 
                               uint8_t *current, uint8_t *previous, int pitch,
345
 
                               svq1_pmv_t *motion, int x, int y) {
 
390
                               uint8_t *current, uint8_t *previous, int pitch,
 
391
                               svq1_pmv_t *motion, int x, int y) {
346
392
  uint8_t    *src;
347
393
  uint8_t    *dst;
348
394
  svq1_pmv_t  mv;
349
395
  svq1_pmv_t *pmv[3];
350
 
  int         result;
 
396
  int         result;
351
397
 
352
398
  /* predict and decode motion vector */
353
399
  pmv[0] = &motion[0];
365
411
  if (result != 0)
366
412
    return result;
367
413
 
368
 
  motion[0].x           =
369
 
  motion[(x / 8) + 2].x =
370
 
  motion[(x / 8) + 3].x = mv.x;
371
 
  motion[0].y           =
372
 
  motion[(x / 8) + 2].y =
373
 
  motion[(x / 8) + 3].y = mv.y;
374
 
  
 
414
  motion[0].x                =
 
415
  motion[(x / 8) + 2].x      =
 
416
  motion[(x / 8) + 3].x      = mv.x;
 
417
  motion[0].y                =
 
418
  motion[(x / 8) + 2].y      =
 
419
  motion[(x / 8) + 3].y      = mv.y;
 
420
 
375
421
  if(y + (mv.y >> 1)<0)
376
422
     mv.y= 0;
377
423
  if(x + (mv.x >> 1)<0)
381
427
  int w= (s->width+15)&~15;
382
428
  int h= (s->height+15)&~15;
383
429
  if(x + (mv.x >> 1)<0 || y + (mv.y >> 1)<0 || x + (mv.x >> 1) + 16 > w || y + (mv.y >> 1) + 16> h)
384
 
      printf("%d %d %d %d\n", x, y, x + (mv.x >> 1), y + (mv.y >> 1));
 
430
      av_log(s->avctx, AV_LOG_INFO, "%d %d %d %d\n", x, y, x + (mv.x >> 1), y + (mv.y >> 1));
385
431
#endif
386
 
 
 
432
 
387
433
  src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1))*pitch];
388
434
  dst = current;
389
435
 
393
439
}
394
440
 
395
441
static int svq1_motion_inter_4v_block (MpegEncContext *s, GetBitContext *bitbuf,
396
 
                                  uint8_t *current, uint8_t *previous, int pitch,
397
 
                                  svq1_pmv_t *motion,int x, int y) {
 
442
                                  uint8_t *current, uint8_t *previous, int pitch,
 
443
                                  svq1_pmv_t *motion,int x, int y) {
398
444
  uint8_t    *src;
399
445
  uint8_t    *dst;
400
446
  svq1_pmv_t  mv;
401
447
  svq1_pmv_t *pmv[4];
402
 
  int         i, result;
 
448
  int         i, result;
403
449
 
404
450
  /* predict and decode motion vector (0) */
405
451
  pmv[0] = &motion[0];
453
499
  for (i=0; i < 4; i++) {
454
500
    int mvx= pmv[i]->x + (i&1)*16;
455
501
    int mvy= pmv[i]->y + (i>>1)*16;
456
 
  
 
502
 
457
503
    ///XXX /FIXME cliping or padding?
458
504
    if(y + (mvy >> 1)<0)
459
505
       mvy= 0;
464
510
  int w= (s->width+15)&~15;
465
511
  int h= (s->height+15)&~15;
466
512
  if(x + (mvx >> 1)<0 || y + (mvy >> 1)<0 || x + (mvx >> 1) + 8 > w || y + (mvy >> 1) + 8> h)
467
 
      printf("%d %d %d %d\n", x, y, x + (mvx >> 1), y + (mvy >> 1));
 
513
      av_log(s->avctx, AV_LOG_INFO, "%d %d %d %d\n", x, y, x + (mvx >> 1), y + (mvy >> 1));
468
514
#endif
469
515
    src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1))*pitch];
470
516
    dst = current;
471
 
    
 
517
 
472
518
    s->dsp.put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst,src,pitch,8);
473
519
 
474
520
    /* select next block */
483
529
}
484
530
 
485
531
static int svq1_decode_delta_block (MpegEncContext *s, GetBitContext *bitbuf,
486
 
                        uint8_t *current, uint8_t *previous, int pitch,
487
 
                        svq1_pmv_t *motion, int x, int y) {
 
532
                        uint8_t *current, uint8_t *previous, int pitch,
 
533
                        svq1_pmv_t *motion, int x, int y) {
488
534
  uint32_t block_type;
489
 
  int      result = 0;
 
535
  int      result = 0;
490
536
 
491
537
  /* get block type */
492
538
  block_type = get_vlc2(bitbuf, svq1_block_type.table, 2, 2);
493
539
 
494
540
  /* reset motion vectors */
495
541
  if (block_type == SVQ1_BLOCK_SKIP || block_type == SVQ1_BLOCK_INTRA) {
496
 
    motion[0].x           =
497
 
    motion[0].y           =
 
542
    motion[0].x                 =
 
543
    motion[0].y                 =
498
544
    motion[(x / 8) + 2].x =
499
545
    motion[(x / 8) + 2].y =
500
546
    motion[(x / 8) + 3].x =
512
558
    if (result != 0)
513
559
    {
514
560
#ifdef DEBUG_SVQ1
515
 
    printf("Error in svq1_motion_inter_block %i\n",result);
 
561
    av_log(s->avctx, AV_LOG_INFO, "Error in svq1_motion_inter_block %i\n",result);
516
562
#endif
517
563
      break;
518
564
    }
525
571
    if (result != 0)
526
572
    {
527
573
#ifdef DEBUG_SVQ1
528
 
    printf("Error in svq1_motion_inter_4v_block %i\n",result);
 
574
    av_log(s->avctx, AV_LOG_INFO, "Error in svq1_motion_inter_4v_block %i\n",result);
529
575
#endif
530
576
      break;
531
577
    }
556
602
  return value;
557
603
}
558
604
 
 
605
#if 0 /* unused, remove? */
559
606
static uint16_t svq1_component_checksum (uint16_t *pixels, int pitch,
560
607
                                         int width, int height, int value) {
561
608
  int x, y;
570
617
 
571
618
  return value;
572
619
}
 
620
#endif
573
621
 
 
622
#ifdef CONFIG_DECODERS
574
623
static void svq1_parse_string (GetBitContext *bitbuf, uint8_t *out) {
575
624
  uint8_t seed;
576
625
  int     i;
579
628
 
580
629
  seed = string_table[out[0]];
581
630
 
582
 
  for (i=1; i < out[0]; i++) {
 
631
  for (i=1; i <= out[0]; i++) {
583
632
    out[i] = get_bits (bitbuf, 8) ^ seed;
584
633
    seed   = string_table[out[i] ^ seed];
585
634
  }
593
642
 
594
643
  /* frame type */
595
644
  s->pict_type= get_bits (bitbuf, 2)+1;
596
 
  if(s->pict_type==4) 
 
645
  if(s->pict_type==4)
597
646
      return -1;
598
 
      
 
647
 
599
648
  if (s->pict_type == I_TYPE) {
600
649
 
601
650
    /* unknown fields */
604
653
 
605
654
      csum = svq1_packet_checksum ((uint8_t *)bitbuf->buffer, bitbuf->size_in_bits>>3, csum);
606
655
 
607
 
//      printf ("%s checksum (%02x) for packet data\n",
 
656
//      av_log(s->avctx, AV_LOG_INFO, "%s checksum (%02x) for packet data\n",
608
657
//              (csum == 0) ? "correct" : "incorrect", csum);
609
658
    }
610
659
 
611
660
    if ((s->f_code ^ 0x10) >= 0x50) {
612
 
      char msg[256];
613
 
 
614
 
      svq1_parse_string (bitbuf, (char *) msg);
615
 
 
616
 
      printf ("embedded message: \"%s\"\n", (char *) msg);
 
661
      uint8_t msg[256];
 
662
 
 
663
      svq1_parse_string (bitbuf, msg);
 
664
 
 
665
      av_log(s->avctx, AV_LOG_INFO, "embedded message: \"%s\"\n", (char *) msg);
617
666
    }
618
667
 
619
668
    skip_bits (bitbuf, 2);
656
705
      skip_bits (bitbuf, 8);
657
706
    }
658
707
  }
659
 
  
 
708
 
660
709
  return 0;
661
710
}
662
711
 
663
 
static int svq1_decode_frame(AVCodecContext *avctx, 
 
712
static int svq1_decode_frame(AVCodecContext *avctx,
664
713
                             void *data, int *data_size,
665
714
                             uint8_t *buf, int buf_size)
666
715
{
667
716
  MpegEncContext *s=avctx->priv_data;
668
 
  uint8_t      *current, *previous;
669
 
  int           result, i, x, y, width, height;
670
 
  AVFrame *pict = data; 
 
717
  uint8_t        *current, *previous;
 
718
  int             result, i, x, y, width, height;
 
719
  AVFrame *pict = data;
671
720
 
672
721
  /* initialize bit buffer */
673
722
  init_get_bits(&s->gb,buf,buf_size*8);
692
741
  if (result != 0)
693
742
  {
694
743
#ifdef DEBUG_SVQ1
695
 
    printf("Error in svq1_decode_frame_header %i\n",result);
 
744
    av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_frame_header %i\n",result);
696
745
#endif
697
746
    return result;
698
747
  }
699
 
  
 
748
 
700
749
  //FIXME this avoids some confusion for "B frames" without 2 references
701
 
  //this should be removed after libavcodec can handle more flaxible picture types & ordering
 
750
  //this should be removed after libavcodec can handle more flexible picture types & ordering
702
751
  if(s->pict_type==B_TYPE && s->last_picture_ptr==NULL) return buf_size;
703
 
  
 
752
 
704
753
  if(avctx->hurry_up && s->pict_type==B_TYPE) return buf_size;
 
754
  if(  (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
 
755
     ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
 
756
     || avctx->skip_frame >= AVDISCARD_ALL)
 
757
      return buf_size;
705
758
 
706
759
  if(MPV_frame_start(s, avctx) < 0)
707
760
      return -1;
731
784
    if (s->pict_type == I_TYPE) {
732
785
      /* keyframe */
733
786
      for (y=0; y < height; y+=16) {
734
 
        for (x=0; x < width; x+=16) {
735
 
          result = svq1_decode_block_intra (&s->gb, &current[x], linesize);
736
 
          if (result != 0)
737
 
          {
738
 
#ifdef DEBUG_SVQ1
739
 
            printf("Error in svq1_decode_block %i (keyframe)\n",result);
740
 
#endif
741
 
            return result;
742
 
          }
743
 
        }
744
 
        current += 16*linesize;
 
787
        for (x=0; x < width; x+=16) {
 
788
          result = svq1_decode_block_intra (&s->gb, &current[x], linesize);
 
789
          if (result != 0)
 
790
          {
 
791
//#ifdef DEBUG_SVQ1
 
792
            av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_block %i (keyframe)\n",result);
 
793
//#endif
 
794
            return result;
 
795
          }
 
796
        }
 
797
        current += 16*linesize;
745
798
      }
746
799
    } else {
747
800
      svq1_pmv_t pmv[width/8+3];
749
802
      memset (pmv, 0, ((width / 8) + 3) * sizeof(svq1_pmv_t));
750
803
 
751
804
      for (y=0; y < height; y+=16) {
752
 
        for (x=0; x < width; x+=16) {
753
 
          result = svq1_decode_delta_block (s, &s->gb, &current[x], previous,
754
 
                                            linesize, pmv, x, y);
755
 
          if (result != 0)
756
 
          {
 
805
        for (x=0; x < width; x+=16) {
 
806
          result = svq1_decode_delta_block (s, &s->gb, &current[x], previous,
 
807
                                            linesize, pmv, x, y);
 
808
          if (result != 0)
 
809
          {
757
810
#ifdef DEBUG_SVQ1
758
 
    printf("Error in svq1_decode_delta_block %i\n",result);
 
811
    av_log(s->avctx, AV_LOG_INFO, "Error in svq1_decode_delta_block %i\n",result);
759
812
#endif
760
 
            return result;
761
 
          }
762
 
        }
763
 
 
764
 
        pmv[0].x =
765
 
        pmv[0].y = 0;
766
 
 
767
 
        current += 16*linesize;
 
813
            return result;
 
814
          }
 
815
        }
 
816
 
 
817
        pmv[0].x =
 
818
        pmv[0].y = 0;
 
819
 
 
820
        current += 16*linesize;
768
821
      }
769
822
    }
770
823
  }
771
 
  
 
824
 
772
825
  *pict = *(AVFrame*)&s->current_picture;
773
826
 
774
827
 
775
828
  MPV_frame_end(s);
776
 
  
 
829
 
777
830
  *data_size=sizeof(AVFrame);
778
831
  return buf_size;
779
832
}
783
836
    MpegEncContext *s = avctx->priv_data;
784
837
    int i;
785
838
 
 
839
    MPV_decode_defaults(s);
 
840
 
786
841
    s->avctx = avctx;
787
842
    s->width = (avctx->width+3)&~3;
788
843
    s->height = (avctx->height+3)&~3;
794
849
 
795
850
    init_vlc(&svq1_block_type, 2, 4,
796
851
        &svq1_block_type_vlc[0][1], 2, 1,
797
 
        &svq1_block_type_vlc[0][0], 2, 1);
 
852
        &svq1_block_type_vlc[0][0], 2, 1, 1);
798
853
 
799
 
    init_vlc(&svq1_motion_component, 7, 65,
800
 
        &svq1_motion_component_vlc[0][1], 4, 2,
801
 
        &svq1_motion_component_vlc[0][0], 4, 2);
 
854
    init_vlc(&svq1_motion_component, 7, 33,
 
855
        &mvtab[0][1], 2, 1,
 
856
        &mvtab[0][0], 2, 1, 1);
802
857
 
803
858
    for (i = 0; i < 6; i++) {
804
859
        init_vlc(&svq1_intra_multistage[i], 3, 8,
805
860
            &svq1_intra_multistage_vlc[i][0][1], 2, 1,
806
 
            &svq1_intra_multistage_vlc[i][0][0], 2, 1);
 
861
            &svq1_intra_multistage_vlc[i][0][0], 2, 1, 1);
807
862
        init_vlc(&svq1_inter_multistage[i], 3, 8,
808
863
            &svq1_inter_multistage_vlc[i][0][1], 2, 1,
809
 
            &svq1_inter_multistage_vlc[i][0][0], 2, 1);
 
864
            &svq1_inter_multistage_vlc[i][0][0], 2, 1, 1);
810
865
    }
811
866
 
812
867
    init_vlc(&svq1_intra_mean, 8, 256,
813
868
        &svq1_intra_mean_vlc[0][1], 4, 2,
814
 
        &svq1_intra_mean_vlc[0][0], 4, 2);
 
869
        &svq1_intra_mean_vlc[0][0], 4, 2, 1);
815
870
 
816
871
    init_vlc(&svq1_inter_mean, 9, 512,
817
872
        &svq1_inter_mean_vlc[0][1], 4, 2,
818
 
        &svq1_inter_mean_vlc[0][0], 4, 2);
 
873
        &svq1_inter_mean_vlc[0][0], 4, 2, 1);
819
874
 
820
875
    return 0;
821
876
}
827
882
    MPV_common_end(s);
828
883
    return 0;
829
884
}
830
 
 
 
885
#endif /* CONFIG_DECODERS */
 
886
 
 
887
#ifdef CONFIG_ENCODERS
 
888
static void svq1_write_header(SVQ1Context *s, int frame_type)
 
889
{
 
890
    int i;
 
891
 
 
892
    /* frame code */
 
893
    put_bits(&s->pb, 22, 0x20);
 
894
 
 
895
    /* temporal reference (sure hope this is a "don't care") */
 
896
    put_bits(&s->pb, 8, 0x00);
 
897
 
 
898
    /* frame type */
 
899
    put_bits(&s->pb, 2, frame_type - 1);
 
900
 
 
901
    if (frame_type == I_TYPE) {
 
902
 
 
903
        /* no checksum since frame code is 0x20 */
 
904
 
 
905
        /* no embedded string either */
 
906
 
 
907
        /* output 5 unknown bits (2 + 2 + 1) */
 
908
        put_bits(&s->pb, 5, 2); /* 2 needed by quicktime decoder */
 
909
 
 
910
        for (i = 0; i < 7; i++)
 
911
        {
 
912
            if ((svq1_frame_size_table[i].width == s->frame_width) &&
 
913
                (svq1_frame_size_table[i].height == s->frame_height))
 
914
            {
 
915
                put_bits(&s->pb, 3, i);
 
916
                break;
 
917
            }
 
918
        }
 
919
 
 
920
        if (i == 7)
 
921
        {
 
922
            put_bits(&s->pb, 3, 7);
 
923
                put_bits(&s->pb, 12, s->frame_width);
 
924
                put_bits(&s->pb, 12, s->frame_height);
 
925
        }
 
926
    }
 
927
 
 
928
    /* no checksum or extra data (next 2 bits get 0) */
 
929
    put_bits(&s->pb, 2, 0);
 
930
}
 
931
 
 
932
 
 
933
#define QUALITY_THRESHOLD 100
 
934
#define THRESHOLD_MULTIPLIER 0.6
 
935
 
 
936
#if defined(HAVE_ALTIVEC)
 
937
#undef vector
 
938
#endif
 
939
 
 
940
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){
 
941
    int count, y, x, i, j, split, best_mean, best_score, best_count;
 
942
    int best_vector[6];
 
943
    int block_sum[7]= {0, 0, 0, 0, 0, 0};
 
944
    int w= 2<<((level+2)>>1);
 
945
    int h= 2<<((level+1)>>1);
 
946
    int size=w*h;
 
947
    int16_t block[7][256];
 
948
    const int8_t *codebook_sum, *codebook;
 
949
    const uint16_t (*mean_vlc)[2];
 
950
    const uint8_t (*multistage_vlc)[2];
 
951
 
 
952
    best_score=0;
 
953
    //FIXME optimize, this doenst need to be done multiple times
 
954
    if(intra){
 
955
        codebook_sum= svq1_intra_codebook_sum[level];
 
956
        codebook= svq1_intra_codebooks[level];
 
957
        mean_vlc= svq1_intra_mean_vlc;
 
958
        multistage_vlc= svq1_intra_multistage_vlc[level];
 
959
        for(y=0; y<h; y++){
 
960
            for(x=0; x<w; x++){
 
961
                int v= src[x + y*stride];
 
962
                block[0][x + w*y]= v;
 
963
                best_score += v*v;
 
964
                block_sum[0] += v;
 
965
            }
 
966
        }
 
967
    }else{
 
968
        codebook_sum= svq1_inter_codebook_sum[level];
 
969
        codebook= svq1_inter_codebooks[level];
 
970
        mean_vlc= svq1_inter_mean_vlc + 256;
 
971
        multistage_vlc= svq1_inter_multistage_vlc[level];
 
972
        for(y=0; y<h; y++){
 
973
            for(x=0; x<w; x++){
 
974
                int v= src[x + y*stride] - ref[x + y*stride];
 
975
                block[0][x + w*y]= v;
 
976
                best_score += v*v;
 
977
                block_sum[0] += v;
 
978
            }
 
979
        }
 
980
    }
 
981
 
 
982
    best_count=0;
 
983
    best_score -= ((block_sum[0]*block_sum[0])>>(level+3));
 
984
    best_mean= (block_sum[0] + (size>>1)) >> (level+3);
 
985
 
 
986
    if(level<4){
 
987
        for(count=1; count<7; count++){
 
988
            int best_vector_score= INT_MAX;
 
989
            int best_vector_sum=-999, best_vector_mean=-999;
 
990
            const int stage= count-1;
 
991
            const int8_t *vector;
 
992
 
 
993
            for(i=0; i<16; i++){
 
994
                int sum= codebook_sum[stage*16 + i];
 
995
                int sqr=0;
 
996
                int diff, mean, score;
 
997
 
 
998
                vector = codebook + stage*size*16 + i*size;
 
999
 
 
1000
                for(j=0; j<size; j++){
 
1001
                    int v= vector[j];
 
1002
                    sqr += (v - block[stage][j])*(v - block[stage][j]);
 
1003
                }
 
1004
                diff= block_sum[stage] - sum;
 
1005
                mean= (diff + (size>>1)) >> (level+3);
 
1006
                assert(mean >-300 && mean<300);
 
1007
                if(intra) mean= av_clip(mean, 0, 255);
 
1008
                else      mean= av_clip(mean, -256, 255);
 
1009
                score= sqr - ((diff*(int64_t)diff)>>(level+3)); //FIXME 64bit slooow
 
1010
                if(score < best_vector_score){
 
1011
                    best_vector_score= score;
 
1012
                    best_vector[stage]= i;
 
1013
                    best_vector_sum= sum;
 
1014
                    best_vector_mean= mean;
 
1015
                }
 
1016
            }
 
1017
            assert(best_vector_mean != -999);
 
1018
            vector= codebook + stage*size*16 + best_vector[stage]*size;
 
1019
            for(j=0; j<size; j++){
 
1020
                block[stage+1][j] = block[stage][j] - vector[j];
 
1021
            }
 
1022
            block_sum[stage+1]= block_sum[stage] - best_vector_sum;
 
1023
            best_vector_score +=
 
1024
                lambda*(+ 1 + 4*count
 
1025
                        + multistage_vlc[1+count][1]
 
1026
                        + mean_vlc[best_vector_mean][1]);
 
1027
 
 
1028
            if(best_vector_score < best_score){
 
1029
                best_score= best_vector_score;
 
1030
                best_count= count;
 
1031
                best_mean= best_vector_mean;
 
1032
            }
 
1033
        }
 
1034
    }
 
1035
 
 
1036
    split=0;
 
1037
    if(best_score > threshold && level){
 
1038
        int score=0;
 
1039
        int offset= (level&1) ? stride*h/2 : w/2;
 
1040
        PutBitContext backup[6];
 
1041
 
 
1042
        for(i=level-1; i>=0; i--){
 
1043
            backup[i]= s->reorder_pb[i];
 
1044
        }
 
1045
        score += encode_block(s, src         , ref         , decoded         , stride, level-1, threshold>>1, lambda, intra);
 
1046
        score += encode_block(s, src + offset, ref + offset, decoded + offset, stride, level-1, threshold>>1, lambda, intra);
 
1047
        score += lambda;
 
1048
 
 
1049
        if(score < best_score){
 
1050
            best_score= score;
 
1051
            split=1;
 
1052
        }else{
 
1053
            for(i=level-1; i>=0; i--){
 
1054
                s->reorder_pb[i]= backup[i];
 
1055
            }
 
1056
        }
 
1057
    }
 
1058
    if (level > 0)
 
1059
        put_bits(&s->reorder_pb[level], 1, split);
 
1060
 
 
1061
    if(!split){
 
1062
        assert((best_mean >= 0 && best_mean<256) || !intra);
 
1063
        assert(best_mean >= -256 && best_mean<256);
 
1064
        assert(best_count >=0 && best_count<7);
 
1065
        assert(level<4 || best_count==0);
 
1066
 
 
1067
        /* output the encoding */
 
1068
        put_bits(&s->reorder_pb[level],
 
1069
            multistage_vlc[1 + best_count][1],
 
1070
            multistage_vlc[1 + best_count][0]);
 
1071
        put_bits(&s->reorder_pb[level], mean_vlc[best_mean][1],
 
1072
            mean_vlc[best_mean][0]);
 
1073
 
 
1074
        for (i = 0; i < best_count; i++){
 
1075
            assert(best_vector[i]>=0 && best_vector[i]<16);
 
1076
            put_bits(&s->reorder_pb[level], 4, best_vector[i]);
 
1077
        }
 
1078
 
 
1079
        for(y=0; y<h; y++){
 
1080
            for(x=0; x<w; x++){
 
1081
                decoded[x + y*stride]= src[x + y*stride] - block[best_count][x + w*y] + best_mean;
 
1082
            }
 
1083
        }
 
1084
    }
 
1085
 
 
1086
    return best_score;
 
1087
}
 
1088
 
 
1089
 
 
1090
static int svq1_encode_plane(SVQ1Context *s, int plane, unsigned char *src_plane, unsigned char *ref_plane, unsigned char *decoded_plane,
 
1091
    int width, int height, int src_stride, int stride)
 
1092
{
 
1093
    int x, y;
 
1094
    int i;
 
1095
    int block_width, block_height;
 
1096
    int level;
 
1097
    int threshold[6];
 
1098
    const int lambda= (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT);
 
1099
 
 
1100
    /* figure out the acceptable level thresholds in advance */
 
1101
    threshold[5] = QUALITY_THRESHOLD;
 
1102
    for (level = 4; level >= 0; level--)
 
1103
        threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
 
1104
 
 
1105
    block_width = (width + 15) / 16;
 
1106
    block_height = (height + 15) / 16;
 
1107
 
 
1108
    if(s->picture.pict_type == P_TYPE){
 
1109
        s->m.avctx= s->avctx;
 
1110
        s->m.current_picture_ptr= &s->m.current_picture;
 
1111
        s->m.last_picture_ptr   = &s->m.last_picture;
 
1112
        s->m.last_picture.data[0]= ref_plane;
 
1113
        s->m.linesize=
 
1114
        s->m.last_picture.linesize[0]=
 
1115
        s->m.new_picture.linesize[0]=
 
1116
        s->m.current_picture.linesize[0]= stride;
 
1117
        s->m.width= width;
 
1118
        s->m.height= height;
 
1119
        s->m.mb_width= block_width;
 
1120
        s->m.mb_height= block_height;
 
1121
        s->m.mb_stride= s->m.mb_width+1;
 
1122
        s->m.b8_stride= 2*s->m.mb_width+1;
 
1123
        s->m.f_code=1;
 
1124
        s->m.pict_type= s->picture.pict_type;
 
1125
        s->m.me_method= s->avctx->me_method;
 
1126
        s->m.me.scene_change_score=0;
 
1127
        s->m.flags= s->avctx->flags;
 
1128
//        s->m.out_format = FMT_H263;
 
1129
//        s->m.unrestricted_mv= 1;
 
1130
 
 
1131
        s->m.lambda= s->picture.quality;
 
1132
        s->m.qscale= (s->m.lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
 
1133
        s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
 
1134
 
 
1135
        if(!s->motion_val8[plane]){
 
1136
            s->motion_val8 [plane]= av_mallocz((s->m.b8_stride*block_height*2 + 2)*2*sizeof(int16_t));
 
1137
            s->motion_val16[plane]= av_mallocz((s->m.mb_stride*(block_height + 2) + 1)*2*sizeof(int16_t));
 
1138
        }
 
1139
 
 
1140
        s->m.mb_type= s->mb_type;
 
1141
 
 
1142
        //dummies, to avoid segfaults
 
1143
        s->m.current_picture.mb_mean=   (uint8_t *)s->dummy;
 
1144
        s->m.current_picture.mb_var=    (uint16_t*)s->dummy;
 
1145
        s->m.current_picture.mc_mb_var= (uint16_t*)s->dummy;
 
1146
        s->m.current_picture.mb_type= s->dummy;
 
1147
 
 
1148
        s->m.current_picture.motion_val[0]= s->motion_val8[plane] + 2;
 
1149
        s->m.p_mv_table= s->motion_val16[plane] + s->m.mb_stride + 1;
 
1150
        s->m.dsp= s->dsp; //move
 
1151
        ff_init_me(&s->m);
 
1152
 
 
1153
        s->m.me.dia_size= s->avctx->dia_size;
 
1154
        s->m.first_slice_line=1;
 
1155
        for (y = 0; y < block_height; y++) {
 
1156
            uint8_t src[stride*16];
 
1157
 
 
1158
            s->m.new_picture.data[0]= src - y*16*stride; //ugly
 
1159
            s->m.mb_y= y;
 
1160
 
 
1161
            for(i=0; i<16 && i + 16*y<height; i++){
 
1162
                memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
 
1163
                for(x=width; x<16*block_width; x++)
 
1164
                    src[i*stride+x]= src[i*stride+x-1];
 
1165
            }
 
1166
            for(; i<16 && i + 16*y<16*block_height; i++)
 
1167
                memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
 
1168
 
 
1169
            for (x = 0; x < block_width; x++) {
 
1170
                s->m.mb_x= x;
 
1171
                ff_init_block_index(&s->m);
 
1172
                ff_update_block_index(&s->m);
 
1173
 
 
1174
                ff_estimate_p_frame_motion(&s->m, x, y);
 
1175
            }
 
1176
            s->m.first_slice_line=0;
 
1177
        }
 
1178
 
 
1179
        ff_fix_long_p_mvs(&s->m);
 
1180
        ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code, CANDIDATE_MB_TYPE_INTER, 0);
 
1181
    }
 
1182
 
 
1183
    s->m.first_slice_line=1;
 
1184
    for (y = 0; y < block_height; y++) {
 
1185
        uint8_t src[stride*16];
 
1186
 
 
1187
        for(i=0; i<16 && i + 16*y<height; i++){
 
1188
            memcpy(&src[i*stride], &src_plane[(i+16*y)*src_stride], width);
 
1189
            for(x=width; x<16*block_width; x++)
 
1190
                src[i*stride+x]= src[i*stride+x-1];
 
1191
        }
 
1192
        for(; i<16 && i + 16*y<16*block_height; i++)
 
1193
            memcpy(&src[i*stride], &src[(i-1)*stride], 16*block_width);
 
1194
 
 
1195
        s->m.mb_y= y;
 
1196
        for (x = 0; x < block_width; x++) {
 
1197
            uint8_t reorder_buffer[3][6][7*32];
 
1198
            int count[3][6];
 
1199
            int offset = y * 16 * stride + x * 16;
 
1200
            uint8_t *decoded= decoded_plane + offset;
 
1201
            uint8_t *ref= ref_plane + offset;
 
1202
            int score[4]={0,0,0,0}, best;
 
1203
            uint8_t temp[16*stride];
 
1204
 
 
1205
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < 3000){ //FIXME check size
 
1206
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
 
1207
                return -1;
 
1208
            }
 
1209
 
 
1210
            s->m.mb_x= x;
 
1211
            ff_init_block_index(&s->m);
 
1212
            ff_update_block_index(&s->m);
 
1213
 
 
1214
            if(s->picture.pict_type == I_TYPE || (s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTRA)){
 
1215
                for(i=0; i<6; i++){
 
1216
                    init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i], 7*32);
 
1217
                }
 
1218
                if(s->picture.pict_type == P_TYPE){
 
1219
                    const uint8_t *vlc= svq1_block_type_vlc[SVQ1_BLOCK_INTRA];
 
1220
                    put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
 
1221
                    score[0]= vlc[1]*lambda;
 
1222
                }
 
1223
                score[0]+= encode_block(s, src+16*x, NULL, temp, stride, 5, 64, lambda, 1);
 
1224
                for(i=0; i<6; i++){
 
1225
                    count[0][i]= put_bits_count(&s->reorder_pb[i]);
 
1226
                    flush_put_bits(&s->reorder_pb[i]);
 
1227
                }
 
1228
            }else
 
1229
                score[0]= INT_MAX;
 
1230
 
 
1231
            best=0;
 
1232
 
 
1233
            if(s->picture.pict_type == P_TYPE){
 
1234
                const uint8_t *vlc= svq1_block_type_vlc[SVQ1_BLOCK_INTER];
 
1235
                int mx, my, pred_x, pred_y, dxy;
 
1236
                int16_t *motion_ptr;
 
1237
 
 
1238
                motion_ptr= h263_pred_motion(&s->m, 0, 0, &pred_x, &pred_y);
 
1239
                if(s->m.mb_type[x + y*s->m.mb_stride]&CANDIDATE_MB_TYPE_INTER){
 
1240
                    for(i=0; i<6; i++)
 
1241
                        init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i], 7*32);
 
1242
 
 
1243
                    put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
 
1244
 
 
1245
                    s->m.pb= s->reorder_pb[5];
 
1246
                    mx= motion_ptr[0];
 
1247
                    my= motion_ptr[1];
 
1248
                    assert(mx>=-32 && mx<=31);
 
1249
                    assert(my>=-32 && my<=31);
 
1250
                    assert(pred_x>=-32 && pred_x<=31);
 
1251
                    assert(pred_y>=-32 && pred_y<=31);
 
1252
                    ff_h263_encode_motion(&s->m, mx - pred_x, 1);
 
1253
                    ff_h263_encode_motion(&s->m, my - pred_y, 1);
 
1254
                    s->reorder_pb[5]= s->m.pb;
 
1255
                    score[1] += lambda*put_bits_count(&s->reorder_pb[5]);
 
1256
 
 
1257
                    dxy= (mx&1) + 2*(my&1);
 
1258
 
 
1259
                    s->dsp.put_pixels_tab[0][dxy](temp+16, ref + (mx>>1) + stride*(my>>1), stride, 16);
 
1260
 
 
1261
                    score[1]+= encode_block(s, src+16*x, temp+16, decoded, stride, 5, 64, lambda, 0);
 
1262
                    best= score[1] <= score[0];
 
1263
 
 
1264
                    vlc= svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
 
1265
                    score[2]= s->dsp.sse[0](NULL, src+16*x, ref, stride, 16);
 
1266
                    score[2]+= vlc[1]*lambda;
 
1267
                    if(score[2] < score[best] && mx==0 && my==0){
 
1268
                        best=2;
 
1269
                        s->dsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
 
1270
                        for(i=0; i<6; i++){
 
1271
                            count[2][i]=0;
 
1272
                        }
 
1273
                        put_bits(&s->pb, vlc[1], vlc[0]);
 
1274
                    }
 
1275
                }
 
1276
 
 
1277
                if(best==1){
 
1278
                    for(i=0; i<6; i++){
 
1279
                        count[1][i]= put_bits_count(&s->reorder_pb[i]);
 
1280
                        flush_put_bits(&s->reorder_pb[i]);
 
1281
                    }
 
1282
                }else{
 
1283
                    motion_ptr[0                 ] = motion_ptr[1                 ]=
 
1284
                    motion_ptr[2                 ] = motion_ptr[3                 ]=
 
1285
                    motion_ptr[0+2*s->m.b8_stride] = motion_ptr[1+2*s->m.b8_stride]=
 
1286
                    motion_ptr[2+2*s->m.b8_stride] = motion_ptr[3+2*s->m.b8_stride]=0;
 
1287
                }
 
1288
            }
 
1289
 
 
1290
            s->rd_total += score[best];
 
1291
 
 
1292
            for(i=5; i>=0; i--){
 
1293
                ff_copy_bits(&s->pb, reorder_buffer[best][i], count[best][i]);
 
1294
            }
 
1295
            if(best==0){
 
1296
                s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
 
1297
            }
 
1298
        }
 
1299
        s->m.first_slice_line=0;
 
1300
    }
 
1301
    return 0;
 
1302
}
 
1303
 
 
1304
static int svq1_encode_init(AVCodecContext *avctx)
 
1305
{
 
1306
    SVQ1Context * const s = avctx->priv_data;
 
1307
 
 
1308
    dsputil_init(&s->dsp, avctx);
 
1309
    avctx->coded_frame= (AVFrame*)&s->picture;
 
1310
 
 
1311
    s->frame_width = avctx->width;
 
1312
    s->frame_height = avctx->height;
 
1313
 
 
1314
    s->y_block_width = (s->frame_width + 15) / 16;
 
1315
    s->y_block_height = (s->frame_height + 15) / 16;
 
1316
 
 
1317
    s->c_block_width = (s->frame_width / 4 + 15) / 16;
 
1318
    s->c_block_height = (s->frame_height / 4 + 15) / 16;
 
1319
 
 
1320
    s->avctx= avctx;
 
1321
    s->m.avctx= avctx;
 
1322
    s->m.me.scratchpad= av_mallocz((avctx->width+64)*2*16*2*sizeof(uint8_t));
 
1323
    s->m.me.map       = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
 
1324
    s->m.me.score_map = av_mallocz(ME_MAP_SIZE*sizeof(uint32_t));
 
1325
    s->mb_type        = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int16_t));
 
1326
    s->dummy          = av_mallocz((s->y_block_width+1)*s->y_block_height*sizeof(int32_t));
 
1327
    h263_encode_init(&s->m); //mv_penalty
 
1328
 
 
1329
    return 0;
 
1330
}
 
1331
 
 
1332
static int svq1_encode_frame(AVCodecContext *avctx, unsigned char *buf,
 
1333
    int buf_size, void *data)
 
1334
{
 
1335
    SVQ1Context * const s = avctx->priv_data;
 
1336
    AVFrame *pict = data;
 
1337
    AVFrame * const p= (AVFrame*)&s->picture;
 
1338
    AVFrame temp;
 
1339
    int i;
 
1340
 
 
1341
    if(avctx->pix_fmt != PIX_FMT_YUV410P){
 
1342
        av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
 
1343
        return -1;
 
1344
    }
 
1345
 
 
1346
    if(!s->current_picture.data[0]){
 
1347
        avctx->get_buffer(avctx, &s->current_picture);
 
1348
        avctx->get_buffer(avctx, &s->last_picture);
 
1349
    }
 
1350
 
 
1351
    temp= s->current_picture;
 
1352
    s->current_picture= s->last_picture;
 
1353
    s->last_picture= temp;
 
1354
 
 
1355
    init_put_bits(&s->pb, buf, buf_size);
 
1356
 
 
1357
    *p = *pict;
 
1358
    p->pict_type = avctx->gop_size && avctx->frame_number % avctx->gop_size ? P_TYPE : I_TYPE;
 
1359
    p->key_frame = p->pict_type == I_TYPE;
 
1360
 
 
1361
    svq1_write_header(s, p->pict_type);
 
1362
    for(i=0; i<3; i++){
 
1363
        if(svq1_encode_plane(s, i,
 
1364
            s->picture.data[i], s->last_picture.data[i], s->current_picture.data[i],
 
1365
            s->frame_width / (i?4:1), s->frame_height / (i?4:1),
 
1366
            s->picture.linesize[i], s->current_picture.linesize[i]) < 0)
 
1367
                return -1;
 
1368
    }
 
1369
 
 
1370
//    align_put_bits(&s->pb);
 
1371
    while(put_bits_count(&s->pb) & 31)
 
1372
        put_bits(&s->pb, 1, 0);
 
1373
 
 
1374
    flush_put_bits(&s->pb);
 
1375
 
 
1376
    return (put_bits_count(&s->pb) / 8);
 
1377
}
 
1378
 
 
1379
static int svq1_encode_end(AVCodecContext *avctx)
 
1380
{
 
1381
    SVQ1Context * const s = avctx->priv_data;
 
1382
    int i;
 
1383
 
 
1384
    av_log(avctx, AV_LOG_DEBUG, "RD: %f\n", s->rd_total/(double)(avctx->width*avctx->height*avctx->frame_number));
 
1385
 
 
1386
    av_freep(&s->m.me.scratchpad);
 
1387
    av_freep(&s->m.me.map);
 
1388
    av_freep(&s->m.me.score_map);
 
1389
    av_freep(&s->mb_type);
 
1390
    av_freep(&s->dummy);
 
1391
 
 
1392
    for(i=0; i<3; i++){
 
1393
        av_freep(&s->motion_val8[i]);
 
1394
        av_freep(&s->motion_val16[i]);
 
1395
    }
 
1396
 
 
1397
    return 0;
 
1398
}
 
1399
 
 
1400
#endif //CONFIG_ENCODERS
 
1401
 
 
1402
#ifdef CONFIG_DECODERS
831
1403
AVCodec svq1_decoder = {
832
1404
    "svq1",
833
1405
    CODEC_TYPE_VIDEO,
839
1411
    svq1_decode_frame,
840
1412
    CODEC_CAP_DR1,
841
1413
    .flush= ff_mpeg_flush,
842
 
};
 
1414
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV410P, -1},
 
1415
};
 
1416
#endif
 
1417
 
 
1418
#ifdef CONFIG_ENCODERS
 
1419
 
 
1420
AVCodec svq1_encoder = {
 
1421
    "svq1",
 
1422
    CODEC_TYPE_VIDEO,
 
1423
    CODEC_ID_SVQ1,
 
1424
    sizeof(SVQ1Context),
 
1425
    svq1_encode_init,
 
1426
    svq1_encode_frame,
 
1427
    svq1_encode_end,
 
1428
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV410P, -1},
 
1429
};
 
1430
 
 
1431
#endif //CONFIG_ENCODERS