]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvenc.c
avformat/hlsenc: reindent the code
[ffmpeg] / libavcodec / dvenc.c
1 /*
2  * DV encoder
3  * Copyright (c) 2003 Roman Shaposhnik
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * quant_deadzone code and fixes sponsored by NOA GmbH
22  */
23
24 /**
25  * @file
26  * DV encoder
27  */
28
29 #include "config.h"
30
31 #include "libavutil/attributes.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mem_internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36
37 #include "avcodec.h"
38 #include "dv.h"
39 #include "dv_profile_internal.h"
40 #include "dv_tablegen.h"
41 #include "fdctdsp.h"
42 #include "internal.h"
43 #include "mathops.h"
44 #include "me_cmp.h"
45 #include "pixblockdsp.h"
46 #include "put_bits.h"
47
48 static av_cold int dvvideo_encode_init(AVCodecContext *avctx)
49 {
50     DVVideoContext *s = avctx->priv_data;
51     FDCTDSPContext fdsp;
52     MECmpContext mecc;
53     PixblockDSPContext pdsp;
54     int ret;
55
56     s->sys = av_dv_codec_profile2(avctx->width, avctx->height, avctx->pix_fmt, avctx->time_base);
57     if (!s->sys) {
58         av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
59                                     "Valid DV profiles are:\n",
60                avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
61         ff_dv_print_profiles(avctx, AV_LOG_ERROR);
62         return AVERROR(EINVAL);
63     }
64
65     ret = ff_dv_init_dynamic_tables(s, s->sys);
66     if (ret < 0) {
67         av_log(avctx, AV_LOG_ERROR, "Error initializing work tables.\n");
68         return ret;
69     }
70
71     dv_vlc_map_tableinit();
72
73     memset(&fdsp,0, sizeof(fdsp));
74     memset(&mecc,0, sizeof(mecc));
75     memset(&pdsp,0, sizeof(pdsp));
76     ff_fdctdsp_init(&fdsp, avctx);
77     ff_me_cmp_init(&mecc, avctx);
78     ff_pixblockdsp_init(&pdsp, avctx);
79     ff_set_cmp(&mecc, mecc.ildct_cmp, avctx->ildct_cmp);
80
81     s->get_pixels = pdsp.get_pixels;
82     s->ildct_cmp  = mecc.ildct_cmp[5];
83
84     s->fdct[0]    = fdsp.fdct;
85     s->fdct[1]    = fdsp.fdct248;
86
87     return ff_dvvideo_init(avctx);
88 }
89
90 /* bit budget for AC only in 5 MBs */
91 static const int vs_total_ac_bits_hd = (68 * 6 + 52*2) * 5;
92 static const int vs_total_ac_bits = (100 * 4 + 68 * 2) * 5;
93 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
94
95 #if CONFIG_SMALL
96 /* Convert run and level (where level != 0) pair into VLC, returning bit size */
97 static av_always_inline int dv_rl2vlc(int run, int level, int sign,
98                                       uint32_t *vlc)
99 {
100     int size;
101     if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
102         *vlc = dv_vlc_map[run][level].vlc | sign;
103         size = dv_vlc_map[run][level].size;
104     } else {
105         if (level < DV_VLC_MAP_LEV_SIZE) {
106             *vlc = dv_vlc_map[0][level].vlc | sign;
107             size = dv_vlc_map[0][level].size;
108         } else {
109             *vlc = 0xfe00 | (level << 1) | sign;
110             size = 16;
111         }
112         if (run) {
113             *vlc |= ((run < 16) ? dv_vlc_map[run - 1][0].vlc :
114                      (0x1f80 | (run - 1))) << size;
115             size +=  (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
116         }
117     }
118
119     return size;
120 }
121
122 static av_always_inline int dv_rl2vlc_size(int run, int level)
123 {
124     int size;
125
126     if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
127         size = dv_vlc_map[run][level].size;
128     } else {
129         size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
130         if (run)
131             size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
132     }
133     return size;
134 }
135 #else
136 static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t *vlc)
137 {
138     *vlc = dv_vlc_map[run][l].vlc | sign;
139     return dv_vlc_map[run][l].size;
140 }
141
142 static av_always_inline int dv_rl2vlc_size(int run, int l)
143 {
144     return dv_vlc_map[run][l].size;
145 }
146 #endif
147
148 typedef struct EncBlockInfo {
149     int      area_q[4];
150     int      bit_size[4];
151     int      prev[5];
152     int      cur_ac;
153     int      cno;
154     int      dct_mode;
155     int16_t  mb[64];
156     uint8_t  next[64];
157     uint8_t  sign[64];
158     uint8_t  partial_bit_count;
159     uint32_t partial_bit_buffer; /* we can't use uint16_t here */
160     /* used by DV100 only: a copy of the weighted and classified but
161        not-yet-quantized AC coefficients. This is necessary for
162        re-quantizing at different steps. */
163     int16_t  save[64];
164     int      min_qlevel; /* DV100 only: minimum qlevel (for AC coefficients >255) */
165 } EncBlockInfo;
166
167 static av_always_inline PutBitContext *dv_encode_ac(EncBlockInfo *bi,
168                                                     PutBitContext *pb_pool,
169                                                     PutBitContext *pb_end)
170 {
171     int prev, bits_left;
172     PutBitContext *pb = pb_pool;
173     int size          = bi->partial_bit_count;
174     uint32_t vlc      = bi->partial_bit_buffer;
175
176     bi->partial_bit_count  =
177     bi->partial_bit_buffer = 0;
178     for (;;) {
179         /* Find suitable storage space */
180         for (; size > (bits_left = put_bits_left(pb)); pb++) {
181             if (bits_left) {
182                 size -= bits_left;
183                 put_bits(pb, bits_left, vlc >> size);
184                 vlc = av_mod_uintp2(vlc, size);
185             }
186             if (pb + 1 >= pb_end) {
187                 bi->partial_bit_count  = size;
188                 bi->partial_bit_buffer = vlc;
189                 return pb;
190             }
191         }
192
193         /* Store VLC */
194         put_bits(pb, size, vlc);
195
196         if (bi->cur_ac >= 64)
197             break;
198
199         /* Construct the next VLC */
200         prev       = bi->cur_ac;
201         bi->cur_ac = bi->next[prev];
202         if (bi->cur_ac < 64) {
203             size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac],
204                              bi->sign[bi->cur_ac], &vlc);
205         } else {
206             size = 4;
207             vlc  = 6; /* End Of Block stamp */
208         }
209     }
210     return pb;
211 }
212
213 static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data,
214                                               ptrdiff_t linesize)
215 {
216     if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
217         int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
218         if (ps > 0) {
219             int is = s->ildct_cmp(NULL, data,            NULL, linesize * 2, 4) +
220                      s->ildct_cmp(NULL, data + linesize, NULL, linesize * 2, 4);
221             return ps > is;
222         }
223     }
224
225     return 0;
226 }
227
228 static const int dv_weight_bits = 18;
229 static const int dv_weight_88[64] = {
230     131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
231     237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
232     224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
233     212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
234     206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
235     200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
236     174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
237     170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
238 };
239 static const int dv_weight_248[64] = {
240     131072, 262144, 257107, 257107, 242189, 242189, 242189, 242189,
241     237536, 237536, 229376, 229376, 200636, 200636, 224973, 224973,
242     223754, 223754, 235923, 235923, 229376, 229376, 217965, 217965,
243     211916, 211916, 196781, 196781, 185364, 185364, 206433, 206433,
244     211916, 211916, 222935, 222935, 200636, 200636, 205964, 205964,
245     200704, 200704, 180568, 180568, 175557, 175557, 195068, 195068,
246     185364, 185364, 188995, 188995, 174606, 174606, 175557, 175557,
247     170627, 170627, 153560, 153560, 165371, 165371, 144651, 144651,
248 };
249
250 /* setting this to 1 results in a faster codec but
251  * somewhat lower image quality */
252 #define DV100_SACRIFICE_QUALITY_FOR_SPEED 1
253 #define DV100_ENABLE_FINER 1
254
255 /* pack combination of QNO and CNO into a single 8-bit value */
256 #define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno))
257 #define DV100_QLEVEL_QNO(qlevel) (qlevel>>2)
258 #define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3)
259
260 #define DV100_NUM_QLEVELS 31
261
262 /* The quantization step is determined by a combination of QNO and
263    CNO. We refer to these combinations as "qlevels" (this term is our
264    own, it's not mentioned in the spec). We use CNO, a multiplier on
265    the quantization step, to "fill in the gaps" between quantization
266    steps associated with successive values of QNO. e.g. there is no
267    QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to
268    get the same result. The table below encodes combinations of QNO
269    and CNO in order of increasing quantization coarseness. */
270 static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS] = {
271     DV100_MAKE_QLEVEL( 1,0), //  1*1= 1
272     DV100_MAKE_QLEVEL( 1,0), //  1*1= 1
273     DV100_MAKE_QLEVEL( 2,0), //  2*1= 2
274     DV100_MAKE_QLEVEL( 3,0), //  3*1= 3
275     DV100_MAKE_QLEVEL( 4,0), //  4*1= 4
276     DV100_MAKE_QLEVEL( 5,0), //  5*1= 5
277     DV100_MAKE_QLEVEL( 6,0), //  6*1= 6
278     DV100_MAKE_QLEVEL( 7,0), //  7*1= 7
279     DV100_MAKE_QLEVEL( 8,0), //  8*1= 8
280     DV100_MAKE_QLEVEL( 5,1), //  5*2=10
281     DV100_MAKE_QLEVEL( 6,1), //  6*2=12
282     DV100_MAKE_QLEVEL( 7,1), //  7*2=14
283     DV100_MAKE_QLEVEL( 9,0), // 16*1=16
284     DV100_MAKE_QLEVEL(10,0), // 18*1=18
285     DV100_MAKE_QLEVEL(11,0), // 20*1=20
286     DV100_MAKE_QLEVEL(12,0), // 22*1=22
287     DV100_MAKE_QLEVEL(13,0), // 24*1=24
288     DV100_MAKE_QLEVEL(14,0), // 28*1=28
289     DV100_MAKE_QLEVEL( 9,1), // 16*2=32
290     DV100_MAKE_QLEVEL(10,1), // 18*2=36
291     DV100_MAKE_QLEVEL(11,1), // 20*2=40
292     DV100_MAKE_QLEVEL(12,1), // 22*2=44
293     DV100_MAKE_QLEVEL(13,1), // 24*2=48
294     DV100_MAKE_QLEVEL(15,0), // 52*1=52
295     DV100_MAKE_QLEVEL(14,1), // 28*2=56
296     DV100_MAKE_QLEVEL( 9,2), // 16*4=64
297     DV100_MAKE_QLEVEL(10,2), // 18*4=72
298     DV100_MAKE_QLEVEL(11,2), // 20*4=80
299     DV100_MAKE_QLEVEL(12,2), // 22*4=88
300     DV100_MAKE_QLEVEL(13,2), // 24*4=96
301     // ...
302     DV100_MAKE_QLEVEL(15,3), // 52*8=416
303 };
304
305 static const int dv100_min_bias = 0;
306 static const int dv100_chroma_bias = 0;
307 static const int dv100_starting_qno = 1;
308
309 #if DV100_SACRIFICE_QUALITY_FOR_SPEED
310 static const int dv100_qlevel_inc = 4;
311 #else
312 static const int dv100_qlevel_inc = 1;
313 #endif
314
315 // 1/qstep, shifted up by 16 bits
316 static const int dv100_qstep_bits = 16;
317 static const int dv100_qstep_inv[16] = {
318         65536,  65536,  32768,  21845,  16384,  13107,  10923,  9362,  8192,  4096,  3641,  3277,  2979,  2731,  2341,  1260,
319 };
320
321 /* DV100 weights are pre-zigzagged, inverted and multiplied by 2^(dv100_weight_shift)
322    (in DV100 the AC components are divided by the spec weights) */
323 static const int dv100_weight_shift = 16;
324 static const int dv_weight_1080[2][64] = {
325     { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
326       58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
327       55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
328       26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
329       25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
330       24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
331       21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
332       10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
333     { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
334       41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
335       40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
336       26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
337       25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
338       12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
339       10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
340       5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
341 };
342
343 static const int dv_weight_720[2][64] = {
344     { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
345       58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
346       55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
347       26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
348       25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
349       16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
350       10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
351       5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
352     { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
353       29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
354       27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
355       13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
356       12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
357       6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
358       5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
359       2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
360 };
361
362 static av_always_inline int dv_set_class_number_sd(DVVideoContext *s,
363                                                    int16_t *blk, EncBlockInfo *bi,
364                                                    const uint8_t *zigzag_scan,
365                                                    const int *weight, int bias)
366 {
367     int i, area;
368     /* We offer two different methods for class number assignment: the
369      * method suggested in SMPTE 314M Table 22, and an improved
370      * method. The SMPTE method is very conservative; it assigns class
371      * 3 (i.e. severe quantization) to any block where the largest AC
372      * component is greater than 36. FFmpeg's DV encoder tracks AC bit
373      * consumption precisely, so there is no need to bias most blocks
374      * towards strongly lossy compression. Instead, we assign class 2
375      * to most blocks, and use class 3 only when strictly necessary
376      * (for blocks whose largest AC component exceeds 255). */
377
378 #if 0 /* SMPTE spec method */
379     static const int classes[] = { 12, 24, 36, 0xffff };
380 #else /* improved FFmpeg method */
381     static const int classes[] = { -1, -1, 255, 0xffff };
382 #endif
383     int max  = classes[0];
384     int prev = 0;
385     const unsigned deadzone = s->quant_deadzone;
386     const unsigned threshold = 2 * deadzone;
387
388     bi->mb[0] = blk[0];
389
390     for (area = 0; area < 4; area++) {
391         bi->prev[area]     = prev;
392         bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
393         for (i = mb_area_start[area]; i < mb_area_start[area + 1]; i++) {
394             int level = blk[zigzag_scan[i]];
395
396             if (level + deadzone > threshold) {
397                 bi->sign[i] = (level >> 31) & 1;
398                 /* Weight it and shift down into range, adding for rounding.
399                  * The extra division by a factor of 2^4 reverses the 8x
400                  * expansion of the DCT AND the 2x doubling of the weights. */
401                 level     = (FFABS(level) * weight[i] + (1 << (dv_weight_bits + 3))) >>
402                             (dv_weight_bits + 4);
403                 if (!level)
404                     continue;
405                 bi->mb[i] = level;
406                 if (level > max)
407                     max = level;
408                 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
409                 bi->next[prev]      = i;
410                 prev                = i;
411             }
412         }
413     }
414     bi->next[prev] = i;
415     for (bi->cno = 0; max > classes[bi->cno]; bi->cno++)
416         ;
417
418     bi->cno += bias;
419
420     if (bi->cno >= 3) {
421         bi->cno = 3;
422         prev    = 0;
423         i       = bi->next[prev];
424         for (area = 0; area < 4; area++) {
425             bi->prev[area]     = prev;
426             bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
427             for (; i < mb_area_start[area + 1]; i = bi->next[i]) {
428                 bi->mb[i] >>= 1;
429
430                 if (bi->mb[i]) {
431                     bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
432                     bi->next[prev]      = i;
433                     prev                = i;
434                 }
435             }
436         }
437         bi->next[prev] = i;
438     }
439
440     return bi->bit_size[0] + bi->bit_size[1] +
441            bi->bit_size[2] + bi->bit_size[3];
442 }
443
444 /* this function just copies the DCT coefficients and performs
445    the initial (non-)quantization. */
446 static inline void dv_set_class_number_hd(DVVideoContext *s,
447                                           int16_t *blk, EncBlockInfo *bi,
448                                           const uint8_t *zigzag_scan,
449                                           const int *weight, int bias)
450 {
451     int i, max = 0;
452
453     /* the first quantization (none at all) */
454     bi->area_q[0] = 1;
455
456     /* weigh AC components and store to save[] */
457     /* (i=0 is the DC component; we only include it to make the
458        number of loop iterations even, for future possible SIMD optimization) */
459     for (i = 0; i < 64; i += 2) {
460         int level0, level1;
461
462         /* get the AC component (in zig-zag order) */
463         level0 = blk[zigzag_scan[i+0]];
464         level1 = blk[zigzag_scan[i+1]];
465
466         /* extract sign and make it the lowest bit */
467         bi->sign[i+0] = (level0>>31)&1;
468         bi->sign[i+1] = (level1>>31)&1;
469
470         /* take absolute value of the level */
471         level0 = FFABS(level0);
472         level1 = FFABS(level1);
473
474         /* weigh it */
475         level0 = (level0*weight[i+0] + 4096 + (1<<17)) >> 18;
476         level1 = (level1*weight[i+1] + 4096 + (1<<17)) >> 18;
477
478         /* save unquantized value */
479         bi->save[i+0] = level0;
480         bi->save[i+1] = level1;
481
482          /* find max component */
483         if (bi->save[i+0] > max)
484             max = bi->save[i+0];
485         if (bi->save[i+1] > max)
486             max = bi->save[i+1];
487     }
488
489     /* copy DC component */
490     bi->mb[0] = blk[0];
491
492     /* the EOB code is 4 bits */
493     bi->bit_size[0] = 4;
494     bi->bit_size[1] = bi->bit_size[2] = bi->bit_size[3] = 0;
495
496     /* ensure that no AC coefficients are cut off */
497     bi->min_qlevel = ((max+256) >> 8);
498
499     bi->area_q[0] = 25; /* set to an "impossible" value */
500     bi->cno = 0;
501 }
502
503 static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize,
504                                               DVVideoContext *s, int chroma)
505 {
506     LOCAL_ALIGNED_16(int16_t, blk, [64]);
507
508     bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
509     bi->partial_bit_count = 0;
510     bi->partial_bit_buffer = 0;
511     bi->cur_ac = 0;
512
513     if (data) {
514         if (DV_PROFILE_IS_HD(s->sys)) {
515             s->get_pixels(blk, data, linesize * (1 << bi->dct_mode));
516             s->fdct[0](blk);
517         } else {
518             bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
519             s->get_pixels(blk, data, linesize);
520             s->fdct[bi->dct_mode](blk);
521         }
522     } else {
523         /* We rely on the fact that encoding all zeros leads to an immediate EOB,
524            which is precisely what the spec calls for in the "dummy" blocks. */
525         memset(blk, 0, 64*sizeof(*blk));
526         bi->dct_mode = 0;
527     }
528
529     if (DV_PROFILE_IS_HD(s->sys)) {
530         const int *weights;
531         if (s->sys->height == 1080) {
532             weights = dv_weight_1080[chroma];
533         } else { /* 720p */
534             weights = dv_weight_720[chroma];
535         }
536         dv_set_class_number_hd(s, blk, bi,
537                                ff_zigzag_direct,
538                                weights,
539                                dv100_min_bias+chroma*dv100_chroma_bias);
540     } else {
541         dv_set_class_number_sd(s, blk, bi,
542                                bi->dct_mode ? ff_dv_zigzag248_direct : ff_zigzag_direct,
543                                bi->dct_mode ? dv_weight_248 : dv_weight_88,
544                                chroma);
545     }
546
547     return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
548 }
549
550 /* DV100 quantize
551    Perform quantization by divinding the AC component by the qstep.
552    As an optimization we use a fixed-point integer multiply instead
553    of a divide. */
554 static av_always_inline int dv100_quantize(int level, int qsinv)
555 {
556     /* this code is equivalent to */
557     /* return (level + qs/2) / qs; */
558
559     return (level * qsinv + 1024 + (1<<(dv100_qstep_bits-1))) >> dv100_qstep_bits;
560
561     /* the extra +1024 is needed to make the rounding come out right. */
562
563     /* I (DJM) have verified that the results are exactly the same as
564        division for level 0-2048 at all QNOs. */
565 }
566
567 static int dv100_actual_quantize(EncBlockInfo *b, int qlevel)
568 {
569     int prev, k, qsinv;
570
571     int qno = DV100_QLEVEL_QNO(dv100_qlevels[qlevel]);
572     int cno = DV100_QLEVEL_CNO(dv100_qlevels[qlevel]);
573
574     if (b->area_q[0] == qno && b->cno == cno)
575         return b->bit_size[0];
576
577     qsinv = dv100_qstep_inv[qno];
578
579     /* record the new qstep */
580     b->area_q[0] = qno;
581     b->cno = cno;
582
583     /* reset encoded size (EOB = 4 bits) */
584     b->bit_size[0] = 4;
585
586     /* visit nonzero components and quantize */
587     prev = 0;
588     for (k = 1; k < 64; k++) {
589         /* quantize */
590         int ac = dv100_quantize(b->save[k], qsinv) >> cno;
591         if (ac) {
592             if (ac > 255)
593                 ac = 255;
594             b->mb[k] = ac;
595             b->bit_size[0] += dv_rl2vlc_size(k - prev - 1, ac);
596             b->next[prev] = k;
597             prev = k;
598         }
599     }
600     b->next[prev] = k;
601
602     return b->bit_size[0];
603 }
604
605 static inline void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)
606 {
607     EncBlockInfo *b;
608     int min_qlevel[5];
609     int qlevels[5];
610     int size[5];
611     int i, j;
612     /* cache block sizes at hypothetical qlevels */
613     uint16_t size_cache[5*8][DV100_NUM_QLEVELS] = {{0}};
614
615     /* get minimum qlevels */
616     for (i = 0; i < 5; i++) {
617         min_qlevel[i] = 1;
618         for (j = 0; j < 8; j++) {
619             if (blks[8*i+j].min_qlevel > min_qlevel[i])
620                 min_qlevel[i] = blks[8*i+j].min_qlevel;
621         }
622     }
623
624     /* initialize sizes */
625     for (i = 0; i < 5; i++) {
626         qlevels[i] = dv100_starting_qno;
627         if (qlevels[i] < min_qlevel[i])
628             qlevels[i] = min_qlevel[i];
629
630         qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
631         size[i] = 0;
632         for (j = 0; j < 8; j++) {
633             size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(&blks[8*i+j], qlevels[i]);
634             size[i] += size_cache[8*i+j][qlevels[i]];
635         }
636     }
637
638     /* must we go coarser? */
639     if (size[0]+size[1]+size[2]+size[3]+size[4] > vs_total_ac_bits_hd) {
640         int largest = size[0] % 5; /* 'random' number */
641         int qlevels_done = 0;
642
643         do {
644             /* find the macroblock with the lowest qlevel */
645             for (i = 0; i < 5; i++) {
646                 if (qlevels[i] < qlevels[largest])
647                     largest = i;
648             }
649
650             i = largest;
651             /* ensure that we don't enter infinite loop */
652             largest = (largest+1) % 5;
653
654             /* quantize a little bit more */
655             qlevels[i] += dv100_qlevel_inc;
656             if (qlevels[i] > DV100_NUM_QLEVELS-1) {
657                 qlevels[i] = DV100_NUM_QLEVELS-1;
658                 qlevels_done++;
659             }
660
661             qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
662             size[i] = 0;
663
664             /* for each block */
665             b = &blks[8*i];
666             for (j = 0; j < 8; j++, b++) {
667                 /* accumulate block size into macroblock */
668                 if(size_cache[8*i+j][qlevels[i]] == 0) {
669                     /* it is safe to use actual_quantize() here because we only go from finer to coarser,
670                        and it saves the final actual_quantize() down below */
671                     size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
672                 }
673                 size[i] += size_cache[8*i+j][qlevels[i]];
674             } /* for each block */
675
676         } while (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4] && qlevels_done < 5);
677
678         // can we go finer?
679     } else if (DV100_ENABLE_FINER &&
680                size[0]+size[1]+size[2]+size[3]+size[4] < vs_total_ac_bits_hd) {
681         int save_qlevel;
682         int largest = size[0] % 5; /* 'random' number */
683
684         while (qlevels[0] > min_qlevel[0] ||
685                qlevels[1] > min_qlevel[1] ||
686                qlevels[2] > min_qlevel[2] ||
687                qlevels[3] > min_qlevel[3] ||
688                qlevels[4] > min_qlevel[4]) {
689
690             /* find the macroblock with the highest qlevel */
691             for (i = 0; i < 5; i++) {
692                 if (qlevels[i] > min_qlevel[i] && qlevels[i] > qlevels[largest])
693                     largest = i;
694             }
695
696             i = largest;
697
698             /* ensure that we don't enter infinite loop */
699             largest = (largest+1) % 5;
700
701             if (qlevels[i] <= min_qlevel[i]) {
702                 /* can't unquantize any more */
703                 continue;
704             }
705             /* quantize a little bit less */
706             save_qlevel = qlevels[i];
707             qlevels[i] -= dv100_qlevel_inc;
708             if (qlevels[i] < min_qlevel[i])
709                 qlevels[i] = min_qlevel[i];
710
711             qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
712
713             size[i] = 0;
714
715             /* for each block */
716             b = &blks[8*i];
717             for (j = 0; j < 8; j++, b++) {
718                 /* accumulate block size into macroblock */
719                 if(size_cache[8*i+j][qlevels[i]] == 0) {
720                     size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
721                 }
722                 size[i] += size_cache[8*i+j][qlevels[i]];
723             } /* for each block */
724
725             /* did we bust the limit? */
726             if (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4]) {
727                 /* go back down and exit */
728                 qlevels[i] = save_qlevel;
729                 qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
730                 break;
731             }
732         }
733     }
734
735     /* now do the actual quantization */
736     for (i = 0; i < 5; i++) {
737         /* for each block */
738         b = &blks[8*i];
739         size[i] = 0;
740         for (j = 0; j < 8; j++, b++) {
741             /* accumulate block size into macroblock */
742             size[i] += dv100_actual_quantize(b, qlevels[i]);
743         } /* for each block */
744     }
745 }
746
747 static inline void dv_guess_qnos(EncBlockInfo *blks, int *qnos)
748 {
749     int size[5];
750     int i, j, k, a, prev, a2;
751     EncBlockInfo *b;
752
753     size[0] =
754     size[1] =
755     size[2] =
756     size[3] =
757     size[4] = 1 << 24;
758     do {
759         b = blks;
760         for (i = 0; i < 5; i++) {
761             if (!qnos[i])
762                 continue;
763
764             qnos[i]--;
765             size[i] = 0;
766             for (j = 0; j < 6; j++, b++) {
767                 for (a = 0; a < 4; a++) {
768                     if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
769                         b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
770                         b->area_q[a]++;
771                         prev = b->prev[a];
772                         av_assert2(b->next[prev] >= mb_area_start[a + 1] || b->mb[prev]);
773                         for (k = b->next[prev]; k < mb_area_start[a + 1]; k = b->next[k]) {
774                             b->mb[k] >>= 1;
775                             if (b->mb[k]) {
776                                 b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
777                                 prev            = k;
778                             } else {
779                                 if (b->next[k] >= mb_area_start[a + 1] && b->next[k] < 64) {
780                                     for (a2 = a + 1; b->next[k] >= mb_area_start[a2 + 1]; a2++)
781                                         b->prev[a2] = prev;
782                                     av_assert2(a2 < 4);
783                                     av_assert2(b->mb[b->next[k]]);
784                                     b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) -
785                                                        dv_rl2vlc_size(b->next[k] - k    - 1, b->mb[b->next[k]]);
786                                     av_assert2(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2 + 1] != k));
787                                     b->prev[a2] = prev;
788                                 }
789                                 b->next[prev] = b->next[k];
790                             }
791                         }
792                         b->prev[a + 1] = prev;
793                     }
794                     size[i] += b->bit_size[a];
795                 }
796             }
797             if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
798                 return;
799         }
800     } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]);
801
802     for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a) {
803         b       = blks;
804         size[0] = 5 * 6 * 4; // EOB
805         for (j = 0; j < 6 * 5; j++, b++) {
806             prev = b->prev[0];
807             for (k = b->next[prev]; k < 64; k = b->next[k]) {
808                 if (b->mb[k] < a && b->mb[k] > -a) {
809                     b->next[prev] = b->next[k];
810                 } else {
811                     size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
812                     prev     = k;
813                 }
814             }
815         }
816     }
817 }
818
819 /* update all cno values into the blocks, over-writing the old values without
820    touching anything else. (only used for DV100) */
821 static inline void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)
822 {
823     uint8_t *data;
824     int mb_index, i;
825
826     for (mb_index = 0; mb_index < 5; mb_index++) {
827         data = dif + mb_index*80 + 4;
828         for (i = 0; i < profile->bpm; i++) {
829             /* zero out the class number */
830             data[1] &= 0xCF;
831             /* add the new one */
832             data[1] |= blk[profile->bpm*mb_index+i].cno << 4;
833
834             data += profile->block_sizes[i] >> 3;
835         }
836     }
837 }
838
839 static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
840 {
841     DVVideoContext *s = avctx->priv_data;
842     DVwork_chunk *work_chunk = arg;
843     int mb_index, i, j;
844     int mb_x, mb_y, c_offset;
845     ptrdiff_t linesize, y_stride;
846     uint8_t *y_ptr;
847     uint8_t *dif, *p;
848     LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
849     EncBlockInfo enc_blks[5 * DV_MAX_BPM];
850     PutBitContext pbs[5 * DV_MAX_BPM];
851     PutBitContext *pb;
852     EncBlockInfo *enc_blk;
853     int vs_bit_size = 0;
854     int qnos[5];
855     int *qnosp = &qnos[0];
856
857     p = dif = &s->buf[work_chunk->buf_offset * 80];
858     enc_blk = &enc_blks[0];
859     for (mb_index = 0; mb_index < 5; mb_index++) {
860         dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
861
862         qnos[mb_index] = DV_PROFILE_IS_HD(s->sys) ? 1 : 15;
863
864         y_ptr    = s->frame->data[0] + (mb_y * s->frame->linesize[0] + mb_x) * 8;
865         linesize = s->frame->linesize[0];
866
867         if (s->sys->height == 1080 && mb_y < 134)
868             enc_blk->dct_mode = dv_guess_dct_mode(s, y_ptr, linesize);
869         else
870             enc_blk->dct_mode = 0;
871         for (i = 1; i < 8; i++)
872             enc_blk[i].dct_mode = enc_blk->dct_mode;
873
874         /* initializing luminance blocks */
875         if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P)                      ||
876             (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
877             (s->sys->height >= 720 && mb_y != 134)) {
878             y_stride = s->frame->linesize[0] * (1 << (3*!enc_blk->dct_mode));
879         } else {
880             y_stride = 16;
881         }
882         y_ptr    = s->frame->data[0] +
883                    (mb_y * s->frame->linesize[0] + mb_x) * 8;
884         linesize = s->frame->linesize[0];
885
886         if (s->sys->video_stype == 4) { /* SD 422 */
887             vs_bit_size +=
888                 dv_init_enc_block(enc_blk + 0, y_ptr,                linesize, s, 0) +
889                 dv_init_enc_block(enc_blk + 1, NULL,                 linesize, s, 0) +
890                 dv_init_enc_block(enc_blk + 2, y_ptr + 8,            linesize, s, 0) +
891                 dv_init_enc_block(enc_blk + 3, NULL,                 linesize, s, 0);
892         } else {
893             vs_bit_size +=
894                 dv_init_enc_block(enc_blk + 0, y_ptr,                linesize, s, 0) +
895                 dv_init_enc_block(enc_blk + 1, y_ptr + 8,            linesize, s, 0) +
896                 dv_init_enc_block(enc_blk + 2, y_ptr +     y_stride, linesize, s, 0) +
897                 dv_init_enc_block(enc_blk + 3, y_ptr + 8 + y_stride, linesize, s, 0);
898         }
899         enc_blk += 4;
900
901         /* initializing chrominance blocks */
902         c_offset = ((mb_y >>  (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
903                     (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) * 8;
904         for (j = 2; j; j--) {
905             uint8_t *c_ptr = s->frame->data[j] + c_offset;
906             linesize = s->frame->linesize[j];
907             y_stride = (mb_y == 134) ? 8 : (s->frame->linesize[j] * (1 << (3*!enc_blk->dct_mode)));
908             if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
909                 uint8_t *d;
910                 uint8_t *b = scratch;
911                 for (i = 0; i < 8; i++) {
912                     d      = c_ptr + linesize * 8;
913                     b[0]   = c_ptr[0];
914                     b[1]   = c_ptr[1];
915                     b[2]   = c_ptr[2];
916                     b[3]   = c_ptr[3];
917                     b[4]   = d[0];
918                     b[5]   = d[1];
919                     b[6]   = d[2];
920                     b[7]   = d[3];
921                     c_ptr += linesize;
922                     b     += 16;
923                 }
924                 c_ptr    = scratch;
925                 linesize = 16;
926             }
927
928             vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr, linesize, s, 1);
929             if (s->sys->bpm == 8)
930                 vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride,
931                                                  linesize, s, 1);
932         }
933     }
934
935     if (DV_PROFILE_IS_HD(s->sys)) {
936         /* unconditional */
937         dv_guess_qnos_hd(&enc_blks[0], qnosp);
938     } else if (vs_total_ac_bits < vs_bit_size) {
939         dv_guess_qnos(&enc_blks[0], qnosp);
940     }
941
942     /* DIF encoding process */
943     for (j = 0; j < 5 * s->sys->bpm;) {
944         int start_mb = j;
945
946         p[3] = *qnosp++;
947         p += 4;
948
949         /* First pass over individual cells only */
950         for (i = 0; i < s->sys->bpm; i++, j++) {
951             int sz = s->sys->block_sizes[i] >> 3;
952
953             init_put_bits(&pbs[j], p, sz);
954             put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2);
955             put_bits(&pbs[j], 1, DV_PROFILE_IS_HD(s->sys) && i ? 1 : enc_blks[j].dct_mode);
956             put_bits(&pbs[j], 2, enc_blks[j].cno);
957
958             dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j + 1]);
959             p += sz;
960         }
961
962         /* Second pass over each MB space */
963         pb = &pbs[start_mb];
964         for (i = 0; i < s->sys->bpm; i++)
965             if (enc_blks[start_mb + i].partial_bit_count)
966                 pb = dv_encode_ac(&enc_blks[start_mb + i], pb,
967                                   &pbs[start_mb + s->sys->bpm]);
968     }
969
970     /* Third and final pass over the whole video segment space */
971     pb = &pbs[0];
972     for (j = 0; j < 5 * s->sys->bpm; j++) {
973         if (enc_blks[j].partial_bit_count)
974             pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm * 5]);
975         if (enc_blks[j].partial_bit_count)
976             av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n");
977     }
978
979     for (j = 0; j < 5 * s->sys->bpm; j++) {
980         int pos;
981         int size = pbs[j].size_in_bits >> 3;
982         flush_put_bits(&pbs[j]);
983         pos = put_bits_count(&pbs[j]) >> 3;
984         if (pos > size) {
985             av_log(avctx, AV_LOG_ERROR,
986                    "bitstream written beyond buffer size\n");
987             return -1;
988         }
989         memset(pbs[j].buf + pos, 0xff, size - pos);
990     }
991
992     if (DV_PROFILE_IS_HD(s->sys))
993         dv_revise_cnos(dif, enc_blks, s->sys);
994
995     return 0;
996 }
997
998 static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
999                                 uint8_t *buf)
1000 {
1001     /*
1002      * Here's what SMPTE314M says about these two:
1003      *    (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
1004      *             as track application IDs (APTn = 001, AP1n =
1005      *             001, AP2n = 001, AP3n = 001), if the source signal
1006      *             comes from a digital VCR. If the signal source is
1007      *             unknown, all bits for these data shall be set to 1.
1008      *    (page 12) STYPE: STYPE defines a signal type of video signal
1009      *                     00000b = 4:1:1 compression
1010      *                     00100b = 4:2:2 compression
1011      *                     XXXXXX = Reserved
1012      * Now, I've got two problems with these statements:
1013      *   1. it looks like APT == 111b should be a safe bet, but it isn't.
1014      *      It seems that for PAL as defined in IEC 61834 we have to set
1015      *      APT to 000 and for SMPTE314M to 001.
1016      *   2. It is not at all clear what STYPE is used for 4:2:0 PAL
1017      *      compression scheme (if any).
1018      */
1019     uint8_t aspect = 0;
1020     int apt = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1);
1021     int fs;
1022
1023     if (c->avctx->height >= 720)
1024         fs = c->avctx->height == 720 || c->frame->top_field_first ? 0x40 : 0x00;
1025     else
1026         fs = c->frame->top_field_first ? 0x00 : 0x40;
1027
1028     if (DV_PROFILE_IS_HD(c->sys) ||
1029         (int)(av_q2d(c->avctx->sample_aspect_ratio) *
1030               c->avctx->width / c->avctx->height * 10) >= 17)
1031         /* HD formats are always 16:9 */
1032         aspect = 0x02;
1033
1034     buf[0] = (uint8_t) pack_id;
1035     switch (pack_id) {
1036     case dv_header525: /* I can't imagine why these two weren't defined as real */
1037     case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
1038         buf[1] =  0xf8       | /* reserved -- always 1 */
1039                  (apt & 0x07); /* APT: Track application ID */
1040         buf[2] = (0    << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
1041                  (0x0f << 3) | /* reserved -- always 1 */
1042                  (apt & 0x07); /* AP1: Audio application ID */
1043         buf[3] = (0    << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
1044                  (0x0f << 3) | /* reserved -- always 1 */
1045                  (apt & 0x07); /* AP2: Video application ID */
1046         buf[4] = (0    << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
1047                  (0x0f << 3) | /* reserved -- always 1 */
1048                  (apt & 0x07); /* AP3: Subcode application ID */
1049         break;
1050     case dv_video_source:
1051         buf[1] = 0xff;         /* reserved -- always 1 */
1052         buf[2] = (1 << 7) |    /* B/W: 0 - b/w, 1 - color */
1053                  (1 << 6) |    /* following CLF is valid - 0, invalid - 1 */
1054                  (3 << 4) |    /* CLF: color frames ID (see ITU-R BT.470-4) */
1055                  0xf;          /* reserved -- always 1 */
1056         buf[3] = (3 << 6)           | /* reserved -- always 1 */
1057                  (c->sys->dsf << 5) | /*  system: 60fields/50fields */
1058                  c->sys->video_stype; /* signal type video compression */
1059         buf[4] = 0xff;         /* VISC: 0xff -- no information */
1060         break;
1061     case dv_video_control:
1062         buf[1] = (0 << 6) |    /* Copy generation management (CGMS) 0 -- free */
1063                  0x3f;         /* reserved -- always 1 */
1064         buf[2] = 0xc8 |        /* reserved -- always b11001xxx */
1065                  aspect;
1066         buf[3] = (1 << 7) |    /* frame/field flag 1 -- frame, 0 -- field */
1067                  fs       |    /* first/second field flag 0 -- field 2, 1 -- field 1 */
1068                  (1 << 5) |    /* frame change flag 0 -- same picture as before, 1 -- different */
1069                  (1 << 4) |    /* 1 - interlaced, 0 - noninterlaced */
1070                  0xc;          /* reserved -- always b1100 */
1071         buf[4] = 0xff;         /* reserved -- always 1 */
1072         break;
1073     default:
1074         buf[1] =
1075         buf[2] =
1076         buf[3] =
1077         buf[4] = 0xff;
1078     }
1079     return 5;
1080 }
1081
1082 static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
1083                                   uint8_t seq_num, uint8_t dif_num,
1084                                   uint8_t *buf)
1085 {
1086     int fsc = chan_num & 1;
1087     int fsp = 1 - (chan_num >> 1);
1088
1089     buf[0] = (uint8_t) t;      /* Section type */
1090     buf[1] = (seq_num  << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
1091              (fsc << 3) |      /* FSC: for 50 and 100Mb/s 0 - first channel; 1 - second */
1092              (fsp << 2) |      /* FSP: for 100Mb/s 1 - channels 0-1; 0 - channels 2-3 */
1093              3;                /* reserved -- always 1 */
1094     buf[2] = dif_num;          /* DIF block number Video: 0-134, Audio: 0-8 */
1095     return 3;
1096 }
1097
1098 static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf)
1099 {
1100     if (syb_num == 0 || syb_num == 6) {
1101         buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1102                  (0  << 4) | /* AP3 (Subcode application ID) */
1103                  0x0f;       /* reserved -- always 1 */
1104     } else if (syb_num == 11) {
1105         buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1106                  0x7f;       /* reserved -- always 1 */
1107     } else {
1108         buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
1109                  (0  << 4) | /* APT (Track application ID) */
1110                  0x0f;       /* reserved -- always 1 */
1111     }
1112     buf[1] = 0xf0 |            /* reserved -- always 1 */
1113              (syb_num & 0x0f); /* SSYB number 0 - 11   */
1114     buf[2] = 0xff;             /* reserved -- always 1 */
1115     return 3;
1116 }
1117
1118 static void dv_format_frame(DVVideoContext *c, uint8_t *buf)
1119 {
1120     int chan, i, j, k;
1121     /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */
1122     int chan_offset = 2*(c->sys->height == 720 && c->avctx->frame_number & 1);
1123
1124     for (chan = 0; chan < c->sys->n_difchan; chan++) {
1125         for (i = 0; i < c->sys->difseg_size; i++) {
1126             memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
1127
1128             /* DV header: 1DIF */
1129             buf += dv_write_dif_id(dv_sect_header, chan+chan_offset, i, 0, buf);
1130             buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525),
1131                                  c, buf);
1132             buf += 72; /* unused bytes */
1133
1134             /* DV subcode: 2DIFs */
1135             for (j = 0; j < 2; j++) {
1136                 buf += dv_write_dif_id(dv_sect_subcode, chan+chan_offset, i, j, buf);
1137                 for (k = 0; k < 6; k++)
1138                     buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size / 2), buf) + 5;
1139                 buf += 29; /* unused bytes */
1140             }
1141
1142             /* DV VAUX: 3DIFS */
1143             for (j = 0; j < 3; j++) {
1144                 buf += dv_write_dif_id(dv_sect_vaux, chan+chan_offset, i, j, buf);
1145                 buf += dv_write_pack(dv_video_source,  c, buf);
1146                 buf += dv_write_pack(dv_video_control, c, buf);
1147                 buf += 7 * 5;
1148                 buf += dv_write_pack(dv_video_source,  c, buf);
1149                 buf += dv_write_pack(dv_video_control, c, buf);
1150                 buf += 4 * 5 + 2; /* unused bytes */
1151             }
1152
1153             /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
1154             for (j = 0; j < 135; j++) {
1155                 if (j % 15 == 0) {
1156                     memset(buf, 0xff, 80);
1157                     buf += dv_write_dif_id(dv_sect_audio, chan+chan_offset, i, j/15, buf);
1158                     buf += 77; /* audio control & shuffled PCM audio */
1159                 }
1160                 buf += dv_write_dif_id(dv_sect_video, chan+chan_offset, i, j, buf);
1161                 buf += 77; /* 1 video macroblock: 1 bytes control
1162                             * 4 * 14 bytes Y 8x8 data
1163                             * 10 bytes Cr 8x8 data
1164                             * 10 bytes Cb 8x8 data */
1165             }
1166         }
1167     }
1168 }
1169
1170 static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt,
1171                                 const AVFrame *frame, int *got_packet)
1172 {
1173     DVVideoContext *s = c->priv_data;
1174     int ret;
1175
1176     if ((ret = ff_alloc_packet2(c, pkt, s->sys->frame_size, 0)) < 0)
1177         return ret;
1178
1179     c->pix_fmt                = s->sys->pix_fmt;
1180     s->frame                  = frame;
1181 #if FF_API_CODED_FRAME
1182 FF_DISABLE_DEPRECATION_WARNINGS
1183     c->coded_frame->key_frame = 1;
1184     c->coded_frame->pict_type = AV_PICTURE_TYPE_I;
1185 FF_ENABLE_DEPRECATION_WARNINGS
1186 #endif
1187     s->buf = pkt->data;
1188
1189     dv_format_frame(s, pkt->data);
1190
1191     c->execute(c, dv_encode_video_segment, s->work_chunks, NULL,
1192                dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1193
1194     emms_c();
1195
1196     pkt->flags |= AV_PKT_FLAG_KEY;
1197     *got_packet = 1;
1198
1199     return 0;
1200 }
1201
1202 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1203 #define OFFSET(x) offsetof(DVVideoContext, x)
1204 static const AVOption dv_options[] = {
1205     { "quant_deadzone",        "Quantizer dead zone",    OFFSET(quant_deadzone),       AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024, VE },
1206     { NULL },
1207 };
1208
1209 static const AVClass dvvideo_encode_class = {
1210     .class_name = "dvvideo encoder",
1211     .item_name  = av_default_item_name,
1212     .option     = dv_options,
1213     .version    = LIBAVUTIL_VERSION_INT,
1214 };
1215
1216 AVCodec ff_dvvideo_encoder = {
1217     .name           = "dvvideo",
1218     .long_name      = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
1219     .type           = AVMEDIA_TYPE_VIDEO,
1220     .id             = AV_CODEC_ID_DVVIDEO,
1221     .priv_data_size = sizeof(DVVideoContext),
1222     .init           = dvvideo_encode_init,
1223     .encode2        = dvvideo_encode_frame,
1224     .capabilities   = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
1225     .pix_fmts       = (const enum AVPixelFormat[]) {
1226         AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P,
1227         AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
1228     },
1229     .priv_class     = &dvvideo_encode_class,
1230 };