]> git.sesse.net Git - ffmpeg/blob - libavcodec/dv.c
c6f039923d2c8bd2eda9d0bd3fdeb251444df2a8
[ffmpeg] / libavcodec / dv.c
1 /*
2  * DV decoder
3  * Copyright (c) 2002 Fabrice Bellard.
4  * Copyright (c) 2004 Roman Shaposhnik.
5  *
6  * DV encoder
7  * Copyright (c) 2003 Roman Shaposhnik.
8  *
9  * 50 Mbps (DVCPRO50) support
10  * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
11  *
12  * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
13  * of DV technical info.
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29
30 /**
31  * @file dv.c
32  * DV codec.
33  */
34 #define ALT_BITSTREAM_READER
35 #include "avcodec.h"
36 #include "dsputil.h"
37 #include "mpegvideo.h"
38 #include "simple_idct.h"
39 #include "dvdata.h"
40
41 //#undef NDEBUG
42 //#include <assert.h>
43
44 typedef struct DVVideoContext {
45     const DVprofile* sys;
46     AVFrame picture;
47     AVCodecContext *avctx;
48     uint8_t *buf;
49
50     uint8_t dv_zigzag[2][64];
51     uint8_t dv_idct_shift[2][2][22][64];
52
53     void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
54     void (*fdct[2])(DCTELEM *block);
55     void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
56 } DVVideoContext;
57
58 /* MultiThreading - dv_anchor applies to entire DV codec, not just the avcontext */
59 /* one element is needed for each video segment in a DV frame */
60 /* at most there are 2 DIF channels * 12 DIF sequences * 27 video segments (PAL 50Mbps) */
61 #define DV_ANCHOR_SIZE (2*12*27)
62
63 static void* dv_anchor[DV_ANCHOR_SIZE];
64
65 #define TEX_VLC_BITS 9
66
67 #ifdef DV_CODEC_TINY_TARGET
68 #define DV_VLC_MAP_RUN_SIZE 15
69 #define DV_VLC_MAP_LEV_SIZE 23
70 #else
71 #define DV_VLC_MAP_RUN_SIZE  64
72 #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check
73 #endif
74
75 /* XXX: also include quantization */
76 static RL_VLC_ELEM *dv_rl_vlc;
77 /* VLC encoding lookup table */
78 static struct dv_vlc_pair {
79    uint32_t vlc;
80    uint8_t  size;
81 } (*dv_vlc_map)[DV_VLC_MAP_LEV_SIZE] = NULL;
82
83 static void dv_build_unquantize_tables(DVVideoContext *s, uint8_t* perm)
84 {
85     int i, q, j;
86
87     /* NOTE: max left shift is 6 */
88     for(q = 0; q < 22; q++) {
89         /* 88DCT */
90         for(i = 1; i < 64; i++) {
91             /* 88 table */
92             j = perm[i];
93             s->dv_idct_shift[0][0][q][j] =
94                 dv_quant_shifts[q][dv_88_areas[i]] + 1;
95             s->dv_idct_shift[1][0][q][j] = s->dv_idct_shift[0][0][q][j] + 1;
96         }
97
98         /* 248DCT */
99         for(i = 1; i < 64; i++) {
100             /* 248 table */
101             s->dv_idct_shift[0][1][q][i] =
102                 dv_quant_shifts[q][dv_248_areas[i]] + 1;
103             s->dv_idct_shift[1][1][q][i] = s->dv_idct_shift[0][1][q][i] + 1;
104         }
105     }
106 }
107
108 static int dvvideo_init(AVCodecContext *avctx)
109 {
110     DVVideoContext *s = avctx->priv_data;
111     DSPContext dsp;
112     static int done=0;
113     int i, j;
114
115     if (!done) {
116         VLC dv_vlc;
117         uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
118         uint8_t new_dv_vlc_len[NB_DV_VLC*2];
119         uint8_t new_dv_vlc_run[NB_DV_VLC*2];
120         int16_t new_dv_vlc_level[NB_DV_VLC*2];
121
122         done = 1;
123
124         dv_vlc_map = av_mallocz_static(DV_VLC_MAP_LEV_SIZE*DV_VLC_MAP_RUN_SIZE*sizeof(struct dv_vlc_pair));
125         if (!dv_vlc_map)
126             return -ENOMEM;
127
128         /* dv_anchor lets each thread know its Id */
129         for (i=0; i<DV_ANCHOR_SIZE; i++)
130             dv_anchor[i] = (void*)(size_t)i;
131
132         /* it's faster to include sign bit in a generic VLC parsing scheme */
133         for (i=0, j=0; i<NB_DV_VLC; i++, j++) {
134             new_dv_vlc_bits[j] = dv_vlc_bits[i];
135             new_dv_vlc_len[j] = dv_vlc_len[i];
136             new_dv_vlc_run[j] = dv_vlc_run[i];
137             new_dv_vlc_level[j] = dv_vlc_level[i];
138
139             if (dv_vlc_level[i]) {
140                 new_dv_vlc_bits[j] <<= 1;
141                 new_dv_vlc_len[j]++;
142
143                 j++;
144                 new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
145                 new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
146                 new_dv_vlc_run[j] = dv_vlc_run[i];
147                 new_dv_vlc_level[j] = -dv_vlc_level[i];
148             }
149         }
150
151         /* NOTE: as a trick, we use the fact the no codes are unused
152            to accelerate the parsing of partial codes */
153         init_vlc(&dv_vlc, TEX_VLC_BITS, j,
154                  new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
155
156         dv_rl_vlc = av_mallocz_static(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
157         if (!dv_rl_vlc)
158             return -ENOMEM;
159
160         for(i = 0; i < dv_vlc.table_size; i++){
161             int code= dv_vlc.table[i][0];
162             int len = dv_vlc.table[i][1];
163             int level, run;
164
165             if(len<0){ //more bits needed
166                 run= 0;
167                 level= code;
168             } else {
169                 run=   new_dv_vlc_run[code] + 1;
170                 level= new_dv_vlc_level[code];
171             }
172             dv_rl_vlc[i].len = len;
173             dv_rl_vlc[i].level = level;
174             dv_rl_vlc[i].run = run;
175         }
176         free_vlc(&dv_vlc);
177
178         for (i = 0; i < NB_DV_VLC - 1; i++) {
179            if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE)
180                continue;
181 #ifdef DV_CODEC_TINY_TARGET
182            if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
183                continue;
184 #endif
185
186            if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
187                continue;
188
189            dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc = dv_vlc_bits[i] <<
190                                                             (!!dv_vlc_level[i]);
191            dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size = dv_vlc_len[i] +
192                                                              (!!dv_vlc_level[i]);
193         }
194         for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
195 #ifdef DV_CODEC_TINY_TARGET
196            for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
197               if (dv_vlc_map[i][j].size == 0) {
198                   dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
199                             (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
200                   dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
201                                           dv_vlc_map[0][j].size;
202               }
203            }
204 #else
205            for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
206               if (dv_vlc_map[i][j].size == 0) {
207                   dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
208                             (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
209                   dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
210                                           dv_vlc_map[0][j].size;
211               }
212               dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
213                                             dv_vlc_map[i][j].vlc | 1;
214               dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
215                                             dv_vlc_map[i][j].size;
216            }
217 #endif
218         }
219     }
220
221     /* Generic DSP setup */
222     dsputil_init(&dsp, avctx);
223     s->get_pixels = dsp.get_pixels;
224
225     /* 88DCT setup */
226     s->fdct[0] = dsp.fdct;
227     s->idct_put[0] = dsp.idct_put;
228     for (i=0; i<64; i++)
229        s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
230
231     /* 248DCT setup */
232     s->fdct[1] = dsp.fdct248;
233     s->idct_put[1] = simple_idct248_put;  // FIXME: need to add it to DSP
234     if(avctx->lowres){
235         for (i=0; i<64; i++){
236             int j= ff_zigzag248_direct[i];
237             s->dv_zigzag[1][i] = dsp.idct_permutation[(j&7) + (j&8)*4 + (j&48)/2];
238         }
239     }else
240         memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
241
242     /* XXX: do it only for constant case */
243     dv_build_unquantize_tables(s, dsp.idct_permutation);
244
245     avctx->coded_frame = &s->picture;
246     s->avctx= avctx;
247
248     return 0;
249 }
250
251 // #define VLC_DEBUG
252 // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
253
254 typedef struct BlockInfo {
255     const uint8_t *shift_table;
256     const uint8_t *scan_table;
257     const int *iweight_table;
258     uint8_t pos; /* position in block */
259     uint8_t dct_mode;
260     uint8_t partial_bit_count;
261     uint16_t partial_bit_buffer;
262     int shift_offset;
263 } BlockInfo;
264
265 /* block size in bits */
266 static const uint16_t block_sizes[6] = {
267     112, 112, 112, 112, 80, 80
268 };
269 /* bit budget for AC only in 5 MBs */
270 static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
271 /* see dv_88_areas and dv_248_areas for details */
272 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
273
274 static inline int get_bits_left(GetBitContext *s)
275 {
276     return s->size_in_bits - get_bits_count(s);
277 }
278
279 static inline int get_bits_size(GetBitContext *s)
280 {
281     return s->size_in_bits;
282 }
283
284 static inline int put_bits_left(PutBitContext* s)
285 {
286     return (s->buf_end - s->buf) * 8 - put_bits_count(s);
287 }
288
289 /* decode ac coefs */
290 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
291 {
292     int last_index = get_bits_size(gb);
293     const uint8_t *scan_table = mb->scan_table;
294     const uint8_t *shift_table = mb->shift_table;
295     const int *iweight_table = mb->iweight_table;
296     int pos = mb->pos;
297     int partial_bit_count = mb->partial_bit_count;
298     int level, pos1, run, vlc_len, index;
299
300     OPEN_READER(re, gb);
301     UPDATE_CACHE(re, gb);
302
303     /* if we must parse a partial vlc, we do it here */
304     if (partial_bit_count > 0) {
305         re_cache = ((unsigned)re_cache >> partial_bit_count) |
306                    (mb->partial_bit_buffer << (sizeof(re_cache)*8 - partial_bit_count));
307         re_index -= partial_bit_count;
308         mb->partial_bit_count = 0;
309     }
310
311     /* get the AC coefficients until last_index is reached */
312     for(;;) {
313 #ifdef VLC_DEBUG
314         printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index);
315 #endif
316         /* our own optimized GET_RL_VLC */
317         index = NEG_USR32(re_cache, TEX_VLC_BITS);
318         vlc_len = dv_rl_vlc[index].len;
319         if (vlc_len < 0) {
320             index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
321             vlc_len = TEX_VLC_BITS - vlc_len;
322         }
323         level = dv_rl_vlc[index].level;
324         run = dv_rl_vlc[index].run;
325
326         /* gotta check if we're still within gb boundaries */
327         if (re_index + vlc_len > last_index) {
328             /* should be < 16 bits otherwise a codeword could have been parsed */
329             mb->partial_bit_count = last_index - re_index;
330             mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
331             re_index = last_index;
332             break;
333         }
334         re_index += vlc_len;
335
336 #ifdef VLC_DEBUG
337         printf("run=%d level=%d\n", run, level);
338 #endif
339         pos += run;
340         if (pos >= 64)
341             break;
342
343         pos1 = scan_table[pos];
344         level <<= shift_table[pos1];
345
346         /* unweigh, round, and shift down */
347         level = (level*iweight_table[pos] + (1 << (dv_iweight_bits-1))) >> dv_iweight_bits;
348
349         block[pos1] = level;
350
351         UPDATE_CACHE(re, gb);
352     }
353     CLOSE_READER(re, gb);
354     mb->pos = pos;
355 }
356
357 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
358 {
359     int bits_left = get_bits_left(gb);
360     while (bits_left >= MIN_CACHE_BITS) {
361         put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
362         bits_left -= MIN_CACHE_BITS;
363     }
364     if (bits_left > 0) {
365         put_bits(pb, bits_left, get_bits(gb, bits_left));
366     }
367 }
368
369 /* mb_x and mb_y are in units of 8 pixels */
370 static inline void dv_decode_video_segment(DVVideoContext *s,
371                                            uint8_t *buf_ptr1,
372                                            const uint16_t *mb_pos_ptr)
373 {
374     int quant, dc, dct_mode, class1, j;
375     int mb_index, mb_x, mb_y, v, last_index;
376     DCTELEM *block, *block1;
377     int c_offset;
378     uint8_t *y_ptr;
379     void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
380     uint8_t *buf_ptr;
381     PutBitContext pb, vs_pb;
382     GetBitContext gb;
383     BlockInfo mb_data[5 * 6], *mb, *mb1;
384     DECLARE_ALIGNED_8(DCTELEM, sblock[5*6][64]);
385     DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */
386     DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */
387     const int log2_blocksize= 3-s->avctx->lowres;
388
389     assert((((int)mb_bit_buffer)&7)==0);
390     assert((((int)vs_bit_buffer)&7)==0);
391
392     memset(sblock, 0, sizeof(sblock));
393
394     /* pass 1 : read DC and AC coefficients in blocks */
395     buf_ptr = buf_ptr1;
396     block1 = &sblock[0][0];
397     mb1 = mb_data;
398     init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
399     for(mb_index = 0; mb_index < 5; mb_index++, mb1 += 6, block1 += 6 * 64) {
400         /* skip header */
401         quant = buf_ptr[3] & 0x0f;
402         buf_ptr += 4;
403         init_put_bits(&pb, mb_bit_buffer, 80);
404         mb = mb1;
405         block = block1;
406         for(j = 0;j < 6; j++) {
407             last_index = block_sizes[j];
408             init_get_bits(&gb, buf_ptr, last_index);
409
410             /* get the dc */
411             dc = get_sbits(&gb, 9);
412             dct_mode = get_bits1(&gb);
413             mb->dct_mode = dct_mode;
414             mb->scan_table = s->dv_zigzag[dct_mode];
415             mb->iweight_table = dct_mode ? dv_iweight_248 : dv_iweight_88;
416             class1 = get_bits(&gb, 2);
417             mb->shift_table = s->dv_idct_shift[class1 == 3][dct_mode]
418                 [quant + dv_quant_offset[class1]];
419             dc = dc << 2;
420             /* convert to unsigned because 128 is not added in the
421                standard IDCT */
422             dc += 1024;
423             block[0] = dc;
424             buf_ptr += last_index >> 3;
425             mb->pos = 0;
426             mb->partial_bit_count = 0;
427
428 #ifdef VLC_DEBUG
429             printf("MB block: %d, %d ", mb_index, j);
430 #endif
431             dv_decode_ac(&gb, mb, block);
432
433             /* write the remaining bits  in a new buffer only if the
434                block is finished */
435             if (mb->pos >= 64)
436                 bit_copy(&pb, &gb);
437
438             block += 64;
439             mb++;
440         }
441
442         /* pass 2 : we can do it just after */
443 #ifdef VLC_DEBUG
444         printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
445 #endif
446         block = block1;
447         mb = mb1;
448         init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
449         flush_put_bits(&pb);
450         for(j = 0;j < 6; j++, block += 64, mb++) {
451             if (mb->pos < 64 && get_bits_left(&gb) > 0) {
452                 dv_decode_ac(&gb, mb, block);
453                 /* if still not finished, no need to parse other blocks */
454                 if (mb->pos < 64)
455                     break;
456             }
457         }
458         /* all blocks are finished, so the extra bytes can be used at
459            the video segment level */
460         if (j >= 6)
461             bit_copy(&vs_pb, &gb);
462     }
463
464     /* we need a pass other the whole video segment */
465 #ifdef VLC_DEBUG
466     printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
467 #endif
468     block = &sblock[0][0];
469     mb = mb_data;
470     init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
471     flush_put_bits(&vs_pb);
472     for(mb_index = 0; mb_index < 5; mb_index++) {
473         for(j = 0;j < 6; j++) {
474             if (mb->pos < 64) {
475 #ifdef VLC_DEBUG
476                 printf("start %d:%d\n", mb_index, j);
477 #endif
478                 dv_decode_ac(&gb, mb, block);
479             }
480             if (mb->pos >= 64 && mb->pos < 127)
481                 av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
482             block += 64;
483             mb++;
484         }
485     }
486
487     /* compute idct and place blocks */
488     block = &sblock[0][0];
489     mb = mb_data;
490     for(mb_index = 0; mb_index < 5; mb_index++) {
491         v = *mb_pos_ptr++;
492         mb_x = v & 0xff;
493         mb_y = v >> 8;
494         if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
495             y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + (mb_x>>1))<<log2_blocksize);
496             c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
497         } else { /* 4:1:1 or 4:2:0 */
498             y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
499             if (s->sys->pix_fmt == PIX_FMT_YUV411P)
500                 c_offset = ((mb_y * s->picture.linesize[1] + (mb_x >> 2))<<log2_blocksize);
501             else /* 4:2:0 */
502                 c_offset = (((mb_y >> 1) * s->picture.linesize[1] + (mb_x >> 1))<<log2_blocksize);
503         }
504         for(j = 0;j < 6; j++) {
505             idct_put = s->idct_put[mb->dct_mode && log2_blocksize==3];
506             if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
507                 if (j == 0 || j == 2) {
508                     /* Y0 Y1 */
509                     idct_put(y_ptr + ((j >> 1)<<log2_blocksize),
510                              s->picture.linesize[0], block);
511                 } else if(j > 3) {
512                     /* Cr Cb */
513                     idct_put(s->picture.data[6 - j] + c_offset,
514                              s->picture.linesize[6 - j], block);
515                 }
516                 /* note: j=1 and j=3 are "dummy" blocks in 4:2:2 */
517             } else { /* 4:1:1 or 4:2:0 */
518                 if (j < 4) {
519                     if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
520                         /* NOTE: at end of line, the macroblock is handled as 420 */
521                         idct_put(y_ptr + (j<<log2_blocksize), s->picture.linesize[0], block);
522                     } else {
523                         idct_put(y_ptr + (((j & 1) + (j >> 1) * s->picture.linesize[0])<<log2_blocksize),
524                                  s->picture.linesize[0], block);
525                     }
526                 } else {
527                     if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
528                         uint64_t aligned_pixels[64/8];
529                         uint8_t *pixels= (uint8_t*)aligned_pixels;
530                         uint8_t *c_ptr, *c_ptr1, *ptr, *ptr1;
531                         int x, y, linesize;
532                         /* NOTE: at end of line, the macroblock is handled as 420 */
533                         idct_put(pixels, 8, block);
534                         linesize = s->picture.linesize[6 - j];
535                         c_ptr = s->picture.data[6 - j] + c_offset;
536                         ptr = pixels;
537                         for(y = 0;y < (1<<log2_blocksize); y++) {
538                             ptr1= ptr + (1<<(log2_blocksize-1));
539                             c_ptr1 = c_ptr + (linesize<<log2_blocksize);
540                             for(x=0; x < (1<<(log2_blocksize-1)); x++){
541                                 c_ptr[x]= ptr[x]; c_ptr1[x]= ptr1[x];
542                             }
543                             c_ptr += linesize;
544                             ptr += 8;
545                         }
546                     } else {
547                         /* don't ask me why they inverted Cb and Cr ! */
548                         idct_put(s->picture.data[6 - j] + c_offset,
549                                  s->picture.linesize[6 - j], block);
550                     }
551                 }
552             }
553             block += 64;
554             mb++;
555         }
556     }
557 }
558
559 #ifdef DV_CODEC_TINY_TARGET
560 /* Converts run and level (where level != 0) pair into vlc, returning bit size */
561 static always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
562 {
563     int size;
564     if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
565         *vlc = dv_vlc_map[run][level].vlc | sign;
566         size = dv_vlc_map[run][level].size;
567     }
568     else {
569         if (level < DV_VLC_MAP_LEV_SIZE) {
570             *vlc = dv_vlc_map[0][level].vlc | sign;
571             size = dv_vlc_map[0][level].size;
572         } else {
573             *vlc = 0xfe00 | (level << 1) | sign;
574             size = 16;
575         }
576         if (run) {
577             *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
578                                   (0x1f80 | (run - 1))) << size;
579             size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
580         }
581     }
582
583     return size;
584 }
585
586 static always_inline int dv_rl2vlc_size(int run, int level)
587 {
588     int size;
589
590     if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
591         size = dv_vlc_map[run][level].size;
592     }
593     else {
594         size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
595         if (run) {
596             size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
597         }
598     }
599     return size;
600 }
601 #else
602 static always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
603 {
604     *vlc = dv_vlc_map[run][l].vlc | sign;
605     return dv_vlc_map[run][l].size;
606 }
607
608 static always_inline int dv_rl2vlc_size(int run, int l)
609 {
610     return dv_vlc_map[run][l].size;
611 }
612 #endif
613
614 typedef struct EncBlockInfo {
615     int area_q[4];
616     int bit_size[4];
617     int prev[5];
618     int cur_ac;
619     int cno;
620     int dct_mode;
621     DCTELEM mb[64];
622     uint8_t next[64];
623     uint8_t sign[64];
624     uint8_t partial_bit_count;
625     uint32_t partial_bit_buffer; /* we can't use uint16_t here */
626 } EncBlockInfo;
627
628 static always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi, PutBitContext* pb_pool,
629                                        PutBitContext* pb_end)
630 {
631     int prev;
632     int bits_left;
633     PutBitContext* pb = pb_pool;
634     int size = bi->partial_bit_count;
635     uint32_t vlc = bi->partial_bit_buffer;
636
637     bi->partial_bit_count = bi->partial_bit_buffer = 0;
638     for(;;){
639        /* Find suitable storage space */
640        for (; size > (bits_left = put_bits_left(pb)); pb++) {
641           if (bits_left) {
642               size -= bits_left;
643               put_bits(pb, bits_left, vlc >> size);
644               vlc = vlc & ((1<<size)-1);
645           }
646           if (pb + 1 >= pb_end) {
647               bi->partial_bit_count = size;
648               bi->partial_bit_buffer = vlc;
649               return pb;
650           }
651        }
652
653        /* Store VLC */
654        put_bits(pb, size, vlc);
655
656        if(bi->cur_ac>=64)
657            break;
658
659        /* Construct the next VLC */
660        prev= bi->cur_ac;
661        bi->cur_ac = bi->next[prev];
662        if(bi->cur_ac < 64){
663            size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
664        } else {
665            size = 4; vlc = 6; /* End Of Block stamp */
666        }
667     }
668     return pb;
669 }
670
671 static always_inline void dv_set_class_number(DCTELEM* blk, EncBlockInfo* bi,
672                                               const uint8_t* zigzag_scan, const int *weight, int bias)
673 {
674     int i, area;
675     /* We offer two different methods for class number assignment: the
676        method suggested in SMPTE 314M Table 22, and an improved
677        method. The SMPTE method is very conservative; it assigns class
678        3 (i.e. severe quantization) to any block where the largest AC
679        component is greater than 36. ffmpeg's DV encoder tracks AC bit
680        consumption precisely, so there is no need to bias most blocks
681        towards strongly lossy compression. Instead, we assign class 2
682        to most blocks, and use class 3 only when strictly necessary
683        (for blocks whose largest AC component exceeds 255). */
684
685 #if 0 /* SMPTE spec method */
686     static const int classes[] = {12, 24, 36, 0xffff};
687 #else /* improved ffmpeg method */
688     static const int classes[] = {-1, -1, 255, 0xffff};
689 #endif
690     int max=classes[0];
691     int prev=0;
692
693     bi->mb[0] = blk[0];
694
695     for (area = 0; area < 4; area++) {
696        bi->prev[area] = prev;
697        bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
698        for (i=mb_area_start[area]; i<mb_area_start[area+1]; i++) {
699           int level = blk[zigzag_scan[i]];
700
701           if (level+15 > 30U) {
702               bi->sign[i] = (level>>31)&1;
703               /* weigh it and and shift down into range, adding for rounding */
704               /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
705                  AND the 2x doubling of the weights */
706               level = (ABS(level) * weight[i] + (1<<(dv_weight_bits+3))) >> (dv_weight_bits+4);
707               bi->mb[i] = level;
708               if(level>max) max= level;
709               bi->bit_size[area] += dv_rl2vlc_size(i - prev  - 1, level);
710               bi->next[prev]= i;
711               prev= i;
712           }
713        }
714     }
715     bi->next[prev]= i;
716     for(bi->cno = 0; max > classes[bi->cno]; bi->cno++);
717
718     bi->cno += bias;
719
720     if (bi->cno >= 3) {
721         bi->cno = 3;
722         prev=0;
723         i= bi->next[prev];
724         for (area = 0; area < 4; area++) {
725             bi->prev[area] = prev;
726             bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
727             for (; i<mb_area_start[area+1]; i= bi->next[i]) {
728                 bi->mb[i] >>=1;
729
730                 if (bi->mb[i]) {
731                     bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
732                     bi->next[prev]= i;
733                     prev= i;
734                 }
735             }
736         }
737         bi->next[prev]= i;
738     }
739 }
740
741 //FIXME replace this by dsputil
742 #define SC(x, y) ((s[x] - s[y]) ^ ((s[x] - s[y]) >> 7))
743 static always_inline int dv_guess_dct_mode(DCTELEM *blk) {
744     DCTELEM *s;
745     int score88 = 0;
746     int score248 = 0;
747     int i;
748
749     /* Compute 8-8 score (small values give a better chance for 8-8 DCT) */
750     s = blk;
751     for(i=0; i<7; i++) {
752         score88 += SC(0,  8) + SC(1, 9) + SC(2, 10) + SC(3, 11) +
753                    SC(4, 12) + SC(5,13) + SC(6, 14) + SC(7, 15);
754         s += 8;
755     }
756     /* Compute 2-4-8 score (small values give a better chance for 2-4-8 DCT) */
757     s = blk;
758     for(i=0; i<6; i++) {
759         score248 += SC(0, 16) + SC(1,17) + SC(2, 18) + SC(3, 19) +
760                     SC(4, 20) + SC(5,21) + SC(6, 22) + SC(7, 23);
761         s += 8;
762     }
763
764     return (score88 - score248 > -10);
765 }
766
767 static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
768 {
769     int size[5];
770     int i, j, k, a, prev, a2;
771     EncBlockInfo* b;
772
773     size[0] = size[1] = size[2] = size[3] = size[4] = 1<<24;
774     do {
775        b = blks;
776        for (i=0; i<5; i++) {
777           if (!qnos[i])
778               continue;
779
780           qnos[i]--;
781           size[i] = 0;
782           for (j=0; j<6; j++, b++) {
783              for (a=0; a<4; a++) {
784                 if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
785                     b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
786                     b->area_q[a]++;
787                     prev= b->prev[a];
788                     assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
789                     for (k= b->next[prev] ; k<mb_area_start[a+1]; k= b->next[k]) {
790                        b->mb[k] >>= 1;
791                        if (b->mb[k]) {
792                            b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
793                            prev= k;
794                        } else {
795                            if(b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
796                                 for(a2=a+1; b->next[k] >= mb_area_start[a2+1]; a2++)
797                                     b->prev[a2] = prev;
798                                 assert(a2<4);
799                                 assert(b->mb[b->next[k]]);
800                                 b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
801                                                   -dv_rl2vlc_size(b->next[k] -    k - 1, b->mb[b->next[k]]);
802                                 assert(b->prev[a2]==k && (a2+1 >= 4 || b->prev[a2+1]!=k));
803                                 b->prev[a2] = prev;
804                            }
805                            b->next[prev] = b->next[k];
806                        }
807                     }
808                     b->prev[a+1]= prev;
809                 }
810                 size[i] += b->bit_size[a];
811              }
812           }
813           if(vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
814                 return;
815        }
816     } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
817
818
819     for(a=2; a==2 || vs_total_ac_bits < size[0]; a+=a){
820         b = blks;
821         size[0] = 5*6*4; //EOB
822         for (j=0; j<6*5; j++, b++) {
823             prev= b->prev[0];
824             for (k= b->next[prev]; k<64; k= b->next[k]) {
825                 if(b->mb[k] < a && b->mb[k] > -a){
826                     b->next[prev] = b->next[k];
827                 }else{
828                     size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
829                     prev= k;
830                 }
831             }
832         }
833     }
834 }
835
836 static inline void dv_encode_video_segment(DVVideoContext *s,
837                                            uint8_t *dif,
838                                            const uint16_t *mb_pos_ptr)
839 {
840     int mb_index, i, j, v;
841     int mb_x, mb_y, c_offset, linesize;
842     uint8_t*  y_ptr;
843     uint8_t*  data;
844     uint8_t*  ptr;
845     int       do_edge_wrap;
846     DECLARE_ALIGNED_8(DCTELEM, block[64]);
847     EncBlockInfo  enc_blks[5*6];
848     PutBitContext pbs[5*6];
849     PutBitContext* pb;
850     EncBlockInfo* enc_blk;
851     int       vs_bit_size = 0;
852     int       qnos[5];
853
854     assert((((int)block) & 7) == 0);
855
856     enc_blk = &enc_blks[0];
857     pb = &pbs[0];
858     for(mb_index = 0; mb_index < 5; mb_index++) {
859         v = *mb_pos_ptr++;
860         mb_x = v & 0xff;
861         mb_y = v >> 8;
862         if (s->sys->pix_fmt == PIX_FMT_YUV422P) {
863             y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 4);
864         } else { /* 4:1:1 */
865             y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
866         }
867         if (s->sys->pix_fmt == PIX_FMT_YUV420P) {
868             c_offset = (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
869         } else { /* 4:2:2 or 4:1:1 */
870             c_offset = ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8));
871         }
872         do_edge_wrap = 0;
873         qnos[mb_index] = 15; /* No quantization */
874         ptr = dif + mb_index*80 + 4;
875         for(j = 0;j < 6; j++) {
876             int dummy = 0;
877             if (s->sys->pix_fmt == PIX_FMT_YUV422P) { /* 4:2:2 */
878                 if (j == 0 || j == 2) {
879                     /* Y0 Y1 */
880                     data = y_ptr + ((j>>1) * 8);
881                     linesize = s->picture.linesize[0];
882                 } else if (j > 3) {
883                     /* Cr Cb */
884                     data = s->picture.data[6 - j] + c_offset;
885                     linesize = s->picture.linesize[6 - j];
886                 } else {
887                     /* j=1 and j=3 are "dummy" blocks, used for AC data only */
888                     data = 0;
889                     linesize = 0;
890                     dummy = 1;
891                 }
892             } else { /* 4:1:1 or 4:2:0 */
893                 if (j < 4) {  /* Four Y blocks */
894                     /* NOTE: at end of line, the macroblock is handled as 420 */
895                     if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
896                         data = y_ptr + (j * 8);
897                     } else {
898                         data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
899                     }
900                     linesize = s->picture.linesize[0];
901                 } else {      /* Cr and Cb blocks */
902                     /* don't ask Fabrice why they inverted Cb and Cr ! */
903                     data = s->picture.data[6 - j] + c_offset;
904                     linesize = s->picture.linesize[6 - j];
905                     if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
906                         do_edge_wrap = 1;
907                 }
908             }
909
910             /* Everything is set up -- now just copy data -> DCT block */
911             if (do_edge_wrap) {  /* Edge wrap copy: 4x16 -> 8x8 */
912                 uint8_t* d;
913                 DCTELEM *b = block;
914                 for (i=0;i<8;i++) {
915                    d = data + 8 * linesize;
916                    b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
917                    b[4] =    d[0]; b[5] =    d[1]; b[6] =    d[2]; b[7] =    d[3];
918                    data += linesize;
919                    b += 8;
920                 }
921             } else {             /* Simple copy: 8x8 -> 8x8 */
922                 if (!dummy)
923                     s->get_pixels(block, data, linesize);
924             }
925
926             if(s->avctx->flags & CODEC_FLAG_INTERLACED_DCT)
927                 enc_blk->dct_mode = dv_guess_dct_mode(block);
928             else
929                 enc_blk->dct_mode = 0;
930             enc_blk->area_q[0] = enc_blk->area_q[1] = enc_blk->area_q[2] = enc_blk->area_q[3] = 0;
931             enc_blk->partial_bit_count = 0;
932             enc_blk->partial_bit_buffer = 0;
933             enc_blk->cur_ac = 0;
934
935             if (dummy) {
936                 /* We rely on the fact that encoding all zeros leads to an immediate EOB,
937                    which is precisely what the spec calls for in the "dummy" blocks. */
938                 memset(block, 0, sizeof(block));
939             } else {
940                 s->fdct[enc_blk->dct_mode](block);
941             }
942
943             dv_set_class_number(block, enc_blk,
944                                 enc_blk->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct,
945                                 enc_blk->dct_mode ? dv_weight_248 : dv_weight_88,
946                                 j/4);
947
948             init_put_bits(pb, ptr, block_sizes[j]/8);
949             put_bits(pb, 9, (uint16_t)(((enc_blk->mb[0] >> 3) - 1024 + 2) >> 2));
950             put_bits(pb, 1, enc_blk->dct_mode);
951             put_bits(pb, 2, enc_blk->cno);
952
953             vs_bit_size += enc_blk->bit_size[0] + enc_blk->bit_size[1] +
954                            enc_blk->bit_size[2] + enc_blk->bit_size[3];
955             ++enc_blk;
956             ++pb;
957             ptr += block_sizes[j]/8;
958         }
959     }
960
961     if (vs_total_ac_bits < vs_bit_size)
962         dv_guess_qnos(&enc_blks[0], &qnos[0]);
963
964     for (i=0; i<5; i++) {
965        dif[i*80 + 3] = qnos[i];
966     }
967
968     /* First pass over individual cells only */
969     for (j=0; j<5*6; j++)
970        dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
971
972     /* Second pass over each MB space */
973     for (j=0; j<5*6; j+=6) {
974         pb= &pbs[j];
975         for (i=0; i<6; i++) {
976             if (enc_blks[i+j].partial_bit_count)
977                 pb=dv_encode_ac(&enc_blks[i+j], pb, &pbs[j+6]);
978         }
979     }
980
981     /* Third and final pass over the whole vides segment space */
982     pb= &pbs[0];
983     for (j=0; j<5*6; j++) {
984        if (enc_blks[j].partial_bit_count)
985            pb=dv_encode_ac(&enc_blks[j], pb, &pbs[6*5]);
986        if (enc_blks[j].partial_bit_count)
987             av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
988     }
989
990     for (j=0; j<5*6; j++)
991        flush_put_bits(&pbs[j]);
992 }
993
994 static int dv_decode_mt(AVCodecContext *avctx, void* sl)
995 {
996     DVVideoContext *s = avctx->priv_data;
997     int slice = (size_t)sl;
998
999     /* which DIF channel is this? */
1000     int chan = slice / (s->sys->difseg_size * 27);
1001
1002     /* slice within the DIF channel */
1003     int chan_slice = slice % (s->sys->difseg_size * 27);
1004
1005     /* byte offset of this channel's data */
1006     int chan_offset = chan * s->sys->difseg_size * 150 * 80;
1007
1008     dv_decode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
1009                             &s->sys->video_place[slice*5]);
1010     return 0;
1011 }
1012
1013 #ifdef CONFIG_ENCODERS
1014 static int dv_encode_mt(AVCodecContext *avctx, void* sl)
1015 {
1016     DVVideoContext *s = avctx->priv_data;
1017     int slice = (size_t)sl;
1018
1019     /* which DIF channel is this? */
1020     int chan = slice / (s->sys->difseg_size * 27);
1021
1022     /* slice within the DIF channel */
1023     int chan_slice = slice % (s->sys->difseg_size * 27);
1024
1025     /* byte offset of this channel's data */
1026     int chan_offset = chan * s->sys->difseg_size * 150 * 80;
1027
1028     dv_encode_video_segment(s, &s->buf[((chan_slice/27)*6+(chan_slice/3)+chan_slice*5+7)*80 + chan_offset],
1029                             &s->sys->video_place[slice*5]);
1030     return 0;
1031 }
1032 #endif
1033
1034 #ifdef CONFIG_DECODERS
1035 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
1036    144000 bytes for PAL - or twice those for 50Mbps) */
1037 static int dvvideo_decode_frame(AVCodecContext *avctx,
1038                                  void *data, int *data_size,
1039                                  uint8_t *buf, int buf_size)
1040 {
1041     DVVideoContext *s = avctx->priv_data;
1042
1043     s->sys = dv_frame_profile(buf);
1044     if (!s->sys || buf_size < s->sys->frame_size)
1045         return -1; /* NOTE: we only accept several full frames */
1046
1047     if(s->picture.data[0])
1048         avctx->release_buffer(avctx, &s->picture);
1049
1050     s->picture.reference = 0;
1051     s->picture.key_frame = 1;
1052     s->picture.pict_type = FF_I_TYPE;
1053     avctx->pix_fmt = s->sys->pix_fmt;
1054     avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
1055     if(avctx->get_buffer(avctx, &s->picture) < 0) {
1056         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1057         return -1;
1058     }
1059     s->picture.interlaced_frame = 1;
1060     s->picture.top_field_first = 0;
1061
1062     s->buf = buf;
1063     avctx->execute(avctx, dv_decode_mt, (void**)&dv_anchor[0], NULL,
1064                    s->sys->n_difchan * s->sys->difseg_size * 27);
1065
1066     emms_c();
1067
1068     /* return image */
1069     *data_size = sizeof(AVFrame);
1070     *(AVFrame*)data= s->picture;
1071
1072     return s->sys->frame_size;
1073 }
1074 #endif
1075
1076
1077 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, uint8_t* buf)
1078 {
1079     /*
1080      * Here's what SMPTE314M says about these two:
1081      *    (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
1082      *             as track application IDs (APTn = 001, AP1n =
1083      *             001, AP2n = 001, AP3n = 001), if the source signal
1084      *             comes from a digital VCR. If the signal source is
1085      *             unknown, all bits for these data shall be set to 1.
1086      *    (page 12) STYPE: STYPE defines a signal type of video signal
1087      *                     00000b = 4:1:1 compression
1088      *                     00100b = 4:2:2 compression
1089      *                     XXXXXX = Reserved
1090      * Now, I've got two problems with these statements:
1091      *   1. it looks like APT == 111b should be a safe bet, but it isn't.
1092      *      It seems that for PAL as defined in IEC 61834 we have to set
1093      *      APT to 000 and for SMPTE314M to 001.
1094      *   2. It is not at all clear what STYPE is used for 4:2:0 PAL
1095      *      compression scheme (if any).
1096      */
1097     int apt = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1);
1098     int stype = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 4 : 0);
1099
1100     uint8_t aspect = 0;
1101     if((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) == 17) /* 16:9 */
1102         aspect = 0x02;
1103
1104     buf[0] = (uint8_t)pack_id;
1105     switch (pack_id) {
1106     case dv_header525: /* I can't imagine why these two weren't defined as real */
1107     case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
1108           buf[1] = 0xf8 |               /* reserved -- always 1 */
1109                    (apt & 0x07);        /* APT: Track application ID */
1110           buf[2] = (0 << 7)    | /* TF1: audio data is 0 - valid; 1 - invalid */
1111                    (0x0f << 3) | /* reserved -- always 1 */
1112                    (apt & 0x07); /* AP1: Audio application ID */
1113           buf[3] = (0 << 7)    | /* TF2: video data is 0 - valid; 1 - invalid */
1114                    (0x0f << 3) | /* reserved -- always 1 */
1115                    (apt & 0x07); /* AP2: Video application ID */
1116           buf[4] = (0 << 7)    | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
1117                    (0x0f << 3) | /* reserved -- always 1 */
1118                    (apt & 0x07); /* AP3: Subcode application ID */
1119           break;
1120     case dv_video_source:
1121           buf[1] = 0xff; /* reserved -- always 1 */
1122           buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
1123                    (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
1124                    (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
1125                    0xf; /* reserved -- always 1 */
1126           buf[3] = (3 << 6) | /* reserved -- always 1 */
1127                    (c->sys->dsf << 5) | /*  system: 60fields/50fields */
1128                    stype; /* signal type video compression */
1129           buf[4] = 0xff; /* VISC: 0xff -- no information */
1130           break;
1131     case dv_video_control:
1132           buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
1133                    0x3f; /* reserved -- always 1 */
1134           buf[2] = 0xc8 | /* reserved -- always b11001xxx */
1135                    aspect;
1136           buf[3] = (1 << 7) | /* Frame/field flag 1 -- frame, 0 -- field */
1137                    (1 << 6) | /* First/second field flag 0 -- field 2, 1 -- field 1 */
1138                    (1 << 5) | /* Frame change flag 0 -- same picture as before, 1 -- different */
1139                    (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
1140                    0xc; /* reserved -- always b1100 */
1141           buf[4] = 0xff; /* reserved -- always 1 */
1142           break;
1143     default:
1144           buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
1145     }
1146     return 5;
1147 }
1148
1149 static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
1150 {
1151     int chan, i, j, k;
1152
1153     for (chan = 0; chan < c->sys->n_difchan; chan++) {
1154         for (i = 0; i < c->sys->difseg_size; i++) {
1155             memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
1156
1157             /* DV header: 1DIF */
1158             buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
1159             buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
1160             buf += 72; /* unused bytes */
1161
1162             /* DV subcode: 2DIFs */
1163             for (j = 0; j < 2; j++) {
1164                 buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
1165                 for (k = 0; k < 6; k++)
1166                      buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5;
1167                 buf += 29; /* unused bytes */
1168             }
1169
1170             /* DV VAUX: 3DIFS */
1171             for (j = 0; j < 3; j++) {
1172                 buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
1173                 buf += dv_write_pack(dv_video_source,  c, buf);
1174                 buf += dv_write_pack(dv_video_control, c, buf);
1175                 buf += 7*5;
1176                 buf += dv_write_pack(dv_video_source,  c, buf);
1177                 buf += dv_write_pack(dv_video_control, c, buf);
1178                 buf += 4*5 + 2; /* unused bytes */
1179             }
1180
1181             /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
1182             for (j = 0; j < 135; j++) {
1183                 if (j%15 == 0) {
1184                     memset(buf, 0xff, 80);
1185                     buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
1186                     buf += 77; /* audio control & shuffled PCM audio */
1187                 }
1188                 buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
1189                 buf += 77; /* 1 video macro block: 1 bytes control
1190                               4 * 14 bytes Y 8x8 data
1191                               10 bytes Cr 8x8 data
1192                               10 bytes Cb 8x8 data */
1193             }
1194         }
1195     }
1196 }
1197
1198
1199
1200 static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
1201                                 void *data)
1202 {
1203     DVVideoContext *s = c->priv_data;
1204
1205     s->sys = dv_codec_profile(c);
1206     if (!s->sys)
1207         return -1;
1208     if(buf_size < s->sys->frame_size)
1209         return -1;
1210
1211     c->pix_fmt = s->sys->pix_fmt;
1212     s->picture = *((AVFrame *)data);
1213     s->picture.key_frame = 1;
1214     s->picture.pict_type = FF_I_TYPE;
1215
1216     s->buf = buf;
1217     c->execute(c, dv_encode_mt, (void**)&dv_anchor[0], NULL,
1218                s->sys->n_difchan * s->sys->difseg_size * 27);
1219
1220     emms_c();
1221
1222     dv_format_frame(s, buf);
1223
1224     return s->sys->frame_size;
1225 }
1226
1227 static int dvvideo_close(AVCodecContext *c)
1228 {
1229
1230     return 0;
1231 }
1232
1233
1234 #ifdef CONFIG_DVVIDEO_ENCODER
1235 AVCodec dvvideo_encoder = {
1236     "dvvideo",
1237     CODEC_TYPE_VIDEO,
1238     CODEC_ID_DVVIDEO,
1239     sizeof(DVVideoContext),
1240     dvvideo_init,
1241     dvvideo_encode_frame,
1242     dvvideo_close,
1243     NULL,
1244     CODEC_CAP_DR1,
1245     NULL
1246 };
1247 #endif // CONFIG_DVVIDEO_ENCODER
1248
1249 #ifdef CONFIG_DVVIDEO_DECODER
1250 AVCodec dvvideo_decoder = {
1251     "dvvideo",
1252     CODEC_TYPE_VIDEO,
1253     CODEC_ID_DVVIDEO,
1254     sizeof(DVVideoContext),
1255     dvvideo_init,
1256     NULL,
1257     dvvideo_close,
1258     dvvideo_decode_frame,
1259     CODEC_CAP_DR1,
1260     NULL
1261 };
1262 #endif