FFmpeg
av1_parse.c
Go to the documentation of this file.
1 /*
2  * AV1 common parsing code
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "config.h"
22 
23 #include "libavutil/mem.h"
24 
25 #include "av1.h"
26 #include "av1_parse.h"
27 #include "bytestream.h"
28 
29 int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
30 {
31  int64_t obu_size;
32  int start_pos, type, temporal_id, spatial_id;
33  int len;
34 
35  len = parse_obu_header(buf, length, &obu_size, &start_pos,
36  &type, &temporal_id, &spatial_id);
37  if (len < 0)
38  return len;
39 
40  obu->type = type;
41  obu->temporal_id = temporal_id;
42  obu->spatial_id = spatial_id;
43 
44  obu->data = buf + start_pos;
45  obu->size = obu_size;
46  obu->raw_data = buf;
47  obu->raw_size = len;
48 
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);
52 
53  return len;
54 }
55 
56 int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, void *logctx)
57 {
58  GetByteContext bc;
59  int consumed;
60 
61  bytestream2_init(&bc, buf, length);
62  pkt->nb_obus = 0;
63 
64  while (bytestream2_get_bytes_left(&bc) > 0) {
65  AV1OBU *obu;
66 
67  if (pkt->obus_allocated < pkt->nb_obus + 1) {
68  int new_size = pkt->obus_allocated + 1;
69  AV1OBU *tmp;
70 
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));
74  if (!tmp)
75  return AVERROR(ENOMEM);
76 
77  pkt->obus = tmp;
78  memset(pkt->obus + pkt->obus_allocated, 0, sizeof(*pkt->obus));
79  pkt->obus_allocated = new_size;
80  }
81  obu = &pkt->obus[pkt->nb_obus];
82 
83  consumed = ff_av1_extract_obu(obu, bc.buffer, bytestream2_get_bytes_left(&bc), logctx);
84  if (consumed < 0)
85  return consumed;
86 
87  bytestream2_skip(&bc, consumed);
88 
89  obu->size_bits = get_obu_bit_length(obu->data, obu->size, obu->type);
90 
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);
95  continue;
96  }
97 
98  pkt->nb_obus++;
99  }
100 
101  return 0;
102 }
103 
105 {
106  av_freep(&pkt->obus);
107  pkt->obus_allocated = pkt->obus_allocated_size = 0;
108 }
109 
110 AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick,
111  int64_t time_scale)
112 {
113  AVRational fr;
114 
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))
119  return fr;
120 
121  return (AVRational){ 0, 1 };
122 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV1OBU::size
int size
Size of payload.
Definition: av1_parse.h:40
GetByteContext
Definition: bytestream.h:33
int64_t
long long int64_t
Definition: coverity.c:34
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AV1OBU
Definition: av1_parse.h:38
AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
AV1OBU::data
const uint8_t * data
Definition: av1_parse.h:41
ff_av1_packet_split
int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, void *logctx)
Split an input packet into OBUs.
Definition: av1_parse.c:56
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
av1_parse.h
AV1Packet
An input packet split into OBUs.
Definition: av1_parse.h:60
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_av1_extract_obu
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
Extract an OBU from a raw bitstream.
Definition: av1_parse.c:29
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
parse_obu_header
static int parse_obu_header(const uint8_t *buf, int buf_size, int64_t *obu_size, int *start_pos, int *type, int *temporal_id, int *spatial_id)
Definition: av1_parse.h:92
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
get_obu_bit_length
static int get_obu_bit_length(const uint8_t *buf, int size, int type)
Definition: av1_parse.h:136
ff_av1_framerate
AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick, int64_t time_scale)
Definition: av1_parse.c:110
av1.h
AV1OBU::raw_size
int raw_size
Size of entire OBU, including header.
Definition: av1_parse.h:50
AV1OBU::size_bits
int size_bits
Size, in bits, of just the data, excluding the trailing_one_bit and any trailing padding.
Definition: av1_parse.h:47
AV1_OBU_PADDING
@ AV1_OBU_PADDING
Definition: av1.h:39
AV1OBU::spatial_id
int spatial_id
Definition: av1_parse.h:56
AV1OBU::raw_data
const uint8_t * raw_data
Definition: av1_parse.h:51
ff_av1_packet_uninit
void ff_av1_packet_uninit(AV1Packet *pkt)
Free all the allocated memory in the packet.
Definition: av1_parse.c:104
len
int len
Definition: vorbis_enc_data.h:426
AV1OBU::temporal_id
int temporal_id
Definition: av1_parse.h:55
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV1OBU::type
int type
Definition: av1_parse.h:53
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27