2 * AV1 common parsing code
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/mem.h"
26 #include "av1_parse.h"
27 #include "bytestream.h"
29 int ff_av1_extract_obu(AV1OBU
*obu
, const uint8_t *buf
, int length
, void *logctx
)
32 int start_pos
, type
, temporal_id
, spatial_id
;
35 len
= parse_obu_header(buf
, length
, &obu_size
, &start_pos
,
36 &type
, &temporal_id
, &spatial_id
);
41 obu
->temporal_id
= temporal_id
;
42 obu
->spatial_id
= spatial_id
;
44 obu
->data
= buf
+ start_pos
;
49 av_log(logctx
, AV_LOG_DEBUG
,
50 "obu_type: %d, temporal_id: %d, spatial_id: %d, payload size: %d\n",
51 obu
->type
, obu
->temporal_id
, obu
->spatial_id
, obu
->size
);
56 int ff_av1_packet_split(AV1Packet
*pkt
, const uint8_t *buf
, int length
, void *logctx
)
61 bytestream2_init(&bc
, buf
, length
);
64 while (bytestream2_get_bytes_left(&bc
) > 0) {
67 if (pkt
->obus_allocated
< pkt
->nb_obus
+ 1) {
68 int new_size
= pkt
->obus_allocated
+ 1;
71 if (new_size
>= INT_MAX
/ sizeof(*tmp
))
72 return AVERROR(ENOMEM
);
73 tmp
= av_fast_realloc(pkt
->obus
, &pkt
->obus_allocated_size
, new_size
* sizeof(*tmp
));
75 return AVERROR(ENOMEM
);
78 memset(pkt
->obus
+ pkt
->obus_allocated
, 0, sizeof(*pkt
->obus
));
79 pkt
->obus_allocated
= new_size
;
81 obu
= &pkt
->obus
[pkt
->nb_obus
];
83 consumed
= ff_av1_extract_obu(obu
, bc
.buffer
, bytestream2_get_bytes_left(&bc
), logctx
);
87 bytestream2_skip(&bc
, consumed
);
89 obu
->size_bits
= get_obu_bit_length(obu
->data
, obu
->size
, obu
->type
);
91 if (obu
->size_bits
< 0 ||
92 (obu
->size_bits
== 0 && (obu
->type
!= AV1_OBU_TEMPORAL_DELIMITER
&&
93 obu
->type
!= AV1_OBU_PADDING
))) {
94 av_log(logctx
, AV_LOG_ERROR
, "Invalid OBU of type %d, skipping.\n", obu
->type
);
104 void ff_av1_packet_uninit(AV1Packet
*pkt
)
106 av_freep(&pkt
->obus
);
107 pkt
->obus_allocated
= pkt
->obus_allocated_size
= 0;
110 AVRational
ff_av1_framerate(int64_t ticks_per_frame
, int64_t units_per_tick
,
115 if (ticks_per_frame
&& units_per_tick
&& time_scale
&&
116 ticks_per_frame
< INT64_MAX
/ units_per_tick
&&
117 av_reduce(&fr
.den
, &fr
.num
, units_per_tick
* ticks_per_frame
,
118 time_scale
, INT_MAX
))
121 return (AVRational
){ 0, 1 };