Coverage Report

Created: 2025-12-31 07:57

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