]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs.c
Merge commit '22241208eb7d0168b2afc128af5a128a9ef0a89b'
[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/common.h"
25
26 #include "cbs.h"
27 #include "cbs_internal.h"
28
29
30 static const CodedBitstreamType *cbs_type_table[] = {
31 #if CONFIG_CBS_H264
32     &ff_cbs_type_h264,
33 #endif
34 #if CONFIG_CBS_H265
35     &ff_cbs_type_h265,
36 #endif
37 #if CONFIG_CBS_MPEG2
38     &ff_cbs_type_mpeg2,
39 #endif
40 };
41
42 int ff_cbs_init(CodedBitstreamContext **ctx_ptr,
43                 enum AVCodecID codec_id, void *log_ctx)
44 {
45     CodedBitstreamContext *ctx;
46     const CodedBitstreamType *type;
47     int i;
48
49     type = NULL;
50     for (i = 0; i < FF_ARRAY_ELEMS(cbs_type_table); i++) {
51         if (cbs_type_table[i]->codec_id == codec_id) {
52             type = cbs_type_table[i];
53             break;
54         }
55     }
56     if (!type)
57         return AVERROR(EINVAL);
58
59     ctx = av_mallocz(sizeof(*ctx));
60     if (!ctx)
61         return AVERROR(ENOMEM);
62
63     ctx->log_ctx = log_ctx;
64     ctx->codec   = type;
65
66     ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
67     if (!ctx->priv_data) {
68         av_freep(&ctx);
69         return AVERROR(ENOMEM);
70     }
71
72     ctx->decompose_unit_types = NULL;
73
74     ctx->trace_enable = 0;
75     ctx->trace_level  = AV_LOG_TRACE;
76
77     *ctx_ptr = ctx;
78     return 0;
79 }
80
81 void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
82 {
83     CodedBitstreamContext *ctx = *ctx_ptr;
84
85     if (!ctx)
86         return;
87
88     if (ctx->codec && ctx->codec->close)
89         ctx->codec->close(ctx);
90
91     av_freep(&ctx->priv_data);
92     av_freep(ctx_ptr);
93 }
94
95 static void cbs_unit_uninit(CodedBitstreamContext *ctx,
96                             CodedBitstreamUnit *unit)
97 {
98     if (ctx->codec->free_unit && unit->content && !unit->content_external)
99         ctx->codec->free_unit(unit);
100
101     av_freep(&unit->data);
102     unit->data_size = 0;
103     unit->data_bit_padding = 0;
104 }
105
106 void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
107                             CodedBitstreamFragment *frag)
108 {
109     int i;
110
111     for (i = 0; i < frag->nb_units; i++)
112         cbs_unit_uninit(ctx, &frag->units[i]);
113     av_freep(&frag->units);
114     frag->nb_units = 0;
115
116     av_freep(&frag->data);
117     frag->data_size        = 0;
118     frag->data_bit_padding = 0;
119 }
120
121 static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
122                                      CodedBitstreamFragment *frag)
123 {
124     int err, i, j;
125
126     for (i = 0; i < frag->nb_units; i++) {
127         if (ctx->decompose_unit_types) {
128             for (j = 0; j < ctx->nb_decompose_unit_types; j++) {
129                 if (ctx->decompose_unit_types[j] == frag->units[i].type)
130                     break;
131             }
132             if (j >= ctx->nb_decompose_unit_types)
133                 continue;
134         }
135
136         err = ctx->codec->read_unit(ctx, &frag->units[i]);
137         if (err == AVERROR(ENOSYS)) {
138             av_log(ctx->log_ctx, AV_LOG_WARNING,
139                    "Decomposition unimplemented for unit %d "
140                    "(type %"PRIu32").\n", i, frag->units[i].type);
141         } else if (err < 0) {
142             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to read unit %d "
143                    "(type %"PRIu32").\n", i, frag->units[i].type);
144             return err;
145         }
146     }
147
148     return 0;
149 }
150
151 int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
152                           CodedBitstreamFragment *frag,
153                           const AVCodecParameters *par)
154 {
155     int err;
156
157     memset(frag, 0, sizeof(*frag));
158
159     frag->data      = par->extradata;
160     frag->data_size = par->extradata_size;
161
162     err = ctx->codec->split_fragment(ctx, frag, 1);
163     if (err < 0)
164         return err;
165
166     frag->data      = NULL;
167     frag->data_size = 0;
168
169     return cbs_read_fragment_content(ctx, frag);
170 }
171
172 int ff_cbs_read_packet(CodedBitstreamContext *ctx,
173                        CodedBitstreamFragment *frag,
174                        const AVPacket *pkt)
175 {
176     int err;
177
178     memset(frag, 0, sizeof(*frag));
179
180     frag->data      = pkt->data;
181     frag->data_size = pkt->size;
182
183     err = ctx->codec->split_fragment(ctx, frag, 0);
184     if (err < 0)
185         return err;
186
187     frag->data      = NULL;
188     frag->data_size = 0;
189
190     return cbs_read_fragment_content(ctx, frag);
191 }
192
193 int ff_cbs_read(CodedBitstreamContext *ctx,
194                 CodedBitstreamFragment *frag,
195                 const uint8_t *data, size_t size)
196 {
197     int err;
198
199     memset(frag, 0, sizeof(*frag));
200
201     // (We won't write to this during split.)
202     frag->data      = (uint8_t*)data;
203     frag->data_size = size;
204
205     err = ctx->codec->split_fragment(ctx, frag, 0);
206     if (err < 0)
207         return err;
208
209     frag->data      = NULL;
210     frag->data_size = 0;
211
212     return cbs_read_fragment_content(ctx, frag);
213 }
214
215
216 int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
217                                CodedBitstreamFragment *frag)
218 {
219     int err, i;
220
221     for (i = 0; i < frag->nb_units; i++) {
222         if (!frag->units[i].content)
223             continue;
224
225         err = ctx->codec->write_unit(ctx, &frag->units[i]);
226         if (err < 0) {
227             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
228                    "(type %"PRIu32").\n", i, frag->units[i].type);
229             return err;
230         }
231     }
232
233     err = ctx->codec->assemble_fragment(ctx, frag);
234     if (err < 0) {
235         av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
236         return err;
237     }
238
239     return 0;
240 }
241
242 int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
243                            AVCodecParameters *par,
244                            CodedBitstreamFragment *frag)
245 {
246     int err;
247
248     err = ff_cbs_write_fragment_data(ctx, frag);
249     if (err < 0)
250         return err;
251
252     av_freep(&par->extradata);
253
254     par->extradata = av_malloc(frag->data_size +
255                                AV_INPUT_BUFFER_PADDING_SIZE);
256     if (!par->extradata)
257         return AVERROR(ENOMEM);
258
259     memcpy(par->extradata, frag->data, frag->data_size);
260     memset(par->extradata + frag->data_size, 0,
261            AV_INPUT_BUFFER_PADDING_SIZE);
262     par->extradata_size = frag->data_size;
263
264     return 0;
265 }
266
267 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
268                         AVPacket *pkt,
269                         CodedBitstreamFragment *frag)
270 {
271     int err;
272
273     err = ff_cbs_write_fragment_data(ctx, frag);
274     if (err < 0)
275         return err;
276
277     err = av_new_packet(pkt, frag->data_size);
278     if (err < 0)
279         return err;
280
281     memcpy(pkt->data, frag->data, frag->data_size);
282     pkt->size = frag->data_size;
283
284     return 0;
285 }
286
287
288 void ff_cbs_trace_header(CodedBitstreamContext *ctx,
289                          const char *name)
290 {
291     if (!ctx->trace_enable)
292         return;
293
294     av_log(ctx->log_ctx, ctx->trace_level, "%s\n", name);
295 }
296
297 void ff_cbs_trace_syntax_element(CodedBitstreamContext *ctx, int position,
298                                  const char *name, const char *bits,
299                                  int64_t value)
300 {
301     size_t name_len, bits_len;
302     int pad;
303
304     if (!ctx->trace_enable)
305         return;
306
307     av_assert0(value >= INT_MIN && value <= UINT32_MAX);
308
309     name_len = strlen(name);
310     bits_len = strlen(bits);
311
312     if (name_len + bits_len > 60)
313         pad = bits_len + 2;
314     else
315         pad = 61 - name_len;
316
317     av_log(ctx->log_ctx, ctx->trace_level, "%-10d  %s%*s = %"PRId64"\n",
318            position, name, pad, bits, value);
319 }
320
321 int ff_cbs_read_unsigned(CodedBitstreamContext *ctx, GetBitContext *gbc,
322                          int width, const char *name, uint32_t *write_to,
323                          uint32_t range_min, uint32_t range_max)
324 {
325     uint32_t value;
326     int position;
327
328     av_assert0(width > 0 && width <= 32);
329
330     if (get_bits_left(gbc) < width) {
331         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid value at "
332                "%s: bitstream ended.\n", name);
333         return AVERROR_INVALIDDATA;
334     }
335
336     if (ctx->trace_enable)
337         position = get_bits_count(gbc);
338
339     value = get_bits_long(gbc, width);
340
341     if (ctx->trace_enable) {
342         char bits[33];
343         int i;
344         for (i = 0; i < width; i++)
345             bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
346         bits[i] = 0;
347
348         ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
349     }
350
351     if (value < range_min || value > range_max) {
352         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
353                "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
354                name, value, range_min, range_max);
355         return AVERROR_INVALIDDATA;
356     }
357
358     *write_to = value;
359     return 0;
360 }
361
362 int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
363                           int width, const char *name, uint32_t value,
364                           uint32_t range_min, uint32_t range_max)
365 {
366     av_assert0(width > 0 && width <= 32);
367
368     if (value < range_min || value > range_max) {
369         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
370                "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
371                name, value, range_min, range_max);
372         return AVERROR_INVALIDDATA;
373     }
374
375     if (put_bits_left(pbc) < width)
376         return AVERROR(ENOSPC);
377
378     if (ctx->trace_enable) {
379         char bits[33];
380         int i;
381         for (i = 0; i < width; i++)
382             bits[i] = value >> (width - i - 1) & 1 ? '1' : '0';
383         bits[i] = 0;
384
385         ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
386     }
387
388     if (width < 32)
389         put_bits(pbc, width, value);
390     else
391         put_bits32(pbc, value);
392
393     return 0;
394 }
395
396
397 static int cbs_insert_unit(CodedBitstreamContext *ctx,
398                            CodedBitstreamFragment *frag,
399                            int position)
400 {
401     CodedBitstreamUnit *units;
402
403     units = av_malloc_array(frag->nb_units + 1, sizeof(*units));
404     if (!units)
405         return AVERROR(ENOMEM);
406
407     if (position > 0)
408         memcpy(units, frag->units, position * sizeof(*units));
409     if (position < frag->nb_units)
410         memcpy(units + position + 1, frag->units + position,
411                (frag->nb_units - position) * sizeof(*units));
412
413     memset(units + position, 0, sizeof(*units));
414
415     av_freep(&frag->units);
416     frag->units = units;
417     ++frag->nb_units;
418
419     return 0;
420 }
421
422 int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
423                                CodedBitstreamFragment *frag,
424                                int position,
425                                CodedBitstreamUnitType type,
426                                void *content)
427 {
428     int err;
429
430     if (position == -1)
431         position = frag->nb_units;
432     av_assert0(position >= 0 && position <= frag->nb_units);
433
434     err = cbs_insert_unit(ctx, frag, position);
435     if (err < 0)
436         return err;
437
438     frag->units[position].type             = type;
439     frag->units[position].content          = content;
440     frag->units[position].content_external = 1;
441
442     return 0;
443 }
444
445 int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
446                             CodedBitstreamFragment *frag,
447                             int position,
448                             CodedBitstreamUnitType type,
449                             uint8_t *data, size_t data_size)
450 {
451     int err;
452
453     if (position == -1)
454         position = frag->nb_units;
455     av_assert0(position >= 0 && position <= frag->nb_units);
456
457     err = cbs_insert_unit(ctx, frag, position);
458     if (err < 0)
459         return err;
460
461     frag->units[position].type      = type;
462     frag->units[position].data      = data;
463     frag->units[position].data_size = data_size;
464
465     return 0;
466 }
467
468 int ff_cbs_delete_unit(CodedBitstreamContext *ctx,
469                        CodedBitstreamFragment *frag,
470                        int position)
471 {
472     if (position < 0 || position >= frag->nb_units)
473         return AVERROR(EINVAL);
474
475     cbs_unit_uninit(ctx, &frag->units[position]);
476
477     --frag->nb_units;
478
479     if (frag->nb_units == 0) {
480         av_freep(&frag->units);
481
482     } else {
483         memmove(frag->units + position,
484                 frag->units + position + 1,
485                 (frag->nb_units - position) * sizeof(*frag->units));
486
487         // Don't bother reallocating the unit array.
488     }
489
490     return 0;
491 }