]> git.sesse.net Git - ffmpeg/blob - libavcodec/dv.c
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
[ffmpeg] / libavcodec / dv.c
1 /*
2  * DV decoder
3  * Copyright (c) 2002 Fabrice Bellard.
4  *
5  * DV encoder 
6  * Copyright (c) 2003 Roman Shaposhnik.
7  *
8  * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
9  * of DV technical info.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 /**
27  * @file dv.c
28  * DV codec.
29  */
30 #include "avcodec.h"
31 #include "dsputil.h"
32 #include "mpegvideo.h"
33 #include "simple_idct.h"
34 #include "dvdata.h"
35
36 typedef struct DVVideoDecodeContext {
37     const DVprofile* sys;
38     AVFrame picture;
39     
40     uint8_t dv_zigzag[2][64];
41     uint8_t dv_idct_shift[2][22][64];
42     uint8_t dv_dct_shift[2][22][64];
43   
44     void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
45     void (*fdct[2])(DCTELEM *block);
46     void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
47     
48     GetBitContext gb;
49     DCTELEM block[5*6][64] __align8;
50 } DVVideoDecodeContext;
51
52 #define TEX_VLC_BITS 9
53 /* XXX: also include quantization */
54 static RL_VLC_ELEM *dv_rl_vlc[1];
55 static VLC_TYPE dv_vlc_codes[15][23];
56
57 static void dv_build_unquantize_tables(DVVideoDecodeContext *s, uint8_t* perm)
58 {
59     int i, q, j;
60
61     /* NOTE: max left shift is 6 */
62     for(q = 0; q < 22; q++) {
63         /* 88DCT */
64         for(i = 1; i < 64; i++) {
65             /* 88 table */
66             j = perm[i];
67             s->dv_idct_shift[0][q][j] =
68                 dv_quant_shifts[q][dv_88_areas[i]] + 1;
69             s->dv_dct_shift[0][q][i] =
70                 dv_quant_shifts[q][dv_88_areas[ff_zigzag_direct[i]]] + 4;
71         }
72         
73         /* 248DCT */
74         for(i = 1; i < 64; i++) {
75             /* 248 table */
76             s->dv_idct_shift[1][q][i] =  
77                 dv_quant_shifts[q][dv_248_areas[i]] + 1;
78             s->dv_dct_shift[1][q][i] =  
79                 dv_quant_shifts[q][dv_248_areas[ff_zigzag248_direct[i]]] + 4;
80         }
81     }
82 }
83
84 static int dvvideo_init(AVCodecContext *avctx)
85 {
86     DVVideoDecodeContext *s = avctx->priv_data;
87     DSPContext dsp;
88     static int done=0;
89     int i;
90
91     if (!done) {
92         int i;
93         VLC dv_vlc;
94
95         done = 1;
96
97         /* NOTE: as a trick, we use the fact the no codes are unused
98            to accelerate the parsing of partial codes */
99         init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC, 
100                  dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
101
102         dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
103         for(i = 0; i < dv_vlc.table_size; i++){
104             int code= dv_vlc.table[i][0];
105             int len = dv_vlc.table[i][1];
106             int level, run;
107         
108             if(len<0){ //more bits needed
109                 run= 0;
110                 level= code;
111             } else if (code == (NB_DV_VLC - 1)) {
112                 /* EOB */
113                 run = 0;
114                 level = 256;
115             } else {
116                 run=   dv_vlc_run[code] + 1;
117                 level= dv_vlc_level[code];
118             }
119             dv_rl_vlc[0][i].len = len;
120             dv_rl_vlc[0][i].level = level;
121             dv_rl_vlc[0][i].run = run;
122         }
123
124         memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
125         for (i = 0; i < NB_DV_VLC - 1; i++) {
126            if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
127                dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
128         }
129     }
130
131     /* Generic DSP setup */
132     dsputil_init(&dsp, avctx);
133     s->get_pixels = dsp.get_pixels;
134
135     /* 88DCT setup */
136     s->fdct[0] = dsp.fdct;
137     s->idct_put[0] = dsp.idct_put;
138     for (i=0; i<64; i++)
139        s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
140
141     /* 248DCT setup */
142     s->fdct[1] = dsp.fdct248;
143     s->idct_put[1] = simple_idct248_put;  // FIXME: need to add it to DSP
144     memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
145
146     /* XXX: do it only for constant case */
147     dv_build_unquantize_tables(s, dsp.idct_permutation);
148
149     /* FIXME: I really don't think this should be here */
150     if (dv_codec_profile(avctx))
151         avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt; 
152     avctx->coded_frame = &s->picture;
153     
154     return 0;
155 }
156
157 // #define VLC_DEBUG
158
159 typedef struct BlockInfo {
160     const uint8_t *shift_table;
161     const uint8_t *scan_table;
162     uint8_t pos; /* position in block */
163     uint8_t eob_reached; /* true if EOB has been reached */
164     uint8_t dct_mode;
165     uint8_t partial_bit_count;
166     uint16_t partial_bit_buffer;
167     int shift_offset;
168 } BlockInfo;
169
170 /* block size in bits */
171 static const uint16_t block_sizes[6] = {
172     112, 112, 112, 112, 80, 80
173 };
174
175 #ifndef ALT_BITSTREAM_READER
176 #warning only works with ALT_BITSTREAM_READER
177 #endif
178
179 /* decode ac coefs */
180 static void dv_decode_ac(DVVideoDecodeContext *s, 
181                          BlockInfo *mb, DCTELEM *block, int last_index)
182 {
183     int last_re_index;
184     int shift_offset = mb->shift_offset;
185     const uint8_t *scan_table = mb->scan_table;
186     const uint8_t *shift_table = mb->shift_table;
187     int pos = mb->pos;
188     int level, pos1, sign, run;
189     int partial_bit_count;
190 #ifndef ALT_BITSTREAM_READER //FIXME
191     int re_index=0; 
192     int re1_index=0;
193 #endif
194     OPEN_READER(re, &s->gb);
195     
196 #ifdef VLC_DEBUG
197     printf("start\n");
198 #endif
199
200     /* if we must parse a partial vlc, we do it here */
201     partial_bit_count = mb->partial_bit_count;
202     if (partial_bit_count > 0) {
203         uint8_t buf[4];
204         uint32_t v;
205         int l, l1;
206         GetBitContext gb1;
207
208         /* build the dummy bit buffer */
209         l = 16 - partial_bit_count;
210         UPDATE_CACHE(re, &s->gb);
211 #ifdef VLC_DEBUG
212         printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
213 #endif
214         v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
215         buf[0] = v >> 8;
216         buf[1] = v;
217 #ifdef VLC_DEBUG
218         printf("v=%04x cnt=%d %04x\n", 
219                v, partial_bit_count, (mb->partial_bit_buffer << l));
220 #endif
221         /* try to read the codeword */
222         init_get_bits(&gb1, buf, 4*8);
223         {
224             OPEN_READER(re1, &gb1);
225             UPDATE_CACHE(re1, &gb1);
226             GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0], 
227                        TEX_VLC_BITS, 2);
228             l = re1_index;
229             CLOSE_READER(re1, &gb1);
230         }
231 #ifdef VLC_DEBUG
232         printf("****run=%d level=%d size=%d\n", run, level, l);
233 #endif
234         /* compute codeword length */
235         l1 = (level != 256 && level != 0);
236         /* if too long, we cannot parse */
237         l -= partial_bit_count;
238         if ((re_index + l + l1) > last_index)
239             return;
240         /* skip read bits */
241         last_re_index = 0; /* avoid warning */
242         re_index += l;
243         /* by definition, if we can read the vlc, all partial bits
244            will be read (otherwise we could have read the vlc before) */
245         mb->partial_bit_count = 0;
246         UPDATE_CACHE(re, &s->gb);
247         goto handle_vlc;
248     }
249
250     /* get the AC coefficients until last_index is reached */
251     for(;;) {
252         UPDATE_CACHE(re, &s->gb);
253 #ifdef VLC_DEBUG
254         printf("%2d: bits=%04x index=%d\n", 
255                pos, SHOW_UBITS(re, &s->gb, 16), re_index);
256 #endif
257         last_re_index = re_index;
258         GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0], 
259                    TEX_VLC_BITS, 2);
260     handle_vlc:
261 #ifdef VLC_DEBUG
262         printf("run=%d level=%d\n", run, level);
263 #endif
264         if (level == 256) {
265             if (re_index > last_index) {
266             cannot_read:
267                 /* put position before read code */
268                 re_index = last_re_index;
269                 mb->eob_reached = 0;
270                 break;
271             }
272             /* EOB */
273             mb->eob_reached = 1;
274             break;
275         } else if (level != 0) {
276             if ((re_index + 1) > last_index)
277                 goto cannot_read;
278             sign = SHOW_SBITS(re, &s->gb, 1);
279             level = (level ^ sign) - sign;
280             LAST_SKIP_BITS(re, &s->gb, 1);
281             pos += run;
282             /* error */
283             if (pos >= 64) {
284                 goto read_error;
285             }
286             pos1 = scan_table[pos];
287             level = level << (shift_table[pos1] + shift_offset);
288             block[pos1] = level;
289             //            printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
290         } else {
291             if (re_index > last_index)
292                 goto cannot_read;
293             /* level is zero: means run without coding. No
294                sign is coded */
295             pos += run;
296             /* error */
297             if (pos >= 64) {
298             read_error:
299 #if defined(VLC_DEBUG) || 1
300                 av_log(NULL, AV_LOG_ERROR, "error pos=%d\n", pos);
301 #endif
302                 /* for errors, we consider the eob is reached */
303                 mb->eob_reached = 1;
304                 break;
305             }
306         }
307     }
308     CLOSE_READER(re, &s->gb);
309     mb->pos = pos;
310 }
311
312 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
313 {
314     while (bits_left >= 16) {
315         put_bits(pb, 16, get_bits(gb, 16));
316         bits_left -= 16;
317     }
318     if (bits_left > 0) {
319         put_bits(pb, bits_left, get_bits(gb, bits_left));
320     }
321 }
322
323 /* mb_x and mb_y are in units of 8 pixels */
324 static inline void dv_decode_video_segment(DVVideoDecodeContext *s, 
325                                            uint8_t *buf_ptr1, 
326                                            const uint16_t *mb_pos_ptr)
327 {
328     int quant, dc, dct_mode, class1, j;
329     int mb_index, mb_x, mb_y, v, last_index;
330     DCTELEM *block, *block1;
331     int c_offset, bits_left;
332     uint8_t *y_ptr;
333     BlockInfo mb_data[5 * 6], *mb, *mb1;
334     void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
335     uint8_t *buf_ptr;
336     PutBitContext pb, vs_pb;
337     uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
338     int mb_bit_count;
339     uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
340     int vs_bit_count;
341     
342     memset(s->block, 0, sizeof(s->block));
343
344     /* pass 1 : read DC and AC coefficients in blocks */
345     buf_ptr = buf_ptr1;
346     block1 = &s->block[0][0];
347     mb1 = mb_data;
348     init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
349     vs_bit_count = 0;
350     for(mb_index = 0; mb_index < 5; mb_index++) {
351         /* skip header */
352         quant = buf_ptr[3] & 0x0f;
353         buf_ptr += 4;
354         init_put_bits(&pb, mb_bit_buffer, 80);
355         mb_bit_count = 0;
356         mb = mb1;
357         block = block1;
358         for(j = 0;j < 6; j++) {
359             /* NOTE: size is not important here */
360             init_get_bits(&s->gb, buf_ptr, 14*8);
361             
362             /* get the dc */
363             dc = get_bits(&s->gb, 9);
364             dc = (dc << (32 - 9)) >> (32 - 9);
365             dct_mode = get_bits1(&s->gb);
366             mb->dct_mode = dct_mode;
367             mb->scan_table = s->dv_zigzag[dct_mode];
368             class1 = get_bits(&s->gb, 2);
369             mb->shift_offset = (class1 == 3);
370             mb->shift_table = s->dv_idct_shift[dct_mode]
371                 [quant + dv_quant_offset[class1]];
372             dc = dc << 2;
373             /* convert to unsigned because 128 is not added in the
374                standard IDCT */
375             dc += 1024;
376             block[0] = dc;
377             last_index = block_sizes[j];
378             buf_ptr += last_index >> 3;
379             mb->pos = 0;
380             mb->partial_bit_count = 0;
381
382 #ifdef VLC_DEBUG
383             printf("MB block: %d, %d ", mb_index, j);
384 #endif
385             dv_decode_ac(s, mb, block, last_index);
386
387             /* write the remaining bits  in a new buffer only if the
388                block is finished */
389             bits_left = last_index - get_bits_count(&s->gb);
390             if (mb->eob_reached) {
391                 mb->partial_bit_count = 0;
392                 mb_bit_count += bits_left;
393                 bit_copy(&pb, &s->gb, bits_left);
394             } else {
395                 /* should be < 16 bits otherwise a codeword could have
396                    been parsed */
397                 mb->partial_bit_count = bits_left;
398                 mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
399             }
400             block += 64;
401             mb++;
402         }
403         
404         flush_put_bits(&pb);
405
406         /* pass 2 : we can do it just after */
407 #ifdef VLC_DEBUG
408         printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index);
409 #endif
410         block = block1;
411         mb = mb1;
412         init_get_bits(&s->gb, mb_bit_buffer, 80*8);
413         for(j = 0;j < 6; j++) {
414             if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
415                 dv_decode_ac(s, mb, block, mb_bit_count);
416                 /* if still not finished, no need to parse other blocks */
417                 if (!mb->eob_reached) {
418                     /* we could not parse the current AC coefficient,
419                        so we add the remaining bytes */
420                     bits_left = mb_bit_count - get_bits_count(&s->gb);
421                     if (bits_left > 0) {
422                         mb->partial_bit_count += bits_left;
423                         mb->partial_bit_buffer = 
424                             (mb->partial_bit_buffer << bits_left) | 
425                             get_bits(&s->gb, bits_left);
426                     }
427                     goto next_mb;
428                 }
429             }
430             block += 64;
431             mb++;
432         }
433         /* all blocks are finished, so the extra bytes can be used at
434            the video segment level */
435         bits_left = mb_bit_count - get_bits_count(&s->gb);
436         vs_bit_count += bits_left;
437         bit_copy(&vs_pb, &s->gb, bits_left);
438     next_mb:
439         mb1 += 6;
440         block1 += 6 * 64;
441     }
442
443     /* we need a pass other the whole video segment */
444     flush_put_bits(&vs_pb);
445         
446 #ifdef VLC_DEBUG
447     printf("***pass 3 size=%d\n", vs_bit_count);
448 #endif
449     block = &s->block[0][0];
450     mb = mb_data;
451     init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
452     for(mb_index = 0; mb_index < 5; mb_index++) {
453         for(j = 0;j < 6; j++) {
454             if (!mb->eob_reached) {
455 #ifdef VLC_DEBUG
456                 printf("start %d:%d\n", mb_index, j);
457 #endif
458                 dv_decode_ac(s, mb, block, vs_bit_count);
459             }
460             block += 64;
461             mb++;
462         }
463     }
464     
465     /* compute idct and place blocks */
466     block = &s->block[0][0];
467     mb = mb_data;
468     for(mb_index = 0; mb_index < 5; mb_index++) {
469         v = *mb_pos_ptr++;
470         mb_x = v & 0xff;
471         mb_y = v >> 8;
472         y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
473         if (s->sys->pix_fmt == PIX_FMT_YUV411P)
474             c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
475         else
476             c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
477         for(j = 0;j < 6; j++) {
478             idct_put = s->idct_put[mb->dct_mode];
479             if (j < 4) {
480                 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
481                     /* NOTE: at end of line, the macroblock is handled as 420 */
482                     idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
483                 } else {
484                     idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
485                              s->picture.linesize[0], block);
486                 }
487             } else {
488                 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
489                     uint64_t aligned_pixels[64/8];
490                     uint8_t *pixels= (uint8_t*)aligned_pixels;
491                     uint8_t *c_ptr, *c_ptr1, *ptr;
492                     int y, linesize;
493                     /* NOTE: at end of line, the macroblock is handled as 420 */
494                     idct_put(pixels, 8, block);
495                     linesize = s->picture.linesize[6 - j];
496                     c_ptr = s->picture.data[6 - j] + c_offset;
497                     ptr = pixels;
498                     for(y = 0;y < 8; y++) {
499                         /* convert to 411P */
500                         c_ptr1 = c_ptr + 8*linesize;
501                         c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
502                         c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
503                         c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
504                         c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
505                         c_ptr += linesize;
506                         ptr += 8;
507                     }
508                 } else {
509                     /* don't ask me why they inverted Cb and Cr ! */
510                     idct_put(s->picture.data[6 - j] + c_offset, 
511                              s->picture.linesize[6 - j], block);
512                 }
513             }
514             block += 64;
515             mb++;
516         }
517     }
518 }
519
520 /* Converts run and level (where level != 0) pair into vlc, returning bit size */
521 static inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
522 {
523     int sign = l >> 8;
524     int level = (l ^ sign) - sign;
525     int size;
526     
527     sign = (sign & 1);
528
529     if (run < 15 && level < 23 && dv_vlc_codes[run][level] != -1) {
530         *vlc = (dv_vlc_bits[dv_vlc_codes[run][level]] << 1) | sign; 
531         size = dv_vlc_len[dv_vlc_codes[run][level]] + 1;
532     }
533     else { 
534         if (level < 23) {
535             *vlc = (dv_vlc_bits[dv_vlc_codes[0][level]] << 1) | sign; 
536             size = dv_vlc_len[dv_vlc_codes[0][level]] + 1;
537         } else {
538             *vlc = 0xfe00 | (level << 1) | sign;
539             size = 16;
540         }
541
542         switch(run) {
543         case 0:
544             break;
545         case 1:
546         case 2:
547             *vlc |= ((0x7ce | (run - 1)) << size);
548             size += 11;
549             break;
550         case 3:
551         case 4:
552         case 5:
553         case 6:
554             *vlc |= ((0xfac | (run - 3)) << size);
555             size += 12;
556             break;
557         default:
558             *vlc |= ((0x1f80 | (run - 1)) << size);
559             size += 13;
560             break;
561         }
562     }
563     
564     return size;
565 }
566
567 typedef struct EncBlockInfo {
568     int qno;
569     int cno;
570     int dct_mode;
571     int block_size;
572     DCTELEM *mb;
573     PutBitContext pb;
574     const uint8_t* zigzag_scan;
575     uint8_t *dv_shift;
576 } EncBlockInfo;
577
578 static inline int dv_bits_left(EncBlockInfo* bi)
579 {
580     return (bi->block_size - get_bit_count(&bi->pb));
581 }
582
583 static inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* heap)
584 {
585     int i, level, size, run = 0;
586     uint32_t vlc;
587     PutBitContext* cpb = &bi->pb;
588     int bias = (bi->cno == 3);
589     
590     for (i=1; i<64; i++) {
591        level = bi->mb[bi->zigzag_scan[i]] / (1<<(bi->dv_shift[i] + bias));
592        if (level != 0) {
593            size = dv_rl2vlc(run, level, &vlc);
594 put_vlc:
595
596 #ifdef VLC_DEBUG
597            printf(" %3d:%3d", run, level);
598 #endif
599            if (cpb == &bi->pb && size > dv_bits_left(bi)) {
600                size -= dv_bits_left(bi);
601                put_bits(cpb, dv_bits_left(bi), vlc >> size);
602                vlc = vlc & ((1<<size)-1);
603                cpb = heap;
604            }
605            put_bits(cpb, size, vlc);
606            run = 0;
607        } else
608            run++;
609     }
610    
611     if (i == 64) {
612         size = 4; vlc = 6; /* End Of Block stamp */
613         goto put_vlc;
614     }
615 }
616
617 static inline void dv_redistr_bits(EncBlockInfo* bi, int count, uint8_t* extra_data, int extra_bits, PutBitContext* heap)
618 {
619     int i;
620     GetBitContext gb;
621     
622     init_get_bits(&gb, extra_data, extra_bits);
623     
624     for (i=0; i<count; i++) {
625        int bits_left = dv_bits_left(bi);
626 #ifdef VLC_DEBUG
627        if (bits_left)
628            printf("------------> inserting %d bytes in %d:%d\n", bits_left, i/6, i%6);
629 #endif
630        if (bits_left > extra_bits) {
631            bit_copy(&bi->pb, &gb, extra_bits); 
632            extra_bits = 0;
633            break;
634        } else
635            bit_copy(&bi->pb, &gb, bits_left);
636            
637        extra_bits -= bits_left;
638        bi++;
639     }
640     
641     if (extra_bits > 0 && heap)
642         bit_copy(heap, &gb, extra_bits);
643 }
644
645 static inline void dv_set_class_number(EncBlockInfo* bi, int j)
646 {
647     int i, max_ac = 0;
648
649     for (i=1; i<64; i++) {
650        int ac = abs(bi->mb[ff_zigzag_direct[i]]) / 4;
651        if (max_ac < ac)
652            max_ac = ac;
653     }
654     if (max_ac < 12)
655         bi->cno = j;
656     else if (max_ac < 24)
657         bi->cno = j + 1;
658     else if (max_ac < 36)
659         bi->cno = j + 2;
660     else
661         bi->cno = j + 3;
662     
663     if (bi->cno > 3)
664         bi->cno = 3;
665 }
666
667 #define SQ(a) ((a)*(a))
668 static int dv_score_lines(DCTELEM *s, int stride) {
669     int score=0;
670     int x, y;
671     
672     for(y=0; y<4; y++) {
673         for(x=0; x<8; x+=4){
674             score+= SQ(s[x  ] - s[x  +stride]) + SQ(s[x+1] - s[x+1+stride]) 
675                    +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
676         }
677         s+= stride;
678     }
679     
680     return score;
681 }
682
683 /*
684  * This is a very rough initial implementaion. The performance is
685  * horrible and the weighting is missing. But it's missing from the 
686  * decoding step also -- so at least we're on the same page with decoder ;-)
687  */
688 static inline void dv_encode_video_segment(DVVideoDecodeContext *s, 
689                                            uint8_t *dif, 
690                                            const uint16_t *mb_pos_ptr)
691 {
692     int mb_index, i, j, v;
693     int mb_x, mb_y, c_offset, linesize; 
694     uint8_t*  y_ptr;
695     uint8_t*  data;
696     int       do_edge_wrap;
697     DCTELEM  *block;
698     EncBlockInfo  enc_blks[5*6];
699     EncBlockInfo* enc_blk;
700     int       free_vs_bits;
701     int extra_bits;
702     PutBitContext extra_vs;
703     uint8_t   extra_vs_data[5*6*128];
704     uint8_t   extra_mb_data[6*128];
705
706     int       QNO = 15;
707    
708     /* Stage 1 -- doing DCT on 5 MBs */
709     block = &s->block[0][0];
710     enc_blk = &enc_blks[0];
711     for(mb_index = 0; mb_index < 5; mb_index++) {
712         v = *mb_pos_ptr++;
713         mb_x = v & 0xff;
714         mb_y = v >> 8;
715         y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
716         c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
717                    ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
718                    (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
719         do_edge_wrap = 0;
720         for(j = 0;j < 6; j++) {
721             if (j < 4) {  /* Four Y blocks */
722                 /* NOTE: at end of line, the macroblock is handled as 420 */
723                 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
724                     data = y_ptr + (j * 8);
725                 } else {
726                     data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
727                 }
728                 linesize = s->picture.linesize[0];
729             } else {      /* Cr and Cb blocks */
730                 /* don't ask Fabrice why they inverted Cb and Cr ! */
731                 data = s->picture.data[6 - j] + c_offset;
732                 linesize = s->picture.linesize[6 - j];
733                 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
734                     do_edge_wrap = 1;
735             }   
736             
737             /* Everything is set up -- now just copy data -> DCT block */
738             if (do_edge_wrap) {  /* Edge wrap copy: 4x16 -> 8x8 */
739                 uint8_t* d;
740                 DCTELEM *b = block;
741                 for (i=0;i<8;i++) {
742                    d = data + 8 * linesize;
743                    b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
744                    b[4] =    d[0]; b[5] =    d[1]; b[6] =    d[2]; b[7] =    d[3];
745                    data += linesize;
746                    b += 8;
747                 }
748             } else {             /* Simple copy: 8x8 -> 8x8 */
749                 s->get_pixels(block, data, linesize);
750             }
751           
752             if (dv_score_lines(block, 8) + dv_score_lines(block+8*4, 8) - 100 >
753                 dv_score_lines(block, 16) + dv_score_lines(block+8, 16)) {
754                enc_blk->dct_mode = 1;
755                enc_blk->zigzag_scan = ff_zigzag248_direct; 
756             } else {
757                enc_blk->dct_mode = 0;
758                enc_blk->zigzag_scan = ff_zigzag_direct;
759             }
760             enc_blk->mb = block;
761             enc_blk->block_size = block_sizes[j];
762             
763             s->fdct[enc_blk->dct_mode](block);
764             
765             dv_set_class_number(enc_blk, j/4*(j%2));
766
767             block += 64;
768             enc_blk++;
769         }
770     }
771
772     /* Stage 2 -- encoding by trial-and-error */
773 encode_vs:
774     enc_blk = &enc_blks[0];
775     for (i=0; i<5; i++) {
776        uint8_t* p = dif + i*80 + 4;
777        for (j=0; j<6; j++) {
778           enc_blk->qno = QNO;
779           enc_blk->dv_shift = &(s->dv_dct_shift[0]
780                                    [QNO + dv_quant_offset[enc_blk->cno]][0]);
781           init_put_bits(&enc_blk->pb, p, block_sizes[j]/8);
782           enc_blk++;
783           p += block_sizes[j]/8;
784        }
785     }
786
787     init_put_bits(&extra_vs, extra_vs_data, sizeof(extra_vs_data));
788     free_vs_bits = 0;
789     enc_blk = &enc_blks[0];
790     for (i=0; i<5; i++) {
791        PutBitContext extra_mb;
792        EncBlockInfo* enc_blk2 = enc_blk;
793        int free_mb_bits = 0;
794
795        init_put_bits(&extra_mb, extra_mb_data, sizeof(extra_mb_data));
796        dif[i*80 + 3] = enc_blk->qno;
797        
798        for (j=0; j<6; j++) {
799           uint16_t dc = ((enc_blk->mb[0] >> 3) - 1024) >> 2;
800
801           put_bits(&enc_blk->pb, 9, dc);
802           put_bits(&enc_blk->pb, 1, enc_blk->dct_mode);
803           put_bits(&enc_blk->pb, 2, enc_blk->cno);
804
805 #ifdef VLC_DEBUG
806           printf("[%d, %d]: ", i, j);
807 #endif
808           dv_encode_ac(enc_blk, &extra_mb);
809 #ifdef VLC_DEBUG
810           printf("\n");
811 #endif
812           
813           free_mb_bits += dv_bits_left(enc_blk);
814           enc_blk++;
815        }
816        
817        /* We can't flush extra_mb just yet -- since it'll round up bit number */
818        extra_bits = get_bit_count(&extra_mb);
819        if (free_mb_bits > extra_bits)
820            free_vs_bits += free_mb_bits - extra_bits;
821     
822        if (extra_bits) {  /* FIXME: speed up things when free_mb_bits == 0 */
823            flush_put_bits(&extra_mb);
824            dv_redistr_bits(enc_blk2, 6, extra_mb_data, extra_bits, &extra_vs);
825        }
826     }
827     
828     /* We can't flush extra_mb just yet -- since it'll round up bit number */
829     extra_bits = get_bit_count(&extra_vs);
830     if (extra_bits > free_vs_bits && QNO) { /* FIXME: very crude trial-and-error */
831         QNO--;
832         goto encode_vs;
833     }
834     
835     if (extra_bits) {
836         flush_put_bits(&extra_vs);
837         dv_redistr_bits(&enc_blks[0], 5*6, extra_vs_data, extra_bits, NULL);
838     }
839
840     for (i=0; i<6*5; i++) {
841        flush_put_bits(&enc_blks[i].pb);
842 #ifdef VLC_DEBUG
843        printf("[%d:%d] qno=%d cno=%d\n", i/6, i%6, enc_blks[i].qno, enc_blks[i].cno);
844 #endif
845     }
846 }
847
848 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
849    144000 bytes for PAL) */
850 static int dvvideo_decode_frame(AVCodecContext *avctx, 
851                                  void *data, int *data_size,
852                                  uint8_t *buf, int buf_size)
853 {
854     DVVideoDecodeContext *s = avctx->priv_data;
855     int ds, vs;
856     const uint16_t *mb_pos_ptr;
857     
858     s->sys = dv_frame_profile(buf);
859     if (!s->sys || buf_size < s->sys->frame_size)
860         return -1; /* NOTE: we only accept several full frames */
861
862         
863     if(s->picture.data[0])
864         avctx->release_buffer(avctx, &s->picture);
865     
866     s->picture.reference = 0;
867     avctx->pix_fmt = s->sys->pix_fmt;
868     avctx->width = s->sys->width;
869     avctx->height = s->sys->height;
870     if(avctx->get_buffer(avctx, &s->picture) < 0) {
871         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
872         return -1;
873     }
874     s->picture.interlaced_frame = 1;
875     s->picture.top_field_first = 0;
876
877     /* for each DIF segment */
878     mb_pos_ptr = s->sys->video_place;
879     for (ds = 0; ds < s->sys->difseg_size; ds++) {
880         buf += 6 * 80; /* skip DIF segment header */
881         
882         for(vs = 0; vs < 27; vs++) {
883             if ((vs % 3) == 0)
884                 buf += 80; /* skip audio block */
885             
886 #ifdef VLC_DEBUG
887             printf("********************* %d, %d **********************\n", ds, vs);
888 #endif
889             dv_decode_video_segment(s, buf, mb_pos_ptr);
890             buf += 5 * 80;
891             mb_pos_ptr += 5;
892         }
893     }
894
895     emms_c();
896
897     /* return image */
898     *data_size = sizeof(AVFrame);
899     *(AVFrame*)data= s->picture;
900     
901     return s->sys->frame_size;
902 }
903
904 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size, 
905                                 void *data)
906 {
907     DVVideoDecodeContext *s = c->priv_data;
908     const uint16_t *mb_pos_ptr;
909     int ds, vs;
910
911     s->sys = dv_codec_profile(c);
912     if (!s->sys)
913         return -1;
914     
915     c->pix_fmt = s->sys->pix_fmt;
916     s->picture = *((AVFrame *)data);
917
918     /* for each DIF segment */
919     mb_pos_ptr = s->sys->video_place;
920     for (ds = 0; ds < s->sys->difseg_size; ds++) {
921         buf += 6 * 80; /* skip DIF segment header */
922         
923         for(vs = 0; vs < 27; vs++) {
924             if ((vs % 3) == 0)
925                 buf += 80; /* skip audio block */
926
927 #ifdef VLC_DEBUG
928             printf("********************* %d, %d **********************\n", ds, vs);
929 #endif
930             dv_encode_video_segment(s, buf, mb_pos_ptr);
931             buf += 5 * 80;
932             mb_pos_ptr += 5;
933         }
934     }
935
936     emms_c();
937     return s->sys->frame_size;
938 }
939
940 static int dvvideo_end(AVCodecContext *avctx)
941 {
942     avcodec_default_free_buffers(avctx);    
943     return 0;
944 }
945
946 AVCodec dvvideo_decoder = {
947     "dvvideo",
948     CODEC_TYPE_VIDEO,
949     CODEC_ID_DVVIDEO,
950     sizeof(DVVideoDecodeContext),
951     dvvideo_init,
952     dvvideo_encode_frame,
953     dvvideo_end,
954     dvvideo_decode_frame,
955     CODEC_CAP_DR1,
956     NULL
957 };