GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/cbs_h2645.c Lines: 446 739 60.4 %
Date: 2020-01-27 20:17:04 Branches: 250 509 49.1 %

Line Branch Exec Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include "libavutil/attributes.h"
20
#include "libavutil/avassert.h"
21
22
#include "bytestream.h"
23
#include "cbs.h"
24
#include "cbs_internal.h"
25
#include "cbs_h264.h"
26
#include "cbs_h265.h"
27
#include "h264.h"
28
#include "h264_sei.h"
29
#include "h2645_parse.h"
30
#include "hevc.h"
31
#include "hevc_sei.h"
32
33
34
69106
static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
35
                              const char *name, const int *subscripts,
36
                              uint32_t *write_to,
37
                              uint32_t range_min, uint32_t range_max)
38
{
39
    uint32_t value;
40
    int position, i, j;
41
    unsigned int k;
42
    char bits[65];
43
44
69106
    position = get_bits_count(gbc);
45
46
117141
    for (i = 0; i < 32; i++) {
47
117141
        if (get_bits_left(gbc) < i + 1) {
48
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
49
                   "%s: bitstream ended.\n", name);
50
            return AVERROR_INVALIDDATA;
51
        }
52
117141
        k = get_bits1(gbc);
53
117141
        bits[i] = k ? '1' : '0';
54
117141
        if (k)
55
69106
            break;
56
    }
57
69106
    if (i >= 32) {
58
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
59
               "%s: more than 31 zeroes.\n", name);
60
        return AVERROR_INVALIDDATA;
61
    }
62
69106
    value = 1;
63
117141
    for (j = 0; j < i; j++) {
64
48035
        k = get_bits1(gbc);
65
48035
        bits[i + j + 1] = k ? '1' : '0';
66
48035
        value = value << 1 | k;
67
    }
68
69106
    bits[i + j + 1] = 0;
69
69106
    --value;
70
71
69106
    if (ctx->trace_enable)
72
        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
73
                                    bits, value);
74
75

69106
    if (value < range_min || value > range_max) {
76
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
77
               "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
78
               name, value, range_min, range_max);
79
        return AVERROR_INVALIDDATA;
80
    }
81
82
69106
    *write_to = value;
83
69106
    return 0;
84
}
85
86
75672
static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
87
                              const char *name, const int *subscripts,
88
                              int32_t *write_to,
89
                              int32_t range_min, int32_t range_max)
90
{
91
    int32_t value;
92
    int position, i, j;
93
    unsigned int k;
94
    uint32_t v;
95
    char bits[65];
96
97
75672
    position = get_bits_count(gbc);
98
99
267890
    for (i = 0; i < 32; i++) {
100
267890
        if (get_bits_left(gbc) < i + 1) {
101
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
102
                   "%s: bitstream ended.\n", name);
103
            return AVERROR_INVALIDDATA;
104
        }
105
267890
        k = get_bits1(gbc);
106
267890
        bits[i] = k ? '1' : '0';
107
267890
        if (k)
108
75672
            break;
109
    }
110
75672
    if (i >= 32) {
111
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
112
               "%s: more than 31 zeroes.\n", name);
113
        return AVERROR_INVALIDDATA;
114
    }
115
75672
    v = 1;
116
267890
    for (j = 0; j < i; j++) {
117
192218
        k = get_bits1(gbc);
118
192218
        bits[i + j + 1] = k ? '1' : '0';
119
192218
        v = v << 1 | k;
120
    }
121
75672
    bits[i + j + 1] = 0;
122
75672
    if (v & 1)
123
43754
        value = -(int32_t)(v / 2);
124
    else
125
31918
        value = v / 2;
126
127
75672
    if (ctx->trace_enable)
128
        ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
129
                                    bits, value);
130
131

75672
    if (value < range_min || value > range_max) {
132
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
133
               "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
134
               name, value, range_min, range_max);
135
        return AVERROR_INVALIDDATA;
136
    }
137
138
75672
    *write_to = value;
139
75672
    return 0;
140
}
141
142
69106
static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
143
                               const char *name, const int *subscripts,
144
                               uint32_t value,
145
                               uint32_t range_min, uint32_t range_max)
146
{
147
    int len;
148
149

69106
    if (value < range_min || value > range_max) {
150
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
151
               "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
152
               name, value, range_min, range_max);
153
        return AVERROR_INVALIDDATA;
154
    }
155
69106
    av_assert0(value != UINT32_MAX);
156
157
69106
    len = av_log2(value + 1);
158
69106
    if (put_bits_left(pbc) < 2 * len + 1)
159
        return AVERROR(ENOSPC);
160
161
69106
    if (ctx->trace_enable) {
162
        char bits[65];
163
        int i;
164
165
        for (i = 0; i < len; i++)
166
            bits[i] = '0';
167
        bits[len] = '1';
168
        for (i = 0; i < len; i++)
169
            bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
170
        bits[len + len + 1] = 0;
171
172
        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
173
                                    name, subscripts, bits, value);
174
    }
175
176
69106
    put_bits(pbc, len, 0);
177
69106
    if (len + 1 < 32)
178
69106
        put_bits(pbc, len + 1, value + 1);
179
    else
180
        put_bits32(pbc, value + 1);
181
182
69106
    return 0;
183
}
184
185
75672
static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
186
                               const char *name, const int *subscripts,
187
                               int32_t value,
188
                               int32_t range_min, int32_t range_max)
189
{
190
    int len;
191
    uint32_t uvalue;
192
193

75672
    if (value < range_min || value > range_max) {
194
        av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
195
               "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
196
               name, value, range_min, range_max);
197
        return AVERROR_INVALIDDATA;
198
    }
199
75672
    av_assert0(value != INT32_MIN);
200
201
75672
    if (value == 0)
202
17146
        uvalue = 0;
203
58526
    else if (value > 0)
204
31918
        uvalue = 2 * (uint32_t)value - 1;
205
    else
206
26608
        uvalue = 2 * (uint32_t)-value;
207
208
75672
    len = av_log2(uvalue + 1);
209
75672
    if (put_bits_left(pbc) < 2 * len + 1)
210
        return AVERROR(ENOSPC);
211
212
75672
    if (ctx->trace_enable) {
213
        char bits[65];
214
        int i;
215
216
        for (i = 0; i < len; i++)
217
            bits[i] = '0';
218
        bits[len] = '1';
219
        for (i = 0; i < len; i++)
220
            bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
221
        bits[len + len + 1] = 0;
222
223
        ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
224
                                    name, subscripts, bits, value);
225
    }
226
227
75672
    put_bits(pbc, len, 0);
228
75672
    if (len + 1 < 32)
229
75672
        put_bits(pbc, len + 1, uvalue + 1);
230
    else
231
        put_bits32(pbc, uvalue + 1);
232
233
75672
    return 0;
234
}
235
236
#define HEADER(name) do { \
237
        ff_cbs_trace_header(ctx, name); \
238
    } while (0)
239
240
#define CHECK(call) do { \
241
        err = (call); \
242
        if (err < 0) \
243
            return err; \
244
    } while (0)
245
246
#define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
247
#define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
248
#define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
249
250
#define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
251
252
#define u(width, name, range_min, range_max) \
253
        xu(width, name, current->name, range_min, range_max, 0)
254
#define ub(width, name) \
255
        xu(width, name, current->name, 0, MAX_UINT_BITS(width), 0)
256
#define flag(name) ub(1, name)
257
#define ue(name, range_min, range_max) \
258
        xue(name, current->name, range_min, range_max, 0)
259
#define i(width, name, range_min, range_max) \
260
        xi(width, name, current->name, range_min, range_max, 0)
261
#define ib(width, name) \
262
        xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), 0)
263
#define se(name, range_min, range_max) \
264
        xse(name, current->name, range_min, range_max, 0)
265
266
#define us(width, name, range_min, range_max, subs, ...) \
267
        xu(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
268
#define ubs(width, name, subs, ...) \
269
        xu(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
270
#define flags(name, subs, ...) \
271
        xu(1, name, current->name, 0, 1, subs, __VA_ARGS__)
272
#define ues(name, range_min, range_max, subs, ...) \
273
        xue(name, current->name, range_min, range_max, subs, __VA_ARGS__)
274
#define is(width, name, range_min, range_max, subs, ...) \
275
        xi(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
276
#define ibs(width, name, subs, ...) \
277
        xi(width, name, current->name, MIN_INT_BITS(width), MAX_INT_BITS(width), subs, __VA_ARGS__)
278
#define ses(name, range_min, range_max, subs, ...) \
279
        xse(name, current->name, range_min, range_max, subs, __VA_ARGS__)
280
281
#define fixed(width, name, value) do { \
282
        av_unused uint32_t fixed_value = value; \
283
        xu(width, name, fixed_value, value, value, 0); \
284
    } while (0)
285
286
287
#define READ
288
#define READWRITE read
289
#define RWContext GetBitContext
290
291
#define xu(width, name, var, range_min, range_max, subs, ...) do { \
292
        uint32_t value; \
293
        CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
294
                                   SUBSCRIPTS(subs, __VA_ARGS__), \
295
                                   &value, range_min, range_max)); \
296
        var = value; \
297
    } while (0)
298
#define xue(name, var, range_min, range_max, subs, ...) do { \
299
        uint32_t value; \
300
        CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
301
                                 SUBSCRIPTS(subs, __VA_ARGS__), \
302
                                 &value, range_min, range_max)); \
303
        var = value; \
304
    } while (0)
305
#define xi(width, name, var, range_min, range_max, subs, ...) do { \
306
        int32_t value; \
307
        CHECK(ff_cbs_read_signed(ctx, rw, width, #name, \
308
                                 SUBSCRIPTS(subs, __VA_ARGS__), \
309
                                 &value, range_min, range_max)); \
310
        var = value; \
311
    } while (0)
312
#define xse(name, var, range_min, range_max, subs, ...) do { \
313
        int32_t value; \
314
        CHECK(cbs_read_se_golomb(ctx, rw, #name, \
315
                                 SUBSCRIPTS(subs, __VA_ARGS__), \
316
                                 &value, range_min, range_max)); \
317
        var = value; \
318
    } while (0)
319
320
321
#define infer(name, value) do { \
322
        current->name = value; \
323
    } while (0)
324
325
3020
static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
326
{
327
3020
    int bits_left = get_bits_left(gbc);
328
3020
    if (bits_left > 8)
329
20
        return 1;
330
3000
    if (bits_left == 0)
331
        return 0;
332
3000
    if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
333
        return 1;
334
3000
    return 0;
335
}
336
337
#define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
338
339
#define byte_alignment(rw) (get_bits_count(rw) % 8)
340
341
#define allocate(name, size) do { \
342
        name ## _ref = av_buffer_allocz(size + \
343
                                        AV_INPUT_BUFFER_PADDING_SIZE); \
344
        if (!name ## _ref) \
345
            return AVERROR(ENOMEM); \
346
        name = name ## _ref->data; \
347
    } while (0)
348
349
#define FUNC(name) FUNC_H264(READWRITE, name)
350
#include "cbs_h264_syntax_template.c"
351
#undef FUNC
352
353
#define FUNC(name) FUNC_H265(READWRITE, name)
354
#include "cbs_h265_syntax_template.c"
355
#undef FUNC
356
357
#undef READ
358
#undef READWRITE
359
#undef RWContext
360
#undef xu
361
#undef xi
362
#undef xue
363
#undef xse
364
#undef infer
365
#undef more_rbsp_data
366
#undef byte_alignment
367
#undef allocate
368
369
370
#define WRITE
371
#define READWRITE write
372
#define RWContext PutBitContext
373
374
#define xu(width, name, var, range_min, range_max, subs, ...) do { \
375
        uint32_t value = var; \
376
        CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
377
                                    SUBSCRIPTS(subs, __VA_ARGS__), \
378
                                    value, range_min, range_max)); \
379
    } while (0)
380
#define xue(name, var, range_min, range_max, subs, ...) do { \
381
        uint32_t value = var; \
382
        CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
383
                                  SUBSCRIPTS(subs, __VA_ARGS__), \
384
                                  value, range_min, range_max)); \
385
    } while (0)
386
#define xi(width, name, var, range_min, range_max, subs, ...) do { \
387
        int32_t value = var; \
388
        CHECK(ff_cbs_write_signed(ctx, rw, width, #name, \
389
                                  SUBSCRIPTS(subs, __VA_ARGS__), \
390
                                  value, range_min, range_max)); \
391
    } while (0)
392
#define xse(name, var, range_min, range_max, subs, ...) do { \
393
        int32_t value = var; \
394
        CHECK(cbs_write_se_golomb(ctx, rw, #name, \
395
                                  SUBSCRIPTS(subs, __VA_ARGS__), \
396
                                  value, range_min, range_max)); \
397
    } while (0)
398
399
#define infer(name, value) do { \
400
        if (current->name != (value)) { \
401
            av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
402
                   "%s does not match inferred value: " \
403
                   "%"PRId64", but should be %"PRId64".\n", \
404
                   #name, (int64_t)current->name, (int64_t)(value)); \
405
        } \
406
    } while (0)
407
408
#define more_rbsp_data(var) (var)
409
410
#define byte_alignment(rw) (put_bits_count(rw) % 8)
411
412
#define allocate(name, size) do { \
413
        if (!name) { \
414
            av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
415
                   "for writing.\n", #name); \
416
            return AVERROR_INVALIDDATA; \
417
        } \
418
    } while (0)
419
420
#define FUNC(name) FUNC_H264(READWRITE, name)
421
#include "cbs_h264_syntax_template.c"
422
#undef FUNC
423
424
#define FUNC(name) FUNC_H265(READWRITE, name)
425
#include "cbs_h265_syntax_template.c"
426
#undef FUNC
427
428
#undef WRITE
429
#undef READWRITE
430
#undef RWContext
431
#undef xu
432
#undef xi
433
#undef xue
434
#undef xse
435
#undef u
436
#undef i
437
#undef flag
438
#undef ue
439
#undef se
440
#undef infer
441
#undef more_rbsp_data
442
#undef byte_alignment
443
#undef allocate
444
445
446
1182
static void cbs_h264_free_pps(void *opaque, uint8_t *content)
447
{
448
1182
    H264RawPPS *pps = (H264RawPPS*)content;
449
1182
    av_buffer_unref(&pps->slice_group_id_ref);
450
1182
    av_freep(&content);
451
1182
}
452
453
303
static void cbs_h264_free_sei_payload(H264RawSEIPayload *payload)
454
{
455

303
    switch (payload->payload_type) {
456
293
    case H264_SEI_TYPE_BUFFERING_PERIOD:
457
    case H264_SEI_TYPE_PIC_TIMING:
458
    case H264_SEI_TYPE_PAN_SCAN_RECT:
459
    case H264_SEI_TYPE_RECOVERY_POINT:
460
    case H264_SEI_TYPE_DISPLAY_ORIENTATION:
461
    case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
462
    case H264_SEI_TYPE_ALTERNATIVE_TRANSFER:
463
293
        break;
464
5
    case H264_SEI_TYPE_USER_DATA_REGISTERED:
465
5
        av_buffer_unref(&payload->payload.user_data_registered.data_ref);
466
5
        break;
467
    case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
468
        av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
469
        break;
470
5
    default:
471
5
        av_buffer_unref(&payload->payload.other.data_ref);
472
5
        break;
473
    }
474
303
}
475
476
283
static void cbs_h264_free_sei(void *opaque, uint8_t *content)
477
{
478
283
    H264RawSEI *sei = (H264RawSEI*)content;
479
    int i;
480
586
    for (i = 0; i < sei->payload_count; i++)
481
303
        cbs_h264_free_sei_payload(&sei->payload[i]);
482
283
    av_freep(&content);
483
283
}
484
485
4913
static void cbs_h264_free_slice(void *opaque, uint8_t *content)
486
{
487
4913
    H264RawSlice *slice = (H264RawSlice*)content;
488
4913
    av_buffer_unref(&slice->data_ref);
489
4913
    av_freep(&content);
490
4913
}
491
492
48
static void cbs_h265_free_vps(void *opaque, uint8_t *content)
493
{
494
48
    H265RawVPS *vps = (H265RawVPS*)content;
495
48
    av_buffer_unref(&vps->extension_data.data_ref);
496
48
    av_freep(&content);
497
48
}
498
499
50
static void cbs_h265_free_sps(void *opaque, uint8_t *content)
500
{
501
50
    H265RawSPS *sps = (H265RawSPS*)content;
502
50
    av_buffer_unref(&sps->extension_data.data_ref);
503
50
    av_freep(&content);
504
50
}
505
506
275
static void cbs_h265_free_pps(void *opaque, uint8_t *content)
507
{
508
275
    H265RawPPS *pps = (H265RawPPS*)content;
509
275
    av_buffer_unref(&pps->extension_data.data_ref);
510
275
    av_freep(&content);
511
275
}
512
513
5864
static void cbs_h265_free_slice(void *opaque, uint8_t *content)
514
{
515
5864
    H265RawSlice *slice = (H265RawSlice*)content;
516
5864
    av_buffer_unref(&slice->data_ref);
517
5864
    av_freep(&content);
518
5864
}
519
520
1535
static void cbs_h265_free_sei_payload(H265RawSEIPayload *payload)
521
{
522

1535
    switch (payload->payload_type) {
523
1535
    case HEVC_SEI_TYPE_BUFFERING_PERIOD:
524
    case HEVC_SEI_TYPE_PICTURE_TIMING:
525
    case HEVC_SEI_TYPE_PAN_SCAN_RECT:
526
    case HEVC_SEI_TYPE_RECOVERY_POINT:
527
    case HEVC_SEI_TYPE_DISPLAY_ORIENTATION:
528
    case HEVC_SEI_TYPE_ACTIVE_PARAMETER_SETS:
529
    case HEVC_SEI_TYPE_DECODED_PICTURE_HASH:
530
    case HEVC_SEI_TYPE_TIME_CODE:
531
    case HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO:
532
    case HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
533
    case HEVC_SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
534
    case HEVC_SEI_TYPE_ALPHA_CHANNEL_INFO:
535
1535
        break;
536
    case HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
537
        av_buffer_unref(&payload->payload.user_data_registered.data_ref);
538
        break;
539
    case HEVC_SEI_TYPE_USER_DATA_UNREGISTERED:
540
        av_buffer_unref(&payload->payload.user_data_unregistered.data_ref);
541
        break;
542
    default:
543
        av_buffer_unref(&payload->payload.other.data_ref);
544
        break;
545
    }
546
1535
}
547
548
1535
static void cbs_h265_free_sei(void *opaque, uint8_t *content)
549
{
550
1535
    H265RawSEI *sei = (H265RawSEI*)content;
551
    int i;
552
3070
    for (i = 0; i < sei->payload_count; i++)
553
1535
        cbs_h265_free_sei_payload(&sei->payload[i]);
554
1535
    av_freep(&content);
555
1535
}
556
557
4502
static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
558
                                       CodedBitstreamFragment *frag,
559
                                       const H2645Packet *packet)
560
{
561
    int err, i;
562
563
18950
    for (i = 0; i < packet->nb_nals; i++) {
564
14448
        const H2645NAL *nal = &packet->nals[i];
565
        AVBufferRef *ref;
566
14448
        size_t size = nal->size;
567
568
14448
        if (nal->nuh_layer_id > 0)
569
            continue;
570
571
        // Remove trailing zeroes.
572

22282
        while (size > 0 && nal->data[size - 1] == 0)
573
7834
            --size;
574
14448
        if (size == 0) {
575
            av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
576
            continue;
577
        }
578
579
28896
        ref = (nal->data == nal->raw_data) ? frag->data_ref
580
14448
                                           : packet->rbsp.rbsp_buffer_ref;
581
582
14448
        err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
583
14448
                            (uint8_t*)nal->data, size, ref);
584
14448
        if (err < 0)
585
            return err;
586
    }
587
588
4502
    return 0;
589
}
590
591
4502
static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
592
                                    CodedBitstreamFragment *frag,
593
                                    int header)
594
{
595
4502
    enum AVCodecID codec_id = ctx->codec->codec_id;
596
4502
    CodedBitstreamH2645Context *priv = ctx->priv_data;
597
    GetByteContext gbc;
598
    int err;
599
600

4502
    av_assert0(frag->data && frag->nb_units == 0);
601
4502
    if (frag->data_size == 0)
602
        return 0;
603
604

4502
    if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
605
        // AVCC header.
606
        size_t size, start, end;
607
        int i, count, version;
608
609
        priv->mp4 = 1;
610
611
        bytestream2_init(&gbc, frag->data, frag->data_size);
612
613
        if (bytestream2_get_bytes_left(&gbc) < 6)
614
            return AVERROR_INVALIDDATA;
615
616
        version = bytestream2_get_byte(&gbc);
617
        if (version != 1) {
618
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
619
                   "first byte %u.", version);
620
            return AVERROR_INVALIDDATA;
621
        }
622
623
        bytestream2_skip(&gbc, 3);
624
        priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
625
626
        // SPS array.
627
        count = bytestream2_get_byte(&gbc) & 0x1f;
628
        start = bytestream2_tell(&gbc);
629
        for (i = 0; i < count; i++) {
630
            if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
631
                return AVERROR_INVALIDDATA;
632
            size = bytestream2_get_be16(&gbc);
633
            if (bytestream2_get_bytes_left(&gbc) < size)
634
                return AVERROR_INVALIDDATA;
635
            bytestream2_skip(&gbc, size);
636
        }
637
        end = bytestream2_tell(&gbc);
638
639
        err = ff_h2645_packet_split(&priv->read_packet,
640
                                    frag->data + start, end - start,
641
                                    ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
642
        if (err < 0) {
643
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
644
            return err;
645
        }
646
        err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
647
        if (err < 0)
648
            return err;
649
650
        // PPS array.
651
        count = bytestream2_get_byte(&gbc);
652
        start = bytestream2_tell(&gbc);
653
        for (i = 0; i < count; i++) {
654
            if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
655
                return AVERROR_INVALIDDATA;
656
            size = bytestream2_get_be16(&gbc);
657
            if (bytestream2_get_bytes_left(&gbc) < size)
658
                return AVERROR_INVALIDDATA;
659
            bytestream2_skip(&gbc, size);
660
        }
661
        end = bytestream2_tell(&gbc);
662
663
        err = ff_h2645_packet_split(&priv->read_packet,
664
                                    frag->data + start, end - start,
665
                                    ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1, 1);
666
        if (err < 0) {
667
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
668
            return err;
669
        }
670
        err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
671
        if (err < 0)
672
            return err;
673
674
        if (bytestream2_get_bytes_left(&gbc) > 0) {
675
            av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
676
                   "header.\n", bytestream2_get_bytes_left(&gbc));
677
        }
678
679

4502
    } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
680
        // HVCC header.
681
        size_t size, start, end;
682
        int i, j, nb_arrays, nal_unit_type, nb_nals, version;
683
684
        priv->mp4 = 1;
685
686
        bytestream2_init(&gbc, frag->data, frag->data_size);
687
688
        if (bytestream2_get_bytes_left(&gbc) < 23)
689
            return AVERROR_INVALIDDATA;
690
691
        version = bytestream2_get_byte(&gbc);
692
        if (version != 1) {
693
            av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
694
                   "first byte %u.", version);
695
            return AVERROR_INVALIDDATA;
696
        }
697
698
        bytestream2_skip(&gbc, 20);
699
        priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
700
701
        nb_arrays = bytestream2_get_byte(&gbc);
702
        for (i = 0; i < nb_arrays; i++) {
703
            nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
704
            nb_nals = bytestream2_get_be16(&gbc);
705
706
            start = bytestream2_tell(&gbc);
707
            for (j = 0; j < nb_nals; j++) {
708
                if (bytestream2_get_bytes_left(&gbc) < 2)
709
                    return AVERROR_INVALIDDATA;
710
                size = bytestream2_get_be16(&gbc);
711
                if (bytestream2_get_bytes_left(&gbc) < size)
712
                    return AVERROR_INVALIDDATA;
713
                bytestream2_skip(&gbc, size);
714
            }
715
            end = bytestream2_tell(&gbc);
716
717
            err = ff_h2645_packet_split(&priv->read_packet,
718
                                        frag->data + start, end - start,
719
                                        ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1, 1);
720
            if (err < 0) {
721
                av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
722
                       "HVCC array %d (%d NAL units of type %d).\n",
723
                       i, nb_nals, nal_unit_type);
724
                return err;
725
            }
726
            err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
727
            if (err < 0)
728
                return err;
729
        }
730
731
    } else {
732
        // Annex B, or later MP4 with already-known parameters.
733
734
4502
        err = ff_h2645_packet_split(&priv->read_packet,
735
4502
                                    frag->data, frag->data_size,
736
                                    ctx->log_ctx,
737
                                    priv->mp4, priv->nal_length_size,
738
                                    codec_id, 1, 1);
739
4502
        if (err < 0)
740
            return err;
741
742
4502
        err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
743
4502
        if (err < 0)
744
            return err;
745
    }
746
747
4502
    return 0;
748
}
749
750
#define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
751
static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
752
                                                  CodedBitstreamUnit *unit)  \
753
{ \
754
    CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
755
    H26 ## h26n ## Raw ## ps_name *ps_var = unit->content; \
756
    unsigned int id = ps_var->id_element; \
757
    if (id >= FF_ARRAY_ELEMS(priv->ps_var)) { \
758
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
759
               " id : %d.\n", id); \
760
        return AVERROR_INVALIDDATA; \
761
    } \
762
    if (priv->ps_var[id] == priv->active_ ## ps_var) \
763
        priv->active_ ## ps_var = NULL ; \
764
    av_buffer_unref(&priv->ps_var ## _ref[id]); \
765
    if (unit->content_ref) \
766
        priv->ps_var ## _ref[id] = av_buffer_ref(unit->content_ref); \
767
    else \
768
        priv->ps_var ## _ref[id] = av_buffer_alloc(sizeof(*ps_var)); \
769
    if (!priv->ps_var ## _ref[id]) \
770
        return AVERROR(ENOMEM); \
771
    priv->ps_var[id] = (H26 ## h26n ## Raw ## ps_name *)priv->ps_var ## _ref[id]->data; \
772
    if (!unit->content_ref) \
773
        memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
774
    return 0; \
775
}
776
777


80
cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
778


2364
cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
779


96
cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
780


100
cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
781


550
cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
782
783
6616
static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
784
                                  CodedBitstreamUnit *unit)
785
{
786
    GetBitContext gbc;
787
    int err;
788
789
6616
    err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
790
6616
    if (err < 0)
791
        return err;
792
793


6616
    switch (unit->type) {
794
40
    case H264_NAL_SPS:
795
        {
796
            H264RawSPS *sps;
797
798
40
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
799
40
            if (err < 0)
800
                return err;
801
40
            sps = unit->content;
802
803
40
            err = cbs_h264_read_sps(ctx, &gbc, sps);
804
40
            if (err < 0)
805
                return err;
806
807
40
            err = cbs_h264_replace_sps(ctx, unit);
808
40
            if (err < 0)
809
                return err;
810
        }
811
40
        break;
812
813
    case H264_NAL_SPS_EXT:
814
        {
815
            err = ff_cbs_alloc_unit_content(ctx, unit,
816
                                            sizeof(H264RawSPSExtension),
817
                                            NULL);
818
            if (err < 0)
819
                return err;
820
821
            err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
822
            if (err < 0)
823
                return err;
824
        }
825
        break;
826
827
1182
    case H264_NAL_PPS:
828
        {
829
            H264RawPPS *pps;
830
831
1182
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
832
                                            &cbs_h264_free_pps);
833
1182
            if (err < 0)
834
                return err;
835
1182
            pps = unit->content;
836
837
1182
            err = cbs_h264_read_pps(ctx, &gbc, pps);
838
1182
            if (err < 0)
839
                return err;
840
841
1182
            err = cbs_h264_replace_pps(ctx, unit);
842
1182
            if (err < 0)
843
                return err;
844
        }
845
1182
        break;
846
847
4913
    case H264_NAL_SLICE:
848
    case H264_NAL_IDR_SLICE:
849
    case H264_NAL_AUXILIARY_SLICE:
850
        {
851
            H264RawSlice *slice;
852
            int pos, len;
853
854
4913
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
855
                                            &cbs_h264_free_slice);
856
4913
            if (err < 0)
857
                return err;
858
4913
            slice = unit->content;
859
860
4913
            err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
861
4913
            if (err < 0)
862
                return err;
863
864
4913
            pos = get_bits_count(&gbc);
865
4913
            len = unit->data_size;
866
4913
            if (!unit->data[len - 1]) {
867
                int z;
868
                for (z = 0; z < len && !unit->data[len - z - 1]; z++);
869
                av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
870
                       "from slice data.\n", z);
871
                len -= z;
872
            }
873
874
4913
            slice->data_size = len - pos / 8;
875
4913
            slice->data_ref  = av_buffer_ref(unit->data_ref);
876
4913
            if (!slice->data_ref)
877
                return AVERROR(ENOMEM);
878
4913
            slice->data = unit->data + pos / 8;
879
4913
            slice->data_bit_start = pos % 8;
880
        }
881
4913
        break;
882
883
195
    case H264_NAL_AUD:
884
        {
885
195
            err = ff_cbs_alloc_unit_content(ctx, unit,
886
                                            sizeof(H264RawAUD), NULL);
887
195
            if (err < 0)
888
                return err;
889
890
195
            err = cbs_h264_read_aud(ctx, &gbc, unit->content);
891
195
            if (err < 0)
892
                return err;
893
        }
894
195
        break;
895
896
283
    case H264_NAL_SEI:
897
        {
898
283
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
899
                                            &cbs_h264_free_sei);
900
283
            if (err < 0)
901
                return err;
902
903
283
            err = cbs_h264_read_sei(ctx, &gbc, unit->content);
904
283
            if (err < 0)
905
                return err;
906
        }
907
283
        break;
908
909
    case H264_NAL_FILLER_DATA:
910
        {
911
            err = ff_cbs_alloc_unit_content(ctx, unit,
912
                                            sizeof(H264RawFiller), NULL);
913
            if (err < 0)
914
                return err;
915
916
            err = cbs_h264_read_filler(ctx, &gbc, unit->content);
917
            if (err < 0)
918
                return err;
919
        }
920
        break;
921
922
3
    case H264_NAL_END_SEQUENCE:
923
    case H264_NAL_END_STREAM:
924
        {
925
3
            err = ff_cbs_alloc_unit_content(ctx, unit,
926
                                            sizeof(H264RawNALUnitHeader),
927
                                            NULL);
928
3
            if (err < 0)
929
                return err;
930
931
3
            err = (unit->type == H264_NAL_END_SEQUENCE ?
932
3
                   cbs_h264_read_end_of_sequence :
933
3
                   cbs_h264_read_end_of_stream)(ctx, &gbc, unit->content);
934
3
            if (err < 0)
935
                return err;
936
        }
937
3
        break;
938
939
    default:
940
        return AVERROR(ENOSYS);
941
    }
942
943
6616
    return 0;
944
}
945
946
7832
static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
947
                                  CodedBitstreamUnit *unit)
948
{
949
    GetBitContext gbc;
950
    int err;
951
952
7832
    err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
953
7832
    if (err < 0)
954
        return err;
955
956

7832
    switch (unit->type) {
957
48
    case HEVC_NAL_VPS:
958
        {
959
            H265RawVPS *vps;
960
961
48
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
962
                                            &cbs_h265_free_vps);
963
48
            if (err < 0)
964
                return err;
965
48
            vps = unit->content;
966
967
48
            err = cbs_h265_read_vps(ctx, &gbc, vps);
968
48
            if (err < 0)
969
                return err;
970
971
48
            err = cbs_h265_replace_vps(ctx, unit);
972
48
            if (err < 0)
973
                return err;
974
        }
975
48
        break;
976
50
    case HEVC_NAL_SPS:
977
        {
978
            H265RawSPS *sps;
979
980
50
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
981
                                            &cbs_h265_free_sps);
982
50
            if (err < 0)
983
                return err;
984
50
            sps = unit->content;
985
986
50
            err = cbs_h265_read_sps(ctx, &gbc, sps);
987
50
            if (err < 0)
988
                return err;
989
990
50
            err = cbs_h265_replace_sps(ctx, unit);
991
50
            if (err < 0)
992
                return err;
993
        }
994
50
        break;
995
996
275
    case HEVC_NAL_PPS:
997
        {
998
            H265RawPPS *pps;
999
1000
275
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
1001
                                            &cbs_h265_free_pps);
1002
275
            if (err < 0)
1003
                return err;
1004
275
            pps = unit->content;
1005
1006
275
            err = cbs_h265_read_pps(ctx, &gbc, pps);
1007
275
            if (err < 0)
1008
                return err;
1009
1010
275
            err = cbs_h265_replace_pps(ctx, unit);
1011
275
            if (err < 0)
1012
                return err;
1013
        }
1014
275
        break;
1015
1016
5864
    case HEVC_NAL_TRAIL_N:
1017
    case HEVC_NAL_TRAIL_R:
1018
    case HEVC_NAL_TSA_N:
1019
    case HEVC_NAL_TSA_R:
1020
    case HEVC_NAL_STSA_N:
1021
    case HEVC_NAL_STSA_R:
1022
    case HEVC_NAL_RADL_N:
1023
    case HEVC_NAL_RADL_R:
1024
    case HEVC_NAL_RASL_N:
1025
    case HEVC_NAL_RASL_R:
1026
    case HEVC_NAL_BLA_W_LP:
1027
    case HEVC_NAL_BLA_W_RADL:
1028
    case HEVC_NAL_BLA_N_LP:
1029
    case HEVC_NAL_IDR_W_RADL:
1030
    case HEVC_NAL_IDR_N_LP:
1031
    case HEVC_NAL_CRA_NUT:
1032
        {
1033
            H265RawSlice *slice;
1034
            int pos, len;
1035
1036
5864
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
1037
                                            &cbs_h265_free_slice);
1038
5864
            if (err < 0)
1039
                return err;
1040
5864
            slice = unit->content;
1041
1042
5864
            err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
1043
5864
            if (err < 0)
1044
                return err;
1045
1046
5864
            pos = get_bits_count(&gbc);
1047
5864
            len = unit->data_size;
1048
5864
            if (!unit->data[len - 1]) {
1049
                int z;
1050
                for (z = 0; z < len && !unit->data[len - z - 1]; z++);
1051
                av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
1052
                       "from slice data.\n", z);
1053
                len -= z;
1054
            }
1055
1056
5864
            slice->data_size = len - pos / 8;
1057
5864
            slice->data_ref  = av_buffer_ref(unit->data_ref);
1058
5864
            if (!slice->data_ref)
1059
                return AVERROR(ENOMEM);
1060
5864
            slice->data = unit->data + pos / 8;
1061
5864
            slice->data_bit_start = pos % 8;
1062
        }
1063
5864
        break;
1064
1065
60
    case HEVC_NAL_AUD:
1066
        {
1067
60
            err = ff_cbs_alloc_unit_content(ctx, unit,
1068
                                            sizeof(H265RawAUD), NULL);
1069
60
            if (err < 0)
1070
                return err;
1071
1072
60
            err = cbs_h265_read_aud(ctx, &gbc, unit->content);
1073
60
            if (err < 0)
1074
                return err;
1075
        }
1076
60
        break;
1077
1078
1535
    case HEVC_NAL_SEI_PREFIX:
1079
    case HEVC_NAL_SEI_SUFFIX:
1080
        {
1081
1535
            err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H265RawSEI),
1082
                                            &cbs_h265_free_sei);
1083
1084
1535
            if (err < 0)
1085
                return err;
1086
1087
1535
            err = cbs_h265_read_sei(ctx, &gbc, unit->content,
1088
1535
                                    unit->type == HEVC_NAL_SEI_PREFIX);
1089
1090
1535
            if (err < 0)
1091
                return err;
1092
        }
1093
1535
        break;
1094
1095
    default:
1096
        return AVERROR(ENOSYS);
1097
    }
1098
1099
7832
    return 0;
1100
}
1101
1102
10777
static int cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
1103
                                      PutBitContext *pbc, const uint8_t *data,
1104
                                      size_t data_size, int data_bit_start)
1105
{
1106
10777
    size_t rest  = data_size - (data_bit_start + 7) / 8;
1107
10777
    const uint8_t *pos = data + data_bit_start / 8;
1108
1109

10777
    av_assert0(data_bit_start >= 0 &&
1110
               data_size > data_bit_start / 8);
1111
1112
10777
    if (data_size * 8 + 8 > put_bits_left(pbc))
1113
        return AVERROR(ENOSPC);
1114
1115
10777
    if (!rest)
1116
1
        goto rbsp_stop_one_bit;
1117
1118
    // First copy the remaining bits of the first byte
1119
    // The above check ensures that we do not accidentally
1120
    // copy beyond the rbsp_stop_one_bit.
1121
10776
    if (data_bit_start % 8)
1122
3547
        put_bits(pbc, 8 - data_bit_start % 8,
1123
3547
                 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
1124
1125
10776
    if (put_bits_count(pbc) % 8 == 0) {
1126
        // If the writer is aligned at this point,
1127
        // memcpy can be used to improve performance.
1128
        // This happens normally for CABAC.
1129
10776
        flush_put_bits(pbc);
1130
10776
        memcpy(put_bits_ptr(pbc), pos, rest);
1131
10776
        skip_put_bytes(pbc, rest);
1132
    } else {
1133
        // If not, we have to copy manually.
1134
        // rbsp_stop_one_bit forces us to special-case
1135
        // the last byte.
1136
        uint8_t temp;
1137
        int i;
1138
1139
        for (; rest > 4; rest -= 4, pos += 4)
1140
            put_bits32(pbc, AV_RB32(pos));
1141
1142
        for (; rest > 1; rest--, pos++)
1143
            put_bits(pbc, 8, *pos);
1144
1145
    rbsp_stop_one_bit:
1146
1
        temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
1147
1148
1
        av_assert0(temp);
1149
1
        i = ff_ctz(*pos);
1150
1
        temp = temp >> i;
1151
1
        i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
1152
1
        put_bits(pbc, i, temp);
1153
1
        if (put_bits_count(pbc) % 8)
1154
            put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
1155
    }
1156
1157
10777
    return 0;
1158
}
1159
1160
6616
static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
1161
                                   CodedBitstreamUnit *unit,
1162
                                   PutBitContext *pbc)
1163
{
1164
    int err;
1165
1166


6616
    switch (unit->type) {
1167
40
    case H264_NAL_SPS:
1168
        {
1169
40
            H264RawSPS *sps = unit->content;
1170
1171
40
            err = cbs_h264_write_sps(ctx, pbc, sps);
1172
40
            if (err < 0)
1173
                return err;
1174
1175
40
            err = cbs_h264_replace_sps(ctx, unit);
1176
40
            if (err < 0)
1177
                return err;
1178
        }
1179
40
        break;
1180
1181
    case H264_NAL_SPS_EXT:
1182
        {
1183
            H264RawSPSExtension *sps_ext = unit->content;
1184
1185
            err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
1186
            if (err < 0)
1187
                return err;
1188
        }
1189
        break;
1190
1191
1182
    case H264_NAL_PPS:
1192
        {
1193
1182
            H264RawPPS *pps = unit->content;
1194
1195
1182
            err = cbs_h264_write_pps(ctx, pbc, pps);
1196
1182
            if (err < 0)
1197
                return err;
1198
1199
1182
            err = cbs_h264_replace_pps(ctx, unit);
1200
1182
            if (err < 0)
1201
                return err;
1202
        }
1203
1182
        break;
1204
1205
4913
    case H264_NAL_SLICE:
1206
    case H264_NAL_IDR_SLICE:
1207
    case H264_NAL_AUXILIARY_SLICE:
1208
        {
1209
4913
            H264RawSlice *slice = unit->content;
1210
1211
4913
            err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
1212
4913
            if (err < 0)
1213
                return err;
1214
1215
4913
            if (slice->data) {
1216
4913
                err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1217
                                                 slice->data_size,
1218
                                                 slice->data_bit_start);
1219
4913
                if (err < 0)
1220
                    return err;
1221
            } else {
1222
                // No slice data - that was just the header.
1223
                // (Bitstream may be unaligned!)
1224
            }
1225
        }
1226
4913
        break;
1227
1228
195
    case H264_NAL_AUD:
1229
        {
1230
195
            err = cbs_h264_write_aud(ctx, pbc, unit->content);
1231
195
            if (err < 0)
1232
                return err;
1233
        }
1234
195
        break;
1235
1236
283
    case H264_NAL_SEI:
1237
        {
1238
283
            err = cbs_h264_write_sei(ctx, pbc, unit->content);
1239
283
            if (err < 0)
1240
                return err;
1241
        }
1242
283
        break;
1243
1244
    case H264_NAL_FILLER_DATA:
1245
        {
1246
            err = cbs_h264_write_filler(ctx, pbc, unit->content);
1247
            if (err < 0)
1248
                return err;
1249
        }
1250
        break;
1251
1252
1
    case H264_NAL_END_SEQUENCE:
1253
        {
1254
1
            err = cbs_h264_write_end_of_sequence(ctx, pbc, unit->content);
1255
1
            if (err < 0)
1256
                return err;
1257
        }
1258
1
        break;
1259
1260
2
    case H264_NAL_END_STREAM:
1261
        {
1262
2
            err = cbs_h264_write_end_of_stream(ctx, pbc, unit->content);
1263
2
            if (err < 0)
1264
                return err;
1265
        }
1266
2
        break;
1267
1268
    default:
1269
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1270
               "NAL unit type %"PRIu32".\n", unit->type);
1271
        return AVERROR_PATCHWELCOME;
1272
    }
1273
1274
6616
    return 0;
1275
}
1276
1277
7832
static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
1278
                                   CodedBitstreamUnit *unit,
1279
                                   PutBitContext *pbc)
1280
{
1281
    int err;
1282
1283

7832
    switch (unit->type) {
1284
48
    case HEVC_NAL_VPS:
1285
        {
1286
48
            H265RawVPS *vps = unit->content;
1287
1288
48
            err = cbs_h265_write_vps(ctx, pbc, vps);
1289
48
            if (err < 0)
1290
                return err;
1291
1292
48
            err = cbs_h265_replace_vps(ctx, unit);
1293
48
            if (err < 0)
1294
                return err;
1295
        }
1296
48
        break;
1297
1298
50
    case HEVC_NAL_SPS:
1299
        {
1300
50
            H265RawSPS *sps = unit->content;
1301
1302
50
            err = cbs_h265_write_sps(ctx, pbc, sps);
1303
50
            if (err < 0)
1304
                return err;
1305
1306
50
            err = cbs_h265_replace_sps(ctx, unit);
1307
50
            if (err < 0)
1308
                return err;
1309
        }
1310
50
        break;
1311
1312
275
    case HEVC_NAL_PPS:
1313
        {
1314
275
            H265RawPPS *pps = unit->content;
1315
1316
275
            err = cbs_h265_write_pps(ctx, pbc, pps);
1317
275
            if (err < 0)
1318
                return err;
1319
1320
275
            err = cbs_h265_replace_pps(ctx, unit);
1321
275
            if (err < 0)
1322
                return err;
1323
        }
1324
275
        break;
1325
1326
5864
    case HEVC_NAL_TRAIL_N:
1327
    case HEVC_NAL_TRAIL_R:
1328
    case HEVC_NAL_TSA_N:
1329
    case HEVC_NAL_TSA_R:
1330
    case HEVC_NAL_STSA_N:
1331
    case HEVC_NAL_STSA_R:
1332
    case HEVC_NAL_RADL_N:
1333
    case HEVC_NAL_RADL_R:
1334
    case HEVC_NAL_RASL_N:
1335
    case HEVC_NAL_RASL_R:
1336
    case HEVC_NAL_BLA_W_LP:
1337
    case HEVC_NAL_BLA_W_RADL:
1338
    case HEVC_NAL_BLA_N_LP:
1339
    case HEVC_NAL_IDR_W_RADL:
1340
    case HEVC_NAL_IDR_N_LP:
1341
    case HEVC_NAL_CRA_NUT:
1342
        {
1343
5864
            H265RawSlice *slice = unit->content;
1344
1345
5864
            err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
1346
5864
            if (err < 0)
1347
                return err;
1348
1349
5864
            if (slice->data) {
1350
5864
                err = cbs_h2645_write_slice_data(ctx, pbc, slice->data,
1351
                                                 slice->data_size,
1352
                                                 slice->data_bit_start);
1353
5864
                if (err < 0)
1354
                    return err;
1355
            } else {
1356
                // No slice data - that was just the header.
1357
            }
1358
        }
1359
5864
        break;
1360
1361
60
    case HEVC_NAL_AUD:
1362
        {
1363
60
            err = cbs_h265_write_aud(ctx, pbc, unit->content);
1364
60
            if (err < 0)
1365
                return err;
1366
        }
1367
60
        break;
1368
1369
1535
    case HEVC_NAL_SEI_PREFIX:
1370
    case HEVC_NAL_SEI_SUFFIX:
1371
        {
1372
1535
            err = cbs_h265_write_sei(ctx, pbc, unit->content,
1373
1535
                                     unit->type == HEVC_NAL_SEI_PREFIX);
1374
1375
1535
            if (err < 0)
1376
                return err;
1377
        }
1378
1535
        break;
1379
1380
    default:
1381
        av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
1382
               "NAL unit type %"PRIu32".\n", unit->type);
1383
        return AVERROR_PATCHWELCOME;
1384
    }
1385
1386
7832
    return 0;
1387
}
1388
1389
4502
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
1390
                                       CodedBitstreamFragment *frag)
1391
{
1392
    uint8_t *data;
1393
    size_t max_size, dp, sp;
1394
    int err, i, zero_run;
1395
1396
18950
    for (i = 0; i < frag->nb_units; i++) {
1397
        // Data should already all have been written when we get here.
1398
14448
        av_assert0(frag->units[i].data);
1399
    }
1400
1401
4502
    max_size = 0;
1402
18950
    for (i = 0; i < frag->nb_units; i++) {
1403
        // Start code + content with worst-case emulation prevention.
1404
14448
        max_size += 4 + frag->units[i].data_size * 3 / 2;
1405
    }
1406
1407
4502
    data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
1408
4502
    if (!data)
1409
        return AVERROR(ENOMEM);
1410
1411
4502
    dp = 0;
1412
18950
    for (i = 0; i < frag->nb_units; i++) {
1413
14448
        CodedBitstreamUnit *unit = &frag->units[i];
1414
1415
14448
        if (unit->data_bit_padding > 0) {
1416
            if (i < frag->nb_units - 1)
1417
                av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
1418
                       "unaligned padding on non-final NAL unit.\n");
1419
            else
1420
                frag->data_bit_padding = unit->data_bit_padding;
1421
        }
1422
1423
14448
        if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
1424
6616
             (unit->type == H264_NAL_SPS ||
1425
6576
              unit->type == H264_NAL_PPS)) ||
1426
13226
            (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
1427
7832
             (unit->type == HEVC_NAL_VPS ||
1428
7784
              unit->type == HEVC_NAL_SPS ||
1429

13128
              unit->type == HEVC_NAL_PPS)) ||
1430
            i == 0 /* (Assume this is the start of an access unit.) */) {
1431
            // zero_byte
1432
5026
            data[dp++] = 0;
1433
        }
1434
        // start_code_prefix_one_3bytes
1435
14448
        data[dp++] = 0;
1436
14448
        data[dp++] = 0;
1437
14448
        data[dp++] = 1;
1438
1439
14448
        zero_run = 0;
1440
10695035
        for (sp = 0; sp < unit->data_size; sp++) {
1441
10680587
            if (zero_run < 2) {
1442
10676594
                if (unit->data[sp] == 0)
1443
106112
                    ++zero_run;
1444
                else
1445
10570482
                    zero_run = 0;
1446
            } else {
1447
3993
                if ((unit->data[sp] & ~3) == 0) {
1448
                    // emulation_prevention_three_byte
1449
748
                    data[dp++] = 3;
1450
                }
1451
3993
                zero_run = unit->data[sp] == 0;
1452
            }
1453
10680587
            data[dp++] = unit->data[sp];
1454
        }
1455
    }
1456
1457
4502
    av_assert0(dp <= max_size);
1458
4502
    err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
1459
4502
    if (err)
1460
        return err;
1461
4502
    memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1462
1463
4502
    frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
1464
                                      NULL, NULL, 0);
1465
4502
    if (!frag->data_ref) {
1466
        av_freep(&data);
1467
        return AVERROR(ENOMEM);
1468
    }
1469
1470
4502
    frag->data = data;
1471
4502
    frag->data_size = dp;
1472
1473
4502
    return 0;
1474
}
1475
1476
15
static void cbs_h264_close(CodedBitstreamContext *ctx)
1477
{
1478
15
    CodedBitstreamH264Context *h264 = ctx->priv_data;
1479
    int i;
1480
1481
15
    ff_h2645_packet_uninit(&h264->common.read_packet);
1482
1483
495
    for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
1484
480
        av_buffer_unref(&h264->sps_ref[i]);
1485
3855
    for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
1486
3840
        av_buffer_unref(&h264->pps_ref[i]);
1487
15
}
1488
1489
20
static void cbs_h265_close(CodedBitstreamContext *ctx)
1490
{
1491
20
    CodedBitstreamH265Context *h265 = ctx->priv_data;
1492
    int i;
1493
1494
20
    ff_h2645_packet_uninit(&h265->common.read_packet);
1495
1496
340
    for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
1497
320
        av_buffer_unref(&h265->vps_ref[i]);
1498
340
    for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
1499
320
        av_buffer_unref(&h265->sps_ref[i]);
1500
1300
    for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
1501
1280
        av_buffer_unref(&h265->pps_ref[i]);
1502
20
}
1503
1504
const CodedBitstreamType ff_cbs_type_h264 = {
1505
    .codec_id          = AV_CODEC_ID_H264,
1506
1507
    .priv_data_size    = sizeof(CodedBitstreamH264Context),
1508
1509
    .split_fragment    = &cbs_h2645_split_fragment,
1510
    .read_unit         = &cbs_h264_read_nal_unit,
1511
    .write_unit        = &cbs_h264_write_nal_unit,
1512
    .assemble_fragment = &cbs_h2645_assemble_fragment,
1513
1514
    .close             = &cbs_h264_close,
1515
};
1516
1517
const CodedBitstreamType ff_cbs_type_h265 = {
1518
    .codec_id          = AV_CODEC_ID_HEVC,
1519
1520
    .priv_data_size    = sizeof(CodedBitstreamH265Context),
1521
1522
    .split_fragment    = &cbs_h2645_split_fragment,
1523
    .read_unit         = &cbs_h265_read_nal_unit,
1524
    .write_unit        = &cbs_h265_write_nal_unit,
1525
    .assemble_fragment = &cbs_h2645_assemble_fragment,
1526
1527
    .close             = &cbs_h265_close,
1528
};
1529
1530
int ff_cbs_h264_add_sei_message(CodedBitstreamContext *ctx,
1531
                                CodedBitstreamFragment *au,
1532
                                H264RawSEIPayload *payload)
1533
{
1534
    H264RawSEI *sei = NULL;
1535
    int err, i;
1536
1537
    // Find an existing SEI NAL unit to add to.
1538
    for (i = 0; i < au->nb_units; i++) {
1539
        if (au->units[i].type == H264_NAL_SEI) {
1540
            sei = au->units[i].content;
1541
            if (sei->payload_count < H264_MAX_SEI_PAYLOADS)
1542
                break;
1543
1544
            sei = NULL;
1545
        }
1546
    }
1547
1548
    if (!sei) {
1549
        // Need to make a new SEI NAL unit.  Insert it before the first
1550
        // slice data NAL unit; if no slice data, add at the end.
1551
        AVBufferRef *sei_ref;
1552
1553
        sei = av_mallocz(sizeof(*sei));
1554
        if (!sei) {
1555
            err = AVERROR(ENOMEM);
1556
            goto fail;
1557
        }
1558
1559
        sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
1560
        sei->nal_unit_header.nal_ref_idc   = 0;
1561
1562
        sei_ref = av_buffer_create((uint8_t*)sei, sizeof(*sei),
1563
                                   &cbs_h264_free_sei, NULL, 0);
1564
        if (!sei_ref) {
1565
            av_freep(&sei);
1566
            err = AVERROR(ENOMEM);
1567
            goto fail;
1568
        }
1569
1570
        for (i = 0; i < au->nb_units; i++) {
1571
            if (au->units[i].type == H264_NAL_SLICE ||
1572
                au->units[i].type == H264_NAL_IDR_SLICE)
1573
                break;
1574
        }
1575
1576
        err = ff_cbs_insert_unit_content(ctx, au, i, H264_NAL_SEI,
1577
                                         sei, sei_ref);
1578
        av_buffer_unref(&sei_ref);
1579
        if (err < 0)
1580
            goto fail;
1581
    }
1582
1583
    memcpy(&sei->payload[sei->payload_count], payload, sizeof(*payload));
1584
    ++sei->payload_count;
1585
1586
    return 0;
1587
fail:
1588
    cbs_h264_free_sei_payload(payload);
1589
    return err;
1590
}
1591
1592
void ff_cbs_h264_delete_sei_message(CodedBitstreamContext *ctx,
1593
                                    CodedBitstreamFragment *au,
1594
                                    CodedBitstreamUnit *nal,
1595
                                    int position)
1596
{
1597
    H264RawSEI *sei = nal->content;
1598
1599
    av_assert0(nal->type == H264_NAL_SEI);
1600
    av_assert0(position >= 0 && position < sei->payload_count);
1601
1602
    if (position == 0 && sei->payload_count == 1) {
1603
        // Deleting NAL unit entirely.
1604
        int i;
1605
1606
        for (i = 0; i < au->nb_units; i++) {
1607
            if (&au->units[i] == nal)
1608
                break;
1609
        }
1610
1611
        ff_cbs_delete_unit(ctx, au, i);
1612
    } else {
1613
        cbs_h264_free_sei_payload(&sei->payload[position]);
1614
1615
        --sei->payload_count;
1616
        memmove(sei->payload + position,
1617
                sei->payload + position + 1,
1618
                (sei->payload_count - position) * sizeof(*sei->payload));
1619
    }
1620
}