]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs.c
Merge commit 'a957e9379d11f2982d615f92c30580a57ea8bb40'
[ffmpeg] / libavcodec / cbs.c
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 <string.h>
20
21 #include "config.h"
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/buffer.h"
25 #include "libavutil/common.h"
26
27 #include "cbs.h"
28 #include "cbs_internal.h"
29
30
31 static const CodedBitstreamType *cbs_type_table[] = {
32 #if CONFIG_CBS_AV1
33     &ff_cbs_type_av1,
34 #endif
35 #if CONFIG_CBS_H264
36     &ff_cbs_type_h264,
37 #endif
38 #if CONFIG_CBS_H265
39     &ff_cbs_type_h265,
40 #endif
41 #if CONFIG_CBS_JPEG
42     &ff_cbs_type_jpeg,
43 #endif
44 #if CONFIG_CBS_MPEG2
45     &ff_cbs_type_mpeg2,
46 #endif
47 #if CONFIG_CBS_VP9
48     &ff_cbs_type_vp9,
49 #endif
50 };
51
52 const enum AVCodecID ff_cbs_all_codec_ids[] = {
53 #if CONFIG_CBS_AV1
54     AV_CODEC_ID_AV1,
55 #endif
56 #if CONFIG_CBS_H264
57     AV_CODEC_ID_H264,
58 #endif
59 #if CONFIG_CBS_H265
60     AV_CODEC_ID_H265,
61 #endif
62 #if CONFIG_CBS_JPEG
63     AV_CODEC_ID_MJPEG,
64 #endif
65 #if CONFIG_CBS_MPEG2
66     AV_CODEC_ID_MPEG2VIDEO,
67 #endif
68 #if CONFIG_CBS_VP9
69     AV_CODEC_ID_VP9,
70 #endif
71     AV_CODEC_ID_NONE
72 };
73
74 int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
75                 enum AVCodecID codec_id, void *log_ctx)
76 {
77     CodedBitstreamContext *ctx;
78     const CodedBitstreamType *type;
79     int i;
80
81     type = NULL;
82     for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
83         if (cbs_type_table[i]->codec_id == codec_id) {
84             type = cbs_type_table[i];
85             break;
86         }
87     }
88     if (!type)
89         return AVERROR(EINVAL);
90
91     ctx = av_mallocz(sizeof(*ctx));
92     if (!ctx)
93         return AVERROR(ENOMEM);
94
95     ctx->log_ctx = log_ctx;
96     ctx->codec   = type;
97
98     ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
99     if (!ctx->priv_data) {
100         av_freep(&ctx);
101         return AVERROR(ENOMEM);
102     }
103
104     ctx->decompose_unit_types = NULL;
105
106     ctx->trace_enable = 0;
107     ctx->trace_level  = AV_LOG_TRACE;
108
109     *ctx_ptr = ctx;
110     return 0;
111 }
112
113 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
114 {
115     CodedBitstreamContext *ctx = *ctx_ptr;
116
117     if (!ctx)
118         return;
119
120     if (ctx->codec && ctx->codec->close)
121         ctx->codec->close(ctx);
122
123     av_freep(&ctx->priv_data);
124     av_freep(ctx_ptr);
125 }
126
127 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
128                             CodedBitstreamUnit *unit)
129 {
130     av_buffer_unref(&unit->content_ref);
131     unit->content = NULL;
132
133     av_buffer_unref(&unit->data_ref);
134     unit->data             = NULL;
135     unit->data_size        = 0;
136     unit->data_bit_padding = 0;
137 }
138
139 void ff_cbs_fragment_reset(CodedBitstreamContext *ctx,
140                            CodedBitstreamFragment *frag)
141 {
142     int i;
143
144     for (i = 0; i < frag->nb_units; i++)
145         cbs_unit_uninit(ctx, &frag->units[i]);
146     frag->nb_units = 0;
147
148     av_buffer_unref(&frag->data_ref);
149     frag->data             = NULL;
150     frag->data_size        = 0;
151     frag->data_bit_padding = 0;
152 }
153
154 void ff_cbs_fragment_free(CodedBitstreamContext *ctx,
155                           CodedBitstreamFragment *frag)
156 {
157     ff_cbs_fragment_reset(ctx, frag);
158
159     av_freep(&frag->units);
160     frag->nb_units_allocated = 0;
161 }
162
163 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
164                                      CodedBitstreamFragment *frag)
165 {
166     int err, i, j;
167
168     for (i = 0; i < frag->nb_units; i++) {
169         CodedBitstreamUnit *unit = &frag->units[i];
170
171         if (ctx->decompose_unit_types) {
172             for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
173                 if (ctx->decompose_unit_types[j] == unit->type)
174                     break;
175             }
176             if (j >= ctx->nb_decompose_unit_types)
177                 continue;
178         }
179
180         av_buffer_unref(&unit->content_ref);
181         unit->content = NULL;
182
183         av_assert0(unit->data && unit->data_ref);
184
185         err = ctx->codec->read_unit(ctx, unit);
186         if (err == AVERROR(ENOSYS)) {
187             av_log(ctx->log_ctx, AV_LOG_VERBOSE,
188                    "Decomposition unimplemented for unit %d "
189                    "(type %"PRIu32").\n", i, unit->type);
190         } else if (err < 0) {
191             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
192                    "(type %"PRIu32").\n", i, unit->type);
193             return err;
194         }
195     }
196
197     return 0;
198 }
199
200 static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
201                                   CodedBitstreamFragment *frag,
202                                   const uint8_t *data, size_t size)
203 {
204     av_assert0(!frag->data && !frag->data_ref);
205
206     frag->data_ref =
207         av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
208     if (!frag->data_ref)
209         return AVERROR(ENOMEM);
210
211     frag->data      = frag->data_ref->data;
212     frag->data_size = size;
213
214     memcpy(frag->data, data, size);
215     memset(frag->data + size, 0,
216            AV_INPUT_BUFFER_PADDING_SIZE);
217
218     return 0;
219 }
220
221 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
222                           CodedBitstreamFragment *frag,
223                           const AVCodecParameters *par)
224 {
225     int err;
226
227     err = cbs_fill_fragment_data(ctx, frag, par->extradata,
228                                  par->extradata_size);
229     if (err < 0)
230         return err;
231
232     err = ctx->codec->split_fragment(ctx, frag, 1);
233     if (err < 0)
234         return err;
235
236     return cbs_read_fragment_content(ctx, frag);
237 }
238
239 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
240                        CodedBitstreamFragment *frag,
241                        const AVPacket *pkt)
242 {
243     int err;
244
245     if (pkt->buf) {
246         frag->data_ref = av_buffer_ref(pkt->buf);
247         if (!frag->data_ref)
248             return AVERROR(ENOMEM);
249
250         frag->data      = pkt->data;
251         frag->data_size = pkt->size;
252
253     } else {
254         err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
255         if (err < 0)
256             return err;
257     }
258
259     err = ctx->codec->split_fragment(ctx, frag, 0);
260     if (err < 0)
261         return err;
262
263     return cbs_read_fragment_content(ctx, frag);
264 }
265
266 int ff_cbs_read(CodedBitstreamContext *ctx,
267                 CodedBitstreamFragment *frag,
268                 const uint8_t *data, size_t size)
269 {
270     int err;
271
272     err = cbs_fill_fragment_data(ctx, frag, data, size);
273     if (err < 0)
274         return err;
275
276     err = ctx->codec->split_fragment(ctx, frag, 0);
277     if (err < 0)
278         return err;
279
280     return cbs_read_fragment_content(ctx, frag);
281 }
282
283
284 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
285                                CodedBitstreamFragment *frag)
286 {
287     int err, i;
288
289     for (i = 0; i < frag->nb_units; i++) {
290         CodedBitstreamUnit *unit = &frag->units[i];
291
292         if (!unit->content)
293             continue;
294
295         av_buffer_unref(&unit->data_ref);
296         unit->data = NULL;
297
298         err = ctx->codec->write_unit(ctx, unit);
299         if (err < 0) {
300             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
301                    "(type %"PRIu32").\n", i, unit->type);
302             return err;
303         }
304         av_assert0(unit->data && unit->data_ref);
305     }
306
307     av_buffer_unref(&frag->data_ref);
308     frag->data = NULL;
309
310     err = ctx->codec->assemble_fragment(ctx, frag);
311     if (err < 0) {
312         av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
313         return err;
314     }
315     av_assert0(frag->data && frag->data_ref);
316
317     return 0;
318 }
319
320 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
321                            AVCodecParameters *par,
322                            CodedBitstreamFragment *frag)
323 {
324     int err;
325
326     err = ff_cbs_write_fragment_data(ctx, frag);
327     if (err < 0)
328         return err;
329
330     av_freep(&par->extradata);
331
332     par->extradata = av_malloc(frag->data_size +
333                                AV_INPUT_BUFFER_PADDING_SIZE);
334     if (!par->extradata)
335         return AVERROR(ENOMEM);
336
337     memcpy(par->extradata, frag->data, frag->data_size);
338     memset(par->extradata + frag->data_size, 0,
339            AV_INPUT_BUFFER_PADDING_SIZE);
340     par->extradata_size = frag->data_size;
341
342     return 0;
343 }
344
345 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
346                         AVPacket *pkt,
347                         CodedBitstreamFragment *frag)
348 {
349     AVBufferRef *buf;
350     int err;
351
352     err = ff_cbs_write_fragment_data(ctx, frag);
353     if (err < 0)
354         return err;
355
356     buf = av_buffer_ref(frag->data_ref);
357     if (!buf)
358         return AVERROR(ENOMEM);
359
360     av_init_packet(pkt);
361     pkt->buf  = buf;
362     pkt->data = frag->data;
363     pkt->size = frag->data_size;
364
365     return 0;
366 }
367
368
369 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
370                          const char *name)
371 {
372     if (!ctx->trace_enable)
373         return;
374
375     av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
376 }
377
378 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
379                                  const char *str, const int *subscripts,
380                                  const char *bits, int64_t value)
381 {
382     char name[256];
383     size_t name_len, bits_len;
384     int pad, subs, i, j, k, n;
385
386     if (!ctx->trace_enable)
387         return;
388
389     av_assert0(value >= INT_MIN && value <= UINT32_MAX);
390
391     subs = subscripts ? subscripts[0] : 0;
392     n = 0;
393     for (i = j = 0; str[i];) {
394         if (str[i] == '[') {
395             if (n < subs) {
396                 ++n;
397                 k = snprintf(name + j, sizeof(name) - j, "[%d", subscripts[n]);
398                 av_assert0(k > 0 && j + k < sizeof(name));
399                 j += k;
400                 for (++i; str[i] && str[i] != ']'; i++);
401                 av_assert0(str[i] == ']');
402             } else {
403                 while (str[i] && str[i] != ']')
404                     name[j++] = str[i++];
405                 av_assert0(str[i] == ']');
406             }
407         } else {
408             av_assert0(j + 1 < sizeof(name));
409             name[j++] = str[i++];
410         }
411     }
412     av_assert0(j + 1 < sizeof(name));
413     name[j] = 0;
414     av_assert0(n == subs);
415
416     name_len = strlen(name);
417     bits_len = strlen(bits);
418
419     if (name_len + bits_len > 60)
420         pad = bits_len + 2;
421     else
422         pad = 61 - name_len;
423
424     av_log(ctx->log_ctx, ctx->trace_level, "%-10d  %s%*s = %"PRId64"\n",
425            position, name, pad, bits, value);
426 }
427
428 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
429                          int width, const char *name,
430                          const int *subscripts, uint32_t *write_to,
431                          uint32_t range_min, uint32_t range_max)
432 {
433     uint32_t value;
434     int position;
435
436     av_assert0(width > 0 && width <= 32);
437
438     if (get_bits_left(gbc) < width) {
439         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
440                "%s: bitstream ended.\n", name);
441         return AVERROR_INVALIDDATA;
442     }
443
444     if (ctx->trace_enable)
445         position = get_bits_count(gbc);
446
447     value = get_bits_long(gbc, width);
448
449     if (ctx->trace_enable) {
450         char bits[33];
451         int i;
452         for (i = 0; i < width; i++)
453             bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
454         bits[i] = 0;
455
456         ff_cbs_trace_syntax_element(ctx, position, name, subscripts,
457                                     bits, value);
458     }
459
460     if (value < range_min || value > range_max) {
461         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
462                "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
463                name, value, range_min, range_max);
464         return AVERROR_INVALIDDATA;
465     }
466
467     *write_to = value;
468     return 0;
469 }
470
471 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
472                           int width, const char *name,
473                           const int *subscripts, uint32_t value,
474                           uint32_t range_min, uint32_t range_max)
475 {
476     av_assert0(width > 0 && width <= 32);
477
478     if (value < range_min || value > range_max) {
479         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
480                "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
481                name, value, range_min, range_max);
482         return AVERROR_INVALIDDATA;
483     }
484
485     if (put_bits_left(pbc) < width)
486         return AVERROR(ENOSPC);
487
488     if (ctx->trace_enable) {
489         char bits[33];
490         int i;
491         for (i = 0; i < width; i++)
492             bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
493         bits[i] = 0;
494
495         ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc),
496                                     name, subscripts, bits, value);
497     }
498
499     if (width < 32)
500         put_bits(pbc, width, value);
501     else
502         put_bits32(pbc, value);
503
504     return 0;
505 }
506
507
508 int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
509                               CodedBitstreamUnit *unit,
510                               size_t size,
511                               void (*free)(void *opaque, uint8_t *data))
512 {
513     av_assert0(!unit->content && !unit->content_ref);
514
515     unit->content = av_mallocz(size);
516     if (!unit->content)
517         return AVERROR(ENOMEM);
518
519     unit->content_ref = av_buffer_create(unit->content, size,
520                                          free, ctx, 0);
521     if (!unit->content_ref) {
522         av_freep(&unit->content);
523         return AVERROR(ENOMEM);
524     }
525
526     return 0;
527 }
528
529 int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
530                            CodedBitstreamUnit *unit,
531                            size_t size)
532 {
533     av_assert0(!unit->data && !unit->data_ref);
534
535     unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
536     if (!unit->data_ref)
537         return AVERROR(ENOMEM);
538
539     unit->data      = unit->data_ref->data;
540     unit->data_size = size;
541
542     memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
543
544     return 0;
545 }
546
547 static int cbs_insert_unit(CodedBitstreamContext *ctx,
548                            CodedBitstreamFragment *frag,
549                            int position)
550 {
551     CodedBitstreamUnit *units;
552
553     if (frag->nb_units < frag->nb_units_allocated) {
554         units = frag->units;
555
556         if (position < frag->nb_units)
557             memmove(units + position + 1, units + position,
558                     (frag->nb_units - position) * sizeof(*units));
559     } else {
560         units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
561         if (!units)
562             return AVERROR(ENOMEM);
563
564         ++frag->nb_units_allocated;
565
566         if (position > 0)
567             memcpy(units, frag->units, position * sizeof(*units));
568
569         if (position < frag->nb_units)
570             memcpy(units + position + 1, frag->units + position,
571                    (frag->nb_units - position) * sizeof(*units));
572     }
573
574     memset(units + position, 0, sizeof(*units));
575
576     if (units != frag->units) {
577         av_free(frag->units);
578         frag->units = units;
579     }
580
581     ++frag->nb_units;
582
583     return 0;
584 }
585
586 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
587                                CodedBitstreamFragment *frag,
588                                int position,
589                                CodedBitstreamUnitType type,
590                                void *content,
591                                AVBufferRef *content_buf)
592 {
593     CodedBitstreamUnit *unit;
594     AVBufferRef *content_ref;
595     int err;
596
597     if (position == -1)
598         position = frag->nb_units;
599     av_assert0(position >= 0 && position <= frag->nb_units);
600
601     if (content_buf) {
602         content_ref = av_buffer_ref(content_buf);
603         if (!content_ref)
604             return AVERROR(ENOMEM);
605     } else {
606         content_ref = NULL;
607     }
608
609     err = cbs_insert_unit(ctx, frag, position);
610     if (err < 0) {
611         av_buffer_unref(&content_ref);
612         return err;
613     }
614
615     unit = &frag->units[position];
616     unit->type        = type;
617     unit->content     = content;
618     unit->content_ref = content_ref;
619
620     return 0;
621 }
622
623 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
624                             CodedBitstreamFragment *frag,
625                             int position,
626                             CodedBitstreamUnitType type,
627                             uint8_t *data, size_t data_size,
628                             AVBufferRef *data_buf)
629 {
630     CodedBitstreamUnit *unit;
631     AVBufferRef *data_ref;
632     int err;
633
634     if (position == -1)
635         position = frag->nb_units;
636     av_assert0(position >= 0 && position <= frag->nb_units);
637
638     if (data_buf)
639         data_ref = av_buffer_ref(data_buf);
640     else
641         data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
642     if (!data_ref)
643         return AVERROR(ENOMEM);
644
645     err = cbs_insert_unit(ctx, frag, position);
646     if (err < 0) {
647         av_buffer_unref(&data_ref);
648         return err;
649     }
650
651     unit = &frag->units[position];
652     unit->type      = type;
653     unit->data      = data;
654     unit->data_size = data_size;
655     unit->data_ref  = data_ref;
656
657     return 0;
658 }
659
660 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
661                        CodedBitstreamFragment *frag,
662                        int position)
663 {
664     if (position < 0 || position >= frag->nb_units)
665         return AVERROR(EINVAL);
666
667     cbs_unit_uninit(ctx, &frag->units[position]);
668
669     --frag->nb_units;
670
671     if (frag->nb_units > 0)
672         memmove(frag->units + position,
673                 frag->units + position + 1,
674                 (frag->nb_units - position) * sizeof(*frag->units));
675
676     return 0;
677 }