aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/dovi_rpuenc.c
blob: a14c9cc181d9cfe4f3cfb46a1f42683a9ec3bf7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
 * Dolby Vision RPU encoder
 *
 * Copyright (C) 2024 Niklas Haas
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/avassert.h"
#include "libavutil/crc.h"
#include "libavutil/mem.h"

#include "avcodec.h"
#include "dovi_rpu.h"
#include "itut35.h"
#include "put_bits.h"
#include "put_golomb.h"
#include "refstruct.h"

static struct {
    uint64_t pps; // maximum pixels per second
    int width; // maximum width
    int main; // maximum bitrate in main tier
    int high; // maximum bitrate in high tier
} dv_levels[] = {
     [1] = {1280*720*24,    1280,  20,  50},
     [2] = {1280*720*30,    1280,  20,  50},
     [3] = {1920*1080*24,   1920,  20,  70},
     [4] = {1920*1080*30,   2560,  20,  70},
     [5] = {1920*1080*60,   3840,  20,  70},
     [6] = {3840*2160*24,   3840,  25, 130},
     [7] = {3840*2160*30,   3840,  25, 130},
     [8] = {3840*2160*48,   3840,  40, 130},
     [9] = {3840*2160*60,   3840,  40, 130},
    [10] = {3840*2160*120,  3840,  60, 240},
    [11] = {3840*2160*120,  7680,  60, 240},
    [12] = {7680*4320*60,   7680, 120, 450},
    [13] = {7680*4320*120u, 7680, 240, 800},
};

int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx)
{
    AVDOVIDecoderConfigurationRecord *cfg;
    const AVDOVIRpuDataHeader *hdr = NULL;
    const AVFrameSideData *sd;
    int dv_profile, dv_level, bl_compat_id = -1;
    size_t cfg_size;
    uint64_t pps;

    if (!s->enable)
        goto skip;

    sd = av_frame_side_data_get(avctx->decoded_side_data,
                                avctx->nb_decoded_side_data, AV_FRAME_DATA_DOVI_METADATA);

    if (sd)
        hdr = av_dovi_get_header((const AVDOVIMetadata *) sd->data);

    if (s->enable == FF_DOVI_AUTOMATIC && !hdr)
        goto skip;

    switch (avctx->codec_id) {
    case AV_CODEC_ID_AV1:  dv_profile = 10; break;
    case AV_CODEC_ID_H264: dv_profile = 9;  break;
    case AV_CODEC_ID_HEVC: dv_profile = hdr ? ff_dovi_guess_profile_hevc(hdr) : 8; break;
    default:
        /* No other encoder should be calling this! */
        av_assert0(0);
        return AVERROR_BUG;
    }

    if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
        if (dv_profile == 9) {
            if (avctx->pix_fmt != AV_PIX_FMT_YUV420P)
                dv_profile = 0;
        } else {
            if (avctx->pix_fmt != AV_PIX_FMT_YUV420P10)
                dv_profile = 0;
        }
    }

    switch (dv_profile) {
    case 4: /* HEVC with enhancement layer */
    case 7:
        if (s->enable > 0) {
            av_log(s->logctx, AV_LOG_ERROR, "Coding of Dolby Vision enhancement "
                   "layers is currently unsupported.");
            return AVERROR_PATCHWELCOME;
        } else {
            goto skip;
        }
    case 5: /* HEVC with proprietary IPTPQc2 */
        bl_compat_id = 0;
        break;
    case 10:
        /* FIXME: check for proper H.273 tags once those are added */
        if (hdr && hdr->bl_video_full_range_flag) {
            /* AV1 with proprietary IPTPQc2 */
            bl_compat_id = 0;
            break;
        }
        /* fall through */
    case 8: /* HEVC (or AV1) with BL compatibility */
        if (avctx->colorspace == AVCOL_SPC_BT2020_NCL &&
            avctx->color_primaries == AVCOL_PRI_BT2020 &&
            avctx->color_trc == AVCOL_TRC_SMPTE2084) {
            bl_compat_id = 1;
        } else if (avctx->colorspace == AVCOL_SPC_BT2020_NCL &&
                   avctx->color_primaries == AVCOL_PRI_BT2020 &&
                   avctx->color_trc == AVCOL_TRC_ARIB_STD_B67) {
            bl_compat_id = 4;
        } else if (avctx->colorspace == AVCOL_SPC_BT709 &&
                   avctx->color_primaries == AVCOL_PRI_BT709 &&
                   avctx->color_trc == AVCOL_TRC_BT709) {
            bl_compat_id = 2;
        }
    }

    if (!dv_profile || bl_compat_id < 0) {
        if (s->enable > 0) {
            av_log(s->logctx, AV_LOG_ERROR, "Dolby Vision enabled, but could "
                   "not determine profile and compatibility mode. Double-check "
                   "colorspace and format settings for compatibility?\n");
            return AVERROR(EINVAL);
        }
        goto skip;
    }

    pps = avctx->width * avctx->height;
    if (avctx->framerate.num) {
        pps = pps * avctx->framerate.num / avctx->framerate.den;
    } else {
        pps *= 25; /* sanity fallback */
    }

    dv_level = 0;
    for (int i = 1; i < FF_ARRAY_ELEMS(dv_levels); i++) {
        if (pps > dv_levels[i].pps)
            continue;
        if (avctx->width > dv_levels[i].width)
            continue;
        /* In theory, we should also test the bitrate when known, and
         * distinguish between main and high tier. In practice, just ignore
         * the bitrate constraints and hope they work out. This would ideally
         * be handled by either the encoder or muxer directly. */
        dv_level = i;
        break;
    }

    if (!dv_level) {
        if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
            av_log(s->logctx, AV_LOG_ERROR, "Coded PPS (%"PRIu64") and width (%d) "
                   "exceed Dolby Vision limitations\n", pps, avctx->width);
            return AVERROR(EINVAL);
        } else {
            av_log(s->logctx, AV_LOG_WARNING, "Coded PPS (%"PRIu64") and width (%d) "
                   "exceed Dolby Vision limitations. Ignoring, resulting file "
                   "may be non-conforming.\n", pps, avctx->width);
            dv_level = FF_ARRAY_ELEMS(dv_levels) - 1;
        }
    }

    cfg = av_dovi_alloc(&cfg_size);
    if (!cfg)
        return AVERROR(ENOMEM);

    if (!av_packet_side_data_add(&avctx->coded_side_data, &avctx->nb_coded_side_data,
                                 AV_PKT_DATA_DOVI_CONF, cfg, cfg_size, 0)) {
        av_free(cfg);
        return AVERROR(ENOMEM);
    }

    cfg->dv_version_major = 1;
    cfg->dv_version_minor = 0;
    cfg->dv_profile = dv_profile;
    cfg->dv_level = dv_level;
    cfg->rpu_present_flag = 1;
    cfg->el_present_flag = 0;
    cfg->bl_present_flag = 1;
    cfg->dv_bl_signal_compatibility_id = bl_compat_id;

    s->cfg = *cfg;
    return 0;

skip:
    s->cfg = (AVDOVIDecoderConfigurationRecord) {0};
    return 0;
}

static inline void put_ue_coef(PutBitContext *pb, const AVDOVIRpuDataHeader *hdr,
                               uint64_t coef)
{
    union { uint32_t u32; float f32; } fpart;

    switch (hdr->coef_data_type) {
    case RPU_COEFF_FIXED:
        set_ue_golomb(pb, coef >> hdr->coef_log2_denom);
        put_bits64(pb, hdr->coef_log2_denom,
                   coef & ((1LL << hdr->coef_log2_denom) - 1));
        break;
    case RPU_COEFF_FLOAT:
        fpart.f32 = coef / (float) (1LL << hdr->coef_log2_denom);
        put_bits64(pb, hdr->coef_log2_denom, fpart.u32);
        break;
    }
}

static inline void put_se_coef(PutBitContext *pb, const AVDOVIRpuDataHeader *hdr,
                               uint64_t coef)
{
    union { uint32_t u32; float f32; } fpart;

    switch (hdr->coef_data_type) {
    case RPU_COEFF_FIXED:
        set_se_golomb(pb, coef >> hdr->coef_log2_denom);
        put_bits64(pb, hdr->coef_log2_denom,
                   coef & ((1LL << hdr->coef_log2_denom) - 1));
        break;
    case RPU_COEFF_FLOAT:
        fpart.f32 = coef / (float) (1LL << hdr->coef_log2_denom);
        put_bits64(pb, hdr->coef_log2_denom, fpart.u32);
        break;
    }
}

static int av_q2den(AVRational q, int den)
{
    if (!q.den || q.den == den)
        return q.num;
    q = av_mul_q(q, av_make_q(den, 1));
    return (q.num + (q.den >> 1)) / q.den;
}

static void generate_ext_v1(PutBitContext *pb, const AVDOVIDmData *dm)
{
    int ext_block_length, start_pos, pad_bits;

    switch (dm->level) {
    case 1:   ext_block_length = 5;  break;
    case 2:   ext_block_length = 11; break;
    case 4:   ext_block_length = 3;  break;
    case 5:   ext_block_length = 7;  break;
    case 6:   ext_block_length = 8;  break;
    case 255: ext_block_length = 6;  break;
    default: return;
    }

    set_ue_golomb(pb, ext_block_length);
    put_bits(pb, 8, dm->level);
    start_pos = put_bits_count(pb);

    switch (dm->level) {
    case 1:
        put_bits(pb, 12, dm->l1.min_pq);
        put_bits(pb, 12, dm->l1.max_pq);
        put_bits(pb, 12, dm->l1.avg_pq);
        break;
    case 2:
        put_bits(pb, 12, dm->l2.target_max_pq);
        put_bits(pb, 12, dm->l2.trim_slope);
        put_bits(pb, 12, dm->l2.trim_offset);
        put_bits(pb, 12, dm->l2.trim_power);
        put_bits(pb, 12, dm->l2.trim_chroma_weight);
        put_bits(pb, 12, dm->l2.trim_saturation_gain);
        put_sbits(pb, 13, dm->l2.ms_weight);
        break;
    case 4:
        put_bits(pb, 12, dm->l4.anchor_pq);
        put_bits(pb, 12, dm->l4.anchor_power);
        break;
    case 5:
        put_bits(pb, 13, dm->l5.left_offset);
        put_bits(pb, 13, dm->l5.right_offset);
        put_bits(pb, 13, dm->l5.top_offset);
        put_bits(pb, 13, dm->l5.bottom_offset);
        break;
    case 6:
        put_bits(pb, 16, dm->l6.max_luminance);
        put_bits(pb, 16, dm->l6.min_luminance);
        put_bits(pb, 16, dm->l6.max_cll);
        put_bits(pb, 16, dm->l6.max_fall);
        break;
    case 255:
        put_bits(pb, 8, dm->l255.dm_run_mode);
        put_bits(pb, 8, dm->l255.dm_run_version);
        for (int i = 0; i < 4; i++)
            put_bits(pb, 8, dm->l255.dm_debug[i]);
        break;
    }

    pad_bits = ext_block_length * 8 - (put_bits_count(pb) - start_pos);
    av_assert1(pad_bits >= 0);
    put_bits(pb, pad_bits, 0);
}

static void put_cie_xy(PutBitContext *pb, AVCIExy xy)
{
    const int denom = 32767;
    put_sbits(pb, 16, av_q2den(xy.x, denom));
    put_sbits(pb, 16, av_q2den(xy.y, denom));
}

#define ANY6(arr) (arr[0] || arr[1] || arr[2] || arr[3] || arr[4] || arr[5])
#define ANY_XY(xy) (xy.x.num || xy.y.num)
#define ANY_CSP(csp) (ANY_XY(csp.prim.r) || ANY_XY(csp.prim.g) || \
                      ANY_XY(csp.prim.b) || ANY_XY(csp.wp))

static void generate_ext_v2(PutBitContext *pb, const AVDOVIDmData *dm)
{
    int ext_block_length, start_pos, pad_bits;

    switch (dm->level) {
    case 3: ext_block_length = 5; break;
    case 8:
        if (ANY6(dm->l8.hue_vector_field)) {
            ext_block_length = 25;
        } else if (ANY6(dm->l8.saturation_vector_field)) {
            ext_block_length = 19;
        } else if (dm->l8.clip_trim) {
            ext_block_length = 13;
        } else if (dm->l8.target_mid_contrast) {
            ext_block_length = 12;
        } else {
            ext_block_length = 10;
        }
        break;
    case 9:
        if (ANY_CSP(dm->l9.source_display_primaries)) {
            ext_block_length = 17;
        } else {
            ext_block_length = 1;
        }
        break;
    case 10:
        if (ANY_CSP(dm->l10.target_display_primaries)) {
            ext_block_length = 21;
        } else {
            ext_block_length = 5;
        }
        break;
    case 11:  ext_block_length = 4; break;
    case 254: ext_block_length = 2; break;
    default: return;
    }

    set_ue_golomb(pb, ext_block_length);
    put_bits(pb, 8, dm->level);
    start_pos = put_bits_count(pb);

    switch (dm->level) {
    case 3:
        put_bits(pb, 12, dm->l3.min_pq_offset);
        put_bits(pb, 12, dm->l3.max_pq_offset);
        put_bits(pb, 12, dm->l3.avg_pq_offset);
        break;
    case 8:
        put_bits(pb, 8, dm->l8.target_display_index);
        put_bits(pb, 12, dm->l8.trim_slope);
        put_bits(pb, 12, dm->l8.trim_offset);
        put_bits(pb, 12, dm->l8.trim_power);
        put_bits(pb, 12, dm->l8.trim_chroma_weight);
        put_bits(pb, 12, dm->l8.trim_saturation_gain);
        put_bits(pb, 12, dm->l8.ms_weight);
        if (ext_block_length < 12)
            break;
        put_bits(pb, 12, dm->l8.target_mid_contrast);
        if (ext_block_length < 13)
            break;
        put_bits(pb, 12, dm->l8.clip_trim);
        if (ext_block_length < 19)
            break;
        for (int i = 0; i < 6; i++)
            put_bits(pb, 8, dm->l8.saturation_vector_field[i]);
        if (ext_block_length < 25)
            break;
        for (int i = 0; i < 6; i++)
            put_bits(pb, 8, dm->l8.hue_vector_field[i]);
        break;
    case 9:
        put_bits(pb, 8, dm->l9.source_primary_index);
        if (ext_block_length < 17)
            break;
        put_cie_xy(pb, dm->l9.source_display_primaries.prim.r);
        put_cie_xy(pb, dm->l9.source_display_primaries.prim.g);
        put_cie_xy(pb, dm->l9.source_display_primaries.prim.b);
        put_cie_xy(pb, dm->l9.source_display_primaries.wp);
        break;
    case 10:
        put_bits(pb, 8, dm->l10.target_display_index);
        put_bits(pb, 12, dm->l10.target_max_pq);
        put_bits(pb, 12, dm->l10.target_min_pq);
        put_bits(pb, 8, dm->l10.target_primary_index);
        if (ext_block_length < 21)
            break;
        put_cie_xy(pb, dm->l10.target_display_primaries.prim.r);
        put_cie_xy(pb, dm->l10.target_display_primaries.prim.g);
        put_cie_xy(pb, dm->l10.target_display_primaries.prim.b);
        put_cie_xy(pb, dm->l10.target_display_primaries.wp);
        break;
    case 11:
        put_bits(pb, 8, dm->l11.content_type);
        put_bits(pb, 4, dm->l11.whitepoint);
        put_bits(pb, 1, dm->l11.reference_mode_flag);
        put_bits(pb, 3, 0); /* reserved */
        put_bits(pb, 2, dm->l11.sharpness);
        put_bits(pb, 2, dm->l11.noise_reduction);
        put_bits(pb, 2, dm->l11.mpeg_noise_reduction);
        put_bits(pb, 2, dm->l11.frame_rate_conversion);
        put_bits(pb, 2, dm->l11.brightness);
        put_bits(pb, 2, dm->l11.color);
        break;
    case 254:
        put_bits(pb, 8, dm->l254.dm_mode);
        put_bits(pb, 8, dm->l254.dm_version_index);
        break;
    }

    pad_bits = ext_block_length * 8 - (put_bits_count(pb) - start_pos);
    av_assert1(pad_bits >= 0);
    put_bits(pb, pad_bits, 0);
}

int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata,
                         uint8_t **out_rpu, int *out_size)
{
    PutBitContext *pb = &(PutBitContext){0};
    const AVDOVIRpuDataHeader *hdr;
    const AVDOVIDataMapping *mapping;
    const AVDOVIColorMetadata *color;
    int vdr_dm_metadata_present, vdr_rpu_id, use_prev_vdr_rpu, profile,
        buffer_size, rpu_size, pad, zero_run;
    int num_ext_blocks_v1, num_ext_blocks_v2;
    uint32_t crc;
    uint8_t *dst;
    if (!metadata) {
        *out_rpu = NULL;
        *out_size = 0;
        return 0;
    }

    hdr = av_dovi_get_header(metadata);
    mapping = av_dovi_get_mapping(metadata);
    color = av_dovi_get_color(metadata);
    av_assert0(s->cfg.dv_profile);

    if (hdr->rpu_type != 2) {
        av_log(s->logctx, AV_LOG_ERROR, "Unhandled RPU type %"PRIu8"\n",
               hdr->rpu_type);
        return AVERROR_INVALIDDATA;
    }

    vdr_rpu_id = -1;
    for (int i = 0; i <= DOVI_MAX_DM_ID; i++) {
        if (s->vdr[i] && !memcmp(s->vdr[i], mapping, sizeof(*mapping))) {
            vdr_rpu_id = i;
            break;
        } else if (vdr_rpu_id < 0 && (!s->vdr[i] || i == DOVI_MAX_DM_ID)) {
            vdr_rpu_id = i;
        }
    }

    if (!s->vdr[vdr_rpu_id]) {
        s->vdr[vdr_rpu_id] = ff_refstruct_allocz(sizeof(AVDOVIDataMapping));
        if (!s->vdr[vdr_rpu_id])
            return AVERROR(ENOMEM);
    }

    num_ext_blocks_v1 = num_ext_blocks_v2 = 0;
    for (int i = 0; i < metadata->num_ext_blocks; i++) {
        const AVDOVIDmData *dm = av_dovi_get_ext(metadata, i);
        switch (dm->level) {
        case 1:
        case 2:
        case 4:
        case 5:
        case 6:
        case 255:
            num_ext_blocks_v1++;
            break;
        case 3:
        case 8:
        case 9:
        case 10:
        case 11:
        case 254:
            num_ext_blocks_v2++;
            break;
        default:
            av_log(s->logctx, AV_LOG_ERROR, "Invalid ext block level %d\n",
                   dm->level);
            return AVERROR_INVALIDDATA;
        }
    }

    vdr_dm_metadata_present = memcmp(color, &ff_dovi_color_default, sizeof(*color));
    use_prev_vdr_rpu = !memcmp(s->vdr[vdr_rpu_id], mapping, sizeof(*mapping));
    if (num_ext_blocks_v1 || num_ext_blocks_v2)
        vdr_dm_metadata_present = 1;

    if (vdr_dm_metadata_present && !s->dm) {
        s->dm = ff_refstruct_allocz(sizeof(AVDOVIColorMetadata));
        if (!s->dm)
            return AVERROR(ENOMEM);
    }

    buffer_size = 12 /* vdr seq info */ + 5 /* CRC32 + terminator */;
    buffer_size += num_ext_blocks_v1 * 13;
    buffer_size += num_ext_blocks_v2 * 28;
    if (!use_prev_vdr_rpu) {
        buffer_size += 160;
        for (int c = 0; c < 3; c++) {
            for (int i = 0; i < mapping->curves[c].num_pivots - 1; i++) {
                switch (mapping->curves[c].mapping_idc[i]) {
                case AV_DOVI_MAPPING_POLYNOMIAL: buffer_size += 26;  break;
                case AV_DOVI_MAPPING_MMR:        buffer_size += 177; break;
                }
            }
        }
    }
    if (vdr_dm_metadata_present)
        buffer_size += 67;

    av_fast_padded_malloc(&s->rpu_buf, &s->rpu_buf_sz, buffer_size);
    if (!s->rpu_buf)
        return AVERROR(ENOMEM);
    init_put_bits(pb, s->rpu_buf, s->rpu_buf_sz);

    /* RPU header */
    put_bits(pb, 6, hdr->rpu_type);
    put_bits(pb, 11, hdr->rpu_format);
    put_bits(pb, 4, hdr->vdr_rpu_profile);
    put_bits(pb, 4, hdr->vdr_rpu_level);
    put_bits(pb, 1, 1); /* vdr_seq_info_present */
    put_bits(pb, 1, hdr->chroma_resampling_explicit_filter_flag);
    put_bits(pb, 2, hdr->coef_data_type);
    if (hdr->coef_data_type == RPU_COEFF_FIXED)
        set_ue_golomb(pb, hdr->coef_log2_denom);
    put_bits(pb, 2, hdr->vdr_rpu_normalized_idc);
    put_bits(pb, 1, hdr->bl_video_full_range_flag);
    if ((hdr->rpu_format & 0x700) == 0) {
        int ext_mapping_idc = (hdr->ext_mapping_idc_5_7 << 5) | hdr->ext_mapping_idc_0_4;
        set_ue_golomb(pb, hdr->bl_bit_depth - 8);
        set_ue_golomb(pb, (ext_mapping_idc << 8) | (hdr->el_bit_depth - 8));
        set_ue_golomb(pb, hdr->vdr_bit_depth - 8);
        put_bits(pb, 1, hdr->spatial_resampling_filter_flag);
        put_bits(pb, 3, 0); /* reserved_zero_3bits */
        put_bits(pb, 1, hdr->el_spatial_resampling_filter_flag);
        put_bits(pb, 1, hdr->disable_residual_flag);
    }
    s->header = *hdr;

    put_bits(pb, 1, vdr_dm_metadata_present);
    put_bits(pb, 1, use_prev_vdr_rpu);
    set_ue_golomb(pb, vdr_rpu_id);
    s->mapping = s->vdr[vdr_rpu_id];

    profile = s->cfg.dv_profile ? s->cfg.dv_profile : ff_dovi_guess_profile_hevc(hdr);

    if (!use_prev_vdr_rpu) {
        set_ue_golomb(pb, mapping->mapping_color_space);
        set_ue_golomb(pb, mapping->mapping_chroma_format_idc);
        for (int c = 0; c < 3; c++) {
            const AVDOVIReshapingCurve *curve = &mapping->curves[c];
            int prev = 0;
            set_ue_golomb(pb, curve->num_pivots - 2);
            for (int i = 0; i < curve->num_pivots; i++) {
                put_bits(pb, hdr->bl_bit_depth, curve->pivots[i] - prev);
                prev = curve->pivots[i];
            }
        }

        if (mapping->nlq_method_idc != AV_DOVI_NLQ_NONE) {
            put_bits(pb, 3, mapping->nlq_method_idc);
            put_bits(pb, hdr->bl_bit_depth, mapping->nlq_pivots[0]);
            put_bits(pb, hdr->bl_bit_depth, mapping->nlq_pivots[1] - mapping->nlq_pivots[0]);
        }

        set_ue_golomb(pb, mapping->num_x_partitions - 1);
        set_ue_golomb(pb, mapping->num_y_partitions - 1);

        for (int c = 0; c < 3; c++) {
            const AVDOVIReshapingCurve *curve = &mapping->curves[c];
            for (int i = 0; i < curve->num_pivots - 1; i++) {
                set_ue_golomb(pb, curve->mapping_idc[i]);
                switch (curve->mapping_idc[i]) {
                case AV_DOVI_MAPPING_POLYNOMIAL: {
                    set_ue_golomb(pb, curve->poly_order[i] - 1);
                    if (curve->poly_order[i] == 1)
                        put_bits(pb, 1, 0); /* linear_interp_flag */
                    for (int k = 0; k <= curve->poly_order[i]; k++)
                        put_se_coef(pb, hdr, curve->poly_coef[i][k]);
                    break;
                }
                case AV_DOVI_MAPPING_MMR: {
                    put_bits(pb, 2, curve->mmr_order[i] - 1);
                    put_se_coef(pb, hdr, curve->mmr_constant[i]);
                    for (int j = 0; j < curve->mmr_order[i]; j++) {
                        for (int k = 0; k < 7; k++)
                            put_se_coef(pb, hdr, curve->mmr_coef[i][j][k]);
                    }
                    break;
                }
                }
            }
        }

        if (mapping->nlq_method_idc != AV_DOVI_NLQ_NONE) {
            for (int c = 0; c < 3; c++) {
                const AVDOVINLQParams *nlq = &mapping->nlq[c];
                put_bits(pb, hdr->el_bit_depth, nlq->nlq_offset);
                put_ue_coef(pb, hdr, nlq->vdr_in_max);
                switch (mapping->nlq_method_idc) {
                case AV_DOVI_NLQ_LINEAR_DZ:
                    put_ue_coef(pb, hdr, nlq->linear_deadzone_slope);
                    put_ue_coef(pb, hdr, nlq->linear_deadzone_threshold);
                    break;
                }
            }
        }

        memcpy(s->vdr[vdr_rpu_id], mapping, sizeof(*mapping));
    }

    if (vdr_dm_metadata_present) {
        const int denom = profile == 4 ? (1 << 30) : (1 << 28);
        set_ue_golomb(pb, color->dm_metadata_id); /* affected_dm_id */
        set_ue_golomb(pb, color->dm_metadata_id); /* current_dm_id */
        set_ue_golomb(pb, color->scene_refresh_flag);
        for (int i = 0; i < 9; i++)
            put_sbits(pb, 16, av_q2den(color->ycc_to_rgb_matrix[i], 1 << 13));
        for (int i = 0; i < 3; i++)
            put_bits32(pb, av_q2den(color->ycc_to_rgb_offset[i], denom));
        for (int i = 0; i < 9; i++)
            put_sbits(pb, 16, av_q2den(color->rgb_to_lms_matrix[i], 1 << 14));
        put_bits(pb, 16, color->signal_eotf);
        put_bits(pb, 16, color->signal_eotf_param0);
        put_bits(pb, 16, color->signal_eotf_param1);
        put_bits32(pb, color->signal_eotf_param2);
        put_bits(pb, 5, color->signal_bit_depth);
        put_bits(pb, 2, color->signal_color_space);
        put_bits(pb, 2, color->signal_chroma_format);
        put_bits(pb, 2, color->signal_full_range_flag);
        put_bits(pb, 12, color->source_min_pq);
        put_bits(pb, 12, color->source_max_pq);
        put_bits(pb, 10, color->source_diagonal);

        memcpy(s->dm, color, sizeof(*color));
        s->color = s->dm;

        /* Extension blocks */
        set_ue_golomb(pb, num_ext_blocks_v1);
        align_put_bits(pb);
        for (int i = 0; i < metadata->num_ext_blocks; i++)
            generate_ext_v1(pb, av_dovi_get_ext(metadata, i));

        if (num_ext_blocks_v2) {
            set_ue_golomb(pb, num_ext_blocks_v2);
            align_put_bits(pb);
            for (int i = 0; i < metadata->num_ext_blocks; i++)
                generate_ext_v2(pb, av_dovi_get_ext(metadata, i));
        }
    } else {
        s->color = &ff_dovi_color_default;
        s->num_ext_blocks = 0;
    }

    flush_put_bits(pb);
    crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
                            s->rpu_buf, put_bytes_output(pb)));
    put_bits32(pb, crc);
    put_bits(pb, 8, 0x80); /* terminator */
    flush_put_bits(pb);

    rpu_size = put_bytes_output(pb);
    switch (s->cfg.dv_profile) {
    case 10:
        /* AV1 uses T.35 OBU with EMDF header */
        *out_rpu = av_malloc(rpu_size + 15);
        if (!*out_rpu)
            return AVERROR(ENOMEM);
        init_put_bits(pb, *out_rpu, rpu_size + 15);
        put_bits(pb,  8, ITU_T_T35_COUNTRY_CODE_US);
        put_bits(pb, 16, ITU_T_T35_PROVIDER_CODE_DOLBY);
        put_bits32(pb, 0x800); /* provider_oriented_code */
        put_bits(pb, 27, 0x01be6841u); /* fixed EMDF header, see above */
        if (rpu_size > 0xFF) {
            av_assert2(rpu_size <= 0x10000);
            put_bits(pb, 8, (rpu_size >> 8) - 1);
            put_bits(pb, 1, 1); /* read_more */
            put_bits(pb, 8, rpu_size & 0xFF);
            put_bits(pb, 1, 0);
        } else {
            put_bits(pb, 8, rpu_size);
            put_bits(pb, 1, 0);
        }
        ff_copy_bits(pb, s->rpu_buf, rpu_size * 8);
        put_bits(pb, 17, 0x400); /* emdf payload id + emdf_protection */

        pad = pb->bit_left & 7;
        put_bits(pb, pad, (1 << pad) - 1); /* pad to next byte with 1 bits */
        flush_put_bits(pb);
        *out_size = put_bytes_output(pb);
        return 0;

    case 5:
    case 8:
        *out_rpu = dst = av_malloc(1 + rpu_size * 3 / 2); /* worst case */
        if (!*out_rpu)
            return AVERROR(ENOMEM);
        *dst++ = 25; /* NAL prefix */
        zero_run = 0;
        for (int i = 0; i < rpu_size; i++) {
            if (zero_run < 2) {
                if (s->rpu_buf[i] == 0) {
                    zero_run++;
                } else {
                    zero_run = 0;
                }
            } else {
                if ((s->rpu_buf[i] & ~3) == 0) {
                    /* emulation prevention */
                    *dst++ = 3;
                }
                zero_run = s->rpu_buf[i] == 0;
            }
            *dst++ = s->rpu_buf[i];
        }
        *out_size = dst - *out_rpu;
        return 0;

    default:
        /* Should be unreachable */
        av_assert0(0);
        return AVERROR_BUG;
    }
}