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 #include "libavutil/thread.h"
37 
38 #include "avcodec.h"
39 #include "codec_internal.h"
40 #include "dv.h"
41 #include "dv_profile_internal.h"
42 #include "dv_tablegen.h"
43 #include "encode.h"
44 #include "fdctdsp.h"
45 #include "mathops.h"
46 #include "me_cmp.h"
47 #include "pixblockdsp.h"
48 #include "put_bits.h"
49 
dvvideo_encode_init(AVCodecContext *avctx)50 static av_cold int dvvideo_encode_init(AVCodecContext *avctx)
51 {
52     DVVideoContext *s = avctx->priv_data;
53     FDCTDSPContext fdsp;
54     MECmpContext mecc;
55     PixblockDSPContext pdsp;
56     int ret;
57 
58     s->sys = av_dv_codec_profile2(avctx->width, avctx->height, avctx->pix_fmt, avctx->time_base);
59     if (!s->sys) {
60         av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
61                                     "Valid DV profiles are:\n",
62                avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
63         ff_dv_print_profiles(avctx, AV_LOG_ERROR);
64         return AVERROR(EINVAL);
65     }
66 
67     ret = ff_dv_init_dynamic_tables(s, s->sys);
68     if (ret < 0) {
69         av_log(avctx, AV_LOG_ERROR, "Error initializing work tables.\n");
70         return ret;
71     }
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     ret = ff_set_cmp(&mecc, mecc.ildct_cmp, avctx->ildct_cmp);
80     if (ret < 0)
81         return AVERROR(EINVAL);
82 
83     s->get_pixels = pdsp.get_pixels;
84     s->ildct_cmp  = mecc.ildct_cmp[5];
85 
86     s->fdct[0]    = fdsp.fdct;
87     s->fdct[1]    = fdsp.fdct248;
88 
89 #if !CONFIG_HARDCODED_TABLES
90     {
91         static AVOnce init_static_once = AV_ONCE_INIT;
92         ff_thread_once(&init_static_once, dv_vlc_map_tableinit);
93     }
94 #endif
95 
96     return ff_dvvideo_init(avctx);
97 }
98 
99 /* bit budget for AC only in 5 MBs */
100 static const int vs_total_ac_bits_hd = (68 * 6 + 52*2) * 5;
101 static const int vs_total_ac_bits = (100 * 4 + 68 * 2) * 5;
102 static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
103 
104 #if CONFIG_SMALL
105 /* Convert run and level (where level != 0) pair into VLC, returning bit size */
dv_rl2vlc(int run, int level, int sign, uint32_t *vlc)106 static av_always_inline int dv_rl2vlc(int run, int level, int sign,
107                                       uint32_t *vlc)
108 {
109     int size;
110     if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
111         *vlc = dv_vlc_map[run][level].vlc | sign;
112         size = dv_vlc_map[run][level].size;
113     } else {
114         if (level < DV_VLC_MAP_LEV_SIZE) {
115             *vlc = dv_vlc_map[0][level].vlc | sign;
116             size = dv_vlc_map[0][level].size;
117         } else {
118             *vlc = 0xfe00 | (level << 1) | sign;
119             size = 16;
120         }
121         if (run) {
122             *vlc |= ((run < 16) ? dv_vlc_map[run - 1][0].vlc :
123                      (0x1f80 | (run - 1))) << size;
124             size +=  (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
125         }
126     }
127 
128     return size;
129 }
130 
dv_rl2vlc_size(int run, int level)131 static av_always_inline int dv_rl2vlc_size(int run, int level)
132 {
133     int size;
134 
135     if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
136         size = dv_vlc_map[run][level].size;
137     } else {
138         size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
139         if (run)
140             size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
141     }
142     return size;
143 }
144 #else
dv_rl2vlc(int run, int l, int sign, uint32_t *vlc)145 static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t *vlc)
146 {
147     *vlc = dv_vlc_map[run][l].vlc | sign;
148     return dv_vlc_map[run][l].size;
149 }
150 
dv_rl2vlc_size(int run, int l)151 static av_always_inline int dv_rl2vlc_size(int run, int l)
152 {
153     return dv_vlc_map[run][l].size;
154 }
155 #endif
156 
157 typedef struct EncBlockInfo {
158     int      area_q[4];
159     int      bit_size[4];
160     int      prev[5];
161     int      cur_ac;
162     int      cno;
163     int      dct_mode;
164     int16_t  mb[64];
165     uint8_t  next[64];
166     uint8_t  sign[64];
167     uint8_t  partial_bit_count;
168     uint32_t partial_bit_buffer; /* we can't use uint16_t here */
169     /* used by DV100 only: a copy of the weighted and classified but
170        not-yet-quantized AC coefficients. This is necessary for
171        re-quantizing at different steps. */
172     int16_t  save[64];
173     int      min_qlevel; /* DV100 only: minimum qlevel (for AC coefficients >255) */
174 } EncBlockInfo;
175 
dv_encode_ac(EncBlockInfo *bi, PutBitContext *pb_pool, PutBitContext *pb_end)176 static av_always_inline PutBitContext *dv_encode_ac(EncBlockInfo *bi,
177                                                     PutBitContext *pb_pool,
178                                                     PutBitContext *pb_end)
179 {
180     int prev, bits_left;
181     PutBitContext *pb = pb_pool;
182     int size          = bi->partial_bit_count;
183     uint32_t vlc      = bi->partial_bit_buffer;
184 
185     bi->partial_bit_count  =
186     bi->partial_bit_buffer = 0;
187     for (;;) {
188         /* Find suitable storage space */
189         for (; size > (bits_left = put_bits_left(pb)); pb++) {
190             if (bits_left) {
191                 size -= bits_left;
192                 put_bits(pb, bits_left, vlc >> size);
193                 vlc = av_mod_uintp2(vlc, size);
194             }
195             if (pb + 1 >= pb_end) {
196                 bi->partial_bit_count  = size;
197                 bi->partial_bit_buffer = vlc;
198                 return pb;
199             }
200         }
201 
202         /* Store VLC */
203         put_bits(pb, size, vlc);
204 
205         if (bi->cur_ac >= 64)
206             break;
207 
208         /* Construct the next VLC */
209         prev       = bi->cur_ac;
210         bi->cur_ac = bi->next[prev];
211         if (bi->cur_ac < 64) {
212             size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac],
213                              bi->sign[bi->cur_ac], &vlc);
214         } else {
215             size = 4;
216             vlc  = 6; /* End Of Block stamp */
217         }
218     }
219     return pb;
220 }
221 
dv_guess_dct_mode(DVVideoContext *s, uint8_t *data, ptrdiff_t linesize)222 static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data,
223                                               ptrdiff_t linesize)
224 {
225     if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
226         int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
227         if (ps > 0) {
228             int is = s->ildct_cmp(NULL, data,            NULL, linesize * 2, 4) +
229                      s->ildct_cmp(NULL, data + linesize, NULL, linesize * 2, 4);
230             return ps > is;
231         }
232     }
233 
234     return 0;
235 }
236 
237 static const int dv_weight_bits = 18;
238 static const int dv_weight_88[64] = {
239     131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
240     237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
241     224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
242     212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
243     206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
244     200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
245     174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
246     170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
247 };
248 static const int dv_weight_248[64] = {
249     131072, 262144, 257107, 257107, 242189, 242189, 242189, 242189,
250     237536, 237536, 229376, 229376, 200636, 200636, 224973, 224973,
251     223754, 223754, 235923, 235923, 229376, 229376, 217965, 217965,
252     211916, 211916, 196781, 196781, 185364, 185364, 206433, 206433,
253     211916, 211916, 222935, 222935, 200636, 200636, 205964, 205964,
254     200704, 200704, 180568, 180568, 175557, 175557, 195068, 195068,
255     185364, 185364, 188995, 188995, 174606, 174606, 175557, 175557,
256     170627, 170627, 153560, 153560, 165371, 165371, 144651, 144651,
257 };
258 
259 /* setting this to 1 results in a faster codec but
260  * somewhat lower image quality */
261 #define DV100_SACRIFICE_QUALITY_FOR_SPEED 1
262 #define DV100_ENABLE_FINER 1
263 
264 /* pack combination of QNO and CNO into a single 8-bit value */
265 #define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno))
266 #define DV100_QLEVEL_QNO(qlevel) (qlevel>>2)
267 #define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3)
268 
269 #define DV100_NUM_QLEVELS 31
270 
271 /* The quantization step is determined by a combination of QNO and
272    CNO. We refer to these combinations as "qlevels" (this term is our
273    own, it's not mentioned in the spec). We use CNO, a multiplier on
274    the quantization step, to "fill in the gaps" between quantization
275    steps associated with successive values of QNO. e.g. there is no
276    QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to
277    get the same result. The table below encodes combinations of QNO
278    and CNO in order of increasing quantization coarseness. */
279 static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS] = {
280     DV100_MAKE_QLEVEL( 1,0), //  1*1= 1
281     DV100_MAKE_QLEVEL( 1,0), //  1*1= 1
282     DV100_MAKE_QLEVEL( 2,0), //  2*1= 2
283     DV100_MAKE_QLEVEL( 3,0), //  3*1= 3
284     DV100_MAKE_QLEVEL( 4,0), //  4*1= 4
285     DV100_MAKE_QLEVEL( 5,0), //  5*1= 5
286     DV100_MAKE_QLEVEL( 6,0), //  6*1= 6
287     DV100_MAKE_QLEVEL( 7,0), //  7*1= 7
288     DV100_MAKE_QLEVEL( 8,0), //  8*1= 8
289     DV100_MAKE_QLEVEL( 5,1), //  5*2=10
290     DV100_MAKE_QLEVEL( 6,1), //  6*2=12
291     DV100_MAKE_QLEVEL( 7,1), //  7*2=14
292     DV100_MAKE_QLEVEL( 9,0), // 16*1=16
293     DV100_MAKE_QLEVEL(10,0), // 18*1=18
294     DV100_MAKE_QLEVEL(11,0), // 20*1=20
295     DV100_MAKE_QLEVEL(12,0), // 22*1=22
296     DV100_MAKE_QLEVEL(13,0), // 24*1=24
297     DV100_MAKE_QLEVEL(14,0), // 28*1=28
298     DV100_MAKE_QLEVEL( 9,1), // 16*2=32
299     DV100_MAKE_QLEVEL(10,1), // 18*2=36
300     DV100_MAKE_QLEVEL(11,1), // 20*2=40
301     DV100_MAKE_QLEVEL(12,1), // 22*2=44
302     DV100_MAKE_QLEVEL(13,1), // 24*2=48
303     DV100_MAKE_QLEVEL(15,0), // 52*1=52
304     DV100_MAKE_QLEVEL(14,1), // 28*2=56
305     DV100_MAKE_QLEVEL( 9,2), // 16*4=64
306     DV100_MAKE_QLEVEL(10,2), // 18*4=72
307     DV100_MAKE_QLEVEL(11,2), // 20*4=80
308     DV100_MAKE_QLEVEL(12,2), // 22*4=88
309     DV100_MAKE_QLEVEL(13,2), // 24*4=96
310     // ...
311     DV100_MAKE_QLEVEL(15,3), // 52*8=416
312 };
313 
314 static const int dv100_min_bias = 0;
315 static const int dv100_chroma_bias = 0;
316 static const int dv100_starting_qno = 1;
317 
318 #if DV100_SACRIFICE_QUALITY_FOR_SPEED
319 static const int dv100_qlevel_inc = 4;
320 #else
321 static const int dv100_qlevel_inc = 1;
322 #endif
323 
324 // 1/qstep, shifted up by 16 bits
325 static const int dv100_qstep_bits = 16;
326 static const int dv100_qstep_inv[16] = {
327         65536,  65536,  32768,  21845,  16384,  13107,  10923,  9362,  8192,  4096,  3641,  3277,  2979,  2731,  2341,  1260,
328 };
329 
330 /* DV100 weights are pre-zigzagged, inverted and multiplied by 2^16
331    (in DV100 the AC components are divided by the spec weights) */
332 static const int dv_weight_1080[2][64] = {
333     { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
334       58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
335       55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
336       26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
337       25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
338       24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
339       21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
340       10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
341     { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
342       41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
343       40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
344       26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
345       25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
346       12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
347       10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
348       5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
349 };
350 
351 static const int dv_weight_720[2][64] = {
352     { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
353       58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
354       55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
355       26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
356       25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
357       16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
358       10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
359       5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
360     { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
361       29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
362       27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
363       13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
364       12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
365       6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
366       5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
367       2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
368 };
369 
dv_set_class_number_sd(DVVideoContext *s, int16_t *blk, EncBlockInfo *bi, const uint8_t *zigzag_scan, const int *weight, int bias)370 static av_always_inline int dv_set_class_number_sd(DVVideoContext *s,
371                                                    int16_t *blk, EncBlockInfo *bi,
372                                                    const uint8_t *zigzag_scan,
373                                                    const int *weight, int bias)
374 {
375     int i, area;
376     /* We offer two different methods for class number assignment: the
377      * method suggested in SMPTE 314M Table 22, and an improved
378      * method. The SMPTE method is very conservative; it assigns class
379      * 3 (i.e. severe quantization) to any block where the largest AC
380      * component is greater than 36. FFmpeg's DV encoder tracks AC bit
381      * consumption precisely, so there is no need to bias most blocks
382      * towards strongly lossy compression. Instead, we assign class 2
383      * to most blocks, and use class 3 only when strictly necessary
384      * (for blocks whose largest AC component exceeds 255). */
385 
386 #if 0 /* SMPTE spec method */
387     static const int classes[] = { 12, 24, 36, 0xffff };
388 #else /* improved FFmpeg method */
389     static const int classes[] = { -1, -1, 255, 0xffff };
390 #endif
391     int max  = classes[0];
392     int prev = 0;
393     const unsigned deadzone = s->quant_deadzone;
394     const unsigned threshold = 2 * deadzone;
395 
396     bi->mb[0] = blk[0];
397 
398     for (area = 0; area < 4; area++) {
399         bi->prev[area]     = prev;
400         bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
401         for (i = mb_area_start[area]; i < mb_area_start[area + 1]; i++) {
402             int level = blk[zigzag_scan[i]];
403 
404             if (level + deadzone > threshold) {
405                 bi->sign[i] = (level >> 31) & 1;
406                 /* Weight it and shift down into range, adding for rounding.
407                  * The extra division by a factor of 2^4 reverses the 8x
408                  * expansion of the DCT AND the 2x doubling of the weights. */
409                 level     = (FFABS(level) * weight[i] + (1 << (dv_weight_bits + 3))) >>
410                             (dv_weight_bits + 4);
411                 if (!level)
412                     continue;
413                 bi->mb[i] = level;
414                 if (level > max)
415                     max = level;
416                 bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
417                 bi->next[prev]      = i;
418                 prev                = i;
419             }
420         }
421     }
422     bi->next[prev] = i;
423     for (bi->cno = 0; max > classes[bi->cno]; bi->cno++)
424         ;
425 
426     bi->cno += bias;
427 
428     if (bi->cno >= 3) {
429         bi->cno = 3;
430         prev    = 0;
431         i       = bi->next[prev];
432         for (area = 0; area < 4; area++) {
433             bi->prev[area]     = prev;
434             bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
435             for (; i < mb_area_start[area + 1]; i = bi->next[i]) {
436                 bi->mb[i] >>= 1;
437 
438                 if (bi->mb[i]) {
439                     bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
440                     bi->next[prev]      = i;
441                     prev                = i;
442                 }
443             }
444         }
445         bi->next[prev] = i;
446     }
447 
448     return bi->bit_size[0] + bi->bit_size[1] +
449            bi->bit_size[2] + bi->bit_size[3];
450 }
451 
452 /* this function just copies the DCT coefficients and performs
453    the initial (non-)quantization. */
dv_set_class_number_hd(DVVideoContext *s, int16_t *blk, EncBlockInfo *bi, const uint8_t *zigzag_scan, const int *weight, int bias)454 static inline void dv_set_class_number_hd(DVVideoContext *s,
455                                           int16_t *blk, EncBlockInfo *bi,
456                                           const uint8_t *zigzag_scan,
457                                           const int *weight, int bias)
458 {
459     int i, max = 0;
460 
461     /* the first quantization (none at all) */
462     bi->area_q[0] = 1;
463 
464     /* weigh AC components and store to save[] */
465     /* (i=0 is the DC component; we only include it to make the
466        number of loop iterations even, for future possible SIMD optimization) */
467     for (i = 0; i < 64; i += 2) {
468         int level0, level1;
469 
470         /* get the AC component (in zig-zag order) */
471         level0 = blk[zigzag_scan[i+0]];
472         level1 = blk[zigzag_scan[i+1]];
473 
474         /* extract sign and make it the lowest bit */
475         bi->sign[i+0] = (level0>>31)&1;
476         bi->sign[i+1] = (level1>>31)&1;
477 
478         /* take absolute value of the level */
479         level0 = FFABS(level0);
480         level1 = FFABS(level1);
481 
482         /* weigh it */
483         level0 = (level0*weight[i+0] + 4096 + (1<<17)) >> 18;
484         level1 = (level1*weight[i+1] + 4096 + (1<<17)) >> 18;
485 
486         /* save unquantized value */
487         bi->save[i+0] = level0;
488         bi->save[i+1] = level1;
489 
490          /* find max component */
491         if (bi->save[i+0] > max)
492             max = bi->save[i+0];
493         if (bi->save[i+1] > max)
494             max = bi->save[i+1];
495     }
496 
497     /* copy DC component */
498     bi->mb[0] = blk[0];
499 
500     /* the EOB code is 4 bits */
501     bi->bit_size[0] = 4;
502     bi->bit_size[1] = bi->bit_size[2] = bi->bit_size[3] = 0;
503 
504     /* ensure that no AC coefficients are cut off */
505     bi->min_qlevel = ((max+256) >> 8);
506 
507     bi->area_q[0] = 25; /* set to an "impossible" value */
508     bi->cno = 0;
509 }
510 
dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int chroma)511 static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize,
512                                               DVVideoContext *s, int chroma)
513 {
514     LOCAL_ALIGNED_16(int16_t, blk, [64]);
515 
516     bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
517     bi->partial_bit_count = 0;
518     bi->partial_bit_buffer = 0;
519     bi->cur_ac = 0;
520 
521     if (data) {
522         if (DV_PROFILE_IS_HD(s->sys)) {
523             s->get_pixels(blk, data, linesize * (1 << bi->dct_mode));
524             s->fdct[0](blk);
525         } else {
526             bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
527             s->get_pixels(blk, data, linesize);
528             s->fdct[bi->dct_mode](blk);
529         }
530     } else {
531         /* We rely on the fact that encoding all zeros leads to an immediate EOB,
532            which is precisely what the spec calls for in the "dummy" blocks. */
533         memset(blk, 0, 64*sizeof(*blk));
534         bi->dct_mode = 0;
535     }
536 
537     if (DV_PROFILE_IS_HD(s->sys)) {
538         const int *weights;
539         if (s->sys->height == 1080) {
540             weights = dv_weight_1080[chroma];
541         } else { /* 720p */
542             weights = dv_weight_720[chroma];
543         }
544         dv_set_class_number_hd(s, blk, bi,
545                                ff_zigzag_direct,
546                                weights,
547                                dv100_min_bias+chroma*dv100_chroma_bias);
548     } else {
549         dv_set_class_number_sd(s, blk, bi,
550                                bi->dct_mode ? ff_dv_zigzag248_direct : ff_zigzag_direct,
551                                bi->dct_mode ? dv_weight_248 : dv_weight_88,
552                                chroma);
553     }
554 
555     return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
556 }
557 
558 /* DV100 quantize
559    Perform quantization by divinding the AC component by the qstep.
560    As an optimization we use a fixed-point integer multiply instead
561    of a divide. */
dv100_quantize(int level, int qsinv)562 static av_always_inline int dv100_quantize(int level, int qsinv)
563 {
564     /* this code is equivalent to */
565     /* return (level + qs/2) / qs; */
566 
567     return (level * qsinv + 1024 + (1<<(dv100_qstep_bits-1))) >> dv100_qstep_bits;
568 
569     /* the extra +1024 is needed to make the rounding come out right. */
570 
571     /* I (DJM) have verified that the results are exactly the same as
572        division for level 0-2048 at all QNOs. */
573 }
574 
dv100_actual_quantize(EncBlockInfo *b, int qlevel)575 static int dv100_actual_quantize(EncBlockInfo *b, int qlevel)
576 {
577     int prev, k, qsinv;
578 
579     int qno = DV100_QLEVEL_QNO(dv100_qlevels[qlevel]);
580     int cno = DV100_QLEVEL_CNO(dv100_qlevels[qlevel]);
581 
582     if (b->area_q[0] == qno && b->cno == cno)
583         return b->bit_size[0];
584 
585     qsinv = dv100_qstep_inv[qno];
586 
587     /* record the new qstep */
588     b->area_q[0] = qno;
589     b->cno = cno;
590 
591     /* reset encoded size (EOB = 4 bits) */
592     b->bit_size[0] = 4;
593 
594     /* visit nonzero components and quantize */
595     prev = 0;
596     for (k = 1; k < 64; k++) {
597         /* quantize */
598         int ac = dv100_quantize(b->save[k], qsinv) >> cno;
599         if (ac) {
600             if (ac > 255)
601                 ac = 255;
602             b->mb[k] = ac;
603             b->bit_size[0] += dv_rl2vlc_size(k - prev - 1, ac);
604             b->next[prev] = k;
605             prev = k;
606         }
607     }
608     b->next[prev] = k;
609 
610     return b->bit_size[0];
611 }
612 
dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)613 static inline void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)
614 {
615     EncBlockInfo *b;
616     int min_qlevel[5];
617     int qlevels[5];
618     int size[5];
619     int i, j;
620     /* cache block sizes at hypothetical qlevels */
621     uint16_t size_cache[5*8][DV100_NUM_QLEVELS] = {{0}};
622 
623     /* get minimum qlevels */
624     for (i = 0; i < 5; i++) {
625         min_qlevel[i] = 1;
626         for (j = 0; j < 8; j++) {
627             if (blks[8*i+j].min_qlevel > min_qlevel[i])
628                 min_qlevel[i] = blks[8*i+j].min_qlevel;
629         }
630     }
631 
632     /* initialize sizes */
633     for (i = 0; i < 5; i++) {
634         qlevels[i] = dv100_starting_qno;
635         if (qlevels[i] < min_qlevel[i])
636             qlevels[i] = min_qlevel[i];
637 
638         qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
639         size[i] = 0;
640         for (j = 0; j < 8; j++) {
641             size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(&blks[8*i+j], qlevels[i]);
642             size[i] += size_cache[8*i+j][qlevels[i]];
643         }
644     }
645 
646     /* must we go coarser? */
647     if (size[0]+size[1]+size[2]+size[3]+size[4] > vs_total_ac_bits_hd) {
648         int largest = size[0] % 5; /* 'random' number */
649         int qlevels_done = 0;
650 
651         do {
652             /* find the macroblock with the lowest qlevel */
653             for (i = 0; i < 5; i++) {
654                 if (qlevels[i] < qlevels[largest])
655                     largest = i;
656             }
657 
658             i = largest;
659             /* ensure that we don't enter infinite loop */
660             largest = (largest+1) % 5;
661 
662             /* quantize a little bit more */
663             qlevels[i] += dv100_qlevel_inc;
664             if (qlevels[i] > DV100_NUM_QLEVELS-1) {
665                 qlevels[i] = DV100_NUM_QLEVELS-1;
666                 qlevels_done++;
667             }
668 
669             qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
670             size[i] = 0;
671 
672             /* for each block */
673             b = &blks[8*i];
674             for (j = 0; j < 8; j++, b++) {
675                 /* accumulate block size into macroblock */
676                 if(size_cache[8*i+j][qlevels[i]] == 0) {
677                     /* it is safe to use actual_quantize() here because we only go from finer to coarser,
678                        and it saves the final actual_quantize() down below */
679                     size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
680                 }
681                 size[i] += size_cache[8*i+j][qlevels[i]];
682             } /* for each block */
683 
684         } while (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4] && qlevels_done < 5);
685 
686         // can we go finer?
687     } else if (DV100_ENABLE_FINER &&
688                size[0]+size[1]+size[2]+size[3]+size[4] < vs_total_ac_bits_hd) {
689         int save_qlevel;
690         int largest = size[0] % 5; /* 'random' number */
691 
692         while (qlevels[0] > min_qlevel[0] ||
693                qlevels[1] > min_qlevel[1] ||
694                qlevels[2] > min_qlevel[2] ||
695                qlevels[3] > min_qlevel[3] ||
696                qlevels[4] > min_qlevel[4]) {
697 
698             /* find the macroblock with the highest qlevel */
699             for (i = 0; i < 5; i++) {
700                 if (qlevels[i] > min_qlevel[i] && qlevels[i] > qlevels[largest])
701                     largest = i;
702             }
703 
704             i = largest;
705 
706             /* ensure that we don't enter infinite loop */
707             largest = (largest+1) % 5;
708 
709             if (qlevels[i] <= min_qlevel[i]) {
710                 /* can't unquantize any more */
711                 continue;
712             }
713             /* quantize a little bit less */
714             save_qlevel = qlevels[i];
715             qlevels[i] -= dv100_qlevel_inc;
716             if (qlevels[i] < min_qlevel[i])
717                 qlevels[i] = min_qlevel[i];
718 
719             qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
720 
721             size[i] = 0;
722 
723             /* for each block */
724             b = &blks[8*i];
725             for (j = 0; j < 8; j++, b++) {
726                 /* accumulate block size into macroblock */
727                 if(size_cache[8*i+j][qlevels[i]] == 0) {
728                     size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
729                 }
730                 size[i] += size_cache[8*i+j][qlevels[i]];
731             } /* for each block */
732 
733             /* did we bust the limit? */
734             if (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4]) {
735                 /* go back down and exit */
736                 qlevels[i] = save_qlevel;
737                 qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
738                 break;
739             }
740         }
741     }
742 
743     /* now do the actual quantization */
744     for (i = 0; i < 5; i++) {
745         /* for each block */
746         b = &blks[8*i];
747         size[i] = 0;
748         for (j = 0; j < 8; j++, b++) {
749             /* accumulate block size into macroblock */
750             size[i] += dv100_actual_quantize(b, qlevels[i]);
751         } /* for each block */
752     }
753 }
754 
dv_guess_qnos(EncBlockInfo *blks, int *qnos)755 static inline void dv_guess_qnos(EncBlockInfo *blks, int *qnos)
756 {
757     int size[5];
758     int i, j, k, a, prev, a2;
759     EncBlockInfo *b;
760 
761     size[0] =
762     size[1] =
763     size[2] =
764     size[3] =
765     size[4] = 1 << 24;
766     do {
767         b = blks;
768         for (i = 0; i < 5; i++) {
769             if (!qnos[i])
770                 continue;
771 
772             qnos[i]--;
773             size[i] = 0;
774             for (j = 0; j < 6; j++, b++) {
775                 for (a = 0; a < 4; a++) {
776                     if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
777                         b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
778                         b->area_q[a]++;
779                         prev = b->prev[a];
780                         av_assert2(b->next[prev] >= mb_area_start[a + 1] || b->mb[prev]);
781                         for (k = b->next[prev]; k < mb_area_start[a + 1]; k = b->next[k]) {
782                             b->mb[k] >>= 1;
783                             if (b->mb[k]) {
784                                 b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
785                                 prev            = k;
786                             } else {
787                                 if (b->next[k] >= mb_area_start[a + 1] && b->next[k] < 64) {
788                                     for (a2 = a + 1; b->next[k] >= mb_area_start[a2 + 1]; a2++)
789                                         b->prev[a2] = prev;
790                                     av_assert2(a2 < 4);
791                                     av_assert2(b->mb[b->next[k]]);
792                                     b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) -
793                                                        dv_rl2vlc_size(b->next[k] - k    - 1, b->mb[b->next[k]]);
794                                     av_assert2(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2 + 1] != k));
795                                     b->prev[a2] = prev;
796                                 }
797                                 b->next[prev] = b->next[k];
798                             }
799                         }
800                         b->prev[a + 1] = prev;
801                     }
802                     size[i] += b->bit_size[a];
803                 }
804             }
805             if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
806                 return;
807         }
808     } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]);
809 
810     for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a) {
811         b       = blks;
812         size[0] = 5 * 6 * 4; // EOB
813         for (j = 0; j < 6 * 5; j++, b++) {
814             prev = b->prev[0];
815             for (k = b->next[prev]; k < 64; k = b->next[k]) {
816                 if (b->mb[k] < a && b->mb[k] > -a) {
817                     b->next[prev] = b->next[k];
818                 } else {
819                     size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
820                     prev     = k;
821                 }
822             }
823         }
824     }
825 }
826 
827 /* update all cno values into the blocks, over-writing the old values without
828    touching anything else. (only used for DV100) */
dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)829 static inline void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)
830 {
831     uint8_t *data;
832     int mb_index, i;
833 
834     for (mb_index = 0; mb_index < 5; mb_index++) {
835         data = dif + mb_index*80 + 4;
836         for (i = 0; i < profile->bpm; i++) {
837             /* zero out the class number */
838             data[1] &= 0xCF;
839             /* add the new one */
840             data[1] |= blk[profile->bpm*mb_index+i].cno << 4;
841 
842             data += profile->block_sizes[i] >> 3;
843         }
844     }
845 }
846 
dv_encode_video_segment(AVCodecContext *avctx, void *arg)847 static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
848 {
849     DVVideoContext *s = avctx->priv_data;
850     DVwork_chunk *work_chunk = arg;
851     int mb_index, i, j;
852     int mb_x, mb_y, c_offset;
853     ptrdiff_t linesize, y_stride;
854     uint8_t *y_ptr;
855     uint8_t *dif, *p;
856     LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
857     EncBlockInfo enc_blks[5 * DV_MAX_BPM];
858     PutBitContext pbs[5 * DV_MAX_BPM];
859     PutBitContext *pb;
860     EncBlockInfo *enc_blk;
861     int vs_bit_size = 0;
862     int qnos[5];
863     int *qnosp = &qnos[0];
864 
865     p = dif = &s->buf[work_chunk->buf_offset * 80];
866     enc_blk = &enc_blks[0];
867     for (mb_index = 0; mb_index < 5; mb_index++) {
868         dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
869 
870         qnos[mb_index] = DV_PROFILE_IS_HD(s->sys) ? 1 : 15;
871 
872         y_ptr    = s->frame->data[0] + (mb_y * s->frame->linesize[0] + mb_x) * 8;
873         linesize = s->frame->linesize[0];
874 
875         if (s->sys->height == 1080 && mb_y < 134)
876             enc_blk->dct_mode = dv_guess_dct_mode(s, y_ptr, linesize);
877         else
878             enc_blk->dct_mode = 0;
879         for (i = 1; i < 8; i++)
880             enc_blk[i].dct_mode = enc_blk->dct_mode;
881 
882         /* initializing luminance blocks */
883         if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P)                      ||
884             (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
885             (s->sys->height >= 720 && mb_y != 134)) {
886             y_stride = s->frame->linesize[0] * (1 << (3*!enc_blk->dct_mode));
887         } else {
888             y_stride = 16;
889         }
890         y_ptr    = s->frame->data[0] +
891                    (mb_y * s->frame->linesize[0] + mb_x) * 8;
892         linesize = s->frame->linesize[0];
893 
894         if (s->sys->video_stype == 4) { /* SD 422 */
895             vs_bit_size +=
896                 dv_init_enc_block(enc_blk + 0, y_ptr,                linesize, s, 0) +
897                 dv_init_enc_block(enc_blk + 1, NULL,                 linesize, s, 0) +
898                 dv_init_enc_block(enc_blk + 2, y_ptr + 8,            linesize, s, 0) +
899                 dv_init_enc_block(enc_blk + 3, NULL,                 linesize, s, 0);
900         } else {
901             vs_bit_size +=
902                 dv_init_enc_block(enc_blk + 0, y_ptr,                linesize, s, 0) +
903                 dv_init_enc_block(enc_blk + 1, y_ptr + 8,            linesize, s, 0) +
904                 dv_init_enc_block(enc_blk + 2, y_ptr +     y_stride, linesize, s, 0) +
905                 dv_init_enc_block(enc_blk + 3, y_ptr + 8 + y_stride, linesize, s, 0);
906         }
907         enc_blk += 4;
908 
909         /* initializing chrominance blocks */
910         c_offset = ((mb_y >>  (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
911                     (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) * 8;
912         for (j = 2; j; j--) {
913             uint8_t *c_ptr = s->frame->data[j] + c_offset;
914             linesize = s->frame->linesize[j];
915             y_stride = (mb_y == 134) ? 8 : (s->frame->linesize[j] * (1 << (3*!enc_blk->dct_mode)));
916             if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
917                 uint8_t *d;
918                 uint8_t *b = scratch;
919                 for (i = 0; i < 8; i++) {
920                     d      = c_ptr + linesize * 8;
921                     b[0]   = c_ptr[0];
922                     b[1]   = c_ptr[1];
923                     b[2]   = c_ptr[2];
924                     b[3]   = c_ptr[3];
925                     b[4]   = d[0];
926                     b[5]   = d[1];
927                     b[6]   = d[2];
928                     b[7]   = d[3];
929                     c_ptr += linesize;
930                     b     += 16;
931                 }
932                 c_ptr    = scratch;
933                 linesize = 16;
934             }
935 
936             vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr, linesize, s, 1);
937             if (s->sys->bpm == 8)
938                 vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride,
939                                                  linesize, s, 1);
940         }
941     }
942 
943     if (DV_PROFILE_IS_HD(s->sys)) {
944         /* unconditional */
945         dv_guess_qnos_hd(&enc_blks[0], qnosp);
946     } else if (vs_total_ac_bits < vs_bit_size) {
947         dv_guess_qnos(&enc_blks[0], qnosp);
948     }
949 
950     /* DIF encoding process */
951     for (j = 0; j < 5 * s->sys->bpm;) {
952         int start_mb = j;
953 
954         p[3] = *qnosp++;
955         p += 4;
956 
957         /* First pass over individual cells only */
958         for (i = 0; i < s->sys->bpm; i++, j++) {
959             int sz = s->sys->block_sizes[i] >> 3;
960 
961             init_put_bits(&pbs[j], p, sz);
962             put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2);
963             put_bits(&pbs[j], 1, DV_PROFILE_IS_HD(s->sys) && i ? 1 : enc_blks[j].dct_mode);
964             put_bits(&pbs[j], 2, enc_blks[j].cno);
965 
966             dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j + 1]);
967             p += sz;
968         }
969 
970         /* Second pass over each MB space */
971         pb = &pbs[start_mb];
972         for (i = 0; i < s->sys->bpm; i++)
973             if (enc_blks[start_mb + i].partial_bit_count)
974                 pb = dv_encode_ac(&enc_blks[start_mb + i], pb,
975                                   &pbs[start_mb + s->sys->bpm]);
976     }
977 
978     /* Third and final pass over the whole video segment space */
979     pb = &pbs[0];
980     for (j = 0; j < 5 * s->sys->bpm; j++) {
981         if (enc_blks[j].partial_bit_count)
982             pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm * 5]);
983         if (enc_blks[j].partial_bit_count)
984             av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n");
985     }
986 
987     for (j = 0; j < 5 * s->sys->bpm; j++) {
988         flush_put_bits(&pbs[j]);
989         memset(put_bits_ptr(&pbs[j]), 0xff, put_bytes_left(&pbs[j], 0));
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 
dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c, uint8_t *buf)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 
dv_write_dif_id(enum dv_section_type t, uint8_t chan_num, uint8_t seq_num, uint8_t dif_num, uint8_t *buf)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 
dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf)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 
dv_format_frame(DVVideoContext *c, uint8_t *buf)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 
dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt, const AVFrame *frame, int *got_packet)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_get_encode_buffer(c, pkt, s->sys->frame_size, 0)) < 0)
1177         return ret;
1178     /* Fixme: Only zero the part that is not overwritten later. */
1179     memset(pkt->data, 0, pkt->size);
1180 
1181     c->pix_fmt                = s->sys->pix_fmt;
1182     s->frame                  = frame;
1183     s->buf = pkt->data;
1184 
1185     dv_format_frame(s, pkt->data);
1186 
1187     c->execute(c, dv_encode_video_segment, s->work_chunks, NULL,
1188                dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1189 
1190     emms_c();
1191 
1192     *got_packet = 1;
1193 
1194     return 0;
1195 }
1196 
1197 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1198 #define OFFSET(x) offsetof(DVVideoContext, x)
1199 static const AVOption dv_options[] = {
1200     { "quant_deadzone",        "Quantizer dead zone",    OFFSET(quant_deadzone),       AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024, VE },
1201     { NULL },
1202 };
1203 
1204 static const AVClass dvvideo_encode_class = {
1205     .class_name = "dvvideo encoder",
1206     .item_name  = av_default_item_name,
1207     .option     = dv_options,
1208     .version    = LIBAVUTIL_VERSION_INT,
1209 };
1210 
1211 const FFCodec ff_dvvideo_encoder = {
1212     .p.name         = "dvvideo",
1213     .p.long_name    = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
1214     .p.type         = AVMEDIA_TYPE_VIDEO,
1215     .p.id           = AV_CODEC_ID_DVVIDEO,
1216     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1217                       AV_CODEC_CAP_SLICE_THREADS,
1218     .priv_data_size = sizeof(DVVideoContext),
1219     .init           = dvvideo_encode_init,
1220     FF_CODEC_ENCODE_CB(dvvideo_encode_frame),
1221     .p.pix_fmts     = (const enum AVPixelFormat[]) {
1222         AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P,
1223         AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
1224     },
1225     .p.priv_class   = &dvvideo_encode_class,
1226     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
1227 };
1228